// Copyright 2012 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef V8_COMPILER_H_ #define V8_COMPILER_H_ #include "src/allocation.h" #include "src/ast.h" #include "src/bailout-reason.h" #include "src/zone.h" namespace v8 { namespace internal { class AstValueFactory; class HydrogenCodeStub; // ParseRestriction is used to restrict the set of valid statements in a // unit of compilation. Restriction violations cause a syntax error. enum ParseRestriction { NO_PARSE_RESTRICTION, // All expressions are allowed. ONLY_SINGLE_FUNCTION_LITERAL // Only a single FunctionLiteral expression. }; struct OffsetRange { OffsetRange(int from, int to) : from(from), to(to) {} int from; int to; }; // This class encapsulates encoding and decoding of sources positions from // which hydrogen values originated. // When FLAG_track_hydrogen_positions is set this object encodes the // identifier of the inlining and absolute offset from the start of the // inlined function. // When the flag is not set we simply track absolute offset from the // script start. class SourcePosition { public: SourcePosition(const SourcePosition& other) : value_(other.value_) {} static SourcePosition Unknown() { return SourcePosition(kNoPosition); } bool IsUnknown() const { return value_ == kNoPosition; } uint32_t position() const { return PositionField::decode(value_); } void set_position(uint32_t position) { if (FLAG_hydrogen_track_positions) { value_ = static_cast(PositionField::update(value_, position)); } else { value_ = position; } } uint32_t inlining_id() const { return InliningIdField::decode(value_); } void set_inlining_id(uint32_t inlining_id) { if (FLAG_hydrogen_track_positions) { value_ = static_cast(InliningIdField::update(value_, inlining_id)); } } uint32_t raw() const { return value_; } private: static const uint32_t kNoPosition = static_cast(RelocInfo::kNoPosition); typedef BitField InliningIdField; // Offset from the start of the inlined function. typedef BitField PositionField; explicit SourcePosition(uint32_t value) : value_(value) {} friend class HPositionInfo; friend class LCodeGenBase; // If FLAG_hydrogen_track_positions is set contains bitfields InliningIdField // and PositionField. // Otherwise contains absolute offset from the script start. uint32_t value_; }; std::ostream& operator<<(std::ostream& os, const SourcePosition& p); class InlinedFunctionInfo { public: explicit InlinedFunctionInfo(Handle shared) : shared_(shared), start_position_(shared->start_position()) {} Handle shared() const { return shared_; } int start_position() const { return start_position_; } private: Handle shared_; int start_position_; }; class ScriptData { public: ScriptData(const byte* data, int length); ~ScriptData() { if (owns_data_) DeleteArray(data_); } const byte* data() const { return data_; } int length() const { return length_; } bool rejected() const { return rejected_; } void Reject() { rejected_ = true; } void AcquireDataOwnership() { DCHECK(!owns_data_); owns_data_ = true; } void ReleaseDataOwnership() { DCHECK(owns_data_); owns_data_ = false; } private: bool owns_data_ : 1; bool rejected_ : 1; const byte* data_; int length_; DISALLOW_COPY_AND_ASSIGN(ScriptData); }; // CompilationInfo encapsulates some information known at compile time. It // is constructed based on the resources available at compile-time. class CompilationInfo { public: // Various configuration flags for a compilation, as well as some properties // of the compiled code produced by a compilation. enum Flag { kLazy = 1 << 0, kEval = 1 << 1, kGlobal = 1 << 2, kStrictMode = 1 << 3, kStrongMode = 1 << 4, kThisHasUses = 1 << 5, kNative = 1 << 6, kDeferredCalling = 1 << 7, kNonDeferredCalling = 1 << 8, kSavesCallerDoubles = 1 << 9, kRequiresFrame = 1 << 10, kMustNotHaveEagerFrame = 1 << 11, kDeoptimizationSupport = 1 << 12, kDebug = 1 << 13, kCompilingForDebugging = 1 << 14, kParseRestriction = 1 << 15, kSerializing = 1 << 16, kContextSpecializing = 1 << 17, kInliningEnabled = 1 << 18, kTypingEnabled = 1 << 19, kDisableFutureOptimization = 1 << 20, kModule = 1 << 21, kToplevel = 1 << 22, kSplittingEnabled = 1 << 23 }; CompilationInfo(Handle closure, Zone* zone); CompilationInfo(Handle