[llvm-commits] [hlvm] r38104 - in /hlvm/trunk: hlvm/AST/AST.h hlvm/Pass/Pass.cpp hlvm/Pass/Pass.h hlvm/Pass/Validate.cpp hlvm/Pass/Validate.h hlvm/Reader/XML/HLVM.rng hlvm/Reader/XML/XMLReader.cpp hlvm/Writer/XML/XMLWriter.cpp tools/hlvm-xml2xml/SConscript

Reid Spencer reid at x10sys.com
Sat Jul 7 16:59:51 PDT 2007


Author: reid
Date: Sat Jul  7 18:59:51 2007
New Revision: 38104

URL: http://llvm.org/viewvc/llvm-project?rev=38104&view=rev
Log:
Implement the Pass and PassManager frameworks. Adjust the Validate pass.
Implement the XMLWriter as a Pass to exercise the framework.

Modified:
    hlvm/trunk/hlvm/AST/AST.h
    hlvm/trunk/hlvm/Pass/Pass.cpp
    hlvm/trunk/hlvm/Pass/Pass.h
    hlvm/trunk/hlvm/Pass/Validate.cpp
    hlvm/trunk/hlvm/Pass/Validate.h
    hlvm/trunk/hlvm/Reader/XML/HLVM.rng
    hlvm/trunk/hlvm/Reader/XML/XMLReader.cpp
    hlvm/trunk/hlvm/Writer/XML/XMLWriter.cpp
    hlvm/trunk/tools/hlvm-xml2xml/SConscript

Modified: hlvm/trunk/hlvm/AST/AST.h
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/AST/AST.h?rev=38104&r1=38103&r2=38104&view=diff

==============================================================================
--- hlvm/trunk/hlvm/AST/AST.h (original)
+++ hlvm/trunk/hlvm/AST/AST.h Sat Jul  7 18:59:51 2007
@@ -31,6 +31,7 @@
 #define HLVM_AST_AST_H
 
 #include <string>
+#include <vector>
 
 /// This namespace is for all HLVM software. It ensures that HLVM software does
 /// not collide with any other software. Hopefully HLVM is not a namespace used
@@ -68,6 +69,14 @@
 /// @brief AST Container Class
 class AST
 {
+  /// @name Types
+  /// @{
+  public:
+    typedef std::vector<Bundle*> NodeList;
+    typedef NodeList::iterator   iterator;
+    typedef NodeList::const_iterator const_iterator;
+
+  /// @}
   /// @name Constructors
   /// @{
   public:
@@ -75,7 +84,7 @@
     static void destroy(AST* ast);
 
   protected:
-    AST() : sysid(), pubid(), root(0) {}
+    AST() : sysid(), pubid(), nodes(0) {}
     ~AST();
 
   /// @}
@@ -84,7 +93,6 @@
   public:
     const std::string& getSystemID() { return sysid; }
     const std::string& getPublicID() { return pubid; }
-    Bundle* getRoot() { return root; }
 
   /// @}
   /// @name Mutators
@@ -92,7 +100,7 @@
   public:
     void setSystemID(const std::string& id) { sysid = id; }
     void setPublicID(const std::string& id) { pubid = id; }
-    void setRoot(Bundle* top) { root = top; }
+    void addBundle(Bundle* b) { nodes.push_back(b); }
 
   /// @}
   /// @name Lookup
@@ -101,6 +109,21 @@
     Type* resolveType(const std::string& name);
 
   /// @}
+  /// @name Iterators
+  /// @{
+  public:
+    iterator           begin()       { return nodes.begin(); }
+    const_iterator     begin() const { return nodes.begin(); }
+    iterator           end  ()       { return nodes.end(); }
+    const_iterator     end  () const { return nodes.end(); }
+    size_t             size () const { return nodes.size(); }
+    bool               empty() const { return nodes.empty(); }
+    Bundle*            front()       { return nodes.front(); }
+    const Bundle*      front() const { return nodes.front(); }
+    Bundle*            back()        { return nodes.back(); }
+    const Bundle*      back()  const { return nodes.back(); }
+
+  /// @}
   /// @name Factories
   /// @{
   public:
@@ -207,7 +230,7 @@
   protected:
     std::string sysid;
     std::string pubid;
-    Bundle* root;
+    NodeList nodes;
   /// @}
 };
 

Modified: hlvm/trunk/hlvm/Pass/Pass.cpp
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/Pass/Pass.cpp?rev=38104&r1=38103&r2=38104&view=diff

==============================================================================
--- hlvm/trunk/hlvm/Pass/Pass.cpp (original)
+++ hlvm/trunk/hlvm/Pass/Pass.cpp Sat Jul  7 18:59:51 2007
@@ -1,4 +1,4 @@
-//===-- AST Pass Classes ----------------------------------------*- C++ -*-===//
+//===-- Pass Interface Class ------------------------------------*- C++ -*-===//
 //
 //                      High Level Virtual Machine (HLVM)
 //
@@ -20,19 +20,129 @@
 // MA 02110-1301 USA
 //
 //===----------------------------------------------------------------------===//
-/// @file hlvm/AST/Pass.cpp
+/// @file hlvm/Pass/Pass.cpp
 /// @author Reid Spencer <rspencer at reidspencer.org> (original author)
 /// @date 2006/05/18
 /// @since 0.1.0
-/// @brief Implements the functions of class hlvm::AST::Pass.
+/// @brief Implements the functions of class hlvm::Pass::Pass.
 //===----------------------------------------------------------------------===//
 
 #include <hlvm/Pass/Pass.h>
+#include <hlvm/AST/AST.h>
+#include <hlvm/AST/Bundle.h>
+#include <hlvm/AST/ContainerType.h>
+#include <hlvm/AST/Program.h>
+#include <hlvm/AST/Variable.h>
 
-namespace hlvm { namespace AST {
+using namespace hlvm;
+
+namespace {
+
+class PassManagerImpl : public PassManager
+{
+public:
+  PassManagerImpl() : PassManager(), begins(), ends(0) {}
+  void addPass(Pass* p);
+  void runOn(AST* tree);
+
+  inline void runIfInterested(Pass* p, Node* n, Pass::PassMode m);
+  inline void runBegins(Node* n);
+  inline void runEnds(Node* n);
+  inline void runOn(Bundle* b);
+
+private:
+  std::vector<Pass*> begins;
+  std::vector<Pass*> ends;
+  Pass* bfsFirst; // First pass for breadth first traversal
+};
+
+void
+PassManagerImpl::addPass(Pass* p)
+{
+  if (p->mode() & Pass::Begin_Mode)
+    begins.push_back(p);
+  if (p->mode() & Pass::End_Mode)
+    ends.push_back(p);
+}
+
+inline void
+PassManagerImpl::runIfInterested(Pass* p, Node* n, Pass::PassMode m)
+{
+  int interest = p->interest();
+  if (interest == 0 ||
+     ((interest & Pass::Type_Interest) && n->isType()) ||
+     ((interest & Pass::Function_Interest) && n->isFunction()) ||
+     ((interest & Pass::Block_Interest) && n->isBlock()) ||
+     ((interest & Pass::Operator_Interest) && n->isOperator()) ||
+     ((interest & Pass::Program_Interest) && n->isProgram()) ||
+     ((interest & Pass::Variable_Interest) && n->isVariable())
+     ) {
+    p->handle(n,m);
+  }
+}
+
+inline void 
+PassManagerImpl::runBegins(Node* n)
+{
+  std::vector<Pass*>::iterator I = begins.begin(), E = begins.end();
+  while (I != E) {
+    runIfInterested(*I,n,Pass::Begin_Mode);
+    ++I;
+  }
+}
+
+inline void 
+PassManagerImpl::runEnds(Node* n)
+{
+  std::vector<Pass*>::iterator I = ends.begin(), E = ends.end();
+  while (I != E) {
+    runIfInterested(*I,n,Pass::End_Mode);
+    ++I;
+  }
+}
+
+inline void 
+PassManagerImpl::runOn(Bundle* b)
+{
+  runBegins(b);
+  for (Bundle::type_iterator I=b->type_begin(), E=b->type_end(); I != E; ++I) {
+    runBegins((*I));
+    runEnds((*I));
+  }
+  for (Bundle::var_iterator I=b->var_begin(), E=b->var_end(); I != E; ++I) {
+    runBegins((*I));
+    runEnds((*I));
+  }
+  for (Bundle::func_iterator I=b->func_begin(), E=b->func_end(); I != E; ++I) {
+    runBegins((*I));
+    runEnds((*I));
+  }
+  runEnds(b);
+}
+
+void PassManagerImpl::runOn(AST* tree)
+{
+  if (begins.empty() && ends.empty())
+    return;
+
+  for (AST::iterator I = tree->begin(), E = tree->end(); I != E; ++I)
+  {
+    runOn(*I);
+  }
+}
+
+} // anonymous namespace
 
 Pass::~Pass()
 {
 }
 
-}}
+PassManager::~PassManager()
+{
+}
+
+PassManager*
+PassManager::create()
+{
+  return new PassManagerImpl;
+}

Modified: hlvm/trunk/hlvm/Pass/Pass.h
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/Pass/Pass.h?rev=38104&r1=38103&r2=38104&view=diff

==============================================================================
--- hlvm/trunk/hlvm/Pass/Pass.h (original)
+++ hlvm/trunk/hlvm/Pass/Pass.h Sat Jul  7 18:59:51 2007
@@ -33,6 +33,7 @@
 namespace hlvm 
 {
 class Node;
+class AST;
 
 /// This class provides an abstract interface to Pass execution. This class
 /// is meant to be subclassed and the various "handle" methods overriden to
@@ -51,13 +52,22 @@
       Operator_Interest = 2,      ///< Pass is interested in Operators
       Function_Interest = 4,      ///< Pass is interested in Functions
       Program_Interest = 8,       ///< Pass is interested in Programs
-      Type_Interest = 16          ///< Pass is interested in Types
+      Type_Interest = 16,         ///< Pass is interested in Types
+      Variable_Interest = 32      ///< Pass is interested in Variables
     };
+
+    enum PassMode {
+      None_Mode = 0,   ///< Pass doesn't want to be called!
+      Begin_Mode = 1,  ///< Call pass at node begin (going down tree)
+      End_Mode = 2,    ///< Call pass at node end (going up tree)
+      Both_Mode = 3    ///< Call pass at both begin and end of node
+    };
+
   /// @}
   /// @name Constructors
   /// @{
   protected:
-    Pass(int interest);
+    Pass(int i, int m) : interest_(i), mode_(m) {}
   public:
     virtual ~Pass();
 
@@ -69,25 +79,34 @@
     /// implementation does nothing. This handler is only called if the
     /// interest is set to 0 (interested in everything). It is left to the
     /// subclass to disambiguate the Node.
-    virtual void handle(Node* n);
+    virtual void handle(Node* n, PassMode mode) = 0;
 
   /// @}
-  /// @name Invocators
+  /// @name Accessors
   /// @{
   public:
-    /// Run the pass with the default interest given during construction.
-    void run();
-
-    /// Run the pass with the interest specified
-    void runWithInterest(int interest);
+    int mode() { return mode_; }
+    int interest() { return interest_; }
 
   /// @}
   /// @name Data
   /// @{
-  protected:
-    int interest;
+  private:
+    int interest_;
+    int mode_;
   /// @}
 };
 
+class PassManager 
+{
+  protected:
+    PassManager() {}
+    virtual ~PassManager();
+  public:
+    static  PassManager* create();
+    virtual void addPass(Pass* p) = 0;
+    virtual void runOn(AST* tree) = 0;
+};
+
 } // hlvm
 #endif

Modified: hlvm/trunk/hlvm/Pass/Validate.cpp
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/Pass/Validate.cpp?rev=38104&r1=38103&r2=38104&view=diff

==============================================================================
--- hlvm/trunk/hlvm/Pass/Validate.cpp (original)
+++ hlvm/trunk/hlvm/Pass/Validate.cpp Sat Jul  7 18:59:51 2007
@@ -28,20 +28,285 @@
 //===----------------------------------------------------------------------===//
 
 #include <hlvm/Pass/Validate.h>
+#include <hlvm/Base/Assert.h>
+#include <hlvm/AST/ContainerType.h>
+#include <hlvm/AST/Bundle.h>
+#include <hlvm/AST/Variable.h>
+#include <hlvm/AST/Program.h>
+#include <llvm/Support/Casting.h>
+#include <iostream>
 
 using namespace hlvm;
+using namespace llvm;
 
 namespace {
 class ValidateImpl : public Validate
 {
   public:
     ValidateImpl() : Validate(0) {}
-    virtual void handle(Node* b);
+    virtual void handle(Node* b,Pass::PassMode m);
+    inline void error(Node*n, const char* msg);
+    inline void validateName(NamedNode* n);
+    inline void validateIntegerType(IntegerType* n);
+    inline void validateRangeType(RangeType* n);
+    inline void validateEnumerationType(EnumerationType* n);
+    inline void validateRealType(RealType* n);
+    inline void validateAliasType(AliasType* n);
+    inline void validatePointerType(PointerType* n);
+    inline void validateArrayType(ArrayType* n);
+    inline void validateVectorType(VectorType* n);
+    inline void validateStructureType(StructureType* n);
+    inline void validateSignatureType(SignatureType* n);
+    inline void validateVariable(Variable* n);
+    inline void validateFunction(Variable* n);
+    inline void validateProgram(Program* n);
+    inline void validateBundle(Bundle* n);
 };
 
+void 
+ValidateImpl::error(Node* n, const char* msg)
+{
+  std::cerr << "Node(" << n << "): " << msg << "\n";
+}
+
+inline void
+ValidateImpl::validateName(NamedNode* n)
+{
+  const std::string& name = n->getName();
+  if (name.empty()) {
+    error(n,"Empty Name");
+  }
+}
+inline void
+ValidateImpl::validateIntegerType(IntegerType* n)
+{
+  validateName(n);
+  if (n->getBits() == 0) {
+    error(n,"Invalid number of bits");
+  }
+}
+
+inline void
+ValidateImpl::validateRangeType(RangeType* n)
+{
+}
+
+inline void
+ValidateImpl::validateEnumerationType(EnumerationType* n)
+{
+}
+
+inline void
+ValidateImpl::validateRealType(RealType* n)
+{
+}
+
+inline void
+ValidateImpl::validateAliasType(AliasType* n)
+{
+}
+
+inline void
+ValidateImpl::validatePointerType(PointerType* n)
+{
+}
+
+inline void
+ValidateImpl::validateArrayType(ArrayType* n)
+{
+}
+
+inline void
+ValidateImpl::validateVectorType(VectorType* n)
+{
+}
+
+inline void
+ValidateImpl::validateStructureType(StructureType* n)
+{
+}
+
+inline void
+ValidateImpl::validateSignatureType(SignatureType* n)
+{
+}
+
+inline void
+ValidateImpl::validateVariable(Variable* n)
+{
+}
+
+inline void
+ValidateImpl::validateFunction(Variable* n)
+{
+}
+
+inline void
+ValidateImpl::validateProgram(Program* n)
+{
+}
+
+inline void
+ValidateImpl::validateBundle(Bundle* n)
+{
+}
+
 void
-ValidateImpl::handle(Node* b)
+ValidateImpl::handle(Node* n,Pass::PassMode m)
 {
+  switch (n->getID())
+  {
+    case NoTypeID:
+      hlvmDeadCode("Invalid Node Kind");
+      break;
+    case VoidTypeID:
+    case AnyTypeID:
+    case BooleanTypeID:
+    case CharacterTypeID:
+    case OctetTypeID:
+      validateName(cast<NamedNode>(n));
+      break; 
+    case IntegerTypeID:
+      validateIntegerType(cast<IntegerType>(n));
+      break;
+    case RangeTypeID:
+      validateRangeType(cast<RangeType>(n));
+      break;
+    case EnumerationTypeID:
+      validateEnumerationType(cast<EnumerationType>(n));
+      break;
+    case RealTypeID:
+      validateRealType(cast<RealType>(n));
+      break;
+    case RationalTypeID:
+      break; // Not implemented yet
+    case StringTypeID:
+      break; // Not imlpemented yet
+    case AliasTypeID:
+      validateAliasType(cast<AliasType>(n));
+      break;
+    case PointerTypeID:
+      validatePointerType(cast<PointerType>(n));
+      break;
+    case ArrayTypeID:
+      validateArrayType(cast<ArrayType>(n));
+      break;
+    case VectorTypeID:
+      validateVectorType(cast<VectorType>(n));
+      break;
+    case StructureTypeID:
+      validateStructureType(cast<StructureType>(n));
+      break;
+    case SignatureTypeID:
+      validateSignatureType(cast<SignatureType>(n));
+      break;
+    case ContinuationTypeID:
+    case OpaqueTypeID:
+    case InterfaceID:
+    case ClassID:
+    case MethodID:
+    case ImplementsID:
+      break; // Not implemented yet
+    case VariableID:
+      validateVariable(cast<Variable>(n));
+      break;
+    case FunctionID:
+      validateFunction(cast<Variable>(n));
+      break;
+    case ProgramID:
+      validateProgram(cast<Program>(n));
+      break;
+    case BundleID:
+      validateBundle(cast<Bundle>(n));
+      break;
+    case BlockID:
+    case ImportID:
+    case CallOpID:
+    case InvokeOpID:
+    case DispatchOpID:
+    case CreateContOpID:
+    case CallWithContOpID:
+    case ReturnOpID:
+    case ThrowOpID:
+    case JumpToOpID:
+    case BreakOpID:
+    case IfOpID:
+    case LoopOpID:
+    case SelectOpID:
+    case WithOpID:
+    case LoadOpID:
+    case StoreOpID:
+    case AllocateOpID:
+    case FreeOpID:
+    case ReallocateOpID:
+    case StackAllocOpID:
+    case ReferenceOpID:
+    case DereferenceOpID:
+    case NegateOpID:
+    case ComplementOpID:
+    case PreIncrOpID:
+    case PostIncrOpID:
+    case PreDecrOpID:
+    case PostDecrOpID:
+    case AddOpID:
+    case SubtractOpID:
+    case MultiplyOpID:
+    case DivideOpID:
+    case ModulusOpID:
+    case BAndOpID:
+    case BOrOpID:
+    case BXOrOpID:
+    case AndOpID:
+    case OrOpID:
+    case NorOpID:
+    case XorOpID:
+    case NotOpID:
+    case LTOpID:
+    case GTOpID:
+    case LEOpID:
+    case GEOpID:
+    case EQOpID:
+    case NEOpID:
+    case IsPInfOpID:
+    case IsNInfOpID:
+    case IsNaNOpID:
+    case TruncOpID:
+    case RoundOpID:
+    case FloorOpID:
+    case CeilingOpID:
+    case PowerOpID:
+    case LogEOpID:
+    case Log2OpID:
+    case Log10OpID:
+    case SqRootOpID:
+    case RootOpID:
+    case FactorialOpID:
+    case GCDOpID:
+    case LCMOpID:
+    case MungeOpID:
+    case LengthOpID:
+    case MapFileOpID:
+    case OpenOpID:
+    case CloseOpID:
+    case ReadOpID:
+    case WriteOpID:
+    case PositionOpID:
+    case IntOpID:
+    case RealOpID:
+    case PInfOpID:
+    case NInfOpID:
+    case NaNOpID:
+    case StringOpID:
+    case ArrayOpID:
+    case VectorOpID:
+    case StructureOpID:
+      break; // Not implemented yet
+    case DocumentationID:
+      break;
+    default:
+      hlvmDeadCode("Invalid Node Kind");
+      break;
+    }
 }
 
 }

Modified: hlvm/trunk/hlvm/Pass/Validate.h
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/Pass/Validate.h?rev=38104&r1=38103&r2=38104&view=diff

==============================================================================
--- hlvm/trunk/hlvm/Pass/Validate.h (original)
+++ hlvm/trunk/hlvm/Pass/Validate.h Sat Jul  7 18:59:51 2007
@@ -44,7 +44,7 @@
   /// @name Constructors
   /// @{
   protected:
-    Validate(int interest) : Pass(interest) {}
+    Validate(int interest) : Pass(interest,true) {}
   public:
     Validate* new_Validate();
     virtual ~Validate();

Modified: hlvm/trunk/hlvm/Reader/XML/HLVM.rng
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/Reader/XML/HLVM.rng?rev=38104&r1=38103&r2=38104&view=diff

==============================================================================
--- hlvm/trunk/hlvm/Reader/XML/HLVM.rng (original)
+++ hlvm/trunk/hlvm/Reader/XML/HLVM.rng Sat Jul  7 18:59:51 2007
@@ -144,7 +144,9 @@
   <!-- HLVM PATTERN -->
   <define name="HLVM.elem">
     <element name="hlvm">
-      <ref name="Bundle.elem"/>
+      <oneOrMore>
+        <ref name="Bundle.elem"/>
+      </oneOrMore>
     </element>
   </define>
 

Modified: hlvm/trunk/hlvm/Reader/XML/XMLReader.cpp
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/Reader/XML/XMLReader.cpp?rev=38104&r1=38103&r2=38104&view=diff

==============================================================================
--- hlvm/trunk/hlvm/Reader/XML/XMLReader.cpp (original)
+++ hlvm/trunk/hlvm/Reader/XML/XMLReader.cpp Sat Jul  7 18:59:51 2007
@@ -554,7 +554,7 @@
   cur = cur->children;
   if (skipBlanks(cur)) {
     Bundle* bundle = parseBundle(cur);
-    ast->setRoot(bundle);
+    ast->addBundle(bundle);
   }
 }
 

Modified: hlvm/trunk/hlvm/Writer/XML/XMLWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/Writer/XML/XMLWriter.cpp?rev=38104&r1=38103&r2=38104&view=diff

==============================================================================
--- hlvm/trunk/hlvm/Writer/XML/XMLWriter.cpp (original)
+++ hlvm/trunk/hlvm/Writer/XML/XMLWriter.cpp Sat Jul  7 18:59:51 2007
@@ -36,6 +36,7 @@
 #include <hlvm/AST/ContainerType.h>
 #include <hlvm/AST/Variable.h>
 #include <hlvm/Base/Assert.h>
+#include <hlvm/Pass/Pass.h>
 #include <llvm/ADT/StringExtras.h>
 #include <libxml/xmlwriter.h>
 #include <iostream>
@@ -46,75 +47,84 @@
 namespace {
 
 class XMLWriterImpl : public XMLWriter {
-  xmlTextWriterPtr writer;
-  AST* node;
 public:
-  XMLWriterImpl(const char* fname)
-    : writer(0), node(0)
-  { 
-    writer = xmlNewTextWriterFilename(fname,0);
-    hlvmAssert(writer && "Can't allocate writer");
-    xmlTextWriterSetIndent(writer,1);
-    xmlTextWriterSetIndentString(writer,reinterpret_cast<const xmlChar*>("  "));
-  }
-
-  virtual ~XMLWriterImpl() 
-  { 
-    xmlFreeTextWriter(writer);
-  }
-
+  XMLWriterImpl(const char* fname) : pass(fname) { }
+  virtual ~XMLWriterImpl() { }
   virtual void write(AST* node);
 
 private:
-  inline void writeComment(const char* cmt)
-    { xmlTextWriterWriteComment(writer,
-        reinterpret_cast<const xmlChar*>(cmt)); }
-  inline void startElement(const char* elem) 
-    { xmlTextWriterStartElement(writer, 
-        reinterpret_cast<const xmlChar*>(elem)); }
-  inline void endElement() 
-    { xmlTextWriterEndElement(writer); }
-  inline void writeAttribute(const char*name, const char*val)
-    { xmlTextWriterWriteAttribute(writer, 
-        reinterpret_cast<const xmlChar*>(name), 
-        reinterpret_cast<const xmlChar*>(val)); }
-  inline void writeAttribute(const char* name, const std::string& val) 
-    { writeAttribute(name, val.c_str()); }
-  inline void writeAttribute(const char* name, Type* t)
-    { writeAttribute(name, t->getName()); }
-  inline void writeAttribute(const char* name, uint64_t val)
-    { writeAttribute(name, llvm::utostr(val)); }
-  inline void writeElement(const char* elem, const char* body)
-    { xmlTextWriterWriteElement(writer,
-        reinterpret_cast<const xmlChar*>(elem),
-        reinterpret_cast<const xmlChar*>(body)); }
-
-  inline void putHeader();
-  inline void putFooter();
-  inline void putDoc(Documentable* node);
-  inline void put(Bundle* b);
-  inline void put(Documentation* b);
-  inline void put(Variable* v);
-  inline void put(Function* f);
-  inline void put(AliasType* t);
-  inline void put(AnyType* t);
-  inline void put(BooleanType* t);
-  inline void put(CharacterType* t);
-  inline void put(IntegerType* t);
-  inline void put(RangeType* t);
-  inline void put(EnumerationType* t);
-  inline void put(RealType* t);
-  inline void put(OctetType* t);
-  inline void put(VoidType* t);
-  inline void put(PointerType* t);
-  inline void put(ArrayType* t);
-  inline void put(VectorType* t);
-  inline void put(StructureType* t);
-  inline void put(SignatureType* t);
+  class WriterPass : public Pass
+  {
+    public:
+      WriterPass(const char* fname) : Pass(0,Pass::Both_Mode) {
+        writer = xmlNewTextWriterFilename(fname,0);
+        hlvmAssert(writer && "Can't allocate writer");
+        xmlTextWriterSetIndent(writer,1);
+        xmlTextWriterSetIndentString(writer,
+            reinterpret_cast<const xmlChar*>("  "));
+      }
+      ~WriterPass() {
+        xmlFreeTextWriter(writer);
+      }
+    inline void writeComment(const char* cmt)
+      { xmlTextWriterWriteComment(writer,
+          reinterpret_cast<const xmlChar*>(cmt)); }
+    inline void startElement(const char* elem) 
+      { xmlTextWriterStartElement(writer, 
+          reinterpret_cast<const xmlChar*>(elem)); }
+    inline void endElement() 
+      { xmlTextWriterEndElement(writer); }
+    inline void writeAttribute(const char*name, const char*val)
+      { xmlTextWriterWriteAttribute(writer, 
+          reinterpret_cast<const xmlChar*>(name), 
+          reinterpret_cast<const xmlChar*>(val)); }
+    inline void writeAttribute(const char* name, const std::string& val) 
+      { writeAttribute(name, val.c_str()); }
+    inline void writeAttribute(const char* name, Type* t)
+      { writeAttribute(name, t->getName()); }
+    inline void writeAttribute(const char* name, uint64_t val)
+      { writeAttribute(name, llvm::utostr(val)); }
+    inline void writeElement(const char* elem, const char* body)
+      { xmlTextWriterWriteElement(writer,
+          reinterpret_cast<const xmlChar*>(elem),
+          reinterpret_cast<const xmlChar*>(body)); }
+
+    inline void putHeader();
+    inline void putFooter();
+    inline void putDoc(Documentable* node);
+    inline void put(Bundle* b);
+    inline void put(Documentation* b);
+    inline void put(Variable* v);
+    inline void put(Function* f);
+    inline void put(AliasType* t);
+    inline void put(AnyType* t);
+    inline void put(BooleanType* t);
+    inline void put(CharacterType* t);
+    inline void put(IntegerType* t);
+    inline void put(RangeType* t);
+    inline void put(EnumerationType* t);
+    inline void put(RealType* t);
+    inline void put(OctetType* t);
+    inline void put(VoidType* t);
+    inline void put(PointerType* t);
+    inline void put(ArrayType* t);
+    inline void put(VectorType* t);
+    inline void put(StructureType* t);
+    inline void put(SignatureType* t);
+
+    virtual void handle(Node* n,Pass::PassMode mode);
+
+  private:
+    xmlTextWriterPtr writer;
+    AST* node;
+    friend class XMLWriterImpl;
+  };
+private:
+  WriterPass pass;
 };
 
 inline void
-XMLWriterImpl::putDoc(Documentable* node)
+XMLWriterImpl::WriterPass::putDoc(Documentable* node)
 {
   Documentation* theDoc = node->getDoc();
   if (theDoc) {
@@ -123,7 +133,7 @@
 }
 
 void
-XMLWriterImpl::putHeader() 
+XMLWriterImpl::WriterPass::putHeader() 
 {
   xmlTextWriterStartDocument(writer,0,"UTF-8",0);
   startElement("hlvm");
@@ -131,19 +141,19 @@
 }
 
 void
-XMLWriterImpl::putFooter()
+XMLWriterImpl::WriterPass::putFooter()
 {
   endElement();
   xmlTextWriterEndDocument(writer);
 }
 
 void
-XMLWriterImpl::put(Function* f)
+XMLWriterImpl::WriterPass::put(Function* f)
 {
 }
 
 void 
-XMLWriterImpl::put(Documentation* b)
+XMLWriterImpl::WriterPass::put(Documentation* b)
 {
   startElement("doc");
   const std::string& data = b->getDoc();
@@ -153,16 +163,15 @@
 }
 
 void 
-XMLWriterImpl::put(AliasType* t)
+XMLWriterImpl::WriterPass::put(AliasType* t)
 {
   startElement("alias");
   writeAttribute("name",t->getName());
   writeAttribute("renames",t->getType());
   putDoc(t);
-  endElement();
 }
 void 
-XMLWriterImpl::put(AnyType* t)
+XMLWriterImpl::WriterPass::put(AnyType* t)
 {
   startElement("atom");
   writeAttribute("name",t->getName());
@@ -170,11 +179,10 @@
   startElement("intrinsic");
   writeAttribute("is","any");
   endElement();
-  endElement();
 }
 
 void
-XMLWriterImpl::put(BooleanType* t)
+XMLWriterImpl::WriterPass::put(BooleanType* t)
 {
   startElement("atom");
   writeAttribute("name",t->getName().c_str());
@@ -182,11 +190,10 @@
   startElement("intrinsic");
   writeAttribute("is","bool");
   endElement();
-  endElement();
 }
 
 void
-XMLWriterImpl::put(CharacterType* t)
+XMLWriterImpl::WriterPass::put(CharacterType* t)
 {
   startElement("atom");
   writeAttribute("name",t->getName().c_str());
@@ -194,11 +201,10 @@
   startElement("intrinsic");
   writeAttribute("is","char");
   endElement();
-  endElement();
 }
 
 void
-XMLWriterImpl::put(IntegerType* t)
+XMLWriterImpl::WriterPass::put(IntegerType* t)
 {
   startElement("atom");
   writeAttribute("name",t->getName().c_str());
@@ -215,22 +221,20 @@
     writeAttribute("bits", llvm::utostr(t->getBits()));
   }
   endElement();
-  endElement();
 }
 
 void
-XMLWriterImpl::put(RangeType* t)
+XMLWriterImpl::WriterPass::put(RangeType* t)
 {
   startElement("range");
   writeAttribute("name",t->getName());
   writeAttribute("min",t->getMin());
   writeAttribute("max",t->getMax());
   putDoc(t);
-  endElement();
 }
 
 void 
-XMLWriterImpl::put(EnumerationType* t)
+XMLWriterImpl::WriterPass::put(EnumerationType* t)
 {
   startElement("enumeration");
   writeAttribute("name",t->getName());
@@ -242,11 +246,10 @@
     writeAttribute("id",*I);
     endElement();
   }
-  endElement();
 }
 
 void
-XMLWriterImpl::put(RealType* t)
+XMLWriterImpl::WriterPass::put(RealType* t)
 {
   startElement("atom");
   writeAttribute("name",t->getName().c_str());
@@ -262,11 +265,10 @@
     writeAttribute("exponent", llvm::utostr(t->getExponent()));
     endElement();
   }
-  endElement();
 }
 
 void
-XMLWriterImpl::put(OctetType* t)
+XMLWriterImpl::WriterPass::put(OctetType* t)
 {
   startElement("atom");
   writeAttribute("name",t->getName().c_str());
@@ -274,11 +276,10 @@
   startElement("intrinsic");
   writeAttribute("is","octet");
   endElement();
-  endElement();
 }
 
 void
-XMLWriterImpl::put(VoidType* t)
+XMLWriterImpl::WriterPass::put(VoidType* t)
 {
   startElement("atom");
   writeAttribute("name",t->getName());
@@ -286,43 +287,39 @@
   startElement("intrinsic");
   writeAttribute("is","void");
   endElement();
-  endElement();
 }
 
 void 
-XMLWriterImpl::put(PointerType* t)
+XMLWriterImpl::WriterPass::put(PointerType* t)
 {
   startElement("pointer");
   writeAttribute("name", t->getName());
   writeAttribute("to", t->getTargetType());
   putDoc(t);
-  endElement();
 }
 
 void 
-XMLWriterImpl::put(ArrayType* t)
+XMLWriterImpl::WriterPass::put(ArrayType* t)
 {
   startElement("array");
   writeAttribute("name", t->getName());
   writeAttribute("of", t->getElementType());
   writeAttribute("length", t->getMaxSize());
   putDoc(t);
-  endElement();
 }
 
 void 
-XMLWriterImpl::put(VectorType* t)
+XMLWriterImpl::WriterPass::put(VectorType* t)
 {
   startElement("vector");
   writeAttribute("name", t->getName());
   writeAttribute("of", t->getElementType());
   writeAttribute("length", t->getSize());
   putDoc(t);
-  endElement();
 }
 
 void 
-XMLWriterImpl::put(StructureType* t)
+XMLWriterImpl::WriterPass::put(StructureType* t)
 {
   startElement("structure");
   writeAttribute("name",t->getName());
@@ -335,11 +332,10 @@
     putDoc(alias);
     endElement();
   }
-  endElement();
 }
 
 void 
-XMLWriterImpl::put(SignatureType* t)
+XMLWriterImpl::WriterPass::put(SignatureType* t)
 {
   startElement("signature");
   writeAttribute("name",t->getName());
@@ -354,65 +350,68 @@
     putDoc(alias);
     endElement();
   }
-  endElement();
 }
 
 void
-XMLWriterImpl::put(Variable* v)
+XMLWriterImpl::WriterPass::put(Variable* v)
 {
   startElement("var");
   writeAttribute("name",v->getName().c_str());
   writeAttribute("type",v->getType()->getName().c_str());
   putDoc(v);
-  endElement();
 }
 
 void 
-XMLWriterImpl::put(Bundle* b)
+XMLWriterImpl::WriterPass::put(Bundle* b)
 {
   startElement("bundle");
   writeAttribute("pubid",b->getName().c_str());
   putDoc(b);
-  for (Bundle::type_const_iterator I = b->type_begin(), E = b->type_end();
-       I != E; ++I)
-  {
-    switch ((*I)->getID()) {
-      case AliasTypeID:        put(cast<AliasType>(*I)); break;
-      case AnyTypeID:          put(cast<AnyType>(*I)); break;
-      case BooleanTypeID:      put(cast<BooleanType>(*I)); break;
-      case CharacterTypeID:    put(cast<CharacterType>(*I)); break;
-      case IntegerTypeID:      put(cast<IntegerType>(*I)); break;
-      case RangeTypeID:        put(cast<RangeType>(*I)); break;
-      case EnumerationTypeID:  put(cast<EnumerationType>(*I)); break;
-      case RealTypeID:         put(cast<RealType>(*I)); break;
-      case OctetTypeID:        put(cast<OctetType>(*I)); break;
-      case VoidTypeID:         put(cast<VoidType>(*I)); break;
-      case PointerTypeID:      put(cast<PointerType>(*I)); break;
-      case ArrayTypeID:        put(cast<ArrayType>(*I)); break;
-      case VectorTypeID:       put(cast<VectorType>(*I)); break;
-      case StructureTypeID:    put(cast<StructureType>(*I)); break;
-      case SignatureTypeID:    put(cast<SignatureType>(*I)); break;
+}
+
+void
+XMLWriterImpl::WriterPass::handle(Node* n,Pass::PassMode mode)
+{
+  if (mode & Pass::Begin_Mode) {
+    switch (n->getID()) 
+    {
+      case AliasTypeID:        put(cast<AliasType>(n)); break;
+      case AnyTypeID:          put(cast<AnyType>(n)); break;
+      case BooleanTypeID:      put(cast<BooleanType>(n)); break;
+      case BundleID:           put(cast<Bundle>(n)); break;
+      case CharacterTypeID:    put(cast<CharacterType>(n)); break;
+      case IntegerTypeID:      put(cast<IntegerType>(n)); break;
+      case RangeTypeID:        put(cast<RangeType>(n)); break;
+      case EnumerationTypeID:  put(cast<EnumerationType>(n)); break;
+      case RealTypeID:         put(cast<RealType>(n)); break;
+      case OctetTypeID:        put(cast<OctetType>(n)); break;
+      case VoidTypeID:         put(cast<VoidType>(n)); break;
+      case PointerTypeID:      put(cast<PointerType>(n)); break;
+      case ArrayTypeID:        put(cast<ArrayType>(n)); break;
+      case VectorTypeID:       put(cast<VectorType>(n)); break;
+      case StructureTypeID:    put(cast<StructureType>(n)); break;
+      case SignatureTypeID:    put(cast<SignatureType>(n)); break;
+      case VariableID:         put(cast<Variable>(n)); break;
+      case FunctionID:         put(cast<Function>(n)); break;
       default:
         hlvmDeadCode("Unknown Type");
         break;
     }
   }
-  for (Bundle::var_const_iterator I = b->var_begin(), E = b->var_end();
-       I != E; ++I)
-    put(cast<Variable>(*I));
-  for (Bundle::func_const_iterator I = b->func_begin(), E = b->func_end();
-       I != E; ++I)
-    put(cast<Function>(*I));
-  endElement();
+  if (mode & Pass::End_Mode) {
+    endElement();
+  }
 }
 
 void
 XMLWriterImpl::write(AST* ast) 
 {
-  node = ast;
-  putHeader();
-  put(ast->getRoot());
-  putFooter();
+  pass.node = ast;
+  pass.putHeader();
+  PassManager* PM = PassManager::create();
+  PM->addPass(&pass);
+  PM->runOn(ast);
+  pass.putFooter();
 }
 
 }

Modified: hlvm/trunk/tools/hlvm-xml2xml/SConscript
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/tools/hlvm-xml2xml/SConscript?rev=38104&r1=38103&r2=38104&view=diff

==============================================================================
--- hlvm/trunk/tools/hlvm-xml2xml/SConscript (original)
+++ hlvm/trunk/tools/hlvm-xml2xml/SConscript Sat Jul  7 18:59:51 2007
@@ -26,6 +26,7 @@
  LIBS=[
    'HLVMXMLReader',
    'HLVMXMLWriter',
+   'HLVMPass',
    'HLVMAST',
    'HLVMBase',
    'xml2',





More information about the llvm-commits mailing list