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