blob: 88f4479ad654dd24053b88c172ea4edc8fcf1568 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
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
31#include "frame-element.h"
32#include "parser.h"
33#include "zone.h"
34
35namespace v8 {
36namespace internal {
37
Andrei Popescu31002712010-02-23 13:46:05 +000038// CompilationInfo encapsulates some information known at compile time. It
39// is constructed based on the resources available at compile-time.
Leon Clarke4515c472010-02-03 11:58:03 +000040class CompilationInfo BASE_EMBEDDED {
41 public:
Andrei Popescu31002712010-02-23 13:46:05 +000042 // Lazy compilation of a JSFunction.
43 CompilationInfo(Handle<JSFunction> closure,
44 int loop_nesting,
45 Handle<Object> receiver)
46 : closure_(closure),
47 function_(NULL),
48 is_eval_(false),
Leon Clarke4515c472010-02-03 11:58:03 +000049 loop_nesting_(loop_nesting),
Andrei Popescu31002712010-02-23 13:46:05 +000050 receiver_(receiver) {
51 Initialize();
52 ASSERT(!closure_.is_null() &&
53 shared_info_.is_null() &&
54 script_.is_null());
Leon Clarke4515c472010-02-03 11:58:03 +000055 }
56
Andrei Popescu31002712010-02-23 13:46:05 +000057 // Lazy compilation based on SharedFunctionInfo.
58 explicit CompilationInfo(Handle<SharedFunctionInfo> shared_info)
59 : shared_info_(shared_info),
60 function_(NULL),
61 is_eval_(false),
62 loop_nesting_(0) {
63 Initialize();
64 ASSERT(closure_.is_null() &&
65 !shared_info_.is_null() &&
66 script_.is_null());
67 }
Leon Clarke4515c472010-02-03 11:58:03 +000068
Andrei Popescu31002712010-02-23 13:46:05 +000069 // Eager compilation.
70 CompilationInfo(FunctionLiteral* literal, Handle<Script> script, bool is_eval)
71 : script_(script),
72 function_(literal),
73 is_eval_(is_eval),
74 loop_nesting_(0) {
75 Initialize();
76 ASSERT(closure_.is_null() &&
77 shared_info_.is_null() &&
78 !script_.is_null());
79 }
80
81 // We can only get a JSFunction if we actually have one.
82 Handle<JSFunction> closure() { return closure_; }
83
84 // We can get a SharedFunctionInfo from a JSFunction or if we actually
85 // have one.
86 Handle<SharedFunctionInfo> shared_info() {
87 if (!closure().is_null()) {
88 return Handle<SharedFunctionInfo>(closure()->shared());
89 } else {
90 return shared_info_;
91 }
92 }
93
94 // We can always get a script. Either we have one or we can get a shared
95 // function info.
96 Handle<Script> script() {
97 if (!script_.is_null()) {
98 return script_;
99 } else {
100 ASSERT(shared_info()->script()->IsScript());
101 return Handle<Script>(Script::cast(shared_info()->script()));
102 }
103 }
104
105 // There should always be a function literal, but it may be set after
106 // construction (for lazy compilation).
107 FunctionLiteral* function() { return function_; }
108 void set_function(FunctionLiteral* literal) {
109 ASSERT(function_ == NULL);
110 function_ = literal;
111 }
112
113 // Simple accessors.
114 bool is_eval() { return is_eval_; }
115 int loop_nesting() { return loop_nesting_; }
Leon Clarke4515c472010-02-03 11:58:03 +0000116 bool has_receiver() { return !receiver_.is_null(); }
117 Handle<Object> receiver() { return receiver_; }
118
Andrei Popescu31002712010-02-23 13:46:05 +0000119 // Accessors for mutable fields, possibly set by analysis passes with
120 // default values given by Initialize.
Leon Clarke4515c472010-02-03 11:58:03 +0000121 bool has_this_properties() { return has_this_properties_; }
122 void set_has_this_properties(bool flag) { has_this_properties_ = flag; }
123
Andrei Popescu31002712010-02-23 13:46:05 +0000124 bool has_global_object() {
125 return !closure().is_null() && (closure()->context()->global() != NULL);
126 }
127
128 GlobalObject* global_object() {
129 return has_global_object() ? closure()->context()->global() : NULL;
130 }
131
Leon Clarke4515c472010-02-03 11:58:03 +0000132 bool has_globals() { return has_globals_; }
133 void set_has_globals(bool flag) { has_globals_ = flag; }
134
Andrei Popescu31002712010-02-23 13:46:05 +0000135 // Derived accessors.
136 Scope* scope() { return function()->scope(); }
137
Leon Clarke4515c472010-02-03 11:58:03 +0000138 private:
Andrei Popescu31002712010-02-23 13:46:05 +0000139 void Initialize() {
140 has_this_properties_ = false;
141 has_globals_ = false;
142 }
143
144 Handle<JSFunction> closure_;
Leon Clarke4515c472010-02-03 11:58:03 +0000145 Handle<SharedFunctionInfo> shared_info_;
Andrei Popescu31002712010-02-23 13:46:05 +0000146 Handle<Script> script_;
147
148 FunctionLiteral* function_;
149
150 bool is_eval_;
Leon Clarke4515c472010-02-03 11:58:03 +0000151 int loop_nesting_;
Andrei Popescu31002712010-02-23 13:46:05 +0000152
153 Handle<Object> receiver_;
154
Leon Clarke4515c472010-02-03 11:58:03 +0000155 bool has_this_properties_;
156 bool has_globals_;
Andrei Popescu31002712010-02-23 13:46:05 +0000157
158 DISALLOW_COPY_AND_ASSIGN(CompilationInfo);
Leon Clarke4515c472010-02-03 11:58:03 +0000159};
160
161
Steve Blocka7e24c12009-10-30 11:49:00 +0000162// The V8 compiler
163//
164// General strategy: Source code is translated into an anonymous function w/o
165// parameters which then can be executed. If the source code contains other
166// functions, they will be compiled and allocated as part of the compilation
167// of the source code.
168
169// Please note this interface returns function boilerplates.
170// This means you need to call Factory::NewFunctionFromBoilerplate
171// before you have a real function with context.
172
173class Compiler : public AllStatic {
174 public:
175 enum ValidationState { VALIDATE_JSON, DONT_VALIDATE_JSON };
176
177 // All routines return a JSFunction.
178 // If an error occurs an exception is raised and
179 // the return handle contains NULL.
180
181 // Compile a String source within a context.
182 static Handle<JSFunction> Compile(Handle<String> source,
183 Handle<Object> script_name,
184 int line_offset, int column_offset,
185 v8::Extension* extension,
Andrei Popescu31002712010-02-23 13:46:05 +0000186 ScriptDataImpl* script_Data,
187 NativesFlag is_natives_code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000188
189 // Compile a String source within a context for Eval.
190 static Handle<JSFunction> CompileEval(Handle<String> source,
191 Handle<Context> context,
192 bool is_global,
193 ValidationState validation);
194
195 // Compile from function info (used for lazy compilation). Returns
196 // true on success and false if the compilation resulted in a stack
197 // overflow.
Leon Clarke4515c472010-02-03 11:58:03 +0000198 static bool CompileLazy(CompilationInfo* info);
Steve Blockd0582a62009-12-15 09:54:21 +0000199
200 // Compile a function boilerplate object (the function is possibly
201 // lazily compiled). Called recursively from a backend code
202 // generator 'caller' to build the boilerplate.
203 static Handle<JSFunction> BuildBoilerplate(FunctionLiteral* node,
204 Handle<Script> script,
205 AstVisitor* caller);
206
207 // Set the function info for a newly compiled function.
208 static void SetFunctionInfo(Handle<JSFunction> fun,
209 FunctionLiteral* lit,
210 bool is_toplevel,
211 Handle<Script> script);
Andrei Popescu31002712010-02-23 13:46:05 +0000212
213 private:
214
215#if defined ENABLE_LOGGING_AND_PROFILING || defined ENABLE_OPROFILE_AGENT
216 static void LogCodeCreateEvent(Logger::LogEventsAndTags tag,
217 Handle<String> name,
218 Handle<String> inferred_name,
219 int start_position,
220 Handle<Script> script,
221 Handle<Code> code);
222#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000223};
224
225
226// During compilation we need a global list of handles to constants
227// for frame elements. When the zone gets deleted, we make sure to
228// clear this list of handles as well.
229class CompilationZoneScope : public ZoneScope {
230 public:
231 explicit CompilationZoneScope(ZoneScopeMode mode) : ZoneScope(mode) { }
232 virtual ~CompilationZoneScope() {
233 if (ShouldDeleteOnExit()) {
234 FrameElement::ClearConstantList();
235 Result::ClearConstantList();
236 }
237 }
238};
239
240
241} } // namespace v8::internal
242
243#endif // V8_COMPILER_H_