blob: 798a908c65a1f2b6fbcb84e634b6c30ea491fab6 [file] [log] [blame]
Misha Brukman3d9a6c22004-08-11 00:09:42 +00001//===-- PPC32AsmPrinter.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"
21#include "PowerPCInstrInfo.h"
Nate Begemane59bf592004-08-14 22:09:10 +000022#include "PowerPCTargetMachine.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000023#include "llvm/Constants.h"
24#include "llvm/DerivedTypes.h"
25#include "llvm/Module.h"
26#include "llvm/Assembly/Writer.h"
Chris Lattnera3840792004-08-16 23:25:21 +000027#include "llvm/CodeGen/AsmPrinter.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000028#include "llvm/CodeGen/MachineConstantPool.h"
Misha Brukman05794492004-06-24 17:31:42 +000029#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000030#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner7bb424f2004-08-14 23:27:29 +000031#include "llvm/CodeGen/ValueTypes.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000032#include "llvm/Target/TargetMachine.h"
33#include "llvm/Support/Mangler.h"
Misha Brukman05794492004-06-24 17:31:42 +000034#include "Support/CommandLine.h"
35#include "Support/Debug.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000036#include "Support/Statistic.h"
37#include "Support/StringExtras.h"
Misha Brukman05794492004-06-24 17:31:42 +000038#include <set>
Chris Lattnera3840792004-08-16 23:25:21 +000039using namespace llvm;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000040
41namespace {
42 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
43
Chris Lattnera3840792004-08-16 23:25:21 +000044 struct PowerPCAsmPrinter : public AsmPrinter {
Misha Brukman97a296f2004-07-21 20:11:11 +000045 std::set<std::string> FnStubs, GVStubs, LinkOnceStubs;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000046 std::set<std::string> Strings;
47
Chris Lattnera3840792004-08-16 23:25:21 +000048 PowerPCAsmPrinter(std::ostream &O, TargetMachine &TM)
Chris Lattner505e7832004-08-17 02:29:00 +000049 : AsmPrinter(O, TM), LabelNumber(0) {
50 UsesUnderscorePrefix = 1;
51 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +000052
Misha Brukmancf8d2442004-07-26 16:28:33 +000053 /// Unique incrementer for label values for referencing Global values.
Misha Brukman218bec72004-06-29 17:13:26 +000054 ///
Misha Brukmancf8d2442004-07-26 16:28:33 +000055 unsigned LabelNumber;
56
Misha Brukman5dfe3a92004-06-21 16:55:25 +000057 virtual const char *getPassName() const {
Nate Begemane59bf592004-08-14 22:09:10 +000058 return "PowerPC Assembly Printer";
Misha Brukman5dfe3a92004-06-21 16:55:25 +000059 }
60
Chris Lattnera3840792004-08-16 23:25:21 +000061 PowerPCTargetMachine &getTM() {
62 return static_cast<PowerPCTargetMachine&>(TM);
63 }
64
Nate Begemane59bf592004-08-14 22:09:10 +000065 /// printInstruction - This method is automatically generated by tablegen
66 /// from the instruction set description. This method returns true if the
67 /// machine instruction was sufficiently described to print it, otherwise it
68 /// returns false.
69 bool printInstruction(const MachineInstr *MI);
70
Misha Brukman5dfe3a92004-06-21 16:55:25 +000071 void printMachineInstruction(const MachineInstr *MI);
Nate Begemanb73a7112004-08-13 09:32:01 +000072 void printOp(const MachineOperand &MO, bool LoadAddrOp = false);
Misha Brukmanaf313fb2004-07-28 00:00:48 +000073 void printImmOp(const MachineOperand &MO, unsigned ArgType);
Chris Lattner7bb424f2004-08-14 23:27:29 +000074
75 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
76 const MachineOperand &MO = MI->getOperand(OpNo);
77 if (MO.getType() == MachineOperand::MO_MachineRegister) {
78 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
79 O << LowercaseString(TM.getRegisterInfo()->get(MO.getReg()).Name);
Chris Lattner0ea31712004-08-15 05:46:14 +000080 } else if (MO.isImmediate()) {
81 O << MO.getImmedValue();
Chris Lattner7bb424f2004-08-14 23:27:29 +000082 } else {
83 printOp(MO);
84 }
85 }
86
Chris Lattner97b2a2e2004-08-15 05:20:16 +000087 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo,
88 MVT::ValueType VT) {
89 O << (unsigned short)MI->getOperand(OpNo).getImmedValue();
90 }
91
Misha Brukman5dfe3a92004-06-21 16:55:25 +000092 void printConstantPool(MachineConstantPool *MCP);
93 bool runOnMachineFunction(MachineFunction &F);
Misha Brukman5dfe3a92004-06-21 16:55:25 +000094 bool doFinalization(Module &M);
95 void emitGlobalConstant(const Constant* CV);
Misha Brukman5dfe3a92004-06-21 16:55:25 +000096 };
97} // end of anonymous namespace
98
Misha Brukman3d9a6c22004-08-11 00:09:42 +000099/// createPPC32AsmPrinterPass - Returns a pass that prints the PPC
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000100/// assembly code for a MachineFunction to the given output stream,
101/// using the given target machine description. This should work
Misha Brukman7103fba2004-08-09 22:27:45 +0000102/// regardless of whether the function is in SSA form or not.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000103///
Chris Lattnera3840792004-08-16 23:25:21 +0000104FunctionPass *llvm::createPPCAsmPrinter(std::ostream &o,TargetMachine &tm) {
Nate Begemane59bf592004-08-14 22:09:10 +0000105 return new PowerPCAsmPrinter(o, tm);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000106}
107
Nate Begemane59bf592004-08-14 22:09:10 +0000108// Include the auto-generated portion of the assembly writer
109#include "PowerPCGenAsmWriter.inc"
110
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000111/// toOctal - Convert the low order bits of X into an octal digit.
112///
113static inline char toOctal(int X) {
114 return (X&7)+'0';
115}
116
117/// getAsCString - Return the specified array as a C compatible
Chris Lattner36aacdb2004-08-16 23:30:16 +0000118/// string, only if the predicate isString is true.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000119///
120static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
Chris Lattner36aacdb2004-08-16 23:30:16 +0000121 assert(CVA->isString() && "Array is not string compatible!");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000122
123 O << "\"";
Chris Lattner36aacdb2004-08-16 23:30:16 +0000124 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000125 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
126
127 if (C == '"') {
128 O << "\\\"";
129 } else if (C == '\\') {
130 O << "\\\\";
131 } else if (isprint(C)) {
132 O << C;
133 } else {
Chris Lattner36aacdb2004-08-16 23:30:16 +0000134 switch(C) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000135 case '\b': O << "\\b"; break;
136 case '\f': O << "\\f"; break;
137 case '\n': O << "\\n"; break;
138 case '\r': O << "\\r"; break;
139 case '\t': O << "\\t"; break;
140 default:
141 O << '\\';
142 O << toOctal(C >> 6);
143 O << toOctal(C >> 3);
144 O << toOctal(C >> 0);
145 break;
146 }
147 }
148 }
149 O << "\"";
150}
151
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000152// Print a constant value or values, with the appropriate storage class as a
153// prefix.
Nate Begemane59bf592004-08-14 22:09:10 +0000154void PowerPCAsmPrinter::emitGlobalConstant(const Constant *CV) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000155 const TargetData &TD = TM.getTargetData();
156
Chris Lattner416ca332004-08-16 23:38:36 +0000157 if (CV->isNullValue()) {
158 O << "\t.space\t" << TD.getTypeSize(CV->getType()) << "\n";
159 return;
160 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
161 if (CVA->isString()) {
162 O << "\t.ascii\t";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000163 printAsCString(O, CVA);
164 O << "\n";
165 } else { // Not a string. Print the values in successive locations
Chris Lattner416ca332004-08-16 23:38:36 +0000166 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattner6173cd92004-08-04 17:29:14 +0000167 emitGlobalConstant(CVA->getOperand(i));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000168 }
169 return;
170 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
171 // Print the fields in successive locations. Pad to align if needed!
172 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000173 unsigned sizeSoFar = 0;
Chris Lattner416ca332004-08-16 23:38:36 +0000174 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
Chris Lattner6173cd92004-08-04 17:29:14 +0000175 const Constant* field = CVS->getOperand(i);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000176
177 // Check if padding is needed and insert one or more 0s.
178 unsigned fieldSize = TD.getTypeSize(field->getType());
Chris Lattner6173cd92004-08-04 17:29:14 +0000179 unsigned padSize = ((i == e-1? cvsLayout->StructSize
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000180 : cvsLayout->MemberOffsets[i+1])
181 - cvsLayout->MemberOffsets[i]) - fieldSize;
182 sizeSoFar += fieldSize + padSize;
183
184 // Now print the actual field value
185 emitGlobalConstant(field);
186
187 // Insert the field padding unless it's zero bytes...
188 if (padSize)
189 O << "\t.space\t " << padSize << "\n";
190 }
191 assert(sizeSoFar == cvsLayout->StructSize &&
192 "Layout of constant struct may be incorrect!");
193 return;
194 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
195 // FP Constants are printed as integer constants to avoid losing
196 // precision...
197 double Val = CFP->getValue();
Chris Lattner469ab792004-08-17 02:48:44 +0000198 if (CFP->getType() == Type::DoubleTy) {
Chris Lattner505e7832004-08-17 02:29:00 +0000199 union DU { // Abide by C TBAA rules
200 double FVal;
201 uint64_t UVal;
Chris Lattner505e7832004-08-17 02:29:00 +0000202 } U;
203 U.FVal = Val;
Chris Lattner469ab792004-08-17 02:48:44 +0000204
205 if (TD.isBigEndian()) {
206 O << ".long\t" << unsigned(U.UVal >> 32)
207 << "\t; double most significant word " << Val << "\n";
208 O << ".long\t" << unsigned(U.UVal)
209 << "\t; double least significant word " << Val << "\n";
210 } else {
211 O << ".long\t" << unsigned(U.UVal)
212 << "\t; double least significant word " << Val << "\n";
213 O << ".long\t" << unsigned(U.UVal >> 32)
214 << "\t; double most significant word " << Val << "\n";
215 }
Chris Lattner505e7832004-08-17 02:29:00 +0000216 return;
217 } else {
218 union FU { // Abide by C TBAA rules
219 float FVal;
220 int32_t UVal;
221 } U;
222 U.FVal = Val;
223
224 O << ".long\t" << U.UVal << "\t; float " << Val << "\n";
225 return;
226 }
Misha Brukmanf63bc192004-07-28 19:12:24 +0000227 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
Misha Brukman2bf183c2004-06-25 15:42:10 +0000228 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattner469ab792004-08-17 02:48:44 +0000229 uint64_t Val = CI->getRawValue();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000230
Chris Lattner469ab792004-08-17 02:48:44 +0000231 if (TD.isBigEndian()) {
232 O << ".long\t" << unsigned(Val >> 32)
233 << "\t; Double-word most significant word " << Val << "\n";
234 O << ".long\t" << unsigned(Val)
235 << "\t; Double-word least significant word " << Val << "\n";
236 } else {
237 O << ".long\t" << unsigned(Val)
238 << "\t; Double-word least significant word " << Val << "\n";
239 O << ".long\t" << unsigned(Val >> 32)
240 << "\t; Double-word most significant word " << Val << "\n";
241 }
Misha Brukman46fd00a2004-06-24 23:04:11 +0000242 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000243 }
244 }
245
246 const Type *type = CV->getType();
247 O << "\t";
Misha Brukmand71bd562004-06-21 17:19:08 +0000248 switch (type->getTypeID()) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000249 case Type::UByteTyID: case Type::SByteTyID:
250 O << ".byte";
251 break;
252 case Type::UShortTyID: case Type::ShortTyID:
253 O << ".short";
254 break;
255 case Type::BoolTyID:
256 case Type::PointerTyID:
257 case Type::UIntTyID: case Type::IntTyID:
258 O << ".long";
259 break;
260 case Type::ULongTyID: case Type::LongTyID:
Misha Brukman46fd00a2004-06-24 23:04:11 +0000261 assert (0 && "Should have already output double-word constant.");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000262 case Type::FloatTyID: case Type::DoubleTyID:
263 assert (0 && "Should have already output floating point constant.");
264 default:
Chris Lattner416ca332004-08-16 23:38:36 +0000265 assert (0 && "Can't handle printing this type of thing");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000266 break;
267 }
268 O << "\t";
269 emitConstantValueOnly(CV);
270 O << "\n";
271}
272
273/// printConstantPool - Print to the current output stream assembly
274/// representations of the constants in the constant pool MCP. This is
275/// used to print out constants which have been "spilled to memory" by
276/// the code generator.
277///
Nate Begemane59bf592004-08-14 22:09:10 +0000278void PowerPCAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000279 const std::vector<Constant*> &CP = MCP->getConstants();
280 const TargetData &TD = TM.getTargetData();
281
282 if (CP.empty()) return;
283
284 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
285 O << "\t.const\n";
286 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
287 << "\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000288 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t;"
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000289 << *CP[i] << "\n";
290 emitGlobalConstant(CP[i]);
291 }
292}
293
294/// runOnMachineFunction - This uses the printMachineInstruction()
295/// method to print assembly for each instruction.
296///
Nate Begemane59bf592004-08-14 22:09:10 +0000297bool PowerPCAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattnera3840792004-08-16 23:25:21 +0000298 setupMachineFunction(MF);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000299 O << "\n\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000300
301 // Print out constants referenced by the function
302 printConstantPool(MF.getConstantPool());
303
304 // Print out labels for the function.
305 O << "\t.text\n";
306 O << "\t.globl\t" << CurrentFnName << "\n";
Misha Brukman61297ee2004-06-29 23:40:57 +0000307 O << "\t.align 2\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000308 O << CurrentFnName << ":\n";
309
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000310 // Print out code for the function.
311 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
312 I != E; ++I) {
313 // Print a label for the basic block.
Misha Brukman218bec72004-06-29 17:13:26 +0000314 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t; "
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000315 << I->getBasicBlock()->getName() << "\n";
316 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukman46fd00a2004-06-24 23:04:11 +0000317 II != E; ++II) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000318 // Print the assembly for the instruction.
319 O << "\t";
320 printMachineInstruction(II);
321 }
322 }
Misha Brukmancf8d2442004-07-26 16:28:33 +0000323 ++LabelNumber;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000324
325 // We didn't modify anything.
326 return false;
327}
328
Nate Begemane59bf592004-08-14 22:09:10 +0000329void PowerPCAsmPrinter::printOp(const MachineOperand &MO,
Nate Begemanb73a7112004-08-13 09:32:01 +0000330 bool LoadAddrOp /* = false */) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000331 const MRegisterInfo &RI = *TM.getRegisterInfo();
332 int new_symbol;
333
334 switch (MO.getType()) {
335 case MachineOperand::MO_VirtualRegister:
336 if (Value *V = MO.getVRegValueOrNull()) {
337 O << "<" << V->getName() << ">";
338 return;
339 }
340 // FALLTHROUGH
341 case MachineOperand::MO_MachineRegister:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000342 case MachineOperand::MO_CCRegister:
Misha Brukman7f484a52004-06-24 23:51:00 +0000343 O << LowercaseString(RI.get(MO.getReg()).Name);
344 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000345
346 case MachineOperand::MO_SignExtendedImmed:
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000347 case MachineOperand::MO_UnextendedImmed:
348 std::cerr << "printOp() does not handle immediate values\n";
349 abort();
Misha Brukman97a296f2004-07-21 20:11:11 +0000350 return;
351
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000352 case MachineOperand::MO_PCRelativeDisp:
353 std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
354 abort();
355 return;
356
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000357 case MachineOperand::MO_MachineBasicBlock: {
358 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
359 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
Misha Brukman218bec72004-06-29 17:13:26 +0000360 << "_" << MBBOp->getNumber() << "\t; "
Misha Brukman2bf183c2004-06-25 15:42:10 +0000361 << MBBOp->getBasicBlock()->getName();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000362 return;
363 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000364
365 case MachineOperand::MO_ConstantPoolIndex:
366 O << ".CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000367 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000368
369 case MachineOperand::MO_ExternalSymbol:
370 O << MO.getSymbolName();
371 return;
372
Nate Begemanb73a7112004-08-13 09:32:01 +0000373 case MachineOperand::MO_GlobalAddress: {
374 GlobalValue *GV = MO.getGlobal();
375 std::string Name = Mang->getValueName(GV);
Misha Brukmane2eceb52004-07-23 16:08:20 +0000376
Nate Begemanb73a7112004-08-13 09:32:01 +0000377 // Dynamically-resolved functions need a stub for the function. Be
378 // wary however not to output $stub for external functions whose addresses
379 // are taken. Those should be emitted as $non_lazy_ptr below.
380 Function *F = dyn_cast<Function>(GV);
381 if (F && F->isExternal() && !LoadAddrOp &&
Chris Lattnera3840792004-08-16 23:25:21 +0000382 getTM().CalledFunctions.count(F)) {
Nate Begemanb73a7112004-08-13 09:32:01 +0000383 FnStubs.insert(Name);
384 O << "L" << Name << "$stub";
385 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000386 }
Nate Begemane59bf592004-08-14 22:09:10 +0000387
Nate Begemanb73a7112004-08-13 09:32:01 +0000388 // External global variables need a non-lazily-resolved stub
Chris Lattnera3840792004-08-16 23:25:21 +0000389 if (GV->isExternal() && getTM().AddressTaken.count(GV)) {
Nate Begemanb73a7112004-08-13 09:32:01 +0000390 GVStubs.insert(Name);
391 O << "L" << Name << "$non_lazy_ptr";
392 return;
393 }
Nate Begemane59bf592004-08-14 22:09:10 +0000394
Chris Lattnera3840792004-08-16 23:25:21 +0000395 if (F && LoadAddrOp && getTM().AddressTaken.count(GV)) {
Nate Begemane59bf592004-08-14 22:09:10 +0000396 LinkOnceStubs.insert(Name);
397 O << "L" << Name << "$non_lazy_ptr";
398 return;
399 }
Nate Begemanb73a7112004-08-13 09:32:01 +0000400
401 O << Mang->getValueName(GV);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000402 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000403 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000404
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000405 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000406 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000407 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000408 }
409}
410
Nate Begemane59bf592004-08-14 22:09:10 +0000411void PowerPCAsmPrinter::printImmOp(const MachineOperand &MO, unsigned ArgType) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000412 int Imm = MO.getImmedValue();
Misha Brukman5b570812004-08-10 22:47:03 +0000413 if (ArgType == PPCII::Simm16 || ArgType == PPCII::Disimm16) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000414 O << (short)Imm;
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000415 } else {
416 O << Imm;
417 }
418}
419
Nate Begemane59bf592004-08-14 22:09:10 +0000420/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
421/// the current output stream.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000422///
Nate Begemane59bf592004-08-14 22:09:10 +0000423void PowerPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
424 ++EmittedInsts;
425 if (printInstruction(MI))
426 return; // Printer was automatically generated
427
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000428 unsigned Opcode = MI->getOpcode();
429 const TargetInstrInfo &TII = *TM.getInstrInfo();
430 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000431 unsigned i;
Misha Brukmanc6cc10f2004-06-25 19:24:52 +0000432
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000433 unsigned ArgCount = MI->getNumOperands();
434 unsigned ArgType[] = {
Misha Brukman5b570812004-08-10 22:47:03 +0000435 (Desc.TSFlags >> PPCII::Arg0TypeShift) & PPCII::ArgTypeMask,
436 (Desc.TSFlags >> PPCII::Arg1TypeShift) & PPCII::ArgTypeMask,
437 (Desc.TSFlags >> PPCII::Arg2TypeShift) & PPCII::ArgTypeMask,
438 (Desc.TSFlags >> PPCII::Arg3TypeShift) & PPCII::ArgTypeMask,
439 (Desc.TSFlags >> PPCII::Arg4TypeShift) & PPCII::ArgTypeMask
Misha Brukman22e12072004-06-25 15:11:34 +0000440 };
Misha Brukman5b570812004-08-10 22:47:03 +0000441 assert(((Desc.TSFlags & PPCII::VMX) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000442 "Instruction requires VMX support");
Misha Brukman5b570812004-08-10 22:47:03 +0000443 assert(((Desc.TSFlags & PPCII::PPC64) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000444 "Instruction requires 64 bit support");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000445
Misha Brukman61114612004-07-20 00:42:19 +0000446 // CALLpcrel and CALLindirect are handled specially here to print only the
447 // appropriate number of args that the assembler expects. This is because
448 // may have many arguments appended to record the uses of registers that are
449 // holding arguments to the called function.
Misha Brukman5b570812004-08-10 22:47:03 +0000450 if (Opcode == PPC::COND_BRANCH) {
Misha Brukmanab967902004-07-27 18:40:39 +0000451 std::cerr << "Error: untranslated conditional branch psuedo instruction!\n";
452 abort();
Misha Brukman5b570812004-08-10 22:47:03 +0000453 } else if (Opcode == PPC::IMPLICIT_DEF) {
Misha Brukman29188c62004-07-16 19:01:13 +0000454 O << "; IMPLICIT DEF ";
455 printOp(MI->getOperand(0));
456 O << "\n";
457 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000458 } else if (Opcode == PPC::CALLpcrel) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000459 O << TII.getName(Opcode) << " ";
Misha Brukman61114612004-07-20 00:42:19 +0000460 printOp(MI->getOperand(0));
461 O << "\n";
462 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000463 } else if (Opcode == PPC::CALLindirect) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000464 O << TII.getName(Opcode) << " ";
465 printImmOp(MI->getOperand(0), ArgType[0]);
Misha Brukman61114612004-07-20 00:42:19 +0000466 O << ", ";
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000467 printImmOp(MI->getOperand(1), ArgType[0]);
Misha Brukman61114612004-07-20 00:42:19 +0000468 O << "\n";
469 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000470 } else if (Opcode == PPC::MovePCtoLR) {
Misha Brukman61114612004-07-20 00:42:19 +0000471 // FIXME: should probably be converted to cout.width and cout.fill
Misha Brukmancf8d2442004-07-26 16:28:33 +0000472 O << "bl \"L0000" << LabelNumber << "$pb\"\n";
473 O << "\"L0000" << LabelNumber << "$pb\":\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000474 O << "\tmflr ";
475 printOp(MI->getOperand(0));
Misha Brukman218bec72004-06-29 17:13:26 +0000476 O << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000477 return;
478 }
479
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000480 O << TII.getName(Opcode) << " ";
Misha Brukman5b570812004-08-10 22:47:03 +0000481 if (Opcode == PPC::LOADLoDirect || Opcode == PPC::LOADLoIndirect) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000482 printOp(MI->getOperand(0));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000483 O << ", lo16(";
Nate Begemanb73a7112004-08-13 09:32:01 +0000484 printOp(MI->getOperand(2), true /* LoadAddrOp */);
Misha Brukmancf8d2442004-07-26 16:28:33 +0000485 O << "-\"L0000" << LabelNumber << "$pb\")";
Misha Brukman218bec72004-06-29 17:13:26 +0000486 O << "(";
Misha Brukman5b570812004-08-10 22:47:03 +0000487 if (MI->getOperand(1).getReg() == PPC::R0)
Misha Brukman218bec72004-06-29 17:13:26 +0000488 O << "0";
489 else
490 printOp(MI->getOperand(1));
491 O << ")\n";
Misha Brukman5b570812004-08-10 22:47:03 +0000492 } else if (Opcode == PPC::LOADHiAddr) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000493 printOp(MI->getOperand(0));
494 O << ", ";
Misha Brukman5b570812004-08-10 22:47:03 +0000495 if (MI->getOperand(1).getReg() == PPC::R0)
Misha Brukman218bec72004-06-29 17:13:26 +0000496 O << "0";
497 else
498 printOp(MI->getOperand(1));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000499 O << ", ha16(" ;
Nate Begemanb73a7112004-08-13 09:32:01 +0000500 printOp(MI->getOperand(2), true /* LoadAddrOp */);
Misha Brukmancf8d2442004-07-26 16:28:33 +0000501 O << "-\"L0000" << LabelNumber << "$pb\")\n";
Misha Brukman5b570812004-08-10 22:47:03 +0000502 } else if (ArgCount == 3 && ArgType[1] == PPCII::Disimm16) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000503 printOp(MI->getOperand(0));
504 O << ", ";
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000505 printImmOp(MI->getOperand(1), ArgType[1]);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000506 O << "(";
Misha Brukmanb9e8f972004-06-30 21:54:12 +0000507 if (MI->getOperand(2).hasAllocatedReg() &&
Misha Brukman5b570812004-08-10 22:47:03 +0000508 MI->getOperand(2).getReg() == PPC::R0)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000509 O << "0";
510 else
511 printOp(MI->getOperand(2));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000512 O << ")\n";
513 } else {
Misha Brukman7f484a52004-06-24 23:51:00 +0000514 for (i = 0; i < ArgCount; ++i) {
Misha Brukmanab967902004-07-27 18:40:39 +0000515 // addi and friends
Misha Brukman5b570812004-08-10 22:47:03 +0000516 if (i == 1 && ArgCount == 3 && ArgType[2] == PPCII::Simm16 &&
Misha Brukman4363bdb2004-07-01 21:09:12 +0000517 MI->getOperand(1).hasAllocatedReg() &&
Misha Brukman5b570812004-08-10 22:47:03 +0000518 MI->getOperand(1).getReg() == PPC::R0) {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000519 O << "0";
Misha Brukmanab967902004-07-27 18:40:39 +0000520 // for long branch support, bc $+8
521 } else if (i == 1 && ArgCount == 2 && MI->getOperand(1).isImmediate() &&
522 TII.isBranch(MI->getOpcode())) {
523 O << "$+8";
524 assert(8 == MI->getOperand(i).getImmedValue()
525 && "branch off PC not to pc+8?");
526 //printOp(MI->getOperand(i));
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000527 } else if (MI->getOperand(i).isImmediate()) {
528 printImmOp(MI->getOperand(i), ArgType[i]);
Misha Brukman218bec72004-06-29 17:13:26 +0000529 } else {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000530 printOp(MI->getOperand(i));
531 }
Misha Brukman7f484a52004-06-24 23:51:00 +0000532 if (ArgCount - 1 == i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000533 O << "\n";
534 else
535 O << ", ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000536 }
537 }
Nate Begemane59bf592004-08-14 22:09:10 +0000538 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000539}
540
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000541// SwitchSection - Switch to the specified section of the executable if we are
542// not already in it!
543//
544static void SwitchSection(std::ostream &OS, std::string &CurSection,
545 const char *NewSection) {
546 if (CurSection != NewSection) {
547 CurSection = NewSection;
548 if (!CurSection.empty())
549 OS << "\t" << NewSection << "\n";
550 }
551}
552
Nate Begemane59bf592004-08-14 22:09:10 +0000553bool PowerPCAsmPrinter::doFinalization(Module &M) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000554 const TargetData &TD = TM.getTargetData();
555 std::string CurSection;
556
557 // Print out module-level global variables here.
558 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
559 if (I->hasInitializer()) { // External global require no code
560 O << "\n\n";
561 std::string name = Mang->getValueName(I);
562 Constant *C = I->getInitializer();
563 unsigned Size = TD.getTypeSize(C->getType());
564 unsigned Align = TD.getTypeAlignment(C->getType());
565
Misha Brukman97a296f2004-07-21 20:11:11 +0000566 if (C->isNullValue() && /* FIXME: Verify correct */
567 (I->hasInternalLinkage() || I->hasWeakLinkage())) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000568 SwitchSection(O, CurSection, ".data");
569 if (I->hasInternalLinkage())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000570 O << ".lcomm " << name << "," << TD.getTypeSize(C->getType())
Misha Brukman218bec72004-06-29 17:13:26 +0000571 << "," << (unsigned)TD.getTypeAlignment(C->getType());
572 else
Misha Brukmane2eceb52004-07-23 16:08:20 +0000573 O << ".comm " << name << "," << TD.getTypeSize(C->getType());
Misha Brukman218bec72004-06-29 17:13:26 +0000574 O << "\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000575 WriteAsOperand(O, I, true, true, &M);
576 O << "\n";
577 } else {
578 switch (I->getLinkage()) {
579 case GlobalValue::LinkOnceLinkage:
Misha Brukman97a296f2004-07-21 20:11:11 +0000580 O << ".section __TEXT,__textcoal_nt,coalesced,no_toc\n"
581 << ".weak_definition " << name << '\n'
582 << ".private_extern " << name << '\n'
583 << ".section __DATA,__datacoal_nt,coalesced,no_toc\n";
584 LinkOnceStubs.insert(name);
585 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000586 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
587 // Nonnull linkonce -> weak
588 O << "\t.weak " << name << "\n";
589 SwitchSection(O, CurSection, "");
590 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
591 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000592 case GlobalValue::AppendingLinkage:
593 // FIXME: appending linkage variables should go into a section of
594 // their name or something. For now, just emit them as external.
595 case GlobalValue::ExternalLinkage:
596 // If external or appending, declare as a global symbol
597 O << "\t.globl " << name << "\n";
598 // FALL THROUGH
599 case GlobalValue::InternalLinkage:
Misha Brukman61297ee2004-06-29 23:40:57 +0000600 SwitchSection(O, CurSection, ".data");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000601 break;
602 }
603
604 O << "\t.align " << Align << "\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000605 O << name << ":\t\t\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000606 WriteAsOperand(O, I, true, true, &M);
607 O << " = ";
608 WriteAsOperand(O, C, false, false, &M);
609 O << "\n";
610 emitGlobalConstant(C);
611 }
612 }
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000613
614 // Output stubs for dynamically-linked functions
615 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
616 i != e; ++i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000617 {
Misha Brukmane2eceb52004-07-23 16:08:20 +0000618 O << ".data\n";
619 O << ".section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n";
620 O << "\t.align 2\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000621 O << "L" << *i << "$stub:\n";
622 O << "\t.indirect_symbol " << *i << "\n";
623 O << "\tmflr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000624 O << "\tbcl 20,31,L0$" << *i << "\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000625 O << "L0$" << *i << ":\n";
626 O << "\tmflr r11\n";
627 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
628 O << "\tmtlr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000629 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000630 O << "\tmtctr r12\n";
631 O << "\tbctr\n";
632 O << ".data\n";
633 O << ".lazy_symbol_pointer\n";
634 O << "L" << *i << "$lazy_ptr:\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000635 O << "\t.indirect_symbol " << *i << "\n";
636 O << "\t.long dyld_stub_binding_helper\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000637 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000638
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000639 O << "\n";
640
641 // Output stubs for external global variables
642 if (GVStubs.begin() != GVStubs.end())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000643 O << ".data\n.non_lazy_symbol_pointer\n";
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000644 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
645 i != e; ++i) {
646 O << "L" << *i << "$non_lazy_ptr:\n";
647 O << "\t.indirect_symbol " << *i << "\n";
648 O << "\t.long\t0\n";
649 }
650
Nate Begemane59bf592004-08-14 22:09:10 +0000651 // Output stubs for link-once variables
652 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
653 O << ".data\n.align 2\n";
654 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
655 e = LinkOnceStubs.end(); i != e; ++i) {
656 O << "L" << *i << "$non_lazy_ptr:\n"
657 << "\t.long\t" << *i << '\n';
658 }
659
Chris Lattnera3840792004-08-16 23:25:21 +0000660 AsmPrinter::doFinalization(M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000661 return false; // success
662}