blob: 9ff41cab90c68a072bfd9e6b1a0161ba61a9fe85 [file] [log] [blame]
Nate Begeman6cca84e2005-10-16 05:39:50 +00001//===-- PPCAsmPrinter.cpp - Print machine instrs to PowerPC assembly --------=//
Misha Brukmanb4402432005-04-21 23:30:14 +00002//
Misha Brukmane05203f2004-06-21 16:55:25 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb4402432005-04-21 23:30:14 +00007//
Misha Brukmane05203f2004-06-21 16:55:25 +00008//===----------------------------------------------------------------------===//
9//
Misha Brukmanb604b4d2004-07-08 17:58:04 +000010// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to PowerPC assembly language. This printer is
Chris Lattner07fad1c2004-07-28 20:18:53 +000012// the output mechanism used by `llc'.
Misha Brukmane05203f2004-06-21 16:55:25 +000013//
Misha Brukmanb604b4d2004-07-08 17:58:04 +000014// Documentation at http://developer.apple.com/documentation/DeveloperTools/
15// Reference/Assembler/ASMIntroduction/chapter_1_section_1.html
Misha Brukman811f5c22004-06-29 17:13:26 +000016//
Misha Brukmane05203f2004-06-21 16:55:25 +000017//===----------------------------------------------------------------------===//
18
Misha Brukmanf57c3cd2004-06-24 17:31:42 +000019#define DEBUG_TYPE "asmprinter"
Chris Lattnerbfca1ab2005-10-14 23:51:18 +000020#include "PPC.h"
Chris Lattner8c6a41e2006-11-17 22:10:59 +000021#include "PPCPredicates.h"
Chris Lattner6f3b9542005-10-14 23:59:06 +000022#include "PPCTargetMachine.h"
Chris Lattnerbfca1ab2005-10-14 23:51:18 +000023#include "PPCSubtarget.h"
Misha Brukmane05203f2004-06-21 16:55:25 +000024#include "llvm/Constants.h"
25#include "llvm/DerivedTypes.h"
26#include "llvm/Module.h"
27#include "llvm/Assembly/Writer.h"
Chris Lattner0ced9052004-08-16 23:25:21 +000028#include "llvm/CodeGen/AsmPrinter.h"
Jim Laskeyb0609d92006-01-04 13:52:30 +000029#include "llvm/CodeGen/DwarfWriter.h"
Jim Laskeyc56315c2007-01-26 21:22:28 +000030#include "llvm/CodeGen/MachineModuleInfo.h"
Misha Brukmanf57c3cd2004-06-24 17:31:42 +000031#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukmane05203f2004-06-21 16:55:25 +000032#include "llvm/CodeGen/MachineInstr.h"
Misha Brukmane05203f2004-06-21 16:55:25 +000033#include "llvm/Support/Mangler.h"
Nate Begeman4d847042004-09-04 14:51:26 +000034#include "llvm/Support/MathExtras.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000035#include "llvm/Support/CommandLine.h"
36#include "llvm/Support/Debug.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000037#include "llvm/Support/Compiler.h"
Jim Laskeya6211dc2006-09-06 18:34:40 +000038#include "llvm/Target/TargetAsmInfo.h"
Nate Begemandd8f1d82004-10-26 06:02:38 +000039#include "llvm/Target/MRegisterInfo.h"
Nate Begeman3f76eb62004-11-25 07:09:01 +000040#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng5f997602006-02-18 00:08:58 +000041#include "llvm/Target/TargetOptions.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000042#include "llvm/ADT/Statistic.h"
43#include "llvm/ADT/StringExtras.h"
Misha Brukmanf57c3cd2004-06-24 17:31:42 +000044#include <set>
Chris Lattner0ced9052004-08-16 23:25:21 +000045using namespace llvm;
Misha Brukmane05203f2004-06-21 16:55:25 +000046
Chris Lattner1ef9cd42006-12-19 22:59:26 +000047STATISTIC(EmittedInsts, "Number of machine instrs printed");
Misha Brukmane05203f2004-06-21 16:55:25 +000048
Chris Lattner1ef9cd42006-12-19 22:59:26 +000049namespace {
Jim Laskeya6211dc2006-09-06 18:34:40 +000050 struct VISIBILITY_HIDDEN PPCAsmPrinter : public AsmPrinter {
Chris Lattner57575112005-12-16 00:22:14 +000051 std::set<std::string> FnStubs, GVStubs;
Chris Lattner8597a2f2006-09-20 17:07:15 +000052 const PPCSubtarget &Subtarget;
Evan Chengfa54c0b2006-12-01 07:56:37 +000053
Jim Laskey261779b2006-09-07 22:06:40 +000054 PPCAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
Chris Lattner8597a2f2006-09-20 17:07:15 +000055 : AsmPrinter(O, TM, T), Subtarget(TM.getSubtarget<PPCSubtarget>()) {
56 }
Misha Brukmanb4402432005-04-21 23:30:14 +000057
Misha Brukmane05203f2004-06-21 16:55:25 +000058 virtual const char *getPassName() const {
Nate Begeman4bfceb12004-09-04 05:00:00 +000059 return "PowerPC Assembly Printer";
Misha Brukmane05203f2004-06-21 16:55:25 +000060 }
61
Nate Begeman6cca84e2005-10-16 05:39:50 +000062 PPCTargetMachine &getTM() {
63 return static_cast<PPCTargetMachine&>(TM);
Chris Lattner0ced9052004-08-16 23:25:21 +000064 }
65
Nate Begeman8465fe82005-07-20 22:42:00 +000066 unsigned enumRegToMachineReg(unsigned enumReg) {
67 switch (enumReg) {
68 default: assert(0 && "Unhandled register!"); break;
69 case PPC::CR0: return 0;
70 case PPC::CR1: return 1;
71 case PPC::CR2: return 2;
72 case PPC::CR3: return 3;
73 case PPC::CR4: return 4;
74 case PPC::CR5: return 5;
75 case PPC::CR6: return 6;
76 case PPC::CR7: return 7;
77 }
78 abort();
79 }
80
Nate Begeman0ad7f812004-08-14 22:09:10 +000081 /// printInstruction - This method is automatically generated by tablegen
82 /// from the instruction set description. This method returns true if the
83 /// machine instruction was sufficiently described to print it, otherwise it
84 /// returns false.
85 bool printInstruction(const MachineInstr *MI);
86
Misha Brukmane05203f2004-06-21 16:55:25 +000087 void printMachineInstruction(const MachineInstr *MI);
Chris Lattner6ab87fa2005-11-17 19:25:59 +000088 void printOp(const MachineOperand &MO);
Jim Laskey41621a72006-12-20 20:56:46 +000089
90 /// stripRegisterPrefix - This method strips the character prefix from a
91 /// register name so that only the number is left. Used by for linux asm.
Jim Laskeyc2c861d2006-12-20 21:33:34 +000092 const char *stripRegisterPrefix(const char *RegName) {
93 switch (RegName[0]) {
94 case 'r':
95 case 'f':
96 case 'v': return RegName + 1;
Jim Laskey4c90a6d2006-12-20 21:35:00 +000097 case 'c': if (RegName[1] == 'r') return RegName + 2;
Jim Laskey41621a72006-12-20 20:56:46 +000098 }
Jim Laskeyc2c861d2006-12-20 21:33:34 +000099
100 return RegName;
Jim Laskey41621a72006-12-20 20:56:46 +0000101 }
102
103 /// printRegister - Print register according to target requirements.
104 ///
105 void printRegister(const MachineOperand &MO, bool R0AsZero) {
106 unsigned RegNo = MO.getReg();
107 assert(MRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
108
109 // If we should use 0 for R0.
110 if (R0AsZero && RegNo == PPC::R0) {
111 O << "0";
112 return;
113 }
114
Jim Laskeyc2c861d2006-12-20 21:33:34 +0000115 const char *RegName = TM.getRegisterInfo()->get(RegNo).Name;
Jim Laskey41621a72006-12-20 20:56:46 +0000116 // Linux assembler (Others?) does not take register mnemonics.
117 // FIXME - What about special registers used in mfspr/mtspr?
Jim Laskeyc2c861d2006-12-20 21:33:34 +0000118 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
119 O << RegName;
Jim Laskey41621a72006-12-20 20:56:46 +0000120 }
Chris Lattnerec1cc1b2004-08-14 23:27:29 +0000121
Chris Lattnerf7f05672006-02-01 22:38:46 +0000122 void printOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnerec1cc1b2004-08-14 23:27:29 +0000123 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattner10b71c02006-05-04 18:05:43 +0000124 if (MO.isRegister()) {
Jim Laskey41621a72006-12-20 20:56:46 +0000125 printRegister(MO, false);
Chris Lattnerda2e56f2004-08-15 05:46:14 +0000126 } else if (MO.isImmediate()) {
127 O << MO.getImmedValue();
Chris Lattnerec1cc1b2004-08-14 23:27:29 +0000128 } else {
129 printOp(MO);
130 }
131 }
Chris Lattnerf7f05672006-02-01 22:38:46 +0000132
133 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner1bad2542006-02-23 19:31:10 +0000134 unsigned AsmVariant, const char *ExtraCode);
Chris Lattner7674d902006-02-24 20:27:40 +0000135 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
136 unsigned AsmVariant, const char *ExtraCode);
137
Chris Lattnerf7f05672006-02-01 22:38:46 +0000138
Chris Lattner2771e2c2006-03-25 06:12:06 +0000139 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
140 char value = MI->getOperand(OpNo).getImmedValue();
141 value = (value << (32-5)) >> (32-5);
142 O << (int)value;
143 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000144 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman3ad3ad42004-08-21 05:56:39 +0000145 unsigned char value = MI->getOperand(OpNo).getImmedValue();
Chris Lattnerf7833ba2004-08-21 19:11:03 +0000146 assert(value <= 31 && "Invalid u5imm argument!");
Nate Begeman3ad3ad42004-08-21 05:56:39 +0000147 O << (unsigned int)value;
148 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000149 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman143cf942004-08-30 02:28:06 +0000150 unsigned char value = MI->getOperand(OpNo).getImmedValue();
151 assert(value <= 63 && "Invalid u6imm argument!");
152 O << (unsigned int)value;
153 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000154 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman4bfceb12004-09-04 05:00:00 +0000155 O << (short)MI->getOperand(OpNo).getImmedValue();
156 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000157 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner8a796852004-08-15 05:20:16 +0000158 O << (unsigned short)MI->getOperand(OpNo).getImmedValue();
159 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000160 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner30055b92006-11-16 21:45:30 +0000161 if (MI->getOperand(OpNo).isImmediate()) {
162 O << (short)(MI->getOperand(OpNo).getImmedValue()*4);
163 } else {
164 O << "lo16(";
165 printOp(MI->getOperand(OpNo));
166 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Chengcdf36092007-10-14 05:57:21 +0000167 O << "-\"L" << getFunctionNumber() << "$pb\")";
Chris Lattner30055b92006-11-16 21:45:30 +0000168 else
169 O << ')';
170 }
Chris Lattner5a2fb972005-10-18 16:51:22 +0000171 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000172 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman4bfceb12004-09-04 05:00:00 +0000173 // Branches can take an immediate operand. This is used by the branch
174 // selection pass to print $+8, an eight byte displacement from the PC.
175 if (MI->getOperand(OpNo).isImmediate()) {
Evan Chengd7572fb2006-08-25 21:54:44 +0000176 O << "$+" << MI->getOperand(OpNo).getImmedValue()*4;
Nate Begeman4bfceb12004-09-04 05:00:00 +0000177 } else {
Chris Lattnerbd9efdb2005-11-17 19:16:08 +0000178 printOp(MI->getOperand(OpNo));
Nate Begeman4bfceb12004-09-04 05:00:00 +0000179 }
Nate Begeman61738782004-09-02 08:13:00 +0000180 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000181 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner6ab87fa2005-11-17 19:25:59 +0000182 const MachineOperand &MO = MI->getOperand(OpNo);
Evan Cheng73136df2006-02-22 20:19:42 +0000183 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner57575112005-12-16 00:22:14 +0000184 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
185 GlobalValue *GV = MO.getGlobal();
Reid Spencer5301e7c2007-01-30 20:08:39 +0000186 if (((GV->isDeclaration() || GV->hasWeakLinkage() ||
Chris Lattner57575112005-12-16 00:22:14 +0000187 GV->hasLinkOnceLinkage()))) {
188 // Dynamically-resolved functions need a stub for the function.
189 std::string Name = Mang->getValueName(GV);
190 FnStubs.insert(Name);
191 O << "L" << Name << "$stub";
Evan Chengfa54c0b2006-12-01 07:56:37 +0000192 if (GV->hasExternalWeakLinkage())
Rafael Espindolad7998d02006-12-18 03:37:18 +0000193 ExtWeakSymbols.insert(GV);
Chris Lattner57575112005-12-16 00:22:14 +0000194 return;
195 }
196 }
Chris Lattnercdde99902005-11-17 19:40:30 +0000197 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000198 std::string Name(TAI->getGlobalPrefix()); Name += MO.getSymbolName();
Chris Lattnercdde99902005-11-17 19:40:30 +0000199 FnStubs.insert(Name);
200 O << "L" << Name << "$stub";
201 return;
Chris Lattnercdde99902005-11-17 19:40:30 +0000202 }
Chris Lattner6ab87fa2005-11-17 19:25:59 +0000203 }
Chris Lattnercdde99902005-11-17 19:40:30 +0000204
205 printOp(MI->getOperand(OpNo));
Chris Lattnerbd9efdb2005-11-17 19:16:08 +0000206 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000207 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begemana171f6b2005-11-16 00:48:01 +0000208 O << (int)MI->getOperand(OpNo).getImmedValue()*4;
209 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000210 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Evan Chengcdf36092007-10-14 05:57:21 +0000211 O << "\"L" << getFunctionNumber() << "$pb\"\n";
212 O << "\"L" << getFunctionNumber() << "$pb\":";
Nate Begeman61738782004-09-02 08:13:00 +0000213 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000214 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Nate Begemana9443f22005-07-21 20:44:43 +0000215 if (MI->getOperand(OpNo).isImmediate()) {
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000216 printS16ImmOperand(MI, OpNo);
Nate Begemana9443f22005-07-21 20:44:43 +0000217 } else {
Nick Lewyckye6049c22007-03-03 05:29:51 +0000218 if (Subtarget.isDarwin()) O << "ha16(";
Nate Begemana9443f22005-07-21 20:44:43 +0000219 printOp(MI->getOperand(OpNo));
Chris Lattner9e56e5c2006-07-26 21:12:04 +0000220 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Chengcdf36092007-10-14 05:57:21 +0000221 O << "-\"L" << getFunctionNumber() << "$pb\"";
Nick Lewyckye6049c22007-03-03 05:29:51 +0000222 if (Subtarget.isDarwin())
Nate Begemana9443f22005-07-21 20:44:43 +0000223 O << ')';
Nick Lewyckye6049c22007-03-03 05:29:51 +0000224 else
225 O << "@ha";
Nate Begemana9443f22005-07-21 20:44:43 +0000226 }
Nate Begeman4bfceb12004-09-04 05:00:00 +0000227 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000228 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman4bfceb12004-09-04 05:00:00 +0000229 if (MI->getOperand(OpNo).isImmediate()) {
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000230 printS16ImmOperand(MI, OpNo);
Nate Begeman4bfceb12004-09-04 05:00:00 +0000231 } else {
Nick Lewyckye6049c22007-03-03 05:29:51 +0000232 if (Subtarget.isDarwin()) O << "lo16(";
Nate Begeman3f76eb62004-11-25 07:09:01 +0000233 printOp(MI->getOperand(OpNo));
Chris Lattner9e56e5c2006-07-26 21:12:04 +0000234 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Chengcdf36092007-10-14 05:57:21 +0000235 O << "-\"L" << getFunctionNumber() << "$pb\"";
Nick Lewyckye6049c22007-03-03 05:29:51 +0000236 if (Subtarget.isDarwin())
Nate Begemana9443f22005-07-21 20:44:43 +0000237 O << ')';
Nick Lewyckye6049c22007-03-03 05:29:51 +0000238 else
239 O << "@l";
Nate Begeman4bfceb12004-09-04 05:00:00 +0000240 }
241 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000242 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman8465fe82005-07-20 22:42:00 +0000243 unsigned CCReg = MI->getOperand(OpNo).getReg();
244 unsigned RegNo = enumRegToMachineReg(CCReg);
245 O << (0x80 >> RegNo);
246 }
Chris Lattner7674d902006-02-24 20:27:40 +0000247 // The new addressing mode printers.
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000248 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
249 printSymbolLo(MI, OpNo);
250 O << '(';
Chris Lattner139eac52006-03-21 17:21:13 +0000251 if (MI->getOperand(OpNo+1).isRegister() &&
252 MI->getOperand(OpNo+1).getReg() == PPC::R0)
253 O << "0";
254 else
255 printOperand(MI, OpNo+1);
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000256 O << ')';
257 }
Chris Lattner77373d12006-03-22 05:26:03 +0000258 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
259 if (MI->getOperand(OpNo).isImmediate())
260 printS16X4ImmOperand(MI, OpNo);
261 else
262 printSymbolLo(MI, OpNo);
263 O << '(';
264 if (MI->getOperand(OpNo+1).isRegister() &&
265 MI->getOperand(OpNo+1).getReg() == PPC::R0)
266 O << "0";
267 else
268 printOperand(MI, OpNo+1);
269 O << ')';
270 }
271
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000272 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
Nate Begemanc1263972005-12-19 23:40:42 +0000273 // When used as the base register, r0 reads constant zero rather than
274 // the value contained in the register. For this reason, the darwin
275 // assembler requires that we print r0 as 0 (no r) when used as the base.
276 const MachineOperand &MO = MI->getOperand(OpNo);
Jim Laskey41621a72006-12-20 20:56:46 +0000277 printRegister(MO, true);
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000278 O << ", ";
279 printOperand(MI, OpNo+1);
280 }
281
Chris Lattner6be72602006-11-04 05:27:39 +0000282 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
283 const char *Modifier);
284
Misha Brukmanb4402432005-04-21 23:30:14 +0000285 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
Nate Begeman4bfceb12004-09-04 05:00:00 +0000286 virtual bool doFinalization(Module &M) = 0;
Jim Laskey18fc0972007-02-21 22:47:38 +0000287
288 virtual void EmitExternalGlobal(const GlobalVariable *GV);
Nate Begeman4bfceb12004-09-04 05:00:00 +0000289 };
Misha Brukmanb4402432005-04-21 23:30:14 +0000290
Jim Laskey28663c72006-12-21 20:26:09 +0000291 /// LinuxAsmPrinter - PowerPC assembly printer, customized for Linux
292 struct VISIBILITY_HIDDEN LinuxAsmPrinter : public PPCAsmPrinter {
293
294 DwarfWriter DW;
295
296 LinuxAsmPrinter(std::ostream &O, PPCTargetMachine &TM,
297 const TargetAsmInfo *T)
298 : PPCAsmPrinter(O, TM, T), DW(O, this, T) {
299 }
300
301 virtual const char *getPassName() const {
302 return "Linux PPC Assembly Printer";
303 }
304
305 bool runOnMachineFunction(MachineFunction &F);
306 bool doInitialization(Module &M);
307 bool doFinalization(Module &M);
308
309 void getAnalysisUsage(AnalysisUsage &AU) const {
310 AU.setPreservesAll();
Jim Laskeyc56315c2007-01-26 21:22:28 +0000311 AU.addRequired<MachineModuleInfo>();
Jim Laskey28663c72006-12-21 20:26:09 +0000312 PPCAsmPrinter::getAnalysisUsage(AU);
313 }
314
315 /// getSectionForFunction - Return the section that we should emit the
316 /// specified function body into.
317 virtual std::string getSectionForFunction(const Function &F) const;
318 };
319
Misha Brukman175fdd42004-09-05 02:42:44 +0000320 /// DarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac OS
321 /// X
Chris Lattner996795b2006-06-28 23:17:24 +0000322 struct VISIBILITY_HIDDEN DarwinAsmPrinter : public PPCAsmPrinter {
Jim Laskeyb0609d92006-01-04 13:52:30 +0000323
Jim Laskeya6211dc2006-09-06 18:34:40 +0000324 DwarfWriter DW;
Nate Begeman4bfceb12004-09-04 05:00:00 +0000325
Jim Laskey261779b2006-09-07 22:06:40 +0000326 DarwinAsmPrinter(std::ostream &O, PPCTargetMachine &TM,
327 const TargetAsmInfo *T)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000328 : PPCAsmPrinter(O, TM, T), DW(O, this, T) {
Nate Begeman4bfceb12004-09-04 05:00:00 +0000329 }
330
331 virtual const char *getPassName() const {
332 return "Darwin PPC Assembly Printer";
333 }
Chris Lattnere0f5f8e2005-12-09 18:24:29 +0000334
Misha Brukmanb4402432005-04-21 23:30:14 +0000335 bool runOnMachineFunction(MachineFunction &F);
Nate Begeman15527112005-07-21 01:25:49 +0000336 bool doInitialization(Module &M);
Misha Brukmane05203f2004-06-21 16:55:25 +0000337 bool doFinalization(Module &M);
Jim Laskey219d5592006-01-04 22:28:25 +0000338
339 void getAnalysisUsage(AnalysisUsage &AU) const {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000340 AU.setPreservesAll();
Jim Laskeyc56315c2007-01-26 21:22:28 +0000341 AU.addRequired<MachineModuleInfo>();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000342 PPCAsmPrinter::getAnalysisUsage(AU);
Jim Laskey219d5592006-01-04 22:28:25 +0000343 }
344
Chris Lattner028d6632006-10-05 02:42:20 +0000345 /// getSectionForFunction - Return the section that we should emit the
346 /// specified function body into.
347 virtual std::string getSectionForFunction(const Function &F) const;
Misha Brukmane05203f2004-06-21 16:55:25 +0000348 };
349} // end of anonymous namespace
350
Nate Begeman0ad7f812004-08-14 22:09:10 +0000351// Include the auto-generated portion of the assembly writer
Chris Lattner0921e3b2005-10-14 23:37:35 +0000352#include "PPCGenAsmWriter.inc"
Nate Begeman0ad7f812004-08-14 22:09:10 +0000353
Chris Lattner6ab87fa2005-11-17 19:25:59 +0000354void PPCAsmPrinter::printOp(const MachineOperand &MO) {
Misha Brukmane05203f2004-06-21 16:55:25 +0000355 switch (MO.getType()) {
Chris Lattnerfef7a2d2006-05-04 17:21:20 +0000356 case MachineOperand::MO_Immediate:
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000357 cerr << "printOp() does not handle immediate values\n";
Misha Brukman47d5a222004-07-28 00:00:48 +0000358 abort();
Misha Brukman8d75aa42004-07-21 20:11:11 +0000359 return;
360
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000361 case MachineOperand::MO_MachineBasicBlock:
362 printBasicBlockLabel(MO.getMachineBasicBlock());
Misha Brukmane05203f2004-06-21 16:55:25 +0000363 return;
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000364 case MachineOperand::MO_JumpTableIndex:
Evan Chengcdf36092007-10-14 05:57:21 +0000365 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
366 << '_' << MO.getJumpTableIndex();
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000367 // FIXME: PIC relocation model
368 return;
Misha Brukmanb604b4d2004-07-08 17:58:04 +0000369 case MachineOperand::MO_ConstantPoolIndex:
Evan Chengcdf36092007-10-14 05:57:21 +0000370 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
371 << '_' << MO.getConstantPoolIndex();
Misha Brukmane05203f2004-06-21 16:55:25 +0000372 return;
Misha Brukmanb604b4d2004-07-08 17:58:04 +0000373 case MachineOperand::MO_ExternalSymbol:
Chris Lattner57575112005-12-16 00:22:14 +0000374 // Computing the address of an external symbol, not calling it.
Evan Cheng73136df2006-02-22 20:19:42 +0000375 if (TM.getRelocationModel() != Reloc::Static) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000376 std::string Name(TAI->getGlobalPrefix()); Name += MO.getSymbolName();
Chris Lattner57575112005-12-16 00:22:14 +0000377 GVStubs.insert(Name);
378 O << "L" << Name << "$non_lazy_ptr";
379 return;
380 }
Jim Laskeya6211dc2006-09-06 18:34:40 +0000381 O << TAI->getGlobalPrefix() << MO.getSymbolName();
Misha Brukmanb604b4d2004-07-08 17:58:04 +0000382 return;
Nate Begeman5bf9bfe2004-08-13 09:32:01 +0000383 case MachineOperand::MO_GlobalAddress: {
Chris Lattner57575112005-12-16 00:22:14 +0000384 // Computing the address of a global symbol, not calling it.
Nate Begeman5bf9bfe2004-08-13 09:32:01 +0000385 GlobalValue *GV = MO.getGlobal();
386 std::string Name = Mang->getValueName(GV);
Misha Brukman7dba17d2004-07-23 16:08:20 +0000387
Nate Begeman844186b2004-10-17 23:01:34 +0000388 // External or weakly linked global variables need non-lazily-resolved stubs
Evan Cheng73136df2006-02-22 20:19:42 +0000389 if (TM.getRelocationModel() != Reloc::Static) {
Reid Spencer5301e7c2007-01-30 20:08:39 +0000390 if (((GV->isDeclaration() || GV->hasWeakLinkage() ||
Chris Lattner57575112005-12-16 00:22:14 +0000391 GV->hasLinkOnceLinkage()))) {
Nate Begeman3f76eb62004-11-25 07:09:01 +0000392 GVStubs.insert(Name);
Chris Lattner57575112005-12-16 00:22:14 +0000393 O << "L" << Name << "$non_lazy_ptr";
394 return;
395 }
Nate Begeman5bf9bfe2004-08-13 09:32:01 +0000396 }
Chris Lattnercdde99902005-11-17 19:40:30 +0000397 O << Name;
Evan Chengfa54c0b2006-12-01 07:56:37 +0000398
Chris Lattnerc1a2a3b2007-05-03 16:39:48 +0000399 if (MO.getOffset() > 0)
400 O << "+" << MO.getOffset();
401 else if (MO.getOffset() < 0)
402 O << MO.getOffset();
403
Evan Chengfa54c0b2006-12-01 07:56:37 +0000404 if (GV->hasExternalWeakLinkage())
Rafael Espindolad7998d02006-12-18 03:37:18 +0000405 ExtWeakSymbols.insert(GV);
Misha Brukmane05203f2004-06-21 16:55:25 +0000406 return;
Nate Begeman5bf9bfe2004-08-13 09:32:01 +0000407 }
Misha Brukmanb4402432005-04-21 23:30:14 +0000408
Misha Brukmane05203f2004-06-21 16:55:25 +0000409 default:
Misha Brukmanb604b4d2004-07-08 17:58:04 +0000410 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukmana2737582004-06-25 15:11:34 +0000411 return;
Misha Brukmane05203f2004-06-21 16:55:25 +0000412 }
413}
414
Jim Laskey18fc0972007-02-21 22:47:38 +0000415/// EmitExternalGlobal - In this case we need to use the indirect symbol.
416///
417void PPCAsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
418 std::string Name = getGlobalLinkName(GV);
419 if (TM.getRelocationModel() != Reloc::Static) {
420 GVStubs.insert(Name);
421 O << "L" << Name << "$non_lazy_ptr";
422 return;
423 }
424 O << Name;
425}
426
Chris Lattner1bad2542006-02-23 19:31:10 +0000427/// PrintAsmOperand - Print out an operand for an inline asm expression.
428///
429bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
430 unsigned AsmVariant,
431 const char *ExtraCode) {
432 // Does this asm operand have a single letter operand modifier?
433 if (ExtraCode && ExtraCode[0]) {
434 if (ExtraCode[1] != 0) return true; // Unknown modifier.
435
436 switch (ExtraCode[0]) {
437 default: return true; // Unknown modifier.
Chris Lattner48518542007-01-25 02:52:50 +0000438 case 'c': // Don't print "$" before a global var name or constant.
439 // PPC never has a prefix.
440 printOperand(MI, OpNo);
441 return false;
Chris Lattner1bad2542006-02-23 19:31:10 +0000442 case 'L': // Write second word of DImode reference.
443 // Verify that this operand has two consecutive registers.
444 if (!MI->getOperand(OpNo).isRegister() ||
445 OpNo+1 == MI->getNumOperands() ||
446 !MI->getOperand(OpNo+1).isRegister())
447 return true;
448 ++OpNo; // Return the high-part.
449 break;
Chris Lattnercb35c612007-04-24 22:51:03 +0000450 case 'I':
451 // Write 'i' if an integer constant, otherwise nothing. Used to print
452 // addi vs add, etc.
Dan Gohman9da02f52007-09-14 20:33:02 +0000453 if (MI->getOperand(OpNo).isImmediate())
Chris Lattnercb35c612007-04-24 22:51:03 +0000454 O << "i";
455 return false;
Chris Lattner1bad2542006-02-23 19:31:10 +0000456 }
457 }
458
459 printOperand(MI, OpNo);
460 return false;
461}
462
Chris Lattner7674d902006-02-24 20:27:40 +0000463bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
464 unsigned AsmVariant,
465 const char *ExtraCode) {
466 if (ExtraCode && ExtraCode[0])
467 return true; // Unknown modifier.
Chris Lattnerf7937002007-02-01 00:39:08 +0000468 if (MI->getOperand(OpNo).isRegister())
469 printMemRegReg(MI, OpNo);
470 else
471 printMemRegImm(MI, OpNo);
Chris Lattner7674d902006-02-24 20:27:40 +0000472 return false;
473}
Chris Lattner1bad2542006-02-23 19:31:10 +0000474
Chris Lattner6be72602006-11-04 05:27:39 +0000475void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
476 const char *Modifier) {
477 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
478 unsigned Code = MI->getOperand(OpNo).getImm();
479 if (!strcmp(Modifier, "cc")) {
480 switch ((PPC::Predicate)Code) {
481 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
482 case PPC::PRED_LT: O << "lt"; return;
483 case PPC::PRED_LE: O << "le"; return;
484 case PPC::PRED_EQ: O << "eq"; return;
485 case PPC::PRED_GE: O << "ge"; return;
486 case PPC::PRED_GT: O << "gt"; return;
487 case PPC::PRED_NE: O << "ne"; return;
488 case PPC::PRED_UN: O << "un"; return;
489 case PPC::PRED_NU: O << "nu"; return;
490 }
491
492 } else {
493 assert(!strcmp(Modifier, "reg") &&
494 "Need to specify 'cc' or 'reg' as predicate op modifier!");
495 // Don't print the register for 'always'.
496 if (Code == PPC::PRED_ALWAYS) return;
497 printOperand(MI, OpNo+1);
498 }
499}
500
501
Nate Begeman0ad7f812004-08-14 22:09:10 +0000502/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
503/// the current output stream.
Misha Brukmane05203f2004-06-21 16:55:25 +0000504///
Chris Lattner0921e3b2005-10-14 23:37:35 +0000505void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Nate Begeman0ad7f812004-08-14 22:09:10 +0000506 ++EmittedInsts;
Chris Lattner2121f3c2005-10-14 22:44:13 +0000507
Nate Begeman52441732005-04-05 18:19:50 +0000508 // Check for slwi/srwi mnemonics.
509 if (MI->getOpcode() == PPC::RLWINM) {
510 bool FoundMnemonic = false;
511 unsigned char SH = MI->getOperand(2).getImmedValue();
512 unsigned char MB = MI->getOperand(3).getImmedValue();
513 unsigned char ME = MI->getOperand(4).getImmedValue();
514 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
515 O << "slwi "; FoundMnemonic = true;
516 }
517 if (SH <= 31 && MB == (32-SH) && ME == 31) {
518 O << "srwi "; FoundMnemonic = true;
519 SH = 32-SH;
520 }
521 if (FoundMnemonic) {
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000522 printOperand(MI, 0);
Misha Brukmanb4402432005-04-21 23:30:14 +0000523 O << ", ";
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000524 printOperand(MI, 1);
Nate Begeman52441732005-04-05 18:19:50 +0000525 O << ", " << (unsigned int)SH << "\n";
526 return;
527 }
Chris Lattner52a956d2006-06-20 23:18:58 +0000528 } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
Chris Lattnerf7b962d2006-02-08 06:56:40 +0000529 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
530 O << "mr ";
531 printOperand(MI, 0);
532 O << ", ";
533 printOperand(MI, 1);
534 O << "\n";
535 return;
536 }
Chris Lattner9ca15c82006-11-18 01:23:56 +0000537 } else if (MI->getOpcode() == PPC::RLDICR) {
538 unsigned char SH = MI->getOperand(2).getImmedValue();
539 unsigned char ME = MI->getOperand(3).getImmedValue();
540 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
541 if (63-SH == ME) {
542 O << "sldi ";
543 printOperand(MI, 0);
544 O << ", ";
545 printOperand(MI, 1);
546 O << ", " << (unsigned int)SH << "\n";
547 return;
548 }
Nate Begeman52441732005-04-05 18:19:50 +0000549 }
Misha Brukmanb4402432005-04-21 23:30:14 +0000550
Nate Begeman0ad7f812004-08-14 22:09:10 +0000551 if (printInstruction(MI))
552 return; // Printer was automatically generated
Misha Brukmanb4402432005-04-21 23:30:14 +0000553
Nate Begeman4bfceb12004-09-04 05:00:00 +0000554 assert(0 && "Unhandled instruction in asm writer!");
555 abort();
Nate Begeman0ad7f812004-08-14 22:09:10 +0000556 return;
Misha Brukmane05203f2004-06-21 16:55:25 +0000557}
558
Jim Laskey28663c72006-12-21 20:26:09 +0000559/// runOnMachineFunction - This uses the printMachineInstruction()
560/// method to print assembly for each instruction.
561///
562bool LinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Jim Laskeyc56315c2007-01-26 21:22:28 +0000563 DW.SetModuleInfo(&getAnalysis<MachineModuleInfo>());
Chris Lattner028d6632006-10-05 02:42:20 +0000564
Jim Laskey28663c72006-12-21 20:26:09 +0000565 SetupMachineFunction(MF);
566 O << "\n\n";
567
568 // Print out constants referenced by the function
569 EmitConstantPool(MF.getConstantPool());
570
571 // Print out labels for the function.
572 const Function *F = MF.getFunction();
573 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
574
575 switch (F->getLinkage()) {
576 default: assert(0 && "Unknown linkage type!");
577 case Function::InternalLinkage: // Symbols default to internal.
578 break;
579 case Function::ExternalLinkage:
580 O << "\t.global\t" << CurrentFnName << '\n'
581 << "\t.type\t" << CurrentFnName << ", @function\n";
582 break;
583 case Function::WeakLinkage:
584 case Function::LinkOnceLinkage:
585 O << "\t.global\t" << CurrentFnName << '\n';
586 O << "\t.weak\t" << CurrentFnName << '\n';
587 break;
588 }
Chris Lattner3c84b552007-01-14 06:37:54 +0000589
590 if (F->hasHiddenVisibility())
591 if (const char *Directive = TAI->getHiddenDirective())
592 O << Directive << CurrentFnName << "\n";
593
Jim Laskey28663c72006-12-21 20:26:09 +0000594 EmitAlignment(2, F);
595 O << CurrentFnName << ":\n";
596
597 // Emit pre-function debug information.
598 DW.BeginFunction(&MF);
599
600 // Print out code for the function.
601 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
602 I != E; ++I) {
603 // Print a label for the basic block.
604 if (I != MF.begin()) {
605 printBasicBlockLabel(I, true);
606 O << '\n';
607 }
608 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
609 II != E; ++II) {
610 // Print the assembly for the instruction.
611 O << "\t";
612 printMachineInstruction(II);
613 }
614 }
615
616 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << "\n";
617
618 // Print out jump tables referenced by the function.
619 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
620
621 // Emit post-function debug information.
622 DW.EndFunction();
623
624 // We didn't modify anything.
625 return false;
626}
627
628bool LinuxAsmPrinter::doInitialization(Module &M) {
Dan Gohmancf0a5342007-07-25 19:33:14 +0000629 bool Result = AsmPrinter::doInitialization(M);
Jim Laskey28663c72006-12-21 20:26:09 +0000630
631 // GNU as handles section names wrapped in quotes
632 Mang->setUseQuotes(true);
633
634 SwitchToTextSection(TAI->getTextSection());
635
636 // Emit initial debug information.
637 DW.BeginModule(&M);
Dan Gohmancf0a5342007-07-25 19:33:14 +0000638 return Result;
Jim Laskey28663c72006-12-21 20:26:09 +0000639}
640
641bool LinuxAsmPrinter::doFinalization(Module &M) {
642 const TargetData *TD = TM.getTargetData();
643
644 // Print out module-level global variables here.
645 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
646 I != E; ++I) {
647 if (!I->hasInitializer()) continue; // External global require no code
648
649 // Check to see if this is a special global used by LLVM, if so, emit it.
650 if (EmitSpecialLLVMGlobal(I))
651 continue;
Chris Lattner3c84b552007-01-14 06:37:54 +0000652
Jim Laskey28663c72006-12-21 20:26:09 +0000653 std::string name = Mang->getValueName(I);
Chris Lattner3c84b552007-01-14 06:37:54 +0000654
655 if (I->hasHiddenVisibility())
656 if (const char *Directive = TAI->getHiddenDirective())
657 O << Directive << name << "\n";
658
Jim Laskey28663c72006-12-21 20:26:09 +0000659 Constant *C = I->getInitializer();
660 unsigned Size = TD->getTypeSize(C->getType());
661 unsigned Align = TD->getPreferredAlignmentLog(I);
662
663 if (C->isNullValue() && /* FIXME: Verify correct */
Evan Cheng1ff71872007-09-21 00:41:19 +0000664 !I->hasSection() &&
Jim Laskey28663c72006-12-21 20:26:09 +0000665 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
Evan Cheng1ff71872007-09-21 00:41:19 +0000666 I->hasLinkOnceLinkage() || I->hasExternalLinkage())) {
Jim Laskey28663c72006-12-21 20:26:09 +0000667 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
668 if (I->hasExternalLinkage()) {
669 O << "\t.global " << name << '\n';
670 O << "\t.type " << name << ", @object\n";
Nick Lewycky5805c462007-07-25 03:48:45 +0000671 O << name << ":\n";
672 O << "\t.zero " << Size << "\n";
Jim Laskey28663c72006-12-21 20:26:09 +0000673 } else if (I->hasInternalLinkage()) {
674 SwitchToDataSection("\t.data", I);
675 O << TAI->getLCOMMDirective() << name << "," << Size;
676 } else {
677 SwitchToDataSection("\t.data", I);
678 O << ".comm " << name << "," << Size;
679 }
680 O << "\t\t" << TAI->getCommentString() << " '" << I->getName() << "'\n";
681 } else {
682 switch (I->getLinkage()) {
683 case GlobalValue::LinkOnceLinkage:
684 case GlobalValue::WeakLinkage:
685 O << "\t.global " << name << '\n'
686 << "\t.type " << name << ", @object\n"
687 << "\t.weak " << name << '\n';
688 SwitchToDataSection("\t.data", I);
689 break;
690 case GlobalValue::AppendingLinkage:
691 // FIXME: appending linkage variables should go into a section of
692 // their name or something. For now, just emit them as external.
693 case GlobalValue::ExternalLinkage:
694 // If external or appending, declare as a global symbol
695 O << "\t.global " << name << "\n"
696 << "\t.type " << name << ", @object\n";
697 // FALL THROUGH
698 case GlobalValue::InternalLinkage:
699 if (I->isConstant()) {
700 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
701 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
702 SwitchToDataSection(TAI->getCStringSection(), I);
703 break;
704 }
705 }
706
707 // FIXME: special handling for ".ctors" & ".dtors" sections
708 if (I->hasSection() &&
709 (I->getSection() == ".ctors" ||
710 I->getSection() == ".dtors")) {
711 std::string SectionName = ".section " + I->getSection()
712 + ",\"aw\",@progbits";
713 SwitchToDataSection(SectionName.c_str());
714 } else {
Evan Chengddf08202007-03-08 08:31:54 +0000715 if (I->isConstant() && TAI->getReadOnlySection())
716 SwitchToDataSection(TAI->getReadOnlySection(), I);
717 else
718 SwitchToDataSection(TAI->getDataSection(), I);
Jim Laskey28663c72006-12-21 20:26:09 +0000719 }
720 break;
721 default:
722 cerr << "Unknown linkage type!";
723 abort();
724 }
725
726 EmitAlignment(Align, I);
727 O << name << ":\t\t\t\t" << TAI->getCommentString() << " '"
728 << I->getName() << "'\n";
729
730 // If the initializer is a extern weak symbol, remember to emit the weak
731 // reference!
732 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
733 if (GV->hasExternalWeakLinkage())
734 ExtWeakSymbols.insert(GV);
735
736 EmitGlobalConstant(C);
737 O << '\n';
738 }
739 }
740
741 // TODO
742
743 // Emit initial debug information.
744 DW.EndModule();
745
Dan Gohmancf0a5342007-07-25 19:33:14 +0000746 return AsmPrinter::doFinalization(M);
Jim Laskey28663c72006-12-21 20:26:09 +0000747}
748
749std::string LinuxAsmPrinter::getSectionForFunction(const Function &F) const {
750 switch (F.getLinkage()) {
751 default: assert(0 && "Unknown linkage type!");
752 case Function::ExternalLinkage:
753 case Function::InternalLinkage: return TAI->getTextSection();
754 case Function::WeakLinkage:
755 case Function::LinkOnceLinkage:
756 return ".text";
757 }
758}
Chris Lattner028d6632006-10-05 02:42:20 +0000759
760std::string DarwinAsmPrinter::getSectionForFunction(const Function &F) const {
761 switch (F.getLinkage()) {
762 default: assert(0 && "Unknown linkage type!");
763 case Function::ExternalLinkage:
764 case Function::InternalLinkage: return TAI->getTextSection();
765 case Function::WeakLinkage:
766 case Function::LinkOnceLinkage:
767 return ".section __TEXT,__textcoal_nt,coalesced,pure_instructions";
768 }
769}
770
Nate Begeman4bfceb12004-09-04 05:00:00 +0000771/// runOnMachineFunction - This uses the printMachineInstruction()
772/// method to print assembly for each instruction.
773///
774bool DarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Jim Laskeyc56315c2007-01-26 21:22:28 +0000775 DW.SetModuleInfo(&getAnalysis<MachineModuleInfo>());
Jim Laskey762e9ec2006-01-05 01:25:28 +0000776
Chris Lattner99946fb2005-11-21 07:51:23 +0000777 SetupMachineFunction(MF);
Nate Begeman4bfceb12004-09-04 05:00:00 +0000778 O << "\n\n";
Jim Laskey7c462762005-12-16 22:45:29 +0000779
Nate Begeman4bfceb12004-09-04 05:00:00 +0000780 // Print out constants referenced by the function
Chris Lattneref83ebd2005-11-21 08:26:15 +0000781 EmitConstantPool(MF.getConstantPool());
Nate Begeman4bfceb12004-09-04 05:00:00 +0000782
783 // Print out labels for the function.
Chris Lattner0aacd2a2005-11-14 18:52:46 +0000784 const Function *F = MF.getFunction();
Chris Lattner028d6632006-10-05 02:42:20 +0000785 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Chris Lattner0d236452006-10-05 00:35:50 +0000786
Chris Lattner57575112005-12-16 00:22:14 +0000787 switch (F->getLinkage()) {
788 default: assert(0 && "Unknown linkage type!");
789 case Function::InternalLinkage: // Symbols default to internal.
Chris Lattner57575112005-12-16 00:22:14 +0000790 break;
791 case Function::ExternalLinkage:
Chris Lattner97d72c82005-10-28 18:44:07 +0000792 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner57575112005-12-16 00:22:14 +0000793 break;
794 case Function::WeakLinkage:
795 case Function::LinkOnceLinkage:
Chris Lattner57575112005-12-16 00:22:14 +0000796 O << "\t.globl\t" << CurrentFnName << "\n";
797 O << "\t.weak_definition\t" << CurrentFnName << "\n";
798 break;
799 }
Chris Lattner3c84b552007-01-14 06:37:54 +0000800
801 if (F->hasHiddenVisibility())
802 if (const char *Directive = TAI->getHiddenDirective())
803 O << Directive << CurrentFnName << "\n";
804
Chris Lattner84fb09e2006-02-14 20:42:33 +0000805 EmitAlignment(4, F);
Nate Begeman4bfceb12004-09-04 05:00:00 +0000806 O << CurrentFnName << ":\n";
807
Jim Laskeyc0d65182006-04-07 20:44:42 +0000808 // Emit pre-function debug information.
Jim Laskeya7b2bd52006-06-23 12:51:53 +0000809 DW.BeginFunction(&MF);
Jim Laskeyc0d65182006-04-07 20:44:42 +0000810
Nate Begeman4bfceb12004-09-04 05:00:00 +0000811 // Print out code for the function.
812 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
813 I != E; ++I) {
814 // Print a label for the basic block.
Chris Lattner968aeb12005-08-21 19:09:33 +0000815 if (I != MF.begin()) {
Nate Begemanb9d4f832006-05-02 05:37:32 +0000816 printBasicBlockLabel(I, true);
817 O << '\n';
Chris Lattner968aeb12005-08-21 19:09:33 +0000818 }
Nate Begeman4bfceb12004-09-04 05:00:00 +0000819 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
820 II != E; ++II) {
821 // Print the assembly for the instruction.
822 O << "\t";
823 printMachineInstruction(II);
824 }
825 }
Nate Begeman4bfceb12004-09-04 05:00:00 +0000826
Chris Lattner41e22a52006-10-05 00:26:05 +0000827 // Print out jump tables referenced by the function.
Chris Lattnera6a570e2006-10-05 03:01:21 +0000828 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Chris Lattner41e22a52006-10-05 00:26:05 +0000829
Jim Laskeyb0609d92006-01-04 13:52:30 +0000830 // Emit post-function debug information.
Jim Laskeycf0166f2006-03-23 18:09:44 +0000831 DW.EndFunction();
Chris Lattneraad26a12006-10-05 00:24:46 +0000832
Nate Begeman4bfceb12004-09-04 05:00:00 +0000833 // We didn't modify anything.
834 return false;
835}
836
Misha Brukmane05203f2004-06-21 16:55:25 +0000837
Nate Begeman15527112005-07-21 01:25:49 +0000838bool DarwinAsmPrinter::doInitialization(Module &M) {
Jim Laskey59e7a772006-12-12 20:57:08 +0000839 static const char *CPUDirectives[] = {
840 "ppc",
841 "ppc601",
842 "ppc602",
843 "ppc603",
844 "ppc7400",
845 "ppc750",
846 "ppc970",
847 "ppc64"
848 };
849
850 unsigned Directive = Subtarget.getDarwinDirective();
851 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
852 Directive = PPC::DIR_970;
853 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
854 Directive = PPC::DIR_7400;
855 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
856 Directive = PPC::DIR_64;
857 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
858 O << "\t.machine " << CPUDirectives[Directive] << "\n";
Jim Laskey7c3cab92006-12-12 16:07:33 +0000859
Dan Gohmancf0a5342007-07-25 19:33:14 +0000860 bool Result = AsmPrinter::doInitialization(M);
Chris Lattner9eb7dfa2005-11-10 19:33:43 +0000861
862 // Darwin wants symbols to be quoted if they have complex names.
863 Mang->setUseQuotes(true);
Jim Laskeyb0609d92006-01-04 13:52:30 +0000864
Jim Laskeyec05b042006-11-28 18:21:52 +0000865 // Prime text sections so they are adjacent. This reduces the likelihood a
866 // large data or debug section causes a branch to exceed 16M limit.
867 SwitchToTextSection(".section __TEXT,__textcoal_nt,coalesced,"
868 "pure_instructions");
869 if (TM.getRelocationModel() == Reloc::PIC_) {
870 SwitchToTextSection(".section __TEXT,__picsymbolstub1,symbol_stubs,"
871 "pure_instructions,32");
872 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
873 SwitchToTextSection(".section __TEXT,__symbol_stub1,symbol_stubs,"
874 "pure_instructions,16");
875 }
876 SwitchToTextSection(TAI->getTextSection());
877
Jim Laskeyb0609d92006-01-04 13:52:30 +0000878 // Emit initial debug information.
Jim Laskeycf0166f2006-03-23 18:09:44 +0000879 DW.BeginModule(&M);
Dan Gohmancf0a5342007-07-25 19:33:14 +0000880 return Result;
Nate Begeman15527112005-07-21 01:25:49 +0000881}
882
Nate Begeman4bfceb12004-09-04 05:00:00 +0000883bool DarwinAsmPrinter::doFinalization(Module &M) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000884 const TargetData *TD = TM.getTargetData();
Misha Brukmane05203f2004-06-21 16:55:25 +0000885
886 // Print out module-level global variables here.
Chris Lattner1a4adc72005-11-14 19:00:30 +0000887 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Chris Lattner54a11df2005-12-13 04:33:58 +0000888 I != E; ++I) {
889 if (!I->hasInitializer()) continue; // External global require no code
890
Chris Lattner87079882005-12-13 06:32:50 +0000891 // Check to see if this is a special global used by LLVM, if so, emit it.
Evan Cheng0e083d02007-01-30 08:04:53 +0000892 if (EmitSpecialLLVMGlobal(I)) {
893 if (TM.getRelocationModel() == Reloc::Static) {
894 if (I->getName() == "llvm.global_ctors")
895 O << ".reference .constructors_used\n";
896 else if (I->getName() == "llvm.global_dtors")
897 O << ".reference .destructors_used\n";
898 }
Chris Lattner87079882005-12-13 06:32:50 +0000899 continue;
Evan Cheng0e083d02007-01-30 08:04:53 +0000900 }
Chris Lattner54a11df2005-12-13 04:33:58 +0000901
Chris Lattner54a11df2005-12-13 04:33:58 +0000902 std::string name = Mang->getValueName(I);
Chris Lattner3c84b552007-01-14 06:37:54 +0000903
904 if (I->hasHiddenVisibility())
905 if (const char *Directive = TAI->getHiddenDirective())
906 O << Directive << name << "\n";
907
Chris Lattner54a11df2005-12-13 04:33:58 +0000908 Constant *C = I->getInitializer();
Evan Chengd9184772007-03-08 01:25:25 +0000909 const Type *Type = C->getType();
910 unsigned Size = TD->getTypeSize(Type);
Devang Patel71b99292006-10-24 20:32:14 +0000911 unsigned Align = TD->getPreferredAlignmentLog(I);
Chris Lattner54a11df2005-12-13 04:33:58 +0000912
913 if (C->isNullValue() && /* FIXME: Verify correct */
Devang Pateld29f9382007-09-20 23:07:37 +0000914 !I->hasSection() &&
Chris Lattner54a11df2005-12-13 04:33:58 +0000915 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
Devang Pateld29f9382007-09-20 23:07:37 +0000916 I->hasLinkOnceLinkage() || I->hasExternalLinkage())) {
Chris Lattner54a11df2005-12-13 04:33:58 +0000917 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Chris Lattnerb1345202006-02-14 22:18:23 +0000918 if (I->hasExternalLinkage()) {
919 O << "\t.globl " << name << '\n';
920 O << "\t.zerofill __DATA, __common, " << name << ", "
921 << Size << ", " << Align;
922 } else if (I->hasInternalLinkage()) {
Chris Lattner28141342006-05-09 16:15:00 +0000923 SwitchToDataSection("\t.data", I);
Jim Laskeya6211dc2006-09-06 18:34:40 +0000924 O << TAI->getLCOMMDirective() << name << "," << Size << "," << Align;
Chris Lattnerb1345202006-02-14 22:18:23 +0000925 } else {
Chris Lattner28141342006-05-09 16:15:00 +0000926 SwitchToDataSection("\t.data", I);
Chris Lattner54a11df2005-12-13 04:33:58 +0000927 O << ".comm " << name << "," << Size;
Chris Lattnerb1345202006-02-14 22:18:23 +0000928 }
Jim Laskey41621a72006-12-20 20:56:46 +0000929 O << "\t\t" << TAI->getCommentString() << " '" << I->getName() << "'\n";
Chris Lattner54a11df2005-12-13 04:33:58 +0000930 } else {
931 switch (I->getLinkage()) {
Jim Laskey311622f2006-12-01 14:37:39 +0000932 case GlobalValue::LinkOnceLinkage:
Chris Lattner54a11df2005-12-13 04:33:58 +0000933 case GlobalValue::WeakLinkage:
Chris Lattner6b0325a2005-12-22 21:15:17 +0000934 O << "\t.globl " << name << '\n'
935 << "\t.weak_definition " << name << '\n';
Chris Lattner8488ba22006-05-09 04:59:56 +0000936 SwitchToDataSection(".section __DATA,__datacoal_nt,coalesced", I);
Chris Lattner54a11df2005-12-13 04:33:58 +0000937 break;
938 case GlobalValue::AppendingLinkage:
939 // FIXME: appending linkage variables should go into a section of
940 // their name or something. For now, just emit them as external.
941 case GlobalValue::ExternalLinkage:
942 // If external or appending, declare as a global symbol
943 O << "\t.globl " << name << "\n";
944 // FALL THROUGH
945 case GlobalValue::InternalLinkage:
Evan Chengae8c29a2006-10-28 05:56:51 +0000946 if (I->isConstant()) {
Evan Chenge1e06c22006-10-26 21:48:57 +0000947 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
Evan Chengae8c29a2006-10-28 05:56:51 +0000948 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
Evan Chenge1e06c22006-10-26 21:48:57 +0000949 SwitchToDataSection(TAI->getCStringSection(), I);
950 break;
951 }
952 }
953
Evan Chengd9184772007-03-08 01:25:25 +0000954 if (!I->isConstant())
955 SwitchToDataSection(TAI->getDataSection(), I);
956 else {
957 // Read-only data.
Evan Chengddf08202007-03-08 08:31:54 +0000958 bool HasReloc = C->ContainsRelocations();
959 if (HasReloc &&
Evan Chengd9184772007-03-08 01:25:25 +0000960 TM.getRelocationModel() != Reloc::Static)
961 SwitchToDataSection("\t.const_data\n");
Evan Chengddf08202007-03-08 08:31:54 +0000962 else if (!HasReloc && Size == 4 &&
Evan Chengd9184772007-03-08 01:25:25 +0000963 TAI->getFourByteConstantSection())
964 SwitchToDataSection(TAI->getFourByteConstantSection(), I);
Evan Chengddf08202007-03-08 08:31:54 +0000965 else if (!HasReloc && Size == 8 &&
Evan Chengd9184772007-03-08 01:25:25 +0000966 TAI->getEightByteConstantSection())
967 SwitchToDataSection(TAI->getEightByteConstantSection(), I);
Evan Chengddf08202007-03-08 08:31:54 +0000968 else if (!HasReloc && Size == 16 &&
Evan Chengd9184772007-03-08 01:25:25 +0000969 TAI->getSixteenByteConstantSection())
970 SwitchToDataSection(TAI->getSixteenByteConstantSection(), I);
971 else if (TAI->getReadOnlySection())
972 SwitchToDataSection(TAI->getReadOnlySection(), I);
973 else
974 SwitchToDataSection(TAI->getDataSection(), I);
975 }
Chris Lattner54a11df2005-12-13 04:33:58 +0000976 break;
977 default:
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000978 cerr << "Unknown linkage type!";
Chris Lattner54a11df2005-12-13 04:33:58 +0000979 abort();
980 }
981
982 EmitAlignment(Align, I);
Jim Laskey41621a72006-12-20 20:56:46 +0000983 O << name << ":\t\t\t\t" << TAI->getCommentString() << " '"
984 << I->getName() << "'\n";
Evan Cheng5fb2c762006-12-01 09:13:26 +0000985
986 // If the initializer is a extern weak symbol, remember to emit the weak
987 // reference!
988 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
989 if (GV->hasExternalWeakLinkage())
Rafael Espindolad7998d02006-12-18 03:37:18 +0000990 ExtWeakSymbols.insert(GV);
Evan Cheng5fb2c762006-12-01 09:13:26 +0000991
Chris Lattner54a11df2005-12-13 04:33:58 +0000992 EmitGlobalConstant(C);
Chris Lattner9436aa72006-01-21 01:35:26 +0000993 O << '\n';
Chris Lattner54a11df2005-12-13 04:33:58 +0000994 }
995 }
Misha Brukmand4ac8182004-07-16 20:29:04 +0000996
Chris Lattner1df08392006-06-27 01:02:25 +0000997 bool isPPC64 = TD->getPointerSizeInBits() == 64;
998
Misha Brukmand4ac8182004-07-16 20:29:04 +0000999 // Output stubs for dynamically-linked functions
Chris Lattner9e56e5c2006-07-26 21:12:04 +00001000 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner54a11df2005-12-13 04:33:58 +00001001 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
1002 i != e; ++i) {
Chris Lattner8488ba22006-05-09 04:59:56 +00001003 SwitchToTextSection(".section __TEXT,__picsymbolstub1,symbol_stubs,"
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +00001004 "pure_instructions,32");
Chris Lattner1df08392006-06-27 01:02:25 +00001005 EmitAlignment(4);
Chris Lattner54a11df2005-12-13 04:33:58 +00001006 O << "L" << *i << "$stub:\n";
1007 O << "\t.indirect_symbol " << *i << "\n";
1008 O << "\tmflr r0\n";
1009 O << "\tbcl 20,31,L0$" << *i << "\n";
1010 O << "L0$" << *i << ":\n";
1011 O << "\tmflr r11\n";
1012 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
1013 O << "\tmtlr r0\n";
Chris Lattner1df08392006-06-27 01:02:25 +00001014 if (isPPC64)
1015 O << "\tldu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
1016 else
1017 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
Chris Lattner54a11df2005-12-13 04:33:58 +00001018 O << "\tmtctr r12\n";
1019 O << "\tbctr\n";
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +00001020 SwitchToDataSection(".lazy_symbol_pointer");
Chris Lattner54a11df2005-12-13 04:33:58 +00001021 O << "L" << *i << "$lazy_ptr:\n";
1022 O << "\t.indirect_symbol " << *i << "\n";
Chris Lattner1df08392006-06-27 01:02:25 +00001023 if (isPPC64)
1024 O << "\t.quad dyld_stub_binding_helper\n";
1025 else
1026 O << "\t.long dyld_stub_binding_helper\n";
Chris Lattner54a11df2005-12-13 04:33:58 +00001027 }
1028 } else {
1029 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
1030 i != e; ++i) {
Chris Lattner8488ba22006-05-09 04:59:56 +00001031 SwitchToTextSection(".section __TEXT,__symbol_stub1,symbol_stubs,"
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +00001032 "pure_instructions,16");
Chris Lattner54a11df2005-12-13 04:33:58 +00001033 EmitAlignment(4);
1034 O << "L" << *i << "$stub:\n";
1035 O << "\t.indirect_symbol " << *i << "\n";
1036 O << "\tlis r11,ha16(L" << *i << "$lazy_ptr)\n";
Chris Lattner1df08392006-06-27 01:02:25 +00001037 if (isPPC64)
1038 O << "\tldu r12,lo16(L" << *i << "$lazy_ptr)(r11)\n";
1039 else
1040 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr)(r11)\n";
Chris Lattner54a11df2005-12-13 04:33:58 +00001041 O << "\tmtctr r12\n";
1042 O << "\tbctr\n";
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +00001043 SwitchToDataSection(".lazy_symbol_pointer");
Chris Lattner54a11df2005-12-13 04:33:58 +00001044 O << "L" << *i << "$lazy_ptr:\n";
1045 O << "\t.indirect_symbol " << *i << "\n";
Chris Lattner1df08392006-06-27 01:02:25 +00001046 if (isPPC64)
1047 O << "\t.quad dyld_stub_binding_helper\n";
1048 else
1049 O << "\t.long dyld_stub_binding_helper\n";
Nate Begemana9443f22005-07-21 20:44:43 +00001050 }
Misha Brukmanf62ee7a2004-06-24 23:04:11 +00001051 }
Misha Brukmane05203f2004-06-21 16:55:25 +00001052
Misha Brukmand4ac8182004-07-16 20:29:04 +00001053 O << "\n";
1054
Chris Lattner57575112005-12-16 00:22:14 +00001055 // Output stubs for external and common global variables.
Dan Gohmanc731c972007-10-03 19:26:29 +00001056 if (!GVStubs.empty()) {
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +00001057 SwitchToDataSection(".non_lazy_symbol_pointer");
Chris Lattner57575112005-12-16 00:22:14 +00001058 for (std::set<std::string>::iterator I = GVStubs.begin(),
1059 E = GVStubs.end(); I != E; ++I) {
1060 O << "L" << *I << "$non_lazy_ptr:\n";
1061 O << "\t.indirect_symbol " << *I << "\n";
Chris Lattner82ab3e22006-06-27 20:20:53 +00001062 if (isPPC64)
1063 O << "\t.quad\t0\n";
1064 else
1065 O << "\t.long\t0\n";
1066
Chris Lattner54a11df2005-12-13 04:33:58 +00001067 }
Nate Begeman0ad7f812004-08-14 22:09:10 +00001068 }
Misha Brukmanb4402432005-04-21 23:30:14 +00001069
Jim Laskeyb9966022006-01-17 17:31:53 +00001070 // Emit initial debug information.
Jim Laskeycf0166f2006-03-23 18:09:44 +00001071 DW.EndModule();
Jim Laskeyb9966022006-01-17 17:31:53 +00001072
Chris Lattner7432ceef2005-11-01 00:12:36 +00001073 // Funny Darwin hack: This flag tells the linker that no global symbols
1074 // contain code that falls through to other global symbols (e.g. the obvious
1075 // implementation of multiple entry points). If this doesn't occur, the
1076 // linker can safely perform dead code stripping. Since LLVM never generates
1077 // code that does this, it is always safe to set.
Chris Lattnera81a75c2006-09-20 17:12:19 +00001078 O << "\t.subsections_via_symbols\n";
Chris Lattner7432ceef2005-11-01 00:12:36 +00001079
Dan Gohmancf0a5342007-07-25 19:33:14 +00001080 return AsmPrinter::doFinalization(M);
Misha Brukmane05203f2004-06-21 16:55:25 +00001081}
Nate Begeman4bfceb12004-09-04 05:00:00 +00001082
Chris Lattnera81a75c2006-09-20 17:12:19 +00001083
1084
Jim Laskey41621a72006-12-20 20:56:46 +00001085/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1086/// for a MachineFunction to the given output stream, in a format that the
Chris Lattnera81a75c2006-09-20 17:12:19 +00001087/// Darwin assembler can deal with.
1088///
1089FunctionPass *llvm::createPPCAsmPrinterPass(std::ostream &o,
1090 PPCTargetMachine &tm) {
Jim Laskey28663c72006-12-21 20:26:09 +00001091 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1092
1093 if (Subtarget->isDarwin()) {
1094 return new DarwinAsmPrinter(o, tm, tm.getTargetAsmInfo());
1095 } else {
1096 return new LinuxAsmPrinter(o, tm, tm.getTargetAsmInfo());
1097 }
Chris Lattnera81a75c2006-09-20 17:12:19 +00001098}
1099