src/Types.h
author David Anderson <danderson@mozilla.com>
Wed Jul 25 00:48:08 2012 -0700 (2012-07-25)
changeset 91 5f4402180204
parent 86 32ad0523b309
child 143 393f1b8ac8c3
permissions -rw-r--r--
Added clang support. A lot of the motion here is introducing a new offsetof because clang's seems broken?
     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_Char,         // signed, 8-bit character
    33     PrimitiveType_Int32,        // signed, 32-bit integer
    34     PrimitiveType_Float,        // 32-bit IEEE-754 float
    35     PrimitiveType_Native,       // native-sized word
    36     PrimitiveTypes_Total
    37 };
    38 
    39 class Type : public Object
    40 {
    41     enum Kind {
    42         PRIMITIVE,
    43         ARRAY,
    44         REFERENCE,
    45         FUNCTION,
    46         NATIVE,
    47         VOID
    48     };
    49 
    50   private:
    51     Kind kind() {
    52         return kind_;
    53     }
    54 
    55   public:
    56     static const int UNKNOWN_SIZE = -1;
    57     static const size_t MAX_UNSAFE_ARRAY_DEPTH = 5;
    58     
    59     static Type *NewVoid(Zone *zone);
    60     static Type *NewPrimitive(Zone *zone, Handle<String> tag);
    61     static Type *NewPrimitive(Zone *zone, PrimitiveType type);
    62     static Type *NewArray(Zone *zone, Handle<Type> contained, int elements, bool isConst);
    63     static Type *NewReference(Zone *zone, Handle<Type> contained, bool isConst);
    64     static Type *NewFunction(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters);
    65     static Type *NewNative(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters, bool isVariadic);
    66 
    67     static bool Compare(Type *left, Type *right);
    68 
    69     static Type *cast(Object *obj) {
    70         assert(!obj || obj->is(MapKind_Type));
    71         return (Type *)obj;
    72     }
    73 
    74     bool isPrimitive() {
    75         return kind() == PRIMITIVE;
    76     }
    77     bool isArray() {
    78         return kind() == ARRAY;
    79     }
    80     bool isReference() {
    81         return kind() == REFERENCE;
    82     }
    83     bool isFunction() {
    84         return kind() == FUNCTION || kind() == NATIVE;
    85     }
    86     bool isNative() {
    87         return kind() == NATIVE;
    88     }
    89     bool isVoid() {
    90         return kind() == VOID;
    91     }
    92     bool isNativeVariadic() {
    93         assert(isNative());
    94         return variadicNative_;
    95     }
    96     PrimitiveType primitive() {
    97         assert(isPrimitive());
    98         return primitive_;
    99     }
   100     Type *contained() {
   101         assert(isArray() || isReference());
   102         return Type::cast(contained_);
   103     }
   104     int elements() {
   105         assert(isArray());
   106         return elements_;
   107     }
   108     unsigned levels() {
   109         assert(isArray());
   110         return levels_;
   111     }
   112     String *tag() {
   113         assert(isPrimitive());
   114         return tag_;
   115     }
   116     Type *innermost() {
   117         assert(isArray());
   118         Type *temp = contained();
   119         while (temp->isArray())
   120             temp = temp->contained();
   121         return temp;
   122     }
   123     bool isFloat() {
   124         return isPrimitive() && primitive() == PrimitiveType_Float;
   125     }
   126     bool isInt32() {
   127         return isPrimitive() && primitive() == PrimitiveType_Int32;
   128     }
   129     bool isCharArray() {
   130         return isArray() &&
   131                contained()->isPrimitive() &&
   132                contained()->primitive() == PrimitiveType_Char;
   133     }
   134     Type *returnType() {
   135         assert(isFunction());
   136         return returnType_;
   137     }
   138     FixedArray *parameters() {
   139         assert(isFunction());
   140         return parameters_;
   141     }
   142     Type *parameterAt(size_t i) {
   143         return Type::cast(parameters()->at(i));
   144     }
   145     bool isTraceable() {
   146         return !isPrimitive();
   147     }
   148     bool isConst() {
   149         assert(kind() == PRIMITIVE || kind() == ARRAY || kind() == REFERENCE);
   150         return const_;
   151     }
   152 
   153     // GC Descriptor.
   154     static inline size_t offsetOfContained() {
   155         return OFFSETOF(Type, contained_);
   156     }
   157     static inline size_t offsetOfReturnType() {
   158         return OFFSETOF(Type, returnType_);
   159     }
   160     static inline size_t offsetOfParameters() {
   161         return OFFSETOF(Type, parameters_);
   162     }
   163     static inline size_t offsetOfTag() {
   164         return OFFSETOF(Type, tag_);
   165     }
   166     // End GC Descriptor.
   167 
   168   private:
   169     Kind kind_;
   170     PrimitiveType primitive_;
   171     Barriered<String> tag_;
   172 
   173     // Primitives, arrays, and references.
   174     bool const_;
   175 
   176     // Arrays only.
   177     int32 elements_;
   178     unsigned levels_;
   179 
   180     // Arrays and references only.
   181     Barriered<Type> contained_;
   182 
   183     // Functions only.
   184     Barriered<Type> returnType_;
   185     Barriered<FixedArray> parameters_;
   186     bool variadicNative_;
   187 };
   188 
   189 class BoxedPrimitive
   190 {
   191     PrimitiveType type_;
   192     union {
   193         int i_;
   194     };
   195 
   196   public:
   197     static BoxedPrimitive Int(int i) {
   198         BoxedPrimitive b;
   199         b.type_ = PrimitiveType_Int32;
   200         b.i_ = i;
   201         return b;
   202     }
   203 
   204     PrimitiveType type() const {
   205         return type_;
   206     }
   207     int toInt() const {
   208         assert(type() == PrimitiveType_Int32);
   209         return i_;
   210     }
   211 };
   212 
   213 }
   214 
   215 #endif // _include_jc_types_h_