Added root and object tracing.
authorDavid Anderson <dvander@alliedmods.net>
Tue Jul 03 09:25:18 2012 -0700 (2012-07-03)
changeset 4525b995bc75a0
parent 44 c920f9328716
child 46 735503e448a0
Added root and object tracing.
jitcraft/Barriers.h
jitcraft/ObjectVisitor.h
jitcraft/RootScopes.cpp
jitcraft/RootScopes.h
jitcraft/TypeManager.cpp
jitcraft/TypeManager.h
jitcraft/VirtualObjectVisitor.h
jitcraft/Zone.cpp
jitcraft/Zone.h
     1.1 --- a/jitcraft/Barriers.h	Tue Jul 03 08:30:09 2012 -0700
     1.2 +++ b/jitcraft/Barriers.h	Tue Jul 03 09:25:18 2012 -0700
     1.3 @@ -18,9 +18,9 @@
     1.4  #ifndef _include_jitcraft_barriers_h_
     1.5  #define _include_jitcraft_barriers_h_
     1.6  
     1.7 -class Object;
     1.8 +namespace jc {
     1.9  
    1.10 -namespace jc {
    1.11 +class Object;
    1.12  
    1.13  // Used to store unbarriered heap locations.
    1.14  template <typename T>
    1.15 @@ -44,8 +44,8 @@
    1.16          obj_ = obj;
    1.17          return *this;
    1.18      }
    1.19 -    Object **addr() {
    1.20 -        return &obj_;
    1.21 +    Object **address() {
    1.22 +        return reinterpret_cast<Object **>(&obj_);
    1.23      }
    1.24      operator bool () const {
    1.25          return !!obj_;
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/jitcraft/ObjectVisitor.h	Tue Jul 03 09:25:18 2012 -0700
     2.3 @@ -0,0 +1,143 @@
     2.4 +/* vim: set ts=4 sw=4 tw=99 et:
     2.5 + *
     2.6 + * Copyright (C) 2012 David Anderson
     2.7 + *
     2.8 + * This file is part of JITCraft.
     2.9 + *
    2.10 + * JITCraft is free software: you can redistribute it and/or modify it under
    2.11 + * the terms of the GNU General Public License as published by the Free
    2.12 + * Software Foundation, either version 3 of the License, or (at your option)
    2.13 + * any later version.
    2.14 + * 
    2.15 + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    2.16 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    2.17 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    2.18 + *
    2.19 + * You should have received a copy of the GNU General Public License along with
    2.20 + * JITCraft. If not, see http://www.gnu.org/licenses/.
    2.21 + */
    2.22 +#ifndef _include_jc_object_visitor_h_
    2.23 +#define _include_jc_object_visitor_h_
    2.24 +
    2.25 +#include "Objects.h"
    2.26 +#include "Types.h"
    2.27 +#include "FixedArray.h"
    2.28 +#include "Code.h"
    2.29 +#include "Reference.h"
    2.30 +#include "Modules.h"
    2.31 +#include "Functions.h"
    2.32 +#include "Array.h"
    2.33 +
    2.34 +namespace jc {
    2.35 +
    2.36 +// An ObjectVisitor is used for traversing object references in the heap. Since
    2.37 +// this pattern must be lightning fast, we avoid virtual dispatch by forcing
    2.38 +// visitors to compose themselves with the visiting logic.
    2.39 +template <typename T>
    2.40 +class ObjectVisitor : public T
    2.41 +{
    2.42 +  private:
    2.43 +    void visitField(Object *obj, size_t offset) {
    2.44 +        this->visit(reinterpret_cast<Object **>(Address(obj) + offset));
    2.45 +    }
    2.46 +
    2.47 +    size_t iterateType(Object *obj) {
    2.48 +        visitField(obj, Type::offsetOfContained());
    2.49 +        visitField(obj, Type::offsetOfReturnType());
    2.50 +        visitField(obj, Type::offsetOfParameters());
    2.51 +        return sizeof(Type);
    2.52 +    }
    2.53 +
    2.54 +    size_t iterateFixedArray(FixedArray *array) {
    2.55 +        unsigned length = array->length();
    2.56 +        Object **ptr = reinterpret_cast<Object **>(array + 1);
    2.57 +        for (unsigned i = 0; i < length; i++)
    2.58 +            this->visit(ptr + i);
    2.59 +        return FixedArray::SizeFor(length);
    2.60 +    }
    2.61 +
    2.62 +    size_t iterateByteArray(ByteArray *array) {
    2.63 +        return ByteArray::SizeFor(array->length());
    2.64 +    }
    2.65 +
    2.66 +    size_t iterateCode(Object *obj) {
    2.67 +        visitField(obj, Code::offsetOfModule());
    2.68 +        visitField(obj, Code::offsetOfBytecode());
    2.69 +        visitField(obj, Code::offsetOfLocals());
    2.70 +        visitField(obj, Code::offsetOfObjects());
    2.71 +        return sizeof(Code);
    2.72 +    }
    2.73 +
    2.74 +    size_t iterateReference(Object *obj) {
    2.75 +        visitField(obj, Reference::offsetOfBase());
    2.76 +        return sizeof(Reference);
    2.77 +    }
    2.78 +
    2.79 +    size_t iterateModule(Object *obj) {
    2.80 +        visitField(obj, Module::offsetOfFunctions());
    2.81 +        return sizeof(Module);
    2.82 +    }
    2.83 +
    2.84 +    size_t iterateFunction(Object *obj) {
    2.85 +        visitField(obj, Module::offsetOfModule());
    2.86 +        visitField(obj, Module::offsetOfCode());
    2.87 +        visitField(obj, Module::offsetOfType());
    2.88 +        return sizeof(Function);
    2.89 +    }
    2.90 +
    2.91 +    size_t iterateArray(Array *array, ArrayMap *map) {
    2.92 +        if (map->type()->isArray()) {
    2.93 +            unsigned length = array->nelements();
    2.94 +            Object **vec = array->objVec();
    2.95 +            for (unsigned i = 0; i < length; i++)
    2.96 +                this->visit(vec + i);
    2.97 +        }
    2.98 +        return Array::SizeOf(array);
    2.99 +    }
   2.100 +
   2.101 +    size_t iterateArrayMap(ArrayMap *map) {
   2.102 +        visitField(obj, ArrayMap::offsetOfType());
   2.103 +        visitFIeld(obj, ArrayMap::offsetOfContained());
   2.104 +        return sizeof(ArrayMap);
   2.105 +    }
   2.106 +
   2.107 +  public:
   2.108 +    size_t iterate(Object *obj, Map *map) {
   2.109 +        switch (map->kind()) {
   2.110 +          case MapKind_Type:
   2.111 +            return iterateType(obj);
   2.112 +
   2.113 +          case MapKind_FixedArray:
   2.114 +            return iterateFixedArray(FixedArray::cast(obj));
   2.115 +
   2.116 +          case MapKind_ByteArray:
   2.117 +            return iterateByteArray(ByteArray::cast(obj));
   2.118 +
   2.119 +          case MapKind_Code:
   2.120 +            return iterateCode(obj);
   2.121 +
   2.122 +          case MapKind_Reference:
   2.123 +            return iterateReference(obj);
   2.124 +
   2.125 +          case MapKind_Module:
   2.126 +            return iterateModule(obj);
   2.127 +
   2.128 +          case MapKind_Function:
   2.129 +            return iterateFunction(obj);
   2.130 +
   2.131 +          case MapKind_Array:
   2.132 +            return iterateArray(Array::cast(obj), ArrayMap::cast(map));
   2.133 +
   2.134 +          case MapKind_MetaArray:
   2.135 +            return iterateArrayMap(obj);
   2.136 +
   2.137 +          default:
   2.138 +            assert(map->kind() == MapKind_Meta);
   2.139 +            break;
   2.140 +        }
   2.141 +    }
   2.142 +};
   2.143 +
   2.144 +}
   2.145 +
   2.146 +#endif // _include_jc_object_visitor_h_
     3.1 --- a/jitcraft/RootScopes.cpp	Tue Jul 03 08:30:09 2012 -0700
     3.2 +++ b/jitcraft/RootScopes.cpp	Tue Jul 03 09:25:18 2012 -0700
     3.3 @@ -64,3 +64,13 @@
     3.4      blocks_->cur = pos;
     3.5  }
     3.6  
     3.7 +void
     3.8 +RootScopeManager::accept(VirtualObjectVisitor *visitor)
     3.9 +{
    3.10 +    Block *block = blocks_;
    3.11 +    while (block) {
    3.12 +        for (Object **ptr = block->objects; ptr < block->cur; ptr++)
    3.13 +            visitor->visit(ptr);
    3.14 +        block = block->prev;
    3.15 +    }
    3.16 +}
     4.1 --- a/jitcraft/RootScopes.h	Tue Jul 03 08:30:09 2012 -0700
     4.2 +++ b/jitcraft/RootScopes.h	Tue Jul 03 09:25:18 2012 -0700
     4.3 @@ -20,6 +20,7 @@
     4.4  #define _include_jc_root_scopes_h_
     4.5  
     4.6  #include "Objects.h"
     4.7 +#include "VirtualObjectVisitor.h"
     4.8  
     4.9  namespace jc {
    4.10  
    4.11 @@ -84,6 +85,8 @@
    4.12          return &scope_;
    4.13      }
    4.14  
    4.15 +    void accept(VirtualObjectVisitor *visitor);
    4.16 +
    4.17    private:
    4.18      Block *blocks_;
    4.19      Block first_;
     5.1 --- a/jitcraft/TypeManager.cpp	Tue Jul 03 08:30:09 2012 -0700
     5.2 +++ b/jitcraft/TypeManager.cpp	Tue Jul 03 09:25:18 2012 -0700
     5.3 @@ -85,3 +85,13 @@
     5.4      // :TODO: cache this.
     5.5      return Type::NewFunction(zone_, returnType, args);
     5.6  }
     5.7 +
     5.8 +void
     5.9 +TypeManager::accept(VirtualObjectVisitor *visitor)
    5.10 +{
    5.11 +    for (unsigned i = 0; i < PrimitiveTypes_Total; i++) {
    5.12 +        visitor->visit(primitiveTypes_[i].address());
    5.13 +        visitor->visit(referenceTypes_[i].address());
    5.14 +    }
    5.15 +}
    5.16 +
     6.1 --- a/jitcraft/TypeManager.h	Tue Jul 03 08:30:09 2012 -0700
     6.2 +++ b/jitcraft/TypeManager.h	Tue Jul 03 09:25:18 2012 -0700
     6.3 @@ -20,6 +20,7 @@
     6.4  
     6.5  #include "Barriers.h"
     6.6  #include "Types.h"
     6.7 +#include "VirtualObjectVisitor.h"
     6.8  
     6.9  namespace jc {
    6.10  
    6.11 @@ -44,6 +45,8 @@
    6.12      Type *getReference(Handle<Type> type);
    6.13      Type *unsafeArray(Handle<Type> contained, int elements);
    6.14      Type *newFunction(Handle<Type> returnType, Handle<FixedArray> args);
    6.15 +
    6.16 +    void accept(VirtualObjectVisitor *visitor);
    6.17  };
    6.18  
    6.19  }
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/jitcraft/VirtualObjectVisitor.h	Tue Jul 03 09:25:18 2012 -0700
     7.3 @@ -0,0 +1,34 @@
     7.4 +/* vim: set ts=4 sw=4 tw=99 et:
     7.5 + *
     7.6 + * Copyright (C) 2012 David Anderson
     7.7 + *
     7.8 + * This file is part of JITCraft.
     7.9 + *
    7.10 + * JITCraft is free software: you can redistribute it and/or modify it under
    7.11 + * the terms of the GNU General Public License as published by the Free
    7.12 + * Software Foundation, either version 3 of the License, or (at your option)
    7.13 + * any later version.
    7.14 + * 
    7.15 + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    7.16 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    7.17 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    7.18 + *
    7.19 + * You should have received a copy of the GNU General Public License along with
    7.20 + * JITCraft. If not, see http://www.gnu.org/licenses/.
    7.21 + */
    7.22 +#ifndef _include_jc_object_visitor_h_
    7.23 +#define _include_jc_object_visitor_h_
    7.24 +
    7.25 +namespace jc {
    7.26 +
    7.27 +class Object;
    7.28 +
    7.29 +class VirtualObjectVisitor
    7.30 +{
    7.31 +  public:
    7.32 +    virtual void visit(Object **ptr) = 0;
    7.33 +};
    7.34 +
    7.35 +}
    7.36 +
    7.37 +#endif // _include_jc_object_visitor_h_
     8.1 --- a/jitcraft/Zone.cpp	Tue Jul 03 08:30:09 2012 -0700
     8.2 +++ b/jitcraft/Zone.cpp	Tue Jul 03 09:25:18 2012 -0700
     8.3 @@ -1,4 +1,5 @@
     8.4 -/*
     8.5 +/* vim: set ts=4 sw=4 tw=99 et:
     8.6 + *
     8.7   * Copyright (C) 2012 David Anderson
     8.8   *
     8.9   * This file is part of JITCraft.
    8.10 @@ -71,3 +72,20 @@
    8.11      map->init(maps_[parent]);
    8.12      return map;
    8.13  }
    8.14 +
    8.15 +void
    8.16 +Zone::accept(VirtualObjectVisitor *visitor)
    8.17 +{
    8.18 +    for (unsigned i = 0; i < MapKinds_Total; i++)
    8.19 +        visitor->visit(maps_[i].address());
    8.20 +    types()->accept(visitor);
    8.21 +
    8.22 +    Local<Object> *local = lastLocalRoot_;
    8.23 +    while (local) {
    8.24 +        visitor->visit(local->address());
    8.25 +        local = local->prev();
    8.26 +    }
    8.27 +
    8.28 +    rootScopes_.accept(visitor);
    8.29 +}
    8.30 +
     9.1 --- a/jitcraft/Zone.h	Tue Jul 03 08:30:09 2012 -0700
     9.2 +++ b/jitcraft/Zone.h	Tue Jul 03 09:25:18 2012 -0700
     9.3 @@ -25,6 +25,7 @@
     9.4  #include "Barriers.h"
     9.5  #include "Handles.h"
     9.6  #include "RootScopes.h"
     9.7 +#include "VirtualObjectVisitor.h"
     9.8  
     9.9  namespace jc {
    9.10  
    9.11 @@ -80,6 +81,8 @@
    9.12      RootScopeManager &rootScopes() {
    9.13          return rootScopes_;
    9.14      }
    9.15 +
    9.16 +    void accept(VirtualObjectVisitor *visitor);
    9.17  };
    9.18  
    9.19  // In a multi-threaded system, this would be a TLS variable.