@@ -63,3 +63,86 @@ def ConstantTestOp : RTGTestOp<"constant_test", [
63
63
let assemblyFormat = "type($result) attr-dict";
64
64
let hasFolder = 1;
65
65
}
66
+
67
+ //===- Instruction Formats -------------------------------------------------===//
68
+
69
+ class InstFormatIOpBase<string mnemonic, int opcode7, int funct3>
70
+ : RTGTestOp<mnemonic, [InstructionOpInterface]> {
71
+
72
+ let arguments = (ins IntegerRegisterType:$rd,
73
+ IntegerRegisterType:$rs,
74
+ Imm12Type:$imm);
75
+
76
+ let assemblyFormat = "$rd `,` $rs `,` $imm attr-dict";
77
+
78
+ let extraClassDeclaration = [{
79
+ static void printInstructionBinary(llvm::raw_ostream &os,
80
+ ArrayRef<Attribute> operands) {
81
+ FoldAdaptor adaptor(operands);
82
+
83
+ auto binary = APInt(12, cast<Imm12Attr>(adaptor.getImm()).getValue())
84
+ .concat(APInt(5, cast<rtg::RegisterAttrInterface>(
85
+ adaptor.getRs()).getClassIndex()))
86
+ .concat(APInt(3, }] # funct3 # [{))
87
+ .concat(APInt(5, cast<rtg::RegisterAttrInterface>(
88
+ adaptor.getRd()).getClassIndex()))
89
+ .concat(APInt(7, }] # opcode7 # [{));
90
+
91
+ SmallVector<char> str;
92
+ binary.toStringUnsigned(str, 16);
93
+ os << str;
94
+ }
95
+
96
+ static void printInstructionAssembly(llvm::raw_ostream &os,
97
+ ArrayRef<Attribute> operands) {
98
+ FoldAdaptor adaptor(operands);
99
+
100
+ os << getOperationName().split('.').second << " "
101
+ << cast<rtg::RegisterAttrInterface>(adaptor.getRd())
102
+ .getRegisterAssembly()
103
+ << ", "
104
+ << cast<Imm12Attr>(adaptor.getImm()).getValue()
105
+ << "("
106
+ << cast<rtg::RegisterAttrInterface>(adaptor.getRs())
107
+ .getRegisterAssembly()
108
+ << ")";
109
+ }
110
+ }];
111
+ }
112
+
113
+ class InstFormatIImmOpBase<string mnemonic, int opcode7, int funct12>
114
+ : RTGTestOp<mnemonic, [InstructionOpInterface]> {
115
+
116
+ let assemblyFormat = "attr-dict";
117
+
118
+ let extraClassDeclaration = [{
119
+ static void printInstructionBinary(llvm::raw_ostream &os,
120
+ ArrayRef<Attribute> operands) {
121
+ auto binary = APInt(12, }] # funct12 # [{)
122
+ .concat(APInt(13, 0))
123
+ .concat(llvm::APInt(7, }] # opcode7 # [{));
124
+
125
+ SmallVector<char> str;
126
+ binary.toStringUnsigned(str, 16);
127
+ os << str;
128
+ }
129
+
130
+ static void printInstructionAssembly(llvm::raw_ostream &os,
131
+ ArrayRef<Attribute> operands) {
132
+ os << getOperationName().split('.').second;
133
+ }
134
+ }];
135
+ }
136
+
137
+ //===- Instructions -------------------------------------------------------===//
138
+
139
+ def JALROp : InstFormatIOpBase<"jalr", 0b1100111, 0b000>;
140
+
141
+ def LBOp : InstFormatIOpBase<"lb", 0b0000011, 0b000>;
142
+ def LHOp : InstFormatIOpBase<"lh", 0b0000011, 0b001>;
143
+ def LWOp : InstFormatIOpBase<"lw", 0b0000011, 0b010>;
144
+ def LBUOp : InstFormatIOpBase<"lbu", 0b0000011, 0b100>;
145
+ def LHUOp : InstFormatIOpBase<"lhu", 0b0000011, 0b101>;
146
+
147
+ def ECALLOp : InstFormatIImmOpBase<"ecall", 0b1110011, 0b000000000000>;
148
+ def EBREAKOp : InstFormatIImmOpBase<"ebreak", 0b1110011, 0b000000000001>;
0 commit comments