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; } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 52 | bool is_strict() const { return (flags_ & IsStrict::mask()) != 0; } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 53 | bool is_in_loop() const { return (flags_ & IsInLoop::mask()) != 0; } |
| 54 | FunctionLiteral* function() const { return function_; } |
| 55 | Scope* scope() const { return scope_; } |
| 56 | Handle<Code> code() const { return code_; } |
| 57 | Handle<JSFunction> closure() const { return closure_; } |
| 58 | Handle<SharedFunctionInfo> shared_info() const { return shared_info_; } |
| 59 | Handle<Script> script() const { return script_; } |
| 60 | v8::Extension* extension() const { return extension_; } |
| 61 | ScriptDataImpl* pre_parse_data() const { return pre_parse_data_; } |
| 62 | Handle<Context> calling_context() const { return calling_context_; } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 63 | int osr_ast_id() const { return osr_ast_id_; } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 64 | |
| 65 | void MarkAsEval() { |
| 66 | ASSERT(!is_lazy()); |
| 67 | flags_ |= IsEval::encode(true); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 68 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 69 | void MarkAsGlobal() { |
| 70 | ASSERT(!is_lazy()); |
| 71 | flags_ |= IsGlobal::encode(true); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 72 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 73 | void MarkAsStrict() { |
| 74 | flags_ |= IsStrict::encode(true); |
| 75 | } |
| 76 | StrictModeFlag StrictMode() { |
| 77 | return is_strict() ? kStrictMode : kNonStrictMode; |
| 78 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 79 | void MarkAsInLoop() { |
| 80 | ASSERT(is_lazy()); |
| 81 | flags_ |= IsInLoop::encode(true); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 82 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 83 | void SetFunction(FunctionLiteral* literal) { |
| 84 | ASSERT(function_ == NULL); |
| 85 | function_ = literal; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 86 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 87 | void SetScope(Scope* scope) { |
| 88 | ASSERT(scope_ == NULL); |
| 89 | scope_ = scope; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 90 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 91 | void SetCode(Handle<Code> code) { code_ = code; } |
| 92 | void SetExtension(v8::Extension* extension) { |
| 93 | ASSERT(!is_lazy()); |
| 94 | extension_ = extension; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 95 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 96 | void SetPreParseData(ScriptDataImpl* pre_parse_data) { |
| 97 | ASSERT(!is_lazy()); |
| 98 | pre_parse_data_ = pre_parse_data; |
| 99 | } |
| 100 | void SetCallingContext(Handle<Context> context) { |
| 101 | ASSERT(is_eval()); |
| 102 | calling_context_ = context; |
| 103 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 104 | void SetOsrAstId(int osr_ast_id) { |
| 105 | ASSERT(IsOptimizing()); |
| 106 | osr_ast_id_ = osr_ast_id; |
| 107 | } |
| 108 | |
| 109 | bool has_global_object() const { |
| 110 | return !closure().is_null() && (closure()->context()->global() != NULL); |
| 111 | } |
| 112 | |
| 113 | GlobalObject* global_object() const { |
| 114 | return has_global_object() ? closure()->context()->global() : NULL; |
| 115 | } |
| 116 | |
| 117 | // Accessors for the different compilation modes. |
| 118 | bool IsOptimizing() const { return mode_ == OPTIMIZE; } |
| 119 | bool IsOptimizable() const { return mode_ == BASE; } |
| 120 | void SetOptimizing(int osr_ast_id) { |
| 121 | SetMode(OPTIMIZE); |
| 122 | osr_ast_id_ = osr_ast_id; |
| 123 | } |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 124 | void DisableOptimization(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 125 | |
| 126 | // Deoptimization support. |
| 127 | bool HasDeoptimizationSupport() const { return supports_deoptimization_; } |
| 128 | void EnableDeoptimizationSupport() { |
| 129 | ASSERT(IsOptimizable()); |
| 130 | supports_deoptimization_ = true; |
| 131 | } |
| 132 | |
| 133 | // Determine whether or not we can adaptively optimize. |
| 134 | bool AllowOptimize() { |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 135 | return V8::UseCrankshaft() && !closure_.is_null(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 136 | } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 137 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 138 | private: |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 139 | // Compilation mode. |
| 140 | // BASE is generated by the full codegen, optionally prepared for bailouts. |
| 141 | // OPTIMIZE is optimized code generated by the Hydrogen-based backend. |
| 142 | // NONOPT is generated by the full codegen or the classic backend |
| 143 | // and is not prepared for recompilation/bailouts. These functions |
| 144 | // are never recompiled. |
| 145 | enum Mode { |
| 146 | BASE, |
| 147 | OPTIMIZE, |
| 148 | NONOPT |
| 149 | }; |
| 150 | |
| 151 | CompilationInfo() : function_(NULL) {} |
| 152 | |
| 153 | void Initialize(Mode mode) { |
| 154 | mode_ = V8::UseCrankshaft() ? mode : NONOPT; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 155 | if (!shared_info_.is_null() && shared_info_->strict_mode()) { |
| 156 | MarkAsStrict(); |
| 157 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void SetMode(Mode mode) { |
| 161 | ASSERT(V8::UseCrankshaft()); |
| 162 | mode_ = mode; |
| 163 | } |
| 164 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 165 | // Flags using template class BitField<type, start, length>. All are |
| 166 | // false by default. |
| 167 | // |
| 168 | // Compilation is either eager or lazy. |
| 169 | class IsLazy: public BitField<bool, 0, 1> {}; |
| 170 | // Flags that can be set for eager compilation. |
| 171 | class IsEval: public BitField<bool, 1, 1> {}; |
| 172 | class IsGlobal: public BitField<bool, 2, 1> {}; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 173 | // Flags that can be set for lazy compilation. |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 174 | class IsInLoop: public BitField<bool, 3, 1> {}; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 175 | // Strict mode - used in eager compilation. |
| 176 | class IsStrict: public BitField<bool, 4, 1> {}; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 177 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 178 | unsigned flags_; |
| 179 | |
| 180 | // Fields filled in by the compilation pipeline. |
| 181 | // AST filled in by the parser. |
| 182 | FunctionLiteral* function_; |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 183 | // The scope of the function literal as a convenience. Set to indicate |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 184 | // that scopes have been analyzed. |
| 185 | Scope* scope_; |
| 186 | // The compiled code. |
| 187 | Handle<Code> code_; |
| 188 | |
| 189 | // Possible initial inputs to the compilation process. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 190 | Handle<JSFunction> closure_; |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 191 | Handle<SharedFunctionInfo> shared_info_; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 192 | Handle<Script> script_; |
| 193 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 194 | // Fields possibly needed for eager compilation, NULL by default. |
| 195 | v8::Extension* extension_; |
| 196 | ScriptDataImpl* pre_parse_data_; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 197 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 198 | // The context of the caller is needed for eval code, and will be a null |
| 199 | // handle otherwise. |
| 200 | Handle<Context> calling_context_; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 201 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 202 | // Compilation mode flag and whether deoptimization is allowed. |
| 203 | Mode mode_; |
| 204 | bool supports_deoptimization_; |
| 205 | int osr_ast_id_; |
| 206 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 207 | DISALLOW_COPY_AND_ASSIGN(CompilationInfo); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 211 | // The V8 compiler |
| 212 | // |
| 213 | // General strategy: Source code is translated into an anonymous function w/o |
| 214 | // parameters which then can be executed. If the source code contains other |
| 215 | // functions, they will be compiled and allocated as part of the compilation |
| 216 | // of the source code. |
| 217 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 218 | // Please note this interface returns shared function infos. This means you |
| 219 | // need to call Factory::NewFunctionFromSharedFunctionInfo before you have a |
| 220 | // real function with a context. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 221 | |
| 222 | class Compiler : public AllStatic { |
| 223 | public: |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 224 | // Default maximum number of function optimization attempts before we |
| 225 | // give up. |
| 226 | static const int kDefaultMaxOptCount = 10; |
| 227 | |
| 228 | // All routines return a SharedFunctionInfo. |
| 229 | // If an error occurs an exception is raised and the return handle |
| 230 | // contains NULL. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 231 | |
| 232 | // Compile a String source within a context. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 233 | static Handle<SharedFunctionInfo> Compile(Handle<String> source, |
| 234 | Handle<Object> script_name, |
| 235 | int line_offset, |
| 236 | int column_offset, |
| 237 | v8::Extension* extension, |
| 238 | ScriptDataImpl* pre_data, |
| 239 | Handle<Object> script_data, |
| 240 | NativesFlag is_natives_code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 241 | |
| 242 | // Compile a String source within a context for Eval. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 243 | static Handle<SharedFunctionInfo> CompileEval(Handle<String> source, |
| 244 | Handle<Context> context, |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 245 | bool is_global, |
| 246 | StrictModeFlag strict_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 247 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 248 | // Compile from function info (used for lazy compilation). Returns true on |
| 249 | // success and false if the compilation resulted in a stack overflow. |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 250 | static bool CompileLazy(CompilationInfo* info); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 251 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 252 | // Compile a shared function info object (the function is possibly lazily |
| 253 | // compiled). |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 254 | static Handle<SharedFunctionInfo> BuildFunctionInfo(FunctionLiteral* node, |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 255 | Handle<Script> script); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 256 | |
| 257 | // Set the function info for a newly compiled function. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 258 | static void SetFunctionInfo(Handle<SharedFunctionInfo> function_info, |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 259 | FunctionLiteral* lit, |
| 260 | bool is_toplevel, |
| 261 | Handle<Script> script); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 262 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 263 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 264 | static bool MakeCodeForLiveEdit(CompilationInfo* info); |
| 265 | #endif |
| 266 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 267 | static void RecordFunctionCompilation(Logger::LogEventsAndTags tag, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 268 | CompilationInfo* info, |
| 269 | Handle<SharedFunctionInfo> shared); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 270 | }; |
| 271 | |
| 272 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 273 | // During compilation we need a global list of handles to constants |
| 274 | // for frame elements. When the zone gets deleted, we make sure to |
| 275 | // clear this list of handles as well. |
| 276 | class CompilationZoneScope : public ZoneScope { |
| 277 | public: |
| 278 | explicit CompilationZoneScope(ZoneScopeMode mode) : ZoneScope(mode) { } |
| 279 | virtual ~CompilationZoneScope() { |
| 280 | if (ShouldDeleteOnExit()) { |
| 281 | FrameElement::ClearConstantList(); |
| 282 | Result::ClearConstantList(); |
| 283 | } |
| 284 | } |
| 285 | }; |
| 286 | |
| 287 | |
| 288 | } } // namespace v8::internal |
| 289 | |
| 290 | #endif // V8_COMPILER_H_ |