1//===-- MSP430InstPrinter.cpp - Convert MSP430 MCInst to assembly syntax --===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class prints an MSP430 MCInst to a .s file.
11//
12//===----------------------------------------------------------------------===//
13
14#include "MSP430InstPrinter.h"
15#include "MSP430.h"
16#include "llvm/MC/MCAsmInfo.h"
17#include "llvm/MC/MCExpr.h"
18#include "llvm/MC/MCInst.h"
19#include "llvm/Support/ErrorHandling.h"
20#include "llvm/Support/FormattedStream.h"
21using namespace llvm;
22
23#define DEBUG_TYPE "asm-printer"
24
25
26// Include the auto-generated portion of the assembly writer.
27#include "MSP430GenAsmWriter.inc"
28
29void MSP430InstPrinter::printInst(const MCInst *MI, raw_ostream &O,
30                                  StringRef Annot, const MCSubtargetInfo &STI) {
31  printInstruction(MI, O);
32  printAnnotation(O, Annot);
33}
34
35void MSP430InstPrinter::printPCRelImmOperand(const MCInst *MI, unsigned OpNo,
36                                             raw_ostream &O) {
37  const MCOperand &Op = MI->getOperand(OpNo);
38  if (Op.isImm())
39    O << Op.getImm();
40  else {
41    assert(Op.isExpr() && "unknown pcrel immediate operand");
42    Op.getExpr()->print(O, &MAI);
43  }
44}
45
46void MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
47                                     raw_ostream &O, const char *Modifier) {
48  assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
49  const MCOperand &Op = MI->getOperand(OpNo);
50  if (Op.isReg()) {
51    O << getRegisterName(Op.getReg());
52  } else if (Op.isImm()) {
53    O << '#' << Op.getImm();
54  } else {
55    assert(Op.isExpr() && "unknown operand kind in printOperand");
56    O << '#';
57    Op.getExpr()->print(O, &MAI);
58  }
59}
60
61void MSP430InstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo,
62                                           raw_ostream &O,
63                                           const char *Modifier) {
64  const MCOperand &Base = MI->getOperand(OpNo);
65  const MCOperand &Disp = MI->getOperand(OpNo+1);
66
67  // Print displacement first
68
69  // If the global address expression is a part of displacement field with a
70  // register base, we should not emit any prefix symbol here, e.g.
71  //   mov.w &foo, r1
72  // vs
73  //   mov.w glb(r1), r2
74  // Otherwise (!) msp430-as will silently miscompile the output :(
75  if (!Base.getReg())
76    O << '&';
77
78  if (Disp.isExpr())
79    Disp.getExpr()->print(O, &MAI);
80  else {
81    assert(Disp.isImm() && "Expected immediate in displacement field");
82    O << Disp.getImm();
83  }
84
85  // Print register base field
86  if (Base.getReg())
87    O << '(' << getRegisterName(Base.getReg()) << ')';
88}
89
90void MSP430InstPrinter::printCCOperand(const MCInst *MI, unsigned OpNo,
91                                       raw_ostream &O) {
92  unsigned CC = MI->getOperand(OpNo).getImm();
93
94  switch (CC) {
95  default:
96   llvm_unreachable("Unsupported CC code");
97  case MSP430CC::COND_E:
98   O << "eq";
99   break;
100  case MSP430CC::COND_NE:
101   O << "ne";
102   break;
103  case MSP430CC::COND_HS:
104   O << "hs";
105   break;
106  case MSP430CC::COND_LO:
107   O << "lo";
108   break;
109  case MSP430CC::COND_GE:
110   O << "ge";
111   break;
112  case MSP430CC::COND_L:
113   O << 'l';
114   break;
115  }
116}
117