jitcraft/Types.h
author David Anderson <dvander@alliedmods.net>
Sun Jul 08 01:54:08 2012 -0700 (2012-07-08)
changeset 60 581a9c793062
parent 59 b7d61ddb3c8d
child 63 dd57743d220e
permissions -rw-r--r--
Initial import of some kind of API.
     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     };
    47 
    48   private:
    49     Kind kind() {
    50         return kind_;
    51     }
    52 
    53   public:
    54     static const int UNKNOWN_SIZE = -1;
    55     static const size_t MAX_UNSAFE_ARRAY_DEPTH = 5;
    56     
    57     static Type *NewPrimitive(Zone *zone, Handle<String> tag);
    58     static Type *NewPrimitive(Zone *zone, PrimitiveType type);
    59     static Type *NewArray(Zone *zone, Handle<Type> contained, int elements, bool isConst);
    60     static Type *NewReference(Zone *zone, Handle<Type> contained);
    61     static Type *NewFunction(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters);
    62 
    63     static Type *cast(Object *obj) {
    64         assert(!obj || obj->is(MapKind_Type));
    65         return (Type *)obj;
    66     }
    67 
    68     bool isPrimitive() {
    69         return kind() == PRIMITIVE;
    70     }
    71     bool isArray() {
    72         return kind() == ARRAY;
    73     }
    74     bool isReference() {
    75         return kind() == REFERENCE;
    76     }
    77     bool isFunction() {
    78         return kind() == FUNCTION;
    79     }
    80     PrimitiveType primitive() {
    81         assert(isPrimitive());
    82         return primitive_;
    83     }
    84     Type *contained() {
    85         assert(isArray() || isReference());
    86         return Type::cast(contained_);
    87     }
    88     int elements() {
    89         assert(isArray());
    90         return elements_;
    91     }
    92     unsigned levels() {
    93         assert(isArray());
    94         return levels_;
    95     }
    96     String *tag() {
    97         assert(isPrimitive());
    98         return tag_;
    99     }
   100     Type *innermost() {
   101         assert(isArray());
   102         Type *temp = contained();
   103         while (temp->isArray())
   104             temp = temp->contained();
   105         return temp;
   106     }
   107     bool isFloat() {
   108         return isPrimitive() && primitive() == PrimitiveType_Float;
   109     }
   110     bool isInt32() {
   111         return isPrimitive() && primitive() == PrimitiveType_Int32;
   112     }
   113     bool isCharArray() {
   114         return isArray() &&
   115                contained()->isPrimitive() &&
   116                contained()->primitive() == PrimitiveType_Char;
   117     }
   118     bool isConst() {
   119         assert(isArray());
   120         return const_;
   121     }
   122     Type *returnType() {
   123         assert(isFunction());
   124         return returnType_;
   125     }
   126     FixedArray *parameters() {
   127         assert(isFunction());
   128         return parameters_;
   129     }
   130     Type *parameterAt(size_t i) {
   131         return Type::cast(parameters()->at(i));
   132     }
   133     bool isTraceable() {
   134         return !isPrimitive();
   135     }
   136 
   137     // GC Descriptor.
   138     static inline size_t offsetOfContained() {
   139         return offsetof(Type, contained_);
   140     }
   141     static inline size_t offsetOfReturnType() {
   142         return offsetof(Type, returnType_);
   143     }
   144     static inline size_t offsetOfParameters() {
   145         return offsetof(Type, parameters_);
   146     }
   147     static inline size_t offsetOfTag() {
   148         return offsetof(Type, tag_);
   149     }
   150     // End GC Descriptor.
   151 
   152   private:
   153     Kind kind_;
   154     PrimitiveType primitive_;
   155     Barriered<String> tag_;
   156 
   157     // Arrays only.
   158     int32 elements_;
   159     unsigned levels_;
   160     bool const_;
   161 
   162     // Arrays and references only.
   163     Barriered<Type> contained_;
   164 
   165     // Functions only.
   166     Barriered<Type> returnType_;
   167     Barriered<FixedArray> parameters_;
   168 };
   169 
   170 class BoxedPrimitive
   171 {
   172     PrimitiveType type_;
   173     union {
   174         int i_;
   175     };
   176 
   177   public:
   178     static BoxedPrimitive Int(int i) {
   179         BoxedPrimitive b;
   180         b.type_ = PrimitiveType_Int32;
   181         b.i_ = i;
   182         return b;
   183     }
   184 
   185     PrimitiveType type() const {
   186         return type_;
   187     }
   188     int toInt() const {
   189         assert(type() == PrimitiveType_Int32);
   190         return i_;
   191     }
   192 };
   193 
   194 }
   195 
   196 #endif // _include_jc_types_h_