src/Types.h
author David Anderson <dvander@alliedmods.net>
Sun Nov 18 16:10:43 2012 -0800 (2012-11-18)
changeset 218 ccb089827f87
parent 212 443d24cfdf2d
child 222 0cde1379b29f
permissions -rw-r--r--
Re-add postinc support.
     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 #include "compiler/Token.h"
    26 
    27 namespace ke {
    28 
    29 class String;
    30 class Descriptor;
    31 class StructMap;
    32 class ArrayMap;
    33 
    34 #define MAX_ARRAY_DEPTH     5
    35 
    36 enum PrimitiveType
    37 {
    38     // A boolean type semantically represents 0 or 1. As an optimization, its
    39     // internal storage type is the same as int32, and any non-zero value
    40     // compares positively to "true".
    41     PrimitiveType_Bool,
    42 
    43     // A character is an arbitrary 8-bit value. Characters are not supported
    44     // as a normal storage class; they may only be used for arrays. When read
    45     // from an array, they are immediately sign extended to 32 bits. Integers
    46     // stored into an char array are truncated to 8 bits.
    47     PrimitiveType_Char,         // signed, 8-bit character
    48 
    49     // Enumerations and untyped variables are stored as signed, 32-bit integers.
    50     PrimitiveType_Int32,
    51 
    52     // Floating point values are 32-bit IEEE-754 floats. This is required ro
    53     // SourcePawn 1 compatibility.
    54     PrimitiveType_Float,
    55 
    56     // This is a native-sized integer type (32-bit on 32-bit systems, 64-bit
    57     // on 64-bit systems), and does not exist in language semantics.
    58     PrimitiveType_Native,
    59 
    60     PrimitiveTypes_Total
    61 };
    62 
    63 class Type : public Object
    64 {
    65   protected:
    66     enum Kind {
    67         // A primitive is a plain-old-data type.
    68         PRIMITIVE,
    69 
    70         // An array is a fixed-length vector of any other type.
    71         ARRAY,
    72         EXTERNAL_ARRAY,
    73 
    74         // A reference type may only be specified on parameters, and
    75         // references may only be computed to primitives or enums.
    76         REFERENCE,
    77 
    78         // A function type encapsulates a function signature.
    79         FUNCTION,
    80 
    81         // Void is an internal type used to specify that a function returns no
    82         // value.
    83         VOID,
    84 
    85         // Enums are plain-old-data with int32 storage. They allow for extra
    86         // type checking that would not be possible with integers.
    87         ENUM,
    88 
    89         // Structs are composite records that may contain primitives, arrays,
    90         // functions, enums, and other structs.
    91         STRUCT,
    92 
    93         // Unchecked is a magic type that has implicit, bitwise coercion to
    94         // int32, float, bool, or an enum.
    95         UNCHECKED
    96     };
    97 
    98   private:
    99     Kind kind() {
   100         return kind_;
   101     }
   102 
   103   public:   
   104     static Type *NewVoid(Zone *zone);
   105     static Type *NewUnchecked(Zone *zone);
   106     static Type *NewPrimitive(Zone *zone, PrimitiveType type);
   107     static Type *NewReference(Zone *zone, Handle<Type> contained, bool isConst);
   108     static Type *NewEnum(Zone *zone, Handle<String> name);
   109 
   110     static bool Compare(Type *left, Type *right);
   111 
   112     static Type *cast(Object *obj) {
   113         assert(!obj ||
   114                obj->is(MapKind_Type) ||
   115                obj->is(MapKind_FunctionType) ||
   116                obj->is(MapKind_StructType) ||
   117                obj->is(MapKind_ArrayType));
   118         return (Type *)obj;
   119     }
   120 
   121     bool isPrimitive() {
   122         return kind() == PRIMITIVE;
   123     }
   124     bool isArray() {
   125         return kind() == ARRAY || kind() == EXTERNAL_ARRAY;
   126     }
   127     bool isReference() {
   128         return kind() == REFERENCE;
   129     }
   130     bool isFunction() {
   131         return kind() == FUNCTION;
   132     }
   133     bool isVoid() {
   134         return kind() == VOID;
   135     }
   136     bool isUnchecked() {
   137         return kind() == UNCHECKED;
   138     }
   139     bool isStruct() {
   140         return kind() == STRUCT;
   141     }
   142     PrimitiveType primitive() {
   143         assert(isPrimitive());
   144         return primitive_;
   145     }
   146     bool isPod() {
   147         return isPrimitive() || isEnum() || isUnchecked();
   148     }
   149     PrimitiveType pod() {
   150         assert(isPod());
   151         return isPrimitive()
   152                ? (primitive() == PrimitiveType_Bool)
   153                  ? PrimitiveType_Int32
   154                  : primitive()
   155                : PrimitiveType_Int32;
   156     }
   157     bool isChar() {
   158         return isPrimitive() && primitive() == PrimitiveType_Char;
   159     }
   160     bool isFloat() {
   161         return isPrimitive() && primitive() == PrimitiveType_Float;
   162     }
   163     bool isInt32() {
   164         return isPrimitive() && primitive() == PrimitiveType_Int32;
   165     }
   166     bool isInt32OrEnum() {
   167         return isInt32() || isEnum();
   168     }
   169     bool isBool() {
   170         return isPrimitive() && primitive() == PrimitiveType_Bool;
   171     }
   172     bool isExternalArray() {
   173         return kind() == EXTERNAL_ARRAY;
   174     }
   175     bool isTraceable() {
   176         return !isPrimitive();
   177     }
   178     bool isConst() {
   179         assert(isPrimitive() ||
   180                isArray() ||
   181                isReference());
   182         return const_;
   183     }
   184     bool isEnum() {
   185         return kind() == ENUM;
   186     }
   187 
   188     String *name() {
   189         assert(isEnum() || isStruct());
   190         return name_;
   191     }
   192     Type *contained_ref() {
   193         assert(false);
   194         return NULL;
   195     }
   196 
   197     // GC Descriptor.
   198     static inline size_t offsetOfName() {
   199         return OFFSETOF(Type, name_);
   200     }
   201     // End GC Descriptor.
   202 
   203   protected:
   204     Kind kind_;
   205     PrimitiveType primitive_;
   206 
   207     // Primitives, arrays, and references.
   208     bool const_;
   209 
   210     // Enums and structs only.
   211     Barriered<String> name_;
   212 };
   213 
   214 class ArrayType : public Type
   215 {
   216   public:
   217     static const int DYNAMIC_ARRAY_SIZE = -1;
   218 
   219     static ArrayType *New(Zone *zone, Handle<Type> contained, int elements);
   220     static ArrayType *NewExternal(Zone *zone, Handle<Type> contained);
   221 
   222     Type *innermost() {
   223         assert(isArray());
   224         Type *temp = contained();
   225         while (temp->isArray())
   226             temp = ArrayType::cast(temp)->contained();
   227         return temp;
   228     }
   229 
   230     Type *contained() {
   231         return contained_;
   232     }
   233     bool isFixedLength() {
   234         assert(isArray());
   235         return elements_ > 0;
   236     }
   237     bool isCharArray() {
   238         return contained()->isPrimitive() &&
   239                contained()->primitive() == PrimitiveType_Char;
   240     }
   241     unsigned fixedLength() {
   242         assert(isFixedLength());
   243         return elements_;
   244     }
   245     unsigned levels() {
   246         assert(isArray());
   247         return levels_;
   248     }
   249     bool hasInlineElements() {
   250         assert(isArray());
   251 
   252         // We don't allocate traceable elements inline, since there's no
   253         // point to the optimization; we're going to be allocating lots of
   254         // objects anyway (in theory).
   255         return isFixedLength() && !contained()->isTraceable();
   256     }
   257 
   258     // Arrays of this type can be created with this cached map.
   259     ArrayMap *newMap() {
   260         return newMap_;
   261     }
   262     void setNewMap(ArrayMap *map) {
   263         assert(!newMap());
   264         newMap_ = map;
   265     }
   266 
   267     // GC Descriptor.
   268     static inline size_t offsetOfContained() {
   269         return OFFSETOF(ArrayType, contained_);
   270     }
   271     static inline size_t offsetOfNewMap() {
   272         return OFFSETOF(ArrayType, newMap_);
   273     }
   274     // End GC Descriptor.
   275 
   276     static ArrayType *cast(Object *obj) {
   277         assert(!obj || obj->is(MapKind_ArrayType));
   278         return (ArrayType *)obj;
   279     }
   280 
   281   private:
   282     int32 elements_;
   283     unsigned levels_;
   284     Barriered<Type> contained_;
   285     Barriered<ArrayMap> newMap_;
   286 };
   287 
   288 class StructType : public Type
   289 {
   290   public:
   291     static StructType *New(Zone *zone, Handle<String> name, Handle<FixedArray> fields);
   292 
   293     FixedArray *fields() {
   294         assert(isStruct());
   295         return fields_;
   296     }
   297     Descriptor *lookupField(Handle<String> name, unsigned *index = NULL);
   298 
   299     // Objects of this type can be created with this cached map.
   300     StructMap *newMap() {
   301         return newMap_;
   302     }
   303     void setNewMap(StructMap *map) {
   304         assert(!newMap());
   305         newMap_ = map;
   306     }
   307 
   308     // GC Descriptor.
   309     static inline size_t offsetOfFields() {
   310         return OFFSETOF(StructType, fields_);
   311     }
   312     static inline size_t offsetOfNewMap() {
   313         return OFFSETOF(StructType, newMap_);
   314     }
   315     // End GC Descriptor.
   316 
   317     static StructType *cast(Object *obj) {
   318         assert(!obj || obj->is(MapKind_StructType));
   319         return (StructType *)obj;
   320     }
   321 
   322   private:
   323     // Structs only.
   324     Barriered<FixedArray> fields_;
   325     Barriered<StructMap> newMap_;
   326 };
   327 
   328 class FunctionType : public Type
   329 {
   330   public:
   331     static FunctionType *New(Zone *zone, TokenKind kind);
   332 
   333     Type *parameterAt(size_t i) {
   334         return Type::cast(parameters()->at(i));
   335     }
   336     Type *returnType() {
   337         assert(isFunction());
   338         return returnType_;
   339     }
   340     FixedArray *parameters() {
   341         assert(isFunction());
   342         return parameters_;
   343     }
   344     FixedArray *defaults() {
   345         assert(isFunction());
   346         return defaults_;
   347     }
   348     bool isNative() {
   349         return token_ == TOK_NATIVE;
   350     }
   351     bool isForward() {
   352         return token_ == TOK_FORWARD;
   353     }
   354     bool isNativeVariadic() {
   355         assert(isNative());
   356         return variadicNative_;
   357     }
   358     void setReturnType(Handle<Type> returnType) {
   359         assert(!returnType_);
   360         returnType_ = returnType;
   361     }
   362     void setParameterTypes(Handle<FixedArray> types) {
   363         assert(!parameters_);
   364         parameters_ = types;
   365     }
   366     TokenKind token() {
   367         return token_;
   368     }
   369 
   370     // GC Descriptor.
   371     static inline size_t offsetOfReturnType() {
   372         return OFFSETOF(FunctionType, returnType_);
   373     }
   374     static inline size_t offsetOfParameters() {
   375         return OFFSETOF(FunctionType, parameters_);
   376     }
   377     static inline size_t offsetOfDefaults() {
   378         return OFFSETOF(FunctionType, defaults_);
   379     }
   380     // End GC Descriptor.
   381 
   382     static FunctionType *cast(Object *obj) {
   383         assert(!obj || obj->is(MapKind_FunctionType));
   384         return (FunctionType *)obj;
   385     }
   386 
   387   private:
   388     // Functions only.
   389     Barriered<Type> returnType_;
   390     Barriered<FixedArray> parameters_;
   391     Barriered<FixedArray> defaults_;
   392     TokenKind token_;
   393     bool variadicNative_;
   394 };
   395 
   396 class BoxedPrimitive
   397 {
   398     PrimitiveType type_;
   399     union {
   400         int i_;
   401         float f_;
   402     };
   403 
   404   public:
   405     static BoxedPrimitive Int(int i) {
   406         BoxedPrimitive b;
   407         b.type_ = PrimitiveType_Int32;
   408         b.i_ = i;
   409         return b;
   410     }
   411     static BoxedPrimitive Float(float f) {
   412         BoxedPrimitive b;
   413         b.type_ = PrimitiveType_Float;
   414         b.f_ = f;
   415         return b;
   416     }
   417 
   418     PrimitiveType type() const {
   419         return type_;
   420     }
   421     int toInt() const {
   422         assert(type() == PrimitiveType_Int32);
   423         return i_;
   424     }
   425     float toFloat() const {
   426         assert(type() == PrimitiveType_Float);
   427         return f_;
   428     }
   429 };
   430 
   431 const char *GetTypeName(Type *type);
   432 
   433 }
   434 
   435 #endif // _include_jc_types_h_