Added incdec support for fields.
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 *NewExternal(Zone *zone, Handle<ArrayMap> map, uint32 nelements,
40 void *elements, Heap::Tenure = Heap::Tenure_Default);
49 inline int *int32Vec();
50 inline float *floatVec();
51 inline Object **objVec();
55 inline size_t elementSize();
57 int readInt32(size_t index) {
58 assert(index < nelements());
59 return int32Vec()[index];
61 void writeInt32(size_t index, int i) {
62 assert(index < nelements());
63 int32Vec()[index] = i;
65 char readChar(size_t index) {
66 assert(index < nelements());
67 return chars()[index];
69 void writeChar(size_t index, char c) {
70 assert(index < nelements());
73 float readFloat(size_t index) {
74 assert(index < nelements());
75 return floatVec()[index];
77 void writeFloat(size_t index, float f) {
78 assert(index < nelements());
79 floatVec()[index] = f;
81 inline void *slotPtr(size_t index);
82 Object *readObject(size_t index) {
83 assert(index < nelements());
84 return objVec()[index];
86 void writeObject(Zone *zone, size_t index, Object *ptr) {
87 assert(index < nelements());
88 write(zone, &objVec()[index], ptr);
92 // Inline arrays always have their elements pointing at the next slot.
93 Address buffer = reinterpret_cast<Address>(elements_);
94 return buffer != reinterpret_cast<Address>(this + 1);
97 static inline size_t SizeOf(Array *array) {
98 if (array->isExternal())
100 return sizeof(Array) + array->nelements() * array->elementSize();
103 static Array *cast(Object *obj) {
104 assert(!obj || obj->is(MapKind_Array));
105 return reinterpret_cast<Array *>(obj);
109 class ArrayMap : public Map
111 Barriered<Type> type_;
112 Barriered<ArrayMap> contained_;
115 static ArrayMap *New(Zone *zone, Handle<Type> type, Handle<ArrayMap> contained);
120 ArrayMap *contained() {
125 static inline size_t offsetOfType() {
126 return OFFSETOF(ArrayMap, type_);
128 static inline size_t offsetOfContained() {
129 return OFFSETOF(ArrayMap, contained_);
131 // End GC Descriptor.
133 static ArrayMap *cast(Object *obj) {
134 assert(!obj || obj->is(MapKind_MetaArray));
135 return reinterpret_cast<ArrayMap *>(obj);
142 assert(type()->levels() == 1);
143 assert(type()->contained()->isInt32());
144 return reinterpret_cast<int *>(vec());
150 assert(type()->levels() == 1);
151 assert(type()->contained()->isFloat());
152 return reinterpret_cast<float *>(vec());
158 assert(type()->levels() > 1);
159 return reinterpret_cast<Object **>(vec());
163 Array::slotPtr(size_t index)
165 assert(type()->contained()->primitive() == PrimitiveType_Int32 ||
166 type()->contained()->primitive() == PrimitiveType_Float);
167 assert(index < nelements());
168 return reinterpret_cast<int *>(vec()) + index;
174 assert(type()->levels() == 1);
175 assert(type()->contained()->primitive() == PrimitiveType_Char);
176 return reinterpret_cast<char *>(vec());
182 return ArrayMap::cast(map())->type();
188 assert(type()->isArray());
189 if (type()->contained()->isArray())
190 return sizeof(Object *);
191 switch (type()->contained()->primitive()) {
192 case PrimitiveType_Float:
193 return sizeof(float);
194 case PrimitiveType_Int32:
197 assert(type()->contained()->primitive() == PrimitiveType_Char);
204 #endif // _include_jitcraft_array_h_