Added support for forwards.
2 * Copyright (C) 2012 David Anderson
4 * This file is part of JITCraft.
6 * JITCraft is free software: you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free
8 * Software Foundation, either version 3 of the License, or (at your option)
11 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along with
16 * JITCraft. If not, see http://www.gnu.org/licenses/.
18 #ifndef _include_jitcraft_array_h_
19 #define _include_jitcraft_array_h_
29 class Array : public Object
31 friend class ArrayMap;
37 static Array *New(Zone *zone, Handle<ArrayMap> map, uint32 elements,
38 Heap::Tenure = Heap::Tenure_Default);
47 inline int *int32Vec();
48 inline float *floatVec();
49 inline Object **objVec();
53 inline size_t elementSize();
55 int readInt32(size_t index) {
56 assert(index < nelements());
57 return int32Vec()[index];
59 void writeInt32(size_t index, int i) {
60 assert(index < nelements());
61 int32Vec()[index] = i;
63 char readChar(size_t index) {
64 assert(index < nelements());
65 return chars()[index];
67 void writeChar(size_t index, char c) {
68 assert(index < nelements());
71 float readFloat(size_t index) {
72 assert(index < nelements());
73 return floatVec()[index];
75 void writeFloat(size_t index, float f) {
76 assert(index < nelements());
77 floatVec()[index] = f;
79 inline void *slotPtr(size_t index);
80 Object *readObject(size_t index) {
81 assert(index < nelements());
82 return objVec()[index];
84 void writeObject(Zone *zone, size_t index, Object *ptr) {
85 assert(index < nelements());
86 write(zone, &objVec()[index], ptr);
89 static inline size_t SizeOf(Array *array) {
90 return sizeof(Array) + array->nelements() * array->elementSize();
93 static Array *cast(Object *obj) {
94 assert(!obj || obj->is(MapKind_Array));
95 return reinterpret_cast<Array *>(obj);
99 class ArrayMap : public Map
101 Barriered<Type> type_;
102 Barriered<ArrayMap> contained_;
105 static ArrayMap *New(Zone *zone, Handle<Type> type, Handle<ArrayMap> contained);
110 ArrayMap *contained() {
115 static inline size_t offsetOfType() {
116 return offsetof(ArrayMap, type_);
118 static inline size_t offsetOfContained() {
119 return offsetof(ArrayMap, contained_);
121 // End GC Descriptor.
123 static ArrayMap *cast(Object *obj) {
124 assert(!obj || obj->is(MapKind_MetaArray));
125 return reinterpret_cast<ArrayMap *>(obj);
132 assert(type()->levels() == 1);
133 assert(type()->contained()->isInt32());
134 return reinterpret_cast<int *>(vec());
140 assert(type()->levels() == 1);
141 assert(type()->contained()->isFloat());
142 return reinterpret_cast<float *>(vec());
148 assert(type()->levels() > 1);
149 return reinterpret_cast<Object **>(vec());
153 Array::slotPtr(size_t index)
155 assert(type()->contained()->primitive() == PrimitiveType_Int32 ||
156 type()->contained()->primitive() == PrimitiveType_Float);
157 assert(index < nelements());
158 return reinterpret_cast<int *>(vec()) + index;
164 assert(type()->levels() == 1);
165 assert(type()->contained()->primitive() == PrimitiveType_Char);
166 return reinterpret_cast<char *>(vec());
172 return ArrayMap::cast(map())->type();
178 if (type()->isArray())
179 return sizeof(Object *);
180 switch (type()->contained()->primitive()) {
181 case PrimitiveType_Float:
182 return sizeof(float);
183 case PrimitiveType_Int32:
186 assert(type()->contained()->primitive() == PrimitiveType_Char);
193 #endif // _include_jitcraft_array_h_