MSP430InstPrinter.cpp revision 218885
1218885Sdim//===-- MSP430InstPrinter.cpp - Convert MSP430 MCInst to assembly syntax --===//
2218885Sdim//
3218885Sdim//                     The LLVM Compiler Infrastructure
4218885Sdim//
5218885Sdim// This file is distributed under the University of Illinois Open Source
6218885Sdim// License. See LICENSE.TXT for details.
7218885Sdim//
8218885Sdim//===----------------------------------------------------------------------===//
9218885Sdim//
10218885Sdim// This class prints an MSP430 MCInst to a .s file.
11218885Sdim//
12218885Sdim//===----------------------------------------------------------------------===//
13218885Sdim
14218885Sdim#define DEBUG_TYPE "asm-printer"
15218885Sdim#include "MSP430.h"
16218885Sdim#include "MSP430InstPrinter.h"
17218885Sdim#include "llvm/MC/MCInst.h"
18218885Sdim#include "llvm/MC/MCAsmInfo.h"
19218885Sdim#include "llvm/MC/MCExpr.h"
20218885Sdim#include "llvm/Support/ErrorHandling.h"
21218885Sdim#include "llvm/Support/FormattedStream.h"
22218885Sdimusing namespace llvm;
23218885Sdim
24218885Sdim
25218885Sdim// Include the auto-generated portion of the assembly writer.
26218885Sdim#include "MSP430GenAsmWriter.inc"
27218885Sdim
28218885Sdimvoid MSP430InstPrinter::printInst(const MCInst *MI, raw_ostream &O) {
29218885Sdim  printInstruction(MI, O);
30218885Sdim}
31218885Sdim
32218885Sdimvoid MSP430InstPrinter::printPCRelImmOperand(const MCInst *MI, unsigned OpNo,
33218885Sdim                                             raw_ostream &O) {
34218885Sdim  const MCOperand &Op = MI->getOperand(OpNo);
35218885Sdim  if (Op.isImm())
36218885Sdim    O << Op.getImm();
37218885Sdim  else {
38218885Sdim    assert(Op.isExpr() && "unknown pcrel immediate operand");
39218885Sdim    O << *Op.getExpr();
40218885Sdim  }
41218885Sdim}
42218885Sdim
43218885Sdimvoid MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
44218885Sdim                                     raw_ostream &O, const char *Modifier) {
45218885Sdim  assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
46218885Sdim  const MCOperand &Op = MI->getOperand(OpNo);
47218885Sdim  if (Op.isReg()) {
48218885Sdim    O << getRegisterName(Op.getReg());
49218885Sdim  } else if (Op.isImm()) {
50218885Sdim    O << '#' << Op.getImm();
51218885Sdim  } else {
52218885Sdim    assert(Op.isExpr() && "unknown operand kind in printOperand");
53218885Sdim    O << '#' << *Op.getExpr();
54218885Sdim  }
55218885Sdim}
56218885Sdim
57218885Sdimvoid MSP430InstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo,
58218885Sdim                                           raw_ostream &O,
59218885Sdim                                           const char *Modifier) {
60218885Sdim  const MCOperand &Base = MI->getOperand(OpNo);
61218885Sdim  const MCOperand &Disp = MI->getOperand(OpNo+1);
62218885Sdim
63218885Sdim  // Print displacement first
64218885Sdim
65218885Sdim  // If the global address expression is a part of displacement field with a
66218885Sdim  // register base, we should not emit any prefix symbol here, e.g.
67218885Sdim  //   mov.w &foo, r1
68218885Sdim  // vs
69218885Sdim  //   mov.w glb(r1), r2
70218885Sdim  // Otherwise (!) msp430-as will silently miscompile the output :(
71218885Sdim  if (!Base.getReg())
72218885Sdim    O << '&';
73218885Sdim
74218885Sdim  if (Disp.isExpr())
75218885Sdim    O << *Disp.getExpr();
76218885Sdim  else {
77218885Sdim    assert(Disp.isImm() && "Expected immediate in displacement field");
78218885Sdim    O << Disp.getImm();
79218885Sdim  }
80218885Sdim
81218885Sdim  // Print register base field
82218885Sdim  if (Base.getReg())
83218885Sdim    O << '(' << getRegisterName(Base.getReg()) << ')';
84218885Sdim}
85218885Sdim
86218885Sdimvoid MSP430InstPrinter::printCCOperand(const MCInst *MI, unsigned OpNo,
87218885Sdim                                       raw_ostream &O) {
88218885Sdim  unsigned CC = MI->getOperand(OpNo).getImm();
89218885Sdim
90218885Sdim  switch (CC) {
91218885Sdim  default:
92218885Sdim   llvm_unreachable("Unsupported CC code");
93218885Sdim   break;
94218885Sdim  case MSP430CC::COND_E:
95218885Sdim   O << "eq";
96218885Sdim   break;
97218885Sdim  case MSP430CC::COND_NE:
98218885Sdim   O << "ne";
99218885Sdim   break;
100218885Sdim  case MSP430CC::COND_HS:
101218885Sdim   O << "hs";
102218885Sdim   break;
103218885Sdim  case MSP430CC::COND_LO:
104218885Sdim   O << "lo";
105218885Sdim   break;
106218885Sdim  case MSP430CC::COND_GE:
107218885Sdim   O << "ge";
108218885Sdim   break;
109218885Sdim  case MSP430CC::COND_L:
110218885Sdim   O << 'l';
111218885Sdim   break;
112218885Sdim  }
113218885Sdim}
114