blob: 432167e33554260037086ebc6f76f69f33e5de08 [file] [log] [blame]
Chris Lattnera76eab42010-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"
Hal Finkelfeea6532013-03-26 20:08:20 +000016#include "MCTargetDesc/PPCMCTargetDesc.h"
Evan Cheng11424442011-07-26 00:24:13 +000017#include "MCTargetDesc/PPCPredicates.h"
Chris Lattner7a5c57e2010-11-14 20:02:39 +000018#include "llvm/MC/MCExpr.h"
Chris Lattnera76eab42010-11-14 19:40:38 +000019#include "llvm/MC/MCInst.h"
Craig Topperdab9e352012-04-02 07:01:04 +000020#include "llvm/MC/MCInstrInfo.h"
Chris Lattnera76eab42010-11-14 19:40:38 +000021#include "llvm/Support/raw_ostream.h"
Chris Lattnera76eab42010-11-14 19:40:38 +000022using namespace llvm;
23
Chris Lattnera76eab42010-11-14 19:40:38 +000024#include "PPCGenAsmWriter.inc"
25
Rafael Espindolad6860522011-06-02 02:34:55 +000026void PPCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
27 OS << getRegisterName(RegNo);
Rafael Espindola08600bc2011-05-30 20:20:15 +000028}
Chris Lattnera76eab42010-11-14 19:40:38 +000029
Owen Andersona0c3b972011-09-15 23:38:46 +000030void PPCInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
31 StringRef Annot) {
Chris Lattner219cc3d2010-11-14 21:39:51 +000032 // Check for slwi/srwi mnemonics.
33 if (MI->getOpcode() == PPC::RLWINM) {
34 unsigned char SH = MI->getOperand(2).getImm();
35 unsigned char MB = MI->getOperand(3).getImm();
36 unsigned char ME = MI->getOperand(4).getImm();
37 bool useSubstituteMnemonic = false;
38 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
39 O << "\tslwi "; useSubstituteMnemonic = true;
40 }
41 if (SH <= 31 && MB == (32-SH) && ME == 31) {
42 O << "\tsrwi "; useSubstituteMnemonic = true;
43 SH = 32-SH;
44 }
45 if (useSubstituteMnemonic) {
46 printOperand(MI, 0, O);
47 O << ", ";
48 printOperand(MI, 1, O);
49 O << ", " << (unsigned int)SH;
Owen Andersona0c3b972011-09-15 23:38:46 +000050
Owen Andersonbcc3fad2011-09-21 17:58:45 +000051 printAnnotation(O, Annot);
Chris Lattner219cc3d2010-11-14 21:39:51 +000052 return;
53 }
54 }
55
56 if ((MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) &&
57 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
58 O << "\tmr ";
59 printOperand(MI, 0, O);
60 O << ", ";
61 printOperand(MI, 1, O);
Owen Andersonbcc3fad2011-09-21 17:58:45 +000062 printAnnotation(O, Annot);
Chris Lattner219cc3d2010-11-14 21:39:51 +000063 return;
64 }
65
66 if (MI->getOpcode() == PPC::RLDICR) {
67 unsigned char SH = MI->getOperand(2).getImm();
68 unsigned char ME = MI->getOperand(3).getImm();
69 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
70 if (63-SH == ME) {
71 O << "\tsldi ";
72 printOperand(MI, 0, O);
73 O << ", ";
74 printOperand(MI, 1, O);
75 O << ", " << (unsigned int)SH;
Owen Andersonbcc3fad2011-09-21 17:58:45 +000076 printAnnotation(O, Annot);
Chris Lattner219cc3d2010-11-14 21:39:51 +000077 return;
78 }
79 }
80
Chris Lattnera76eab42010-11-14 19:40:38 +000081 printInstruction(MI, O);
Owen Andersonbcc3fad2011-09-21 17:58:45 +000082 printAnnotation(O, Annot);
Chris Lattnera76eab42010-11-14 19:40:38 +000083}
84
Chris Lattnerf2cb69c2010-11-14 21:51:37 +000085
86void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
87 raw_ostream &O,
88 const char *Modifier) {
Chris Lattnerf2cb69c2010-11-14 21:51:37 +000089 unsigned Code = MI->getOperand(OpNo).getImm();
Hal Finkel460e94d2012-06-22 23:10:08 +000090
Chris Lattnerf2cb69c2010-11-14 21:51:37 +000091 if (StringRef(Modifier) == "cc") {
92 switch ((PPC::Predicate)Code) {
Chris Lattnerf2cb69c2010-11-14 21:51:37 +000093 case PPC::PRED_LT: O << "lt"; return;
94 case PPC::PRED_LE: O << "le"; return;
95 case PPC::PRED_EQ: O << "eq"; return;
96 case PPC::PRED_GE: O << "ge"; return;
97 case PPC::PRED_GT: O << "gt"; return;
98 case PPC::PRED_NE: O << "ne"; return;
99 case PPC::PRED_UN: O << "un"; return;
100 case PPC::PRED_NU: O << "nu"; return;
101 }
102 }
103
104 assert(StringRef(Modifier) == "reg" &&
105 "Need to specify 'cc' or 'reg' as predicate op modifier!");
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000106 printOperand(MI, OpNo+1, O);
107}
108
Chris Lattner94881432010-11-14 20:11:21 +0000109void PPCInstPrinter::printS5ImmOperand(const MCInst *MI, unsigned OpNo,
110 raw_ostream &O) {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000111 int Value = MI->getOperand(OpNo).getImm();
Richard Smith228e6d42012-08-24 23:29:28 +0000112 Value = SignExtend32<5>(Value);
Chris Lattner94881432010-11-14 20:11:21 +0000113 O << (int)Value;
114}
115
116void PPCInstPrinter::printU5ImmOperand(const MCInst *MI, unsigned OpNo,
117 raw_ostream &O) {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000118 unsigned int Value = MI->getOperand(OpNo).getImm();
Chris Lattner94881432010-11-14 20:11:21 +0000119 assert(Value <= 31 && "Invalid u5imm argument!");
120 O << (unsigned int)Value;
121}
122
123void PPCInstPrinter::printU6ImmOperand(const MCInst *MI, unsigned OpNo,
124 raw_ostream &O) {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000125 unsigned int Value = MI->getOperand(OpNo).getImm();
Chris Lattner94881432010-11-14 20:11:21 +0000126 assert(Value <= 63 && "Invalid u6imm argument!");
127 O << (unsigned int)Value;
128}
129
130void PPCInstPrinter::printS16ImmOperand(const MCInst *MI, unsigned OpNo,
131 raw_ostream &O) {
Ulrich Weigand41789de2013-05-23 22:26:41 +0000132 if (MI->getOperand(OpNo).isImm())
133 O << (short)MI->getOperand(OpNo).getImm();
134 else
135 printOperand(MI, OpNo, O);
Chris Lattner94881432010-11-14 20:11:21 +0000136}
137
138void PPCInstPrinter::printU16ImmOperand(const MCInst *MI, unsigned OpNo,
139 raw_ostream &O) {
140 O << (unsigned short)MI->getOperand(OpNo).getImm();
141}
142
Chris Lattner3dc9bb22010-11-14 21:20:46 +0000143void PPCInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
144 raw_ostream &O) {
145 if (!MI->getOperand(OpNo).isImm())
146 return printOperand(MI, OpNo, O);
147
148 // Branches can take an immediate operand. This is used by the branch
Ulrich Weigandb9d5d072013-05-03 19:53:04 +0000149 // selection pass to print .+8, an eight byte displacement from the PC.
150 O << ".+";
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000151 printAbsAddrOperand(MI, OpNo, O);
Chris Lattner3dc9bb22010-11-14 21:20:46 +0000152}
153
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000154void PPCInstPrinter::printAbsAddrOperand(const MCInst *MI, unsigned OpNo,
155 raw_ostream &O) {
156 O << (int)MI->getOperand(OpNo).getImm()*4;
157}
Chris Lattner3dc9bb22010-11-14 21:20:46 +0000158
159
Chris Lattner0dcd8002010-11-14 20:22:56 +0000160void PPCInstPrinter::printcrbitm(const MCInst *MI, unsigned OpNo,
161 raw_ostream &O) {
162 unsigned CCReg = MI->getOperand(OpNo).getReg();
163 unsigned RegNo;
164 switch (CCReg) {
Craig Toppere55c5562012-02-07 02:50:20 +0000165 default: llvm_unreachable("Unknown CR register");
Chris Lattner0dcd8002010-11-14 20:22:56 +0000166 case PPC::CR0: RegNo = 0; break;
167 case PPC::CR1: RegNo = 1; break;
168 case PPC::CR2: RegNo = 2; break;
169 case PPC::CR3: RegNo = 3; break;
170 case PPC::CR4: RegNo = 4; break;
171 case PPC::CR5: RegNo = 5; break;
172 case PPC::CR6: RegNo = 6; break;
173 case PPC::CR7: RegNo = 7; break;
174 }
175 O << (0x80 >> RegNo);
176}
177
178void PPCInstPrinter::printMemRegImm(const MCInst *MI, unsigned OpNo,
179 raw_ostream &O) {
Ulrich Weigand41789de2013-05-23 22:26:41 +0000180 printS16ImmOperand(MI, OpNo, O);
Chris Lattner0dcd8002010-11-14 20:22:56 +0000181 O << '(';
Chris Lattnerfd56ee22010-11-15 03:51:13 +0000182 if (MI->getOperand(OpNo+1).getReg() == PPC::R0)
Chris Lattner0dcd8002010-11-14 20:22:56 +0000183 O << "0";
184 else
185 printOperand(MI, OpNo+1, O);
186 O << ')';
187}
188
Chris Lattner0dcd8002010-11-14 20:22:56 +0000189void PPCInstPrinter::printMemRegReg(const MCInst *MI, unsigned OpNo,
190 raw_ostream &O) {
191 // When used as the base register, r0 reads constant zero rather than
192 // the value contained in the register. For this reason, the darwin
193 // assembler requires that we print r0 as 0 (no r) when used as the base.
194 if (MI->getOperand(OpNo).getReg() == PPC::R0)
195 O << "0";
196 else
197 printOperand(MI, OpNo, O);
198 O << ", ";
199 printOperand(MI, OpNo+1, O);
200}
201
202
Chris Lattner94881432010-11-14 20:11:21 +0000203
Chris Lattner7a5c57e2010-11-14 20:02:39 +0000204/// stripRegisterPrefix - This method strips the character prefix from a
205/// register name so that only the number is left. Used by for linux asm.
Benjamin Krameraef5bd02010-11-25 16:42:51 +0000206static const char *stripRegisterPrefix(const char *RegName) {
Chris Lattner7a5c57e2010-11-14 20:02:39 +0000207 switch (RegName[0]) {
208 case 'r':
209 case 'f':
210 case 'v': return RegName + 1;
211 case 'c': if (RegName[1] == 'r') return RegName + 2;
212 }
213
214 return RegName;
215}
216
217void PPCInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
218 raw_ostream &O) {
219 const MCOperand &Op = MI->getOperand(OpNo);
220 if (Op.isReg()) {
221 const char *RegName = getRegisterName(Op.getReg());
222 // The linux and AIX assembler does not take register prefixes.
223 if (!isDarwinSyntax())
224 RegName = stripRegisterPrefix(RegName);
225
226 O << RegName;
227 return;
228 }
229
230 if (Op.isImm()) {
231 O << Op.getImm();
232 return;
233 }
234
235 assert(Op.isExpr() && "unknown operand kind in printOperand");
236 O << *Op.getExpr();
237}
Chris Lattnercfb62872010-11-14 21:54:34 +0000238