| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef V8_COMPILER_H_ | 5 #ifndef V8_COMPILER_H_ |
| 6 #define V8_COMPILER_H_ | 6 #define V8_COMPILER_H_ |
| 7 | 7 |
| 8 #include "src/allocation.h" | 8 #include "src/allocation.h" |
| 9 #include "src/ast/ast.h" | 9 #include "src/ast/ast.h" |
| 10 #include "src/bailout-reason.h" | 10 #include "src/bailout-reason.h" |
| 11 #include "src/compilation-dependencies.h" | 11 #include "src/compilation-dependencies.h" |
| 12 #include "src/signature.h" | |
| 13 #include "src/source-position.h" | 12 #include "src/source-position.h" |
| 14 #include "src/zone.h" | 13 #include "src/zone.h" |
| 15 | 14 |
| 16 namespace v8 { | 15 namespace v8 { |
| 17 namespace internal { | 16 namespace internal { |
| 18 | 17 |
| 19 // Forward declarations. | 18 // Forward declarations. |
| 19 class CompilationInfo; |
| 20 class JavaScriptFrame; | 20 class JavaScriptFrame; |
| 21 class OptimizedCompileJob; |
| 21 class ParseInfo; | 22 class ParseInfo; |
| 22 class ScriptData; | 23 class ScriptData; |
| 23 | 24 |
| 25 // The V8 compiler API. |
| 26 // |
| 27 // This is the central hub for dispatching to the various compilers within V8. |
| 28 // Logic for which compiler to choose and how to wire compilation results into |
| 29 // the object heap should be kept inside this class. |
| 30 // |
| 31 // General strategy: Scripts are translated into anonymous functions w/o |
| 32 // parameters which then can be executed. If the source code contains other |
| 33 // functions, they might be compiled and allocated as part of the compilation |
| 34 // of the source code or deferred for lazy compilation at a later point. |
| 35 class Compiler : public AllStatic { |
| 36 public: |
| 37 enum ClearExceptionFlag { KEEP_EXCEPTION, CLEAR_EXCEPTION }; |
| 38 enum ConcurrencyMode { NOT_CONCURRENT, CONCURRENT }; |
| 39 |
| 40 // =========================================================================== |
| 41 // The following family of methods ensures a given function is compiled. The |
| 42 // general contract is that failures will be reported by returning {false}, |
| 43 // whereas successful compilation ensures the {is_compiled} predicate on the |
| 44 // given function holds (except for live-edit, which compiles the world). |
| 45 |
| 46 static bool Compile(Handle<JSFunction> function, ClearExceptionFlag flag); |
| 47 static bool CompileOptimized(Handle<JSFunction> function, ConcurrencyMode); |
| 48 static bool CompileDebugCode(Handle<JSFunction> function); |
| 49 static bool CompileDebugCode(Handle<SharedFunctionInfo> shared); |
| 50 static void CompileForLiveEdit(Handle<Script> script); |
| 51 |
| 52 // Generate and install code from previously queued optimization job. |
| 53 static void FinalizeOptimizedCompileJob(OptimizedCompileJob* job); |
| 54 |
| 55 // Give the compiler a chance to perform low-latency initialization tasks of |
| 56 // the given {function} on its instantiation. Note that only the runtime will |
| 57 // offer this chance, optimized closure instantiation will not call this. |
| 58 static void PostInstantiation(Handle<JSFunction> function, PretenureFlag); |
| 59 |
| 60 // Parser::Parse, then Compiler::Analyze. |
| 61 static bool ParseAndAnalyze(ParseInfo* info); |
| 62 // Rewrite, analyze scopes, and renumber. |
| 63 static bool Analyze(ParseInfo* info); |
| 64 // Adds deoptimization support, requires ParseAndAnalyze. |
| 65 static bool EnsureDeoptimizationSupport(CompilationInfo* info); |
| 66 |
| 67 // =========================================================================== |
| 68 // The following family of methods instantiates new functions for scripts or |
| 69 // function literals. The decision whether those functions will be compiled, |
| 70 // is left to the discretion of the compiler. |
| 71 // |
| 72 // Please note this interface returns shared function infos. This means you |
| 73 // need to call Factory::NewFunctionFromSharedFunctionInfo before you have a |
| 74 // real function with a context. |
| 75 |
| 76 // Create a (bound) function for a String source within a context for eval. |
| 77 MUST_USE_RESULT static MaybeHandle<JSFunction> GetFunctionFromEval( |
| 78 Handle<String> source, Handle<SharedFunctionInfo> outer_info, |
| 79 Handle<Context> context, LanguageMode language_mode, |
| 80 ParseRestriction restriction, int line_offset, int column_offset = 0, |
| 81 Handle<Object> script_name = Handle<Object>(), |
| 82 ScriptOriginOptions options = ScriptOriginOptions()); |
| 83 |
| 84 // Create a shared function info object for a String source within a context. |
| 85 static Handle<SharedFunctionInfo> GetSharedFunctionInfoForScript( |
| 86 Handle<String> source, Handle<Object> script_name, int line_offset, |
| 87 int column_offset, ScriptOriginOptions resource_options, |
| 88 Handle<Object> source_map_url, Handle<Context> context, |
| 89 v8::Extension* extension, ScriptData** cached_data, |
| 90 ScriptCompiler::CompileOptions compile_options, |
| 91 NativesFlag is_natives_code, bool is_module); |
| 92 |
| 93 // Create a shared function info object for a Script that has already been |
| 94 // parsed while the script was being loaded from a streamed source. |
| 95 static Handle<SharedFunctionInfo> GetSharedFunctionInfoForStreamedScript( |
| 96 Handle<Script> script, ParseInfo* info, int source_length); |
| 97 |
| 98 // Create a shared function info object (the code may be lazily compiled). |
| 99 static Handle<SharedFunctionInfo> GetSharedFunctionInfo( |
| 100 FunctionLiteral* node, Handle<Script> script, CompilationInfo* outer); |
| 101 |
| 102 // Create a shared function info object for a native function literal. |
| 103 static Handle<SharedFunctionInfo> GetSharedFunctionInfoForNative( |
| 104 v8::Extension* extension, Handle<String> name); |
| 105 |
| 106 // =========================================================================== |
| 107 // The following family of methods provides support for OSR. Code generated |
| 108 // for entry via OSR might not be suitable for normal entry, hence will be |
| 109 // returned directly to the caller. |
| 110 // |
| 111 // Please note this interface is the only part dealing with {Code} objects |
| 112 // directly. Other methods are agnostic to {Code} and can use an interpreter |
| 113 // instead of generating JIT code for a function at all. |
| 114 |
| 115 // Generate and return optimized code for OSR, or empty handle on failure. |
| 116 MUST_USE_RESULT static MaybeHandle<Code> GetOptimizedCodeForOSR( |
| 117 Handle<JSFunction> function, BailoutId osr_ast_id, |
| 118 JavaScriptFrame* osr_frame); |
| 119 }; |
| 24 | 120 |
| 25 struct InlinedFunctionInfo { | 121 struct InlinedFunctionInfo { |
| 26 InlinedFunctionInfo(int parent_id, SourcePosition inline_position, | 122 InlinedFunctionInfo(int parent_id, SourcePosition inline_position, |
| 27 int script_id, int start_position) | 123 int script_id, int start_position) |
| 28 : parent_id(parent_id), | 124 : parent_id(parent_id), |
| 29 inline_position(inline_position), | 125 inline_position(inline_position), |
| 30 script_id(script_id), | 126 script_id(script_id), |
| 31 start_position(start_position) {} | 127 start_position(start_position) {} |
| 32 int parent_id; | 128 int parent_id; |
| 33 SourcePosition inline_position; | 129 SourcePosition inline_position; |
| (...skipping 520 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 554 ~Timer() { | 650 ~Timer() { |
| 555 *location_ += timer_.Elapsed(); | 651 *location_ += timer_.Elapsed(); |
| 556 } | 652 } |
| 557 | 653 |
| 558 OptimizedCompileJob* job_; | 654 OptimizedCompileJob* job_; |
| 559 base::ElapsedTimer timer_; | 655 base::ElapsedTimer timer_; |
| 560 base::TimeDelta* location_; | 656 base::TimeDelta* location_; |
| 561 }; | 657 }; |
| 562 }; | 658 }; |
| 563 | 659 |
| 564 // The V8 compiler API. | |
| 565 // | |
| 566 // This is the central hub for dispatching to the various compilers within V8. | |
| 567 // Logic for which compiler to choose and how to wire compilation results into | |
| 568 // the object heap should be kept inside this class. | |
| 569 // | |
| 570 // General strategy: Source code is translated into an anonymous function w/o | |
| 571 // parameters which then can be executed. If the source code contains other | |
| 572 // functions, they might be compiled and allocated as part of the compilation | |
| 573 // of the source code or deferred for lazy compilation at a later point. | |
| 574 class Compiler : public AllStatic { | |
| 575 public: | |
| 576 enum ClearExceptionFlag { KEEP_EXCEPTION, CLEAR_EXCEPTION }; | |
| 577 enum ConcurrencyMode { NOT_CONCURRENT, CONCURRENT }; | |
| 578 | |
| 579 // =========================================================================== | |
| 580 // The following family of methods ensures a given function is compiled. The | |
| 581 // general contract is that failures will be reported by returning {false}, | |
| 582 // whereas successful compilation ensures the {is_compiled} predicate on the | |
| 583 // given function holds. | |
| 584 | |
| 585 static bool Compile(Handle<JSFunction> function, ClearExceptionFlag flag); | |
| 586 static bool CompileOptimized(Handle<JSFunction> function, ConcurrencyMode); | |
| 587 static bool CompileDebugCode(Handle<JSFunction> function); | |
| 588 static bool CompileDebugCode(Handle<SharedFunctionInfo> shared); | |
| 589 static void CompileForLiveEdit(Handle<Script> script); | |
| 590 | |
| 591 // Generate and install code from previously queued optimization job. | |
| 592 static void FinalizeOptimizedCompileJob(OptimizedCompileJob* job); | |
| 593 | |
| 594 // Give the compiler a chance to perform low-latency initialization tasks of | |
| 595 // the given {function} on its instantiation. Note that only the runtime will | |
| 596 // offer this chance, optimized closure instantiation will not call this. | |
| 597 static void PostInstantiation(Handle<JSFunction> function, PretenureFlag); | |
| 598 | |
| 599 // Parser::Parse, then Compiler::Analyze. | |
| 600 static bool ParseAndAnalyze(ParseInfo* info); | |
| 601 // Rewrite, analyze scopes, and renumber. | |
| 602 static bool Analyze(ParseInfo* info); | |
| 603 // Adds deoptimization support, requires ParseAndAnalyze. | |
| 604 static bool EnsureDeoptimizationSupport(CompilationInfo* info); | |
| 605 | |
| 606 // =========================================================================== | |
| 607 // The following family of methods instantiates new functions for script or | |
| 608 // function literals. The decision whether those functions have been compiled | |
| 609 // is left to the discretion of the compiler. | |
| 610 // | |
| 611 // Please note this interface returns shared function infos. This means you | |
| 612 // need to call Factory::NewFunctionFromSharedFunctionInfo before you have a | |
| 613 // real function with a context. | |
| 614 | |
| 615 // Compile a String source within a context for eval. | |
| 616 MUST_USE_RESULT static MaybeHandle<JSFunction> GetFunctionFromEval( | |
| 617 Handle<String> source, Handle<SharedFunctionInfo> outer_info, | |
| 618 Handle<Context> context, LanguageMode language_mode, | |
| 619 ParseRestriction restriction, int line_offset, int column_offset = 0, | |
| 620 Handle<Object> script_name = Handle<Object>(), | |
| 621 ScriptOriginOptions options = ScriptOriginOptions()); | |
| 622 | |
| 623 // Compile a String source within a context. | |
| 624 static Handle<SharedFunctionInfo> CompileScript( | |
| 625 Handle<String> source, Handle<Object> script_name, int line_offset, | |
| 626 int column_offset, ScriptOriginOptions resource_options, | |
| 627 Handle<Object> source_map_url, Handle<Context> context, | |
| 628 v8::Extension* extension, ScriptData** cached_data, | |
| 629 ScriptCompiler::CompileOptions compile_options, | |
| 630 NativesFlag is_natives_code, bool is_module); | |
| 631 | |
| 632 static Handle<SharedFunctionInfo> CompileStreamedScript(Handle<Script> script, | |
| 633 ParseInfo* info, | |
| 634 int source_length); | |
| 635 | |
| 636 // Create a shared function info object (the code may be lazily compiled). | |
| 637 static Handle<SharedFunctionInfo> GetSharedFunctionInfo( | |
| 638 FunctionLiteral* node, Handle<Script> script, CompilationInfo* outer); | |
| 639 | |
| 640 // Create a shared function info object for a native function literal. | |
| 641 static Handle<SharedFunctionInfo> GetSharedFunctionInfoForNative( | |
| 642 v8::Extension* extension, Handle<String> name); | |
| 643 | |
| 644 // =========================================================================== | |
| 645 // The following family of methods provides support for OSR. Code generated | |
| 646 // for entry via OSR might not be suitable for normal entry, hence will be | |
| 647 // returned directly to the caller. | |
| 648 // | |
| 649 // Please note this interface is the only part dealing with {Code} objects | |
| 650 // directly. Other methods are agnostic to {Code} and can use an interpreter | |
| 651 // instead of generating JIT code for a function at all. | |
| 652 | |
| 653 // Generate and return optimized code for OSR, or empty handle on failure. | |
| 654 MUST_USE_RESULT static MaybeHandle<Code> GetOptimizedCodeForOSR( | |
| 655 Handle<JSFunction> function, BailoutId osr_ast_id, | |
| 656 JavaScriptFrame* osr_frame); | |
| 657 }; | |
| 658 | |
| 659 } // namespace internal | 660 } // namespace internal |
| 660 } // namespace v8 | 661 } // namespace v8 |
| 661 | 662 |
| 662 #endif // V8_COMPILER_H_ | 663 #endif // V8_COMPILER_H_ |
| OLD | NEW |