blob: f4e36bab2e8c458be93b90cdf17e7640197d24c0 [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"
Nate Begemand4c8bea2004-11-25 07:09:01 +000036#include "llvm/Target/TargetInstrInfo.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000037#include "llvm/ADT/Statistic.h"
38#include "llvm/ADT/StringExtras.h"
Misha Brukman05794492004-06-24 17:31:42 +000039#include <set>
Chris Lattnera3840792004-08-16 23:25:21 +000040using namespace llvm;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000041
42namespace {
43 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
44
Misha Brukman3e0b51a2004-09-05 02:42:44 +000045 struct PowerPCAsmPrinter : public AsmPrinter {
Misha Brukman97a296f2004-07-21 20:11:11 +000046 std::set<std::string> FnStubs, GVStubs, LinkOnceStubs;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000047 std::set<std::string> Strings;
Nate Begemaned428532004-09-04 05:00:00 +000048
Misha Brukman3e0b51a2004-09-05 02:42:44 +000049 PowerPCAsmPrinter(std::ostream &O, TargetMachine &TM)
Nate Begemaned428532004-09-04 05:00:00 +000050 : AsmPrinter(O, TM), LabelNumber(0) {}
Misha Brukman5dfe3a92004-06-21 16:55:25 +000051
Misha Brukmancf8d2442004-07-26 16:28:33 +000052 /// Unique incrementer for label values for referencing Global values.
Misha Brukman218bec72004-06-29 17:13:26 +000053 ///
Misha Brukmancf8d2442004-07-26 16:28:33 +000054 unsigned LabelNumber;
55
Misha Brukman5dfe3a92004-06-21 16:55:25 +000056 virtual const char *getPassName() const {
Nate Begemaned428532004-09-04 05:00:00 +000057 return "PowerPC Assembly Printer";
Misha Brukman5dfe3a92004-06-21 16:55:25 +000058 }
59
Nate Begemaned428532004-09-04 05:00:00 +000060 PowerPCTargetMachine &getTM() {
61 return static_cast<PowerPCTargetMachine&>(TM);
Chris Lattnera3840792004-08-16 23:25:21 +000062 }
63
Nate Begemane59bf592004-08-14 22:09:10 +000064 /// printInstruction - This method is automatically generated by tablegen
65 /// from the instruction set description. This method returns true if the
66 /// machine instruction was sufficiently described to print it, otherwise it
67 /// returns false.
68 bool printInstruction(const MachineInstr *MI);
69
Misha Brukman5dfe3a92004-06-21 16:55:25 +000070 void printMachineInstruction(const MachineInstr *MI);
Nate Begemand4c8bea2004-11-25 07:09:01 +000071 void printOp(const MachineOperand &MO, bool IsCallOp = false);
Chris Lattner7bb424f2004-08-14 23:27:29 +000072
73 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
74 const MachineOperand &MO = MI->getOperand(OpNo);
75 if (MO.getType() == MachineOperand::MO_MachineRegister) {
76 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
77 O << LowercaseString(TM.getRegisterInfo()->get(MO.getReg()).Name);
Chris Lattner0ea31712004-08-15 05:46:14 +000078 } else if (MO.isImmediate()) {
79 O << MO.getImmedValue();
Chris Lattner7bb424f2004-08-14 23:27:29 +000080 } else {
81 printOp(MO);
82 }
83 }
84
Nate Begemanc3306122004-08-21 05:56:39 +000085 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo,
86 MVT::ValueType VT) {
87 unsigned char value = MI->getOperand(OpNo).getImmedValue();
Chris Lattner12585ba2004-08-21 19:11:03 +000088 assert(value <= 31 && "Invalid u5imm argument!");
Nate Begemanc3306122004-08-21 05:56:39 +000089 O << (unsigned int)value;
90 }
Nate Begeman07aada82004-08-30 02:28:06 +000091 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo,
92 MVT::ValueType VT) {
93 unsigned char value = MI->getOperand(OpNo).getImmedValue();
94 assert(value <= 63 && "Invalid u6imm argument!");
95 O << (unsigned int)value;
96 }
Nate Begemaned428532004-09-04 05:00:00 +000097 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo,
98 MVT::ValueType VT) {
99 O << (short)MI->getOperand(OpNo).getImmedValue();
100 }
Chris Lattner97b2a2e2004-08-15 05:20:16 +0000101 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo,
102 MVT::ValueType VT) {
103 O << (unsigned short)MI->getOperand(OpNo).getImmedValue();
104 }
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000105 void printBranchOperand(const MachineInstr *MI, unsigned OpNo,
Misha Brukman3a060e52004-10-23 04:58:32 +0000106 MVT::ValueType VT) {
Nate Begemaned428532004-09-04 05:00:00 +0000107 // Branches can take an immediate operand. This is used by the branch
108 // selection pass to print $+8, an eight byte displacement from the PC.
109 if (MI->getOperand(OpNo).isImmediate()) {
110 O << "$+" << MI->getOperand(OpNo).getImmedValue() << '\n';
111 } else {
Nate Begemand4c8bea2004-11-25 07:09:01 +0000112 printOp(MI->getOperand(OpNo),
113 TM.getInstrInfo()->isCall(MI->getOpcode()));
Nate Begemaned428532004-09-04 05:00:00 +0000114 }
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000115 }
116 void printPICLabel(const MachineInstr *MI, unsigned OpNo,
Misha Brukman3a060e52004-10-23 04:58:32 +0000117 MVT::ValueType VT) {
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000118 // FIXME: should probably be converted to cout.width and cout.fill
119 O << "\"L0000" << LabelNumber << "$pb\"\n";
120 O << "\"L0000" << LabelNumber << "$pb\":";
121 }
Nate Begemaned428532004-09-04 05:00:00 +0000122 void printSymbolHi(const MachineInstr *MI, unsigned OpNo,
Misha Brukman3a060e52004-10-23 04:58:32 +0000123 MVT::ValueType VT) {
Nate Begemaned428532004-09-04 05:00:00 +0000124 O << "ha16(";
Nate Begemand4c8bea2004-11-25 07:09:01 +0000125 printOp(MI->getOperand(OpNo));
Nate Begemaned428532004-09-04 05:00:00 +0000126 O << "-\"L0000" << LabelNumber << "$pb\")";
127 }
128 void printSymbolLo(const MachineInstr *MI, unsigned OpNo,
Misha Brukman3a060e52004-10-23 04:58:32 +0000129 MVT::ValueType VT) {
Nate Begemaned428532004-09-04 05:00:00 +0000130 // FIXME: Because LFS, LFD, and LWZ can be used either with a s16imm or
131 // a lo16 of a global or constant pool operand, we must handle both here.
132 // this isn't a great design, but it works for now.
133 if (MI->getOperand(OpNo).isImmediate()) {
134 O << (short)MI->getOperand(OpNo).getImmedValue();
135 } else {
136 O << "lo16(";
Nate Begemand4c8bea2004-11-25 07:09:01 +0000137 printOp(MI->getOperand(OpNo));
Nate Begemaned428532004-09-04 05:00:00 +0000138 O << "-\"L0000" << LabelNumber << "$pb\")";
139 }
140 }
141
142 virtual void printConstantPool(MachineConstantPool *MCP) = 0;
143 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
144 virtual bool doFinalization(Module &M) = 0;
145 };
146
Misha Brukman3e0b51a2004-09-05 02:42:44 +0000147 /// DarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac OS
148 /// X
149 ///
150 struct DarwinAsmPrinter : public PowerPCAsmPrinter {
Nate Begemaned428532004-09-04 05:00:00 +0000151
152 DarwinAsmPrinter(std::ostream &O, TargetMachine &TM)
Misha Brukman3e0b51a2004-09-05 02:42:44 +0000153 : PowerPCAsmPrinter(O, TM) {
Nate Begemaned428532004-09-04 05:00:00 +0000154 CommentString = ";";
155 GlobalPrefix = "_";
156 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
157 Data64bitsDirective = 0; // we can't emit a 64-bit unit
158 AlignmentIsInBytes = false; // Alignment is by power of 2.
159 }
160
161 virtual const char *getPassName() const {
162 return "Darwin PPC Assembly Printer";
163 }
Chris Lattner97b2a2e2004-08-15 05:20:16 +0000164
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000165 void printConstantPool(MachineConstantPool *MCP);
166 bool runOnMachineFunction(MachineFunction &F);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000167 bool doFinalization(Module &M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000168 };
Nate Begemaned428532004-09-04 05:00:00 +0000169
Misha Brukman3e0b51a2004-09-05 02:42:44 +0000170 /// AIXAsmPrinter - PowerPC assembly printer, customized for AIX
171 ///
172 struct AIXAsmPrinter : public PowerPCAsmPrinter {
Nate Begemaned428532004-09-04 05:00:00 +0000173 /// Map for labels corresponding to global variables
174 ///
175 std::map<const GlobalVariable*,std::string> GVToLabelMap;
176
177 AIXAsmPrinter(std::ostream &O, TargetMachine &TM)
Misha Brukman3e0b51a2004-09-05 02:42:44 +0000178 : PowerPCAsmPrinter(O, TM) {
Nate Begemaned428532004-09-04 05:00:00 +0000179 CommentString = "#";
180 GlobalPrefix = "_";
181 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
182 Data64bitsDirective = 0; // we can't emit a 64-bit unit
183 AlignmentIsInBytes = false; // Alignment is by power of 2.
184 }
185
186 virtual const char *getPassName() const {
187 return "AIX PPC Assembly Printer";
188 }
189
190 void printConstantPool(MachineConstantPool *MCP);
191 bool runOnMachineFunction(MachineFunction &F);
192 bool doInitialization(Module &M);
193 bool doFinalization(Module &M);
194 };
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000195} // end of anonymous namespace
196
Nate Begemaned428532004-09-04 05:00:00 +0000197// SwitchSection - Switch to the specified section of the executable if we are
198// not already in it!
199//
200static void SwitchSection(std::ostream &OS, std::string &CurSection,
201 const char *NewSection) {
202 if (CurSection != NewSection) {
203 CurSection = NewSection;
204 if (!CurSection.empty())
205 OS << "\t" << NewSection << "\n";
206 }
207}
208
209/// isStringCompatible - Can we treat the specified array as a string?
210/// Only if it is an array of ubytes or non-negative sbytes.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000211///
Nate Begemaned428532004-09-04 05:00:00 +0000212static bool isStringCompatible(const ConstantArray *CVA) {
213 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
214 if (ETy == Type::UByteTy) return true;
215 if (ETy != Type::SByteTy) return false;
216
217 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
218 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
219 return false;
220
221 return true;
222}
223
224/// toOctal - Convert the low order bits of X into an octal digit.
225///
226static inline char toOctal(int X) {
227 return (X&7)+'0';
228}
229
230// Possible states while outputting ASCII strings
231namespace {
232 enum StringSection {
233 None,
234 Alpha,
235 Numeric
236 };
237}
238
239/// SwitchStringSection - manage the changes required to output bytes as
240/// characters in a string vs. numeric decimal values
241///
242static inline void SwitchStringSection(std::ostream &O, StringSection NewSect,
243 StringSection &Current) {
244 if (Current == None) {
245 if (NewSect == Alpha)
246 O << "\t.byte \"";
247 else if (NewSect == Numeric)
248 O << "\t.byte ";
249 } else if (Current == Alpha) {
250 if (NewSect == None)
251 O << "\"";
252 else if (NewSect == Numeric)
253 O << "\"\n"
254 << "\t.byte ";
255 } else if (Current == Numeric) {
256 if (NewSect == Alpha)
257 O << '\n'
258 << "\t.byte \"";
259 else if (NewSect == Numeric)
260 O << ", ";
261 }
262
263 Current = NewSect;
264}
265
266/// getAsCString - Return the specified array as a C compatible
267/// string, only if the predicate isStringCompatible is true.
268///
269static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
270 assert(isStringCompatible(CVA) && "Array is not string compatible!");
271
272 if (CVA->getNumOperands() == 0)
273 return;
274
275 StringSection Current = None;
276 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i) {
277 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
278 if (C == '"') {
279 SwitchStringSection(O, Alpha, Current);
280 O << "\"\"";
281 } else if (isprint(C)) {
282 SwitchStringSection(O, Alpha, Current);
283 O << C;
284 } else {
285 SwitchStringSection(O, Numeric, Current);
286 O << utostr((unsigned)C);
287 }
288 }
289 SwitchStringSection(O, None, Current);
290 O << '\n';
291}
292
293/// createDarwinAsmPrinterPass - Returns a pass that prints the PPC assembly
294/// code for a MachineFunction to the given output stream, in a format that the
295/// Darwin assembler can deal with.
296///
297FunctionPass *llvm::createDarwinAsmPrinter(std::ostream &o, TargetMachine &tm) {
298 return new DarwinAsmPrinter(o, tm);
299}
300
301/// createAIXAsmPrinterPass - Returns a pass that prints the PPC assembly code
302/// for a MachineFunction to the given output stream, in a format that the
303/// AIX 5L assembler can deal with.
304///
305FunctionPass *llvm::createAIXAsmPrinter(std::ostream &o, TargetMachine &tm) {
306 return new AIXAsmPrinter(o, tm);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000307}
308
Nate Begemane59bf592004-08-14 22:09:10 +0000309// Include the auto-generated portion of the assembly writer
310#include "PowerPCGenAsmWriter.inc"
311
Nate Begemand4c8bea2004-11-25 07:09:01 +0000312void PowerPCAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000313 const MRegisterInfo &RI = *TM.getRegisterInfo();
314 int new_symbol;
315
316 switch (MO.getType()) {
317 case MachineOperand::MO_VirtualRegister:
318 if (Value *V = MO.getVRegValueOrNull()) {
319 O << "<" << V->getName() << ">";
320 return;
321 }
322 // FALLTHROUGH
323 case MachineOperand::MO_MachineRegister:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000324 case MachineOperand::MO_CCRegister:
Misha Brukman7f484a52004-06-24 23:51:00 +0000325 O << LowercaseString(RI.get(MO.getReg()).Name);
326 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000327
328 case MachineOperand::MO_SignExtendedImmed:
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000329 case MachineOperand::MO_UnextendedImmed:
330 std::cerr << "printOp() does not handle immediate values\n";
331 abort();
Misha Brukman97a296f2004-07-21 20:11:11 +0000332 return;
333
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000334 case MachineOperand::MO_PCRelativeDisp:
335 std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
336 abort();
337 return;
338
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000339 case MachineOperand::MO_MachineBasicBlock: {
340 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
341 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
Misha Brukman218bec72004-06-29 17:13:26 +0000342 << "_" << MBBOp->getNumber() << "\t; "
Misha Brukman2bf183c2004-06-25 15:42:10 +0000343 << MBBOp->getBasicBlock()->getName();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000344 return;
345 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000346
347 case MachineOperand::MO_ConstantPoolIndex:
348 O << ".CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000349 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000350
351 case MachineOperand::MO_ExternalSymbol:
352 O << MO.getSymbolName();
353 return;
354
Nate Begemanb73a7112004-08-13 09:32:01 +0000355 case MachineOperand::MO_GlobalAddress: {
356 GlobalValue *GV = MO.getGlobal();
357 std::string Name = Mang->getValueName(GV);
Misha Brukmane2eceb52004-07-23 16:08:20 +0000358
Nate Begemanb73a7112004-08-13 09:32:01 +0000359 // Dynamically-resolved functions need a stub for the function. Be
360 // wary however not to output $stub for external functions whose addresses
361 // are taken. Those should be emitted as $non_lazy_ptr below.
362 Function *F = dyn_cast<Function>(GV);
Nate Begemand4c8bea2004-11-25 07:09:01 +0000363 if (F && F->isExternal() && IsCallOp && 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
Nate Begemand4c8bea2004-11-25 07:09:01 +0000370 if ((GV->isExternal() || GV->hasWeakLinkage() || GV->hasLinkOnceLinkage())
Nate Begemana5914572004-11-09 04:01:18 +0000371 && getTM().AddressTaken.count(GV)) {
Nate Begemand4c8bea2004-11-25 07:09:01 +0000372 if (GV->hasLinkOnceLinkage())
373 LinkOnceStubs.insert(Name);
374 else
375 GVStubs.insert(Name);
Nate Begemanb73a7112004-08-13 09:32:01 +0000376 O << "L" << Name << "$non_lazy_ptr";
377 return;
378 }
Nate Begemand4c8bea2004-11-25 07:09:01 +0000379
Nate Begemanb73a7112004-08-13 09:32:01 +0000380 O << Mang->getValueName(GV);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000381 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000382 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000383
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000384 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000385 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000386 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000387 }
388}
389
Nate Begemane59bf592004-08-14 22:09:10 +0000390/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
391/// the current output stream.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000392///
Misha Brukman3e0b51a2004-09-05 02:42:44 +0000393void PowerPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Nate Begemane59bf592004-08-14 22:09:10 +0000394 ++EmittedInsts;
395 if (printInstruction(MI))
396 return; // Printer was automatically generated
Nate Begemaned428532004-09-04 05:00:00 +0000397
398 assert(0 && "Unhandled instruction in asm writer!");
399 abort();
Nate Begemane59bf592004-08-14 22:09:10 +0000400 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000401}
402
Nate Begemaned428532004-09-04 05:00:00 +0000403/// runOnMachineFunction - This uses the printMachineInstruction()
404/// method to print assembly for each instruction.
405///
406bool DarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
407 setupMachineFunction(MF);
408 O << "\n\n";
409
410 // Print out constants referenced by the function
411 printConstantPool(MF.getConstantPool());
412
413 // Print out labels for the function.
414 O << "\t.text\n";
415 emitAlignment(2);
416 O << "\t.globl\t" << CurrentFnName << "\n";
417 O << CurrentFnName << ":\n";
418
419 // Print out code for the function.
420 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
421 I != E; ++I) {
422 // Print a label for the basic block.
423 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
424 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
425 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
426 II != E; ++II) {
427 // Print the assembly for the instruction.
428 O << "\t";
429 printMachineInstruction(II);
430 }
431 }
432 ++LabelNumber;
433
434 // We didn't modify anything.
435 return false;
436}
437
438/// printConstantPool - Print to the current output stream assembly
439/// representations of the constants in the constant pool MCP. This is
440/// used to print out constants which have been "spilled to memory" by
441/// the code generator.
442///
443void DarwinAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
444 const std::vector<Constant*> &CP = MCP->getConstants();
445 const TargetData &TD = TM.getTargetData();
446
447 if (CP.empty()) return;
448
449 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
450 O << "\t.const\n";
451 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
452 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
453 << *CP[i] << "\n";
454 emitGlobalConstant(CP[i]);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000455 }
456}
457
Nate Begemaned428532004-09-04 05:00:00 +0000458bool DarwinAsmPrinter::doFinalization(Module &M) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000459 const TargetData &TD = TM.getTargetData();
460 std::string CurSection;
461
462 // Print out module-level global variables here.
463 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
464 if (I->hasInitializer()) { // External global require no code
Nate Begemana2de1022004-09-22 04:40:25 +0000465 O << '\n';
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000466 std::string name = Mang->getValueName(I);
467 Constant *C = I->getInitializer();
468 unsigned Size = TD.getTypeSize(C->getType());
Chris Lattner69d485e2004-08-17 19:26:03 +0000469 unsigned Align = TD.getTypeAlignmentShift(C->getType());
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000470
Misha Brukman97a296f2004-07-21 20:11:11 +0000471 if (C->isNullValue() && /* FIXME: Verify correct */
Nate Begemana5914572004-11-09 04:01:18 +0000472 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
473 I->hasLinkOnceLinkage())) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000474 SwitchSection(O, CurSection, ".data");
475 if (I->hasInternalLinkage())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000476 O << ".lcomm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattner69d485e2004-08-17 19:26:03 +0000477 << "," << Align;
Misha Brukman218bec72004-06-29 17:13:26 +0000478 else
Misha Brukmane2eceb52004-07-23 16:08:20 +0000479 O << ".comm " << name << "," << TD.getTypeSize(C->getType());
Misha Brukman218bec72004-06-29 17:13:26 +0000480 O << "\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000481 WriteAsOperand(O, I, true, true, &M);
Nate Begemana2de1022004-09-22 04:40:25 +0000482 O << '\n';
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000483 } else {
484 switch (I->getLinkage()) {
485 case GlobalValue::LinkOnceLinkage:
Misha Brukman97a296f2004-07-21 20:11:11 +0000486 O << ".section __TEXT,__textcoal_nt,coalesced,no_toc\n"
487 << ".weak_definition " << name << '\n'
488 << ".private_extern " << name << '\n'
489 << ".section __DATA,__datacoal_nt,coalesced,no_toc\n";
490 LinkOnceStubs.insert(name);
491 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000492 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
493 // Nonnull linkonce -> weak
494 O << "\t.weak " << name << "\n";
495 SwitchSection(O, CurSection, "");
496 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
497 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000498 case GlobalValue::AppendingLinkage:
499 // FIXME: appending linkage variables should go into a section of
500 // their name or something. For now, just emit them as external.
501 case GlobalValue::ExternalLinkage:
502 // If external or appending, declare as a global symbol
503 O << "\t.globl " << name << "\n";
504 // FALL THROUGH
505 case GlobalValue::InternalLinkage:
Misha Brukman61297ee2004-06-29 23:40:57 +0000506 SwitchSection(O, CurSection, ".data");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000507 break;
Misha Brukman3a8a42a2004-11-14 21:03:30 +0000508 case GlobalValue::GhostLinkage:
509 std::cerr << "Error: unmaterialized (GhostLinkage) function in asm!";
510 abort();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000511 }
512
Chris Lattner69d485e2004-08-17 19:26:03 +0000513 emitAlignment(Align);
Misha Brukman218bec72004-06-29 17:13:26 +0000514 O << name << ":\t\t\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000515 WriteAsOperand(O, I, true, true, &M);
516 O << " = ";
517 WriteAsOperand(O, C, false, false, &M);
518 O << "\n";
519 emitGlobalConstant(C);
520 }
521 }
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000522
523 // Output stubs for dynamically-linked functions
524 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
525 i != e; ++i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000526 {
Misha Brukmane2eceb52004-07-23 16:08:20 +0000527 O << ".data\n";
528 O << ".section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n";
Chris Lattner69d485e2004-08-17 19:26:03 +0000529 emitAlignment(2);
Misha Brukman46fd00a2004-06-24 23:04:11 +0000530 O << "L" << *i << "$stub:\n";
531 O << "\t.indirect_symbol " << *i << "\n";
532 O << "\tmflr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000533 O << "\tbcl 20,31,L0$" << *i << "\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000534 O << "L0$" << *i << ":\n";
535 O << "\tmflr r11\n";
536 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
537 O << "\tmtlr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000538 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000539 O << "\tmtctr r12\n";
540 O << "\tbctr\n";
541 O << ".data\n";
542 O << ".lazy_symbol_pointer\n";
543 O << "L" << *i << "$lazy_ptr:\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000544 O << "\t.indirect_symbol " << *i << "\n";
545 O << "\t.long dyld_stub_binding_helper\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000546 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000547
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000548 O << "\n";
549
550 // Output stubs for external global variables
551 if (GVStubs.begin() != GVStubs.end())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000552 O << ".data\n.non_lazy_symbol_pointer\n";
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000553 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
554 i != e; ++i) {
555 O << "L" << *i << "$non_lazy_ptr:\n";
556 O << "\t.indirect_symbol " << *i << "\n";
557 O << "\t.long\t0\n";
558 }
559
Nate Begemane59bf592004-08-14 22:09:10 +0000560 // Output stubs for link-once variables
561 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
562 O << ".data\n.align 2\n";
563 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
564 e = LinkOnceStubs.end(); i != e; ++i) {
565 O << "L" << *i << "$non_lazy_ptr:\n"
566 << "\t.long\t" << *i << '\n';
567 }
568
Chris Lattnera3840792004-08-16 23:25:21 +0000569 AsmPrinter::doFinalization(M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000570 return false; // success
571}
Nate Begemaned428532004-09-04 05:00:00 +0000572
573/// runOnMachineFunction - This uses the printMachineInstruction()
574/// method to print assembly for each instruction.
575///
576bool AIXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
577 CurrentFnName = MF.getFunction()->getName();
578
579 // Print out constants referenced by the function
580 printConstantPool(MF.getConstantPool());
581
582 // Print out header for the function.
583 O << "\t.csect .text[PR]\n"
584 << "\t.align 2\n"
585 << "\t.globl " << CurrentFnName << '\n'
586 << "\t.globl ." << CurrentFnName << '\n'
587 << "\t.csect " << CurrentFnName << "[DS],3\n"
588 << CurrentFnName << ":\n"
589 << "\t.llong ." << CurrentFnName << ", TOC[tc0], 0\n"
590 << "\t.csect .text[PR]\n"
591 << '.' << CurrentFnName << ":\n";
592
593 // Print out code for the function.
594 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
595 I != E; ++I) {
596 // Print a label for the basic block.
597 O << "LBB" << CurrentFnName << "_" << I->getNumber() << ":\t# "
598 << I->getBasicBlock()->getName() << "\n";
599 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
600 II != E; ++II) {
601 // Print the assembly for the instruction.
602 O << "\t";
603 printMachineInstruction(II);
604 }
605 }
606 ++LabelNumber;
607
608 O << "LT.." << CurrentFnName << ":\n"
609 << "\t.long 0\n"
610 << "\t.byte 0,0,32,65,128,0,0,0\n"
611 << "\t.long LT.." << CurrentFnName << "-." << CurrentFnName << '\n'
612 << "\t.short 3\n"
613 << "\t.byte \"" << CurrentFnName << "\"\n"
614 << "\t.align 2\n";
615
616 // We didn't modify anything.
617 return false;
618}
619
620/// printConstantPool - Print to the current output stream assembly
621/// representations of the constants in the constant pool MCP. This is
622/// used to print out constants which have been "spilled to memory" by
623/// the code generator.
624///
625void AIXAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
626 const std::vector<Constant*> &CP = MCP->getConstants();
627 const TargetData &TD = TM.getTargetData();
628
629 if (CP.empty()) return;
630
631 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
632 O << "\t.const\n";
633 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
634 << "\n";
635 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t;"
636 << *CP[i] << "\n";
637 emitGlobalConstant(CP[i]);
638 }
639}
640
641bool AIXAsmPrinter::doInitialization(Module &M) {
642 const TargetData &TD = TM.getTargetData();
643 std::string CurSection;
644
645 O << "\t.machine \"ppc64\"\n"
646 << "\t.toc\n"
647 << "\t.csect .text[PR]\n";
648
649 // Print out module-level global variables
650 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) {
651 if (!I->hasInitializer())
652 continue;
653
654 std::string Name = I->getName();
655 Constant *C = I->getInitializer();
656 // N.B.: We are defaulting to writable strings
657 if (I->hasExternalLinkage()) {
658 O << "\t.globl " << Name << '\n'
659 << "\t.csect .data[RW],3\n";
660 } else {
661 O << "\t.csect _global.rw_c[RW],3\n";
662 }
663 O << Name << ":\n";
664 emitGlobalConstant(C);
665 }
666
667 // Output labels for globals
668 if (M.gbegin() != M.gend()) O << "\t.toc\n";
669 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) {
670 const GlobalVariable *GV = I;
671 // Do not output labels for unused variables
672 if (GV->isExternal() && GV->use_begin() == GV->use_end())
673 continue;
674
675 std::string Name = GV->getName();
676 std::string Label = "LC.." + utostr(LabelNumber++);
677 GVToLabelMap[GV] = Label;
678 O << Label << ":\n"
679 << "\t.tc " << Name << "[TC]," << Name;
680 if (GV->isExternal()) O << "[RW]";
681 O << '\n';
682 }
683
684 Mang = new Mangler(M, ".");
685 return false; // success
686}
687
688bool AIXAsmPrinter::doFinalization(Module &M) {
689 const TargetData &TD = TM.getTargetData();
690 // Print out module-level global variables
691 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) {
692 if (I->hasInitializer() || I->hasExternalLinkage())
693 continue;
694
695 std::string Name = I->getName();
696 if (I->hasInternalLinkage()) {
697 O << "\t.lcomm " << Name << ",16,_global.bss_c";
698 } else {
699 O << "\t.comm " << Name << "," << TD.getTypeSize(I->getType())
700 << "," << log2((unsigned)TD.getTypeAlignment(I->getType()));
701 }
702 O << "\t\t# ";
703 WriteAsOperand(O, I, true, true, &M);
704 O << "\n";
705 }
706
707 O << "_section_.text:\n"
708 << "\t.csect .data[RW],3\n"
709 << "\t.llong _section_.text\n";
710
711 delete Mang;
712 return false; // success
713}