blob: 53282ab9b52edabe3321b7e44e0fb946e150bc81 [file] [log] [blame]
Akira Hatanaka83dee992011-09-09 00:13:35 +00001//===-- MipsInstPrinter.cpp - Convert Mips MCInst to assembly syntax ------===//
Akira Hatanaka9c6028f2011-07-07 23:56:50 +00002//
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 Mips MCInst to a .s file.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "asm-printer"
15#include "MipsInstPrinter.h"
16#include "llvm/MC/MCExpr.h"
17#include "llvm/MC/MCInst.h"
Benjamin Kramerdbdff472011-07-08 20:18:13 +000018#include "llvm/Support/ErrorHandling.h"
Akira Hatanaka9c6028f2011-07-07 23:56:50 +000019#include "llvm/Support/raw_ostream.h"
Akira Hatanaka9c6028f2011-07-07 23:56:50 +000020using namespace llvm;
21
22#define GET_INSTRUCTION_NAME
23#include "MipsGenAsmWriter.inc"
24
25const char* Mips::MipsFCCToString(Mips::CondCode CC) {
26 switch (CC) {
27 case FCOND_F:
28 case FCOND_T: return "f";
29 case FCOND_UN:
30 case FCOND_OR: return "un";
31 case FCOND_OEQ:
32 case FCOND_UNE: return "eq";
33 case FCOND_UEQ:
34 case FCOND_ONE: return "ueq";
35 case FCOND_OLT:
36 case FCOND_UGE: return "olt";
37 case FCOND_ULT:
38 case FCOND_OGE: return "ult";
39 case FCOND_OLE:
40 case FCOND_UGT: return "ole";
41 case FCOND_ULE:
42 case FCOND_OGT: return "ule";
43 case FCOND_SF:
44 case FCOND_ST: return "sf";
45 case FCOND_NGLE:
46 case FCOND_GLE: return "ngle";
47 case FCOND_SEQ:
48 case FCOND_SNE: return "seq";
49 case FCOND_NGL:
50 case FCOND_GL: return "ngl";
51 case FCOND_LT:
52 case FCOND_NLT: return "lt";
53 case FCOND_NGE:
54 case FCOND_GE: return "nge";
55 case FCOND_LE:
56 case FCOND_NLE: return "le";
57 case FCOND_NGT:
58 case FCOND_GT: return "ngt";
59 }
Benjamin Kramerdbdff472011-07-08 20:18:13 +000060 llvm_unreachable("Impossible condition code!");
Akira Hatanaka9c6028f2011-07-07 23:56:50 +000061}
62
63StringRef MipsInstPrinter::getOpcodeName(unsigned Opcode) const {
64 return getInstructionName(Opcode);
65}
66
67void MipsInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
Benjamin Kramer20baffb2011-11-06 20:37:06 +000068 OS << '$' << StringRef(getRegisterName(RegNo)).lower();
Akira Hatanaka9c6028f2011-07-07 23:56:50 +000069}
70
Owen Andersona0c3b972011-09-15 23:38:46 +000071void MipsInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
72 StringRef Annot) {
Akira Hatanaka9c6028f2011-07-07 23:56:50 +000073 printInstruction(MI, O);
Owen Andersonbcc3fad2011-09-21 17:58:45 +000074 printAnnotation(O, Annot);
Akira Hatanaka9c6028f2011-07-07 23:56:50 +000075}
76
77void MipsInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
78 raw_ostream &O) {
79 const MCOperand &Op = MI->getOperand(OpNo);
80 if (Op.isReg()) {
81 printRegName(O, Op.getReg());
82 return;
83 }
84
85 if (Op.isImm()) {
86 O << Op.getImm();
87 return;
88 }
89
90 assert(Op.isExpr() && "unknown operand kind in printOperand");
91 O << *Op.getExpr();
92}
93
94void MipsInstPrinter::printUnsignedImm(const MCInst *MI, int opNum,
95 raw_ostream &O) {
96 const MCOperand &MO = MI->getOperand(opNum);
97 if (MO.isImm())
98 O << (unsigned short int)MO.getImm();
99 else
100 printOperand(MI, opNum, O);
101}
102
103void MipsInstPrinter::
104printMemOperand(const MCInst *MI, int opNum, raw_ostream &O) {
105 // Load/Store memory operands -- imm($reg)
106 // If PIC target the target is loaded as the
107 // pattern lw $25,%call16($28)
108 printOperand(MI, opNum+1, O);
109 O << "(";
110 printOperand(MI, opNum, O);
111 O << ")";
112}
113
114void MipsInstPrinter::
115printMemOperandEA(const MCInst *MI, int opNum, raw_ostream &O) {
116 // when using stack locations for not load/store instructions
117 // print the same way as all normal 3 operand instructions.
118 printOperand(MI, opNum, O);
119 O << ", ";
120 printOperand(MI, opNum+1, O);
121 return;
122}
123
124void MipsInstPrinter::
125printFCCOperand(const MCInst *MI, int opNum, raw_ostream &O) {
126 const MCOperand& MO = MI->getOperand(opNum);
127 O << MipsFCCToString((Mips::CondCode)MO.getImm());
128}