blob: 7149de4c47830b774f07addca54cb4c5ff728b48 [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 Lattner1520fd62010-11-14 21:20:46 +000091void PPCInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
92 raw_ostream &O) {
93 if (!MI->getOperand(OpNo).isImm())
94 return printOperand(MI, OpNo, O);
95
96 // Branches can take an immediate operand. This is used by the branch
97 // selection pass to print $+8, an eight byte displacement from the PC.
98 O << "$+" << MI->getOperand(OpNo).getImm()*4;
99}
100
101
102
103
Chris Lattnerfdb2ded2010-11-14 20:22:56 +0000104void PPCInstPrinter::printcrbitm(const MCInst *MI, unsigned OpNo,
105 raw_ostream &O) {
106 unsigned CCReg = MI->getOperand(OpNo).getReg();
107 unsigned RegNo;
108 switch (CCReg) {
109 default: assert(0 && "Unknown CR register");
110 case PPC::CR0: RegNo = 0; break;
111 case PPC::CR1: RegNo = 1; break;
112 case PPC::CR2: RegNo = 2; break;
113 case PPC::CR3: RegNo = 3; break;
114 case PPC::CR4: RegNo = 4; break;
115 case PPC::CR5: RegNo = 5; break;
116 case PPC::CR6: RegNo = 6; break;
117 case PPC::CR7: RegNo = 7; break;
118 }
119 O << (0x80 >> RegNo);
120}
121
122void PPCInstPrinter::printMemRegImm(const MCInst *MI, unsigned OpNo,
123 raw_ostream &O) {
124 printSymbolLo(MI, OpNo, O);
125 O << '(';
126 assert(MI->getOperand(OpNo+1).isReg() && "Bad operand");
127 // FIXME: Simplify.
128 if (MI->getOperand(OpNo+1).isReg() &&
129 MI->getOperand(OpNo+1).getReg() == PPC::R0)
130 O << "0";
131 else
132 printOperand(MI, OpNo+1, O);
133 O << ')';
134}
135
136void PPCInstPrinter::printMemRegImmShifted(const MCInst *MI, unsigned OpNo,
137 raw_ostream &O) {
138 if (MI->getOperand(OpNo).isImm())
139 printS16X4ImmOperand(MI, OpNo, O);
140 else
141 printSymbolLo(MI, OpNo, O);
142 O << '(';
143
144 assert(MI->getOperand(OpNo+1).isReg() && "Bad operand");
145 // FIXME: Simplify.
146 if (MI->getOperand(OpNo+1).isReg() &&
147 MI->getOperand(OpNo+1).getReg() == PPC::R0)
148 O << "0";
149 else
150 printOperand(MI, OpNo+1, O);
151 O << ')';
152}
153
154
155void PPCInstPrinter::printMemRegReg(const MCInst *MI, unsigned OpNo,
156 raw_ostream &O) {
157 // When used as the base register, r0 reads constant zero rather than
158 // the value contained in the register. For this reason, the darwin
159 // assembler requires that we print r0 as 0 (no r) when used as the base.
160 if (MI->getOperand(OpNo).getReg() == PPC::R0)
161 O << "0";
162 else
163 printOperand(MI, OpNo, O);
164 O << ", ";
165 printOperand(MI, OpNo+1, O);
166}
167
168
Chris Lattner99889132010-11-14 20:11:21 +0000169
Chris Lattner0d1b7d92010-11-14 20:02:39 +0000170/// stripRegisterPrefix - This method strips the character prefix from a
171/// register name so that only the number is left. Used by for linux asm.
172const char *stripRegisterPrefix(const char *RegName) {
173 switch (RegName[0]) {
174 case 'r':
175 case 'f':
176 case 'v': return RegName + 1;
177 case 'c': if (RegName[1] == 'r') return RegName + 2;
178 }
179
180 return RegName;
181}
182
183void PPCInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
184 raw_ostream &O) {
185 const MCOperand &Op = MI->getOperand(OpNo);
186 if (Op.isReg()) {
187 const char *RegName = getRegisterName(Op.getReg());
188 // The linux and AIX assembler does not take register prefixes.
189 if (!isDarwinSyntax())
190 RegName = stripRegisterPrefix(RegName);
191
192 O << RegName;
193 return;
194 }
195
196 if (Op.isImm()) {
197 O << Op.getImm();
198 return;
199 }
200
201 assert(Op.isExpr() && "unknown operand kind in printOperand");
202 O << *Op.getExpr();
203}
204
Chris Lattner58d014f2010-11-14 21:33:07 +0000205void PPCInstPrinter::printSymbolLo(const MCInst *MI, unsigned OpNo,
206 raw_ostream &O) {
207 if (MI->getOperand(OpNo).isImm())
208 printS16ImmOperand(MI, OpNo, O);
209 else
210 printOperand(MI, OpNo, O);
211}
212
213void PPCInstPrinter::printSymbolHi(const MCInst *MI, unsigned OpNo,
214 raw_ostream &O) {
215 if (MI->getOperand(OpNo).isImm())
216 printS16ImmOperand(MI, OpNo, O);
217 else
218 printOperand(MI, OpNo, O);
219}