Start to implement context and compilation unit creation
authorbsegovia <bsegovia@bsegovia-PC>
Fri, 10 Feb 2012 17:26:59 +0000 (09:26 -0800)
committerKeith Packard <keithp@keithp.com>
Fri, 10 Aug 2012 23:15:13 +0000 (16:15 -0700)
backend/src/CMakeLists.txt
backend/src/ir/ir_context.cpp [new file with mode: 0644]
backend/src/ir/ir_context.hpp
backend/src/ir/ir_function.cpp
backend/src/ir/ir_function.hpp
backend/src/ir/ir_instruction.cpp
backend/src/ir/ir_instruction.hpp
backend/src/ir/ir_register.hpp
backend/src/sys/exception.hpp [new file with mode: 0644]

index 0dfb3d6..66ba2c3 100644 (file)
@@ -25,6 +25,8 @@ set (GBE_SRC
   sys/logging.hpp
   sys/default_path.cpp
   sys/default_path.hpp
+  ir/ir_context.cpp
+  ir/ir_context.hpp
   ir/ir_unit.cpp
   ir/ir_unit.hpp
   ir/ir_constant.cpp
diff --git a/backend/src/ir/ir_context.cpp b/backend/src/ir/ir_context.cpp
new file mode 100644 (file)
index 0000000..be8b40c
--- /dev/null
@@ -0,0 +1,33 @@
+/* 
+ * Copyright © 2012 Intel Corporation
+ *
+ * This library 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 of the License, or (at your option) any later version.
+ *
+ * This library 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. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Benjamin Segovia <benjamin.segovia@intel.com>
+ */
+
+#include "ir_context.hpp"
+#include "ir_unit.hpp"
+
+namespace gbe
+{
+  Context::Context(Unit &unit) : unit(unit), fn(NULL), bb(NULL) {}
+  void Context::startFunction(const std::string &name) {
+    Function *fn = unit.newFunction(name);
+    if (UNLIKELY(fn == NULL))
+        throw std::exception("Function " + name " already defined");
+    fnStack.push_back(fn);
+  }
+} /* namespace gbe */
+
index 6d04df0..cf36dba 100644 (file)
 #ifndef __GBE_IR_CONTEXT_HPP__
 #define __GBE_IR_CONTEXT_HPP__
 
+#include "ir_instruction.hpp"
+#include "ir_function.hpp"
+#include "ir_register.hpp"
+#include "sys/vector.hpp"
+
 namespace gbe
 {
+  // We compile a unit
+  class Unit;
+
   /*! A context allows an easy creation of the functions (instruction stream and
    *  the set of immediates and registers needed for it) and constant arrays
    */
   class Context
   {
-    
+  public:
+    /*! Create a new context for this unit */
+    Context(Unit &unit);
+    /*! Create a new function "name" */
+    void startFunction(const std::string &name);
+    /*! Close the function */
+    void endFunction(void);
+    /*! Create a new register for the given type */
+    RegisterIndex reg(Register::Family type);
+    /*! Append a new input register for the function */
+    void input(RegisterIndex reg);
+    /*! Append a new output register for the function */
+    void output(RegisterIndex reg);
+    /*! Append a new tuple */
+    template <typename... Args>
+    INLINE TupleIndex tuple(Args...args) {
+      if (UNLIKELY(fn == NULL))
+        throw std::exception("Tuple not defined in a function");
+      fn->file.append(args...);
+    }
+  private:
+    Unit &unit;               //!< A unit is associated to a contect
+    Function *fn;             //!< Current function we are processing
+    Function::BasicBlock *bb; //!< Current basic block we are filling
+    vector<Function*> fnStack;//!< Stack of functions still to finish
   };
 
 } /* namespace gbe */
index 976968d..242bb44 100644 (file)
@@ -21,5 +21,8 @@
 namespace gbe
 {
   Function::Function(void) {}
+  Function::~Function(void) {
+    for (auto it = bb.begin(); it != bb.end(); ++it) GBE_DELETE(*it);
+  }
 } /* namespace gbe */
 
index 3699c3f..c746d01 100644 (file)
@@ -35,6 +35,8 @@ namespace gbe
   public:
     /*! Create an empty function */
     Function(void);
+    /*! Release everything *including* the basic block pointers */
+    ~Function(void);
 
     /*! Function basic blocks really belong to a function since:
      * 1 - registers used in the basic blocks belongs to the function register
@@ -65,11 +67,13 @@ namespace gbe
     }
 
   private:
-    vector<uint16> input;    //!< Input registers of the function
-    vector<uint16> output;   //!< Output registers of the function
-    vector<Value> value;     //!< All immediate values stored in the function
-    vector<BasicBlock> insn; //!< All the basic blocks one after the others
-    RegisterFile file;       //!< All the registers used in the instructions
+    friend class Context;         //!< Can freely modify a function
+    vector<RegisterIndex> input;  //!< Input registers of the function
+    vector<RegisterIndex> output; //!< Output registers of the function
+    vector<BasicBlock*> labels;   //!< Each label points to a basic block
+    vector<Value> value;    //!< All immediate values stored in the function
+    vector<BasicBlock*> bb; //!< All the basic blocks one after the others
+    RegisterFile file;      //!< All the registers used in the instructions
   };
 
 } /* namespace gbe */
index 1239f3c..46ec368 100644 (file)
@@ -522,11 +522,11 @@ DECL_MEM_FN(BranchInstruction, bool, isPredicated(void), isPredicated())
   }
 
   // BRA
-  Instruction bra(RegisterIndex dst, LabelIndex labelIndex) {
+  Instruction bra(LabelIndex labelIndex) {
     internal::BranchInstruction insn(labelIndex);
     return *reinterpret_cast<Instruction*>(&insn);
   }
-  Instruction bra(RegisterIndex dst, LabelIndex labelIndex, RegisterIndex pred) {
+  Instruction bra(LabelIndex labelIndex, RegisterIndex pred) {
     internal::BranchInstruction insn(labelIndex, pred);
     return *reinterpret_cast<Instruction*>(&insn);
   }
index 3d23c50..e2067b8 100644 (file)
@@ -191,7 +191,7 @@ namespace gbe
       return this->getSrc(fn, 0);
     }
     /*! Return the predicate register index (if predicated) */
-    Register getPredicateIndex(const Function &fn) const {
+    RegisterIndex getPredicateIndex(const Function &fn) const {
       assert(this->isPredicated() == true);
       return this->getSrcIndex(fn, 0);
     }
@@ -252,35 +252,65 @@ namespace gbe
   /// All emission functions
   ///////////////////////////////////////////////////////////////////////////
 
+  /*! mov.type dst src */
   Instruction mov(Type type, RegisterIndex dst, RegisterIndex src);
+  /*! cos.type dst src */
   Instruction cos(Type type, RegisterIndex dst, RegisterIndex src);
+  /*! sin.type dst src */
   Instruction sin(Type type, RegisterIndex dst, RegisterIndex src);
+  /*! tan.type dst src */
   Instruction tan(Type type, RegisterIndex dst, RegisterIndex src);
+  /*! log.type dst src */
   Instruction log(Type type, RegisterIndex dst, RegisterIndex src);
+  /*! sqr.type dst src */
   Instruction sqr(Type type, RegisterIndex dst, RegisterIndex src);
+  /*! rsq.type dst src */
   Instruction rsq(Type type, RegisterIndex dst, RegisterIndex src);
+  /*! pow.type dst src0 src1 */
   Instruction pow(Type type, RegisterIndex dst, RegisterIndex src0, RegisterIndex src1);
+  /*! mul.type dst src0 src1 */
   Instruction mul(Type type, RegisterIndex dst, RegisterIndex src0, RegisterIndex src1);
+  /*! add.type dst src0 src1 */
   Instruction add(Type type, RegisterIndex dst, RegisterIndex src0, RegisterIndex src1);
+  /*! sub.type dst src0 src1 */
   Instruction sub(Type type, RegisterIndex dst, RegisterIndex src0, RegisterIndex src1);
+  /*! div.type dst src0 src1 */
   Instruction div(Type type, RegisterIndex dst, RegisterIndex src0, RegisterIndex src1);
+  /*! rem.type dst src0 src1 */
   Instruction rem(Type type, RegisterIndex dst, RegisterIndex src0, RegisterIndex src1);
+  /*! shl.type dst src0 src1 */
   Instruction shl(Type type, RegisterIndex dst, RegisterIndex src0, RegisterIndex src1);
+  /*! shr.type dst src0 src1 */
   Instruction shr(Type type, RegisterIndex dst, RegisterIndex src0, RegisterIndex src1);
+  /*! asr.type dst src0 src1 */
   Instruction asr(Type type, RegisterIndex dst, RegisterIndex src0, RegisterIndex src1);
+  /*! bsf.type dst src0 src1 */
   Instruction bsf(Type type, RegisterIndex dst, RegisterIndex src0, RegisterIndex src1);
+  /*! bsb.type dst src0 src1 */
   Instruction bsb(Type type, RegisterIndex dst, RegisterIndex src0, RegisterIndex src1);
+  /*! or.type dst src0 src1 */
   Instruction or$(Type type, RegisterIndex dst, RegisterIndex src0, RegisterIndex src1);
+  /*! xor.type dst src0 src1 */
   Instruction xor$(Type type, RegisterIndex dst, RegisterIndex src0, RegisterIndex src1);
+  /*! and.type dst src0 src1 */
   Instruction and$(Type type, RegisterIndex dst, RegisterIndex src0, RegisterIndex src1);
+  /*! mad.type dst {src0, src1, src2} == src */
   Instruction mad(Type type, RegisterIndex dst, TupleIndex src);
+  /*! cvt.{dstType <- srcType} dst src */
   Instruction cvt(Type dstType, Type srcType, RegisterIndex dst, RegisterIndex src0, RegisterIndex src1);
-  Instruction bra(RegisterIndex dst, LabelIndex labelIndex);
-  Instruction bra(RegisterIndex dst, LabelIndex labelIndex, RegisterIndex pred);
+  /*! bra labelIndex */
+  Instruction bra(LabelIndex labelIndex);
+  /*! (pred) bra labelIndex */
+  Instruction bra(LabelIndex labelIndex, RegisterIndex pred);
+  /*! loadi.type dst value */
   Instruction loadi(Type type, RegisterIndex dst, ValueIndex value);
+  /*! load.type.space {dst1,...,dst_valueNum} offset value */
   Instruction load(Type type, TupleIndex dst, RegisterIndex offset, MemorySpace space, uint16 valueNum);
+  /*! store.type.space offset {src1,...,src_valueNum} value */
   Instruction store(Type type, TupleIndex src, RegisterIndex offset, MemorySpace space, uint16 valueNum);
+  /*! fence.space */
   Instruction fence(MemorySpace space);
+  /*! label labelIndex */
   Instruction label(LabelIndex labelIndex);
 
 } /* namespace gbe */
index a0dd90e..04976ce 100644 (file)
@@ -30,26 +30,26 @@ namespace gbe
   class Register
   {
   public:
+    /*! Register family */
+    enum Family : uint8 {
+      BOOL  = 0,
+      BYTE  = 1,
+      WORD  = 2,
+      DWORD = 3,
+      QWORD = 4
+    };
     /*! Build a register. All fields will be immutable */
-    INLINE Register(uint8 type = 0) : type(type) {}
+    INLINE Register(Family family = DWORD) : family(family) {}
     /*! Copy constructor */
-    INLINE Register(const Register &other) : type(other.type) {}
+    INLINE Register(const Register &other) : family(other.family) {}
     /*! Copy operator */
     INLINE Register &operator= (const Register &other) {
-      this->type = other.type;
+      this->family = other.family;
       return *this;
     }
     /*! Nothing really happens here */
     INLINE ~Register(void) {}
-    /*! Register type */
-    enum {
-      BOOL  = 0,
-      BYTE  = 1,
-      WORD  = 2,
-      DWORD = 3,
-      QWORD = 4
-    };
-    uint8 type;
+    Family family;
     GBE_CLASS(Register);
   };
 
@@ -66,9 +66,9 @@ namespace gbe
   {
   public:
     /*! Return the index of a newly allocated register register */
-    INLINE RegisterIndex append(uint32 type) {
+    INLINE RegisterIndex append(Register::Family family) {
       const uint32 index = regs.size();
-      const Register reg(type);
+      const Register reg(family);
       assert(index <= MAX_INDEX);
       regs.push_back(reg);
       return index;
@@ -77,6 +77,7 @@ namespace gbe
     template <typename First, typename... Rest>
     INLINE TupleIndex appendTuple(First first, Rest... rest) {
       const TupleIndex index = regTuples.size();
+      assert(first < regs.size());
       regTuples.push_back(first);
       appendTuple(rest...);
       return index;
diff --git a/backend/src/sys/exception.hpp b/backend/src/sys/exception.hpp
new file mode 100644 (file)
index 0000000..6437ef2
--- /dev/null
@@ -0,0 +1,24 @@
+/* 
+ * Copyright © 2012 Intel Corporation
+ *
+ * This library 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 of the License, or (at your option) any later version.
+ *
+ * This library 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. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Benjamin Segovia <benjamin.segovia@intel.com>
+ */
+
+#ifndef __GBE_EXCEPTION_HPP__
+#define __GBE_EXCEPTION_HPP__
+
+#endif /* __GBE_EXCEPTION_HPP__ */
+