src/Types.h
author David Anderson <dvander@alliedmods.net>
Mon Jul 30 22:24:57 2012 -0700 (2012-07-30)
changeset 143 393f1b8ac8c3
parent 91 5f4402180204
child 145 e9d804821c48
permissions -rw-r--r--
Initial support for booleans. Coercion does not work yet.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of JITCraft.
     6  *
     7  * JITCraft is free software: you can redistribute it and/or modify it under
     8  * the terms of the GNU General Public License as published by the Free
     9  * Software Foundation, either version 3 of the License, or (at your option)
    10  * any later version.
    11  * 
    12  * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    15  *
    16  * You should have received a copy of the GNU General Public License along with
    17  * JITCraft. If not, see http://www.gnu.org/licenses/.
    18  */
    19 #ifndef _include_jc_types_h_
    20 #define _include_jc_types_h_
    21 
    22 #include "Heap.h"
    23 #include "Barriers.h"
    24 #include "FixedArray.h"
    25 
    26 namespace ke {
    27 
    28 class String;
    29 
    30 enum PrimitiveType
    31 {
    32     PrimitiveType_Bool,         // 0 or 1
    33     PrimitiveType_Char,         // signed, 8-bit character
    34     PrimitiveType_Int32,        // signed, 32-bit integer
    35     PrimitiveType_Float,        // 32-bit IEEE-754 float
    36     PrimitiveType_Native,       // native-sized word
    37     PrimitiveTypes_Total
    38 };
    39 
    40 class Type : public Object
    41 {
    42     enum Kind {
    43         PRIMITIVE,
    44         ARRAY,
    45         REFERENCE,
    46         FUNCTION,
    47         NATIVE,
    48         VOID
    49     };
    50 
    51   private:
    52     Kind kind() {
    53         return kind_;
    54     }
    55 
    56   public:
    57     static const int UNKNOWN_SIZE = -1;
    58     static const size_t MAX_UNSAFE_ARRAY_DEPTH = 5;
    59     
    60     static Type *NewVoid(Zone *zone);
    61     static Type *NewPrimitive(Zone *zone, Handle<String> tag);
    62     static Type *NewPrimitive(Zone *zone, PrimitiveType type);
    63     static Type *NewArray(Zone *zone, Handle<Type> contained, int elements, bool isConst);
    64     static Type *NewReference(Zone *zone, Handle<Type> contained, bool isConst);
    65     static Type *NewFunction(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters);
    66     static Type *NewNative(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters, bool isVariadic);
    67 
    68     static bool Compare(Type *left, Type *right);
    69 
    70     static Type *cast(Object *obj) {
    71         assert(!obj || obj->is(MapKind_Type));
    72         return (Type *)obj;
    73     }
    74 
    75     bool isPrimitive() {
    76         return kind() == PRIMITIVE;
    77     }
    78     bool isArray() {
    79         return kind() == ARRAY;
    80     }
    81     bool isReference() {
    82         return kind() == REFERENCE;
    83     }
    84     bool isFunction() {
    85         return kind() == FUNCTION || kind() == NATIVE;
    86     }
    87     bool isNative() {
    88         return kind() == NATIVE;
    89     }
    90     bool isVoid() {
    91         return kind() == VOID;
    92     }
    93     bool isNativeVariadic() {
    94         assert(isNative());
    95         return variadicNative_;
    96     }
    97     PrimitiveType primitive() {
    98         assert(isPrimitive());
    99         return primitive_;
   100     }
   101     Type *contained() {
   102         assert(isArray() || isReference());
   103         return Type::cast(contained_);
   104     }
   105     int elements() {
   106         assert(isArray());
   107         return elements_;
   108     }
   109     unsigned levels() {
   110         assert(isArray());
   111         return levels_;
   112     }
   113     String *tag() {
   114         assert(isPrimitive());
   115         return tag_;
   116     }
   117     Type *innermost() {
   118         assert(isArray());
   119         Type *temp = contained();
   120         while (temp->isArray())
   121             temp = temp->contained();
   122         return temp;
   123     }
   124     bool isFloat() {
   125         return isPrimitive() && primitive() == PrimitiveType_Float;
   126     }
   127     bool isInt32() {
   128         return isPrimitive() && primitive() == PrimitiveType_Int32;
   129     }
   130     bool isCharArray() {
   131         return isArray() &&
   132                contained()->isPrimitive() &&
   133                contained()->primitive() == PrimitiveType_Char;
   134     }
   135     Type *returnType() {
   136         assert(isFunction());
   137         return returnType_;
   138     }
   139     FixedArray *parameters() {
   140         assert(isFunction());
   141         return parameters_;
   142     }
   143     Type *parameterAt(size_t i) {
   144         return Type::cast(parameters()->at(i));
   145     }
   146     bool isTraceable() {
   147         return !isPrimitive();
   148     }
   149     bool isConst() {
   150         assert(kind() == PRIMITIVE || kind() == ARRAY || kind() == REFERENCE);
   151         return const_;
   152     }
   153 
   154     // GC Descriptor.
   155     static inline size_t offsetOfContained() {
   156         return OFFSETOF(Type, contained_);
   157     }
   158     static inline size_t offsetOfReturnType() {
   159         return OFFSETOF(Type, returnType_);
   160     }
   161     static inline size_t offsetOfParameters() {
   162         return OFFSETOF(Type, parameters_);
   163     }
   164     static inline size_t offsetOfTag() {
   165         return OFFSETOF(Type, tag_);
   166     }
   167     // End GC Descriptor.
   168 
   169   private:
   170     Kind kind_;
   171     PrimitiveType primitive_;
   172     Barriered<String> tag_;
   173 
   174     // Primitives, arrays, and references.
   175     bool const_;
   176 
   177     // Arrays only.
   178     int32 elements_;
   179     unsigned levels_;
   180 
   181     // Arrays and references only.
   182     Barriered<Type> contained_;
   183 
   184     // Functions only.
   185     Barriered<Type> returnType_;
   186     Barriered<FixedArray> parameters_;
   187     bool variadicNative_;
   188 };
   189 
   190 class BoxedPrimitive
   191 {
   192     PrimitiveType type_;
   193     union {
   194         int i_;
   195     };
   196 
   197   public:
   198     static BoxedPrimitive Int(int i) {
   199         BoxedPrimitive b;
   200         b.type_ = PrimitiveType_Int32;
   201         b.i_ = i;
   202         return b;
   203     }
   204 
   205     PrimitiveType type() const {
   206         return type_;
   207     }
   208     int toInt() const {
   209         assert(type() == PrimitiveType_Int32);
   210         return i_;
   211     }
   212 };
   213 
   214 }
   215 
   216 #endif // _include_jc_types_h_