[llvm] [LoongArch][MC] Add basic assembly syntax highlighting for LoongArch (PR #218946)

Rong Mantle Bao via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 26 07:59:00 PDT 2026


https://github.com/CSharperMantle created https://github.com/llvm/llvm-project/pull/218946

This PR adds color highlighting of registers and immediates in LoongArch disassembly.

This is similar to #65853, but does not distinguish regular immediates and branch offsets, due to needing to edit the same file as #217971. Branch target colorization will be added in a follow-up PR.

After this PR:

<img width="1492" height="868" alt="colorized output" src="https://github.com/user-attachments/assets/b48f494e-8354-4c98-8394-b6c20c473c06" />

CC: @SixWeining, @wangleiat


>From 1f4e14abac1de70a6235ffa2a7fed9379563c590 Mon Sep 17 00:00:00 2001
From: Rong Bao <rong.bao at csmantle.top>
Date: Wed, 26 Aug 2026 22:40:28 +0800
Subject: [PATCH 1/2] [LoongArch][MC] Un-hardcode disassembly operands of
 SET_CFR_{TRUE,FALSE}

This makes them markup-able in the next commit.
---
 .../Target/LoongArch/LoongArchFloatInstrFormats.td   |  6 ++++--
 .../LoongArch/MCTargetDesc/LoongArchInstPrinter.cpp  | 10 ++++++++++
 .../LoongArch/MCTargetDesc/LoongArchInstPrinter.h    |  2 ++
 llvm/test/MC/Disassembler/LoongArch/lit.local.cfg    |  2 ++
 llvm/test/MC/Disassembler/LoongArch/marked-up.txt    | 12 ++++++++++++
 5 files changed, 30 insertions(+), 2 deletions(-)
 create mode 100644 llvm/test/MC/Disassembler/LoongArch/lit.local.cfg
 create mode 100644 llvm/test/MC/Disassembler/LoongArch/marked-up.txt

diff --git a/llvm/lib/Target/LoongArch/LoongArchFloatInstrFormats.td b/llvm/lib/Target/LoongArch/LoongArchFloatInstrFormats.td
index ce42236895c76..c8fd203aa5898 100644
--- a/llvm/lib/Target/LoongArch/LoongArchFloatInstrFormats.td
+++ b/llvm/lib/Target/LoongArch/LoongArchFloatInstrFormats.td
@@ -219,6 +219,8 @@ class FP_STORE_2RI12<bits<32> op, RegisterClass rc = FPR32>
                  "$fd, $rj, $imm12">;
 } // hasSideEffects = 0, mayLoad = 0, mayStore = 1
 
+def CFRSetDest : RegisterOperand<CFR, "printCFRSetDest">;
+
 // This class is used to define `SET_CFR_{FALSE,TRUE}` instructions which are
 // used to expand `PseudoCopyCFR`.
 class SET_CFR<bits<32> op, string opcstr>
@@ -226,7 +228,7 @@ class SET_CFR<bits<32> op, string opcstr>
   let isCodeGenOnly = 1;
   let fj = 0; // fa0
   let fk = 0; // fa0
-  let AsmString = opcstr # "\t$cd, $$fa0, $$fa0";
-  let OutOperandList = (outs CFR:$cd);
+  let AsmString = opcstr # "\t$cd";
+  let OutOperandList = (outs CFRSetDest:$cd);
   let InOperandList = (ins);
 }
diff --git a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchInstPrinter.cpp b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchInstPrinter.cpp
index f912af330e34e..a05ea75c80c09 100644
--- a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchInstPrinter.cpp
+++ b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchInstPrinter.cpp
@@ -95,6 +95,16 @@ void LoongArchInstPrinter::printAtomicMemOp(const MCInst *MI, unsigned OpNo,
   printRegName(O, MO.getReg());
 }
 
+void LoongArchInstPrinter::printCFRSetDest(const MCInst *MI, unsigned OpNo,
+                                           const MCSubtargetInfo &STI,
+                                           raw_ostream &O) {
+  printRegName(O, MI->getOperand(OpNo).getReg());
+  O << ", ";
+  printRegName(O, LoongArch::F0);
+  O << ", ";
+  printRegName(O, LoongArch::F0);
+}
+
 const char *LoongArchInstPrinter::getRegisterName(MCRegister Reg) {
   // Default print reg alias name
   return getRegisterName(Reg, NumericReg ? LoongArch::NoRegAltName
diff --git a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchInstPrinter.h b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchInstPrinter.h
index 235f967f50ff4..bf96d869f185c 100644
--- a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchInstPrinter.h
+++ b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchInstPrinter.h
@@ -31,6 +31,8 @@ class LoongArchInstPrinter : public MCInstPrinter {
   void printRegName(raw_ostream &O, MCRegister Reg) override;
   void printAtomicMemOp(const MCInst *MI, unsigned OpNo,
                         const MCSubtargetInfo &STI, raw_ostream &O);
+  void printCFRSetDest(const MCInst *MI, unsigned OpNo,
+                       const MCSubtargetInfo &STI, raw_ostream &O);
 
   // Autogenerated by tblgen.
   std::pair<const char *, uint64_t>
diff --git a/llvm/test/MC/Disassembler/LoongArch/lit.local.cfg b/llvm/test/MC/Disassembler/LoongArch/lit.local.cfg
new file mode 100644
index 0000000000000..cc24278acbb41
--- /dev/null
+++ b/llvm/test/MC/Disassembler/LoongArch/lit.local.cfg
@@ -0,0 +1,2 @@
+if not "LoongArch" in config.root.targets:
+    config.unsupported = True
diff --git a/llvm/test/MC/Disassembler/LoongArch/marked-up.txt b/llvm/test/MC/Disassembler/LoongArch/marked-up.txt
new file mode 100644
index 0000000000000..c1f2d153f6457
--- /dev/null
+++ b/llvm/test/MC/Disassembler/LoongArch/marked-up.txt
@@ -0,0 +1,12 @@
+# RUN: llvm-mc -triple=loongarch64 -mattr=+f --mdis < %s | FileCheck %s
+
+## Ordinary registers and immediates remain marked up.
+0xe5 0xd8 0x83 0x02
+# CHECK: addi.w <reg:$a1>, <reg:$a3>, <imm:246>
+
+## The encoding is decoded as the architectural compare instruction. This
+## covers the visible fixed-register spelling used by SET_CFR.
+0x00 0x00 0x10 0x0c
+# CHECK: fcmp.caf.s <reg:$fcc0>, <reg:$fa0>, <reg:$fa0>
+0x00 0x00 0x16 0x0c
+# CHECK: fcmp.cueq.s <reg:$fcc0>, <reg:$fa0>, <reg:$fa0>

>From 999fabdb0a3b2d67912e11e8e6ec0f31b8f67df3 Mon Sep 17 00:00:00 2001
From: Rong Bao <rong.bao at csmantle.top>
Date: Wed, 26 Aug 2026 21:47:38 +0800
Subject: [PATCH 2/2] [LoongArch][MC] Add basic assembly syntax highlighting
 for LoongArch

This version only highlights registers and immediates. Branch targets
are to be added in a follow-up PR due to dependency on #217971.

Link: https://github.com/llvm/llvm-project/pull/65853 ("[MC][RISCV] Add
 assembly syntax highlighting for RISCV")
Link: https://reviews.llvm.org/D159162 ("[llvm] Add assembly syntax
 highlighting", AArch64)
Link: https://reviews.llvm.org/D159241 ("[llvm] Add assembly syntax
 highlighting for X86")
---
 .../Target/LoongArch/MCTargetDesc/LoongArchInstPrinter.cpp    | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchInstPrinter.cpp b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchInstPrinter.cpp
index a05ea75c80c09..3a0aa20e56a33 100644
--- a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchInstPrinter.cpp
+++ b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchInstPrinter.cpp
@@ -65,7 +65,7 @@ void LoongArchInstPrinter::printInst(const MCInst *MI, uint64_t Address,
 }
 
 void LoongArchInstPrinter::printRegName(raw_ostream &O, MCRegister Reg) {
-  O << '$' << getRegisterName(Reg);
+  markup(O, Markup::Register) << '$' << getRegisterName(Reg);
 }
 
 void LoongArchInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
@@ -79,7 +79,7 @@ void LoongArchInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
   }
 
   if (MO.isImm()) {
-    O << MO.getImm();
+    markup(O, Markup::Immediate) << MO.getImm();
     return;
   }
 



More information about the llvm-commits mailing list