Introduced type semantics, completely replacing tag semantics.
1 /* vim: set ts=4 sw=4 tw=99 et:
3 * Copyright (C) 2012 David Anderson
5 * This file is part of JITCraft.
7 * JITCraft is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License as published by the Free
9 * Software Foundation, either version 3 of the License, or (at your option)
12 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with
17 * JITCraft. If not, see http://www.gnu.org/licenses/.
19 #ifndef _include_jc_types_h_
20 #define _include_jc_types_h_
24 #include "FixedArray.h"
32 // A boolean type semantically represents 0 or 1. As an optimization, its
33 // internal storage type is the same as int32, and any non-zero value
34 // compares positively to "true".
37 // A character is an arbitrary 8-bit value. Characters are not supported
38 // as a normal storage class; they may only be used for arrays. When read
39 // from an array, they are immediately sign extended to 32 bits. Integers
40 // stored into an char array are truncated to 8 bits.
41 PrimitiveType_Char, // signed, 8-bit character
43 // Enumerations and untyped variables are stored as signed, 32-bit integers.
46 // Floating point values are 32-bit IEEE-754 floats. This is required ro
47 // SourcePawn 1 compatibility.
50 // This is a native-sized integer type (32-bit on 32-bit systems, 64-bit
51 // on 64-bit systems), and does not exist in language semantics.
57 class Type : public Object
75 static const int UNKNOWN_SIZE = -1;
76 static const size_t MAX_UNSAFE_ARRAY_DEPTH = 5;
78 static Type *NewVoid(Zone *zone);
79 static Type *NewPrimitive(Zone *zone, PrimitiveType type);
80 static Type *NewArray(Zone *zone, Handle<Type> contained, int elements, bool isConst);
81 static Type *NewReference(Zone *zone, Handle<Type> contained, bool isConst);
82 static Type *NewFunction(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters);
83 static Type *NewNative(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters, bool isVariadic);
84 static Type *NewEnum(Zone *zone, Handle<String> name);
86 static bool Compare(Type *left, Type *right);
88 static Type *cast(Object *obj) {
89 assert(!obj || obj->is(MapKind_Type));
94 return kind() == PRIMITIVE;
97 return kind() == ARRAY;
100 return kind() == REFERENCE;
103 return kind() == FUNCTION || kind() == NATIVE;
106 return kind() == NATIVE;
109 return kind() == VOID;
111 bool isNativeVariadic() {
113 return variadicNative_;
115 PrimitiveType primitive() {
116 assert(isPrimitive());
119 PrimitiveType pod() {
120 assert(isPrimitive() || isEnum());
123 : PrimitiveType_Int32;
126 assert(isArray() || isReference());
127 return Type::cast(contained_);
139 Type *temp = contained();
140 while (temp->isArray())
141 temp = temp->contained();
145 return isPrimitive() && primitive() == PrimitiveType_Float;
148 return isPrimitive() && primitive() == PrimitiveType_Int32;
150 bool isInt32OrEnum() {
151 return isInt32() || isEnum();
155 contained()->isPrimitive() &&
156 contained()->primitive() == PrimitiveType_Char;
159 assert(isFunction());
162 FixedArray *parameters() {
163 assert(isFunction());
166 Type *parameterAt(size_t i) {
167 return Type::cast(parameters()->at(i));
170 return !isPrimitive();
173 assert(kind() == PRIMITIVE || kind() == ARRAY || kind() == REFERENCE);
177 return kind() == ENUM;
186 static inline size_t offsetOfContained() {
187 return OFFSETOF(Type, contained_);
189 static inline size_t offsetOfReturnType() {
190 return OFFSETOF(Type, returnType_);
192 static inline size_t offsetOfParameters() {
193 return OFFSETOF(Type, parameters_);
195 static inline size_t offsetOfName() {
196 return OFFSETOF(Type, name_);
198 // End GC Descriptor.
202 PrimitiveType primitive_;
204 // Primitives, arrays, and references.
211 // Arrays and references only.
212 Barriered<Type> contained_;
215 Barriered<Type> returnType_;
216 Barriered<FixedArray> parameters_;
217 bool variadicNative_;
220 Barriered<String> name_;
231 static BoxedPrimitive Int(int i) {
233 b.type_ = PrimitiveType_Int32;
238 PrimitiveType type() const {
242 assert(type() == PrimitiveType_Int32);
249 #endif // _include_jc_types_h_