jitcraft/Types.cpp
author David Anderson <dvander@alliedmods.net>
Sun Jul 01 16:36:39 2012 -0700 (2012-07-01)
changeset 22 765ab1b672a3
parent 21 af5d8e985502
child 25 23e8b3d8e4e6
permissions -rw-r--r--
Added support for fused math-assign ops (+=, etc).
     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     Object *obj = zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old);
    28     if (!obj)
    29         return NULL;
    30 
    31     Type *type = Type::cast(obj);
    32     type->kind_ = PRIMITIVE;
    33     type->tag_ = tag;
    34     type->contained_ = NULL;
    35 
    36     if (tag && strcmp(tag, "Float") == 0)
    37         type->primitive_ = PrimitiveType_Float;
    38     else if (tag && strcmp(tag, "String") == 0)
    39         type->primitive_ = PrimitiveType_Char;
    40     else
    41         type->primitive_ = PrimitiveType_Int32;
    42     return type;
    43 }
    44 
    45 Type *
    46 Type::NewPrimitive(Zone *zone, PrimitiveType prim)
    47 {
    48     Object *obj = zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old);
    49     if (!obj)
    50         return NULL;
    51 
    52     Type *type = Type::cast(obj);
    53     type->kind_ = PRIMITIVE;
    54     type->tag_ = NULL;
    55     type->contained_ = NULL;
    56     type->primitive_ = prim;
    57     return type;
    58 }
    59 
    60 Type *
    61 Type::NewArray(Zone *zone, Type *contained, int elements, bool isConst)
    62 {
    63     Object *obj = zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old);
    64     if (!obj)
    65         return NULL;
    66 
    67     Type *type = Type::cast(obj);
    68     type->kind_ = ARRAY;
    69     type->tag_ = NULL;
    70     type->contained_ = contained;
    71     type->elements_ = elements;
    72     if (contained->isPrimitive())
    73         type->levels_ = 1;
    74     else
    75         type->levels_ = contained->levels() + 1;
    76     type->const_ = isConst;
    77     return type;
    78 }
    79 
    80 Type *
    81 Type::NewReference(Zone *zone, Type *contained)
    82 {
    83     Object *obj = zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old);
    84     if (!obj)
    85         return NULL;
    86 
    87     Type *type = Type::cast(obj);
    88     type->kind_ = REFERENCE;
    89     type->tag_ = NULL;
    90     type->contained_ = contained;
    91     return type;
    92 }
    93 
    94 Type *
    95 Type::NewFunction(Zone *zone, Type *returnType, FixedArray *parameters)
    96 {
    97     Object *obj = zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old);
    98     if (!obj)
    99         return NULL;
   100 
   101     Type *type = Type::cast(obj);
   102     type->kind_ = FUNCTION;
   103     type->contained_ = returnType;
   104     type->parameters_ = parameters;
   105     return type;
   106 }