blob: fd86f1b59cd9985ae6ace0491cadccb422d04b69 [file] [log] [blame]
Nate Begemaned428532004-09-04 05:00:00 +00001//===-- PowerPCAsmPrinter.cpp - Print machine instrs to PowerPC assembly --===//
Misha Brukman5dfe3a92004-06-21 16:55:25 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000020#include "PowerPC.h"
Nate Begemaned428532004-09-04 05:00:00 +000021#include "PowerPCTargetMachine.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000022#include "llvm/Constants.h"
23#include "llvm/DerivedTypes.h"
24#include "llvm/Module.h"
25#include "llvm/Assembly/Writer.h"
Chris Lattnera3840792004-08-16 23:25:21 +000026#include "llvm/CodeGen/AsmPrinter.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000027#include "llvm/CodeGen/MachineConstantPool.h"
Misha Brukman05794492004-06-24 17:31:42 +000028#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000029#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner7bb424f2004-08-14 23:27:29 +000030#include "llvm/CodeGen/ValueTypes.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000031#include "llvm/Support/Mangler.h"
Nate Begemana11c2e82004-09-04 14:51:26 +000032#include "llvm/Support/MathExtras.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000033#include "llvm/Support/CommandLine.h"
34#include "llvm/Support/Debug.h"
Nate Begeman17304c32004-10-26 06:02:38 +000035#include "llvm/Target/MRegisterInfo.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000036#include "llvm/ADT/Statistic.h"
37#include "llvm/ADT/StringExtras.h"
Misha Brukman05794492004-06-24 17:31:42 +000038#include <set>
Chris Lattnera3840792004-08-16 23:25:21 +000039using namespace llvm;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000040
41namespace {
42 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
43
Misha Brukman3e0b51a2004-09-05 02:42:44 +000044 struct PowerPCAsmPrinter : public AsmPrinter {
Misha Brukman97a296f2004-07-21 20:11:11 +000045 std::set<std::string> FnStubs, GVStubs, LinkOnceStubs;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000046 std::set<std::string> Strings;
Nate Begemaned428532004-09-04 05:00:00 +000047
Misha Brukman3e0b51a2004-09-05 02:42:44 +000048 PowerPCAsmPrinter(std::ostream &O, TargetMachine &TM)
Nate Begemaned428532004-09-04 05:00:00 +000049 : AsmPrinter(O, TM), LabelNumber(0) {}
Misha Brukman5dfe3a92004-06-21 16:55:25 +000050
Misha Brukmancf8d2442004-07-26 16:28:33 +000051 /// Unique incrementer for label values for referencing Global values.
Misha Brukman218bec72004-06-29 17:13:26 +000052 ///
Misha Brukmancf8d2442004-07-26 16:28:33 +000053 unsigned LabelNumber;
54
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 Begemaned428532004-09-04 05:00:00 +000059 PowerPCTargetMachine &getTM() {
60 return static_cast<PowerPCTargetMachine&>(TM);
Chris Lattnera3840792004-08-16 23:25:21 +000061 }
62
Nate Begemane59bf592004-08-14 22:09:10 +000063 /// printInstruction - This method is automatically generated by tablegen
64 /// from the instruction set description. This method returns true if the
65 /// machine instruction was sufficiently described to print it, otherwise it
66 /// returns false.
67 bool printInstruction(const MachineInstr *MI);
68
Misha Brukman5dfe3a92004-06-21 16:55:25 +000069 void printMachineInstruction(const MachineInstr *MI);
Nate Begemanb73a7112004-08-13 09:32:01 +000070 void printOp(const MachineOperand &MO, bool LoadAddrOp = false);
Chris Lattner7bb424f2004-08-14 23:27:29 +000071
72 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
73 const MachineOperand &MO = MI->getOperand(OpNo);
74 if (MO.getType() == MachineOperand::MO_MachineRegister) {
75 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
76 O << LowercaseString(TM.getRegisterInfo()->get(MO.getReg()).Name);
Chris Lattner0ea31712004-08-15 05:46:14 +000077 } else if (MO.isImmediate()) {
78 O << MO.getImmedValue();
Chris Lattner7bb424f2004-08-14 23:27:29 +000079 } else {
80 printOp(MO);
81 }
82 }
83
Nate Begemanc3306122004-08-21 05:56:39 +000084 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo,
85 MVT::ValueType VT) {
86 unsigned char value = MI->getOperand(OpNo).getImmedValue();
Chris Lattner12585ba2004-08-21 19:11:03 +000087 assert(value <= 31 && "Invalid u5imm argument!");
Nate Begemanc3306122004-08-21 05:56:39 +000088 O << (unsigned int)value;
89 }
Nate Begeman07aada82004-08-30 02:28:06 +000090 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo,
91 MVT::ValueType VT) {
92 unsigned char value = MI->getOperand(OpNo).getImmedValue();
93 assert(value <= 63 && "Invalid u6imm argument!");
94 O << (unsigned int)value;
95 }
Nate Begemaned428532004-09-04 05:00:00 +000096 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo,
97 MVT::ValueType VT) {
98 O << (short)MI->getOperand(OpNo).getImmedValue();
99 }
Chris Lattner97b2a2e2004-08-15 05:20:16 +0000100 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo,
101 MVT::ValueType VT) {
102 O << (unsigned short)MI->getOperand(OpNo).getImmedValue();
103 }
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000104 void printBranchOperand(const MachineInstr *MI, unsigned OpNo,
Misha Brukman3a060e52004-10-23 04:58:32 +0000105 MVT::ValueType VT) {
Nate Begemaned428532004-09-04 05:00:00 +0000106 // Branches can take an immediate operand. This is used by the branch
107 // selection pass to print $+8, an eight byte displacement from the PC.
108 if (MI->getOperand(OpNo).isImmediate()) {
109 O << "$+" << MI->getOperand(OpNo).getImmedValue() << '\n';
110 } else {
111 printOp(MI->getOperand(OpNo));
112 }
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000113 }
114 void printPICLabel(const MachineInstr *MI, unsigned OpNo,
Misha Brukman3a060e52004-10-23 04:58:32 +0000115 MVT::ValueType VT) {
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000116 // FIXME: should probably be converted to cout.width and cout.fill
117 O << "\"L0000" << LabelNumber << "$pb\"\n";
118 O << "\"L0000" << LabelNumber << "$pb\":";
119 }
Nate Begemaned428532004-09-04 05:00:00 +0000120 void printSymbolHi(const MachineInstr *MI, unsigned OpNo,
Misha Brukman3a060e52004-10-23 04:58:32 +0000121 MVT::ValueType VT) {
Nate Begemaned428532004-09-04 05:00:00 +0000122 O << "ha16(";
123 printOp(MI->getOperand(OpNo), true /* LoadAddrOp */);
124 O << "-\"L0000" << LabelNumber << "$pb\")";
125 }
126 void printSymbolLo(const MachineInstr *MI, unsigned OpNo,
Misha Brukman3a060e52004-10-23 04:58:32 +0000127 MVT::ValueType VT) {
Nate Begemaned428532004-09-04 05:00:00 +0000128 // FIXME: Because LFS, LFD, and LWZ can be used either with a s16imm or
129 // a lo16 of a global or constant pool operand, we must handle both here.
130 // this isn't a great design, but it works for now.
131 if (MI->getOperand(OpNo).isImmediate()) {
132 O << (short)MI->getOperand(OpNo).getImmedValue();
133 } else {
134 O << "lo16(";
135 printOp(MI->getOperand(OpNo), true /* LoadAddrOp */);
136 O << "-\"L0000" << LabelNumber << "$pb\")";
137 }
138 }
139
140 virtual void printConstantPool(MachineConstantPool *MCP) = 0;
141 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
142 virtual bool doFinalization(Module &M) = 0;
143 };
144
Misha Brukman3e0b51a2004-09-05 02:42:44 +0000145 /// DarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac OS
146 /// X
147 ///
148 struct DarwinAsmPrinter : public PowerPCAsmPrinter {
Nate Begemaned428532004-09-04 05:00:00 +0000149
150 DarwinAsmPrinter(std::ostream &O, TargetMachine &TM)
Misha Brukman3e0b51a2004-09-05 02:42:44 +0000151 : PowerPCAsmPrinter(O, TM) {
Nate Begemaned428532004-09-04 05:00:00 +0000152 CommentString = ";";
153 GlobalPrefix = "_";
154 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
155 Data64bitsDirective = 0; // we can't emit a 64-bit unit
156 AlignmentIsInBytes = false; // Alignment is by power of 2.
157 }
158
159 virtual const char *getPassName() const {
160 return "Darwin PPC Assembly Printer";
161 }
Chris Lattner97b2a2e2004-08-15 05:20:16 +0000162
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000163 void printConstantPool(MachineConstantPool *MCP);
164 bool runOnMachineFunction(MachineFunction &F);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000165 bool doFinalization(Module &M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000166 };
Nate Begemaned428532004-09-04 05:00:00 +0000167
Misha Brukman3e0b51a2004-09-05 02:42:44 +0000168 /// AIXAsmPrinter - PowerPC assembly printer, customized for AIX
169 ///
170 struct AIXAsmPrinter : public PowerPCAsmPrinter {
Nate Begemaned428532004-09-04 05:00:00 +0000171 /// Map for labels corresponding to global variables
172 ///
173 std::map<const GlobalVariable*,std::string> GVToLabelMap;
174
175 AIXAsmPrinter(std::ostream &O, TargetMachine &TM)
Misha Brukman3e0b51a2004-09-05 02:42:44 +0000176 : PowerPCAsmPrinter(O, TM) {
Nate Begemaned428532004-09-04 05:00:00 +0000177 CommentString = "#";
178 GlobalPrefix = "_";
179 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
180 Data64bitsDirective = 0; // we can't emit a 64-bit unit
181 AlignmentIsInBytes = false; // Alignment is by power of 2.
182 }
183
184 virtual const char *getPassName() const {
185 return "AIX PPC Assembly Printer";
186 }
187
188 void printConstantPool(MachineConstantPool *MCP);
189 bool runOnMachineFunction(MachineFunction &F);
190 bool doInitialization(Module &M);
191 bool doFinalization(Module &M);
192 };
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000193} // end of anonymous namespace
194
Nate Begemaned428532004-09-04 05:00:00 +0000195// SwitchSection - Switch to the specified section of the executable if we are
196// not already in it!
197//
198static void SwitchSection(std::ostream &OS, std::string &CurSection,
199 const char *NewSection) {
200 if (CurSection != NewSection) {
201 CurSection = NewSection;
202 if (!CurSection.empty())
203 OS << "\t" << NewSection << "\n";
204 }
205}
206
207/// isStringCompatible - Can we treat the specified array as a string?
208/// Only if it is an array of ubytes or non-negative sbytes.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000209///
Nate Begemaned428532004-09-04 05:00:00 +0000210static bool isStringCompatible(const ConstantArray *CVA) {
211 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
212 if (ETy == Type::UByteTy) return true;
213 if (ETy != Type::SByteTy) return false;
214
215 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
216 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
217 return false;
218
219 return true;
220}
221
222/// toOctal - Convert the low order bits of X into an octal digit.
223///
224static inline char toOctal(int X) {
225 return (X&7)+'0';
226}
227
228// Possible states while outputting ASCII strings
229namespace {
230 enum StringSection {
231 None,
232 Alpha,
233 Numeric
234 };
235}
236
237/// SwitchStringSection - manage the changes required to output bytes as
238/// characters in a string vs. numeric decimal values
239///
240static inline void SwitchStringSection(std::ostream &O, StringSection NewSect,
241 StringSection &Current) {
242 if (Current == None) {
243 if (NewSect == Alpha)
244 O << "\t.byte \"";
245 else if (NewSect == Numeric)
246 O << "\t.byte ";
247 } else if (Current == Alpha) {
248 if (NewSect == None)
249 O << "\"";
250 else if (NewSect == Numeric)
251 O << "\"\n"
252 << "\t.byte ";
253 } else if (Current == Numeric) {
254 if (NewSect == Alpha)
255 O << '\n'
256 << "\t.byte \"";
257 else if (NewSect == Numeric)
258 O << ", ";
259 }
260
261 Current = NewSect;
262}
263
264/// getAsCString - Return the specified array as a C compatible
265/// string, only if the predicate isStringCompatible is true.
266///
267static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
268 assert(isStringCompatible(CVA) && "Array is not string compatible!");
269
270 if (CVA->getNumOperands() == 0)
271 return;
272
273 StringSection Current = None;
274 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i) {
275 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
276 if (C == '"') {
277 SwitchStringSection(O, Alpha, Current);
278 O << "\"\"";
279 } else if (isprint(C)) {
280 SwitchStringSection(O, Alpha, Current);
281 O << C;
282 } else {
283 SwitchStringSection(O, Numeric, Current);
284 O << utostr((unsigned)C);
285 }
286 }
287 SwitchStringSection(O, None, Current);
288 O << '\n';
289}
290
291/// createDarwinAsmPrinterPass - Returns a pass that prints the PPC assembly
292/// code for a MachineFunction to the given output stream, in a format that the
293/// Darwin assembler can deal with.
294///
295FunctionPass *llvm::createDarwinAsmPrinter(std::ostream &o, TargetMachine &tm) {
296 return new DarwinAsmPrinter(o, tm);
297}
298
299/// createAIXAsmPrinterPass - Returns a pass that prints the PPC assembly code
300/// for a MachineFunction to the given output stream, in a format that the
301/// AIX 5L assembler can deal with.
302///
303FunctionPass *llvm::createAIXAsmPrinter(std::ostream &o, TargetMachine &tm) {
304 return new AIXAsmPrinter(o, tm);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000305}
306
Nate Begemane59bf592004-08-14 22:09:10 +0000307// Include the auto-generated portion of the assembly writer
308#include "PowerPCGenAsmWriter.inc"
309
Misha Brukman3e0b51a2004-09-05 02:42:44 +0000310void PowerPCAsmPrinter::printOp(const MachineOperand &MO,
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000311 bool LoadAddrOp /* = false */) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000312 const MRegisterInfo &RI = *TM.getRegisterInfo();
313 int new_symbol;
314
315 switch (MO.getType()) {
316 case MachineOperand::MO_VirtualRegister:
317 if (Value *V = MO.getVRegValueOrNull()) {
318 O << "<" << V->getName() << ">";
319 return;
320 }
321 // FALLTHROUGH
322 case MachineOperand::MO_MachineRegister:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000323 case MachineOperand::MO_CCRegister:
Misha Brukman7f484a52004-06-24 23:51:00 +0000324 O << LowercaseString(RI.get(MO.getReg()).Name);
325 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000326
327 case MachineOperand::MO_SignExtendedImmed:
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000328 case MachineOperand::MO_UnextendedImmed:
329 std::cerr << "printOp() does not handle immediate values\n";
330 abort();
Misha Brukman97a296f2004-07-21 20:11:11 +0000331 return;
332
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000333 case MachineOperand::MO_PCRelativeDisp:
334 std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
335 abort();
336 return;
337
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000338 case MachineOperand::MO_MachineBasicBlock: {
339 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
340 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
Misha Brukman218bec72004-06-29 17:13:26 +0000341 << "_" << MBBOp->getNumber() << "\t; "
Misha Brukman2bf183c2004-06-25 15:42:10 +0000342 << MBBOp->getBasicBlock()->getName();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000343 return;
344 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000345
346 case MachineOperand::MO_ConstantPoolIndex:
347 O << ".CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000348 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000349
350 case MachineOperand::MO_ExternalSymbol:
351 O << MO.getSymbolName();
352 return;
353
Nate Begemanb73a7112004-08-13 09:32:01 +0000354 case MachineOperand::MO_GlobalAddress: {
355 GlobalValue *GV = MO.getGlobal();
356 std::string Name = Mang->getValueName(GV);
Misha Brukmane2eceb52004-07-23 16:08:20 +0000357
Nate Begemanb73a7112004-08-13 09:32:01 +0000358 // Dynamically-resolved functions need a stub for the function. Be
359 // wary however not to output $stub for external functions whose addresses
360 // are taken. Those should be emitted as $non_lazy_ptr below.
361 Function *F = dyn_cast<Function>(GV);
362 if (F && F->isExternal() && !LoadAddrOp &&
Chris Lattnera3840792004-08-16 23:25:21 +0000363 getTM().CalledFunctions.count(F)) {
Nate Begemanb73a7112004-08-13 09:32:01 +0000364 FnStubs.insert(Name);
365 O << "L" << Name << "$stub";
366 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000367 }
Nate Begemane59bf592004-08-14 22:09:10 +0000368
Nate Begemanfcf4a422004-10-17 23:01:34 +0000369 // External or weakly linked global variables need non-lazily-resolved stubs
370 if ((GV->isExternal() || GV->hasWeakLinkage()) &&
371 getTM().AddressTaken.count(GV)) {
Nate Begemanb73a7112004-08-13 09:32:01 +0000372 GVStubs.insert(Name);
373 O << "L" << Name << "$non_lazy_ptr";
374 return;
375 }
Nate Begemane59bf592004-08-14 22:09:10 +0000376
Chris Lattnera3840792004-08-16 23:25:21 +0000377 if (F && LoadAddrOp && getTM().AddressTaken.count(GV)) {
Nate Begemane59bf592004-08-14 22:09:10 +0000378 LinkOnceStubs.insert(Name);
379 O << "L" << Name << "$non_lazy_ptr";
380 return;
381 }
Nate Begemanb73a7112004-08-13 09:32:01 +0000382
383 O << Mang->getValueName(GV);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000384 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000385 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000386
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000387 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000388 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000389 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000390 }
391}
392
Nate Begemane59bf592004-08-14 22:09:10 +0000393/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
394/// the current output stream.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000395///
Misha Brukman3e0b51a2004-09-05 02:42:44 +0000396void PowerPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Nate Begemane59bf592004-08-14 22:09:10 +0000397 ++EmittedInsts;
398 if (printInstruction(MI))
399 return; // Printer was automatically generated
Nate Begemaned428532004-09-04 05:00:00 +0000400
401 assert(0 && "Unhandled instruction in asm writer!");
402 abort();
Nate Begemane59bf592004-08-14 22:09:10 +0000403 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000404}
405
Nate Begemaned428532004-09-04 05:00:00 +0000406/// runOnMachineFunction - This uses the printMachineInstruction()
407/// method to print assembly for each instruction.
408///
409bool DarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
410 setupMachineFunction(MF);
411 O << "\n\n";
412
413 // Print out constants referenced by the function
414 printConstantPool(MF.getConstantPool());
415
416 // Print out labels for the function.
417 O << "\t.text\n";
418 emitAlignment(2);
419 O << "\t.globl\t" << CurrentFnName << "\n";
420 O << CurrentFnName << ":\n";
421
422 // Print out code for the function.
423 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
424 I != E; ++I) {
425 // Print a label for the basic block.
426 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
427 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
428 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
429 II != E; ++II) {
430 // Print the assembly for the instruction.
431 O << "\t";
432 printMachineInstruction(II);
433 }
434 }
435 ++LabelNumber;
436
437 // We didn't modify anything.
438 return false;
439}
440
441/// printConstantPool - Print to the current output stream assembly
442/// representations of the constants in the constant pool MCP. This is
443/// used to print out constants which have been "spilled to memory" by
444/// the code generator.
445///
446void DarwinAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
447 const std::vector<Constant*> &CP = MCP->getConstants();
448 const TargetData &TD = TM.getTargetData();
449
450 if (CP.empty()) return;
451
452 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
453 O << "\t.const\n";
454 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
455 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
456 << *CP[i] << "\n";
457 emitGlobalConstant(CP[i]);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000458 }
459}
460
Nate Begemaned428532004-09-04 05:00:00 +0000461bool DarwinAsmPrinter::doFinalization(Module &M) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000462 const TargetData &TD = TM.getTargetData();
463 std::string CurSection;
464
465 // Print out module-level global variables here.
466 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
467 if (I->hasInitializer()) { // External global require no code
Nate Begemana2de1022004-09-22 04:40:25 +0000468 O << '\n';
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000469 std::string name = Mang->getValueName(I);
470 Constant *C = I->getInitializer();
471 unsigned Size = TD.getTypeSize(C->getType());
Chris Lattner69d485e2004-08-17 19:26:03 +0000472 unsigned Align = TD.getTypeAlignmentShift(C->getType());
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000473
Misha Brukman97a296f2004-07-21 20:11:11 +0000474 if (C->isNullValue() && /* FIXME: Verify correct */
475 (I->hasInternalLinkage() || I->hasWeakLinkage())) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000476 SwitchSection(O, CurSection, ".data");
477 if (I->hasInternalLinkage())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000478 O << ".lcomm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattner69d485e2004-08-17 19:26:03 +0000479 << "," << Align;
Misha Brukman218bec72004-06-29 17:13:26 +0000480 else
Misha Brukmane2eceb52004-07-23 16:08:20 +0000481 O << ".comm " << name << "," << TD.getTypeSize(C->getType());
Misha Brukman218bec72004-06-29 17:13:26 +0000482 O << "\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000483 WriteAsOperand(O, I, true, true, &M);
Nate Begemana2de1022004-09-22 04:40:25 +0000484 O << '\n';
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000485 } else {
486 switch (I->getLinkage()) {
487 case GlobalValue::LinkOnceLinkage:
Misha Brukman97a296f2004-07-21 20:11:11 +0000488 O << ".section __TEXT,__textcoal_nt,coalesced,no_toc\n"
489 << ".weak_definition " << name << '\n'
490 << ".private_extern " << name << '\n'
491 << ".section __DATA,__datacoal_nt,coalesced,no_toc\n";
492 LinkOnceStubs.insert(name);
493 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000494 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
495 // Nonnull linkonce -> weak
496 O << "\t.weak " << name << "\n";
497 SwitchSection(O, CurSection, "");
498 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
499 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000500 case GlobalValue::AppendingLinkage:
501 // FIXME: appending linkage variables should go into a section of
502 // their name or something. For now, just emit them as external.
503 case GlobalValue::ExternalLinkage:
504 // If external or appending, declare as a global symbol
505 O << "\t.globl " << name << "\n";
506 // FALL THROUGH
507 case GlobalValue::InternalLinkage:
Misha Brukman61297ee2004-06-29 23:40:57 +0000508 SwitchSection(O, CurSection, ".data");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000509 break;
510 }
511
Chris Lattner69d485e2004-08-17 19:26:03 +0000512 emitAlignment(Align);
Misha Brukman218bec72004-06-29 17:13:26 +0000513 O << name << ":\t\t\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000514 WriteAsOperand(O, I, true, true, &M);
515 O << " = ";
516 WriteAsOperand(O, C, false, false, &M);
517 O << "\n";
518 emitGlobalConstant(C);
519 }
520 }
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000521
522 // Output stubs for dynamically-linked functions
523 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
524 i != e; ++i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000525 {
Misha Brukmane2eceb52004-07-23 16:08:20 +0000526 O << ".data\n";
527 O << ".section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n";
Chris Lattner69d485e2004-08-17 19:26:03 +0000528 emitAlignment(2);
Misha Brukman46fd00a2004-06-24 23:04:11 +0000529 O << "L" << *i << "$stub:\n";
530 O << "\t.indirect_symbol " << *i << "\n";
531 O << "\tmflr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000532 O << "\tbcl 20,31,L0$" << *i << "\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000533 O << "L0$" << *i << ":\n";
534 O << "\tmflr r11\n";
535 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
536 O << "\tmtlr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000537 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000538 O << "\tmtctr r12\n";
539 O << "\tbctr\n";
540 O << ".data\n";
541 O << ".lazy_symbol_pointer\n";
542 O << "L" << *i << "$lazy_ptr:\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000543 O << "\t.indirect_symbol " << *i << "\n";
544 O << "\t.long dyld_stub_binding_helper\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000545 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000546
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000547 O << "\n";
548
549 // Output stubs for external global variables
550 if (GVStubs.begin() != GVStubs.end())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000551 O << ".data\n.non_lazy_symbol_pointer\n";
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000552 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
553 i != e; ++i) {
554 O << "L" << *i << "$non_lazy_ptr:\n";
555 O << "\t.indirect_symbol " << *i << "\n";
556 O << "\t.long\t0\n";
557 }
558
Nate Begemane59bf592004-08-14 22:09:10 +0000559 // Output stubs for link-once variables
560 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
561 O << ".data\n.align 2\n";
562 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
563 e = LinkOnceStubs.end(); i != e; ++i) {
564 O << "L" << *i << "$non_lazy_ptr:\n"
565 << "\t.long\t" << *i << '\n';
566 }
567
Chris Lattnera3840792004-08-16 23:25:21 +0000568 AsmPrinter::doFinalization(M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000569 return false; // success
570}
Nate Begemaned428532004-09-04 05:00:00 +0000571
572/// runOnMachineFunction - This uses the printMachineInstruction()
573/// method to print assembly for each instruction.
574///
575bool AIXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
576 CurrentFnName = MF.getFunction()->getName();
577
578 // Print out constants referenced by the function
579 printConstantPool(MF.getConstantPool());
580
581 // Print out header for the function.
582 O << "\t.csect .text[PR]\n"
583 << "\t.align 2\n"
584 << "\t.globl " << CurrentFnName << '\n'
585 << "\t.globl ." << CurrentFnName << '\n'
586 << "\t.csect " << CurrentFnName << "[DS],3\n"
587 << CurrentFnName << ":\n"
588 << "\t.llong ." << CurrentFnName << ", TOC[tc0], 0\n"
589 << "\t.csect .text[PR]\n"
590 << '.' << CurrentFnName << ":\n";
591
592 // Print out code for the function.
593 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
594 I != E; ++I) {
595 // Print a label for the basic block.
596 O << "LBB" << CurrentFnName << "_" << I->getNumber() << ":\t# "
597 << I->getBasicBlock()->getName() << "\n";
598 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
599 II != E; ++II) {
600 // Print the assembly for the instruction.
601 O << "\t";
602 printMachineInstruction(II);
603 }
604 }
605 ++LabelNumber;
606
607 O << "LT.." << CurrentFnName << ":\n"
608 << "\t.long 0\n"
609 << "\t.byte 0,0,32,65,128,0,0,0\n"
610 << "\t.long LT.." << CurrentFnName << "-." << CurrentFnName << '\n'
611 << "\t.short 3\n"
612 << "\t.byte \"" << CurrentFnName << "\"\n"
613 << "\t.align 2\n";
614
615 // We didn't modify anything.
616 return false;
617}
618
619/// printConstantPool - Print to the current output stream assembly
620/// representations of the constants in the constant pool MCP. This is
621/// used to print out constants which have been "spilled to memory" by
622/// the code generator.
623///
624void AIXAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
625 const std::vector<Constant*> &CP = MCP->getConstants();
626 const TargetData &TD = TM.getTargetData();
627
628 if (CP.empty()) return;
629
630 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
631 O << "\t.const\n";
632 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
633 << "\n";
634 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t;"
635 << *CP[i] << "\n";
636 emitGlobalConstant(CP[i]);
637 }
638}
639
640bool AIXAsmPrinter::doInitialization(Module &M) {
641 const TargetData &TD = TM.getTargetData();
642 std::string CurSection;
643
644 O << "\t.machine \"ppc64\"\n"
645 << "\t.toc\n"
646 << "\t.csect .text[PR]\n";
647
648 // Print out module-level global variables
649 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) {
650 if (!I->hasInitializer())
651 continue;
652
653 std::string Name = I->getName();
654 Constant *C = I->getInitializer();
655 // N.B.: We are defaulting to writable strings
656 if (I->hasExternalLinkage()) {
657 O << "\t.globl " << Name << '\n'
658 << "\t.csect .data[RW],3\n";
659 } else {
660 O << "\t.csect _global.rw_c[RW],3\n";
661 }
662 O << Name << ":\n";
663 emitGlobalConstant(C);
664 }
665
666 // Output labels for globals
667 if (M.gbegin() != M.gend()) O << "\t.toc\n";
668 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) {
669 const GlobalVariable *GV = I;
670 // Do not output labels for unused variables
671 if (GV->isExternal() && GV->use_begin() == GV->use_end())
672 continue;
673
674 std::string Name = GV->getName();
675 std::string Label = "LC.." + utostr(LabelNumber++);
676 GVToLabelMap[GV] = Label;
677 O << Label << ":\n"
678 << "\t.tc " << Name << "[TC]," << Name;
679 if (GV->isExternal()) O << "[RW]";
680 O << '\n';
681 }
682
683 Mang = new Mangler(M, ".");
684 return false; // success
685}
686
687bool AIXAsmPrinter::doFinalization(Module &M) {
688 const TargetData &TD = TM.getTargetData();
689 // Print out module-level global variables
690 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) {
691 if (I->hasInitializer() || I->hasExternalLinkage())
692 continue;
693
694 std::string Name = I->getName();
695 if (I->hasInternalLinkage()) {
696 O << "\t.lcomm " << Name << ",16,_global.bss_c";
697 } else {
698 O << "\t.comm " << Name << "," << TD.getTypeSize(I->getType())
699 << "," << log2((unsigned)TD.getTypeAlignment(I->getType()));
700 }
701 O << "\t\t# ";
702 WriteAsOperand(O, I, true, true, &M);
703 O << "\n";
704 }
705
706 O << "_section_.text:\n"
707 << "\t.csect .data[RW],3\n"
708 << "\t.llong _section_.text\n";
709
710 delete Mang;
711 return false; // success
712}