blob: 76ca256832745c37fc3c00c3c4477fd161a8d568 [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 Lattner58873272006-02-01 22:38:46 +0000101
Nate Begeman391c5d22005-11-30 18:54:35 +0000102 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begemanc3306122004-08-21 05:56:39 +0000103 unsigned char value = MI->getOperand(OpNo).getImmedValue();
Chris Lattner12585ba2004-08-21 19:11:03 +0000104 assert(value <= 31 && "Invalid u5imm argument!");
Nate Begemanc3306122004-08-21 05:56:39 +0000105 O << (unsigned int)value;
106 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000107 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman07aada82004-08-30 02:28:06 +0000108 unsigned char value = MI->getOperand(OpNo).getImmedValue();
109 assert(value <= 63 && "Invalid u6imm argument!");
110 O << (unsigned int)value;
111 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000112 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begemaned428532004-09-04 05:00:00 +0000113 O << (short)MI->getOperand(OpNo).getImmedValue();
114 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000115 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner97b2a2e2004-08-15 05:20:16 +0000116 O << (unsigned short)MI->getOperand(OpNo).getImmedValue();
117 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000118 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner841d12d2005-10-18 16:51:22 +0000119 O << (short)MI->getOperand(OpNo).getImmedValue()*4;
120 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000121 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begemaned428532004-09-04 05:00:00 +0000122 // Branches can take an immediate operand. This is used by the branch
123 // selection pass to print $+8, an eight byte displacement from the PC.
124 if (MI->getOperand(OpNo).isImmediate()) {
Nate Begeman27499e32005-04-10 01:48:29 +0000125 O << "$+" << MI->getOperand(OpNo).getImmedValue();
Nate Begemaned428532004-09-04 05:00:00 +0000126 } else {
Chris Lattner3e7f86a2005-11-17 19:16:08 +0000127 printOp(MI->getOperand(OpNo));
Nate Begemaned428532004-09-04 05:00:00 +0000128 }
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000129 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000130 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9ba13e42005-11-17 19:25:59 +0000131 const MachineOperand &MO = MI->getOperand(OpNo);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000132 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattnera637c322005-12-16 00:22:14 +0000133 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
134 GlobalValue *GV = MO.getGlobal();
135 if (((GV->isExternal() || GV->hasWeakLinkage() ||
136 GV->hasLinkOnceLinkage()))) {
137 // Dynamically-resolved functions need a stub for the function.
138 std::string Name = Mang->getValueName(GV);
139 FnStubs.insert(Name);
140 O << "L" << Name << "$stub";
141 return;
142 }
143 }
Chris Lattner9542f9712005-11-17 19:40:30 +0000144 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
145 std::string Name(GlobalPrefix); Name += MO.getSymbolName();
146 FnStubs.insert(Name);
147 O << "L" << Name << "$stub";
148 return;
Chris Lattner9542f9712005-11-17 19:40:30 +0000149 }
Chris Lattner9ba13e42005-11-17 19:25:59 +0000150 }
Chris Lattner9542f9712005-11-17 19:40:30 +0000151
152 printOp(MI->getOperand(OpNo));
Chris Lattner3e7f86a2005-11-17 19:16:08 +0000153 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000154 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman422b0ce2005-11-16 00:48:01 +0000155 O << (int)MI->getOperand(OpNo).getImmedValue()*4;
156 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000157 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera637c322005-12-16 00:22:14 +0000158 O << "\"L" << getFunctionNumber() << "$pb\"\n";
159 O << "\"L" << getFunctionNumber() << "$pb\":";
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000160 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000161 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman2497e632005-07-21 20:44:43 +0000162 if (MI->getOperand(OpNo).isImmediate()) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000163 printS16ImmOperand(MI, OpNo);
Nate Begeman2497e632005-07-21 20:44:43 +0000164 } else {
165 O << "ha16(";
166 printOp(MI->getOperand(OpNo));
Evan Cheng4c1aa862006-02-22 20:19:42 +0000167 if (TM.getRelocationModel() == Reloc::PIC)
Chris Lattnera637c322005-12-16 00:22:14 +0000168 O << "-\"L" << getFunctionNumber() << "$pb\")";
Nate Begeman2497e632005-07-21 20:44:43 +0000169 else
170 O << ')';
171 }
Nate Begemaned428532004-09-04 05:00:00 +0000172 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000173 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Nate Begemaned428532004-09-04 05:00:00 +0000174 if (MI->getOperand(OpNo).isImmediate()) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000175 printS16ImmOperand(MI, OpNo);
Nate Begemaned428532004-09-04 05:00:00 +0000176 } else {
177 O << "lo16(";
Nate Begemand4c8bea2004-11-25 07:09:01 +0000178 printOp(MI->getOperand(OpNo));
Evan Cheng4c1aa862006-02-22 20:19:42 +0000179 if (TM.getRelocationModel() == Reloc::PIC)
Chris Lattnera637c322005-12-16 00:22:14 +0000180 O << "-\"L" << getFunctionNumber() << "$pb\")";
Nate Begeman2497e632005-07-21 20:44:43 +0000181 else
182 O << ')';
Nate Begemaned428532004-09-04 05:00:00 +0000183 }
184 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000185 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
Nate Begemanadeb43d2005-07-20 22:42:00 +0000186 unsigned CCReg = MI->getOperand(OpNo).getReg();
187 unsigned RegNo = enumRegToMachineReg(CCReg);
188 O << (0x80 >> RegNo);
189 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000190 // The new addressing mode printers, currently empty
191 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
192 printSymbolLo(MI, OpNo);
193 O << '(';
194 printOperand(MI, OpNo+1);
195 O << ')';
196 }
197 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman88276b82005-12-19 23:40:42 +0000198 // When used as the base register, r0 reads constant zero rather than
199 // the value contained in the register. For this reason, the darwin
200 // assembler requires that we print r0 as 0 (no r) when used as the base.
201 const MachineOperand &MO = MI->getOperand(OpNo);
202 if (MO.getReg() == PPC::R0)
203 O << '0';
204 else
205 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000206 O << ", ";
207 printOperand(MI, OpNo+1);
208 }
209
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000210 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
Nate Begemaned428532004-09-04 05:00:00 +0000211 virtual bool doFinalization(Module &M) = 0;
Jim Laskeyb2efb852006-01-04 22:28:25 +0000212
Nate Begemaned428532004-09-04 05:00:00 +0000213 };
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000214
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000215 /// DarwinDwarfWriter - Dwarf debug info writer customized for Darwin/Mac OS X
216 ///
217 struct DarwinDwarfWriter : public DwarfWriter {
218 // Ctor.
Jim Laskeyb2efb852006-01-04 22:28:25 +0000219 DarwinDwarfWriter(std::ostream &o, AsmPrinter *ap)
220 : DwarfWriter(o, ap)
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000221 {
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000222 needsSet = true;
Jim Laskey0420f2a2006-02-22 19:02:11 +0000223 DwarfAbbrevSection = ".section __DWARFA,__debug_abbrev";
224 DwarfInfoSection = ".section __DWARFA,__debug_info";
225 DwarfLineSection = ".section __DWARFA,__debug_line";
226 DwarfFrameSection = ".section __DWARFA,__debug_frame";
227 DwarfPubNamesSection = ".section __DWARFA,__debug_pubnames";
228 DwarfPubTypesSection = ".section __DWARFA,__debug_pubtypes";
229 DwarfStrSection = ".section __DWARFA,__debug_str";
230 DwarfLocSection = ".section __DWARFA,__debug_loc";
231 DwarfARangesSection = ".section __DWARFA,__debug_aranges";
232 DwarfRangesSection = ".section __DWARFA,__debug_ranges";
233 DwarfMacInfoSection = ".section __DWARFA,__debug_macinfo";
Jim Laskey063e7652006-01-17 17:31:53 +0000234 TextSection = ".text";
235 DataSection = ".data";
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000236 }
237 };
238
Misha Brukman3e0b51a2004-09-05 02:42:44 +0000239 /// DarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac OS
240 /// X
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000241 struct DarwinAsmPrinter : public PPCAsmPrinter {
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000242
243 DarwinDwarfWriter DW;
Nate Begemaned428532004-09-04 05:00:00 +0000244
245 DarwinAsmPrinter(std::ostream &O, TargetMachine &TM)
Chris Lattner418caa12006-02-08 23:42:22 +0000246 : PPCAsmPrinter(O, TM), DW(O, this) {
Nate Begemaned428532004-09-04 05:00:00 +0000247 CommentString = ";";
248 GlobalPrefix = "_";
Chris Lattnerf55366e2005-11-21 06:47:58 +0000249 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
Nate Begemaned428532004-09-04 05:00:00 +0000250 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
251 Data64bitsDirective = 0; // we can't emit a 64-bit unit
252 AlignmentIsInBytes = false; // Alignment is by power of 2.
Chris Lattnerc569e612005-11-21 08:26:15 +0000253 ConstantPoolSection = "\t.const\t";
Chris Lattnerd717b192005-12-11 07:45:47 +0000254 LCOMMDirective = "\t.lcomm\t";
Chris Lattnerd1239b72005-12-13 06:32:50 +0000255 StaticCtorsSection = ".mod_init_func";
256 StaticDtorsSection = ".mod_term_func";
Chris Lattner418caa12006-02-08 23:42:22 +0000257 InlineAsmStart = InlineAsmEnd = ""; // Don't use #APP/#NO_APP
Nate Begemaned428532004-09-04 05:00:00 +0000258 }
259
260 virtual const char *getPassName() const {
261 return "Darwin PPC Assembly Printer";
262 }
Chris Lattner646f7af2005-12-09 18:24:29 +0000263
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000264 bool runOnMachineFunction(MachineFunction &F);
Nate Begeman18ed0292005-07-21 01:25:49 +0000265 bool doInitialization(Module &M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000266 bool doFinalization(Module &M);
Jim Laskeyb2efb852006-01-04 22:28:25 +0000267
268 void getAnalysisUsage(AnalysisUsage &AU) const {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000269 AU.setPreservesAll();
Jim Laskeyb2efb852006-01-04 22:28:25 +0000270 AU.addRequired<MachineDebugInfo>();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000271 PPCAsmPrinter::getAnalysisUsage(AU);
Jim Laskeyb2efb852006-01-04 22:28:25 +0000272 }
273
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000274 };
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000275
Misha Brukman3e0b51a2004-09-05 02:42:44 +0000276 /// AIXAsmPrinter - PowerPC assembly printer, customized for AIX
277 ///
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000278 struct AIXAsmPrinter : public PPCAsmPrinter {
Nate Begemaned428532004-09-04 05:00:00 +0000279 /// Map for labels corresponding to global variables
280 ///
281 std::map<const GlobalVariable*,std::string> GVToLabelMap;
282
283 AIXAsmPrinter(std::ostream &O, TargetMachine &TM)
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000284 : PPCAsmPrinter(O, TM) {
Nate Begemaned428532004-09-04 05:00:00 +0000285 CommentString = "#";
Chris Lattner3459bfb2005-11-10 18:20:29 +0000286 GlobalPrefix = ".";
Nate Begemaned428532004-09-04 05:00:00 +0000287 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
288 Data64bitsDirective = 0; // we can't emit a 64-bit unit
289 AlignmentIsInBytes = false; // Alignment is by power of 2.
Chris Lattnerc569e612005-11-21 08:26:15 +0000290 ConstantPoolSection = "\t.const\t";
Nate Begemaned428532004-09-04 05:00:00 +0000291 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000292
Nate Begemaned428532004-09-04 05:00:00 +0000293 virtual const char *getPassName() const {
294 return "AIX PPC Assembly Printer";
295 }
296
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000297 bool runOnMachineFunction(MachineFunction &F);
Nate Begemaned428532004-09-04 05:00:00 +0000298 bool doInitialization(Module &M);
299 bool doFinalization(Module &M);
300 };
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000301} // end of anonymous namespace
302
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000303/// createDarwinAsmPrinterPass - Returns a pass that prints the PPC assembly
304/// code for a MachineFunction to the given output stream, in a format that the
Nate Begemaned428532004-09-04 05:00:00 +0000305/// Darwin assembler can deal with.
306///
307FunctionPass *llvm::createDarwinAsmPrinter(std::ostream &o, TargetMachine &tm) {
308 return new DarwinAsmPrinter(o, tm);
309}
310
311/// createAIXAsmPrinterPass - Returns a pass that prints the PPC assembly code
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000312/// for a MachineFunction to the given output stream, in a format that the
Nate Begemaned428532004-09-04 05:00:00 +0000313/// AIX 5L assembler can deal with.
314///
315FunctionPass *llvm::createAIXAsmPrinter(std::ostream &o, TargetMachine &tm) {
316 return new AIXAsmPrinter(o, tm);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000317}
318
Nate Begemane59bf592004-08-14 22:09:10 +0000319// Include the auto-generated portion of the assembly writer
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000320#include "PPCGenAsmWriter.inc"
Nate Begemane59bf592004-08-14 22:09:10 +0000321
Chris Lattner9ba13e42005-11-17 19:25:59 +0000322void PPCAsmPrinter::printOp(const MachineOperand &MO) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000323 const MRegisterInfo &RI = *TM.getRegisterInfo();
324 int new_symbol;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000325
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000326 switch (MO.getType()) {
327 case MachineOperand::MO_VirtualRegister:
328 if (Value *V = MO.getVRegValueOrNull()) {
329 O << "<" << V->getName() << ">";
330 return;
331 }
332 // FALLTHROUGH
333 case MachineOperand::MO_MachineRegister:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000334 case MachineOperand::MO_CCRegister:
Chris Lattner9dc4d3c2005-08-22 22:00:02 +0000335 O << RI.get(MO.getReg()).Name;
Misha Brukman7f484a52004-06-24 23:51:00 +0000336 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000337
338 case MachineOperand::MO_SignExtendedImmed:
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000339 case MachineOperand::MO_UnextendedImmed:
340 std::cerr << "printOp() does not handle immediate values\n";
341 abort();
Misha Brukman97a296f2004-07-21 20:11:11 +0000342 return;
343
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000344 case MachineOperand::MO_PCRelativeDisp:
345 std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
346 abort();
347 return;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000348
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000349 case MachineOperand::MO_MachineBasicBlock: {
350 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
Chris Lattner07455362005-11-21 08:14:07 +0000351 O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << "_"
Chris Lattner7f9ccde2005-11-21 07:41:05 +0000352 << MBBOp->getNumber() << "\t; " << MBBOp->getBasicBlock()->getName();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000353 return;
354 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000355
356 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner07455362005-11-21 08:14:07 +0000357 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber()
Chris Lattnerf55366e2005-11-21 06:47:58 +0000358 << '_' << MO.getConstantPoolIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000359 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000360 case MachineOperand::MO_ExternalSymbol:
Chris Lattnera637c322005-12-16 00:22:14 +0000361 // Computing the address of an external symbol, not calling it.
Evan Cheng4c1aa862006-02-22 20:19:42 +0000362 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattnera637c322005-12-16 00:22:14 +0000363 std::string Name(GlobalPrefix); Name += MO.getSymbolName();
364 GVStubs.insert(Name);
365 O << "L" << Name << "$non_lazy_ptr";
366 return;
367 }
Nate Begeman01d05262005-03-30 01:45:43 +0000368 O << GlobalPrefix << MO.getSymbolName();
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000369 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000370 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera637c322005-12-16 00:22:14 +0000371 // Computing the address of a global symbol, not calling it.
Nate Begemanb73a7112004-08-13 09:32:01 +0000372 GlobalValue *GV = MO.getGlobal();
373 std::string Name = Mang->getValueName(GV);
Nate Begeman50fb3c42005-12-24 01:00:15 +0000374 int offset = MO.getOffset();
Misha Brukmane2eceb52004-07-23 16:08:20 +0000375
Nate Begemanfcf4a422004-10-17 23:01:34 +0000376 // External or weakly linked global variables need non-lazily-resolved stubs
Evan Cheng4c1aa862006-02-22 20:19:42 +0000377 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattnera637c322005-12-16 00:22:14 +0000378 if (((GV->isExternal() || GV->hasWeakLinkage() ||
379 GV->hasLinkOnceLinkage()))) {
Nate Begemand4c8bea2004-11-25 07:09:01 +0000380 GVStubs.insert(Name);
Chris Lattnera637c322005-12-16 00:22:14 +0000381 O << "L" << Name << "$non_lazy_ptr";
382 return;
383 }
Nate Begemanb73a7112004-08-13 09:32:01 +0000384 }
Nate Begemand4c8bea2004-11-25 07:09:01 +0000385
Chris Lattner9542f9712005-11-17 19:40:30 +0000386 O << Name;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000387 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000388 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000389
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000390 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000391 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000392 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000393 }
394}
395
Chris Lattnere3f01572006-02-23 19:31:10 +0000396/// PrintAsmOperand - Print out an operand for an inline asm expression.
397///
398bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
399 unsigned AsmVariant,
400 const char *ExtraCode) {
401 // Does this asm operand have a single letter operand modifier?
402 if (ExtraCode && ExtraCode[0]) {
403 if (ExtraCode[1] != 0) return true; // Unknown modifier.
404
405 switch (ExtraCode[0]) {
406 default: return true; // Unknown modifier.
407 case 'L': // Write second word of DImode reference.
408 // Verify that this operand has two consecutive registers.
409 if (!MI->getOperand(OpNo).isRegister() ||
410 OpNo+1 == MI->getNumOperands() ||
411 !MI->getOperand(OpNo+1).isRegister())
412 return true;
413 ++OpNo; // Return the high-part.
414 break;
415 }
416 }
417
418 printOperand(MI, OpNo);
419 return false;
420}
421
422
Nate Begemane59bf592004-08-14 22:09:10 +0000423/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
424/// the current output stream.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000425///
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000426void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Nate Begemane59bf592004-08-14 22:09:10 +0000427 ++EmittedInsts;
Chris Lattner617742b2005-10-14 22:44:13 +0000428
Nate Begemanad5f65c2005-04-05 18:19:50 +0000429 // Check for slwi/srwi mnemonics.
430 if (MI->getOpcode() == PPC::RLWINM) {
431 bool FoundMnemonic = false;
432 unsigned char SH = MI->getOperand(2).getImmedValue();
433 unsigned char MB = MI->getOperand(3).getImmedValue();
434 unsigned char ME = MI->getOperand(4).getImmedValue();
435 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
436 O << "slwi "; FoundMnemonic = true;
437 }
438 if (SH <= 31 && MB == (32-SH) && ME == 31) {
439 O << "srwi "; FoundMnemonic = true;
440 SH = 32-SH;
441 }
442 if (FoundMnemonic) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000443 printOperand(MI, 0);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000444 O << ", ";
Nate Begeman391c5d22005-11-30 18:54:35 +0000445 printOperand(MI, 1);
Nate Begemanad5f65c2005-04-05 18:19:50 +0000446 O << ", " << (unsigned int)SH << "\n";
447 return;
448 }
Chris Lattner4d73a5a2006-02-08 06:56:40 +0000449 } else if (MI->getOpcode() == PPC::OR4 || MI->getOpcode() == PPC::OR8) {
450 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
451 O << "mr ";
452 printOperand(MI, 0);
453 O << ", ";
454 printOperand(MI, 1);
455 O << "\n";
456 return;
457 }
Nate Begemanad5f65c2005-04-05 18:19:50 +0000458 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000459
Nate Begemane59bf592004-08-14 22:09:10 +0000460 if (printInstruction(MI))
461 return; // Printer was automatically generated
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000462
Nate Begemaned428532004-09-04 05:00:00 +0000463 assert(0 && "Unhandled instruction in asm writer!");
464 abort();
Nate Begemane59bf592004-08-14 22:09:10 +0000465 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000466}
467
Chris Lattneref658742005-11-21 07:57:37 +0000468
Nate Begemaned428532004-09-04 05:00:00 +0000469/// runOnMachineFunction - This uses the printMachineInstruction()
470/// method to print assembly for each instruction.
471///
472bool DarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000473 // FIXME - is this the earliest this can be set?
Jim Laskeyabf6d172006-01-05 01:25:28 +0000474 DW.SetDebugInfo(&getAnalysis<MachineDebugInfo>());
475
Chris Lattner8b8b9512005-11-21 07:51:23 +0000476 SetupMachineFunction(MF);
Nate Begemaned428532004-09-04 05:00:00 +0000477 O << "\n\n";
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000478
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000479 // Emit pre-function debug information.
Jim Laskey52060a02006-01-24 00:49:18 +0000480 DW.BeginFunction(MF);
Nate Begemaned428532004-09-04 05:00:00 +0000481
482 // Print out constants referenced by the function
Chris Lattnerc569e612005-11-21 08:26:15 +0000483 EmitConstantPool(MF.getConstantPool());
Nate Begemaned428532004-09-04 05:00:00 +0000484
485 // Print out labels for the function.
Chris Lattnerac7fd7f2005-11-14 18:52:46 +0000486 const Function *F = MF.getFunction();
Chris Lattnera637c322005-12-16 00:22:14 +0000487 switch (F->getLinkage()) {
488 default: assert(0 && "Unknown linkage type!");
489 case Function::InternalLinkage: // Symbols default to internal.
490 SwitchSection(".text", F);
Chris Lattnera637c322005-12-16 00:22:14 +0000491 break;
492 case Function::ExternalLinkage:
493 SwitchSection(".text", F);
Chris Lattnerf02a9162005-10-28 18:44:07 +0000494 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattnera637c322005-12-16 00:22:14 +0000495 break;
496 case Function::WeakLinkage:
497 case Function::LinkOnceLinkage:
498 SwitchSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions",
499 F);
500 O << "\t.globl\t" << CurrentFnName << "\n";
501 O << "\t.weak_definition\t" << CurrentFnName << "\n";
502 break;
503 }
Chris Lattner33d50822006-02-14 20:42:33 +0000504 EmitAlignment(4, F);
Nate Begemaned428532004-09-04 05:00:00 +0000505 O << CurrentFnName << ":\n";
506
507 // Print out code for the function.
508 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
509 I != E; ++I) {
510 // Print a label for the basic block.
Chris Lattner1db1adb2005-08-21 19:09:33 +0000511 if (I != MF.begin()) {
Chris Lattner07455362005-11-21 08:14:07 +0000512 O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << '_'
Chris Lattner7f9ccde2005-11-21 07:41:05 +0000513 << I->getNumber() << ":\t";
Chris Lattner1db1adb2005-08-21 19:09:33 +0000514 if (!I->getBasicBlock()->getName().empty())
515 O << CommentString << " " << I->getBasicBlock()->getName();
516 O << "\n";
517 }
Nate Begemaned428532004-09-04 05:00:00 +0000518 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
519 II != E; ++II) {
520 // Print the assembly for the instruction.
521 O << "\t";
522 printMachineInstruction(II);
523 }
524 }
Nate Begemaned428532004-09-04 05:00:00 +0000525
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000526 // Emit post-function debug information.
Jim Laskey52060a02006-01-24 00:49:18 +0000527 DW.EndFunction(MF);
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000528
Nate Begemaned428532004-09-04 05:00:00 +0000529 // We didn't modify anything.
530 return false;
531}
532
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000533
Nate Begeman18ed0292005-07-21 01:25:49 +0000534bool DarwinAsmPrinter::doInitialization(Module &M) {
Chris Lattner3c304a32005-08-05 22:05:03 +0000535 if (TM.getSubtarget<PPCSubtarget>().isGigaProcessor())
536 O << "\t.machine ppc970\n";
Nate Begeman18ed0292005-07-21 01:25:49 +0000537 AsmPrinter::doInitialization(M);
Chris Lattner85eac0d2005-11-10 19:33:43 +0000538
539 // Darwin wants symbols to be quoted if they have complex names.
540 Mang->setUseQuotes(true);
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000541
542 // Emit initial debug information.
Jim Laskey52060a02006-01-24 00:49:18 +0000543 DW.BeginModule(M);
Nate Begeman18ed0292005-07-21 01:25:49 +0000544 return false;
545}
546
Nate Begemaned428532004-09-04 05:00:00 +0000547bool DarwinAsmPrinter::doFinalization(Module &M) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000548 const TargetData &TD = TM.getTargetData();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000549
550 // Print out module-level global variables here.
Chris Lattnerced704b2005-11-14 19:00:30 +0000551 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Chris Lattnerdeea4162005-12-13 04:33:58 +0000552 I != E; ++I) {
553 if (!I->hasInitializer()) continue; // External global require no code
554
Chris Lattnerd1239b72005-12-13 06:32:50 +0000555 // Check to see if this is a special global used by LLVM, if so, emit it.
556 if (I->hasAppendingLinkage() && EmitSpecialLLVMGlobal(I))
557 continue;
Chris Lattnerdeea4162005-12-13 04:33:58 +0000558
Chris Lattnerdeea4162005-12-13 04:33:58 +0000559 std::string name = Mang->getValueName(I);
560 Constant *C = I->getInitializer();
561 unsigned Size = TD.getTypeSize(C->getType());
Chris Lattner7d8d5a52006-02-05 01:30:45 +0000562 unsigned Align = getPreferredAlignmentLog(I);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000563
564 if (C->isNullValue() && /* FIXME: Verify correct */
565 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
Chris Lattner0d7db6f2006-02-14 22:18:23 +0000566 I->hasLinkOnceLinkage() ||
567 (I->hasExternalLinkage() && !I->hasSection()))) {
Chris Lattnerdeea4162005-12-13 04:33:58 +0000568 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Chris Lattner0d7db6f2006-02-14 22:18:23 +0000569 if (I->hasExternalLinkage()) {
570 O << "\t.globl " << name << '\n';
571 O << "\t.zerofill __DATA, __common, " << name << ", "
572 << Size << ", " << Align;
573 } else if (I->hasInternalLinkage()) {
574 SwitchSection(".data", I);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000575 O << LCOMMDirective << name << "," << Size << "," << Align;
Chris Lattner0d7db6f2006-02-14 22:18:23 +0000576 } else {
577 SwitchSection(".data", I);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000578 O << ".comm " << name << "," << Size;
Chris Lattner0d7db6f2006-02-14 22:18:23 +0000579 }
Chris Lattnerdeea4162005-12-13 04:33:58 +0000580 O << "\t\t; '" << I->getName() << "'\n";
581 } else {
582 switch (I->getLinkage()) {
583 case GlobalValue::LinkOnceLinkage:
Chris Lattnerdeea4162005-12-13 04:33:58 +0000584 case GlobalValue::WeakLinkage:
Chris Lattnercec26fc2005-12-22 21:15:17 +0000585 O << "\t.globl " << name << '\n'
586 << "\t.weak_definition " << name << '\n';
Chris Lattnera637c322005-12-16 00:22:14 +0000587 SwitchSection(".section __DATA,__datacoal_nt,coalesced", I);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000588 break;
589 case GlobalValue::AppendingLinkage:
590 // FIXME: appending linkage variables should go into a section of
591 // their name or something. For now, just emit them as external.
592 case GlobalValue::ExternalLinkage:
593 // If external or appending, declare as a global symbol
594 O << "\t.globl " << name << "\n";
595 // FALL THROUGH
596 case GlobalValue::InternalLinkage:
597 SwitchSection(".data", I);
598 break;
599 default:
600 std::cerr << "Unknown linkage type!";
601 abort();
602 }
603
604 EmitAlignment(Align, I);
605 O << name << ":\t\t\t\t; '" << I->getName() << "'\n";
606 EmitGlobalConstant(C);
Chris Lattnerbc1c2152006-01-21 01:35:26 +0000607 O << '\n';
Chris Lattnerdeea4162005-12-13 04:33:58 +0000608 }
609 }
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000610
611 // Output stubs for dynamically-linked functions
Evan Cheng4c1aa862006-02-22 20:19:42 +0000612 if (TM.getRelocationModel() == Reloc::PIC) {
Chris Lattnerdeea4162005-12-13 04:33:58 +0000613 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
614 i != e; ++i) {
Chris Lattnera637c322005-12-16 00:22:14 +0000615 SwitchSection(".section __TEXT,__picsymbolstub1,symbol_stubs,"
616 "pure_instructions,32", 0);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000617 EmitAlignment(2);
618 O << "L" << *i << "$stub:\n";
619 O << "\t.indirect_symbol " << *i << "\n";
620 O << "\tmflr r0\n";
621 O << "\tbcl 20,31,L0$" << *i << "\n";
622 O << "L0$" << *i << ":\n";
623 O << "\tmflr r11\n";
624 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
625 O << "\tmtlr r0\n";
626 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
627 O << "\tmtctr r12\n";
628 O << "\tbctr\n";
Chris Lattnera637c322005-12-16 00:22:14 +0000629 SwitchSection(".lazy_symbol_pointer", 0);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000630 O << "L" << *i << "$lazy_ptr:\n";
631 O << "\t.indirect_symbol " << *i << "\n";
632 O << "\t.long dyld_stub_binding_helper\n";
633 }
634 } else {
635 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
636 i != e; ++i) {
Chris Lattnera637c322005-12-16 00:22:14 +0000637 SwitchSection(".section __TEXT,__symbol_stub1,symbol_stubs,"
638 "pure_instructions,16", 0);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000639 EmitAlignment(4);
640 O << "L" << *i << "$stub:\n";
641 O << "\t.indirect_symbol " << *i << "\n";
642 O << "\tlis r11,ha16(L" << *i << "$lazy_ptr)\n";
643 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr)(r11)\n";
644 O << "\tmtctr r12\n";
645 O << "\tbctr\n";
Chris Lattnera637c322005-12-16 00:22:14 +0000646 SwitchSection(".lazy_symbol_pointer", 0);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000647 O << "L" << *i << "$lazy_ptr:\n";
648 O << "\t.indirect_symbol " << *i << "\n";
649 O << "\t.long dyld_stub_binding_helper\n";
Nate Begeman2497e632005-07-21 20:44:43 +0000650 }
Misha Brukman46fd00a2004-06-24 23:04:11 +0000651 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000652
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000653 O << "\n";
654
Chris Lattnera637c322005-12-16 00:22:14 +0000655 // Output stubs for external and common global variables.
656 if (GVStubs.begin() != GVStubs.end()) {
657 SwitchSection(".non_lazy_symbol_pointer", 0);
658 for (std::set<std::string>::iterator I = GVStubs.begin(),
659 E = GVStubs.end(); I != E; ++I) {
660 O << "L" << *I << "$non_lazy_ptr:\n";
661 O << "\t.indirect_symbol " << *I << "\n";
662 O << "\t.long\t0\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +0000663 }
Nate Begemane59bf592004-08-14 22:09:10 +0000664 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000665
Jim Laskey063e7652006-01-17 17:31:53 +0000666 // Emit initial debug information.
Jim Laskey52060a02006-01-24 00:49:18 +0000667 DW.EndModule(M);
Jim Laskey063e7652006-01-17 17:31:53 +0000668
Chris Lattner5b0ac992005-11-01 00:12:36 +0000669 // Funny Darwin hack: This flag tells the linker that no global symbols
670 // contain code that falls through to other global symbols (e.g. the obvious
671 // implementation of multiple entry points). If this doesn't occur, the
672 // linker can safely perform dead code stripping. Since LLVM never generates
673 // code that does this, it is always safe to set.
674 O << "\t.subsections_via_symbols\n";
675
Chris Lattnera3840792004-08-16 23:25:21 +0000676 AsmPrinter::doFinalization(M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000677 return false; // success
678}
Nate Begemaned428532004-09-04 05:00:00 +0000679
Jim Laskeyb2efb852006-01-04 22:28:25 +0000680/// runOnMachineFunction - This uses the e()
Nate Begemaned428532004-09-04 05:00:00 +0000681/// method to print assembly for each instruction.
682///
683bool AIXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner6d5a4f62005-11-21 08:02:41 +0000684 SetupMachineFunction(MF);
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000685
Nate Begemaned428532004-09-04 05:00:00 +0000686 // Print out constants referenced by the function
Chris Lattnerc569e612005-11-21 08:26:15 +0000687 EmitConstantPool(MF.getConstantPool());
Nate Begemaned428532004-09-04 05:00:00 +0000688
689 // Print out header for the function.
690 O << "\t.csect .text[PR]\n"
691 << "\t.align 2\n"
692 << "\t.globl " << CurrentFnName << '\n'
693 << "\t.globl ." << CurrentFnName << '\n'
694 << "\t.csect " << CurrentFnName << "[DS],3\n"
695 << CurrentFnName << ":\n"
696 << "\t.llong ." << CurrentFnName << ", TOC[tc0], 0\n"
697 << "\t.csect .text[PR]\n"
698 << '.' << CurrentFnName << ":\n";
699
700 // Print out code for the function.
701 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
702 I != E; ++I) {
703 // Print a label for the basic block.
Chris Lattner07455362005-11-21 08:14:07 +0000704 O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << '_'
705 << I->getNumber()
Chris Lattner6d5a4f62005-11-21 08:02:41 +0000706 << ":\t" << CommentString << I->getBasicBlock()->getName() << '\n';
Nate Begemaned428532004-09-04 05:00:00 +0000707 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
708 II != E; ++II) {
709 // Print the assembly for the instruction.
710 O << "\t";
711 printMachineInstruction(II);
712 }
713 }
Nate Begemaned428532004-09-04 05:00:00 +0000714
715 O << "LT.." << CurrentFnName << ":\n"
716 << "\t.long 0\n"
717 << "\t.byte 0,0,32,65,128,0,0,0\n"
718 << "\t.long LT.." << CurrentFnName << "-." << CurrentFnName << '\n'
719 << "\t.short 3\n"
720 << "\t.byte \"" << CurrentFnName << "\"\n"
721 << "\t.align 2\n";
722
723 // We didn't modify anything.
724 return false;
725}
726
Nate Begemaned428532004-09-04 05:00:00 +0000727bool AIXAsmPrinter::doInitialization(Module &M) {
Chris Lattnerac7fd7f2005-11-14 18:52:46 +0000728 SwitchSection("", 0);
Nate Begemaned428532004-09-04 05:00:00 +0000729 const TargetData &TD = TM.getTargetData();
Nate Begemaned428532004-09-04 05:00:00 +0000730
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000731 O << "\t.machine \"ppc64\"\n"
Nate Begemaned428532004-09-04 05:00:00 +0000732 << "\t.toc\n"
733 << "\t.csect .text[PR]\n";
734
735 // Print out module-level global variables
Chris Lattner2e00d7d2005-07-26 19:03:27 +0000736 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
737 I != E; ++I) {
Nate Begemaned428532004-09-04 05:00:00 +0000738 if (!I->hasInitializer())
739 continue;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000740
Nate Begemaned428532004-09-04 05:00:00 +0000741 std::string Name = I->getName();
742 Constant *C = I->getInitializer();
743 // N.B.: We are defaulting to writable strings
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000744 if (I->hasExternalLinkage()) {
Nate Begemaned428532004-09-04 05:00:00 +0000745 O << "\t.globl " << Name << '\n'
746 << "\t.csect .data[RW],3\n";
747 } else {
748 O << "\t.csect _global.rw_c[RW],3\n";
749 }
750 O << Name << ":\n";
Chris Lattner8b8b9512005-11-21 07:51:23 +0000751 EmitGlobalConstant(C);
Nate Begemaned428532004-09-04 05:00:00 +0000752 }
753
754 // Output labels for globals
Chris Lattnere4d5c442005-03-15 04:54:21 +0000755 if (M.global_begin() != M.global_end()) O << "\t.toc\n";
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 const GlobalVariable *GV = I;
759 // Do not output labels for unused variables
760 if (GV->isExternal() && GV->use_begin() == GV->use_end())
761 continue;
762
Chris Lattner07455362005-11-21 08:14:07 +0000763 IncrementFunctionNumber();
Nate Begemaned428532004-09-04 05:00:00 +0000764 std::string Name = GV->getName();
Chris Lattner07455362005-11-21 08:14:07 +0000765 std::string Label = "LC.." + utostr(getFunctionNumber());
Nate Begemaned428532004-09-04 05:00:00 +0000766 GVToLabelMap[GV] = Label;
767 O << Label << ":\n"
768 << "\t.tc " << Name << "[TC]," << Name;
769 if (GV->isExternal()) O << "[RW]";
770 O << '\n';
Chris Lattner07455362005-11-21 08:14:07 +0000771 }
Nate Begemaned428532004-09-04 05:00:00 +0000772
Chris Lattner3459bfb2005-11-10 18:20:29 +0000773 AsmPrinter::doInitialization(M);
Nate Begemaned428532004-09-04 05:00:00 +0000774 return false; // success
775}
776
777bool AIXAsmPrinter::doFinalization(Module &M) {
778 const TargetData &TD = TM.getTargetData();
779 // Print out module-level global variables
Chris Lattner2e00d7d2005-07-26 19:03:27 +0000780 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
781 I != E; ++I) {
Nate Begemaned428532004-09-04 05:00:00 +0000782 if (I->hasInitializer() || I->hasExternalLinkage())
783 continue;
784
785 std::string Name = I->getName();
786 if (I->hasInternalLinkage()) {
787 O << "\t.lcomm " << Name << ",16,_global.bss_c";
788 } else {
789 O << "\t.comm " << Name << "," << TD.getTypeSize(I->getType())
Chris Lattner0561b3f2005-08-02 19:26:06 +0000790 << "," << Log2_32((unsigned)TD.getTypeAlignment(I->getType()));
Nate Begemaned428532004-09-04 05:00:00 +0000791 }
Chris Lattner6d5a4f62005-11-21 08:02:41 +0000792 O << "\t\t" << CommentString << " ";
Chris Lattner8ca02912005-10-03 07:08:36 +0000793 WriteAsOperand(O, I, false, true, &M);
Nate Begemaned428532004-09-04 05:00:00 +0000794 O << "\n";
795 }
796
797 O << "_section_.text:\n"
798 << "\t.csect .data[RW],3\n"
799 << "\t.llong _section_.text\n";
Chris Lattner3459bfb2005-11-10 18:20:29 +0000800 AsmPrinter::doFinalization(M);
Nate Begemaned428532004-09-04 05:00:00 +0000801 return false; // success
802}