blob: a857f3afebbf060dc1b4045e0b2292606dcfd3a3 [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"
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/Target/TargetMachine.h"
32#include "llvm/Support/Mangler.h"
Misha Brukman05794492004-06-24 17:31:42 +000033#include "Support/CommandLine.h"
34#include "Support/Debug.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000035#include "Support/Statistic.h"
36#include "Support/StringExtras.h"
Misha Brukman05794492004-06-24 17:31:42 +000037#include <set>
Misha Brukman5dfe3a92004-06-21 16:55:25 +000038
39namespace llvm {
40
41namespace {
42 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
43
Nate Begemane59bf592004-08-14 22:09:10 +000044 struct PowerPCAsmPrinter : public MachineFunctionPass {
Misha Brukman5dfe3a92004-06-21 16:55:25 +000045 /// Output stream on which we're printing assembly code.
46 ///
47 std::ostream &O;
48
49 /// Target machine description which we query for reg. names, data
50 /// layout, etc.
51 ///
Nate Begemane59bf592004-08-14 22:09:10 +000052 PowerPCTargetMachine &TM;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000053
54 /// Name-mangler for global names.
55 ///
56 Mangler *Mang;
Misha Brukman97a296f2004-07-21 20:11:11 +000057 std::set<std::string> FnStubs, GVStubs, LinkOnceStubs;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000058 std::set<std::string> Strings;
59
Nate Begemane59bf592004-08-14 22:09:10 +000060 PowerPCAsmPrinter(std::ostream &o, TargetMachine &tm) : O(o),
61 TM(reinterpret_cast<PowerPCTargetMachine&>(tm)), LabelNumber(0) {}
Misha Brukman5dfe3a92004-06-21 16:55:25 +000062
Misha Brukman5dfe3a92004-06-21 16:55:25 +000063 /// Cache of mangled name for current function. This is
64 /// recalculated at the beginning of each call to
65 /// runOnMachineFunction().
66 ///
67 std::string CurrentFnName;
68
Misha Brukmancf8d2442004-07-26 16:28:33 +000069 /// Unique incrementer for label values for referencing Global values.
Misha Brukman218bec72004-06-29 17:13:26 +000070 ///
Misha Brukmancf8d2442004-07-26 16:28:33 +000071 unsigned LabelNumber;
72
Misha Brukman5dfe3a92004-06-21 16:55:25 +000073 virtual const char *getPassName() const {
Nate Begemane59bf592004-08-14 22:09:10 +000074 return "PowerPC Assembly Printer";
Misha Brukman5dfe3a92004-06-21 16:55:25 +000075 }
76
Nate Begemane59bf592004-08-14 22:09:10 +000077 /// printInstruction - This method is automatically generated by tablegen
78 /// from the instruction set description. This method returns true if the
79 /// machine instruction was sufficiently described to print it, otherwise it
80 /// returns false.
81 bool printInstruction(const MachineInstr *MI);
82
Misha Brukman5dfe3a92004-06-21 16:55:25 +000083 void printMachineInstruction(const MachineInstr *MI);
Nate Begemanb73a7112004-08-13 09:32:01 +000084 void printOp(const MachineOperand &MO, bool LoadAddrOp = false);
Misha Brukmanaf313fb2004-07-28 00:00:48 +000085 void printImmOp(const MachineOperand &MO, unsigned ArgType);
Chris Lattner7bb424f2004-08-14 23:27:29 +000086
87 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
88 const MachineOperand &MO = MI->getOperand(OpNo);
89 if (MO.getType() == MachineOperand::MO_MachineRegister) {
90 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
91 O << LowercaseString(TM.getRegisterInfo()->get(MO.getReg()).Name);
92 } else {
93 printOp(MO);
94 }
95 }
96
Misha Brukman5dfe3a92004-06-21 16:55:25 +000097 void printConstantPool(MachineConstantPool *MCP);
98 bool runOnMachineFunction(MachineFunction &F);
99 bool doInitialization(Module &M);
100 bool doFinalization(Module &M);
101 void emitGlobalConstant(const Constant* CV);
102 void emitConstantValueOnly(const Constant *CV);
103 };
104} // end of anonymous namespace
105
Misha Brukman3d9a6c22004-08-11 00:09:42 +0000106/// createPPC32AsmPrinterPass - Returns a pass that prints the PPC
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000107/// assembly code for a MachineFunction to the given output stream,
108/// using the given target machine description. This should work
Misha Brukman7103fba2004-08-09 22:27:45 +0000109/// regardless of whether the function is in SSA form or not.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000110///
Nate Begemane59bf592004-08-14 22:09:10 +0000111FunctionPass *createPPCAsmPrinter(std::ostream &o,TargetMachine &tm) {
112 return new PowerPCAsmPrinter(o, tm);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000113}
114
Nate Begemane59bf592004-08-14 22:09:10 +0000115// Include the auto-generated portion of the assembly writer
116#include "PowerPCGenAsmWriter.inc"
117
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000118/// isStringCompatible - Can we treat the specified array as a string?
119/// Only if it is an array of ubytes or non-negative sbytes.
120///
121static bool isStringCompatible(const ConstantArray *CVA) {
122 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
123 if (ETy == Type::UByteTy) return true;
124 if (ETy != Type::SByteTy) return false;
125
126 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
127 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
128 return false;
129
130 return true;
131}
132
133/// toOctal - Convert the low order bits of X into an octal digit.
134///
135static inline char toOctal(int X) {
136 return (X&7)+'0';
137}
138
139/// getAsCString - Return the specified array as a C compatible
140/// string, only if the predicate isStringCompatible is true.
141///
142static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
143 assert(isStringCompatible(CVA) && "Array is not string compatible!");
144
145 O << "\"";
146 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
147 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
148
149 if (C == '"') {
150 O << "\\\"";
151 } else if (C == '\\') {
152 O << "\\\\";
153 } else if (isprint(C)) {
154 O << C;
155 } else {
Misha Brukmane2eceb52004-07-23 16:08:20 +0000156 switch (C) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000157 case '\b': O << "\\b"; break;
158 case '\f': O << "\\f"; break;
159 case '\n': O << "\\n"; break;
160 case '\r': O << "\\r"; break;
161 case '\t': O << "\\t"; break;
162 default:
163 O << '\\';
164 O << toOctal(C >> 6);
165 O << toOctal(C >> 3);
166 O << toOctal(C >> 0);
167 break;
168 }
169 }
170 }
171 O << "\"";
172}
173
174// Print out the specified constant, without a storage class. Only the
175// constants valid in constant expressions can occur here.
Nate Begemane59bf592004-08-14 22:09:10 +0000176void PowerPCAsmPrinter::emitConstantValueOnly(const Constant *CV) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000177 if (CV->isNullValue())
178 O << "0";
179 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
180 assert(CB == ConstantBool::True);
181 O << "1";
182 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
183 O << CI->getValue();
184 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
185 O << CI->getValue();
Chris Lattner67910e12004-07-18 07:29:35 +0000186 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000187 // This is a constant address for a global variable or function. Use the
188 // name of the variable or function as the address value.
Chris Lattner67910e12004-07-18 07:29:35 +0000189 O << Mang->getValueName(GV);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000190 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
191 const TargetData &TD = TM.getTargetData();
Misha Brukmane2eceb52004-07-23 16:08:20 +0000192 switch (CE->getOpcode()) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000193 case Instruction::GetElementPtr: {
194 // generate a symbolic expression for the byte address
195 const Constant *ptrVal = CE->getOperand(0);
196 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
197 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
198 O << "(";
199 emitConstantValueOnly(ptrVal);
200 O << ") + " << Offset;
201 } else {
202 emitConstantValueOnly(ptrVal);
203 }
204 break;
205 }
206 case Instruction::Cast: {
207 // Support only non-converting or widening casts for now, that is, ones
208 // that do not involve a change in value. This assertion is really gross,
209 // and may not even be a complete check.
210 Constant *Op = CE->getOperand(0);
211 const Type *OpTy = Op->getType(), *Ty = CE->getType();
212
213 // Remember, kids, pointers on x86 can be losslessly converted back and
214 // forth into 32-bit or wider integers, regardless of signedness. :-P
215 assert(((isa<PointerType>(OpTy)
216 && (Ty == Type::LongTy || Ty == Type::ULongTy
217 || Ty == Type::IntTy || Ty == Type::UIntTy))
218 || (isa<PointerType>(Ty)
219 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
220 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
221 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
222 && OpTy->isLosslesslyConvertibleTo(Ty))))
223 && "FIXME: Don't yet support this kind of constant cast expr");
224 O << "(";
225 emitConstantValueOnly(Op);
226 O << ")";
227 break;
228 }
229 case Instruction::Add:
230 O << "(";
231 emitConstantValueOnly(CE->getOperand(0));
232 O << ") + (";
233 emitConstantValueOnly(CE->getOperand(1));
234 O << ")";
235 break;
236 default:
237 assert(0 && "Unsupported operator!");
238 }
239 } else {
240 assert(0 && "Unknown constant value!");
241 }
242}
243
244// Print a constant value or values, with the appropriate storage class as a
245// prefix.
Nate Begemane59bf592004-08-14 22:09:10 +0000246void PowerPCAsmPrinter::emitGlobalConstant(const Constant *CV) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000247 const TargetData &TD = TM.getTargetData();
248
Misha Brukmane48178e2004-07-20 15:45:27 +0000249 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000250 if (isStringCompatible(CVA)) {
Misha Brukman218bec72004-06-29 17:13:26 +0000251 O << "\t.ascii ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000252 printAsCString(O, CVA);
253 O << "\n";
254 } else { // Not a string. Print the values in successive locations
Chris Lattner6173cd92004-08-04 17:29:14 +0000255 for (unsigned i=0, e = CVA->getNumOperands(); i != e; i++)
256 emitGlobalConstant(CVA->getOperand(i));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000257 }
258 return;
259 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
260 // Print the fields in successive locations. Pad to align if needed!
261 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000262 unsigned sizeSoFar = 0;
Chris Lattner6173cd92004-08-04 17:29:14 +0000263 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; i++) {
264 const Constant* field = CVS->getOperand(i);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000265
266 // Check if padding is needed and insert one or more 0s.
267 unsigned fieldSize = TD.getTypeSize(field->getType());
Chris Lattner6173cd92004-08-04 17:29:14 +0000268 unsigned padSize = ((i == e-1? cvsLayout->StructSize
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000269 : cvsLayout->MemberOffsets[i+1])
270 - cvsLayout->MemberOffsets[i]) - fieldSize;
271 sizeSoFar += fieldSize + padSize;
272
273 // Now print the actual field value
274 emitGlobalConstant(field);
275
276 // Insert the field padding unless it's zero bytes...
277 if (padSize)
278 O << "\t.space\t " << padSize << "\n";
279 }
280 assert(sizeSoFar == cvsLayout->StructSize &&
281 "Layout of constant struct may be incorrect!");
282 return;
283 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
284 // FP Constants are printed as integer constants to avoid losing
285 // precision...
286 double Val = CFP->getValue();
Nate Begemane59bf592004-08-14 22:09:10 +0000287 union DU { // Abide by C TBAA rules
288 double FVal;
289 uint64_t UVal;
290 struct {
291 uint32_t MSWord;
292 uint32_t LSWord;
293 } T;
294 } U;
295 U.FVal = Val;
296
297 O << ".long\t" << U.T.MSWord << "\t; double most significant word "
298 << Val << "\n";
299 O << ".long\t" << U.T.LSWord << "\t; double least significant word "
300 << Val << "\n";
301 return;
Misha Brukmanf63bc192004-07-28 19:12:24 +0000302 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
Misha Brukman2bf183c2004-06-25 15:42:10 +0000303 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
304 union DU { // Abide by C TBAA rules
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000305 int64_t UVal;
306 struct {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000307 uint32_t MSWord;
308 uint32_t LSWord;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000309 } T;
310 } U;
311 U.UVal = CI->getRawValue();
312
Misha Brukman218bec72004-06-29 17:13:26 +0000313 O << ".long\t" << U.T.MSWord << "\t; Double-word most significant word "
Misha Brukman46fd00a2004-06-24 23:04:11 +0000314 << U.UVal << "\n";
Misha Brukman29188c62004-07-16 19:01:13 +0000315 O << ".long\t" << U.T.LSWord << "\t; Double-word least significant word "
Misha Brukman46fd00a2004-06-24 23:04:11 +0000316 << U.UVal << "\n";
317 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000318 }
319 }
320
321 const Type *type = CV->getType();
322 O << "\t";
Misha Brukmand71bd562004-06-21 17:19:08 +0000323 switch (type->getTypeID()) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000324 case Type::UByteTyID: case Type::SByteTyID:
325 O << ".byte";
326 break;
327 case Type::UShortTyID: case Type::ShortTyID:
328 O << ".short";
329 break;
330 case Type::BoolTyID:
331 case Type::PointerTyID:
332 case Type::UIntTyID: case Type::IntTyID:
333 O << ".long";
334 break;
335 case Type::ULongTyID: case Type::LongTyID:
Misha Brukman46fd00a2004-06-24 23:04:11 +0000336 assert (0 && "Should have already output double-word constant.");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000337 case Type::FloatTyID: case Type::DoubleTyID:
338 assert (0 && "Should have already output floating point constant.");
339 default:
Misha Brukman97a296f2004-07-21 20:11:11 +0000340 if (CV == Constant::getNullValue(type)) { // Zero initializer?
341 O << ".space\t" << TD.getTypeSize(type) << "\n";
342 return;
343 }
344 std::cerr << "Can't handle printing: " << *CV;
345 abort();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000346 break;
347 }
348 O << "\t";
349 emitConstantValueOnly(CV);
350 O << "\n";
351}
352
353/// printConstantPool - Print to the current output stream assembly
354/// representations of the constants in the constant pool MCP. This is
355/// used to print out constants which have been "spilled to memory" by
356/// the code generator.
357///
Nate Begemane59bf592004-08-14 22:09:10 +0000358void PowerPCAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000359 const std::vector<Constant*> &CP = MCP->getConstants();
360 const TargetData &TD = TM.getTargetData();
361
362 if (CP.empty()) return;
363
364 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
365 O << "\t.const\n";
366 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
367 << "\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000368 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t;"
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000369 << *CP[i] << "\n";
370 emitGlobalConstant(CP[i]);
371 }
372}
373
374/// runOnMachineFunction - This uses the printMachineInstruction()
375/// method to print assembly for each instruction.
376///
Nate Begemane59bf592004-08-14 22:09:10 +0000377bool PowerPCAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000378 O << "\n\n";
379 // What's my mangled name?
380 CurrentFnName = Mang->getValueName(MF.getFunction());
381
382 // Print out constants referenced by the function
383 printConstantPool(MF.getConstantPool());
384
385 // Print out labels for the function.
386 O << "\t.text\n";
387 O << "\t.globl\t" << CurrentFnName << "\n";
Misha Brukman61297ee2004-06-29 23:40:57 +0000388 O << "\t.align 2\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000389 O << CurrentFnName << ":\n";
390
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000391 // Print out code for the function.
392 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
393 I != E; ++I) {
394 // Print a label for the basic block.
Misha Brukman218bec72004-06-29 17:13:26 +0000395 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t; "
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000396 << I->getBasicBlock()->getName() << "\n";
397 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukman46fd00a2004-06-24 23:04:11 +0000398 II != E; ++II) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000399 // Print the assembly for the instruction.
400 O << "\t";
401 printMachineInstruction(II);
402 }
403 }
Misha Brukmancf8d2442004-07-26 16:28:33 +0000404 ++LabelNumber;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000405
406 // We didn't modify anything.
407 return false;
408}
409
Nate Begemane59bf592004-08-14 22:09:10 +0000410void PowerPCAsmPrinter::printOp(const MachineOperand &MO,
Nate Begemanb73a7112004-08-13 09:32:01 +0000411 bool LoadAddrOp /* = false */) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000412 const MRegisterInfo &RI = *TM.getRegisterInfo();
413 int new_symbol;
414
415 switch (MO.getType()) {
416 case MachineOperand::MO_VirtualRegister:
417 if (Value *V = MO.getVRegValueOrNull()) {
418 O << "<" << V->getName() << ">";
419 return;
420 }
421 // FALLTHROUGH
422 case MachineOperand::MO_MachineRegister:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000423 case MachineOperand::MO_CCRegister:
Misha Brukman7f484a52004-06-24 23:51:00 +0000424 O << LowercaseString(RI.get(MO.getReg()).Name);
425 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000426
427 case MachineOperand::MO_SignExtendedImmed:
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000428 case MachineOperand::MO_UnextendedImmed:
429 std::cerr << "printOp() does not handle immediate values\n";
430 abort();
Misha Brukman97a296f2004-07-21 20:11:11 +0000431 return;
432
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000433 case MachineOperand::MO_PCRelativeDisp:
434 std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
435 abort();
436 return;
437
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000438 case MachineOperand::MO_MachineBasicBlock: {
439 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
440 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
Misha Brukman218bec72004-06-29 17:13:26 +0000441 << "_" << MBBOp->getNumber() << "\t; "
Misha Brukman2bf183c2004-06-25 15:42:10 +0000442 << MBBOp->getBasicBlock()->getName();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000443 return;
444 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000445
446 case MachineOperand::MO_ConstantPoolIndex:
447 O << ".CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000448 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000449
450 case MachineOperand::MO_ExternalSymbol:
451 O << MO.getSymbolName();
452 return;
453
Nate Begemanb73a7112004-08-13 09:32:01 +0000454 case MachineOperand::MO_GlobalAddress: {
455 GlobalValue *GV = MO.getGlobal();
456 std::string Name = Mang->getValueName(GV);
Misha Brukmane2eceb52004-07-23 16:08:20 +0000457
Nate Begemanb73a7112004-08-13 09:32:01 +0000458 // Dynamically-resolved functions need a stub for the function. Be
459 // wary however not to output $stub for external functions whose addresses
460 // are taken. Those should be emitted as $non_lazy_ptr below.
461 Function *F = dyn_cast<Function>(GV);
462 if (F && F->isExternal() && !LoadAddrOp &&
463 TM.CalledFunctions.find(F) != TM.CalledFunctions.end()) {
464 FnStubs.insert(Name);
465 O << "L" << Name << "$stub";
466 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000467 }
Nate Begemane59bf592004-08-14 22:09:10 +0000468
Nate Begemanb73a7112004-08-13 09:32:01 +0000469 // External global variables need a non-lazily-resolved stub
Nate Begemane59bf592004-08-14 22:09:10 +0000470 if (GV->isExternal() && TM.AddressTaken.find(GV) != TM.AddressTaken.end()) {
Nate Begemanb73a7112004-08-13 09:32:01 +0000471 GVStubs.insert(Name);
472 O << "L" << Name << "$non_lazy_ptr";
473 return;
474 }
Nate Begemane59bf592004-08-14 22:09:10 +0000475
476 if (F && LoadAddrOp && TM.AddressTaken.find(GV) != TM.AddressTaken.end()) {
477 LinkOnceStubs.insert(Name);
478 O << "L" << Name << "$non_lazy_ptr";
479 return;
480 }
Nate Begemanb73a7112004-08-13 09:32:01 +0000481
482 O << Mang->getValueName(GV);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000483 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000484 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000485
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000486 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000487 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000488 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000489 }
490}
491
Nate Begemane59bf592004-08-14 22:09:10 +0000492void PowerPCAsmPrinter::printImmOp(const MachineOperand &MO, unsigned ArgType) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000493 int Imm = MO.getImmedValue();
Misha Brukman5b570812004-08-10 22:47:03 +0000494 if (ArgType == PPCII::Simm16 || ArgType == PPCII::Disimm16) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000495 O << (short)Imm;
Misha Brukman5b570812004-08-10 22:47:03 +0000496 } else if (ArgType == PPCII::Zimm16) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000497 O << (unsigned short)Imm;
498 } else {
499 O << Imm;
500 }
501}
502
Nate Begemane59bf592004-08-14 22:09:10 +0000503/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
504/// the current output stream.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000505///
Nate Begemane59bf592004-08-14 22:09:10 +0000506void PowerPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
507 ++EmittedInsts;
508 if (printInstruction(MI))
509 return; // Printer was automatically generated
510
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000511 unsigned Opcode = MI->getOpcode();
512 const TargetInstrInfo &TII = *TM.getInstrInfo();
513 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000514 unsigned i;
Misha Brukmanc6cc10f2004-06-25 19:24:52 +0000515
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000516 unsigned ArgCount = MI->getNumOperands();
517 unsigned ArgType[] = {
Misha Brukman5b570812004-08-10 22:47:03 +0000518 (Desc.TSFlags >> PPCII::Arg0TypeShift) & PPCII::ArgTypeMask,
519 (Desc.TSFlags >> PPCII::Arg1TypeShift) & PPCII::ArgTypeMask,
520 (Desc.TSFlags >> PPCII::Arg2TypeShift) & PPCII::ArgTypeMask,
521 (Desc.TSFlags >> PPCII::Arg3TypeShift) & PPCII::ArgTypeMask,
522 (Desc.TSFlags >> PPCII::Arg4TypeShift) & PPCII::ArgTypeMask
Misha Brukman22e12072004-06-25 15:11:34 +0000523 };
Misha Brukman5b570812004-08-10 22:47:03 +0000524 assert(((Desc.TSFlags & PPCII::VMX) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000525 "Instruction requires VMX support");
Misha Brukman5b570812004-08-10 22:47:03 +0000526 assert(((Desc.TSFlags & PPCII::PPC64) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000527 "Instruction requires 64 bit support");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000528
Misha Brukman61114612004-07-20 00:42:19 +0000529 // CALLpcrel and CALLindirect are handled specially here to print only the
530 // appropriate number of args that the assembler expects. This is because
531 // may have many arguments appended to record the uses of registers that are
532 // holding arguments to the called function.
Misha Brukman5b570812004-08-10 22:47:03 +0000533 if (Opcode == PPC::COND_BRANCH) {
Misha Brukmanab967902004-07-27 18:40:39 +0000534 std::cerr << "Error: untranslated conditional branch psuedo instruction!\n";
535 abort();
Misha Brukman5b570812004-08-10 22:47:03 +0000536 } else if (Opcode == PPC::IMPLICIT_DEF) {
Misha Brukman29188c62004-07-16 19:01:13 +0000537 O << "; IMPLICIT DEF ";
538 printOp(MI->getOperand(0));
539 O << "\n";
540 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000541 } else if (Opcode == PPC::CALLpcrel) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000542 O << TII.getName(Opcode) << " ";
Misha Brukman61114612004-07-20 00:42:19 +0000543 printOp(MI->getOperand(0));
544 O << "\n";
545 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000546 } else if (Opcode == PPC::CALLindirect) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000547 O << TII.getName(Opcode) << " ";
548 printImmOp(MI->getOperand(0), ArgType[0]);
Misha Brukman61114612004-07-20 00:42:19 +0000549 O << ", ";
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000550 printImmOp(MI->getOperand(1), ArgType[0]);
Misha Brukman61114612004-07-20 00:42:19 +0000551 O << "\n";
552 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000553 } else if (Opcode == PPC::MovePCtoLR) {
Misha Brukman61114612004-07-20 00:42:19 +0000554 // FIXME: should probably be converted to cout.width and cout.fill
Misha Brukmancf8d2442004-07-26 16:28:33 +0000555 O << "bl \"L0000" << LabelNumber << "$pb\"\n";
556 O << "\"L0000" << LabelNumber << "$pb\":\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000557 O << "\tmflr ";
558 printOp(MI->getOperand(0));
Misha Brukman218bec72004-06-29 17:13:26 +0000559 O << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000560 return;
561 }
562
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000563 O << TII.getName(Opcode) << " ";
Misha Brukman5b570812004-08-10 22:47:03 +0000564 if (Opcode == PPC::LOADLoDirect || Opcode == PPC::LOADLoIndirect) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000565 printOp(MI->getOperand(0));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000566 O << ", lo16(";
Nate Begemanb73a7112004-08-13 09:32:01 +0000567 printOp(MI->getOperand(2), true /* LoadAddrOp */);
Misha Brukmancf8d2442004-07-26 16:28:33 +0000568 O << "-\"L0000" << LabelNumber << "$pb\")";
Misha Brukman218bec72004-06-29 17:13:26 +0000569 O << "(";
Misha Brukman5b570812004-08-10 22:47:03 +0000570 if (MI->getOperand(1).getReg() == PPC::R0)
Misha Brukman218bec72004-06-29 17:13:26 +0000571 O << "0";
572 else
573 printOp(MI->getOperand(1));
574 O << ")\n";
Misha Brukman5b570812004-08-10 22:47:03 +0000575 } else if (Opcode == PPC::LOADHiAddr) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000576 printOp(MI->getOperand(0));
577 O << ", ";
Misha Brukman5b570812004-08-10 22:47:03 +0000578 if (MI->getOperand(1).getReg() == PPC::R0)
Misha Brukman218bec72004-06-29 17:13:26 +0000579 O << "0";
580 else
581 printOp(MI->getOperand(1));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000582 O << ", ha16(" ;
Nate Begemanb73a7112004-08-13 09:32:01 +0000583 printOp(MI->getOperand(2), true /* LoadAddrOp */);
Misha Brukmancf8d2442004-07-26 16:28:33 +0000584 O << "-\"L0000" << LabelNumber << "$pb\")\n";
Misha Brukman5b570812004-08-10 22:47:03 +0000585 } else if (ArgCount == 3 && ArgType[1] == PPCII::Disimm16) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000586 printOp(MI->getOperand(0));
587 O << ", ";
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000588 printImmOp(MI->getOperand(1), ArgType[1]);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000589 O << "(";
Misha Brukmanb9e8f972004-06-30 21:54:12 +0000590 if (MI->getOperand(2).hasAllocatedReg() &&
Misha Brukman5b570812004-08-10 22:47:03 +0000591 MI->getOperand(2).getReg() == PPC::R0)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000592 O << "0";
593 else
594 printOp(MI->getOperand(2));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000595 O << ")\n";
596 } else {
Misha Brukman7f484a52004-06-24 23:51:00 +0000597 for (i = 0; i < ArgCount; ++i) {
Misha Brukmanab967902004-07-27 18:40:39 +0000598 // addi and friends
Misha Brukman5b570812004-08-10 22:47:03 +0000599 if (i == 1 && ArgCount == 3 && ArgType[2] == PPCII::Simm16 &&
Misha Brukman4363bdb2004-07-01 21:09:12 +0000600 MI->getOperand(1).hasAllocatedReg() &&
Misha Brukman5b570812004-08-10 22:47:03 +0000601 MI->getOperand(1).getReg() == PPC::R0) {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000602 O << "0";
Misha Brukmanab967902004-07-27 18:40:39 +0000603 // for long branch support, bc $+8
604 } else if (i == 1 && ArgCount == 2 && MI->getOperand(1).isImmediate() &&
605 TII.isBranch(MI->getOpcode())) {
606 O << "$+8";
607 assert(8 == MI->getOperand(i).getImmedValue()
608 && "branch off PC not to pc+8?");
609 //printOp(MI->getOperand(i));
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000610 } else if (MI->getOperand(i).isImmediate()) {
611 printImmOp(MI->getOperand(i), ArgType[i]);
Misha Brukman218bec72004-06-29 17:13:26 +0000612 } else {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000613 printOp(MI->getOperand(i));
614 }
Misha Brukman7f484a52004-06-24 23:51:00 +0000615 if (ArgCount - 1 == i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000616 O << "\n";
617 else
618 O << ", ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000619 }
620 }
Nate Begemane59bf592004-08-14 22:09:10 +0000621 return;
622
623 // Call the autogenerated instruction printer routines.
624 bool Handled = printInstruction(MI);
625 if (!Handled) {
626 MI->dump();
627 assert(0 && "Do not know how to print this instruction!");
628 abort();
629 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000630}
631
Nate Begemane59bf592004-08-14 22:09:10 +0000632bool PowerPCAsmPrinter::doInitialization(Module &M) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000633 Mang = new Mangler(M, true);
634 return false; // success
635}
636
637// SwitchSection - Switch to the specified section of the executable if we are
638// not already in it!
639//
640static void SwitchSection(std::ostream &OS, std::string &CurSection,
641 const char *NewSection) {
642 if (CurSection != NewSection) {
643 CurSection = NewSection;
644 if (!CurSection.empty())
645 OS << "\t" << NewSection << "\n";
646 }
647}
648
Nate Begemane59bf592004-08-14 22:09:10 +0000649bool PowerPCAsmPrinter::doFinalization(Module &M) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000650 const TargetData &TD = TM.getTargetData();
651 std::string CurSection;
652
653 // Print out module-level global variables here.
654 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
655 if (I->hasInitializer()) { // External global require no code
656 O << "\n\n";
657 std::string name = Mang->getValueName(I);
658 Constant *C = I->getInitializer();
659 unsigned Size = TD.getTypeSize(C->getType());
660 unsigned Align = TD.getTypeAlignment(C->getType());
661
Misha Brukman97a296f2004-07-21 20:11:11 +0000662 if (C->isNullValue() && /* FIXME: Verify correct */
663 (I->hasInternalLinkage() || I->hasWeakLinkage())) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000664 SwitchSection(O, CurSection, ".data");
665 if (I->hasInternalLinkage())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000666 O << ".lcomm " << name << "," << TD.getTypeSize(C->getType())
Misha Brukman218bec72004-06-29 17:13:26 +0000667 << "," << (unsigned)TD.getTypeAlignment(C->getType());
668 else
Misha Brukmane2eceb52004-07-23 16:08:20 +0000669 O << ".comm " << name << "," << TD.getTypeSize(C->getType());
Misha Brukman218bec72004-06-29 17:13:26 +0000670 O << "\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000671 WriteAsOperand(O, I, true, true, &M);
672 O << "\n";
673 } else {
674 switch (I->getLinkage()) {
675 case GlobalValue::LinkOnceLinkage:
Misha Brukman97a296f2004-07-21 20:11:11 +0000676 O << ".section __TEXT,__textcoal_nt,coalesced,no_toc\n"
677 << ".weak_definition " << name << '\n'
678 << ".private_extern " << name << '\n'
679 << ".section __DATA,__datacoal_nt,coalesced,no_toc\n";
680 LinkOnceStubs.insert(name);
681 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000682 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
683 // Nonnull linkonce -> weak
684 O << "\t.weak " << name << "\n";
685 SwitchSection(O, CurSection, "");
686 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
687 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000688 case GlobalValue::AppendingLinkage:
689 // FIXME: appending linkage variables should go into a section of
690 // their name or something. For now, just emit them as external.
691 case GlobalValue::ExternalLinkage:
692 // If external or appending, declare as a global symbol
693 O << "\t.globl " << name << "\n";
694 // FALL THROUGH
695 case GlobalValue::InternalLinkage:
Misha Brukman61297ee2004-06-29 23:40:57 +0000696 SwitchSection(O, CurSection, ".data");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000697 break;
698 }
699
700 O << "\t.align " << Align << "\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000701 O << name << ":\t\t\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000702 WriteAsOperand(O, I, true, true, &M);
703 O << " = ";
704 WriteAsOperand(O, C, false, false, &M);
705 O << "\n";
706 emitGlobalConstant(C);
707 }
708 }
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000709
710 // Output stubs for dynamically-linked functions
711 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
712 i != e; ++i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000713 {
Misha Brukmane2eceb52004-07-23 16:08:20 +0000714 O << ".data\n";
715 O << ".section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n";
716 O << "\t.align 2\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000717 O << "L" << *i << "$stub:\n";
718 O << "\t.indirect_symbol " << *i << "\n";
719 O << "\tmflr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000720 O << "\tbcl 20,31,L0$" << *i << "\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000721 O << "L0$" << *i << ":\n";
722 O << "\tmflr r11\n";
723 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
724 O << "\tmtlr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000725 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000726 O << "\tmtctr r12\n";
727 O << "\tbctr\n";
728 O << ".data\n";
729 O << ".lazy_symbol_pointer\n";
730 O << "L" << *i << "$lazy_ptr:\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000731 O << "\t.indirect_symbol " << *i << "\n";
732 O << "\t.long dyld_stub_binding_helper\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000733 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000734
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000735 O << "\n";
736
737 // Output stubs for external global variables
738 if (GVStubs.begin() != GVStubs.end())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000739 O << ".data\n.non_lazy_symbol_pointer\n";
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000740 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
741 i != e; ++i) {
742 O << "L" << *i << "$non_lazy_ptr:\n";
743 O << "\t.indirect_symbol " << *i << "\n";
744 O << "\t.long\t0\n";
745 }
746
Nate Begemane59bf592004-08-14 22:09:10 +0000747 // Output stubs for link-once variables
748 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
749 O << ".data\n.align 2\n";
750 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
751 e = LinkOnceStubs.end(); i != e; ++i) {
752 O << "L" << *i << "$non_lazy_ptr:\n"
753 << "\t.long\t" << *i << '\n';
754 }
755
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000756 delete Mang;
757 return false; // success
758}
759
760} // End llvm namespace