1.1 --- a/src/Environments.h Mon Jun 10 00:46:29 2013 +0100
1.2 +++ b/src/Environments.h Mon Jun 10 01:04:43 2013 +0100
1.3 @@ -1,169 +1,169 @@
1.4 -/*
1.5 - * Copyright (C) 2012 David Anderson
1.6 - *
1.7 - * This file is part of JITCraft.
1.8 - *
1.9 - * JITCraft is free software: you can redistribute it and/or modify it under
1.10 - * the terms of the GNU General Public License as published by the Free
1.11 - * Software Foundation, either version 3 of the License, or (at your option)
1.12 - * any later version.
1.13 - *
1.14 - * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
1.15 - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.16 - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
1.17 - *
1.18 - * You should have received a copy of the GNU General Public License along with
1.19 - * JITCraft. If not, see http://www.gnu.org/licenses/.
1.20 - */
1.21 -#ifndef _include_ke_environment_h_
1.22 -#define _include_ke_environment_h_
1.23 -
1.24 -#include "Objects.h"
1.25 -#include "Handles.h"
1.26 -#include "Heap.h"
1.27 -#include "FixedArray.h"
1.28 -
1.29 -namespace ke {
1.30 -
1.31 -class EnvironmentMap;
1.32 -class Variable;
1.33 -class ByteArray;
1.34 -class Type;
1.35 -
1.36 -// An environment represents the lexical environment of a code or function
1.37 -// object. It used for variables which may be accessed outside a function's
1.38 -// normal activation (i.e., upvars).
1.39 -//
1.40 -// A module uses an Environment to store its global variables.
1.41 -class Environment : public Object
1.42 -{
1.43 - template <typename T>
1.44 - inline T *pointerToSlot(unsigned slot);
1.45 -
1.46 - public:
1.47 - static Environment *New(Zone *zone, Handle<EnvironmentMap> map, Heap::Tenure tenure = Heap::Tenure_Default);
1.48 -
1.49 - inline EnvironmentMap *map();
1.50 - inline size_t objSize();
1.51 -
1.52 - float getFloat(unsigned slot) {
1.53 - return *pointerToSlot<float>(slot);
1.54 - }
1.55 - int getInt32(unsigned slot) {
1.56 - return *pointerToSlot<int>(slot);
1.57 - }
1.58 - Object *getObject(unsigned slot) {
1.59 - return *pointerToSlot<Object *>(slot);
1.60 - }
1.61 - void setFloat(unsigned slot, float f) {
1.62 - *pointerToSlot<float>(slot) = f;
1.63 - }
1.64 - void setInt32(unsigned slot, int i) {
1.65 - *pointerToSlot<int>(slot) = i;
1.66 - }
1.67 - void setObject(unsigned slot, Object *obj) {
1.68 - // :TODO: write barrier
1.69 - *pointerToSlot<Object *>(slot) = obj;
1.70 - }
1.71 -
1.72 - static inline size_t offsetOfSlotOffset(unsigned offset) {
1.73 - return sizeof(Environment) + offset;
1.74 - }
1.75 -
1.76 - static inline Environment *cast(Object *obj) {
1.77 - assert(!obj || obj->is(MapKind_Environment));
1.78 - return reinterpret_cast<Environment *>(obj);
1.79 - }
1.80 -};
1.81 -
1.82 -struct EnvironmentSlotDescriptor
1.83 -{
1.84 - Barriered<Type> type;
1.85 - unsigned offset;
1.86 -};
1.87 -
1.88 -class EnvironmentSlotDescriptorArray : public Object
1.89 -{
1.90 - unsigned length_;
1.91 -
1.92 - public:
1.93 - static EnvironmentSlotDescriptorArray *New(Zone *zone, unsigned length);
1.94 -
1.95 - EnvironmentSlotDescriptor *slots() {
1.96 - return reinterpret_cast<EnvironmentSlotDescriptor *>(this + 1);
1.97 - }
1.98 - EnvironmentSlotDescriptor &at(unsigned slot) {
1.99 - assert(slot < length_);
1.100 - return slots()[slot];
1.101 - }
1.102 - unsigned length() {
1.103 - return length_;
1.104 - }
1.105 - unsigned objSize() {
1.106 - return sizeof(*this) + length() * sizeof(EnvironmentSlotDescriptorArray);
1.107 - }
1.108 -
1.109 - static inline EnvironmentSlotDescriptorArray *cast(Object *obj) {
1.110 - assert(!obj || obj->is(MapKind_EnvironmentSlotDescriptorArray));
1.111 - return reinterpret_cast<EnvironmentSlotDescriptorArray *>(obj);
1.112 - }
1.113 -};
1.114 -
1.115 -class EnvironmentMap : public Map
1.116 -{
1.117 - unsigned structureSize_;
1.118 - unsigned traceOffsetCount_;
1.119 - Barriered<EnvironmentSlotDescriptorArray> slots_;
1.120 -
1.121 - public:
1.122 - static EnvironmentMap *New(Zone *zone, unsigned nvars, Variable **vars);
1.123 -
1.124 - unsigned structureSize() {
1.125 - return structureSize_;
1.126 - }
1.127 - unsigned traceOffsetCount() {
1.128 - return traceOffsetCount_;
1.129 - }
1.130 - const unsigned *traceOffsets() {
1.131 - return reinterpret_cast<unsigned *>(this + 1);
1.132 - }
1.133 - size_t objSize() {
1.134 - return sizeof(EnvironmentMap) + sizeof(unsigned) * traceOffsetCount();
1.135 - }
1.136 - EnvironmentSlotDescriptor &slotDescriptor(unsigned slot) {
1.137 - return slots_->at(slot);
1.138 - }
1.139 -
1.140 - // Begin GC descriptors.
1.141 - static inline size_t offsetOfSlots() {
1.142 - return OFFSETOF(EnvironmentMap, slots_);
1.143 - }
1.144 - // End GC descriptors.
1.145 -
1.146 - static inline EnvironmentMap *cast(Object *obj) {
1.147 - assert(!obj || obj->is(MapKind_MetaEnvironment));
1.148 - return reinterpret_cast<EnvironmentMap *>(obj);
1.149 - }
1.150 -};
1.151 -
1.152 -size_t
1.153 -Environment::objSize()
1.154 -{
1.155 - return sizeof(Environment) + map()->structureSize();
1.156 -}
1.157 -
1.158 -EnvironmentMap *
1.159 -Environment::map()
1.160 -{
1.161 - return EnvironmentMap::cast(Object::map());
1.162 -}
1.163 -
1.164 -template <typename T> T *
1.165 -Environment::pointerToSlot(unsigned slot)
1.166 -{
1.167 - return reinterpret_cast<T *>(reinterpret_cast<char *>(this + 1) + map()->slotDescriptor(slot).offset);
1.168 -}
1.169 -
1.170 -}
1.171 -
1.172 -#endif // _include_ke_environment_h_
1.173 +/*
1.174 + * Copyright (C) 2012 David Anderson
1.175 + *
1.176 + * This file is part of JITCraft.
1.177 + *
1.178 + * JITCraft is free software: you can redistribute it and/or modify it under
1.179 + * the terms of the GNU General Public License as published by the Free
1.180 + * Software Foundation, either version 3 of the License, or (at your option)
1.181 + * any later version.
1.182 + *
1.183 + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
1.184 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.185 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
1.186 + *
1.187 + * You should have received a copy of the GNU General Public License along with
1.188 + * JITCraft. If not, see http://www.gnu.org/licenses/.
1.189 + */
1.190 +#ifndef _include_ke_environment_h_
1.191 +#define _include_ke_environment_h_
1.192 +
1.193 +#include "Objects.h"
1.194 +#include "Handles.h"
1.195 +#include "Heap.h"
1.196 +#include "FixedArray.h"
1.197 +
1.198 +namespace ke {
1.199 +
1.200 +class EnvironmentMap;
1.201 +class Variable;
1.202 +class ByteArray;
1.203 +class Type;
1.204 +
1.205 +// An environment represents the lexical environment of a code or function
1.206 +// object. It used for variables which may be accessed outside a function's
1.207 +// normal activation (i.e., upvars).
1.208 +//
1.209 +// A module uses an Environment to store its global variables.
1.210 +class Environment : public Object
1.211 +{
1.212 + template <typename T>
1.213 + inline T *pointerToSlot(unsigned slot);
1.214 +
1.215 + public:
1.216 + static Environment *New(Zone *zone, Handle<EnvironmentMap> map, Heap::Tenure tenure = Heap::Tenure_Default);
1.217 +
1.218 + inline EnvironmentMap *map();
1.219 + inline size_t objSize();
1.220 +
1.221 + float getFloat(unsigned slot) {
1.222 + return *pointerToSlot<float>(slot);
1.223 + }
1.224 + int getInt32(unsigned slot) {
1.225 + return *pointerToSlot<int>(slot);
1.226 + }
1.227 + Object *getObject(unsigned slot) {
1.228 + return *pointerToSlot<Object *>(slot);
1.229 + }
1.230 + void setFloat(unsigned slot, float f) {
1.231 + *pointerToSlot<float>(slot) = f;
1.232 + }
1.233 + void setInt32(unsigned slot, int i) {
1.234 + *pointerToSlot<int>(slot) = i;
1.235 + }
1.236 + void setObject(unsigned slot, Object *obj) {
1.237 + // :TODO: write barrier
1.238 + *pointerToSlot<Object *>(slot) = obj;
1.239 + }
1.240 +
1.241 + static inline size_t offsetOfSlotOffset(unsigned offset) {
1.242 + return sizeof(Environment) + offset;
1.243 + }
1.244 +
1.245 + static inline Environment *cast(Object *obj) {
1.246 + assert(!obj || obj->is(MapKind_Environment));
1.247 + return reinterpret_cast<Environment *>(obj);
1.248 + }
1.249 +};
1.250 +
1.251 +struct EnvironmentSlotDescriptor
1.252 +{
1.253 + Barriered<Type> type;
1.254 + unsigned offset;
1.255 +};
1.256 +
1.257 +class EnvironmentSlotDescriptorArray : public Object
1.258 +{
1.259 + unsigned length_;
1.260 +
1.261 + public:
1.262 + static EnvironmentSlotDescriptorArray *New(Zone *zone, unsigned length);
1.263 +
1.264 + EnvironmentSlotDescriptor *slots() {
1.265 + return reinterpret_cast<EnvironmentSlotDescriptor *>(this + 1);
1.266 + }
1.267 + EnvironmentSlotDescriptor &at(unsigned slot) {
1.268 + assert(slot < length_);
1.269 + return slots()[slot];
1.270 + }
1.271 + unsigned length() {
1.272 + return length_;
1.273 + }
1.274 + unsigned objSize() {
1.275 + return sizeof(*this) + length() * sizeof(EnvironmentSlotDescriptorArray);
1.276 + }
1.277 +
1.278 + static inline EnvironmentSlotDescriptorArray *cast(Object *obj) {
1.279 + assert(!obj || obj->is(MapKind_EnvironmentSlotDescriptorArray));
1.280 + return reinterpret_cast<EnvironmentSlotDescriptorArray *>(obj);
1.281 + }
1.282 +};
1.283 +
1.284 +class EnvironmentMap : public Map
1.285 +{
1.286 + unsigned structureSize_;
1.287 + unsigned traceOffsetCount_;
1.288 + Barriered<EnvironmentSlotDescriptorArray> slots_;
1.289 +
1.290 + public:
1.291 + static EnvironmentMap *New(Zone *zone, unsigned nvars, Variable **vars);
1.292 +
1.293 + unsigned structureSize() {
1.294 + return structureSize_;
1.295 + }
1.296 + unsigned traceOffsetCount() {
1.297 + return traceOffsetCount_;
1.298 + }
1.299 + const unsigned *traceOffsets() {
1.300 + return reinterpret_cast<unsigned *>(this + 1);
1.301 + }
1.302 + size_t objSize() {
1.303 + return sizeof(EnvironmentMap) + sizeof(unsigned) * traceOffsetCount();
1.304 + }
1.305 + EnvironmentSlotDescriptor &slotDescriptor(unsigned slot) {
1.306 + return slots_->at(slot);
1.307 + }
1.308 +
1.309 + // Begin GC descriptors.
1.310 + static inline size_t offsetOfSlots() {
1.311 + return OFFSETOF(EnvironmentMap, slots_);
1.312 + }
1.313 + // End GC descriptors.
1.314 +
1.315 + static inline EnvironmentMap *cast(Object *obj) {
1.316 + assert(!obj || obj->is(MapKind_MetaEnvironment));
1.317 + return reinterpret_cast<EnvironmentMap *>(obj);
1.318 + }
1.319 +};
1.320 +
1.321 +size_t
1.322 +Environment::objSize()
1.323 +{
1.324 + return sizeof(Environment) + map()->structureSize();
1.325 +}
1.326 +
1.327 +EnvironmentMap *
1.328 +Environment::map()
1.329 +{
1.330 + return EnvironmentMap::cast(Object::map());
1.331 +}
1.332 +
1.333 +template <typename T> T *
1.334 +Environment::pointerToSlot(unsigned slot)
1.335 +{
1.336 + return reinterpret_cast<T *>(reinterpret_cast<char *>(this + 1) + map()->slotDescriptor(slot).offset);
1.337 +}
1.338 +
1.339 +}
1.340 +
1.341 +#endif // _include_ke_environment_h_