MSP430InstPrinter.cpp revision dce4a407a24b04eebc6a376f8e62b41aaa7b071f
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) {
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    O << *Op.getExpr();
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 << '#' << *Op.getExpr();
57  }
58}
59
60void MSP430InstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo,
61                                           raw_ostream &O,
62                                           const char *Modifier) {
63  const MCOperand &Base = MI->getOperand(OpNo);
64  const MCOperand &Disp = MI->getOperand(OpNo+1);
65
66  // Print displacement first
67
68  // If the global address expression is a part of displacement field with a
69  // register base, we should not emit any prefix symbol here, e.g.
70  //   mov.w &foo, r1
71  // vs
72  //   mov.w glb(r1), r2
73  // Otherwise (!) msp430-as will silently miscompile the output :(
74  if (!Base.getReg())
75    O << '&';
76
77  if (Disp.isExpr())
78    O << *Disp.getExpr();
79  else {
80    assert(Disp.isImm() && "Expected immediate in displacement field");
81    O << Disp.getImm();
82  }
83
84  // Print register base field
85  if (Base.getReg())
86    O << '(' << getRegisterName(Base.getReg()) << ')';
87}
88
89void MSP430InstPrinter::printCCOperand(const MCInst *MI, unsigned OpNo,
90                                       raw_ostream &O) {
91  unsigned CC = MI->getOperand(OpNo).getImm();
92
93  switch (CC) {
94  default:
95   llvm_unreachable("Unsupported CC code");
96  case MSP430CC::COND_E:
97   O << "eq";
98   break;
99  case MSP430CC::COND_NE:
100   O << "ne";
101   break;
102  case MSP430CC::COND_HS:
103   O << "hs";
104   break;
105  case MSP430CC::COND_LO:
106   O << "lo";
107   break;
108  case MSP430CC::COND_GE:
109   O << "ge";
110   break;
111  case MSP430CC::COND_L:
112   O << 'l';
113   break;
114  }
115}
116