2010-10-04 14:30:43 +00:00
|
|
|
// Copyright 2010 the V8 project authors. All rights reserved.
|
2008-07-03 15:10:15 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#ifndef V8_COMPILER_H_
|
|
|
|
#define V8_COMPILER_H_
|
|
|
|
|
2010-02-08 14:33:34 +00:00
|
|
|
#include "ast.h"
|
2009-05-14 12:18:25 +00:00
|
|
|
#include "frame-element.h"
|
2010-02-08 14:33:34 +00:00
|
|
|
#include "register-allocator.h"
|
2009-05-14 12:18:25 +00:00
|
|
|
#include "zone.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2010-10-01 14:10:47 +00:00
|
|
|
class ScriptDataImpl;
|
|
|
|
|
2010-02-03 16:12:55 +00:00
|
|
|
// CompilationInfo encapsulates some information known at compile time. It
|
|
|
|
// is constructed based on the resources available at compile-time.
|
2010-01-29 11:55:40 +00:00
|
|
|
class CompilationInfo BASE_EMBEDDED {
|
|
|
|
public:
|
2010-10-04 11:35:46 +00:00
|
|
|
explicit CompilationInfo(Handle<Script> script);
|
|
|
|
explicit CompilationInfo(Handle<SharedFunctionInfo> shared_info);
|
|
|
|
explicit CompilationInfo(Handle<JSFunction> closure);
|
|
|
|
|
|
|
|
bool is_lazy() const { return (flags_ & IsLazy::mask()) != 0; }
|
|
|
|
bool is_eval() const { return (flags_ & IsEval::mask()) != 0; }
|
|
|
|
bool is_global() const { return (flags_ & IsGlobal::mask()) != 0; }
|
|
|
|
bool is_in_loop() const { return (flags_ & IsInLoop::mask()) != 0; }
|
|
|
|
FunctionLiteral* function() const { return function_; }
|
2010-10-04 14:30:43 +00:00
|
|
|
Scope* scope() const { return scope_; }
|
|
|
|
Handle<Code> code() const { return code_; }
|
2010-10-04 11:35:46 +00:00
|
|
|
Handle<JSFunction> closure() const { return closure_; }
|
|
|
|
Handle<SharedFunctionInfo> shared_info() const { return shared_info_; }
|
|
|
|
Handle<Script> script() const { return script_; }
|
|
|
|
v8::Extension* extension() const { return extension_; }
|
|
|
|
ScriptDataImpl* pre_parse_data() const { return pre_parse_data_; }
|
2010-10-04 14:30:43 +00:00
|
|
|
Handle<Context> calling_context() const { return calling_context_; }
|
2010-12-07 11:31:57 +00:00
|
|
|
int osr_ast_id() const { return osr_ast_id_; }
|
2010-10-04 11:35:46 +00:00
|
|
|
|
|
|
|
void MarkAsEval() {
|
|
|
|
ASSERT(!is_lazy());
|
|
|
|
flags_ |= IsEval::encode(true);
|
2010-02-03 16:12:55 +00:00
|
|
|
}
|
2010-10-04 11:35:46 +00:00
|
|
|
void MarkAsGlobal() {
|
|
|
|
ASSERT(!is_lazy());
|
|
|
|
flags_ |= IsGlobal::encode(true);
|
2010-02-05 13:57:18 +00:00
|
|
|
}
|
2010-10-04 11:35:46 +00:00
|
|
|
void MarkAsInLoop() {
|
|
|
|
ASSERT(is_lazy());
|
|
|
|
flags_ |= IsInLoop::encode(true);
|
2010-09-30 08:48:37 +00:00
|
|
|
}
|
2010-10-04 11:35:46 +00:00
|
|
|
void SetFunction(FunctionLiteral* literal) {
|
|
|
|
ASSERT(function_ == NULL);
|
|
|
|
function_ = literal;
|
2010-09-30 08:48:37 +00:00
|
|
|
}
|
2010-10-04 14:30:43 +00:00
|
|
|
void SetScope(Scope* scope) {
|
|
|
|
ASSERT(scope_ == NULL);
|
|
|
|
scope_ = scope;
|
|
|
|
}
|
|
|
|
void SetCode(Handle<Code> code) { code_ = code; }
|
2010-10-04 11:35:46 +00:00
|
|
|
void SetExtension(v8::Extension* extension) {
|
|
|
|
ASSERT(!is_lazy());
|
|
|
|
extension_ = extension;
|
2010-09-30 08:48:37 +00:00
|
|
|
}
|
2010-10-04 11:35:46 +00:00
|
|
|
void SetPreParseData(ScriptDataImpl* pre_parse_data) {
|
|
|
|
ASSERT(!is_lazy());
|
|
|
|
pre_parse_data_ = pre_parse_data;
|
2010-09-30 08:48:37 +00:00
|
|
|
}
|
2010-10-04 14:30:43 +00:00
|
|
|
void SetCallingContext(Handle<Context> context) {
|
|
|
|
ASSERT(is_eval());
|
|
|
|
calling_context_ = context;
|
|
|
|
}
|
2010-12-07 11:31:57 +00:00
|
|
|
void SetOsrAstId(int osr_ast_id) {
|
|
|
|
ASSERT(IsOptimizing());
|
|
|
|
osr_ast_id_ = osr_ast_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool has_global_object() const {
|
|
|
|
return !closure().is_null() && (closure()->context()->global() != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
GlobalObject* global_object() const {
|
|
|
|
return has_global_object() ? closure()->context()->global() : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accessors for the different compilation modes.
|
|
|
|
bool IsOptimizing() const { return mode_ == OPTIMIZE; }
|
|
|
|
bool IsOptimizable() const { return mode_ == BASE; }
|
|
|
|
void SetOptimizing(int osr_ast_id) {
|
|
|
|
SetMode(OPTIMIZE);
|
|
|
|
osr_ast_id_ = osr_ast_id;
|
|
|
|
}
|
2011-01-17 08:11:03 +00:00
|
|
|
void DisableOptimization();
|
2010-12-07 11:31:57 +00:00
|
|
|
|
|
|
|
// Deoptimization support.
|
|
|
|
bool HasDeoptimizationSupport() const { return supports_deoptimization_; }
|
|
|
|
void EnableDeoptimizationSupport() {
|
|
|
|
ASSERT(IsOptimizable());
|
|
|
|
supports_deoptimization_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine whether or not we can adaptively optimize.
|
|
|
|
bool AllowOptimize() {
|
2011-01-17 08:11:03 +00:00
|
|
|
return V8::UseCrankshaft() && !closure_.is_null();
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
2010-09-30 08:48:37 +00:00
|
|
|
|
2010-10-04 11:35:46 +00:00
|
|
|
private:
|
2010-12-07 11:31:57 +00:00
|
|
|
// Compilation mode.
|
|
|
|
// BASE is generated by the full codegen, optionally prepared for bailouts.
|
|
|
|
// OPTIMIZE is optimized code generated by the Hydrogen-based backend.
|
|
|
|
// NONOPT is generated by the full codegen or the classic backend
|
|
|
|
// and is not prepared for recompilation/bailouts. These functions
|
|
|
|
// are never recompiled.
|
|
|
|
enum Mode {
|
|
|
|
BASE,
|
|
|
|
OPTIMIZE,
|
|
|
|
NONOPT
|
|
|
|
};
|
|
|
|
|
|
|
|
CompilationInfo() : function_(NULL) {}
|
|
|
|
|
|
|
|
void Initialize(Mode mode) {
|
|
|
|
mode_ = V8::UseCrankshaft() ? mode : NONOPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetMode(Mode mode) {
|
|
|
|
ASSERT(V8::UseCrankshaft());
|
|
|
|
mode_ = mode;
|
|
|
|
}
|
|
|
|
|
2010-10-04 11:35:46 +00:00
|
|
|
// Flags using template class BitField<type, start, length>. All are
|
|
|
|
// false by default.
|
|
|
|
//
|
|
|
|
// Compilation is either eager or lazy.
|
|
|
|
class IsLazy: public BitField<bool, 0, 1> {};
|
|
|
|
// Flags that can be set for eager compilation.
|
|
|
|
class IsEval: public BitField<bool, 1, 1> {};
|
|
|
|
class IsGlobal: public BitField<bool, 2, 1> {};
|
|
|
|
// Flags that can be set for lazy compilation.
|
2010-10-27 09:19:43 +00:00
|
|
|
class IsInLoop: public BitField<bool, 3, 1> {};
|
2010-10-04 11:35:46 +00:00
|
|
|
|
|
|
|
unsigned flags_;
|
|
|
|
|
|
|
|
// Fields filled in by the compilation pipeline.
|
2010-10-04 14:30:43 +00:00
|
|
|
// AST filled in by the parser.
|
2010-10-04 11:35:46 +00:00
|
|
|
FunctionLiteral* function_;
|
2010-10-27 09:19:43 +00:00
|
|
|
// The scope of the function literal as a convenience. Set to indicate
|
2010-10-04 14:30:43 +00:00
|
|
|
// that scopes have been analyzed.
|
2010-10-04 11:35:46 +00:00
|
|
|
Scope* scope_;
|
2010-10-04 14:30:43 +00:00
|
|
|
// The compiled code.
|
|
|
|
Handle<Code> code_;
|
2010-09-30 08:48:37 +00:00
|
|
|
|
2010-10-04 11:35:46 +00:00
|
|
|
// Possible initial inputs to the compilation process.
|
|
|
|
Handle<JSFunction> closure_;
|
|
|
|
Handle<SharedFunctionInfo> shared_info_;
|
|
|
|
Handle<Script> script_;
|
2010-09-30 08:48:37 +00:00
|
|
|
|
2010-10-04 11:35:46 +00:00
|
|
|
// Fields possibly needed for eager compilation, NULL by default.
|
|
|
|
v8::Extension* extension_;
|
|
|
|
ScriptDataImpl* pre_parse_data_;
|
2010-09-30 08:48:37 +00:00
|
|
|
|
2010-10-04 14:30:43 +00:00
|
|
|
// The context of the caller is needed for eval code, and will be a null
|
|
|
|
// handle otherwise.
|
|
|
|
Handle<Context> calling_context_;
|
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
// Compilation mode flag and whether deoptimization is allowed.
|
|
|
|
Mode mode_;
|
|
|
|
bool supports_deoptimization_;
|
|
|
|
int osr_ast_id_;
|
|
|
|
|
2010-10-04 11:35:46 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(CompilationInfo);
|
2010-01-29 11:55:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// The V8 compiler
|
|
|
|
//
|
|
|
|
// General strategy: Source code is translated into an anonymous function w/o
|
|
|
|
// parameters which then can be executed. If the source code contains other
|
|
|
|
// functions, they will be compiled and allocated as part of the compilation
|
|
|
|
// of the source code.
|
|
|
|
|
2010-10-04 14:30:43 +00:00
|
|
|
// Please note this interface returns shared function infos. This means you
|
|
|
|
// need to call Factory::NewFunctionFromSharedFunctionInfo before you have a
|
|
|
|
// real function with a context.
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
class Compiler : public AllStatic {
|
|
|
|
public:
|
2011-01-18 13:43:48 +00:00
|
|
|
// Default maximum number of function optimization attempts before we
|
|
|
|
// give up.
|
|
|
|
static const int kDefaultMaxOptCount = 10;
|
|
|
|
|
|
|
|
// All routines return a SharedFunctionInfo.
|
|
|
|
// If an error occurs an exception is raised and the return handle
|
|
|
|
// contains NULL.
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Compile a String source within a context.
|
2010-03-23 06:04:44 +00:00
|
|
|
static Handle<SharedFunctionInfo> Compile(Handle<String> source,
|
|
|
|
Handle<Object> script_name,
|
|
|
|
int line_offset,
|
|
|
|
int column_offset,
|
|
|
|
v8::Extension* extension,
|
|
|
|
ScriptDataImpl* pre_data,
|
|
|
|
Handle<Object> script_data,
|
|
|
|
NativesFlag is_natives_code);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Compile a String source within a context for Eval.
|
2010-03-23 06:04:44 +00:00
|
|
|
static Handle<SharedFunctionInfo> CompileEval(Handle<String> source,
|
|
|
|
Handle<Context> context,
|
2010-10-27 09:19:43 +00:00
|
|
|
bool is_global);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2010-10-04 14:30:43 +00:00
|
|
|
// Compile from function info (used for lazy compilation). Returns true on
|
|
|
|
// success and false if the compilation resulted in a stack overflow.
|
2010-01-29 11:55:40 +00:00
|
|
|
static bool CompileLazy(CompilationInfo* info);
|
2009-11-04 17:59:24 +00:00
|
|
|
|
2010-10-04 14:30:43 +00:00
|
|
|
// Compile a shared function info object (the function is possibly lazily
|
|
|
|
// compiled).
|
2010-03-23 06:04:44 +00:00
|
|
|
static Handle<SharedFunctionInfo> BuildFunctionInfo(FunctionLiteral* node,
|
2010-10-04 14:30:43 +00:00
|
|
|
Handle<Script> script);
|
2009-11-04 17:59:24 +00:00
|
|
|
|
|
|
|
// Set the function info for a newly compiled function.
|
2010-03-23 06:04:44 +00:00
|
|
|
static void SetFunctionInfo(Handle<SharedFunctionInfo> function_info,
|
2009-11-04 17:59:24 +00:00
|
|
|
FunctionLiteral* lit,
|
|
|
|
bool is_toplevel,
|
|
|
|
Handle<Script> script);
|
2010-02-04 15:35:42 +00:00
|
|
|
|
2010-10-04 14:30:43 +00:00
|
|
|
#ifdef ENABLE_DEBUGGER_SUPPORT
|
|
|
|
static bool MakeCodeForLiveEdit(CompilationInfo* info);
|
|
|
|
#endif
|
|
|
|
|
2010-03-25 14:55:53 +00:00
|
|
|
static void RecordFunctionCompilation(Logger::LogEventsAndTags tag,
|
|
|
|
Handle<String> name,
|
|
|
|
int start_position,
|
2010-10-04 14:30:43 +00:00
|
|
|
CompilationInfo* info);
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
2009-05-14 12:18:25 +00:00
|
|
|
|
|
|
|
// During compilation we need a global list of handles to constants
|
|
|
|
// for frame elements. When the zone gets deleted, we make sure to
|
|
|
|
// clear this list of handles as well.
|
|
|
|
class CompilationZoneScope : public ZoneScope {
|
|
|
|
public:
|
|
|
|
explicit CompilationZoneScope(ZoneScopeMode mode) : ZoneScope(mode) { }
|
|
|
|
virtual ~CompilationZoneScope() {
|
2009-05-15 11:09:51 +00:00
|
|
|
if (ShouldDeleteOnExit()) {
|
|
|
|
FrameElement::ClearConstantList();
|
|
|
|
Result::ClearConstantList();
|
|
|
|
}
|
2009-05-14 12:18:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
} } // namespace v8::internal
|
|
|
|
|
|
|
|
#endif // V8_COMPILER_H_
|