jitcraft/Types.cpp
author David Anderson <dvander@alliedmods.net>
Sat Jul 07 17:11:31 2012 -0700 (2012-07-07)
changeset 53 13bd3a8df334
parent 41 34987baade41
child 54 8e22792a3921
permissions -rw-r--r--
Added a mark-sweep garbage collector.
     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 "Types.h"
    19 #include "Zone.h"
    20 #include "Heap-inl.h"
    21 
    22 using namespace jc;
    23 
    24 Type *
    25 Type::NewPrimitive(Zone *zone, Name *tag)
    26 {
    27     Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
    28     if (!type)
    29         return NULL;
    30 
    31     type->kind_ = PRIMITIVE;
    32     type->tag_ = tag;
    33     type->contained_ = NULL;
    34     type->returnType_ = NULL;
    35     type->parameters_ = NULL;
    36 
    37     if (tag && strcmp(tag, "Float") == 0)
    38         type->primitive_ = PrimitiveType_Float;
    39     else if (tag && strcmp(tag, "String") == 0)
    40         type->primitive_ = PrimitiveType_Char;
    41     else
    42         type->primitive_ = PrimitiveType_Int32;
    43     return type;
    44 }
    45 
    46 Type *
    47 Type::NewPrimitive(Zone *zone, PrimitiveType prim)
    48 {
    49     Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
    50     if (!type)
    51         return NULL;
    52 
    53     type->kind_ = PRIMITIVE;
    54     type->tag_ = NULL;
    55     type->contained_ = NULL;
    56     type->returnType_ = NULL;
    57     type->parameters_ = NULL;
    58     type->primitive_ = prim;
    59     return type;
    60 }
    61 
    62 Type *
    63 Type::NewArray(Zone *zone, Handle<Type> contained, int elements, bool isConst)
    64 {
    65     Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
    66     if (!type)
    67         return NULL;
    68 
    69     type->kind_ = ARRAY;
    70     type->tag_ = NULL;
    71     type->contained_ = contained;
    72     type->returnType_ = NULL;
    73     type->parameters_ = NULL;
    74     type->elements_ = elements;
    75     if (contained->isPrimitive())
    76         type->levels_ = 1;
    77     else
    78         type->levels_ = contained->levels() + 1;
    79     type->const_ = isConst;
    80     return type;
    81 }
    82 
    83 Type *
    84 Type::NewReference(Zone *zone, Handle<Type> contained)
    85 {
    86     Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
    87     if (!type)
    88         return NULL;
    89 
    90     type->kind_ = REFERENCE;
    91     type->tag_ = NULL;
    92     type->contained_ = contained;
    93     type->returnType_ = NULL;
    94     type->parameters_ = NULL;
    95     return type;
    96 }
    97 
    98 Type *
    99 Type::NewFunction(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters)
   100 {
   101     Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
   102     if (!type)
   103         return NULL;
   104 
   105     type->kind_ = FUNCTION;
   106     type->contained_ = NULL;
   107     type->returnType_ = returnType;
   108     type->parameters_ = parameters;
   109     return type;
   110 }