Add some more API.
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 PrimitiveType_Char, // signed, 8-bit character
33 PrimitiveType_Int32, // signed, 32-bit integer
34 PrimitiveType_Float, // 32-bit IEEE-754 float
35 PrimitiveType_Native, // native-sized word
39 class Type : public Object
54 static const int UNKNOWN_SIZE = -1;
55 static const size_t MAX_UNSAFE_ARRAY_DEPTH = 5;
57 static Type *NewPrimitive(Zone *zone, Handle<String> tag);
58 static Type *NewPrimitive(Zone *zone, PrimitiveType type);
59 static Type *NewArray(Zone *zone, Handle<Type> contained, int elements, bool isConst);
60 static Type *NewReference(Zone *zone, Handle<Type> contained);
61 static Type *NewFunction(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters);
63 static Type *cast(Object *obj) {
64 assert(!obj || obj->is(MapKind_Type));
69 return kind() == PRIMITIVE;
72 return kind() == ARRAY;
75 return kind() == REFERENCE;
78 return kind() == FUNCTION;
80 PrimitiveType primitive() {
81 assert(isPrimitive());
85 assert(isArray() || isReference());
86 return Type::cast(contained_);
97 assert(isPrimitive());
102 Type *temp = contained();
103 while (temp->isArray())
104 temp = temp->contained();
108 return isPrimitive() && primitive() == PrimitiveType_Float;
111 return isPrimitive() && primitive() == PrimitiveType_Int32;
115 contained()->isPrimitive() &&
116 contained()->primitive() == PrimitiveType_Char;
123 assert(isFunction());
126 FixedArray *parameters() {
127 assert(isFunction());
130 Type *parameterAt(size_t i) {
131 return Type::cast(parameters()->at(i));
134 return !isPrimitive();
138 static inline size_t offsetOfContained() {
139 return offsetof(Type, contained_);
141 static inline size_t offsetOfReturnType() {
142 return offsetof(Type, returnType_);
144 static inline size_t offsetOfParameters() {
145 return offsetof(Type, parameters_);
147 static inline size_t offsetOfTag() {
148 return offsetof(Type, tag_);
150 // End GC Descriptor.
154 PrimitiveType primitive_;
155 Barriered<String> tag_;
162 // Arrays and references only.
163 Barriered<Type> contained_;
166 Barriered<Type> returnType_;
167 Barriered<FixedArray> parameters_;
178 static BoxedPrimitive Int(int i) {
180 b.type_ = PrimitiveType_Int32;
185 PrimitiveType type() const {
189 assert(type() == PrimitiveType_Int32);
196 #endif // _include_jc_types_h_