LLVM  6.0.0svn
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 #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"
21 using 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 
30  StringRef Annot, const MCSubtargetInfo &STI) {
31  printInstruction(MI, O);
32  printAnnotation(O, Annot);
33 }
34 
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  Op.getExpr()->print(O, &MAI);
43  }
44 }
45 
46 void 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 << '#';
57  Op.getExpr()->print(O, &MAI);
58  }
59 }
60 
61 void MSP430InstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo,
62  raw_ostream &O,
63  const char *Modifier) {
64  const MCOperand &Base = MI->getOperand(OpNo);
65  const MCOperand &Disp = MI->getOperand(OpNo+1);
66 
67  // Print displacement first
68 
69  // If the global address expression is a part of displacement field with a
70  // register base, we should not emit any prefix symbol here, e.g.
71  // mov.w &foo, r1
72  // vs
73  // mov.w glb(r1), r2
74  // Otherwise (!) msp430-as will silently miscompile the output :(
75  if (!Base.getReg())
76  O << '&';
77 
78  if (Disp.isExpr())
79  Disp.getExpr()->print(O, &MAI);
80  else {
81  assert(Disp.isImm() && "Expected immediate in displacement field");
82  O << Disp.getImm();
83  }
84 
85  // Print register base field
86  if (Base.getReg())
87  O << '(' << getRegisterName(Base.getReg()) << ')';
88 }
89 
90 void MSP430InstPrinter::printCCOperand(const MCInst *MI, unsigned OpNo,
91  raw_ostream &O) {
92  unsigned CC = MI->getOperand(OpNo).getImm();
93 
94  switch (CC) {
95  default:
96  llvm_unreachable("Unsupported CC code");
97  case MSP430CC::COND_E:
98  O << "eq";
99  break;
100  case MSP430CC::COND_NE:
101  O << "ne";
102  break;
103  case MSP430CC::COND_HS:
104  O << "hs";
105  break;
106  case MSP430CC::COND_LO:
107  O << "lo";
108  break;
109  case MSP430CC::COND_GE:
110  O << "ge";
111  break;
112  case MSP430CC::COND_L:
113  O << 'l';
114  break;
115  }
116 }
bool isImm() const
Definition: MCInst.h:59
void printInst(const MCInst *MI, raw_ostream &O, StringRef Annot, const MCSubtargetInfo &STI) override
Print the specified MCInst to the specified raw_ostream.
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
static const char * getRegisterName(unsigned RegNo)
bool isReg() const
Definition: MCInst.h:58
void printPCRelImmOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O)
unsigned getReg() const
Returns the register number.
Definition: MCInst.h:65
const MCExpr * getExpr() const
Definition: MCInst.h:96
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:159
int64_t getImm() const
Definition: MCInst.h:76
void print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens=false) const
Definition: MCExpr.cpp:40
void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O, const char *Modifier=nullptr)
bool isExpr() const
Definition: MCInst.h:61
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void printCCOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O)
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:173
const MCAsmInfo & MAI
Definition: MCInstPrinter.h:47
void printInstruction(const MCInst *MI, raw_ostream &O)
void printSrcMemOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O, const char *Modifier=nullptr)
MCSubtargetInfo - Generic base class for all target subtargets.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void printAnnotation(raw_ostream &OS, StringRef Annot)
Utility function for printing annotations.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:35