[llvm-commits] [hlvm] r38106 - in /hlvm/trunk: hlvm/AST/Block.h hlvm/Pass/Pass.cpp hlvm/Pass/Pass.h hlvm/Pass/Validate.cpp hlvm/Pass/Validate.h hlvm/Writer/XML/XMLWriter.cpp tools/hlvm-xml2xml/hlvm-xml2xml.cpp

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


Author: reid
Date: Sat Jul  7 18:59:52 2007
New Revision: 38106

URL: http://llvm.org/viewvc/llvm-project?rev=38106&view=rev
Log:
Pass changes:
1. Don't publicly declare the interface of the Validate pass. There is no need.
2. Change PassMode to have a more meaningful name: TraversalKinds with 
   enumerators that indicate the PreOrder or PostOrder traversal being 
   requested. Thanks to David Koontz for this idea.
3. Implement traversal of functions, blocks and operators
4. Make "Block" be an operator so that we can nest blocks in blocks. Also,
   logically, a block is an operator that returns the result of the last
   operator in the block.


Removed:
    hlvm/trunk/hlvm/Pass/Validate.h
Modified:
    hlvm/trunk/hlvm/AST/Block.h
    hlvm/trunk/hlvm/Pass/Pass.cpp
    hlvm/trunk/hlvm/Pass/Pass.h
    hlvm/trunk/hlvm/Pass/Validate.cpp
    hlvm/trunk/hlvm/Writer/XML/XMLWriter.cpp
    hlvm/trunk/tools/hlvm-xml2xml/hlvm-xml2xml.cpp

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

==============================================================================
--- hlvm/trunk/hlvm/AST/Block.h (original)
+++ hlvm/trunk/hlvm/AST/Block.h Sat Jul  7 18:59:52 2007
@@ -30,20 +30,18 @@
 #ifndef HLVM_AST_BLOCK_H
 #define HLVM_AST_BLOCK_H
 
-#include <hlvm/AST/Node.h>
+#include <hlvm/AST/Operator.h>
 
 namespace hlvm 
 {
 
-class Operator; // Forward declare
-
 /// This class represents an Variable in the HLVM Abstract Syntax Tree.  
 /// A Variable is a storage location of a specific type. It can either be
 /// global or local, depending on its parent. Global variables are always
 /// contained in a Bundle. Local variables are always contained in a
 /// Function.
 /// @brief HLVM AST Variable Node
-class Block : public Node
+class Block : public Operator
 {
   /// @name Types
   /// @{
@@ -56,7 +54,7 @@
   /// @name Constructors
   /// @{
   public:
-    Block() : Node(BlockID), ops() {}
+    Block() : Operator(BlockID), ops() {}
     virtual ~Block();
 
   /// @}
@@ -64,6 +62,7 @@
   /// @{
   public:
     static inline bool classof(const Block*) { return true; }
+    static inline bool classof(const Operator* O) { return O->isBlock(); }
     static inline bool classof(const Node* N) { return N->isBlock(); }
 
   /// @}

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

==============================================================================
--- hlvm/trunk/hlvm/Pass/Pass.cpp (original)
+++ hlvm/trunk/hlvm/Pass/Pass.cpp Sat Jul  7 18:59:52 2007
@@ -35,38 +35,40 @@
 #include <hlvm/AST/Variable.h>
 
 using namespace hlvm;
+using namespace llvm;
 
 namespace {
 
 class PassManagerImpl : public PassManager
 {
 public:
-  PassManagerImpl() : PassManager(), begins(), ends(0) {}
+  PassManagerImpl() : PassManager(), pre(), post() {}
   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 runIfInterested(Pass* p, Node* n, Pass::TraversalKinds m);
+  inline void runPreOrder(Node* n);
+  inline void runPostOrder(Node* n);
+  inline void runOn(Operator* b);
+  inline void runOn(Block* b);
   inline void runOn(Bundle* b);
 
 private:
-  std::vector<Pass*> begins;
-  std::vector<Pass*> ends;
-  Pass* bfsFirst; // First pass for breadth first traversal
+  std::vector<Pass*> pre;
+  std::vector<Pass*> post;
 };
 
 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);
+  if (p->mode() & Pass::PreOrderTraversal)
+    pre.push_back(p);
+  if (p->mode() & Pass::PostOrderTraversal)
+    post.push_back(p);
 }
 
 inline void
-PassManagerImpl::runIfInterested(Pass* p, Node* n, Pass::PassMode m)
+PassManagerImpl::runIfInterested(Pass* p, Node* n, Pass::TraversalKinds m)
 {
   int interest = p->interest();
   if (interest == 0 ||
@@ -82,53 +84,77 @@
 }
 
 inline void 
-PassManagerImpl::runBegins(Node* n)
+PassManagerImpl::runPreOrder(Node* n)
 {
-  std::vector<Pass*>::iterator I = begins.begin(), E = begins.end();
+  std::vector<Pass*>::iterator I = pre.begin(), E = pre.end();
   while (I != E) {
-    runIfInterested(*I,n,Pass::Begin_Mode);
+    runIfInterested(*I,n,Pass::PreOrderTraversal);
     ++I;
   }
 }
 
 inline void 
-PassManagerImpl::runEnds(Node* n)
+PassManagerImpl::runPostOrder(Node* n)
 {
-  std::vector<Pass*>::iterator I = ends.begin(), E = ends.end();
+  std::vector<Pass*>::iterator I = post.begin(), E = post.end();
   while (I != E) {
-    runIfInterested(*I,n,Pass::End_Mode);
+    runIfInterested(*I,n,Pass::PostOrderTraversal);
     ++I;
   }
 }
 
+inline void
+PassManagerImpl::runOn(Operator* op)
+{
+  runPreOrder(op);
+  runPostOrder(op);
+}
+
+inline void
+PassManagerImpl::runOn(Block* b)
+{
+  runPreOrder(b);
+  for (Block::iterator I = b->begin(), E = b->end(); I != E; ++I) {
+    if (isa<Block>(*I))
+      runOn(cast<Block>(*I)); // recurse!
+    else
+      runOn(cast<Operator>(*I)); 
+  }
+  runPostOrder(b);
+}
+
 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));
+  runPreOrder(b);
+  for (Bundle::type_iterator TI =b->type_begin(), TE = b->type_end(); 
+       TI != TE; ++TI) {
+    runPreOrder(*TI);
+    runPostOrder(*TI);
+  }
+  for (Bundle::var_iterator VI = b->var_begin(), VE = b->var_end(); 
+       VI != VE; ++VI) {
+    runPreOrder(*VI);
+    runPostOrder(*VI);
+  }
+  for (Bundle::func_iterator FI = b->func_begin(), FE = b->func_end(); 
+       FI != FE; ++FI) {
+    runPreOrder(*FI);
+    runOn((*FI)->getBlock());
+    runPostOrder(*FI);
   }
-  runEnds(b);
+  runPostOrder(b);
 }
 
 void PassManagerImpl::runOn(AST* tree)
 {
-  if (begins.empty() && ends.empty())
+  // Just a little optimization for empty pass managers
+  if (pre.empty() && post.empty())
     return;
 
+  // Traverse each of the bundles in the AST node.
   for (AST::iterator I = tree->begin(), E = tree->end(); I != E; ++I)
-  {
     runOn(*I);
-  }
 }
 
 } // anonymous namespace

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

==============================================================================
--- hlvm/trunk/hlvm/Pass/Pass.h (original)
+++ hlvm/trunk/hlvm/Pass/Pass.h Sat Jul  7 18:59:52 2007
@@ -46,7 +46,7 @@
   public:
     /// Or these together and pass to the constructor to indicate which
     /// kinds of things you are interested in.
-    enum PassInterest {
+    enum InterestKinds {
       All_Interest = 0,           ///< Pass is interested in everything
       Block_Interest = 1,         ///< Pass is interested in Blocks
       Operator_Interest = 2,      ///< Pass is interested in Operators
@@ -56,21 +56,30 @@
       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
+    /// Specifies the kinds of traversals that a pass can request. The
+    /// PassManager always walks the tree in a depth-first search (DFS) and it
+    /// can call the pass either when it arrives at the node (preorder) on the
+    /// way down, or when it leaves the node (postorder) on the way up. A pass
+    /// can also specify that it wants both pre-order and post-order traversal.
+    enum TraversalKinds {
+      NoTraversal = 0,             ///< Pass doesn't want to be called!
+      PreOrderTraversal = 1,       ///< Call pass on the way down the DFS walk
+      PostOrderTraversal = 2,      ///< Call pass on the way up the DFS walk
+      PreAndPostOrderTraversal = 3 ///< Call pass on both the way down and up
     };
 
   /// @}
   /// @name Constructors
   /// @{
   protected:
-    Pass(int i, int m) : interest_(i), mode_(m) {}
+    Pass(int i, TraversalKinds m) : interest_(i), mode_(m) {}
   public:
     virtual ~Pass();
 
+    /// Instantiate the standard passes
+    static Pass* new_ValidatePass();
+    static Pass* new_ResolveTypesPass();
+
   /// @}
   /// @name Handlers
   /// @{
@@ -79,21 +88,21 @@
     /// 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, PassMode mode) = 0;
+    virtual void handle(Node* n, TraversalKinds mode) = 0;
 
   /// @}
   /// @name Accessors
   /// @{
   public:
-    int mode() { return mode_; }
-    int interest() { return interest_; }
+    int mode()     const { return mode_; }
+    int interest() const { return interest_; }
 
   /// @}
   /// @name Data
   /// @{
   private:
     int interest_;
-    int mode_;
+    TraversalKinds mode_;
   /// @}
 };
 

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

==============================================================================
--- hlvm/trunk/hlvm/Pass/Validate.cpp (original)
+++ hlvm/trunk/hlvm/Pass/Validate.cpp Sat Jul  7 18:59:52 2007
@@ -27,7 +27,7 @@
 /// @brief Implements the functions of class hlvm::Pass::Validate.
 //===----------------------------------------------------------------------===//
 
-#include <hlvm/Pass/Validate.h>
+#include <hlvm/Pass/Pass.h>
 #include <hlvm/Base/Assert.h>
 #include <hlvm/AST/ContainerType.h>
 #include <hlvm/AST/Bundle.h>
@@ -40,11 +40,11 @@
 using namespace llvm;
 
 namespace {
-class ValidateImpl : public Validate
+class ValidateImpl : public Pass
 {
   public:
-    ValidateImpl() : Validate(0) {}
-    virtual void handle(Node* b,Pass::PassMode m);
+    ValidateImpl() : Pass(0,Pass::PostOrderTraversal) {}
+    virtual void handle(Node* b,Pass::TraversalKinds k);
     inline void error(Node*n, const char* msg);
     inline void validateName(NamedNode* n);
     inline void validateIntegerType(IntegerType* n);
@@ -152,7 +152,7 @@
 }
 
 void
-ValidateImpl::handle(Node* n,Pass::PassMode m)
+ValidateImpl::handle(Node* n,Pass::TraversalKinds k)
 {
   switch (n->getID())
   {
@@ -311,11 +311,8 @@
 
 }
 
-Validate::~Validate()
-{
-}
-
-Validate* Validate::create()
+Pass* 
+Pass::new_ValidatePass()
 {
   return new ValidateImpl();
 }

Removed: hlvm/trunk/hlvm/Pass/Validate.h
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/Pass/Validate.h?rev=38105&view=auto

==============================================================================
--- hlvm/trunk/hlvm/Pass/Validate.h (original)
+++ hlvm/trunk/hlvm/Pass/Validate.h (removed)
@@ -1,56 +0,0 @@
-//===-- Abstract Syntax Tree Validation Pass --------------------*- C++ -*-===//
-//
-//                      High Level Virtual Machine (HLVM)
-//
-// Copyright (C) 2006 Reid Spencer. All Rights Reserved.
-//
-// This software is free software; you can redistribute it and/or modify it 
-// under the terms of the GNU Lesser General Public License as published by 
-// the Free Software Foundation; either version 2.1 of the License, or (at 
-// your option) any later version.
-//
-// This software is distributed in the hope that it will be useful, but WITHOUT
-// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-// FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for 
-// more details.
-//
-// You should have received a copy of the GNU Lesser General Public License 
-// along with this library in the file named LICENSE.txt; if not, write to the 
-// Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
-// MA 02110-1301 USA
-//
-//===----------------------------------------------------------------------===//
-/// @file hlvm/Pass/Validate.h
-/// @author Reid Spencer <rspencer at reidspencer.org> (original author)
-/// @date 2006/05/19
-/// @since 0.1.0
-/// @brief Declares the class hlvm::Pass::Validate
-//===----------------------------------------------------------------------===//
-
-#ifndef HLVM_PASS_VALIDATE_H
-#define HLVM_PASS_VALIDATE_H
-
-#include <hlvm/Pass/Pass.h>
-
-namespace hlvm 
-{
-
-/// This class provides a type resolution capability. It searches for 
-/// instances of OpaqueType and resolves all their uses to the correct actual
-/// type.
-/// @brief Type Resolution Pass
-class Validate : public Pass 
-{
-  /// @name Constructors
-  /// @{
-  protected:
-    Validate(int interest) : Pass(interest,true) {}
-  public:
-    static Validate* create();
-    virtual ~Validate();
-
-  /// @}
-};
-
-} // hlvm
-#endif

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

==============================================================================
--- hlvm/trunk/hlvm/Writer/XML/XMLWriter.cpp (original)
+++ hlvm/trunk/hlvm/Writer/XML/XMLWriter.cpp Sat Jul  7 18:59:52 2007
@@ -56,7 +56,7 @@
   class WriterPass : public Pass
   {
     public:
-      WriterPass(const char* fname) : Pass(0,Pass::Both_Mode) {
+      WriterPass(const char* fname) : Pass(0,Pass::PreAndPostOrderTraversal) {
         writer = xmlNewTextWriterFilename(fname,0);
         hlvmAssert(writer && "Can't allocate writer");
         xmlTextWriterSetIndent(writer,1);
@@ -112,7 +112,7 @@
     inline void put(StructureType* t);
     inline void put(SignatureType* t);
 
-    virtual void handle(Node* n,Pass::PassMode mode);
+    virtual void handle(Node* n,Pass::TraversalKinds mode);
 
   private:
     xmlTextWriterPtr writer;
@@ -370,9 +370,9 @@
 }
 
 void
-XMLWriterImpl::WriterPass::handle(Node* n,Pass::PassMode mode)
+XMLWriterImpl::WriterPass::handle(Node* n,Pass::TraversalKinds mode)
 {
-  if (mode & Pass::Begin_Mode) {
+  if (mode & Pass::PreOrderTraversal) {
     switch (n->getID()) 
     {
       case AliasTypeID:        put(cast<AliasType>(n)); break;
@@ -398,7 +398,7 @@
         break;
     }
   }
-  if (mode & Pass::End_Mode) {
+  if (mode & Pass::PostOrderTraversal) {
     endElement();
   }
 }

Modified: hlvm/trunk/tools/hlvm-xml2xml/hlvm-xml2xml.cpp
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/tools/hlvm-xml2xml/hlvm-xml2xml.cpp?rev=38106&r1=38105&r2=38106&view=diff

==============================================================================
--- hlvm/trunk/tools/hlvm-xml2xml/hlvm-xml2xml.cpp (original)
+++ hlvm/trunk/tools/hlvm-xml2xml/hlvm-xml2xml.cpp Sat Jul  7 18:59:52 2007
@@ -30,7 +30,7 @@
 #include <hlvm/Base/Memory.h>
 #include <hlvm/Reader/XML/XMLReader.h>
 #include <hlvm/Writer/XML/XMLWriter.h>
-#include <hlvm/Pass/Validate.h>
+#include <hlvm/Pass/Pass.h>
 #include <llvm/Support/CommandLine.h>
 #include <llvm/System/Signals.h>
 #include <fstream>
@@ -95,7 +95,7 @@
     AST* node = rdr->get();
     if (node) {
       PassManager* PM = PassManager::create();
-      Validate* pass = Validate::create();
+      Pass* pass = Pass::new_ValidatePass(); 
       PM->addPass( pass );
       PM->runOn(node);
       delete PM;





More information about the llvm-commits mailing list