Skip to content

Commit f80801b

Browse files
committed
[MC][RISCV] Add assembly syntax highlighting for RISCV
1 parent 057564f commit f80801b

File tree

2 files changed

+47
-21
lines changed

2 files changed

+47
-21
lines changed

llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.cpp

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "llvm/MC/MCAsmInfo.h"
1717
#include "llvm/MC/MCExpr.h"
1818
#include "llvm/MC/MCInst.h"
19+
#include "llvm/MC/MCInstPrinter.h"
1920
#include "llvm/MC/MCRegisterInfo.h"
2021
#include "llvm/MC/MCSubtargetInfo.h"
2122
#include "llvm/MC/MCSymbol.h"
@@ -75,7 +76,7 @@ void RISCVInstPrinter::printInst(const MCInst *MI, uint64_t Address,
7576
}
7677

7778
void RISCVInstPrinter::printRegName(raw_ostream &O, MCRegister Reg) const {
78-
O << getRegisterName(Reg);
79+
markup(O, Markup::Register) << getRegisterName(Reg);
7980
}
8081

8182
void RISCVInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
@@ -90,7 +91,7 @@ void RISCVInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
9091
}
9192

9293
if (MO.isImm()) {
93-
O << MO.getImm();
94+
markup(O, Markup::Immediate) << MO.getImm();
9495
return;
9596
}
9697

@@ -110,9 +111,9 @@ void RISCVInstPrinter::printBranchOperand(const MCInst *MI, uint64_t Address,
110111
uint64_t Target = Address + MO.getImm();
111112
if (!STI.hasFeature(RISCV::Feature64Bit))
112113
Target &= 0xffffffff;
113-
O << formatHex(Target);
114+
markup(O, Markup::Target) << formatHex(Target);
114115
} else {
115-
O << MO.getImm();
116+
markup(O, Markup::Target) << MO.getImm();
116117
}
117118
}
118119

@@ -123,11 +124,11 @@ void RISCVInstPrinter::printCSRSystemRegister(const MCInst *MI, unsigned OpNo,
123124
auto SiFiveReg = RISCVSysReg::lookupSiFiveRegByEncoding(Imm);
124125
auto SysReg = RISCVSysReg::lookupSysRegByEncoding(Imm);
125126
if (SiFiveReg && SiFiveReg->haveVendorRequiredFeatures(STI.getFeatureBits()))
126-
O << SiFiveReg->Name;
127+
markup(O, Markup::Register) << SiFiveReg->Name;
127128
else if (SysReg && SysReg->haveRequiredFeatures(STI.getFeatureBits()))
128-
O << SysReg->Name;
129+
markup(O, Markup::Register) << SysReg->Name;
129130
else
130-
O << Imm;
131+
markup(O, Markup::Register) << Imm;
131132
}
132133

133134
void RISCVInstPrinter::printFenceArg(const MCInst *MI, unsigned OpNo,
@@ -162,21 +163,21 @@ void RISCVInstPrinter::printFPImmOperand(const MCInst *MI, unsigned OpNo,
162163
raw_ostream &O) {
163164
unsigned Imm = MI->getOperand(OpNo).getImm();
164165
if (Imm == 1) {
165-
O << "min";
166+
markup(O, Markup::Immediate) << "min";
166167
} else if (Imm == 30) {
167-
O << "inf";
168+
markup(O, Markup::Immediate) << "inf";
168169
} else if (Imm == 31) {
169-
O << "nan";
170+
markup(O, Markup::Immediate) << "nan";
170171
} else {
171172
float FPVal = RISCVLoadFPImm::getFPImm(Imm);
172173
// If the value is an integer, print a .0 fraction. Otherwise, use %g to
173174
// which will not print trailing zeros and will use scientific notation
174175
// if it is shorter than printing as a decimal. The smallest value requires
175176
// 12 digits of precision including the decimal.
176177
if (FPVal == (int)(FPVal))
177-
O << format("%.1f", FPVal);
178+
markup(O, Markup::Immediate) << format("%.1f", FPVal);
178179
else
179-
O << format("%.12g", FPVal);
180+
markup(O, Markup::Immediate) << format("%.12g", FPVal);
180181
}
181182
}
182183

@@ -208,19 +209,20 @@ void RISCVInstPrinter::printVTypeI(const MCInst *MI, unsigned OpNo,
208209
void RISCVInstPrinter::printRlist(const MCInst *MI, unsigned OpNo,
209210
const MCSubtargetInfo &STI, raw_ostream &O) {
210211
unsigned Imm = MI->getOperand(OpNo).getImm();
211-
O << "{";
212+
auto OS = markup(O, Markup::Register);
213+
OS << "{";
212214
switch (Imm) {
213215
case RISCVZC::RLISTENCODE::RA:
214-
O << (ArchRegNames ? "x1" : "ra");
216+
OS << (ArchRegNames ? "x1" : "ra");
215217
break;
216218
case RISCVZC::RLISTENCODE::RA_S0:
217-
O << (ArchRegNames ? "x1, x8" : "ra, s0");
219+
OS << (ArchRegNames ? "x1, x8" : "ra, s0");
218220
break;
219221
case RISCVZC::RLISTENCODE::RA_S0_S1:
220-
O << (ArchRegNames ? "x1, x8-x9" : "ra, s0-s1");
222+
OS << (ArchRegNames ? "x1, x8-x9" : "ra, s0-s1");
221223
break;
222224
case RISCVZC::RLISTENCODE::RA_S0_S2:
223-
O << (ArchRegNames ? "x1, x8-x9, x18" : "ra, s0-s2");
225+
OS << (ArchRegNames ? "x1, x8-x9, x18" : "ra, s0-s2");
224226
break;
225227
case RISCVZC::RLISTENCODE::RA_S0_S3:
226228
case RISCVZC::RLISTENCODE::RA_S0_S4:
@@ -229,16 +231,16 @@ void RISCVInstPrinter::printRlist(const MCInst *MI, unsigned OpNo,
229231
case RISCVZC::RLISTENCODE::RA_S0_S7:
230232
case RISCVZC::RLISTENCODE::RA_S0_S8:
231233
case RISCVZC::RLISTENCODE::RA_S0_S9:
232-
O << (ArchRegNames ? "x1, x8-x9, x18-" : "ra, s0-")
233-
<< getRegisterName(RISCV::X19 + (Imm - RISCVZC::RLISTENCODE::RA_S0_S3));
234+
OS << (ArchRegNames ? "x1, x8-x9, x18-" : "ra, s0-")
235+
<< getRegisterName(RISCV::X19 + (Imm - RISCVZC::RLISTENCODE::RA_S0_S3));
234236
break;
235237
case RISCVZC::RLISTENCODE::RA_S0_S11:
236-
O << (ArchRegNames ? "x1, x8-x9, x18-x27" : "ra, s0-s11");
238+
OS << (ArchRegNames ? "x1, x8-x9, x18-x27" : "ra, s0-s11");
237239
break;
238240
default:
239241
llvm_unreachable("invalid register list");
240242
}
241-
O << "}";
243+
OS << "}";
242244
}
243245

244246
void RISCVInstPrinter::printSpimm(const MCInst *MI, unsigned OpNo,
@@ -256,6 +258,7 @@ void RISCVInstPrinter::printSpimm(const MCInst *MI, unsigned OpNo,
256258
if (Opcode == RISCV::CM_PUSH)
257259
Spimm = -Spimm;
258260

261+
auto OS = markup(O, Markup::Immediate);
259262
RISCVZC::printSpimm(Spimm, O);
260263
}
261264

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# RUN: llvm-mc -triple=riscv64 -mattr=+zcmp,+experimental-zfa --cdis %s | FileCheck %s --strict-whitespace --match-full-lines
2+
3+
# Registers and immediates
4+
0x03 0xe0 0x40 0x00
5+
# CHECK: lwu zero, 4(ra)
6+
7+
# Branch targets
8+
0x63 0x00 0xb5 0x04
9+
# CHECK-NEXT: beq a0, a1, 64
10+
11+
# CSRs
12+
0xf3 0x23 0x10 0xf1
13+
# CHECK-NEXT: csrr t2, mvendorid
14+
15+
# FP immediates
16+
0xd3 0x00 0x1f 0xf0
17+
# CHECK-NEXT: fli.s ft1, inf
18+
0xd3 0x80 0x1e 0xf0
19+
# CHECK-NEXT: fli.s ft1, 65536.0
20+
21+
# Rlist and spimm
22+
0x62 0xbe
23+
# CHECK-NEXT: cm.popret {ra, s0-s1}, 32

0 commit comments

Comments
 (0)