blob: 7548bbf6ebe1d62f6d940dc69750f167f45b3309 [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"
Evan Cheng2bb40352011-08-24 18:08:43 +000026#include "llvm/Support/TargetRegistry.h"
Chris Lattnerd20699b2010-04-04 08:18:47 +000027#include "llvm/Support/raw_ostream.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:
Chris Lattnerd20699b2010-04-04 08:18:47 +000033 explicit SparcAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
34 : AsmPrinter(TM, Streamer) {}
Chris Lattner158e1f52006-02-05 05:50:24 +000035
36 virtual const char *getPassName() const {
37 return "Sparc Assembly Printer";
38 }
39
Chris Lattner76c564b2010-04-04 04:47:45 +000040 void printOperand(const MachineInstr *MI, int opNum, raw_ostream &OS);
41 void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +000042 const char *Modifier = 0);
Chris Lattner76c564b2010-04-04 04:47:45 +000043 void printCCOperand(const MachineInstr *MI, int opNum, raw_ostream &OS);
Chris Lattner158e1f52006-02-05 05:50:24 +000044
Chris Lattnerfd97a332010-01-28 01:48:52 +000045 virtual void EmitInstruction(const MachineInstr *MI) {
Chris Lattner3d86cd62010-04-04 06:12:20 +000046 SmallString<128> Str;
47 raw_svector_ostream OS(Str);
48 printInstruction(MI, OS);
49 OutStreamer.EmitRawText(OS.str());
Chris Lattnerfd97a332010-01-28 01:48:52 +000050 }
Chris Lattner76c564b2010-04-04 04:47:45 +000051 void printInstruction(const MachineInstr *MI, raw_ostream &OS);// autogen'd.
Chris Lattnerad10b3b2009-09-13 20:19:22 +000052 static const char *getRegisterName(unsigned RegNo);
Chris Lattner06c5eed2009-09-13 20:08:00 +000053
Anton Korobeynikov3db21732008-10-10 10:15:03 +000054 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner3bb09762010-04-04 05:29:35 +000055 unsigned AsmVariant, const char *ExtraCode,
56 raw_ostream &O);
Anton Korobeynikov3db21732008-10-10 10:15:03 +000057 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner3bb09762010-04-04 05:29:35 +000058 unsigned AsmVariant, const char *ExtraCode,
59 raw_ostream &O);
Chris Lattner840c7002009-09-15 17:46:24 +000060
Chris Lattner76c564b2010-04-04 04:47:45 +000061 bool printGetPCX(const MachineInstr *MI, unsigned OpNo, raw_ostream &OS);
Chris Lattner1fa9c2cc2010-02-17 18:52:56 +000062
63 virtual bool isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB)
64 const;
Chris Lattner158e1f52006-02-05 05:50:24 +000065 };
66} // end of anonymous namespace
67
68#include "SparcGenAsmWriter.inc"
69
Chris Lattner76c564b2010-04-04 04:47:45 +000070void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
71 raw_ostream &O) {
Chris Lattner158e1f52006-02-05 05:50:24 +000072 const MachineOperand &MO = MI->getOperand (opNum);
Chris Lattner158e1f52006-02-05 05:50:24 +000073 bool CloseParen = false;
Dan Gohman0d1e9a82008-10-03 15:45:36 +000074 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
Chris Lattner158e1f52006-02-05 05:50:24 +000075 O << "%hi(";
76 CloseParen = true;
Dan Gohman0d1e9a82008-10-03 15:45:36 +000077 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
78 !MO.isReg() && !MO.isImm()) {
Chris Lattner158e1f52006-02-05 05:50:24 +000079 O << "%lo(";
80 CloseParen = true;
81 }
82 switch (MO.getType()) {
Chris Lattner10b71c02006-05-04 18:05:43 +000083 case MachineOperand::MO_Register:
Benjamin Kramer20baffb2011-11-06 20:37:06 +000084 O << "%" << StringRef(getRegisterName(MO.getReg())).lower();
Chris Lattner158e1f52006-02-05 05:50:24 +000085 break;
86
Chris Lattnerfef7a2d2006-05-04 17:21:20 +000087 case MachineOperand::MO_Immediate:
Chris Lattner5c463782007-12-30 20:49:49 +000088 O << (int)MO.getImm();
Chris Lattner158e1f52006-02-05 05:50:24 +000089 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +000090 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner29bdac42010-03-13 21:04:28 +000091 O << *MO.getMBB()->getSymbol();
Chris Lattner158e1f52006-02-05 05:50:24 +000092 return;
Chris Lattner158e1f52006-02-05 05:50:24 +000093 case MachineOperand::MO_GlobalAddress:
Chris Lattner0b822ab2010-03-12 21:19:23 +000094 O << *Mang->getSymbol(MO.getGlobal());
Chris Lattner158e1f52006-02-05 05:50:24 +000095 break;
96 case MachineOperand::MO_ExternalSymbol:
97 O << MO.getSymbolName();
98 break;
99 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnere9a75a62009-08-22 21:43:10 +0000100 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattnera5bb3702007-12-30 23:10:15 +0000101 << MO.getIndex();
Chris Lattner158e1f52006-02-05 05:50:24 +0000102 break;
103 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000104 llvm_unreachable("<unknown operand type>");
Chris Lattner158e1f52006-02-05 05:50:24 +0000105 }
106 if (CloseParen) O << ")";
107}
108
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000109void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
Chris Lattner76c564b2010-04-04 04:47:45 +0000110 raw_ostream &O, const char *Modifier) {
111 printOperand(MI, opNum, O);
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000112
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000113 // If this is an ADD operand, emit it like normal operands.
114 if (Modifier && !strcmp(Modifier, "arith")) {
115 O << ", ";
Chris Lattner76c564b2010-04-04 04:47:45 +0000116 printOperand(MI, opNum+1, O);
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000117 return;
118 }
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000119
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000120 if (MI->getOperand(opNum+1).isReg() &&
Chris Lattner158e1f52006-02-05 05:50:24 +0000121 MI->getOperand(opNum+1).getReg() == SP::G0)
122 return; // don't print "+%g0"
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000123 if (MI->getOperand(opNum+1).isImm() &&
Chris Lattner5c463782007-12-30 20:49:49 +0000124 MI->getOperand(opNum+1).getImm() == 0)
Chris Lattner158e1f52006-02-05 05:50:24 +0000125 return; // don't print "+0"
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000126
Chris Lattner158e1f52006-02-05 05:50:24 +0000127 O << "+";
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000128 if (MI->getOperand(opNum+1).isGlobal() ||
129 MI->getOperand(opNum+1).isCPI()) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000130 O << "%lo(";
Chris Lattner76c564b2010-04-04 04:47:45 +0000131 printOperand(MI, opNum+1, O);
Chris Lattner158e1f52006-02-05 05:50:24 +0000132 O << ")";
133 } else {
Chris Lattner76c564b2010-04-04 04:47:45 +0000134 printOperand(MI, opNum+1, O);
Chris Lattner158e1f52006-02-05 05:50:24 +0000135 }
136}
137
Chris Lattner76c564b2010-04-04 04:47:45 +0000138bool SparcAsmPrinter::printGetPCX(const MachineInstr *MI, unsigned opNum,
139 raw_ostream &O) {
Chris Lattner840c7002009-09-15 17:46:24 +0000140 std::string operand = "";
141 const MachineOperand &MO = MI->getOperand(opNum);
142 switch (MO.getType()) {
143 default: assert(0 && "Operand is not a register ");
144 case MachineOperand::MO_Register:
145 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
146 "Operand is not a physical register ");
Venkatraman Govindarajuee347f82011-01-12 03:52:59 +0000147 assert(MO.getReg() != SP::O7 &&
148 "%o7 is assigned as destination for getpcx!");
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000149 operand = "%" + StringRef(getRegisterName(MO.getReg())).lower();
Chris Lattner840c7002009-09-15 17:46:24 +0000150 break;
151 }
152
Chris Lattnerbdceca62010-02-17 18:57:19 +0000153 unsigned mfNum = MI->getParent()->getParent()->getFunctionNumber();
Chris Lattner2ab11002010-01-27 00:20:02 +0000154 unsigned bbNum = MI->getParent()->getNumber();
Chris Lattner840c7002009-09-15 17:46:24 +0000155
Chris Lattnerbdceca62010-02-17 18:57:19 +0000156 O << '\n' << ".LLGETPCH" << mfNum << '_' << bbNum << ":\n";
157 O << "\tcall\t.LLGETPC" << mfNum << '_' << bbNum << '\n' ;
Chris Lattner840c7002009-09-15 17:46:24 +0000158
159 O << "\t sethi\t"
Venkatraman Govindarajuee347f82011-01-12 03:52:59 +0000160 << "%hi(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << mfNum << '_' << bbNum
161 << ")), " << operand << '\n' ;
Chris Lattner840c7002009-09-15 17:46:24 +0000162
Chris Lattnerbdceca62010-02-17 18:57:19 +0000163 O << ".LLGETPC" << mfNum << '_' << bbNum << ":\n" ;
Chris Lattner840c7002009-09-15 17:46:24 +0000164 O << "\tor\t" << operand
Chris Lattner76c564b2010-04-04 04:47:45 +0000165 << ", %lo(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << mfNum << '_' << bbNum
166 << ")), " << operand << '\n';
Chris Lattner840c7002009-09-15 17:46:24 +0000167 O << "\tadd\t" << operand << ", %o7, " << operand << '\n';
168
169 return true;
170}
171
Chris Lattner76c564b2010-04-04 04:47:45 +0000172void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum,
173 raw_ostream &O) {
Chris Lattner5c463782007-12-30 20:49:49 +0000174 int CC = (int)MI->getOperand(opNum).getImm();
Chris Lattner158e1f52006-02-05 05:50:24 +0000175 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
176}
177
Anton Korobeynikov3db21732008-10-10 10:15:03 +0000178/// PrintAsmOperand - Print out an operand for an inline asm expression.
179///
180bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
181 unsigned AsmVariant,
Chris Lattner3bb09762010-04-04 05:29:35 +0000182 const char *ExtraCode,
183 raw_ostream &O) {
Anton Korobeynikovb80b4852008-10-10 20:29:50 +0000184 if (ExtraCode && ExtraCode[0]) {
185 if (ExtraCode[1] != 0) return true; // Unknown modifier.
186
187 switch (ExtraCode[0]) {
188 default: return true; // Unknown modifier.
189 case 'r':
190 break;
191 }
192 }
Anton Korobeynikov3db21732008-10-10 10:15:03 +0000193
Chris Lattner76c564b2010-04-04 04:47:45 +0000194 printOperand(MI, OpNo, O);
Anton Korobeynikov3db21732008-10-10 10:15:03 +0000195
196 return false;
197}
198
199bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Chris Lattner3bb09762010-04-04 05:29:35 +0000200 unsigned OpNo, unsigned AsmVariant,
201 const char *ExtraCode,
202 raw_ostream &O) {
Anton Korobeynikov3db21732008-10-10 10:15:03 +0000203 if (ExtraCode && ExtraCode[0])
204 return true; // Unknown modifier
205
206 O << '[';
Chris Lattner76c564b2010-04-04 04:47:45 +0000207 printMemOperand(MI, OpNo, O);
Anton Korobeynikov3db21732008-10-10 10:15:03 +0000208 O << ']';
209
210 return false;
211}
Douglas Gregor1b731d52009-06-16 20:12:29 +0000212
Chris Lattner1fa9c2cc2010-02-17 18:52:56 +0000213/// isBlockOnlyReachableByFallthough - Return true if the basic block has
214/// exactly one predecessor and the control transfer mechanism between
215/// the predecessor and this block is a fall-through.
Chris Lattner29146d42010-03-06 07:02:28 +0000216///
217/// This overrides AsmPrinter's implementation to handle delay slots.
218bool SparcAsmPrinter::
219isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
Chris Lattner1fa9c2cc2010-02-17 18:52:56 +0000220 // If this is a landing pad, it isn't a fall through. If it has no preds,
221 // then nothing falls through to it.
222 if (MBB->isLandingPad() || MBB->pred_empty())
223 return false;
224
225 // If there isn't exactly one predecessor, it can't be a fall through.
226 MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI;
227 ++PI2;
228 if (PI2 != MBB->pred_end())
229 return false;
230
231 // The predecessor has to be immediately before this block.
232 const MachineBasicBlock *Pred = *PI;
233
234 if (!Pred->isLayoutSuccessor(MBB))
235 return false;
236
Chris Lattner29146d42010-03-06 07:02:28 +0000237 // Check if the last terminator is an unconditional branch.
Chris Lattner1fa9c2cc2010-02-17 18:52:56 +0000238 MachineBasicBlock::const_iterator I = Pred->end();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000239 while (I != Pred->begin() && !(--I)->isTerminator())
Chris Lattner29146d42010-03-06 07:02:28 +0000240 ; // Noop
Evan Cheng7f8e5632011-12-07 07:15:52 +0000241 return I == Pred->end() || !I->isBarrier();
Chris Lattner1fa9c2cc2010-02-17 18:52:56 +0000242}
243
244
245
Bob Wilson5a495fe2009-06-23 23:59:40 +0000246// Force static initialization.
Daniel Dunbare8338102009-07-15 20:24:03 +0000247extern "C" void LLVMInitializeSparcAsmPrinter() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +0000248 RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);
Chris Lattner8228b112010-02-04 06:34:01 +0000249 RegisterAsmPrinter<SparcAsmPrinter> Y(TheSparcV9Target);
Daniel Dunbare8338102009-07-15 20:24:03 +0000250}