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