1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "full-codegen.h"
35 #include "macro-assembler.h"
36 #include "prettyprinter.h"
38 #include "scopeinfo.h"
39 #include "stub-cache.h"
44 void BreakableStatementChecker::Check(Statement* stmt) {
49 void BreakableStatementChecker::Check(Expression* expr) {
54 void BreakableStatementChecker::VisitVariableDeclaration(
55 VariableDeclaration* decl) {
58 void BreakableStatementChecker::VisitFunctionDeclaration(
59 FunctionDeclaration* decl) {
62 void BreakableStatementChecker::VisitModuleDeclaration(
63 ModuleDeclaration* decl) {
66 void BreakableStatementChecker::VisitImportDeclaration(
67 ImportDeclaration* decl) {
70 void BreakableStatementChecker::VisitExportDeclaration(
71 ExportDeclaration* decl) {
75 void BreakableStatementChecker::VisitModuleLiteral(ModuleLiteral* module) {
78 void BreakableStatementChecker::VisitModuleVariable(ModuleVariable* module) {
81 void BreakableStatementChecker::VisitModulePath(ModulePath* module) {
84 void BreakableStatementChecker::VisitModuleUrl(ModuleUrl* module) {
88 void BreakableStatementChecker::VisitBlock(Block* stmt) {
92 void BreakableStatementChecker::VisitExpressionStatement(
93 ExpressionStatement* stmt) {
94 // Check if expression is breakable.
95 Visit(stmt->expression());
99 void BreakableStatementChecker::VisitEmptyStatement(EmptyStatement* stmt) {
103 void BreakableStatementChecker::VisitIfStatement(IfStatement* stmt) {
104 // If the condition is breakable the if statement is breakable.
105 Visit(stmt->condition());
109 void BreakableStatementChecker::VisitContinueStatement(
110 ContinueStatement* stmt) {
114 void BreakableStatementChecker::VisitBreakStatement(BreakStatement* stmt) {
118 void BreakableStatementChecker::VisitReturnStatement(ReturnStatement* stmt) {
119 // Return is breakable if the expression is.
120 Visit(stmt->expression());
124 void BreakableStatementChecker::VisitWithStatement(WithStatement* stmt) {
125 Visit(stmt->expression());
129 void BreakableStatementChecker::VisitSwitchStatement(SwitchStatement* stmt) {
130 // Switch statements breakable if the tag expression is.
135 void BreakableStatementChecker::VisitDoWhileStatement(DoWhileStatement* stmt) {
136 // Mark do while as breakable to avoid adding a break slot in front of it.
137 is_breakable_ = true;
141 void BreakableStatementChecker::VisitWhileStatement(WhileStatement* stmt) {
142 // Mark while statements breakable if the condition expression is.
147 void BreakableStatementChecker::VisitForStatement(ForStatement* stmt) {
148 // Mark for statements breakable if the condition expression is.
149 if (stmt->cond() != NULL) {
155 void BreakableStatementChecker::VisitForInStatement(ForInStatement* stmt) {
156 // Mark for in statements breakable if the enumerable expression is.
157 Visit(stmt->enumerable());
161 void BreakableStatementChecker::VisitTryCatchStatement(
162 TryCatchStatement* stmt) {
163 // Mark try catch as breakable to avoid adding a break slot in front of it.
164 is_breakable_ = true;
168 void BreakableStatementChecker::VisitTryFinallyStatement(
169 TryFinallyStatement* stmt) {
170 // Mark try finally as breakable to avoid adding a break slot in front of it.
171 is_breakable_ = true;
175 void BreakableStatementChecker::VisitDebuggerStatement(
176 DebuggerStatement* stmt) {
177 // The debugger statement is breakable.
178 is_breakable_ = true;
182 void BreakableStatementChecker::VisitFunctionLiteral(FunctionLiteral* expr) {
186 void BreakableStatementChecker::VisitSharedFunctionInfoLiteral(
187 SharedFunctionInfoLiteral* expr) {
191 void BreakableStatementChecker::VisitConditional(Conditional* expr) {
195 void BreakableStatementChecker::VisitVariableProxy(VariableProxy* expr) {
199 void BreakableStatementChecker::VisitLiteral(Literal* expr) {
203 void BreakableStatementChecker::VisitRegExpLiteral(RegExpLiteral* expr) {
207 void BreakableStatementChecker::VisitObjectLiteral(ObjectLiteral* expr) {
211 void BreakableStatementChecker::VisitArrayLiteral(ArrayLiteral* expr) {
215 void BreakableStatementChecker::VisitAssignment(Assignment* expr) {
216 // If assigning to a property (including a global property) the assignment is
218 VariableProxy* proxy = expr->target()->AsVariableProxy();
219 Property* prop = expr->target()->AsProperty();
220 if (prop != NULL || (proxy != NULL && proxy->var()->IsUnallocated())) {
221 is_breakable_ = true;
225 // Otherwise the assignment is breakable if the assigned value is.
226 Visit(expr->value());
230 void BreakableStatementChecker::VisitThrow(Throw* expr) {
231 // Throw is breakable if the expression is.
232 Visit(expr->exception());
236 void BreakableStatementChecker::VisitProperty(Property* expr) {
237 // Property load is breakable.
238 is_breakable_ = true;
242 void BreakableStatementChecker::VisitCall(Call* expr) {
243 // Function calls both through IC and call stub are breakable.
244 is_breakable_ = true;
248 void BreakableStatementChecker::VisitCallNew(CallNew* expr) {
249 // Function calls through new are breakable.
250 is_breakable_ = true;
254 void BreakableStatementChecker::VisitCallRuntime(CallRuntime* expr) {
258 void BreakableStatementChecker::VisitUnaryOperation(UnaryOperation* expr) {
259 Visit(expr->expression());
263 void BreakableStatementChecker::VisitCountOperation(CountOperation* expr) {
264 Visit(expr->expression());
268 void BreakableStatementChecker::VisitBinaryOperation(BinaryOperation* expr) {
270 if (expr->op() != Token::AND &&
271 expr->op() != Token::OR) {
272 Visit(expr->right());
277 void BreakableStatementChecker::VisitCompareOperation(CompareOperation* expr) {
279 Visit(expr->right());
283 void BreakableStatementChecker::VisitThisFunction(ThisFunction* expr) {
287 #define __ ACCESS_MASM(masm())
289 bool FullCodeGenerator::MakeCode(CompilationInfo* info) {
290 Isolate* isolate = info->isolate();
291 Handle<Script> script = info->script();
292 if (!script->IsUndefined() && !script->source()->IsUndefined()) {
293 int len = String::cast(script->source())->length();
294 isolate->counters()->total_full_codegen_source_size()->Increment(len);
296 if (FLAG_trace_codegen) {
297 PrintF("Full Compiler - ");
299 CodeGenerator::MakeCodePrologue(info);
300 const int kInitialBufferSize = 4 * KB;
301 MacroAssembler masm(info->isolate(), NULL, kInitialBufferSize);
302 #ifdef ENABLE_GDB_JIT_INTERFACE
303 masm.positions_recorder()->StartGDBJITLineInfoRecording();
306 FullCodeGenerator cgen(&masm, info);
308 if (cgen.HasStackOverflow()) {
309 ASSERT(!isolate->has_pending_exception());
312 unsigned table_offset = cgen.EmitStackCheckTable();
314 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION);
315 Handle<Code> code = CodeGenerator::MakeCodeEpilogue(&masm, flags, info);
316 code->set_optimizable(info->IsOptimizable() &&
317 !info->function()->flags()->Contains(kDontOptimize));
318 cgen.PopulateDeoptimizationData(code);
319 cgen.PopulateTypeFeedbackInfo(code);
320 cgen.PopulateTypeFeedbackCells(code);
321 code->set_has_deoptimization_support(info->HasDeoptimizationSupport());
322 code->set_handler_table(*cgen.handler_table());
323 #ifdef ENABLE_DEBUGGER_SUPPORT
324 code->set_has_debug_break_slots(
325 info->isolate()->debugger()->IsDebuggerActive());
326 code->set_compiled_optimizable(info->IsOptimizable());
327 #endif // ENABLE_DEBUGGER_SUPPORT
328 code->set_allow_osr_at_loop_nesting_level(0);
329 code->set_profiler_ticks(0);
330 code->set_stack_check_table_offset(table_offset);
331 CodeGenerator::PrintCode(code, info);
332 info->SetCode(code); // May be an empty handle.
333 #ifdef ENABLE_GDB_JIT_INTERFACE
334 if (FLAG_gdbjit && !code.is_null()) {
335 GDBJITLineInfo* lineinfo =
336 masm.positions_recorder()->DetachGDBJITLineInfo();
338 GDBJIT(RegisterDetailedLineInfo(*code, lineinfo));
341 return !code.is_null();
345 unsigned FullCodeGenerator::EmitStackCheckTable() {
346 // The stack check table consists of a length (in number of entries)
347 // field, and then a sequence of entries. Each entry is a pair of AST id
348 // and code-relative pc offset.
349 masm()->Align(kIntSize);
350 unsigned offset = masm()->pc_offset();
351 unsigned length = stack_checks_.length();
353 for (unsigned i = 0; i < length; ++i) {
354 __ dd(stack_checks_[i].id);
355 __ dd(stack_checks_[i].pc_and_state);
361 void FullCodeGenerator::PopulateDeoptimizationData(Handle<Code> code) {
362 // Fill in the deoptimization information.
363 ASSERT(info_->HasDeoptimizationSupport() || bailout_entries_.is_empty());
364 if (!info_->HasDeoptimizationSupport()) return;
365 int length = bailout_entries_.length();
366 Handle<DeoptimizationOutputData> data = isolate()->factory()->
367 NewDeoptimizationOutputData(length, TENURED);
368 for (int i = 0; i < length; i++) {
369 data->SetAstId(i, Smi::FromInt(bailout_entries_[i].id));
370 data->SetPcAndState(i, Smi::FromInt(bailout_entries_[i].pc_and_state));
372 code->set_deoptimization_data(*data);
376 void FullCodeGenerator::PopulateTypeFeedbackInfo(Handle<Code> code) {
377 Handle<TypeFeedbackInfo> info = isolate()->factory()->NewTypeFeedbackInfo();
378 info->set_ic_total_count(ic_total_count_);
379 ASSERT(!isolate()->heap()->InNewSpace(*info));
380 code->set_type_feedback_info(*info);
384 void FullCodeGenerator::PopulateTypeFeedbackCells(Handle<Code> code) {
385 if (type_feedback_cells_.is_empty()) return;
386 int length = type_feedback_cells_.length();
387 int array_size = TypeFeedbackCells::LengthOfFixedArray(length);
388 Handle<TypeFeedbackCells> cache = Handle<TypeFeedbackCells>::cast(
389 isolate()->factory()->NewFixedArray(array_size, TENURED));
390 for (int i = 0; i < length; i++) {
391 cache->SetAstId(i, Smi::FromInt(type_feedback_cells_[i].ast_id));
392 cache->SetCell(i, *type_feedback_cells_[i].cell);
394 TypeFeedbackInfo::cast(code->type_feedback_info())->set_type_feedback_cells(
400 void FullCodeGenerator::PrepareForBailout(Expression* node, State state) {
401 PrepareForBailoutForId(node->id(), state);
405 void FullCodeGenerator::RecordJSReturnSite(Call* call) {
406 // We record the offset of the function return so we can rebuild the frame
407 // if the function was inlined, i.e., this is the return address in the
408 // inlined function's frame.
410 // The state is ignored. We defensively set it to TOS_REG, which is the
411 // real state of the unoptimized code at the return site.
412 PrepareForBailoutForId(call->ReturnId(), TOS_REG);
414 // In debug builds, mark the return so we can verify that this function
416 ASSERT(!call->return_is_recorded_);
417 call->return_is_recorded_ = true;
422 void FullCodeGenerator::PrepareForBailoutForId(unsigned id, State state) {
423 // There's no need to prepare this code for bailouts from already optimized
424 // code or code that can't be optimized.
425 if (!info_->HasDeoptimizationSupport()) return;
426 unsigned pc_and_state =
427 StateField::encode(state) | PcField::encode(masm_->pc_offset());
428 ASSERT(Smi::IsValid(pc_and_state));
429 BailoutEntry entry = { id, pc_and_state };
431 if (FLAG_enable_slow_asserts) {
432 // Assert that we don't have multiple bailout entries for the same node.
433 for (int i = 0; i < bailout_entries_.length(); i++) {
434 if (bailout_entries_.at(i).id == entry.id) {
436 PrintF("%s", printer.PrintProgram(info_->function()));
442 bailout_entries_.Add(entry);
446 void FullCodeGenerator::RecordTypeFeedbackCell(
447 unsigned id, Handle<JSGlobalPropertyCell> cell) {
448 TypeFeedbackCellEntry entry = { id, cell };
449 type_feedback_cells_.Add(entry);
453 void FullCodeGenerator::RecordStackCheck(unsigned ast_id) {
454 // The pc offset does not need to be encoded and packed together with a
456 ASSERT(masm_->pc_offset() > 0);
457 BailoutEntry entry = { ast_id, static_cast<unsigned>(masm_->pc_offset()) };
458 stack_checks_.Add(entry);
462 bool FullCodeGenerator::ShouldInlineSmiCase(Token::Value op) {
463 // Inline smi case inside loops, but not division and modulo which
464 // are too complicated and take up too much space.
465 if (op == Token::DIV ||op == Token::MOD) return false;
466 if (FLAG_always_inline_smi_code) return true;
467 return loop_depth_ > 0;
471 void FullCodeGenerator::EffectContext::Plug(Register reg) const {
475 void FullCodeGenerator::AccumulatorValueContext::Plug(Register reg) const {
476 __ Move(result_register(), reg);
480 void FullCodeGenerator::StackValueContext::Plug(Register reg) const {
485 void FullCodeGenerator::TestContext::Plug(Register reg) const {
486 // For simplicity we always test the accumulator register.
487 __ Move(result_register(), reg);
488 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
489 codegen()->DoTest(this);
493 void FullCodeGenerator::EffectContext::PlugTOS() const {
498 void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const {
499 __ pop(result_register());
503 void FullCodeGenerator::StackValueContext::PlugTOS() const {
507 void FullCodeGenerator::TestContext::PlugTOS() const {
508 // For simplicity we always test the accumulator register.
509 __ pop(result_register());
510 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
511 codegen()->DoTest(this);
515 void FullCodeGenerator::EffectContext::PrepareTest(
516 Label* materialize_true,
517 Label* materialize_false,
520 Label** fall_through) const {
521 // In an effect context, the true and the false case branch to the
523 *if_true = *if_false = *fall_through = materialize_true;
527 void FullCodeGenerator::AccumulatorValueContext::PrepareTest(
528 Label* materialize_true,
529 Label* materialize_false,
532 Label** fall_through) const {
533 *if_true = *fall_through = materialize_true;
534 *if_false = materialize_false;
538 void FullCodeGenerator::StackValueContext::PrepareTest(
539 Label* materialize_true,
540 Label* materialize_false,
543 Label** fall_through) const {
544 *if_true = *fall_through = materialize_true;
545 *if_false = materialize_false;
549 void FullCodeGenerator::TestContext::PrepareTest(
550 Label* materialize_true,
551 Label* materialize_false,
554 Label** fall_through) const {
555 *if_true = true_label_;
556 *if_false = false_label_;
557 *fall_through = fall_through_;
561 void FullCodeGenerator::DoTest(const TestContext* context) {
562 DoTest(context->condition(),
563 context->true_label(),
564 context->false_label(),
565 context->fall_through());
569 void FullCodeGenerator::VisitDeclarations(
570 ZoneList<Declaration*>* declarations) {
571 int save_global_count = global_count_;
574 AstVisitor::VisitDeclarations(declarations);
576 // Batch declare global functions and variables.
577 if (global_count_ > 0) {
578 Handle<FixedArray> array =
579 isolate()->factory()->NewFixedArray(3 * global_count_, TENURED);
580 int length = declarations->length();
581 for (int j = 0, i = 0; i < length; i++) {
582 Declaration* decl = declarations->at(i);
583 Variable* var = decl->proxy()->var();
585 if (var->IsUnallocated()) {
586 array->set(j++, *(var->name()));
587 FunctionDeclaration* fun_decl = decl->AsFunctionDeclaration();
588 if (fun_decl == NULL) {
589 if (var->binding_needs_init()) {
590 // In case this binding needs initialization use the hole.
591 array->set_the_hole(j++);
593 array->set_undefined(j++);
596 Handle<SharedFunctionInfo> function =
597 Compiler::BuildFunctionInfo(fun_decl->fun(), script());
598 // Check for stack-overflow exception.
599 if (function.is_null()) {
603 array->set(j++, *function);
605 array->set(j++, Smi::FromInt(var->is_qml_global()));
608 // Invoke the platform-dependent code generator to do the actual
609 // declaration the global functions and variables.
610 DeclareGlobals(array);
613 global_count_ = save_global_count;
617 void FullCodeGenerator::VisitVariableDeclaration(VariableDeclaration* decl) {
618 EmitDeclaration(decl->proxy(), decl->mode(), NULL);
622 void FullCodeGenerator::VisitFunctionDeclaration(FunctionDeclaration* decl) {
623 EmitDeclaration(decl->proxy(), decl->mode(), decl->fun());
627 void FullCodeGenerator::VisitModuleDeclaration(ModuleDeclaration* decl) {
628 EmitDeclaration(decl->proxy(), decl->mode(), NULL);
632 void FullCodeGenerator::VisitImportDeclaration(ImportDeclaration* decl) {
633 EmitDeclaration(decl->proxy(), decl->mode(), NULL);
637 void FullCodeGenerator::VisitExportDeclaration(ExportDeclaration* decl) {
642 void FullCodeGenerator::VisitModuleLiteral(ModuleLiteral* module) {
647 void FullCodeGenerator::VisitModuleVariable(ModuleVariable* module) {
652 void FullCodeGenerator::VisitModulePath(ModulePath* module) {
657 void FullCodeGenerator::VisitModuleUrl(ModuleUrl* decl) {
662 int FullCodeGenerator::DeclareGlobalsFlags() {
663 ASSERT(DeclareGlobalsLanguageMode::is_valid(language_mode()));
664 return DeclareGlobalsEvalFlag::encode(is_eval()) |
665 DeclareGlobalsNativeFlag::encode(is_native()) |
666 DeclareGlobalsLanguageMode::encode(language_mode());
670 void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) {
671 CodeGenerator::RecordPositions(masm_, fun->start_position());
675 void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
676 CodeGenerator::RecordPositions(masm_, fun->end_position() - 1);
680 void FullCodeGenerator::SetStatementPosition(Statement* stmt) {
681 #ifdef ENABLE_DEBUGGER_SUPPORT
682 if (!isolate()->debugger()->IsDebuggerActive()) {
683 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
685 // Check if the statement will be breakable without adding a debug break
687 BreakableStatementChecker checker;
689 // Record the statement position right here if the statement is not
690 // breakable. For breakable statements the actual recording of the
691 // position will be postponed to the breakable code (typically an IC).
692 bool position_recorded = CodeGenerator::RecordPositions(
693 masm_, stmt->statement_pos(), !checker.is_breakable());
694 // If the position recording did record a new position generate a debug
695 // break slot to make the statement breakable.
696 if (position_recorded) {
697 Debug::GenerateSlot(masm_);
701 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
706 void FullCodeGenerator::SetExpressionPosition(Expression* expr, int pos) {
707 #ifdef ENABLE_DEBUGGER_SUPPORT
708 if (!isolate()->debugger()->IsDebuggerActive()) {
709 CodeGenerator::RecordPositions(masm_, pos);
711 // Check if the expression will be breakable without adding a debug break
713 BreakableStatementChecker checker;
715 // Record a statement position right here if the expression is not
716 // breakable. For breakable expressions the actual recording of the
717 // position will be postponed to the breakable code (typically an IC).
718 // NOTE this will record a statement position for something which might
719 // not be a statement. As stepping in the debugger will only stop at
720 // statement positions this is used for e.g. the condition expression of
722 bool position_recorded = CodeGenerator::RecordPositions(
723 masm_, pos, !checker.is_breakable());
724 // If the position recording did record a new position generate a debug
725 // break slot to make the statement breakable.
726 if (position_recorded) {
727 Debug::GenerateSlot(masm_);
731 CodeGenerator::RecordPositions(masm_, pos);
736 void FullCodeGenerator::SetStatementPosition(int pos) {
737 CodeGenerator::RecordPositions(masm_, pos);
741 void FullCodeGenerator::SetSourcePosition(int pos) {
742 if (pos != RelocInfo::kNoPosition) {
743 masm_->positions_recorder()->RecordPosition(pos);
748 // Lookup table for code generators for special runtime calls which are
750 #define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \
751 &FullCodeGenerator::Emit##Name,
753 const FullCodeGenerator::InlineFunctionGenerator
754 FullCodeGenerator::kInlineFunctionGenerators[] = {
755 INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
756 INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
758 #undef INLINE_FUNCTION_GENERATOR_ADDRESS
761 FullCodeGenerator::InlineFunctionGenerator
762 FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) {
764 static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction);
765 ASSERT(lookup_index >= 0);
766 ASSERT(static_cast<size_t>(lookup_index) <
767 ARRAY_SIZE(kInlineFunctionGenerators));
768 return kInlineFunctionGenerators[lookup_index];
772 void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* expr) {
773 const Runtime::Function* function = expr->function();
774 ASSERT(function != NULL);
775 ASSERT(function->intrinsic_type == Runtime::INLINE);
776 InlineFunctionGenerator generator =
777 FindInlineFunctionGenerator(function->function_id);
778 ((*this).*(generator))(expr);
782 void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
783 switch (expr->op()) {
785 return VisitComma(expr);
788 return VisitLogicalExpression(expr);
790 return VisitArithmeticExpression(expr);
795 void FullCodeGenerator::VisitInDuplicateContext(Expression* expr) {
796 if (context()->IsEffect()) {
797 VisitForEffect(expr);
798 } else if (context()->IsAccumulatorValue()) {
799 VisitForAccumulatorValue(expr);
800 } else if (context()->IsStackValue()) {
801 VisitForStackValue(expr);
802 } else if (context()->IsTest()) {
803 const TestContext* test = TestContext::cast(context());
804 VisitForControl(expr, test->true_label(), test->false_label(),
805 test->fall_through());
810 void FullCodeGenerator::VisitComma(BinaryOperation* expr) {
811 Comment cmnt(masm_, "[ Comma");
812 VisitForEffect(expr->left());
813 VisitInDuplicateContext(expr->right());
817 void FullCodeGenerator::VisitLogicalExpression(BinaryOperation* expr) {
818 bool is_logical_and = expr->op() == Token::AND;
819 Comment cmnt(masm_, is_logical_and ? "[ Logical AND" : "[ Logical OR");
820 Expression* left = expr->left();
821 Expression* right = expr->right();
822 int right_id = expr->RightId();
825 if (context()->IsTest()) {
827 const TestContext* test = TestContext::cast(context());
828 if (is_logical_and) {
829 VisitForControl(left, &eval_right, test->false_label(), &eval_right);
831 VisitForControl(left, test->true_label(), &eval_right, &eval_right);
833 PrepareForBailoutForId(right_id, NO_REGISTERS);
834 __ bind(&eval_right);
836 } else if (context()->IsAccumulatorValue()) {
837 VisitForAccumulatorValue(left);
838 // We want the value in the accumulator for the test, and on the stack in
840 __ push(result_register());
841 Label discard, restore;
842 if (is_logical_and) {
843 DoTest(left, &discard, &restore, &restore);
845 DoTest(left, &restore, &discard, &restore);
848 __ pop(result_register());
852 PrepareForBailoutForId(right_id, NO_REGISTERS);
854 } else if (context()->IsStackValue()) {
855 VisitForAccumulatorValue(left);
856 // We want the value in the accumulator for the test, and on the stack in
858 __ push(result_register());
860 if (is_logical_and) {
861 DoTest(left, &discard, &done, &discard);
863 DoTest(left, &done, &discard, &discard);
867 PrepareForBailoutForId(right_id, NO_REGISTERS);
870 ASSERT(context()->IsEffect());
872 if (is_logical_and) {
873 VisitForControl(left, &eval_right, &done, &eval_right);
875 VisitForControl(left, &done, &eval_right, &eval_right);
877 PrepareForBailoutForId(right_id, NO_REGISTERS);
878 __ bind(&eval_right);
881 VisitInDuplicateContext(right);
886 void FullCodeGenerator::VisitArithmeticExpression(BinaryOperation* expr) {
887 Token::Value op = expr->op();
888 Comment cmnt(masm_, "[ ArithmeticExpression");
889 Expression* left = expr->left();
890 Expression* right = expr->right();
892 left->ResultOverwriteAllowed()
894 : (right->ResultOverwriteAllowed() ? OVERWRITE_RIGHT : NO_OVERWRITE);
896 VisitForStackValue(left);
897 VisitForAccumulatorValue(right);
899 SetSourcePosition(expr->position());
900 if (ShouldInlineSmiCase(op)) {
901 EmitInlineSmiBinaryOp(expr, op, mode, left, right);
903 EmitBinaryOp(expr, op, mode);
908 void FullCodeGenerator::VisitBlock(Block* stmt) {
909 Comment cmnt(masm_, "[ Block");
910 NestedBlock nested_block(this, stmt);
911 SetStatementPosition(stmt);
913 Scope* saved_scope = scope();
914 // Push a block context when entering a block with block scoped variables.
915 if (stmt->block_scope() != NULL) {
916 { Comment cmnt(masm_, "[ Extend block context");
917 scope_ = stmt->block_scope();
918 Handle<ScopeInfo> scope_info = scope_->GetScopeInfo();
919 int heap_slots = scope_info->ContextLength() - Context::MIN_CONTEXT_SLOTS;
921 PushFunctionArgumentForContextAllocation();
922 if (heap_slots <= FastNewBlockContextStub::kMaximumSlots) {
923 FastNewBlockContextStub stub(heap_slots);
926 __ CallRuntime(Runtime::kPushBlockContext, 2);
929 // Replace the context stored in the frame.
930 StoreToFrameField(StandardFrameConstants::kContextOffset,
933 { Comment cmnt(masm_, "[ Declarations");
934 VisitDeclarations(scope_->declarations());
937 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
938 VisitStatements(stmt->statements());
939 scope_ = saved_scope;
940 __ bind(nested_block.break_label());
941 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
943 // Pop block context if necessary.
944 if (stmt->block_scope() != NULL) {
945 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
946 // Update local stack frame context field.
947 StoreToFrameField(StandardFrameConstants::kContextOffset,
953 void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
954 Comment cmnt(masm_, "[ ExpressionStatement");
955 SetStatementPosition(stmt);
956 VisitForEffect(stmt->expression());
960 void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
961 Comment cmnt(masm_, "[ EmptyStatement");
962 SetStatementPosition(stmt);
966 void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
967 Comment cmnt(masm_, "[ IfStatement");
968 SetStatementPosition(stmt);
969 Label then_part, else_part, done;
971 if (stmt->HasElseStatement()) {
972 VisitForControl(stmt->condition(), &then_part, &else_part, &then_part);
973 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
975 Visit(stmt->then_statement());
978 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
980 Visit(stmt->else_statement());
982 VisitForControl(stmt->condition(), &then_part, &done, &then_part);
983 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
985 Visit(stmt->then_statement());
987 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
990 PrepareForBailoutForId(stmt->IfId(), NO_REGISTERS);
994 void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
995 Comment cmnt(masm_, "[ ContinueStatement");
996 SetStatementPosition(stmt);
997 NestedStatement* current = nesting_stack_;
999 int context_length = 0;
1000 // When continuing, we clobber the unpredictable value in the accumulator
1001 // with one that's safe for GC. If we hit an exit from the try block of
1002 // try...finally on our way out, we will unconditionally preserve the
1003 // accumulator on the stack.
1005 while (!current->IsContinueTarget(stmt->target())) {
1006 current = current->Exit(&stack_depth, &context_length);
1008 __ Drop(stack_depth);
1009 if (context_length > 0) {
1010 while (context_length > 0) {
1011 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1014 StoreToFrameField(StandardFrameConstants::kContextOffset,
1015 context_register());
1018 __ jmp(current->AsIteration()->continue_label());
1022 void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
1023 Comment cmnt(masm_, "[ BreakStatement");
1024 SetStatementPosition(stmt);
1025 NestedStatement* current = nesting_stack_;
1026 int stack_depth = 0;
1027 int context_length = 0;
1028 // When breaking, we clobber the unpredictable value in the accumulator
1029 // with one that's safe for GC. If we hit an exit from the try block of
1030 // try...finally on our way out, we will unconditionally preserve the
1031 // accumulator on the stack.
1033 while (!current->IsBreakTarget(stmt->target())) {
1034 current = current->Exit(&stack_depth, &context_length);
1036 __ Drop(stack_depth);
1037 if (context_length > 0) {
1038 while (context_length > 0) {
1039 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1042 StoreToFrameField(StandardFrameConstants::kContextOffset,
1043 context_register());
1046 __ jmp(current->AsBreakable()->break_label());
1050 void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
1051 Comment cmnt(masm_, "[ ReturnStatement");
1052 SetStatementPosition(stmt);
1053 Expression* expr = stmt->expression();
1054 VisitForAccumulatorValue(expr);
1056 // Exit all nested statements.
1057 NestedStatement* current = nesting_stack_;
1058 int stack_depth = 0;
1059 int context_length = 0;
1060 while (current != NULL) {
1061 current = current->Exit(&stack_depth, &context_length);
1063 __ Drop(stack_depth);
1065 EmitReturnSequence();
1069 void FullCodeGenerator::VisitWithStatement(WithStatement* stmt) {
1070 Comment cmnt(masm_, "[ WithStatement");
1071 SetStatementPosition(stmt);
1073 VisitForStackValue(stmt->expression());
1074 PushFunctionArgumentForContextAllocation();
1075 __ CallRuntime(Runtime::kPushWithContext, 2);
1076 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
1078 { WithOrCatch body(this);
1079 Visit(stmt->statement());
1083 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1084 // Update local stack frame context field.
1085 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
1089 void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
1090 Comment cmnt(masm_, "[ DoWhileStatement");
1091 SetStatementPosition(stmt);
1092 Label body, stack_check;
1094 Iteration loop_statement(this, stmt);
1095 increment_loop_depth();
1098 Visit(stmt->body());
1100 // Record the position of the do while condition and make sure it is
1101 // possible to break on the condition.
1102 __ bind(loop_statement.continue_label());
1103 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
1104 SetExpressionPosition(stmt->cond(), stmt->condition_position());
1105 VisitForControl(stmt->cond(),
1107 loop_statement.break_label(),
1110 // Check stack before looping.
1111 PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS);
1112 __ bind(&stack_check);
1113 EmitStackCheck(stmt, &body);
1116 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
1117 __ bind(loop_statement.break_label());
1118 decrement_loop_depth();
1122 void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
1123 Comment cmnt(masm_, "[ WhileStatement");
1126 Iteration loop_statement(this, stmt);
1127 increment_loop_depth();
1129 // Emit the test at the bottom of the loop.
1132 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
1134 Visit(stmt->body());
1136 // Emit the statement position here as this is where the while
1137 // statement code starts.
1138 __ bind(loop_statement.continue_label());
1139 SetStatementPosition(stmt);
1141 // Check stack before looping.
1142 EmitStackCheck(stmt, &body);
1145 VisitForControl(stmt->cond(),
1147 loop_statement.break_label(),
1148 loop_statement.break_label());
1150 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
1151 __ bind(loop_statement.break_label());
1152 decrement_loop_depth();
1156 void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
1157 Comment cmnt(masm_, "[ ForStatement");
1160 Iteration loop_statement(this, stmt);
1162 // Set statement position for a break slot before entering the for-body.
1163 SetStatementPosition(stmt);
1165 if (stmt->init() != NULL) {
1166 Visit(stmt->init());
1169 increment_loop_depth();
1170 // Emit the test at the bottom of the loop (even if empty).
1173 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
1175 Visit(stmt->body());
1177 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
1178 __ bind(loop_statement.continue_label());
1179 if (stmt->next() != NULL) {
1180 Visit(stmt->next());
1183 // Emit the statement position here as this is where the for
1184 // statement code starts.
1185 SetStatementPosition(stmt);
1187 // Check stack before looping.
1188 EmitStackCheck(stmt, &body);
1191 if (stmt->cond() != NULL) {
1192 VisitForControl(stmt->cond(),
1194 loop_statement.break_label(),
1195 loop_statement.break_label());
1200 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
1201 __ bind(loop_statement.break_label());
1202 decrement_loop_depth();
1206 void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1207 Comment cmnt(masm_, "[ TryCatchStatement");
1208 SetStatementPosition(stmt);
1209 // The try block adds a handler to the exception handler chain before
1210 // entering, and removes it again when exiting normally. If an exception
1211 // is thrown during execution of the try block, the handler is consumed
1212 // and control is passed to the catch block with the exception in the
1215 Label try_entry, handler_entry, exit;
1217 __ bind(&handler_entry);
1218 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1219 // Exception handler code, the exception is in the result register.
1220 // Extend the context before executing the catch block.
1221 { Comment cmnt(masm_, "[ Extend catch context");
1222 __ Push(stmt->variable()->name());
1223 __ push(result_register());
1224 PushFunctionArgumentForContextAllocation();
1225 __ CallRuntime(Runtime::kPushCatchContext, 3);
1226 StoreToFrameField(StandardFrameConstants::kContextOffset,
1227 context_register());
1230 Scope* saved_scope = scope();
1231 scope_ = stmt->scope();
1232 ASSERT(scope_->declarations()->is_empty());
1233 { WithOrCatch catch_body(this);
1234 Visit(stmt->catch_block());
1236 // Restore the context.
1237 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1238 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
1239 scope_ = saved_scope;
1242 // Try block code. Sets up the exception handler chain.
1243 __ bind(&try_entry);
1244 __ PushTryHandler(StackHandler::CATCH, stmt->index());
1245 { TryCatch try_body(this);
1246 Visit(stmt->try_block());
1253 void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1254 Comment cmnt(masm_, "[ TryFinallyStatement");
1255 SetStatementPosition(stmt);
1256 // Try finally is compiled by setting up a try-handler on the stack while
1257 // executing the try body, and removing it again afterwards.
1259 // The try-finally construct can enter the finally block in three ways:
1260 // 1. By exiting the try-block normally. This removes the try-handler and
1261 // calls the finally block code before continuing.
1262 // 2. By exiting the try-block with a function-local control flow transfer
1263 // (break/continue/return). The site of the, e.g., break removes the
1264 // try handler and calls the finally block code before continuing
1265 // its outward control transfer.
1266 // 3. By exiting the try-block with a thrown exception.
1267 // This can happen in nested function calls. It traverses the try-handler
1268 // chain and consumes the try-handler entry before jumping to the
1269 // handler code. The handler code then calls the finally-block before
1270 // rethrowing the exception.
1272 // The finally block must assume a return address on top of the stack
1273 // (or in the link register on ARM chips) and a value (return value or
1274 // exception) in the result register (rax/eax/r0), both of which must
1275 // be preserved. The return address isn't GC-safe, so it should be
1276 // cooked before GC.
1277 Label try_entry, handler_entry, finally_entry;
1279 // Jump to try-handler setup and try-block code.
1281 __ bind(&handler_entry);
1282 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1283 // Exception handler code. This code is only executed when an exception
1284 // is thrown. The exception is in the result register, and must be
1285 // preserved by the finally block. Call the finally block and then
1286 // rethrow the exception if it returns.
1287 __ Call(&finally_entry);
1288 __ push(result_register());
1289 __ CallRuntime(Runtime::kReThrow, 1);
1291 // Finally block implementation.
1292 __ bind(&finally_entry);
1293 EnterFinallyBlock();
1294 { Finally finally_body(this);
1295 Visit(stmt->finally_block());
1297 ExitFinallyBlock(); // Return to the calling code.
1299 // Set up try handler.
1300 __ bind(&try_entry);
1301 __ PushTryHandler(StackHandler::FINALLY, stmt->index());
1302 { TryFinally try_body(this, &finally_entry);
1303 Visit(stmt->try_block());
1306 // Execute the finally block on the way out. Clobber the unpredictable
1307 // value in the result register with one that's safe for GC because the
1308 // finally block will unconditionally preserve the result register on the
1311 __ Call(&finally_entry);
1315 void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1316 #ifdef ENABLE_DEBUGGER_SUPPORT
1317 Comment cmnt(masm_, "[ DebuggerStatement");
1318 SetStatementPosition(stmt);
1321 // Ignore the return value.
1326 void FullCodeGenerator::VisitConditional(Conditional* expr) {
1327 Comment cmnt(masm_, "[ Conditional");
1328 Label true_case, false_case, done;
1329 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
1331 PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS);
1332 __ bind(&true_case);
1333 SetExpressionPosition(expr->then_expression(),
1334 expr->then_expression_position());
1335 if (context()->IsTest()) {
1336 const TestContext* for_test = TestContext::cast(context());
1337 VisitForControl(expr->then_expression(),
1338 for_test->true_label(),
1339 for_test->false_label(),
1342 VisitInDuplicateContext(expr->then_expression());
1346 PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
1347 __ bind(&false_case);
1348 SetExpressionPosition(expr->else_expression(),
1349 expr->else_expression_position());
1350 VisitInDuplicateContext(expr->else_expression());
1351 // If control flow falls through Visit, merge it with true case here.
1352 if (!context()->IsTest()) {
1358 void FullCodeGenerator::VisitLiteral(Literal* expr) {
1359 Comment cmnt(masm_, "[ Literal");
1360 context()->Plug(expr->handle());
1364 void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1365 Comment cmnt(masm_, "[ FunctionLiteral");
1367 // Build the function boilerplate and instantiate it.
1368 Handle<SharedFunctionInfo> function_info =
1369 Compiler::BuildFunctionInfo(expr, script());
1370 if (function_info.is_null()) {
1374 EmitNewClosure(function_info, expr->pretenure());
1378 void FullCodeGenerator::VisitSharedFunctionInfoLiteral(
1379 SharedFunctionInfoLiteral* expr) {
1380 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
1381 EmitNewClosure(expr->shared_function_info(), false);
1385 void FullCodeGenerator::VisitThrow(Throw* expr) {
1386 Comment cmnt(masm_, "[ Throw");
1387 VisitForStackValue(expr->exception());
1388 __ CallRuntime(Runtime::kThrow, 1);
1389 // Never returns here.
1393 FullCodeGenerator::NestedStatement* FullCodeGenerator::TryCatch::Exit(
1395 int* context_length) {
1396 // The macros used here must preserve the result register.
1397 __ Drop(*stack_depth);
1404 bool FullCodeGenerator::TryLiteralCompare(CompareOperation* expr) {
1405 Expression* sub_expr;
1406 Handle<String> check;
1407 if (expr->IsLiteralCompareTypeof(&sub_expr, &check)) {
1408 EmitLiteralCompareTypeof(expr, sub_expr, check);
1412 if (expr->IsLiteralCompareUndefined(&sub_expr)) {
1413 EmitLiteralCompareNil(expr, sub_expr, kUndefinedValue);
1417 if (expr->IsLiteralCompareNull(&sub_expr)) {
1418 EmitLiteralCompareNil(expr, sub_expr, kNullValue);
1429 } } // namespace v8::internal