Added bogo "unchecked" type that will evolve later.
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"
33 // A boolean type semantically represents 0 or 1. As an optimization, its
34 // internal storage type is the same as int32, and any non-zero value
35 // compares positively to "true".
38 // A character is an arbitrary 8-bit value. Characters are not supported
39 // as a normal storage class; they may only be used for arrays. When read
40 // from an array, they are immediately sign extended to 32 bits. Integers
41 // stored into an char array are truncated to 8 bits.
42 PrimitiveType_Char, // signed, 8-bit character
44 // Enumerations and untyped variables are stored as signed, 32-bit integers.
47 // Floating point values are 32-bit IEEE-754 floats. This is required ro
48 // SourcePawn 1 compatibility.
51 // This is a native-sized integer type (32-bit on 32-bit systems, 64-bit
52 // on 64-bit systems), and does not exist in language semantics.
58 class Type : public Object
61 // A primitive is a plain-old-data type.
64 // An array is a fixed-length vector of any other type.
67 // A reference type may only be specified on parameters, and
68 // references may only be computed to primitives or enums.
71 // A function type encapsulates a function signature.
74 // A native is a limited type of function used for system calls.
77 // Void is an internal type used to specify that a function returns no
81 // Enums are plain-old-data with int32 storage. They allow for extra
82 // type checking that would not be possible with integers.
85 // Structs are composite records that may contain primitives, arrays,
86 // functions, enums, and other structs.
89 // Unchecked is a magic type that has implicit, bitwise coercion to
90 // int32, float, bool, or an enum.
100 static const int UNKNOWN_SIZE = -1;
101 static const size_t MAX_UNSAFE_ARRAY_DEPTH = 5;
103 static Type *NewVoid(Zone *zone);
104 static Type *NewUnchecked(Zone *zone);
105 static Type *NewPrimitive(Zone *zone, PrimitiveType type);
106 static Type *NewArray(Zone *zone, Handle<Type> contained, int elements, bool isConst);
107 static Type *NewReference(Zone *zone, Handle<Type> contained, bool isConst);
108 static Type *NewFunction(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters);
109 static Type *NewNative(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters, bool isVariadic);
110 static Type *NewEnum(Zone *zone, Handle<String> name);
111 static Type *NewStruct(Zone *zone, Handle<String> name, Handle<FixedArray> fields, Handle<Map> newMap);
113 static bool Compare(Type *left, Type *right);
115 static Type *cast(Object *obj) {
116 assert(!obj || obj->is(MapKind_Type));
121 return kind() == PRIMITIVE;
124 return kind() == ARRAY;
127 return kind() == REFERENCE;
130 return kind() == FUNCTION || kind() == NATIVE;
133 return kind() == NATIVE;
136 return kind() == VOID;
139 return kind() == UNCHECKED;
142 return kind() == STRUCT;
144 bool isNativeVariadic() {
146 return variadicNative_;
148 PrimitiveType primitive() {
149 assert(isPrimitive());
152 PrimitiveType pod() {
153 assert(isPrimitive() || isEnum());
155 ? (primitive() == PrimitiveType_Bool)
156 ? PrimitiveType_Int32
158 : PrimitiveType_Int32;
161 assert(isArray() || isReference());
162 return Type::cast(contained_);
174 Type *temp = contained();
175 while (temp->isArray())
176 temp = temp->contained();
180 return isPrimitive() && primitive() == PrimitiveType_Float;
183 return isPrimitive() && primitive() == PrimitiveType_Int32;
185 bool isInt32OrEnum() {
186 return isInt32() || isEnum();
189 return isPrimitive() && primitive() == PrimitiveType_Bool;
193 contained()->isPrimitive() &&
194 contained()->primitive() == PrimitiveType_Char;
197 assert(isFunction());
200 FixedArray *parameters() {
201 assert(isFunction());
204 Type *parameterAt(size_t i) {
205 return Type::cast(parameters()->at(i));
208 return !isPrimitive();
211 assert(kind() == PRIMITIVE || kind() == ARRAY || kind() == REFERENCE);
215 return kind() == ENUM;
219 assert(isEnum() || isStruct());
222 FixedArray *fields() {
226 Descriptor *lookupField(Handle<String> name, unsigned *index);
228 // Objects of this type can be created with this cached map.
230 assert(isStruct() || isArray());
233 void setNewMap(Map *map) {
239 static inline size_t offsetOfContained() {
240 return OFFSETOF(Type, contained_);
242 static inline size_t offsetOfReturnType() {
243 return OFFSETOF(Type, returnType_);
245 static inline size_t offsetOfParameters() {
246 return OFFSETOF(Type, parameters_);
248 static inline size_t offsetOfName() {
249 return OFFSETOF(Type, name_);
251 static inline size_t offsetOfFields() {
252 return OFFSETOF(Type, fields_);
254 static inline size_t offsetOfNewMap() {
255 return OFFSETOF(Type, newMap_);
257 // End GC Descriptor.
261 PrimitiveType primitive_;
263 // Primitives, arrays, and references.
270 // Arrays and references only.
271 Barriered<Type> contained_;
274 Barriered<Type> returnType_;
275 Barriered<FixedArray> parameters_;
276 bool variadicNative_;
278 // Enums and structs only.
279 Barriered<String> name_;
282 Barriered<FixedArray> fields_;
283 Barriered<Map> newMap_;
294 static BoxedPrimitive Int(int i) {
296 b.type_ = PrimitiveType_Int32;
301 PrimitiveType type() const {
305 assert(type() == PrimitiveType_Int32);
312 #endif // _include_jc_types_h_