blob: 2e5060b019bbd7d3aba3274ce34b86c4ec572fe5 [file] [log] [blame]
Chris Lattner60d5b5f2010-11-14 19:40:38 +00001//===-- PPCInstPrinter.cpp - Convert PPC 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 PPC MCInst to a .s file.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "asm-printer"
15#include "PPCInstPrinter.h"
Chris Lattner0d1b7d92010-11-14 20:02:39 +000016#include "llvm/MC/MCExpr.h"
Chris Lattner60d5b5f2010-11-14 19:40:38 +000017#include "llvm/MC/MCInst.h"
18//#include "llvm/MC/MCAsmInfo.h"
Chris Lattner60d5b5f2010-11-14 19:40:38 +000019//#include "llvm/ADT/StringExtras.h"
20#include "llvm/Support/raw_ostream.h"
21
22#include "PPCGenRegisterNames.inc"
23#include "PPCGenInstrNames.inc"
24using namespace llvm;
25
26#define GET_INSTRUCTION_NAME
27#define PPCAsmPrinter PPCInstPrinter
28#define MachineInstr MCInst
29#include "PPCGenAsmWriter.inc"
30
31StringRef PPCInstPrinter::getOpcodeName(unsigned Opcode) const {
32 return getInstructionName(Opcode);
33}
34
35
36void PPCInstPrinter::printInst(const MCInst *MI, raw_ostream &O) {
37 // TODO: pseudo ops.
38
39 printInstruction(MI, O);
40}
41
Chris Lattner99889132010-11-14 20:11:21 +000042void PPCInstPrinter::printS5ImmOperand(const MCInst *MI, unsigned OpNo,
43 raw_ostream &O) {
44 char Value = MI->getOperand(OpNo).getImm();
45 Value = (Value << (32-5)) >> (32-5);
46 O << (int)Value;
47}
48
49void PPCInstPrinter::printU5ImmOperand(const MCInst *MI, unsigned OpNo,
50 raw_ostream &O) {
51 unsigned char Value = MI->getOperand(OpNo).getImm();
52 assert(Value <= 31 && "Invalid u5imm argument!");
53 O << (unsigned int)Value;
54}
55
56void PPCInstPrinter::printU6ImmOperand(const MCInst *MI, unsigned OpNo,
57 raw_ostream &O) {
58 unsigned char Value = MI->getOperand(OpNo).getImm();
59 assert(Value <= 63 && "Invalid u6imm argument!");
60 O << (unsigned int)Value;
61}
62
63void PPCInstPrinter::printS16ImmOperand(const MCInst *MI, unsigned OpNo,
64 raw_ostream &O) {
65 O << (short)MI->getOperand(OpNo).getImm();
66}
67
68void PPCInstPrinter::printU16ImmOperand(const MCInst *MI, unsigned OpNo,
69 raw_ostream &O) {
70 O << (unsigned short)MI->getOperand(OpNo).getImm();
71}
72
73void PPCInstPrinter::printS16X4ImmOperand(const MCInst *MI, unsigned OpNo,
74 raw_ostream &O) {
75 if (MI->getOperand(OpNo).isImm()) {
76 O << (short)(MI->getOperand(OpNo).getImm()*4);
77 return;
78 }
79
80 assert(0 && "Unhandled operand");
81#if 0
82 O << "lo16(";
83 printOp(MI->getOperand(OpNo), O);
84 if (TM.getRelocationModel() == Reloc::PIC_)
85 O << "-\"L" << getFunctionNumber() << "$pb\")";
86 else
87 O << ')';
88#endif
89}
90
Chris Lattnerfdb2ded2010-11-14 20:22:56 +000091void PPCInstPrinter::printcrbitm(const MCInst *MI, unsigned OpNo,
92 raw_ostream &O) {
93 unsigned CCReg = MI->getOperand(OpNo).getReg();
94 unsigned RegNo;
95 switch (CCReg) {
96 default: assert(0 && "Unknown CR register");
97 case PPC::CR0: RegNo = 0; break;
98 case PPC::CR1: RegNo = 1; break;
99 case PPC::CR2: RegNo = 2; break;
100 case PPC::CR3: RegNo = 3; break;
101 case PPC::CR4: RegNo = 4; break;
102 case PPC::CR5: RegNo = 5; break;
103 case PPC::CR6: RegNo = 6; break;
104 case PPC::CR7: RegNo = 7; break;
105 }
106 O << (0x80 >> RegNo);
107}
108
109void PPCInstPrinter::printMemRegImm(const MCInst *MI, unsigned OpNo,
110 raw_ostream &O) {
111 printSymbolLo(MI, OpNo, O);
112 O << '(';
113 assert(MI->getOperand(OpNo+1).isReg() && "Bad operand");
114 // FIXME: Simplify.
115 if (MI->getOperand(OpNo+1).isReg() &&
116 MI->getOperand(OpNo+1).getReg() == PPC::R0)
117 O << "0";
118 else
119 printOperand(MI, OpNo+1, O);
120 O << ')';
121}
122
123void PPCInstPrinter::printMemRegImmShifted(const MCInst *MI, unsigned OpNo,
124 raw_ostream &O) {
125 if (MI->getOperand(OpNo).isImm())
126 printS16X4ImmOperand(MI, OpNo, O);
127 else
128 printSymbolLo(MI, OpNo, O);
129 O << '(';
130
131 assert(MI->getOperand(OpNo+1).isReg() && "Bad operand");
132 // FIXME: Simplify.
133 if (MI->getOperand(OpNo+1).isReg() &&
134 MI->getOperand(OpNo+1).getReg() == PPC::R0)
135 O << "0";
136 else
137 printOperand(MI, OpNo+1, O);
138 O << ')';
139}
140
141
142void PPCInstPrinter::printMemRegReg(const MCInst *MI, unsigned OpNo,
143 raw_ostream &O) {
144 // When used as the base register, r0 reads constant zero rather than
145 // the value contained in the register. For this reason, the darwin
146 // assembler requires that we print r0 as 0 (no r) when used as the base.
147 if (MI->getOperand(OpNo).getReg() == PPC::R0)
148 O << "0";
149 else
150 printOperand(MI, OpNo, O);
151 O << ", ";
152 printOperand(MI, OpNo+1, O);
153}
154
155
Chris Lattner99889132010-11-14 20:11:21 +0000156
Chris Lattner0d1b7d92010-11-14 20:02:39 +0000157/// stripRegisterPrefix - This method strips the character prefix from a
158/// register name so that only the number is left. Used by for linux asm.
159const char *stripRegisterPrefix(const char *RegName) {
160 switch (RegName[0]) {
161 case 'r':
162 case 'f':
163 case 'v': return RegName + 1;
164 case 'c': if (RegName[1] == 'r') return RegName + 2;
165 }
166
167 return RegName;
168}
169
170void PPCInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
171 raw_ostream &O) {
172 const MCOperand &Op = MI->getOperand(OpNo);
173 if (Op.isReg()) {
174 const char *RegName = getRegisterName(Op.getReg());
175 // The linux and AIX assembler does not take register prefixes.
176 if (!isDarwinSyntax())
177 RegName = stripRegisterPrefix(RegName);
178
179 O << RegName;
180 return;
181 }
182
183 if (Op.isImm()) {
184 O << Op.getImm();
185 return;
186 }
187
188 assert(Op.isExpr() && "unknown operand kind in printOperand");
189 O << *Op.getExpr();
190}
191