LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MSP430InstPrinter.cpp
Go to the documentation of this file.
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"
22 using namespace llvm;
23 
24 
25 // Include the auto-generated portion of the assembly writer.
26 #include "MSP430GenAsmWriter.inc"
27 
29  StringRef Annot) {
30  printInstruction(MI, O);
31  printAnnotation(O, Annot);
32 }
33 
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 
45 void 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 
59 void 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 
88 void 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 }
bool isReg() const
Definition: MCInst.h:56
static const char * getRegisterName(unsigned RegNo)
void printPCRelImmOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O)
void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O, const char *Modifier=0)
#define llvm_unreachable(msg)
unsigned getReg() const
getReg - Returns the register number.
Definition: MCInst.h:63
bool isImm() const
Definition: MCInst.h:57
const MCExpr * getExpr() const
Definition: MCInst.h:93
virtual void printInst(const MCInst *MI, raw_ostream &O, StringRef Annot)
bool isExpr() const
Definition: MCInst.h:59
void printCCOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O)
int64_t getImm() const
Definition: MCInst.h:74
void printSrcMemOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O, const char *Modifier=0)
void printInstruction(const MCInst *MI, raw_ostream &O)
void printAnnotation(raw_ostream &OS, StringRef Annot)
Utility function for printing annotations.
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:163