1 //===- ARCInstPrinter.cpp - ARC MCInst to assembly syntax -------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This class prints an ARC MCInst to a .s file.
11 //===----------------------------------------------------------------------===//
13 #include "ARCInstPrinter.h"
14 #include "MCTargetDesc/ARCInfo.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/MC/MCExpr.h"
17 #include "llvm/MC/MCInst.h"
18 #include "llvm/MC/MCInstrInfo.h"
19 #include "llvm/MC/MCSymbol.h"
20 #include "llvm/Support/Casting.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/raw_ostream.h"
26 #define DEBUG_TYPE "asm-printer"
28 #include "ARCGenAsmWriter.inc"
31 static const char *BadConditionCode(T cc
) {
32 LLVM_DEBUG(dbgs() << "Unknown condition code passed: " << cc
<< "\n");
33 return "{unknown-cc}";
36 static const char *ARCBRCondCodeToString(ARCCC::BRCondCode BRCC
) {
51 return BadConditionCode(BRCC
);
54 static const char *ARCCondCodeToString(ARCCC::CondCode CC
) {
93 return BadConditionCode(CC
);
96 void ARCInstPrinter::printRegName(raw_ostream
&OS
, unsigned RegNo
) const {
97 OS
<< StringRef(getRegisterName(RegNo
)).lower();
100 void ARCInstPrinter::printInst(const MCInst
*MI
, raw_ostream
&O
,
101 StringRef Annot
, const MCSubtargetInfo
&STI
) {
102 printInstruction(MI
, O
);
103 printAnnotation(O
, Annot
);
106 static void printExpr(const MCExpr
*Expr
, const MCAsmInfo
*MAI
,
109 const MCSymbolRefExpr
*SRE
;
111 if (const auto *CE
= dyn_cast
<MCConstantExpr
>(Expr
)) {
113 OS
.write_hex(CE
->getValue());
117 if (const auto *BE
= dyn_cast
<MCBinaryExpr
>(Expr
)) {
118 SRE
= dyn_cast
<MCSymbolRefExpr
>(BE
->getLHS());
119 const auto *CE
= dyn_cast
<MCConstantExpr
>(BE
->getRHS());
120 assert(SRE
&& CE
&& "Binary expression must be sym+const.");
121 Offset
= CE
->getValue();
123 SRE
= dyn_cast
<MCSymbolRefExpr
>(Expr
);
124 assert(SRE
&& "Unexpected MCExpr type.");
126 assert(SRE
->getKind() == MCSymbolRefExpr::VK_None
);
128 // Symbols are prefixed with '@'
130 SRE
->getSymbol().print(OS
, MAI
);
139 void ARCInstPrinter::printOperand(const MCInst
*MI
, unsigned OpNum
,
141 const MCOperand
&Op
= MI
->getOperand(OpNum
);
143 printRegName(O
, Op
.getReg());
152 assert(Op
.isExpr() && "unknown operand kind in printOperand");
153 printExpr(Op
.getExpr(), &MAI
, O
);
156 void ARCInstPrinter::printMemOperandRI(const MCInst
*MI
, unsigned OpNum
,
158 const MCOperand
&base
= MI
->getOperand(OpNum
);
159 const MCOperand
&offset
= MI
->getOperand(OpNum
+ 1);
160 assert(base
.isReg() && "Base should be register.");
161 assert(offset
.isImm() && "Offset should be immediate.");
162 printRegName(O
, base
.getReg());
163 O
<< "," << offset
.getImm();
166 void ARCInstPrinter::printPredicateOperand(const MCInst
*MI
, unsigned OpNum
,
169 const MCOperand
&Op
= MI
->getOperand(OpNum
);
170 assert(Op
.isImm() && "Predicate operand is immediate.");
171 O
<< ARCCondCodeToString((ARCCC::CondCode
)Op
.getImm());
174 void ARCInstPrinter::printBRCCPredicateOperand(const MCInst
*MI
, unsigned OpNum
,
176 const MCOperand
&Op
= MI
->getOperand(OpNum
);
177 assert(Op
.isImm() && "Predicate operand is immediate.");
178 O
<< ARCBRCondCodeToString((ARCCC::BRCondCode
)Op
.getImm());