diff --git a/include/circt/Dialect/RTG/IR/RTGISAAssemblyInterfaces.td b/include/circt/Dialect/RTG/IR/RTGISAAssemblyInterfaces.td index 0bcebc5dacc4..8999aace9da8 100644 --- a/include/circt/Dialect/RTG/IR/RTGISAAssemblyInterfaces.td +++ b/include/circt/Dialect/RTG/IR/RTGISAAssemblyInterfaces.td @@ -68,4 +68,33 @@ def InstructionOpInterface : OpInterface<"InstructionOpInterface"> { ]; } +def InstructionOpAdaptorTrait : NativeOpTrait<"InstructionOpAdaptorTrait"> { + let extraConcreteClassDeclaration = [{ + static void printInstructionBinary(llvm::raw_ostream &os, + FoldAdaptor adaptor); + + static void printInstructionAssembly(llvm::raw_ostream &os, + FoldAdaptor adaptor); + }]; + + let extraConcreteClassDefinition = [{ + void $cppClass::printInstructionBinary( + llvm::raw_ostream &os, llvm::ArrayRef operands) { + printInstructionBinary(os, FoldAdaptor(operands)); + } + + void $cppClass::printInstructionAssembly( + llvm::raw_ostream &os, llvm::ArrayRef operands) { + printInstructionAssembly(os, FoldAdaptor(operands)); + } + }]; + + let cppNamespace = "::circt::rtg"; +} + +def InstructionOpAdaptor : TraitList<[ + DeclareOpInterfaceMethods, + InstructionOpAdaptorTrait, +]>; + #endif // CIRCT_DIALECT_RTG_IR_RTGISAASSEMBLYINTERFACES_TD diff --git a/include/circt/Dialect/RTG/IR/RTGISAAssemblyOpInterfaces.h b/include/circt/Dialect/RTG/IR/RTGISAAssemblyOpInterfaces.h index c3b59177b510..981284126ac2 100644 --- a/include/circt/Dialect/RTG/IR/RTGISAAssemblyOpInterfaces.h +++ b/include/circt/Dialect/RTG/IR/RTGISAAssemblyOpInterfaces.h @@ -19,4 +19,15 @@ #include "circt/Dialect/RTG/IR/RTGISAAssemblyOpInterfaces.h.inc" +namespace circt { +namespace rtg { + +template +class InstructionOpAdaptorTrait + : public mlir::OpTrait::TraitBase { +}; + +} // namespace rtg +} // namespace circt + #endif // CIRCT_DIALECT_RTG_IR_RTGISAASSEMBLYOPINTERFACES_H diff --git a/include/circt/Dialect/RTGTest/IR/RTGTestOps.td b/include/circt/Dialect/RTGTest/IR/RTGTestOps.td index 8d6c83c382dd..9bea77dc3419 100644 --- a/include/circt/Dialect/RTGTest/IR/RTGTestOps.td +++ b/include/circt/Dialect/RTGTest/IR/RTGTestOps.td @@ -67,7 +67,7 @@ def ConstantTestOp : RTGTestOp<"constant_test", [ //===- Instruction Formats -------------------------------------------------===// class InstFormatIOpBase - : RTGTestOp<"rv32i." # mnemonic, [InstructionOpInterface]> { + : RTGTestOp<"rv32i." # mnemonic, [InstructionOpAdaptor]> { let arguments = (ins IntegerRegisterType:$rd, IntegerRegisterType:$rs, @@ -75,11 +75,9 @@ class InstFormatIOpBase let assemblyFormat = "$rd `,` $rs `,` $imm attr-dict"; - let extraClassDeclaration = [{ - static void printInstructionBinary(llvm::raw_ostream &os, - ArrayRef operands) { - FoldAdaptor adaptor(operands); - + let extraClassDefinition = [{ + void $cppClass::printInstructionBinary(llvm::raw_ostream &os, + FoldAdaptor adaptor) { auto binary = APInt(12, cast(adaptor.getImm()).getValue()) .concat(APInt(5, cast( adaptor.getRs()).getClassIndex())) @@ -93,10 +91,8 @@ class InstFormatIOpBase os << str; } - static void printInstructionAssembly(llvm::raw_ostream &os, - ArrayRef operands) { - FoldAdaptor adaptor(operands); - + void $cppClass::printInstructionAssembly(llvm::raw_ostream &os, + FoldAdaptor adaptor) { os << getOperationName().rsplit('.').second << " " << cast(adaptor.getRd()) .getRegisterAssembly() @@ -111,13 +107,13 @@ class InstFormatIOpBase } class InstFormatIImmOpBase - : RTGTestOp<"rv32i." # mnemonic, [InstructionOpInterface]> { + : RTGTestOp<"rv32i." # mnemonic, [InstructionOpAdaptor]> { let assemblyFormat = "attr-dict"; - let extraClassDeclaration = [{ - static void printInstructionBinary(llvm::raw_ostream &os, - ArrayRef operands) { + let extraClassDefinition = [{ + void $cppClass::printInstructionBinary(llvm::raw_ostream &os, + FoldAdaptor adaptor) { auto binary = APInt(12, }] # funct12 # [{) .concat(APInt(13, 0)) .concat(llvm::APInt(7, }] # opcode7 # [{)); @@ -127,8 +123,8 @@ class InstFormatIImmOpBase os << str; } - static void printInstructionAssembly(llvm::raw_ostream &os, - ArrayRef operands) { + void $cppClass::printInstructionAssembly(llvm::raw_ostream &os, + FoldAdaptor adaptor) { os << getOperationName().rsplit('.').second; } }];