blob: e85516f861d80165310e256310abff0cb67e9d69 [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"
35#include "llvm/ADT/Statistic.h"
36#include "llvm/ADT/StringExtras.h"
Misha Brukman05794492004-06-24 17:31:42 +000037#include <set>
Chris Lattnera3840792004-08-16 23:25:21 +000038using namespace llvm;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000039
40namespace {
41 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
42
Misha Brukman3e0b51a2004-09-05 02:42:44 +000043 struct PowerPCAsmPrinter : public AsmPrinter {
Misha Brukman97a296f2004-07-21 20:11:11 +000044 std::set<std::string> FnStubs, GVStubs, LinkOnceStubs;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000045 std::set<std::string> Strings;
Nate Begemaned428532004-09-04 05:00:00 +000046
Misha Brukman3e0b51a2004-09-05 02:42:44 +000047 PowerPCAsmPrinter(std::ostream &O, TargetMachine &TM)
Nate Begemaned428532004-09-04 05:00:00 +000048 : AsmPrinter(O, TM), LabelNumber(0) {}
Misha Brukman5dfe3a92004-06-21 16:55:25 +000049
Misha Brukmancf8d2442004-07-26 16:28:33 +000050 /// Unique incrementer for label values for referencing Global values.
Misha Brukman218bec72004-06-29 17:13:26 +000051 ///
Misha Brukmancf8d2442004-07-26 16:28:33 +000052 unsigned LabelNumber;
53
Misha Brukman5dfe3a92004-06-21 16:55:25 +000054 virtual const char *getPassName() const {
Nate Begemaned428532004-09-04 05:00:00 +000055 return "PowerPC Assembly Printer";
Misha Brukman5dfe3a92004-06-21 16:55:25 +000056 }
57
Nate Begemaned428532004-09-04 05:00:00 +000058 PowerPCTargetMachine &getTM() {
59 return static_cast<PowerPCTargetMachine&>(TM);
Chris Lattnera3840792004-08-16 23:25:21 +000060 }
61
Nate Begemane59bf592004-08-14 22:09:10 +000062 /// printInstruction - This method is automatically generated by tablegen
63 /// from the instruction set description. This method returns true if the
64 /// machine instruction was sufficiently described to print it, otherwise it
65 /// returns false.
66 bool printInstruction(const MachineInstr *MI);
67
Misha Brukman5dfe3a92004-06-21 16:55:25 +000068 void printMachineInstruction(const MachineInstr *MI);
Nate Begemanb73a7112004-08-13 09:32:01 +000069 void printOp(const MachineOperand &MO, bool LoadAddrOp = false);
Chris Lattner7bb424f2004-08-14 23:27:29 +000070
71 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
72 const MachineOperand &MO = MI->getOperand(OpNo);
73 if (MO.getType() == MachineOperand::MO_MachineRegister) {
74 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
75 O << LowercaseString(TM.getRegisterInfo()->get(MO.getReg()).Name);
Chris Lattner0ea31712004-08-15 05:46:14 +000076 } else if (MO.isImmediate()) {
77 O << MO.getImmedValue();
Chris Lattner7bb424f2004-08-14 23:27:29 +000078 } else {
79 printOp(MO);
80 }
81 }
82
Nate Begemanc3306122004-08-21 05:56:39 +000083 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo,
84 MVT::ValueType VT) {
85 unsigned char value = MI->getOperand(OpNo).getImmedValue();
Chris Lattner12585ba2004-08-21 19:11:03 +000086 assert(value <= 31 && "Invalid u5imm argument!");
Nate Begemanc3306122004-08-21 05:56:39 +000087 O << (unsigned int)value;
88 }
Nate Begeman07aada82004-08-30 02:28:06 +000089 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo,
90 MVT::ValueType VT) {
91 unsigned char value = MI->getOperand(OpNo).getImmedValue();
92 assert(value <= 63 && "Invalid u6imm argument!");
93 O << (unsigned int)value;
94 }
Nate Begemaned428532004-09-04 05:00:00 +000095 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo,
96 MVT::ValueType VT) {
97 O << (short)MI->getOperand(OpNo).getImmedValue();
98 }
Chris Lattner97b2a2e2004-08-15 05:20:16 +000099 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo,
100 MVT::ValueType VT) {
101 O << (unsigned short)MI->getOperand(OpNo).getImmedValue();
102 }
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000103 void printBranchOperand(const MachineInstr *MI, unsigned OpNo,
Misha Brukman3a060e52004-10-23 04:58:32 +0000104 MVT::ValueType VT) {
Nate Begemaned428532004-09-04 05:00:00 +0000105 // Branches can take an immediate operand. This is used by the branch
106 // selection pass to print $+8, an eight byte displacement from the PC.
107 if (MI->getOperand(OpNo).isImmediate()) {
108 O << "$+" << MI->getOperand(OpNo).getImmedValue() << '\n';
109 } else {
110 printOp(MI->getOperand(OpNo));
111 }
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000112 }
113 void printPICLabel(const MachineInstr *MI, unsigned OpNo,
Misha Brukman3a060e52004-10-23 04:58:32 +0000114 MVT::ValueType VT) {
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000115 // FIXME: should probably be converted to cout.width and cout.fill
116 O << "\"L0000" << LabelNumber << "$pb\"\n";
117 O << "\"L0000" << LabelNumber << "$pb\":";
118 }
Nate Begemaned428532004-09-04 05:00:00 +0000119 void printSymbolHi(const MachineInstr *MI, unsigned OpNo,
Misha Brukman3a060e52004-10-23 04:58:32 +0000120 MVT::ValueType VT) {
Nate Begemaned428532004-09-04 05:00:00 +0000121 O << "ha16(";
122 printOp(MI->getOperand(OpNo), true /* LoadAddrOp */);
123 O << "-\"L0000" << LabelNumber << "$pb\")";
124 }
125 void printSymbolLo(const MachineInstr *MI, unsigned OpNo,
Misha Brukman3a060e52004-10-23 04:58:32 +0000126 MVT::ValueType VT) {
Nate Begemaned428532004-09-04 05:00:00 +0000127 // FIXME: Because LFS, LFD, and LWZ can be used either with a s16imm or
128 // a lo16 of a global or constant pool operand, we must handle both here.
129 // this isn't a great design, but it works for now.
130 if (MI->getOperand(OpNo).isImmediate()) {
131 O << (short)MI->getOperand(OpNo).getImmedValue();
132 } else {
133 O << "lo16(";
134 printOp(MI->getOperand(OpNo), true /* LoadAddrOp */);
135 O << "-\"L0000" << LabelNumber << "$pb\")";
136 }
137 }
138
139 virtual void printConstantPool(MachineConstantPool *MCP) = 0;
140 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
141 virtual bool doFinalization(Module &M) = 0;
142 };
143
Misha Brukman3e0b51a2004-09-05 02:42:44 +0000144 /// DarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac OS
145 /// X
146 ///
147 struct DarwinAsmPrinter : public PowerPCAsmPrinter {
Nate Begemaned428532004-09-04 05:00:00 +0000148
149 DarwinAsmPrinter(std::ostream &O, TargetMachine &TM)
Misha Brukman3e0b51a2004-09-05 02:42:44 +0000150 : PowerPCAsmPrinter(O, TM) {
Nate Begemaned428532004-09-04 05:00:00 +0000151 CommentString = ";";
152 GlobalPrefix = "_";
153 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
154 Data64bitsDirective = 0; // we can't emit a 64-bit unit
155 AlignmentIsInBytes = false; // Alignment is by power of 2.
156 }
157
158 virtual const char *getPassName() const {
159 return "Darwin PPC Assembly Printer";
160 }
Chris Lattner97b2a2e2004-08-15 05:20:16 +0000161
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000162 void printConstantPool(MachineConstantPool *MCP);
163 bool runOnMachineFunction(MachineFunction &F);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000164 bool doFinalization(Module &M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000165 };
Nate Begemaned428532004-09-04 05:00:00 +0000166
Misha Brukman3e0b51a2004-09-05 02:42:44 +0000167 /// AIXAsmPrinter - PowerPC assembly printer, customized for AIX
168 ///
169 struct AIXAsmPrinter : public PowerPCAsmPrinter {
Nate Begemaned428532004-09-04 05:00:00 +0000170 /// Map for labels corresponding to global variables
171 ///
172 std::map<const GlobalVariable*,std::string> GVToLabelMap;
173
174 AIXAsmPrinter(std::ostream &O, TargetMachine &TM)
Misha Brukman3e0b51a2004-09-05 02:42:44 +0000175 : PowerPCAsmPrinter(O, TM) {
Nate Begemaned428532004-09-04 05:00:00 +0000176 CommentString = "#";
177 GlobalPrefix = "_";
178 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
179 Data64bitsDirective = 0; // we can't emit a 64-bit unit
180 AlignmentIsInBytes = false; // Alignment is by power of 2.
181 }
182
183 virtual const char *getPassName() const {
184 return "AIX PPC Assembly Printer";
185 }
186
187 void printConstantPool(MachineConstantPool *MCP);
188 bool runOnMachineFunction(MachineFunction &F);
189 bool doInitialization(Module &M);
190 bool doFinalization(Module &M);
191 };
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000192} // end of anonymous namespace
193
Nate Begemaned428532004-09-04 05:00:00 +0000194// SwitchSection - Switch to the specified section of the executable if we are
195// not already in it!
196//
197static void SwitchSection(std::ostream &OS, std::string &CurSection,
198 const char *NewSection) {
199 if (CurSection != NewSection) {
200 CurSection = NewSection;
201 if (!CurSection.empty())
202 OS << "\t" << NewSection << "\n";
203 }
204}
205
206/// isStringCompatible - Can we treat the specified array as a string?
207/// Only if it is an array of ubytes or non-negative sbytes.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000208///
Nate Begemaned428532004-09-04 05:00:00 +0000209static bool isStringCompatible(const ConstantArray *CVA) {
210 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
211 if (ETy == Type::UByteTy) return true;
212 if (ETy != Type::SByteTy) return false;
213
214 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
215 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
216 return false;
217
218 return true;
219}
220
221/// toOctal - Convert the low order bits of X into an octal digit.
222///
223static inline char toOctal(int X) {
224 return (X&7)+'0';
225}
226
227// Possible states while outputting ASCII strings
228namespace {
229 enum StringSection {
230 None,
231 Alpha,
232 Numeric
233 };
234}
235
236/// SwitchStringSection - manage the changes required to output bytes as
237/// characters in a string vs. numeric decimal values
238///
239static inline void SwitchStringSection(std::ostream &O, StringSection NewSect,
240 StringSection &Current) {
241 if (Current == None) {
242 if (NewSect == Alpha)
243 O << "\t.byte \"";
244 else if (NewSect == Numeric)
245 O << "\t.byte ";
246 } else if (Current == Alpha) {
247 if (NewSect == None)
248 O << "\"";
249 else if (NewSect == Numeric)
250 O << "\"\n"
251 << "\t.byte ";
252 } else if (Current == Numeric) {
253 if (NewSect == Alpha)
254 O << '\n'
255 << "\t.byte \"";
256 else if (NewSect == Numeric)
257 O << ", ";
258 }
259
260 Current = NewSect;
261}
262
263/// getAsCString - Return the specified array as a C compatible
264/// string, only if the predicate isStringCompatible is true.
265///
266static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
267 assert(isStringCompatible(CVA) && "Array is not string compatible!");
268
269 if (CVA->getNumOperands() == 0)
270 return;
271
272 StringSection Current = None;
273 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i) {
274 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
275 if (C == '"') {
276 SwitchStringSection(O, Alpha, Current);
277 O << "\"\"";
278 } else if (isprint(C)) {
279 SwitchStringSection(O, Alpha, Current);
280 O << C;
281 } else {
282 SwitchStringSection(O, Numeric, Current);
283 O << utostr((unsigned)C);
284 }
285 }
286 SwitchStringSection(O, None, Current);
287 O << '\n';
288}
289
290/// createDarwinAsmPrinterPass - Returns a pass that prints the PPC assembly
291/// code for a MachineFunction to the given output stream, in a format that the
292/// Darwin assembler can deal with.
293///
294FunctionPass *llvm::createDarwinAsmPrinter(std::ostream &o, TargetMachine &tm) {
295 return new DarwinAsmPrinter(o, tm);
296}
297
298/// createAIXAsmPrinterPass - Returns a pass that prints the PPC assembly code
299/// for a MachineFunction to the given output stream, in a format that the
300/// AIX 5L assembler can deal with.
301///
302FunctionPass *llvm::createAIXAsmPrinter(std::ostream &o, TargetMachine &tm) {
303 return new AIXAsmPrinter(o, tm);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000304}
305
Nate Begemane59bf592004-08-14 22:09:10 +0000306// Include the auto-generated portion of the assembly writer
307#include "PowerPCGenAsmWriter.inc"
308
Misha Brukman3e0b51a2004-09-05 02:42:44 +0000309void PowerPCAsmPrinter::printOp(const MachineOperand &MO,
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000310 bool LoadAddrOp /* = false */) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000311 const MRegisterInfo &RI = *TM.getRegisterInfo();
312 int new_symbol;
313
314 switch (MO.getType()) {
315 case MachineOperand::MO_VirtualRegister:
316 if (Value *V = MO.getVRegValueOrNull()) {
317 O << "<" << V->getName() << ">";
318 return;
319 }
320 // FALLTHROUGH
321 case MachineOperand::MO_MachineRegister:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000322 case MachineOperand::MO_CCRegister:
Misha Brukman7f484a52004-06-24 23:51:00 +0000323 O << LowercaseString(RI.get(MO.getReg()).Name);
324 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000325
326 case MachineOperand::MO_SignExtendedImmed:
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000327 case MachineOperand::MO_UnextendedImmed:
328 std::cerr << "printOp() does not handle immediate values\n";
329 abort();
Misha Brukman97a296f2004-07-21 20:11:11 +0000330 return;
331
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000332 case MachineOperand::MO_PCRelativeDisp:
333 std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
334 abort();
335 return;
336
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000337 case MachineOperand::MO_MachineBasicBlock: {
338 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
339 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
Misha Brukman218bec72004-06-29 17:13:26 +0000340 << "_" << MBBOp->getNumber() << "\t; "
Misha Brukman2bf183c2004-06-25 15:42:10 +0000341 << MBBOp->getBasicBlock()->getName();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000342 return;
343 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000344
345 case MachineOperand::MO_ConstantPoolIndex:
346 O << ".CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000347 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000348
349 case MachineOperand::MO_ExternalSymbol:
350 O << MO.getSymbolName();
351 return;
352
Nate Begemanb73a7112004-08-13 09:32:01 +0000353 case MachineOperand::MO_GlobalAddress: {
354 GlobalValue *GV = MO.getGlobal();
355 std::string Name = Mang->getValueName(GV);
Misha Brukmane2eceb52004-07-23 16:08:20 +0000356
Nate Begemanb73a7112004-08-13 09:32:01 +0000357 // Dynamically-resolved functions need a stub for the function. Be
358 // wary however not to output $stub for external functions whose addresses
359 // are taken. Those should be emitted as $non_lazy_ptr below.
360 Function *F = dyn_cast<Function>(GV);
361 if (F && F->isExternal() && !LoadAddrOp &&
Chris Lattnera3840792004-08-16 23:25:21 +0000362 getTM().CalledFunctions.count(F)) {
Nate Begemanb73a7112004-08-13 09:32:01 +0000363 FnStubs.insert(Name);
364 O << "L" << Name << "$stub";
365 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000366 }
Nate Begemane59bf592004-08-14 22:09:10 +0000367
Nate Begemanfcf4a422004-10-17 23:01:34 +0000368 // External or weakly linked global variables need non-lazily-resolved stubs
369 if ((GV->isExternal() || GV->hasWeakLinkage()) &&
370 getTM().AddressTaken.count(GV)) {
Nate Begemanb73a7112004-08-13 09:32:01 +0000371 GVStubs.insert(Name);
372 O << "L" << Name << "$non_lazy_ptr";
373 return;
374 }
Nate Begemane59bf592004-08-14 22:09:10 +0000375
Chris Lattnera3840792004-08-16 23:25:21 +0000376 if (F && LoadAddrOp && getTM().AddressTaken.count(GV)) {
Nate Begemane59bf592004-08-14 22:09:10 +0000377 LinkOnceStubs.insert(Name);
378 O << "L" << Name << "$non_lazy_ptr";
379 return;
380 }
Nate Begemanb73a7112004-08-13 09:32:01 +0000381
382 O << Mang->getValueName(GV);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000383 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000384 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000385
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000386 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000387 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000388 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000389 }
390}
391
Nate Begemane59bf592004-08-14 22:09:10 +0000392/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
393/// the current output stream.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000394///
Misha Brukman3e0b51a2004-09-05 02:42:44 +0000395void PowerPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Nate Begemane59bf592004-08-14 22:09:10 +0000396 ++EmittedInsts;
397 if (printInstruction(MI))
398 return; // Printer was automatically generated
Nate Begemaned428532004-09-04 05:00:00 +0000399
400 assert(0 && "Unhandled instruction in asm writer!");
401 abort();
Nate Begemane59bf592004-08-14 22:09:10 +0000402 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000403}
404
Nate Begemaned428532004-09-04 05:00:00 +0000405/// runOnMachineFunction - This uses the printMachineInstruction()
406/// method to print assembly for each instruction.
407///
408bool DarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
409 setupMachineFunction(MF);
410 O << "\n\n";
411
412 // Print out constants referenced by the function
413 printConstantPool(MF.getConstantPool());
414
415 // Print out labels for the function.
416 O << "\t.text\n";
417 emitAlignment(2);
418 O << "\t.globl\t" << CurrentFnName << "\n";
419 O << CurrentFnName << ":\n";
420
421 // Print out code for the function.
422 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
423 I != E; ++I) {
424 // Print a label for the basic block.
425 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
426 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
427 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
428 II != E; ++II) {
429 // Print the assembly for the instruction.
430 O << "\t";
431 printMachineInstruction(II);
432 }
433 }
434 ++LabelNumber;
435
436 // We didn't modify anything.
437 return false;
438}
439
440/// printConstantPool - Print to the current output stream assembly
441/// representations of the constants in the constant pool MCP. This is
442/// used to print out constants which have been "spilled to memory" by
443/// the code generator.
444///
445void DarwinAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
446 const std::vector<Constant*> &CP = MCP->getConstants();
447 const TargetData &TD = TM.getTargetData();
448
449 if (CP.empty()) return;
450
451 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
452 O << "\t.const\n";
453 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
454 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
455 << *CP[i] << "\n";
456 emitGlobalConstant(CP[i]);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000457 }
458}
459
Nate Begemaned428532004-09-04 05:00:00 +0000460bool DarwinAsmPrinter::doFinalization(Module &M) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000461 const TargetData &TD = TM.getTargetData();
462 std::string CurSection;
463
464 // Print out module-level global variables here.
465 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
466 if (I->hasInitializer()) { // External global require no code
Nate Begemana2de1022004-09-22 04:40:25 +0000467 O << '\n';
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000468 std::string name = Mang->getValueName(I);
469 Constant *C = I->getInitializer();
470 unsigned Size = TD.getTypeSize(C->getType());
Chris Lattner69d485e2004-08-17 19:26:03 +0000471 unsigned Align = TD.getTypeAlignmentShift(C->getType());
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000472
Misha Brukman97a296f2004-07-21 20:11:11 +0000473 if (C->isNullValue() && /* FIXME: Verify correct */
474 (I->hasInternalLinkage() || I->hasWeakLinkage())) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000475 SwitchSection(O, CurSection, ".data");
476 if (I->hasInternalLinkage())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000477 O << ".lcomm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattner69d485e2004-08-17 19:26:03 +0000478 << "," << Align;
Misha Brukman218bec72004-06-29 17:13:26 +0000479 else
Misha Brukmane2eceb52004-07-23 16:08:20 +0000480 O << ".comm " << name << "," << TD.getTypeSize(C->getType());
Misha Brukman218bec72004-06-29 17:13:26 +0000481 O << "\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000482 WriteAsOperand(O, I, true, true, &M);
Nate Begemana2de1022004-09-22 04:40:25 +0000483 O << '\n';
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000484 } else {
485 switch (I->getLinkage()) {
486 case GlobalValue::LinkOnceLinkage:
Misha Brukman97a296f2004-07-21 20:11:11 +0000487 O << ".section __TEXT,__textcoal_nt,coalesced,no_toc\n"
488 << ".weak_definition " << name << '\n'
489 << ".private_extern " << name << '\n'
490 << ".section __DATA,__datacoal_nt,coalesced,no_toc\n";
491 LinkOnceStubs.insert(name);
492 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000493 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
494 // Nonnull linkonce -> weak
495 O << "\t.weak " << name << "\n";
496 SwitchSection(O, CurSection, "");
497 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
498 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000499 case GlobalValue::AppendingLinkage:
500 // FIXME: appending linkage variables should go into a section of
501 // their name or something. For now, just emit them as external.
502 case GlobalValue::ExternalLinkage:
503 // If external or appending, declare as a global symbol
504 O << "\t.globl " << name << "\n";
505 // FALL THROUGH
506 case GlobalValue::InternalLinkage:
Misha Brukman61297ee2004-06-29 23:40:57 +0000507 SwitchSection(O, CurSection, ".data");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000508 break;
509 }
510
Chris Lattner69d485e2004-08-17 19:26:03 +0000511 emitAlignment(Align);
Misha Brukman218bec72004-06-29 17:13:26 +0000512 O << name << ":\t\t\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000513 WriteAsOperand(O, I, true, true, &M);
514 O << " = ";
515 WriteAsOperand(O, C, false, false, &M);
516 O << "\n";
517 emitGlobalConstant(C);
518 }
519 }
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000520
521 // Output stubs for dynamically-linked functions
522 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
523 i != e; ++i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000524 {
Misha Brukmane2eceb52004-07-23 16:08:20 +0000525 O << ".data\n";
526 O << ".section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n";
Chris Lattner69d485e2004-08-17 19:26:03 +0000527 emitAlignment(2);
Misha Brukman46fd00a2004-06-24 23:04:11 +0000528 O << "L" << *i << "$stub:\n";
529 O << "\t.indirect_symbol " << *i << "\n";
530 O << "\tmflr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000531 O << "\tbcl 20,31,L0$" << *i << "\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000532 O << "L0$" << *i << ":\n";
533 O << "\tmflr r11\n";
534 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
535 O << "\tmtlr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000536 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000537 O << "\tmtctr r12\n";
538 O << "\tbctr\n";
539 O << ".data\n";
540 O << ".lazy_symbol_pointer\n";
541 O << "L" << *i << "$lazy_ptr:\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000542 O << "\t.indirect_symbol " << *i << "\n";
543 O << "\t.long dyld_stub_binding_helper\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000544 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000545
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000546 O << "\n";
547
548 // Output stubs for external global variables
549 if (GVStubs.begin() != GVStubs.end())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000550 O << ".data\n.non_lazy_symbol_pointer\n";
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000551 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
552 i != e; ++i) {
553 O << "L" << *i << "$non_lazy_ptr:\n";
554 O << "\t.indirect_symbol " << *i << "\n";
555 O << "\t.long\t0\n";
556 }
557
Nate Begemane59bf592004-08-14 22:09:10 +0000558 // Output stubs for link-once variables
559 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
560 O << ".data\n.align 2\n";
561 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
562 e = LinkOnceStubs.end(); i != e; ++i) {
563 O << "L" << *i << "$non_lazy_ptr:\n"
564 << "\t.long\t" << *i << '\n';
565 }
566
Chris Lattnera3840792004-08-16 23:25:21 +0000567 AsmPrinter::doFinalization(M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000568 return false; // success
569}
Nate Begemaned428532004-09-04 05:00:00 +0000570
571/// runOnMachineFunction - This uses the printMachineInstruction()
572/// method to print assembly for each instruction.
573///
574bool AIXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
575 CurrentFnName = MF.getFunction()->getName();
576
577 // Print out constants referenced by the function
578 printConstantPool(MF.getConstantPool());
579
580 // Print out header for the function.
581 O << "\t.csect .text[PR]\n"
582 << "\t.align 2\n"
583 << "\t.globl " << CurrentFnName << '\n'
584 << "\t.globl ." << CurrentFnName << '\n'
585 << "\t.csect " << CurrentFnName << "[DS],3\n"
586 << CurrentFnName << ":\n"
587 << "\t.llong ." << CurrentFnName << ", TOC[tc0], 0\n"
588 << "\t.csect .text[PR]\n"
589 << '.' << CurrentFnName << ":\n";
590
591 // Print out code for the function.
592 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
593 I != E; ++I) {
594 // Print a label for the basic block.
595 O << "LBB" << CurrentFnName << "_" << I->getNumber() << ":\t# "
596 << I->getBasicBlock()->getName() << "\n";
597 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
598 II != E; ++II) {
599 // Print the assembly for the instruction.
600 O << "\t";
601 printMachineInstruction(II);
602 }
603 }
604 ++LabelNumber;
605
606 O << "LT.." << CurrentFnName << ":\n"
607 << "\t.long 0\n"
608 << "\t.byte 0,0,32,65,128,0,0,0\n"
609 << "\t.long LT.." << CurrentFnName << "-." << CurrentFnName << '\n'
610 << "\t.short 3\n"
611 << "\t.byte \"" << CurrentFnName << "\"\n"
612 << "\t.align 2\n";
613
614 // We didn't modify anything.
615 return false;
616}
617
618/// printConstantPool - Print to the current output stream assembly
619/// representations of the constants in the constant pool MCP. This is
620/// used to print out constants which have been "spilled to memory" by
621/// the code generator.
622///
623void AIXAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
624 const std::vector<Constant*> &CP = MCP->getConstants();
625 const TargetData &TD = TM.getTargetData();
626
627 if (CP.empty()) return;
628
629 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
630 O << "\t.const\n";
631 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
632 << "\n";
633 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t;"
634 << *CP[i] << "\n";
635 emitGlobalConstant(CP[i]);
636 }
637}
638
639bool AIXAsmPrinter::doInitialization(Module &M) {
640 const TargetData &TD = TM.getTargetData();
641 std::string CurSection;
642
643 O << "\t.machine \"ppc64\"\n"
644 << "\t.toc\n"
645 << "\t.csect .text[PR]\n";
646
647 // Print out module-level global variables
648 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) {
649 if (!I->hasInitializer())
650 continue;
651
652 std::string Name = I->getName();
653 Constant *C = I->getInitializer();
654 // N.B.: We are defaulting to writable strings
655 if (I->hasExternalLinkage()) {
656 O << "\t.globl " << Name << '\n'
657 << "\t.csect .data[RW],3\n";
658 } else {
659 O << "\t.csect _global.rw_c[RW],3\n";
660 }
661 O << Name << ":\n";
662 emitGlobalConstant(C);
663 }
664
665 // Output labels for globals
666 if (M.gbegin() != M.gend()) O << "\t.toc\n";
667 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) {
668 const GlobalVariable *GV = I;
669 // Do not output labels for unused variables
670 if (GV->isExternal() && GV->use_begin() == GV->use_end())
671 continue;
672
673 std::string Name = GV->getName();
674 std::string Label = "LC.." + utostr(LabelNumber++);
675 GVToLabelMap[GV] = Label;
676 O << Label << ":\n"
677 << "\t.tc " << Name << "[TC]," << Name;
678 if (GV->isExternal()) O << "[RW]";
679 O << '\n';
680 }
681
682 Mang = new Mangler(M, ".");
683 return false; // success
684}
685
686bool AIXAsmPrinter::doFinalization(Module &M) {
687 const TargetData &TD = TM.getTargetData();
688 // Print out module-level global variables
689 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) {
690 if (I->hasInitializer() || I->hasExternalLinkage())
691 continue;
692
693 std::string Name = I->getName();
694 if (I->hasInternalLinkage()) {
695 O << "\t.lcomm " << Name << ",16,_global.bss_c";
696 } else {
697 O << "\t.comm " << Name << "," << TD.getTypeSize(I->getType())
698 << "," << log2((unsigned)TD.getTypeAlignment(I->getType()));
699 }
700 O << "\t\t# ";
701 WriteAsOperand(O, I, true, true, &M);
702 O << "\n";
703 }
704
705 O << "_section_.text:\n"
706 << "\t.csect .data[RW],3\n"
707 << "\t.llong _section_.text\n";
708
709 delete Mang;
710 return false; // success
711}