blob: 181446b2fe86bc34c36ac88fb64409f989f727da [file] [log] [blame]
Ben Murdoch8b112d22011-06-08 16:22:53 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// 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
Ben Murdoch257744e2011-11-30 15:57:28 +000031#include "allocation.h"
Andrei Popescu402d9372010-02-26 13:31:12 +000032#include "ast.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000033#include "zone.h"
34
35namespace v8 {
36namespace internal {
37
Ben Murdochf87a2032010-10-22 12:50:53 +010038class ScriptDataImpl;
39
Andrei Popescu31002712010-02-23 13:46:05 +000040// CompilationInfo encapsulates some information known at compile time. It
41// is constructed based on the resources available at compile-time.
Leon Clarke4515c472010-02-03 11:58:03 +000042class CompilationInfo BASE_EMBEDDED {
43 public:
Ben Murdochf87a2032010-10-22 12:50:53 +010044 explicit CompilationInfo(Handle<Script> script);
45 explicit CompilationInfo(Handle<SharedFunctionInfo> shared_info);
46 explicit CompilationInfo(Handle<JSFunction> closure);
47
Steve Block44f0eee2011-05-26 01:26:41 +010048 Isolate* isolate() {
49 ASSERT(Isolate::Current() == isolate_);
50 return isolate_;
51 }
Ben Murdochf87a2032010-10-22 12:50:53 +010052 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; }
Ben Murdoch8b112d22011-06-08 16:22:53 +010055 bool is_strict_mode() const { return (flags_ & IsStrictMode::mask()) != 0; }
Ben Murdochf87a2032010-10-22 12:50:53 +010056 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_; }
Ben Murdochb0fe1622011-05-05 13:52:32 +010066 int osr_ast_id() const { return osr_ast_id_; }
Ben Murdochf87a2032010-10-22 12:50:53 +010067
68 void MarkAsEval() {
69 ASSERT(!is_lazy());
70 flags_ |= IsEval::encode(true);
Leon Clarke4515c472010-02-03 11:58:03 +000071 }
Ben Murdochf87a2032010-10-22 12:50:53 +010072 void MarkAsGlobal() {
73 ASSERT(!is_lazy());
74 flags_ |= IsGlobal::encode(true);
Andrei Popescu31002712010-02-23 13:46:05 +000075 }
Ben Murdoch8b112d22011-06-08 16:22:53 +010076 void MarkAsStrictMode() {
77 flags_ |= IsStrictMode::encode(true);
Steve Block1e0659c2011-05-24 12:43:12 +010078 }
79 StrictModeFlag StrictMode() {
Ben Murdoch8b112d22011-06-08 16:22:53 +010080 return is_strict_mode() ? kStrictMode : kNonStrictMode;
Steve Block1e0659c2011-05-24 12:43:12 +010081 }
Ben Murdochf87a2032010-10-22 12:50:53 +010082 void MarkAsInLoop() {
83 ASSERT(is_lazy());
84 flags_ |= IsInLoop::encode(true);
Andrei Popescu31002712010-02-23 13:46:05 +000085 }
Ben Murdoch203a29f2011-10-20 14:36:23 +010086 void MarkAsNative() {
87 flags_ |= IsNative::encode(true);
Steve Block44f0eee2011-05-26 01:26:41 +010088 }
Ben Murdoch203a29f2011-10-20 14:36:23 +010089 bool is_native() const {
90 return IsNative::decode(flags_);
Steve Block44f0eee2011-05-26 01:26:41 +010091 }
Ben Murdochf87a2032010-10-22 12:50:53 +010092 void SetFunction(FunctionLiteral* literal) {
93 ASSERT(function_ == NULL);
94 function_ = literal;
Andrei Popescu31002712010-02-23 13:46:05 +000095 }
Ben Murdochf87a2032010-10-22 12:50:53 +010096 void SetScope(Scope* scope) {
97 ASSERT(scope_ == NULL);
98 scope_ = scope;
Andrei Popescu31002712010-02-23 13:46:05 +000099 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100100 void SetCode(Handle<Code> code) { code_ = code; }
101 void SetExtension(v8::Extension* extension) {
102 ASSERT(!is_lazy());
103 extension_ = extension;
Andrei Popescu31002712010-02-23 13:46:05 +0000104 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100105 void SetPreParseData(ScriptDataImpl* pre_parse_data) {
106 ASSERT(!is_lazy());
107 pre_parse_data_ = pre_parse_data;
108 }
109 void SetCallingContext(Handle<Context> context) {
110 ASSERT(is_eval());
111 calling_context_ = context;
112 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100113 void SetOsrAstId(int osr_ast_id) {
114 ASSERT(IsOptimizing());
115 osr_ast_id_ = osr_ast_id;
116 }
117
118 bool has_global_object() const {
119 return !closure().is_null() && (closure()->context()->global() != NULL);
120 }
121
122 GlobalObject* global_object() const {
123 return has_global_object() ? closure()->context()->global() : NULL;
124 }
125
126 // Accessors for the different compilation modes.
127 bool IsOptimizing() const { return mode_ == OPTIMIZE; }
128 bool IsOptimizable() const { return mode_ == BASE; }
129 void SetOptimizing(int osr_ast_id) {
130 SetMode(OPTIMIZE);
131 osr_ast_id_ = osr_ast_id;
132 }
Ben Murdochb8e0da22011-05-16 14:20:40 +0100133 void DisableOptimization();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100134
135 // Deoptimization support.
136 bool HasDeoptimizationSupport() const { return supports_deoptimization_; }
137 void EnableDeoptimizationSupport() {
138 ASSERT(IsOptimizable());
139 supports_deoptimization_ = true;
140 }
141
142 // Determine whether or not we can adaptively optimize.
143 bool AllowOptimize() {
Ben Murdochb8e0da22011-05-16 14:20:40 +0100144 return V8::UseCrankshaft() && !closure_.is_null();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100145 }
Andrei Popescu31002712010-02-23 13:46:05 +0000146
Ben Murdoch257744e2011-11-30 15:57:28 +0000147 // Disable all optimization attempts of this info for the rest of the
148 // current compilation pipeline.
149 void AbortOptimization();
150
Leon Clarke4515c472010-02-03 11:58:03 +0000151 private:
Steve Block44f0eee2011-05-26 01:26:41 +0100152 Isolate* isolate_;
153
Ben Murdochb0fe1622011-05-05 13:52:32 +0100154 // Compilation mode.
155 // BASE is generated by the full codegen, optionally prepared for bailouts.
156 // OPTIMIZE is optimized code generated by the Hydrogen-based backend.
157 // NONOPT is generated by the full codegen or the classic backend
158 // and is not prepared for recompilation/bailouts. These functions
159 // are never recompiled.
160 enum Mode {
161 BASE,
162 OPTIMIZE,
163 NONOPT
164 };
165
166 CompilationInfo() : function_(NULL) {}
167
168 void Initialize(Mode mode) {
169 mode_ = V8::UseCrankshaft() ? mode : NONOPT;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000170 ASSERT(!script_.is_null());
Ben Murdoch203a29f2011-10-20 14:36:23 +0100171 if (script_->type()->value() == Script::TYPE_NATIVE) {
172 MarkAsNative();
173 }
Steve Block1e0659c2011-05-24 12:43:12 +0100174 if (!shared_info_.is_null() && shared_info_->strict_mode()) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100175 MarkAsStrictMode();
Steve Block1e0659c2011-05-24 12:43:12 +0100176 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100177 }
178
179 void SetMode(Mode mode) {
180 ASSERT(V8::UseCrankshaft());
181 mode_ = mode;
182 }
183
Ben Murdochf87a2032010-10-22 12:50:53 +0100184 // Flags using template class BitField<type, start, length>. All are
185 // false by default.
186 //
187 // Compilation is either eager or lazy.
188 class IsLazy: public BitField<bool, 0, 1> {};
189 // Flags that can be set for eager compilation.
190 class IsEval: public BitField<bool, 1, 1> {};
191 class IsGlobal: public BitField<bool, 2, 1> {};
Ben Murdochf87a2032010-10-22 12:50:53 +0100192 // Flags that can be set for lazy compilation.
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800193 class IsInLoop: public BitField<bool, 3, 1> {};
Steve Block1e0659c2011-05-24 12:43:12 +0100194 // Strict mode - used in eager compilation.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100195 class IsStrictMode: public BitField<bool, 4, 1> {};
Ben Murdoch203a29f2011-10-20 14:36:23 +0100196 // Is this a function from our natives.
197 class IsNative: public BitField<bool, 6, 1> {};
Andrei Popescu31002712010-02-23 13:46:05 +0000198
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000199
Ben Murdochf87a2032010-10-22 12:50:53 +0100200 unsigned flags_;
201
202 // Fields filled in by the compilation pipeline.
203 // AST filled in by the parser.
204 FunctionLiteral* function_;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800205 // The scope of the function literal as a convenience. Set to indicate
Ben Murdochf87a2032010-10-22 12:50:53 +0100206 // that scopes have been analyzed.
207 Scope* scope_;
208 // The compiled code.
209 Handle<Code> code_;
210
211 // Possible initial inputs to the compilation process.
Andrei Popescu31002712010-02-23 13:46:05 +0000212 Handle<JSFunction> closure_;
Leon Clarke4515c472010-02-03 11:58:03 +0000213 Handle<SharedFunctionInfo> shared_info_;
Andrei Popescu31002712010-02-23 13:46:05 +0000214 Handle<Script> script_;
215
Ben Murdochf87a2032010-10-22 12:50:53 +0100216 // Fields possibly needed for eager compilation, NULL by default.
217 v8::Extension* extension_;
218 ScriptDataImpl* pre_parse_data_;
Andrei Popescu31002712010-02-23 13:46:05 +0000219
Ben Murdochf87a2032010-10-22 12:50:53 +0100220 // The context of the caller is needed for eval code, and will be a null
221 // handle otherwise.
222 Handle<Context> calling_context_;
Andrei Popescu31002712010-02-23 13:46:05 +0000223
Ben Murdochb0fe1622011-05-05 13:52:32 +0100224 // Compilation mode flag and whether deoptimization is allowed.
225 Mode mode_;
226 bool supports_deoptimization_;
227 int osr_ast_id_;
228
Andrei Popescu31002712010-02-23 13:46:05 +0000229 DISALLOW_COPY_AND_ASSIGN(CompilationInfo);
Leon Clarke4515c472010-02-03 11:58:03 +0000230};
231
232
Steve Blocka7e24c12009-10-30 11:49:00 +0000233// The V8 compiler
234//
235// General strategy: Source code is translated into an anonymous function w/o
236// parameters which then can be executed. If the source code contains other
237// functions, they will be compiled and allocated as part of the compilation
238// of the source code.
239
Ben Murdochf87a2032010-10-22 12:50:53 +0100240// Please note this interface returns shared function infos. This means you
241// need to call Factory::NewFunctionFromSharedFunctionInfo before you have a
242// real function with a context.
Steve Blocka7e24c12009-10-30 11:49:00 +0000243
244class Compiler : public AllStatic {
245 public:
Ben Murdochb8e0da22011-05-16 14:20:40 +0100246 // Default maximum number of function optimization attempts before we
247 // give up.
248 static const int kDefaultMaxOptCount = 10;
249
Ben Murdoch8b112d22011-06-08 16:22:53 +0100250 static const int kMaxInliningLevels = 3;
251
Ben Murdochb8e0da22011-05-16 14:20:40 +0100252 // All routines return a SharedFunctionInfo.
253 // If an error occurs an exception is raised and the return handle
254 // contains NULL.
Steve Blocka7e24c12009-10-30 11:49:00 +0000255
256 // Compile a String source within a context.
Steve Block6ded16b2010-05-10 14:33:55 +0100257 static Handle<SharedFunctionInfo> Compile(Handle<String> source,
258 Handle<Object> script_name,
259 int line_offset,
260 int column_offset,
261 v8::Extension* extension,
262 ScriptDataImpl* pre_data,
263 Handle<Object> script_data,
264 NativesFlag is_natives_code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000265
266 // Compile a String source within a context for Eval.
Steve Block6ded16b2010-05-10 14:33:55 +0100267 static Handle<SharedFunctionInfo> CompileEval(Handle<String> source,
268 Handle<Context> context,
Steve Block1e0659c2011-05-24 12:43:12 +0100269 bool is_global,
270 StrictModeFlag strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000271
Ben Murdochf87a2032010-10-22 12:50:53 +0100272 // Compile from function info (used for lazy compilation). Returns true on
273 // success and false if the compilation resulted in a stack overflow.
Leon Clarke4515c472010-02-03 11:58:03 +0000274 static bool CompileLazy(CompilationInfo* info);
Steve Blockd0582a62009-12-15 09:54:21 +0000275
Ben Murdochf87a2032010-10-22 12:50:53 +0100276 // Compile a shared function info object (the function is possibly lazily
277 // compiled).
Steve Block6ded16b2010-05-10 14:33:55 +0100278 static Handle<SharedFunctionInfo> BuildFunctionInfo(FunctionLiteral* node,
Ben Murdochf87a2032010-10-22 12:50:53 +0100279 Handle<Script> script);
Steve Blockd0582a62009-12-15 09:54:21 +0000280
281 // Set the function info for a newly compiled function.
Steve Block6ded16b2010-05-10 14:33:55 +0100282 static void SetFunctionInfo(Handle<SharedFunctionInfo> function_info,
Steve Blockd0582a62009-12-15 09:54:21 +0000283 FunctionLiteral* lit,
284 bool is_toplevel,
285 Handle<Script> script);
Andrei Popescu31002712010-02-23 13:46:05 +0000286
Ben Murdochf87a2032010-10-22 12:50:53 +0100287#ifdef ENABLE_DEBUGGER_SUPPORT
288 static bool MakeCodeForLiveEdit(CompilationInfo* info);
289#endif
290
Steve Block6ded16b2010-05-10 14:33:55 +0100291 static void RecordFunctionCompilation(Logger::LogEventsAndTags tag,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100292 CompilationInfo* info,
293 Handle<SharedFunctionInfo> shared);
Steve Blocka7e24c12009-10-30 11:49:00 +0000294};
295
296
Steve Blocka7e24c12009-10-30 11:49:00 +0000297} } // namespace v8::internal
298
299#endif // V8_COMPILER_H_