MBlazeInstPrinter.cpp revision 13a949071ce2c887ae81db9f6880a660ff33a76d
1//===-- MBlazeInstPrinter.cpp - Convert MBlaze 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 MBlaze MCInst to a .s file.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "asm-printer"
15#include "MBlaze.h"
16#include "MBlazeInstPrinter.h"
17#include "llvm/MC/MCInst.h"
18#include "llvm/MC/MCAsmInfo.h"
19#include "llvm/MC/MCExpr.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 "MBlazeGenAsmWriter.inc"
27
28void MBlazeInstPrinter::printInst(const MCInst *MI, raw_ostream &O) {
29  printInstruction(MI, O);
30}
31
32void MBlazeInstPrinter::printPCRelImmOperand(const MCInst *MI, unsigned OpNo,
33                                             raw_ostream &O) {
34  const MCOperand &Op = MI->getOperand(OpNo);
35  if (Op.isImm())
36    O << Op.getImm();
37  else {
38    assert(Op.isExpr() && "unknown pcrel immediate operand");
39    O << *Op.getExpr();
40  }
41}
42
43void MBlazeInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
44                                     raw_ostream &O, const char *Modifier) {
45  assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
46  const MCOperand &Op = MI->getOperand(OpNo);
47  if (Op.isReg()) {
48    O << getRegisterName(Op.getReg());
49  } else if (Op.isImm()) {
50    O << (int32_t)Op.getImm();
51  } else {
52    assert(Op.isExpr() && "unknown operand kind in printOperand");
53    O << *Op.getExpr();
54  }
55}
56
57void MBlazeInstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo,
58                                           raw_ostream &O,
59                                           const char *Modifier) {
60  const MCOperand &Base = MI->getOperand(OpNo);
61  const MCOperand &Disp = MI->getOperand(OpNo+1);
62
63  // Print displacement first
64
65  // If the global address expression is a part of displacement field with a
66  // register base, we should not emit any prefix symbol here, e.g.
67  //   mov.w &foo, r1
68  // vs
69  //   mov.w glb(r1), r2
70  // Otherwise (!) msp430-as will silently miscompile the output :(
71  if (!Base.getReg())
72    O << '&';
73
74  if (Disp.isExpr())
75    O << *Disp.getExpr();
76  else {
77    assert(Disp.isImm() && "Expected immediate in displacement field");
78    O << Disp.getImm();
79  }
80
81  // Print register base field
82  if (Base.getReg())
83    O << getRegisterName(Base.getReg());
84}
85
86void MBlazeInstPrinter::printFSLImm(const MCInst *MI, int OpNo,
87                                    raw_ostream &O) {
88  const MCOperand &MO = MI->getOperand(OpNo);
89  if (MO.isImm())
90    O << "rfsl" << MO.getImm();
91  else
92    printOperand(MI, OpNo, O, NULL);
93}
94
95void MBlazeInstPrinter::printUnsignedImm(const MCInst *MI, int OpNo,
96                                        raw_ostream &O) {
97  const MCOperand &MO = MI->getOperand(OpNo);
98  if (MO.isImm())
99    O << MO.getImm();
100  else
101    printOperand(MI, OpNo, O, NULL);
102}
103
104void MBlazeInstPrinter::printMemOperand(const MCInst *MI, int OpNo,
105                                        raw_ostream &O, const char *Modifier ) {
106  printOperand(MI, OpNo+1, O, NULL);
107  O << ", ";
108  printOperand(MI, OpNo, O, NULL);
109}
110
111/*
112void MBlazeInstPrinter::printCCOperand(const MCInst *MI, unsigned OpNo,
113                                       raw_ostream &O) {
114  unsigned CC = MI->getOperand(OpNo).getImm();
115
116  switch (CC) {
117  default:
118   llvm_unreachable("Unsupported CC code");
119   break;
120  case MBlazeCC::COND_E:
121   O << "eq";
122   break;
123  case MBlazeCC::COND_NE:
124   O << "ne";
125   break;
126  case MBlazeCC::COND_HS:
127   O << "hs";
128   break;
129  case MBlazeCC::COND_LO:
130   O << "lo";
131   break;
132  case MBlazeCC::COND_GE:
133   O << "ge";
134   break;
135  case MBlazeCC::COND_L:
136   O << 'l';
137   break;
138  }
139}
140*/
141