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