Temporary checkin of rooting stuff (breaks build).
authorDavid Anderson <dvander@alliedmods.net>
Mon Jul 02 14:04:29 2012 -0700 (2012-07-02)
changeset 2728697d267222
parent 26 a48382113da6
child 28 a9665857cfd5
Temporary checkin of rooting stuff (breaks build).
jitcraft/Array.cpp
jitcraft/Array.h
jitcraft/BytecodeCompiler.cpp
jitcraft/Handles.h
jitcraft/Heap-inl.h
jitcraft/Heap.h
jitcraft/Interpreter.cpp
jitcraft/Zone.h
     1.1 --- a/jitcraft/Array.cpp	Mon Jul 02 01:52:57 2012 -0700
     1.2 +++ b/jitcraft/Array.cpp	Mon Jul 02 14:04:29 2012 -0700
     1.3 @@ -23,7 +23,7 @@
     1.4  using namespace jc;
     1.5  
     1.6  ArrayMap *
     1.7 -ArrayMap::New(Zone *zone, Type *type, ArrayMap *contained)
     1.8 +ArrayMap::New(Zone *zone, Handle<Type> type, Handle<ArrayMap> contained)
     1.9  {
    1.10      // :TODO: use Local
    1.11      Map *obj = zone->allocateMap(MapKind_Array, sizeof(ArrayMap), MapKind_MetaArray);
    1.12 @@ -50,7 +50,7 @@
    1.13  }
    1.14  
    1.15  Array *
    1.16 -Array::New(Zone *zone, ArrayMap *map, uint32 elements, Heap::Tenure tenure)
    1.17 +Array::New(Zone *zone, Handle<ArrayMap> map, uint32 elements, Heap::Tenure tenure)
    1.18  {
    1.19      // :TODO: handles
    1.20      size_t bytes = SizeFor(map->type(), elements);
     2.1 --- a/jitcraft/Array.h	Mon Jul 02 01:52:57 2012 -0700
     2.2 +++ b/jitcraft/Array.h	Mon Jul 02 14:04:29 2012 -0700
     2.3 @@ -19,6 +19,7 @@
     2.4  #define _include_jitcraft_array_h_
     2.5  
     2.6  #include "Heap.h"
     2.7 +#include "Handles.h"
     2.8  #include "Types.h"
     2.9  
    2.10  namespace jc {
    2.11 @@ -33,7 +34,7 @@
    2.12      void *elements_;
    2.13  
    2.14    public:
    2.15 -    static Array *New(Zone *zone, ArrayMap *map, uint32 elements,
    2.16 +    static Array *New(Zone *zone, Handle<ArrayMap> map, uint32 elements,
    2.17                        Heap::Tenure = Heap::Tenure_Default);
    2.18  
    2.19      uint32 nelements() {
    2.20 @@ -97,7 +98,7 @@
    2.21      Barriered<ArrayMap> contained_;
    2.22  
    2.23    public:
    2.24 -    static ArrayMap *New(Zone *zone, Type *type, ArrayMap *contained);
    2.25 +    static ArrayMap *New(Zone *zone, Handle<Type> type, Handle<ArrayMap> contained);
    2.26  
    2.27      Type *type() {
    2.28          return type_;
     3.1 --- a/jitcraft/BytecodeCompiler.cpp	Mon Jul 02 01:52:57 2012 -0700
     3.2 +++ b/jitcraft/BytecodeCompiler.cpp	Mon Jul 02 14:04:29 2012 -0700
     3.3 @@ -527,12 +527,13 @@
     3.4  }
     3.5  
     3.6  static inline ArrayMap *
     3.7 -BuildArrayMap(Zone *zone, Type *type)
     3.8 +BuildArrayMap(Zone *zone, Handle<Type> type)
     3.9  {
    3.10      // :TODO: use local
    3.11 -    ArrayMap *contained = NULL;
    3.12 +    Local<ArrayMap> contained(zone);
    3.13      if (type->levels() > 1) {
    3.14 -        contained = BuildArrayMap(zone, type->contained());
    3.15 +        Local<Type> containedType(zone, type->contained());
    3.16 +        contained = BuildArrayMap(zone, containedType);
    3.17          if (!contained)
    3.18              return NULL;
    3.19      }
    3.20 @@ -543,12 +544,13 @@
    3.21  void
    3.22  BytecodeCompiler::visit(StringLiteral *node)
    3.23  {
    3.24 -    // :TODO: use Local
    3.25 -    ArrayMap *map = BuildArrayMap(ZONE(), node->type());
    3.26 +    // :TODO: remove this Local
    3.27 +    Local<Type> type(ZONE(), node->type());
    3.28 +    Local<ArrayMap> map(ZONE(), BuildArrayMap(ZONE(), type));
    3.29      if (!map)
    3.30          return;
    3.31  
    3.32 -    Array *array = Array::New(ZONE(), map, node->string()->length(), Heap::Tenure_Old);
    3.33 +    Local<Array> array(ZONE(), Array::New(ZONE(), map, node->string()->length(), Heap::Tenure_Old));
    3.34      if (!array)
    3.35          return;
    3.36  
     4.1 --- a/jitcraft/Handles.h	Mon Jul 02 01:52:57 2012 -0700
     4.2 +++ b/jitcraft/Handles.h	Mon Jul 02 14:04:29 2012 -0700
     4.3 @@ -122,6 +122,46 @@
     4.4      Local &operator =(const Local &other);
     4.5  };
     4.6  
     4.7 +// A ScopedRoot is a special kind of handle for storing references on the
     4.8 +// heap in bulk.
     4.9 +template <typename T>
    4.10 +class ScopedRoot
    4.11 +{
    4.12 +    T *thingp_;
    4.13 +
    4.14 +  private:
    4.15 +    inline T **make(const T *&other);
    4.16 +    inline void assign(const T *&other) {
    4.17 +        if (!thingp_)
    4.18 +            thingp_ = make(other);
    4.19 +        else
    4.20 +            *thingp_ = other;
    4.21 +    }
    4.22 +
    4.23 +  public:
    4.24 +    ScopedRoot()
    4.25 +    { }
    4.26 +    inline ScopedRoot(const T *&thing);
    4.27 +    inline ScopedRoot(const ScopedRoot<T> &other);
    4.28 +    ScopedRoot &operator =(const ScopedRoot<T> &other) {
    4.29 +        assign(*other);
    4.30 +        return *this;
    4.31 +    }
    4.32 +    ScopedRoot &operator =(const T *&other) {
    4.33 +        assign(other);
    4.34 +        return *this;
    4.35 +    }
    4.36 +    T *operator ->() const {
    4.37 +        return operator *();
    4.38 +    }
    4.39 +    T *operator *() const {
    4.40 +        return thing_;
    4.41 +    }
    4.42 +    operator bool () const {
    4.43 +        return !!thing_;
    4.44 +    }
    4.45 +};
    4.46 +
    4.47  template <typename T> template <typename S> inline
    4.48  Handle<T>::Handle(const Local<S> &local)
    4.49  {
    4.50 @@ -129,6 +169,33 @@
    4.51      thingp_ = reinterpret_cast<T * const*>(local.addr());
    4.52  }
    4.53  
    4.54 +// A RootScope is necessary for using ScopedRoot<>.
    4.55 +class RootScope
    4.56 +{
    4.57 +    friend class Zone;
    4.58 +
    4.59 +    struct Block
    4.60 +    {
    4.61 +        static const size_t kItemsPerBlock = 4096 / sizeof(Object *);
    4.62 +
    4.63 +        Object **base;
    4.64 +        Object **ptr;
    4.65 +        Object **end;
    4.66 +        Block *prev;
    4.67 +        
    4.68 +        Block(Block *prev, Object **base)
    4.69 +          : base(base),
    4.70 +            ptr(base),
    4.71 +            end(base + kItemsPerBlock),
    4.72 +            prev(prev)
    4.73 +        {
    4.74 +        }
    4.75 +    };
    4.76 +
    4.77 +  private:
    4.78 +    
    4.79 +};
    4.80 +
    4.81  }
    4.82  
    4.83  #endif // _include_jitcraft_rooting_h_
     5.1 --- a/jitcraft/Heap-inl.h	Mon Jul 02 01:52:57 2012 -0700
     5.2 +++ b/jitcraft/Heap-inl.h	Mon Jul 02 14:04:29 2012 -0700
     5.3 @@ -83,6 +83,18 @@
     5.4      return InitAddressIntoObject(address, map);
     5.5  }
     5.6  
     5.7 +inline Object *
     5.8 +Heap::allocate(Handle<Map> map, size_t bytes, Tenure age)
     5.9 +{
    5.10 +    size_t actualBytes = Align(bytes, kObjectAlignment);
    5.11 +    Address address = newObject(actualBytes, age);
    5.12 +    if (!address) {
    5.13 +        assert(false);
    5.14 +        return NULL;
    5.15 +    }
    5.16 +    return InitAddressIntoObject(address, map);
    5.17 +}
    5.18 +
    5.19  inline Address
    5.20  Heap::newStackObject(size_t bytes)
    5.21  {
     6.1 --- a/jitcraft/Heap.h	Mon Jul 02 01:52:57 2012 -0700
     6.2 +++ b/jitcraft/Heap.h	Mon Jul 02 14:04:29 2012 -0700
     6.3 @@ -156,6 +156,7 @@
     6.4      bool initialize();
     6.5  
     6.6      inline Object *allocate(Map *map, size_t byes, Tenure pretenure);
     6.7 +    inline Object *allocate(Handle<Map> map, size_t byes, Tenure pretenure);
     6.8      Map *allocateMap(MapKind kind, size_t bytes);
     6.9  
    6.10      Map *metaMap() {
     7.1 --- a/jitcraft/Interpreter.cpp	Mon Jul 02 01:52:57 2012 -0700
     7.2 +++ b/jitcraft/Interpreter.cpp	Mon Jul 02 14:04:29 2012 -0700
     7.3 @@ -80,7 +80,7 @@
     7.4  
     7.5  // Generate an array with completely static sizes.
     7.6  static Array *
     7.7 -GenerateArray(Zone *zone, ArrayMap *map)
     7.8 +GenerateArray(Zone *zone, Handle<ArrayMap> map)
     7.9  {
    7.10      assert(map->type()->elements() != -1);
    7.11  
    7.12 @@ -92,8 +92,9 @@
    7.13      if (map->type()->levels() == 1)
    7.14          return array;
    7.15  
    7.16 +    Local<ArrayMap> contained(zone, map->contained());
    7.17      for (unsigned i = 0; i < unsigned(map->type()->elements()); i++) {
    7.18 -        Array *sub = GenerateArray(zone, map->contained());
    7.19 +        Array *sub = GenerateArray(zone, contained);
    7.20          if (!sub)
    7.21              return NULL;
    7.22          array->writeObject(zone, i, sub);
    7.23 @@ -473,7 +474,7 @@
    7.24  case OP_NEWARRAY_LIFO:
    7.25  {
    7.26      // :TODO: use Local
    7.27 -    ArrayMap *map = ArrayMap::cast(frame->code()->map(READ_UINT32(top.pc)));
    7.28 +    Local<ArrayMap> map(ZONE(), ArrayMap::cast(frame->code()->map(READ_UINT32(top.pc))));
    7.29      Array *array = GenerateArray(ZONE(), map);
    7.30      if (!array) {
    7.31          assert(false);
     8.1 --- a/jitcraft/Zone.h	Mon Jul 02 01:52:57 2012 -0700
     8.2 +++ b/jitcraft/Zone.h	Mon Jul 02 14:04:29 2012 -0700
     8.3 @@ -22,6 +22,7 @@
     8.4  #include "Stack.h"
     8.5  #include "Heap.h"
     8.6  #include "Barriers.h"
     8.7 +#include "Handles.h"
     8.8  
     8.9  namespace jc {
    8.10  
    8.11 @@ -38,6 +39,9 @@
    8.12  
    8.13      TypeManager *types_;
    8.14  
    8.15 +    // Root list.
    8.16 +    Local<Object> *lastThingLocal_;
    8.17 +
    8.18    public:
    8.19      Zone();
    8.20      bool initialize();
    8.21 @@ -59,6 +63,9 @@
    8.22      Object *allocate(Map *map, size_t bytes, Heap::Tenure tenure = Heap::Tenure_Default) {
    8.23          return heap_.allocate(map, bytes, tenure);
    8.24      }
    8.25 +    Object *allocate(Handle<Map> map, size_t bytes, Heap::Tenure tenure = Heap::Tenure_Default) {
    8.26 +        return heap_.allocate(map, bytes, tenure);
    8.27 +    }
    8.28      Map *allocateMap(MapKind kind, size_t bytes, MapKind parent = MapKind_Meta);
    8.29      TypeManager *types() const {
    8.30          return types_;