Enable dynamic array declarations.
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"
31 #define MAX_ARRAY_DEPTH 5
35 // A boolean type semantically represents 0 or 1. As an optimization, its
36 // internal storage type is the same as int32, and any non-zero value
37 // compares positively to "true".
40 // A character is an arbitrary 8-bit value. Characters are not supported
41 // as a normal storage class; they may only be used for arrays. When read
42 // from an array, they are immediately sign extended to 32 bits. Integers
43 // stored into an char array are truncated to 8 bits.
44 PrimitiveType_Char, // signed, 8-bit character
46 // Enumerations and untyped variables are stored as signed, 32-bit integers.
49 // Floating point values are 32-bit IEEE-754 floats. This is required ro
50 // SourcePawn 1 compatibility.
53 // This is a native-sized integer type (32-bit on 32-bit systems, 64-bit
54 // on 64-bit systems), and does not exist in language semantics.
60 class Type : public Object
63 // A primitive is a plain-old-data type.
66 // An array is a fixed-length vector of any other type.
70 // A reference type may only be specified on parameters, and
71 // references may only be computed to primitives or enums.
74 // A function type encapsulates a function signature.
77 // A native is a limited type of function used for system calls.
80 // Void is an internal type used to specify that a function returns no
84 // Enums are plain-old-data with int32 storage. They allow for extra
85 // type checking that would not be possible with integers.
88 // Structs are composite records that may contain primitives, arrays,
89 // functions, enums, and other structs.
92 // Unchecked is a magic type that has implicit, bitwise coercion to
93 // int32, float, bool, or an enum.
103 static const int DYNAMIC_ARRAY_SIZE = -1;
105 static Type *NewVoid(Zone *zone);
106 static Type *NewUnchecked(Zone *zone);
107 static Type *NewPrimitive(Zone *zone, PrimitiveType type);
108 static Type *NewArray(Zone *zone, Handle<Type> contained, int elements, bool isConst);
109 static Type *NewExternalArray(Zone *zone, Handle<Type> contained);
110 static Type *NewReference(Zone *zone, Handle<Type> contained, bool isConst);
111 static Type *NewFunction(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters,
112 Handle<FixedArray> defaults);
113 static Type *NewNative(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters,
114 Handle<FixedArray> defaults, bool isVariadic);
115 static Type *NewEnum(Zone *zone, Handle<String> name);
116 static Type *NewStruct(Zone *zone, Handle<String> name, Handle<FixedArray> fields, Handle<Map> newMap);
118 static bool Compare(Type *left, Type *right);
120 static Type *cast(Object *obj) {
121 assert(!obj || obj->is(MapKind_Type));
126 return kind() == PRIMITIVE;
129 return kind() == ARRAY || kind() == EXTERNAL_ARRAY;
132 return kind() == REFERENCE;
135 return kind() == FUNCTION || kind() == NATIVE;
138 return kind() == NATIVE;
141 return kind() == VOID;
144 return kind() == UNCHECKED;
147 return kind() == STRUCT;
149 bool isNativeVariadic() {
151 return variadicNative_;
153 PrimitiveType primitive() {
154 assert(isPrimitive());
158 return isPrimitive() || isEnum() || isUnchecked();
160 PrimitiveType pod() {
163 ? (primitive() == PrimitiveType_Bool)
164 ? PrimitiveType_Int32
166 : PrimitiveType_Int32;
169 assert(isArray() || isReference());
170 return Type::cast(contained_);
172 bool isFixedLength() {
174 return elements_ > 0;
176 unsigned fixedLength() {
177 assert(isFixedLength());
184 bool hasInlineElements() {
187 // We don't allocate traceable elements inline, since there's no
188 // point to the optimization; we're going to be allocating lots of
189 // objects anyway (in theory).
190 return isFixedLength() && !contained()->isTraceable();
194 Type *temp = contained();
195 while (temp->isArray())
196 temp = temp->contained();
200 return isPrimitive() && primitive() == PrimitiveType_Float;
203 return isPrimitive() && primitive() == PrimitiveType_Int32;
205 bool isInt32OrEnum() {
206 return isInt32() || isEnum();
209 return isPrimitive() && primitive() == PrimitiveType_Bool;
213 contained()->isPrimitive() &&
214 contained()->primitive() == PrimitiveType_Char;
216 bool isExternalArray() {
217 return kind() == EXTERNAL_ARRAY;
220 assert(isFunction());
223 FixedArray *parameters() {
224 assert(isFunction());
227 FixedArray *defaults() {
228 assert(isFunction());
231 Type *parameterAt(size_t i) {
232 return Type::cast(parameters()->at(i));
235 return !isPrimitive();
238 assert(isPrimitive() ||
244 return kind() == ENUM;
248 assert(isEnum() || isStruct());
251 FixedArray *fields() {
255 Descriptor *lookupField(Handle<String> name, unsigned *index);
257 // Objects of this type can be created with this cached map.
259 assert(isStruct() || isArray());
262 void setNewMap(Map *map) {
268 static inline size_t offsetOfContained() {
269 return OFFSETOF(Type, contained_);
271 static inline size_t offsetOfReturnType() {
272 return OFFSETOF(Type, returnType_);
274 static inline size_t offsetOfParameters() {
275 return OFFSETOF(Type, parameters_);
277 static inline size_t offsetOfName() {
278 return OFFSETOF(Type, name_);
280 static inline size_t offsetOfFields() {
281 return OFFSETOF(Type, fields_);
283 static inline size_t offsetOfNewMap() {
284 return OFFSETOF(Type, newMap_);
286 static inline size_t offsetOfDefaults() {
287 return OFFSETOF(Type, defaults_);
289 // End GC Descriptor.
293 PrimitiveType primitive_;
295 // Primitives, arrays, and references.
302 // Arrays and references only.
303 Barriered<Type> contained_;
306 Barriered<Type> returnType_;
307 Barriered<FixedArray> parameters_;
308 Barriered<FixedArray> defaults_;
309 bool variadicNative_;
311 // Enums and structs only.
312 Barriered<String> name_;
315 Barriered<FixedArray> fields_;
316 Barriered<Map> newMap_;
328 static BoxedPrimitive Int(int i) {
330 b.type_ = PrimitiveType_Int32;
334 static BoxedPrimitive Float(float f) {
336 b.type_ = PrimitiveType_Float;
341 PrimitiveType type() const {
345 assert(type() == PrimitiveType_Int32);
348 float toFloat() const {
349 assert(type() == PrimitiveType_Float);
356 #endif // _include_jc_types_h_