blob: b0503d2a2da38518148ac38f525e69abad081da8 [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 Brukmanf2ccb772004-08-17 04:55:41 +000043 struct PPC32AsmPrinter : 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 Brukmanf2ccb772004-08-17 04:55:41 +000047 PPC32AsmPrinter(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,
104 MVT::ValueType VT) {
Nate Begemaned428532004-09-04 05:00:00 +0000105
106 // 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,
115 MVT::ValueType VT) {
116 // 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,
121 MVT::ValueType VT) {
122 O << "ha16(";
123 printOp(MI->getOperand(OpNo), true /* LoadAddrOp */);
124 O << "-\"L0000" << LabelNumber << "$pb\")";
125 }
126 void printSymbolLo(const MachineInstr *MI, unsigned OpNo,
127 MVT::ValueType VT) {
128 // 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
145 //
146 //
147 struct DarwinAsmPrinter : public PPC32AsmPrinter {
148
149 DarwinAsmPrinter(std::ostream &O, TargetMachine &TM)
150 : PPC32AsmPrinter(O, TM) {
151 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
167 //
168 //
169 struct AIXAsmPrinter : public PPC32AsmPrinter {
170 /// Map for labels corresponding to global variables
171 ///
172 std::map<const GlobalVariable*,std::string> GVToLabelMap;
173
174 AIXAsmPrinter(std::ostream &O, TargetMachine &TM)
175 : PPC32AsmPrinter(O, TM) {
176 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 Brukmanf2ccb772004-08-17 04:55:41 +0000309void PPC32AsmPrinter::printOp(const MachineOperand &MO,
310 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 Begemanb73a7112004-08-13 09:32:01 +0000368 // External global variables need a non-lazily-resolved stub
Chris Lattnera3840792004-08-16 23:25:21 +0000369 if (GV->isExternal() && getTM().AddressTaken.count(GV)) {
Nate Begemanb73a7112004-08-13 09:32:01 +0000370 GVStubs.insert(Name);
371 O << "L" << Name << "$non_lazy_ptr";
372 return;
373 }
Nate Begemane59bf592004-08-14 22:09:10 +0000374
Chris Lattnera3840792004-08-16 23:25:21 +0000375 if (F && LoadAddrOp && getTM().AddressTaken.count(GV)) {
Nate Begemane59bf592004-08-14 22:09:10 +0000376 LinkOnceStubs.insert(Name);
377 O << "L" << Name << "$non_lazy_ptr";
378 return;
379 }
Nate Begemanb73a7112004-08-13 09:32:01 +0000380
381 O << Mang->getValueName(GV);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000382 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000383 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000384
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000385 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000386 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000387 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000388 }
389}
390
Nate Begemane59bf592004-08-14 22:09:10 +0000391/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
392/// the current output stream.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000393///
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000394void PPC32AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Nate Begemane59bf592004-08-14 22:09:10 +0000395 ++EmittedInsts;
396 if (printInstruction(MI))
397 return; // Printer was automatically generated
Nate Begemaned428532004-09-04 05:00:00 +0000398
399 assert(0 && "Unhandled instruction in asm writer!");
400 abort();
Nate Begemane59bf592004-08-14 22:09:10 +0000401 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000402}
403
Nate Begemaned428532004-09-04 05:00:00 +0000404/// runOnMachineFunction - This uses the printMachineInstruction()
405/// method to print assembly for each instruction.
406///
407bool DarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
408 setupMachineFunction(MF);
409 O << "\n\n";
410
411 // Print out constants referenced by the function
412 printConstantPool(MF.getConstantPool());
413
414 // Print out labels for the function.
415 O << "\t.text\n";
416 emitAlignment(2);
417 O << "\t.globl\t" << CurrentFnName << "\n";
418 O << CurrentFnName << ":\n";
419
420 // Print out code for the function.
421 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
422 I != E; ++I) {
423 // Print a label for the basic block.
424 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
425 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
426 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
427 II != E; ++II) {
428 // Print the assembly for the instruction.
429 O << "\t";
430 printMachineInstruction(II);
431 }
432 }
433 ++LabelNumber;
434
435 // We didn't modify anything.
436 return false;
437}
438
439/// printConstantPool - Print to the current output stream assembly
440/// representations of the constants in the constant pool MCP. This is
441/// used to print out constants which have been "spilled to memory" by
442/// the code generator.
443///
444void DarwinAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
445 const std::vector<Constant*> &CP = MCP->getConstants();
446 const TargetData &TD = TM.getTargetData();
447
448 if (CP.empty()) return;
449
450 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
451 O << "\t.const\n";
452 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
453 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
454 << *CP[i] << "\n";
455 emitGlobalConstant(CP[i]);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000456 }
457}
458
Nate Begemaned428532004-09-04 05:00:00 +0000459bool DarwinAsmPrinter::doFinalization(Module &M) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000460 const TargetData &TD = TM.getTargetData();
461 std::string CurSection;
462
463 // Print out module-level global variables here.
464 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
465 if (I->hasInitializer()) { // External global require no code
466 O << "\n\n";
467 std::string name = Mang->getValueName(I);
468 Constant *C = I->getInitializer();
469 unsigned Size = TD.getTypeSize(C->getType());
Chris Lattner69d485e2004-08-17 19:26:03 +0000470 unsigned Align = TD.getTypeAlignmentShift(C->getType());
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000471
Misha Brukman97a296f2004-07-21 20:11:11 +0000472 if (C->isNullValue() && /* FIXME: Verify correct */
473 (I->hasInternalLinkage() || I->hasWeakLinkage())) {
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);
482 O << "\n";
483 } 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;
508 }
509
Chris Lattner69d485e2004-08-17 19:26:03 +0000510 emitAlignment(Align);
Misha Brukman218bec72004-06-29 17:13:26 +0000511 O << name << ":\t\t\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000512 WriteAsOperand(O, I, true, true, &M);
513 O << " = ";
514 WriteAsOperand(O, C, false, false, &M);
515 O << "\n";
516 emitGlobalConstant(C);
517 }
518 }
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000519
520 // Output stubs for dynamically-linked functions
521 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
522 i != e; ++i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000523 {
Misha Brukmane2eceb52004-07-23 16:08:20 +0000524 O << ".data\n";
525 O << ".section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n";
Chris Lattner69d485e2004-08-17 19:26:03 +0000526 emitAlignment(2);
Misha Brukman46fd00a2004-06-24 23:04:11 +0000527 O << "L" << *i << "$stub:\n";
528 O << "\t.indirect_symbol " << *i << "\n";
529 O << "\tmflr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000530 O << "\tbcl 20,31,L0$" << *i << "\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000531 O << "L0$" << *i << ":\n";
532 O << "\tmflr r11\n";
533 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
534 O << "\tmtlr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000535 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000536 O << "\tmtctr r12\n";
537 O << "\tbctr\n";
538 O << ".data\n";
539 O << ".lazy_symbol_pointer\n";
540 O << "L" << *i << "$lazy_ptr:\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000541 O << "\t.indirect_symbol " << *i << "\n";
542 O << "\t.long dyld_stub_binding_helper\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000543 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000544
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000545 O << "\n";
546
547 // Output stubs for external global variables
548 if (GVStubs.begin() != GVStubs.end())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000549 O << ".data\n.non_lazy_symbol_pointer\n";
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000550 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
551 i != e; ++i) {
552 O << "L" << *i << "$non_lazy_ptr:\n";
553 O << "\t.indirect_symbol " << *i << "\n";
554 O << "\t.long\t0\n";
555 }
556
Nate Begemane59bf592004-08-14 22:09:10 +0000557 // Output stubs for link-once variables
558 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
559 O << ".data\n.align 2\n";
560 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
561 e = LinkOnceStubs.end(); i != e; ++i) {
562 O << "L" << *i << "$non_lazy_ptr:\n"
563 << "\t.long\t" << *i << '\n';
564 }
565
Chris Lattnera3840792004-08-16 23:25:21 +0000566 AsmPrinter::doFinalization(M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000567 return false; // success
568}
Nate Begemaned428532004-09-04 05:00:00 +0000569
570/// runOnMachineFunction - This uses the printMachineInstruction()
571/// method to print assembly for each instruction.
572///
573bool AIXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
574 CurrentFnName = MF.getFunction()->getName();
575
576 // Print out constants referenced by the function
577 printConstantPool(MF.getConstantPool());
578
579 // Print out header for the function.
580 O << "\t.csect .text[PR]\n"
581 << "\t.align 2\n"
582 << "\t.globl " << CurrentFnName << '\n'
583 << "\t.globl ." << CurrentFnName << '\n'
584 << "\t.csect " << CurrentFnName << "[DS],3\n"
585 << CurrentFnName << ":\n"
586 << "\t.llong ." << CurrentFnName << ", TOC[tc0], 0\n"
587 << "\t.csect .text[PR]\n"
588 << '.' << CurrentFnName << ":\n";
589
590 // Print out code for the function.
591 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
592 I != E; ++I) {
593 // Print a label for the basic block.
594 O << "LBB" << CurrentFnName << "_" << I->getNumber() << ":\t# "
595 << I->getBasicBlock()->getName() << "\n";
596 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
597 II != E; ++II) {
598 // Print the assembly for the instruction.
599 O << "\t";
600 printMachineInstruction(II);
601 }
602 }
603 ++LabelNumber;
604
605 O << "LT.." << CurrentFnName << ":\n"
606 << "\t.long 0\n"
607 << "\t.byte 0,0,32,65,128,0,0,0\n"
608 << "\t.long LT.." << CurrentFnName << "-." << CurrentFnName << '\n'
609 << "\t.short 3\n"
610 << "\t.byte \"" << CurrentFnName << "\"\n"
611 << "\t.align 2\n";
612
613 // We didn't modify anything.
614 return false;
615}
616
617/// printConstantPool - Print to the current output stream assembly
618/// representations of the constants in the constant pool MCP. This is
619/// used to print out constants which have been "spilled to memory" by
620/// the code generator.
621///
622void AIXAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
623 const std::vector<Constant*> &CP = MCP->getConstants();
624 const TargetData &TD = TM.getTargetData();
625
626 if (CP.empty()) return;
627
628 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
629 O << "\t.const\n";
630 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
631 << "\n";
632 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t;"
633 << *CP[i] << "\n";
634 emitGlobalConstant(CP[i]);
635 }
636}
637
638bool AIXAsmPrinter::doInitialization(Module &M) {
639 const TargetData &TD = TM.getTargetData();
640 std::string CurSection;
641
642 O << "\t.machine \"ppc64\"\n"
643 << "\t.toc\n"
644 << "\t.csect .text[PR]\n";
645
646 // Print out module-level global variables
647 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) {
648 if (!I->hasInitializer())
649 continue;
650
651 std::string Name = I->getName();
652 Constant *C = I->getInitializer();
653 // N.B.: We are defaulting to writable strings
654 if (I->hasExternalLinkage()) {
655 O << "\t.globl " << Name << '\n'
656 << "\t.csect .data[RW],3\n";
657 } else {
658 O << "\t.csect _global.rw_c[RW],3\n";
659 }
660 O << Name << ":\n";
661 emitGlobalConstant(C);
662 }
663
664 // Output labels for globals
665 if (M.gbegin() != M.gend()) O << "\t.toc\n";
666 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) {
667 const GlobalVariable *GV = I;
668 // Do not output labels for unused variables
669 if (GV->isExternal() && GV->use_begin() == GV->use_end())
670 continue;
671
672 std::string Name = GV->getName();
673 std::string Label = "LC.." + utostr(LabelNumber++);
674 GVToLabelMap[GV] = Label;
675 O << Label << ":\n"
676 << "\t.tc " << Name << "[TC]," << Name;
677 if (GV->isExternal()) O << "[RW]";
678 O << '\n';
679 }
680
681 Mang = new Mangler(M, ".");
682 return false; // success
683}
684
685bool AIXAsmPrinter::doFinalization(Module &M) {
686 const TargetData &TD = TM.getTargetData();
687 // Print out module-level global variables
688 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) {
689 if (I->hasInitializer() || I->hasExternalLinkage())
690 continue;
691
692 std::string Name = I->getName();
693 if (I->hasInternalLinkage()) {
694 O << "\t.lcomm " << Name << ",16,_global.bss_c";
695 } else {
696 O << "\t.comm " << Name << "," << TD.getTypeSize(I->getType())
697 << "," << log2((unsigned)TD.getTypeAlignment(I->getType()));
698 }
699 O << "\t\t# ";
700 WriteAsOperand(O, I, true, true, &M);
701 O << "\n";
702 }
703
704 O << "_section_.text:\n"
705 << "\t.csect .data[RW],3\n"
706 << "\t.llong _section_.text\n";
707
708 delete Mang;
709 return false; // success
710}