Added global variable support.
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_ke_environment_h_
19 #define _include_ke_environment_h_
24 #include "FixedArray.h"
33 // An environment represents the lexical environment of a code or function
34 // object. It used for variables which may be accessed outside a function's
35 // normal activation (i.e., upvars).
37 // A module uses an Environment to store its global variables.
38 class Environment : public Object
41 inline T *pointerToSlot(unsigned slot);
44 static Environment *New(Zone *zone, Handle<EnvironmentMap> map, Heap::Tenure tenure = Heap::Tenure_Default);
46 inline EnvironmentMap *map();
49 float getFloat(unsigned slot) {
50 return *pointerToSlot<float>(slot);
52 int getInt32(unsigned slot) {
53 return *pointerToSlot<int>(slot);
55 Object *getObject(unsigned slot) {
56 return *pointerToSlot<Object *>(slot);
58 void setFloat(unsigned slot, float f) {
59 *pointerToSlot<float>(slot) = f;
61 void setInt32(unsigned slot, int i) {
62 *pointerToSlot<int>(slot) = i;
64 void setObject(unsigned slot, Object *obj) {
65 // :TODO: write barrier
66 *pointerToSlot<Object *>(slot) = obj;
69 static inline size_t offsetOfSlotOffset(unsigned offset) {
70 return sizeof(Environment) + offset;
73 static inline Environment *cast(Object *obj) {
74 assert(!obj || obj->is(MapKind_Environment));
75 return reinterpret_cast<Environment *>(obj);
79 struct EnvironmentSlotDescriptor
85 class EnvironmentSlotDescriptorArray : public Object
90 static EnvironmentSlotDescriptorArray *New(Zone *zone, unsigned length);
92 EnvironmentSlotDescriptor *slots() {
93 return reinterpret_cast<EnvironmentSlotDescriptor *>(this + 1);
95 EnvironmentSlotDescriptor &at(unsigned slot) {
96 assert(slot < length_);
103 return Align(sizeof(*this) + length() * sizeof(EnvironmentSlotDescriptorArray),
107 static inline EnvironmentSlotDescriptorArray *cast(Object *obj) {
108 assert(!obj || obj->is(MapKind_EnvironmentSlotDescriptorArray));
109 return reinterpret_cast<EnvironmentSlotDescriptorArray *>(obj);
113 class EnvironmentMap : public Map
115 unsigned structureSize_;
116 unsigned traceOffsetCount_;
117 Barriered<EnvironmentSlotDescriptorArray> slots_;
120 static EnvironmentMap *New(Zone *zone, unsigned nvars, Variable **vars);
122 unsigned structureSize() {
123 return structureSize_;
125 unsigned traceOffsetCount() {
126 return traceOffsetCount_;
128 const unsigned *traceOffsets() {
129 return reinterpret_cast<unsigned *>(this + 1);
132 return Align(sizeof(EnvironmentMap) + sizeof(unsigned) * traceOffsetCount(), kObjectAlignment);
134 EnvironmentSlotDescriptor &slotDescriptor(unsigned slot) {
135 return slots_->at(slot);
138 static inline size_t offsetOfSlots() {
139 return offsetof(EnvironmentMap, slots_);
142 static inline EnvironmentMap *cast(Object *obj) {
143 assert(!obj || obj->is(MapKind_MetaEnvironment));
144 return reinterpret_cast<EnvironmentMap *>(obj);
151 return Align(sizeof(Environment) + map()->structureSize(), kObjectAlignment);
157 return EnvironmentMap::cast(Object::map());
160 template <typename T> T *
161 Environment::pointerToSlot(unsigned slot)
163 return reinterpret_cast<T *>(reinterpret_cast<char *>(this + 1) + map()->slotDescriptor(slot).offset);
168 #endif // _include_ke_environment_h_