blob: 714a36d1e7df339f2681298ddc7eb39374178c5c [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 Lattner815ef5b2006-02-06 22:18:19 +0000100 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner58873272006-02-01 22:38:46 +0000101 printOperand(MI, OpNo);
102 return false;
103 }
104
Nate Begeman391c5d22005-11-30 18:54:35 +0000105 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begemanc3306122004-08-21 05:56:39 +0000106 unsigned char value = MI->getOperand(OpNo).getImmedValue();
Chris Lattner12585ba2004-08-21 19:11:03 +0000107 assert(value <= 31 && "Invalid u5imm argument!");
Nate Begemanc3306122004-08-21 05:56:39 +0000108 O << (unsigned int)value;
109 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000110 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman07aada82004-08-30 02:28:06 +0000111 unsigned char value = MI->getOperand(OpNo).getImmedValue();
112 assert(value <= 63 && "Invalid u6imm argument!");
113 O << (unsigned int)value;
114 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000115 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begemaned428532004-09-04 05:00:00 +0000116 O << (short)MI->getOperand(OpNo).getImmedValue();
117 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000118 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner97b2a2e2004-08-15 05:20:16 +0000119 O << (unsigned short)MI->getOperand(OpNo).getImmedValue();
120 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000121 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner841d12d2005-10-18 16:51:22 +0000122 O << (short)MI->getOperand(OpNo).getImmedValue()*4;
123 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000124 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begemaned428532004-09-04 05:00:00 +0000125 // Branches can take an immediate operand. This is used by the branch
126 // selection pass to print $+8, an eight byte displacement from the PC.
127 if (MI->getOperand(OpNo).isImmediate()) {
Nate Begeman27499e32005-04-10 01:48:29 +0000128 O << "$+" << MI->getOperand(OpNo).getImmedValue();
Nate Begemaned428532004-09-04 05:00:00 +0000129 } else {
Chris Lattner3e7f86a2005-11-17 19:16:08 +0000130 printOp(MI->getOperand(OpNo));
Nate Begemaned428532004-09-04 05:00:00 +0000131 }
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000132 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000133 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9ba13e42005-11-17 19:25:59 +0000134 const MachineOperand &MO = MI->getOperand(OpNo);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000135 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattnera637c322005-12-16 00:22:14 +0000136 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
137 GlobalValue *GV = MO.getGlobal();
138 if (((GV->isExternal() || GV->hasWeakLinkage() ||
139 GV->hasLinkOnceLinkage()))) {
140 // Dynamically-resolved functions need a stub for the function.
141 std::string Name = Mang->getValueName(GV);
142 FnStubs.insert(Name);
143 O << "L" << Name << "$stub";
144 return;
145 }
146 }
Chris Lattner9542f9712005-11-17 19:40:30 +0000147 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
148 std::string Name(GlobalPrefix); Name += MO.getSymbolName();
149 FnStubs.insert(Name);
150 O << "L" << Name << "$stub";
151 return;
Chris Lattner9542f9712005-11-17 19:40:30 +0000152 }
Chris Lattner9ba13e42005-11-17 19:25:59 +0000153 }
Chris Lattner9542f9712005-11-17 19:40:30 +0000154
155 printOp(MI->getOperand(OpNo));
Chris Lattner3e7f86a2005-11-17 19:16:08 +0000156 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000157 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman422b0ce2005-11-16 00:48:01 +0000158 O << (int)MI->getOperand(OpNo).getImmedValue()*4;
159 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000160 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera637c322005-12-16 00:22:14 +0000161 O << "\"L" << getFunctionNumber() << "$pb\"\n";
162 O << "\"L" << getFunctionNumber() << "$pb\":";
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000163 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000164 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman2497e632005-07-21 20:44:43 +0000165 if (MI->getOperand(OpNo).isImmediate()) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000166 printS16ImmOperand(MI, OpNo);
Nate Begeman2497e632005-07-21 20:44:43 +0000167 } else {
168 O << "ha16(";
169 printOp(MI->getOperand(OpNo));
Evan Cheng4c1aa862006-02-22 20:19:42 +0000170 if (TM.getRelocationModel() == Reloc::PIC)
Chris Lattnera637c322005-12-16 00:22:14 +0000171 O << "-\"L" << getFunctionNumber() << "$pb\")";
Nate Begeman2497e632005-07-21 20:44:43 +0000172 else
173 O << ')';
174 }
Nate Begemaned428532004-09-04 05:00:00 +0000175 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000176 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Nate Begemaned428532004-09-04 05:00:00 +0000177 if (MI->getOperand(OpNo).isImmediate()) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000178 printS16ImmOperand(MI, OpNo);
Nate Begemaned428532004-09-04 05:00:00 +0000179 } else {
180 O << "lo16(";
Nate Begemand4c8bea2004-11-25 07:09:01 +0000181 printOp(MI->getOperand(OpNo));
Evan Cheng4c1aa862006-02-22 20:19:42 +0000182 if (TM.getRelocationModel() == Reloc::PIC)
Chris Lattnera637c322005-12-16 00:22:14 +0000183 O << "-\"L" << getFunctionNumber() << "$pb\")";
Nate Begeman2497e632005-07-21 20:44:43 +0000184 else
185 O << ')';
Nate Begemaned428532004-09-04 05:00:00 +0000186 }
187 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000188 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
Nate Begemanadeb43d2005-07-20 22:42:00 +0000189 unsigned CCReg = MI->getOperand(OpNo).getReg();
190 unsigned RegNo = enumRegToMachineReg(CCReg);
191 O << (0x80 >> RegNo);
192 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000193 // The new addressing mode printers, currently empty
194 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
195 printSymbolLo(MI, OpNo);
196 O << '(';
197 printOperand(MI, OpNo+1);
198 O << ')';
199 }
200 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman88276b82005-12-19 23:40:42 +0000201 // When used as the base register, r0 reads constant zero rather than
202 // the value contained in the register. For this reason, the darwin
203 // assembler requires that we print r0 as 0 (no r) when used as the base.
204 const MachineOperand &MO = MI->getOperand(OpNo);
205 if (MO.getReg() == PPC::R0)
206 O << '0';
207 else
208 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000209 O << ", ";
210 printOperand(MI, OpNo+1);
211 }
212
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000213 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
Nate Begemaned428532004-09-04 05:00:00 +0000214 virtual bool doFinalization(Module &M) = 0;
Jim Laskeyb2efb852006-01-04 22:28:25 +0000215
Nate Begemaned428532004-09-04 05:00:00 +0000216 };
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000217
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000218 /// DarwinDwarfWriter - Dwarf debug info writer customized for Darwin/Mac OS X
219 ///
220 struct DarwinDwarfWriter : public DwarfWriter {
221 // Ctor.
Jim Laskeyb2efb852006-01-04 22:28:25 +0000222 DarwinDwarfWriter(std::ostream &o, AsmPrinter *ap)
223 : DwarfWriter(o, ap)
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000224 {
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000225 needsSet = true;
Jim Laskey0420f2a2006-02-22 19:02:11 +0000226 DwarfAbbrevSection = ".section __DWARFA,__debug_abbrev";
227 DwarfInfoSection = ".section __DWARFA,__debug_info";
228 DwarfLineSection = ".section __DWARFA,__debug_line";
229 DwarfFrameSection = ".section __DWARFA,__debug_frame";
230 DwarfPubNamesSection = ".section __DWARFA,__debug_pubnames";
231 DwarfPubTypesSection = ".section __DWARFA,__debug_pubtypes";
232 DwarfStrSection = ".section __DWARFA,__debug_str";
233 DwarfLocSection = ".section __DWARFA,__debug_loc";
234 DwarfARangesSection = ".section __DWARFA,__debug_aranges";
235 DwarfRangesSection = ".section __DWARFA,__debug_ranges";
236 DwarfMacInfoSection = ".section __DWARFA,__debug_macinfo";
Jim Laskey063e7652006-01-17 17:31:53 +0000237 TextSection = ".text";
238 DataSection = ".data";
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000239 }
240 };
241
Misha Brukman3e0b51a2004-09-05 02:42:44 +0000242 /// DarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac OS
243 /// X
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000244 struct DarwinAsmPrinter : public PPCAsmPrinter {
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000245
246 DarwinDwarfWriter DW;
Nate Begemaned428532004-09-04 05:00:00 +0000247
248 DarwinAsmPrinter(std::ostream &O, TargetMachine &TM)
Chris Lattner418caa12006-02-08 23:42:22 +0000249 : PPCAsmPrinter(O, TM), DW(O, this) {
Nate Begemaned428532004-09-04 05:00:00 +0000250 CommentString = ";";
251 GlobalPrefix = "_";
Chris Lattnerf55366e2005-11-21 06:47:58 +0000252 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
Nate Begemaned428532004-09-04 05:00:00 +0000253 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
254 Data64bitsDirective = 0; // we can't emit a 64-bit unit
255 AlignmentIsInBytes = false; // Alignment is by power of 2.
Chris Lattnerc569e612005-11-21 08:26:15 +0000256 ConstantPoolSection = "\t.const\t";
Chris Lattnerd717b192005-12-11 07:45:47 +0000257 LCOMMDirective = "\t.lcomm\t";
Chris Lattnerd1239b72005-12-13 06:32:50 +0000258 StaticCtorsSection = ".mod_init_func";
259 StaticDtorsSection = ".mod_term_func";
Chris Lattner418caa12006-02-08 23:42:22 +0000260 InlineAsmStart = InlineAsmEnd = ""; // Don't use #APP/#NO_APP
Nate Begemaned428532004-09-04 05:00:00 +0000261 }
262
263 virtual const char *getPassName() const {
264 return "Darwin PPC Assembly Printer";
265 }
Chris Lattner646f7af2005-12-09 18:24:29 +0000266
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000267 bool runOnMachineFunction(MachineFunction &F);
Nate Begeman18ed0292005-07-21 01:25:49 +0000268 bool doInitialization(Module &M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000269 bool doFinalization(Module &M);
Jim Laskeyb2efb852006-01-04 22:28:25 +0000270
271 void getAnalysisUsage(AnalysisUsage &AU) const {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000272 AU.setPreservesAll();
Jim Laskeyb2efb852006-01-04 22:28:25 +0000273 AU.addRequired<MachineDebugInfo>();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000274 PPCAsmPrinter::getAnalysisUsage(AU);
Jim Laskeyb2efb852006-01-04 22:28:25 +0000275 }
276
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000277 };
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000278
Misha Brukman3e0b51a2004-09-05 02:42:44 +0000279 /// AIXAsmPrinter - PowerPC assembly printer, customized for AIX
280 ///
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000281 struct AIXAsmPrinter : public PPCAsmPrinter {
Nate Begemaned428532004-09-04 05:00:00 +0000282 /// Map for labels corresponding to global variables
283 ///
284 std::map<const GlobalVariable*,std::string> GVToLabelMap;
285
286 AIXAsmPrinter(std::ostream &O, TargetMachine &TM)
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000287 : PPCAsmPrinter(O, TM) {
Nate Begemaned428532004-09-04 05:00:00 +0000288 CommentString = "#";
Chris Lattner3459bfb2005-11-10 18:20:29 +0000289 GlobalPrefix = ".";
Nate Begemaned428532004-09-04 05:00:00 +0000290 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
291 Data64bitsDirective = 0; // we can't emit a 64-bit unit
292 AlignmentIsInBytes = false; // Alignment is by power of 2.
Chris Lattnerc569e612005-11-21 08:26:15 +0000293 ConstantPoolSection = "\t.const\t";
Nate Begemaned428532004-09-04 05:00:00 +0000294 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000295
Nate Begemaned428532004-09-04 05:00:00 +0000296 virtual const char *getPassName() const {
297 return "AIX PPC Assembly Printer";
298 }
299
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000300 bool runOnMachineFunction(MachineFunction &F);
Nate Begemaned428532004-09-04 05:00:00 +0000301 bool doInitialization(Module &M);
302 bool doFinalization(Module &M);
303 };
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000304} // end of anonymous namespace
305
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000306/// createDarwinAsmPrinterPass - Returns a pass that prints the PPC assembly
307/// code for a MachineFunction to the given output stream, in a format that the
Nate Begemaned428532004-09-04 05:00:00 +0000308/// Darwin assembler can deal with.
309///
310FunctionPass *llvm::createDarwinAsmPrinter(std::ostream &o, TargetMachine &tm) {
311 return new DarwinAsmPrinter(o, tm);
312}
313
314/// createAIXAsmPrinterPass - Returns a pass that prints the PPC assembly code
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000315/// for a MachineFunction to the given output stream, in a format that the
Nate Begemaned428532004-09-04 05:00:00 +0000316/// AIX 5L assembler can deal with.
317///
318FunctionPass *llvm::createAIXAsmPrinter(std::ostream &o, TargetMachine &tm) {
319 return new AIXAsmPrinter(o, tm);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000320}
321
Nate Begemane59bf592004-08-14 22:09:10 +0000322// Include the auto-generated portion of the assembly writer
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000323#include "PPCGenAsmWriter.inc"
Nate Begemane59bf592004-08-14 22:09:10 +0000324
Chris Lattner9ba13e42005-11-17 19:25:59 +0000325void PPCAsmPrinter::printOp(const MachineOperand &MO) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000326 const MRegisterInfo &RI = *TM.getRegisterInfo();
327 int new_symbol;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000328
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000329 switch (MO.getType()) {
330 case MachineOperand::MO_VirtualRegister:
331 if (Value *V = MO.getVRegValueOrNull()) {
332 O << "<" << V->getName() << ">";
333 return;
334 }
335 // FALLTHROUGH
336 case MachineOperand::MO_MachineRegister:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000337 case MachineOperand::MO_CCRegister:
Chris Lattner9dc4d3c2005-08-22 22:00:02 +0000338 O << RI.get(MO.getReg()).Name;
Misha Brukman7f484a52004-06-24 23:51:00 +0000339 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000340
341 case MachineOperand::MO_SignExtendedImmed:
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000342 case MachineOperand::MO_UnextendedImmed:
343 std::cerr << "printOp() does not handle immediate values\n";
344 abort();
Misha Brukman97a296f2004-07-21 20:11:11 +0000345 return;
346
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000347 case MachineOperand::MO_PCRelativeDisp:
348 std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
349 abort();
350 return;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000351
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000352 case MachineOperand::MO_MachineBasicBlock: {
353 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
Chris Lattner07455362005-11-21 08:14:07 +0000354 O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << "_"
Chris Lattner7f9ccde2005-11-21 07:41:05 +0000355 << MBBOp->getNumber() << "\t; " << MBBOp->getBasicBlock()->getName();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000356 return;
357 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000358
359 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner07455362005-11-21 08:14:07 +0000360 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber()
Chris Lattnerf55366e2005-11-21 06:47:58 +0000361 << '_' << MO.getConstantPoolIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000362 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000363 case MachineOperand::MO_ExternalSymbol:
Chris Lattnera637c322005-12-16 00:22:14 +0000364 // Computing the address of an external symbol, not calling it.
Evan Cheng4c1aa862006-02-22 20:19:42 +0000365 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattnera637c322005-12-16 00:22:14 +0000366 std::string Name(GlobalPrefix); Name += MO.getSymbolName();
367 GVStubs.insert(Name);
368 O << "L" << Name << "$non_lazy_ptr";
369 return;
370 }
Nate Begeman01d05262005-03-30 01:45:43 +0000371 O << GlobalPrefix << MO.getSymbolName();
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000372 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000373 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera637c322005-12-16 00:22:14 +0000374 // Computing the address of a global symbol, not calling it.
Nate Begemanb73a7112004-08-13 09:32:01 +0000375 GlobalValue *GV = MO.getGlobal();
376 std::string Name = Mang->getValueName(GV);
Nate Begeman50fb3c42005-12-24 01:00:15 +0000377 int offset = MO.getOffset();
Misha Brukmane2eceb52004-07-23 16:08:20 +0000378
Nate Begemanfcf4a422004-10-17 23:01:34 +0000379 // External or weakly linked global variables need non-lazily-resolved stubs
Evan Cheng4c1aa862006-02-22 20:19:42 +0000380 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattnera637c322005-12-16 00:22:14 +0000381 if (((GV->isExternal() || GV->hasWeakLinkage() ||
382 GV->hasLinkOnceLinkage()))) {
Nate Begemand4c8bea2004-11-25 07:09:01 +0000383 GVStubs.insert(Name);
Chris Lattnera637c322005-12-16 00:22:14 +0000384 O << "L" << Name << "$non_lazy_ptr";
385 return;
386 }
Nate Begemanb73a7112004-08-13 09:32:01 +0000387 }
Nate Begemand4c8bea2004-11-25 07:09:01 +0000388
Chris Lattner9542f9712005-11-17 19:40:30 +0000389 O << Name;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000390 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000391 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000392
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000393 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000394 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000395 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000396 }
397}
398
Nate Begemane59bf592004-08-14 22:09:10 +0000399/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
400/// the current output stream.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000401///
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000402void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Nate Begemane59bf592004-08-14 22:09:10 +0000403 ++EmittedInsts;
Chris Lattner617742b2005-10-14 22:44:13 +0000404
Nate Begemanad5f65c2005-04-05 18:19:50 +0000405 // Check for slwi/srwi mnemonics.
406 if (MI->getOpcode() == PPC::RLWINM) {
407 bool FoundMnemonic = false;
408 unsigned char SH = MI->getOperand(2).getImmedValue();
409 unsigned char MB = MI->getOperand(3).getImmedValue();
410 unsigned char ME = MI->getOperand(4).getImmedValue();
411 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
412 O << "slwi "; FoundMnemonic = true;
413 }
414 if (SH <= 31 && MB == (32-SH) && ME == 31) {
415 O << "srwi "; FoundMnemonic = true;
416 SH = 32-SH;
417 }
418 if (FoundMnemonic) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000419 printOperand(MI, 0);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000420 O << ", ";
Nate Begeman391c5d22005-11-30 18:54:35 +0000421 printOperand(MI, 1);
Nate Begemanad5f65c2005-04-05 18:19:50 +0000422 O << ", " << (unsigned int)SH << "\n";
423 return;
424 }
Chris Lattner4d73a5a2006-02-08 06:56:40 +0000425 } else if (MI->getOpcode() == PPC::OR4 || MI->getOpcode() == PPC::OR8) {
426 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
427 O << "mr ";
428 printOperand(MI, 0);
429 O << ", ";
430 printOperand(MI, 1);
431 O << "\n";
432 return;
433 }
Nate Begemanad5f65c2005-04-05 18:19:50 +0000434 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000435
Nate Begemane59bf592004-08-14 22:09:10 +0000436 if (printInstruction(MI))
437 return; // Printer was automatically generated
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000438
Nate Begemaned428532004-09-04 05:00:00 +0000439 assert(0 && "Unhandled instruction in asm writer!");
440 abort();
Nate Begemane59bf592004-08-14 22:09:10 +0000441 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000442}
443
Chris Lattneref658742005-11-21 07:57:37 +0000444
Nate Begemaned428532004-09-04 05:00:00 +0000445/// runOnMachineFunction - This uses the printMachineInstruction()
446/// method to print assembly for each instruction.
447///
448bool DarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000449 // FIXME - is this the earliest this can be set?
Jim Laskeyabf6d172006-01-05 01:25:28 +0000450 DW.SetDebugInfo(&getAnalysis<MachineDebugInfo>());
451
Chris Lattner8b8b9512005-11-21 07:51:23 +0000452 SetupMachineFunction(MF);
Nate Begemaned428532004-09-04 05:00:00 +0000453 O << "\n\n";
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000454
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000455 // Emit pre-function debug information.
Jim Laskey52060a02006-01-24 00:49:18 +0000456 DW.BeginFunction(MF);
Nate Begemaned428532004-09-04 05:00:00 +0000457
458 // Print out constants referenced by the function
Chris Lattnerc569e612005-11-21 08:26:15 +0000459 EmitConstantPool(MF.getConstantPool());
Nate Begemaned428532004-09-04 05:00:00 +0000460
461 // Print out labels for the function.
Chris Lattnerac7fd7f2005-11-14 18:52:46 +0000462 const Function *F = MF.getFunction();
Chris Lattnera637c322005-12-16 00:22:14 +0000463 switch (F->getLinkage()) {
464 default: assert(0 && "Unknown linkage type!");
465 case Function::InternalLinkage: // Symbols default to internal.
466 SwitchSection(".text", F);
Chris Lattnera637c322005-12-16 00:22:14 +0000467 break;
468 case Function::ExternalLinkage:
469 SwitchSection(".text", F);
Chris Lattnerf02a9162005-10-28 18:44:07 +0000470 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattnera637c322005-12-16 00:22:14 +0000471 break;
472 case Function::WeakLinkage:
473 case Function::LinkOnceLinkage:
474 SwitchSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions",
475 F);
476 O << "\t.globl\t" << CurrentFnName << "\n";
477 O << "\t.weak_definition\t" << CurrentFnName << "\n";
478 break;
479 }
Chris Lattner33d50822006-02-14 20:42:33 +0000480 EmitAlignment(4, F);
Nate Begemaned428532004-09-04 05:00:00 +0000481 O << CurrentFnName << ":\n";
482
483 // Print out code for the function.
484 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
485 I != E; ++I) {
486 // Print a label for the basic block.
Chris Lattner1db1adb2005-08-21 19:09:33 +0000487 if (I != MF.begin()) {
Chris Lattner07455362005-11-21 08:14:07 +0000488 O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << '_'
Chris Lattner7f9ccde2005-11-21 07:41:05 +0000489 << I->getNumber() << ":\t";
Chris Lattner1db1adb2005-08-21 19:09:33 +0000490 if (!I->getBasicBlock()->getName().empty())
491 O << CommentString << " " << I->getBasicBlock()->getName();
492 O << "\n";
493 }
Nate Begemaned428532004-09-04 05:00:00 +0000494 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
495 II != E; ++II) {
496 // Print the assembly for the instruction.
497 O << "\t";
498 printMachineInstruction(II);
499 }
500 }
Nate Begemaned428532004-09-04 05:00:00 +0000501
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000502 // Emit post-function debug information.
Jim Laskey52060a02006-01-24 00:49:18 +0000503 DW.EndFunction(MF);
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000504
Nate Begemaned428532004-09-04 05:00:00 +0000505 // We didn't modify anything.
506 return false;
507}
508
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000509
Nate Begeman18ed0292005-07-21 01:25:49 +0000510bool DarwinAsmPrinter::doInitialization(Module &M) {
Chris Lattner3c304a32005-08-05 22:05:03 +0000511 if (TM.getSubtarget<PPCSubtarget>().isGigaProcessor())
512 O << "\t.machine ppc970\n";
Nate Begeman18ed0292005-07-21 01:25:49 +0000513 AsmPrinter::doInitialization(M);
Chris Lattner85eac0d2005-11-10 19:33:43 +0000514
515 // Darwin wants symbols to be quoted if they have complex names.
516 Mang->setUseQuotes(true);
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000517
518 // Emit initial debug information.
Jim Laskey52060a02006-01-24 00:49:18 +0000519 DW.BeginModule(M);
Nate Begeman18ed0292005-07-21 01:25:49 +0000520 return false;
521}
522
Nate Begemaned428532004-09-04 05:00:00 +0000523bool DarwinAsmPrinter::doFinalization(Module &M) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000524 const TargetData &TD = TM.getTargetData();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000525
526 // Print out module-level global variables here.
Chris Lattnerced704b2005-11-14 19:00:30 +0000527 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Chris Lattnerdeea4162005-12-13 04:33:58 +0000528 I != E; ++I) {
529 if (!I->hasInitializer()) continue; // External global require no code
530
Chris Lattnerd1239b72005-12-13 06:32:50 +0000531 // Check to see if this is a special global used by LLVM, if so, emit it.
532 if (I->hasAppendingLinkage() && EmitSpecialLLVMGlobal(I))
533 continue;
Chris Lattnerdeea4162005-12-13 04:33:58 +0000534
Chris Lattnerdeea4162005-12-13 04:33:58 +0000535 std::string name = Mang->getValueName(I);
536 Constant *C = I->getInitializer();
537 unsigned Size = TD.getTypeSize(C->getType());
Chris Lattner7d8d5a52006-02-05 01:30:45 +0000538 unsigned Align = getPreferredAlignmentLog(I);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000539
540 if (C->isNullValue() && /* FIXME: Verify correct */
541 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
Chris Lattner0d7db6f2006-02-14 22:18:23 +0000542 I->hasLinkOnceLinkage() ||
543 (I->hasExternalLinkage() && !I->hasSection()))) {
Chris Lattnerdeea4162005-12-13 04:33:58 +0000544 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Chris Lattner0d7db6f2006-02-14 22:18:23 +0000545 if (I->hasExternalLinkage()) {
546 O << "\t.globl " << name << '\n';
547 O << "\t.zerofill __DATA, __common, " << name << ", "
548 << Size << ", " << Align;
549 } else if (I->hasInternalLinkage()) {
550 SwitchSection(".data", I);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000551 O << LCOMMDirective << name << "," << Size << "," << Align;
Chris Lattner0d7db6f2006-02-14 22:18:23 +0000552 } else {
553 SwitchSection(".data", I);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000554 O << ".comm " << name << "," << Size;
Chris Lattner0d7db6f2006-02-14 22:18:23 +0000555 }
Chris Lattnerdeea4162005-12-13 04:33:58 +0000556 O << "\t\t; '" << I->getName() << "'\n";
557 } else {
558 switch (I->getLinkage()) {
559 case GlobalValue::LinkOnceLinkage:
Chris Lattnerdeea4162005-12-13 04:33:58 +0000560 case GlobalValue::WeakLinkage:
Chris Lattnercec26fc2005-12-22 21:15:17 +0000561 O << "\t.globl " << name << '\n'
562 << "\t.weak_definition " << name << '\n';
Chris Lattnera637c322005-12-16 00:22:14 +0000563 SwitchSection(".section __DATA,__datacoal_nt,coalesced", I);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000564 break;
565 case GlobalValue::AppendingLinkage:
566 // FIXME: appending linkage variables should go into a section of
567 // their name or something. For now, just emit them as external.
568 case GlobalValue::ExternalLinkage:
569 // If external or appending, declare as a global symbol
570 O << "\t.globl " << name << "\n";
571 // FALL THROUGH
572 case GlobalValue::InternalLinkage:
573 SwitchSection(".data", I);
574 break;
575 default:
576 std::cerr << "Unknown linkage type!";
577 abort();
578 }
579
580 EmitAlignment(Align, I);
581 O << name << ":\t\t\t\t; '" << I->getName() << "'\n";
582 EmitGlobalConstant(C);
Chris Lattnerbc1c2152006-01-21 01:35:26 +0000583 O << '\n';
Chris Lattnerdeea4162005-12-13 04:33:58 +0000584 }
585 }
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000586
587 // Output stubs for dynamically-linked functions
Evan Cheng4c1aa862006-02-22 20:19:42 +0000588 if (TM.getRelocationModel() == Reloc::PIC) {
Chris Lattnerdeea4162005-12-13 04:33:58 +0000589 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
590 i != e; ++i) {
Chris Lattnera637c322005-12-16 00:22:14 +0000591 SwitchSection(".section __TEXT,__picsymbolstub1,symbol_stubs,"
592 "pure_instructions,32", 0);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000593 EmitAlignment(2);
594 O << "L" << *i << "$stub:\n";
595 O << "\t.indirect_symbol " << *i << "\n";
596 O << "\tmflr r0\n";
597 O << "\tbcl 20,31,L0$" << *i << "\n";
598 O << "L0$" << *i << ":\n";
599 O << "\tmflr r11\n";
600 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
601 O << "\tmtlr r0\n";
602 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
603 O << "\tmtctr r12\n";
604 O << "\tbctr\n";
Chris Lattnera637c322005-12-16 00:22:14 +0000605 SwitchSection(".lazy_symbol_pointer", 0);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000606 O << "L" << *i << "$lazy_ptr:\n";
607 O << "\t.indirect_symbol " << *i << "\n";
608 O << "\t.long dyld_stub_binding_helper\n";
609 }
610 } else {
611 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
612 i != e; ++i) {
Chris Lattnera637c322005-12-16 00:22:14 +0000613 SwitchSection(".section __TEXT,__symbol_stub1,symbol_stubs,"
614 "pure_instructions,16", 0);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000615 EmitAlignment(4);
616 O << "L" << *i << "$stub:\n";
617 O << "\t.indirect_symbol " << *i << "\n";
618 O << "\tlis r11,ha16(L" << *i << "$lazy_ptr)\n";
619 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr)(r11)\n";
620 O << "\tmtctr r12\n";
621 O << "\tbctr\n";
Chris Lattnera637c322005-12-16 00:22:14 +0000622 SwitchSection(".lazy_symbol_pointer", 0);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000623 O << "L" << *i << "$lazy_ptr:\n";
624 O << "\t.indirect_symbol " << *i << "\n";
625 O << "\t.long dyld_stub_binding_helper\n";
Nate Begeman2497e632005-07-21 20:44:43 +0000626 }
Misha Brukman46fd00a2004-06-24 23:04:11 +0000627 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000628
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000629 O << "\n";
630
Chris Lattnera637c322005-12-16 00:22:14 +0000631 // Output stubs for external and common global variables.
632 if (GVStubs.begin() != GVStubs.end()) {
633 SwitchSection(".non_lazy_symbol_pointer", 0);
634 for (std::set<std::string>::iterator I = GVStubs.begin(),
635 E = GVStubs.end(); I != E; ++I) {
636 O << "L" << *I << "$non_lazy_ptr:\n";
637 O << "\t.indirect_symbol " << *I << "\n";
638 O << "\t.long\t0\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +0000639 }
Nate Begemane59bf592004-08-14 22:09:10 +0000640 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000641
Jim Laskey063e7652006-01-17 17:31:53 +0000642 // Emit initial debug information.
Jim Laskey52060a02006-01-24 00:49:18 +0000643 DW.EndModule(M);
Jim Laskey063e7652006-01-17 17:31:53 +0000644
Chris Lattner5b0ac992005-11-01 00:12:36 +0000645 // Funny Darwin hack: This flag tells the linker that no global symbols
646 // contain code that falls through to other global symbols (e.g. the obvious
647 // implementation of multiple entry points). If this doesn't occur, the
648 // linker can safely perform dead code stripping. Since LLVM never generates
649 // code that does this, it is always safe to set.
650 O << "\t.subsections_via_symbols\n";
651
Chris Lattnera3840792004-08-16 23:25:21 +0000652 AsmPrinter::doFinalization(M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000653 return false; // success
654}
Nate Begemaned428532004-09-04 05:00:00 +0000655
Jim Laskeyb2efb852006-01-04 22:28:25 +0000656/// runOnMachineFunction - This uses the e()
Nate Begemaned428532004-09-04 05:00:00 +0000657/// method to print assembly for each instruction.
658///
659bool AIXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner6d5a4f62005-11-21 08:02:41 +0000660 SetupMachineFunction(MF);
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000661
Nate Begemaned428532004-09-04 05:00:00 +0000662 // Print out constants referenced by the function
Chris Lattnerc569e612005-11-21 08:26:15 +0000663 EmitConstantPool(MF.getConstantPool());
Nate Begemaned428532004-09-04 05:00:00 +0000664
665 // Print out header for the function.
666 O << "\t.csect .text[PR]\n"
667 << "\t.align 2\n"
668 << "\t.globl " << CurrentFnName << '\n'
669 << "\t.globl ." << CurrentFnName << '\n'
670 << "\t.csect " << CurrentFnName << "[DS],3\n"
671 << CurrentFnName << ":\n"
672 << "\t.llong ." << CurrentFnName << ", TOC[tc0], 0\n"
673 << "\t.csect .text[PR]\n"
674 << '.' << CurrentFnName << ":\n";
675
676 // Print out code for the function.
677 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
678 I != E; ++I) {
679 // Print a label for the basic block.
Chris Lattner07455362005-11-21 08:14:07 +0000680 O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << '_'
681 << I->getNumber()
Chris Lattner6d5a4f62005-11-21 08:02:41 +0000682 << ":\t" << CommentString << I->getBasicBlock()->getName() << '\n';
Nate Begemaned428532004-09-04 05:00:00 +0000683 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
684 II != E; ++II) {
685 // Print the assembly for the instruction.
686 O << "\t";
687 printMachineInstruction(II);
688 }
689 }
Nate Begemaned428532004-09-04 05:00:00 +0000690
691 O << "LT.." << CurrentFnName << ":\n"
692 << "\t.long 0\n"
693 << "\t.byte 0,0,32,65,128,0,0,0\n"
694 << "\t.long LT.." << CurrentFnName << "-." << CurrentFnName << '\n'
695 << "\t.short 3\n"
696 << "\t.byte \"" << CurrentFnName << "\"\n"
697 << "\t.align 2\n";
698
699 // We didn't modify anything.
700 return false;
701}
702
Nate Begemaned428532004-09-04 05:00:00 +0000703bool AIXAsmPrinter::doInitialization(Module &M) {
Chris Lattnerac7fd7f2005-11-14 18:52:46 +0000704 SwitchSection("", 0);
Nate Begemaned428532004-09-04 05:00:00 +0000705 const TargetData &TD = TM.getTargetData();
Nate Begemaned428532004-09-04 05:00:00 +0000706
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000707 O << "\t.machine \"ppc64\"\n"
Nate Begemaned428532004-09-04 05:00:00 +0000708 << "\t.toc\n"
709 << "\t.csect .text[PR]\n";
710
711 // Print out module-level global variables
Chris Lattner2e00d7d2005-07-26 19:03:27 +0000712 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
713 I != E; ++I) {
Nate Begemaned428532004-09-04 05:00:00 +0000714 if (!I->hasInitializer())
715 continue;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000716
Nate Begemaned428532004-09-04 05:00:00 +0000717 std::string Name = I->getName();
718 Constant *C = I->getInitializer();
719 // N.B.: We are defaulting to writable strings
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000720 if (I->hasExternalLinkage()) {
Nate Begemaned428532004-09-04 05:00:00 +0000721 O << "\t.globl " << Name << '\n'
722 << "\t.csect .data[RW],3\n";
723 } else {
724 O << "\t.csect _global.rw_c[RW],3\n";
725 }
726 O << Name << ":\n";
Chris Lattner8b8b9512005-11-21 07:51:23 +0000727 EmitGlobalConstant(C);
Nate Begemaned428532004-09-04 05:00:00 +0000728 }
729
730 // Output labels for globals
Chris Lattnere4d5c442005-03-15 04:54:21 +0000731 if (M.global_begin() != M.global_end()) O << "\t.toc\n";
Chris Lattner2e00d7d2005-07-26 19:03:27 +0000732 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
733 I != E; ++I) {
Nate Begemaned428532004-09-04 05:00:00 +0000734 const GlobalVariable *GV = I;
735 // Do not output labels for unused variables
736 if (GV->isExternal() && GV->use_begin() == GV->use_end())
737 continue;
738
Chris Lattner07455362005-11-21 08:14:07 +0000739 IncrementFunctionNumber();
Nate Begemaned428532004-09-04 05:00:00 +0000740 std::string Name = GV->getName();
Chris Lattner07455362005-11-21 08:14:07 +0000741 std::string Label = "LC.." + utostr(getFunctionNumber());
Nate Begemaned428532004-09-04 05:00:00 +0000742 GVToLabelMap[GV] = Label;
743 O << Label << ":\n"
744 << "\t.tc " << Name << "[TC]," << Name;
745 if (GV->isExternal()) O << "[RW]";
746 O << '\n';
Chris Lattner07455362005-11-21 08:14:07 +0000747 }
Nate Begemaned428532004-09-04 05:00:00 +0000748
Chris Lattner3459bfb2005-11-10 18:20:29 +0000749 AsmPrinter::doInitialization(M);
Nate Begemaned428532004-09-04 05:00:00 +0000750 return false; // success
751}
752
753bool AIXAsmPrinter::doFinalization(Module &M) {
754 const TargetData &TD = TM.getTargetData();
755 // Print out module-level global variables
Chris Lattner2e00d7d2005-07-26 19:03:27 +0000756 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
757 I != E; ++I) {
Nate Begemaned428532004-09-04 05:00:00 +0000758 if (I->hasInitializer() || I->hasExternalLinkage())
759 continue;
760
761 std::string Name = I->getName();
762 if (I->hasInternalLinkage()) {
763 O << "\t.lcomm " << Name << ",16,_global.bss_c";
764 } else {
765 O << "\t.comm " << Name << "," << TD.getTypeSize(I->getType())
Chris Lattner0561b3f2005-08-02 19:26:06 +0000766 << "," << Log2_32((unsigned)TD.getTypeAlignment(I->getType()));
Nate Begemaned428532004-09-04 05:00:00 +0000767 }
Chris Lattner6d5a4f62005-11-21 08:02:41 +0000768 O << "\t\t" << CommentString << " ";
Chris Lattner8ca02912005-10-03 07:08:36 +0000769 WriteAsOperand(O, I, false, true, &M);
Nate Begemaned428532004-09-04 05:00:00 +0000770 O << "\n";
771 }
772
773 O << "_section_.text:\n"
774 << "\t.csect .data[RW],3\n"
775 << "\t.llong _section_.text\n";
Chris Lattner3459bfb2005-11-10 18:20:29 +0000776 AsmPrinter::doFinalization(M);
Nate Begemaned428532004-09-04 05:00:00 +0000777 return false; // success
778}