[llvm-commits] [vmkit] r51851 - in /vmkit/trunk: include/mvm/CollectableArea.h include/mvm/Method.h include/mvm/MvmMemoryManager.h include/mvm/Object.h include/mvm/PrintBuffer.h lib/Mvm/MvmMemoryManager.cpp lib/Mvm/Object.cpp

Nicolas Geoffray nicolas.geoffray at lip6.fr
Sun Jun 1 15:32:31 PDT 2008


Author: geoffray
Date: Sun Jun  1 17:32:30 2008
New Revision: 51851

URL: http://llvm.org/viewvc/llvm-project?rev=51851&view=rev
Log:
Simplify code.
Make the memory manager global (for now).
Add comments.


Removed:
    vmkit/trunk/include/mvm/CollectableArea.h
Modified:
    vmkit/trunk/include/mvm/Method.h
    vmkit/trunk/include/mvm/MvmMemoryManager.h
    vmkit/trunk/include/mvm/Object.h
    vmkit/trunk/include/mvm/PrintBuffer.h
    vmkit/trunk/lib/Mvm/MvmMemoryManager.cpp
    vmkit/trunk/lib/Mvm/Object.cpp

Removed: vmkit/trunk/include/mvm/CollectableArea.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/include/mvm/CollectableArea.h?rev=51850&view=auto

==============================================================================
--- vmkit/trunk/include/mvm/CollectableArea.h (original)
+++ vmkit/trunk/include/mvm/CollectableArea.h (removed)
@@ -1,75 +0,0 @@
-//===----------- CollectableArea.h - Collectable memory -------------------===//
-//
-//                     The Micro Virtual Machine
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef MVM_COLLECTABLE_AREA_H
-#define MVM_COLLECTABLE_AREA_H
-
-#include "mvm/Object.h"
-#include "mvm/PrintBuffer.h"
-
-namespace mvm {
-
-template <class T>
-class CollectableArea : public Object {
-public:
-  
-  static VirtualTable* VT;
-  unsigned int firstIndex;
-  unsigned int lastIndex;
-  unsigned int capacity;
-
-  inline T **elements() { return (T **)(this + 1); }
-
-  inline T *setAt(T *o, unsigned int idx) { 
-    return (T *)gcset(elements() + idx, o); 
-  }
-  
-  inline T *getAt(unsigned int idx) { return elements()[idx]; }
-
-  static inline CollectableArea *alloc(size_t cap, size_t st, size_t e) {
-    CollectableArea *res = 
-      (CollectableArea *)gc::operator new(sizeof(CollectableArea) + cap<<2, VT);
-    res->firstIndex = st;
-    res->lastIndex = e;
-    res->capacity = cap;
-    return res;
-  }
-
-  inline CollectableArea *realloc(size_t new_cap) {
-    CollectableArea *res = 
-      (CollectableArea *)gc::realloc(sizeof(CollectableArea) + new_cap<<2);
-    res->capacity = new_cap;
-    return res;
-  }
-
-  virtual void print(PrintBuffer *buf) {
-     CollectableArea *const self= (CollectableArea *)this;
-     buf->write("");
-    for (size_t idx= self->firstIndex; idx < self->lastIndex; ++idx) {   
-      if (idx > self->firstIndex)
-        buf->write(" ");
-      buf->writeObj(self->getAt(idx));
-    }   
-  }
-
-
-  virtual void TRACER {
-    CollectableArea *const self= (CollectableArea *)this;
-    register T **e = self->elements();
-    size_t lim= self->lastIndex;
-    size_t idx= self->firstIndex;
-    lim = ((lim << 2) < sz) ? lim : 0;
-    idx = ((idx << 2) < sz) ? idx : (sz >> 2); 
-    for (; idx < lim; ++idx)
-      e[idx]->MARK_AND_TRACE;
-  }
-};
-
-} // end namespace mvm
-#endif // MVM_COLLECTABLE_AREA_H

Modified: vmkit/trunk/include/mvm/Method.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/include/mvm/Method.h?rev=51851&r1=51850&r2=51851&view=diff

==============================================================================
--- vmkit/trunk/include/mvm/Method.h (original)
+++ vmkit/trunk/include/mvm/Method.h Sun Jun  1 17:32:30 2008
@@ -1,4 +1,4 @@
-//===-------------- Method.h - Collectable methods ------------------------===//
+//===--------- Method.h - Meta information for methods --------------------===//
 //
 //                     The Micro Virtual Machine
 //
@@ -14,7 +14,16 @@
 
 namespace mvm {
 
+/// Code - This class contains meta information of a dynamicall generated
+/// function.
+///
 class Code {
+private:
+  
+  /// metaInfo - A meta info field to store virtual machine informations.
+  ///
+  void* metaInfo;
+
 public:
 
   Code() {
@@ -26,19 +35,42 @@
     exceptionTable = 0;
     metaInfo = 0;
   }
-
+  
+  /// stubSize - The size of the stub that will generate the function.
+  ///
   unsigned stubSize;
+  
+  /// FunctionStart - The starting address in memory of the generated function.
+  ///
   unsigned char* FunctionStart;
+
+  /// FunctionStart - The ending address in memory of the generated function.
+  ///
   unsigned char* FunctionEnd;
+
+  /// stub - The starting address of the stub.
+  ///
   unsigned char* stub;
-  unsigned char* frameRegister;
+
+  /// exceptionTable - The exception table of this function.
+  ///
   unsigned char* exceptionTable;
+  
+  /// frameRegister - The address where to register and unregister the
+  /// exception table.
+  unsigned char* frameRegister;
 
-  void* metaInfo;
 
+  /// getMetaInfo - Returns the virtual machine information related to this
+  /// method.
   void* getMetaInfo() { return metaInfo; }
+  
+  /// setMetaInfo - Sets the virtual machine information of this method.
+  ///
   void  setMetaInfo(void* m) { metaInfo = m; }
 
+  /// unregisterFrame - Unregisters the exception table of this method.
+  ///
   inline void unregisterFrame() {
     __deregister_frame((unsigned char*)frameRegister);
   }

Modified: vmkit/trunk/include/mvm/MvmMemoryManager.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/include/mvm/MvmMemoryManager.h?rev=51851&r1=51850&r2=51851&view=diff

==============================================================================
--- vmkit/trunk/include/mvm/MvmMemoryManager.h (original)
+++ vmkit/trunk/include/mvm/MvmMemoryManager.h Sun Jun  1 17:32:30 2008
@@ -13,9 +13,6 @@
 
 #include <map>
 
-#include "MvmGC.h"
-#include "mvm/Threads/Locks.h"
-#include "llvm/Module.h"
 #include <llvm/ExecutionEngine/JITMemoryManager.h>
 
 using namespace llvm;
@@ -24,39 +21,26 @@
 
 class Code;
 
+/// MvmMemoryManager - This class is a wrapper to the default JITMemoryManager
+/// in LLVM. It creates Code objects for backtraces and getting virtual machine
+/// information out of dynamically generated native code.
+///
 class MvmMemoryManager : public JITMemoryManager {
-  Code* currentMethod;       // Current method being compiled
-  unsigned char *GOTBase;      // Target Specific reserved memory
-#ifdef MULTIPLE_GC
-  std::map<const Module*, Collector*> GCMap;
-  mvm::Lock* lock;
-#endif
+  
+  /// currentMethod -  Current method being compiled.
+  ///
+  Code* currentMethod;
+  
+  /// realMemoryManager - The real allocator 
   JITMemoryManager* realMemoryManager;
+
 public:
   
   MvmMemoryManager() : JITMemoryManager() { 
-      GOTBase = 0; 
-#ifdef MULTIPLE_GC
-      lock = mvm::Lock::allocNormal();
-#endif
-      realMemoryManager= JITMemoryManager::CreateDefaultMemManager();
+    realMemoryManager = JITMemoryManager::CreateDefaultMemManager();
   }
-   ~MvmMemoryManager() { 
-      delete[] GOTBase;
-#ifdef MULTIPLE_GC
-      delete lock;
-#endif
-  }
-
-#ifdef MULTIPLE_GC 
-   void addGCForModule(Module* M, Collector* GC){
-      lock->lock();
-      GCMap[M] = GC;
-      lock->unlock();
-   }
-#endif
-
-   /// startFunctionBody - When we start JITing a function, the JIT calls this 
+  
+  /// startFunctionBody - When we start JITing a function, the JIT calls this 
   /// method to allocate a block of free RWX memory, which returns a pointer to
   /// it.  The JIT doesn't know ahead of time how much space it will need to
   /// emit the function, so it doesn't pass in the size.  Instead, this method

Modified: vmkit/trunk/include/mvm/Object.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/include/mvm/Object.h?rev=51851&r1=51850&r2=51851&view=diff

==============================================================================
--- vmkit/trunk/include/mvm/Object.h (original)
+++ vmkit/trunk/include/mvm/Object.h Sun Jun  1 17:32:30 2008
@@ -24,69 +24,54 @@
 #define VT_TRACER_OFFSET 3
 #define VT_PRINT_OFFSET 4
 #define VT_HASHCODE_OFFSET 5
-#define VT_SIZE 24
+#define VT_SIZE 6 * sizeof(void*)
 
 
-#define GC_defass(TYPE, name)                                  \
-  TYPE    *_hidden_##name;                                     \
-	inline TYPE    *name()        { return _hidden_##name; }     \
-	inline TYPE    *name(TYPE *v) { return _hidden_##name = v; }
-
 class PrintBuffer;
-class Object;
 
+/// Object - Root of all objects. Define default implementations of virtual
+/// methods and some commodity functions.
+///
 class Object : public gc {
 public:
-  static VirtualTable* VT; 
   
+  /// getVirtualTable - Returns the virtual table of this object.
+  ///
   VirtualTable* getVirtualTable() const {
     return ((VirtualTable**)(this))[0];
   }
   
+  /// setVirtualTable - Sets the virtual table of this object.
+  ///
   void setVirtualTable(VirtualTable* VT) {
     ((VirtualTable**)(this))[0] = VT;
   }
 
+  /// printString - Returns a string representation of this object.
+  ///
   char *printString(void) const;
 
-#if !defined(GC_GEN)
-  inline gc *gcset(void *ptr, gc *src) { return *((gc **)ptr) = src; }
-#endif
-  
+  /// destroyer - Default implementation of destroyer. Does nothing.
+  ///
   virtual void      destroyer(size_t) {}
-  virtual void      TRACER {}
-  virtual void      print(PrintBuffer *buf) const;
-  virtual intptr_t  hashCode(){ return (intptr_t)this;}
 
-protected:
-  static Object **rootTable;
-  static int	   rootTableSize, rootTableLimit;
+  /// tracer - Default implementation of tracer. Does nothing.
+  ///
+  virtual void      TRACER {}
 
-  static void growRootTable(void);
+  /// print - Default implementation of print.
+  ///
+  virtual void      print(PrintBuffer *buf) const;
 
-public:
+  /// hashCode - Default implementation of hashCode. Returns the address
+  /// of this object.
+  ///
+  virtual intptr_t  hashCode(){ return (intptr_t)this;}
   
-  inline static void pushRoot(Object *obj) {
-    if (rootTableSize >= rootTableLimit)
-      growRootTable();
-    rootTable[rootTableSize++]= obj;
-  }
-
-  inline static void pushRoot(Object &var) {
-    pushRoot(var);
-  }
-
-  inline static Object *popRoots(size_t nRoots) {
-    rootTableSize-= nRoots;
-    assert(rootTableSize >= 0);
-    return rootTable[rootTableSize];
-  }
-
-  inline static Object *popRoot(void) {
-    return popRoots(1);
-  }
-
-  static void markAndTraceRoots(void*);
+  /// initialise - All virtual machines must call Object::initialise to
+  /// property initialise the PrintBuffer and NativeString classes. These
+  /// classes are mainly used for debugging but also for object hashing.
+  ///
   static void initialise();
 
 };

Modified: vmkit/trunk/include/mvm/PrintBuffer.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/include/mvm/PrintBuffer.h?rev=51851&r1=51850&r2=51851&view=diff

==============================================================================
--- vmkit/trunk/include/mvm/PrintBuffer.h (original)
+++ vmkit/trunk/include/mvm/PrintBuffer.h Sun Jun  1 17:32:30 2008
@@ -10,21 +10,30 @@
 #ifndef MVM_PRINTBUFFER_H
 #define MVM_PRINTBUFFER_H
 
-#include <stdio.h>
-#include <string.h>
+#include <stdio.h> // sprintf
+#include <string.h> // memcpy
 
 #include "types.h"
 #include "mvm/Object.h"
 
 namespace mvm {
 
+
+/// NativeString - This class is the equivalent of a char*, but allocated
+/// by the GC, hence has a virtual table.
+///
 class NativeString : public Object {
 public:
   
+  /// VT - The virtual table of this class.
+  ///
   static VirtualTable* VT;
 
+  /// cString - Returns the C equivalent of the NativeString.
+  ///
   inline char *cString() { return (char *)(this + 1); }
 
+  /// readString - Copies the C string to a newly allocated NativeString.
   inline static NativeString *readString(char *cStr) {
     size_t nbb = strlen(cStr);
     NativeString * res = alloc(nbb + 1);
@@ -32,60 +41,86 @@
     return res;
   }
 
+  /// alloc - Allocates a NativeString of size len.
+  ///
   static inline NativeString *alloc(size_t len) {
     return (NativeString *)gc::operator new(len, VT);
   }
 
+  /// realloc - Reallocate a native string of size len.
+  ///
   inline NativeString *realloc(size_t len) {
     return (NativeString *)gc::realloc(len);
   }
 
+  /// setAt - Sets the char c at position pos in the NativeString.
+  ///
   inline void setAt(int pos, char c) {
     cString()[pos] = c;
   }
 
 public:
+  
+  /// print - Just prints the NativeString.
+  ///
   virtual void print(PrintBuffer *buf) const;
 
-  inline bool compare(char *str) {
-    return !strcmp(cString(), str);
-  }
-
-  inline bool compare(char *str, int len) {
-    return ((int)strlen(cString()) == len) && !strncmp(cString(), str, len);
-  }
 };
 
-
+/// PrintBuffer - This class is a buffered string.
+///
 class PrintBuffer : public Object {
-public:
-  static VirtualTable* VT;
-  size_t  capacity;
-  size_t  writePosition;
-  GC_defass(NativeString, contents);
+private:
+ 
+  /// _contents - The buffer.
+  ///
+  NativeString* _contents;
+
+  /// capacity - The capacity of the current buffer.
+  ///
+  uint32  capacity;
+
+  /// writePosition - The position in the buffer where the next write will
+  /// happen.
+  ///
+  uint32  writePosition;
+
 
 public:
+  
+  /// VT - The virtual table of this class.
+  ///
+  static VirtualTable* VT;
+  
+  
+  /// contents - Returns the buffer.
+  ///
+  NativeString* contents() {
+    return _contents;
+  }
 
-  static inline PrintBuffer* allocPrintBuffer(void) {
-    PrintBuffer* pbf = gc_new(PrintBuffer)();
-    pbf->capacity= 32;
-    pbf->writePosition= 0;
-    pbf->contents(NativeString::alloc(pbf->capacity));
-    return pbf;
+  /// setContents - Sets the buffer.
+  ///
+  void setContents(NativeString* n) {
+    _contents = n;
   }
 
+  /// write - Writes to this PrintBuffer.
+  ///
   inline PrintBuffer *write(const char *string) {
     size_t len= strlen(string);
     if ((writePosition + len + 1) >= capacity) {
       while ((writePosition + len + 1) >= capacity)
         capacity*= 4;
-      contents(contents()->realloc(capacity));
+      setContents(contents()->realloc(capacity));
     }
     strcpy(contents()->cString() + writePosition, string);
     writePosition+= len;
     return this;
   }
   
+
+  /// writeChar - Writes a char.
   inline PrintBuffer *writeChar(char v) {
     char buf[32];
     sprintf(buf, "%c", v);
@@ -93,30 +128,35 @@
   }
   
 
+  /// writeChar - Writes a int32.
   inline PrintBuffer *writeS4(int v) {
     char buf[32];
     sprintf(buf, "%d", v);
     return write(buf);
   }
   
+  /// writes8 - Writes a int64.
   inline PrintBuffer *writeS8(sint64 v) {
     char buf[32];
     sprintf(buf, "%lld", v);
     return write(buf);
   }
   
+  /// writeFP - Writes a double.
   inline PrintBuffer *writeFP(double v) {
     char buf[32];
     sprintf(buf, "%f", v);
     return write(buf);
   }
 
+  /// writePtr - Writes a pointer.
   inline PrintBuffer *writePtr(void *p) {
     char buf[32];
     sprintf(buf, "%p", p);
     return write(buf);
   }
 
+  /// writeBytes - Writes len bytes in the buffer bytes.
   inline PrintBuffer *writeBytes(unsigned char *bytes, size_t len) {
     write("[");
     for (size_t idx= 0; idx < len; ++idx) {
@@ -130,15 +170,24 @@
     return this;
   }
 
+  /// writeObj - Writes an Object to the buffer.
+  ///
   PrintBuffer *writeObj(const Object *);
 
-  NativeString      *getContents();
-  
-  static PrintBuffer *write_static(PrintBuffer*, char *);
-
 public:
-  static PrintBuffer *alloc(void);
+  
+  /// alloc - Allocates a default PrintBuffer.
+  ///
+  static PrintBuffer *alloc(void) {
+    PrintBuffer* pbf = gc_new(PrintBuffer)();
+    pbf->capacity= 32;
+    pbf->writePosition= 0;
+    pbf->setContents(NativeString::alloc(pbf->capacity));
+    return pbf;
+  }
 
+  /// tracer - Traces this PrintBuffer.
+  ///
   virtual void TRACER;
 };
 

Modified: vmkit/trunk/lib/Mvm/MvmMemoryManager.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/Mvm/MvmMemoryManager.cpp?rev=51851&r1=51850&r2=51851&view=diff

==============================================================================
--- vmkit/trunk/lib/Mvm/MvmMemoryManager.cpp (original)
+++ vmkit/trunk/lib/Mvm/MvmMemoryManager.cpp Sun Jun  1 17:32:30 2008
@@ -54,13 +54,11 @@
 }
 
 void MvmMemoryManager::AllocateGOT() {
-  assert(GOTBase == 0 && "Cannot allocate the got multiple times");
-  GOTBase = new unsigned char[sizeof(void*) * 8192];
-  HasGOT = true;
+  realMemoryManager->AllocateGOT();
 }
 
 unsigned char *MvmMemoryManager::getGOTBase() const {
-  return GOTBase;
+  return realMemoryManager->getGOTBase();
 }
 
 unsigned char *MvmMemoryManager::startExceptionTable(const Function* F, 

Modified: vmkit/trunk/lib/Mvm/Object.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/Mvm/Object.cpp?rev=51851&r1=51850&r2=51851&view=diff

==============================================================================
--- vmkit/trunk/lib/Mvm/Object.cpp (original)
+++ vmkit/trunk/lib/Mvm/Object.cpp Sun Jun  1 17:32:30 2008
@@ -19,46 +19,17 @@
 using namespace mvm;
 
 
-VirtualTable *Object::VT = 0;
 VirtualTable *NativeString::VT = 0;
 VirtualTable *PrintBuffer::VT = 0;
 
 mvm::Key<mvm::Thread>* mvm::Thread::threadKey = 0;
 
 
-Object **Object::rootTable= 0;
-int    Object::rootTableSize= 0;
-int    Object::rootTableLimit= 0;
-
-void Object::growRootTable(void) {
-  if (rootTableLimit != 0) {
-    assert(rootTable != 0);
-    rootTableLimit*= 2;
-    rootTable= (Object **)
-      ::realloc((void *)rootTable, rootTableLimit * sizeof(Object **));
-    return;
-  }
-  rootTableLimit= 32;
-  rootTable= (Object **)malloc(32 * sizeof(Object **));
-}
-
-
-void Object::markAndTraceRoots(void* GC) {
-  for (int i= 0; i < rootTableSize; ++i) {
-#ifdef MULTIPLE_GC
-    rootTable[i]->markAndTrace((Collector*)GC);
-#else
-    rootTable[i]->markAndTrace();;
-#endif
-  }
-}
-
 void Object::initialise() {
 # define INIT(X) { \
   X fake; \
   X::VT = ((void**)(void*)(&fake))[0]; }
   
-  INIT(Object);
   INIT(NativeString);
   INIT(PrintBuffer);
   
@@ -69,10 +40,6 @@
   ((PrintBuffer *)this)->contents()->MARK_AND_TRACE;
 }
 
-PrintBuffer *PrintBuffer::alloc(void) {
-  return allocPrintBuffer();
-}
-
 
 PrintBuffer *PrintBuffer::writeObj(const Object *obj) {
 #ifdef MULTIPLE_GC
@@ -147,5 +114,3 @@
   }
   buf->write("\"");
 }
-
-NativeString *PrintBuffer::getContents() { return contents(); }





More information about the llvm-commits mailing list