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"
20 #include "llvm/Support/FormattedStream.h"
23 #define DEBUG_TYPE "asm-printer"
25 // Include the auto-generated portion of the assembly writer.
26 #define PRINT_ALIAS_INSTR
27 #include "MSP430GenAsmWriter.inc"
29 void MSP430InstPrinter::printRegName(raw_ostream
&O
, MCRegister Reg
) const {
30 O
<< getRegisterName(Reg
);
33 void MSP430InstPrinter::printInst(const MCInst
*MI
, uint64_t Address
,
34 StringRef Annot
, const MCSubtargetInfo
&STI
,
36 if (!printAliasInstr(MI
, Address
, O
))
37 printInstruction(MI
, Address
, O
);
38 printAnnotation(O
, Annot
);
41 void MSP430InstPrinter::printPCRelImmOperand(const MCInst
*MI
, unsigned OpNo
,
43 const MCOperand
&Op
= MI
->getOperand(OpNo
);
45 int64_t Imm
= Op
.getImm() * 2 + 2;
51 assert(Op
.isExpr() && "unknown pcrel immediate operand");
52 Op
.getExpr()->print(O
, &MAI
);
56 void MSP430InstPrinter::printOperand(const MCInst
*MI
, unsigned OpNo
,
57 raw_ostream
&O
, const char *Modifier
) {
58 assert((Modifier
== nullptr || Modifier
[0] == 0) && "No modifiers supported");
59 const MCOperand
&Op
= MI
->getOperand(OpNo
);
61 O
<< getRegisterName(Op
.getReg());
62 } else if (Op
.isImm()) {
63 O
<< '#' << Op
.getImm();
65 assert(Op
.isExpr() && "unknown operand kind in printOperand");
67 Op
.getExpr()->print(O
, &MAI
);
71 void MSP430InstPrinter::printSrcMemOperand(const MCInst
*MI
, unsigned OpNo
,
73 const char *Modifier
) {
74 const MCOperand
&Base
= MI
->getOperand(OpNo
);
75 const MCOperand
&Disp
= MI
->getOperand(OpNo
+1);
77 // Print displacement first
79 // If the global address expression is a part of displacement field with a
80 // register base, we should not emit any prefix symbol here, e.g.
84 // Otherwise (!) msp430-as will silently miscompile the output :(
85 if (Base
.getReg() == MSP430::SR
)
89 Disp
.getExpr()->print(O
, &MAI
);
91 assert(Disp
.isImm() && "Expected immediate in displacement field");
95 // Print register base field
96 if ((Base
.getReg() != MSP430::SR
) &&
97 (Base
.getReg() != MSP430::PC
))
98 O
<< '(' << getRegisterName(Base
.getReg()) << ')';
101 void MSP430InstPrinter::printIndRegOperand(const MCInst
*MI
, unsigned OpNo
,
103 const MCOperand
&Base
= MI
->getOperand(OpNo
);
104 O
<< "@" << getRegisterName(Base
.getReg());
107 void MSP430InstPrinter::printPostIndRegOperand(const MCInst
*MI
, unsigned OpNo
,
109 const MCOperand
&Base
= MI
->getOperand(OpNo
);
110 O
<< "@" << getRegisterName(Base
.getReg()) << "+";
113 void MSP430InstPrinter::printCCOperand(const MCInst
*MI
, unsigned OpNo
,
115 unsigned CC
= MI
->getOperand(OpNo
).getImm();
119 llvm_unreachable("Unsupported CC code");
120 case MSP430CC::COND_E
:
123 case MSP430CC::COND_NE
:
126 case MSP430CC::COND_HS
:
129 case MSP430CC::COND_LO
:
132 case MSP430CC::COND_GE
:
135 case MSP430CC::COND_L
:
138 case MSP430CC::COND_N
: