Commit 95560650 authored by heimbuef's avatar heimbuef Committed by Commit bot

Better pack fields in Variable

Used a BitField to for Variable fields instead of relying on the compiler, saving some memory probably.
This reduces sizeof(Variable) from 64 to 40 on x64

BUG=v8:5209

Review-Url: https://codereview.chromium.org/2257493002
Cr-Commit-Position: refs/heads/master@{#38891}
parent 3c1d076a
...@@ -33,16 +33,15 @@ Variable::Variable(Scope* scope, const AstRawString* name, VariableMode mode, ...@@ -33,16 +33,15 @@ Variable::Variable(Scope* scope, const AstRawString* name, VariableMode mode,
MaybeAssignedFlag maybe_assigned_flag) MaybeAssignedFlag maybe_assigned_flag)
: scope_(scope), : scope_(scope),
name_(name), name_(name),
mode_(mode), local_if_not_shadowed_(nullptr),
kind_(kind),
location_(VariableLocation::UNALLOCATED),
index_(-1), index_(-1),
initializer_position_(kNoSourcePosition), initializer_position_(kNoSourcePosition),
local_if_not_shadowed_(NULL), bit_field_(MaybeAssignedFlagField::encode(maybe_assigned_flag) |
force_context_allocation_(false), InitializationFlagField::encode(initialization_flag) |
is_used_(false), VariableModeField::encode(mode) | IsUsedField::encode(false) |
initialization_flag_(initialization_flag), ForceContextAllocationField::encode(false) |
maybe_assigned_(maybe_assigned_flag) { LocationField::encode(VariableLocation::UNALLOCATED) |
KindField::encode(kind)) {
// Var declared variables never need initialization. // Var declared variables never need initialization.
DCHECK(!(mode == VAR && initialization_flag == kNeedsInitialization)); DCHECK(!(mode == VAR && initialization_flag == kNeedsInitialization));
} }
...@@ -51,8 +50,8 @@ Variable::Variable(Scope* scope, const AstRawString* name, VariableMode mode, ...@@ -51,8 +50,8 @@ Variable::Variable(Scope* scope, const AstRawString* name, VariableMode mode,
bool Variable::IsGlobalObjectProperty() const { bool Variable::IsGlobalObjectProperty() const {
// Temporaries are never global, they must always be allocated in the // Temporaries are never global, they must always be allocated in the
// activation frame. // activation frame.
return (IsDynamicVariableMode(mode_) || return (IsDynamicVariableMode(mode()) ||
(IsDeclaredVariableMode(mode_) && !IsLexicalVariableMode(mode_))) && (IsDeclaredVariableMode(mode()) && !IsLexicalVariableMode(mode()))) &&
scope_ != NULL && scope_->is_script_scope(); scope_ != NULL && scope_->is_script_scope();
} }
...@@ -60,7 +59,7 @@ bool Variable::IsGlobalObjectProperty() const { ...@@ -60,7 +59,7 @@ bool Variable::IsGlobalObjectProperty() const {
bool Variable::IsStaticGlobalObjectProperty() const { bool Variable::IsStaticGlobalObjectProperty() const {
// Temporaries are never global, they must always be allocated in the // Temporaries are never global, they must always be allocated in the
// activation frame. // activation frame.
return (IsDeclaredVariableMode(mode_) && !IsLexicalVariableMode(mode_)) && return (IsDeclaredVariableMode(mode()) && !IsLexicalVariableMode(mode())) &&
scope_ != NULL && scope_->is_script_scope(); scope_ != NULL && scope_->is_script_scope();
} }
......
...@@ -17,7 +17,13 @@ namespace internal { ...@@ -17,7 +17,13 @@ namespace internal {
// after binding and variable allocation. // after binding and variable allocation.
class Variable final : public ZoneObject { class Variable final : public ZoneObject {
public: public:
enum Kind { NORMAL, FUNCTION, THIS, ARGUMENTS }; enum Kind : uint8_t {
NORMAL,
FUNCTION,
THIS,
ARGUMENTS,
kLastKind = ARGUMENTS
};
Variable(Scope* scope, const AstRawString* name, VariableMode mode, Kind kind, Variable(Scope* scope, const AstRawString* name, VariableMode mode, Kind kind,
InitializationFlag initialization_flag, InitializationFlag initialization_flag,
...@@ -38,51 +44,54 @@ class Variable final : public ZoneObject { ...@@ -38,51 +44,54 @@ class Variable final : public ZoneObject {
Handle<String> name() const { return name_->string(); } Handle<String> name() const { return name_->string(); }
const AstRawString* raw_name() const { return name_; } const AstRawString* raw_name() const { return name_; }
VariableMode mode() const { return mode_; } VariableMode mode() const { return VariableModeField::decode(bit_field_); }
bool has_forced_context_allocation() const { bool has_forced_context_allocation() const {
return force_context_allocation_; return ForceContextAllocationField::decode(bit_field_);
} }
void ForceContextAllocation() { void ForceContextAllocation() {
DCHECK(IsUnallocated() || IsContextSlot()); bit_field_ = ForceContextAllocationField::update(bit_field_, true);
force_context_allocation_ = true; }
bool is_used() { return IsUsedField::decode(bit_field_); }
void set_is_used() { bit_field_ = IsUsedField::update(bit_field_, true); }
MaybeAssignedFlag maybe_assigned() const {
return MaybeAssignedFlagField::decode(bit_field_);
}
void set_maybe_assigned() {
bit_field_ = MaybeAssignedFlagField::update(bit_field_, kMaybeAssigned);
} }
bool is_used() { return is_used_; }
void set_is_used() { is_used_ = true; }
MaybeAssignedFlag maybe_assigned() const { return maybe_assigned_; }
void set_maybe_assigned() { maybe_assigned_ = kMaybeAssigned; }
int initializer_position() { return initializer_position_; } int initializer_position() { return initializer_position_; }
void set_initializer_position(int pos) { initializer_position_ = pos; } void set_initializer_position(int pos) { initializer_position_ = pos; }
bool IsUnallocated() const { bool IsUnallocated() const {
return location_ == VariableLocation::UNALLOCATED; return location() == VariableLocation::UNALLOCATED;
} }
bool IsParameter() const { return location_ == VariableLocation::PARAMETER; } bool IsParameter() const { return location() == VariableLocation::PARAMETER; }
bool IsStackLocal() const { return location_ == VariableLocation::LOCAL; } bool IsStackLocal() const { return location() == VariableLocation::LOCAL; }
bool IsStackAllocated() const { return IsParameter() || IsStackLocal(); } bool IsStackAllocated() const { return IsParameter() || IsStackLocal(); }
bool IsContextSlot() const { return location_ == VariableLocation::CONTEXT; } bool IsContextSlot() const { return location() == VariableLocation::CONTEXT; }
bool IsGlobalSlot() const { return location_ == VariableLocation::GLOBAL; } bool IsGlobalSlot() const { return location() == VariableLocation::GLOBAL; }
bool IsUnallocatedOrGlobalSlot() const { bool IsUnallocatedOrGlobalSlot() const {
return IsUnallocated() || IsGlobalSlot(); return IsUnallocated() || IsGlobalSlot();
} }
bool IsLookupSlot() const { return location_ == VariableLocation::LOOKUP; } bool IsLookupSlot() const { return location() == VariableLocation::LOOKUP; }
bool IsGlobalObjectProperty() const; bool IsGlobalObjectProperty() const;
bool IsStaticGlobalObjectProperty() const; bool IsStaticGlobalObjectProperty() const;
bool is_dynamic() const { return IsDynamicVariableMode(mode_); } bool is_dynamic() const { return IsDynamicVariableMode(mode()); }
bool is_const_mode() const { return IsImmutableVariableMode(mode_); } bool is_const_mode() const { return IsImmutableVariableMode(mode()); }
bool binding_needs_init() const { bool binding_needs_init() const {
DCHECK(initialization_flag_ != kNeedsInitialization || DCHECK(initialization_flag() != kNeedsInitialization ||
IsLexicalVariableMode(mode_)); IsLexicalVariableMode(mode()));
return initialization_flag_ == kNeedsInitialization; return initialization_flag() == kNeedsInitialization;
} }
bool is_function() const { return kind_ == FUNCTION; } bool is_function() const { return kind() == FUNCTION; }
bool is_this() const { return kind_ == THIS; } bool is_this() const { return kind() == THIS; }
bool is_arguments() const { return kind_ == ARGUMENTS; } bool is_arguments() const { return kind() == ARGUMENTS; }
Variable* local_if_not_shadowed() const { Variable* local_if_not_shadowed() const {
DCHECK(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL); DCHECK(mode() == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL);
return local_if_not_shadowed_; return local_if_not_shadowed_;
} }
...@@ -90,15 +99,21 @@ class Variable final : public ZoneObject { ...@@ -90,15 +99,21 @@ class Variable final : public ZoneObject {
local_if_not_shadowed_ = local; local_if_not_shadowed_ = local;
} }
VariableLocation location() const { return location_; } VariableLocation location() const {
int index() const { return index_; } return LocationField::decode(bit_field_);
}
Kind kind() const { return KindField::decode(bit_field_); }
InitializationFlag initialization_flag() const { InitializationFlag initialization_flag() const {
return initialization_flag_; return InitializationFlagField::decode(bit_field_);
} }
int index() const { return index_; }
void AllocateTo(VariableLocation location, int index) { void AllocateTo(VariableLocation location, int index) {
DCHECK(IsUnallocated() || (location_ == location && index_ == index)); DCHECK(IsUnallocated() ||
location_ = location; (this->location() == location && this->index() == index));
bit_field_ = LocationField::update(bit_field_, location);
DCHECK_EQ(location, this->location());
index_ = index; index_ = index;
} }
...@@ -107,23 +122,29 @@ class Variable final : public ZoneObject { ...@@ -107,23 +122,29 @@ class Variable final : public ZoneObject {
private: private:
Scope* scope_; Scope* scope_;
const AstRawString* name_; const AstRawString* name_;
VariableMode mode_;
Kind kind_;
VariableLocation location_;
int index_;
int initializer_position_;
// If this field is set, this variable references the stored locally bound // If this field is set, this variable references the stored locally bound
// variable, but it might be shadowed by variable bindings introduced by // variable, but it might be shadowed by variable bindings introduced by
// sloppy 'eval' calls between the reference scope (inclusive) and the // sloppy 'eval' calls between the reference scope (inclusive) and the
// binding scope (exclusive). // binding scope (exclusive).
Variable* local_if_not_shadowed_; Variable* local_if_not_shadowed_;
int index_;
// Usage info. int initializer_position_;
bool force_context_allocation_; // set by variable resolver uint16_t bit_field_;
bool is_used_;
InitializationFlag initialization_flag_; class VariableModeField : public BitField16<VariableMode, 0, 3> {};
MaybeAssignedFlag maybe_assigned_; class KindField : public BitField16<Kind, VariableModeField::kNext, 2> {};
class LocationField
: public BitField16<VariableLocation, KindField::kNext, 3> {};
class ForceContextAllocationField
: public BitField16<bool, LocationField::kNext, 1> {};
class IsUsedField
: public BitField16<bool, ForceContextAllocationField::kNext, 1> {};
class InitializationFlagField
: public BitField16<InitializationFlag, IsUsedField::kNext, 2> {};
class MaybeAssignedFlagField
: public BitField16<MaybeAssignedFlag, InitializationFlagField::kNext,
2> {};
}; };
} // namespace internal } // namespace internal
} // namespace v8 } // namespace v8
......
...@@ -877,7 +877,7 @@ const double kMaxSafeInteger = 9007199254740991.0; // 2^53-1 ...@@ -877,7 +877,7 @@ const double kMaxSafeInteger = 9007199254740991.0; // 2^53-1
// The order of this enum has to be kept in sync with the predicates below. // The order of this enum has to be kept in sync with the predicates below.
enum VariableMode { enum VariableMode : uint8_t {
// User declared variables: // User declared variables:
VAR, // declared via 'var', and 'function' declarations VAR, // declared via 'var', and 'function' declarations
...@@ -898,10 +898,12 @@ enum VariableMode { ...@@ -898,10 +898,12 @@ enum VariableMode {
// variable is global unless it has been shadowed // variable is global unless it has been shadowed
// by an eval-introduced variable // by an eval-introduced variable
DYNAMIC_LOCAL // requires dynamic lookup, but we know that the DYNAMIC_LOCAL, // requires dynamic lookup, but we know that the
// variable is local and where it is unless it // variable is local and where it is unless it
// has been shadowed by an eval-introduced // has been shadowed by an eval-introduced
// variable // variable
kLastVariableMode = DYNAMIC_LOCAL
}; };
inline bool IsDynamicVariableMode(VariableMode mode) { inline bool IsDynamicVariableMode(VariableMode mode) {
...@@ -923,7 +925,7 @@ inline bool IsImmutableVariableMode(VariableMode mode) { ...@@ -923,7 +925,7 @@ inline bool IsImmutableVariableMode(VariableMode mode) {
return mode == CONST || mode == CONST_LEGACY; return mode == CONST || mode == CONST_LEGACY;
} }
enum class VariableLocation { enum VariableLocation : uint8_t {
// Before and during variable allocation, a variable whose location is // Before and during variable allocation, a variable whose location is
// not yet determined. After allocation, a variable looked up as a // not yet determined. After allocation, a variable looked up as a
// property on the global object (and possibly absent). name() is the // property on the global object (and possibly absent). name() is the
...@@ -956,7 +958,9 @@ enum class VariableLocation { ...@@ -956,7 +958,9 @@ enum class VariableLocation {
LOOKUP, LOOKUP,
// A named slot in a module's export table. // A named slot in a module's export table.
MODULE MODULE,
kLastVariableLocation = MODULE
}; };
// ES6 Draft Rev3 10.2 specifies declarative environment records with mutable // ES6 Draft Rev3 10.2 specifies declarative environment records with mutable
...@@ -990,14 +994,9 @@ enum class VariableLocation { ...@@ -990,14 +994,9 @@ enum class VariableLocation {
// The following enum specifies a flag that indicates if the binding needs a // The following enum specifies a flag that indicates if the binding needs a
// distinct initialization step (kNeedsInitialization) or if the binding is // distinct initialization step (kNeedsInitialization) or if the binding is
// immediately initialized upon creation (kCreatedInitialized). // immediately initialized upon creation (kCreatedInitialized).
enum InitializationFlag { enum InitializationFlag : uint8_t { kNeedsInitialization, kCreatedInitialized };
kNeedsInitialization,
kCreatedInitialized
};
enum MaybeAssignedFlag { kNotAssigned, kMaybeAssigned };
enum MaybeAssignedFlag : uint8_t { kNotAssigned, kMaybeAssigned };
// Serialized in PreparseData, so numeric values should not be changed. // Serialized in PreparseData, so numeric values should not be changed.
enum ParseErrorType { kSyntaxError = 0, kReferenceError = 1 }; enum ParseErrorType { kSyntaxError = 0, kReferenceError = 1 };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment