src/Environments.cpp
author Michael Busby <prodigysim@gmail.com>
Mon Jun 10 01:50:18 2013 +0100 (2013-06-10)
changeset 264 70b8f318c379
parent 263 ba85a47ee414
permissions -rw-r--r--
Parse assignment RTL instead of LTR, fixes multiple assignment (bug 5420).
asherkin@263
     1
/*
asherkin@263
     2
 * Copyright (C) 2012 David Anderson
asherkin@263
     3
 *
asherkin@263
     4
 * This file is part of SourcePawn.
asherkin@263
     5
 *
asherkin@263
     6
 * SourcePawn is free software: you can redistribute it and/or modify it under
asherkin@263
     7
 * the terms of the GNU General Public License as published by the Free
asherkin@263
     8
 * Software Foundation, either version 3 of the License, or (at your option)
asherkin@263
     9
 * any later version.
asherkin@263
    10
 * 
asherkin@263
    11
 * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
asherkin@263
    12
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
asherkin@263
    13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
asherkin@263
    14
 *
asherkin@263
    15
 * You should have received a copy of the GNU General Public License along with
asherkin@263
    16
 * SourcePawn. If not, see http://www.gnu.org/licenses/.
asherkin@263
    17
 */
asherkin@263
    18
#include <string.h>
asherkin@263
    19
#include "Utility.h"
asherkin@263
    20
#include "Environments.h"
asherkin@263
    21
#include "Structures.h"
asherkin@263
    22
#include "FixedArray.h"
asherkin@263
    23
#include "Zone.h"
asherkin@263
    24
#include "compiler/NameBinding.h"
asherkin@263
    25
#include "Spaces-inl.h"
asherkin@263
    26
#include "Heap-inl.h"
asherkin@263
    27
asherkin@263
    28
using namespace ke;
asherkin@263
    29
asherkin@263
    30
EnvironmentSlotDescriptorArray *
asherkin@263
    31
EnvironmentSlotDescriptorArray::New(Zone *zone, unsigned nslots)
asherkin@263
    32
{
asherkin@263
    33
    size_t bytesNeeded = sizeof(EnvironmentSlotDescriptorArray) +
asherkin@263
    34
                         sizeof(EnvironmentSlotDescriptor) * nslots;
asherkin@263
    35
asherkin@263
    36
    Local<EnvironmentSlotDescriptorArray> array(zone);
asherkin@263
    37
    Local<Map> map(zone, zone->getMap(MapKind_EnvironmentSlotDescriptorArray));
asherkin@263
    38
asherkin@263
    39
    array = EnvironmentSlotDescriptorArray::cast(zone->allocate(map, bytesNeeded, Heap::Tenure_Old));
asherkin@263
    40
    if (!array)
asherkin@263
    41
        return NULL;
asherkin@263
    42
asherkin@263
    43
    // We zero the array for safety, since we could GC or fail before initialization.
asherkin@263
    44
    array->length_ = nslots;
asherkin@263
    45
    memset(array->slots(), 0, sizeof(EnvironmentSlotDescriptor) * nslots);
asherkin@263
    46
    return array;
asherkin@263
    47
}
asherkin@263
    48
asherkin@263
    49
Environment *
asherkin@263
    50
Environment::New(Zone *zone, Handle<EnvironmentMap> map, Heap::Tenure tenure)
asherkin@263
    51
{
asherkin@263
    52
    size_t bytesNeeded = sizeof(Environment) + map->structureSize();
asherkin@263
    53
    Local<Environment> env(zone, Environment::cast(zone->allocate(map, bytesNeeded, tenure)));
asherkin@263
    54
    if (!env)
asherkin@263
    55
        return NULL;
asherkin@263
    56
asherkin@263
    57
    // Initialize when called from C++, to make sure object slots are NULL.
asherkin@263
    58
    memset((Object *)env + 1, 0, map->structureSize());
asherkin@263
    59
    return env;
asherkin@263
    60
}
asherkin@263
    61
asherkin@263
    62
#if 0
asherkin@263
    63
EnvironmentMap *
asherkin@263
    64
EnvironmentMap::New(Zone *zone, unsigned nvars, Variable **vars)
asherkin@263
    65
{
asherkin@263
    66
    StructureBufferBuilder structure;
asherkin@263
    67
asherkin@263
    68
    Local<EnvironmentSlotDescriptorArray> slots(zone);
asherkin@263
    69
    slots = EnvironmentSlotDescriptorArray::New(zone, nvars);
asherkin@263
    70
    if (!slots)
asherkin@263
    71
        return NULL;
asherkin@263
    72
asherkin@263
    73
    // Build the descriptor table, mapping each slot to an offset and type.
asherkin@263
    74
    Local<Type> type(zone);
asherkin@263
    75
    for (unsigned i = 0; i < nvars; i++) {
asherkin@263
    76
        assert(vars[i]->slot() == i);
asherkin@263
    77
        type = vars[i]->type();
asherkin@263
    78
asherkin@263
    79
        EnvironmentSlotDescriptor &desc = slots->slots()[i];
asherkin@263
    80
        if (!structure.add(type, &desc.offset))
asherkin@263
    81
            return NULL;
asherkin@263
    82
asherkin@263
    83
        desc.type = type;
asherkin@263
    84
    }
asherkin@263
    85
asherkin@263
    86
    // Allocate the actual map now.
asherkin@263
    87
    size_t bytesNeeded = sizeof(EnvironmentMap) + structure.traceableOffsetCount() * sizeof(unsigned);
asherkin@263
    88
asherkin@263
    89
    Local<EnvironmentMap> map(zone);
asherkin@263
    90
    map = EnvironmentMap::cast(zone->allocateMap(MapKind_Environment, bytesNeeded, MapKind_MetaEnvironment));
asherkin@263
    91
    if (!map)
asherkin@263
    92
        return NULL;
asherkin@263
    93
    
asherkin@263
    94
    // Fill in the map fields, and copy the traceable offsets structure.
asherkin@263
    95
    map->structureSize_ = structure.structureSize();
asherkin@263
    96
    map->traceOffsetCount_ = structure.traceableOffsetCount();
asherkin@263
    97
    map->slots_ = slots;
asherkin@263
    98
    memcpy(const_cast<unsigned *>(map->traceOffsets()), structure.offsets(), sizeof(unsigned) * structure.traceableOffsetCount());
asherkin@263
    99
asherkin@263
   100
    return map;
asherkin@263
   101
}
dvander@195
   102
#endif