blob: 3a4aff1a3e85c4f5bf48a00c373e0955b6f3fcd9 [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"
Brian Gaeke4acfd032004-03-04 06:00:41 +000019#include "llvm/Constants.h"
20#include "llvm/DerivedTypes.h"
21#include "llvm/Module.h"
Chris Lattner1dbed162005-12-17 07:04:29 +000022#include "llvm/CodeGen/AsmPrinter.h"
Bill Wendlingcb819f12009-02-18 23:12:06 +000023#include "llvm/CodeGen/DwarfWriter.h"
Brian Gaeke4acfd032004-03-04 06:00:41 +000024#include "llvm/CodeGen/MachineFunctionPass.h"
25#include "llvm/CodeGen/MachineConstantPool.h"
26#include "llvm/CodeGen/MachineInstr.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000027#include "llvm/MC/MCAsmInfo.h"
Chris Lattner325d3dc2009-09-13 17:14:04 +000028#include "llvm/MC/MCStreamer.h"
29#include "llvm/MC/MCSymbol.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000030#include "llvm/Target/TargetLoweringObjectFile.h"
Daniel Dunbar51b198a2009-07-15 20:24:03 +000031#include "llvm/Target/TargetRegistry.h"
Brian Gaeke74dfcf12004-09-02 02:37:43 +000032#include "llvm/ADT/Statistic.h"
33#include "llvm/ADT/StringExtras.h"
34#include "llvm/Support/CommandLine.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000035#include "llvm/Support/ErrorHandling.h"
36#include "llvm/Support/FormattedStream.h"
Jim Laskeyb8df7c22005-08-17 20:04:34 +000037#include "llvm/Support/MathExtras.h"
Brian Gaekea8b00ca2004-03-06 05:30:21 +000038#include <cctype>
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000039#include <cstring>
Owen Andersonc0078482008-07-10 01:44:27 +000040#include <map>
Brian Gaeke4acfd032004-03-04 06:00:41 +000041using namespace llvm;
42
Chris Lattner95b2c7d2006-12-19 22:59:26 +000043STATISTIC(EmittedInsts, "Number of machine instrs printed");
Brian Gaeke4acfd032004-03-04 06:00:41 +000044
Chris Lattner95b2c7d2006-12-19 22:59:26 +000045namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000046 class SparcAsmPrinter : public AsmPrinter {
Brian Gaeke4acfd032004-03-04 06:00:41 +000047 /// We name each basic block in a Function with a unique number, so
48 /// that we can consistently refer to them later. This is cleared
49 /// at the beginning of each call to runOnMachineFunction().
50 ///
51 typedef std::map<const Value *, unsigned> ValueMapTy;
52 ValueMapTy NumberForBB;
Owen Anderson2af72d42009-06-26 21:45:04 +000053 unsigned BBNumber;
Bill Wendling57f0db82009-02-24 08:30:20 +000054 public:
David Greene71847812009-07-14 20:18:05 +000055 explicit SparcAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattneraf76e592009-08-22 20:48:53 +000056 const MCAsmInfo *T, bool V)
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +000057 : AsmPrinter(O, TM, T, V), BBNumber(0) {}
Brian Gaeke4acfd032004-03-04 06:00:41 +000058
Brian Gaeke4acfd032004-03-04 06:00:41 +000059 virtual const char *getPassName() const {
Chris Lattner7c90f732006-02-05 05:50:24 +000060 return "Sparc Assembly Printer";
Brian Gaeke4acfd032004-03-04 06:00:41 +000061 }
62
Brian Gaeke446ae112004-06-15 19:52:59 +000063 void printOperand(const MachineInstr *MI, int opNum);
Chris Lattnerad7a3e62006-02-10 07:35:42 +000064 void printMemOperand(const MachineInstr *MI, int opNum,
65 const char *Modifier = 0);
Chris Lattner7c90f732006-02-05 05:50:24 +000066 void printCCOperand(const MachineInstr *MI, int opNum);
Chris Lattner6788faa2006-01-31 06:49:09 +000067
Chris Lattner41aefdc2009-08-08 01:32:19 +000068 void printInstruction(const MachineInstr *MI); // autogenerated.
Chris Lattnerd95148f2009-09-13 20:19:22 +000069 static const char *getRegisterName(unsigned RegNo);
Chris Lattner05af2612009-09-13 20:08:00 +000070
Misha Brukmanb5f662f2005-04-21 23:30:14 +000071 bool runOnMachineFunction(MachineFunction &F);
Anton Korobeynikovf3693302008-10-10 10:15:03 +000072 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
73 unsigned AsmVariant, const char *ExtraCode);
74 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
75 unsigned AsmVariant, const char *ExtraCode);
Chris Lattnerdb486a62009-09-15 17:46:24 +000076
Chris Lattnerdb486a62009-09-15 17:46:24 +000077 bool printGetPCX(const MachineInstr *MI, unsigned OpNo);
Brian Gaeke4acfd032004-03-04 06:00:41 +000078 };
79} // end of anonymous namespace
80
Chris Lattner7c90f732006-02-05 05:50:24 +000081#include "SparcGenAsmWriter.inc"
Chris Lattner994b7352005-12-16 06:34:17 +000082
Evan Cheng4eecdeb2008-02-02 08:39:46 +000083/// runOnMachineFunction - This uses the printInstruction()
Brian Gaeke4acfd032004-03-04 06:00:41 +000084/// method to print assembly for each instruction.
85///
Chris Lattner7c90f732006-02-05 05:50:24 +000086bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner1dbed162005-12-17 07:04:29 +000087 SetupMachineFunction(MF);
88
Chris Lattner4129ccd2010-01-27 00:17:20 +000089 EmitFunctionHeader();
Chris Lattnerb5e9eb62005-12-17 07:11:43 +000090
Brian Gaeke4acfd032004-03-04 06:00:41 +000091 // BBNumber is used here so that a given Printer will never give two
92 // BBs the same name. (If you have a better way, please let me know!)
Brian Gaeke4acfd032004-03-04 06:00:41 +000093
Brian Gaeke4acfd032004-03-04 06:00:41 +000094 // Number each basic block so that we can consistently refer to them
95 // in PC-relative references.
Chris Lattner29bd9e12006-10-05 02:48:40 +000096 // FIXME: Why not use the MBB numbers?
Brian Gaeke4acfd032004-03-04 06:00:41 +000097 NumberForBB.clear();
98 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
99 I != E; ++I) {
100 NumberForBB[I->getBasicBlock()] = BBNumber++;
101 }
102
103 // Print out code for the function.
104 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
105 I != E; ++I) {
106 // Print a label for the basic block.
Nate Begemancdf38c42006-05-02 05:37:32 +0000107 if (I != MF.begin()) {
Chris Lattner70a54c02009-09-13 18:25:37 +0000108 EmitBasicBlockStart(I);
Nate Begemancdf38c42006-05-02 05:37:32 +0000109 }
Brian Gaeke4acfd032004-03-04 06:00:41 +0000110 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukman27177f82005-04-22 18:06:01 +0000111 II != E; ++II) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000112 // Print the assembly for the instruction.
Devang Patelaf0e2722009-10-06 02:19:11 +0000113 processDebugLoc(II, true);
Chris Lattner0d8fcd32005-12-17 06:54:41 +0000114 printInstruction(II);
Chris Lattnerc5ea2632009-09-09 23:14:36 +0000115
David Greene1924aab2009-11-13 21:34:57 +0000116 if (VerboseAsm)
Chris Lattnerc5ea2632009-09-09 23:14:36 +0000117 EmitComments(*II);
118 O << '\n';
Devang Patelaf0e2722009-10-06 02:19:11 +0000119 processDebugLoc(II, false);
Chris Lattner1dbed162005-12-17 07:04:29 +0000120 ++EmittedInsts;
Brian Gaeke4acfd032004-03-04 06:00:41 +0000121 }
122 }
123
Richard Pennington930e4d92009-09-08 12:47:30 +0000124 // Emit post-function debug information.
125 DW->EndFunction(&MF);
126
Brian Gaeke4acfd032004-03-04 06:00:41 +0000127 // We didn't modify anything.
Chris Lattner10b318b2010-01-17 21:43:43 +0000128 O << "\t.size\t" << *CurrentFnSym << ", .-" << *CurrentFnSym << '\n';
Brian Gaeke4acfd032004-03-04 06:00:41 +0000129 return false;
130}
131
Chris Lattnerdb486a62009-09-15 17:46:24 +0000132
Chris Lattner7c90f732006-02-05 05:50:24 +0000133void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Brian Gaeke446ae112004-06-15 19:52:59 +0000134 const MachineOperand &MO = MI->getOperand (opNum);
Brian Gaeke446ae112004-06-15 19:52:59 +0000135 bool CloseParen = false;
Dan Gohmand735b802008-10-03 15:45:36 +0000136 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
Brian Gaeke446ae112004-06-15 19:52:59 +0000137 O << "%hi(";
138 CloseParen = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000139 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
140 !MO.isReg() && !MO.isImm()) {
Brian Gaeke446ae112004-06-15 19:52:59 +0000141 O << "%lo(";
142 CloseParen = true;
143 }
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000144 switch (MO.getType()) {
Chris Lattner2d90ac72006-05-04 18:05:43 +0000145 case MachineOperand::MO_Register:
Chris Lattner762ccea2009-09-13 20:31:40 +0000146 O << "%" << LowercaseString(getRegisterName(MO.getReg()));
Brian Gaeke446ae112004-06-15 19:52:59 +0000147 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000148
Chris Lattner63b3d712006-05-04 17:21:20 +0000149 case MachineOperand::MO_Immediate:
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000150 O << (int)MO.getImm();
Brian Gaeke446ae112004-06-15 19:52:59 +0000151 break;
Nate Begeman37efe672006-04-22 18:53:45 +0000152 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerf71cb012010-01-26 04:55:51 +0000153 O << *MO.getMBB()->getSymbol(OutContext);
Brian Gaeke09c13092004-06-17 19:39:23 +0000154 return;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000155 case MachineOperand::MO_GlobalAddress:
Chris Lattner10b318b2010-01-17 21:43:43 +0000156 O << *GetGlobalValueSymbol(MO.getGlobal());
Brian Gaeke446ae112004-06-15 19:52:59 +0000157 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000158 case MachineOperand::MO_ExternalSymbol:
159 O << MO.getSymbolName();
Brian Gaeke446ae112004-06-15 19:52:59 +0000160 break;
Brian Gaeke8a0ae9e2004-06-27 22:50:44 +0000161 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000162 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000163 << MO.getIndex();
Brian Gaeke8a0ae9e2004-06-27 22:50:44 +0000164 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000165 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000166 llvm_unreachable("<unknown operand type>");
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000167 }
Brian Gaeke446ae112004-06-15 19:52:59 +0000168 if (CloseParen) O << ")";
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000169}
170
Chris Lattnerad7a3e62006-02-10 07:35:42 +0000171void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
172 const char *Modifier) {
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000173 printOperand(MI, opNum);
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000174
Chris Lattnerad7a3e62006-02-10 07:35:42 +0000175 // If this is an ADD operand, emit it like normal operands.
176 if (Modifier && !strcmp(Modifier, "arith")) {
177 O << ", ";
178 printOperand(MI, opNum+1);
179 return;
180 }
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000181
Dan Gohmand735b802008-10-03 15:45:36 +0000182 if (MI->getOperand(opNum+1).isReg() &&
Chris Lattner7c90f732006-02-05 05:50:24 +0000183 MI->getOperand(opNum+1).getReg() == SP::G0)
Chris Lattner76acc872005-12-18 02:37:35 +0000184 return; // don't print "+%g0"
Dan Gohmand735b802008-10-03 15:45:36 +0000185 if (MI->getOperand(opNum+1).isImm() &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000186 MI->getOperand(opNum+1).getImm() == 0)
Chris Lattner76acc872005-12-18 02:37:35 +0000187 return; // don't print "+0"
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000188
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000189 O << "+";
Dan Gohmand735b802008-10-03 15:45:36 +0000190 if (MI->getOperand(opNum+1).isGlobal() ||
191 MI->getOperand(opNum+1).isCPI()) {
Chris Lattnere1389ad2005-12-18 02:27:00 +0000192 O << "%lo(";
193 printOperand(MI, opNum+1);
194 O << ")";
195 } else {
196 printOperand(MI, opNum+1);
197 }
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000198}
199
Chris Lattnerdb486a62009-09-15 17:46:24 +0000200bool SparcAsmPrinter::printGetPCX(const MachineInstr *MI, unsigned opNum) {
201 std::string operand = "";
202 const MachineOperand &MO = MI->getOperand(opNum);
203 switch (MO.getType()) {
204 default: assert(0 && "Operand is not a register ");
205 case MachineOperand::MO_Register:
206 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
207 "Operand is not a physical register ");
208 operand = "%" + LowercaseString(getRegisterName(MO.getReg()));
209 break;
210 }
211
212 unsigned bbNum = NumberForBB[MI->getParent()->getBasicBlock()];
213
214 O << '\n' << ".LLGETPCH" << bbNum << ":\n";
215 O << "\tcall\t.LLGETPC" << bbNum << '\n' ;
216
217 O << "\t sethi\t"
218 << "%hi(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << bbNum << ")), "
219 << operand << '\n' ;
220
221 O << ".LLGETPC" << bbNum << ":\n" ;
222 O << "\tor\t" << operand
223 << ", %lo(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << bbNum << ")), "
224 << operand << '\n';
225 O << "\tadd\t" << operand << ", %o7, " << operand << '\n';
226
227 return true;
228}
229
Chris Lattner7c90f732006-02-05 05:50:24 +0000230void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000231 int CC = (int)MI->getOperand(opNum).getImm();
Chris Lattner7c90f732006-02-05 05:50:24 +0000232 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
Chris Lattner6788faa2006-01-31 06:49:09 +0000233}
234
Anton Korobeynikovf3693302008-10-10 10:15:03 +0000235/// PrintAsmOperand - Print out an operand for an inline asm expression.
236///
237bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
238 unsigned AsmVariant,
239 const char *ExtraCode) {
Anton Korobeynikov4cf5e2e2008-10-10 20:29:50 +0000240 if (ExtraCode && ExtraCode[0]) {
241 if (ExtraCode[1] != 0) return true; // Unknown modifier.
242
243 switch (ExtraCode[0]) {
244 default: return true; // Unknown modifier.
245 case 'r':
246 break;
247 }
248 }
Anton Korobeynikovf3693302008-10-10 10:15:03 +0000249
250 printOperand(MI, OpNo);
251
252 return false;
253}
254
255bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
256 unsigned OpNo,
257 unsigned AsmVariant,
258 const char *ExtraCode) {
259 if (ExtraCode && ExtraCode[0])
260 return true; // Unknown modifier
261
262 O << '[';
263 printMemOperand(MI, OpNo);
264 O << ']';
265
266 return false;
267}
Douglas Gregor1555a232009-06-16 20:12:29 +0000268
Bob Wilsona96751f2009-06-23 23:59:40 +0000269// Force static initialization.
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000270extern "C" void LLVMInitializeSparcAsmPrinter() {
Daniel Dunbar0c795d62009-07-25 06:49:55 +0000271 RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000272}