Handle-ize Array.
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 Array *cast(Object *obj) {
90 assert(!obj || obj->is(MapKind_Array));
91 return reinterpret_cast<Array *>(obj);
95 class ArrayMap : public Map
97 Barriered<Type> type_;
98 Barriered<ArrayMap> contained_;
101 static ArrayMap *New(Zone *zone, Handle<Type> type, Handle<ArrayMap> contained);
106 ArrayMap *contained() {
110 static ArrayMap *cast(Object *obj) {
111 assert(!obj || obj->is(MapKind_MetaArray));
112 return reinterpret_cast<ArrayMap *>(obj);
119 assert(type()->levels() == 1);
120 assert(type()->contained()->isInt32());
121 return reinterpret_cast<int *>(vec());
127 assert(type()->levels() == 1);
128 assert(type()->contained()->isFloat());
129 return reinterpret_cast<float *>(vec());
135 assert(type()->levels() > 1);
136 return reinterpret_cast<Object **>(vec());
140 Array::slotPtr(size_t index)
142 assert(type()->contained()->primitive() == PrimitiveType_Int32 ||
143 type()->contained()->primitive() == PrimitiveType_Float);
144 assert(index < nelements());
145 return reinterpret_cast<int *>(vec()) + index;
151 assert(type()->levels() == 1);
152 assert(type()->contained()->primitive() == PrimitiveType_Char);
153 return reinterpret_cast<char *>(vec());
159 return ArrayMap::cast(map())->type();
165 if (type()->isArray())
166 return sizeof(Object *);
167 switch (type()->contained()->primitive()) {
168 case PrimitiveType_Float:
169 return sizeof(float);
170 case PrimitiveType_Int32:
173 assert(type()->contained()->primitive() == PrimitiveType_Char);
180 #endif // _include_jitcraft_array_h_