src/Objects.h
author David Anderson <dvander@alliedmods.net>
Sat Aug 04 17:51:43 2012 -0700 (2012-08-04)
changeset 146 dc7d3b7a918c
parent 91 5f4402180204
child 147 9c544823d090
permissions -rw-r--r--
Basic support for parsing and creating types from structure syntax.
     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_jitcraft_objects_h_
    20 #define _include_jitcraft_objects_h_
    21 
    22 #include "Utility.h"
    23 #include "Spaces.h"
    24 #include "Barriers.h"
    25 
    26 namespace ke {
    27 
    28 class Zone;
    29 
    30 // Garbage-collection objects require a header to describe their layout. This
    31 // header is called a "map". The different kinds of maps are enumerated below.
    32 class Map;
    33 
    34 enum MapKind
    35 {
    36     // Represents Type objects.
    37     MapKind_Type,
    38 
    39     // Represents basic data structures. When adding to this, don't forget
    40     // to update Zone::initialize(), or you'll get a NULL crash trying to
    41     // allocate, since these maps are cached.
    42     MapKind_FixedArray,
    43     MapKind_ByteArray,
    44     MapKind_Code,
    45     MapKind_Reference,
    46     MapKind_Module,
    47     MapKind_Function,
    48     MapKind_String,
    49     MapKind_Native,
    50     MapKind_FreeRegion,     // Special map denoting freed space.
    51     MapKind_EnvironmentSlotDescriptorArray,
    52     MapKind_Environment,
    53     MapKind_Descriptor,
    54 
    55     // Used to represent Arrays. This does not have a canonical map.
    56     MapKind_Array,
    57 
    58     // These represent special Map subclasses that require special tracing.
    59     MapKinds_Special,
    60     MapKind_Meta,               // (default map class)
    61     MapKind_MetaArray,          // ArrayMap instances
    62     MapKind_MetaEnvironment,    // EnvironmentMap instances
    63     MapKinds_EndSpecial,
    64 
    65     MapKinds_Total
    66 };
    67 
    68 // All garbage collected objects must inherit from GCObject. Note that using
    69 // virtual functions is illegal, as the |map| field would be displaced.
    70 class Object
    71 {
    72     Barriered<Map> map_;
    73 
    74   public:
    75     void init(Map *map) {
    76         map_ = map;
    77     }
    78 
    79     Map *map() {
    80         return map_;
    81     }
    82     inline bool is(MapKind map);
    83 
    84     inline void write(Zone *zone, Object **ptr, Object *value);
    85 
    86     static inline size_t offsetOfMap() {
    87         // Clang doesn't accept offsetof here and it's not clear why...
    88         return reinterpret_cast<size_t>(&((Object *)NULL)->map_);
    89     }
    90 };
    91 
    92 // Maps describe the layout of GCObjects. Every GCObject requires a map.
    93 class Map : public Object
    94 {
    95     MapKind kind_;
    96 
    97   public:
    98     void setKind(MapKind kind) {
    99         kind_ = kind;
   100     }
   101     MapKind kind() {
   102         return kind_;
   103     }
   104 
   105     static inline Map *cast(Object *obj) {
   106         // Note that ArrayMap is a map, but a special one: since it is non-
   107         assert(obj->map()->kind() > MapKinds_Special &&
   108                obj->map()->kind() < MapKinds_EndSpecial);
   109         return reinterpret_cast<Map *>(obj);
   110     }
   111 };
   112 
   113 inline bool
   114 Object::is(MapKind kind)
   115 {
   116     return map()->kind() == kind;
   117 }
   118 
   119 }
   120 
   121 #endif //_include_jitcraft_objects_h_