diff -r c4b1341297e5 -r ba85a47ee414 src/Environments.h --- a/src/Environments.h Mon Jun 10 00:46:29 2013 +0100 +++ b/src/Environments.h Mon Jun 10 01:04:43 2013 +0100 @@ -1,169 +1,169 @@ -/* - * Copyright (C) 2012 David Anderson - * - * This file is part of JITCraft. - * - * JITCraft is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free - * Software Foundation, either version 3 of the License, or (at your option) - * any later version. - * - * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with - * JITCraft. If not, see http://www.gnu.org/licenses/. - */ -#ifndef _include_ke_environment_h_ -#define _include_ke_environment_h_ - -#include "Objects.h" -#include "Handles.h" -#include "Heap.h" -#include "FixedArray.h" - -namespace ke { - -class EnvironmentMap; -class Variable; -class ByteArray; -class Type; - -// An environment represents the lexical environment of a code or function -// object. It used for variables which may be accessed outside a function's -// normal activation (i.e., upvars). -// -// A module uses an Environment to store its global variables. -class Environment : public Object -{ - template - inline T *pointerToSlot(unsigned slot); - - public: - static Environment *New(Zone *zone, Handle map, Heap::Tenure tenure = Heap::Tenure_Default); - - inline EnvironmentMap *map(); - inline size_t objSize(); - - float getFloat(unsigned slot) { - return *pointerToSlot(slot); - } - int getInt32(unsigned slot) { - return *pointerToSlot(slot); - } - Object *getObject(unsigned slot) { - return *pointerToSlot(slot); - } - void setFloat(unsigned slot, float f) { - *pointerToSlot(slot) = f; - } - void setInt32(unsigned slot, int i) { - *pointerToSlot(slot) = i; - } - void setObject(unsigned slot, Object *obj) { - // :TODO: write barrier - *pointerToSlot(slot) = obj; - } - - static inline size_t offsetOfSlotOffset(unsigned offset) { - return sizeof(Environment) + offset; - } - - static inline Environment *cast(Object *obj) { - assert(!obj || obj->is(MapKind_Environment)); - return reinterpret_cast(obj); - } -}; - -struct EnvironmentSlotDescriptor -{ - Barriered type; - unsigned offset; -}; - -class EnvironmentSlotDescriptorArray : public Object -{ - unsigned length_; - - public: - static EnvironmentSlotDescriptorArray *New(Zone *zone, unsigned length); - - EnvironmentSlotDescriptor *slots() { - return reinterpret_cast(this + 1); - } - EnvironmentSlotDescriptor &at(unsigned slot) { - assert(slot < length_); - return slots()[slot]; - } - unsigned length() { - return length_; - } - unsigned objSize() { - return sizeof(*this) + length() * sizeof(EnvironmentSlotDescriptorArray); - } - - static inline EnvironmentSlotDescriptorArray *cast(Object *obj) { - assert(!obj || obj->is(MapKind_EnvironmentSlotDescriptorArray)); - return reinterpret_cast(obj); - } -}; - -class EnvironmentMap : public Map -{ - unsigned structureSize_; - unsigned traceOffsetCount_; - Barriered slots_; - - public: - static EnvironmentMap *New(Zone *zone, unsigned nvars, Variable **vars); - - unsigned structureSize() { - return structureSize_; - } - unsigned traceOffsetCount() { - return traceOffsetCount_; - } - const unsigned *traceOffsets() { - return reinterpret_cast(this + 1); - } - size_t objSize() { - return sizeof(EnvironmentMap) + sizeof(unsigned) * traceOffsetCount(); - } - EnvironmentSlotDescriptor &slotDescriptor(unsigned slot) { - return slots_->at(slot); - } - - // Begin GC descriptors. - static inline size_t offsetOfSlots() { - return OFFSETOF(EnvironmentMap, slots_); - } - // End GC descriptors. - - static inline EnvironmentMap *cast(Object *obj) { - assert(!obj || obj->is(MapKind_MetaEnvironment)); - return reinterpret_cast(obj); - } -}; - -size_t -Environment::objSize() -{ - return sizeof(Environment) + map()->structureSize(); -} - -EnvironmentMap * -Environment::map() -{ - return EnvironmentMap::cast(Object::map()); -} - -template T * -Environment::pointerToSlot(unsigned slot) -{ - return reinterpret_cast(reinterpret_cast(this + 1) + map()->slotDescriptor(slot).offset); -} - -} - -#endif // _include_ke_environment_h_ +/* + * Copyright (C) 2012 David Anderson + * + * This file is part of JITCraft. + * + * JITCraft is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * JITCraft. If not, see http://www.gnu.org/licenses/. + */ +#ifndef _include_ke_environment_h_ +#define _include_ke_environment_h_ + +#include "Objects.h" +#include "Handles.h" +#include "Heap.h" +#include "FixedArray.h" + +namespace ke { + +class EnvironmentMap; +class Variable; +class ByteArray; +class Type; + +// An environment represents the lexical environment of a code or function +// object. It used for variables which may be accessed outside a function's +// normal activation (i.e., upvars). +// +// A module uses an Environment to store its global variables. +class Environment : public Object +{ + template + inline T *pointerToSlot(unsigned slot); + + public: + static Environment *New(Zone *zone, Handle map, Heap::Tenure tenure = Heap::Tenure_Default); + + inline EnvironmentMap *map(); + inline size_t objSize(); + + float getFloat(unsigned slot) { + return *pointerToSlot(slot); + } + int getInt32(unsigned slot) { + return *pointerToSlot(slot); + } + Object *getObject(unsigned slot) { + return *pointerToSlot(slot); + } + void setFloat(unsigned slot, float f) { + *pointerToSlot(slot) = f; + } + void setInt32(unsigned slot, int i) { + *pointerToSlot(slot) = i; + } + void setObject(unsigned slot, Object *obj) { + // :TODO: write barrier + *pointerToSlot(slot) = obj; + } + + static inline size_t offsetOfSlotOffset(unsigned offset) { + return sizeof(Environment) + offset; + } + + static inline Environment *cast(Object *obj) { + assert(!obj || obj->is(MapKind_Environment)); + return reinterpret_cast(obj); + } +}; + +struct EnvironmentSlotDescriptor +{ + Barriered type; + unsigned offset; +}; + +class EnvironmentSlotDescriptorArray : public Object +{ + unsigned length_; + + public: + static EnvironmentSlotDescriptorArray *New(Zone *zone, unsigned length); + + EnvironmentSlotDescriptor *slots() { + return reinterpret_cast(this + 1); + } + EnvironmentSlotDescriptor &at(unsigned slot) { + assert(slot < length_); + return slots()[slot]; + } + unsigned length() { + return length_; + } + unsigned objSize() { + return sizeof(*this) + length() * sizeof(EnvironmentSlotDescriptorArray); + } + + static inline EnvironmentSlotDescriptorArray *cast(Object *obj) { + assert(!obj || obj->is(MapKind_EnvironmentSlotDescriptorArray)); + return reinterpret_cast(obj); + } +}; + +class EnvironmentMap : public Map +{ + unsigned structureSize_; + unsigned traceOffsetCount_; + Barriered slots_; + + public: + static EnvironmentMap *New(Zone *zone, unsigned nvars, Variable **vars); + + unsigned structureSize() { + return structureSize_; + } + unsigned traceOffsetCount() { + return traceOffsetCount_; + } + const unsigned *traceOffsets() { + return reinterpret_cast(this + 1); + } + size_t objSize() { + return sizeof(EnvironmentMap) + sizeof(unsigned) * traceOffsetCount(); + } + EnvironmentSlotDescriptor &slotDescriptor(unsigned slot) { + return slots_->at(slot); + } + + // Begin GC descriptors. + static inline size_t offsetOfSlots() { + return OFFSETOF(EnvironmentMap, slots_); + } + // End GC descriptors. + + static inline EnvironmentMap *cast(Object *obj) { + assert(!obj || obj->is(MapKind_MetaEnvironment)); + return reinterpret_cast(obj); + } +}; + +size_t +Environment::objSize() +{ + return sizeof(Environment) + map()->structureSize(); +} + +EnvironmentMap * +Environment::map() +{ + return EnvironmentMap::cast(Object::map()); +} + +template T * +Environment::pointerToSlot(unsigned slot) +{ + return reinterpret_cast(reinterpret_cast(this + 1) + map()->slotDescriptor(slot).offset); +} + +} + +#endif // _include_ke_environment_h_