blob: f6753a637cd05b2547938f3c341f2d3ba24402cd [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"
Daniel Dunbar51b198a2009-07-15 20:24:03 +000024#include "llvm/Target/TargetRegistry.h"
Brian Gaeke74dfcf12004-09-02 02:37:43 +000025#include "llvm/ADT/StringExtras.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000026#include "llvm/Support/FormattedStream.h"
Brian Gaeke4acfd032004-03-04 06:00:41 +000027using namespace llvm;
28
Chris Lattner95b2c7d2006-12-19 22:59:26 +000029namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000030 class SparcAsmPrinter : public AsmPrinter {
Bill Wendling57f0db82009-02-24 08:30:20 +000031 public:
David Greene71847812009-07-14 20:18:05 +000032 explicit SparcAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner56591ab2010-02-02 23:37:42 +000033 MCContext &Ctx, MCStreamer &Streamer,
34 const MCAsmInfo *T)
35 : AsmPrinter(O, TM, Ctx, Streamer, T) {}
Brian Gaeke4acfd032004-03-04 06:00:41 +000036
Brian Gaeke4acfd032004-03-04 06:00:41 +000037 virtual const char *getPassName() const {
Chris Lattner7c90f732006-02-05 05:50:24 +000038 return "Sparc Assembly Printer";
Brian Gaeke4acfd032004-03-04 06:00:41 +000039 }
40
Brian Gaeke446ae112004-06-15 19:52:59 +000041 void printOperand(const MachineInstr *MI, int opNum);
Chris Lattnerad7a3e62006-02-10 07:35:42 +000042 void printMemOperand(const MachineInstr *MI, int opNum,
43 const char *Modifier = 0);
Chris Lattner7c90f732006-02-05 05:50:24 +000044 void printCCOperand(const MachineInstr *MI, int opNum);
Chris Lattner6788faa2006-01-31 06:49:09 +000045
Chris Lattner745ec062010-01-28 01:48:52 +000046 virtual void EmitInstruction(const MachineInstr *MI) {
47 printInstruction(MI);
Chris Lattner8e089a92010-02-10 00:36:00 +000048 OutStreamer.AddBlankLine();
Chris Lattner745ec062010-01-28 01:48:52 +000049 }
Chris Lattner41aefdc2009-08-08 01:32:19 +000050 void printInstruction(const MachineInstr *MI); // autogenerated.
Chris Lattnerd95148f2009-09-13 20:19:22 +000051 static const char *getRegisterName(unsigned RegNo);
Chris Lattner05af2612009-09-13 20:08:00 +000052
Anton Korobeynikovf3693302008-10-10 10:15:03 +000053 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
54 unsigned AsmVariant, const char *ExtraCode);
55 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
56 unsigned AsmVariant, const char *ExtraCode);
Chris Lattnerdb486a62009-09-15 17:46:24 +000057
Chris Lattnerdb486a62009-09-15 17:46:24 +000058 bool printGetPCX(const MachineInstr *MI, unsigned OpNo);
Chris Lattner0a3f3992010-02-17 18:52:56 +000059
60 virtual bool isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB)
61 const;
Brian Gaeke4acfd032004-03-04 06:00:41 +000062 };
63} // end of anonymous namespace
64
Chris Lattner7c90f732006-02-05 05:50:24 +000065#include "SparcGenAsmWriter.inc"
Chris Lattner994b7352005-12-16 06:34:17 +000066
Chris Lattner7c90f732006-02-05 05:50:24 +000067void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Brian Gaeke446ae112004-06-15 19:52:59 +000068 const MachineOperand &MO = MI->getOperand (opNum);
Brian Gaeke446ae112004-06-15 19:52:59 +000069 bool CloseParen = false;
Dan Gohmand735b802008-10-03 15:45:36 +000070 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
Brian Gaeke446ae112004-06-15 19:52:59 +000071 O << "%hi(";
72 CloseParen = true;
Dan Gohmand735b802008-10-03 15:45:36 +000073 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
74 !MO.isReg() && !MO.isImm()) {
Brian Gaeke446ae112004-06-15 19:52:59 +000075 O << "%lo(";
76 CloseParen = true;
77 }
Brian Gaeke62aa28a2004-03-05 08:39:09 +000078 switch (MO.getType()) {
Chris Lattner2d90ac72006-05-04 18:05:43 +000079 case MachineOperand::MO_Register:
Chris Lattner762ccea2009-09-13 20:31:40 +000080 O << "%" << LowercaseString(getRegisterName(MO.getReg()));
Brian Gaeke446ae112004-06-15 19:52:59 +000081 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +000082
Chris Lattner63b3d712006-05-04 17:21:20 +000083 case MachineOperand::MO_Immediate:
Chris Lattner9a1ceae2007-12-30 20:49:49 +000084 O << (int)MO.getImm();
Brian Gaeke446ae112004-06-15 19:52:59 +000085 break;
Nate Begeman37efe672006-04-22 18:53:45 +000086 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerf71cb012010-01-26 04:55:51 +000087 O << *MO.getMBB()->getSymbol(OutContext);
Brian Gaeke09c13092004-06-17 19:39:23 +000088 return;
Brian Gaeke62aa28a2004-03-05 08:39:09 +000089 case MachineOperand::MO_GlobalAddress:
Chris Lattner10b318b2010-01-17 21:43:43 +000090 O << *GetGlobalValueSymbol(MO.getGlobal());
Brian Gaeke446ae112004-06-15 19:52:59 +000091 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +000092 case MachineOperand::MO_ExternalSymbol:
93 O << MO.getSymbolName();
Brian Gaeke446ae112004-06-15 19:52:59 +000094 break;
Brian Gaeke8a0ae9e2004-06-27 22:50:44 +000095 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +000096 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner8aa797a2007-12-30 23:10:15 +000097 << MO.getIndex();
Brian Gaeke8a0ae9e2004-06-27 22:50:44 +000098 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +000099 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000100 llvm_unreachable("<unknown operand type>");
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000101 }
Brian Gaeke446ae112004-06-15 19:52:59 +0000102 if (CloseParen) O << ")";
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000103}
104
Chris Lattnerad7a3e62006-02-10 07:35:42 +0000105void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
106 const char *Modifier) {
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000107 printOperand(MI, opNum);
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000108
Chris Lattnerad7a3e62006-02-10 07:35:42 +0000109 // If this is an ADD operand, emit it like normal operands.
110 if (Modifier && !strcmp(Modifier, "arith")) {
111 O << ", ";
112 printOperand(MI, opNum+1);
113 return;
114 }
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000115
Dan Gohmand735b802008-10-03 15:45:36 +0000116 if (MI->getOperand(opNum+1).isReg() &&
Chris Lattner7c90f732006-02-05 05:50:24 +0000117 MI->getOperand(opNum+1).getReg() == SP::G0)
Chris Lattner76acc872005-12-18 02:37:35 +0000118 return; // don't print "+%g0"
Dan Gohmand735b802008-10-03 15:45:36 +0000119 if (MI->getOperand(opNum+1).isImm() &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000120 MI->getOperand(opNum+1).getImm() == 0)
Chris Lattner76acc872005-12-18 02:37:35 +0000121 return; // don't print "+0"
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000122
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000123 O << "+";
Dan Gohmand735b802008-10-03 15:45:36 +0000124 if (MI->getOperand(opNum+1).isGlobal() ||
125 MI->getOperand(opNum+1).isCPI()) {
Chris Lattnere1389ad2005-12-18 02:27:00 +0000126 O << "%lo(";
127 printOperand(MI, opNum+1);
128 O << ")";
129 } else {
130 printOperand(MI, opNum+1);
131 }
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000132}
133
Chris Lattnerdb486a62009-09-15 17:46:24 +0000134bool SparcAsmPrinter::printGetPCX(const MachineInstr *MI, unsigned opNum) {
135 std::string operand = "";
136 const MachineOperand &MO = MI->getOperand(opNum);
137 switch (MO.getType()) {
138 default: assert(0 && "Operand is not a register ");
139 case MachineOperand::MO_Register:
140 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
141 "Operand is not a physical register ");
142 operand = "%" + LowercaseString(getRegisterName(MO.getReg()));
143 break;
144 }
145
Chris Lattnerf3047cd2010-02-17 18:57:19 +0000146 unsigned mfNum = MI->getParent()->getParent()->getFunctionNumber();
Chris Lattnerd3b31a72010-01-27 00:20:02 +0000147 unsigned bbNum = MI->getParent()->getNumber();
Chris Lattnerdb486a62009-09-15 17:46:24 +0000148
Chris Lattnerf3047cd2010-02-17 18:57:19 +0000149 O << '\n' << ".LLGETPCH" << mfNum << '_' << bbNum << ":\n";
150 O << "\tcall\t.LLGETPC" << mfNum << '_' << bbNum << '\n' ;
Chris Lattnerdb486a62009-09-15 17:46:24 +0000151
152 O << "\t sethi\t"
Chris Lattnerf3047cd2010-02-17 18:57:19 +0000153 << "%hi(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << mfNum << '_' << bbNum << ")), "
Chris Lattnerdb486a62009-09-15 17:46:24 +0000154 << operand << '\n' ;
155
Chris Lattnerf3047cd2010-02-17 18:57:19 +0000156 O << ".LLGETPC" << mfNum << '_' << bbNum << ":\n" ;
Chris Lattnerdb486a62009-09-15 17:46:24 +0000157 O << "\tor\t" << operand
Chris Lattnerf3047cd2010-02-17 18:57:19 +0000158 << ", %lo(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << mfNum << '_' << bbNum << ")), "
Chris Lattnerdb486a62009-09-15 17:46:24 +0000159 << operand << '\n';
160 O << "\tadd\t" << operand << ", %o7, " << operand << '\n';
161
162 return true;
163}
164
Chris Lattner7c90f732006-02-05 05:50:24 +0000165void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000166 int CC = (int)MI->getOperand(opNum).getImm();
Chris Lattner7c90f732006-02-05 05:50:24 +0000167 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
Chris Lattner6788faa2006-01-31 06:49:09 +0000168}
169
Anton Korobeynikovf3693302008-10-10 10:15:03 +0000170/// PrintAsmOperand - Print out an operand for an inline asm expression.
171///
172bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
173 unsigned AsmVariant,
174 const char *ExtraCode) {
Anton Korobeynikov4cf5e2e2008-10-10 20:29:50 +0000175 if (ExtraCode && ExtraCode[0]) {
176 if (ExtraCode[1] != 0) return true; // Unknown modifier.
177
178 switch (ExtraCode[0]) {
179 default: return true; // Unknown modifier.
180 case 'r':
181 break;
182 }
183 }
Anton Korobeynikovf3693302008-10-10 10:15:03 +0000184
185 printOperand(MI, OpNo);
186
187 return false;
188}
189
190bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
191 unsigned OpNo,
192 unsigned AsmVariant,
193 const char *ExtraCode) {
194 if (ExtraCode && ExtraCode[0])
195 return true; // Unknown modifier
196
197 O << '[';
198 printMemOperand(MI, OpNo);
199 O << ']';
200
201 return false;
202}
Douglas Gregor1555a232009-06-16 20:12:29 +0000203
Chris Lattner0a3f3992010-02-17 18:52:56 +0000204/// isBlockOnlyReachableByFallthough - Return true if the basic block has
205/// exactly one predecessor and the control transfer mechanism between
206/// the predecessor and this block is a fall-through.
Chris Lattnereea3f7e2010-03-06 07:02:28 +0000207///
208/// This overrides AsmPrinter's implementation to handle delay slots.
209bool SparcAsmPrinter::
210isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
Chris Lattner0a3f3992010-02-17 18:52:56 +0000211 // If this is a landing pad, it isn't a fall through. If it has no preds,
212 // then nothing falls through to it.
213 if (MBB->isLandingPad() || MBB->pred_empty())
214 return false;
215
216 // If there isn't exactly one predecessor, it can't be a fall through.
217 MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI;
218 ++PI2;
219 if (PI2 != MBB->pred_end())
220 return false;
221
222 // The predecessor has to be immediately before this block.
223 const MachineBasicBlock *Pred = *PI;
224
225 if (!Pred->isLayoutSuccessor(MBB))
226 return false;
227
Chris Lattnereea3f7e2010-03-06 07:02:28 +0000228 // Check if the last terminator is an unconditional branch.
Chris Lattner0a3f3992010-02-17 18:52:56 +0000229 MachineBasicBlock::const_iterator I = Pred->end();
Chris Lattnereea3f7e2010-03-06 07:02:28 +0000230 while (I != Pred->begin() && !(--I)->getDesc().isTerminator())
231 ; // Noop
Chris Lattner0a3f3992010-02-17 18:52:56 +0000232 return I == Pred->end() || !I->getDesc().isBarrier();
233}
234
235
236
Bob Wilsona96751f2009-06-23 23:59:40 +0000237// Force static initialization.
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000238extern "C" void LLVMInitializeSparcAsmPrinter() {
Daniel Dunbar0c795d62009-07-25 06:49:55 +0000239 RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);
Chris Lattner87c06d62010-02-04 06:34:01 +0000240 RegisterAsmPrinter<SparcAsmPrinter> Y(TheSparcV9Target);
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000241}