blob: 25548625e76096573ac6adc6f10bd2d4146434de [file] [log] [blame]
Chris Lattner7c90f732006-02-05 05:50:24 +00001//===-- SparcAsmPrinter.cpp - Sparc LLVM assembly writer ------------------===//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002//
Brian Gaeke4acfd032004-03-04 06:00:41 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00007//
Brian Gaeke4acfd032004-03-04 06:00:41 +00008//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
Chris Lattner7c90f732006-02-05 05:50:24 +000011// of machine-dependent LLVM code to GAS-format SPARC assembly language.
Brian Gaeke4acfd032004-03-04 06:00:41 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattner95b2c7d2006-12-19 22:59:26 +000015#define DEBUG_TYPE "asm-printer"
Chris Lattner7c90f732006-02-05 05:50:24 +000016#include "Sparc.h"
17#include "SparcInstrInfo.h"
Chris Lattner225503a2009-06-19 15:48:10 +000018#include "SparcTargetMachine.h"
Chris Lattner1dbed162005-12-17 07:04:29 +000019#include "llvm/CodeGen/AsmPrinter.h"
Brian Gaeke4acfd032004-03-04 06:00:41 +000020#include "llvm/CodeGen/MachineInstr.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000021#include "llvm/MC/MCAsmInfo.h"
Chris Lattner8e089a92010-02-10 00:36:00 +000022#include "llvm/MC/MCStreamer.h"
Chris Lattner325d3dc2009-09-13 17:14:04 +000023#include "llvm/MC/MCSymbol.h"
Chris Lattnerd62f1b42010-03-12 21:19:23 +000024#include "llvm/Target/Mangler.h"
Chris Lattner7ad07c42010-04-04 06:12:20 +000025#include "llvm/ADT/SmallString.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000026#include "llvm/Support/TargetRegistry.h"
Chris Lattnerb23569a2010-04-04 08:18:47 +000027#include "llvm/Support/raw_ostream.h"
Brian Gaeke4acfd032004-03-04 06:00:41 +000028using namespace llvm;
29
Chris Lattner95b2c7d2006-12-19 22:59:26 +000030namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000031 class SparcAsmPrinter : public AsmPrinter {
Bill Wendling57f0db82009-02-24 08:30:20 +000032 public:
Chris Lattnerb23569a2010-04-04 08:18:47 +000033 explicit SparcAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
34 : AsmPrinter(TM, Streamer) {}
Brian Gaeke4acfd032004-03-04 06:00:41 +000035
Brian Gaeke4acfd032004-03-04 06:00:41 +000036 virtual const char *getPassName() const {
Chris Lattner7c90f732006-02-05 05:50:24 +000037 return "Sparc Assembly Printer";
Brian Gaeke4acfd032004-03-04 06:00:41 +000038 }
39
Chris Lattner35c33bd2010-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 Lattnerad7a3e62006-02-10 07:35:42 +000042 const char *Modifier = 0);
Chris Lattner35c33bd2010-04-04 04:47:45 +000043 void printCCOperand(const MachineInstr *MI, int opNum, raw_ostream &OS);
Chris Lattner6788faa2006-01-31 06:49:09 +000044
Chris Lattner745ec062010-01-28 01:48:52 +000045 virtual void EmitInstruction(const MachineInstr *MI) {
Chris Lattner7ad07c42010-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 Lattner745ec062010-01-28 01:48:52 +000050 }
Chris Lattner35c33bd2010-04-04 04:47:45 +000051 void printInstruction(const MachineInstr *MI, raw_ostream &OS);// autogen'd.
Chris Lattnerd95148f2009-09-13 20:19:22 +000052 static const char *getRegisterName(unsigned RegNo);
Chris Lattner05af2612009-09-13 20:08:00 +000053
Anton Korobeynikovf3693302008-10-10 10:15:03 +000054 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnerc75c0282010-04-04 05:29:35 +000055 unsigned AsmVariant, const char *ExtraCode,
56 raw_ostream &O);
Anton Korobeynikovf3693302008-10-10 10:15:03 +000057 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnerc75c0282010-04-04 05:29:35 +000058 unsigned AsmVariant, const char *ExtraCode,
59 raw_ostream &O);
Chris Lattnerdb486a62009-09-15 17:46:24 +000060
Chris Lattner35c33bd2010-04-04 04:47:45 +000061 bool printGetPCX(const MachineInstr *MI, unsigned OpNo, raw_ostream &OS);
Chris Lattner0a3f3992010-02-17 18:52:56 +000062
63 virtual bool isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB)
64 const;
Venkatraman Govindaraju55caf9c2011-12-25 18:50:24 +000065
66 virtual MachineLocation getDebugValueLocation(const MachineInstr *MI) const;
Brian Gaeke4acfd032004-03-04 06:00:41 +000067 };
68} // end of anonymous namespace
69
Chris Lattner7c90f732006-02-05 05:50:24 +000070#include "SparcGenAsmWriter.inc"
Chris Lattner994b7352005-12-16 06:34:17 +000071
Chris Lattner35c33bd2010-04-04 04:47:45 +000072void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
73 raw_ostream &O) {
Brian Gaeke446ae112004-06-15 19:52:59 +000074 const MachineOperand &MO = MI->getOperand (opNum);
Brian Gaeke446ae112004-06-15 19:52:59 +000075 bool CloseParen = false;
Dan Gohmand735b802008-10-03 15:45:36 +000076 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
Brian Gaeke446ae112004-06-15 19:52:59 +000077 O << "%hi(";
78 CloseParen = true;
Dan Gohmand735b802008-10-03 15:45:36 +000079 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
80 !MO.isReg() && !MO.isImm()) {
Brian Gaeke446ae112004-06-15 19:52:59 +000081 O << "%lo(";
82 CloseParen = true;
83 }
Brian Gaeke62aa28a2004-03-05 08:39:09 +000084 switch (MO.getType()) {
Chris Lattner2d90ac72006-05-04 18:05:43 +000085 case MachineOperand::MO_Register:
Benjamin Kramer59085362011-11-06 20:37:06 +000086 O << "%" << StringRef(getRegisterName(MO.getReg())).lower();
Brian Gaeke446ae112004-06-15 19:52:59 +000087 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +000088
Chris Lattner63b3d712006-05-04 17:21:20 +000089 case MachineOperand::MO_Immediate:
Chris Lattner9a1ceae2007-12-30 20:49:49 +000090 O << (int)MO.getImm();
Brian Gaeke446ae112004-06-15 19:52:59 +000091 break;
Nate Begeman37efe672006-04-22 18:53:45 +000092 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner1b2eb0e2010-03-13 21:04:28 +000093 O << *MO.getMBB()->getSymbol();
Brian Gaeke09c13092004-06-17 19:39:23 +000094 return;
Brian Gaeke62aa28a2004-03-05 08:39:09 +000095 case MachineOperand::MO_GlobalAddress:
Chris Lattnerd62f1b42010-03-12 21:19:23 +000096 O << *Mang->getSymbol(MO.getGlobal());
Brian Gaeke446ae112004-06-15 19:52:59 +000097 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +000098 case MachineOperand::MO_ExternalSymbol:
99 O << MO.getSymbolName();
Brian Gaeke446ae112004-06-15 19:52:59 +0000100 break;
Brian Gaeke8a0ae9e2004-06-27 22:50:44 +0000101 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000102 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000103 << MO.getIndex();
Brian Gaeke8a0ae9e2004-06-27 22:50:44 +0000104 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000105 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000106 llvm_unreachable("<unknown operand type>");
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000107 }
Brian Gaeke446ae112004-06-15 19:52:59 +0000108 if (CloseParen) O << ")";
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000109}
110
Chris Lattnerad7a3e62006-02-10 07:35:42 +0000111void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
Chris Lattner35c33bd2010-04-04 04:47:45 +0000112 raw_ostream &O, const char *Modifier) {
113 printOperand(MI, opNum, O);
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000114
Chris Lattnerad7a3e62006-02-10 07:35:42 +0000115 // If this is an ADD operand, emit it like normal operands.
116 if (Modifier && !strcmp(Modifier, "arith")) {
117 O << ", ";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000118 printOperand(MI, opNum+1, O);
Chris Lattnerad7a3e62006-02-10 07:35:42 +0000119 return;
120 }
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000121
Dan Gohmand735b802008-10-03 15:45:36 +0000122 if (MI->getOperand(opNum+1).isReg() &&
Chris Lattner7c90f732006-02-05 05:50:24 +0000123 MI->getOperand(opNum+1).getReg() == SP::G0)
Chris Lattner76acc872005-12-18 02:37:35 +0000124 return; // don't print "+%g0"
Dan Gohmand735b802008-10-03 15:45:36 +0000125 if (MI->getOperand(opNum+1).isImm() &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000126 MI->getOperand(opNum+1).getImm() == 0)
Chris Lattner76acc872005-12-18 02:37:35 +0000127 return; // don't print "+0"
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000128
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000129 O << "+";
Dan Gohmand735b802008-10-03 15:45:36 +0000130 if (MI->getOperand(opNum+1).isGlobal() ||
131 MI->getOperand(opNum+1).isCPI()) {
Chris Lattnere1389ad2005-12-18 02:27:00 +0000132 O << "%lo(";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000133 printOperand(MI, opNum+1, O);
Chris Lattnere1389ad2005-12-18 02:27:00 +0000134 O << ")";
135 } else {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000136 printOperand(MI, opNum+1, O);
Chris Lattnere1389ad2005-12-18 02:27:00 +0000137 }
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000138}
139
Chris Lattner35c33bd2010-04-04 04:47:45 +0000140bool SparcAsmPrinter::printGetPCX(const MachineInstr *MI, unsigned opNum,
141 raw_ostream &O) {
Chris Lattnerdb486a62009-09-15 17:46:24 +0000142 std::string operand = "";
143 const MachineOperand &MO = MI->getOperand(opNum);
144 switch (MO.getType()) {
Craig Topperbc219812012-02-07 02:50:20 +0000145 default: llvm_unreachable("Operand is not a register");
Chris Lattnerdb486a62009-09-15 17:46:24 +0000146 case MachineOperand::MO_Register:
147 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
148 "Operand is not a physical register ");
Venkatraman Govindarajuc1783082011-01-12 03:52:59 +0000149 assert(MO.getReg() != SP::O7 &&
150 "%o7 is assigned as destination for getpcx!");
Benjamin Kramer59085362011-11-06 20:37:06 +0000151 operand = "%" + StringRef(getRegisterName(MO.getReg())).lower();
Chris Lattnerdb486a62009-09-15 17:46:24 +0000152 break;
153 }
154
Chris Lattnerf3047cd2010-02-17 18:57:19 +0000155 unsigned mfNum = MI->getParent()->getParent()->getFunctionNumber();
Chris Lattnerd3b31a72010-01-27 00:20:02 +0000156 unsigned bbNum = MI->getParent()->getNumber();
Chris Lattnerdb486a62009-09-15 17:46:24 +0000157
Chris Lattnerf3047cd2010-02-17 18:57:19 +0000158 O << '\n' << ".LLGETPCH" << mfNum << '_' << bbNum << ":\n";
159 O << "\tcall\t.LLGETPC" << mfNum << '_' << bbNum << '\n' ;
Chris Lattnerdb486a62009-09-15 17:46:24 +0000160
161 O << "\t sethi\t"
Venkatraman Govindarajuc1783082011-01-12 03:52:59 +0000162 << "%hi(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << mfNum << '_' << bbNum
163 << ")), " << operand << '\n' ;
Chris Lattnerdb486a62009-09-15 17:46:24 +0000164
Chris Lattnerf3047cd2010-02-17 18:57:19 +0000165 O << ".LLGETPC" << mfNum << '_' << bbNum << ":\n" ;
Chris Lattnerdb486a62009-09-15 17:46:24 +0000166 O << "\tor\t" << operand
Chris Lattner35c33bd2010-04-04 04:47:45 +0000167 << ", %lo(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << mfNum << '_' << bbNum
168 << ")), " << operand << '\n';
Chris Lattnerdb486a62009-09-15 17:46:24 +0000169 O << "\tadd\t" << operand << ", %o7, " << operand << '\n';
170
171 return true;
172}
173
Chris Lattner35c33bd2010-04-04 04:47:45 +0000174void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum,
175 raw_ostream &O) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000176 int CC = (int)MI->getOperand(opNum).getImm();
Chris Lattner7c90f732006-02-05 05:50:24 +0000177 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
Chris Lattner6788faa2006-01-31 06:49:09 +0000178}
179
Anton Korobeynikovf3693302008-10-10 10:15:03 +0000180/// PrintAsmOperand - Print out an operand for an inline asm expression.
181///
182bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
183 unsigned AsmVariant,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000184 const char *ExtraCode,
185 raw_ostream &O) {
Anton Korobeynikov4cf5e2e2008-10-10 20:29:50 +0000186 if (ExtraCode && ExtraCode[0]) {
187 if (ExtraCode[1] != 0) return true; // Unknown modifier.
188
189 switch (ExtraCode[0]) {
Jack Carter0518fca2012-06-26 13:49:27 +0000190 default:
191 // See if this is a generic print operand
192 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
Anton Korobeynikov4cf5e2e2008-10-10 20:29:50 +0000193 case 'r':
194 break;
195 }
196 }
Anton Korobeynikovf3693302008-10-10 10:15:03 +0000197
Chris Lattner35c33bd2010-04-04 04:47:45 +0000198 printOperand(MI, OpNo, O);
Anton Korobeynikovf3693302008-10-10 10:15:03 +0000199
200 return false;
201}
202
203bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000204 unsigned OpNo, unsigned AsmVariant,
205 const char *ExtraCode,
206 raw_ostream &O) {
Anton Korobeynikovf3693302008-10-10 10:15:03 +0000207 if (ExtraCode && ExtraCode[0])
208 return true; // Unknown modifier
209
210 O << '[';
Chris Lattner35c33bd2010-04-04 04:47:45 +0000211 printMemOperand(MI, OpNo, O);
Anton Korobeynikovf3693302008-10-10 10:15:03 +0000212 O << ']';
213
214 return false;
215}
Douglas Gregor1555a232009-06-16 20:12:29 +0000216
Chris Lattner0a3f3992010-02-17 18:52:56 +0000217/// isBlockOnlyReachableByFallthough - Return true if the basic block has
218/// exactly one predecessor and the control transfer mechanism between
219/// the predecessor and this block is a fall-through.
Chris Lattnereea3f7e2010-03-06 07:02:28 +0000220///
221/// This overrides AsmPrinter's implementation to handle delay slots.
222bool SparcAsmPrinter::
223isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
Chris Lattner0a3f3992010-02-17 18:52:56 +0000224 // If this is a landing pad, it isn't a fall through. If it has no preds,
225 // then nothing falls through to it.
226 if (MBB->isLandingPad() || MBB->pred_empty())
227 return false;
228
229 // If there isn't exactly one predecessor, it can't be a fall through.
230 MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI;
231 ++PI2;
232 if (PI2 != MBB->pred_end())
233 return false;
234
235 // The predecessor has to be immediately before this block.
236 const MachineBasicBlock *Pred = *PI;
237
238 if (!Pred->isLayoutSuccessor(MBB))
239 return false;
240
Chris Lattnereea3f7e2010-03-06 07:02:28 +0000241 // Check if the last terminator is an unconditional branch.
Chris Lattner0a3f3992010-02-17 18:52:56 +0000242 MachineBasicBlock::const_iterator I = Pred->end();
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000243 while (I != Pred->begin() && !(--I)->isTerminator())
Chris Lattnereea3f7e2010-03-06 07:02:28 +0000244 ; // Noop
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000245 return I == Pred->end() || !I->isBarrier();
Chris Lattner0a3f3992010-02-17 18:52:56 +0000246}
247
Venkatraman Govindaraju55caf9c2011-12-25 18:50:24 +0000248MachineLocation SparcAsmPrinter::
249getDebugValueLocation(const MachineInstr *MI) const {
250 assert(MI->getNumOperands() == 4 && "Invalid number of operands!");
251 assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm() &&
252 "Unexpected MachineOperand types");
253 return MachineLocation(MI->getOperand(0).getReg(),
254 MI->getOperand(1).getImm());
255}
Chris Lattner0a3f3992010-02-17 18:52:56 +0000256
Bob Wilsona96751f2009-06-23 23:59:40 +0000257// Force static initialization.
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000258extern "C" void LLVMInitializeSparcAsmPrinter() {
Daniel Dunbar0c795d62009-07-25 06:49:55 +0000259 RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);
Chris Lattner87c06d62010-02-04 06:34:01 +0000260 RegisterAsmPrinter<SparcAsmPrinter> Y(TheSparcV9Target);
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000261}