Trigger build.
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();
47 inline size_t objSize();
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 sizeof(*this) + length() * sizeof(EnvironmentSlotDescriptorArray);
106 static inline EnvironmentSlotDescriptorArray *cast(Object *obj) {
107 assert(!obj || obj->is(MapKind_EnvironmentSlotDescriptorArray));
108 return reinterpret_cast<EnvironmentSlotDescriptorArray *>(obj);
112 class EnvironmentMap : public Map
114 unsigned structureSize_;
115 unsigned traceOffsetCount_;
116 Barriered<EnvironmentSlotDescriptorArray> slots_;
119 static EnvironmentMap *New(Zone *zone, unsigned nvars, Variable **vars);
121 unsigned structureSize() {
122 return structureSize_;
124 unsigned traceOffsetCount() {
125 return traceOffsetCount_;
127 const unsigned *traceOffsets() {
128 return reinterpret_cast<unsigned *>(this + 1);
131 return sizeof(EnvironmentMap) + sizeof(unsigned) * traceOffsetCount();
133 EnvironmentSlotDescriptor &slotDescriptor(unsigned slot) {
134 return slots_->at(slot);
137 // Begin GC descriptors.
138 static inline size_t offsetOfSlots() {
139 return OFFSETOF(EnvironmentMap, slots_);
141 // End GC descriptors.
143 static inline EnvironmentMap *cast(Object *obj) {
144 assert(!obj || obj->is(MapKind_MetaEnvironment));
145 return reinterpret_cast<EnvironmentMap *>(obj);
150 Environment::objSize()
152 return sizeof(Environment) + map()->structureSize();
158 return EnvironmentMap::cast(Object::map());
161 template <typename T> T *
162 Environment::pointerToSlot(unsigned slot)
164 return reinterpret_cast<T *>(reinterpret_cast<char *>(this + 1) + map()->slotDescriptor(slot).offset);
169 #endif // _include_ke_environment_h_