[llvm-commits] [hlvm] r38165 - in /hlvm/trunk/hlvm: AST/AST.cpp AST/AST.h AST/MemoryOps.cpp AST/MemoryOps.h AST/Operator.cpp AST/Operator.h Reader/XML/XMLReader.cpp Runtime/Main.cpp

Reid Spencer reid at x10sys.com
Sat Jul 7 17:00:38 PDT 2007


Author: reid
Date: Sat Jul  7 19:00:38 2007
New Revision: 38165

URL: http://llvm.org/viewvc/llvm-project?rev=38165&view=rev
Log:
This patch provides the necessary operator definitions and parsing to permit
the helloworld.hlx test to parse correctly. The generated code for the program
is merely "return 0", however, because the code gen for the operators needed
has not been implemented yet.

Modified:
    hlvm/trunk/hlvm/AST/AST.cpp
    hlvm/trunk/hlvm/AST/AST.h
    hlvm/trunk/hlvm/AST/MemoryOps.cpp
    hlvm/trunk/hlvm/AST/MemoryOps.h
    hlvm/trunk/hlvm/AST/Operator.cpp
    hlvm/trunk/hlvm/AST/Operator.h
    hlvm/trunk/hlvm/Reader/XML/XMLReader.cpp
    hlvm/trunk/hlvm/Runtime/Main.cpp

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

==============================================================================
--- hlvm/trunk/hlvm/AST/AST.cpp (original)
+++ hlvm/trunk/hlvm/AST/AST.cpp Sat Jul  7 19:00:38 2007
@@ -39,6 +39,8 @@
 #include <hlvm/AST/Program.h>
 #include <hlvm/AST/Block.h>
 #include <hlvm/AST/ControlFlow.h>
+#include <hlvm/AST/MemoryOps.h>
+#include <hlvm/AST/InputOutput.h>
 #include <hlvm/AST/SymbolTable.h>
 #include <hlvm/Base/Assert.h>
 #include <hlvm/Base/Pool.h>
@@ -508,14 +510,83 @@
   return result;
 }
 
-ReturnOp*
-AST::new_ReturnOp(const Locator* loc)
+template<class OpClass> 
+OpClass* 
+AST::new_NilaryOp(
+  const Locator* loc ///< The source locator
+)
 {
-  ReturnOp* result = new ReturnOp();
+  OpClass* result = new OpClass();
   result->setLocator(loc);
   return result;
 }
 
+/// Provide a template function for creating a unary operator
+template<class OpClass>
+OpClass* 
+AST::new_UnaryOp(
+  Value* oprnd1,     ///< The first operand
+  const Locator* loc ///< The source locator
+)
+{
+  OpClass* result = new OpClass();
+  result->setLocator(loc);
+  result->setOperand(0,oprnd1);
+  return result;
+}
+
+/// Provide a template function for creating a binary operator
+template<class OpClass>
+OpClass* 
+AST::new_BinaryOp(
+  Value* oprnd1,     ///< The first operand
+  Value* oprnd2,     ///< The second operand
+  const Locator* loc ///< The source locator
+)
+{
+  OpClass* result = new OpClass();
+  result->setLocator(loc);
+  result->setOperand(0,oprnd1);
+  result->setOperand(1,oprnd2);
+  return result;
+}
+
+/// Provide a template function for creating a ternary operator
+template<class OpClass>
+OpClass* 
+AST::new_TernaryOp(
+  Value* oprnd1,     ///< The first operand
+  Value* oprnd2,     ///< The second operand
+  Value* oprnd3,     ///< The third operand
+  const Locator* loc ///< The source locator
+)
+{
+  OpClass* result = new OpClass();
+  result->setLocator(loc);
+  result->setOperand(0,oprnd1);
+  result->setOperand(1,oprnd2);
+  result->setOperand(2,oprnd3);
+  return result;
+}
+
+// Control Flow Operators
+template ReturnOp* 
+AST::new_UnaryOp<ReturnOp>(Value*op1,const Locator*loc);
+// Memory Operators
+template StoreOp*  
+AST::new_BinaryOp<StoreOp>(Value*op1,Value*op2,const Locator*loc);
+template LoadOp*   
+AST::new_UnaryOp<LoadOp>(Value*op1,const Locator*loc);
+template ReferenceOp* 
+AST::new_NilaryOp<ReferenceOp>(const Locator*loc);
+// Input/Output Operators
+template OpenOp* 
+AST::new_UnaryOp<OpenOp>(Value*op1,const Locator*loc);
+template WriteOp* 
+AST::new_TernaryOp<WriteOp>(Value*op1,Value*op2,Value*op3,const Locator*loc);
+template CloseOp* 
+AST::new_UnaryOp<CloseOp>(Value*op1,const Locator*loc);
+
 Documentation* 
 AST::new_Documentation(const Locator* loc)
 {

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

==============================================================================
--- hlvm/trunk/hlvm/AST/AST.h (original)
+++ hlvm/trunk/hlvm/AST/AST.h Sat Jul  7 19:00:38 2007
@@ -55,9 +55,14 @@
 class ConstantReal;
 class ConstantText;
 class ConstantZero;
-class ReturnOp;
 class AliasType;
 class Pool;
+class ReturnOp;
+class StoreOp;
+class LoadOp;
+class OpenOp;
+class CloseOp;
+class WriteOp;
 typedef AliasType Argument;
 typedef AliasType Field;
 
@@ -417,11 +422,70 @@
       const std::string& value, ///< The value of the ConstantText
       const Locator* loc = 0    ///< The source locator
     );
-    /// Create a new ConstantText node.
+
+    /// Provide a template function for creating a nilary operator
+    template<class OpClass>
+    OpClass* new_NilaryOp(
+      const Locator* loc = 0 ///< The source locator
+    );
+
+    /// Provide a template function for creating a unary operator
+    template<class OpClass>
+    OpClass* new_UnaryOp(
+      Value* oprnd1,         ///< The first operand
+      const Locator* loc = 0 ///< The source locator
+    );
+
+    /// Provide a template function for creating a binary operator
+    template<class OpClass>
+    OpClass* new_BinaryOp(
+      Value* oprnd1,         ///< The first operand
+      Value* oprnd2,         ///< The second operand
+      const Locator* loc = 0 ///< The source locator
+    );
+
+    /// Provide a template function for creating a ternary operator
+    template<class OpClass>
+    OpClass* new_TernaryOp(
+      Value* oprnd1,         ///< The first operand
+      Value* oprnd2,         ///< The second operand
+      Value* oprnd3,         ///< The third operand
+      const Locator* loc = 0 ///< The source locator
+    );
+
     /// Create a new ReturnOp node. The ReturnOp is an operator that returns
     /// immediately from the enclosing function, possibly with a result value.
     ReturnOp* new_ReturnOp(
-      const Locator* loc = 0 ///< The 
+      Value* val,            ///< The value to return
+      const Locator* loc = 0 ///< The source locator
+    );
+    /// Create a new StoreOp node.
+    StoreOp* new_StoreOp(
+      Value* var,            ///< The variable whose value is assigned
+      Value* val,            ///< The value to assign to the variable
+      const Locator* loc = 0 ///< The source locator
+    );
+    /// Create a new LoadOp node.
+    LoadOp* new_LoadOp(
+      Value* var,            ///< The variable from which the value is loaded
+      const Locator* loc = 0 ///< The source locator
+    );
+    /// Create a new OpenOp node.
+    OpenOp* new_OpenOp(
+      Value* uri,            ///< The URI saying what to open and how
+      const Locator* loc = 0 ///< The source locator
+    );
+    /// Create a new WriteOp node.
+    WriteOp* new_WriteOp(
+      Value* strm,           ///< The stream to write
+      Value* buffer,         ///< The buffer that should be written
+      Value* len,            ///< The length of the buffer
+      const Locator* loc = 0 ///< The source locator
+    );
+    /// Create a new CloseOp node.
+    CloseOp* new_CloseOp(
+      const Value* strm,     ///< The stream to close
+      const Locator* loc = 0 ///< The source locator
     );
 
   /// @}

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

==============================================================================
--- hlvm/trunk/hlvm/AST/MemoryOps.cpp (original)
+++ hlvm/trunk/hlvm/AST/MemoryOps.cpp Sat Jul  7 19:00:38 2007
@@ -1,4 +1,4 @@
-//===-- HLVM AST String Operators -------------------------------*- C++ -*-===//
+//===-- HLVM AST Memory Operators -------------------------------*- C++ -*-===//
 //
 //                      High Level Virtual Machine (HLVM)
 //
@@ -20,16 +20,19 @@
 // MA 02110-1301 USA
 //
 //===----------------------------------------------------------------------===//
-/// @file hlvm/AST/StringOps.cpp
+/// @file hlvm/AST/MemoryOps.cpp
 /// @author Reid Spencer <rspencer at reidspencer.org> (original author)
 /// @date 2006/05/18
 /// @since 0.1.0
-/// @brief Implements the various AST string operators.
+/// @brief Implements the various AST memory operators
 //===----------------------------------------------------------------------===//
 
-#include <hlvm/AST/StringOps.h>
+#include <hlvm/AST/MemoryOps.h>
 
 namespace hlvm {
 
+LoadOp::~LoadOp() {}
+StoreOp::~StoreOp() {}
+ReferenceOp::~ReferenceOp() {}
 
 }

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

==============================================================================
--- hlvm/trunk/hlvm/AST/MemoryOps.h (original)
+++ hlvm/trunk/hlvm/AST/MemoryOps.h Sat Jul  7 19:00:38 2007
@@ -70,22 +70,22 @@
 
 /// This operator represents the 
 /// @brief HLVM AST String Insert Node
-class LoadOp : public UnaryOperator
+class StoreOp : public BinaryOperator
 {
   /// @name Constructors
   /// @{
   protected:
-    LoadOp() : UnaryOperator(LoadOpID) {}
+    StoreOp() : BinaryOperator(LoadOpID) {}
 
   public:
-    virtual ~LoadOp();
+    virtual ~StoreOp();
 
   /// @}
   /// @name Accessors
   /// @{
   public:
-    static inline bool classof(const LoadOp*) { return true; }
-    static inline bool classof(const Node* N) { return N->is(LoadOpID); }
+    static inline bool classof(const StoreOp*) { return true; }
+    static inline bool classof(const Node* N) { return N->is(StoreOpID); }
 
   /// @}
   /// @name Mutators
@@ -99,6 +99,41 @@
   /// @}
   friend class AST;
 };
+
+/// This operator yields the value of a named variable
+/// @brief HLVM AST Variable Reference Operator
+class ReferenceOp : public NilaryOperator
+{
+  /// @name Constructors
+  /// @{
+  protected:
+    ReferenceOp() : NilaryOperator(ReferenceOpID) {}
+
+  public:
+    virtual ~ReferenceOp();
+
+  /// @}
+  /// @name Accessors
+  /// @{
+  public:
+    const std::string getVarName() const { return varName; }
+    static inline bool classof(const ReferenceOp*) { return true; }
+    static inline bool classof(const Node* N) { return N->is(ReferenceOpID); }
+
+  /// @}
+  /// @name Mutators
+  /// @{
+  public:
+    void setVarName(const std::string& name) { varName = name; }
+
+  /// @}
+  /// @name Data
+  /// @{
+  protected:
+    std::string varName;
+  /// @}
+  friend class AST;
+};
 } // hlvm
 
 #endif

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

==============================================================================
--- hlvm/trunk/hlvm/AST/Operator.cpp (original)
+++ hlvm/trunk/hlvm/AST/Operator.cpp Sat Jul  7 19:00:38 2007
@@ -56,6 +56,12 @@
   return 0;
 }
 
+void
+NilaryOperator::setOperand(unsigned opnum, Value* operand)
+{
+  hlvmAssert(!"Can't set operands on a NilaryOperator!");
+}
+
 void 
 NilaryOperator::insertChild(Node* child)
 {
@@ -85,6 +91,14 @@
   return op1 != 0;
 }
 
+void
+UnaryOperator::setOperand(unsigned opnum, Value* operand)
+{
+  hlvmAssert(opnum == 0 && "Operand Index out of range for UnaryOperator!");
+  operand->setParent(this);
+  op1 = operand;
+}
+
 void 
 UnaryOperator::insertChild(Node* child)
 {
@@ -122,6 +136,14 @@
   return (ops[0] ? 1 : 0) + (ops[1] ? 1 : 0);
 }
 
+void
+BinaryOperator::setOperand(unsigned opnum, Value* operand)
+{
+  hlvmAssert(opnum <= 1 && "Operand Index out of range for BinaryOperator!");
+  operand->setParent(this);
+  ops[opnum] = operand;
+}
+
 void 
 BinaryOperator::insertChild(Node* child)
 {
@@ -163,6 +185,14 @@
   return (ops[0] ? 1 : 0) + (ops[1] ? 1 : 0) + (ops[2] ? 1 : 0);
 }
 
+void
+TernaryOperator::setOperand(unsigned opnum, Value* operand)
+{
+  hlvmAssert(opnum <= 2 && "Operand Index out of range for TernaryOperator!");
+  operand->setParent(this);
+  ops[opnum] = operand;
+}
+
 void 
 TernaryOperator::insertChild(Node* child)
 {
@@ -208,6 +238,15 @@
   return ops.size();
 }
 
+void
+MultiOperator::setOperand(unsigned opnum, Value* operand)
+{
+  if (ops.capacity() < opnum + 1)
+    ops.resize(opnum+1);
+  operand->setParent(this);
+  ops[opnum] = operand;
+}
+
 void 
 MultiOperator::insertChild(Node* child)
 {

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

==============================================================================
--- hlvm/trunk/hlvm/AST/Operator.h (original)
+++ hlvm/trunk/hlvm/AST/Operator.h Sat Jul  7 19:00:38 2007
@@ -61,6 +61,12 @@
     static inline bool classof(const Node* N) { return N->isOperator(); }
 
   /// @}
+  /// @name Mutators
+  /// @{
+  public:
+    virtual void setOperand(unsigned opnum, Value* oprnd) = 0;
+
+  /// @}
   friend class AST;
 };
 
@@ -85,6 +91,9 @@
   /// @}
   /// @name Mutators
   /// @{
+  public:
+    virtual void setOperand(unsigned opnum, Value* oprnd);
+
   protected:
     virtual void insertChild(Node* child);
     virtual void removeChild(Node* child);
@@ -112,6 +121,8 @@
   /// @}
   /// @name Mutators
   /// @{
+  public:
+    virtual void setOperand(unsigned opnum, Value* oprnd);
   protected:
     virtual void insertChild(Node* child);
     virtual void removeChild(Node* child);
@@ -147,6 +158,8 @@
   /// @}
   /// @name Mutators
   /// @{
+  public:
+    virtual void setOperand(unsigned opnum, Value* oprnd);
   protected:
     virtual void insertChild(Node* child);
     virtual void removeChild(Node* child);
@@ -181,6 +194,8 @@
   /// @}
   /// @name Mutators
   /// @{
+  public:
+    virtual void setOperand(unsigned opnum, Value* oprnd);
   protected:
     virtual void insertChild(Node* child);
     virtual void removeChild(Node* child);
@@ -238,6 +253,7 @@
   /// @name Mutators
   /// @{
   public:
+    virtual void setOperand(unsigned opnum, Value* oprnd);
     void addOperand(Value* v) { v->setParent(this); }
   protected:
     virtual void insertChild(Node* child);

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

==============================================================================
--- hlvm/trunk/hlvm/Reader/XML/XMLReader.cpp (original)
+++ hlvm/trunk/hlvm/Reader/XML/XMLReader.cpp Sat Jul  7 19:00:38 2007
@@ -41,6 +41,8 @@
 #include <hlvm/AST/Program.h>
 #include <hlvm/AST/Constants.h>
 #include <hlvm/AST/ControlFlow.h>
+#include <hlvm/AST/MemoryOps.h>
+#include <hlvm/AST/InputOutput.h>
 #include <hlvm/Base/Assert.h>
 #include <libxml/parser.h>
 #include <libxml/relaxng.h>
@@ -131,8 +133,18 @@
   Value*         parseValue         (xmlNodePtr& cur, int token);
   ReturnOp*      parseReturn        (xmlNodePtr& cur);
   Block*         parseBlock         (xmlNodePtr& cur);
+  ReferenceOp*   parseReference     (xmlNodePtr& cur);
   Program*       parseProgram       (xmlNodePtr& cur);
   inline xmlNodePtr   checkDoc(xmlNodePtr cur, Documentable* node);
+  inline Value*  getValue(xmlNodePtr&);
+  template<class OpClass>
+  OpClass* parseNilaryOp(xmlNodePtr& cur);
+  template<class OpClass>
+  OpClass* parseUnaryOp(xmlNodePtr& cur);
+  template<class OpClass>
+  OpClass* parseBinaryOp(xmlNodePtr& cur);
+  template<class OpClass>
+  OpClass* parseTernaryOp(xmlNodePtr& cur);
 private:
 };
 
@@ -465,6 +477,16 @@
   return child;
 }
 
+inline Value*
+XMLReaderImpl::getValue(xmlNodePtr& cur)
+{
+  if (cur && skipBlanks(cur) && cur->type == XML_ELEMENT_NODE)
+    return parseValue(cur);
+  else
+    hlvmDeadCode("Expecting a value");
+  return 0;
+}
+
 Import*
 XMLReaderImpl::parseImport(xmlNodePtr& cur)
 {
@@ -715,18 +737,56 @@
   return var;
 }
 
-ReturnOp*
-XMLReaderImpl::parseReturn(xmlNodePtr& cur)
+ReferenceOp*
+XMLReaderImpl::parseReference(xmlNodePtr& cur)
 {
-  hlvmAssert(getToken(cur->name) == TKN_ret && "Expecting block element");
+  std::string id = getAttribute(cur,"id");
   Locator* loc = getLocator(cur);
-  ReturnOp* ret = ast->new_ReturnOp(loc);
+  ReferenceOp* result = ast->new_NilaryOp<ReferenceOp>(loc);
+  result->setVarName(id);
+  return result;
+}
+
+template<class OpClass>
+OpClass*
+XMLReaderImpl::parseNilaryOp(xmlNodePtr& cur)
+{
   xmlNodePtr child = cur->children;
-  if (child && skipBlanks(child) && child->type == XML_ELEMENT_NODE) {
-    Value* op = parseValue(child);
-    ret->setResult(op);
-  }
-  return ret;
+  Locator* loc = getLocator(cur);
+  return ast->new_UnaryOp<OpClass>(loc);
+}
+
+template<class OpClass>
+OpClass*
+XMLReaderImpl::parseUnaryOp(xmlNodePtr& cur)
+{
+  xmlNodePtr child = cur->children;
+  Value* oprnd1 = getValue(child);
+  Locator* loc = getLocator(cur);
+  return ast->new_UnaryOp<OpClass>(oprnd1,loc);
+}
+
+template<class OpClass>
+OpClass*
+XMLReaderImpl::parseBinaryOp(xmlNodePtr& cur)
+{
+  xmlNodePtr child = cur->children;
+  Value* oprnd1 = getValue(child);
+  Value* oprnd2 = getValue(child);
+  Locator* loc = getLocator(cur);
+  return ast->new_BinaryOp<OpClass>(oprnd1,oprnd2,loc);
+}
+
+template<class OpClass>
+OpClass*
+XMLReaderImpl::parseTernaryOp(xmlNodePtr& cur)
+{
+  xmlNodePtr child = cur->children;
+  Value* oprnd1 = getValue(child);
+  Value* oprnd2 = getValue(child);
+  Value* oprnd3 = getValue(child);
+  Locator* loc = getLocator(cur);
+  return ast->new_TernaryOp<OpClass>(oprnd1,oprnd2,oprnd3,loc);
 }
 
 inline Constant*
@@ -766,15 +826,14 @@
 {
   Operator* op = 0;
   switch (tkn) {
-    case TKN_ret:          op = parseReturn(cur); break;
+    case TKN_ret:          op = parseUnaryOp<ReturnOp>(cur); break;
     case TKN_block:        op = parseBlock(cur); break;
-    case TKN_store:
-    case TKN_load:
-    case TKN_open:
-    case TKN_write:
-    case TKN_close:
-      std::cerr << "Operator " << cur->name << " not imlpemented.\n";
-      break;
+    case TKN_store:        op = parseBinaryOp<StoreOp>(cur); break;
+    case TKN_load:         op = parseUnaryOp<LoadOp>(cur); break;
+    case TKN_open:         op = parseUnaryOp<OpenOp>(cur); break;
+    case TKN_write:        op = parseTernaryOp<WriteOp>(cur); break;
+    case TKN_close:        op = parseUnaryOp<CloseOp>(cur); break;
+    case TKN_ref:          op = parseReference(cur); break;
     default:
       hlvmDeadCode("Unrecognized operator");
       break;
@@ -809,9 +868,12 @@
     case TKN_close:
     case TKN_store:
     case TKN_load:
-    case TKN_block: 
+    case TKN_ref:
       v = parseOperator(cur,tkn);
       break;
+    case TKN_block: 
+      v = parseBlock(cur);
+      break;
     case TKN_program:
       v = parseProgram(cur);
       break;

Modified: hlvm/trunk/hlvm/Runtime/Main.cpp
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/Runtime/Main.cpp?rev=38165&r1=38164&r2=38165&view=diff

==============================================================================
--- hlvm/trunk/hlvm/Runtime/Main.cpp (original)
+++ hlvm/trunk/hlvm/Runtime/Main.cpp Sat Jul  7 19:00:38 2007
@@ -66,7 +66,7 @@
     // If we got a start function ..
     if (func) {
       // Invoke it.
-      return (*func)(argc-1,(signed char**)&argv[1]);
+      return (*func)(argc-1,reinterpret_cast<signed char**>(&argv[1]));
     } else {
       // Give an error
       std::cerr << argv[0] << ": Program '" << Start << "' not found.\n";





More information about the llvm-commits mailing list