blob: 345e1bca54c647507d54e299935a4a20f235a9d5 [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"
Chris Lattner3d86cd62010-04-04 06:12:20 +000025#include "llvm/ADT/SmallString.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000026#include "llvm/ADT/StringExtras.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000027#include "llvm/Support/TargetRegistry.h"
Chris Lattnerd20699b2010-04-04 08:18:47 +000028#include "llvm/Support/raw_ostream.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000029using namespace llvm;
30
Chris Lattner1ef9cd42006-12-19 22:59:26 +000031namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000032 class SparcAsmPrinter : public AsmPrinter {
Bill Wendlingc5437ea2009-02-24 08:30:20 +000033 public:
Chris Lattnerd20699b2010-04-04 08:18:47 +000034 explicit SparcAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
35 : AsmPrinter(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 Lattner3d86cd62010-04-04 06:12:20 +000047 SmallString<128> Str;
48 raw_svector_ostream OS(Str);
49 printInstruction(MI, OS);
50 OutStreamer.EmitRawText(OS.str());
Chris Lattnerfd97a332010-01-28 01:48:52 +000051 }
Chris Lattner76c564b2010-04-04 04:47:45 +000052 void printInstruction(const MachineInstr *MI, raw_ostream &OS);// autogen'd.
Chris Lattnerad10b3b2009-09-13 20:19:22 +000053 static const char *getRegisterName(unsigned RegNo);
Chris Lattner06c5eed2009-09-13 20:08:00 +000054
Anton Korobeynikov3db21732008-10-10 10:15:03 +000055 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner3bb09762010-04-04 05:29:35 +000056 unsigned AsmVariant, const char *ExtraCode,
57 raw_ostream &O);
Anton Korobeynikov3db21732008-10-10 10:15:03 +000058 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner3bb09762010-04-04 05:29:35 +000059 unsigned AsmVariant, const char *ExtraCode,
60 raw_ostream &O);
Chris Lattner840c7002009-09-15 17:46:24 +000061
Chris Lattner76c564b2010-04-04 04:47:45 +000062 bool printGetPCX(const MachineInstr *MI, unsigned OpNo, raw_ostream &OS);
Chris Lattner1fa9c2cc2010-02-17 18:52:56 +000063
64 virtual bool isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB)
65 const;
Chris Lattner158e1f52006-02-05 05:50:24 +000066 };
67} // end of anonymous namespace
68
69#include "SparcGenAsmWriter.inc"
70
Chris Lattner76c564b2010-04-04 04:47:45 +000071void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
72 raw_ostream &O) {
Chris Lattner158e1f52006-02-05 05:50:24 +000073 const MachineOperand &MO = MI->getOperand (opNum);
Chris Lattner158e1f52006-02-05 05:50:24 +000074 bool CloseParen = false;
Dan Gohman0d1e9a82008-10-03 15:45:36 +000075 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
Chris Lattner158e1f52006-02-05 05:50:24 +000076 O << "%hi(";
77 CloseParen = true;
Dan Gohman0d1e9a82008-10-03 15:45:36 +000078 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
79 !MO.isReg() && !MO.isImm()) {
Chris Lattner158e1f52006-02-05 05:50:24 +000080 O << "%lo(";
81 CloseParen = true;
82 }
83 switch (MO.getType()) {
Chris Lattner10b71c02006-05-04 18:05:43 +000084 case MachineOperand::MO_Register:
Chris Lattnereb2cc682009-09-13 20:31:40 +000085 O << "%" << LowercaseString(getRegisterName(MO.getReg()));
Chris Lattner158e1f52006-02-05 05:50:24 +000086 break;
87
Chris Lattnerfef7a2d2006-05-04 17:21:20 +000088 case MachineOperand::MO_Immediate:
Chris Lattner5c463782007-12-30 20:49:49 +000089 O << (int)MO.getImm();
Chris Lattner158e1f52006-02-05 05:50:24 +000090 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +000091 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner29bdac42010-03-13 21:04:28 +000092 O << *MO.getMBB()->getSymbol();
Chris Lattner158e1f52006-02-05 05:50:24 +000093 return;
Chris Lattner158e1f52006-02-05 05:50:24 +000094 case MachineOperand::MO_GlobalAddress:
Chris Lattner0b822ab2010-03-12 21:19:23 +000095 O << *Mang->getSymbol(MO.getGlobal());
Chris Lattner158e1f52006-02-05 05:50:24 +000096 break;
97 case MachineOperand::MO_ExternalSymbol:
98 O << MO.getSymbolName();
99 break;
100 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnere9a75a62009-08-22 21:43:10 +0000101 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattnera5bb3702007-12-30 23:10:15 +0000102 << MO.getIndex();
Chris Lattner158e1f52006-02-05 05:50:24 +0000103 break;
104 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000105 llvm_unreachable("<unknown operand type>");
Chris Lattner158e1f52006-02-05 05:50:24 +0000106 }
107 if (CloseParen) O << ")";
108}
109
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000110void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
Chris Lattner76c564b2010-04-04 04:47:45 +0000111 raw_ostream &O, const char *Modifier) {
112 printOperand(MI, opNum, O);
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000113
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000114 // If this is an ADD operand, emit it like normal operands.
115 if (Modifier && !strcmp(Modifier, "arith")) {
116 O << ", ";
Chris Lattner76c564b2010-04-04 04:47:45 +0000117 printOperand(MI, opNum+1, O);
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000118 return;
119 }
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000120
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000121 if (MI->getOperand(opNum+1).isReg() &&
Chris Lattner158e1f52006-02-05 05:50:24 +0000122 MI->getOperand(opNum+1).getReg() == SP::G0)
123 return; // don't print "+%g0"
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000124 if (MI->getOperand(opNum+1).isImm() &&
Chris Lattner5c463782007-12-30 20:49:49 +0000125 MI->getOperand(opNum+1).getImm() == 0)
Chris Lattner158e1f52006-02-05 05:50:24 +0000126 return; // don't print "+0"
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000127
Chris Lattner158e1f52006-02-05 05:50:24 +0000128 O << "+";
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000129 if (MI->getOperand(opNum+1).isGlobal() ||
130 MI->getOperand(opNum+1).isCPI()) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000131 O << "%lo(";
Chris Lattner76c564b2010-04-04 04:47:45 +0000132 printOperand(MI, opNum+1, O);
Chris Lattner158e1f52006-02-05 05:50:24 +0000133 O << ")";
134 } else {
Chris Lattner76c564b2010-04-04 04:47:45 +0000135 printOperand(MI, opNum+1, O);
Chris Lattner158e1f52006-02-05 05:50:24 +0000136 }
137}
138
Chris Lattner76c564b2010-04-04 04:47:45 +0000139bool SparcAsmPrinter::printGetPCX(const MachineInstr *MI, unsigned opNum,
140 raw_ostream &O) {
Chris Lattner840c7002009-09-15 17:46:24 +0000141 std::string operand = "";
142 const MachineOperand &MO = MI->getOperand(opNum);
143 switch (MO.getType()) {
144 default: assert(0 && "Operand is not a register ");
145 case MachineOperand::MO_Register:
146 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
147 "Operand is not a physical register ");
Venkatraman Govindarajuee347f82011-01-12 03:52:59 +0000148 assert(MO.getReg() != SP::O7 &&
149 "%o7 is assigned as destination for getpcx!");
Chris Lattner840c7002009-09-15 17:46:24 +0000150 operand = "%" + LowercaseString(getRegisterName(MO.getReg()));
151 break;
152 }
153
Chris Lattnerbdceca62010-02-17 18:57:19 +0000154 unsigned mfNum = MI->getParent()->getParent()->getFunctionNumber();
Chris Lattner2ab11002010-01-27 00:20:02 +0000155 unsigned bbNum = MI->getParent()->getNumber();
Chris Lattner840c7002009-09-15 17:46:24 +0000156
Chris Lattnerbdceca62010-02-17 18:57:19 +0000157 O << '\n' << ".LLGETPCH" << mfNum << '_' << bbNum << ":\n";
158 O << "\tcall\t.LLGETPC" << mfNum << '_' << bbNum << '\n' ;
Chris Lattner840c7002009-09-15 17:46:24 +0000159
160 O << "\t sethi\t"
Venkatraman Govindarajuee347f82011-01-12 03:52:59 +0000161 << "%hi(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << mfNum << '_' << bbNum
162 << ")), " << operand << '\n' ;
Chris Lattner840c7002009-09-15 17:46:24 +0000163
Chris Lattnerbdceca62010-02-17 18:57:19 +0000164 O << ".LLGETPC" << mfNum << '_' << bbNum << ":\n" ;
Chris Lattner840c7002009-09-15 17:46:24 +0000165 O << "\tor\t" << operand
Chris Lattner76c564b2010-04-04 04:47:45 +0000166 << ", %lo(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << mfNum << '_' << bbNum
167 << ")), " << operand << '\n';
Chris Lattner840c7002009-09-15 17:46:24 +0000168 O << "\tadd\t" << operand << ", %o7, " << operand << '\n';
169
170 return true;
171}
172
Chris Lattner76c564b2010-04-04 04:47:45 +0000173void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum,
174 raw_ostream &O) {
Chris Lattner5c463782007-12-30 20:49:49 +0000175 int CC = (int)MI->getOperand(opNum).getImm();
Chris Lattner158e1f52006-02-05 05:50:24 +0000176 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
177}
178
Anton Korobeynikov3db21732008-10-10 10:15:03 +0000179/// PrintAsmOperand - Print out an operand for an inline asm expression.
180///
181bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
182 unsigned AsmVariant,
Chris Lattner3bb09762010-04-04 05:29:35 +0000183 const char *ExtraCode,
184 raw_ostream &O) {
Anton Korobeynikovb80b4852008-10-10 20:29:50 +0000185 if (ExtraCode && ExtraCode[0]) {
186 if (ExtraCode[1] != 0) return true; // Unknown modifier.
187
188 switch (ExtraCode[0]) {
189 default: return true; // Unknown modifier.
190 case 'r':
191 break;
192 }
193 }
Anton Korobeynikov3db21732008-10-10 10:15:03 +0000194
Chris Lattner76c564b2010-04-04 04:47:45 +0000195 printOperand(MI, OpNo, O);
Anton Korobeynikov3db21732008-10-10 10:15:03 +0000196
197 return false;
198}
199
200bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Chris Lattner3bb09762010-04-04 05:29:35 +0000201 unsigned OpNo, unsigned AsmVariant,
202 const char *ExtraCode,
203 raw_ostream &O) {
Anton Korobeynikov3db21732008-10-10 10:15:03 +0000204 if (ExtraCode && ExtraCode[0])
205 return true; // Unknown modifier
206
207 O << '[';
Chris Lattner76c564b2010-04-04 04:47:45 +0000208 printMemOperand(MI, OpNo, O);
Anton Korobeynikov3db21732008-10-10 10:15:03 +0000209 O << ']';
210
211 return false;
212}
Douglas Gregor1b731d52009-06-16 20:12:29 +0000213
Chris Lattner1fa9c2cc2010-02-17 18:52:56 +0000214/// isBlockOnlyReachableByFallthough - Return true if the basic block has
215/// exactly one predecessor and the control transfer mechanism between
216/// the predecessor and this block is a fall-through.
Chris Lattner29146d42010-03-06 07:02:28 +0000217///
218/// This overrides AsmPrinter's implementation to handle delay slots.
219bool SparcAsmPrinter::
220isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
Chris Lattner1fa9c2cc2010-02-17 18:52:56 +0000221 // If this is a landing pad, it isn't a fall through. If it has no preds,
222 // then nothing falls through to it.
223 if (MBB->isLandingPad() || MBB->pred_empty())
224 return false;
225
226 // If there isn't exactly one predecessor, it can't be a fall through.
227 MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI;
228 ++PI2;
229 if (PI2 != MBB->pred_end())
230 return false;
231
232 // The predecessor has to be immediately before this block.
233 const MachineBasicBlock *Pred = *PI;
234
235 if (!Pred->isLayoutSuccessor(MBB))
236 return false;
237
Chris Lattner29146d42010-03-06 07:02:28 +0000238 // Check if the last terminator is an unconditional branch.
Chris Lattner1fa9c2cc2010-02-17 18:52:56 +0000239 MachineBasicBlock::const_iterator I = Pred->end();
Chris Lattner29146d42010-03-06 07:02:28 +0000240 while (I != Pred->begin() && !(--I)->getDesc().isTerminator())
241 ; // Noop
Chris Lattner1fa9c2cc2010-02-17 18:52:56 +0000242 return I == Pred->end() || !I->getDesc().isBarrier();
243}
244
245
246
Bob Wilson5a495fe2009-06-23 23:59:40 +0000247// Force static initialization.
Daniel Dunbare8338102009-07-15 20:24:03 +0000248extern "C" void LLVMInitializeSparcAsmPrinter() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +0000249 RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);
Chris Lattner8228b112010-02-04 06:34:01 +0000250 RegisterAsmPrinter<SparcAsmPrinter> Y(TheSparcV9Target);
Daniel Dunbare8338102009-07-15 20:24:03 +0000251}