[llvm-commits] [hlvm] r38323 - in /hlvm/trunk: hlvm/AST/AST.cpp hlvm/AST/AST.h hlvm/AST/ContainerType.h hlvm/AST/Linkables.cpp hlvm/AST/Linkables.h hlvm/AST/Node.h hlvm/AST/Operator.cpp hlvm/Reader/HLVM.rng hlvm/Reader/XMLReader.cpp hlvm/Writer/XMLWriter.cpp test/xml2xml/progargs.hlx

Reid Spencer reid at x10sys.com
Sat Jul 7 17:02:20 PDT 2007


Author: reid
Date: Sat Jul  7 19:02:20 2007
New Revision: 38323

URL: http://llvm.org/viewvc/llvm-project?rev=38323&view=rev
Log:
Changes to allow program and function arguments to be accessed in
a block with the ReferenceOp. 

Added:
    hlvm/trunk/test/xml2xml/progargs.hlx
Modified:
    hlvm/trunk/hlvm/AST/AST.cpp
    hlvm/trunk/hlvm/AST/AST.h
    hlvm/trunk/hlvm/AST/ContainerType.h
    hlvm/trunk/hlvm/AST/Linkables.cpp
    hlvm/trunk/hlvm/AST/Linkables.h
    hlvm/trunk/hlvm/AST/Node.h
    hlvm/trunk/hlvm/AST/Operator.cpp
    hlvm/trunk/hlvm/Reader/HLVM.rng
    hlvm/trunk/hlvm/Reader/XMLReader.cpp
    hlvm/trunk/hlvm/Writer/XMLWriter.cpp

Modified: hlvm/trunk/hlvm/AST/AST.cpp
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/AST/AST.cpp?rev=38323&r1=38322&r2=38323&view=diff

==============================================================================
--- hlvm/trunk/hlvm/AST/AST.cpp (original)
+++ hlvm/trunk/hlvm/AST/AST.cpp Sat Jul  7 19:02:20 2007
@@ -196,7 +196,7 @@
 }
 
 SignatureType* 
-AST::getProgramType() const
+AST::getProgramType()
 {
   ASTImpl* ast = const_cast<ASTImpl*>(static_cast<const ASTImpl*>(this));
   if (!ast->ProgramTypeSingleton) {
@@ -205,11 +205,16 @@
     ast->ProgramTypeSingleton->setName("ProgramType");
     Type* intType = ast->getPrimitiveType(SInt32TypeID);
     ast->ProgramTypeSingleton->setResultType(intType);
-    ArrayType* arg_array = ast->new_ArrayType("arg_array",
-      ast->new_TextType("string",loc),0,loc);
-    PointerType* args = ast->new_PointerType("arg_array_ptr",arg_array,loc);
-    Argument* param = ast->new_Argument("args",args,loc);
-    ast->ProgramTypeSingleton->addArgument(param);
+    ArgumentType* argc = new ArgumentType();
+    argc->setName("argc");
+    argc->setElementType(intType);
+    ast->ProgramTypeSingleton->addArgument(argc);
+    const PointerType* argv_type = getPointerTo(getPointerTo(
+      ast->getPrimitiveType(StringTypeID)));
+    ArgumentType* argv = new ArgumentType();
+    argv->setName("argv");
+    argv->setElementType(argv);
+    ast->ProgramTypeSingleton->addArgument(argv);
   }
   return ast->ProgramTypeSingleton;
 }
@@ -586,7 +591,7 @@
   Argument* result = new Argument();
   result->setLocator(loc);
   result->setName(id);
-  result->setElementType(ty);
+  result->setType(ty);
   return result;
 }
 
@@ -598,20 +603,40 @@
   result->setLocator(loc);
   result->setName(id);
   result->setType(ty);
+  for (SignatureType::const_iterator I = ty->begin(), E = ty->end(); 
+       I != E; ++I ) 
+  {
+    const Type* Ty = (*I)->getElementType();
+    assert(!Ty->is(VoidTypeID) && "Arguments can't be void type");
+    Argument* arg = new Argument();
+    arg->setType(Ty);
+    arg->setName((*I)->getName());
+    result->addArgument(arg);
+  }
   return result;
 }
 
 Program*
 AST::new_Program(const std::string& id, const Locator* loc)
 {
-  SignatureType* program_type = getProgramType();
+  SignatureType* ty = getProgramType();
   ASTImpl* ast = static_cast<ASTImpl*>(this);
 
   Program* result = new Program();
   result->setLocator(loc);
   result->setName(id);
-  result->setSignature(program_type);
+  result->setType(ty);
   result->setLinkageKind(ExternalLinkage);
+  for (SignatureType::const_iterator I = ty->begin(), E = ty->end(); 
+       I != E; ++I ) 
+  {
+    const Type* Ty = (*I)->getElementType();
+    assert(!Ty->is(VoidTypeID) && "Arguments can't be void type");
+    Argument* arg = new Argument();
+    arg->setType(Ty);
+    arg->setName((*I)->getName());
+    result->addArgument(arg);
+  }
   return result;
 }
 
@@ -643,14 +668,15 @@
 AST::new_ReferenceOp(const Value* V, const Locator*loc)
 {
   hlvmAssert(V != 0 && "ReferenceOp must have a Value to reference");
-  hlvmAssert(llvm::isa<Constant>(V) || llvm::isa<AutoVarOp>(V));
   ReferenceOp* result = new ReferenceOp();
   const Type* refType = V->getType();
-  if (llvm::isa<ConstantValue>(V)) {
+  if (llvm::isa<ConstantValue>(V) || llvm::isa<Argument>(V)) {
     result->setType(refType);
-  } else {
+  } else if (llvm::isa<AutoVarOp>(V) || llvm::isa<Constant>(V)) {
     PointerType* PT = getPointerTo(refType);
     result->setType(PT);
+  } else {
+    hlvmAssert(!"Invalid referent type");
   }
   result->setLocator(loc);
   result->setReferent(V);

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

==============================================================================
--- hlvm/trunk/hlvm/AST/AST.h (original)
+++ hlvm/trunk/hlvm/AST/AST.h Sat Jul  7 19:02:20 2007
@@ -48,6 +48,7 @@
 class Documentation;
 class Block;
 class Function; 
+class Argument;
 class Program; 
 class Import;
 class Locator; 
@@ -96,7 +97,7 @@
     const std::string& getSystemID() const { return sysid; }
     const std::string& getPublicID() const { return pubid; }
     Pool* getPool() const { return pool; }
-    SignatureType* getProgramType() const;
+    SignatureType* getProgramType();
 
   /// @}
   /// @name Mutators

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

==============================================================================
--- hlvm/trunk/hlvm/AST/ContainerType.h (original)
+++ hlvm/trunk/hlvm/AST/ContainerType.h Sat Jul  7 19:02:20 2007
@@ -291,7 +291,9 @@
   /// @}
 };
 
-typedef AliasType Field;
+/// This typedef just uses a more common name when accessing the fields of a
+/// structure.
+typedef AliasType FieldType;
 
 /// This class provides an Abstract Syntax Tree node that represents an 
 /// sequence type. A sequence type is a type that lays out its elements in 
@@ -320,7 +322,7 @@
   /// @name Mutators
   /// @{
   protected:
-    void addField(Field* field) { contents.push_back(field); }
+    void addField(FieldType* field) { contents.push_back(field); }
 
   /// @}
   /// @name Data
@@ -359,10 +361,9 @@
   friend class AST;
 };
 
-/// This typedef is used to just provide a more convenient name for AliasType
-/// when AliasType is being used as the Argument to a SignatureType.
-/// @brief AST Argument Type Node
-typedef AliasType Argument;
+/// This typedef just provides a more common name for accessing the types of
+/// the arguments of a signature.
+typedef AliasType ArgumentType;
 
 /// This class provides an Abstract Syntax Tree node that represents the call
 /// signature of an HLVM function. A SignatureType encapsulates the
@@ -396,7 +397,7 @@
   public:
     void setResultType(const Type* ty) { result = ty; }
     void setIsVarArgs(bool is) { flags = is ? 1 : 0; }
-    void addArgument(Argument* arg) { contents.push_back(arg); }
+    void addArgument(ArgumentType* arg) { contents.push_back(arg); }
 
   /// @}
   /// @name Data

Modified: hlvm/trunk/hlvm/AST/Linkables.cpp
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/AST/Linkables.cpp?rev=38323&r1=38322&r2=38323&view=diff

==============================================================================
--- hlvm/trunk/hlvm/AST/Linkables.cpp (original)
+++ hlvm/trunk/hlvm/AST/Linkables.cpp Sat Jul  7 19:02:20 2007
@@ -38,13 +38,16 @@
 
 Linkable::~Linkable() { }
 Variable::~Variable() { }
+Argument::~Argument() { }
 Function::~Function() { }
-Program::~Program() { }
 
-const SignatureType* 
-Function::getSignature() const 
+Value* 
+Function::getArgument(const std::string& name) const
 {
-  return cast<SignatureType>(type); 
+  for (const_iterator I = begin(), E = end(); I != E ; ++I )
+    if ((*I)->getName() == name)
+      return *I;
+  return 0;
 }
 
 void 
@@ -69,4 +72,6 @@
   }
 }
 
+Program::~Program() { }
+
 }

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

==============================================================================
--- hlvm/trunk/hlvm/AST/Linkables.h (original)
+++ hlvm/trunk/hlvm/AST/Linkables.h Sat Jul  7 19:02:20 2007
@@ -137,6 +137,42 @@
   friend class AST;
 };
 
+/// This class provides an Abstract Syntax Tree node that represents an argument
+/// to a Function. An Argument is simply a Value that has a name. Arguments can
+/// be used within a function's blocks to access the value of the argument.
+/// @see Value
+/// @see Function
+/// @brief AST Argument Node
+class Argument : public Value
+{
+  /// @name Constructors
+  /// @{
+  protected:
+    Argument() : Value(ArgumentID) {}
+    virtual ~Argument();
+
+  /// @}
+  /// @name Accessors
+  /// @{
+  public:
+    const std::string& getName() const { return name; }
+    static inline bool classof(const Argument*) { return true; }
+    static inline bool classof(const Node* N) { return N->is(ArgumentID); }
+
+  /// @}
+  /// @name Mutators
+  /// @{
+  public:
+    void setName(const std::string& N) { name = N; }
+  /// @}
+  /// @name Data
+  /// @{
+  protected:
+    std::string name;
+  /// @}
+  friend class AST;
+};
+
 /// This class provides an Abstract Syntax Tree node that represents a Function.
 /// A Function is a callable block of code that accepts parameters and 
 /// returns a result.  This is the basic unit of code in HLVM. A Function
@@ -154,10 +190,18 @@
 /// @brief AST Function Node
 class Function : public Linkable
 {
+  /// @name Types
+  /// @{
+  public:
+    typedef std::vector<Argument*> ArgumentList;
+    typedef ArgumentList::iterator iterator;
+    typedef ArgumentList::const_iterator const_iterator;
+
+  /// @}
   /// @name Constructors
   /// @{
   protected:
-    Function(NodeIDs id = FunctionID) : Linkable(id), block(0) {}
+    Function(NodeIDs id = FunctionID) : Linkable(id), block(0), args() {}
     virtual ~Function();
 
   /// @}
@@ -166,33 +210,49 @@
   public:
     bool hasBlock() const { return block != 0; }
     Block* getBlock() const { return block; }
-    const SignatureType* getSignature() const;
-    const Type* getResultType() const { return getSignature()->getResultType();}
+    const SignatureType* getSignature() const 
+      { return static_cast<const SignatureType*>(type); }
+    const Type* getResultType() const 
+      { return getSignature()->getResultType();}
+    Value* getArgument(const std::string& name) const;
 
     static inline bool classof(const Function*) { return true; }
     static inline bool classof(const Node* N) { return N->isFunction(); }
 
   /// @}
+  /// @name Iterators
+  /// @{
+  public:
+    iterator         begin()       { return args.begin(); }
+    const_iterator   begin() const { return args.begin(); }
+    iterator         end  ()       { return args.end();   }
+    const_iterator   end  () const { return args.end();   }
+    size_t           size () const { return args.size();  }
+    bool             empty() const { return args.empty(); }
+    Argument*        front()       { return args.front(); }
+    const Argument*  front() const { return args.front(); }
+    Argument*        back()        { return args.back();  }
+    const Argument*  back()  const { return args.back();  }
+
+  /// @}
   /// @name Mutators
   /// @{
   public:
     virtual void insertChild(Node* kid);
     virtual void removeChild(Node* kid);
-    void setSignature(SignatureType* sig) { type = sig; }
     void setBlock(Block* blk) { blk->setParent(this); }
+    void addArgument(Argument* arg) { args.push_back(arg); }
 
   /// @}
   /// @name Data
   /// @{
   protected:
     Block * block;                   ///< The code block to be executed
+    ArgumentList args;
   /// @}
   friend class AST;
 };
 
-class Block; // Forward declare
-class SignatureType;  // Forward declare
-
 /// This class provides an Abstract Syntax Tree node that represents a Program 
 /// in HLVM. A Program is an entry point for running HLVM programs. It is 
 /// simply a Function with a specific signature. It represents the first

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

==============================================================================
--- hlvm/trunk/hlvm/AST/Node.h (original)
+++ hlvm/trunk/hlvm/AST/Node.h Sat Jul  7 19:02:20 2007
@@ -129,10 +129,11 @@
   ImplementsID,            ///< Specifies set of Interfaces implemented by class
 
   // SUBCLASSES OF VALUE
+  ArgumentID,              ///< An argument to a function
+FirstValueID = ArgumentID,
 
   // Constants
   ConstantBooleanID,       ///< A constant boolean value
-FirstValueID = ConstantBooleanID,
 FirstConstantID = ConstantBooleanID,
 FirstConstantValueID = ConstantBooleanID,
   ConstantIntegerID,       ///< A constant integer value

Modified: hlvm/trunk/hlvm/AST/Operator.cpp
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/AST/Operator.cpp?rev=38323&r1=38322&r2=38323&view=diff

==============================================================================
--- hlvm/trunk/hlvm/AST/Operator.cpp (original)
+++ hlvm/trunk/hlvm/AST/Operator.cpp Sat Jul  7 19:02:20 2007
@@ -45,7 +45,7 @@
 Operator::getContainingFunction()
 {
   Node* p = getParent();
-  while (p && !isa<Function>(p)) p = p->getParent();
+  while (p && !p->isFunction()) p = p->getParent();
   if (!p)
     return 0;
   return cast<Function>(p);

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

==============================================================================
--- hlvm/trunk/hlvm/Reader/HLVM.rng (original)
+++ hlvm/trunk/hlvm/Reader/HLVM.rng Sat Jul  7 19:02:20 2007
@@ -614,12 +614,12 @@
       <optional>
         <attribute name="linkage"><ref name="Linkage.type"/></attribute>
       </optional>
-      <choice>
-        <element name="as"><ref name="C_Identifier.type"/></element>
-        <zeroOrMore>
+      <optional>
+        <choice>
+          <element name="as"><ref name="C_Identifier.type"/></element>
           <ref name="block.elem"/>
-        </zeroOrMore>
-      </choice>
+        </choice>
+      </optional>
     </element>
   </define>
 

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

==============================================================================
--- hlvm/trunk/hlvm/Reader/XMLReader.cpp (original)
+++ hlvm/trunk/hlvm/Reader/XMLReader.cpp Sat Jul  7 19:02:20 2007
@@ -738,6 +738,10 @@
     blk = blk->getParentBlock();
   }
 
+  // Didn't find an autovar? Try a function argument
+  if (!referent)
+    referent = func->getArgument(id);
+
   // Didn't find an autovar? Try a constant value.
   if (!referent)
     referent= bundle->find_cval(id);
@@ -746,7 +750,6 @@
   if (!referent)
     referent = bundle->find_linkable(id);
 
-
   // Didn't find a linkable? Try an error message for size
   if (!referent)
       error(loc,std::string("Referent '") + id + "' not found");

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

==============================================================================
--- hlvm/trunk/hlvm/Writer/XMLWriter.cpp (original)
+++ hlvm/trunk/hlvm/Writer/XMLWriter.cpp Sat Jul  7 19:02:20 2007
@@ -777,6 +777,8 @@
   std::string name;
   if (isa<AutoVarOp>(ref))
     name = cast<AutoVarOp>(ref)->getName();
+  else if (isa<Argument>(ref))
+    name = cast<Argument>(ref)->getName();
   else if (isa<Constant>(ref))
     name = cast<Constant>(ref)->getName();
   else

Added: hlvm/trunk/test/xml2xml/progargs.hlx
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/test/xml2xml/progargs.hlx?rev=38323&view=auto

==============================================================================
--- hlvm/trunk/test/xml2xml/progargs.hlx (added)
+++ hlvm/trunk/test/xml2xml/progargs.hlx Sat Jul  7 19:02:20 2007
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<hlvm xmlns="http://hlvm.org/src/hlvm/Reader/XML/HLVM.rng" pubid="http://hlvm.org/src/hlvm/test/xml2xml/progargs.hlx">
+  <bundle id="progargs">
+    <program id="progargs">
+      <block>
+        <result>
+          <ref id="argc"/>
+        </result>
+        <ret/>
+      </block>
+    </program>
+  </bundle>
+</hlvm>





More information about the llvm-commits mailing list