Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #ifndef V8_COMPILER_H_ |
| 29 | #define V8_COMPILER_H_ |
| 30 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 31 | #include "ast.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 32 | #include "frame-element.h" |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 33 | #include "register-allocator.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 34 | #include "zone.h" |
| 35 | |
| 36 | namespace v8 { |
| 37 | namespace internal { |
| 38 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 39 | class ScriptDataImpl; |
| 40 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 41 | // CompilationInfo encapsulates some information known at compile time. It |
| 42 | // is constructed based on the resources available at compile-time. |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 43 | class CompilationInfo BASE_EMBEDDED { |
| 44 | public: |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 45 | explicit CompilationInfo(Handle<Script> script); |
| 46 | explicit CompilationInfo(Handle<SharedFunctionInfo> shared_info); |
| 47 | explicit CompilationInfo(Handle<JSFunction> closure); |
| 48 | |
| 49 | bool is_lazy() const { return (flags_ & IsLazy::mask()) != 0; } |
| 50 | bool is_eval() const { return (flags_ & IsEval::mask()) != 0; } |
| 51 | bool is_global() const { return (flags_ & IsGlobal::mask()) != 0; } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 52 | bool is_in_loop() const { return (flags_ & IsInLoop::mask()) != 0; } |
| 53 | FunctionLiteral* function() const { return function_; } |
| 54 | Scope* scope() const { return scope_; } |
| 55 | Handle<Code> code() const { return code_; } |
| 56 | Handle<JSFunction> closure() const { return closure_; } |
| 57 | Handle<SharedFunctionInfo> shared_info() const { return shared_info_; } |
| 58 | Handle<Script> script() const { return script_; } |
| 59 | v8::Extension* extension() const { return extension_; } |
| 60 | ScriptDataImpl* pre_parse_data() const { return pre_parse_data_; } |
| 61 | Handle<Context> calling_context() const { return calling_context_; } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 62 | int osr_ast_id() const { return osr_ast_id_; } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 63 | |
| 64 | void MarkAsEval() { |
| 65 | ASSERT(!is_lazy()); |
| 66 | flags_ |= IsEval::encode(true); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 67 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 68 | void MarkAsGlobal() { |
| 69 | ASSERT(!is_lazy()); |
| 70 | flags_ |= IsGlobal::encode(true); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 71 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 72 | void MarkAsInLoop() { |
| 73 | ASSERT(is_lazy()); |
| 74 | flags_ |= IsInLoop::encode(true); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 75 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 76 | void SetFunction(FunctionLiteral* literal) { |
| 77 | ASSERT(function_ == NULL); |
| 78 | function_ = literal; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 79 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 80 | void SetScope(Scope* scope) { |
| 81 | ASSERT(scope_ == NULL); |
| 82 | scope_ = scope; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 83 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 84 | void SetCode(Handle<Code> code) { code_ = code; } |
| 85 | void SetExtension(v8::Extension* extension) { |
| 86 | ASSERT(!is_lazy()); |
| 87 | extension_ = extension; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 88 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 89 | void SetPreParseData(ScriptDataImpl* pre_parse_data) { |
| 90 | ASSERT(!is_lazy()); |
| 91 | pre_parse_data_ = pre_parse_data; |
| 92 | } |
| 93 | void SetCallingContext(Handle<Context> context) { |
| 94 | ASSERT(is_eval()); |
| 95 | calling_context_ = context; |
| 96 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 97 | void SetOsrAstId(int osr_ast_id) { |
| 98 | ASSERT(IsOptimizing()); |
| 99 | osr_ast_id_ = osr_ast_id; |
| 100 | } |
| 101 | |
| 102 | bool has_global_object() const { |
| 103 | return !closure().is_null() && (closure()->context()->global() != NULL); |
| 104 | } |
| 105 | |
| 106 | GlobalObject* global_object() const { |
| 107 | return has_global_object() ? closure()->context()->global() : NULL; |
| 108 | } |
| 109 | |
| 110 | // Accessors for the different compilation modes. |
| 111 | bool IsOptimizing() const { return mode_ == OPTIMIZE; } |
| 112 | bool IsOptimizable() const { return mode_ == BASE; } |
| 113 | void SetOptimizing(int osr_ast_id) { |
| 114 | SetMode(OPTIMIZE); |
| 115 | osr_ast_id_ = osr_ast_id; |
| 116 | } |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame^] | 117 | void DisableOptimization(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 118 | |
| 119 | // Deoptimization support. |
| 120 | bool HasDeoptimizationSupport() const { return supports_deoptimization_; } |
| 121 | void EnableDeoptimizationSupport() { |
| 122 | ASSERT(IsOptimizable()); |
| 123 | supports_deoptimization_ = true; |
| 124 | } |
| 125 | |
| 126 | // Determine whether or not we can adaptively optimize. |
| 127 | bool AllowOptimize() { |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame^] | 128 | return V8::UseCrankshaft() && !closure_.is_null(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 129 | } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 130 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 131 | private: |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 132 | // Compilation mode. |
| 133 | // BASE is generated by the full codegen, optionally prepared for bailouts. |
| 134 | // OPTIMIZE is optimized code generated by the Hydrogen-based backend. |
| 135 | // NONOPT is generated by the full codegen or the classic backend |
| 136 | // and is not prepared for recompilation/bailouts. These functions |
| 137 | // are never recompiled. |
| 138 | enum Mode { |
| 139 | BASE, |
| 140 | OPTIMIZE, |
| 141 | NONOPT |
| 142 | }; |
| 143 | |
| 144 | CompilationInfo() : function_(NULL) {} |
| 145 | |
| 146 | void Initialize(Mode mode) { |
| 147 | mode_ = V8::UseCrankshaft() ? mode : NONOPT; |
| 148 | } |
| 149 | |
| 150 | void SetMode(Mode mode) { |
| 151 | ASSERT(V8::UseCrankshaft()); |
| 152 | mode_ = mode; |
| 153 | } |
| 154 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 155 | // Flags using template class BitField<type, start, length>. All are |
| 156 | // false by default. |
| 157 | // |
| 158 | // Compilation is either eager or lazy. |
| 159 | class IsLazy: public BitField<bool, 0, 1> {}; |
| 160 | // Flags that can be set for eager compilation. |
| 161 | class IsEval: public BitField<bool, 1, 1> {}; |
| 162 | class IsGlobal: public BitField<bool, 2, 1> {}; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 163 | // Flags that can be set for lazy compilation. |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 164 | class IsInLoop: public BitField<bool, 3, 1> {}; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 165 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 166 | unsigned flags_; |
| 167 | |
| 168 | // Fields filled in by the compilation pipeline. |
| 169 | // AST filled in by the parser. |
| 170 | FunctionLiteral* function_; |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 171 | // The scope of the function literal as a convenience. Set to indicate |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 172 | // that scopes have been analyzed. |
| 173 | Scope* scope_; |
| 174 | // The compiled code. |
| 175 | Handle<Code> code_; |
| 176 | |
| 177 | // Possible initial inputs to the compilation process. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 178 | Handle<JSFunction> closure_; |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 179 | Handle<SharedFunctionInfo> shared_info_; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 180 | Handle<Script> script_; |
| 181 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 182 | // Fields possibly needed for eager compilation, NULL by default. |
| 183 | v8::Extension* extension_; |
| 184 | ScriptDataImpl* pre_parse_data_; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 185 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 186 | // The context of the caller is needed for eval code, and will be a null |
| 187 | // handle otherwise. |
| 188 | Handle<Context> calling_context_; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 189 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 190 | // Compilation mode flag and whether deoptimization is allowed. |
| 191 | Mode mode_; |
| 192 | bool supports_deoptimization_; |
| 193 | int osr_ast_id_; |
| 194 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 195 | DISALLOW_COPY_AND_ASSIGN(CompilationInfo); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 199 | // The V8 compiler |
| 200 | // |
| 201 | // General strategy: Source code is translated into an anonymous function w/o |
| 202 | // parameters which then can be executed. If the source code contains other |
| 203 | // functions, they will be compiled and allocated as part of the compilation |
| 204 | // of the source code. |
| 205 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 206 | // Please note this interface returns shared function infos. This means you |
| 207 | // need to call Factory::NewFunctionFromSharedFunctionInfo before you have a |
| 208 | // real function with a context. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 209 | |
| 210 | class Compiler : public AllStatic { |
| 211 | public: |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame^] | 212 | // Default maximum number of function optimization attempts before we |
| 213 | // give up. |
| 214 | static const int kDefaultMaxOptCount = 10; |
| 215 | |
| 216 | // All routines return a SharedFunctionInfo. |
| 217 | // If an error occurs an exception is raised and the return handle |
| 218 | // contains NULL. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 219 | |
| 220 | // Compile a String source within a context. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 221 | static Handle<SharedFunctionInfo> Compile(Handle<String> source, |
| 222 | Handle<Object> script_name, |
| 223 | int line_offset, |
| 224 | int column_offset, |
| 225 | v8::Extension* extension, |
| 226 | ScriptDataImpl* pre_data, |
| 227 | Handle<Object> script_data, |
| 228 | NativesFlag is_natives_code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 229 | |
| 230 | // Compile a String source within a context for Eval. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 231 | static Handle<SharedFunctionInfo> CompileEval(Handle<String> source, |
| 232 | Handle<Context> context, |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 233 | bool is_global); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 234 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 235 | // Compile from function info (used for lazy compilation). Returns true on |
| 236 | // success and false if the compilation resulted in a stack overflow. |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 237 | static bool CompileLazy(CompilationInfo* info); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 238 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 239 | // Compile a shared function info object (the function is possibly lazily |
| 240 | // compiled). |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 241 | static Handle<SharedFunctionInfo> BuildFunctionInfo(FunctionLiteral* node, |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 242 | Handle<Script> script); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 243 | |
| 244 | // Set the function info for a newly compiled function. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 245 | static void SetFunctionInfo(Handle<SharedFunctionInfo> function_info, |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 246 | FunctionLiteral* lit, |
| 247 | bool is_toplevel, |
| 248 | Handle<Script> script); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 249 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 250 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 251 | static bool MakeCodeForLiveEdit(CompilationInfo* info); |
| 252 | #endif |
| 253 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 254 | static void RecordFunctionCompilation(Logger::LogEventsAndTags tag, |
| 255 | Handle<String> name, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 256 | int start_position, |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 257 | CompilationInfo* info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 258 | }; |
| 259 | |
| 260 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 261 | // During compilation we need a global list of handles to constants |
| 262 | // for frame elements. When the zone gets deleted, we make sure to |
| 263 | // clear this list of handles as well. |
| 264 | class CompilationZoneScope : public ZoneScope { |
| 265 | public: |
| 266 | explicit CompilationZoneScope(ZoneScopeMode mode) : ZoneScope(mode) { } |
| 267 | virtual ~CompilationZoneScope() { |
| 268 | if (ShouldDeleteOnExit()) { |
| 269 | FrameElement::ClearConstantList(); |
| 270 | Result::ClearConstantList(); |
| 271 | } |
| 272 | } |
| 273 | }; |
| 274 | |
| 275 | |
| 276 | } } // namespace v8::internal |
| 277 | |
| 278 | #endif // V8_COMPILER_H_ |