blob: f95923a5355823152b6acc13bfef59f97af41a25 [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 {
Bill Wendling57f0db82009-02-24 08:30:20 +000047 public:
David Greene71847812009-07-14 20:18:05 +000048 explicit SparcAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattneraf76e592009-08-22 20:48:53 +000049 const MCAsmInfo *T, bool V)
Chris Lattnerd3b31a72010-01-27 00:20:02 +000050 : AsmPrinter(O, TM, T, V) {}
Brian Gaeke4acfd032004-03-04 06:00:41 +000051
Brian Gaeke4acfd032004-03-04 06:00:41 +000052 virtual const char *getPassName() const {
Chris Lattner7c90f732006-02-05 05:50:24 +000053 return "Sparc Assembly Printer";
Brian Gaeke4acfd032004-03-04 06:00:41 +000054 }
55
Brian Gaeke446ae112004-06-15 19:52:59 +000056 void printOperand(const MachineInstr *MI, int opNum);
Chris Lattnerad7a3e62006-02-10 07:35:42 +000057 void printMemOperand(const MachineInstr *MI, int opNum,
58 const char *Modifier = 0);
Chris Lattner7c90f732006-02-05 05:50:24 +000059 void printCCOperand(const MachineInstr *MI, int opNum);
Chris Lattner6788faa2006-01-31 06:49:09 +000060
Chris Lattner41aefdc2009-08-08 01:32:19 +000061 void printInstruction(const MachineInstr *MI); // autogenerated.
Chris Lattnerd95148f2009-09-13 20:19:22 +000062 static const char *getRegisterName(unsigned RegNo);
Chris Lattner05af2612009-09-13 20:08:00 +000063
Misha Brukmanb5f662f2005-04-21 23:30:14 +000064 bool runOnMachineFunction(MachineFunction &F);
Anton Korobeynikovf3693302008-10-10 10:15:03 +000065 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
66 unsigned AsmVariant, const char *ExtraCode);
67 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
68 unsigned AsmVariant, const char *ExtraCode);
Chris Lattnerdb486a62009-09-15 17:46:24 +000069
Chris Lattnerdb486a62009-09-15 17:46:24 +000070 bool printGetPCX(const MachineInstr *MI, unsigned OpNo);
Brian Gaeke4acfd032004-03-04 06:00:41 +000071 };
72} // end of anonymous namespace
73
Chris Lattner7c90f732006-02-05 05:50:24 +000074#include "SparcGenAsmWriter.inc"
Chris Lattner994b7352005-12-16 06:34:17 +000075
Evan Cheng4eecdeb2008-02-02 08:39:46 +000076/// runOnMachineFunction - This uses the printInstruction()
Brian Gaeke4acfd032004-03-04 06:00:41 +000077/// method to print assembly for each instruction.
78///
Chris Lattner7c90f732006-02-05 05:50:24 +000079bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner1dbed162005-12-17 07:04:29 +000080 SetupMachineFunction(MF);
81
Chris Lattner4129ccd2010-01-27 00:17:20 +000082 EmitFunctionHeader();
Chris Lattnerb5e9eb62005-12-17 07:11:43 +000083
Brian Gaeke4acfd032004-03-04 06:00:41 +000084 // Print out code for the function.
85 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
86 I != E; ++I) {
87 // Print a label for the basic block.
Chris Lattnerd3b31a72010-01-27 00:20:02 +000088 if (I != MF.begin())
Chris Lattner70a54c02009-09-13 18:25:37 +000089 EmitBasicBlockStart(I);
Chris Lattnerd3b31a72010-01-27 00:20:02 +000090
Brian Gaeke4acfd032004-03-04 06:00:41 +000091 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukman27177f82005-04-22 18:06:01 +000092 II != E; ++II) {
Brian Gaeke4acfd032004-03-04 06:00:41 +000093 // Print the assembly for the instruction.
Devang Patelaf0e2722009-10-06 02:19:11 +000094 processDebugLoc(II, true);
Chris Lattner0d8fcd32005-12-17 06:54:41 +000095 printInstruction(II);
Chris Lattnerc5ea2632009-09-09 23:14:36 +000096
David Greene1924aab2009-11-13 21:34:57 +000097 if (VerboseAsm)
Chris Lattnerc5ea2632009-09-09 23:14:36 +000098 EmitComments(*II);
99 O << '\n';
Devang Patelaf0e2722009-10-06 02:19:11 +0000100 processDebugLoc(II, false);
Chris Lattner1dbed162005-12-17 07:04:29 +0000101 ++EmittedInsts;
Brian Gaeke4acfd032004-03-04 06:00:41 +0000102 }
103 }
104
Richard Pennington930e4d92009-09-08 12:47:30 +0000105 // Emit post-function debug information.
106 DW->EndFunction(&MF);
107
Brian Gaeke4acfd032004-03-04 06:00:41 +0000108 // We didn't modify anything.
Chris Lattner10b318b2010-01-17 21:43:43 +0000109 O << "\t.size\t" << *CurrentFnSym << ", .-" << *CurrentFnSym << '\n';
Brian Gaeke4acfd032004-03-04 06:00:41 +0000110 return false;
111}
112
Chris Lattnerdb486a62009-09-15 17:46:24 +0000113
Chris Lattner7c90f732006-02-05 05:50:24 +0000114void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Brian Gaeke446ae112004-06-15 19:52:59 +0000115 const MachineOperand &MO = MI->getOperand (opNum);
Brian Gaeke446ae112004-06-15 19:52:59 +0000116 bool CloseParen = false;
Dan Gohmand735b802008-10-03 15:45:36 +0000117 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
Brian Gaeke446ae112004-06-15 19:52:59 +0000118 O << "%hi(";
119 CloseParen = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000120 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
121 !MO.isReg() && !MO.isImm()) {
Brian Gaeke446ae112004-06-15 19:52:59 +0000122 O << "%lo(";
123 CloseParen = true;
124 }
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000125 switch (MO.getType()) {
Chris Lattner2d90ac72006-05-04 18:05:43 +0000126 case MachineOperand::MO_Register:
Chris Lattner762ccea2009-09-13 20:31:40 +0000127 O << "%" << LowercaseString(getRegisterName(MO.getReg()));
Brian Gaeke446ae112004-06-15 19:52:59 +0000128 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000129
Chris Lattner63b3d712006-05-04 17:21:20 +0000130 case MachineOperand::MO_Immediate:
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000131 O << (int)MO.getImm();
Brian Gaeke446ae112004-06-15 19:52:59 +0000132 break;
Nate Begeman37efe672006-04-22 18:53:45 +0000133 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerf71cb012010-01-26 04:55:51 +0000134 O << *MO.getMBB()->getSymbol(OutContext);
Brian Gaeke09c13092004-06-17 19:39:23 +0000135 return;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000136 case MachineOperand::MO_GlobalAddress:
Chris Lattner10b318b2010-01-17 21:43:43 +0000137 O << *GetGlobalValueSymbol(MO.getGlobal());
Brian Gaeke446ae112004-06-15 19:52:59 +0000138 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000139 case MachineOperand::MO_ExternalSymbol:
140 O << MO.getSymbolName();
Brian Gaeke446ae112004-06-15 19:52:59 +0000141 break;
Brian Gaeke8a0ae9e2004-06-27 22:50:44 +0000142 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000143 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000144 << MO.getIndex();
Brian Gaeke8a0ae9e2004-06-27 22:50:44 +0000145 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000146 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000147 llvm_unreachable("<unknown operand type>");
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000148 }
Brian Gaeke446ae112004-06-15 19:52:59 +0000149 if (CloseParen) O << ")";
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000150}
151
Chris Lattnerad7a3e62006-02-10 07:35:42 +0000152void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
153 const char *Modifier) {
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000154 printOperand(MI, opNum);
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000155
Chris Lattnerad7a3e62006-02-10 07:35:42 +0000156 // If this is an ADD operand, emit it like normal operands.
157 if (Modifier && !strcmp(Modifier, "arith")) {
158 O << ", ";
159 printOperand(MI, opNum+1);
160 return;
161 }
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000162
Dan Gohmand735b802008-10-03 15:45:36 +0000163 if (MI->getOperand(opNum+1).isReg() &&
Chris Lattner7c90f732006-02-05 05:50:24 +0000164 MI->getOperand(opNum+1).getReg() == SP::G0)
Chris Lattner76acc872005-12-18 02:37:35 +0000165 return; // don't print "+%g0"
Dan Gohmand735b802008-10-03 15:45:36 +0000166 if (MI->getOperand(opNum+1).isImm() &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000167 MI->getOperand(opNum+1).getImm() == 0)
Chris Lattner76acc872005-12-18 02:37:35 +0000168 return; // don't print "+0"
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000169
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000170 O << "+";
Dan Gohmand735b802008-10-03 15:45:36 +0000171 if (MI->getOperand(opNum+1).isGlobal() ||
172 MI->getOperand(opNum+1).isCPI()) {
Chris Lattnere1389ad2005-12-18 02:27:00 +0000173 O << "%lo(";
174 printOperand(MI, opNum+1);
175 O << ")";
176 } else {
177 printOperand(MI, opNum+1);
178 }
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000179}
180
Chris Lattnerdb486a62009-09-15 17:46:24 +0000181bool SparcAsmPrinter::printGetPCX(const MachineInstr *MI, unsigned opNum) {
182 std::string operand = "";
183 const MachineOperand &MO = MI->getOperand(opNum);
184 switch (MO.getType()) {
185 default: assert(0 && "Operand is not a register ");
186 case MachineOperand::MO_Register:
187 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
188 "Operand is not a physical register ");
189 operand = "%" + LowercaseString(getRegisterName(MO.getReg()));
190 break;
191 }
192
Chris Lattnerd3b31a72010-01-27 00:20:02 +0000193 unsigned bbNum = MI->getParent()->getNumber();
Chris Lattnerdb486a62009-09-15 17:46:24 +0000194
195 O << '\n' << ".LLGETPCH" << bbNum << ":\n";
196 O << "\tcall\t.LLGETPC" << bbNum << '\n' ;
197
198 O << "\t sethi\t"
199 << "%hi(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << bbNum << ")), "
200 << operand << '\n' ;
201
202 O << ".LLGETPC" << bbNum << ":\n" ;
203 O << "\tor\t" << operand
204 << ", %lo(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << bbNum << ")), "
205 << operand << '\n';
206 O << "\tadd\t" << operand << ", %o7, " << operand << '\n';
207
208 return true;
209}
210
Chris Lattner7c90f732006-02-05 05:50:24 +0000211void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000212 int CC = (int)MI->getOperand(opNum).getImm();
Chris Lattner7c90f732006-02-05 05:50:24 +0000213 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
Chris Lattner6788faa2006-01-31 06:49:09 +0000214}
215
Anton Korobeynikovf3693302008-10-10 10:15:03 +0000216/// PrintAsmOperand - Print out an operand for an inline asm expression.
217///
218bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
219 unsigned AsmVariant,
220 const char *ExtraCode) {
Anton Korobeynikov4cf5e2e2008-10-10 20:29:50 +0000221 if (ExtraCode && ExtraCode[0]) {
222 if (ExtraCode[1] != 0) return true; // Unknown modifier.
223
224 switch (ExtraCode[0]) {
225 default: return true; // Unknown modifier.
226 case 'r':
227 break;
228 }
229 }
Anton Korobeynikovf3693302008-10-10 10:15:03 +0000230
231 printOperand(MI, OpNo);
232
233 return false;
234}
235
236bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
237 unsigned OpNo,
238 unsigned AsmVariant,
239 const char *ExtraCode) {
240 if (ExtraCode && ExtraCode[0])
241 return true; // Unknown modifier
242
243 O << '[';
244 printMemOperand(MI, OpNo);
245 O << ']';
246
247 return false;
248}
Douglas Gregor1555a232009-06-16 20:12:29 +0000249
Bob Wilsona96751f2009-06-23 23:59:40 +0000250// Force static initialization.
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000251extern "C" void LLVMInitializeSparcAsmPrinter() {
Daniel Dunbar0c795d62009-07-25 06:49:55 +0000252 RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000253}