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::printInst(const MCInst
*MI
, raw_ostream
&O
,
30 StringRef Annot
, const MCSubtargetInfo
&STI
) {
31 if (!printAliasInstr(MI
, O
))
32 printInstruction(MI
, O
);
33 printAnnotation(O
, Annot
);
36 void MSP430InstPrinter::printPCRelImmOperand(const MCInst
*MI
, unsigned OpNo
,
38 const MCOperand
&Op
= MI
->getOperand(OpNo
);
40 int64_t Imm
= Op
.getImm() * 2 + 2;
46 assert(Op
.isExpr() && "unknown pcrel immediate operand");
47 Op
.getExpr()->print(O
, &MAI
);
51 void MSP430InstPrinter::printOperand(const MCInst
*MI
, unsigned OpNo
,
52 raw_ostream
&O
, const char *Modifier
) {
53 assert((Modifier
== nullptr || Modifier
[0] == 0) && "No modifiers supported");
54 const MCOperand
&Op
= MI
->getOperand(OpNo
);
56 O
<< getRegisterName(Op
.getReg());
57 } else if (Op
.isImm()) {
58 O
<< '#' << Op
.getImm();
60 assert(Op
.isExpr() && "unknown operand kind in printOperand");
62 Op
.getExpr()->print(O
, &MAI
);
66 void MSP430InstPrinter::printSrcMemOperand(const MCInst
*MI
, unsigned OpNo
,
68 const char *Modifier
) {
69 const MCOperand
&Base
= MI
->getOperand(OpNo
);
70 const MCOperand
&Disp
= MI
->getOperand(OpNo
+1);
72 // Print displacement first
74 // If the global address expression is a part of displacement field with a
75 // register base, we should not emit any prefix symbol here, e.g.
79 // Otherwise (!) msp430-as will silently miscompile the output :(
80 if (Base
.getReg() == MSP430::SR
)
84 Disp
.getExpr()->print(O
, &MAI
);
86 assert(Disp
.isImm() && "Expected immediate in displacement field");
90 // Print register base field
91 if ((Base
.getReg() != MSP430::SR
) &&
92 (Base
.getReg() != MSP430::PC
))
93 O
<< '(' << getRegisterName(Base
.getReg()) << ')';
96 void MSP430InstPrinter::printIndRegOperand(const MCInst
*MI
, unsigned OpNo
,
98 const MCOperand
&Base
= MI
->getOperand(OpNo
);
99 O
<< "@" << getRegisterName(Base
.getReg());
102 void MSP430InstPrinter::printPostIndRegOperand(const MCInst
*MI
, unsigned OpNo
,
104 const MCOperand
&Base
= MI
->getOperand(OpNo
);
105 O
<< "@" << getRegisterName(Base
.getReg()) << "+";
108 void MSP430InstPrinter::printCCOperand(const MCInst
*MI
, unsigned OpNo
,
110 unsigned CC
= MI
->getOperand(OpNo
).getImm();
114 llvm_unreachable("Unsupported CC code");
115 case MSP430CC::COND_E
:
118 case MSP430CC::COND_NE
:
121 case MSP430CC::COND_HS
:
124 case MSP430CC::COND_LO
:
127 case MSP430CC::COND_GE
:
130 case MSP430CC::COND_L
:
133 case MSP430CC::COND_N
: