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: Source code is translated into an anonymous function w/o | |
Yang
2016/03/07 06:01:08
I'd prefer "script source code" to be clear that t
Michael Starzinger
2016/03/08 10:23:24
Done.
| |
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. | |
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); | |
Yang
2016/03/07 06:01:08
Comment does not really apply to this one. Compile
Michael Starzinger
2016/03/08 10:23:24
Done. Added a remark as discussed offline.
| |
51 | |
52 // Parser::Parse, then Compiler::Analyze. | |
53 static bool ParseAndAnalyze(ParseInfo* info); | |
54 // Rewrite, analyze scopes, and renumber. | |
55 static bool Analyze(ParseInfo* info); | |
56 // Adds deoptimization support, requires ParseAndAnalyze. | |
57 static bool EnsureDeoptimizationSupport(CompilationInfo* info); | |
58 | |
59 // =========================================================================== | |
60 // The following family of methods instantiates new functions for scripts or | |
61 // function literals. The decision whether those functions will be compiled, | |
62 // is left to the discretion of the compiler. | |
63 // | |
64 // Please note this interface returns shared function infos. This means you | |
65 // need to call Factory::NewFunctionFromSharedFunctionInfo before you have a | |
66 // real function with a context. | |
67 | |
68 // Create a (bound) function for a String source within a context for eval. | |
69 MUST_USE_RESULT static MaybeHandle<JSFunction> GetFunctionFromEval( | |
70 Handle<String> source, Handle<SharedFunctionInfo> outer_info, | |
71 Handle<Context> context, LanguageMode language_mode, | |
72 ParseRestriction restriction, int line_offset, int column_offset = 0, | |
73 Handle<Object> script_name = Handle<Object>(), | |
74 ScriptOriginOptions options = ScriptOriginOptions()); | |
75 | |
76 // Create a shared function info object for a String source within a context. | |
77 static Handle<SharedFunctionInfo> GetSharedFunctionInfoForScript( | |
78 Handle<String> source, Handle<Object> script_name, int line_offset, | |
79 int column_offset, ScriptOriginOptions resource_options, | |
80 Handle<Object> source_map_url, Handle<Context> context, | |
81 v8::Extension* extension, ScriptData** cached_data, | |
82 ScriptCompiler::CompileOptions compile_options, | |
83 NativesFlag is_natives_code, bool is_module); | |
84 | |
85 // Create a shared function info object for a Script that has already been | |
86 // parsed while the script was being loaded from a streamed source. | |
87 static Handle<SharedFunctionInfo> GetSharedFunctionInfoForStreamedScript( | |
88 Handle<Script> script, ParseInfo* info, int source_length); | |
89 | |
90 // Create a shared function info object (the code may be lazily compiled). | |
91 static Handle<SharedFunctionInfo> GetSharedFunctionInfo( | |
92 FunctionLiteral* node, Handle<Script> script, CompilationInfo* outer); | |
93 | |
94 // Create a shared function info object for a native function literal. | |
95 static Handle<SharedFunctionInfo> GetSharedFunctionInfoForNative( | |
96 v8::Extension* extension, Handle<String> name); | |
97 | |
98 // =========================================================================== | |
99 // The following family of methods provides support for OSR. Code generated | |
100 // for entry via OSR might not be suitable for normal entry, hence will be | |
101 // returned directly to the caller. | |
102 // | |
103 // Please note this interface is the only part dealing with {Code} objects | |
104 // directly. Other methods are agnostic to {Code} and can use an interpreter | |
105 // instead of generating JIT code for a function at all. | |
106 | |
107 // Generate and return optimized code for OSR, or empty handle on failure. | |
108 MUST_USE_RESULT static MaybeHandle<Code> GetOptimizedCodeForOSR( | |
109 Handle<JSFunction> function, ConcurrencyMode mode, BailoutId osr_ast_id, | |
110 JavaScriptFrame* osr_frame); | |
111 | |
112 // Generate and return code from previously queued optimization job. | |
113 // On failure, return the empty handle. | |
114 MUST_USE_RESULT static MaybeHandle<Code> GetConcurrentlyOptimizedCode( | |
115 OptimizedCompileJob* job); | |
116 }; | |
24 | 117 |
25 struct InlinedFunctionInfo { | 118 struct InlinedFunctionInfo { |
26 InlinedFunctionInfo(int parent_id, SourcePosition inline_position, | 119 InlinedFunctionInfo(int parent_id, SourcePosition inline_position, |
27 int script_id, int start_position) | 120 int script_id, int start_position) |
28 : parent_id(parent_id), | 121 : parent_id(parent_id), |
29 inline_position(inline_position), | 122 inline_position(inline_position), |
30 script_id(script_id), | 123 script_id(script_id), |
31 start_position(start_position) {} | 124 start_position(start_position) {} |
32 int parent_id; | 125 int parent_id; |
33 SourcePosition inline_position; | 126 SourcePosition inline_position; |
(...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
564 ~Timer() { | 657 ~Timer() { |
565 *location_ += timer_.Elapsed(); | 658 *location_ += timer_.Elapsed(); |
566 } | 659 } |
567 | 660 |
568 OptimizedCompileJob* job_; | 661 OptimizedCompileJob* job_; |
569 base::ElapsedTimer timer_; | 662 base::ElapsedTimer timer_; |
570 base::TimeDelta* location_; | 663 base::TimeDelta* location_; |
571 }; | 664 }; |
572 }; | 665 }; |
573 | 666 |
574 // The V8 compiler API. | |
575 // | |
576 // This is the central hub for dispatching to the various compilers within V8. | |
577 // Logic for which compiler to choose and how to wire compilation results into | |
578 // the object heap should be kept inside this class. | |
579 // | |
580 // General strategy: Source code is translated into an anonymous function w/o | |
581 // parameters which then can be executed. If the source code contains other | |
582 // functions, they might be compiled and allocated as part of the compilation | |
583 // of the source code or deferred for lazy compilation at a later point. | |
584 class Compiler : public AllStatic { | |
585 public: | |
586 enum ClearExceptionFlag { KEEP_EXCEPTION, CLEAR_EXCEPTION }; | |
587 enum ConcurrencyMode { NOT_CONCURRENT, CONCURRENT }; | |
588 | |
589 // =========================================================================== | |
590 // The following family of methods ensures a given function is compiled. The | |
591 // general contract is that failures will be reported by returning {false}, | |
592 // whereas successful compilation ensures the {is_compiled} predicate on the | |
593 // given function holds. | |
594 | |
595 static bool Compile(Handle<JSFunction> function, ClearExceptionFlag flag); | |
596 static bool CompileOptimized(Handle<JSFunction> function, ConcurrencyMode); | |
597 static bool CompileDebugCode(Handle<JSFunction> function); | |
598 static bool CompileDebugCode(Handle<SharedFunctionInfo> shared); | |
599 static void CompileForLiveEdit(Handle<Script> script); | |
600 | |
601 // Parser::Parse, then Compiler::Analyze. | |
602 static bool ParseAndAnalyze(ParseInfo* info); | |
603 // Rewrite, analyze scopes, and renumber. | |
604 static bool Analyze(ParseInfo* info); | |
605 // Adds deoptimization support, requires ParseAndAnalyze. | |
606 static bool EnsureDeoptimizationSupport(CompilationInfo* info); | |
607 | |
608 // =========================================================================== | |
609 // The following family of methods instantiates new functions for script or | |
610 // function literals. The decision whether those functions have been compiled | |
611 // is left to the discretion of the compiler. | |
612 // | |
613 // Please note this interface returns shared function infos. This means you | |
614 // need to call Factory::NewFunctionFromSharedFunctionInfo before you have a | |
615 // real function with a context. | |
616 | |
617 // Compile a String source within a context for eval. | |
618 MUST_USE_RESULT static MaybeHandle<JSFunction> GetFunctionFromEval( | |
619 Handle<String> source, Handle<SharedFunctionInfo> outer_info, | |
620 Handle<Context> context, LanguageMode language_mode, | |
621 ParseRestriction restriction, int line_offset, int column_offset = 0, | |
622 Handle<Object> script_name = Handle<Object>(), | |
623 ScriptOriginOptions options = ScriptOriginOptions()); | |
624 | |
625 // Compile a String source within a context. | |
626 static Handle<SharedFunctionInfo> CompileScript( | |
627 Handle<String> source, Handle<Object> script_name, int line_offset, | |
628 int column_offset, ScriptOriginOptions resource_options, | |
629 Handle<Object> source_map_url, Handle<Context> context, | |
630 v8::Extension* extension, ScriptData** cached_data, | |
631 ScriptCompiler::CompileOptions compile_options, | |
632 NativesFlag is_natives_code, bool is_module); | |
633 | |
634 static Handle<SharedFunctionInfo> CompileStreamedScript(Handle<Script> script, | |
635 ParseInfo* info, | |
636 int source_length); | |
637 | |
638 // Create a shared function info object (the code may be lazily compiled). | |
639 static Handle<SharedFunctionInfo> GetSharedFunctionInfo( | |
640 FunctionLiteral* node, Handle<Script> script, CompilationInfo* outer); | |
641 | |
642 // Create a shared function info object for a native function literal. | |
643 static Handle<SharedFunctionInfo> GetSharedFunctionInfoForNative( | |
644 v8::Extension* extension, Handle<String> name); | |
645 | |
646 // =========================================================================== | |
647 // The following family of methods provides support for OSR. Code generated | |
648 // for entry via OSR might not be suitable for normal entry, hence will be | |
649 // returned directly to the caller. | |
650 // | |
651 // Please note this interface is the only part dealing with {Code} objects | |
652 // directly. Other methods are agnostic to {Code} and can use an interpreter | |
653 // instead of generating JIT code for a function at all. | |
654 | |
655 // Generate and return optimized code for OSR, or empty handle on failure. | |
656 MUST_USE_RESULT static MaybeHandle<Code> GetOptimizedCodeForOSR( | |
657 Handle<JSFunction> function, ConcurrencyMode mode, BailoutId osr_ast_id, | |
658 JavaScriptFrame* osr_frame); | |
659 | |
660 // Generate and return code from previously queued optimization job. | |
661 // On failure, return the empty handle. | |
662 MUST_USE_RESULT static MaybeHandle<Code> GetConcurrentlyOptimizedCode( | |
663 OptimizedCompileJob* job); | |
664 }; | |
665 | |
666 } // namespace internal | 667 } // namespace internal |
667 } // namespace v8 | 668 } // namespace v8 |
668 | 669 |
669 #endif // V8_COMPILER_H_ | 670 #endif // V8_COMPILER_H_ |
OLD | NEW |