1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
9 #include "src/code-factory.h"
10 #include "src/code-stubs.h"
11 #include "src/codegen.h"
12 #include "src/compiler.h"
13 #include "src/debug.h"
14 #include "src/full-codegen.h"
15 #include "src/ic/ic.h"
16 #include "src/parser.h"
17 #include "src/scopes.h"
22 #define __ ACCESS_MASM(masm_)
25 class JumpPatchSite BASE_EMBEDDED {
27 explicit JumpPatchSite(MacroAssembler* masm) : masm_(masm) {
29 info_emitted_ = false;
34 DCHECK(patch_site_.is_bound() == info_emitted_);
37 void EmitJumpIfNotSmi(Register reg,
39 Label::Distance distance = Label::kFar) {
40 __ test(reg, Immediate(kSmiTagMask));
41 EmitJump(not_carry, target, distance); // Always taken before patched.
44 void EmitJumpIfSmi(Register reg,
46 Label::Distance distance = Label::kFar) {
47 __ test(reg, Immediate(kSmiTagMask));
48 EmitJump(carry, target, distance); // Never taken before patched.
51 void EmitPatchInfo() {
52 if (patch_site_.is_bound()) {
53 int delta_to_patch_site = masm_->SizeOfCodeGeneratedSince(&patch_site_);
54 DCHECK(is_uint8(delta_to_patch_site));
55 __ test(eax, Immediate(delta_to_patch_site));
60 __ nop(); // Signals no inlined code.
65 // jc will be patched with jz, jnc will become jnz.
66 void EmitJump(Condition cc, Label* target, Label::Distance distance) {
67 DCHECK(!patch_site_.is_bound() && !info_emitted_);
68 DCHECK(cc == carry || cc == not_carry);
69 __ bind(&patch_site_);
70 __ j(cc, target, distance);
73 MacroAssembler* masm_;
81 // Generate code for a JS function. On entry to the function the receiver
82 // and arguments have been pushed on the stack left to right, with the
83 // return address on top of them. The actual argument count matches the
84 // formal parameter count expected by the function.
86 // The live registers are:
87 // o edi: the JS function object being called (i.e. ourselves)
89 // o ebp: our caller's frame pointer
90 // o esp: stack pointer (pointing to return address)
92 // The function builds a JS frame. Please see JavaScriptFrameConstants in
93 // frames-x87.h for its layout.
94 void FullCodeGenerator::Generate() {
95 CompilationInfo* info = info_;
96 profiling_counter_ = isolate()->factory()->NewCell(
97 Handle<Smi>(Smi::FromInt(FLAG_interrupt_budget), isolate()));
98 SetFunctionPosition(function());
99 Comment cmnt(masm_, "[ function compiled by full code generator");
101 ProfileEntryHookStub::MaybeCallEntryHook(masm_);
104 if (strlen(FLAG_stop_at) > 0 &&
105 info->function()->name()->IsUtf8EqualTo(CStrVector(FLAG_stop_at))) {
110 // Sloppy mode functions and builtins need to replace the receiver with the
111 // global proxy when called as functions (without an explicit receiver
113 if (is_sloppy(info->language_mode()) && !info->is_native() &&
114 info->MayUseThis()) {
116 // +1 for return address.
117 int receiver_offset = (info->scope()->num_parameters() + 1) * kPointerSize;
118 __ mov(ecx, Operand(esp, receiver_offset));
120 __ cmp(ecx, isolate()->factory()->undefined_value());
121 __ j(not_equal, &ok, Label::kNear);
123 __ mov(ecx, GlobalObjectOperand());
124 __ mov(ecx, FieldOperand(ecx, GlobalObject::kGlobalProxyOffset));
126 __ mov(Operand(esp, receiver_offset), ecx);
131 // Open a frame scope to indicate that there is a frame on the stack. The
132 // MANUAL indicates that the scope shouldn't actually generate code to set up
133 // the frame (that is done below).
134 FrameScope frame_scope(masm_, StackFrame::MANUAL);
136 info->set_prologue_offset(masm_->pc_offset());
137 __ Prologue(info->IsCodePreAgingActive());
138 info->AddNoFrameRange(0, masm_->pc_offset());
140 { Comment cmnt(masm_, "[ Allocate locals");
141 int locals_count = info->scope()->num_stack_slots();
142 // Generators allocate locals, if any, in context slots.
143 DCHECK(!IsGeneratorFunction(info->function()->kind()) || locals_count == 0);
144 if (locals_count == 1) {
145 __ push(Immediate(isolate()->factory()->undefined_value()));
146 } else if (locals_count > 1) {
147 if (locals_count >= 128) {
150 __ sub(ecx, Immediate(locals_count * kPointerSize));
151 ExternalReference stack_limit =
152 ExternalReference::address_of_real_stack_limit(isolate());
153 __ cmp(ecx, Operand::StaticVariable(stack_limit));
154 __ j(above_equal, &ok, Label::kNear);
155 __ InvokeBuiltin(Builtins::STACK_OVERFLOW, CALL_FUNCTION);
158 __ mov(eax, Immediate(isolate()->factory()->undefined_value()));
159 const int kMaxPushes = 32;
160 if (locals_count >= kMaxPushes) {
161 int loop_iterations = locals_count / kMaxPushes;
162 __ mov(ecx, loop_iterations);
164 __ bind(&loop_header);
166 for (int i = 0; i < kMaxPushes; i++) {
170 __ j(not_zero, &loop_header, Label::kNear);
172 int remaining = locals_count % kMaxPushes;
173 // Emit the remaining pushes.
174 for (int i = 0; i < remaining; i++) {
180 bool function_in_register = true;
182 // Possibly allocate a local context.
183 if (info->scope()->num_heap_slots() > 0) {
184 Comment cmnt(masm_, "[ Allocate context");
185 bool need_write_barrier = true;
186 int slots = info->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
187 // Argument to NewContext is the function, which is still in edi.
188 if (info->scope()->is_script_scope()) {
190 __ Push(info->scope()->GetScopeInfo(info->isolate()));
191 __ CallRuntime(Runtime::kNewScriptContext, 2);
192 } else if (slots <= FastNewContextStub::kMaximumSlots) {
193 FastNewContextStub stub(isolate(), slots);
195 // Result of FastNewContextStub is always in new space.
196 need_write_barrier = false;
199 __ CallRuntime(Runtime::kNewFunctionContext, 1);
201 function_in_register = false;
202 // Context is returned in eax. It replaces the context passed to us.
203 // It's saved in the stack and kept live in esi.
205 __ mov(Operand(ebp, StandardFrameConstants::kContextOffset), eax);
207 // Copy parameters into context if necessary.
208 int num_parameters = info->scope()->num_parameters();
209 int first_parameter = info->scope()->has_this_declaration() ? -1 : 0;
210 for (int i = first_parameter; i < num_parameters; i++) {
211 Variable* var = (i == -1) ? scope()->receiver() : scope()->parameter(i);
212 if (var->IsContextSlot()) {
213 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
214 (num_parameters - 1 - i) * kPointerSize;
215 // Load parameter from stack.
216 __ mov(eax, Operand(ebp, parameter_offset));
217 // Store it in the context.
218 int context_offset = Context::SlotOffset(var->index());
219 __ mov(Operand(esi, context_offset), eax);
220 // Update the write barrier. This clobbers eax and ebx.
221 if (need_write_barrier) {
222 __ RecordWriteContextSlot(esi, context_offset, eax, ebx,
224 } else if (FLAG_debug_code) {
226 __ JumpIfInNewSpace(esi, eax, &done, Label::kNear);
227 __ Abort(kExpectedNewSpaceObject);
234 // Possibly set up a local binding to the this function which is used in
235 // derived constructors with super calls.
236 Variable* this_function_var = scope()->this_function_var();
237 if (this_function_var != nullptr) {
238 Comment cmnt(masm_, "[ This function");
239 if (!function_in_register) {
240 __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
241 // The write barrier clobbers register again, keep is marked as such.
243 SetVar(this_function_var, edi, ebx, edx);
246 Variable* new_target_var = scope()->new_target_var();
247 if (new_target_var != nullptr) {
248 Comment cmnt(masm_, "[ new.target");
249 __ mov(eax, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
250 Label non_adaptor_frame;
251 __ cmp(Operand(eax, StandardFrameConstants::kContextOffset),
252 Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
253 __ j(not_equal, &non_adaptor_frame);
254 __ mov(eax, Operand(eax, StandardFrameConstants::kCallerFPOffset));
256 __ bind(&non_adaptor_frame);
257 __ cmp(Operand(eax, StandardFrameConstants::kMarkerOffset),
258 Immediate(Smi::FromInt(StackFrame::CONSTRUCT)));
260 Label non_construct_frame, done;
261 __ j(not_equal, &non_construct_frame);
265 Operand(eax, ConstructFrameConstants::kOriginalConstructorOffset));
268 // Non-construct frame
269 __ bind(&non_construct_frame);
270 __ mov(eax, Immediate(isolate()->factory()->undefined_value()));
273 SetVar(new_target_var, eax, ebx, edx);
277 // Possibly allocate RestParameters
279 Variable* rest_param = scope()->rest_parameter(&rest_index);
281 Comment cmnt(masm_, "[ Allocate rest parameter array");
283 int num_parameters = info->scope()->num_parameters();
284 int offset = num_parameters * kPointerSize;
287 Operand(ebp, StandardFrameConstants::kCallerSPOffset + offset));
289 __ push(Immediate(Smi::FromInt(num_parameters)));
290 __ push(Immediate(Smi::FromInt(rest_index)));
291 __ push(Immediate(Smi::FromInt(language_mode())));
293 RestParamAccessStub stub(isolate());
296 SetVar(rest_param, eax, ebx, edx);
299 Variable* arguments = scope()->arguments();
300 if (arguments != NULL) {
301 // Function uses arguments object.
302 Comment cmnt(masm_, "[ Allocate arguments object");
303 if (function_in_register) {
306 __ push(Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
308 // Receiver is just before the parameters on the caller's stack.
309 int num_parameters = info->scope()->num_parameters();
310 int offset = num_parameters * kPointerSize;
312 Operand(ebp, StandardFrameConstants::kCallerSPOffset + offset));
314 __ push(Immediate(Smi::FromInt(num_parameters)));
315 // Arguments to ArgumentsAccessStub:
316 // function, receiver address, parameter count.
317 // The stub will rewrite receiver and parameter count if the previous
318 // stack frame was an arguments adapter frame.
319 ArgumentsAccessStub::Type type;
320 if (is_strict(language_mode()) || !is_simple_parameter_list()) {
321 type = ArgumentsAccessStub::NEW_STRICT;
322 } else if (function()->has_duplicate_parameters()) {
323 type = ArgumentsAccessStub::NEW_SLOPPY_SLOW;
325 type = ArgumentsAccessStub::NEW_SLOPPY_FAST;
328 ArgumentsAccessStub stub(isolate(), type);
331 SetVar(arguments, eax, ebx, edx);
335 __ CallRuntime(Runtime::kTraceEnter, 0);
338 // Visit the declarations and body unless there is an illegal
340 if (scope()->HasIllegalRedeclaration()) {
341 Comment cmnt(masm_, "[ Declarations");
342 scope()->VisitIllegalRedeclaration(this);
345 PrepareForBailoutForId(BailoutId::FunctionEntry(), NO_REGISTERS);
346 { Comment cmnt(masm_, "[ Declarations");
347 // For named function expressions, declare the function name as a
349 if (scope()->is_function_scope() && scope()->function() != NULL) {
350 VariableDeclaration* function = scope()->function();
351 DCHECK(function->proxy()->var()->mode() == CONST ||
352 function->proxy()->var()->mode() == CONST_LEGACY);
353 DCHECK(!function->proxy()->var()->IsUnallocatedOrGlobalSlot());
354 VisitVariableDeclaration(function);
356 VisitDeclarations(scope()->declarations());
359 { Comment cmnt(masm_, "[ Stack check");
360 PrepareForBailoutForId(BailoutId::Declarations(), NO_REGISTERS);
362 ExternalReference stack_limit
363 = ExternalReference::address_of_stack_limit(isolate());
364 __ cmp(esp, Operand::StaticVariable(stack_limit));
365 __ j(above_equal, &ok, Label::kNear);
366 __ call(isolate()->builtins()->StackCheck(), RelocInfo::CODE_TARGET);
370 { Comment cmnt(masm_, "[ Body");
371 DCHECK(loop_depth() == 0);
372 VisitStatements(function()->body());
373 DCHECK(loop_depth() == 0);
377 // Always emit a 'return undefined' in case control fell off the end of
379 { Comment cmnt(masm_, "[ return <undefined>;");
380 __ mov(eax, isolate()->factory()->undefined_value());
381 EmitReturnSequence();
386 void FullCodeGenerator::ClearAccumulator() {
387 __ Move(eax, Immediate(Smi::FromInt(0)));
391 void FullCodeGenerator::EmitProfilingCounterDecrement(int delta) {
392 __ mov(ebx, Immediate(profiling_counter_));
393 __ sub(FieldOperand(ebx, Cell::kValueOffset),
394 Immediate(Smi::FromInt(delta)));
398 void FullCodeGenerator::EmitProfilingCounterReset() {
399 int reset_value = FLAG_interrupt_budget;
400 __ mov(ebx, Immediate(profiling_counter_));
401 __ mov(FieldOperand(ebx, Cell::kValueOffset),
402 Immediate(Smi::FromInt(reset_value)));
406 void FullCodeGenerator::EmitBackEdgeBookkeeping(IterationStatement* stmt,
407 Label* back_edge_target) {
408 Comment cmnt(masm_, "[ Back edge bookkeeping");
411 DCHECK(back_edge_target->is_bound());
412 int distance = masm_->SizeOfCodeGeneratedSince(back_edge_target);
413 int weight = Min(kMaxBackEdgeWeight,
414 Max(1, distance / kCodeSizeMultiplier));
415 EmitProfilingCounterDecrement(weight);
416 __ j(positive, &ok, Label::kNear);
417 __ call(isolate()->builtins()->InterruptCheck(), RelocInfo::CODE_TARGET);
419 // Record a mapping of this PC offset to the OSR id. This is used to find
420 // the AST id from the unoptimized code in order to use it as a key into
421 // the deoptimization input data found in the optimized code.
422 RecordBackEdge(stmt->OsrEntryId());
424 EmitProfilingCounterReset();
427 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
428 // Record a mapping of the OSR id to this PC. This is used if the OSR
429 // entry becomes the target of a bailout. We don't expect it to be, but
430 // we want it to work if it is.
431 PrepareForBailoutForId(stmt->OsrEntryId(), NO_REGISTERS);
435 void FullCodeGenerator::EmitReturnSequence() {
436 Comment cmnt(masm_, "[ Return sequence");
437 if (return_label_.is_bound()) {
438 __ jmp(&return_label_);
440 // Common return label
441 __ bind(&return_label_);
444 __ CallRuntime(Runtime::kTraceExit, 1);
446 // Pretend that the exit is a backwards jump to the entry.
448 if (info_->ShouldSelfOptimize()) {
449 weight = FLAG_interrupt_budget / FLAG_self_opt_count;
451 int distance = masm_->pc_offset();
452 weight = Min(kMaxBackEdgeWeight,
453 Max(1, distance / kCodeSizeMultiplier));
455 EmitProfilingCounterDecrement(weight);
457 __ j(positive, &ok, Label::kNear);
459 __ call(isolate()->builtins()->InterruptCheck(),
460 RelocInfo::CODE_TARGET);
462 EmitProfilingCounterReset();
465 SetReturnPosition(function());
466 int no_frame_start = masm_->pc_offset();
469 int arg_count = info_->scope()->num_parameters() + 1;
470 int arguments_bytes = arg_count * kPointerSize;
471 __ Ret(arguments_bytes, ecx);
472 info_->AddNoFrameRange(no_frame_start, masm_->pc_offset());
477 void FullCodeGenerator::EffectContext::Plug(Variable* var) const {
478 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
482 void FullCodeGenerator::AccumulatorValueContext::Plug(Variable* var) const {
483 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
484 codegen()->GetVar(result_register(), var);
488 void FullCodeGenerator::StackValueContext::Plug(Variable* var) const {
489 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
490 MemOperand operand = codegen()->VarOperand(var, result_register());
491 // Memory operands can be pushed directly.
496 void FullCodeGenerator::TestContext::Plug(Variable* var) const {
497 // For simplicity we always test the accumulator register.
498 codegen()->GetVar(result_register(), var);
499 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
500 codegen()->DoTest(this);
504 void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
505 UNREACHABLE(); // Not used on X87.
509 void FullCodeGenerator::AccumulatorValueContext::Plug(
510 Heap::RootListIndex index) const {
511 UNREACHABLE(); // Not used on X87.
515 void FullCodeGenerator::StackValueContext::Plug(
516 Heap::RootListIndex index) const {
517 UNREACHABLE(); // Not used on X87.
521 void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
522 UNREACHABLE(); // Not used on X87.
526 void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
530 void FullCodeGenerator::AccumulatorValueContext::Plug(
531 Handle<Object> lit) const {
533 __ SafeMove(result_register(), Immediate(lit));
535 __ Move(result_register(), Immediate(lit));
540 void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
542 __ SafePush(Immediate(lit));
544 __ push(Immediate(lit));
549 void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
550 codegen()->PrepareForBailoutBeforeSplit(condition(),
554 DCHECK(!lit->IsUndetectableObject()); // There are no undetectable literals.
555 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
556 if (false_label_ != fall_through_) __ jmp(false_label_);
557 } else if (lit->IsTrue() || lit->IsJSObject()) {
558 if (true_label_ != fall_through_) __ jmp(true_label_);
559 } else if (lit->IsString()) {
560 if (String::cast(*lit)->length() == 0) {
561 if (false_label_ != fall_through_) __ jmp(false_label_);
563 if (true_label_ != fall_through_) __ jmp(true_label_);
565 } else if (lit->IsSmi()) {
566 if (Smi::cast(*lit)->value() == 0) {
567 if (false_label_ != fall_through_) __ jmp(false_label_);
569 if (true_label_ != fall_through_) __ jmp(true_label_);
572 // For simplicity we always test the accumulator register.
573 __ mov(result_register(), lit);
574 codegen()->DoTest(this);
579 void FullCodeGenerator::EffectContext::DropAndPlug(int count,
580 Register reg) const {
586 void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
588 Register reg) const {
591 __ Move(result_register(), reg);
595 void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
596 Register reg) const {
598 if (count > 1) __ Drop(count - 1);
599 __ mov(Operand(esp, 0), reg);
603 void FullCodeGenerator::TestContext::DropAndPlug(int count,
604 Register reg) const {
606 // For simplicity we always test the accumulator register.
608 __ Move(result_register(), reg);
609 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
610 codegen()->DoTest(this);
614 void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
615 Label* materialize_false) const {
616 DCHECK(materialize_true == materialize_false);
617 __ bind(materialize_true);
621 void FullCodeGenerator::AccumulatorValueContext::Plug(
622 Label* materialize_true,
623 Label* materialize_false) const {
625 __ bind(materialize_true);
626 __ mov(result_register(), isolate()->factory()->true_value());
627 __ jmp(&done, Label::kNear);
628 __ bind(materialize_false);
629 __ mov(result_register(), isolate()->factory()->false_value());
634 void FullCodeGenerator::StackValueContext::Plug(
635 Label* materialize_true,
636 Label* materialize_false) const {
638 __ bind(materialize_true);
639 __ push(Immediate(isolate()->factory()->true_value()));
640 __ jmp(&done, Label::kNear);
641 __ bind(materialize_false);
642 __ push(Immediate(isolate()->factory()->false_value()));
647 void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
648 Label* materialize_false) const {
649 DCHECK(materialize_true == true_label_);
650 DCHECK(materialize_false == false_label_);
654 void FullCodeGenerator::EffectContext::Plug(bool flag) const {
658 void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
659 Handle<Object> value = flag
660 ? isolate()->factory()->true_value()
661 : isolate()->factory()->false_value();
662 __ mov(result_register(), value);
666 void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
667 Handle<Object> value = flag
668 ? isolate()->factory()->true_value()
669 : isolate()->factory()->false_value();
670 __ push(Immediate(value));
674 void FullCodeGenerator::TestContext::Plug(bool flag) const {
675 codegen()->PrepareForBailoutBeforeSplit(condition(),
680 if (true_label_ != fall_through_) __ jmp(true_label_);
682 if (false_label_ != fall_through_) __ jmp(false_label_);
687 void FullCodeGenerator::DoTest(Expression* condition,
690 Label* fall_through) {
691 Handle<Code> ic = ToBooleanStub::GetUninitialized(isolate());
692 CallIC(ic, condition->test_id());
693 __ test(result_register(), result_register());
694 // The stub returns nonzero for true.
695 Split(not_zero, if_true, if_false, fall_through);
699 void FullCodeGenerator::Split(Condition cc,
702 Label* fall_through) {
703 if (if_false == fall_through) {
705 } else if (if_true == fall_through) {
706 __ j(NegateCondition(cc), if_false);
714 MemOperand FullCodeGenerator::StackOperand(Variable* var) {
715 DCHECK(var->IsStackAllocated());
716 // Offset is negative because higher indexes are at lower addresses.
717 int offset = -var->index() * kPointerSize;
718 // Adjust by a (parameter or local) base offset.
719 if (var->IsParameter()) {
720 offset += (info_->scope()->num_parameters() + 1) * kPointerSize;
722 offset += JavaScriptFrameConstants::kLocal0Offset;
724 return Operand(ebp, offset);
728 MemOperand FullCodeGenerator::VarOperand(Variable* var, Register scratch) {
729 DCHECK(var->IsContextSlot() || var->IsStackAllocated());
730 if (var->IsContextSlot()) {
731 int context_chain_length = scope()->ContextChainLength(var->scope());
732 __ LoadContext(scratch, context_chain_length);
733 return ContextOperand(scratch, var->index());
735 return StackOperand(var);
740 void FullCodeGenerator::GetVar(Register dest, Variable* var) {
741 DCHECK(var->IsContextSlot() || var->IsStackAllocated());
742 MemOperand location = VarOperand(var, dest);
743 __ mov(dest, location);
747 void FullCodeGenerator::SetVar(Variable* var,
751 DCHECK(var->IsContextSlot() || var->IsStackAllocated());
752 DCHECK(!scratch0.is(src));
753 DCHECK(!scratch0.is(scratch1));
754 DCHECK(!scratch1.is(src));
755 MemOperand location = VarOperand(var, scratch0);
756 __ mov(location, src);
758 // Emit the write barrier code if the location is in the heap.
759 if (var->IsContextSlot()) {
760 int offset = Context::SlotOffset(var->index());
761 DCHECK(!scratch0.is(esi) && !src.is(esi) && !scratch1.is(esi));
762 __ RecordWriteContextSlot(scratch0, offset, src, scratch1, kDontSaveFPRegs);
767 void FullCodeGenerator::PrepareForBailoutBeforeSplit(Expression* expr,
768 bool should_normalize,
771 // Only prepare for bailouts before splits if we're in a test
772 // context. Otherwise, we let the Visit function deal with the
773 // preparation to avoid preparing with the same AST id twice.
774 if (!context()->IsTest() || !info_->IsOptimizable()) return;
777 if (should_normalize) __ jmp(&skip, Label::kNear);
778 PrepareForBailout(expr, TOS_REG);
779 if (should_normalize) {
780 __ cmp(eax, isolate()->factory()->true_value());
781 Split(equal, if_true, if_false, NULL);
787 void FullCodeGenerator::EmitDebugCheckDeclarationContext(Variable* variable) {
788 // The variable in the declaration always resides in the current context.
789 DCHECK_EQ(0, scope()->ContextChainLength(variable->scope()));
790 if (generate_debug_code_) {
791 // Check that we're not inside a with or catch context.
792 __ mov(ebx, FieldOperand(esi, HeapObject::kMapOffset));
793 __ cmp(ebx, isolate()->factory()->with_context_map());
794 __ Check(not_equal, kDeclarationInWithContext);
795 __ cmp(ebx, isolate()->factory()->catch_context_map());
796 __ Check(not_equal, kDeclarationInCatchContext);
801 void FullCodeGenerator::VisitVariableDeclaration(
802 VariableDeclaration* declaration) {
803 // If it was not possible to allocate the variable at compile time, we
804 // need to "declare" it at runtime to make sure it actually exists in the
806 VariableProxy* proxy = declaration->proxy();
807 VariableMode mode = declaration->mode();
808 Variable* variable = proxy->var();
809 bool hole_init = mode == LET || mode == CONST || mode == CONST_LEGACY;
810 switch (variable->location()) {
811 case VariableLocation::GLOBAL:
812 case VariableLocation::UNALLOCATED:
813 globals_->Add(variable->name(), zone());
814 globals_->Add(variable->binding_needs_init()
815 ? isolate()->factory()->the_hole_value()
816 : isolate()->factory()->undefined_value(), zone());
819 case VariableLocation::PARAMETER:
820 case VariableLocation::LOCAL:
822 Comment cmnt(masm_, "[ VariableDeclaration");
823 __ mov(StackOperand(variable),
824 Immediate(isolate()->factory()->the_hole_value()));
828 case VariableLocation::CONTEXT:
830 Comment cmnt(masm_, "[ VariableDeclaration");
831 EmitDebugCheckDeclarationContext(variable);
832 __ mov(ContextOperand(esi, variable->index()),
833 Immediate(isolate()->factory()->the_hole_value()));
834 // No write barrier since the hole value is in old space.
835 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
839 case VariableLocation::LOOKUP: {
840 Comment cmnt(masm_, "[ VariableDeclaration");
842 __ push(Immediate(variable->name()));
843 // VariableDeclaration nodes are always introduced in one of four modes.
844 DCHECK(IsDeclaredVariableMode(mode));
845 PropertyAttributes attr =
846 IsImmutableVariableMode(mode) ? READ_ONLY : NONE;
847 __ push(Immediate(Smi::FromInt(attr)));
848 // Push initial value, if any.
849 // Note: For variables we must not push an initial value (such as
850 // 'undefined') because we may have a (legal) redeclaration and we
851 // must not destroy the current value.
853 __ push(Immediate(isolate()->factory()->the_hole_value()));
855 __ push(Immediate(Smi::FromInt(0))); // Indicates no initial value.
857 __ CallRuntime(Runtime::kDeclareLookupSlot, 4);
864 void FullCodeGenerator::VisitFunctionDeclaration(
865 FunctionDeclaration* declaration) {
866 VariableProxy* proxy = declaration->proxy();
867 Variable* variable = proxy->var();
868 switch (variable->location()) {
869 case VariableLocation::GLOBAL:
870 case VariableLocation::UNALLOCATED: {
871 globals_->Add(variable->name(), zone());
872 Handle<SharedFunctionInfo> function =
873 Compiler::GetSharedFunctionInfo(declaration->fun(), script(), info_);
874 // Check for stack-overflow exception.
875 if (function.is_null()) return SetStackOverflow();
876 globals_->Add(function, zone());
880 case VariableLocation::PARAMETER:
881 case VariableLocation::LOCAL: {
882 Comment cmnt(masm_, "[ FunctionDeclaration");
883 VisitForAccumulatorValue(declaration->fun());
884 __ mov(StackOperand(variable), result_register());
888 case VariableLocation::CONTEXT: {
889 Comment cmnt(masm_, "[ FunctionDeclaration");
890 EmitDebugCheckDeclarationContext(variable);
891 VisitForAccumulatorValue(declaration->fun());
892 __ mov(ContextOperand(esi, variable->index()), result_register());
893 // We know that we have written a function, which is not a smi.
894 __ RecordWriteContextSlot(esi, Context::SlotOffset(variable->index()),
895 result_register(), ecx, kDontSaveFPRegs,
896 EMIT_REMEMBERED_SET, OMIT_SMI_CHECK);
897 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
901 case VariableLocation::LOOKUP: {
902 Comment cmnt(masm_, "[ FunctionDeclaration");
904 __ push(Immediate(variable->name()));
905 __ push(Immediate(Smi::FromInt(NONE)));
906 VisitForStackValue(declaration->fun());
907 __ CallRuntime(Runtime::kDeclareLookupSlot, 4);
914 void FullCodeGenerator::VisitImportDeclaration(ImportDeclaration* declaration) {
915 VariableProxy* proxy = declaration->proxy();
916 Variable* variable = proxy->var();
917 switch (variable->location()) {
918 case VariableLocation::GLOBAL:
919 case VariableLocation::UNALLOCATED:
923 case VariableLocation::CONTEXT: {
924 Comment cmnt(masm_, "[ ImportDeclaration");
925 EmitDebugCheckDeclarationContext(variable);
930 case VariableLocation::PARAMETER:
931 case VariableLocation::LOCAL:
932 case VariableLocation::LOOKUP:
938 void FullCodeGenerator::VisitExportDeclaration(ExportDeclaration* declaration) {
943 void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
944 // Call the runtime to declare the globals.
945 __ push(esi); // The context is the first argument.
947 __ Push(Smi::FromInt(DeclareGlobalsFlags()));
948 __ CallRuntime(Runtime::kDeclareGlobals, 3);
949 // Return value is ignored.
953 void FullCodeGenerator::DeclareModules(Handle<FixedArray> descriptions) {
954 // Call the runtime to declare the modules.
955 __ Push(descriptions);
956 __ CallRuntime(Runtime::kDeclareModules, 1);
957 // Return value is ignored.
961 void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
962 Comment cmnt(masm_, "[ SwitchStatement");
963 Breakable nested_statement(this, stmt);
964 SetStatementPosition(stmt);
966 // Keep the switch value on the stack until a case matches.
967 VisitForStackValue(stmt->tag());
968 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
970 ZoneList<CaseClause*>* clauses = stmt->cases();
971 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
973 Label next_test; // Recycled for each test.
974 // Compile all the tests with branches to their bodies.
975 for (int i = 0; i < clauses->length(); i++) {
976 CaseClause* clause = clauses->at(i);
977 clause->body_target()->Unuse();
979 // The default is not a test, but remember it as final fall through.
980 if (clause->is_default()) {
981 default_clause = clause;
985 Comment cmnt(masm_, "[ Case comparison");
989 // Compile the label expression.
990 VisitForAccumulatorValue(clause->label());
992 // Perform the comparison as if via '==='.
993 __ mov(edx, Operand(esp, 0)); // Switch value.
994 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
995 JumpPatchSite patch_site(masm_);
996 if (inline_smi_code) {
1000 patch_site.EmitJumpIfNotSmi(ecx, &slow_case, Label::kNear);
1003 __ j(not_equal, &next_test);
1004 __ Drop(1); // Switch value is no longer needed.
1005 __ jmp(clause->body_target());
1006 __ bind(&slow_case);
1009 SetExpressionPosition(clause);
1010 Handle<Code> ic = CodeFactory::CompareIC(isolate(), Token::EQ_STRICT,
1011 strength(language_mode())).code();
1012 CallIC(ic, clause->CompareId());
1013 patch_site.EmitPatchInfo();
1016 __ jmp(&skip, Label::kNear);
1017 PrepareForBailout(clause, TOS_REG);
1018 __ cmp(eax, isolate()->factory()->true_value());
1019 __ j(not_equal, &next_test);
1021 __ jmp(clause->body_target());
1025 __ j(not_equal, &next_test);
1026 __ Drop(1); // Switch value is no longer needed.
1027 __ jmp(clause->body_target());
1030 // Discard the test value and jump to the default if present, otherwise to
1031 // the end of the statement.
1032 __ bind(&next_test);
1033 __ Drop(1); // Switch value is no longer needed.
1034 if (default_clause == NULL) {
1035 __ jmp(nested_statement.break_label());
1037 __ jmp(default_clause->body_target());
1040 // Compile all the case bodies.
1041 for (int i = 0; i < clauses->length(); i++) {
1042 Comment cmnt(masm_, "[ Case body");
1043 CaseClause* clause = clauses->at(i);
1044 __ bind(clause->body_target());
1045 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
1046 VisitStatements(clause->statements());
1049 __ bind(nested_statement.break_label());
1050 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
1054 void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
1055 Comment cmnt(masm_, "[ ForInStatement");
1056 SetStatementPosition(stmt, SKIP_BREAK);
1058 FeedbackVectorSlot slot = stmt->ForInFeedbackSlot();
1061 ForIn loop_statement(this, stmt);
1062 increment_loop_depth();
1064 // Get the object to enumerate over. If the object is null or undefined, skip
1065 // over the loop. See ECMA-262 version 5, section 12.6.4.
1066 SetExpressionAsStatementPosition(stmt->enumerable());
1067 VisitForAccumulatorValue(stmt->enumerable());
1068 __ cmp(eax, isolate()->factory()->undefined_value());
1070 __ cmp(eax, isolate()->factory()->null_value());
1073 PrepareForBailoutForId(stmt->PrepareId(), TOS_REG);
1075 // Convert the object to a JS object.
1076 Label convert, done_convert;
1077 __ JumpIfSmi(eax, &convert, Label::kNear);
1078 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
1079 __ j(above_equal, &done_convert, Label::kNear);
1082 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1083 __ bind(&done_convert);
1084 PrepareForBailoutForId(stmt->ToObjectId(), TOS_REG);
1087 // Check for proxies.
1088 Label call_runtime, use_cache, fixed_array;
1089 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1090 __ CmpObjectType(eax, LAST_JS_PROXY_TYPE, ecx);
1091 __ j(below_equal, &call_runtime);
1093 // Check cache validity in generated code. This is a fast case for
1094 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
1095 // guarantee cache validity, call the runtime system to check cache
1096 // validity or get the property names in a fixed array.
1097 __ CheckEnumCache(&call_runtime);
1099 __ mov(eax, FieldOperand(eax, HeapObject::kMapOffset));
1100 __ jmp(&use_cache, Label::kNear);
1102 // Get the set of properties to enumerate.
1103 __ bind(&call_runtime);
1105 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
1106 PrepareForBailoutForId(stmt->EnumId(), TOS_REG);
1107 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
1108 isolate()->factory()->meta_map());
1109 __ j(not_equal, &fixed_array);
1112 // We got a map in register eax. Get the enumeration cache from it.
1113 Label no_descriptors;
1114 __ bind(&use_cache);
1116 __ EnumLength(edx, eax);
1117 __ cmp(edx, Immediate(Smi::FromInt(0)));
1118 __ j(equal, &no_descriptors);
1120 __ LoadInstanceDescriptors(eax, ecx);
1121 __ mov(ecx, FieldOperand(ecx, DescriptorArray::kEnumCacheOffset));
1122 __ mov(ecx, FieldOperand(ecx, DescriptorArray::kEnumCacheBridgeCacheOffset));
1124 // Set up the four remaining stack slots.
1125 __ push(eax); // Map.
1126 __ push(ecx); // Enumeration cache.
1127 __ push(edx); // Number of valid entries for the map in the enum cache.
1128 __ push(Immediate(Smi::FromInt(0))); // Initial index.
1131 __ bind(&no_descriptors);
1132 __ add(esp, Immediate(kPointerSize));
1135 // We got a fixed array in register eax. Iterate through that.
1137 __ bind(&fixed_array);
1139 // No need for a write barrier, we are storing a Smi in the feedback vector.
1140 __ LoadHeapObject(ebx, FeedbackVector());
1141 int vector_index = FeedbackVector()->GetIndex(slot);
1142 __ mov(FieldOperand(ebx, FixedArray::OffsetOfElementAt(vector_index)),
1143 Immediate(TypeFeedbackVector::MegamorphicSentinel(isolate())));
1145 __ mov(ebx, Immediate(Smi::FromInt(1))); // Smi indicates slow check
1146 __ mov(ecx, Operand(esp, 0 * kPointerSize)); // Get enumerated object
1147 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1148 __ CmpObjectType(ecx, LAST_JS_PROXY_TYPE, ecx);
1149 __ j(above, &non_proxy);
1150 __ Move(ebx, Immediate(Smi::FromInt(0))); // Zero indicates proxy
1151 __ bind(&non_proxy);
1152 __ push(ebx); // Smi
1153 __ push(eax); // Array
1154 __ mov(eax, FieldOperand(eax, FixedArray::kLengthOffset));
1155 __ push(eax); // Fixed array length (as smi).
1156 __ push(Immediate(Smi::FromInt(0))); // Initial index.
1158 // Generate code for doing the condition check.
1159 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
1161 SetExpressionAsStatementPosition(stmt->each());
1163 __ mov(eax, Operand(esp, 0 * kPointerSize)); // Get the current index.
1164 __ cmp(eax, Operand(esp, 1 * kPointerSize)); // Compare to the array length.
1165 __ j(above_equal, loop_statement.break_label());
1167 // Get the current entry of the array into register ebx.
1168 __ mov(ebx, Operand(esp, 2 * kPointerSize));
1169 __ mov(ebx, FieldOperand(ebx, eax, times_2, FixedArray::kHeaderSize));
1171 // Get the expected map from the stack or a smi in the
1172 // permanent slow case into register edx.
1173 __ mov(edx, Operand(esp, 3 * kPointerSize));
1175 // Check if the expected map still matches that of the enumerable.
1176 // If not, we may have to filter the key.
1178 __ mov(ecx, Operand(esp, 4 * kPointerSize));
1179 __ cmp(edx, FieldOperand(ecx, HeapObject::kMapOffset));
1180 __ j(equal, &update_each, Label::kNear);
1182 // For proxies, no filtering is done.
1183 // TODO(rossberg): What if only a prototype is a proxy? Not specified yet.
1184 DCHECK(Smi::FromInt(0) == 0);
1186 __ j(zero, &update_each);
1188 // Convert the entry to a string or null if it isn't a property
1189 // anymore. If the property has been removed while iterating, we
1191 __ push(ecx); // Enumerable.
1192 __ push(ebx); // Current entry.
1193 __ CallRuntime(Runtime::kForInFilter, 2);
1194 PrepareForBailoutForId(stmt->FilterId(), TOS_REG);
1195 __ cmp(eax, isolate()->factory()->undefined_value());
1196 __ j(equal, loop_statement.continue_label());
1199 // Update the 'each' property or variable from the possibly filtered
1200 // entry in register ebx.
1201 __ bind(&update_each);
1202 __ mov(result_register(), ebx);
1203 // Perform the assignment as if via '='.
1204 { EffectContext context(this);
1205 EmitAssignment(stmt->each(), stmt->EachFeedbackSlot());
1206 PrepareForBailoutForId(stmt->AssignmentId(), NO_REGISTERS);
1209 // Generate code for the body of the loop.
1210 Visit(stmt->body());
1212 // Generate code for going to the next element by incrementing the
1213 // index (smi) stored on top of the stack.
1214 __ bind(loop_statement.continue_label());
1215 __ add(Operand(esp, 0 * kPointerSize), Immediate(Smi::FromInt(1)));
1217 EmitBackEdgeBookkeeping(stmt, &loop);
1220 // Remove the pointers stored on the stack.
1221 __ bind(loop_statement.break_label());
1222 __ add(esp, Immediate(5 * kPointerSize));
1224 // Exit and decrement the loop depth.
1225 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
1227 decrement_loop_depth();
1231 void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1233 // Use the fast case closure allocation code that allocates in new
1234 // space for nested functions that don't need literals cloning. If
1235 // we're running with the --always-opt or the --prepare-always-opt
1236 // flag, we need to use the runtime function so that the new function
1237 // we are creating here gets a chance to have its code optimized and
1238 // doesn't just get a copy of the existing unoptimized code.
1239 if (!FLAG_always_opt &&
1240 !FLAG_prepare_always_opt &&
1242 scope()->is_function_scope() &&
1243 info->num_literals() == 0) {
1244 FastNewClosureStub stub(isolate(), info->language_mode(), info->kind());
1245 __ mov(ebx, Immediate(info));
1249 __ push(Immediate(info));
1250 __ push(Immediate(pretenure
1251 ? isolate()->factory()->true_value()
1252 : isolate()->factory()->false_value()));
1253 __ CallRuntime(Runtime::kNewClosure, 3);
1255 context()->Plug(eax);
1259 void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
1260 Comment cmnt(masm_, "[ VariableProxy");
1261 EmitVariableLoad(expr);
1265 void FullCodeGenerator::EmitSetHomeObjectIfNeeded(Expression* initializer,
1267 FeedbackVectorICSlot slot) {
1268 if (NeedsHomeObject(initializer)) {
1269 __ mov(StoreDescriptor::ReceiverRegister(), Operand(esp, 0));
1270 __ mov(StoreDescriptor::NameRegister(),
1271 Immediate(isolate()->factory()->home_object_symbol()));
1272 __ mov(StoreDescriptor::ValueRegister(),
1273 Operand(esp, offset * kPointerSize));
1274 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
1280 void FullCodeGenerator::EmitLoadGlobalCheckExtensions(VariableProxy* proxy,
1281 TypeofMode typeof_mode,
1283 Register context = esi;
1284 Register temp = edx;
1288 if (s->num_heap_slots() > 0) {
1289 if (s->calls_sloppy_eval()) {
1290 // Check that extension is NULL.
1291 __ cmp(ContextOperand(context, Context::EXTENSION_INDEX),
1293 __ j(not_equal, slow);
1295 // Load next context in chain.
1296 __ mov(temp, ContextOperand(context, Context::PREVIOUS_INDEX));
1297 // Walk the rest of the chain without clobbering esi.
1300 // If no outer scope calls eval, we do not need to check more
1301 // context extensions. If we have reached an eval scope, we check
1302 // all extensions from this point.
1303 if (!s->outer_scope_calls_sloppy_eval() || s->is_eval_scope()) break;
1304 s = s->outer_scope();
1307 if (s != NULL && s->is_eval_scope()) {
1308 // Loop up the context chain. There is no frame effect so it is
1309 // safe to use raw labels here.
1311 if (!context.is(temp)) {
1312 __ mov(temp, context);
1315 // Terminate at native context.
1316 __ cmp(FieldOperand(temp, HeapObject::kMapOffset),
1317 Immediate(isolate()->factory()->native_context_map()));
1318 __ j(equal, &fast, Label::kNear);
1319 // Check that extension is NULL.
1320 __ cmp(ContextOperand(temp, Context::EXTENSION_INDEX), Immediate(0));
1321 __ j(not_equal, slow);
1322 // Load next context in chain.
1323 __ mov(temp, ContextOperand(temp, Context::PREVIOUS_INDEX));
1328 // All extension objects were empty and it is safe to use a normal global
1330 EmitGlobalVariableLoad(proxy, typeof_mode);
1334 MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(Variable* var,
1336 DCHECK(var->IsContextSlot());
1337 Register context = esi;
1338 Register temp = ebx;
1340 for (Scope* s = scope(); s != var->scope(); s = s->outer_scope()) {
1341 if (s->num_heap_slots() > 0) {
1342 if (s->calls_sloppy_eval()) {
1343 // Check that extension is NULL.
1344 __ cmp(ContextOperand(context, Context::EXTENSION_INDEX),
1346 __ j(not_equal, slow);
1348 __ mov(temp, ContextOperand(context, Context::PREVIOUS_INDEX));
1349 // Walk the rest of the chain without clobbering esi.
1353 // Check that last extension is NULL.
1354 __ cmp(ContextOperand(context, Context::EXTENSION_INDEX), Immediate(0));
1355 __ j(not_equal, slow);
1357 // This function is used only for loads, not stores, so it's safe to
1358 // return an esi-based operand (the write barrier cannot be allowed to
1359 // destroy the esi register).
1360 return ContextOperand(context, var->index());
1364 void FullCodeGenerator::EmitDynamicLookupFastCase(VariableProxy* proxy,
1365 TypeofMode typeof_mode,
1366 Label* slow, Label* done) {
1367 // Generate fast-case code for variables that might be shadowed by
1368 // eval-introduced variables. Eval is used a lot without
1369 // introducing variables. In those cases, we do not want to
1370 // perform a runtime call for all variables in the scope
1371 // containing the eval.
1372 Variable* var = proxy->var();
1373 if (var->mode() == DYNAMIC_GLOBAL) {
1374 EmitLoadGlobalCheckExtensions(proxy, typeof_mode, slow);
1376 } else if (var->mode() == DYNAMIC_LOCAL) {
1377 Variable* local = var->local_if_not_shadowed();
1378 __ mov(eax, ContextSlotOperandCheckExtensions(local, slow));
1379 if (local->mode() == LET || local->mode() == CONST ||
1380 local->mode() == CONST_LEGACY) {
1381 __ cmp(eax, isolate()->factory()->the_hole_value());
1382 __ j(not_equal, done);
1383 if (local->mode() == CONST_LEGACY) {
1384 __ mov(eax, isolate()->factory()->undefined_value());
1385 } else { // LET || CONST
1386 __ push(Immediate(var->name()));
1387 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1395 void FullCodeGenerator::EmitGlobalVariableLoad(VariableProxy* proxy,
1396 TypeofMode typeof_mode) {
1397 Variable* var = proxy->var();
1398 DCHECK(var->IsUnallocatedOrGlobalSlot() ||
1399 (var->IsLookupSlot() && var->mode() == DYNAMIC_GLOBAL));
1400 if (var->IsGlobalSlot()) {
1401 DCHECK(var->index() > 0);
1402 DCHECK(var->IsStaticGlobalObjectProperty());
1403 // Each var occupies two slots in the context: for reads and writes.
1404 int slot_index = var->index();
1405 int depth = scope()->ContextChainLength(var->scope());
1406 __ mov(LoadGlobalViaContextDescriptor::DepthRegister(),
1407 Immediate(Smi::FromInt(depth)));
1408 __ mov(LoadGlobalViaContextDescriptor::SlotRegister(),
1409 Immediate(Smi::FromInt(slot_index)));
1410 __ mov(LoadGlobalViaContextDescriptor::NameRegister(), var->name());
1411 LoadGlobalViaContextStub stub(isolate(), depth);
1415 __ mov(LoadDescriptor::ReceiverRegister(), GlobalObjectOperand());
1416 __ mov(LoadDescriptor::NameRegister(), var->name());
1417 __ mov(LoadDescriptor::SlotRegister(),
1418 Immediate(SmiFromSlot(proxy->VariableFeedbackSlot())));
1419 CallLoadIC(typeof_mode);
1424 void FullCodeGenerator::EmitVariableLoad(VariableProxy* proxy,
1425 TypeofMode typeof_mode) {
1426 SetExpressionPosition(proxy);
1427 PrepareForBailoutForId(proxy->BeforeId(), NO_REGISTERS);
1428 Variable* var = proxy->var();
1430 // Three cases: global variables, lookup variables, and all other types of
1432 switch (var->location()) {
1433 case VariableLocation::GLOBAL:
1434 case VariableLocation::UNALLOCATED: {
1435 Comment cmnt(masm_, "[ Global variable");
1436 EmitGlobalVariableLoad(proxy, typeof_mode);
1437 context()->Plug(eax);
1441 case VariableLocation::PARAMETER:
1442 case VariableLocation::LOCAL:
1443 case VariableLocation::CONTEXT: {
1444 DCHECK_EQ(NOT_INSIDE_TYPEOF, typeof_mode);
1445 Comment cmnt(masm_, var->IsContextSlot() ? "[ Context variable"
1446 : "[ Stack variable");
1447 if (var->binding_needs_init()) {
1448 // var->scope() may be NULL when the proxy is located in eval code and
1449 // refers to a potential outside binding. Currently those bindings are
1450 // always looked up dynamically, i.e. in that case
1451 // var->location() == LOOKUP.
1453 DCHECK(var->scope() != NULL);
1455 // Check if the binding really needs an initialization check. The check
1456 // can be skipped in the following situation: we have a LET or CONST
1457 // binding in harmony mode, both the Variable and the VariableProxy have
1458 // the same declaration scope (i.e. they are both in global code, in the
1459 // same function or in the same eval code) and the VariableProxy is in
1460 // the source physically located after the initializer of the variable.
1462 // We cannot skip any initialization checks for CONST in non-harmony
1463 // mode because const variables may be declared but never initialized:
1464 // if (false) { const x; }; var y = x;
1466 // The condition on the declaration scopes is a conservative check for
1467 // nested functions that access a binding and are called before the
1468 // binding is initialized:
1469 // function() { f(); let x = 1; function f() { x = 2; } }
1471 bool skip_init_check;
1472 if (var->scope()->DeclarationScope() != scope()->DeclarationScope()) {
1473 skip_init_check = false;
1474 } else if (var->is_this()) {
1475 CHECK(info_->function() != nullptr &&
1476 (info_->function()->kind() & kSubclassConstructor) != 0);
1477 // TODO(dslomov): implement 'this' hole check elimination.
1478 skip_init_check = false;
1480 // Check that we always have valid source position.
1481 DCHECK(var->initializer_position() != RelocInfo::kNoPosition);
1482 DCHECK(proxy->position() != RelocInfo::kNoPosition);
1483 skip_init_check = var->mode() != CONST_LEGACY &&
1484 var->initializer_position() < proxy->position();
1487 if (!skip_init_check) {
1488 // Let and const need a read barrier.
1491 __ cmp(eax, isolate()->factory()->the_hole_value());
1492 __ j(not_equal, &done, Label::kNear);
1493 if (var->mode() == LET || var->mode() == CONST) {
1494 // Throw a reference error when using an uninitialized let/const
1495 // binding in harmony mode.
1496 __ push(Immediate(var->name()));
1497 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1499 // Uninitalized const bindings outside of harmony mode are unholed.
1500 DCHECK(var->mode() == CONST_LEGACY);
1501 __ mov(eax, isolate()->factory()->undefined_value());
1504 context()->Plug(eax);
1508 context()->Plug(var);
1512 case VariableLocation::LOOKUP: {
1513 Comment cmnt(masm_, "[ Lookup variable");
1515 // Generate code for loading from variables potentially shadowed
1516 // by eval-introduced variables.
1517 EmitDynamicLookupFastCase(proxy, typeof_mode, &slow, &done);
1519 __ push(esi); // Context.
1520 __ push(Immediate(var->name()));
1521 Runtime::FunctionId function_id =
1522 typeof_mode == NOT_INSIDE_TYPEOF
1523 ? Runtime::kLoadLookupSlot
1524 : Runtime::kLoadLookupSlotNoReferenceError;
1525 __ CallRuntime(function_id, 2);
1527 context()->Plug(eax);
1534 void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
1535 Comment cmnt(masm_, "[ RegExpLiteral");
1537 // Registers will be used as follows:
1538 // edi = JS function.
1539 // ecx = literals array.
1540 // ebx = regexp literal.
1541 // eax = regexp literal clone.
1542 __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1543 __ mov(ecx, FieldOperand(edi, JSFunction::kLiteralsOffset));
1544 int literal_offset =
1545 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1546 __ mov(ebx, FieldOperand(ecx, literal_offset));
1547 __ cmp(ebx, isolate()->factory()->undefined_value());
1548 __ j(not_equal, &materialized, Label::kNear);
1550 // Create regexp literal using runtime function
1551 // Result will be in eax.
1553 __ push(Immediate(Smi::FromInt(expr->literal_index())));
1554 __ push(Immediate(expr->pattern()));
1555 __ push(Immediate(expr->flags()));
1556 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1559 __ bind(&materialized);
1560 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1561 Label allocated, runtime_allocate;
1562 __ Allocate(size, eax, ecx, edx, &runtime_allocate, TAG_OBJECT);
1565 __ bind(&runtime_allocate);
1567 __ push(Immediate(Smi::FromInt(size)));
1568 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1571 __ bind(&allocated);
1572 // Copy the content into the newly allocated memory.
1573 // (Unroll copy loop once for better throughput).
1574 for (int i = 0; i < size - kPointerSize; i += 2 * kPointerSize) {
1575 __ mov(edx, FieldOperand(ebx, i));
1576 __ mov(ecx, FieldOperand(ebx, i + kPointerSize));
1577 __ mov(FieldOperand(eax, i), edx);
1578 __ mov(FieldOperand(eax, i + kPointerSize), ecx);
1580 if ((size % (2 * kPointerSize)) != 0) {
1581 __ mov(edx, FieldOperand(ebx, size - kPointerSize));
1582 __ mov(FieldOperand(eax, size - kPointerSize), edx);
1584 context()->Plug(eax);
1588 void FullCodeGenerator::EmitAccessor(Expression* expression) {
1589 if (expression == NULL) {
1590 __ push(Immediate(isolate()->factory()->null_value()));
1592 VisitForStackValue(expression);
1597 void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
1598 Comment cmnt(masm_, "[ ObjectLiteral");
1600 Handle<FixedArray> constant_properties = expr->constant_properties();
1601 int flags = expr->ComputeFlags();
1602 // If any of the keys would store to the elements array, then we shouldn't
1604 if (MustCreateObjectLiteralWithRuntime(expr)) {
1605 __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1606 __ push(FieldOperand(edi, JSFunction::kLiteralsOffset));
1607 __ push(Immediate(Smi::FromInt(expr->literal_index())));
1608 __ push(Immediate(constant_properties));
1609 __ push(Immediate(Smi::FromInt(flags)));
1610 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
1612 __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1613 __ mov(eax, FieldOperand(edi, JSFunction::kLiteralsOffset));
1614 __ mov(ebx, Immediate(Smi::FromInt(expr->literal_index())));
1615 __ mov(ecx, Immediate(constant_properties));
1616 __ mov(edx, Immediate(Smi::FromInt(flags)));
1617 FastCloneShallowObjectStub stub(isolate(), expr->properties_count());
1620 PrepareForBailoutForId(expr->CreateLiteralId(), TOS_REG);
1622 // If result_saved is true the result is on top of the stack. If
1623 // result_saved is false the result is in eax.
1624 bool result_saved = false;
1626 AccessorTable accessor_table(zone());
1627 int property_index = 0;
1628 // store_slot_index points to the vector IC slot for the next store IC used.
1629 // ObjectLiteral::ComputeFeedbackRequirements controls the allocation of slots
1630 // and must be updated if the number of store ICs emitted here changes.
1631 int store_slot_index = 0;
1632 for (; property_index < expr->properties()->length(); property_index++) {
1633 ObjectLiteral::Property* property = expr->properties()->at(property_index);
1634 if (property->is_computed_name()) break;
1635 if (property->IsCompileTimeValue()) continue;
1637 Literal* key = property->key()->AsLiteral();
1638 Expression* value = property->value();
1639 if (!result_saved) {
1640 __ push(eax); // Save result on the stack
1641 result_saved = true;
1643 switch (property->kind()) {
1644 case ObjectLiteral::Property::CONSTANT:
1646 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1647 DCHECK(!CompileTimeValue::IsCompileTimeValue(value));
1649 case ObjectLiteral::Property::COMPUTED:
1650 // It is safe to use [[Put]] here because the boilerplate already
1651 // contains computed properties with an uninitialized value.
1652 if (key->value()->IsInternalizedString()) {
1653 if (property->emit_store()) {
1654 VisitForAccumulatorValue(value);
1655 DCHECK(StoreDescriptor::ValueRegister().is(eax));
1656 __ mov(StoreDescriptor::NameRegister(), Immediate(key->value()));
1657 __ mov(StoreDescriptor::ReceiverRegister(), Operand(esp, 0));
1658 if (FLAG_vector_stores) {
1659 EmitLoadStoreICSlot(expr->GetNthSlot(store_slot_index++));
1662 CallStoreIC(key->LiteralFeedbackId());
1664 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1666 if (NeedsHomeObject(value)) {
1667 __ mov(StoreDescriptor::ReceiverRegister(), eax);
1668 __ mov(StoreDescriptor::NameRegister(),
1669 Immediate(isolate()->factory()->home_object_symbol()));
1670 __ mov(StoreDescriptor::ValueRegister(), Operand(esp, 0));
1671 if (FLAG_vector_stores) {
1672 EmitLoadStoreICSlot(expr->GetNthSlot(store_slot_index++));
1677 VisitForEffect(value);
1681 __ push(Operand(esp, 0)); // Duplicate receiver.
1682 VisitForStackValue(key);
1683 VisitForStackValue(value);
1684 if (property->emit_store()) {
1685 EmitSetHomeObjectIfNeeded(
1686 value, 2, expr->SlotForHomeObject(value, &store_slot_index));
1687 __ push(Immediate(Smi::FromInt(SLOPPY))); // Language mode
1688 __ CallRuntime(Runtime::kSetProperty, 4);
1693 case ObjectLiteral::Property::PROTOTYPE:
1694 __ push(Operand(esp, 0)); // Duplicate receiver.
1695 VisitForStackValue(value);
1696 DCHECK(property->emit_store());
1697 __ CallRuntime(Runtime::kInternalSetPrototype, 2);
1699 case ObjectLiteral::Property::GETTER:
1700 if (property->emit_store()) {
1701 accessor_table.lookup(key)->second->getter = value;
1704 case ObjectLiteral::Property::SETTER:
1705 if (property->emit_store()) {
1706 accessor_table.lookup(key)->second->setter = value;
1712 // Emit code to define accessors, using only a single call to the runtime for
1713 // each pair of corresponding getters and setters.
1714 for (AccessorTable::Iterator it = accessor_table.begin();
1715 it != accessor_table.end();
1717 __ push(Operand(esp, 0)); // Duplicate receiver.
1718 VisitForStackValue(it->first);
1719 EmitAccessor(it->second->getter);
1720 EmitSetHomeObjectIfNeeded(
1721 it->second->getter, 2,
1722 expr->SlotForHomeObject(it->second->getter, &store_slot_index));
1724 EmitAccessor(it->second->setter);
1725 EmitSetHomeObjectIfNeeded(
1726 it->second->setter, 3,
1727 expr->SlotForHomeObject(it->second->setter, &store_slot_index));
1729 __ push(Immediate(Smi::FromInt(NONE)));
1730 __ CallRuntime(Runtime::kDefineAccessorPropertyUnchecked, 5);
1733 // Object literals have two parts. The "static" part on the left contains no
1734 // computed property names, and so we can compute its map ahead of time; see
1735 // runtime.cc::CreateObjectLiteralBoilerplate. The second "dynamic" part
1736 // starts with the first computed property name, and continues with all
1737 // properties to its right. All the code from above initializes the static
1738 // component of the object literal, and arranges for the map of the result to
1739 // reflect the static order in which the keys appear. For the dynamic
1740 // properties, we compile them into a series of "SetOwnProperty" runtime
1741 // calls. This will preserve insertion order.
1742 for (; property_index < expr->properties()->length(); property_index++) {
1743 ObjectLiteral::Property* property = expr->properties()->at(property_index);
1745 Expression* value = property->value();
1746 if (!result_saved) {
1747 __ push(eax); // Save result on the stack
1748 result_saved = true;
1751 __ push(Operand(esp, 0)); // Duplicate receiver.
1753 if (property->kind() == ObjectLiteral::Property::PROTOTYPE) {
1754 DCHECK(!property->is_computed_name());
1755 VisitForStackValue(value);
1756 DCHECK(property->emit_store());
1757 __ CallRuntime(Runtime::kInternalSetPrototype, 2);
1759 EmitPropertyKey(property, expr->GetIdForProperty(property_index));
1760 VisitForStackValue(value);
1761 EmitSetHomeObjectIfNeeded(
1762 value, 2, expr->SlotForHomeObject(value, &store_slot_index));
1764 switch (property->kind()) {
1765 case ObjectLiteral::Property::CONSTANT:
1766 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1767 case ObjectLiteral::Property::COMPUTED:
1768 if (property->emit_store()) {
1769 __ push(Immediate(Smi::FromInt(NONE)));
1770 __ CallRuntime(Runtime::kDefineDataPropertyUnchecked, 4);
1776 case ObjectLiteral::Property::PROTOTYPE:
1780 case ObjectLiteral::Property::GETTER:
1781 __ push(Immediate(Smi::FromInt(NONE)));
1782 __ CallRuntime(Runtime::kDefineGetterPropertyUnchecked, 4);
1785 case ObjectLiteral::Property::SETTER:
1786 __ push(Immediate(Smi::FromInt(NONE)));
1787 __ CallRuntime(Runtime::kDefineSetterPropertyUnchecked, 4);
1793 if (expr->has_function()) {
1794 DCHECK(result_saved);
1795 __ push(Operand(esp, 0));
1796 __ CallRuntime(Runtime::kToFastProperties, 1);
1800 context()->PlugTOS();
1802 context()->Plug(eax);
1805 // Verify that compilation exactly consumed the number of store ic slots that
1806 // the ObjectLiteral node had to offer.
1807 DCHECK(!FLAG_vector_stores || store_slot_index == expr->slot_count());
1811 void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
1812 Comment cmnt(masm_, "[ ArrayLiteral");
1814 expr->BuildConstantElements(isolate());
1815 Handle<FixedArray> constant_elements = expr->constant_elements();
1816 bool has_constant_fast_elements =
1817 IsFastObjectElementsKind(expr->constant_elements_kind());
1819 AllocationSiteMode allocation_site_mode = TRACK_ALLOCATION_SITE;
1820 if (has_constant_fast_elements && !FLAG_allocation_site_pretenuring) {
1821 // If the only customer of allocation sites is transitioning, then
1822 // we can turn it off if we don't have anywhere else to transition to.
1823 allocation_site_mode = DONT_TRACK_ALLOCATION_SITE;
1826 if (MustCreateArrayLiteralWithRuntime(expr)) {
1827 __ mov(ebx, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1828 __ push(FieldOperand(ebx, JSFunction::kLiteralsOffset));
1829 __ push(Immediate(Smi::FromInt(expr->literal_index())));
1830 __ push(Immediate(constant_elements));
1831 __ push(Immediate(Smi::FromInt(expr->ComputeFlags())));
1832 __ CallRuntime(Runtime::kCreateArrayLiteral, 4);
1834 __ mov(ebx, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1835 __ mov(eax, FieldOperand(ebx, JSFunction::kLiteralsOffset));
1836 __ mov(ebx, Immediate(Smi::FromInt(expr->literal_index())));
1837 __ mov(ecx, Immediate(constant_elements));
1838 FastCloneShallowArrayStub stub(isolate(), allocation_site_mode);
1841 PrepareForBailoutForId(expr->CreateLiteralId(), TOS_REG);
1843 bool result_saved = false; // Is the result saved to the stack?
1844 ZoneList<Expression*>* subexprs = expr->values();
1845 int length = subexprs->length();
1847 // Emit code to evaluate all the non-constant subexpressions and to store
1848 // them into the newly cloned array.
1849 int array_index = 0;
1850 for (; array_index < length; array_index++) {
1851 Expression* subexpr = subexprs->at(array_index);
1852 if (subexpr->IsSpread()) break;
1854 // If the subexpression is a literal or a simple materialized literal it
1855 // is already set in the cloned array.
1856 if (CompileTimeValue::IsCompileTimeValue(subexpr)) continue;
1858 if (!result_saved) {
1859 __ push(eax); // array literal.
1860 __ push(Immediate(Smi::FromInt(expr->literal_index())));
1861 result_saved = true;
1863 VisitForAccumulatorValue(subexpr);
1865 if (has_constant_fast_elements) {
1866 // Fast-case array literal with ElementsKind of FAST_*_ELEMENTS, they
1867 // cannot transition and don't need to call the runtime stub.
1868 int offset = FixedArray::kHeaderSize + (array_index * kPointerSize);
1869 __ mov(ebx, Operand(esp, kPointerSize)); // Copy of array literal.
1870 __ mov(ebx, FieldOperand(ebx, JSObject::kElementsOffset));
1871 // Store the subexpression value in the array's elements.
1872 __ mov(FieldOperand(ebx, offset), result_register());
1873 // Update the write barrier for the array store.
1874 __ RecordWriteField(ebx, offset, result_register(), ecx, kDontSaveFPRegs,
1875 EMIT_REMEMBERED_SET, INLINE_SMI_CHECK);
1877 // Store the subexpression value in the array's elements.
1878 __ mov(ecx, Immediate(Smi::FromInt(array_index)));
1879 StoreArrayLiteralElementStub stub(isolate());
1883 PrepareForBailoutForId(expr->GetIdForElement(array_index), NO_REGISTERS);
1886 // In case the array literal contains spread expressions it has two parts. The
1887 // first part is the "static" array which has a literal index is handled
1888 // above. The second part is the part after the first spread expression
1889 // (inclusive) and these elements gets appended to the array. Note that the
1890 // number elements an iterable produces is unknown ahead of time.
1891 if (array_index < length && result_saved) {
1892 __ Drop(1); // literal index
1894 result_saved = false;
1896 for (; array_index < length; array_index++) {
1897 Expression* subexpr = subexprs->at(array_index);
1900 if (subexpr->IsSpread()) {
1901 VisitForStackValue(subexpr->AsSpread()->expression());
1902 __ InvokeBuiltin(Builtins::CONCAT_ITERABLE_TO_ARRAY, CALL_FUNCTION);
1904 VisitForStackValue(subexpr);
1905 __ CallRuntime(Runtime::kAppendElement, 2);
1908 PrepareForBailoutForId(expr->GetIdForElement(array_index), NO_REGISTERS);
1912 __ Drop(1); // literal index
1913 context()->PlugTOS();
1915 context()->Plug(eax);
1920 void FullCodeGenerator::VisitAssignment(Assignment* expr) {
1921 DCHECK(expr->target()->IsValidReferenceExpression());
1923 Comment cmnt(masm_, "[ Assignment");
1924 SetExpressionPosition(expr, INSERT_BREAK);
1926 Property* property = expr->target()->AsProperty();
1927 LhsKind assign_type = Property::GetAssignType(property);
1929 // Evaluate LHS expression.
1930 switch (assign_type) {
1932 // Nothing to do here.
1934 case NAMED_SUPER_PROPERTY:
1936 property->obj()->AsSuperPropertyReference()->this_var());
1937 VisitForAccumulatorValue(
1938 property->obj()->AsSuperPropertyReference()->home_object());
1939 __ push(result_register());
1940 if (expr->is_compound()) {
1941 __ push(MemOperand(esp, kPointerSize));
1942 __ push(result_register());
1945 case NAMED_PROPERTY:
1946 if (expr->is_compound()) {
1947 // We need the receiver both on the stack and in the register.
1948 VisitForStackValue(property->obj());
1949 __ mov(LoadDescriptor::ReceiverRegister(), Operand(esp, 0));
1951 VisitForStackValue(property->obj());
1954 case KEYED_SUPER_PROPERTY:
1956 property->obj()->AsSuperPropertyReference()->this_var());
1958 property->obj()->AsSuperPropertyReference()->home_object());
1959 VisitForAccumulatorValue(property->key());
1960 __ Push(result_register());
1961 if (expr->is_compound()) {
1962 __ push(MemOperand(esp, 2 * kPointerSize));
1963 __ push(MemOperand(esp, 2 * kPointerSize));
1964 __ push(result_register());
1967 case KEYED_PROPERTY: {
1968 if (expr->is_compound()) {
1969 VisitForStackValue(property->obj());
1970 VisitForStackValue(property->key());
1971 __ mov(LoadDescriptor::ReceiverRegister(), Operand(esp, kPointerSize));
1972 __ mov(LoadDescriptor::NameRegister(), Operand(esp, 0));
1974 VisitForStackValue(property->obj());
1975 VisitForStackValue(property->key());
1981 // For compound assignments we need another deoptimization point after the
1982 // variable/property load.
1983 if (expr->is_compound()) {
1984 AccumulatorValueContext result_context(this);
1985 { AccumulatorValueContext left_operand_context(this);
1986 switch (assign_type) {
1988 EmitVariableLoad(expr->target()->AsVariableProxy());
1989 PrepareForBailout(expr->target(), TOS_REG);
1991 case NAMED_SUPER_PROPERTY:
1992 EmitNamedSuperPropertyLoad(property);
1993 PrepareForBailoutForId(property->LoadId(), TOS_REG);
1995 case NAMED_PROPERTY:
1996 EmitNamedPropertyLoad(property);
1997 PrepareForBailoutForId(property->LoadId(), TOS_REG);
1999 case KEYED_SUPER_PROPERTY:
2000 EmitKeyedSuperPropertyLoad(property);
2001 PrepareForBailoutForId(property->LoadId(), TOS_REG);
2003 case KEYED_PROPERTY:
2004 EmitKeyedPropertyLoad(property);
2005 PrepareForBailoutForId(property->LoadId(), TOS_REG);
2010 Token::Value op = expr->binary_op();
2011 __ push(eax); // Left operand goes on the stack.
2012 VisitForAccumulatorValue(expr->value());
2014 if (ShouldInlineSmiCase(op)) {
2015 EmitInlineSmiBinaryOp(expr->binary_operation(),
2020 EmitBinaryOp(expr->binary_operation(), op);
2023 // Deoptimization point in case the binary operation may have side effects.
2024 PrepareForBailout(expr->binary_operation(), TOS_REG);
2026 VisitForAccumulatorValue(expr->value());
2029 SetExpressionPosition(expr);
2032 switch (assign_type) {
2034 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
2035 expr->op(), expr->AssignmentSlot());
2036 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2037 context()->Plug(eax);
2039 case NAMED_PROPERTY:
2040 EmitNamedPropertyAssignment(expr);
2042 case NAMED_SUPER_PROPERTY:
2043 EmitNamedSuperPropertyStore(property);
2044 context()->Plug(result_register());
2046 case KEYED_SUPER_PROPERTY:
2047 EmitKeyedSuperPropertyStore(property);
2048 context()->Plug(result_register());
2050 case KEYED_PROPERTY:
2051 EmitKeyedPropertyAssignment(expr);
2057 void FullCodeGenerator::VisitYield(Yield* expr) {
2058 Comment cmnt(masm_, "[ Yield");
2059 SetExpressionPosition(expr);
2061 // Evaluate yielded value first; the initial iterator definition depends on
2062 // this. It stays on the stack while we update the iterator.
2063 VisitForStackValue(expr->expression());
2065 switch (expr->yield_kind()) {
2066 case Yield::kSuspend:
2067 // Pop value from top-of-stack slot; box result into result register.
2068 EmitCreateIteratorResult(false);
2069 __ push(result_register());
2071 case Yield::kInitial: {
2072 Label suspend, continuation, post_runtime, resume;
2075 __ bind(&continuation);
2076 __ RecordGeneratorContinuation();
2080 VisitForAccumulatorValue(expr->generator_object());
2081 DCHECK(continuation.pos() > 0 && Smi::IsValid(continuation.pos()));
2082 __ mov(FieldOperand(eax, JSGeneratorObject::kContinuationOffset),
2083 Immediate(Smi::FromInt(continuation.pos())));
2084 __ mov(FieldOperand(eax, JSGeneratorObject::kContextOffset), esi);
2086 __ RecordWriteField(eax, JSGeneratorObject::kContextOffset, ecx, edx,
2088 __ lea(ebx, Operand(ebp, StandardFrameConstants::kExpressionsOffset));
2090 __ j(equal, &post_runtime);
2091 __ push(eax); // generator object
2092 __ CallRuntime(Runtime::kSuspendJSGeneratorObject, 1);
2093 __ mov(context_register(),
2094 Operand(ebp, StandardFrameConstants::kContextOffset));
2095 __ bind(&post_runtime);
2096 __ pop(result_register());
2097 EmitReturnSequence();
2100 context()->Plug(result_register());
2104 case Yield::kFinal: {
2105 VisitForAccumulatorValue(expr->generator_object());
2106 __ mov(FieldOperand(result_register(),
2107 JSGeneratorObject::kContinuationOffset),
2108 Immediate(Smi::FromInt(JSGeneratorObject::kGeneratorClosed)));
2109 // Pop value from top-of-stack slot, box result into result register.
2110 EmitCreateIteratorResult(true);
2111 EmitUnwindBeforeReturn();
2112 EmitReturnSequence();
2116 case Yield::kDelegating: {
2117 VisitForStackValue(expr->generator_object());
2119 // Initial stack layout is as follows:
2120 // [sp + 1 * kPointerSize] iter
2121 // [sp + 0 * kPointerSize] g
2123 Label l_catch, l_try, l_suspend, l_continuation, l_resume;
2124 Label l_next, l_call, l_loop;
2125 Register load_receiver = LoadDescriptor::ReceiverRegister();
2126 Register load_name = LoadDescriptor::NameRegister();
2128 // Initial send value is undefined.
2129 __ mov(eax, isolate()->factory()->undefined_value());
2132 // catch (e) { receiver = iter; f = 'throw'; arg = e; goto l_call; }
2134 __ mov(load_name, isolate()->factory()->throw_string()); // "throw"
2135 __ push(load_name); // "throw"
2136 __ push(Operand(esp, 2 * kPointerSize)); // iter
2137 __ push(eax); // exception
2140 // try { received = %yield result }
2141 // Shuffle the received result above a try handler and yield it without
2144 __ pop(eax); // result
2145 int handler_index = NewHandlerTableEntry();
2146 EnterTryBlock(handler_index, &l_catch);
2147 const int try_block_size = TryCatch::kElementCount * kPointerSize;
2148 __ push(eax); // result
2151 __ bind(&l_continuation);
2152 __ RecordGeneratorContinuation();
2155 __ bind(&l_suspend);
2156 const int generator_object_depth = kPointerSize + try_block_size;
2157 __ mov(eax, Operand(esp, generator_object_depth));
2159 __ push(Immediate(Smi::FromInt(handler_index))); // handler-index
2160 DCHECK(l_continuation.pos() > 0 && Smi::IsValid(l_continuation.pos()));
2161 __ mov(FieldOperand(eax, JSGeneratorObject::kContinuationOffset),
2162 Immediate(Smi::FromInt(l_continuation.pos())));
2163 __ mov(FieldOperand(eax, JSGeneratorObject::kContextOffset), esi);
2165 __ RecordWriteField(eax, JSGeneratorObject::kContextOffset, ecx, edx,
2167 __ CallRuntime(Runtime::kSuspendJSGeneratorObject, 2);
2168 __ mov(context_register(),
2169 Operand(ebp, StandardFrameConstants::kContextOffset));
2170 __ pop(eax); // result
2171 EmitReturnSequence();
2172 __ bind(&l_resume); // received in eax
2173 ExitTryBlock(handler_index);
2175 // receiver = iter; f = iter.next; arg = received;
2178 __ mov(load_name, isolate()->factory()->next_string());
2179 __ push(load_name); // "next"
2180 __ push(Operand(esp, 2 * kPointerSize)); // iter
2181 __ push(eax); // received
2183 // result = receiver[f](arg);
2185 __ mov(load_receiver, Operand(esp, kPointerSize));
2186 __ mov(LoadDescriptor::SlotRegister(),
2187 Immediate(SmiFromSlot(expr->KeyedLoadFeedbackSlot())));
2188 Handle<Code> ic = CodeFactory::KeyedLoadIC(isolate(), SLOPPY).code();
2189 CallIC(ic, TypeFeedbackId::None());
2191 __ mov(Operand(esp, 2 * kPointerSize), edi);
2192 SetCallPosition(expr, 1);
2193 CallFunctionStub stub(isolate(), 1, CALL_AS_METHOD);
2196 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
2197 __ Drop(1); // The function is still on the stack; drop it.
2199 // if (!result.done) goto l_try;
2201 __ push(eax); // save result
2202 __ Move(load_receiver, eax); // result
2204 isolate()->factory()->done_string()); // "done"
2205 __ mov(LoadDescriptor::SlotRegister(),
2206 Immediate(SmiFromSlot(expr->DoneFeedbackSlot())));
2207 CallLoadIC(NOT_INSIDE_TYPEOF); // result.done in eax
2208 Handle<Code> bool_ic = ToBooleanStub::GetUninitialized(isolate());
2214 __ pop(load_receiver); // result
2216 isolate()->factory()->value_string()); // "value"
2217 __ mov(LoadDescriptor::SlotRegister(),
2218 Immediate(SmiFromSlot(expr->ValueFeedbackSlot())));
2219 CallLoadIC(NOT_INSIDE_TYPEOF); // result.value in eax
2220 context()->DropAndPlug(2, eax); // drop iter and g
2227 void FullCodeGenerator::EmitGeneratorResume(Expression *generator,
2229 JSGeneratorObject::ResumeMode resume_mode) {
2230 // The value stays in eax, and is ultimately read by the resumed generator, as
2231 // if CallRuntime(Runtime::kSuspendJSGeneratorObject) returned it. Or it
2232 // is read to throw the value when the resumed generator is already closed.
2233 // ebx will hold the generator object until the activation has been resumed.
2234 VisitForStackValue(generator);
2235 VisitForAccumulatorValue(value);
2238 // Load suspended function and context.
2239 __ mov(esi, FieldOperand(ebx, JSGeneratorObject::kContextOffset));
2240 __ mov(edi, FieldOperand(ebx, JSGeneratorObject::kFunctionOffset));
2243 __ push(FieldOperand(ebx, JSGeneratorObject::kReceiverOffset));
2245 // Push holes for arguments to generator function.
2246 __ mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
2248 FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
2249 __ mov(ecx, isolate()->factory()->the_hole_value());
2250 Label push_argument_holes, push_frame;
2251 __ bind(&push_argument_holes);
2252 __ sub(edx, Immediate(Smi::FromInt(1)));
2253 __ j(carry, &push_frame);
2255 __ jmp(&push_argument_holes);
2257 // Enter a new JavaScript frame, and initialize its slots as they were when
2258 // the generator was suspended.
2259 Label resume_frame, done;
2260 __ bind(&push_frame);
2261 __ call(&resume_frame);
2263 __ bind(&resume_frame);
2264 __ push(ebp); // Caller's frame pointer.
2266 __ push(esi); // Callee's context.
2267 __ push(edi); // Callee's JS Function.
2269 // Load the operand stack size.
2270 __ mov(edx, FieldOperand(ebx, JSGeneratorObject::kOperandStackOffset));
2271 __ mov(edx, FieldOperand(edx, FixedArray::kLengthOffset));
2274 // If we are sending a value and there is no operand stack, we can jump back
2276 if (resume_mode == JSGeneratorObject::NEXT) {
2278 __ cmp(edx, Immediate(0));
2279 __ j(not_zero, &slow_resume);
2280 __ mov(edx, FieldOperand(edi, JSFunction::kCodeEntryOffset));
2281 __ mov(ecx, FieldOperand(ebx, JSGeneratorObject::kContinuationOffset));
2284 __ mov(FieldOperand(ebx, JSGeneratorObject::kContinuationOffset),
2285 Immediate(Smi::FromInt(JSGeneratorObject::kGeneratorExecuting)));
2287 __ bind(&slow_resume);
2290 // Otherwise, we push holes for the operand stack and call the runtime to fix
2291 // up the stack and the handlers.
2292 Label push_operand_holes, call_resume;
2293 __ bind(&push_operand_holes);
2294 __ sub(edx, Immediate(1));
2295 __ j(carry, &call_resume);
2297 __ jmp(&push_operand_holes);
2298 __ bind(&call_resume);
2300 __ push(result_register());
2301 __ Push(Smi::FromInt(resume_mode));
2302 __ CallRuntime(Runtime::kResumeJSGeneratorObject, 3);
2303 // Not reached: the runtime call returns elsewhere.
2304 __ Abort(kGeneratorFailedToResume);
2307 context()->Plug(result_register());
2311 void FullCodeGenerator::EmitCreateIteratorResult(bool done) {
2315 const int instance_size = 5 * kPointerSize;
2316 DCHECK_EQ(isolate()->native_context()->iterator_result_map()->instance_size(),
2319 __ Allocate(instance_size, eax, ecx, edx, &gc_required, TAG_OBJECT);
2322 __ bind(&gc_required);
2323 __ Push(Smi::FromInt(instance_size));
2324 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
2325 __ mov(context_register(),
2326 Operand(ebp, StandardFrameConstants::kContextOffset));
2328 __ bind(&allocated);
2329 __ mov(ebx, Operand(esi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
2330 __ mov(ebx, FieldOperand(ebx, GlobalObject::kNativeContextOffset));
2331 __ mov(ebx, ContextOperand(ebx, Context::ITERATOR_RESULT_MAP_INDEX));
2333 __ mov(edx, isolate()->factory()->ToBoolean(done));
2334 __ mov(FieldOperand(eax, HeapObject::kMapOffset), ebx);
2335 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset),
2336 isolate()->factory()->empty_fixed_array());
2337 __ mov(FieldOperand(eax, JSObject::kElementsOffset),
2338 isolate()->factory()->empty_fixed_array());
2339 __ mov(FieldOperand(eax, JSGeneratorObject::kResultValuePropertyOffset), ecx);
2340 __ mov(FieldOperand(eax, JSGeneratorObject::kResultDonePropertyOffset), edx);
2342 // Only the value field needs a write barrier, as the other values are in the
2344 __ RecordWriteField(eax, JSGeneratorObject::kResultValuePropertyOffset, ecx,
2345 edx, kDontSaveFPRegs);
2349 void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
2350 SetExpressionPosition(prop);
2351 Literal* key = prop->key()->AsLiteral();
2352 DCHECK(!key->value()->IsSmi());
2353 DCHECK(!prop->IsSuperAccess());
2355 __ mov(LoadDescriptor::NameRegister(), Immediate(key->value()));
2356 __ mov(LoadDescriptor::SlotRegister(),
2357 Immediate(SmiFromSlot(prop->PropertyFeedbackSlot())));
2358 CallLoadIC(NOT_INSIDE_TYPEOF, language_mode());
2362 void FullCodeGenerator::EmitNamedSuperPropertyLoad(Property* prop) {
2363 // Stack: receiver, home_object.
2364 SetExpressionPosition(prop);
2365 Literal* key = prop->key()->AsLiteral();
2366 DCHECK(!key->value()->IsSmi());
2367 DCHECK(prop->IsSuperAccess());
2369 __ push(Immediate(key->value()));
2370 __ push(Immediate(Smi::FromInt(language_mode())));
2371 __ CallRuntime(Runtime::kLoadFromSuper, 4);
2375 void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
2376 SetExpressionPosition(prop);
2377 Handle<Code> ic = CodeFactory::KeyedLoadIC(isolate(), language_mode()).code();
2378 __ mov(LoadDescriptor::SlotRegister(),
2379 Immediate(SmiFromSlot(prop->PropertyFeedbackSlot())));
2384 void FullCodeGenerator::EmitKeyedSuperPropertyLoad(Property* prop) {
2385 // Stack: receiver, home_object, key.
2386 SetExpressionPosition(prop);
2387 __ push(Immediate(Smi::FromInt(language_mode())));
2388 __ CallRuntime(Runtime::kLoadKeyedFromSuper, 4);
2392 void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
2395 Expression* right) {
2396 // Do combined smi check of the operands. Left operand is on the
2397 // stack. Right operand is in eax.
2398 Label smi_case, done, stub_call;
2402 JumpPatchSite patch_site(masm_);
2403 patch_site.EmitJumpIfSmi(eax, &smi_case, Label::kNear);
2405 __ bind(&stub_call);
2408 CodeFactory::BinaryOpIC(isolate(), op, strength(language_mode())).code();
2409 CallIC(code, expr->BinaryOperationFeedbackId());
2410 patch_site.EmitPatchInfo();
2411 __ jmp(&done, Label::kNear);
2415 __ mov(eax, edx); // Copy left operand in case of a stub call.
2420 __ sar_cl(eax); // No checks of result necessary
2421 __ and_(eax, Immediate(~kSmiTagMask));
2428 // Check that the *signed* result fits in a smi.
2429 __ cmp(eax, 0xc0000000);
2430 __ j(positive, &result_ok);
2433 __ bind(&result_ok);
2442 __ test(eax, Immediate(0xc0000000));
2443 __ j(zero, &result_ok);
2446 __ bind(&result_ok);
2452 __ j(overflow, &stub_call);
2456 __ j(overflow, &stub_call);
2461 __ j(overflow, &stub_call);
2463 __ j(not_zero, &done, Label::kNear);
2466 __ j(negative, &stub_call);
2472 case Token::BIT_AND:
2475 case Token::BIT_XOR:
2483 context()->Plug(eax);
2487 void FullCodeGenerator::EmitClassDefineProperties(ClassLiteral* lit,
2488 int* used_store_slots) {
2489 // Constructor is in eax.
2490 DCHECK(lit != NULL);
2493 // No access check is needed here since the constructor is created by the
2495 Register scratch = ebx;
2496 __ mov(scratch, FieldOperand(eax, JSFunction::kPrototypeOrInitialMapOffset));
2499 for (int i = 0; i < lit->properties()->length(); i++) {
2500 ObjectLiteral::Property* property = lit->properties()->at(i);
2501 Expression* value = property->value();
2503 if (property->is_static()) {
2504 __ push(Operand(esp, kPointerSize)); // constructor
2506 __ push(Operand(esp, 0)); // prototype
2508 EmitPropertyKey(property, lit->GetIdForProperty(i));
2510 // The static prototype property is read only. We handle the non computed
2511 // property name case in the parser. Since this is the only case where we
2512 // need to check for an own read only property we special case this so we do
2513 // not need to do this for every property.
2514 if (property->is_static() && property->is_computed_name()) {
2515 __ CallRuntime(Runtime::kThrowIfStaticPrototype, 1);
2519 VisitForStackValue(value);
2520 EmitSetHomeObjectIfNeeded(value, 2,
2521 lit->SlotForHomeObject(value, used_store_slots));
2523 switch (property->kind()) {
2524 case ObjectLiteral::Property::CONSTANT:
2525 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2526 case ObjectLiteral::Property::PROTOTYPE:
2528 case ObjectLiteral::Property::COMPUTED:
2529 __ CallRuntime(Runtime::kDefineClassMethod, 3);
2532 case ObjectLiteral::Property::GETTER:
2533 __ push(Immediate(Smi::FromInt(DONT_ENUM)));
2534 __ CallRuntime(Runtime::kDefineGetterPropertyUnchecked, 4);
2537 case ObjectLiteral::Property::SETTER:
2538 __ push(Immediate(Smi::FromInt(DONT_ENUM)));
2539 __ CallRuntime(Runtime::kDefineSetterPropertyUnchecked, 4);
2545 __ CallRuntime(Runtime::kToFastProperties, 1);
2548 __ CallRuntime(Runtime::kToFastProperties, 1);
2550 if (is_strong(language_mode())) {
2552 FieldOperand(eax, JSFunction::kPrototypeOrInitialMapOffset));
2555 // TODO(conradw): It would be more efficient to define the properties with
2556 // the right attributes the first time round.
2557 // Freeze the prototype.
2558 __ CallRuntime(Runtime::kObjectFreeze, 1);
2559 // Freeze the constructor.
2560 __ CallRuntime(Runtime::kObjectFreeze, 1);
2565 void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr, Token::Value op) {
2568 CodeFactory::BinaryOpIC(isolate(), op, strength(language_mode())).code();
2569 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
2570 CallIC(code, expr->BinaryOperationFeedbackId());
2571 patch_site.EmitPatchInfo();
2572 context()->Plug(eax);
2576 void FullCodeGenerator::EmitAssignment(Expression* expr,
2577 FeedbackVectorICSlot slot) {
2578 DCHECK(expr->IsValidReferenceExpression());
2580 Property* prop = expr->AsProperty();
2581 LhsKind assign_type = Property::GetAssignType(prop);
2583 switch (assign_type) {
2585 Variable* var = expr->AsVariableProxy()->var();
2586 EffectContext context(this);
2587 EmitVariableAssignment(var, Token::ASSIGN, slot);
2590 case NAMED_PROPERTY: {
2591 __ push(eax); // Preserve value.
2592 VisitForAccumulatorValue(prop->obj());
2593 __ Move(StoreDescriptor::ReceiverRegister(), eax);
2594 __ pop(StoreDescriptor::ValueRegister()); // Restore value.
2595 __ mov(StoreDescriptor::NameRegister(),
2596 prop->key()->AsLiteral()->value());
2597 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
2601 case NAMED_SUPER_PROPERTY: {
2603 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
2604 VisitForAccumulatorValue(
2605 prop->obj()->AsSuperPropertyReference()->home_object());
2606 // stack: value, this; eax: home_object
2607 Register scratch = ecx;
2608 Register scratch2 = edx;
2609 __ mov(scratch, result_register()); // home_object
2610 __ mov(eax, MemOperand(esp, kPointerSize)); // value
2611 __ mov(scratch2, MemOperand(esp, 0)); // this
2612 __ mov(MemOperand(esp, kPointerSize), scratch2); // this
2613 __ mov(MemOperand(esp, 0), scratch); // home_object
2614 // stack: this, home_object. eax: value
2615 EmitNamedSuperPropertyStore(prop);
2618 case KEYED_SUPER_PROPERTY: {
2620 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
2622 prop->obj()->AsSuperPropertyReference()->home_object());
2623 VisitForAccumulatorValue(prop->key());
2624 Register scratch = ecx;
2625 Register scratch2 = edx;
2626 __ mov(scratch2, MemOperand(esp, 2 * kPointerSize)); // value
2627 // stack: value, this, home_object; eax: key, edx: value
2628 __ mov(scratch, MemOperand(esp, kPointerSize)); // this
2629 __ mov(MemOperand(esp, 2 * kPointerSize), scratch);
2630 __ mov(scratch, MemOperand(esp, 0)); // home_object
2631 __ mov(MemOperand(esp, kPointerSize), scratch);
2632 __ mov(MemOperand(esp, 0), eax);
2633 __ mov(eax, scratch2);
2634 // stack: this, home_object, key; eax: value.
2635 EmitKeyedSuperPropertyStore(prop);
2638 case KEYED_PROPERTY: {
2639 __ push(eax); // Preserve value.
2640 VisitForStackValue(prop->obj());
2641 VisitForAccumulatorValue(prop->key());
2642 __ Move(StoreDescriptor::NameRegister(), eax);
2643 __ pop(StoreDescriptor::ReceiverRegister()); // Receiver.
2644 __ pop(StoreDescriptor::ValueRegister()); // Restore value.
2645 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
2647 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
2652 context()->Plug(eax);
2656 void FullCodeGenerator::EmitStoreToStackLocalOrContextSlot(
2657 Variable* var, MemOperand location) {
2658 __ mov(location, eax);
2659 if (var->IsContextSlot()) {
2661 int offset = Context::SlotOffset(var->index());
2662 __ RecordWriteContextSlot(ecx, offset, edx, ebx, kDontSaveFPRegs);
2667 void FullCodeGenerator::EmitVariableAssignment(Variable* var, Token::Value op,
2668 FeedbackVectorICSlot slot) {
2669 if (var->IsUnallocated()) {
2670 // Global var, const, or let.
2671 __ mov(StoreDescriptor::NameRegister(), var->name());
2672 __ mov(StoreDescriptor::ReceiverRegister(), GlobalObjectOperand());
2673 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
2676 } else if (var->IsGlobalSlot()) {
2677 // Global var, const, or let.
2678 DCHECK(var->index() > 0);
2679 DCHECK(var->IsStaticGlobalObjectProperty());
2680 // Each var occupies two slots in the context: for reads and writes.
2681 int slot_index = var->index() + 1;
2682 int depth = scope()->ContextChainLength(var->scope());
2683 __ mov(StoreGlobalViaContextDescriptor::DepthRegister(),
2684 Immediate(Smi::FromInt(depth)));
2685 __ mov(StoreGlobalViaContextDescriptor::SlotRegister(),
2686 Immediate(Smi::FromInt(slot_index)));
2687 __ mov(StoreGlobalViaContextDescriptor::NameRegister(), var->name());
2688 DCHECK(StoreGlobalViaContextDescriptor::ValueRegister().is(eax));
2689 StoreGlobalViaContextStub stub(isolate(), depth, language_mode());
2692 } else if (var->mode() == LET && op != Token::INIT_LET) {
2693 // Non-initializing assignment to let variable needs a write barrier.
2694 DCHECK(!var->IsLookupSlot());
2695 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2697 MemOperand location = VarOperand(var, ecx);
2698 __ mov(edx, location);
2699 __ cmp(edx, isolate()->factory()->the_hole_value());
2700 __ j(not_equal, &assign, Label::kNear);
2701 __ push(Immediate(var->name()));
2702 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2704 EmitStoreToStackLocalOrContextSlot(var, location);
2706 } else if (var->mode() == CONST && op != Token::INIT_CONST) {
2707 // Assignment to const variable needs a write barrier.
2708 DCHECK(!var->IsLookupSlot());
2709 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2711 MemOperand location = VarOperand(var, ecx);
2712 __ mov(edx, location);
2713 __ cmp(edx, isolate()->factory()->the_hole_value());
2714 __ j(not_equal, &const_error, Label::kNear);
2715 __ push(Immediate(var->name()));
2716 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2717 __ bind(&const_error);
2718 __ CallRuntime(Runtime::kThrowConstAssignError, 0);
2720 } else if (!var->is_const_mode() || op == Token::INIT_CONST) {
2721 if (var->IsLookupSlot()) {
2722 // Assignment to var.
2723 __ push(eax); // Value.
2724 __ push(esi); // Context.
2725 __ push(Immediate(var->name()));
2726 __ push(Immediate(Smi::FromInt(language_mode())));
2727 __ CallRuntime(Runtime::kStoreLookupSlot, 4);
2729 // Assignment to var or initializing assignment to let/const in harmony
2731 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2732 MemOperand location = VarOperand(var, ecx);
2733 if (generate_debug_code_ && op == Token::INIT_LET) {
2734 // Check for an uninitialized let binding.
2735 __ mov(edx, location);
2736 __ cmp(edx, isolate()->factory()->the_hole_value());
2737 __ Check(equal, kLetBindingReInitialization);
2739 EmitStoreToStackLocalOrContextSlot(var, location);
2742 } else if (op == Token::INIT_CONST_LEGACY) {
2743 // Const initializers need a write barrier.
2744 DCHECK(var->mode() == CONST_LEGACY);
2745 DCHECK(!var->IsParameter()); // No const parameters.
2746 if (var->IsLookupSlot()) {
2749 __ push(Immediate(var->name()));
2750 __ CallRuntime(Runtime::kInitializeLegacyConstLookupSlot, 3);
2752 DCHECK(var->IsStackLocal() || var->IsContextSlot());
2754 MemOperand location = VarOperand(var, ecx);
2755 __ mov(edx, location);
2756 __ cmp(edx, isolate()->factory()->the_hole_value());
2757 __ j(not_equal, &skip, Label::kNear);
2758 EmitStoreToStackLocalOrContextSlot(var, location);
2763 DCHECK(var->mode() == CONST_LEGACY && op != Token::INIT_CONST_LEGACY);
2764 if (is_strict(language_mode())) {
2765 __ CallRuntime(Runtime::kThrowConstAssignError, 0);
2767 // Silently ignore store in sloppy mode.
2772 void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
2773 // Assignment to a property, using a named store IC.
2775 // esp[0] : receiver
2776 Property* prop = expr->target()->AsProperty();
2777 DCHECK(prop != NULL);
2778 DCHECK(prop->key()->IsLiteral());
2780 __ mov(StoreDescriptor::NameRegister(), prop->key()->AsLiteral()->value());
2781 __ pop(StoreDescriptor::ReceiverRegister());
2782 if (FLAG_vector_stores) {
2783 EmitLoadStoreICSlot(expr->AssignmentSlot());
2786 CallStoreIC(expr->AssignmentFeedbackId());
2788 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2789 context()->Plug(eax);
2793 void FullCodeGenerator::EmitNamedSuperPropertyStore(Property* prop) {
2794 // Assignment to named property of super.
2796 // stack : receiver ('this'), home_object
2797 DCHECK(prop != NULL);
2798 Literal* key = prop->key()->AsLiteral();
2799 DCHECK(key != NULL);
2801 __ push(Immediate(key->value()));
2803 __ CallRuntime((is_strict(language_mode()) ? Runtime::kStoreToSuper_Strict
2804 : Runtime::kStoreToSuper_Sloppy),
2809 void FullCodeGenerator::EmitKeyedSuperPropertyStore(Property* prop) {
2810 // Assignment to named property of super.
2812 // stack : receiver ('this'), home_object, key
2816 (is_strict(language_mode()) ? Runtime::kStoreKeyedToSuper_Strict
2817 : Runtime::kStoreKeyedToSuper_Sloppy),
2822 void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
2823 // Assignment to a property, using a keyed store IC.
2826 // esp[kPointerSize] : receiver
2828 __ pop(StoreDescriptor::NameRegister()); // Key.
2829 __ pop(StoreDescriptor::ReceiverRegister());
2830 DCHECK(StoreDescriptor::ValueRegister().is(eax));
2832 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
2833 if (FLAG_vector_stores) {
2834 EmitLoadStoreICSlot(expr->AssignmentSlot());
2837 CallIC(ic, expr->AssignmentFeedbackId());
2840 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2841 context()->Plug(eax);
2845 void FullCodeGenerator::VisitProperty(Property* expr) {
2846 Comment cmnt(masm_, "[ Property");
2847 SetExpressionPosition(expr);
2849 Expression* key = expr->key();
2851 if (key->IsPropertyName()) {
2852 if (!expr->IsSuperAccess()) {
2853 VisitForAccumulatorValue(expr->obj());
2854 __ Move(LoadDescriptor::ReceiverRegister(), result_register());
2855 EmitNamedPropertyLoad(expr);
2857 VisitForStackValue(expr->obj()->AsSuperPropertyReference()->this_var());
2859 expr->obj()->AsSuperPropertyReference()->home_object());
2860 EmitNamedSuperPropertyLoad(expr);
2863 if (!expr->IsSuperAccess()) {
2864 VisitForStackValue(expr->obj());
2865 VisitForAccumulatorValue(expr->key());
2866 __ pop(LoadDescriptor::ReceiverRegister()); // Object.
2867 __ Move(LoadDescriptor::NameRegister(), result_register()); // Key.
2868 EmitKeyedPropertyLoad(expr);
2870 VisitForStackValue(expr->obj()->AsSuperPropertyReference()->this_var());
2872 expr->obj()->AsSuperPropertyReference()->home_object());
2873 VisitForStackValue(expr->key());
2874 EmitKeyedSuperPropertyLoad(expr);
2877 PrepareForBailoutForId(expr->LoadId(), TOS_REG);
2878 context()->Plug(eax);
2882 void FullCodeGenerator::CallIC(Handle<Code> code,
2883 TypeFeedbackId ast_id) {
2885 __ call(code, RelocInfo::CODE_TARGET, ast_id);
2889 // Code common for calls using the IC.
2890 void FullCodeGenerator::EmitCallWithLoadIC(Call* expr) {
2891 Expression* callee = expr->expression();
2893 CallICState::CallType call_type =
2894 callee->IsVariableProxy() ? CallICState::FUNCTION : CallICState::METHOD;
2895 // Get the target function.
2896 if (call_type == CallICState::FUNCTION) {
2897 { StackValueContext context(this);
2898 EmitVariableLoad(callee->AsVariableProxy());
2899 PrepareForBailout(callee, NO_REGISTERS);
2901 // Push undefined as receiver. This is patched in the method prologue if it
2902 // is a sloppy mode method.
2903 __ push(Immediate(isolate()->factory()->undefined_value()));
2905 // Load the function from the receiver.
2906 DCHECK(callee->IsProperty());
2907 DCHECK(!callee->AsProperty()->IsSuperAccess());
2908 __ mov(LoadDescriptor::ReceiverRegister(), Operand(esp, 0));
2909 EmitNamedPropertyLoad(callee->AsProperty());
2910 PrepareForBailoutForId(callee->AsProperty()->LoadId(), TOS_REG);
2911 // Push the target function under the receiver.
2912 __ push(Operand(esp, 0));
2913 __ mov(Operand(esp, kPointerSize), eax);
2916 EmitCall(expr, call_type);
2920 void FullCodeGenerator::EmitSuperCallWithLoadIC(Call* expr) {
2921 SetExpressionPosition(expr);
2922 Expression* callee = expr->expression();
2923 DCHECK(callee->IsProperty());
2924 Property* prop = callee->AsProperty();
2925 DCHECK(prop->IsSuperAccess());
2927 Literal* key = prop->key()->AsLiteral();
2928 DCHECK(!key->value()->IsSmi());
2929 // Load the function from the receiver.
2930 SuperPropertyReference* super_ref = prop->obj()->AsSuperPropertyReference();
2931 VisitForStackValue(super_ref->home_object());
2932 VisitForAccumulatorValue(super_ref->this_var());
2935 __ push(Operand(esp, kPointerSize * 2));
2936 __ push(Immediate(key->value()));
2937 __ push(Immediate(Smi::FromInt(language_mode())));
2940 // - this (receiver)
2941 // - this (receiver) <-- LoadFromSuper will pop here and below.
2945 __ CallRuntime(Runtime::kLoadFromSuper, 4);
2947 // Replace home_object with target function.
2948 __ mov(Operand(esp, kPointerSize), eax);
2951 // - target function
2952 // - this (receiver)
2953 EmitCall(expr, CallICState::METHOD);
2957 // Code common for calls using the IC.
2958 void FullCodeGenerator::EmitKeyedCallWithLoadIC(Call* expr,
2961 VisitForAccumulatorValue(key);
2963 Expression* callee = expr->expression();
2965 // Load the function from the receiver.
2966 DCHECK(callee->IsProperty());
2967 __ mov(LoadDescriptor::ReceiverRegister(), Operand(esp, 0));
2968 __ mov(LoadDescriptor::NameRegister(), eax);
2969 EmitKeyedPropertyLoad(callee->AsProperty());
2970 PrepareForBailoutForId(callee->AsProperty()->LoadId(), TOS_REG);
2972 // Push the target function under the receiver.
2973 __ push(Operand(esp, 0));
2974 __ mov(Operand(esp, kPointerSize), eax);
2976 EmitCall(expr, CallICState::METHOD);
2980 void FullCodeGenerator::EmitKeyedSuperCallWithLoadIC(Call* expr) {
2981 Expression* callee = expr->expression();
2982 DCHECK(callee->IsProperty());
2983 Property* prop = callee->AsProperty();
2984 DCHECK(prop->IsSuperAccess());
2986 SetExpressionPosition(prop);
2987 // Load the function from the receiver.
2988 SuperPropertyReference* super_ref = prop->obj()->AsSuperPropertyReference();
2989 VisitForStackValue(super_ref->home_object());
2990 VisitForAccumulatorValue(super_ref->this_var());
2993 __ push(Operand(esp, kPointerSize * 2));
2994 VisitForStackValue(prop->key());
2995 __ push(Immediate(Smi::FromInt(language_mode())));
2998 // - this (receiver)
2999 // - this (receiver) <-- LoadKeyedFromSuper will pop here and below.
3003 __ CallRuntime(Runtime::kLoadKeyedFromSuper, 4);
3005 // Replace home_object with target function.
3006 __ mov(Operand(esp, kPointerSize), eax);
3009 // - target function
3010 // - this (receiver)
3011 EmitCall(expr, CallICState::METHOD);
3015 void FullCodeGenerator::EmitCall(Call* expr, CallICState::CallType call_type) {
3016 // Load the arguments.
3017 ZoneList<Expression*>* args = expr->arguments();
3018 int arg_count = args->length();
3019 for (int i = 0; i < arg_count; i++) {
3020 VisitForStackValue(args->at(i));
3023 SetCallPosition(expr, arg_count);
3024 Handle<Code> ic = CodeFactory::CallIC(isolate(), arg_count, call_type).code();
3025 __ Move(edx, Immediate(SmiFromSlot(expr->CallFeedbackICSlot())));
3026 __ mov(edi, Operand(esp, (arg_count + 1) * kPointerSize));
3027 // Don't assign a type feedback id to the IC, since type feedback is provided
3028 // by the vector above.
3031 RecordJSReturnSite(expr);
3033 // Restore context register.
3034 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
3036 context()->DropAndPlug(1, eax);
3040 void FullCodeGenerator::EmitResolvePossiblyDirectEval(int arg_count) {
3041 // Push copy of the first argument or undefined if it doesn't exist.
3042 if (arg_count > 0) {
3043 __ push(Operand(esp, arg_count * kPointerSize));
3045 __ push(Immediate(isolate()->factory()->undefined_value()));
3048 // Push the enclosing function.
3049 __ push(Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
3051 // Push the language mode.
3052 __ push(Immediate(Smi::FromInt(language_mode())));
3054 // Push the start position of the scope the calls resides in.
3055 __ push(Immediate(Smi::FromInt(scope()->start_position())));
3057 // Do the runtime call.
3058 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 5);
3062 void FullCodeGenerator::EmitInitializeThisAfterSuper(
3063 SuperCallReference* super_call_ref, FeedbackVectorICSlot slot) {
3064 Variable* this_var = super_call_ref->this_var()->var();
3065 GetVar(ecx, this_var);
3066 __ cmp(ecx, isolate()->factory()->the_hole_value());
3068 Label uninitialized_this;
3069 __ j(equal, &uninitialized_this);
3070 __ push(Immediate(this_var->name()));
3071 __ CallRuntime(Runtime::kThrowReferenceError, 1);
3072 __ bind(&uninitialized_this);
3074 EmitVariableAssignment(this_var, Token::INIT_CONST, slot);
3078 // See http://www.ecma-international.org/ecma-262/6.0/#sec-function-calls.
3079 void FullCodeGenerator::PushCalleeAndWithBaseObject(Call* expr) {
3080 VariableProxy* callee = expr->expression()->AsVariableProxy();
3081 if (callee->var()->IsLookupSlot()) {
3083 SetExpressionPosition(callee);
3084 // Generate code for loading from variables potentially shadowed by
3085 // eval-introduced variables.
3086 EmitDynamicLookupFastCase(callee, NOT_INSIDE_TYPEOF, &slow, &done);
3089 // Call the runtime to find the function to call (returned in eax) and
3090 // the object holding it (returned in edx).
3091 __ push(context_register());
3092 __ push(Immediate(callee->name()));
3093 __ CallRuntime(Runtime::kLoadLookupSlot, 2);
3094 __ push(eax); // Function.
3095 __ push(edx); // Receiver.
3096 PrepareForBailoutForId(expr->LookupId(), NO_REGISTERS);
3098 // If fast case code has been generated, emit code to push the function
3099 // and receiver and have the slow path jump around this code.
3100 if (done.is_linked()) {
3102 __ jmp(&call, Label::kNear);
3106 // The receiver is implicitly the global receiver. Indicate this by
3107 // passing the hole to the call function stub.
3108 __ push(Immediate(isolate()->factory()->undefined_value()));
3112 VisitForStackValue(callee);
3113 // refEnv.WithBaseObject()
3114 __ push(Immediate(isolate()->factory()->undefined_value()));
3119 void FullCodeGenerator::VisitCall(Call* expr) {
3121 // We want to verify that RecordJSReturnSite gets called on all paths
3122 // through this function. Avoid early returns.
3123 expr->return_is_recorded_ = false;
3126 Comment cmnt(masm_, "[ Call");
3127 Expression* callee = expr->expression();
3128 Call::CallType call_type = expr->GetCallType(isolate());
3130 if (call_type == Call::POSSIBLY_EVAL_CALL) {
3131 // In a call to eval, we first call RuntimeHidden_ResolvePossiblyDirectEval
3132 // to resolve the function we need to call. Then we call the resolved
3133 // function using the given arguments.
3134 ZoneList<Expression*>* args = expr->arguments();
3135 int arg_count = args->length();
3137 PushCalleeAndWithBaseObject(expr);
3139 // Push the arguments.
3140 for (int i = 0; i < arg_count; i++) {
3141 VisitForStackValue(args->at(i));
3144 // Push a copy of the function (found below the arguments) and
3146 __ push(Operand(esp, (arg_count + 1) * kPointerSize));
3147 EmitResolvePossiblyDirectEval(arg_count);
3149 // Touch up the stack with the resolved function.
3150 __ mov(Operand(esp, (arg_count + 1) * kPointerSize), eax);
3152 PrepareForBailoutForId(expr->EvalId(), NO_REGISTERS);
3154 SetCallPosition(expr, arg_count);
3155 CallFunctionStub stub(isolate(), arg_count, NO_CALL_FUNCTION_FLAGS);
3156 __ mov(edi, Operand(esp, (arg_count + 1) * kPointerSize));
3158 RecordJSReturnSite(expr);
3159 // Restore context register.
3160 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
3161 context()->DropAndPlug(1, eax);
3163 } else if (call_type == Call::GLOBAL_CALL) {
3164 EmitCallWithLoadIC(expr);
3165 } else if (call_type == Call::LOOKUP_SLOT_CALL) {
3166 // Call to a lookup slot (dynamically introduced variable).
3167 PushCalleeAndWithBaseObject(expr);
3169 } else if (call_type == Call::PROPERTY_CALL) {
3170 Property* property = callee->AsProperty();
3171 bool is_named_call = property->key()->IsPropertyName();
3172 if (property->IsSuperAccess()) {
3173 if (is_named_call) {
3174 EmitSuperCallWithLoadIC(expr);
3176 EmitKeyedSuperCallWithLoadIC(expr);
3179 VisitForStackValue(property->obj());
3180 if (is_named_call) {
3181 EmitCallWithLoadIC(expr);
3183 EmitKeyedCallWithLoadIC(expr, property->key());
3186 } else if (call_type == Call::SUPER_CALL) {
3187 EmitSuperConstructorCall(expr);
3189 DCHECK(call_type == Call::OTHER_CALL);
3190 // Call to an arbitrary expression not handled specially above.
3191 VisitForStackValue(callee);
3192 __ push(Immediate(isolate()->factory()->undefined_value()));
3193 // Emit function call.
3198 // RecordJSReturnSite should have been called.
3199 DCHECK(expr->return_is_recorded_);
3204 void FullCodeGenerator::VisitCallNew(CallNew* expr) {
3205 Comment cmnt(masm_, "[ CallNew");
3206 // According to ECMA-262, section 11.2.2, page 44, the function
3207 // expression in new calls must be evaluated before the
3210 // Push constructor on the stack. If it's not a function it's used as
3211 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
3213 DCHECK(!expr->expression()->IsSuperPropertyReference());
3214 VisitForStackValue(expr->expression());
3216 // Push the arguments ("left-to-right") on the stack.
3217 ZoneList<Expression*>* args = expr->arguments();
3218 int arg_count = args->length();
3219 for (int i = 0; i < arg_count; i++) {
3220 VisitForStackValue(args->at(i));
3223 // Call the construct call builtin that handles allocation and
3224 // constructor invocation.
3225 SetConstructCallPosition(expr);
3227 // Load function and argument count into edi and eax.
3228 __ Move(eax, Immediate(arg_count));
3229 __ mov(edi, Operand(esp, arg_count * kPointerSize));
3231 // Record call targets in unoptimized code.
3232 if (FLAG_pretenuring_call_new) {
3233 EnsureSlotContainsAllocationSite(expr->AllocationSiteFeedbackSlot());
3234 DCHECK(expr->AllocationSiteFeedbackSlot().ToInt() ==
3235 expr->CallNewFeedbackSlot().ToInt() + 1);
3238 __ LoadHeapObject(ebx, FeedbackVector());
3239 __ mov(edx, Immediate(SmiFromSlot(expr->CallNewFeedbackSlot())));
3241 CallConstructStub stub(isolate(), RECORD_CONSTRUCTOR_TARGET);
3242 __ call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
3243 PrepareForBailoutForId(expr->ReturnId(), TOS_REG);
3244 context()->Plug(eax);
3248 void FullCodeGenerator::EmitSuperConstructorCall(Call* expr) {
3249 SuperCallReference* super_call_ref =
3250 expr->expression()->AsSuperCallReference();
3251 DCHECK_NOT_NULL(super_call_ref);
3253 EmitLoadSuperConstructor(super_call_ref);
3254 __ push(result_register());
3256 // Push the arguments ("left-to-right") on the stack.
3257 ZoneList<Expression*>* args = expr->arguments();
3258 int arg_count = args->length();
3259 for (int i = 0; i < arg_count; i++) {
3260 VisitForStackValue(args->at(i));
3263 // Call the construct call builtin that handles allocation and
3264 // constructor invocation.
3265 SetConstructCallPosition(expr);
3267 // Load original constructor into ecx.
3268 VisitForAccumulatorValue(super_call_ref->new_target_var());
3269 __ mov(ecx, result_register());
3271 // Load function and argument count into edi and eax.
3272 __ Move(eax, Immediate(arg_count));
3273 __ mov(edi, Operand(esp, arg_count * kPointerSize));
3275 // Record call targets in unoptimized code.
3276 if (FLAG_pretenuring_call_new) {
3278 /* TODO(dslomov): support pretenuring.
3279 EnsureSlotContainsAllocationSite(expr->AllocationSiteFeedbackSlot());
3280 DCHECK(expr->AllocationSiteFeedbackSlot().ToInt() ==
3281 expr->CallNewFeedbackSlot().ToInt() + 1);
3285 __ LoadHeapObject(ebx, FeedbackVector());
3286 __ mov(edx, Immediate(SmiFromSlot(expr->CallFeedbackSlot())));
3288 CallConstructStub stub(isolate(), SUPER_CALL_RECORD_TARGET);
3289 __ call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
3291 RecordJSReturnSite(expr);
3293 EmitInitializeThisAfterSuper(super_call_ref, expr->CallFeedbackICSlot());
3294 context()->Plug(eax);
3298 void FullCodeGenerator::EmitIsSmi(CallRuntime* expr) {
3299 ZoneList<Expression*>* args = expr->arguments();
3300 DCHECK(args->length() == 1);
3302 VisitForAccumulatorValue(args->at(0));
3304 Label materialize_true, materialize_false;
3305 Label* if_true = NULL;
3306 Label* if_false = NULL;
3307 Label* fall_through = NULL;
3308 context()->PrepareTest(&materialize_true, &materialize_false,
3309 &if_true, &if_false, &fall_through);
3311 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3312 __ test(eax, Immediate(kSmiTagMask));
3313 Split(zero, if_true, if_false, fall_through);
3315 context()->Plug(if_true, if_false);
3319 void FullCodeGenerator::EmitIsNonNegativeSmi(CallRuntime* expr) {
3320 ZoneList<Expression*>* args = expr->arguments();
3321 DCHECK(args->length() == 1);
3323 VisitForAccumulatorValue(args->at(0));
3325 Label materialize_true, materialize_false;
3326 Label* if_true = NULL;
3327 Label* if_false = NULL;
3328 Label* fall_through = NULL;
3329 context()->PrepareTest(&materialize_true, &materialize_false,
3330 &if_true, &if_false, &fall_through);
3332 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3333 __ test(eax, Immediate(kSmiTagMask | 0x80000000));
3334 Split(zero, if_true, if_false, fall_through);
3336 context()->Plug(if_true, if_false);
3340 void FullCodeGenerator::EmitIsObject(CallRuntime* expr) {
3341 ZoneList<Expression*>* args = expr->arguments();
3342 DCHECK(args->length() == 1);
3344 VisitForAccumulatorValue(args->at(0));
3346 Label materialize_true, materialize_false;
3347 Label* if_true = NULL;
3348 Label* if_false = NULL;
3349 Label* fall_through = NULL;
3350 context()->PrepareTest(&materialize_true, &materialize_false,
3351 &if_true, &if_false, &fall_through);
3353 __ JumpIfSmi(eax, if_false);
3354 __ cmp(eax, isolate()->factory()->null_value());
3355 __ j(equal, if_true);
3356 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
3357 // Undetectable objects behave like undefined when tested with typeof.
3358 __ movzx_b(ecx, FieldOperand(ebx, Map::kBitFieldOffset));
3359 __ test(ecx, Immediate(1 << Map::kIsUndetectable));
3360 __ j(not_zero, if_false);
3361 __ movzx_b(ecx, FieldOperand(ebx, Map::kInstanceTypeOffset));
3362 __ cmp(ecx, FIRST_NONCALLABLE_SPEC_OBJECT_TYPE);
3363 __ j(below, if_false);
3364 __ cmp(ecx, LAST_NONCALLABLE_SPEC_OBJECT_TYPE);
3365 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3366 Split(below_equal, if_true, if_false, fall_through);
3368 context()->Plug(if_true, if_false);
3372 void FullCodeGenerator::EmitIsSpecObject(CallRuntime* expr) {
3373 ZoneList<Expression*>* args = expr->arguments();
3374 DCHECK(args->length() == 1);
3376 VisitForAccumulatorValue(args->at(0));
3378 Label materialize_true, materialize_false;
3379 Label* if_true = NULL;
3380 Label* if_false = NULL;
3381 Label* fall_through = NULL;
3382 context()->PrepareTest(&materialize_true, &materialize_false,
3383 &if_true, &if_false, &fall_through);
3385 __ JumpIfSmi(eax, if_false);
3386 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ebx);
3387 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3388 Split(above_equal, if_true, if_false, fall_through);
3390 context()->Plug(if_true, if_false);
3394 void FullCodeGenerator::EmitIsUndetectableObject(CallRuntime* expr) {
3395 ZoneList<Expression*>* args = expr->arguments();
3396 DCHECK(args->length() == 1);
3398 VisitForAccumulatorValue(args->at(0));
3400 Label materialize_true, materialize_false;
3401 Label* if_true = NULL;
3402 Label* if_false = NULL;
3403 Label* fall_through = NULL;
3404 context()->PrepareTest(&materialize_true, &materialize_false,
3405 &if_true, &if_false, &fall_through);
3407 __ JumpIfSmi(eax, if_false);
3408 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
3409 __ movzx_b(ebx, FieldOperand(ebx, Map::kBitFieldOffset));
3410 __ test(ebx, Immediate(1 << Map::kIsUndetectable));
3411 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3412 Split(not_zero, if_true, if_false, fall_through);
3414 context()->Plug(if_true, if_false);
3418 void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
3419 CallRuntime* expr) {
3420 ZoneList<Expression*>* args = expr->arguments();
3421 DCHECK(args->length() == 1);
3423 VisitForAccumulatorValue(args->at(0));
3425 Label materialize_true, materialize_false, skip_lookup;
3426 Label* if_true = NULL;
3427 Label* if_false = NULL;
3428 Label* fall_through = NULL;
3429 context()->PrepareTest(&materialize_true, &materialize_false,
3430 &if_true, &if_false, &fall_through);
3432 __ AssertNotSmi(eax);
3434 // Check whether this map has already been checked to be safe for default
3436 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
3437 __ test_b(FieldOperand(ebx, Map::kBitField2Offset),
3438 1 << Map::kStringWrapperSafeForDefaultValueOf);
3439 __ j(not_zero, &skip_lookup);
3441 // Check for fast case object. Return false for slow case objects.
3442 __ mov(ecx, FieldOperand(eax, JSObject::kPropertiesOffset));
3443 __ mov(ecx, FieldOperand(ecx, HeapObject::kMapOffset));
3444 __ cmp(ecx, isolate()->factory()->hash_table_map());
3445 __ j(equal, if_false);
3447 // Look for valueOf string in the descriptor array, and indicate false if
3448 // found. Since we omit an enumeration index check, if it is added via a
3449 // transition that shares its descriptor array, this is a false positive.
3450 Label entry, loop, done;
3452 // Skip loop if no descriptors are valid.
3453 __ NumberOfOwnDescriptors(ecx, ebx);
3457 __ LoadInstanceDescriptors(ebx, ebx);
3458 // ebx: descriptor array.
3459 // ecx: valid entries in the descriptor array.
3460 // Calculate the end of the descriptor array.
3461 STATIC_ASSERT(kSmiTag == 0);
3462 STATIC_ASSERT(kSmiTagSize == 1);
3463 STATIC_ASSERT(kPointerSize == 4);
3464 __ imul(ecx, ecx, DescriptorArray::kDescriptorSize);
3465 __ lea(ecx, Operand(ebx, ecx, times_4, DescriptorArray::kFirstOffset));
3466 // Calculate location of the first key name.
3467 __ add(ebx, Immediate(DescriptorArray::kFirstOffset));
3468 // Loop through all the keys in the descriptor array. If one of these is the
3469 // internalized string "valueOf" the result is false.
3472 __ mov(edx, FieldOperand(ebx, 0));
3473 __ cmp(edx, isolate()->factory()->value_of_string());
3474 __ j(equal, if_false);
3475 __ add(ebx, Immediate(DescriptorArray::kDescriptorSize * kPointerSize));
3478 __ j(not_equal, &loop);
3482 // Reload map as register ebx was used as temporary above.
3483 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
3485 // Set the bit in the map to indicate that there is no local valueOf field.
3486 __ or_(FieldOperand(ebx, Map::kBitField2Offset),
3487 Immediate(1 << Map::kStringWrapperSafeForDefaultValueOf));
3489 __ bind(&skip_lookup);
3491 // If a valueOf property is not found on the object check that its
3492 // prototype is the un-modified String prototype. If not result is false.
3493 __ mov(ecx, FieldOperand(ebx, Map::kPrototypeOffset));
3494 __ JumpIfSmi(ecx, if_false);
3495 __ mov(ecx, FieldOperand(ecx, HeapObject::kMapOffset));
3496 __ mov(edx, Operand(esi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
3498 FieldOperand(edx, GlobalObject::kNativeContextOffset));
3501 Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
3502 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3503 Split(equal, if_true, if_false, fall_through);
3505 context()->Plug(if_true, if_false);
3509 void FullCodeGenerator::EmitIsFunction(CallRuntime* expr) {
3510 ZoneList<Expression*>* args = expr->arguments();
3511 DCHECK(args->length() == 1);
3513 VisitForAccumulatorValue(args->at(0));
3515 Label materialize_true, materialize_false;
3516 Label* if_true = NULL;
3517 Label* if_false = NULL;
3518 Label* fall_through = NULL;
3519 context()->PrepareTest(&materialize_true, &materialize_false,
3520 &if_true, &if_false, &fall_through);
3522 __ JumpIfSmi(eax, if_false);
3523 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
3524 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3525 Split(equal, if_true, if_false, fall_through);
3527 context()->Plug(if_true, if_false);
3531 void FullCodeGenerator::EmitIsMinusZero(CallRuntime* expr) {
3532 ZoneList<Expression*>* args = expr->arguments();
3533 DCHECK(args->length() == 1);
3535 VisitForAccumulatorValue(args->at(0));
3537 Label materialize_true, materialize_false;
3538 Label* if_true = NULL;
3539 Label* if_false = NULL;
3540 Label* fall_through = NULL;
3541 context()->PrepareTest(&materialize_true, &materialize_false,
3542 &if_true, &if_false, &fall_through);
3544 Handle<Map> map = masm()->isolate()->factory()->heap_number_map();
3545 __ CheckMap(eax, map, if_false, DO_SMI_CHECK);
3546 // Check if the exponent half is 0x80000000. Comparing against 1 and
3547 // checking for overflow is the shortest possible encoding.
3548 __ cmp(FieldOperand(eax, HeapNumber::kExponentOffset), Immediate(0x1));
3549 __ j(no_overflow, if_false);
3550 __ cmp(FieldOperand(eax, HeapNumber::kMantissaOffset), Immediate(0x0));
3551 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3552 Split(equal, if_true, if_false, fall_through);
3554 context()->Plug(if_true, if_false);
3558 void FullCodeGenerator::EmitIsArray(CallRuntime* expr) {
3559 ZoneList<Expression*>* args = expr->arguments();
3560 DCHECK(args->length() == 1);
3562 VisitForAccumulatorValue(args->at(0));
3564 Label materialize_true, materialize_false;
3565 Label* if_true = NULL;
3566 Label* if_false = NULL;
3567 Label* fall_through = NULL;
3568 context()->PrepareTest(&materialize_true, &materialize_false,
3569 &if_true, &if_false, &fall_through);
3571 __ JumpIfSmi(eax, if_false);
3572 __ CmpObjectType(eax, JS_ARRAY_TYPE, ebx);
3573 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3574 Split(equal, if_true, if_false, fall_through);
3576 context()->Plug(if_true, if_false);
3580 void FullCodeGenerator::EmitIsTypedArray(CallRuntime* expr) {
3581 ZoneList<Expression*>* args = expr->arguments();
3582 DCHECK(args->length() == 1);
3584 VisitForAccumulatorValue(args->at(0));
3586 Label materialize_true, materialize_false;
3587 Label* if_true = NULL;
3588 Label* if_false = NULL;
3589 Label* fall_through = NULL;
3590 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3591 &if_false, &fall_through);
3593 __ JumpIfSmi(eax, if_false);
3594 __ CmpObjectType(eax, JS_TYPED_ARRAY_TYPE, ebx);
3595 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3596 Split(equal, if_true, if_false, fall_through);
3598 context()->Plug(if_true, if_false);
3602 void FullCodeGenerator::EmitIsRegExp(CallRuntime* expr) {
3603 ZoneList<Expression*>* args = expr->arguments();
3604 DCHECK(args->length() == 1);
3606 VisitForAccumulatorValue(args->at(0));
3608 Label materialize_true, materialize_false;
3609 Label* if_true = NULL;
3610 Label* if_false = NULL;
3611 Label* fall_through = NULL;
3612 context()->PrepareTest(&materialize_true, &materialize_false,
3613 &if_true, &if_false, &fall_through);
3615 __ JumpIfSmi(eax, if_false);
3616 __ CmpObjectType(eax, JS_REGEXP_TYPE, ebx);
3617 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3618 Split(equal, if_true, if_false, fall_through);
3620 context()->Plug(if_true, if_false);
3624 void FullCodeGenerator::EmitIsJSProxy(CallRuntime* expr) {
3625 ZoneList<Expression*>* args = expr->arguments();
3626 DCHECK(args->length() == 1);
3628 VisitForAccumulatorValue(args->at(0));
3630 Label materialize_true, materialize_false;
3631 Label* if_true = NULL;
3632 Label* if_false = NULL;
3633 Label* fall_through = NULL;
3634 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3635 &if_false, &fall_through);
3637 __ JumpIfSmi(eax, if_false);
3639 __ mov(map, FieldOperand(eax, HeapObject::kMapOffset));
3640 __ CmpInstanceType(map, FIRST_JS_PROXY_TYPE);
3641 __ j(less, if_false);
3642 __ CmpInstanceType(map, LAST_JS_PROXY_TYPE);
3643 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3644 Split(less_equal, if_true, if_false, fall_through);
3646 context()->Plug(if_true, if_false);
3650 void FullCodeGenerator::EmitIsConstructCall(CallRuntime* expr) {
3651 DCHECK(expr->arguments()->length() == 0);
3653 Label materialize_true, materialize_false;
3654 Label* if_true = NULL;
3655 Label* if_false = NULL;
3656 Label* fall_through = NULL;
3657 context()->PrepareTest(&materialize_true, &materialize_false,
3658 &if_true, &if_false, &fall_through);
3660 // Get the frame pointer for the calling frame.
3661 __ mov(eax, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
3663 // Skip the arguments adaptor frame if it exists.
3664 Label check_frame_marker;
3665 __ cmp(Operand(eax, StandardFrameConstants::kContextOffset),
3666 Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3667 __ j(not_equal, &check_frame_marker);
3668 __ mov(eax, Operand(eax, StandardFrameConstants::kCallerFPOffset));
3670 // Check the marker in the calling frame.
3671 __ bind(&check_frame_marker);
3672 __ cmp(Operand(eax, StandardFrameConstants::kMarkerOffset),
3673 Immediate(Smi::FromInt(StackFrame::CONSTRUCT)));
3674 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3675 Split(equal, if_true, if_false, fall_through);
3677 context()->Plug(if_true, if_false);
3681 void FullCodeGenerator::EmitObjectEquals(CallRuntime* expr) {
3682 ZoneList<Expression*>* args = expr->arguments();
3683 DCHECK(args->length() == 2);
3685 // Load the two objects into registers and perform the comparison.
3686 VisitForStackValue(args->at(0));
3687 VisitForAccumulatorValue(args->at(1));
3689 Label materialize_true, materialize_false;
3690 Label* if_true = NULL;
3691 Label* if_false = NULL;
3692 Label* fall_through = NULL;
3693 context()->PrepareTest(&materialize_true, &materialize_false,
3694 &if_true, &if_false, &fall_through);
3698 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3699 Split(equal, if_true, if_false, fall_through);
3701 context()->Plug(if_true, if_false);
3705 void FullCodeGenerator::EmitArguments(CallRuntime* expr) {
3706 ZoneList<Expression*>* args = expr->arguments();
3707 DCHECK(args->length() == 1);
3709 // ArgumentsAccessStub expects the key in edx and the formal
3710 // parameter count in eax.
3711 VisitForAccumulatorValue(args->at(0));
3713 __ Move(eax, Immediate(Smi::FromInt(info_->scope()->num_parameters())));
3714 ArgumentsAccessStub stub(isolate(), ArgumentsAccessStub::READ_ELEMENT);
3716 context()->Plug(eax);
3720 void FullCodeGenerator::EmitArgumentsLength(CallRuntime* expr) {
3721 DCHECK(expr->arguments()->length() == 0);
3724 // Get the number of formal parameters.
3725 __ Move(eax, Immediate(Smi::FromInt(info_->scope()->num_parameters())));
3727 // Check if the calling frame is an arguments adaptor frame.
3728 __ mov(ebx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
3729 __ cmp(Operand(ebx, StandardFrameConstants::kContextOffset),
3730 Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3731 __ j(not_equal, &exit);
3733 // Arguments adaptor case: Read the arguments length from the
3735 __ mov(eax, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
3739 context()->Plug(eax);
3743 void FullCodeGenerator::EmitClassOf(CallRuntime* expr) {
3744 ZoneList<Expression*>* args = expr->arguments();
3745 DCHECK(args->length() == 1);
3746 Label done, null, function, non_function_constructor;
3748 VisitForAccumulatorValue(args->at(0));
3750 // If the object is a smi, we return null.
3751 __ JumpIfSmi(eax, &null);
3753 // Check that the object is a JS object but take special care of JS
3754 // functions to make sure they have 'Function' as their class.
3755 // Assume that there are only two callable types, and one of them is at
3756 // either end of the type range for JS object types. Saves extra comparisons.
3757 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
3758 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, eax);
3759 // Map is now in eax.
3761 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
3762 FIRST_SPEC_OBJECT_TYPE + 1);
3763 __ j(equal, &function);
3765 __ CmpInstanceType(eax, LAST_SPEC_OBJECT_TYPE);
3766 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
3767 LAST_SPEC_OBJECT_TYPE - 1);
3768 __ j(equal, &function);
3769 // Assume that there is no larger type.
3770 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE == LAST_TYPE - 1);
3772 // Check if the constructor in the map is a JS function.
3773 __ GetMapConstructor(eax, eax, ebx);
3774 __ CmpInstanceType(ebx, JS_FUNCTION_TYPE);
3775 __ j(not_equal, &non_function_constructor);
3777 // eax now contains the constructor function. Grab the
3778 // instance class name from there.
3779 __ mov(eax, FieldOperand(eax, JSFunction::kSharedFunctionInfoOffset));
3780 __ mov(eax, FieldOperand(eax, SharedFunctionInfo::kInstanceClassNameOffset));
3783 // Functions have class 'Function'.
3785 __ mov(eax, isolate()->factory()->Function_string());
3788 // Objects with a non-function constructor have class 'Object'.
3789 __ bind(&non_function_constructor);
3790 __ mov(eax, isolate()->factory()->Object_string());
3793 // Non-JS objects have class null.
3795 __ mov(eax, isolate()->factory()->null_value());
3800 context()->Plug(eax);
3804 void FullCodeGenerator::EmitSubString(CallRuntime* expr) {
3805 // Load the arguments on the stack and call the stub.
3806 SubStringStub stub(isolate());
3807 ZoneList<Expression*>* args = expr->arguments();
3808 DCHECK(args->length() == 3);
3809 VisitForStackValue(args->at(0));
3810 VisitForStackValue(args->at(1));
3811 VisitForStackValue(args->at(2));
3813 context()->Plug(eax);
3817 void FullCodeGenerator::EmitRegExpExec(CallRuntime* expr) {
3818 // Load the arguments on the stack and call the stub.
3819 RegExpExecStub stub(isolate());
3820 ZoneList<Expression*>* args = expr->arguments();
3821 DCHECK(args->length() == 4);
3822 VisitForStackValue(args->at(0));
3823 VisitForStackValue(args->at(1));
3824 VisitForStackValue(args->at(2));
3825 VisitForStackValue(args->at(3));
3827 context()->Plug(eax);
3831 void FullCodeGenerator::EmitValueOf(CallRuntime* expr) {
3832 ZoneList<Expression*>* args = expr->arguments();
3833 DCHECK(args->length() == 1);
3835 VisitForAccumulatorValue(args->at(0)); // Load the object.
3838 // If the object is a smi return the object.
3839 __ JumpIfSmi(eax, &done, Label::kNear);
3840 // If the object is not a value type, return the object.
3841 __ CmpObjectType(eax, JS_VALUE_TYPE, ebx);
3842 __ j(not_equal, &done, Label::kNear);
3843 __ mov(eax, FieldOperand(eax, JSValue::kValueOffset));
3846 context()->Plug(eax);
3850 void FullCodeGenerator::EmitIsDate(CallRuntime* expr) {
3851 ZoneList<Expression*>* args = expr->arguments();
3852 DCHECK_EQ(1, args->length());
3854 VisitForAccumulatorValue(args->at(0));
3856 Label materialize_true, materialize_false;
3857 Label* if_true = nullptr;
3858 Label* if_false = nullptr;
3859 Label* fall_through = nullptr;
3860 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3861 &if_false, &fall_through);
3863 __ JumpIfSmi(eax, if_false);
3864 __ CmpObjectType(eax, JS_DATE_TYPE, ebx);
3865 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3866 Split(equal, if_true, if_false, fall_through);
3868 context()->Plug(if_true, if_false);
3872 void FullCodeGenerator::EmitDateField(CallRuntime* expr) {
3873 ZoneList<Expression*>* args = expr->arguments();
3874 DCHECK(args->length() == 2);
3875 DCHECK_NOT_NULL(args->at(1)->AsLiteral());
3876 Smi* index = Smi::cast(*(args->at(1)->AsLiteral()->value()));
3878 VisitForAccumulatorValue(args->at(0)); // Load the object.
3880 Register object = eax;
3881 Register result = eax;
3882 Register scratch = ecx;
3884 if (index->value() == 0) {
3885 __ mov(result, FieldOperand(object, JSDate::kValueOffset));
3887 Label runtime, done;
3888 if (index->value() < JSDate::kFirstUncachedField) {
3889 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
3890 __ mov(scratch, Operand::StaticVariable(stamp));
3891 __ cmp(scratch, FieldOperand(object, JSDate::kCacheStampOffset));
3892 __ j(not_equal, &runtime, Label::kNear);
3893 __ mov(result, FieldOperand(object, JSDate::kValueOffset +
3894 kPointerSize * index->value()));
3895 __ jmp(&done, Label::kNear);
3898 __ PrepareCallCFunction(2, scratch);
3899 __ mov(Operand(esp, 0), object);
3900 __ mov(Operand(esp, 1 * kPointerSize), Immediate(index));
3901 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
3905 context()->Plug(result);
3909 void FullCodeGenerator::EmitOneByteSeqStringSetChar(CallRuntime* expr) {
3910 ZoneList<Expression*>* args = expr->arguments();
3911 DCHECK_EQ(3, args->length());
3913 Register string = eax;
3914 Register index = ebx;
3915 Register value = ecx;
3917 VisitForStackValue(args->at(0)); // index
3918 VisitForStackValue(args->at(1)); // value
3919 VisitForAccumulatorValue(args->at(2)); // string
3924 if (FLAG_debug_code) {
3925 __ test(value, Immediate(kSmiTagMask));
3926 __ Check(zero, kNonSmiValue);
3927 __ test(index, Immediate(kSmiTagMask));
3928 __ Check(zero, kNonSmiValue);
3934 if (FLAG_debug_code) {
3935 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
3936 __ EmitSeqStringSetCharCheck(string, index, value, one_byte_seq_type);
3939 __ mov_b(FieldOperand(string, index, times_1, SeqOneByteString::kHeaderSize),
3941 context()->Plug(string);
3945 void FullCodeGenerator::EmitTwoByteSeqStringSetChar(CallRuntime* expr) {
3946 ZoneList<Expression*>* args = expr->arguments();
3947 DCHECK_EQ(3, args->length());
3949 Register string = eax;
3950 Register index = ebx;
3951 Register value = ecx;
3953 VisitForStackValue(args->at(0)); // index
3954 VisitForStackValue(args->at(1)); // value
3955 VisitForAccumulatorValue(args->at(2)); // string
3959 if (FLAG_debug_code) {
3960 __ test(value, Immediate(kSmiTagMask));
3961 __ Check(zero, kNonSmiValue);
3962 __ test(index, Immediate(kSmiTagMask));
3963 __ Check(zero, kNonSmiValue);
3965 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
3966 __ EmitSeqStringSetCharCheck(string, index, value, two_byte_seq_type);
3971 // No need to untag a smi for two-byte addressing.
3972 __ mov_w(FieldOperand(string, index, times_1, SeqTwoByteString::kHeaderSize),
3974 context()->Plug(string);
3978 void FullCodeGenerator::EmitMathPow(CallRuntime* expr) {
3979 // Load the arguments on the stack and call the runtime function.
3980 ZoneList<Expression*>* args = expr->arguments();
3981 DCHECK(args->length() == 2);
3982 VisitForStackValue(args->at(0));
3983 VisitForStackValue(args->at(1));
3985 __ CallRuntime(Runtime::kMathPowSlow, 2);
3986 context()->Plug(eax);
3990 void FullCodeGenerator::EmitSetValueOf(CallRuntime* expr) {
3991 ZoneList<Expression*>* args = expr->arguments();
3992 DCHECK(args->length() == 2);
3994 VisitForStackValue(args->at(0)); // Load the object.
3995 VisitForAccumulatorValue(args->at(1)); // Load the value.
3996 __ pop(ebx); // eax = value. ebx = object.
3999 // If the object is a smi, return the value.
4000 __ JumpIfSmi(ebx, &done, Label::kNear);
4002 // If the object is not a value type, return the value.
4003 __ CmpObjectType(ebx, JS_VALUE_TYPE, ecx);
4004 __ j(not_equal, &done, Label::kNear);
4007 __ mov(FieldOperand(ebx, JSValue::kValueOffset), eax);
4009 // Update the write barrier. Save the value as it will be
4010 // overwritten by the write barrier code and is needed afterward.
4012 __ RecordWriteField(ebx, JSValue::kValueOffset, edx, ecx, kDontSaveFPRegs);
4015 context()->Plug(eax);
4019 void FullCodeGenerator::EmitNumberToString(CallRuntime* expr) {
4020 ZoneList<Expression*>* args = expr->arguments();
4021 DCHECK_EQ(args->length(), 1);
4023 // Load the argument into eax and call the stub.
4024 VisitForAccumulatorValue(args->at(0));
4026 NumberToStringStub stub(isolate());
4028 context()->Plug(eax);
4032 void FullCodeGenerator::EmitStringCharFromCode(CallRuntime* expr) {
4033 ZoneList<Expression*>* args = expr->arguments();
4034 DCHECK(args->length() == 1);
4036 VisitForAccumulatorValue(args->at(0));
4039 StringCharFromCodeGenerator generator(eax, ebx);
4040 generator.GenerateFast(masm_);
4043 NopRuntimeCallHelper call_helper;
4044 generator.GenerateSlow(masm_, call_helper);
4047 context()->Plug(ebx);
4051 void FullCodeGenerator::EmitStringCharCodeAt(CallRuntime* expr) {
4052 ZoneList<Expression*>* args = expr->arguments();
4053 DCHECK(args->length() == 2);
4055 VisitForStackValue(args->at(0));
4056 VisitForAccumulatorValue(args->at(1));
4058 Register object = ebx;
4059 Register index = eax;
4060 Register result = edx;
4064 Label need_conversion;
4065 Label index_out_of_range;
4067 StringCharCodeAtGenerator generator(object,
4072 &index_out_of_range,
4073 STRING_INDEX_IS_NUMBER);
4074 generator.GenerateFast(masm_);
4077 __ bind(&index_out_of_range);
4078 // When the index is out of range, the spec requires us to return
4080 __ Move(result, Immediate(isolate()->factory()->nan_value()));
4083 __ bind(&need_conversion);
4084 // Move the undefined value into the result register, which will
4085 // trigger conversion.
4086 __ Move(result, Immediate(isolate()->factory()->undefined_value()));
4089 NopRuntimeCallHelper call_helper;
4090 generator.GenerateSlow(masm_, NOT_PART_OF_IC_HANDLER, call_helper);
4093 context()->Plug(result);
4097 void FullCodeGenerator::EmitStringCharAt(CallRuntime* expr) {
4098 ZoneList<Expression*>* args = expr->arguments();
4099 DCHECK(args->length() == 2);
4101 VisitForStackValue(args->at(0));
4102 VisitForAccumulatorValue(args->at(1));
4104 Register object = ebx;
4105 Register index = eax;
4106 Register scratch = edx;
4107 Register result = eax;
4111 Label need_conversion;
4112 Label index_out_of_range;
4114 StringCharAtGenerator generator(object,
4120 &index_out_of_range,
4121 STRING_INDEX_IS_NUMBER);
4122 generator.GenerateFast(masm_);
4125 __ bind(&index_out_of_range);
4126 // When the index is out of range, the spec requires us to return
4127 // the empty string.
4128 __ Move(result, Immediate(isolate()->factory()->empty_string()));
4131 __ bind(&need_conversion);
4132 // Move smi zero into the result register, which will trigger
4134 __ Move(result, Immediate(Smi::FromInt(0)));
4137 NopRuntimeCallHelper call_helper;
4138 generator.GenerateSlow(masm_, NOT_PART_OF_IC_HANDLER, call_helper);
4141 context()->Plug(result);
4145 void FullCodeGenerator::EmitStringAdd(CallRuntime* expr) {
4146 ZoneList<Expression*>* args = expr->arguments();
4147 DCHECK_EQ(2, args->length());
4148 VisitForStackValue(args->at(0));
4149 VisitForAccumulatorValue(args->at(1));
4152 StringAddStub stub(isolate(), STRING_ADD_CHECK_BOTH, NOT_TENURED);
4154 context()->Plug(eax);
4158 void FullCodeGenerator::EmitStringCompare(CallRuntime* expr) {
4159 ZoneList<Expression*>* args = expr->arguments();
4160 DCHECK_EQ(2, args->length());
4162 VisitForStackValue(args->at(0));
4163 VisitForStackValue(args->at(1));
4165 StringCompareStub stub(isolate());
4167 context()->Plug(eax);
4171 void FullCodeGenerator::EmitCallFunction(CallRuntime* expr) {
4172 ZoneList<Expression*>* args = expr->arguments();
4173 DCHECK(args->length() >= 2);
4175 int arg_count = args->length() - 2; // 2 ~ receiver and function.
4176 for (int i = 0; i < arg_count + 1; ++i) {
4177 VisitForStackValue(args->at(i));
4179 VisitForAccumulatorValue(args->last()); // Function.
4181 Label runtime, done;
4182 // Check for non-function argument (including proxy).
4183 __ JumpIfSmi(eax, &runtime);
4184 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
4185 __ j(not_equal, &runtime);
4187 // InvokeFunction requires the function in edi. Move it in there.
4188 __ mov(edi, result_register());
4189 ParameterCount count(arg_count);
4190 __ InvokeFunction(edi, count, CALL_FUNCTION, NullCallWrapper());
4191 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
4196 __ CallRuntime(Runtime::kCall, args->length());
4199 context()->Plug(eax);
4203 void FullCodeGenerator::EmitDefaultConstructorCallSuper(CallRuntime* expr) {
4204 ZoneList<Expression*>* args = expr->arguments();
4205 DCHECK(args->length() == 2);
4208 VisitForStackValue(args->at(0));
4211 VisitForStackValue(args->at(1));
4212 __ CallRuntime(Runtime::kGetPrototype, 1);
4213 __ push(result_register());
4215 // Load original constructor into ecx.
4216 __ mov(ecx, Operand(esp, 1 * kPointerSize));
4218 // Check if the calling frame is an arguments adaptor frame.
4219 Label adaptor_frame, args_set_up, runtime;
4220 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
4221 __ mov(ebx, Operand(edx, StandardFrameConstants::kContextOffset));
4222 __ cmp(ebx, Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4223 __ j(equal, &adaptor_frame);
4224 // default constructor has no arguments, so no adaptor frame means no args.
4225 __ mov(eax, Immediate(0));
4226 __ jmp(&args_set_up);
4228 // Copy arguments from adaptor frame.
4230 __ bind(&adaptor_frame);
4231 __ mov(ebx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
4235 __ lea(edx, Operand(edx, ebx, times_pointer_size,
4236 StandardFrameConstants::kCallerSPOffset));
4239 __ push(Operand(edx, -1 * kPointerSize));
4240 __ sub(edx, Immediate(kPointerSize));
4242 __ j(not_zero, &loop);
4245 __ bind(&args_set_up);
4247 __ mov(edi, Operand(esp, eax, times_pointer_size, 0));
4248 __ mov(ebx, Immediate(isolate()->factory()->undefined_value()));
4249 CallConstructStub stub(isolate(), SUPER_CONSTRUCTOR_CALL);
4250 __ call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
4254 context()->Plug(eax);
4258 void FullCodeGenerator::EmitRegExpConstructResult(CallRuntime* expr) {
4259 // Load the arguments on the stack and call the stub.
4260 RegExpConstructResultStub stub(isolate());
4261 ZoneList<Expression*>* args = expr->arguments();
4262 DCHECK(args->length() == 3);
4263 VisitForStackValue(args->at(0));
4264 VisitForStackValue(args->at(1));
4265 VisitForAccumulatorValue(args->at(2));
4269 context()->Plug(eax);
4273 void FullCodeGenerator::EmitGetFromCache(CallRuntime* expr) {
4274 ZoneList<Expression*>* args = expr->arguments();
4275 DCHECK_EQ(2, args->length());
4277 DCHECK_NOT_NULL(args->at(0)->AsLiteral());
4278 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->value()))->value();
4280 Handle<FixedArray> jsfunction_result_caches(
4281 isolate()->native_context()->jsfunction_result_caches());
4282 if (jsfunction_result_caches->length() <= cache_id) {
4283 __ Abort(kAttemptToUseUndefinedCache);
4284 __ mov(eax, isolate()->factory()->undefined_value());
4285 context()->Plug(eax);
4289 VisitForAccumulatorValue(args->at(1));
4292 Register cache = ebx;
4294 __ mov(cache, ContextOperand(esi, Context::GLOBAL_OBJECT_INDEX));
4296 FieldOperand(cache, GlobalObject::kNativeContextOffset));
4297 __ mov(cache, ContextOperand(cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
4299 FieldOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
4301 Label done, not_found;
4302 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
4303 __ mov(tmp, FieldOperand(cache, JSFunctionResultCache::kFingerOffset));
4304 // tmp now holds finger offset as a smi.
4305 __ cmp(key, FixedArrayElementOperand(cache, tmp));
4306 __ j(not_equal, ¬_found);
4308 __ mov(eax, FixedArrayElementOperand(cache, tmp, 1));
4311 __ bind(¬_found);
4312 // Call runtime to perform the lookup.
4315 __ CallRuntime(Runtime::kGetFromCacheRT, 2);
4318 context()->Plug(eax);
4322 void FullCodeGenerator::EmitHasCachedArrayIndex(CallRuntime* expr) {
4323 ZoneList<Expression*>* args = expr->arguments();
4324 DCHECK(args->length() == 1);
4326 VisitForAccumulatorValue(args->at(0));
4328 __ AssertString(eax);
4330 Label materialize_true, materialize_false;
4331 Label* if_true = NULL;
4332 Label* if_false = NULL;
4333 Label* fall_through = NULL;
4334 context()->PrepareTest(&materialize_true, &materialize_false,
4335 &if_true, &if_false, &fall_through);
4337 __ test(FieldOperand(eax, String::kHashFieldOffset),
4338 Immediate(String::kContainsCachedArrayIndexMask));
4339 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
4340 Split(zero, if_true, if_false, fall_through);
4342 context()->Plug(if_true, if_false);
4346 void FullCodeGenerator::EmitGetCachedArrayIndex(CallRuntime* expr) {
4347 ZoneList<Expression*>* args = expr->arguments();
4348 DCHECK(args->length() == 1);
4349 VisitForAccumulatorValue(args->at(0));
4351 __ AssertString(eax);
4353 __ mov(eax, FieldOperand(eax, String::kHashFieldOffset));
4354 __ IndexFromHash(eax, eax);
4356 context()->Plug(eax);
4360 void FullCodeGenerator::EmitFastOneByteArrayJoin(CallRuntime* expr) {
4361 Label bailout, done, one_char_separator, long_separator,
4362 non_trivial_array, not_size_one_array, loop,
4363 loop_1, loop_1_condition, loop_2, loop_2_entry, loop_3, loop_3_entry;
4365 ZoneList<Expression*>* args = expr->arguments();
4366 DCHECK(args->length() == 2);
4367 // We will leave the separator on the stack until the end of the function.
4368 VisitForStackValue(args->at(1));
4369 // Load this to eax (= array)
4370 VisitForAccumulatorValue(args->at(0));
4371 // All aliases of the same register have disjoint lifetimes.
4372 Register array = eax;
4373 Register elements = no_reg; // Will be eax.
4375 Register index = edx;
4377 Register string_length = ecx;
4379 Register string = esi;
4381 Register scratch = ebx;
4383 Register array_length = edi;
4384 Register result_pos = no_reg; // Will be edi.
4386 // Separator operand is already pushed.
4387 Operand separator_operand = Operand(esp, 2 * kPointerSize);
4388 Operand result_operand = Operand(esp, 1 * kPointerSize);
4389 Operand array_length_operand = Operand(esp, 0);
4390 __ sub(esp, Immediate(2 * kPointerSize));
4392 // Check that the array is a JSArray
4393 __ JumpIfSmi(array, &bailout);
4394 __ CmpObjectType(array, JS_ARRAY_TYPE, scratch);
4395 __ j(not_equal, &bailout);
4397 // Check that the array has fast elements.
4398 __ CheckFastElements(scratch, &bailout);
4400 // If the array has length zero, return the empty string.
4401 __ mov(array_length, FieldOperand(array, JSArray::kLengthOffset));
4402 __ SmiUntag(array_length);
4403 __ j(not_zero, &non_trivial_array);
4404 __ mov(result_operand, isolate()->factory()->empty_string());
4407 // Save the array length.
4408 __ bind(&non_trivial_array);
4409 __ mov(array_length_operand, array_length);
4411 // Save the FixedArray containing array's elements.
4412 // End of array's live range.
4414 __ mov(elements, FieldOperand(array, JSArray::kElementsOffset));
4418 // Check that all array elements are sequential one-byte strings, and
4419 // accumulate the sum of their lengths, as a smi-encoded value.
4420 __ Move(index, Immediate(0));
4421 __ Move(string_length, Immediate(0));
4422 // Loop condition: while (index < length).
4423 // Live loop registers: index, array_length, string,
4424 // scratch, string_length, elements.
4425 if (generate_debug_code_) {
4426 __ cmp(index, array_length);
4427 __ Assert(less, kNoEmptyArraysHereInEmitFastOneByteArrayJoin);
4430 __ mov(string, FieldOperand(elements,
4433 FixedArray::kHeaderSize));
4434 __ JumpIfSmi(string, &bailout);
4435 __ mov(scratch, FieldOperand(string, HeapObject::kMapOffset));
4436 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
4437 __ and_(scratch, Immediate(
4438 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask));
4439 __ cmp(scratch, kStringTag | kOneByteStringTag | kSeqStringTag);
4440 __ j(not_equal, &bailout);
4441 __ add(string_length,
4442 FieldOperand(string, SeqOneByteString::kLengthOffset));
4443 __ j(overflow, &bailout);
4444 __ add(index, Immediate(1));
4445 __ cmp(index, array_length);
4448 // If array_length is 1, return elements[0], a string.
4449 __ cmp(array_length, 1);
4450 __ j(not_equal, ¬_size_one_array);
4451 __ mov(scratch, FieldOperand(elements, FixedArray::kHeaderSize));
4452 __ mov(result_operand, scratch);
4455 __ bind(¬_size_one_array);
4457 // End of array_length live range.
4458 result_pos = array_length;
4459 array_length = no_reg;
4462 // string_length: Sum of string lengths, as a smi.
4463 // elements: FixedArray of strings.
4465 // Check that the separator is a flat one-byte string.
4466 __ mov(string, separator_operand);
4467 __ JumpIfSmi(string, &bailout);
4468 __ mov(scratch, FieldOperand(string, HeapObject::kMapOffset));
4469 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
4470 __ and_(scratch, Immediate(
4471 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask));
4472 __ cmp(scratch, kStringTag | kOneByteStringTag | kSeqStringTag);
4473 __ j(not_equal, &bailout);
4475 // Add (separator length times array_length) - separator length
4476 // to string_length.
4477 __ mov(scratch, separator_operand);
4478 __ mov(scratch, FieldOperand(scratch, SeqOneByteString::kLengthOffset));
4479 __ sub(string_length, scratch); // May be negative, temporarily.
4480 __ imul(scratch, array_length_operand);
4481 __ j(overflow, &bailout);
4482 __ add(string_length, scratch);
4483 __ j(overflow, &bailout);
4485 __ shr(string_length, 1);
4486 // Live registers and stack values:
4489 __ AllocateOneByteString(result_pos, string_length, scratch, index, string,
4491 __ mov(result_operand, result_pos);
4492 __ lea(result_pos, FieldOperand(result_pos, SeqOneByteString::kHeaderSize));
4495 __ mov(string, separator_operand);
4496 __ cmp(FieldOperand(string, SeqOneByteString::kLengthOffset),
4497 Immediate(Smi::FromInt(1)));
4498 __ j(equal, &one_char_separator);
4499 __ j(greater, &long_separator);
4502 // Empty separator case
4503 __ mov(index, Immediate(0));
4504 __ jmp(&loop_1_condition);
4505 // Loop condition: while (index < length).
4507 // Each iteration of the loop concatenates one string to the result.
4508 // Live values in registers:
4509 // index: which element of the elements array we are adding to the result.
4510 // result_pos: the position to which we are currently copying characters.
4511 // elements: the FixedArray of strings we are joining.
4513 // Get string = array[index].
4514 __ mov(string, FieldOperand(elements, index,
4516 FixedArray::kHeaderSize));
4517 __ mov(string_length,
4518 FieldOperand(string, String::kLengthOffset));
4519 __ shr(string_length, 1);
4521 FieldOperand(string, SeqOneByteString::kHeaderSize));
4522 __ CopyBytes(string, result_pos, string_length, scratch);
4523 __ add(index, Immediate(1));
4524 __ bind(&loop_1_condition);
4525 __ cmp(index, array_length_operand);
4526 __ j(less, &loop_1); // End while (index < length).
4531 // One-character separator case
4532 __ bind(&one_char_separator);
4533 // Replace separator with its one-byte character value.
4534 __ mov_b(scratch, FieldOperand(string, SeqOneByteString::kHeaderSize));
4535 __ mov_b(separator_operand, scratch);
4537 __ Move(index, Immediate(0));
4538 // Jump into the loop after the code that copies the separator, so the first
4539 // element is not preceded by a separator
4540 __ jmp(&loop_2_entry);
4541 // Loop condition: while (index < length).
4543 // Each iteration of the loop concatenates one string to the result.
4544 // Live values in registers:
4545 // index: which element of the elements array we are adding to the result.
4546 // result_pos: the position to which we are currently copying characters.
4548 // Copy the separator character to the result.
4549 __ mov_b(scratch, separator_operand);
4550 __ mov_b(Operand(result_pos, 0), scratch);
4553 __ bind(&loop_2_entry);
4554 // Get string = array[index].
4555 __ mov(string, FieldOperand(elements, index,
4557 FixedArray::kHeaderSize));
4558 __ mov(string_length,
4559 FieldOperand(string, String::kLengthOffset));
4560 __ shr(string_length, 1);
4562 FieldOperand(string, SeqOneByteString::kHeaderSize));
4563 __ CopyBytes(string, result_pos, string_length, scratch);
4564 __ add(index, Immediate(1));
4566 __ cmp(index, array_length_operand);
4567 __ j(less, &loop_2); // End while (index < length).
4571 // Long separator case (separator is more than one character).
4572 __ bind(&long_separator);
4574 __ Move(index, Immediate(0));
4575 // Jump into the loop after the code that copies the separator, so the first
4576 // element is not preceded by a separator
4577 __ jmp(&loop_3_entry);
4578 // Loop condition: while (index < length).
4580 // Each iteration of the loop concatenates one string to the result.
4581 // Live values in registers:
4582 // index: which element of the elements array we are adding to the result.
4583 // result_pos: the position to which we are currently copying characters.
4585 // Copy the separator to the result.
4586 __ mov(string, separator_operand);
4587 __ mov(string_length,
4588 FieldOperand(string, String::kLengthOffset));
4589 __ shr(string_length, 1);
4591 FieldOperand(string, SeqOneByteString::kHeaderSize));
4592 __ CopyBytes(string, result_pos, string_length, scratch);
4594 __ bind(&loop_3_entry);
4595 // Get string = array[index].
4596 __ mov(string, FieldOperand(elements, index,
4598 FixedArray::kHeaderSize));
4599 __ mov(string_length,
4600 FieldOperand(string, String::kLengthOffset));
4601 __ shr(string_length, 1);
4603 FieldOperand(string, SeqOneByteString::kHeaderSize));
4604 __ CopyBytes(string, result_pos, string_length, scratch);
4605 __ add(index, Immediate(1));
4607 __ cmp(index, array_length_operand);
4608 __ j(less, &loop_3); // End while (index < length).
4613 __ mov(result_operand, isolate()->factory()->undefined_value());
4615 __ mov(eax, result_operand);
4616 // Drop temp values from the stack, and restore context register.
4617 __ add(esp, Immediate(3 * kPointerSize));
4619 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
4620 context()->Plug(eax);
4624 void FullCodeGenerator::EmitDebugIsActive(CallRuntime* expr) {
4625 DCHECK(expr->arguments()->length() == 0);
4626 ExternalReference debug_is_active =
4627 ExternalReference::debug_is_active_address(isolate());
4628 __ movzx_b(eax, Operand::StaticVariable(debug_is_active));
4630 context()->Plug(eax);
4634 void FullCodeGenerator::EmitCallSuperWithSpread(CallRuntime* expr) {
4635 // Assert: expr == CallRuntime("ReflectConstruct")
4636 DCHECK_EQ(1, expr->arguments()->length());
4637 CallRuntime* call = expr->arguments()->at(0)->AsCallRuntime();
4639 ZoneList<Expression*>* args = call->arguments();
4640 DCHECK_EQ(3, args->length());
4642 SuperCallReference* super_call_ref = args->at(0)->AsSuperCallReference();
4643 DCHECK_NOT_NULL(super_call_ref);
4645 // Load ReflectConstruct function
4646 EmitLoadJSRuntimeFunction(call);
4648 // Push the target function under the receiver
4649 __ push(Operand(esp, 0));
4650 __ mov(Operand(esp, kPointerSize), eax);
4652 // Push super constructor
4653 EmitLoadSuperConstructor(super_call_ref);
4654 __ Push(result_register());
4656 // Push arguments array
4657 VisitForStackValue(args->at(1));
4660 DCHECK(args->at(2)->IsVariableProxy());
4661 VisitForStackValue(args->at(2));
4663 EmitCallJSRuntimeFunction(call);
4665 // Restore context register.
4666 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
4667 context()->DropAndPlug(1, eax);
4669 // TODO(mvstanton): with FLAG_vector_stores this needs a slot id.
4670 EmitInitializeThisAfterSuper(super_call_ref);
4674 void FullCodeGenerator::EmitLoadJSRuntimeFunction(CallRuntime* expr) {
4675 // Push the builtins object as receiver.
4676 __ mov(eax, GlobalObjectOperand());
4677 __ push(FieldOperand(eax, GlobalObject::kBuiltinsOffset));
4679 // Load the function from the receiver.
4680 __ mov(LoadDescriptor::ReceiverRegister(), Operand(esp, 0));
4681 __ mov(LoadDescriptor::NameRegister(), Immediate(expr->name()));
4682 __ mov(LoadDescriptor::SlotRegister(),
4683 Immediate(SmiFromSlot(expr->CallRuntimeFeedbackSlot())));
4684 CallLoadIC(NOT_INSIDE_TYPEOF);
4688 void FullCodeGenerator::EmitCallJSRuntimeFunction(CallRuntime* expr) {
4689 ZoneList<Expression*>* args = expr->arguments();
4690 int arg_count = args->length();
4692 SetCallPosition(expr, arg_count);
4693 CallFunctionStub stub(isolate(), arg_count, NO_CALL_FUNCTION_FLAGS);
4694 __ mov(edi, Operand(esp, (arg_count + 1) * kPointerSize));
4699 void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
4700 ZoneList<Expression*>* args = expr->arguments();
4701 int arg_count = args->length();
4703 if (expr->is_jsruntime()) {
4704 Comment cmnt(masm_, "[ CallRuntime");
4705 EmitLoadJSRuntimeFunction(expr);
4707 // Push the target function under the receiver.
4708 __ push(Operand(esp, 0));
4709 __ mov(Operand(esp, kPointerSize), eax);
4711 // Push the arguments ("left-to-right").
4712 for (int i = 0; i < arg_count; i++) {
4713 VisitForStackValue(args->at(i));
4716 PrepareForBailoutForId(expr->CallId(), NO_REGISTERS);
4717 EmitCallJSRuntimeFunction(expr);
4719 // Restore context register.
4720 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
4721 context()->DropAndPlug(1, eax);
4724 const Runtime::Function* function = expr->function();
4725 switch (function->function_id) {
4726 #define CALL_INTRINSIC_GENERATOR(Name) \
4727 case Runtime::kInline##Name: { \
4728 Comment cmnt(masm_, "[ Inline" #Name); \
4729 return Emit##Name(expr); \
4731 FOR_EACH_FULL_CODE_INTRINSIC(CALL_INTRINSIC_GENERATOR)
4732 #undef CALL_INTRINSIC_GENERATOR
4734 Comment cmnt(masm_, "[ CallRuntime for unhandled intrinsic");
4735 // Push the arguments ("left-to-right").
4736 for (int i = 0; i < arg_count; i++) {
4737 VisitForStackValue(args->at(i));
4740 // Call the C runtime function.
4741 PrepareForBailoutForId(expr->CallId(), NO_REGISTERS);
4742 __ CallRuntime(expr->function(), arg_count);
4743 context()->Plug(eax);
4750 void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
4751 switch (expr->op()) {
4752 case Token::DELETE: {
4753 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
4754 Property* property = expr->expression()->AsProperty();
4755 VariableProxy* proxy = expr->expression()->AsVariableProxy();
4757 if (property != NULL) {
4758 VisitForStackValue(property->obj());
4759 VisitForStackValue(property->key());
4760 __ push(Immediate(Smi::FromInt(language_mode())));
4761 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
4762 context()->Plug(eax);
4763 } else if (proxy != NULL) {
4764 Variable* var = proxy->var();
4765 // Delete of an unqualified identifier is disallowed in strict mode but
4766 // "delete this" is allowed.
4767 bool is_this = var->HasThisName(isolate());
4768 DCHECK(is_sloppy(language_mode()) || is_this);
4769 if (var->IsUnallocatedOrGlobalSlot()) {
4770 __ push(GlobalObjectOperand());
4771 __ push(Immediate(var->name()));
4772 __ push(Immediate(Smi::FromInt(SLOPPY)));
4773 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
4774 context()->Plug(eax);
4775 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
4776 // Result of deleting non-global variables is false. 'this' is
4777 // not really a variable, though we implement it as one. The
4778 // subexpression does not have side effects.
4779 context()->Plug(is_this);
4781 // Non-global variable. Call the runtime to try to delete from the
4782 // context where the variable was introduced.
4783 __ push(context_register());
4784 __ push(Immediate(var->name()));
4785 __ CallRuntime(Runtime::kDeleteLookupSlot, 2);
4786 context()->Plug(eax);
4789 // Result of deleting non-property, non-variable reference is true.
4790 // The subexpression may have side effects.
4791 VisitForEffect(expr->expression());
4792 context()->Plug(true);
4798 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
4799 VisitForEffect(expr->expression());
4800 context()->Plug(isolate()->factory()->undefined_value());
4805 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
4806 if (context()->IsEffect()) {
4807 // Unary NOT has no side effects so it's only necessary to visit the
4808 // subexpression. Match the optimizing compiler by not branching.
4809 VisitForEffect(expr->expression());
4810 } else if (context()->IsTest()) {
4811 const TestContext* test = TestContext::cast(context());
4812 // The labels are swapped for the recursive call.
4813 VisitForControl(expr->expression(),
4814 test->false_label(),
4816 test->fall_through());
4817 context()->Plug(test->true_label(), test->false_label());
4819 // We handle value contexts explicitly rather than simply visiting
4820 // for control and plugging the control flow into the context,
4821 // because we need to prepare a pair of extra administrative AST ids
4822 // for the optimizing compiler.
4823 DCHECK(context()->IsAccumulatorValue() || context()->IsStackValue());
4824 Label materialize_true, materialize_false, done;
4825 VisitForControl(expr->expression(),
4829 __ bind(&materialize_true);
4830 PrepareForBailoutForId(expr->MaterializeTrueId(), NO_REGISTERS);
4831 if (context()->IsAccumulatorValue()) {
4832 __ mov(eax, isolate()->factory()->true_value());
4834 __ Push(isolate()->factory()->true_value());
4836 __ jmp(&done, Label::kNear);
4837 __ bind(&materialize_false);
4838 PrepareForBailoutForId(expr->MaterializeFalseId(), NO_REGISTERS);
4839 if (context()->IsAccumulatorValue()) {
4840 __ mov(eax, isolate()->factory()->false_value());
4842 __ Push(isolate()->factory()->false_value());
4849 case Token::TYPEOF: {
4850 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
4852 AccumulatorValueContext context(this);
4853 VisitForTypeofValue(expr->expression());
4856 TypeofStub typeof_stub(isolate());
4857 __ CallStub(&typeof_stub);
4858 context()->Plug(eax);
4868 void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
4869 DCHECK(expr->expression()->IsValidReferenceExpression());
4871 Comment cmnt(masm_, "[ CountOperation");
4873 Property* prop = expr->expression()->AsProperty();
4874 LhsKind assign_type = Property::GetAssignType(prop);
4876 // Evaluate expression and get value.
4877 if (assign_type == VARIABLE) {
4878 DCHECK(expr->expression()->AsVariableProxy()->var() != NULL);
4879 AccumulatorValueContext context(this);
4880 EmitVariableLoad(expr->expression()->AsVariableProxy());
4882 // Reserve space for result of postfix operation.
4883 if (expr->is_postfix() && !context()->IsEffect()) {
4884 __ push(Immediate(Smi::FromInt(0)));
4886 switch (assign_type) {
4887 case NAMED_PROPERTY: {
4888 // Put the object both on the stack and in the register.
4889 VisitForStackValue(prop->obj());
4890 __ mov(LoadDescriptor::ReceiverRegister(), Operand(esp, 0));
4891 EmitNamedPropertyLoad(prop);
4895 case NAMED_SUPER_PROPERTY: {
4896 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
4897 VisitForAccumulatorValue(
4898 prop->obj()->AsSuperPropertyReference()->home_object());
4899 __ push(result_register());
4900 __ push(MemOperand(esp, kPointerSize));
4901 __ push(result_register());
4902 EmitNamedSuperPropertyLoad(prop);
4906 case KEYED_SUPER_PROPERTY: {
4907 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
4909 prop->obj()->AsSuperPropertyReference()->home_object());
4910 VisitForAccumulatorValue(prop->key());
4911 __ push(result_register());
4912 __ push(MemOperand(esp, 2 * kPointerSize));
4913 __ push(MemOperand(esp, 2 * kPointerSize));
4914 __ push(result_register());
4915 EmitKeyedSuperPropertyLoad(prop);
4919 case KEYED_PROPERTY: {
4920 VisitForStackValue(prop->obj());
4921 VisitForStackValue(prop->key());
4922 __ mov(LoadDescriptor::ReceiverRegister(),
4923 Operand(esp, kPointerSize)); // Object.
4924 __ mov(LoadDescriptor::NameRegister(), Operand(esp, 0)); // Key.
4925 EmitKeyedPropertyLoad(prop);
4934 // We need a second deoptimization point after loading the value
4935 // in case evaluating the property load my have a side effect.
4936 if (assign_type == VARIABLE) {
4937 PrepareForBailout(expr->expression(), TOS_REG);
4939 PrepareForBailoutForId(prop->LoadId(), TOS_REG);
4942 // Inline smi case if we are in a loop.
4943 Label done, stub_call;
4944 JumpPatchSite patch_site(masm_);
4945 if (ShouldInlineSmiCase(expr->op())) {
4947 patch_site.EmitJumpIfNotSmi(eax, &slow, Label::kNear);
4949 // Save result for postfix expressions.
4950 if (expr->is_postfix()) {
4951 if (!context()->IsEffect()) {
4952 // Save the result on the stack. If we have a named or keyed property
4953 // we store the result under the receiver that is currently on top
4955 switch (assign_type) {
4959 case NAMED_PROPERTY:
4960 __ mov(Operand(esp, kPointerSize), eax);
4962 case NAMED_SUPER_PROPERTY:
4963 __ mov(Operand(esp, 2 * kPointerSize), eax);
4965 case KEYED_PROPERTY:
4966 __ mov(Operand(esp, 2 * kPointerSize), eax);
4968 case KEYED_SUPER_PROPERTY:
4969 __ mov(Operand(esp, 3 * kPointerSize), eax);
4975 if (expr->op() == Token::INC) {
4976 __ add(eax, Immediate(Smi::FromInt(1)));
4978 __ sub(eax, Immediate(Smi::FromInt(1)));
4980 __ j(no_overflow, &done, Label::kNear);
4981 // Call stub. Undo operation first.
4982 if (expr->op() == Token::INC) {
4983 __ sub(eax, Immediate(Smi::FromInt(1)));
4985 __ add(eax, Immediate(Smi::FromInt(1)));
4987 __ jmp(&stub_call, Label::kNear);
4990 if (!is_strong(language_mode())) {
4991 ToNumberStub convert_stub(isolate());
4992 __ CallStub(&convert_stub);
4993 PrepareForBailoutForId(expr->ToNumberId(), TOS_REG);
4996 // Save result for postfix expressions.
4997 if (expr->is_postfix()) {
4998 if (!context()->IsEffect()) {
4999 // Save the result on the stack. If we have a named or keyed property
5000 // we store the result under the receiver that is currently on top
5002 switch (assign_type) {
5006 case NAMED_PROPERTY:
5007 __ mov(Operand(esp, kPointerSize), eax);
5009 case NAMED_SUPER_PROPERTY:
5010 __ mov(Operand(esp, 2 * kPointerSize), eax);
5012 case KEYED_PROPERTY:
5013 __ mov(Operand(esp, 2 * kPointerSize), eax);
5015 case KEYED_SUPER_PROPERTY:
5016 __ mov(Operand(esp, 3 * kPointerSize), eax);
5022 SetExpressionPosition(expr);
5024 // Call stub for +1/-1.
5025 __ bind(&stub_call);
5027 __ mov(eax, Immediate(Smi::FromInt(1)));
5028 Handle<Code> code = CodeFactory::BinaryOpIC(isolate(), expr->binary_op(),
5029 strength(language_mode())).code();
5030 CallIC(code, expr->CountBinOpFeedbackId());
5031 patch_site.EmitPatchInfo();
5034 if (is_strong(language_mode())) {
5035 PrepareForBailoutForId(expr->ToNumberId(), TOS_REG);
5037 // Store the value returned in eax.
5038 switch (assign_type) {
5040 if (expr->is_postfix()) {
5041 // Perform the assignment as if via '='.
5042 { EffectContext context(this);
5043 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
5044 Token::ASSIGN, expr->CountSlot());
5045 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
5048 // For all contexts except EffectContext We have the result on
5049 // top of the stack.
5050 if (!context()->IsEffect()) {
5051 context()->PlugTOS();
5054 // Perform the assignment as if via '='.
5055 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
5056 Token::ASSIGN, expr->CountSlot());
5057 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
5058 context()->Plug(eax);
5061 case NAMED_PROPERTY: {
5062 __ mov(StoreDescriptor::NameRegister(),
5063 prop->key()->AsLiteral()->value());
5064 __ pop(StoreDescriptor::ReceiverRegister());
5065 if (FLAG_vector_stores) {
5066 EmitLoadStoreICSlot(expr->CountSlot());
5069 CallStoreIC(expr->CountStoreFeedbackId());
5071 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
5072 if (expr->is_postfix()) {
5073 if (!context()->IsEffect()) {
5074 context()->PlugTOS();
5077 context()->Plug(eax);
5081 case NAMED_SUPER_PROPERTY: {
5082 EmitNamedSuperPropertyStore(prop);
5083 if (expr->is_postfix()) {
5084 if (!context()->IsEffect()) {
5085 context()->PlugTOS();
5088 context()->Plug(eax);
5092 case KEYED_SUPER_PROPERTY: {
5093 EmitKeyedSuperPropertyStore(prop);
5094 if (expr->is_postfix()) {
5095 if (!context()->IsEffect()) {
5096 context()->PlugTOS();
5099 context()->Plug(eax);
5103 case KEYED_PROPERTY: {
5104 __ pop(StoreDescriptor::NameRegister());
5105 __ pop(StoreDescriptor::ReceiverRegister());
5107 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
5108 if (FLAG_vector_stores) {
5109 EmitLoadStoreICSlot(expr->CountSlot());
5112 CallIC(ic, expr->CountStoreFeedbackId());
5114 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
5115 if (expr->is_postfix()) {
5116 // Result is on the stack
5117 if (!context()->IsEffect()) {
5118 context()->PlugTOS();
5121 context()->Plug(eax);
5129 void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
5130 Expression* sub_expr,
5131 Handle<String> check) {
5132 Label materialize_true, materialize_false;
5133 Label* if_true = NULL;
5134 Label* if_false = NULL;
5135 Label* fall_through = NULL;
5136 context()->PrepareTest(&materialize_true, &materialize_false,
5137 &if_true, &if_false, &fall_through);
5139 { AccumulatorValueContext context(this);
5140 VisitForTypeofValue(sub_expr);
5142 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
5144 Factory* factory = isolate()->factory();
5145 if (String::Equals(check, factory->number_string())) {
5146 __ JumpIfSmi(eax, if_true);
5147 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
5148 isolate()->factory()->heap_number_map());
5149 Split(equal, if_true, if_false, fall_through);
5150 } else if (String::Equals(check, factory->string_string())) {
5151 __ JumpIfSmi(eax, if_false);
5152 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, edx);
5153 __ j(above_equal, if_false);
5154 // Check for undetectable objects => false.
5155 __ test_b(FieldOperand(edx, Map::kBitFieldOffset),
5156 1 << Map::kIsUndetectable);
5157 Split(zero, if_true, if_false, fall_through);
5158 } else if (String::Equals(check, factory->symbol_string())) {
5159 __ JumpIfSmi(eax, if_false);
5160 __ CmpObjectType(eax, SYMBOL_TYPE, edx);
5161 Split(equal, if_true, if_false, fall_through);
5162 } else if (String::Equals(check, factory->boolean_string())) {
5163 __ cmp(eax, isolate()->factory()->true_value());
5164 __ j(equal, if_true);
5165 __ cmp(eax, isolate()->factory()->false_value());
5166 Split(equal, if_true, if_false, fall_through);
5167 } else if (String::Equals(check, factory->undefined_string())) {
5168 __ cmp(eax, isolate()->factory()->undefined_value());
5169 __ j(equal, if_true);
5170 __ JumpIfSmi(eax, if_false);
5171 // Check for undetectable objects => true.
5172 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
5173 __ movzx_b(ecx, FieldOperand(edx, Map::kBitFieldOffset));
5174 __ test(ecx, Immediate(1 << Map::kIsUndetectable));
5175 Split(not_zero, if_true, if_false, fall_through);
5176 } else if (String::Equals(check, factory->function_string())) {
5177 __ JumpIfSmi(eax, if_false);
5178 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
5179 __ CmpObjectType(eax, JS_FUNCTION_TYPE, edx);
5180 __ j(equal, if_true);
5181 __ CmpInstanceType(edx, JS_FUNCTION_PROXY_TYPE);
5182 Split(equal, if_true, if_false, fall_through);
5183 } else if (String::Equals(check, factory->object_string())) {
5184 __ JumpIfSmi(eax, if_false);
5185 __ cmp(eax, isolate()->factory()->null_value());
5186 __ j(equal, if_true);
5187 __ CmpObjectType(eax, FIRST_NONCALLABLE_SPEC_OBJECT_TYPE, edx);
5188 __ j(below, if_false);
5189 __ CmpInstanceType(edx, LAST_NONCALLABLE_SPEC_OBJECT_TYPE);
5190 __ j(above, if_false);
5191 // Check for undetectable objects => false.
5192 __ test_b(FieldOperand(edx, Map::kBitFieldOffset),
5193 1 << Map::kIsUndetectable);
5194 Split(zero, if_true, if_false, fall_through);
5196 if (if_false != fall_through) __ jmp(if_false);
5198 context()->Plug(if_true, if_false);
5202 void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
5203 Comment cmnt(masm_, "[ CompareOperation");
5204 SetExpressionPosition(expr);
5206 // First we try a fast inlined version of the compare when one of
5207 // the operands is a literal.
5208 if (TryLiteralCompare(expr)) return;
5210 // Always perform the comparison for its control flow. Pack the result
5211 // into the expression's context after the comparison is performed.
5212 Label materialize_true, materialize_false;
5213 Label* if_true = NULL;
5214 Label* if_false = NULL;
5215 Label* fall_through = NULL;
5216 context()->PrepareTest(&materialize_true, &materialize_false,
5217 &if_true, &if_false, &fall_through);
5219 Token::Value op = expr->op();
5220 VisitForStackValue(expr->left());
5223 VisitForStackValue(expr->right());
5224 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
5225 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
5226 __ cmp(eax, isolate()->factory()->true_value());
5227 Split(equal, if_true, if_false, fall_through);
5230 case Token::INSTANCEOF: {
5231 VisitForStackValue(expr->right());
5232 InstanceofStub stub(isolate(), InstanceofStub::kNoFlags);
5234 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
5236 // The stub returns 0 for true.
5237 Split(zero, if_true, if_false, fall_through);
5242 VisitForAccumulatorValue(expr->right());
5243 Condition cc = CompareIC::ComputeCondition(op);
5246 bool inline_smi_code = ShouldInlineSmiCase(op);
5247 JumpPatchSite patch_site(masm_);
5248 if (inline_smi_code) {
5252 patch_site.EmitJumpIfNotSmi(ecx, &slow_case, Label::kNear);
5254 Split(cc, if_true, if_false, NULL);
5255 __ bind(&slow_case);
5258 Handle<Code> ic = CodeFactory::CompareIC(
5259 isolate(), op, strength(language_mode())).code();
5260 CallIC(ic, expr->CompareOperationFeedbackId());
5261 patch_site.EmitPatchInfo();
5263 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
5265 Split(cc, if_true, if_false, fall_through);
5269 // Convert the result of the comparison into one expected for this
5270 // expression's context.
5271 context()->Plug(if_true, if_false);
5275 void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
5276 Expression* sub_expr,
5278 Label materialize_true, materialize_false;
5279 Label* if_true = NULL;
5280 Label* if_false = NULL;
5281 Label* fall_through = NULL;
5282 context()->PrepareTest(&materialize_true, &materialize_false,
5283 &if_true, &if_false, &fall_through);
5285 VisitForAccumulatorValue(sub_expr);
5286 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
5288 Handle<Object> nil_value = nil == kNullValue
5289 ? isolate()->factory()->null_value()
5290 : isolate()->factory()->undefined_value();
5291 if (expr->op() == Token::EQ_STRICT) {
5292 __ cmp(eax, nil_value);
5293 Split(equal, if_true, if_false, fall_through);
5295 Handle<Code> ic = CompareNilICStub::GetUninitialized(isolate(), nil);
5296 CallIC(ic, expr->CompareOperationFeedbackId());
5298 Split(not_zero, if_true, if_false, fall_through);
5300 context()->Plug(if_true, if_false);
5304 void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
5305 __ mov(eax, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
5306 context()->Plug(eax);
5310 Register FullCodeGenerator::result_register() {
5315 Register FullCodeGenerator::context_register() {
5320 void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
5321 DCHECK_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
5322 __ mov(Operand(ebp, frame_offset), value);
5326 void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
5327 __ mov(dst, ContextOperand(esi, context_index));
5331 void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
5332 Scope* declaration_scope = scope()->DeclarationScope();
5333 if (declaration_scope->is_script_scope() ||
5334 declaration_scope->is_module_scope()) {
5335 // Contexts nested in the native context have a canonical empty function
5336 // as their closure, not the anonymous closure containing the global
5337 // code. Pass a smi sentinel and let the runtime look up the empty
5339 __ push(Immediate(Smi::FromInt(0)));
5340 } else if (declaration_scope->is_eval_scope()) {
5341 // Contexts nested inside eval code have the same closure as the context
5342 // calling eval, not the anonymous closure containing the eval code.
5343 // Fetch it from the context.
5344 __ push(ContextOperand(esi, Context::CLOSURE_INDEX));
5346 DCHECK(declaration_scope->is_function_scope());
5347 __ push(Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
5352 // ----------------------------------------------------------------------------
5353 // Non-local control flow support.
5355 void FullCodeGenerator::EnterFinallyBlock() {
5356 // Cook return address on top of stack (smi encoded Code* delta)
5357 DCHECK(!result_register().is(edx));
5359 __ sub(edx, Immediate(masm_->CodeObject()));
5360 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
5361 STATIC_ASSERT(kSmiTag == 0);
5365 // Store result register while executing finally block.
5366 __ push(result_register());
5368 // Store pending message while executing finally block.
5369 ExternalReference pending_message_obj =
5370 ExternalReference::address_of_pending_message_obj(isolate());
5371 __ mov(edx, Operand::StaticVariable(pending_message_obj));
5374 ClearPendingMessage();
5378 void FullCodeGenerator::ExitFinallyBlock() {
5379 DCHECK(!result_register().is(edx));
5380 // Restore pending message from stack.
5382 ExternalReference pending_message_obj =
5383 ExternalReference::address_of_pending_message_obj(isolate());
5384 __ mov(Operand::StaticVariable(pending_message_obj), edx);
5386 // Restore result register from stack.
5387 __ pop(result_register());
5389 // Uncook return address.
5392 __ add(edx, Immediate(masm_->CodeObject()));
5397 void FullCodeGenerator::ClearPendingMessage() {
5398 DCHECK(!result_register().is(edx));
5399 ExternalReference pending_message_obj =
5400 ExternalReference::address_of_pending_message_obj(isolate());
5401 __ mov(edx, Immediate(isolate()->factory()->the_hole_value()));
5402 __ mov(Operand::StaticVariable(pending_message_obj), edx);
5406 void FullCodeGenerator::EmitLoadStoreICSlot(FeedbackVectorICSlot slot) {
5407 DCHECK(FLAG_vector_stores && !slot.IsInvalid());
5408 __ mov(VectorStoreICTrampolineDescriptor::SlotRegister(),
5409 Immediate(SmiFromSlot(slot)));
5416 static const byte kJnsInstruction = 0x79;
5417 static const byte kJnsOffset = 0x11;
5418 static const byte kNopByteOne = 0x66;
5419 static const byte kNopByteTwo = 0x90;
5421 static const byte kCallInstruction = 0xe8;
5425 void BackEdgeTable::PatchAt(Code* unoptimized_code,
5427 BackEdgeState target_state,
5428 Code* replacement_code) {
5429 Address call_target_address = pc - kIntSize;
5430 Address jns_instr_address = call_target_address - 3;
5431 Address jns_offset_address = call_target_address - 2;
5433 switch (target_state) {
5435 // sub <profiling_counter>, <delta> ;; Not changed
5437 // call <interrupt stub>
5439 *jns_instr_address = kJnsInstruction;
5440 *jns_offset_address = kJnsOffset;
5442 case ON_STACK_REPLACEMENT:
5443 case OSR_AFTER_STACK_CHECK:
5444 // sub <profiling_counter>, <delta> ;; Not changed
5447 // call <on-stack replacment>
5449 *jns_instr_address = kNopByteOne;
5450 *jns_offset_address = kNopByteTwo;
5454 Assembler::set_target_address_at(call_target_address,
5456 replacement_code->entry());
5457 unoptimized_code->GetHeap()->incremental_marking()->RecordCodeTargetPatch(
5458 unoptimized_code, call_target_address, replacement_code);
5462 BackEdgeTable::BackEdgeState BackEdgeTable::GetBackEdgeState(
5464 Code* unoptimized_code,
5466 Address call_target_address = pc - kIntSize;
5467 Address jns_instr_address = call_target_address - 3;
5468 DCHECK_EQ(kCallInstruction, *(call_target_address - 1));
5470 if (*jns_instr_address == kJnsInstruction) {
5471 DCHECK_EQ(kJnsOffset, *(call_target_address - 2));
5472 DCHECK_EQ(isolate->builtins()->InterruptCheck()->entry(),
5473 Assembler::target_address_at(call_target_address,
5478 DCHECK_EQ(kNopByteOne, *jns_instr_address);
5479 DCHECK_EQ(kNopByteTwo, *(call_target_address - 2));
5481 if (Assembler::target_address_at(call_target_address, unoptimized_code) ==
5482 isolate->builtins()->OnStackReplacement()->entry()) {
5483 return ON_STACK_REPLACEMENT;
5486 DCHECK_EQ(isolate->builtins()->OsrAfterStackCheck()->entry(),
5487 Assembler::target_address_at(call_target_address,
5489 return OSR_AFTER_STACK_CHECK;
5493 } // namespace internal
5496 #endif // V8_TARGET_ARCH_X87