Refactored Array, Code to be Objects. Removed Object tagging.
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_
28 class Array : public Object
30 friend class ArrayMap;
32 OBJECT_FIELD(uint32, nelements);
33 OBJECT_FIELD(VoidPtr, elements);
36 void init(unsigned nelements);
38 return READ_FIELD(elements);
42 static Array *New(Zone *zone, ArrayMap *map, uint32 elements,
43 Heap::Tenure = Heap::Tenure_Default);
46 return READ_FIELD(nelements);
48 inline int *int32Vec();
49 inline float *floatVec();
50 inline Object **objVec();
52 int readInt32(size_t index) {
53 assert(index < nelements());
54 return int32Vec()[index];
56 void writeInt32(size_t index, int i) {
57 assert(index < nelements());
58 int32Vec()[index] = i;
60 float readFloat(size_t index) {
61 assert(index < nelements());
62 return floatVec()[index];
64 void writeFloat(size_t index, float f) {
65 assert(index < nelements());
66 floatVec()[index] = f;
68 Object *readObject(size_t index) {
69 assert(index < nelements());
70 return objVec()[index];
72 void writeObject(Zone *zone, size_t index, Object *ptr) {
73 assert(index < nelements());
74 write(zone, &objVec()[index], ptr);
77 static Array *cast(Object *obj) {
78 assert(obj->is(MapKind_Array));
79 return reinterpret_cast<Array *>(obj);
83 class ArrayMap : public Map
85 OBJECT_FIELD(Type *, type);
86 OBJECT_FIELD(ArrayMap *, contained);
89 void init(Type *type, ArrayMap *contained) {
90 WRITE_FIELD_OBJ_NO_BARRIER(type, type);
91 WRITE_FIELD_OBJ_NO_BARRIER(contained, contained);
95 static ArrayMap *New(Zone *zone, Type *type, ArrayMap *contained);
98 return Type::cast(READ_FIELD_OBJ(type));
100 ArrayMap *contained() {
101 return ArrayMap::cast(READ_FIELD_OBJ(contained));
104 static ArrayMap *cast(Object *obj) {
105 assert(!obj || obj->is(MapKind_MetaArray));
106 return reinterpret_cast<ArrayMap *>(obj);
113 assert(ArrayMap::cast(map())->type()->levels() == 1);
114 assert(ArrayMap::cast(map())->type()->contained()->isInt());
115 return reinterpret_cast<int *>(vec());
121 assert(ArrayMap::cast(map())->type()->levels() == 1);
122 assert(ArrayMap::cast(map())->type()->contained()->isFloat());
123 return reinterpret_cast<float *>(vec());
129 assert(ArrayMap::cast(map())->type()->levels() > 1);
130 return reinterpret_cast<Object **>(vec());
135 #endif // _include_jitcraft_array_h_