2010-02-15 14:24:38 +00:00
|
|
|
// Copyright 2010 the V8 project authors. All rights reserved.
|
|
|
|
// 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.
|
|
|
|
|
2010-03-25 12:44:15 +00:00
|
|
|
#ifndef V8_TYPE_INFO_H_
|
|
|
|
#define V8_TYPE_INFO_H_
|
2010-02-15 14:24:38 +00:00
|
|
|
|
2010-03-24 15:55:15 +00:00
|
|
|
#include "globals.h"
|
|
|
|
|
2010-02-15 14:24:38 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2010-03-05 23:54:13 +00:00
|
|
|
// Unknown
|
|
|
|
// |
|
2010-03-24 12:34:27 +00:00
|
|
|
// PrimitiveType
|
|
|
|
// | \--------|
|
|
|
|
// Number String
|
|
|
|
// / | |
|
2010-03-24 15:29:41 +00:00
|
|
|
// Double Integer32 |
|
2010-03-24 12:34:27 +00:00
|
|
|
// | | /
|
|
|
|
// | Smi /
|
|
|
|
// | / /
|
|
|
|
// Uninitialized.
|
2010-03-05 23:54:13 +00:00
|
|
|
|
2010-03-25 12:44:15 +00:00
|
|
|
class TypeInfo {
|
2010-02-15 14:24:38 +00:00
|
|
|
public:
|
2010-03-25 12:44:15 +00:00
|
|
|
TypeInfo() { }
|
2010-03-05 23:54:13 +00:00
|
|
|
|
2010-03-25 12:44:15 +00:00
|
|
|
static inline TypeInfo Unknown();
|
2010-03-24 12:34:27 +00:00
|
|
|
// We know it's a primitive type.
|
2010-03-25 12:44:15 +00:00
|
|
|
static inline TypeInfo Primitive();
|
2010-03-05 23:54:13 +00:00
|
|
|
// We know it's a number of some sort.
|
2010-03-25 12:44:15 +00:00
|
|
|
static inline TypeInfo Number();
|
2010-03-05 23:54:13 +00:00
|
|
|
// We know it's signed or unsigned 32 bit integer.
|
2010-03-25 12:44:15 +00:00
|
|
|
static inline TypeInfo Integer32();
|
2010-03-05 23:54:13 +00:00
|
|
|
// We know it's a Smi.
|
2010-03-25 12:44:15 +00:00
|
|
|
static inline TypeInfo Smi();
|
2010-03-05 23:54:13 +00:00
|
|
|
// We know it's a heap number.
|
2010-03-25 12:44:15 +00:00
|
|
|
static inline TypeInfo Double();
|
2010-03-24 12:34:27 +00:00
|
|
|
// We know it's a string.
|
2010-03-25 12:44:15 +00:00
|
|
|
static inline TypeInfo String();
|
2010-03-05 23:54:13 +00:00
|
|
|
// We haven't started collecting info yet.
|
2010-03-25 12:44:15 +00:00
|
|
|
static inline TypeInfo Uninitialized();
|
2010-03-05 23:54:13 +00:00
|
|
|
|
|
|
|
// Return compact representation. Very sensitive to enum values below!
|
2010-03-24 12:34:27 +00:00
|
|
|
// Compacting drops information about primtive types and strings types.
|
|
|
|
// We use the compact representation when we only care about number types.
|
2010-03-05 23:54:13 +00:00
|
|
|
int ThreeBitRepresentation() {
|
|
|
|
ASSERT(type_ != kUninitializedType);
|
2010-03-24 12:34:27 +00:00
|
|
|
int answer = type_ & 0xf;
|
|
|
|
answer = answer > 6 ? answer - 2 : answer;
|
2010-03-05 23:54:13 +00:00
|
|
|
ASSERT(answer >= 0);
|
|
|
|
ASSERT(answer <= 7);
|
|
|
|
return answer;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode compact representation. Very sensitive to enum values below!
|
2010-03-25 12:44:15 +00:00
|
|
|
static TypeInfo ExpandedRepresentation(int three_bit_representation) {
|
2010-03-05 23:54:13 +00:00
|
|
|
Type t = static_cast<Type>(three_bit_representation >= 6 ?
|
|
|
|
three_bit_representation + 2 :
|
|
|
|
three_bit_representation);
|
2010-03-24 12:34:27 +00:00
|
|
|
t = (t == kUnknownType) ? t : static_cast<Type>(t | kPrimitiveType);
|
2010-03-05 23:54:13 +00:00
|
|
|
ASSERT(t == kUnknownType ||
|
|
|
|
t == kNumberType ||
|
|
|
|
t == kInteger32Type ||
|
|
|
|
t == kSmiType ||
|
2010-03-24 15:29:41 +00:00
|
|
|
t == kDoubleType);
|
2010-03-25 12:44:15 +00:00
|
|
|
return TypeInfo(t);
|
2010-03-05 23:54:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ToInt() {
|
|
|
|
return type_;
|
|
|
|
}
|
|
|
|
|
2010-03-25 12:44:15 +00:00
|
|
|
static TypeInfo FromInt(int bit_representation) {
|
2010-03-05 23:54:13 +00:00
|
|
|
Type t = static_cast<Type>(bit_representation);
|
|
|
|
ASSERT(t == kUnknownType ||
|
2010-03-24 12:34:27 +00:00
|
|
|
t == kPrimitiveType ||
|
2010-03-05 23:54:13 +00:00
|
|
|
t == kNumberType ||
|
|
|
|
t == kInteger32Type ||
|
|
|
|
t == kSmiType ||
|
2010-03-24 15:29:41 +00:00
|
|
|
t == kDoubleType ||
|
2010-03-24 12:34:27 +00:00
|
|
|
t == kStringType);
|
2010-03-25 12:44:15 +00:00
|
|
|
return TypeInfo(t);
|
2010-03-05 23:54:13 +00:00
|
|
|
}
|
2010-02-15 14:24:38 +00:00
|
|
|
|
|
|
|
// Return the weakest (least precise) common type.
|
2010-03-25 12:44:15 +00:00
|
|
|
static TypeInfo Combine(TypeInfo a, TypeInfo b) {
|
|
|
|
return TypeInfo(static_cast<Type>(a.type_ & b.type_));
|
2010-03-05 23:54:13 +00:00
|
|
|
}
|
|
|
|
|
2010-03-24 15:29:41 +00:00
|
|
|
|
|
|
|
// Integer32 is an integer that can be represented as either a signed
|
|
|
|
// 32-bit integer or as an unsigned 32-bit integer. It has to be
|
2010-03-24 16:37:27 +00:00
|
|
|
// in the range [-2^31, 2^32 - 1]. We also have to check for negative 0
|
|
|
|
// as it is not an Integer32.
|
2010-03-24 15:29:41 +00:00
|
|
|
static inline bool IsInt32Double(double value) {
|
2010-03-24 16:37:27 +00:00
|
|
|
const DoubleRepresentation minus_zero(-0.0);
|
|
|
|
DoubleRepresentation rep(value);
|
|
|
|
if (rep.bits == minus_zero.bits) return false;
|
2010-03-24 15:55:15 +00:00
|
|
|
if (value >= kMinInt && value <= kMaxUInt32) {
|
|
|
|
if (value <= kMaxInt && value == static_cast<int32_t>(value)) {
|
2010-03-24 15:29:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (value == static_cast<uint32_t>(value)) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-03-26 11:34:00 +00:00
|
|
|
static TypeInfo TypeFromValue(Handle<Object> value);
|
2010-03-24 15:29:41 +00:00
|
|
|
|
2010-03-05 23:54:13 +00:00
|
|
|
inline bool IsUnknown() {
|
|
|
|
return type_ == kUnknownType;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool IsNumber() {
|
|
|
|
ASSERT(type_ != kUninitializedType);
|
|
|
|
return ((type_ & kNumberType) == kNumberType);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool IsSmi() {
|
|
|
|
ASSERT(type_ != kUninitializedType);
|
|
|
|
return ((type_ & kSmiType) == kSmiType);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool IsInteger32() {
|
|
|
|
ASSERT(type_ != kUninitializedType);
|
|
|
|
return ((type_ & kInteger32Type) == kInteger32Type);
|
|
|
|
}
|
|
|
|
|
2010-03-24 15:29:41 +00:00
|
|
|
inline bool IsDouble() {
|
2010-03-05 23:54:13 +00:00
|
|
|
ASSERT(type_ != kUninitializedType);
|
2010-03-24 15:29:41 +00:00
|
|
|
return ((type_ & kDoubleType) == kDoubleType);
|
2010-02-15 14:24:38 +00:00
|
|
|
}
|
2010-02-16 13:03:16 +00:00
|
|
|
|
2010-03-05 23:54:13 +00:00
|
|
|
inline bool IsUninitialized() {
|
|
|
|
return type_ == kUninitializedType;
|
2010-02-16 13:03:16 +00:00
|
|
|
}
|
|
|
|
|
2010-03-05 23:54:13 +00:00
|
|
|
const char* ToString() {
|
|
|
|
switch (type_) {
|
|
|
|
case kUnknownType: return "UnknownType";
|
2010-03-24 12:34:27 +00:00
|
|
|
case kPrimitiveType: return "PrimitiveType";
|
2010-03-05 23:54:13 +00:00
|
|
|
case kNumberType: return "NumberType";
|
2010-03-24 12:34:27 +00:00
|
|
|
case kInteger32Type: return "Integer32Type";
|
2010-03-05 23:54:13 +00:00
|
|
|
case kSmiType: return "SmiType";
|
2010-03-24 15:29:41 +00:00
|
|
|
case kDoubleType: return "DoubleType";
|
2010-03-24 12:34:27 +00:00
|
|
|
case kStringType: return "StringType";
|
2010-03-05 23:54:13 +00:00
|
|
|
case kUninitializedType:
|
2010-02-16 13:03:16 +00:00
|
|
|
UNREACHABLE();
|
|
|
|
return "UninitializedType";
|
|
|
|
}
|
|
|
|
UNREACHABLE();
|
|
|
|
return "Unreachable code";
|
|
|
|
}
|
2010-03-05 23:54:13 +00:00
|
|
|
|
|
|
|
private:
|
2010-03-24 12:34:27 +00:00
|
|
|
// We use 6 bits to represent the types.
|
2010-03-05 23:54:13 +00:00
|
|
|
enum Type {
|
2010-03-24 12:34:27 +00:00
|
|
|
kUnknownType = 0, // 000000
|
|
|
|
kPrimitiveType = 0x10, // 010000
|
|
|
|
kNumberType = 0x11, // 010001
|
|
|
|
kInteger32Type = 0x13, // 010011
|
|
|
|
kSmiType = 0x17, // 010111
|
2010-03-24 15:29:41 +00:00
|
|
|
kDoubleType = 0x19, // 011001
|
2010-03-24 12:34:27 +00:00
|
|
|
kStringType = 0x30, // 110000
|
|
|
|
kUninitializedType = 0x3f // 111111
|
2010-03-05 23:54:13 +00:00
|
|
|
};
|
2010-03-25 12:44:15 +00:00
|
|
|
explicit inline TypeInfo(Type t) : type_(t) { }
|
2010-03-05 23:54:13 +00:00
|
|
|
|
|
|
|
Type type_;
|
2010-02-15 14:24:38 +00:00
|
|
|
};
|
|
|
|
|
2010-03-05 23:54:13 +00:00
|
|
|
|
2010-03-25 12:44:15 +00:00
|
|
|
TypeInfo TypeInfo::Unknown() {
|
|
|
|
return TypeInfo(kUnknownType);
|
2010-03-05 23:54:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-25 12:44:15 +00:00
|
|
|
TypeInfo TypeInfo::Primitive() {
|
|
|
|
return TypeInfo(kPrimitiveType);
|
2010-03-24 12:34:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-25 12:44:15 +00:00
|
|
|
TypeInfo TypeInfo::Number() {
|
|
|
|
return TypeInfo(kNumberType);
|
2010-03-05 23:54:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-25 12:44:15 +00:00
|
|
|
TypeInfo TypeInfo::Integer32() {
|
|
|
|
return TypeInfo(kInteger32Type);
|
2010-03-05 23:54:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-25 12:44:15 +00:00
|
|
|
TypeInfo TypeInfo::Smi() {
|
|
|
|
return TypeInfo(kSmiType);
|
2010-03-05 23:54:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-25 12:44:15 +00:00
|
|
|
TypeInfo TypeInfo::Double() {
|
|
|
|
return TypeInfo(kDoubleType);
|
2010-03-05 23:54:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-25 12:44:15 +00:00
|
|
|
TypeInfo TypeInfo::String() {
|
|
|
|
return TypeInfo(kStringType);
|
2010-03-24 12:34:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-25 12:44:15 +00:00
|
|
|
TypeInfo TypeInfo::Uninitialized() {
|
|
|
|
return TypeInfo(kUninitializedType);
|
2010-03-05 23:54:13 +00:00
|
|
|
}
|
|
|
|
|
2010-02-15 14:24:38 +00:00
|
|
|
} } // namespace v8::internal
|
|
|
|
|
2010-03-25 12:44:15 +00:00
|
|
|
#endif // V8_TYPE_INFO_H_
|