Add support for array reference semantics.
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);
39 static Array *NewString(Zone *zone, Handle<ArrayMap> map, Address buffer, size_t length, Heap::Tenure = Heap::Tenure_Default);
40 static Array *NewExternal(Zone *zone, Handle<ArrayMap> map, uint32 nelements,
41 void *elements, Heap::Tenure = Heap::Tenure_Default);
50 inline int *int32Vec();
51 inline float *floatVec();
52 inline Object **objVec();
56 inline size_t elementSize();
58 int readInt32(size_t index) {
59 assert(index < length());
60 return int32Vec()[index];
62 void writeInt32(size_t index, int i) {
63 assert(index < length());
64 int32Vec()[index] = i;
66 char readChar(size_t index) {
67 assert(index < length());
68 return chars()[index];
70 void writeChar(size_t index, char c) {
71 assert(index < length());
74 float readFloat(size_t index) {
75 assert(index < length());
76 return floatVec()[index];
78 void writeFloat(size_t index, float f) {
79 assert(index < length());
80 floatVec()[index] = f;
82 inline void *slotPtr(size_t index);
83 Object *readObject(size_t index) {
84 assert(index < length());
85 return objVec()[index];
87 void writeObject(Zone *zone, size_t index, Object *ptr) {
88 assert(index < length());
89 write(zone, &objVec()[index], ptr);
93 // Inline arrays always have their elements pointing at the next slot.
94 Address buffer = reinterpret_cast<Address>(elements_);
95 return buffer != reinterpret_cast<Address>(this + 1);
98 static inline size_t SizeOf(Array *array) {
99 if (array->isExternal())
100 return sizeof(Array);
101 return sizeof(Array) + array->length() * array->elementSize();
104 static Array *cast(Object *obj) {
105 assert(!obj || obj->is(MapKind_Array));
106 return reinterpret_cast<Array *>(obj);
110 class ArrayMap : public Map
112 Barriered<Type> type_;
115 static ArrayMap *New(Zone *zone, Handle<Type> type);
116 static ArrayMap *Attach(Zone *zone, Handle<Type> type);
123 static inline size_t offsetOfType() {
124 return OFFSETOF(ArrayMap, type_);
126 // End GC Descriptor.
128 static ArrayMap *cast(Object *obj) {
129 assert(!obj || obj->is(MapKind_MetaArray));
130 return reinterpret_cast<ArrayMap *>(obj);
137 assert(type()->levels() == 1);
138 assert(type()->contained()->isInt32());
139 return reinterpret_cast<int *>(vec());
145 assert(type()->levels() == 1);
146 assert(type()->contained()->isFloat());
147 return reinterpret_cast<float *>(vec());
153 assert(type()->contained()->isTraceable());
154 return reinterpret_cast<Object **>(vec());
158 Array::slotPtr(size_t index)
160 assert(type()->contained()->primitive() == PrimitiveType_Int32 ||
161 type()->contained()->primitive() == PrimitiveType_Float);
162 assert(index < length());
163 return reinterpret_cast<int *>(vec()) + index;
169 assert(type()->levels() == 1);
170 assert(type()->contained()->primitive() == PrimitiveType_Char);
171 return reinterpret_cast<char *>(vec());
177 return ArrayMap::cast(map())->type();
183 assert(type()->isArray());
184 if (type()->contained()->isArray())
185 return sizeof(Object *);
186 switch (type()->contained()->primitive()) {
187 case PrimitiveType_Float:
188 return sizeof(float);
189 case PrimitiveType_Int32:
192 assert(type()->contained()->primitive() == PrimitiveType_Char);
199 #endif // _include_jitcraft_array_h_