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