karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +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 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 31 | #include "allocation.h" |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 32 | #include "ast.h" |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 33 | #include "zone.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 34 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 35 | namespace v8 { |
| 36 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 37 | |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 38 | class ScriptDataImpl; |
| 39 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 40 | // CompilationInfo encapsulates some information known at compile time. It |
| 41 | // is constructed based on the resources available at compile-time. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 42 | class CompilationInfo BASE_EMBEDDED { |
| 43 | public: |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 44 | explicit CompilationInfo(Handle<Script> script); |
| 45 | explicit CompilationInfo(Handle<SharedFunctionInfo> shared_info); |
| 46 | explicit CompilationInfo(Handle<JSFunction> closure); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 47 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 48 | Isolate* isolate() { |
| 49 | ASSERT(Isolate::Current() == isolate_); |
| 50 | return isolate_; |
| 51 | } |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 52 | bool is_lazy() const { return (flags_ & IsLazy::mask()) != 0; } |
| 53 | bool is_eval() const { return (flags_ & IsEval::mask()) != 0; } |
| 54 | bool is_global() const { return (flags_ & IsGlobal::mask()) != 0; } |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 55 | bool is_strict_mode() const { return (flags_ & IsStrictMode::mask()) != 0; } |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 56 | bool is_in_loop() const { return (flags_ & IsInLoop::mask()) != 0; } |
| 57 | FunctionLiteral* function() const { return function_; } |
| 58 | Scope* scope() const { return scope_; } |
| 59 | Handle<Code> code() const { return code_; } |
| 60 | Handle<JSFunction> closure() const { return closure_; } |
| 61 | Handle<SharedFunctionInfo> shared_info() const { return shared_info_; } |
| 62 | Handle<Script> script() const { return script_; } |
| 63 | v8::Extension* extension() const { return extension_; } |
| 64 | ScriptDataImpl* pre_parse_data() const { return pre_parse_data_; } |
| 65 | Handle<Context> calling_context() const { return calling_context_; } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 66 | int osr_ast_id() const { return osr_ast_id_; } |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 67 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 68 | void MarkAsEval() { |
| 69 | ASSERT(!is_lazy()); |
| 70 | flags_ |= IsEval::encode(true); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 71 | } |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 72 | void MarkAsGlobal() { |
| 73 | ASSERT(!is_lazy()); |
| 74 | flags_ |= IsGlobal::encode(true); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 75 | } |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 76 | void MarkAsStrictMode() { |
| 77 | flags_ |= IsStrictMode::encode(true); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 78 | } |
| 79 | StrictModeFlag StrictMode() { |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 80 | return is_strict_mode() ? kStrictMode : kNonStrictMode; |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 81 | } |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 82 | void MarkAsInLoop() { |
| 83 | ASSERT(is_lazy()); |
| 84 | flags_ |= IsInLoop::encode(true); |
| 85 | } |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 86 | void MarkAsAllowingNativesSyntax() { |
| 87 | flags_ |= IsNativesSyntaxAllowed::encode(true); |
| 88 | } |
| 89 | bool allows_natives_syntax() const { |
| 90 | return IsNativesSyntaxAllowed::decode(flags_); |
| 91 | } |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 92 | void MarkAsNative() { |
| 93 | flags_ |= IsNative::encode(true); |
| 94 | } |
| 95 | bool is_native() const { |
| 96 | return IsNative::decode(flags_); |
| 97 | } |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 98 | void SetFunction(FunctionLiteral* literal) { |
| 99 | ASSERT(function_ == NULL); |
| 100 | function_ = literal; |
| 101 | } |
| 102 | void SetScope(Scope* scope) { |
| 103 | ASSERT(scope_ == NULL); |
| 104 | scope_ = scope; |
| 105 | } |
| 106 | void SetCode(Handle<Code> code) { code_ = code; } |
| 107 | void SetExtension(v8::Extension* extension) { |
| 108 | ASSERT(!is_lazy()); |
| 109 | extension_ = extension; |
| 110 | } |
| 111 | void SetPreParseData(ScriptDataImpl* pre_parse_data) { |
| 112 | ASSERT(!is_lazy()); |
| 113 | pre_parse_data_ = pre_parse_data; |
| 114 | } |
| 115 | void SetCallingContext(Handle<Context> context) { |
| 116 | ASSERT(is_eval()); |
| 117 | calling_context_ = context; |
| 118 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 119 | void SetOsrAstId(int osr_ast_id) { |
| 120 | ASSERT(IsOptimizing()); |
| 121 | osr_ast_id_ = osr_ast_id; |
| 122 | } |
| 123 | |
| 124 | bool has_global_object() const { |
| 125 | return !closure().is_null() && (closure()->context()->global() != NULL); |
| 126 | } |
| 127 | |
| 128 | GlobalObject* global_object() const { |
| 129 | return has_global_object() ? closure()->context()->global() : NULL; |
| 130 | } |
| 131 | |
| 132 | // Accessors for the different compilation modes. |
| 133 | bool IsOptimizing() const { return mode_ == OPTIMIZE; } |
| 134 | bool IsOptimizable() const { return mode_ == BASE; } |
| 135 | void SetOptimizing(int osr_ast_id) { |
| 136 | SetMode(OPTIMIZE); |
| 137 | osr_ast_id_ = osr_ast_id; |
| 138 | } |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 139 | void DisableOptimization(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 140 | |
| 141 | // Deoptimization support. |
| 142 | bool HasDeoptimizationSupport() const { return supports_deoptimization_; } |
| 143 | void EnableDeoptimizationSupport() { |
| 144 | ASSERT(IsOptimizable()); |
| 145 | supports_deoptimization_ = true; |
| 146 | } |
| 147 | |
| 148 | // Determine whether or not we can adaptively optimize. |
| 149 | bool AllowOptimize() { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 150 | return V8::UseCrankshaft() && !closure_.is_null(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 151 | } |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 152 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 153 | // Disable all optimization attempts of this info for the rest of the |
| 154 | // current compilation pipeline. |
| 155 | void AbortOptimization(); |
| 156 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 157 | private: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 158 | Isolate* isolate_; |
| 159 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 160 | // Compilation mode. |
| 161 | // BASE is generated by the full codegen, optionally prepared for bailouts. |
| 162 | // OPTIMIZE is optimized code generated by the Hydrogen-based backend. |
| 163 | // NONOPT is generated by the full codegen or the classic backend |
| 164 | // and is not prepared for recompilation/bailouts. These functions |
| 165 | // are never recompiled. |
| 166 | enum Mode { |
| 167 | BASE, |
| 168 | OPTIMIZE, |
| 169 | NONOPT |
| 170 | }; |
| 171 | |
| 172 | CompilationInfo() : function_(NULL) {} |
| 173 | |
| 174 | void Initialize(Mode mode) { |
| 175 | mode_ = V8::UseCrankshaft() ? mode : NONOPT; |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 176 | ASSERT(!script_.is_null()); |
| 177 | if (script_->type()->value() == Script::TYPE_NATIVE) { |
| 178 | MarkAsNative(); |
| 179 | } |
| 180 | if (!shared_info_.is_null() && shared_info_->strict_mode()) { |
| 181 | MarkAsStrictMode(); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 182 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void SetMode(Mode mode) { |
| 186 | ASSERT(V8::UseCrankshaft()); |
| 187 | mode_ = mode; |
| 188 | } |
| 189 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 190 | // Flags using template class BitField<type, start, length>. All are |
| 191 | // false by default. |
| 192 | // |
| 193 | // Compilation is either eager or lazy. |
| 194 | class IsLazy: public BitField<bool, 0, 1> {}; |
| 195 | // Flags that can be set for eager compilation. |
| 196 | class IsEval: public BitField<bool, 1, 1> {}; |
| 197 | class IsGlobal: public BitField<bool, 2, 1> {}; |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 198 | // Flags that can be set for lazy compilation. |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 199 | class IsInLoop: public BitField<bool, 3, 1> {}; |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 200 | // Strict mode - used in eager compilation. |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 201 | class IsStrictMode: public BitField<bool, 4, 1> {}; |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 202 | // Native syntax (%-stuff) allowed? |
| 203 | class IsNativesSyntaxAllowed: public BitField<bool, 5, 1> {}; |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 204 | // Is this a function from our natives. |
| 205 | class IsNative: public BitField<bool, 6, 1> {}; |
| 206 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 207 | |
| 208 | unsigned flags_; |
| 209 | |
| 210 | // Fields filled in by the compilation pipeline. |
| 211 | // AST filled in by the parser. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 212 | FunctionLiteral* function_; |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 213 | // The scope of the function literal as a convenience. Set to indicate |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 214 | // that scopes have been analyzed. |
| 215 | Scope* scope_; |
| 216 | // The compiled code. |
| 217 | Handle<Code> code_; |
| 218 | |
| 219 | // Possible initial inputs to the compilation process. |
| 220 | Handle<JSFunction> closure_; |
| 221 | Handle<SharedFunctionInfo> shared_info_; |
| 222 | Handle<Script> script_; |
| 223 | |
| 224 | // Fields possibly needed for eager compilation, NULL by default. |
| 225 | v8::Extension* extension_; |
| 226 | ScriptDataImpl* pre_parse_data_; |
| 227 | |
| 228 | // The context of the caller is needed for eval code, and will be a null |
| 229 | // handle otherwise. |
| 230 | Handle<Context> calling_context_; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 231 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 232 | // Compilation mode flag and whether deoptimization is allowed. |
| 233 | Mode mode_; |
| 234 | bool supports_deoptimization_; |
| 235 | int osr_ast_id_; |
| 236 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 237 | DISALLOW_COPY_AND_ASSIGN(CompilationInfo); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 238 | }; |
| 239 | |
| 240 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 241 | // The V8 compiler |
| 242 | // |
| 243 | // General strategy: Source code is translated into an anonymous function w/o |
| 244 | // parameters which then can be executed. If the source code contains other |
| 245 | // functions, they will be compiled and allocated as part of the compilation |
| 246 | // of the source code. |
| 247 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 248 | // Please note this interface returns shared function infos. This means you |
| 249 | // need to call Factory::NewFunctionFromSharedFunctionInfo before you have a |
| 250 | // real function with a context. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 251 | |
| 252 | class Compiler : public AllStatic { |
| 253 | public: |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 254 | // Default maximum number of function optimization attempts before we |
| 255 | // give up. |
| 256 | static const int kDefaultMaxOptCount = 10; |
| 257 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 258 | static const int kMaxInliningLevels = 3; |
| 259 | |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 260 | // All routines return a SharedFunctionInfo. |
| 261 | // If an error occurs an exception is raised and the return handle |
| 262 | // contains NULL. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 263 | |
| 264 | // Compile a String source within a context. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 265 | static Handle<SharedFunctionInfo> Compile(Handle<String> source, |
| 266 | Handle<Object> script_name, |
| 267 | int line_offset, |
| 268 | int column_offset, |
| 269 | v8::Extension* extension, |
| 270 | ScriptDataImpl* pre_data, |
| 271 | Handle<Object> script_data, |
| 272 | NativesFlag is_natives_code); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 273 | |
| 274 | // Compile a String source within a context for Eval. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 275 | static Handle<SharedFunctionInfo> CompileEval(Handle<String> source, |
| 276 | Handle<Context> context, |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 277 | bool is_global, |
| 278 | StrictModeFlag strict_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 279 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 280 | // Compile from function info (used for lazy compilation). Returns true on |
| 281 | // success and false if the compilation resulted in a stack overflow. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 282 | static bool CompileLazy(CompilationInfo* info); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 283 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 284 | // Compile a shared function info object (the function is possibly lazily |
| 285 | // compiled). |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 286 | static Handle<SharedFunctionInfo> BuildFunctionInfo(FunctionLiteral* node, |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 287 | Handle<Script> script); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 288 | |
| 289 | // Set the function info for a newly compiled function. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 290 | static void SetFunctionInfo(Handle<SharedFunctionInfo> function_info, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 291 | FunctionLiteral* lit, |
| 292 | bool is_toplevel, |
| 293 | Handle<Script> script); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 294 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 295 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 296 | static bool MakeCodeForLiveEdit(CompilationInfo* info); |
| 297 | #endif |
| 298 | |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 299 | static void RecordFunctionCompilation(Logger::LogEventsAndTags tag, |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 300 | CompilationInfo* info, |
| 301 | Handle<SharedFunctionInfo> shared); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 302 | }; |
| 303 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 304 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 305 | } } // namespace v8::internal |
| 306 | |
| 307 | #endif // V8_COMPILER_H_ |