src/Environments.h
author David Anderson <dvander@alliedmods.net>
Tue Jul 24 23:53:34 2012 -0700 (2012-07-24)
changeset 86 32ad0523b309
parent 85 jitcraft/Environments.h@06a0015b6a5c
child 91 5f4402180204
permissions -rw-r--r--
Renamed jitcraft/ to src/, fixed some /Wp64 warnings.
     1 /*
     2  * Copyright (C) 2012 David Anderson
     3  *
     4  * This file is part of JITCraft.
     5  *
     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)
     9  * any later version.
    10  * 
    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.
    14  *
    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/.
    17  */
    18 #ifndef _include_ke_environment_h_
    19 #define _include_ke_environment_h_
    20 
    21 #include "Objects.h"
    22 #include "Handles.h"
    23 #include "Heap.h"
    24 #include "FixedArray.h"
    25 
    26 namespace ke {
    27 
    28 class EnvironmentMap;
    29 class Variable;
    30 class ByteArray;
    31 class Type;
    32 
    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).
    36 //
    37 // A module uses an Environment to store its global variables.
    38 class Environment : public Object
    39 {
    40     template <typename T>
    41     inline T *pointerToSlot(unsigned slot);
    42 
    43   public:
    44     static Environment *New(Zone *zone, Handle<EnvironmentMap> map, Heap::Tenure tenure = Heap::Tenure_Default);
    45 
    46     inline EnvironmentMap *map();
    47     inline size_t size();
    48 
    49     float getFloat(unsigned slot) {
    50         return *pointerToSlot<float>(slot);
    51     }
    52     int getInt32(unsigned slot) {
    53         return *pointerToSlot<int>(slot);
    54     }
    55     Object *getObject(unsigned slot) {
    56         return *pointerToSlot<Object *>(slot);
    57     }
    58     void setFloat(unsigned slot, float f) {
    59         *pointerToSlot<float>(slot) = f;
    60     }
    61     void setInt32(unsigned slot, int i) {
    62         *pointerToSlot<int>(slot) = i;
    63     }
    64     void setObject(unsigned slot, Object *obj) {
    65         // :TODO: write barrier
    66         *pointerToSlot<Object *>(slot) = obj;
    67     }
    68 
    69     static inline size_t offsetOfSlotOffset(unsigned offset) {
    70         return sizeof(Environment) + offset;
    71     }
    72 
    73     static inline Environment *cast(Object *obj) {
    74         assert(!obj || obj->is(MapKind_Environment));
    75         return reinterpret_cast<Environment *>(obj);
    76     }
    77 };
    78 
    79 struct EnvironmentSlotDescriptor
    80 {
    81     Barriered<Type> type;
    82     unsigned offset;
    83 };
    84 
    85 class EnvironmentSlotDescriptorArray : public Object
    86 {
    87     unsigned length_;
    88 
    89   public:
    90     static EnvironmentSlotDescriptorArray *New(Zone *zone, unsigned length);
    91 
    92     EnvironmentSlotDescriptor *slots() {
    93         return reinterpret_cast<EnvironmentSlotDescriptor *>(this + 1);
    94     }
    95     EnvironmentSlotDescriptor &at(unsigned slot) {
    96         assert(slot < length_);
    97         return slots()[slot];
    98     }
    99     unsigned length() {
   100         return length_;
   101     }
   102     unsigned size() {
   103         return Align(sizeof(*this) + length() * sizeof(EnvironmentSlotDescriptorArray),
   104                      kObjectAlignment);
   105     }
   106 
   107     static inline EnvironmentSlotDescriptorArray *cast(Object *obj) {
   108         assert(!obj || obj->is(MapKind_EnvironmentSlotDescriptorArray));
   109         return reinterpret_cast<EnvironmentSlotDescriptorArray *>(obj);
   110     }
   111 };
   112 
   113 class EnvironmentMap : public Map
   114 {
   115     unsigned structureSize_;
   116     unsigned traceOffsetCount_;
   117     Barriered<EnvironmentSlotDescriptorArray> slots_;
   118 
   119   public:
   120     static EnvironmentMap *New(Zone *zone, unsigned nvars, Variable **vars);
   121 
   122     unsigned structureSize() {
   123         return structureSize_;
   124     }
   125     unsigned traceOffsetCount() {
   126         return traceOffsetCount_;
   127     }
   128     const unsigned *traceOffsets() {
   129         return reinterpret_cast<unsigned *>(this + 1);
   130     }
   131     size_t size() {
   132         return Align(sizeof(EnvironmentMap) + sizeof(unsigned) * traceOffsetCount(), kObjectAlignment);
   133     }
   134     EnvironmentSlotDescriptor &slotDescriptor(unsigned slot) {
   135         return slots_->at(slot);
   136     }
   137 
   138     static inline size_t offsetOfSlots() {
   139         return offsetof(EnvironmentMap, slots_);
   140     }
   141 
   142     static inline EnvironmentMap *cast(Object *obj) {
   143         assert(!obj || obj->is(MapKind_MetaEnvironment));
   144         return reinterpret_cast<EnvironmentMap *>(obj);
   145     }
   146 };
   147 
   148 size_t
   149 Environment::size()
   150 {
   151     return Align(sizeof(Environment) + map()->structureSize(), kObjectAlignment);
   152 }
   153 
   154 EnvironmentMap *
   155 Environment::map()
   156 {
   157     return EnvironmentMap::cast(Object::map());
   158 }
   159 
   160 template <typename T> T *
   161 Environment::pointerToSlot(unsigned slot)
   162 {
   163     return reinterpret_cast<T *>(reinterpret_cast<char *>(this + 1) + map()->slotDescriptor(slot).offset);
   164 }
   165 
   166 }
   167 
   168 #endif // _include_ke_environment_h_