1 //===-- MSP430InstPrinter.cpp - Convert MSP430 MCInst to assembly syntax --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class prints an MSP430 MCInst to a .s file.
12 //===----------------------------------------------------------------------===//
14 #include "MSP430InstPrinter.h"
16 #include "llvm/MC/MCAsmInfo.h"
17 #include "llvm/MC/MCExpr.h"
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/MC/MCInstrInfo.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/FormattedStream.h"
24 #define DEBUG_TYPE "asm-printer"
26 // Include the auto-generated portion of the assembly writer.
27 #define PRINT_ALIAS_INSTR
28 #include "MSP430GenAsmWriter.inc"
30 void MSP430InstPrinter::printInst(const MCInst *MI, raw_ostream &O,
31 StringRef Annot, const MCSubtargetInfo &STI) {
32 if (!printAliasInstr(MI, O))
33 printInstruction(MI, O);
34 printAnnotation(O, Annot);
37 void MSP430InstPrinter::printPCRelImmOperand(const MCInst *MI, unsigned OpNo,
39 const MCOperand &Op = MI->getOperand(OpNo);
41 int64_t Imm = Op.getImm() * 2 + 2;
47 assert(Op.isExpr() && "unknown pcrel immediate operand");
48 Op.getExpr()->print(O, &MAI);
52 void MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
53 raw_ostream &O, const char *Modifier) {
54 assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
55 const MCOperand &Op = MI->getOperand(OpNo);
57 O << getRegisterName(Op.getReg());
58 } else if (Op.isImm()) {
59 O << '#' << Op.getImm();
61 assert(Op.isExpr() && "unknown operand kind in printOperand");
63 Op.getExpr()->print(O, &MAI);
67 void MSP430InstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo,
69 const char *Modifier) {
70 const MCOperand &Base = MI->getOperand(OpNo);
71 const MCOperand &Disp = MI->getOperand(OpNo+1);
73 // Print displacement first
75 // If the global address expression is a part of displacement field with a
76 // register base, we should not emit any prefix symbol here, e.g.
80 // Otherwise (!) msp430-as will silently miscompile the output :(
81 if (Base.getReg() == MSP430::SR)
85 Disp.getExpr()->print(O, &MAI);
87 assert(Disp.isImm() && "Expected immediate in displacement field");
91 // Print register base field
92 if ((Base.getReg() != MSP430::SR) &&
93 (Base.getReg() != MSP430::PC))
94 O << '(' << getRegisterName(Base.getReg()) << ')';
97 void MSP430InstPrinter::printIndRegOperand(const MCInst *MI, unsigned OpNo,
99 const MCOperand &Base = MI->getOperand(OpNo);
100 O << "@" << getRegisterName(Base.getReg());
103 void MSP430InstPrinter::printPostIndRegOperand(const MCInst *MI, unsigned OpNo,
105 const MCOperand &Base = MI->getOperand(OpNo);
106 O << "@" << getRegisterName(Base.getReg()) << "+";
109 void MSP430InstPrinter::printCCOperand(const MCInst *MI, unsigned OpNo,
111 unsigned CC = MI->getOperand(OpNo).getImm();
115 llvm_unreachable("Unsupported CC code");
116 case MSP430CC::COND_E:
119 case MSP430CC::COND_NE:
122 case MSP430CC::COND_HS:
125 case MSP430CC::COND_LO:
128 case MSP430CC::COND_GE:
131 case MSP430CC::COND_L:
134 case MSP430CC::COND_N: