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
, MCRegister Reg
) {
97 OS
<< StringRef(getRegisterName(Reg
)).lower();
100 void ARCInstPrinter::printInst(const MCInst
*MI
, uint64_t Address
,
101 StringRef Annot
, const MCSubtargetInfo
&STI
,
103 printInstruction(MI
, Address
, O
);
104 printAnnotation(O
, Annot
);
107 static void printExpr(const MCExpr
*Expr
, const MCAsmInfo
*MAI
,
110 const MCSymbolRefExpr
*SRE
;
112 if (const auto *CE
= dyn_cast
<MCConstantExpr
>(Expr
)) {
114 OS
.write_hex(CE
->getValue());
118 if (const auto *BE
= dyn_cast
<MCBinaryExpr
>(Expr
)) {
119 SRE
= dyn_cast
<MCSymbolRefExpr
>(BE
->getLHS());
120 const auto *CE
= dyn_cast
<MCConstantExpr
>(BE
->getRHS());
121 assert(SRE
&& CE
&& "Binary expression must be sym+const.");
122 Offset
= CE
->getValue();
124 SRE
= dyn_cast
<MCSymbolRefExpr
>(Expr
);
125 assert(SRE
&& "Unexpected MCExpr type.");
127 assert(SRE
->getKind() == MCSymbolRefExpr::VK_None
);
129 // Symbols are prefixed with '@'
131 SRE
->getSymbol().print(OS
, MAI
);
140 void ARCInstPrinter::printOperand(const MCInst
*MI
, unsigned OpNum
,
142 const MCOperand
&Op
= MI
->getOperand(OpNum
);
144 printRegName(O
, Op
.getReg());
153 assert(Op
.isExpr() && "unknown operand kind in printOperand");
154 printExpr(Op
.getExpr(), &MAI
, O
);
157 void ARCInstPrinter::printMemOperandRI(const MCInst
*MI
, unsigned OpNum
,
159 const MCOperand
&base
= MI
->getOperand(OpNum
);
160 const MCOperand
&offset
= MI
->getOperand(OpNum
+ 1);
161 assert(base
.isReg() && "Base should be register.");
162 assert(offset
.isImm() && "Offset should be immediate.");
163 printRegName(O
, base
.getReg());
164 O
<< "," << offset
.getImm();
167 void ARCInstPrinter::printPredicateOperand(const MCInst
*MI
, unsigned OpNum
,
170 const MCOperand
&Op
= MI
->getOperand(OpNum
);
171 assert(Op
.isImm() && "Predicate operand is immediate.");
172 O
<< ARCCondCodeToString((ARCCC::CondCode
)Op
.getImm());
175 void ARCInstPrinter::printBRCCPredicateOperand(const MCInst
*MI
, unsigned OpNum
,
177 const MCOperand
&Op
= MI
->getOperand(OpNum
);
178 assert(Op
.isImm() && "Predicate operand is immediate.");
179 O
<< ARCBRCondCodeToString((ARCCC::BRCondCode
)Op
.getImm());
182 void ARCInstPrinter::printCCOperand(const MCInst
*MI
, int OpNum
,
184 O
<< ARCCondCodeToString((ARCCC::CondCode
)MI
->getOperand(OpNum
).getImm());
187 void ARCInstPrinter::printU6ShiftedBy(unsigned ShiftBy
, const MCInst
*MI
,
188 int OpNum
, raw_ostream
&O
) {
189 const MCOperand
&MO
= MI
->getOperand(OpNum
);
191 unsigned Value
= MO
.getImm();
192 unsigned Value2
= Value
>> ShiftBy
;
193 if (Value2
> 0x3F || (Value2
<< ShiftBy
!= Value
)) {
194 errs() << "!!! Instruction has out-of-range U6 immediate operand:\n"
195 << " Opcode is " << MI
->getOpcode() << "; operand value is "
198 errs() << " scaled by " << (1 << ShiftBy
) << "\n";
199 assert(false && "instruction has wrong format");
202 printOperand(MI
, OpNum
, O
);
205 void ARCInstPrinter::printU6(const MCInst
*MI
, int OpNum
, raw_ostream
&O
) {
206 printU6ShiftedBy(0, MI
, OpNum
, O
);