jitcraft/ObjectVisitor.h
author David Anderson <dvander@alliedmods.net>
Mon Jul 09 21:40:20 2012 -0700 (2012-07-09)
changeset 66 cbba1997dc71
parent 64 11c81eb30246
child 68 b40b0e18a918
permissions -rw-r--r--
Add support for passing 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_object_visitor_h_
    20 #define _include_jc_object_visitor_h_
    21 
    22 #include "Objects.h"
    23 #include "Types.h"
    24 #include "FixedArray.h"
    25 #include "Code.h"
    26 #include "Reference.h"
    27 #include "Modules.h"
    28 #include "Functions.h"
    29 #include "Array.h"
    30 #include "Strings.h"
    31 
    32 namespace ke {
    33 
    34 // An ObjectVisitor is used for traversing object references in the heap. Since
    35 // this pattern must be lightning fast, we avoid virtual dispatch by forcing
    36 // visitors to compose themselves with the visiting logic.
    37 template <typename T>
    38 class ObjectVisitor : public T
    39 {
    40   private:
    41     void visitField(Object *obj, size_t offset) {
    42         this->visit(reinterpret_cast<Object **>(Address(obj) + offset));
    43     }
    44 
    45     size_t iterateType(Object *obj) {
    46         visitField(obj, Type::offsetOfContained());
    47         visitField(obj, Type::offsetOfReturnType());
    48         visitField(obj, Type::offsetOfParameters());
    49         visitField(obj, Type::offsetOfTag());
    50         return sizeof(Type);
    51     }
    52 
    53     size_t iterateFixedArray(FixedArray *array) {
    54         unsigned length = array->length();
    55         Object **ptr = reinterpret_cast<Object **>(array + 1);
    56         for (unsigned i = 0; i < length; i++)
    57             this->visit(ptr + i);
    58         return FixedArray::SizeFor(length);
    59     }
    60 
    61     size_t iterateByteArray(ByteArray *array) {
    62         return ByteArray::SizeFor(array->length());
    63     }
    64 
    65     size_t iterateCode(Object *obj) {
    66         visitField(obj, Code::offsetOfModule());
    67         visitField(obj, Code::offsetOfBytecode());
    68         visitField(obj, Code::offsetOfLocals());
    69         visitField(obj, Code::offsetOfObjects());
    70         visitField(obj, Code::offsetOfSafepoints());
    71         return sizeof(Code);
    72     }
    73 
    74     size_t iterateReference(Object *obj) {
    75         visitField(obj, Reference::offsetOfBase());
    76         return sizeof(Reference);
    77     }
    78 
    79     size_t iterateModule(Object *obj) {
    80         visitField(obj, Module::offsetOfFunctions());
    81         visitField(obj, Module::offsetOfPublics());
    82         return sizeof(Module);
    83     }
    84 
    85     size_t iterateFunction(Object *obj) {
    86         visitField(obj, Function::offsetOfCode());
    87         visitField(obj, Function::offsetOfType());
    88         return sizeof(Function);
    89     }
    90 
    91     size_t iterateString(Object *obj) {
    92         return String::SizeFor(String::cast(obj)->length());
    93     }
    94 
    95     size_t iterateArray(Array *array, ArrayMap *map) {
    96         if (map->type()->isArray()) {
    97             unsigned length = array->nelements();
    98             Object **vec = array->objVec();
    99             for (unsigned i = 0; i < length; i++)
   100                 this->visit(vec + i);
   101         }
   102         return Array::SizeOf(array);
   103     }
   104 
   105     size_t iterateArrayMap(ArrayMap *map) {
   106         visitField(map, ArrayMap::offsetOfType());
   107         visitField(map, ArrayMap::offsetOfContained());
   108         return sizeof(ArrayMap);
   109     }
   110 
   111   public:
   112     size_t iterate(Object *obj) {
   113         visitField(obj, Object::offsetOfMap());
   114         return iterate(obj, obj->map());
   115     }
   116 
   117     size_t iterate(Object *obj, Map *map) {
   118         switch (map->kind()) {
   119           case MapKind_Type:
   120             return iterateType(obj);
   121 
   122           case MapKind_FixedArray:
   123             return iterateFixedArray(FixedArray::cast(obj));
   124 
   125           case MapKind_ByteArray:
   126             return iterateByteArray(ByteArray::cast(obj));
   127 
   128           case MapKind_Code:
   129             return iterateCode(obj);
   130 
   131           case MapKind_Reference:
   132             return iterateReference(obj);
   133 
   134           case MapKind_Module:
   135             return iterateModule(obj);
   136 
   137           case MapKind_Function:
   138             return iterateFunction(obj);
   139 
   140           case MapKind_String:
   141             return iterateString(obj);
   142 
   143           case MapKind_Array:
   144             return Align(iterateArray(Array::cast(obj), ArrayMap::cast(map)), kObjectAlignment);
   145 
   146           case MapKind_MetaArray:
   147             return iterateArrayMap(ArrayMap::cast(obj));
   148 
   149           default:
   150             assert(map->kind() == MapKind_Meta);
   151             return sizeof(Map);
   152         }
   153     }
   154 };
   155 
   156 static inline size_t
   157 SizeOfObject(Object *obj, Map *map)
   158 {
   159     switch (map->kind()) {
   160       case MapKind_Type:
   161         return sizeof(Type);
   162 
   163       case MapKind_FixedArray:
   164         return FixedArray::SizeFor(FixedArray::cast(obj)->length());
   165 
   166       case MapKind_ByteArray:
   167         return ByteArray::SizeFor(ByteArray::cast(obj)->length());
   168 
   169       case MapKind_Code:
   170         return sizeof(Code);
   171 
   172       case MapKind_Reference:
   173         return sizeof(Reference);
   174 
   175       case MapKind_Module:
   176         return sizeof(Module);
   177 
   178       case MapKind_Function:
   179         return sizeof(Function);
   180 
   181       case MapKind_String:
   182         return String::SizeFor(String::cast(obj)->length());
   183 
   184       case MapKind_Array:
   185         return Align(Array::SizeOf(Array::cast(obj)), kObjectAlignment);
   186 
   187       case MapKind_MetaArray:
   188         return sizeof(ArrayMap);
   189 
   190       case MapKind_FreeRegion:
   191         return *reinterpret_cast<size_t *>(reinterpret_cast<Address>(obj + 1));
   192 
   193       default:
   194         assert(map->kind() == MapKind_Meta);
   195         return sizeof(Map);
   196     }
   197 }
   198 
   199 class ObjectSizeEvaluator
   200 {
   201   public:
   202     size_t operator()(Object *obj, Map *map) {
   203         return SizeOfObject(obj, map);
   204     }
   205 };
   206 
   207 }
   208 
   209 #endif // _include_jc_object_visitor_h_