blob: 31bb3bdabcba0c68232ae1bd73960f3871500784 [file] [log] [blame]
Chris Lattner158e1f52006-02-05 05:50:24 +00001//===-- SparcAsmPrinter.cpp - Sparc LLVM assembly writer ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner158e1f52006-02-05 05:50:24 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to GAS-format SPARC assembly language.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner1ef9cd42006-12-19 22:59:26 +000015#define DEBUG_TYPE "asm-printer"
Chris Lattner158e1f52006-02-05 05:50:24 +000016#include "Sparc.h"
17#include "SparcInstrInfo.h"
Chris Lattner3773afe2009-06-19 15:48:10 +000018#include "SparcTargetMachine.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000019#include "llvm/CodeGen/AsmPrinter.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000020#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000021#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerff68a422010-02-10 00:36:00 +000022#include "llvm/MC/MCStreamer.h"
Chris Lattner4cd44982009-09-13 17:14:04 +000023#include "llvm/MC/MCSymbol.h"
Chris Lattner0b822ab2010-03-12 21:19:23 +000024#include "llvm/Target/Mangler.h"
Daniel Dunbare8338102009-07-15 20:24:03 +000025#include "llvm/Target/TargetRegistry.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000026#include "llvm/ADT/StringExtras.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000027#include "llvm/Support/FormattedStream.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000028using namespace llvm;
29
Chris Lattner1ef9cd42006-12-19 22:59:26 +000030namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000031 class SparcAsmPrinter : public AsmPrinter {
Bill Wendlingc5437ea2009-02-24 08:30:20 +000032 public:
David Greenea31f96c2009-07-14 20:18:05 +000033 explicit SparcAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattnere468f882010-03-13 20:55:24 +000034 MCStreamer &Streamer)
35 : AsmPrinter(O, TM, Streamer) {}
Chris Lattner158e1f52006-02-05 05:50:24 +000036
37 virtual const char *getPassName() const {
38 return "Sparc Assembly Printer";
39 }
40
Chris Lattner76c564b2010-04-04 04:47:45 +000041 void printOperand(const MachineInstr *MI, int opNum, raw_ostream &OS);
42 void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +000043 const char *Modifier = 0);
Chris Lattner76c564b2010-04-04 04:47:45 +000044 void printCCOperand(const MachineInstr *MI, int opNum, raw_ostream &OS);
Chris Lattner158e1f52006-02-05 05:50:24 +000045
Chris Lattnerfd97a332010-01-28 01:48:52 +000046 virtual void EmitInstruction(const MachineInstr *MI) {
Chris Lattner76c564b2010-04-04 04:47:45 +000047 printInstruction(MI, O);
Chris Lattnerff68a422010-02-10 00:36:00 +000048 OutStreamer.AddBlankLine();
Chris Lattnerfd97a332010-01-28 01:48:52 +000049 }
Chris Lattner76c564b2010-04-04 04:47:45 +000050 void printInstruction(const MachineInstr *MI, raw_ostream &OS);// autogen'd.
Chris Lattnerad10b3b2009-09-13 20:19:22 +000051 static const char *getRegisterName(unsigned RegNo);
Chris Lattner06c5eed2009-09-13 20:08:00 +000052
Anton Korobeynikov3db21732008-10-10 10:15:03 +000053 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner3bb09762010-04-04 05:29:35 +000054 unsigned AsmVariant, const char *ExtraCode,
55 raw_ostream &O);
Anton Korobeynikov3db21732008-10-10 10:15:03 +000056 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner3bb09762010-04-04 05:29:35 +000057 unsigned AsmVariant, const char *ExtraCode,
58 raw_ostream &O);
Chris Lattner840c7002009-09-15 17:46:24 +000059
Chris Lattner76c564b2010-04-04 04:47:45 +000060 bool printGetPCX(const MachineInstr *MI, unsigned OpNo, raw_ostream &OS);
Chris Lattner1fa9c2cc2010-02-17 18:52:56 +000061
62 virtual bool isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB)
63 const;
Chris Lattner158e1f52006-02-05 05:50:24 +000064 };
65} // end of anonymous namespace
66
67#include "SparcGenAsmWriter.inc"
68
Chris Lattner76c564b2010-04-04 04:47:45 +000069void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
70 raw_ostream &O) {
Chris Lattner158e1f52006-02-05 05:50:24 +000071 const MachineOperand &MO = MI->getOperand (opNum);
Chris Lattner158e1f52006-02-05 05:50:24 +000072 bool CloseParen = false;
Dan Gohman0d1e9a82008-10-03 15:45:36 +000073 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
Chris Lattner158e1f52006-02-05 05:50:24 +000074 O << "%hi(";
75 CloseParen = true;
Dan Gohman0d1e9a82008-10-03 15:45:36 +000076 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
77 !MO.isReg() && !MO.isImm()) {
Chris Lattner158e1f52006-02-05 05:50:24 +000078 O << "%lo(";
79 CloseParen = true;
80 }
81 switch (MO.getType()) {
Chris Lattner10b71c02006-05-04 18:05:43 +000082 case MachineOperand::MO_Register:
Chris Lattnereb2cc682009-09-13 20:31:40 +000083 O << "%" << LowercaseString(getRegisterName(MO.getReg()));
Chris Lattner158e1f52006-02-05 05:50:24 +000084 break;
85
Chris Lattnerfef7a2d2006-05-04 17:21:20 +000086 case MachineOperand::MO_Immediate:
Chris Lattner5c463782007-12-30 20:49:49 +000087 O << (int)MO.getImm();
Chris Lattner158e1f52006-02-05 05:50:24 +000088 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +000089 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner29bdac42010-03-13 21:04:28 +000090 O << *MO.getMBB()->getSymbol();
Chris Lattner158e1f52006-02-05 05:50:24 +000091 return;
Chris Lattner158e1f52006-02-05 05:50:24 +000092 case MachineOperand::MO_GlobalAddress:
Chris Lattner0b822ab2010-03-12 21:19:23 +000093 O << *Mang->getSymbol(MO.getGlobal());
Chris Lattner158e1f52006-02-05 05:50:24 +000094 break;
95 case MachineOperand::MO_ExternalSymbol:
96 O << MO.getSymbolName();
97 break;
98 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnere9a75a62009-08-22 21:43:10 +000099 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattnera5bb3702007-12-30 23:10:15 +0000100 << MO.getIndex();
Chris Lattner158e1f52006-02-05 05:50:24 +0000101 break;
102 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000103 llvm_unreachable("<unknown operand type>");
Chris Lattner158e1f52006-02-05 05:50:24 +0000104 }
105 if (CloseParen) O << ")";
106}
107
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000108void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
Chris Lattner76c564b2010-04-04 04:47:45 +0000109 raw_ostream &O, const char *Modifier) {
110 printOperand(MI, opNum, O);
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000111
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000112 // If this is an ADD operand, emit it like normal operands.
113 if (Modifier && !strcmp(Modifier, "arith")) {
114 O << ", ";
Chris Lattner76c564b2010-04-04 04:47:45 +0000115 printOperand(MI, opNum+1, O);
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000116 return;
117 }
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000118
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000119 if (MI->getOperand(opNum+1).isReg() &&
Chris Lattner158e1f52006-02-05 05:50:24 +0000120 MI->getOperand(opNum+1).getReg() == SP::G0)
121 return; // don't print "+%g0"
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000122 if (MI->getOperand(opNum+1).isImm() &&
Chris Lattner5c463782007-12-30 20:49:49 +0000123 MI->getOperand(opNum+1).getImm() == 0)
Chris Lattner158e1f52006-02-05 05:50:24 +0000124 return; // don't print "+0"
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000125
Chris Lattner158e1f52006-02-05 05:50:24 +0000126 O << "+";
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000127 if (MI->getOperand(opNum+1).isGlobal() ||
128 MI->getOperand(opNum+1).isCPI()) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000129 O << "%lo(";
Chris Lattner76c564b2010-04-04 04:47:45 +0000130 printOperand(MI, opNum+1, O);
Chris Lattner158e1f52006-02-05 05:50:24 +0000131 O << ")";
132 } else {
Chris Lattner76c564b2010-04-04 04:47:45 +0000133 printOperand(MI, opNum+1, O);
Chris Lattner158e1f52006-02-05 05:50:24 +0000134 }
135}
136
Chris Lattner76c564b2010-04-04 04:47:45 +0000137bool SparcAsmPrinter::printGetPCX(const MachineInstr *MI, unsigned opNum,
138 raw_ostream &O) {
Chris Lattner840c7002009-09-15 17:46:24 +0000139 std::string operand = "";
140 const MachineOperand &MO = MI->getOperand(opNum);
141 switch (MO.getType()) {
142 default: assert(0 && "Operand is not a register ");
143 case MachineOperand::MO_Register:
144 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
145 "Operand is not a physical register ");
146 operand = "%" + LowercaseString(getRegisterName(MO.getReg()));
147 break;
148 }
149
Chris Lattnerbdceca62010-02-17 18:57:19 +0000150 unsigned mfNum = MI->getParent()->getParent()->getFunctionNumber();
Chris Lattner2ab11002010-01-27 00:20:02 +0000151 unsigned bbNum = MI->getParent()->getNumber();
Chris Lattner840c7002009-09-15 17:46:24 +0000152
Chris Lattnerbdceca62010-02-17 18:57:19 +0000153 O << '\n' << ".LLGETPCH" << mfNum << '_' << bbNum << ":\n";
154 O << "\tcall\t.LLGETPC" << mfNum << '_' << bbNum << '\n' ;
Chris Lattner840c7002009-09-15 17:46:24 +0000155
156 O << "\t sethi\t"
Chris Lattnerbdceca62010-02-17 18:57:19 +0000157 << "%hi(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << mfNum << '_' << bbNum << ")), "
Chris Lattner840c7002009-09-15 17:46:24 +0000158 << operand << '\n' ;
159
Chris Lattnerbdceca62010-02-17 18:57:19 +0000160 O << ".LLGETPC" << mfNum << '_' << bbNum << ":\n" ;
Chris Lattner840c7002009-09-15 17:46:24 +0000161 O << "\tor\t" << operand
Chris Lattner76c564b2010-04-04 04:47:45 +0000162 << ", %lo(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << mfNum << '_' << bbNum
163 << ")), " << operand << '\n';
Chris Lattner840c7002009-09-15 17:46:24 +0000164 O << "\tadd\t" << operand << ", %o7, " << operand << '\n';
165
166 return true;
167}
168
Chris Lattner76c564b2010-04-04 04:47:45 +0000169void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum,
170 raw_ostream &O) {
Chris Lattner5c463782007-12-30 20:49:49 +0000171 int CC = (int)MI->getOperand(opNum).getImm();
Chris Lattner158e1f52006-02-05 05:50:24 +0000172 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
173}
174
Anton Korobeynikov3db21732008-10-10 10:15:03 +0000175/// PrintAsmOperand - Print out an operand for an inline asm expression.
176///
177bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
178 unsigned AsmVariant,
Chris Lattner3bb09762010-04-04 05:29:35 +0000179 const char *ExtraCode,
180 raw_ostream &O) {
Anton Korobeynikovb80b4852008-10-10 20:29:50 +0000181 if (ExtraCode && ExtraCode[0]) {
182 if (ExtraCode[1] != 0) return true; // Unknown modifier.
183
184 switch (ExtraCode[0]) {
185 default: return true; // Unknown modifier.
186 case 'r':
187 break;
188 }
189 }
Anton Korobeynikov3db21732008-10-10 10:15:03 +0000190
Chris Lattner76c564b2010-04-04 04:47:45 +0000191 printOperand(MI, OpNo, O);
Anton Korobeynikov3db21732008-10-10 10:15:03 +0000192
193 return false;
194}
195
196bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Chris Lattner3bb09762010-04-04 05:29:35 +0000197 unsigned OpNo, unsigned AsmVariant,
198 const char *ExtraCode,
199 raw_ostream &O) {
Anton Korobeynikov3db21732008-10-10 10:15:03 +0000200 if (ExtraCode && ExtraCode[0])
201 return true; // Unknown modifier
202
203 O << '[';
Chris Lattner76c564b2010-04-04 04:47:45 +0000204 printMemOperand(MI, OpNo, O);
Anton Korobeynikov3db21732008-10-10 10:15:03 +0000205 O << ']';
206
207 return false;
208}
Douglas Gregor1b731d52009-06-16 20:12:29 +0000209
Chris Lattner1fa9c2cc2010-02-17 18:52:56 +0000210/// isBlockOnlyReachableByFallthough - Return true if the basic block has
211/// exactly one predecessor and the control transfer mechanism between
212/// the predecessor and this block is a fall-through.
Chris Lattner29146d42010-03-06 07:02:28 +0000213///
214/// This overrides AsmPrinter's implementation to handle delay slots.
215bool SparcAsmPrinter::
216isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
Chris Lattner1fa9c2cc2010-02-17 18:52:56 +0000217 // If this is a landing pad, it isn't a fall through. If it has no preds,
218 // then nothing falls through to it.
219 if (MBB->isLandingPad() || MBB->pred_empty())
220 return false;
221
222 // If there isn't exactly one predecessor, it can't be a fall through.
223 MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI;
224 ++PI2;
225 if (PI2 != MBB->pred_end())
226 return false;
227
228 // The predecessor has to be immediately before this block.
229 const MachineBasicBlock *Pred = *PI;
230
231 if (!Pred->isLayoutSuccessor(MBB))
232 return false;
233
Chris Lattner29146d42010-03-06 07:02:28 +0000234 // Check if the last terminator is an unconditional branch.
Chris Lattner1fa9c2cc2010-02-17 18:52:56 +0000235 MachineBasicBlock::const_iterator I = Pred->end();
Chris Lattner29146d42010-03-06 07:02:28 +0000236 while (I != Pred->begin() && !(--I)->getDesc().isTerminator())
237 ; // Noop
Chris Lattner1fa9c2cc2010-02-17 18:52:56 +0000238 return I == Pred->end() || !I->getDesc().isBarrier();
239}
240
241
242
Bob Wilson5a495fe2009-06-23 23:59:40 +0000243// Force static initialization.
Daniel Dunbare8338102009-07-15 20:24:03 +0000244extern "C" void LLVMInitializeSparcAsmPrinter() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +0000245 RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);
Chris Lattner8228b112010-02-04 06:34:01 +0000246 RegisterAsmPrinter<SparcAsmPrinter> Y(TheSparcV9Target);
Daniel Dunbare8338102009-07-15 20:24:03 +0000247}