src/Environments.h
author David Anderson <dvander@alliedmods.net>
Sat Sep 15 13:32:01 2012 -0700 (2012-09-15)
changeset 183 05571208642f
parent 161 b3f0aaee735a
child 263 ba85a47ee414
permissions -rw-r--r--
Fix bug in error reporting for array assignment dimension mismatches.
     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 objSize();
    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 objSize() {
   103         return sizeof(*this) + length() * sizeof(EnvironmentSlotDescriptorArray);
   104     }
   105 
   106     static inline EnvironmentSlotDescriptorArray *cast(Object *obj) {
   107         assert(!obj || obj->is(MapKind_EnvironmentSlotDescriptorArray));
   108         return reinterpret_cast<EnvironmentSlotDescriptorArray *>(obj);
   109     }
   110 };
   111 
   112 class EnvironmentMap : public Map
   113 {
   114     unsigned structureSize_;
   115     unsigned traceOffsetCount_;
   116     Barriered<EnvironmentSlotDescriptorArray> slots_;
   117 
   118   public:
   119     static EnvironmentMap *New(Zone *zone, unsigned nvars, Variable **vars);
   120 
   121     unsigned structureSize() {
   122         return structureSize_;
   123     }
   124     unsigned traceOffsetCount() {
   125         return traceOffsetCount_;
   126     }
   127     const unsigned *traceOffsets() {
   128         return reinterpret_cast<unsigned *>(this + 1);
   129     }
   130     size_t objSize() {
   131         return sizeof(EnvironmentMap) + sizeof(unsigned) * traceOffsetCount();
   132     }
   133     EnvironmentSlotDescriptor &slotDescriptor(unsigned slot) {
   134         return slots_->at(slot);
   135     }
   136 
   137     // Begin GC descriptors.
   138     static inline size_t offsetOfSlots() {
   139         return OFFSETOF(EnvironmentMap, slots_);
   140     }
   141     // End GC descriptors.
   142 
   143     static inline EnvironmentMap *cast(Object *obj) {
   144         assert(!obj || obj->is(MapKind_MetaEnvironment));
   145         return reinterpret_cast<EnvironmentMap *>(obj);
   146     }
   147 };
   148 
   149 size_t
   150 Environment::objSize()
   151 {
   152     return sizeof(Environment) + map()->structureSize();
   153 }
   154 
   155 EnvironmentMap *
   156 Environment::map()
   157 {
   158     return EnvironmentMap::cast(Object::map());
   159 }
   160 
   161 template <typename T> T *
   162 Environment::pointerToSlot(unsigned slot)
   163 {
   164     return reinterpret_cast<T *>(reinterpret_cast<char *>(this + 1) + map()->slotDescriptor(slot).offset);
   165 }
   166 
   167 }
   168 
   169 #endif // _include_ke_environment_h_