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"
15249423Sdim#include "MSP430InstPrinter.h"
16218885Sdim#include "MSP430.h"
17218885Sdim#include "llvm/MC/MCAsmInfo.h"
18218885Sdim#include "llvm/MC/MCExpr.h"
19249423Sdim#include "llvm/MC/MCInst.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
28226633Sdimvoid MSP430InstPrinter::printInst(const MCInst *MI, raw_ostream &O,
29226633Sdim                                  StringRef Annot) {
30218885Sdim  printInstruction(MI, O);
31226633Sdim  printAnnotation(O, Annot);
32218885Sdim}
33218885Sdim
34218885Sdimvoid MSP430InstPrinter::printPCRelImmOperand(const MCInst *MI, unsigned OpNo,
35218885Sdim                                             raw_ostream &O) {
36218885Sdim  const MCOperand &Op = MI->getOperand(OpNo);
37218885Sdim  if (Op.isImm())
38218885Sdim    O << Op.getImm();
39218885Sdim  else {
40218885Sdim    assert(Op.isExpr() && "unknown pcrel immediate operand");
41218885Sdim    O << *Op.getExpr();
42218885Sdim  }
43218885Sdim}
44218885Sdim
45218885Sdimvoid MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
46218885Sdim                                     raw_ostream &O, const char *Modifier) {
47218885Sdim  assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
48218885Sdim  const MCOperand &Op = MI->getOperand(OpNo);
49218885Sdim  if (Op.isReg()) {
50218885Sdim    O << getRegisterName(Op.getReg());
51218885Sdim  } else if (Op.isImm()) {
52218885Sdim    O << '#' << Op.getImm();
53218885Sdim  } else {
54218885Sdim    assert(Op.isExpr() && "unknown operand kind in printOperand");
55218885Sdim    O << '#' << *Op.getExpr();
56218885Sdim  }
57218885Sdim}
58218885Sdim
59218885Sdimvoid MSP430InstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo,
60218885Sdim                                           raw_ostream &O,
61218885Sdim                                           const char *Modifier) {
62218885Sdim  const MCOperand &Base = MI->getOperand(OpNo);
63218885Sdim  const MCOperand &Disp = MI->getOperand(OpNo+1);
64218885Sdim
65218885Sdim  // Print displacement first
66218885Sdim
67218885Sdim  // If the global address expression is a part of displacement field with a
68218885Sdim  // register base, we should not emit any prefix symbol here, e.g.
69218885Sdim  //   mov.w &foo, r1
70218885Sdim  // vs
71218885Sdim  //   mov.w glb(r1), r2
72218885Sdim  // Otherwise (!) msp430-as will silently miscompile the output :(
73218885Sdim  if (!Base.getReg())
74218885Sdim    O << '&';
75218885Sdim
76218885Sdim  if (Disp.isExpr())
77218885Sdim    O << *Disp.getExpr();
78218885Sdim  else {
79218885Sdim    assert(Disp.isImm() && "Expected immediate in displacement field");
80218885Sdim    O << Disp.getImm();
81218885Sdim  }
82218885Sdim
83218885Sdim  // Print register base field
84218885Sdim  if (Base.getReg())
85218885Sdim    O << '(' << getRegisterName(Base.getReg()) << ')';
86218885Sdim}
87218885Sdim
88218885Sdimvoid MSP430InstPrinter::printCCOperand(const MCInst *MI, unsigned OpNo,
89218885Sdim                                       raw_ostream &O) {
90218885Sdim  unsigned CC = MI->getOperand(OpNo).getImm();
91218885Sdim
92218885Sdim  switch (CC) {
93218885Sdim  default:
94218885Sdim   llvm_unreachable("Unsupported CC code");
95218885Sdim  case MSP430CC::COND_E:
96218885Sdim   O << "eq";
97218885Sdim   break;
98218885Sdim  case MSP430CC::COND_NE:
99218885Sdim   O << "ne";
100218885Sdim   break;
101218885Sdim  case MSP430CC::COND_HS:
102218885Sdim   O << "hs";
103218885Sdim   break;
104218885Sdim  case MSP430CC::COND_LO:
105218885Sdim   O << "lo";
106218885Sdim   break;
107218885Sdim  case MSP430CC::COND_GE:
108218885Sdim   O << "ge";
109218885Sdim   break;
110218885Sdim  case MSP430CC::COND_L:
111218885Sdim   O << 'l';
112218885Sdim   break;
113218885Sdim  }
114218885Sdim}
115