1 //===-- MSP430InstPrinter.cpp - Convert MSP430 MCInst to assembly syntax --===//
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 MSP430 MCInst to a .s file.
11 //===----------------------------------------------------------------------===//
13 #include "MSP430InstPrinter.h"
15 #include "llvm/MC/MCAsmInfo.h"
16 #include "llvm/MC/MCExpr.h"
17 #include "llvm/MC/MCInst.h"
18 #include "llvm/MC/MCInstrInfo.h"
19 #include "llvm/Support/ErrorHandling.h"
22 #define DEBUG_TYPE "asm-printer"
24 // Include the auto-generated portion of the assembly writer.
25 #define PRINT_ALIAS_INSTR
26 #include "MSP430GenAsmWriter.inc"
28 void MSP430InstPrinter::printRegName(raw_ostream
&O
, MCRegister Reg
) {
29 O
<< getRegisterName(Reg
);
32 void MSP430InstPrinter::printInst(const MCInst
*MI
, uint64_t Address
,
33 StringRef Annot
, const MCSubtargetInfo
&STI
,
35 if (!printAliasInstr(MI
, Address
, O
))
36 printInstruction(MI
, Address
, O
);
37 printAnnotation(O
, Annot
);
40 void MSP430InstPrinter::printPCRelImmOperand(const MCInst
*MI
, unsigned OpNo
,
42 const MCOperand
&Op
= MI
->getOperand(OpNo
);
44 int64_t Imm
= Op
.getImm() * 2 + 2;
50 assert(Op
.isExpr() && "unknown pcrel immediate operand");
51 Op
.getExpr()->print(O
, &MAI
);
55 void MSP430InstPrinter::printOperand(const MCInst
*MI
, unsigned OpNo
,
56 raw_ostream
&O
, const char *Modifier
) {
57 assert((Modifier
== nullptr || Modifier
[0] == 0) && "No modifiers supported");
58 const MCOperand
&Op
= MI
->getOperand(OpNo
);
60 O
<< getRegisterName(Op
.getReg());
61 } else if (Op
.isImm()) {
62 O
<< '#' << Op
.getImm();
64 assert(Op
.isExpr() && "unknown operand kind in printOperand");
66 Op
.getExpr()->print(O
, &MAI
);
70 void MSP430InstPrinter::printSrcMemOperand(const MCInst
*MI
, unsigned OpNo
,
72 const char *Modifier
) {
73 const MCOperand
&Base
= MI
->getOperand(OpNo
);
74 const MCOperand
&Disp
= MI
->getOperand(OpNo
+1);
76 // Print displacement first
78 // If the global address expression is a part of displacement field with a
79 // register base, we should not emit any prefix symbol here, e.g.
83 // Otherwise (!) msp430-as will silently miscompile the output :(
84 if (Base
.getReg() == MSP430::SR
)
88 Disp
.getExpr()->print(O
, &MAI
);
90 assert(Disp
.isImm() && "Expected immediate in displacement field");
94 // Print register base field
95 if ((Base
.getReg() != MSP430::SR
) &&
96 (Base
.getReg() != MSP430::PC
))
97 O
<< '(' << getRegisterName(Base
.getReg()) << ')';
100 void MSP430InstPrinter::printIndRegOperand(const MCInst
*MI
, unsigned OpNo
,
102 const MCOperand
&Base
= MI
->getOperand(OpNo
);
103 O
<< "@" << getRegisterName(Base
.getReg());
106 void MSP430InstPrinter::printPostIndRegOperand(const MCInst
*MI
, unsigned OpNo
,
108 const MCOperand
&Base
= MI
->getOperand(OpNo
);
109 O
<< "@" << getRegisterName(Base
.getReg()) << "+";
112 void MSP430InstPrinter::printCCOperand(const MCInst
*MI
, unsigned OpNo
,
114 unsigned CC
= MI
->getOperand(OpNo
).getImm();
118 llvm_unreachable("Unsupported CC code");
119 case MSP430CC::COND_E
:
122 case MSP430CC::COND_NE
:
125 case MSP430CC::COND_HS
:
128 case MSP430CC::COND_LO
:
131 case MSP430CC::COND_GE
:
134 case MSP430CC::COND_L
:
137 case MSP430CC::COND_N
: