blob: e66b0e86e2684d4eebb8998b1841165eefba3055 [file] [log] [blame]
Nate Begeman6cca84e2005-10-16 05:39:50 +00001//===-- PPCAsmPrinter.cpp - Print machine instrs to PowerPC assembly --------=//
Misha Brukmanb4402432005-04-21 23:30:14 +00002//
Misha Brukmane05203f2004-06-21 16:55:25 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb4402432005-04-21 23:30:14 +00007//
Misha Brukmane05203f2004-06-21 16:55:25 +00008//===----------------------------------------------------------------------===//
9//
Misha Brukmanb604b4d2004-07-08 17:58:04 +000010// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to PowerPC assembly language. This printer is
Chris Lattner07fad1c2004-07-28 20:18:53 +000012// the output mechanism used by `llc'.
Misha Brukmane05203f2004-06-21 16:55:25 +000013//
Misha Brukmanb604b4d2004-07-08 17:58:04 +000014// Documentation at http://developer.apple.com/documentation/DeveloperTools/
15// Reference/Assembler/ASMIntroduction/chapter_1_section_1.html
Misha Brukman811f5c22004-06-29 17:13:26 +000016//
Misha Brukmane05203f2004-06-21 16:55:25 +000017//===----------------------------------------------------------------------===//
18
Misha Brukmanf57c3cd2004-06-24 17:31:42 +000019#define DEBUG_TYPE "asmprinter"
Chris Lattnerbfca1ab2005-10-14 23:51:18 +000020#include "PPC.h"
Chris Lattner6f3b9542005-10-14 23:59:06 +000021#include "PPCTargetMachine.h"
Chris Lattnerbfca1ab2005-10-14 23:51:18 +000022#include "PPCSubtarget.h"
Misha Brukmane05203f2004-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 Lattner0ced9052004-08-16 23:25:21 +000027#include "llvm/CodeGen/AsmPrinter.h"
Jim Laskeyb0609d92006-01-04 13:52:30 +000028#include "llvm/CodeGen/DwarfWriter.h"
Jim Laskey7c462762005-12-16 22:45:29 +000029#include "llvm/CodeGen/MachineDebugInfo.h"
Misha Brukmanf57c3cd2004-06-24 17:31:42 +000030#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukmane05203f2004-06-21 16:55:25 +000031#include "llvm/CodeGen/MachineInstr.h"
Misha Brukmane05203f2004-06-21 16:55:25 +000032#include "llvm/Support/Mangler.h"
Nate Begeman4d847042004-09-04 14:51:26 +000033#include "llvm/Support/MathExtras.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000034#include "llvm/Support/CommandLine.h"
35#include "llvm/Support/Debug.h"
Nate Begemandd8f1d82004-10-26 06:02:38 +000036#include "llvm/Target/MRegisterInfo.h"
Nate Begeman3f76eb62004-11-25 07:09:01 +000037#include "llvm/Target/TargetInstrInfo.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000038#include "llvm/ADT/Statistic.h"
39#include "llvm/ADT/StringExtras.h"
Chris Lattnerde02d772006-01-22 23:41:00 +000040#include <iostream>
Misha Brukmanf57c3cd2004-06-24 17:31:42 +000041#include <set>
Chris Lattner0ced9052004-08-16 23:25:21 +000042using namespace llvm;
Misha Brukmane05203f2004-06-21 16:55:25 +000043
44namespace {
45 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
46
Chris Lattner0aacd2a2005-11-14 18:52:46 +000047 class PPCAsmPrinter : public AsmPrinter {
Chris Lattner090eed02005-12-11 07:45:47 +000048 public:
Chris Lattner57575112005-12-16 00:22:14 +000049 std::set<std::string> FnStubs, GVStubs;
Chris Lattner0aacd2a2005-11-14 18:52:46 +000050
Chris Lattner0921e3b2005-10-14 23:37:35 +000051 PPCAsmPrinter(std::ostream &O, TargetMachine &TM)
Chris Lattnerffbfa712005-11-21 08:14:07 +000052 : AsmPrinter(O, TM) {}
Misha Brukmanb4402432005-04-21 23:30:14 +000053
Misha Brukmane05203f2004-06-21 16:55:25 +000054 virtual const char *getPassName() const {
Nate Begeman4bfceb12004-09-04 05:00:00 +000055 return "PowerPC Assembly Printer";
Misha Brukmane05203f2004-06-21 16:55:25 +000056 }
57
Nate Begeman6cca84e2005-10-16 05:39:50 +000058 PPCTargetMachine &getTM() {
59 return static_cast<PPCTargetMachine&>(TM);
Chris Lattner0ced9052004-08-16 23:25:21 +000060 }
61
Nate Begeman8465fe82005-07-20 22:42:00 +000062 unsigned enumRegToMachineReg(unsigned enumReg) {
63 switch (enumReg) {
64 default: assert(0 && "Unhandled register!"); break;
65 case PPC::CR0: return 0;
66 case PPC::CR1: return 1;
67 case PPC::CR2: return 2;
68 case PPC::CR3: return 3;
69 case PPC::CR4: return 4;
70 case PPC::CR5: return 5;
71 case PPC::CR6: return 6;
72 case PPC::CR7: return 7;
73 }
74 abort();
75 }
76
Nate Begeman0ad7f812004-08-14 22:09:10 +000077 /// printInstruction - This method is automatically generated by tablegen
78 /// from the instruction set description. This method returns true if the
79 /// machine instruction was sufficiently described to print it, otherwise it
80 /// returns false.
81 bool printInstruction(const MachineInstr *MI);
82
Misha Brukmane05203f2004-06-21 16:55:25 +000083 void printMachineInstruction(const MachineInstr *MI);
Chris Lattner6ab87fa2005-11-17 19:25:59 +000084 void printOp(const MachineOperand &MO);
Chris Lattnerec1cc1b2004-08-14 23:27:29 +000085
Chris Lattnerf7f05672006-02-01 22:38:46 +000086 void printOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnerec1cc1b2004-08-14 23:27:29 +000087 const MachineOperand &MO = MI->getOperand(OpNo);
88 if (MO.getType() == MachineOperand::MO_MachineRegister) {
89 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
Chris Lattner956820d2005-08-22 22:00:02 +000090 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Chris Lattnerda2e56f2004-08-15 05:46:14 +000091 } else if (MO.isImmediate()) {
92 O << MO.getImmedValue();
Chris Lattnerec1cc1b2004-08-14 23:27:29 +000093 } else {
94 printOp(MO);
95 }
96 }
Chris Lattnerf7f05672006-02-01 22:38:46 +000097
98 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner2bf2c8d2006-02-06 22:18:19 +000099 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattnerf7f05672006-02-01 22:38:46 +0000100 printOperand(MI, OpNo);
101 return false;
102 }
103
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000104 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman3ad3ad42004-08-21 05:56:39 +0000105 unsigned char value = MI->getOperand(OpNo).getImmedValue();
Chris Lattnerf7833ba2004-08-21 19:11:03 +0000106 assert(value <= 31 && "Invalid u5imm argument!");
Nate Begeman3ad3ad42004-08-21 05:56:39 +0000107 O << (unsigned int)value;
108 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000109 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman143cf942004-08-30 02:28:06 +0000110 unsigned char value = MI->getOperand(OpNo).getImmedValue();
111 assert(value <= 63 && "Invalid u6imm argument!");
112 O << (unsigned int)value;
113 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000114 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman4bfceb12004-09-04 05:00:00 +0000115 O << (short)MI->getOperand(OpNo).getImmedValue();
116 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000117 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner8a796852004-08-15 05:20:16 +0000118 O << (unsigned short)MI->getOperand(OpNo).getImmedValue();
119 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000120 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner5a2fb972005-10-18 16:51:22 +0000121 O << (short)MI->getOperand(OpNo).getImmedValue()*4;
122 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000123 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman4bfceb12004-09-04 05:00:00 +0000124 // Branches can take an immediate operand. This is used by the branch
125 // selection pass to print $+8, an eight byte displacement from the PC.
126 if (MI->getOperand(OpNo).isImmediate()) {
Nate Begeman6566e8a2005-04-10 01:48:29 +0000127 O << "$+" << MI->getOperand(OpNo).getImmedValue();
Nate Begeman4bfceb12004-09-04 05:00:00 +0000128 } else {
Chris Lattnerbd9efdb2005-11-17 19:16:08 +0000129 printOp(MI->getOperand(OpNo));
Nate Begeman4bfceb12004-09-04 05:00:00 +0000130 }
Nate Begeman61738782004-09-02 08:13:00 +0000131 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000132 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner6ab87fa2005-11-17 19:25:59 +0000133 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattnercdde99902005-11-17 19:40:30 +0000134 if (!PPCGenerateStaticCode) {
Chris Lattner57575112005-12-16 00:22:14 +0000135 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
136 GlobalValue *GV = MO.getGlobal();
137 if (((GV->isExternal() || GV->hasWeakLinkage() ||
138 GV->hasLinkOnceLinkage()))) {
139 // Dynamically-resolved functions need a stub for the function.
140 std::string Name = Mang->getValueName(GV);
141 FnStubs.insert(Name);
142 O << "L" << Name << "$stub";
143 return;
144 }
145 }
Chris Lattnercdde99902005-11-17 19:40:30 +0000146 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
147 std::string Name(GlobalPrefix); Name += MO.getSymbolName();
148 FnStubs.insert(Name);
149 O << "L" << Name << "$stub";
150 return;
Chris Lattnercdde99902005-11-17 19:40:30 +0000151 }
Chris Lattner6ab87fa2005-11-17 19:25:59 +0000152 }
Chris Lattnercdde99902005-11-17 19:40:30 +0000153
154 printOp(MI->getOperand(OpNo));
Chris Lattnerbd9efdb2005-11-17 19:16:08 +0000155 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000156 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begemana171f6b2005-11-16 00:48:01 +0000157 O << (int)MI->getOperand(OpNo).getImmedValue()*4;
158 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000159 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner57575112005-12-16 00:22:14 +0000160 O << "\"L" << getFunctionNumber() << "$pb\"\n";
161 O << "\"L" << getFunctionNumber() << "$pb\":";
Nate Begeman61738782004-09-02 08:13:00 +0000162 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000163 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Nate Begemana9443f22005-07-21 20:44:43 +0000164 if (MI->getOperand(OpNo).isImmediate()) {
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000165 printS16ImmOperand(MI, OpNo);
Nate Begemana9443f22005-07-21 20:44:43 +0000166 } else {
167 O << "ha16(";
168 printOp(MI->getOperand(OpNo));
169 if (PICEnabled)
Chris Lattner57575112005-12-16 00:22:14 +0000170 O << "-\"L" << getFunctionNumber() << "$pb\")";
Nate Begemana9443f22005-07-21 20:44:43 +0000171 else
172 O << ')';
173 }
Nate Begeman4bfceb12004-09-04 05:00:00 +0000174 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000175 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman4bfceb12004-09-04 05:00:00 +0000176 if (MI->getOperand(OpNo).isImmediate()) {
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000177 printS16ImmOperand(MI, OpNo);
Nate Begeman4bfceb12004-09-04 05:00:00 +0000178 } else {
179 O << "lo16(";
Nate Begeman3f76eb62004-11-25 07:09:01 +0000180 printOp(MI->getOperand(OpNo));
Nate Begemana9443f22005-07-21 20:44:43 +0000181 if (PICEnabled)
Chris Lattner57575112005-12-16 00:22:14 +0000182 O << "-\"L" << getFunctionNumber() << "$pb\")";
Nate Begemana9443f22005-07-21 20:44:43 +0000183 else
184 O << ')';
Nate Begeman4bfceb12004-09-04 05:00:00 +0000185 }
186 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000187 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman8465fe82005-07-20 22:42:00 +0000188 unsigned CCReg = MI->getOperand(OpNo).getReg();
189 unsigned RegNo = enumRegToMachineReg(CCReg);
190 O << (0x80 >> RegNo);
191 }
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000192 // The new addressing mode printers, currently empty
193 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
194 printSymbolLo(MI, OpNo);
195 O << '(';
196 printOperand(MI, OpNo+1);
197 O << ')';
198 }
199 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
Nate Begemanc1263972005-12-19 23:40:42 +0000200 // When used as the base register, r0 reads constant zero rather than
201 // the value contained in the register. For this reason, the darwin
202 // assembler requires that we print r0 as 0 (no r) when used as the base.
203 const MachineOperand &MO = MI->getOperand(OpNo);
204 if (MO.getReg() == PPC::R0)
205 O << '0';
206 else
207 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000208 O << ", ";
209 printOperand(MI, OpNo+1);
210 }
211
Misha Brukmanb4402432005-04-21 23:30:14 +0000212 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
Nate Begeman4bfceb12004-09-04 05:00:00 +0000213 virtual bool doFinalization(Module &M) = 0;
Jim Laskey219d5592006-01-04 22:28:25 +0000214
Nate Begeman4bfceb12004-09-04 05:00:00 +0000215 };
Misha Brukmanb4402432005-04-21 23:30:14 +0000216
Jim Laskeyb0609d92006-01-04 13:52:30 +0000217 /// DarwinDwarfWriter - Dwarf debug info writer customized for Darwin/Mac OS X
218 ///
219 struct DarwinDwarfWriter : public DwarfWriter {
220 // Ctor.
Jim Laskey219d5592006-01-04 22:28:25 +0000221 DarwinDwarfWriter(std::ostream &o, AsmPrinter *ap)
222 : DwarfWriter(o, ap)
Jim Laskeyb0609d92006-01-04 13:52:30 +0000223 {
Jim Laskeyb0609d92006-01-04 13:52:30 +0000224 needsSet = true;
Jim Laskey58d48c82006-02-06 14:16:15 +0000225 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev";
226 DwarfInfoSection = ".section __DWARF,__debug_info";
227 DwarfLineSection = ".section __DWARF,__debug_line";
Jim Laskey194a5262006-01-18 16:54:26 +0000228 DwarfFrameSection =
Jim Laskey58d48c82006-02-06 14:16:15 +0000229 ".section __DWARF,__debug_frame,,coalesced,no_toc+strip_static_syms";
230 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames";
231 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes";
232 DwarfStrSection = ".section __DWARF,__debug_str";
233 DwarfLocSection = ".section __DWARF,__debug_loc";
234 DwarfARangesSection = ".section __DWARF,__debug_aranges";
235 DwarfRangesSection = ".section __DWARF,__debug_ranges";
236 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo";
Jim Laskeyb9966022006-01-17 17:31:53 +0000237 TextSection = ".text";
238 DataSection = ".data";
Jim Laskeyb0609d92006-01-04 13:52:30 +0000239 }
240 };
241
Misha Brukman175fdd42004-09-05 02:42:44 +0000242 /// DarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac OS
243 /// X
Chris Lattner0921e3b2005-10-14 23:37:35 +0000244 struct DarwinAsmPrinter : public PPCAsmPrinter {
Jim Laskeyb0609d92006-01-04 13:52:30 +0000245
246 DarwinDwarfWriter DW;
Nate Begeman4bfceb12004-09-04 05:00:00 +0000247
248 DarwinAsmPrinter(std::ostream &O, TargetMachine &TM)
Chris Lattner2e07d632006-02-08 23:42:22 +0000249 : PPCAsmPrinter(O, TM), DW(O, this) {
Nate Begeman4bfceb12004-09-04 05:00:00 +0000250 CommentString = ";";
251 GlobalPrefix = "_";
Chris Lattner41cb1152005-11-21 06:47:58 +0000252 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
Nate Begeman4bfceb12004-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 Lattneref83ebd2005-11-21 08:26:15 +0000256 ConstantPoolSection = "\t.const\t";
Chris Lattner090eed02005-12-11 07:45:47 +0000257 LCOMMDirective = "\t.lcomm\t";
Chris Lattner87079882005-12-13 06:32:50 +0000258 StaticCtorsSection = ".mod_init_func";
259 StaticDtorsSection = ".mod_term_func";
Chris Lattner2e07d632006-02-08 23:42:22 +0000260 InlineAsmStart = InlineAsmEnd = ""; // Don't use #APP/#NO_APP
Nate Begeman4bfceb12004-09-04 05:00:00 +0000261 }
262
263 virtual const char *getPassName() const {
264 return "Darwin PPC Assembly Printer";
265 }
Chris Lattnere0f5f8e2005-12-09 18:24:29 +0000266
Misha Brukmanb4402432005-04-21 23:30:14 +0000267 bool runOnMachineFunction(MachineFunction &F);
Nate Begeman15527112005-07-21 01:25:49 +0000268 bool doInitialization(Module &M);
Misha Brukmane05203f2004-06-21 16:55:25 +0000269 bool doFinalization(Module &M);
Jim Laskey219d5592006-01-04 22:28:25 +0000270
271 void getAnalysisUsage(AnalysisUsage &AU) const {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000272 AU.setPreservesAll();
Jim Laskey219d5592006-01-04 22:28:25 +0000273 AU.addRequired<MachineDebugInfo>();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000274 PPCAsmPrinter::getAnalysisUsage(AU);
Jim Laskey219d5592006-01-04 22:28:25 +0000275 }
276
Misha Brukmane05203f2004-06-21 16:55:25 +0000277 };
Misha Brukmanb4402432005-04-21 23:30:14 +0000278
Misha Brukman175fdd42004-09-05 02:42:44 +0000279 /// AIXAsmPrinter - PowerPC assembly printer, customized for AIX
280 ///
Chris Lattner0921e3b2005-10-14 23:37:35 +0000281 struct AIXAsmPrinter : public PPCAsmPrinter {
Nate Begeman4bfceb12004-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 Lattner0921e3b2005-10-14 23:37:35 +0000287 : PPCAsmPrinter(O, TM) {
Nate Begeman4bfceb12004-09-04 05:00:00 +0000288 CommentString = "#";
Chris Lattner59e44ff2005-11-10 18:20:29 +0000289 GlobalPrefix = ".";
Nate Begeman4bfceb12004-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 Lattneref83ebd2005-11-21 08:26:15 +0000293 ConstantPoolSection = "\t.const\t";
Nate Begeman4bfceb12004-09-04 05:00:00 +0000294 }
Misha Brukmanb4402432005-04-21 23:30:14 +0000295
Nate Begeman4bfceb12004-09-04 05:00:00 +0000296 virtual const char *getPassName() const {
297 return "AIX PPC Assembly Printer";
298 }
299
Misha Brukmanb4402432005-04-21 23:30:14 +0000300 bool runOnMachineFunction(MachineFunction &F);
Nate Begeman4bfceb12004-09-04 05:00:00 +0000301 bool doInitialization(Module &M);
302 bool doFinalization(Module &M);
303 };
Misha Brukmane05203f2004-06-21 16:55:25 +0000304} // end of anonymous namespace
305
Misha Brukmanb4402432005-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 Begeman4bfceb12004-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 Brukmanb4402432005-04-21 23:30:14 +0000315/// for a MachineFunction to the given output stream, in a format that the
Nate Begeman4bfceb12004-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 Brukmane05203f2004-06-21 16:55:25 +0000320}
321
Nate Begeman0ad7f812004-08-14 22:09:10 +0000322// Include the auto-generated portion of the assembly writer
Chris Lattner0921e3b2005-10-14 23:37:35 +0000323#include "PPCGenAsmWriter.inc"
Nate Begeman0ad7f812004-08-14 22:09:10 +0000324
Chris Lattner6ab87fa2005-11-17 19:25:59 +0000325void PPCAsmPrinter::printOp(const MachineOperand &MO) {
Misha Brukmane05203f2004-06-21 16:55:25 +0000326 const MRegisterInfo &RI = *TM.getRegisterInfo();
327 int new_symbol;
Misha Brukmanb4402432005-04-21 23:30:14 +0000328
Misha Brukmane05203f2004-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 Brukmanb604b4d2004-07-08 17:58:04 +0000337 case MachineOperand::MO_CCRegister:
Chris Lattner956820d2005-08-22 22:00:02 +0000338 O << RI.get(MO.getReg()).Name;
Misha Brukmana08a2362004-06-24 23:51:00 +0000339 return;
Misha Brukmane05203f2004-06-21 16:55:25 +0000340
341 case MachineOperand::MO_SignExtendedImmed:
Misha Brukman47d5a222004-07-28 00:00:48 +0000342 case MachineOperand::MO_UnextendedImmed:
343 std::cerr << "printOp() does not handle immediate values\n";
344 abort();
Misha Brukman8d75aa42004-07-21 20:11:11 +0000345 return;
346
Misha Brukmanb604b4d2004-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 Brukmanb4402432005-04-21 23:30:14 +0000351
Misha Brukmane05203f2004-06-21 16:55:25 +0000352 case MachineOperand::MO_MachineBasicBlock: {
353 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
Chris Lattnerffbfa712005-11-21 08:14:07 +0000354 O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << "_"
Chris Lattnerc2bc19a2005-11-21 07:41:05 +0000355 << MBBOp->getNumber() << "\t; " << MBBOp->getBasicBlock()->getName();
Misha Brukmane05203f2004-06-21 16:55:25 +0000356 return;
357 }
Misha Brukmanb604b4d2004-07-08 17:58:04 +0000358
359 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnerffbfa712005-11-21 08:14:07 +0000360 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber()
Chris Lattner41cb1152005-11-21 06:47:58 +0000361 << '_' << MO.getConstantPoolIndex();
Misha Brukmane05203f2004-06-21 16:55:25 +0000362 return;
Misha Brukmanb604b4d2004-07-08 17:58:04 +0000363 case MachineOperand::MO_ExternalSymbol:
Chris Lattner57575112005-12-16 00:22:14 +0000364 // Computing the address of an external symbol, not calling it.
365 if (!PPCGenerateStaticCode) {
366 std::string Name(GlobalPrefix); Name += MO.getSymbolName();
367 GVStubs.insert(Name);
368 O << "L" << Name << "$non_lazy_ptr";
369 return;
370 }
Nate Begeman5851a662005-03-30 01:45:43 +0000371 O << GlobalPrefix << MO.getSymbolName();
Misha Brukmanb604b4d2004-07-08 17:58:04 +0000372 return;
Nate Begeman5bf9bfe2004-08-13 09:32:01 +0000373 case MachineOperand::MO_GlobalAddress: {
Chris Lattner57575112005-12-16 00:22:14 +0000374 // Computing the address of a global symbol, not calling it.
Nate Begeman5bf9bfe2004-08-13 09:32:01 +0000375 GlobalValue *GV = MO.getGlobal();
376 std::string Name = Mang->getValueName(GV);
Nate Begeman9aea6e42005-12-24 01:00:15 +0000377 int offset = MO.getOffset();
Misha Brukman7dba17d2004-07-23 16:08:20 +0000378
Nate Begeman844186b2004-10-17 23:01:34 +0000379 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattner57575112005-12-16 00:22:14 +0000380 if (!PPCGenerateStaticCode) {
381 if (((GV->isExternal() || GV->hasWeakLinkage() ||
382 GV->hasLinkOnceLinkage()))) {
Nate Begeman3f76eb62004-11-25 07:09:01 +0000383 GVStubs.insert(Name);
Chris Lattner57575112005-12-16 00:22:14 +0000384 O << "L" << Name << "$non_lazy_ptr";
385 return;
386 }
Nate Begeman5bf9bfe2004-08-13 09:32:01 +0000387 }
Nate Begeman3f76eb62004-11-25 07:09:01 +0000388
Chris Lattnercdde99902005-11-17 19:40:30 +0000389 O << Name;
Misha Brukmane05203f2004-06-21 16:55:25 +0000390 return;
Nate Begeman5bf9bfe2004-08-13 09:32:01 +0000391 }
Misha Brukmanb4402432005-04-21 23:30:14 +0000392
Misha Brukmane05203f2004-06-21 16:55:25 +0000393 default:
Misha Brukmanb604b4d2004-07-08 17:58:04 +0000394 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukmana2737582004-06-25 15:11:34 +0000395 return;
Misha Brukmane05203f2004-06-21 16:55:25 +0000396 }
397}
398
Nate Begeman0ad7f812004-08-14 22:09:10 +0000399/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
400/// the current output stream.
Misha Brukmane05203f2004-06-21 16:55:25 +0000401///
Chris Lattner0921e3b2005-10-14 23:37:35 +0000402void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Nate Begeman0ad7f812004-08-14 22:09:10 +0000403 ++EmittedInsts;
Chris Lattner2121f3c2005-10-14 22:44:13 +0000404
Nate Begeman52441732005-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 Begeman6f8c1ac2005-11-30 18:54:35 +0000419 printOperand(MI, 0);
Misha Brukmanb4402432005-04-21 23:30:14 +0000420 O << ", ";
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000421 printOperand(MI, 1);
Nate Begeman52441732005-04-05 18:19:50 +0000422 O << ", " << (unsigned int)SH << "\n";
423 return;
424 }
Chris Lattnerf7b962d2006-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 Begeman52441732005-04-05 18:19:50 +0000434 }
Misha Brukmanb4402432005-04-21 23:30:14 +0000435
Nate Begeman0ad7f812004-08-14 22:09:10 +0000436 if (printInstruction(MI))
437 return; // Printer was automatically generated
Misha Brukmanb4402432005-04-21 23:30:14 +0000438
Nate Begeman4bfceb12004-09-04 05:00:00 +0000439 assert(0 && "Unhandled instruction in asm writer!");
440 abort();
Nate Begeman0ad7f812004-08-14 22:09:10 +0000441 return;
Misha Brukmane05203f2004-06-21 16:55:25 +0000442}
443
Chris Lattnera0222a12005-11-21 07:57:37 +0000444
Nate Begeman4bfceb12004-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 Laskey0bbdc552006-01-26 20:21:46 +0000449 // FIXME - is this the earliest this can be set?
Jim Laskey762e9ec2006-01-05 01:25:28 +0000450 DW.SetDebugInfo(&getAnalysis<MachineDebugInfo>());
451
Chris Lattner99946fb2005-11-21 07:51:23 +0000452 SetupMachineFunction(MF);
Nate Begeman4bfceb12004-09-04 05:00:00 +0000453 O << "\n\n";
Jim Laskey7c462762005-12-16 22:45:29 +0000454
Jim Laskeyb0609d92006-01-04 13:52:30 +0000455 // Emit pre-function debug information.
Jim Laskey3e65f282006-01-24 00:49:18 +0000456 DW.BeginFunction(MF);
Nate Begeman4bfceb12004-09-04 05:00:00 +0000457
458 // Print out constants referenced by the function
Chris Lattneref83ebd2005-11-21 08:26:15 +0000459 EmitConstantPool(MF.getConstantPool());
Nate Begeman4bfceb12004-09-04 05:00:00 +0000460
461 // Print out labels for the function.
Chris Lattner0aacd2a2005-11-14 18:52:46 +0000462 const Function *F = MF.getFunction();
Chris Lattner57575112005-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 Lattner57575112005-12-16 00:22:14 +0000467 break;
468 case Function::ExternalLinkage:
469 SwitchSection(".text", F);
Chris Lattner97d72c82005-10-28 18:44:07 +0000470 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner57575112005-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 Lattner84fb09e2006-02-14 20:42:33 +0000480 EmitAlignment(4, F);
Nate Begeman4bfceb12004-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 Lattner968aeb12005-08-21 19:09:33 +0000487 if (I != MF.begin()) {
Chris Lattnerffbfa712005-11-21 08:14:07 +0000488 O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << '_'
Chris Lattnerc2bc19a2005-11-21 07:41:05 +0000489 << I->getNumber() << ":\t";
Chris Lattner968aeb12005-08-21 19:09:33 +0000490 if (!I->getBasicBlock()->getName().empty())
491 O << CommentString << " " << I->getBasicBlock()->getName();
492 O << "\n";
493 }
Nate Begeman4bfceb12004-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 Begeman4bfceb12004-09-04 05:00:00 +0000501
Jim Laskeyb0609d92006-01-04 13:52:30 +0000502 // Emit post-function debug information.
Jim Laskey3e65f282006-01-24 00:49:18 +0000503 DW.EndFunction(MF);
Jim Laskeyb0609d92006-01-04 13:52:30 +0000504
Nate Begeman4bfceb12004-09-04 05:00:00 +0000505 // We didn't modify anything.
506 return false;
507}
508
Misha Brukmane05203f2004-06-21 16:55:25 +0000509
Nate Begeman15527112005-07-21 01:25:49 +0000510bool DarwinAsmPrinter::doInitialization(Module &M) {
Chris Lattner983a4152005-08-05 22:05:03 +0000511 if (TM.getSubtarget<PPCSubtarget>().isGigaProcessor())
512 O << "\t.machine ppc970\n";
Nate Begeman15527112005-07-21 01:25:49 +0000513 AsmPrinter::doInitialization(M);
Chris Lattner9eb7dfa2005-11-10 19:33:43 +0000514
515 // Darwin wants symbols to be quoted if they have complex names.
516 Mang->setUseQuotes(true);
Jim Laskeyb0609d92006-01-04 13:52:30 +0000517
518 // Emit initial debug information.
Jim Laskey3e65f282006-01-24 00:49:18 +0000519 DW.BeginModule(M);
Nate Begeman15527112005-07-21 01:25:49 +0000520 return false;
521}
522
Nate Begeman4bfceb12004-09-04 05:00:00 +0000523bool DarwinAsmPrinter::doFinalization(Module &M) {
Misha Brukmane05203f2004-06-21 16:55:25 +0000524 const TargetData &TD = TM.getTargetData();
Misha Brukmane05203f2004-06-21 16:55:25 +0000525
526 // Print out module-level global variables here.
Chris Lattner1a4adc72005-11-14 19:00:30 +0000527 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Chris Lattner54a11df2005-12-13 04:33:58 +0000528 I != E; ++I) {
529 if (!I->hasInitializer()) continue; // External global require no code
530
Chris Lattner87079882005-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 Lattner54a11df2005-12-13 04:33:58 +0000534
Chris Lattner54a11df2005-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 Lattner1b1a8732006-02-05 01:30:45 +0000538 unsigned Align = getPreferredAlignmentLog(I);
Chris Lattner54a11df2005-12-13 04:33:58 +0000539
540 if (C->isNullValue() && /* FIXME: Verify correct */
541 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
542 I->hasLinkOnceLinkage())) {
543 SwitchSection(".data", I);
544 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
545 if (I->hasInternalLinkage())
546 O << LCOMMDirective << name << "," << Size << "," << Align;
547 else
548 O << ".comm " << name << "," << Size;
549 O << "\t\t; '" << I->getName() << "'\n";
550 } else {
551 switch (I->getLinkage()) {
552 case GlobalValue::LinkOnceLinkage:
Chris Lattner54a11df2005-12-13 04:33:58 +0000553 case GlobalValue::WeakLinkage:
Chris Lattner6b0325a2005-12-22 21:15:17 +0000554 O << "\t.globl " << name << '\n'
555 << "\t.weak_definition " << name << '\n';
Chris Lattner57575112005-12-16 00:22:14 +0000556 SwitchSection(".section __DATA,__datacoal_nt,coalesced", I);
Chris Lattner54a11df2005-12-13 04:33:58 +0000557 break;
558 case GlobalValue::AppendingLinkage:
559 // FIXME: appending linkage variables should go into a section of
560 // their name or something. For now, just emit them as external.
561 case GlobalValue::ExternalLinkage:
562 // If external or appending, declare as a global symbol
563 O << "\t.globl " << name << "\n";
564 // FALL THROUGH
565 case GlobalValue::InternalLinkage:
566 SwitchSection(".data", I);
567 break;
568 default:
569 std::cerr << "Unknown linkage type!";
570 abort();
571 }
572
573 EmitAlignment(Align, I);
574 O << name << ":\t\t\t\t; '" << I->getName() << "'\n";
575 EmitGlobalConstant(C);
Chris Lattner9436aa72006-01-21 01:35:26 +0000576 O << '\n';
Chris Lattner54a11df2005-12-13 04:33:58 +0000577 }
578 }
Misha Brukmand4ac8182004-07-16 20:29:04 +0000579
580 // Output stubs for dynamically-linked functions
Chris Lattner54a11df2005-12-13 04:33:58 +0000581 if (PICEnabled) {
582 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
583 i != e; ++i) {
Chris Lattner57575112005-12-16 00:22:14 +0000584 SwitchSection(".section __TEXT,__picsymbolstub1,symbol_stubs,"
585 "pure_instructions,32", 0);
Chris Lattner54a11df2005-12-13 04:33:58 +0000586 EmitAlignment(2);
587 O << "L" << *i << "$stub:\n";
588 O << "\t.indirect_symbol " << *i << "\n";
589 O << "\tmflr r0\n";
590 O << "\tbcl 20,31,L0$" << *i << "\n";
591 O << "L0$" << *i << ":\n";
592 O << "\tmflr r11\n";
593 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
594 O << "\tmtlr r0\n";
595 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
596 O << "\tmtctr r12\n";
597 O << "\tbctr\n";
Chris Lattner57575112005-12-16 00:22:14 +0000598 SwitchSection(".lazy_symbol_pointer", 0);
Chris Lattner54a11df2005-12-13 04:33:58 +0000599 O << "L" << *i << "$lazy_ptr:\n";
600 O << "\t.indirect_symbol " << *i << "\n";
601 O << "\t.long dyld_stub_binding_helper\n";
602 }
603 } else {
604 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
605 i != e; ++i) {
Chris Lattner57575112005-12-16 00:22:14 +0000606 SwitchSection(".section __TEXT,__symbol_stub1,symbol_stubs,"
607 "pure_instructions,16", 0);
Chris Lattner54a11df2005-12-13 04:33:58 +0000608 EmitAlignment(4);
609 O << "L" << *i << "$stub:\n";
610 O << "\t.indirect_symbol " << *i << "\n";
611 O << "\tlis r11,ha16(L" << *i << "$lazy_ptr)\n";
612 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr)(r11)\n";
613 O << "\tmtctr r12\n";
614 O << "\tbctr\n";
Chris Lattner57575112005-12-16 00:22:14 +0000615 SwitchSection(".lazy_symbol_pointer", 0);
Chris Lattner54a11df2005-12-13 04:33:58 +0000616 O << "L" << *i << "$lazy_ptr:\n";
617 O << "\t.indirect_symbol " << *i << "\n";
618 O << "\t.long dyld_stub_binding_helper\n";
Nate Begemana9443f22005-07-21 20:44:43 +0000619 }
Misha Brukmanf62ee7a2004-06-24 23:04:11 +0000620 }
Misha Brukmane05203f2004-06-21 16:55:25 +0000621
Misha Brukmand4ac8182004-07-16 20:29:04 +0000622 O << "\n";
623
Chris Lattner57575112005-12-16 00:22:14 +0000624 // Output stubs for external and common global variables.
625 if (GVStubs.begin() != GVStubs.end()) {
626 SwitchSection(".non_lazy_symbol_pointer", 0);
627 for (std::set<std::string>::iterator I = GVStubs.begin(),
628 E = GVStubs.end(); I != E; ++I) {
629 O << "L" << *I << "$non_lazy_ptr:\n";
630 O << "\t.indirect_symbol " << *I << "\n";
631 O << "\t.long\t0\n";
Chris Lattner54a11df2005-12-13 04:33:58 +0000632 }
Nate Begeman0ad7f812004-08-14 22:09:10 +0000633 }
Misha Brukmanb4402432005-04-21 23:30:14 +0000634
Jim Laskeyb9966022006-01-17 17:31:53 +0000635 // Emit initial debug information.
Jim Laskey3e65f282006-01-24 00:49:18 +0000636 DW.EndModule(M);
Jim Laskeyb9966022006-01-17 17:31:53 +0000637
Chris Lattner7432ceef2005-11-01 00:12:36 +0000638 // Funny Darwin hack: This flag tells the linker that no global symbols
639 // contain code that falls through to other global symbols (e.g. the obvious
640 // implementation of multiple entry points). If this doesn't occur, the
641 // linker can safely perform dead code stripping. Since LLVM never generates
642 // code that does this, it is always safe to set.
643 O << "\t.subsections_via_symbols\n";
644
Chris Lattner0ced9052004-08-16 23:25:21 +0000645 AsmPrinter::doFinalization(M);
Misha Brukmane05203f2004-06-21 16:55:25 +0000646 return false; // success
647}
Nate Begeman4bfceb12004-09-04 05:00:00 +0000648
Jim Laskey219d5592006-01-04 22:28:25 +0000649/// runOnMachineFunction - This uses the e()
Nate Begeman4bfceb12004-09-04 05:00:00 +0000650/// method to print assembly for each instruction.
651///
652bool AIXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattnerdd3bf8e2005-11-21 08:02:41 +0000653 SetupMachineFunction(MF);
Jim Laskeyb0609d92006-01-04 13:52:30 +0000654
Nate Begeman4bfceb12004-09-04 05:00:00 +0000655 // Print out constants referenced by the function
Chris Lattneref83ebd2005-11-21 08:26:15 +0000656 EmitConstantPool(MF.getConstantPool());
Nate Begeman4bfceb12004-09-04 05:00:00 +0000657
658 // Print out header for the function.
659 O << "\t.csect .text[PR]\n"
660 << "\t.align 2\n"
661 << "\t.globl " << CurrentFnName << '\n'
662 << "\t.globl ." << CurrentFnName << '\n'
663 << "\t.csect " << CurrentFnName << "[DS],3\n"
664 << CurrentFnName << ":\n"
665 << "\t.llong ." << CurrentFnName << ", TOC[tc0], 0\n"
666 << "\t.csect .text[PR]\n"
667 << '.' << CurrentFnName << ":\n";
668
669 // Print out code for the function.
670 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
671 I != E; ++I) {
672 // Print a label for the basic block.
Chris Lattnerffbfa712005-11-21 08:14:07 +0000673 O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << '_'
674 << I->getNumber()
Chris Lattnerdd3bf8e2005-11-21 08:02:41 +0000675 << ":\t" << CommentString << I->getBasicBlock()->getName() << '\n';
Nate Begeman4bfceb12004-09-04 05:00:00 +0000676 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
677 II != E; ++II) {
678 // Print the assembly for the instruction.
679 O << "\t";
680 printMachineInstruction(II);
681 }
682 }
Nate Begeman4bfceb12004-09-04 05:00:00 +0000683
684 O << "LT.." << CurrentFnName << ":\n"
685 << "\t.long 0\n"
686 << "\t.byte 0,0,32,65,128,0,0,0\n"
687 << "\t.long LT.." << CurrentFnName << "-." << CurrentFnName << '\n'
688 << "\t.short 3\n"
689 << "\t.byte \"" << CurrentFnName << "\"\n"
690 << "\t.align 2\n";
691
692 // We didn't modify anything.
693 return false;
694}
695
Nate Begeman4bfceb12004-09-04 05:00:00 +0000696bool AIXAsmPrinter::doInitialization(Module &M) {
Chris Lattner0aacd2a2005-11-14 18:52:46 +0000697 SwitchSection("", 0);
Nate Begeman4bfceb12004-09-04 05:00:00 +0000698 const TargetData &TD = TM.getTargetData();
Nate Begeman4bfceb12004-09-04 05:00:00 +0000699
Misha Brukmanb4402432005-04-21 23:30:14 +0000700 O << "\t.machine \"ppc64\"\n"
Nate Begeman4bfceb12004-09-04 05:00:00 +0000701 << "\t.toc\n"
702 << "\t.csect .text[PR]\n";
703
704 // Print out module-level global variables
Chris Lattner06721f52005-07-26 19:03:27 +0000705 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
706 I != E; ++I) {
Nate Begeman4bfceb12004-09-04 05:00:00 +0000707 if (!I->hasInitializer())
708 continue;
Misha Brukmanb4402432005-04-21 23:30:14 +0000709
Nate Begeman4bfceb12004-09-04 05:00:00 +0000710 std::string Name = I->getName();
711 Constant *C = I->getInitializer();
712 // N.B.: We are defaulting to writable strings
Misha Brukmanb4402432005-04-21 23:30:14 +0000713 if (I->hasExternalLinkage()) {
Nate Begeman4bfceb12004-09-04 05:00:00 +0000714 O << "\t.globl " << Name << '\n'
715 << "\t.csect .data[RW],3\n";
716 } else {
717 O << "\t.csect _global.rw_c[RW],3\n";
718 }
719 O << Name << ":\n";
Chris Lattner99946fb2005-11-21 07:51:23 +0000720 EmitGlobalConstant(C);
Nate Begeman4bfceb12004-09-04 05:00:00 +0000721 }
722
723 // Output labels for globals
Chris Lattner531f9e92005-03-15 04:54:21 +0000724 if (M.global_begin() != M.global_end()) O << "\t.toc\n";
Chris Lattner06721f52005-07-26 19:03:27 +0000725 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
726 I != E; ++I) {
Nate Begeman4bfceb12004-09-04 05:00:00 +0000727 const GlobalVariable *GV = I;
728 // Do not output labels for unused variables
729 if (GV->isExternal() && GV->use_begin() == GV->use_end())
730 continue;
731
Chris Lattnerffbfa712005-11-21 08:14:07 +0000732 IncrementFunctionNumber();
Nate Begeman4bfceb12004-09-04 05:00:00 +0000733 std::string Name = GV->getName();
Chris Lattnerffbfa712005-11-21 08:14:07 +0000734 std::string Label = "LC.." + utostr(getFunctionNumber());
Nate Begeman4bfceb12004-09-04 05:00:00 +0000735 GVToLabelMap[GV] = Label;
736 O << Label << ":\n"
737 << "\t.tc " << Name << "[TC]," << Name;
738 if (GV->isExternal()) O << "[RW]";
739 O << '\n';
Chris Lattnerffbfa712005-11-21 08:14:07 +0000740 }
Nate Begeman4bfceb12004-09-04 05:00:00 +0000741
Chris Lattner59e44ff2005-11-10 18:20:29 +0000742 AsmPrinter::doInitialization(M);
Nate Begeman4bfceb12004-09-04 05:00:00 +0000743 return false; // success
744}
745
746bool AIXAsmPrinter::doFinalization(Module &M) {
747 const TargetData &TD = TM.getTargetData();
748 // Print out module-level global variables
Chris Lattner06721f52005-07-26 19:03:27 +0000749 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
750 I != E; ++I) {
Nate Begeman4bfceb12004-09-04 05:00:00 +0000751 if (I->hasInitializer() || I->hasExternalLinkage())
752 continue;
753
754 std::string Name = I->getName();
755 if (I->hasInternalLinkage()) {
756 O << "\t.lcomm " << Name << ",16,_global.bss_c";
757 } else {
758 O << "\t.comm " << Name << "," << TD.getTypeSize(I->getType())
Chris Lattner6667bdb2005-08-02 19:26:06 +0000759 << "," << Log2_32((unsigned)TD.getTypeAlignment(I->getType()));
Nate Begeman4bfceb12004-09-04 05:00:00 +0000760 }
Chris Lattnerdd3bf8e2005-11-21 08:02:41 +0000761 O << "\t\t" << CommentString << " ";
Chris Lattnerafef68b2005-10-03 07:08:36 +0000762 WriteAsOperand(O, I, false, true, &M);
Nate Begeman4bfceb12004-09-04 05:00:00 +0000763 O << "\n";
764 }
765
766 O << "_section_.text:\n"
767 << "\t.csect .data[RW],3\n"
768 << "\t.llong _section_.text\n";
Chris Lattner59e44ff2005-11-10 18:20:29 +0000769 AsmPrinter::doFinalization(M);
Nate Begeman4bfceb12004-09-04 05:00:00 +0000770 return false; // success
771}