1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/jitcraft/Environments.cpp Mon Jul 23 01:19:44 2012 -0700
1.3 @@ -0,0 +1,97 @@
1.4 +/*
1.5 + * Copyright (C) 2012 David Anderson
1.6 + *
1.7 + * This file is part of JITCraft.
1.8 + *
1.9 + * JITCraft is free software: you can redistribute it and/or modify it under
1.10 + * the terms of the GNU General Public License as published by the Free
1.11 + * Software Foundation, either version 3 of the License, or (at your option)
1.12 + * any later version.
1.13 + *
1.14 + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
1.15 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.16 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
1.17 + *
1.18 + * You should have received a copy of the GNU General Public License along with
1.19 + * JITCraft. If not, see http://www.gnu.org/licenses/.
1.20 + */
1.21 +#include "Utility.h"
1.22 +#include "Environments.h"
1.23 +#include "Structures.h"
1.24 +#include "FixedArray.h"
1.25 +#include "Zone.h"
1.26 +#include "NameBinding.h"
1.27 +
1.28 +using namespace ke;
1.29 +
1.30 +EnvironmentSlotDescriptorArray *
1.31 +EnvironmentSlotDescriptorArray::New(Zone *zone, unsigned nslots)
1.32 +{
1.33 + size_t bytesNeeded = sizeof(EnvironmentSlotDescriptorArray) +
1.34 + sizeof(EnvironmentSlotDescriptor) * nslots;
1.35 +
1.36 + Local<EnvironmentSlotDescriptorArray> array(zone);
1.37 + Local<Map> map(zone, zone->getMap(MapKind_EnvironmentSlotDescriptorArray));
1.38 +
1.39 + array = EnvironmentSlotDescriptorArray::cast(zone->allocate(map, bytesNeeded, Heap::Tenure_Old));
1.40 + if (!array)
1.41 + return NULL;
1.42 +
1.43 + // We zero the array for safety, since we could GC or fail before initialization.
1.44 + array->length_ = nslots;
1.45 + memset(array->slots(), 0, sizeof(EnvironmentSlotDescriptor) * nslots);
1.46 + return array;
1.47 +}
1.48 +
1.49 +Environment *
1.50 +Environment::New(Zone *zone, Handle<EnvironmentMap> map, Heap::Tenure tenure)
1.51 +{
1.52 + size_t bytesNeeded = sizeof(Environment) + map->structureSize();
1.53 + Local<Environment> env(zone, Environment::cast(zone->allocate(map, bytesNeeded, tenure)));
1.54 + if (!env)
1.55 + return NULL;
1.56 +
1.57 + // Initialize when called from C++, to make sure object slots are NULL.
1.58 + memset((Object *)env + 1, 0, map->structureSize());
1.59 + return env;
1.60 +}
1.61 +
1.62 +EnvironmentMap *
1.63 +EnvironmentMap::New(Zone *zone, unsigned nvars, Variable **vars)
1.64 +{
1.65 + StructureBufferBuilder structure;
1.66 +
1.67 + Local<EnvironmentSlotDescriptorArray> slots(zone);
1.68 + slots = EnvironmentSlotDescriptorArray::New(zone, nvars);
1.69 + if (!slots)
1.70 + return NULL;
1.71 +
1.72 + // Build the descriptor table, mapping each slot to an offset and type.
1.73 + Local<Type> type(zone);
1.74 + for (unsigned i = 0; i < nvars; i++) {
1.75 + assert(vars[i]->slot() == i);
1.76 + type = vars[i]->type();
1.77 +
1.78 + EnvironmentSlotDescriptor &desc = slots->slots()[i];
1.79 + if (!structure.add(type, &desc.offset))
1.80 + return NULL;
1.81 +
1.82 + desc.type = type;
1.83 + }
1.84 +
1.85 + // Allocate the actual map now.
1.86 + size_t bytesNeeded = sizeof(EnvironmentMap) + structure.traceableOffsetCount() * sizeof(unsigned);
1.87 + Local<EnvironmentMap> map(zone);
1.88 +
1.89 + map = EnvironmentMap::cast(zone->allocateMap(MapKind_Environment, bytesNeeded, MapKind_MetaEnvironment));
1.90 + if (!map)
1.91 + return NULL;
1.92 +
1.93 + // Fill in the map fields, and copy the traceable offsets structure.
1.94 + map->structureSize_ = structure.structureSize();
1.95 + map->traceOffsetCount_ = structure.traceableOffsetCount();
1.96 + map->slots_ = slots;
1.97 + memcpy(const_cast<unsigned *>(map->traceOffsets()), structure.offsets(), sizeof(unsigned) * structure.traceableOffsetCount());
1.98 +
1.99 + return map;
1.100 +}