Skip to content

Commit

Permalink
[RTGTest] Add a few instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
maerhart committed Jan 9, 2025
1 parent e099496 commit afd891b
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
83 changes: 83 additions & 0 deletions include/circt/Dialect/RTGTest/IR/RTGTestOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,86 @@ def ConstantTestOp : RTGTestOp<"constant_test", [
let assemblyFormat = "type($result) attr-dict";
let hasFolder = 1;
}

//===- Instruction Formats -------------------------------------------------===//

class InstFormatIOpBase<string mnemonic, int opcode7, int funct3>
: RTGTestOp<mnemonic, [InstructionOpInterface]> {

let arguments = (ins IntegerRegisterType:$rd,
IntegerRegisterType:$rs,
Imm12Type:$imm);

let assemblyFormat = "$rd `,` $rs `,` $imm attr-dict";

let extraClassDeclaration = [{
static void printInstructionBinary(llvm::raw_ostream &os,
ArrayRef<Attribute> operands) {
FoldAdaptor adaptor(operands);

auto binary = APInt(12, cast<Imm12Attr>(adaptor.getImm()).getValue())
.concat(APInt(5, cast<rtg::RegisterAttrInterface>(
adaptor.getRs()).getClassIndex()))
.concat(APInt(3, }] # funct3 # [{))
.concat(APInt(5, cast<rtg::RegisterAttrInterface>(
adaptor.getRd()).getClassIndex()))
.concat(APInt(7, }] # opcode7 # [{));

SmallVector<char> str;
binary.toStringUnsigned(str, 16);
os << str;
}

static void printInstructionAssembly(llvm::raw_ostream &os,
ArrayRef<Attribute> operands) {
FoldAdaptor adaptor(operands);

os << getOperationName().split('.').second << " "
<< cast<rtg::RegisterAttrInterface>(adaptor.getRd())
.getRegisterAssembly()
<< ", "
<< cast<Imm12Attr>(adaptor.getImm()).getValue()
<< "("
<< cast<rtg::RegisterAttrInterface>(adaptor.getRs())
.getRegisterAssembly()
<< ")";
}
}];
}

class InstFormatIImmOpBase<string mnemonic, int opcode7, int funct12>
: RTGTestOp<mnemonic, [InstructionOpInterface]> {

let assemblyFormat = "attr-dict";

let extraClassDeclaration = [{
static void printInstructionBinary(llvm::raw_ostream &os,
ArrayRef<Attribute> operands) {
auto binary = APInt(12, }] # funct12 # [{)
.concat(APInt(13, 0))
.concat(llvm::APInt(7, }] # opcode7 # [{));

SmallVector<char> str;
binary.toStringUnsigned(str, 16);
os << str;
}

static void printInstructionAssembly(llvm::raw_ostream &os,
ArrayRef<Attribute> operands) {
os << getOperationName().split('.').second;
}
}];
}

//===- Instructions -------------------------------------------------------===//

def JALROp : InstFormatIOpBase<"jalr", 0b1100111, 0b000>;

def LBOp : InstFormatIOpBase<"lb", 0b0000011, 0b000>;
def LHOp : InstFormatIOpBase<"lh", 0b0000011, 0b001>;
def LWOp : InstFormatIOpBase<"lw", 0b0000011, 0b010>;
def LBUOp : InstFormatIOpBase<"lbu", 0b0000011, 0b100>;
def LHUOp : InstFormatIOpBase<"lhu", 0b0000011, 0b101>;

def ECALLOp : InstFormatIImmOpBase<"ecall", 0b1110011, 0b000000000000>;
def EBREAKOp : InstFormatIImmOpBase<"ebreak", 0b1110011, 0b000000000001>;
22 changes: 22 additions & 0 deletions test/Dialect/RTGTest/IR/basic.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,28 @@ rtg.test @immediates : !rtg.dict<> {
rtgtest.immediate #rtgtest.imm32<3> : !rtgtest.imm32
}

// CHECK-LABEL: @instructions
rtg.test @instructions : !rtg.dict<imm: !rtgtest.imm12, rd: !rtgtest.ireg, rs: !rtgtest.ireg> {
// CHECK: ([[IMM:%.+]]: !rtgtest.imm12, [[RD:%.+]]: !rtgtest.ireg, [[RS:%.+]]: !rtgtest.ireg)
^bb0(%imm: !rtgtest.imm12, %rd: !rtgtest.ireg, %rs: !rtgtest.ireg):
// CHECK: rtgtest.jalr [[RD]], [[RS]], [[IMM]]
rtgtest.jalr %rd, %rs, %imm
// CHECK: rtgtest.lb [[RD]], [[RS]], [[IMM]]
rtgtest.lb %rd, %rs, %imm
// CHECK: rtgtest.lh [[RD]], [[RS]], [[IMM]]
rtgtest.lh %rd, %rs, %imm
// CHECK: rtgtest.lw [[RD]], [[RS]], [[IMM]]
rtgtest.lw %rd, %rs, %imm
// CHECK: rtgtest.lbu [[RD]], [[RS]], [[IMM]]
rtgtest.lbu %rd, %rs, %imm
// CHECK: rtgtest.lhu [[RD]], [[RS]], [[IMM]]
rtgtest.lhu %rd, %rs, %imm
// CHECK: rtgtest.ecall
rtgtest.ecall
// CHECK: rtgtest.ebreak
rtgtest.ebreak
}

// -----

rtg.test @immediateTooBig : !rtg.dict<> {
Expand Down

0 comments on commit afd891b

Please sign in to comment.