src/Reference.cpp
author David Anderson <dvander@alliedmods.net>
Sun Aug 05 12:55:51 2012 -0700 (2012-08-05)
changeset 164 3bf7c007883d
parent 86 32ad0523b309
child 168 4d0fff61d65e
permissions -rw-r--r--
Support FieldExpressions as references.
     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 "Reference.h"
    19 #include "Zone.h"
    20 #include "Array.h"
    21 #include "Structures.h"
    22 #include "Heap-inl.h"
    23 
    24 using namespace ke;
    25 
    26 Reference *
    27 Reference::NewStack(Zone *zone, void *location, Heap::Tenure tenure)
    28 {
    29     Local<Reference> ref(zone, Reference::cast(zone->allocate(MapKind_Reference, sizeof(Reference), tenure)));
    30     if (!ref)
    31         return NULL;
    32 
    33     ref->location_ = location;
    34     ref->base_ = NULL;
    35     return ref;
    36 }
    37 
    38 Reference *
    39 Reference::NewStack(Zone *zone, const LocalSlot &slot, Heap::Tenure tenure)
    40 {
    41     Local<Reference> ref(zone, Reference::cast(zone->allocate(MapKind_Reference, sizeof(Reference), tenure)));
    42     if (!ref)
    43         return NULL;
    44 
    45     ref->location_ = &ref->slot_;
    46     ref->slot_ = slot;
    47     ref->base_ = NULL;
    48     return ref;
    49 }
    50 
    51 Reference *
    52 Reference::NewArray(Zone *zone, Handle<Array> base, size_t index, Heap::Tenure tenure)
    53 {
    54     Local<Reference> ref(zone, Reference::cast(zone->allocate(MapKind_Reference, sizeof(Reference), tenure)));
    55     if (!ref)
    56         return NULL;
    57 
    58     ref->location_ = base->slotPtr(index);
    59     ref->base_ = base;
    60     return ref;
    61 }
    62 
    63 Reference *
    64 Reference::NewField(Zone *zone, Handle<Struct> obj, size_t index, Heap::Tenure tenure)
    65 {
    66     Local<Reference> ref(zone, Reference::cast(zone->allocate(MapKind_Reference, sizeof(Reference), tenure)));
    67     if (!ref)
    68         return NULL;
    69 
    70     Descriptor *desc = Descriptor::cast(obj->map()->type()->fields()->at(index));
    71     ref->location_ = obj->ref(desc);
    72     ref->base_ = obj;
    73     return ref;
    74 }
    75