Started to add cleaner and safer cast facilities for the instructions
authorBenjamin Segovia <segovia.benjamin@gmail.com>
Wed, 1 Feb 2012 11:42:17 +0000 (11:42 +0000)
committerKeith Packard <keithp@keithp.com>
Fri, 10 Aug 2012 23:15:11 +0000 (16:15 -0700)
backend/src/ir/ir_instruction.cpp
backend/src/ir/ir_instruction.hpp

index 56b7d5e..ba0d0ef 100644 (file)
@@ -29,7 +29,6 @@ namespace gbe
     class ALIGNED(AlignOf<Instruction>::value) NaryInstruction
     {
     public:
-      INLINE uint32 getOpcode(void) const { return opcode; }
       INLINE uint32 getSrcNum(void) const { return srcNum; }
       INLINE uint32 getDstNum(void) const { return 1; }
       INLINE uint32 getDstIndex(const Function &fn, uint32 ID) const {
index 3ad9058..0938f49 100644 (file)
@@ -60,11 +60,11 @@ namespace gbe
   class Function;
 
   /*! Store the instruction description in 8 bytes */
-  class Instruction
+  class ALIGNED(sizeof(uint64)) Instruction
   {
   public:
     /*! Get the instruction opcode */
-    uint32 getOpcode(void) const;
+    INLINE uint32 getOpcode(void) const { return op; }
     /*! Get the number of sources for this instruction  */
     uint32 getSrcNum(void) const;
     /*! Get the number of destination for this instruction */
@@ -81,10 +81,18 @@ namespace gbe
      *  Otherwise, fill the string with a help message
      */
     bool check(void) const;
+    /*! Indicates if the instruction belongs to instruction type T. Typically, T
+     *  can be BinaryInstruction, UnaryInstruction, LoadInstruction and so on
+     */
+    template <typename T> bool isTypeOf(void) const;
   protected:
-    uint64 opaque; //!< Data depends on the instruction itself
+    uint8 op;                                   //!< Idendifies the instruction
+    uint8 opaque[sizeof(uint64)-sizeof(uint8)]; //!< Remainder of it
   };
 
+  // Check that the instruction is properly formed by the compiler
+  STATIC_ASSERT(sizeof(Instruction) == sizeof(uint64));
+
   /*! Binary instructions are typed. dst and sources share the same type */
   class BinaryInstruction : public Instruction
   {
@@ -148,8 +156,7 @@ namespace gbe
   class LoadImmInstruction : public Instruction
   {
     /*! The value as stored in the instruction */
-    union Value
-    {
+    union Value {
       int8 s8;
       uint8 u8;
       int16 i16;
@@ -167,6 +174,20 @@ namespace gbe
     uint32 getType(void) const;
   };
 
+  /*! Prevents all cast to non-instruction types */
+  template <typename T> INLINE bool Instruction::isTypeOf(void) const {
+    return false;
+  }
+
+  /*! Specialize the instruction. Also performs typechecking first based on the
+   *  opcode. Crashes if it fails
+   */
+  template <typename T>
+  T *cast(Instruction *insn)
+  {
+
+  }
+
 } /* namespace gbe */
 
 #endif /* __GBE_IR_INSTRUCTION_HPP__ */