blob: 11c0ec008b55d68ef812ba510f9ac938f2dde1fa [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//
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 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 Lattner16e71f22005-10-14 23:59:06 +000021#include "PPCTargetMachine.h"
Chris Lattner26689592005-10-14 23:51:18 +000022#include "PPCSubtarget.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000023#include "llvm/Constants.h"
24#include "llvm/DerivedTypes.h"
25#include "llvm/Module.h"
26#include "llvm/Assembly/Writer.h"
Chris Lattnera3840792004-08-16 23:25:21 +000027#include "llvm/CodeGen/AsmPrinter.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000028#include "llvm/CodeGen/DwarfWriter.h"
Jim Laskeyf5395ce2005-12-16 22:45:29 +000029#include "llvm/CodeGen/MachineDebugInfo.h"
Misha Brukman05794492004-06-24 17:31:42 +000030#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000031#include "llvm/CodeGen/MachineInstr.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000032#include "llvm/Support/Mangler.h"
Nate Begemana11c2e82004-09-04 14:51:26 +000033#include "llvm/Support/MathExtras.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000034#include "llvm/Support/CommandLine.h"
35#include "llvm/Support/Debug.h"
Nate Begeman17304c32004-10-26 06:02:38 +000036#include "llvm/Target/MRegisterInfo.h"
Nate Begemand4c8bea2004-11-25 07:09:01 +000037#include "llvm/Target/TargetInstrInfo.h"
Evan Chengd2ee2182006-02-18 00:08:58 +000038#include "llvm/Target/TargetOptions.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000039#include "llvm/ADT/Statistic.h"
40#include "llvm/ADT/StringExtras.h"
Chris Lattner2c2c6c62006-01-22 23:41:00 +000041#include <iostream>
Misha Brukman05794492004-06-24 17:31:42 +000042#include <set>
Chris Lattnera3840792004-08-16 23:25:21 +000043using namespace llvm;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000044
45namespace {
46 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
47
Chris Lattnerac7fd7f2005-11-14 18:52:46 +000048 class PPCAsmPrinter : public AsmPrinter {
Chris Lattnerd717b192005-12-11 07:45:47 +000049 public:
Chris Lattnera637c322005-12-16 00:22:14 +000050 std::set<std::string> FnStubs, GVStubs;
Chris Lattnerac7fd7f2005-11-14 18:52:46 +000051
Chris Lattner4c7b43b2005-10-14 23:37:35 +000052 PPCAsmPrinter(std::ostream &O, TargetMachine &TM)
Chris Lattner07455362005-11-21 08:14:07 +000053 : AsmPrinter(O, TM) {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +000054
Misha Brukman5dfe3a92004-06-21 16:55:25 +000055 virtual const char *getPassName() const {
Nate Begemaned428532004-09-04 05:00:00 +000056 return "PowerPC Assembly Printer";
Misha Brukman5dfe3a92004-06-21 16:55:25 +000057 }
58
Nate Begeman21e463b2005-10-16 05:39:50 +000059 PPCTargetMachine &getTM() {
60 return static_cast<PPCTargetMachine&>(TM);
Chris Lattnera3840792004-08-16 23:25:21 +000061 }
62
Nate Begemanadeb43d2005-07-20 22:42:00 +000063 unsigned enumRegToMachineReg(unsigned enumReg) {
64 switch (enumReg) {
65 default: assert(0 && "Unhandled register!"); break;
66 case PPC::CR0: return 0;
67 case PPC::CR1: return 1;
68 case PPC::CR2: return 2;
69 case PPC::CR3: return 3;
70 case PPC::CR4: return 4;
71 case PPC::CR5: return 5;
72 case PPC::CR6: return 6;
73 case PPC::CR7: return 7;
74 }
75 abort();
76 }
77
Nate Begemane59bf592004-08-14 22:09:10 +000078 /// printInstruction - This method is automatically generated by tablegen
79 /// from the instruction set description. This method returns true if the
80 /// machine instruction was sufficiently described to print it, otherwise it
81 /// returns false.
82 bool printInstruction(const MachineInstr *MI);
83
Misha Brukman5dfe3a92004-06-21 16:55:25 +000084 void printMachineInstruction(const MachineInstr *MI);
Chris Lattner9ba13e42005-11-17 19:25:59 +000085 void printOp(const MachineOperand &MO);
Chris Lattner7bb424f2004-08-14 23:27:29 +000086
Chris Lattner58873272006-02-01 22:38:46 +000087 void printOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner7bb424f2004-08-14 23:27:29 +000088 const MachineOperand &MO = MI->getOperand(OpNo);
89 if (MO.getType() == MachineOperand::MO_MachineRegister) {
90 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
Chris Lattner9dc4d3c2005-08-22 22:00:02 +000091 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Chris Lattner0ea31712004-08-15 05:46:14 +000092 } else if (MO.isImmediate()) {
93 O << MO.getImmedValue();
Chris Lattner7bb424f2004-08-14 23:27:29 +000094 } else {
95 printOp(MO);
96 }
97 }
Chris Lattner58873272006-02-01 22:38:46 +000098
99 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnere3f01572006-02-23 19:31:10 +0000100 unsigned AsmVariant, const char *ExtraCode);
Chris Lattner2c003e22006-02-24 20:27:40 +0000101 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
102 unsigned AsmVariant, const char *ExtraCode);
103
Chris Lattner58873272006-02-01 22:38:46 +0000104
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000105 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
106 char value = MI->getOperand(OpNo).getImmedValue();
107 value = (value << (32-5)) >> (32-5);
108 O << (int)value;
109 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000110 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begemanc3306122004-08-21 05:56:39 +0000111 unsigned char value = MI->getOperand(OpNo).getImmedValue();
Chris Lattner12585ba2004-08-21 19:11:03 +0000112 assert(value <= 31 && "Invalid u5imm argument!");
Nate Begemanc3306122004-08-21 05:56:39 +0000113 O << (unsigned int)value;
114 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000115 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman07aada82004-08-30 02:28:06 +0000116 unsigned char value = MI->getOperand(OpNo).getImmedValue();
117 assert(value <= 63 && "Invalid u6imm argument!");
118 O << (unsigned int)value;
119 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000120 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begemaned428532004-09-04 05:00:00 +0000121 O << (short)MI->getOperand(OpNo).getImmedValue();
122 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000123 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner97b2a2e2004-08-15 05:20:16 +0000124 O << (unsigned short)MI->getOperand(OpNo).getImmedValue();
125 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000126 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner841d12d2005-10-18 16:51:22 +0000127 O << (short)MI->getOperand(OpNo).getImmedValue()*4;
128 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000129 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begemaned428532004-09-04 05:00:00 +0000130 // Branches can take an immediate operand. This is used by the branch
131 // selection pass to print $+8, an eight byte displacement from the PC.
132 if (MI->getOperand(OpNo).isImmediate()) {
Nate Begeman27499e32005-04-10 01:48:29 +0000133 O << "$+" << MI->getOperand(OpNo).getImmedValue();
Nate Begemaned428532004-09-04 05:00:00 +0000134 } else {
Chris Lattner3e7f86a2005-11-17 19:16:08 +0000135 printOp(MI->getOperand(OpNo));
Nate Begemaned428532004-09-04 05:00:00 +0000136 }
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000137 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000138 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9ba13e42005-11-17 19:25:59 +0000139 const MachineOperand &MO = MI->getOperand(OpNo);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000140 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattnera637c322005-12-16 00:22:14 +0000141 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
142 GlobalValue *GV = MO.getGlobal();
143 if (((GV->isExternal() || GV->hasWeakLinkage() ||
144 GV->hasLinkOnceLinkage()))) {
145 // Dynamically-resolved functions need a stub for the function.
146 std::string Name = Mang->getValueName(GV);
147 FnStubs.insert(Name);
148 O << "L" << Name << "$stub";
149 return;
150 }
151 }
Chris Lattner9542f9712005-11-17 19:40:30 +0000152 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
153 std::string Name(GlobalPrefix); Name += MO.getSymbolName();
154 FnStubs.insert(Name);
155 O << "L" << Name << "$stub";
156 return;
Chris Lattner9542f9712005-11-17 19:40:30 +0000157 }
Chris Lattner9ba13e42005-11-17 19:25:59 +0000158 }
Chris Lattner9542f9712005-11-17 19:40:30 +0000159
160 printOp(MI->getOperand(OpNo));
Chris Lattner3e7f86a2005-11-17 19:16:08 +0000161 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000162 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman422b0ce2005-11-16 00:48:01 +0000163 O << (int)MI->getOperand(OpNo).getImmedValue()*4;
164 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000165 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera637c322005-12-16 00:22:14 +0000166 O << "\"L" << getFunctionNumber() << "$pb\"\n";
167 O << "\"L" << getFunctionNumber() << "$pb\":";
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000168 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000169 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman2497e632005-07-21 20:44:43 +0000170 if (MI->getOperand(OpNo).isImmediate()) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000171 printS16ImmOperand(MI, OpNo);
Nate Begeman2497e632005-07-21 20:44:43 +0000172 } else {
173 O << "ha16(";
174 printOp(MI->getOperand(OpNo));
Evan Cheng4c1aa862006-02-22 20:19:42 +0000175 if (TM.getRelocationModel() == Reloc::PIC)
Chris Lattnera637c322005-12-16 00:22:14 +0000176 O << "-\"L" << getFunctionNumber() << "$pb\")";
Nate Begeman2497e632005-07-21 20:44:43 +0000177 else
178 O << ')';
179 }
Nate Begemaned428532004-09-04 05:00:00 +0000180 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000181 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Nate Begemaned428532004-09-04 05:00:00 +0000182 if (MI->getOperand(OpNo).isImmediate()) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000183 printS16ImmOperand(MI, OpNo);
Nate Begemaned428532004-09-04 05:00:00 +0000184 } else {
185 O << "lo16(";
Nate Begemand4c8bea2004-11-25 07:09:01 +0000186 printOp(MI->getOperand(OpNo));
Evan Cheng4c1aa862006-02-22 20:19:42 +0000187 if (TM.getRelocationModel() == Reloc::PIC)
Chris Lattnera637c322005-12-16 00:22:14 +0000188 O << "-\"L" << getFunctionNumber() << "$pb\")";
Nate Begeman2497e632005-07-21 20:44:43 +0000189 else
190 O << ')';
Nate Begemaned428532004-09-04 05:00:00 +0000191 }
192 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000193 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
Nate Begemanadeb43d2005-07-20 22:42:00 +0000194 unsigned CCReg = MI->getOperand(OpNo).getReg();
195 unsigned RegNo = enumRegToMachineReg(CCReg);
196 O << (0x80 >> RegNo);
197 }
Chris Lattner2c003e22006-02-24 20:27:40 +0000198 // The new addressing mode printers.
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000199 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
200 printSymbolLo(MI, OpNo);
201 O << '(';
Chris Lattner13feb582006-03-21 17:21:13 +0000202 if (MI->getOperand(OpNo+1).isRegister() &&
203 MI->getOperand(OpNo+1).getReg() == PPC::R0)
204 O << "0";
205 else
206 printOperand(MI, OpNo+1);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000207 O << ')';
208 }
Chris Lattnere5ba5802006-03-22 05:26:03 +0000209 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
210 if (MI->getOperand(OpNo).isImmediate())
211 printS16X4ImmOperand(MI, OpNo);
212 else
213 printSymbolLo(MI, OpNo);
214 O << '(';
215 if (MI->getOperand(OpNo+1).isRegister() &&
216 MI->getOperand(OpNo+1).getReg() == PPC::R0)
217 O << "0";
218 else
219 printOperand(MI, OpNo+1);
220 O << ')';
221 }
222
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000223 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman88276b82005-12-19 23:40:42 +0000224 // When used as the base register, r0 reads constant zero rather than
225 // the value contained in the register. For this reason, the darwin
226 // assembler requires that we print r0 as 0 (no r) when used as the base.
227 const MachineOperand &MO = MI->getOperand(OpNo);
228 if (MO.getReg() == PPC::R0)
229 O << '0';
230 else
231 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000232 O << ", ";
233 printOperand(MI, OpNo+1);
234 }
235
Nate Begeman37efe672006-04-22 18:53:45 +0000236 virtual void printBasicBlockLabel(const MachineBasicBlock *MBB) const;
237
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000238 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
Nate Begemaned428532004-09-04 05:00:00 +0000239 virtual bool doFinalization(Module &M) = 0;
Jim Laskeyb2efb852006-01-04 22:28:25 +0000240
Nate Begemaned428532004-09-04 05:00:00 +0000241 };
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000242
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000243 /// DarwinDwarfWriter - Dwarf debug info writer customized for Darwin/Mac OS X
244 ///
245 struct DarwinDwarfWriter : public DwarfWriter {
246 // Ctor.
Jim Laskeyb2efb852006-01-04 22:28:25 +0000247 DarwinDwarfWriter(std::ostream &o, AsmPrinter *ap)
248 : DwarfWriter(o, ap)
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000249 {
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000250 needsSet = true;
Jim Laskey0420f2a2006-02-22 19:02:11 +0000251 DwarfAbbrevSection = ".section __DWARFA,__debug_abbrev";
252 DwarfInfoSection = ".section __DWARFA,__debug_info";
253 DwarfLineSection = ".section __DWARFA,__debug_line";
254 DwarfFrameSection = ".section __DWARFA,__debug_frame";
255 DwarfPubNamesSection = ".section __DWARFA,__debug_pubnames";
256 DwarfPubTypesSection = ".section __DWARFA,__debug_pubtypes";
257 DwarfStrSection = ".section __DWARFA,__debug_str";
258 DwarfLocSection = ".section __DWARFA,__debug_loc";
259 DwarfARangesSection = ".section __DWARFA,__debug_aranges";
260 DwarfRangesSection = ".section __DWARFA,__debug_ranges";
261 DwarfMacInfoSection = ".section __DWARFA,__debug_macinfo";
Jim Laskey063e7652006-01-17 17:31:53 +0000262 TextSection = ".text";
263 DataSection = ".data";
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000264 }
265 };
266
Misha Brukman3e0b51a2004-09-05 02:42:44 +0000267 /// DarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac OS
268 /// X
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000269 struct DarwinAsmPrinter : public PPCAsmPrinter {
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000270
271 DarwinDwarfWriter DW;
Nate Begemaned428532004-09-04 05:00:00 +0000272
273 DarwinAsmPrinter(std::ostream &O, TargetMachine &TM)
Chris Lattner418caa12006-02-08 23:42:22 +0000274 : PPCAsmPrinter(O, TM), DW(O, this) {
Nate Begemaned428532004-09-04 05:00:00 +0000275 CommentString = ";";
276 GlobalPrefix = "_";
Chris Lattnerf55366e2005-11-21 06:47:58 +0000277 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
Nate Begemaned428532004-09-04 05:00:00 +0000278 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
279 Data64bitsDirective = 0; // we can't emit a 64-bit unit
280 AlignmentIsInBytes = false; // Alignment is by power of 2.
Chris Lattnerc569e612005-11-21 08:26:15 +0000281 ConstantPoolSection = "\t.const\t";
Nate Begeman37efe672006-04-22 18:53:45 +0000282 // FIXME: Conditionalize jump table section based on PIC
283 JumpTableSection = ".const";
Chris Lattnerd717b192005-12-11 07:45:47 +0000284 LCOMMDirective = "\t.lcomm\t";
Chris Lattnerd1239b72005-12-13 06:32:50 +0000285 StaticCtorsSection = ".mod_init_func";
286 StaticDtorsSection = ".mod_term_func";
Chris Lattner418caa12006-02-08 23:42:22 +0000287 InlineAsmStart = InlineAsmEnd = ""; // Don't use #APP/#NO_APP
Nate Begemaned428532004-09-04 05:00:00 +0000288 }
289
290 virtual const char *getPassName() const {
291 return "Darwin PPC Assembly Printer";
292 }
Chris Lattner646f7af2005-12-09 18:24:29 +0000293
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000294 bool runOnMachineFunction(MachineFunction &F);
Nate Begeman18ed0292005-07-21 01:25:49 +0000295 bool doInitialization(Module &M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000296 bool doFinalization(Module &M);
Jim Laskeyb2efb852006-01-04 22:28:25 +0000297
298 void getAnalysisUsage(AnalysisUsage &AU) const {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000299 AU.setPreservesAll();
Jim Laskeyb2efb852006-01-04 22:28:25 +0000300 AU.addRequired<MachineDebugInfo>();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000301 PPCAsmPrinter::getAnalysisUsage(AU);
Jim Laskeyb2efb852006-01-04 22:28:25 +0000302 }
303
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000304 };
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000305
Misha Brukman3e0b51a2004-09-05 02:42:44 +0000306 /// AIXAsmPrinter - PowerPC assembly printer, customized for AIX
307 ///
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000308 struct AIXAsmPrinter : public PPCAsmPrinter {
Nate Begemaned428532004-09-04 05:00:00 +0000309 /// Map for labels corresponding to global variables
310 ///
311 std::map<const GlobalVariable*,std::string> GVToLabelMap;
312
313 AIXAsmPrinter(std::ostream &O, TargetMachine &TM)
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000314 : PPCAsmPrinter(O, TM) {
Nate Begemaned428532004-09-04 05:00:00 +0000315 CommentString = "#";
Chris Lattner3459bfb2005-11-10 18:20:29 +0000316 GlobalPrefix = ".";
Nate Begemaned428532004-09-04 05:00:00 +0000317 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
318 Data64bitsDirective = 0; // we can't emit a 64-bit unit
319 AlignmentIsInBytes = false; // Alignment is by power of 2.
Chris Lattnerc569e612005-11-21 08:26:15 +0000320 ConstantPoolSection = "\t.const\t";
Nate Begemaned428532004-09-04 05:00:00 +0000321 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000322
Nate Begemaned428532004-09-04 05:00:00 +0000323 virtual const char *getPassName() const {
324 return "AIX PPC Assembly Printer";
325 }
326
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000327 bool runOnMachineFunction(MachineFunction &F);
Nate Begemaned428532004-09-04 05:00:00 +0000328 bool doInitialization(Module &M);
329 bool doFinalization(Module &M);
330 };
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000331} // end of anonymous namespace
332
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000333/// createDarwinAsmPrinterPass - Returns a pass that prints the PPC assembly
334/// code for a MachineFunction to the given output stream, in a format that the
Nate Begemaned428532004-09-04 05:00:00 +0000335/// Darwin assembler can deal with.
336///
Evan Chengc4c62572006-03-13 23:20:37 +0000337FunctionPass *llvm::createDarwinAsmPrinter(std::ostream &o,
338 PPCTargetMachine &tm) {
Nate Begemaned428532004-09-04 05:00:00 +0000339 return new DarwinAsmPrinter(o, tm);
340}
341
342/// createAIXAsmPrinterPass - Returns a pass that prints the PPC assembly code
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000343/// for a MachineFunction to the given output stream, in a format that the
Nate Begemaned428532004-09-04 05:00:00 +0000344/// AIX 5L assembler can deal with.
345///
Evan Chengc4c62572006-03-13 23:20:37 +0000346FunctionPass *llvm::createAIXAsmPrinter(std::ostream &o, PPCTargetMachine &tm) {
Nate Begemaned428532004-09-04 05:00:00 +0000347 return new AIXAsmPrinter(o, tm);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000348}
349
Nate Begemane59bf592004-08-14 22:09:10 +0000350// Include the auto-generated portion of the assembly writer
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000351#include "PPCGenAsmWriter.inc"
Nate Begemane59bf592004-08-14 22:09:10 +0000352
Chris Lattner9ba13e42005-11-17 19:25:59 +0000353void PPCAsmPrinter::printOp(const MachineOperand &MO) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000354 const MRegisterInfo &RI = *TM.getRegisterInfo();
355 int new_symbol;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000356
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000357 switch (MO.getType()) {
358 case MachineOperand::MO_VirtualRegister:
359 if (Value *V = MO.getVRegValueOrNull()) {
360 O << "<" << V->getName() << ">";
361 return;
362 }
363 // FALLTHROUGH
364 case MachineOperand::MO_MachineRegister:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000365 case MachineOperand::MO_CCRegister:
Chris Lattner9dc4d3c2005-08-22 22:00:02 +0000366 O << RI.get(MO.getReg()).Name;
Misha Brukman7f484a52004-06-24 23:51:00 +0000367 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000368
369 case MachineOperand::MO_SignExtendedImmed:
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000370 case MachineOperand::MO_UnextendedImmed:
371 std::cerr << "printOp() does not handle immediate values\n";
372 abort();
Misha Brukman97a296f2004-07-21 20:11:11 +0000373 return;
374
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000375 case MachineOperand::MO_PCRelativeDisp:
376 std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
377 abort();
378 return;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000379
Nate Begeman37efe672006-04-22 18:53:45 +0000380 case MachineOperand::MO_MachineBasicBlock:
381 printBasicBlockLabel(MO.getMachineBasicBlock());
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000382 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000383 case MachineOperand::MO_JumpTableIndex:
384 O << PrivateGlobalPrefix << "JTI" << getFunctionNumber()
385 << '_' << MO.getJumpTableIndex();
386 // FIXME: PIC relocation model
387 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000388 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner07455362005-11-21 08:14:07 +0000389 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber()
Chris Lattnerf55366e2005-11-21 06:47:58 +0000390 << '_' << MO.getConstantPoolIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000391 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000392 case MachineOperand::MO_ExternalSymbol:
Chris Lattnera637c322005-12-16 00:22:14 +0000393 // Computing the address of an external symbol, not calling it.
Evan Cheng4c1aa862006-02-22 20:19:42 +0000394 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattnera637c322005-12-16 00:22:14 +0000395 std::string Name(GlobalPrefix); Name += MO.getSymbolName();
396 GVStubs.insert(Name);
397 O << "L" << Name << "$non_lazy_ptr";
398 return;
399 }
Nate Begeman01d05262005-03-30 01:45:43 +0000400 O << GlobalPrefix << MO.getSymbolName();
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000401 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000402 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera637c322005-12-16 00:22:14 +0000403 // Computing the address of a global symbol, not calling it.
Nate Begemanb73a7112004-08-13 09:32:01 +0000404 GlobalValue *GV = MO.getGlobal();
405 std::string Name = Mang->getValueName(GV);
Nate Begeman50fb3c42005-12-24 01:00:15 +0000406 int offset = MO.getOffset();
Misha Brukmane2eceb52004-07-23 16:08:20 +0000407
Nate Begemanfcf4a422004-10-17 23:01:34 +0000408 // External or weakly linked global variables need non-lazily-resolved stubs
Evan Cheng4c1aa862006-02-22 20:19:42 +0000409 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattnera637c322005-12-16 00:22:14 +0000410 if (((GV->isExternal() || GV->hasWeakLinkage() ||
411 GV->hasLinkOnceLinkage()))) {
Nate Begemand4c8bea2004-11-25 07:09:01 +0000412 GVStubs.insert(Name);
Chris Lattnera637c322005-12-16 00:22:14 +0000413 O << "L" << Name << "$non_lazy_ptr";
414 return;
415 }
Nate Begemanb73a7112004-08-13 09:32:01 +0000416 }
Nate Begemand4c8bea2004-11-25 07:09:01 +0000417
Chris Lattner9542f9712005-11-17 19:40:30 +0000418 O << Name;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000419 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000420 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000421
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000422 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000423 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000424 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000425 }
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.
439 case 'L': // Write second word of DImode reference.
440 // Verify that this operand has two consecutive registers.
441 if (!MI->getOperand(OpNo).isRegister() ||
442 OpNo+1 == MI->getNumOperands() ||
443 !MI->getOperand(OpNo+1).isRegister())
444 return true;
445 ++OpNo; // Return the high-part.
446 break;
447 }
448 }
449
450 printOperand(MI, OpNo);
451 return false;
452}
453
Chris Lattner2c003e22006-02-24 20:27:40 +0000454bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
455 unsigned AsmVariant,
456 const char *ExtraCode) {
457 if (ExtraCode && ExtraCode[0])
458 return true; // Unknown modifier.
459 printMemRegReg(MI, OpNo);
460 return false;
461}
Chris Lattnere3f01572006-02-23 19:31:10 +0000462
Nate Begemane59bf592004-08-14 22:09:10 +0000463/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
464/// the current output stream.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000465///
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000466void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Nate Begemane59bf592004-08-14 22:09:10 +0000467 ++EmittedInsts;
Chris Lattner617742b2005-10-14 22:44:13 +0000468
Nate Begemanad5f65c2005-04-05 18:19:50 +0000469 // Check for slwi/srwi mnemonics.
470 if (MI->getOpcode() == PPC::RLWINM) {
471 bool FoundMnemonic = false;
472 unsigned char SH = MI->getOperand(2).getImmedValue();
473 unsigned char MB = MI->getOperand(3).getImmedValue();
474 unsigned char ME = MI->getOperand(4).getImmedValue();
475 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
476 O << "slwi "; FoundMnemonic = true;
477 }
478 if (SH <= 31 && MB == (32-SH) && ME == 31) {
479 O << "srwi "; FoundMnemonic = true;
480 SH = 32-SH;
481 }
482 if (FoundMnemonic) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000483 printOperand(MI, 0);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000484 O << ", ";
Nate Begeman391c5d22005-11-30 18:54:35 +0000485 printOperand(MI, 1);
Nate Begemanad5f65c2005-04-05 18:19:50 +0000486 O << ", " << (unsigned int)SH << "\n";
487 return;
488 }
Chris Lattner4d73a5a2006-02-08 06:56:40 +0000489 } else if (MI->getOpcode() == PPC::OR4 || MI->getOpcode() == PPC::OR8) {
490 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
491 O << "mr ";
492 printOperand(MI, 0);
493 O << ", ";
494 printOperand(MI, 1);
495 O << "\n";
496 return;
497 }
Nate Begemanad5f65c2005-04-05 18:19:50 +0000498 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000499
Nate Begemane59bf592004-08-14 22:09:10 +0000500 if (printInstruction(MI))
501 return; // Printer was automatically generated
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000502
Nate Begemaned428532004-09-04 05:00:00 +0000503 assert(0 && "Unhandled instruction in asm writer!");
504 abort();
Nate Begemane59bf592004-08-14 22:09:10 +0000505 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000506}
507
Nate Begeman37efe672006-04-22 18:53:45 +0000508void PPCAsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB) const {
509 O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << "_"
510 << MBB->getNumber() << '\t' << CommentString
511 << MBB->getBasicBlock()->getName();
512}
Chris Lattneref658742005-11-21 07:57:37 +0000513
Nate Begemaned428532004-09-04 05:00:00 +0000514/// runOnMachineFunction - This uses the printMachineInstruction()
515/// method to print assembly for each instruction.
516///
517bool DarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000518 // FIXME - is this the earliest this can be set?
Jim Laskeyabf6d172006-01-05 01:25:28 +0000519 DW.SetDebugInfo(&getAnalysis<MachineDebugInfo>());
520
Chris Lattner8b8b9512005-11-21 07:51:23 +0000521 SetupMachineFunction(MF);
Nate Begemaned428532004-09-04 05:00:00 +0000522 O << "\n\n";
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000523
Nate Begemaned428532004-09-04 05:00:00 +0000524 // Print out constants referenced by the function
Chris Lattnerc569e612005-11-21 08:26:15 +0000525 EmitConstantPool(MF.getConstantPool());
Nate Begemaned428532004-09-04 05:00:00 +0000526
Nate Begeman37efe672006-04-22 18:53:45 +0000527 // Print out jump tables referenced by the function
528 EmitJumpTableInfo(MF.getJumpTableInfo());
529
Nate Begemaned428532004-09-04 05:00:00 +0000530 // Print out labels for the function.
Chris Lattnerac7fd7f2005-11-14 18:52:46 +0000531 const Function *F = MF.getFunction();
Chris Lattnera637c322005-12-16 00:22:14 +0000532 switch (F->getLinkage()) {
533 default: assert(0 && "Unknown linkage type!");
534 case Function::InternalLinkage: // Symbols default to internal.
535 SwitchSection(".text", F);
Chris Lattnera637c322005-12-16 00:22:14 +0000536 break;
537 case Function::ExternalLinkage:
538 SwitchSection(".text", F);
Chris Lattnerf02a9162005-10-28 18:44:07 +0000539 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattnera637c322005-12-16 00:22:14 +0000540 break;
541 case Function::WeakLinkage:
542 case Function::LinkOnceLinkage:
543 SwitchSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions",
544 F);
545 O << "\t.globl\t" << CurrentFnName << "\n";
546 O << "\t.weak_definition\t" << CurrentFnName << "\n";
547 break;
548 }
Chris Lattner33d50822006-02-14 20:42:33 +0000549 EmitAlignment(4, F);
Nate Begemaned428532004-09-04 05:00:00 +0000550 O << CurrentFnName << ":\n";
551
Jim Laskey6b92b8e2006-04-07 20:44:42 +0000552 // Emit pre-function debug information.
553 DW.BeginFunction(&MF);
554
Nate Begemaned428532004-09-04 05:00:00 +0000555 // Print out code for the function.
556 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
557 I != E; ++I) {
558 // Print a label for the basic block.
Chris Lattner1db1adb2005-08-21 19:09:33 +0000559 if (I != MF.begin()) {
Chris Lattner07455362005-11-21 08:14:07 +0000560 O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << '_'
Chris Lattner7f9ccde2005-11-21 07:41:05 +0000561 << I->getNumber() << ":\t";
Chris Lattner1db1adb2005-08-21 19:09:33 +0000562 if (!I->getBasicBlock()->getName().empty())
563 O << CommentString << " " << I->getBasicBlock()->getName();
564 O << "\n";
565 }
Nate Begemaned428532004-09-04 05:00:00 +0000566 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
567 II != E; ++II) {
568 // Print the assembly for the instruction.
569 O << "\t";
570 printMachineInstruction(II);
571 }
572 }
Nate Begemaned428532004-09-04 05:00:00 +0000573
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000574 // Emit post-function debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000575 DW.EndFunction();
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000576
Nate Begemaned428532004-09-04 05:00:00 +0000577 // We didn't modify anything.
578 return false;
579}
580
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000581
Nate Begeman18ed0292005-07-21 01:25:49 +0000582bool DarwinAsmPrinter::doInitialization(Module &M) {
Chris Lattner3c304a32005-08-05 22:05:03 +0000583 if (TM.getSubtarget<PPCSubtarget>().isGigaProcessor())
584 O << "\t.machine ppc970\n";
Nate Begeman18ed0292005-07-21 01:25:49 +0000585 AsmPrinter::doInitialization(M);
Chris Lattner85eac0d2005-11-10 19:33:43 +0000586
587 // Darwin wants symbols to be quoted if they have complex names.
588 Mang->setUseQuotes(true);
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000589
590 // Emit initial debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000591 DW.BeginModule(&M);
Nate Begeman18ed0292005-07-21 01:25:49 +0000592 return false;
593}
594
Nate Begemaned428532004-09-04 05:00:00 +0000595bool DarwinAsmPrinter::doFinalization(Module &M) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000596 const TargetData &TD = TM.getTargetData();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000597
598 // Print out module-level global variables here.
Chris Lattnerced704b2005-11-14 19:00:30 +0000599 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Chris Lattnerdeea4162005-12-13 04:33:58 +0000600 I != E; ++I) {
601 if (!I->hasInitializer()) continue; // External global require no code
602
Chris Lattnerd1239b72005-12-13 06:32:50 +0000603 // Check to see if this is a special global used by LLVM, if so, emit it.
Jim Laskey78098112006-03-07 22:00:35 +0000604 if (EmitSpecialLLVMGlobal(I))
Chris Lattnerd1239b72005-12-13 06:32:50 +0000605 continue;
Chris Lattnerdeea4162005-12-13 04:33:58 +0000606
Chris Lattnerdeea4162005-12-13 04:33:58 +0000607 std::string name = Mang->getValueName(I);
608 Constant *C = I->getInitializer();
609 unsigned Size = TD.getTypeSize(C->getType());
Chris Lattner7d8d5a52006-02-05 01:30:45 +0000610 unsigned Align = getPreferredAlignmentLog(I);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000611
612 if (C->isNullValue() && /* FIXME: Verify correct */
613 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
Chris Lattner0d7db6f2006-02-14 22:18:23 +0000614 I->hasLinkOnceLinkage() ||
615 (I->hasExternalLinkage() && !I->hasSection()))) {
Chris Lattnerdeea4162005-12-13 04:33:58 +0000616 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Chris Lattner0d7db6f2006-02-14 22:18:23 +0000617 if (I->hasExternalLinkage()) {
618 O << "\t.globl " << name << '\n';
619 O << "\t.zerofill __DATA, __common, " << name << ", "
620 << Size << ", " << Align;
621 } else if (I->hasInternalLinkage()) {
622 SwitchSection(".data", I);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000623 O << LCOMMDirective << name << "," << Size << "," << Align;
Chris Lattner0d7db6f2006-02-14 22:18:23 +0000624 } else {
625 SwitchSection(".data", I);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000626 O << ".comm " << name << "," << Size;
Chris Lattner0d7db6f2006-02-14 22:18:23 +0000627 }
Chris Lattnerdeea4162005-12-13 04:33:58 +0000628 O << "\t\t; '" << I->getName() << "'\n";
629 } else {
630 switch (I->getLinkage()) {
631 case GlobalValue::LinkOnceLinkage:
Chris Lattnerdeea4162005-12-13 04:33:58 +0000632 case GlobalValue::WeakLinkage:
Chris Lattnercec26fc2005-12-22 21:15:17 +0000633 O << "\t.globl " << name << '\n'
634 << "\t.weak_definition " << name << '\n';
Chris Lattnera637c322005-12-16 00:22:14 +0000635 SwitchSection(".section __DATA,__datacoal_nt,coalesced", I);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000636 break;
637 case GlobalValue::AppendingLinkage:
638 // FIXME: appending linkage variables should go into a section of
639 // their name or something. For now, just emit them as external.
640 case GlobalValue::ExternalLinkage:
641 // If external or appending, declare as a global symbol
642 O << "\t.globl " << name << "\n";
643 // FALL THROUGH
644 case GlobalValue::InternalLinkage:
645 SwitchSection(".data", I);
646 break;
647 default:
648 std::cerr << "Unknown linkage type!";
649 abort();
650 }
651
652 EmitAlignment(Align, I);
653 O << name << ":\t\t\t\t; '" << I->getName() << "'\n";
654 EmitGlobalConstant(C);
Chris Lattnerbc1c2152006-01-21 01:35:26 +0000655 O << '\n';
Chris Lattnerdeea4162005-12-13 04:33:58 +0000656 }
657 }
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000658
659 // Output stubs for dynamically-linked functions
Evan Cheng4c1aa862006-02-22 20:19:42 +0000660 if (TM.getRelocationModel() == Reloc::PIC) {
Chris Lattnerdeea4162005-12-13 04:33:58 +0000661 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
662 i != e; ++i) {
Chris Lattnera637c322005-12-16 00:22:14 +0000663 SwitchSection(".section __TEXT,__picsymbolstub1,symbol_stubs,"
664 "pure_instructions,32", 0);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000665 EmitAlignment(2);
666 O << "L" << *i << "$stub:\n";
667 O << "\t.indirect_symbol " << *i << "\n";
668 O << "\tmflr r0\n";
669 O << "\tbcl 20,31,L0$" << *i << "\n";
670 O << "L0$" << *i << ":\n";
671 O << "\tmflr r11\n";
672 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
673 O << "\tmtlr r0\n";
674 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
675 O << "\tmtctr r12\n";
676 O << "\tbctr\n";
Chris Lattnera637c322005-12-16 00:22:14 +0000677 SwitchSection(".lazy_symbol_pointer", 0);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000678 O << "L" << *i << "$lazy_ptr:\n";
679 O << "\t.indirect_symbol " << *i << "\n";
680 O << "\t.long dyld_stub_binding_helper\n";
681 }
682 } else {
683 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
684 i != e; ++i) {
Chris Lattnera637c322005-12-16 00:22:14 +0000685 SwitchSection(".section __TEXT,__symbol_stub1,symbol_stubs,"
686 "pure_instructions,16", 0);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000687 EmitAlignment(4);
688 O << "L" << *i << "$stub:\n";
689 O << "\t.indirect_symbol " << *i << "\n";
690 O << "\tlis r11,ha16(L" << *i << "$lazy_ptr)\n";
691 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr)(r11)\n";
692 O << "\tmtctr r12\n";
693 O << "\tbctr\n";
Chris Lattnera637c322005-12-16 00:22:14 +0000694 SwitchSection(".lazy_symbol_pointer", 0);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000695 O << "L" << *i << "$lazy_ptr:\n";
696 O << "\t.indirect_symbol " << *i << "\n";
697 O << "\t.long dyld_stub_binding_helper\n";
Nate Begeman2497e632005-07-21 20:44:43 +0000698 }
Misha Brukman46fd00a2004-06-24 23:04:11 +0000699 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000700
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000701 O << "\n";
702
Chris Lattnera637c322005-12-16 00:22:14 +0000703 // Output stubs for external and common global variables.
704 if (GVStubs.begin() != GVStubs.end()) {
705 SwitchSection(".non_lazy_symbol_pointer", 0);
706 for (std::set<std::string>::iterator I = GVStubs.begin(),
707 E = GVStubs.end(); I != E; ++I) {
708 O << "L" << *I << "$non_lazy_ptr:\n";
709 O << "\t.indirect_symbol " << *I << "\n";
710 O << "\t.long\t0\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +0000711 }
Nate Begemane59bf592004-08-14 22:09:10 +0000712 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000713
Jim Laskey063e7652006-01-17 17:31:53 +0000714 // Emit initial debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000715 DW.EndModule();
Jim Laskey063e7652006-01-17 17:31:53 +0000716
Chris Lattner5b0ac992005-11-01 00:12:36 +0000717 // Funny Darwin hack: This flag tells the linker that no global symbols
718 // contain code that falls through to other global symbols (e.g. the obvious
719 // implementation of multiple entry points). If this doesn't occur, the
720 // linker can safely perform dead code stripping. Since LLVM never generates
721 // code that does this, it is always safe to set.
722 O << "\t.subsections_via_symbols\n";
723
Chris Lattnera3840792004-08-16 23:25:21 +0000724 AsmPrinter::doFinalization(M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000725 return false; // success
726}
Nate Begemaned428532004-09-04 05:00:00 +0000727
Jim Laskeyb2efb852006-01-04 22:28:25 +0000728/// runOnMachineFunction - This uses the e()
Nate Begemaned428532004-09-04 05:00:00 +0000729/// method to print assembly for each instruction.
730///
731bool AIXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner6d5a4f62005-11-21 08:02:41 +0000732 SetupMachineFunction(MF);
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000733
Nate Begemaned428532004-09-04 05:00:00 +0000734 // Print out constants referenced by the function
Chris Lattnerc569e612005-11-21 08:26:15 +0000735 EmitConstantPool(MF.getConstantPool());
Nate Begemaned428532004-09-04 05:00:00 +0000736
737 // Print out header for the function.
738 O << "\t.csect .text[PR]\n"
739 << "\t.align 2\n"
740 << "\t.globl " << CurrentFnName << '\n'
741 << "\t.globl ." << CurrentFnName << '\n'
742 << "\t.csect " << CurrentFnName << "[DS],3\n"
743 << CurrentFnName << ":\n"
744 << "\t.llong ." << CurrentFnName << ", TOC[tc0], 0\n"
745 << "\t.csect .text[PR]\n"
746 << '.' << CurrentFnName << ":\n";
747
748 // Print out code for the function.
749 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
750 I != E; ++I) {
751 // Print a label for the basic block.
Chris Lattner07455362005-11-21 08:14:07 +0000752 O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << '_'
753 << I->getNumber()
Chris Lattner6d5a4f62005-11-21 08:02:41 +0000754 << ":\t" << CommentString << I->getBasicBlock()->getName() << '\n';
Nate Begemaned428532004-09-04 05:00:00 +0000755 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
756 II != E; ++II) {
757 // Print the assembly for the instruction.
758 O << "\t";
759 printMachineInstruction(II);
760 }
761 }
Nate Begemaned428532004-09-04 05:00:00 +0000762
763 O << "LT.." << CurrentFnName << ":\n"
764 << "\t.long 0\n"
765 << "\t.byte 0,0,32,65,128,0,0,0\n"
766 << "\t.long LT.." << CurrentFnName << "-." << CurrentFnName << '\n'
767 << "\t.short 3\n"
768 << "\t.byte \"" << CurrentFnName << "\"\n"
769 << "\t.align 2\n";
770
771 // We didn't modify anything.
772 return false;
773}
774
Nate Begemaned428532004-09-04 05:00:00 +0000775bool AIXAsmPrinter::doInitialization(Module &M) {
Chris Lattnerac7fd7f2005-11-14 18:52:46 +0000776 SwitchSection("", 0);
Nate Begemaned428532004-09-04 05:00:00 +0000777 const TargetData &TD = TM.getTargetData();
Nate Begemaned428532004-09-04 05:00:00 +0000778
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000779 O << "\t.machine \"ppc64\"\n"
Nate Begemaned428532004-09-04 05:00:00 +0000780 << "\t.toc\n"
781 << "\t.csect .text[PR]\n";
782
783 // Print out module-level global variables
Chris Lattner2e00d7d2005-07-26 19:03:27 +0000784 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
785 I != E; ++I) {
Nate Begemaned428532004-09-04 05:00:00 +0000786 if (!I->hasInitializer())
787 continue;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000788
Nate Begemaned428532004-09-04 05:00:00 +0000789 std::string Name = I->getName();
790 Constant *C = I->getInitializer();
791 // N.B.: We are defaulting to writable strings
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000792 if (I->hasExternalLinkage()) {
Nate Begemaned428532004-09-04 05:00:00 +0000793 O << "\t.globl " << Name << '\n'
794 << "\t.csect .data[RW],3\n";
795 } else {
796 O << "\t.csect _global.rw_c[RW],3\n";
797 }
798 O << Name << ":\n";
Chris Lattner8b8b9512005-11-21 07:51:23 +0000799 EmitGlobalConstant(C);
Nate Begemaned428532004-09-04 05:00:00 +0000800 }
801
802 // Output labels for globals
Chris Lattnere4d5c442005-03-15 04:54:21 +0000803 if (M.global_begin() != M.global_end()) O << "\t.toc\n";
Chris Lattner2e00d7d2005-07-26 19:03:27 +0000804 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
805 I != E; ++I) {
Nate Begemaned428532004-09-04 05:00:00 +0000806 const GlobalVariable *GV = I;
807 // Do not output labels for unused variables
808 if (GV->isExternal() && GV->use_begin() == GV->use_end())
809 continue;
810
Chris Lattner07455362005-11-21 08:14:07 +0000811 IncrementFunctionNumber();
Nate Begemaned428532004-09-04 05:00:00 +0000812 std::string Name = GV->getName();
Chris Lattner07455362005-11-21 08:14:07 +0000813 std::string Label = "LC.." + utostr(getFunctionNumber());
Nate Begemaned428532004-09-04 05:00:00 +0000814 GVToLabelMap[GV] = Label;
815 O << Label << ":\n"
816 << "\t.tc " << Name << "[TC]," << Name;
817 if (GV->isExternal()) O << "[RW]";
818 O << '\n';
Chris Lattner07455362005-11-21 08:14:07 +0000819 }
Nate Begemaned428532004-09-04 05:00:00 +0000820
Chris Lattner3459bfb2005-11-10 18:20:29 +0000821 AsmPrinter::doInitialization(M);
Nate Begemaned428532004-09-04 05:00:00 +0000822 return false; // success
823}
824
825bool AIXAsmPrinter::doFinalization(Module &M) {
826 const TargetData &TD = TM.getTargetData();
827 // Print out module-level global variables
Chris Lattner2e00d7d2005-07-26 19:03:27 +0000828 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
829 I != E; ++I) {
Nate Begemaned428532004-09-04 05:00:00 +0000830 if (I->hasInitializer() || I->hasExternalLinkage())
831 continue;
832
833 std::string Name = I->getName();
834 if (I->hasInternalLinkage()) {
835 O << "\t.lcomm " << Name << ",16,_global.bss_c";
836 } else {
837 O << "\t.comm " << Name << "," << TD.getTypeSize(I->getType())
Chris Lattner0561b3f2005-08-02 19:26:06 +0000838 << "," << Log2_32((unsigned)TD.getTypeAlignment(I->getType()));
Nate Begemaned428532004-09-04 05:00:00 +0000839 }
Chris Lattner6d5a4f62005-11-21 08:02:41 +0000840 O << "\t\t" << CommentString << " ";
Chris Lattner8ca02912005-10-03 07:08:36 +0000841 WriteAsOperand(O, I, false, true, &M);
Nate Begemaned428532004-09-04 05:00:00 +0000842 O << "\n";
843 }
844
845 O << "_section_.text:\n"
846 << "\t.csect .data[RW],3\n"
847 << "\t.llong _section_.text\n";
Chris Lattner3459bfb2005-11-10 18:20:29 +0000848 AsmPrinter::doFinalization(M);
Nate Begemaned428532004-09-04 05:00:00 +0000849 return false; // success
850}