blob: 3c6b7fff4249f8506ded338ac0b862bc0a69db94 [file] [log] [blame]
Nate Begeman21e463b2005-10-16 05:39:50 +00001//===-- PPCAsmPrinter.cpp - Print machine instrs to PowerPC assembly --------=//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002//
Misha Brukman5dfe3a92004-06-21 16:55:25 +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//
Misha Brukman5dfe3a92004-06-21 16:55:25 +00008//===----------------------------------------------------------------------===//
9//
Misha Brukman05fcd0c2004-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 Lattner83660c52004-07-28 20:18:53 +000012// the output mechanism used by `llc'.
Misha Brukman5dfe3a92004-06-21 16:55:25 +000013//
Misha Brukman05fcd0c2004-07-08 17:58:04 +000014// Documentation at http://developer.apple.com/documentation/DeveloperTools/
15// Reference/Assembler/ASMIntroduction/chapter_1_section_1.html
Misha Brukman218bec72004-06-29 17:13:26 +000016//
Misha Brukman5dfe3a92004-06-21 16:55:25 +000017//===----------------------------------------------------------------------===//
18
Misha Brukman05794492004-06-24 17:31:42 +000019#define DEBUG_TYPE "asmprinter"
Chris Lattner26689592005-10-14 23:51:18 +000020#include "PPC.h"
Chris Lattnerdf4ed632006-11-17 22:10:59 +000021#include "PPCPredicates.h"
Chris Lattner16e71f22005-10-14 23:59:06 +000022#include "PPCTargetMachine.h"
Chris Lattner26689592005-10-14 23:51:18 +000023#include "PPCSubtarget.h"
Misha Brukman5dfe3a92004-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 Lattnera3840792004-08-16 23:25:21 +000028#include "llvm/CodeGen/AsmPrinter.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000029#include "llvm/CodeGen/DwarfWriter.h"
Jim Laskey44c3b9f2007-01-26 21:22:28 +000030#include "llvm/CodeGen/MachineModuleInfo.h"
Misha Brukman05794492004-06-24 17:31:42 +000031#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000032#include "llvm/CodeGen/MachineInstr.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000033#include "llvm/Support/Mangler.h"
Nate Begemana11c2e82004-09-04 14:51:26 +000034#include "llvm/Support/MathExtras.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000035#include "llvm/Support/CommandLine.h"
36#include "llvm/Support/Debug.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000037#include "llvm/Support/Compiler.h"
Jim Laskey563321a2006-09-06 18:34:40 +000038#include "llvm/Target/TargetAsmInfo.h"
Nate Begeman17304c32004-10-26 06:02:38 +000039#include "llvm/Target/MRegisterInfo.h"
Nate Begemand4c8bea2004-11-25 07:09:01 +000040#include "llvm/Target/TargetInstrInfo.h"
Evan Chengd2ee2182006-02-18 00:08:58 +000041#include "llvm/Target/TargetOptions.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000042#include "llvm/ADT/Statistic.h"
43#include "llvm/ADT/StringExtras.h"
Misha Brukman05794492004-06-24 17:31:42 +000044#include <set>
Chris Lattnera3840792004-08-16 23:25:21 +000045using namespace llvm;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000046
Chris Lattner95b2c7d2006-12-19 22:59:26 +000047STATISTIC(EmittedInsts, "Number of machine instrs printed");
Misha Brukman5dfe3a92004-06-21 16:55:25 +000048
Chris Lattner95b2c7d2006-12-19 22:59:26 +000049namespace {
Jim Laskey563321a2006-09-06 18:34:40 +000050 struct VISIBILITY_HIDDEN PPCAsmPrinter : public AsmPrinter {
Chris Lattnera637c322005-12-16 00:22:14 +000051 std::set<std::string> FnStubs, GVStubs;
Chris Lattnerdadceed2006-09-20 17:07:15 +000052 const PPCSubtarget &Subtarget;
Evan Cheng1c45a662006-12-01 07:56:37 +000053
Jim Laskeya0f3d172006-09-07 22:06:40 +000054 PPCAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
Chris Lattnerdadceed2006-09-20 17:07:15 +000055 : AsmPrinter(O, TM, T), Subtarget(TM.getSubtarget<PPCSubtarget>()) {
56 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +000057
Misha Brukman5dfe3a92004-06-21 16:55:25 +000058 virtual const char *getPassName() const {
Nate Begemaned428532004-09-04 05:00:00 +000059 return "PowerPC Assembly Printer";
Misha Brukman5dfe3a92004-06-21 16:55:25 +000060 }
61
Nate Begeman21e463b2005-10-16 05:39:50 +000062 PPCTargetMachine &getTM() {
63 return static_cast<PPCTargetMachine&>(TM);
Chris Lattnera3840792004-08-16 23:25:21 +000064 }
65
Nate Begemanadeb43d2005-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 Begemane59bf592004-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 Brukman5dfe3a92004-06-21 16:55:25 +000087 void printMachineInstruction(const MachineInstr *MI);
Chris Lattner9ba13e42005-11-17 19:25:59 +000088 void printOp(const MachineOperand &MO);
Jim Laskeyb608a4d2006-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 Laskey40ee69f2006-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 Laskey2aa14aa2006-12-20 21:35:00 +000097 case 'c': if (RegName[1] == 'r') return RegName + 2;
Jim Laskeyb608a4d2006-12-20 20:56:46 +000098 }
Jim Laskey40ee69f2006-12-20 21:33:34 +000099
100 return RegName;
Jim Laskeyb608a4d2006-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 Laskey40ee69f2006-12-20 21:33:34 +0000115 const char *RegName = TM.getRegisterInfo()->get(RegNo).Name;
Jim Laskeyb608a4d2006-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 Laskey40ee69f2006-12-20 21:33:34 +0000118 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
119 O << RegName;
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000120 }
Chris Lattner7bb424f2004-08-14 23:27:29 +0000121
Chris Lattner58873272006-02-01 22:38:46 +0000122 void printOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner7bb424f2004-08-14 23:27:29 +0000123 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattner2d90ac72006-05-04 18:05:43 +0000124 if (MO.isRegister()) {
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000125 printRegister(MO, false);
Chris Lattner0ea31712004-08-15 05:46:14 +0000126 } else if (MO.isImmediate()) {
127 O << MO.getImmedValue();
Chris Lattner7bb424f2004-08-14 23:27:29 +0000128 } else {
129 printOp(MO);
130 }
131 }
Chris Lattner58873272006-02-01 22:38:46 +0000132
133 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnere3f01572006-02-23 19:31:10 +0000134 unsigned AsmVariant, const char *ExtraCode);
Chris Lattner2c003e22006-02-24 20:27:40 +0000135 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
136 unsigned AsmVariant, const char *ExtraCode);
137
Chris Lattner58873272006-02-01 22:38:46 +0000138
Chris Lattner9c61dcf2006-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 Begeman391c5d22005-11-30 18:54:35 +0000144 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begemanc3306122004-08-21 05:56:39 +0000145 unsigned char value = MI->getOperand(OpNo).getImmedValue();
Chris Lattner12585ba2004-08-21 19:11:03 +0000146 assert(value <= 31 && "Invalid u5imm argument!");
Nate Begemanc3306122004-08-21 05:56:39 +0000147 O << (unsigned int)value;
148 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000149 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman07aada82004-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 Begeman391c5d22005-11-30 18:54:35 +0000154 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begemaned428532004-09-04 05:00:00 +0000155 O << (short)MI->getOperand(OpNo).getImmedValue();
156 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000157 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner97b2a2e2004-08-15 05:20:16 +0000158 O << (unsigned short)MI->getOperand(OpNo).getImmedValue();
159 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000160 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner1b0a2d82006-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 Cheng347d39f2007-10-14 05:57:21 +0000167 O << "-\"L" << getFunctionNumber() << "$pb\")";
Chris Lattner1b0a2d82006-11-16 21:45:30 +0000168 else
169 O << ')';
170 }
Chris Lattner841d12d2005-10-18 16:51:22 +0000171 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000172 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begemaned428532004-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 Chengb9ca9262006-08-25 21:54:44 +0000176 O << "$+" << MI->getOperand(OpNo).getImmedValue()*4;
Nate Begemaned428532004-09-04 05:00:00 +0000177 } else {
Chris Lattner3e7f86a2005-11-17 19:16:08 +0000178 printOp(MI->getOperand(OpNo));
Nate Begemaned428532004-09-04 05:00:00 +0000179 }
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000180 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000181 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9ba13e42005-11-17 19:25:59 +0000182 const MachineOperand &MO = MI->getOperand(OpNo);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000183 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattnera637c322005-12-16 00:22:14 +0000184 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
185 GlobalValue *GV = MO.getGlobal();
Reid Spencer5cbf9852007-01-30 20:08:39 +0000186 if (((GV->isDeclaration() || GV->hasWeakLinkage() ||
Chris Lattnera637c322005-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 Cheng1c45a662006-12-01 07:56:37 +0000192 if (GV->hasExternalWeakLinkage())
Rafael Espindola15404d02006-12-18 03:37:18 +0000193 ExtWeakSymbols.insert(GV);
Chris Lattnera637c322005-12-16 00:22:14 +0000194 return;
195 }
196 }
Chris Lattner9542f9712005-11-17 19:40:30 +0000197 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
Jim Laskey563321a2006-09-06 18:34:40 +0000198 std::string Name(TAI->getGlobalPrefix()); Name += MO.getSymbolName();
Chris Lattner9542f9712005-11-17 19:40:30 +0000199 FnStubs.insert(Name);
200 O << "L" << Name << "$stub";
201 return;
Chris Lattner9542f9712005-11-17 19:40:30 +0000202 }
Chris Lattner9ba13e42005-11-17 19:25:59 +0000203 }
Chris Lattner9542f9712005-11-17 19:40:30 +0000204
205 printOp(MI->getOperand(OpNo));
Chris Lattner3e7f86a2005-11-17 19:16:08 +0000206 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000207 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman422b0ce2005-11-16 00:48:01 +0000208 O << (int)MI->getOperand(OpNo).getImmedValue()*4;
209 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000210 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Evan Cheng347d39f2007-10-14 05:57:21 +0000211 O << "\"L" << getFunctionNumber() << "$pb\"\n";
212 O << "\"L" << getFunctionNumber() << "$pb\":";
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000213 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000214 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman2497e632005-07-21 20:44:43 +0000215 if (MI->getOperand(OpNo).isImmediate()) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000216 printS16ImmOperand(MI, OpNo);
Nate Begeman2497e632005-07-21 20:44:43 +0000217 } else {
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000218 if (Subtarget.isDarwin()) O << "ha16(";
Nate Begeman2497e632005-07-21 20:44:43 +0000219 printOp(MI->getOperand(OpNo));
Chris Lattner35d86fe2006-07-26 21:12:04 +0000220 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng347d39f2007-10-14 05:57:21 +0000221 O << "-\"L" << getFunctionNumber() << "$pb\"";
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000222 if (Subtarget.isDarwin())
Nate Begeman2497e632005-07-21 20:44:43 +0000223 O << ')';
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000224 else
225 O << "@ha";
Nate Begeman2497e632005-07-21 20:44:43 +0000226 }
Nate Begemaned428532004-09-04 05:00:00 +0000227 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000228 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Nate Begemaned428532004-09-04 05:00:00 +0000229 if (MI->getOperand(OpNo).isImmediate()) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000230 printS16ImmOperand(MI, OpNo);
Nate Begemaned428532004-09-04 05:00:00 +0000231 } else {
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000232 if (Subtarget.isDarwin()) O << "lo16(";
Nate Begemand4c8bea2004-11-25 07:09:01 +0000233 printOp(MI->getOperand(OpNo));
Chris Lattner35d86fe2006-07-26 21:12:04 +0000234 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng347d39f2007-10-14 05:57:21 +0000235 O << "-\"L" << getFunctionNumber() << "$pb\"";
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000236 if (Subtarget.isDarwin())
Nate Begeman2497e632005-07-21 20:44:43 +0000237 O << ')';
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000238 else
239 O << "@l";
Nate Begemaned428532004-09-04 05:00:00 +0000240 }
241 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000242 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
Nate Begemanadeb43d2005-07-20 22:42:00 +0000243 unsigned CCReg = MI->getOperand(OpNo).getReg();
244 unsigned RegNo = enumRegToMachineReg(CCReg);
245 O << (0x80 >> RegNo);
246 }
Chris Lattner2c003e22006-02-24 20:27:40 +0000247 // The new addressing mode printers.
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000248 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
249 printSymbolLo(MI, OpNo);
250 O << '(';
Chris Lattner13feb582006-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 Begeman7fd1edd2005-12-19 23:25:09 +0000256 O << ')';
257 }
Chris Lattnere5ba5802006-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 Begeman7fd1edd2005-12-19 23:25:09 +0000272 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman88276b82005-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 Laskeyb608a4d2006-12-20 20:56:46 +0000277 printRegister(MO, true);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000278 O << ", ";
279 printOperand(MI, OpNo+1);
280 }
281
Chris Lattneraf53a872006-11-04 05:27:39 +0000282 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
283 const char *Modifier);
284
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000285 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
Nate Begemaned428532004-09-04 05:00:00 +0000286 virtual bool doFinalization(Module &M) = 0;
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000287
288 virtual void EmitExternalGlobal(const GlobalVariable *GV);
Nate Begemaned428532004-09-04 05:00:00 +0000289 };
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000290
Jim Laskeybf111822006-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 Laskey44c3b9f2007-01-26 21:22:28 +0000311 AU.addRequired<MachineModuleInfo>();
Jim Laskeybf111822006-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 Brukman3e0b51a2004-09-05 02:42:44 +0000320 /// DarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac OS
321 /// X
Chris Lattner95255282006-06-28 23:17:24 +0000322 struct VISIBILITY_HIDDEN DarwinAsmPrinter : public PPCAsmPrinter {
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000323
Jim Laskey563321a2006-09-06 18:34:40 +0000324 DwarfWriter DW;
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +0000325 MachineModuleInfo *MMI;
Nate Begemaned428532004-09-04 05:00:00 +0000326
Jim Laskeya0f3d172006-09-07 22:06:40 +0000327 DarwinAsmPrinter(std::ostream &O, PPCTargetMachine &TM,
328 const TargetAsmInfo *T)
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +0000329 : PPCAsmPrinter(O, TM, T), DW(O, this, T), MMI(0) {
Nate Begemaned428532004-09-04 05:00:00 +0000330 }
331
332 virtual const char *getPassName() const {
333 return "Darwin PPC Assembly Printer";
334 }
Chris Lattner646f7af2005-12-09 18:24:29 +0000335
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000336 bool runOnMachineFunction(MachineFunction &F);
Nate Begeman18ed0292005-07-21 01:25:49 +0000337 bool doInitialization(Module &M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000338 bool doFinalization(Module &M);
Jim Laskeyb2efb852006-01-04 22:28:25 +0000339
340 void getAnalysisUsage(AnalysisUsage &AU) const {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000341 AU.setPreservesAll();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000342 AU.addRequired<MachineModuleInfo>();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000343 PPCAsmPrinter::getAnalysisUsage(AU);
Jim Laskeyb2efb852006-01-04 22:28:25 +0000344 }
345
Chris Lattner9b7ce7d2006-10-05 02:42:20 +0000346 /// getSectionForFunction - Return the section that we should emit the
347 /// specified function body into.
348 virtual std::string getSectionForFunction(const Function &F) const;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000349 };
350} // end of anonymous namespace
351
Nate Begemane59bf592004-08-14 22:09:10 +0000352// Include the auto-generated portion of the assembly writer
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000353#include "PPCGenAsmWriter.inc"
Nate Begemane59bf592004-08-14 22:09:10 +0000354
Chris Lattner9ba13e42005-11-17 19:25:59 +0000355void PPCAsmPrinter::printOp(const MachineOperand &MO) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000356 switch (MO.getType()) {
Chris Lattner63b3d712006-05-04 17:21:20 +0000357 case MachineOperand::MO_Immediate:
Bill Wendlingf5da1332006-12-07 22:21:48 +0000358 cerr << "printOp() does not handle immediate values\n";
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000359 abort();
Misha Brukman97a296f2004-07-21 20:11:11 +0000360 return;
361
Nate Begeman37efe672006-04-22 18:53:45 +0000362 case MachineOperand::MO_MachineBasicBlock:
363 printBasicBlockLabel(MO.getMachineBasicBlock());
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000364 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000365 case MachineOperand::MO_JumpTableIndex:
Evan Cheng347d39f2007-10-14 05:57:21 +0000366 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
367 << '_' << MO.getJumpTableIndex();
Nate Begeman37efe672006-04-22 18:53:45 +0000368 // FIXME: PIC relocation model
369 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000370 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng347d39f2007-10-14 05:57:21 +0000371 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
372 << '_' << MO.getConstantPoolIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000373 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000374 case MachineOperand::MO_ExternalSymbol:
Chris Lattnera637c322005-12-16 00:22:14 +0000375 // Computing the address of an external symbol, not calling it.
Evan Cheng4c1aa862006-02-22 20:19:42 +0000376 if (TM.getRelocationModel() != Reloc::Static) {
Jim Laskey563321a2006-09-06 18:34:40 +0000377 std::string Name(TAI->getGlobalPrefix()); Name += MO.getSymbolName();
Chris Lattnera637c322005-12-16 00:22:14 +0000378 GVStubs.insert(Name);
379 O << "L" << Name << "$non_lazy_ptr";
380 return;
381 }
Jim Laskey563321a2006-09-06 18:34:40 +0000382 O << TAI->getGlobalPrefix() << MO.getSymbolName();
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000383 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000384 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera637c322005-12-16 00:22:14 +0000385 // Computing the address of a global symbol, not calling it.
Nate Begemanb73a7112004-08-13 09:32:01 +0000386 GlobalValue *GV = MO.getGlobal();
387 std::string Name = Mang->getValueName(GV);
Misha Brukmane2eceb52004-07-23 16:08:20 +0000388
Nate Begemanfcf4a422004-10-17 23:01:34 +0000389 // External or weakly linked global variables need non-lazily-resolved stubs
Evan Cheng4c1aa862006-02-22 20:19:42 +0000390 if (TM.getRelocationModel() != Reloc::Static) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000391 if (((GV->isDeclaration() || GV->hasWeakLinkage() ||
Chris Lattnera637c322005-12-16 00:22:14 +0000392 GV->hasLinkOnceLinkage()))) {
Nate Begemand4c8bea2004-11-25 07:09:01 +0000393 GVStubs.insert(Name);
Chris Lattnera637c322005-12-16 00:22:14 +0000394 O << "L" << Name << "$non_lazy_ptr";
395 return;
396 }
Nate Begemanb73a7112004-08-13 09:32:01 +0000397 }
Chris Lattner9542f9712005-11-17 19:40:30 +0000398 O << Name;
Evan Cheng1c45a662006-12-01 07:56:37 +0000399
Chris Lattner4105a9f2007-05-03 16:39:48 +0000400 if (MO.getOffset() > 0)
401 O << "+" << MO.getOffset();
402 else if (MO.getOffset() < 0)
403 O << MO.getOffset();
404
Evan Cheng1c45a662006-12-01 07:56:37 +0000405 if (GV->hasExternalWeakLinkage())
Rafael Espindola15404d02006-12-18 03:37:18 +0000406 ExtWeakSymbols.insert(GV);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000407 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000408 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000409
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000410 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000411 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000412 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000413 }
414}
415
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000416/// EmitExternalGlobal - In this case we need to use the indirect symbol.
417///
418void PPCAsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
419 std::string Name = getGlobalLinkName(GV);
420 if (TM.getRelocationModel() != Reloc::Static) {
421 GVStubs.insert(Name);
422 O << "L" << Name << "$non_lazy_ptr";
423 return;
424 }
425 O << Name;
426}
427
Chris Lattnere3f01572006-02-23 19:31:10 +0000428/// PrintAsmOperand - Print out an operand for an inline asm expression.
429///
430bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
431 unsigned AsmVariant,
432 const char *ExtraCode) {
433 // Does this asm operand have a single letter operand modifier?
434 if (ExtraCode && ExtraCode[0]) {
435 if (ExtraCode[1] != 0) return true; // Unknown modifier.
436
437 switch (ExtraCode[0]) {
438 default: return true; // Unknown modifier.
Chris Lattner78192b62007-01-25 02:52:50 +0000439 case 'c': // Don't print "$" before a global var name or constant.
440 // PPC never has a prefix.
441 printOperand(MI, OpNo);
442 return false;
Chris Lattnere3f01572006-02-23 19:31:10 +0000443 case 'L': // Write second word of DImode reference.
444 // Verify that this operand has two consecutive registers.
445 if (!MI->getOperand(OpNo).isRegister() ||
446 OpNo+1 == MI->getNumOperands() ||
447 !MI->getOperand(OpNo+1).isRegister())
448 return true;
449 ++OpNo; // Return the high-part.
450 break;
Chris Lattner6c2d2602007-04-24 22:51:03 +0000451 case 'I':
452 // Write 'i' if an integer constant, otherwise nothing. Used to print
453 // addi vs add, etc.
Dan Gohman92dfe202007-09-14 20:33:02 +0000454 if (MI->getOperand(OpNo).isImmediate())
Chris Lattner6c2d2602007-04-24 22:51:03 +0000455 O << "i";
456 return false;
Chris Lattnere3f01572006-02-23 19:31:10 +0000457 }
458 }
459
460 printOperand(MI, OpNo);
461 return false;
462}
463
Chris Lattner2c003e22006-02-24 20:27:40 +0000464bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
465 unsigned AsmVariant,
466 const char *ExtraCode) {
467 if (ExtraCode && ExtraCode[0])
468 return true; // Unknown modifier.
Chris Lattner9aa28952007-02-01 00:39:08 +0000469 if (MI->getOperand(OpNo).isRegister())
470 printMemRegReg(MI, OpNo);
471 else
472 printMemRegImm(MI, OpNo);
Chris Lattner2c003e22006-02-24 20:27:40 +0000473 return false;
474}
Chris Lattnere3f01572006-02-23 19:31:10 +0000475
Chris Lattneraf53a872006-11-04 05:27:39 +0000476void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
477 const char *Modifier) {
478 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
479 unsigned Code = MI->getOperand(OpNo).getImm();
480 if (!strcmp(Modifier, "cc")) {
481 switch ((PPC::Predicate)Code) {
482 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
483 case PPC::PRED_LT: O << "lt"; return;
484 case PPC::PRED_LE: O << "le"; return;
485 case PPC::PRED_EQ: O << "eq"; return;
486 case PPC::PRED_GE: O << "ge"; return;
487 case PPC::PRED_GT: O << "gt"; return;
488 case PPC::PRED_NE: O << "ne"; return;
489 case PPC::PRED_UN: O << "un"; return;
490 case PPC::PRED_NU: O << "nu"; return;
491 }
492
493 } else {
494 assert(!strcmp(Modifier, "reg") &&
495 "Need to specify 'cc' or 'reg' as predicate op modifier!");
496 // Don't print the register for 'always'.
497 if (Code == PPC::PRED_ALWAYS) return;
498 printOperand(MI, OpNo+1);
499 }
500}
501
502
Nate Begemane59bf592004-08-14 22:09:10 +0000503/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
504/// the current output stream.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000505///
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000506void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Nate Begemane59bf592004-08-14 22:09:10 +0000507 ++EmittedInsts;
Chris Lattner617742b2005-10-14 22:44:13 +0000508
Nate Begemanad5f65c2005-04-05 18:19:50 +0000509 // Check for slwi/srwi mnemonics.
510 if (MI->getOpcode() == PPC::RLWINM) {
511 bool FoundMnemonic = false;
512 unsigned char SH = MI->getOperand(2).getImmedValue();
513 unsigned char MB = MI->getOperand(3).getImmedValue();
514 unsigned char ME = MI->getOperand(4).getImmedValue();
515 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
516 O << "slwi "; FoundMnemonic = true;
517 }
518 if (SH <= 31 && MB == (32-SH) && ME == 31) {
519 O << "srwi "; FoundMnemonic = true;
520 SH = 32-SH;
521 }
522 if (FoundMnemonic) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000523 printOperand(MI, 0);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000524 O << ", ";
Nate Begeman391c5d22005-11-30 18:54:35 +0000525 printOperand(MI, 1);
Nate Begemanad5f65c2005-04-05 18:19:50 +0000526 O << ", " << (unsigned int)SH << "\n";
527 return;
528 }
Chris Lattnerb410dc92006-06-20 23:18:58 +0000529 } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
Chris Lattner4d73a5a2006-02-08 06:56:40 +0000530 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
531 O << "mr ";
532 printOperand(MI, 0);
533 O << ", ";
534 printOperand(MI, 1);
535 O << "\n";
536 return;
537 }
Chris Lattner566c1b12006-11-18 01:23:56 +0000538 } else if (MI->getOpcode() == PPC::RLDICR) {
539 unsigned char SH = MI->getOperand(2).getImmedValue();
540 unsigned char ME = MI->getOperand(3).getImmedValue();
541 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
542 if (63-SH == ME) {
543 O << "sldi ";
544 printOperand(MI, 0);
545 O << ", ";
546 printOperand(MI, 1);
547 O << ", " << (unsigned int)SH << "\n";
548 return;
549 }
Nate Begemanad5f65c2005-04-05 18:19:50 +0000550 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000551
Nate Begemane59bf592004-08-14 22:09:10 +0000552 if (printInstruction(MI))
553 return; // Printer was automatically generated
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000554
Nate Begemaned428532004-09-04 05:00:00 +0000555 assert(0 && "Unhandled instruction in asm writer!");
556 abort();
Nate Begemane59bf592004-08-14 22:09:10 +0000557 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000558}
559
Jim Laskeybf111822006-12-21 20:26:09 +0000560/// runOnMachineFunction - This uses the printMachineInstruction()
561/// method to print assembly for each instruction.
562///
563bool LinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000564 DW.SetModuleInfo(&getAnalysis<MachineModuleInfo>());
Chris Lattner9b7ce7d2006-10-05 02:42:20 +0000565
Jim Laskeybf111822006-12-21 20:26:09 +0000566 SetupMachineFunction(MF);
567 O << "\n\n";
568
569 // Print out constants referenced by the function
570 EmitConstantPool(MF.getConstantPool());
571
572 // Print out labels for the function.
573 const Function *F = MF.getFunction();
574 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
575
576 switch (F->getLinkage()) {
577 default: assert(0 && "Unknown linkage type!");
578 case Function::InternalLinkage: // Symbols default to internal.
579 break;
580 case Function::ExternalLinkage:
581 O << "\t.global\t" << CurrentFnName << '\n'
582 << "\t.type\t" << CurrentFnName << ", @function\n";
583 break;
584 case Function::WeakLinkage:
585 case Function::LinkOnceLinkage:
586 O << "\t.global\t" << CurrentFnName << '\n';
587 O << "\t.weak\t" << CurrentFnName << '\n';
588 break;
589 }
Chris Lattner70d41072007-01-14 06:37:54 +0000590
591 if (F->hasHiddenVisibility())
592 if (const char *Directive = TAI->getHiddenDirective())
593 O << Directive << CurrentFnName << "\n";
594
Jim Laskeybf111822006-12-21 20:26:09 +0000595 EmitAlignment(2, F);
596 O << CurrentFnName << ":\n";
597
598 // Emit pre-function debug information.
599 DW.BeginFunction(&MF);
600
601 // Print out code for the function.
602 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
603 I != E; ++I) {
604 // Print a label for the basic block.
605 if (I != MF.begin()) {
606 printBasicBlockLabel(I, true);
607 O << '\n';
608 }
609 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
610 II != E; ++II) {
611 // Print the assembly for the instruction.
612 O << "\t";
613 printMachineInstruction(II);
614 }
615 }
616
617 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << "\n";
618
619 // Print out jump tables referenced by the function.
620 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
621
622 // Emit post-function debug information.
623 DW.EndFunction();
624
625 // We didn't modify anything.
626 return false;
627}
628
629bool LinuxAsmPrinter::doInitialization(Module &M) {
Dan Gohmanb8275a32007-07-25 19:33:14 +0000630 bool Result = AsmPrinter::doInitialization(M);
Jim Laskeybf111822006-12-21 20:26:09 +0000631
632 // GNU as handles section names wrapped in quotes
633 Mang->setUseQuotes(true);
634
635 SwitchToTextSection(TAI->getTextSection());
636
637 // Emit initial debug information.
638 DW.BeginModule(&M);
Dan Gohmanb8275a32007-07-25 19:33:14 +0000639 return Result;
Jim Laskeybf111822006-12-21 20:26:09 +0000640}
641
642bool LinuxAsmPrinter::doFinalization(Module &M) {
643 const TargetData *TD = TM.getTargetData();
644
645 // Print out module-level global variables here.
646 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
647 I != E; ++I) {
648 if (!I->hasInitializer()) continue; // External global require no code
649
650 // Check to see if this is a special global used by LLVM, if so, emit it.
651 if (EmitSpecialLLVMGlobal(I))
652 continue;
Chris Lattner70d41072007-01-14 06:37:54 +0000653
Jim Laskeybf111822006-12-21 20:26:09 +0000654 std::string name = Mang->getValueName(I);
Chris Lattner70d41072007-01-14 06:37:54 +0000655
656 if (I->hasHiddenVisibility())
657 if (const char *Directive = TAI->getHiddenDirective())
658 O << Directive << name << "\n";
659
Jim Laskeybf111822006-12-21 20:26:09 +0000660 Constant *C = I->getInitializer();
Duncan Sandsca0ed742007-11-05 00:04:43 +0000661 unsigned Size = TD->getABITypeSize(C->getType());
Jim Laskeybf111822006-12-21 20:26:09 +0000662 unsigned Align = TD->getPreferredAlignmentLog(I);
663
664 if (C->isNullValue() && /* FIXME: Verify correct */
Evan Cheng76a40232007-09-21 00:41:19 +0000665 !I->hasSection() &&
Jim Laskeybf111822006-12-21 20:26:09 +0000666 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
Evan Cheng76a40232007-09-21 00:41:19 +0000667 I->hasLinkOnceLinkage() || I->hasExternalLinkage())) {
Jim Laskeybf111822006-12-21 20:26:09 +0000668 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
669 if (I->hasExternalLinkage()) {
670 O << "\t.global " << name << '\n';
671 O << "\t.type " << name << ", @object\n";
Nick Lewyckyf4c164c2007-11-04 17:32:10 +0000672 if (TAI->getBSSSection())
673 SwitchToDataSection(TAI->getBSSSection(), I);
Nick Lewyckye2b90522007-07-25 03:48:45 +0000674 O << name << ":\n";
675 O << "\t.zero " << Size << "\n";
Jim Laskeybf111822006-12-21 20:26:09 +0000676 } else if (I->hasInternalLinkage()) {
677 SwitchToDataSection("\t.data", I);
678 O << TAI->getLCOMMDirective() << name << "," << Size;
679 } else {
680 SwitchToDataSection("\t.data", I);
681 O << ".comm " << name << "," << Size;
682 }
683 O << "\t\t" << TAI->getCommentString() << " '" << I->getName() << "'\n";
684 } else {
685 switch (I->getLinkage()) {
686 case GlobalValue::LinkOnceLinkage:
687 case GlobalValue::WeakLinkage:
688 O << "\t.global " << name << '\n'
689 << "\t.type " << name << ", @object\n"
690 << "\t.weak " << name << '\n';
691 SwitchToDataSection("\t.data", I);
692 break;
693 case GlobalValue::AppendingLinkage:
694 // FIXME: appending linkage variables should go into a section of
695 // their name or something. For now, just emit them as external.
696 case GlobalValue::ExternalLinkage:
697 // If external or appending, declare as a global symbol
698 O << "\t.global " << name << "\n"
699 << "\t.type " << name << ", @object\n";
700 // FALL THROUGH
701 case GlobalValue::InternalLinkage:
702 if (I->isConstant()) {
703 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
704 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
705 SwitchToDataSection(TAI->getCStringSection(), I);
706 break;
707 }
708 }
709
710 // FIXME: special handling for ".ctors" & ".dtors" sections
711 if (I->hasSection() &&
712 (I->getSection() == ".ctors" ||
713 I->getSection() == ".dtors")) {
714 std::string SectionName = ".section " + I->getSection()
715 + ",\"aw\",@progbits";
716 SwitchToDataSection(SectionName.c_str());
717 } else {
Evan Cheng032953d2007-03-08 08:31:54 +0000718 if (I->isConstant() && TAI->getReadOnlySection())
719 SwitchToDataSection(TAI->getReadOnlySection(), I);
720 else
721 SwitchToDataSection(TAI->getDataSection(), I);
Jim Laskeybf111822006-12-21 20:26:09 +0000722 }
723 break;
724 default:
725 cerr << "Unknown linkage type!";
726 abort();
727 }
728
729 EmitAlignment(Align, I);
730 O << name << ":\t\t\t\t" << TAI->getCommentString() << " '"
731 << I->getName() << "'\n";
732
733 // If the initializer is a extern weak symbol, remember to emit the weak
734 // reference!
735 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
736 if (GV->hasExternalWeakLinkage())
737 ExtWeakSymbols.insert(GV);
738
739 EmitGlobalConstant(C);
740 O << '\n';
741 }
742 }
743
744 // TODO
745
746 // Emit initial debug information.
747 DW.EndModule();
748
Dan Gohmanb8275a32007-07-25 19:33:14 +0000749 return AsmPrinter::doFinalization(M);
Jim Laskeybf111822006-12-21 20:26:09 +0000750}
751
752std::string LinuxAsmPrinter::getSectionForFunction(const Function &F) const {
753 switch (F.getLinkage()) {
754 default: assert(0 && "Unknown linkage type!");
755 case Function::ExternalLinkage:
756 case Function::InternalLinkage: return TAI->getTextSection();
757 case Function::WeakLinkage:
758 case Function::LinkOnceLinkage:
759 return ".text";
760 }
761}
Chris Lattner9b7ce7d2006-10-05 02:42:20 +0000762
763std::string DarwinAsmPrinter::getSectionForFunction(const Function &F) const {
764 switch (F.getLinkage()) {
765 default: assert(0 && "Unknown linkage type!");
766 case Function::ExternalLinkage:
767 case Function::InternalLinkage: return TAI->getTextSection();
768 case Function::WeakLinkage:
769 case Function::LinkOnceLinkage:
770 return ".section __TEXT,__textcoal_nt,coalesced,pure_instructions";
771 }
772}
773
Nate Begemaned428532004-09-04 05:00:00 +0000774/// runOnMachineFunction - This uses the printMachineInstruction()
775/// method to print assembly for each instruction.
776///
777bool DarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +0000778 // We need this for Personality functions.
779 MMI = &getAnalysis<MachineModuleInfo>();
780 DW.SetModuleInfo(MMI);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000781
Chris Lattner8b8b9512005-11-21 07:51:23 +0000782 SetupMachineFunction(MF);
Nate Begemaned428532004-09-04 05:00:00 +0000783 O << "\n\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +0000784
Nate Begemaned428532004-09-04 05:00:00 +0000785 // Print out constants referenced by the function
Chris Lattnerc569e612005-11-21 08:26:15 +0000786 EmitConstantPool(MF.getConstantPool());
Nate Begemaned428532004-09-04 05:00:00 +0000787
788 // Print out labels for the function.
Chris Lattnerac7fd7f2005-11-14 18:52:46 +0000789 const Function *F = MF.getFunction();
Chris Lattner9b7ce7d2006-10-05 02:42:20 +0000790 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Chris Lattnerb56dcc42006-10-05 00:35:50 +0000791
Chris Lattnera637c322005-12-16 00:22:14 +0000792 switch (F->getLinkage()) {
793 default: assert(0 && "Unknown linkage type!");
794 case Function::InternalLinkage: // Symbols default to internal.
Chris Lattnera637c322005-12-16 00:22:14 +0000795 break;
796 case Function::ExternalLinkage:
Chris Lattnerf02a9162005-10-28 18:44:07 +0000797 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattnera637c322005-12-16 00:22:14 +0000798 break;
799 case Function::WeakLinkage:
800 case Function::LinkOnceLinkage:
Chris Lattnera637c322005-12-16 00:22:14 +0000801 O << "\t.globl\t" << CurrentFnName << "\n";
802 O << "\t.weak_definition\t" << CurrentFnName << "\n";
803 break;
804 }
Chris Lattner70d41072007-01-14 06:37:54 +0000805
806 if (F->hasHiddenVisibility())
807 if (const char *Directive = TAI->getHiddenDirective())
808 O << Directive << CurrentFnName << "\n";
809
Chris Lattner33d50822006-02-14 20:42:33 +0000810 EmitAlignment(4, F);
Nate Begemaned428532004-09-04 05:00:00 +0000811 O << CurrentFnName << ":\n";
812
Jim Laskey6b92b8e2006-04-07 20:44:42 +0000813 // Emit pre-function debug information.
Jim Laskey89d67fa2006-06-23 12:51:53 +0000814 DW.BeginFunction(&MF);
Jim Laskey6b92b8e2006-04-07 20:44:42 +0000815
Nate Begemaned428532004-09-04 05:00:00 +0000816 // Print out code for the function.
817 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
818 I != E; ++I) {
819 // Print a label for the basic block.
Chris Lattner1db1adb2005-08-21 19:09:33 +0000820 if (I != MF.begin()) {
Nate Begemancdf38c42006-05-02 05:37:32 +0000821 printBasicBlockLabel(I, true);
822 O << '\n';
Chris Lattner1db1adb2005-08-21 19:09:33 +0000823 }
Nate Begemaned428532004-09-04 05:00:00 +0000824 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
825 II != E; ++II) {
826 // Print the assembly for the instruction.
827 O << "\t";
828 printMachineInstruction(II);
829 }
830 }
Nate Begemaned428532004-09-04 05:00:00 +0000831
Chris Lattnerfea13d32006-10-05 00:26:05 +0000832 // Print out jump tables referenced by the function.
Chris Lattner1da31ee2006-10-05 03:01:21 +0000833 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Chris Lattnerfea13d32006-10-05 00:26:05 +0000834
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000835 // Emit post-function debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000836 DW.EndFunction();
Chris Lattner37dfa022006-10-05 00:24:46 +0000837
Nate Begemaned428532004-09-04 05:00:00 +0000838 // We didn't modify anything.
839 return false;
840}
841
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000842
Nate Begeman18ed0292005-07-21 01:25:49 +0000843bool DarwinAsmPrinter::doInitialization(Module &M) {
Jim Laskeyc35010d2006-12-12 20:57:08 +0000844 static const char *CPUDirectives[] = {
845 "ppc",
846 "ppc601",
847 "ppc602",
848 "ppc603",
849 "ppc7400",
850 "ppc750",
851 "ppc970",
852 "ppc64"
853 };
854
855 unsigned Directive = Subtarget.getDarwinDirective();
856 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
857 Directive = PPC::DIR_970;
858 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
859 Directive = PPC::DIR_7400;
860 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
861 Directive = PPC::DIR_64;
862 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
863 O << "\t.machine " << CPUDirectives[Directive] << "\n";
Jim Laskey55a7ec32006-12-12 16:07:33 +0000864
Dan Gohmanb8275a32007-07-25 19:33:14 +0000865 bool Result = AsmPrinter::doInitialization(M);
Chris Lattner85eac0d2005-11-10 19:33:43 +0000866
867 // Darwin wants symbols to be quoted if they have complex names.
868 Mang->setUseQuotes(true);
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000869
Jim Laskey2ada0852006-11-28 18:21:52 +0000870 // Prime text sections so they are adjacent. This reduces the likelihood a
871 // large data or debug section causes a branch to exceed 16M limit.
872 SwitchToTextSection(".section __TEXT,__textcoal_nt,coalesced,"
873 "pure_instructions");
874 if (TM.getRelocationModel() == Reloc::PIC_) {
875 SwitchToTextSection(".section __TEXT,__picsymbolstub1,symbol_stubs,"
876 "pure_instructions,32");
877 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
878 SwitchToTextSection(".section __TEXT,__symbol_stub1,symbol_stubs,"
879 "pure_instructions,16");
880 }
881 SwitchToTextSection(TAI->getTextSection());
882
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000883 // Emit initial debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000884 DW.BeginModule(&M);
Dan Gohmanb8275a32007-07-25 19:33:14 +0000885 return Result;
Nate Begeman18ed0292005-07-21 01:25:49 +0000886}
887
Nate Begemaned428532004-09-04 05:00:00 +0000888bool DarwinAsmPrinter::doFinalization(Module &M) {
Owen Andersona69571c2006-05-03 01:29:57 +0000889 const TargetData *TD = TM.getTargetData();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000890
891 // Print out module-level global variables here.
Chris Lattnerced704b2005-11-14 19:00:30 +0000892 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Chris Lattnerdeea4162005-12-13 04:33:58 +0000893 I != E; ++I) {
894 if (!I->hasInitializer()) continue; // External global require no code
895
Chris Lattnerd1239b72005-12-13 06:32:50 +0000896 // Check to see if this is a special global used by LLVM, if so, emit it.
Evan Chengb267ca12007-01-30 08:04:53 +0000897 if (EmitSpecialLLVMGlobal(I)) {
898 if (TM.getRelocationModel() == Reloc::Static) {
899 if (I->getName() == "llvm.global_ctors")
900 O << ".reference .constructors_used\n";
901 else if (I->getName() == "llvm.global_dtors")
902 O << ".reference .destructors_used\n";
903 }
Chris Lattnerd1239b72005-12-13 06:32:50 +0000904 continue;
Evan Chengb267ca12007-01-30 08:04:53 +0000905 }
Chris Lattnerdeea4162005-12-13 04:33:58 +0000906
Chris Lattnerdeea4162005-12-13 04:33:58 +0000907 std::string name = Mang->getValueName(I);
Chris Lattner70d41072007-01-14 06:37:54 +0000908
909 if (I->hasHiddenVisibility())
910 if (const char *Directive = TAI->getHiddenDirective())
911 O << Directive << name << "\n";
912
Chris Lattnerdeea4162005-12-13 04:33:58 +0000913 Constant *C = I->getInitializer();
Evan Cheng98ded762007-03-08 01:25:25 +0000914 const Type *Type = C->getType();
Duncan Sandsca0ed742007-11-05 00:04:43 +0000915 unsigned Size = TD->getABITypeSize(Type);
Devang Patelf9c197e2006-10-24 20:32:14 +0000916 unsigned Align = TD->getPreferredAlignmentLog(I);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000917
918 if (C->isNullValue() && /* FIXME: Verify correct */
Devang Patel0f0daea2007-09-20 23:07:37 +0000919 !I->hasSection() &&
Chris Lattnerdeea4162005-12-13 04:33:58 +0000920 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
Devang Patel0f0daea2007-09-20 23:07:37 +0000921 I->hasLinkOnceLinkage() || I->hasExternalLinkage())) {
Chris Lattnerdeea4162005-12-13 04:33:58 +0000922 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Chris Lattner0d7db6f2006-02-14 22:18:23 +0000923 if (I->hasExternalLinkage()) {
924 O << "\t.globl " << name << '\n';
925 O << "\t.zerofill __DATA, __common, " << name << ", "
926 << Size << ", " << Align;
927 } else if (I->hasInternalLinkage()) {
Chris Lattner219f1b52006-05-09 16:15:00 +0000928 SwitchToDataSection("\t.data", I);
Jim Laskey563321a2006-09-06 18:34:40 +0000929 O << TAI->getLCOMMDirective() << name << "," << Size << "," << Align;
Chris Lattner0d7db6f2006-02-14 22:18:23 +0000930 } else {
Chris Lattner219f1b52006-05-09 16:15:00 +0000931 SwitchToDataSection("\t.data", I);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000932 O << ".comm " << name << "," << Size;
Chris Lattner0d7db6f2006-02-14 22:18:23 +0000933 }
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000934 O << "\t\t" << TAI->getCommentString() << " '" << I->getName() << "'\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +0000935 } else {
936 switch (I->getLinkage()) {
Jim Laskey45e507c2006-12-01 14:37:39 +0000937 case GlobalValue::LinkOnceLinkage:
Chris Lattnerdeea4162005-12-13 04:33:58 +0000938 case GlobalValue::WeakLinkage:
Chris Lattnercec26fc2005-12-22 21:15:17 +0000939 O << "\t.globl " << name << '\n'
940 << "\t.weak_definition " << name << '\n';
Chris Lattner4632d7a2006-05-09 04:59:56 +0000941 SwitchToDataSection(".section __DATA,__datacoal_nt,coalesced", I);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000942 break;
943 case GlobalValue::AppendingLinkage:
944 // FIXME: appending linkage variables should go into a section of
945 // their name or something. For now, just emit them as external.
946 case GlobalValue::ExternalLinkage:
947 // If external or appending, declare as a global symbol
948 O << "\t.globl " << name << "\n";
949 // FALL THROUGH
950 case GlobalValue::InternalLinkage:
Evan Cheng31b6dda2006-10-28 05:56:51 +0000951 if (I->isConstant()) {
Evan Cheng80aa9a12006-10-26 21:48:57 +0000952 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
Evan Cheng31b6dda2006-10-28 05:56:51 +0000953 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
Evan Cheng80aa9a12006-10-26 21:48:57 +0000954 SwitchToDataSection(TAI->getCStringSection(), I);
955 break;
956 }
957 }
958
Evan Cheng98ded762007-03-08 01:25:25 +0000959 if (!I->isConstant())
960 SwitchToDataSection(TAI->getDataSection(), I);
961 else {
962 // Read-only data.
Evan Cheng032953d2007-03-08 08:31:54 +0000963 bool HasReloc = C->ContainsRelocations();
964 if (HasReloc &&
Evan Cheng98ded762007-03-08 01:25:25 +0000965 TM.getRelocationModel() != Reloc::Static)
966 SwitchToDataSection("\t.const_data\n");
Evan Cheng032953d2007-03-08 08:31:54 +0000967 else if (!HasReloc && Size == 4 &&
Evan Cheng98ded762007-03-08 01:25:25 +0000968 TAI->getFourByteConstantSection())
969 SwitchToDataSection(TAI->getFourByteConstantSection(), I);
Evan Cheng032953d2007-03-08 08:31:54 +0000970 else if (!HasReloc && Size == 8 &&
Evan Cheng98ded762007-03-08 01:25:25 +0000971 TAI->getEightByteConstantSection())
972 SwitchToDataSection(TAI->getEightByteConstantSection(), I);
Evan Cheng032953d2007-03-08 08:31:54 +0000973 else if (!HasReloc && Size == 16 &&
Evan Cheng98ded762007-03-08 01:25:25 +0000974 TAI->getSixteenByteConstantSection())
975 SwitchToDataSection(TAI->getSixteenByteConstantSection(), I);
976 else if (TAI->getReadOnlySection())
977 SwitchToDataSection(TAI->getReadOnlySection(), I);
978 else
979 SwitchToDataSection(TAI->getDataSection(), I);
980 }
Chris Lattnerdeea4162005-12-13 04:33:58 +0000981 break;
982 default:
Bill Wendlingf5da1332006-12-07 22:21:48 +0000983 cerr << "Unknown linkage type!";
Chris Lattnerdeea4162005-12-13 04:33:58 +0000984 abort();
985 }
986
987 EmitAlignment(Align, I);
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000988 O << name << ":\t\t\t\t" << TAI->getCommentString() << " '"
989 << I->getName() << "'\n";
Evan Cheng81cf60f2006-12-01 09:13:26 +0000990
991 // If the initializer is a extern weak symbol, remember to emit the weak
992 // reference!
993 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
994 if (GV->hasExternalWeakLinkage())
Rafael Espindola15404d02006-12-18 03:37:18 +0000995 ExtWeakSymbols.insert(GV);
Evan Cheng81cf60f2006-12-01 09:13:26 +0000996
Chris Lattnerdeea4162005-12-13 04:33:58 +0000997 EmitGlobalConstant(C);
Chris Lattnerbc1c2152006-01-21 01:35:26 +0000998 O << '\n';
Chris Lattnerdeea4162005-12-13 04:33:58 +0000999 }
1000 }
Misha Brukmanda2b13f2004-07-16 20:29:04 +00001001
Chris Lattner7e097e42006-06-27 01:02:25 +00001002 bool isPPC64 = TD->getPointerSizeInBits() == 64;
1003
Misha Brukmanda2b13f2004-07-16 20:29:04 +00001004 // Output stubs for dynamically-linked functions
Chris Lattner35d86fe2006-07-26 21:12:04 +00001005 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattnerdeea4162005-12-13 04:33:58 +00001006 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
1007 i != e; ++i) {
Chris Lattner4632d7a2006-05-09 04:59:56 +00001008 SwitchToTextSection(".section __TEXT,__picsymbolstub1,symbol_stubs,"
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00001009 "pure_instructions,32");
Chris Lattner7e097e42006-06-27 01:02:25 +00001010 EmitAlignment(4);
Chris Lattnerdeea4162005-12-13 04:33:58 +00001011 O << "L" << *i << "$stub:\n";
1012 O << "\t.indirect_symbol " << *i << "\n";
1013 O << "\tmflr r0\n";
1014 O << "\tbcl 20,31,L0$" << *i << "\n";
1015 O << "L0$" << *i << ":\n";
1016 O << "\tmflr r11\n";
1017 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
1018 O << "\tmtlr r0\n";
Chris Lattner7e097e42006-06-27 01:02:25 +00001019 if (isPPC64)
1020 O << "\tldu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
1021 else
1022 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +00001023 O << "\tmtctr r12\n";
1024 O << "\tbctr\n";
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00001025 SwitchToDataSection(".lazy_symbol_pointer");
Chris Lattnerdeea4162005-12-13 04:33:58 +00001026 O << "L" << *i << "$lazy_ptr:\n";
1027 O << "\t.indirect_symbol " << *i << "\n";
Chris Lattner7e097e42006-06-27 01:02:25 +00001028 if (isPPC64)
1029 O << "\t.quad dyld_stub_binding_helper\n";
1030 else
1031 O << "\t.long dyld_stub_binding_helper\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +00001032 }
1033 } else {
1034 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
1035 i != e; ++i) {
Chris Lattner4632d7a2006-05-09 04:59:56 +00001036 SwitchToTextSection(".section __TEXT,__symbol_stub1,symbol_stubs,"
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00001037 "pure_instructions,16");
Chris Lattnerdeea4162005-12-13 04:33:58 +00001038 EmitAlignment(4);
1039 O << "L" << *i << "$stub:\n";
1040 O << "\t.indirect_symbol " << *i << "\n";
1041 O << "\tlis r11,ha16(L" << *i << "$lazy_ptr)\n";
Chris Lattner7e097e42006-06-27 01:02:25 +00001042 if (isPPC64)
1043 O << "\tldu r12,lo16(L" << *i << "$lazy_ptr)(r11)\n";
1044 else
1045 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr)(r11)\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +00001046 O << "\tmtctr r12\n";
1047 O << "\tbctr\n";
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00001048 SwitchToDataSection(".lazy_symbol_pointer");
Chris Lattnerdeea4162005-12-13 04:33:58 +00001049 O << "L" << *i << "$lazy_ptr:\n";
1050 O << "\t.indirect_symbol " << *i << "\n";
Chris Lattner7e097e42006-06-27 01:02:25 +00001051 if (isPPC64)
1052 O << "\t.quad dyld_stub_binding_helper\n";
1053 else
1054 O << "\t.long dyld_stub_binding_helper\n";
Nate Begeman2497e632005-07-21 20:44:43 +00001055 }
Misha Brukman46fd00a2004-06-24 23:04:11 +00001056 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +00001057
Misha Brukmanda2b13f2004-07-16 20:29:04 +00001058 O << "\n";
1059
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00001060 if (ExceptionHandling && TAI->doesSupportExceptionHandling() && MMI) {
1061 // Add the (possibly multiple) personalities to the set of global values.
1062 const std::vector<Function *>& Personalities = MMI->getPersonalities();
1063
1064 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
1065 E = Personalities.end(); I != E; ++I)
1066 if (*I) GVStubs.insert("_" + (*I)->getName());
1067 }
1068
Chris Lattnera637c322005-12-16 00:22:14 +00001069 // Output stubs for external and common global variables.
Dan Gohmancb406c22007-10-03 19:26:29 +00001070 if (!GVStubs.empty()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00001071 SwitchToDataSection(".non_lazy_symbol_pointer");
Chris Lattnera637c322005-12-16 00:22:14 +00001072 for (std::set<std::string>::iterator I = GVStubs.begin(),
1073 E = GVStubs.end(); I != E; ++I) {
1074 O << "L" << *I << "$non_lazy_ptr:\n";
1075 O << "\t.indirect_symbol " << *I << "\n";
Chris Lattner9f029a62006-06-27 20:20:53 +00001076 if (isPPC64)
1077 O << "\t.quad\t0\n";
1078 else
1079 O << "\t.long\t0\n";
1080
Chris Lattnerdeea4162005-12-13 04:33:58 +00001081 }
Nate Begemane59bf592004-08-14 22:09:10 +00001082 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001083
Jim Laskey063e7652006-01-17 17:31:53 +00001084 // Emit initial debug information.
Jim Laskey99db0442006-03-23 18:09:44 +00001085 DW.EndModule();
Jim Laskey063e7652006-01-17 17:31:53 +00001086
Chris Lattner5b0ac992005-11-01 00:12:36 +00001087 // Funny Darwin hack: This flag tells the linker that no global symbols
1088 // contain code that falls through to other global symbols (e.g. the obvious
1089 // implementation of multiple entry points). If this doesn't occur, the
1090 // linker can safely perform dead code stripping. Since LLVM never generates
1091 // code that does this, it is always safe to set.
Chris Lattner4da1c822006-09-20 17:12:19 +00001092 O << "\t.subsections_via_symbols\n";
Chris Lattner5b0ac992005-11-01 00:12:36 +00001093
Dan Gohmanb8275a32007-07-25 19:33:14 +00001094 return AsmPrinter::doFinalization(M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +00001095}
Nate Begemaned428532004-09-04 05:00:00 +00001096
Chris Lattner4da1c822006-09-20 17:12:19 +00001097
1098
Jim Laskeyb608a4d2006-12-20 20:56:46 +00001099/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1100/// for a MachineFunction to the given output stream, in a format that the
Chris Lattner4da1c822006-09-20 17:12:19 +00001101/// Darwin assembler can deal with.
1102///
1103FunctionPass *llvm::createPPCAsmPrinterPass(std::ostream &o,
1104 PPCTargetMachine &tm) {
Jim Laskeybf111822006-12-21 20:26:09 +00001105 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1106
1107 if (Subtarget->isDarwin()) {
1108 return new DarwinAsmPrinter(o, tm, tm.getTargetAsmInfo());
1109 } else {
1110 return new LinuxAsmPrinter(o, tm, tm.getTargetAsmInfo());
1111 }
Chris Lattner4da1c822006-09-20 17:12:19 +00001112}
1113