jitcraft/Environments.cpp
author David Anderson <dvander@alliedmods.net>
Mon Jul 23 01:19:44 2012 -0700 (2012-07-23)
changeset 85 06a0015b6a5c
permissions -rw-r--r--
Added global variable support.
     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 #include "Utility.h"
    19 #include "Environments.h"
    20 #include "Structures.h"
    21 #include "FixedArray.h"
    22 #include "Zone.h"
    23 #include "NameBinding.h"
    24 
    25 using namespace ke;
    26 
    27 EnvironmentSlotDescriptorArray *
    28 EnvironmentSlotDescriptorArray::New(Zone *zone, unsigned nslots)
    29 {
    30     size_t bytesNeeded = sizeof(EnvironmentSlotDescriptorArray) +
    31                          sizeof(EnvironmentSlotDescriptor) * nslots;
    32 
    33     Local<EnvironmentSlotDescriptorArray> array(zone);
    34     Local<Map> map(zone, zone->getMap(MapKind_EnvironmentSlotDescriptorArray));
    35 
    36     array = EnvironmentSlotDescriptorArray::cast(zone->allocate(map, bytesNeeded, Heap::Tenure_Old));
    37     if (!array)
    38         return NULL;
    39 
    40     // We zero the array for safety, since we could GC or fail before initialization.
    41     array->length_ = nslots;
    42     memset(array->slots(), 0, sizeof(EnvironmentSlotDescriptor) * nslots);
    43     return array;
    44 }
    45 
    46 Environment *
    47 Environment::New(Zone *zone, Handle<EnvironmentMap> map, Heap::Tenure tenure)
    48 {
    49     size_t bytesNeeded = sizeof(Environment) + map->structureSize();
    50     Local<Environment> env(zone, Environment::cast(zone->allocate(map, bytesNeeded, tenure)));
    51     if (!env)
    52         return NULL;
    53 
    54     // Initialize when called from C++, to make sure object slots are NULL.
    55     memset((Object *)env + 1, 0, map->structureSize());
    56     return env;
    57 }
    58 
    59 EnvironmentMap *
    60 EnvironmentMap::New(Zone *zone, unsigned nvars, Variable **vars)
    61 {
    62     StructureBufferBuilder structure;
    63 
    64     Local<EnvironmentSlotDescriptorArray> slots(zone);
    65     slots = EnvironmentSlotDescriptorArray::New(zone, nvars);
    66     if (!slots)
    67         return NULL;
    68 
    69     // Build the descriptor table, mapping each slot to an offset and type.
    70     Local<Type> type(zone);
    71     for (unsigned i = 0; i < nvars; i++) {
    72         assert(vars[i]->slot() == i);
    73         type = vars[i]->type();
    74 
    75         EnvironmentSlotDescriptor &desc = slots->slots()[i];
    76         if (!structure.add(type, &desc.offset))
    77             return NULL;
    78 
    79         desc.type = type;
    80     }
    81 
    82     // Allocate the actual map now.
    83     size_t bytesNeeded = sizeof(EnvironmentMap) + structure.traceableOffsetCount() * sizeof(unsigned);
    84     Local<EnvironmentMap> map(zone);
    85     
    86     map = EnvironmentMap::cast(zone->allocateMap(MapKind_Environment, bytesNeeded, MapKind_MetaEnvironment));
    87     if (!map)
    88         return NULL;
    89     
    90     // Fill in the map fields, and copy the traceable offsets structure.
    91     map->structureSize_ = structure.structureSize();
    92     map->traceOffsetCount_ = structure.traceableOffsetCount();
    93     map->slots_ = slots;
    94     memcpy(const_cast<unsigned *>(map->traceOffsets()), structure.offsets(), sizeof(unsigned) * structure.traceableOffsetCount());
    95 
    96     return map;
    97 }