blob: fb3e1824e73413a53a4d4ce841a81f6c5db9b8e2 [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);
Chris Lattner0ea31712004-08-15 05:46:14 +000092 } else if (MO.isImmediate()) {
93 O << MO.getImmedValue();
Chris Lattner7bb424f2004-08-14 23:27:29 +000094 } else {
95 printOp(MO);
96 }
97 }
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 }
103
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000104 void printConstantPool(MachineConstantPool *MCP);
105 bool runOnMachineFunction(MachineFunction &F);
106 bool doInitialization(Module &M);
107 bool doFinalization(Module &M);
108 void emitGlobalConstant(const Constant* CV);
109 void emitConstantValueOnly(const Constant *CV);
110 };
111} // end of anonymous namespace
112
Misha Brukman3d9a6c22004-08-11 00:09:42 +0000113/// createPPC32AsmPrinterPass - Returns a pass that prints the PPC
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000114/// assembly code for a MachineFunction to the given output stream,
115/// using the given target machine description. This should work
Misha Brukman7103fba2004-08-09 22:27:45 +0000116/// regardless of whether the function is in SSA form or not.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000117///
Nate Begemane59bf592004-08-14 22:09:10 +0000118FunctionPass *createPPCAsmPrinter(std::ostream &o,TargetMachine &tm) {
119 return new PowerPCAsmPrinter(o, tm);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000120}
121
Nate Begemane59bf592004-08-14 22:09:10 +0000122// Include the auto-generated portion of the assembly writer
123#include "PowerPCGenAsmWriter.inc"
124
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000125/// isStringCompatible - Can we treat the specified array as a string?
126/// Only if it is an array of ubytes or non-negative sbytes.
127///
128static bool isStringCompatible(const ConstantArray *CVA) {
129 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
130 if (ETy == Type::UByteTy) return true;
131 if (ETy != Type::SByteTy) return false;
132
133 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
134 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
135 return false;
136
137 return true;
138}
139
140/// toOctal - Convert the low order bits of X into an octal digit.
141///
142static inline char toOctal(int X) {
143 return (X&7)+'0';
144}
145
146/// getAsCString - Return the specified array as a C compatible
147/// string, only if the predicate isStringCompatible is true.
148///
149static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
150 assert(isStringCompatible(CVA) && "Array is not string compatible!");
151
152 O << "\"";
153 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
154 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
155
156 if (C == '"') {
157 O << "\\\"";
158 } else if (C == '\\') {
159 O << "\\\\";
160 } else if (isprint(C)) {
161 O << C;
162 } else {
Misha Brukmane2eceb52004-07-23 16:08:20 +0000163 switch (C) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000164 case '\b': O << "\\b"; break;
165 case '\f': O << "\\f"; break;
166 case '\n': O << "\\n"; break;
167 case '\r': O << "\\r"; break;
168 case '\t': O << "\\t"; break;
169 default:
170 O << '\\';
171 O << toOctal(C >> 6);
172 O << toOctal(C >> 3);
173 O << toOctal(C >> 0);
174 break;
175 }
176 }
177 }
178 O << "\"";
179}
180
181// Print out the specified constant, without a storage class. Only the
182// constants valid in constant expressions can occur here.
Nate Begemane59bf592004-08-14 22:09:10 +0000183void PowerPCAsmPrinter::emitConstantValueOnly(const Constant *CV) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000184 if (CV->isNullValue())
185 O << "0";
186 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
187 assert(CB == ConstantBool::True);
188 O << "1";
189 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
190 O << CI->getValue();
191 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
192 O << CI->getValue();
Chris Lattner67910e12004-07-18 07:29:35 +0000193 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000194 // This is a constant address for a global variable or function. Use the
195 // name of the variable or function as the address value.
Chris Lattner67910e12004-07-18 07:29:35 +0000196 O << Mang->getValueName(GV);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000197 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
198 const TargetData &TD = TM.getTargetData();
Misha Brukmane2eceb52004-07-23 16:08:20 +0000199 switch (CE->getOpcode()) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000200 case Instruction::GetElementPtr: {
201 // generate a symbolic expression for the byte address
202 const Constant *ptrVal = CE->getOperand(0);
203 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
204 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
205 O << "(";
206 emitConstantValueOnly(ptrVal);
207 O << ") + " << Offset;
208 } else {
209 emitConstantValueOnly(ptrVal);
210 }
211 break;
212 }
213 case Instruction::Cast: {
214 // Support only non-converting or widening casts for now, that is, ones
215 // that do not involve a change in value. This assertion is really gross,
216 // and may not even be a complete check.
217 Constant *Op = CE->getOperand(0);
218 const Type *OpTy = Op->getType(), *Ty = CE->getType();
219
220 // Remember, kids, pointers on x86 can be losslessly converted back and
221 // forth into 32-bit or wider integers, regardless of signedness. :-P
222 assert(((isa<PointerType>(OpTy)
223 && (Ty == Type::LongTy || Ty == Type::ULongTy
224 || Ty == Type::IntTy || Ty == Type::UIntTy))
225 || (isa<PointerType>(Ty)
226 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
227 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
228 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
229 && OpTy->isLosslesslyConvertibleTo(Ty))))
230 && "FIXME: Don't yet support this kind of constant cast expr");
231 O << "(";
232 emitConstantValueOnly(Op);
233 O << ")";
234 break;
235 }
236 case Instruction::Add:
237 O << "(";
238 emitConstantValueOnly(CE->getOperand(0));
239 O << ") + (";
240 emitConstantValueOnly(CE->getOperand(1));
241 O << ")";
242 break;
243 default:
244 assert(0 && "Unsupported operator!");
245 }
246 } else {
247 assert(0 && "Unknown constant value!");
248 }
249}
250
251// Print a constant value or values, with the appropriate storage class as a
252// prefix.
Nate Begemane59bf592004-08-14 22:09:10 +0000253void PowerPCAsmPrinter::emitGlobalConstant(const Constant *CV) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000254 const TargetData &TD = TM.getTargetData();
255
Misha Brukmane48178e2004-07-20 15:45:27 +0000256 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000257 if (isStringCompatible(CVA)) {
Misha Brukman218bec72004-06-29 17:13:26 +0000258 O << "\t.ascii ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000259 printAsCString(O, CVA);
260 O << "\n";
261 } else { // Not a string. Print the values in successive locations
Chris Lattner6173cd92004-08-04 17:29:14 +0000262 for (unsigned i=0, e = CVA->getNumOperands(); i != e; i++)
263 emitGlobalConstant(CVA->getOperand(i));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000264 }
265 return;
266 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
267 // Print the fields in successive locations. Pad to align if needed!
268 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000269 unsigned sizeSoFar = 0;
Chris Lattner6173cd92004-08-04 17:29:14 +0000270 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; i++) {
271 const Constant* field = CVS->getOperand(i);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000272
273 // Check if padding is needed and insert one or more 0s.
274 unsigned fieldSize = TD.getTypeSize(field->getType());
Chris Lattner6173cd92004-08-04 17:29:14 +0000275 unsigned padSize = ((i == e-1? cvsLayout->StructSize
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000276 : cvsLayout->MemberOffsets[i+1])
277 - cvsLayout->MemberOffsets[i]) - fieldSize;
278 sizeSoFar += fieldSize + padSize;
279
280 // Now print the actual field value
281 emitGlobalConstant(field);
282
283 // Insert the field padding unless it's zero bytes...
284 if (padSize)
285 O << "\t.space\t " << padSize << "\n";
286 }
287 assert(sizeSoFar == cvsLayout->StructSize &&
288 "Layout of constant struct may be incorrect!");
289 return;
290 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
291 // FP Constants are printed as integer constants to avoid losing
292 // precision...
293 double Val = CFP->getValue();
Nate Begemane59bf592004-08-14 22:09:10 +0000294 union DU { // Abide by C TBAA rules
295 double FVal;
296 uint64_t UVal;
297 struct {
298 uint32_t MSWord;
299 uint32_t LSWord;
300 } T;
301 } U;
302 U.FVal = Val;
303
304 O << ".long\t" << U.T.MSWord << "\t; double most significant word "
305 << Val << "\n";
306 O << ".long\t" << U.T.LSWord << "\t; double least significant word "
307 << Val << "\n";
308 return;
Misha Brukmanf63bc192004-07-28 19:12:24 +0000309 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
Misha Brukman2bf183c2004-06-25 15:42:10 +0000310 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
311 union DU { // Abide by C TBAA rules
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000312 int64_t UVal;
313 struct {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000314 uint32_t MSWord;
315 uint32_t LSWord;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000316 } T;
317 } U;
318 U.UVal = CI->getRawValue();
319
Misha Brukman218bec72004-06-29 17:13:26 +0000320 O << ".long\t" << U.T.MSWord << "\t; Double-word most significant word "
Misha Brukman46fd00a2004-06-24 23:04:11 +0000321 << U.UVal << "\n";
Misha Brukman29188c62004-07-16 19:01:13 +0000322 O << ".long\t" << U.T.LSWord << "\t; Double-word least significant word "
Misha Brukman46fd00a2004-06-24 23:04:11 +0000323 << U.UVal << "\n";
324 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000325 }
326 }
327
328 const Type *type = CV->getType();
329 O << "\t";
Misha Brukmand71bd562004-06-21 17:19:08 +0000330 switch (type->getTypeID()) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000331 case Type::UByteTyID: case Type::SByteTyID:
332 O << ".byte";
333 break;
334 case Type::UShortTyID: case Type::ShortTyID:
335 O << ".short";
336 break;
337 case Type::BoolTyID:
338 case Type::PointerTyID:
339 case Type::UIntTyID: case Type::IntTyID:
340 O << ".long";
341 break;
342 case Type::ULongTyID: case Type::LongTyID:
Misha Brukman46fd00a2004-06-24 23:04:11 +0000343 assert (0 && "Should have already output double-word constant.");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000344 case Type::FloatTyID: case Type::DoubleTyID:
345 assert (0 && "Should have already output floating point constant.");
346 default:
Misha Brukman97a296f2004-07-21 20:11:11 +0000347 if (CV == Constant::getNullValue(type)) { // Zero initializer?
348 O << ".space\t" << TD.getTypeSize(type) << "\n";
349 return;
350 }
351 std::cerr << "Can't handle printing: " << *CV;
352 abort();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000353 break;
354 }
355 O << "\t";
356 emitConstantValueOnly(CV);
357 O << "\n";
358}
359
360/// printConstantPool - Print to the current output stream assembly
361/// representations of the constants in the constant pool MCP. This is
362/// used to print out constants which have been "spilled to memory" by
363/// the code generator.
364///
Nate Begemane59bf592004-08-14 22:09:10 +0000365void PowerPCAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000366 const std::vector<Constant*> &CP = MCP->getConstants();
367 const TargetData &TD = TM.getTargetData();
368
369 if (CP.empty()) return;
370
371 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
372 O << "\t.const\n";
373 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
374 << "\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000375 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t;"
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000376 << *CP[i] << "\n";
377 emitGlobalConstant(CP[i]);
378 }
379}
380
381/// runOnMachineFunction - This uses the printMachineInstruction()
382/// method to print assembly for each instruction.
383///
Nate Begemane59bf592004-08-14 22:09:10 +0000384bool PowerPCAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000385 O << "\n\n";
386 // What's my mangled name?
387 CurrentFnName = Mang->getValueName(MF.getFunction());
388
389 // Print out constants referenced by the function
390 printConstantPool(MF.getConstantPool());
391
392 // Print out labels for the function.
393 O << "\t.text\n";
394 O << "\t.globl\t" << CurrentFnName << "\n";
Misha Brukman61297ee2004-06-29 23:40:57 +0000395 O << "\t.align 2\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000396 O << CurrentFnName << ":\n";
397
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000398 // Print out code for the function.
399 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
400 I != E; ++I) {
401 // Print a label for the basic block.
Misha Brukman218bec72004-06-29 17:13:26 +0000402 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t; "
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000403 << I->getBasicBlock()->getName() << "\n";
404 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukman46fd00a2004-06-24 23:04:11 +0000405 II != E; ++II) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000406 // Print the assembly for the instruction.
407 O << "\t";
408 printMachineInstruction(II);
409 }
410 }
Misha Brukmancf8d2442004-07-26 16:28:33 +0000411 ++LabelNumber;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000412
413 // We didn't modify anything.
414 return false;
415}
416
Nate Begemane59bf592004-08-14 22:09:10 +0000417void PowerPCAsmPrinter::printOp(const MachineOperand &MO,
Nate Begemanb73a7112004-08-13 09:32:01 +0000418 bool LoadAddrOp /* = false */) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000419 const MRegisterInfo &RI = *TM.getRegisterInfo();
420 int new_symbol;
421
422 switch (MO.getType()) {
423 case MachineOperand::MO_VirtualRegister:
424 if (Value *V = MO.getVRegValueOrNull()) {
425 O << "<" << V->getName() << ">";
426 return;
427 }
428 // FALLTHROUGH
429 case MachineOperand::MO_MachineRegister:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000430 case MachineOperand::MO_CCRegister:
Misha Brukman7f484a52004-06-24 23:51:00 +0000431 O << LowercaseString(RI.get(MO.getReg()).Name);
432 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000433
434 case MachineOperand::MO_SignExtendedImmed:
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000435 case MachineOperand::MO_UnextendedImmed:
436 std::cerr << "printOp() does not handle immediate values\n";
437 abort();
Misha Brukman97a296f2004-07-21 20:11:11 +0000438 return;
439
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000440 case MachineOperand::MO_PCRelativeDisp:
441 std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
442 abort();
443 return;
444
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000445 case MachineOperand::MO_MachineBasicBlock: {
446 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
447 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
Misha Brukman218bec72004-06-29 17:13:26 +0000448 << "_" << MBBOp->getNumber() << "\t; "
Misha Brukman2bf183c2004-06-25 15:42:10 +0000449 << MBBOp->getBasicBlock()->getName();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000450 return;
451 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000452
453 case MachineOperand::MO_ConstantPoolIndex:
454 O << ".CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000455 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000456
457 case MachineOperand::MO_ExternalSymbol:
458 O << MO.getSymbolName();
459 return;
460
Nate Begemanb73a7112004-08-13 09:32:01 +0000461 case MachineOperand::MO_GlobalAddress: {
462 GlobalValue *GV = MO.getGlobal();
463 std::string Name = Mang->getValueName(GV);
Misha Brukmane2eceb52004-07-23 16:08:20 +0000464
Nate Begemanb73a7112004-08-13 09:32:01 +0000465 // Dynamically-resolved functions need a stub for the function. Be
466 // wary however not to output $stub for external functions whose addresses
467 // are taken. Those should be emitted as $non_lazy_ptr below.
468 Function *F = dyn_cast<Function>(GV);
469 if (F && F->isExternal() && !LoadAddrOp &&
470 TM.CalledFunctions.find(F) != TM.CalledFunctions.end()) {
471 FnStubs.insert(Name);
472 O << "L" << Name << "$stub";
473 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000474 }
Nate Begemane59bf592004-08-14 22:09:10 +0000475
Nate Begemanb73a7112004-08-13 09:32:01 +0000476 // External global variables need a non-lazily-resolved stub
Nate Begemane59bf592004-08-14 22:09:10 +0000477 if (GV->isExternal() && TM.AddressTaken.find(GV) != TM.AddressTaken.end()) {
Nate Begemanb73a7112004-08-13 09:32:01 +0000478 GVStubs.insert(Name);
479 O << "L" << Name << "$non_lazy_ptr";
480 return;
481 }
Nate Begemane59bf592004-08-14 22:09:10 +0000482
483 if (F && LoadAddrOp && TM.AddressTaken.find(GV) != TM.AddressTaken.end()) {
484 LinkOnceStubs.insert(Name);
485 O << "L" << Name << "$non_lazy_ptr";
486 return;
487 }
Nate Begemanb73a7112004-08-13 09:32:01 +0000488
489 O << Mang->getValueName(GV);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000490 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000491 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000492
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000493 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000494 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000495 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000496 }
497}
498
Nate Begemane59bf592004-08-14 22:09:10 +0000499void PowerPCAsmPrinter::printImmOp(const MachineOperand &MO, unsigned ArgType) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000500 int Imm = MO.getImmedValue();
Misha Brukman5b570812004-08-10 22:47:03 +0000501 if (ArgType == PPCII::Simm16 || ArgType == PPCII::Disimm16) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000502 O << (short)Imm;
Misha Brukman5b570812004-08-10 22:47:03 +0000503 } else if (ArgType == PPCII::Zimm16) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000504 O << (unsigned short)Imm;
505 } else {
506 O << Imm;
507 }
508}
509
Nate Begemane59bf592004-08-14 22:09:10 +0000510/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
511/// the current output stream.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000512///
Nate Begemane59bf592004-08-14 22:09:10 +0000513void PowerPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
514 ++EmittedInsts;
515 if (printInstruction(MI))
516 return; // Printer was automatically generated
517
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000518 unsigned Opcode = MI->getOpcode();
519 const TargetInstrInfo &TII = *TM.getInstrInfo();
520 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000521 unsigned i;
Misha Brukmanc6cc10f2004-06-25 19:24:52 +0000522
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000523 unsigned ArgCount = MI->getNumOperands();
524 unsigned ArgType[] = {
Misha Brukman5b570812004-08-10 22:47:03 +0000525 (Desc.TSFlags >> PPCII::Arg0TypeShift) & PPCII::ArgTypeMask,
526 (Desc.TSFlags >> PPCII::Arg1TypeShift) & PPCII::ArgTypeMask,
527 (Desc.TSFlags >> PPCII::Arg2TypeShift) & PPCII::ArgTypeMask,
528 (Desc.TSFlags >> PPCII::Arg3TypeShift) & PPCII::ArgTypeMask,
529 (Desc.TSFlags >> PPCII::Arg4TypeShift) & PPCII::ArgTypeMask
Misha Brukman22e12072004-06-25 15:11:34 +0000530 };
Misha Brukman5b570812004-08-10 22:47:03 +0000531 assert(((Desc.TSFlags & PPCII::VMX) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000532 "Instruction requires VMX support");
Misha Brukman5b570812004-08-10 22:47:03 +0000533 assert(((Desc.TSFlags & PPCII::PPC64) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000534 "Instruction requires 64 bit support");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000535
Misha Brukman61114612004-07-20 00:42:19 +0000536 // CALLpcrel and CALLindirect are handled specially here to print only the
537 // appropriate number of args that the assembler expects. This is because
538 // may have many arguments appended to record the uses of registers that are
539 // holding arguments to the called function.
Misha Brukman5b570812004-08-10 22:47:03 +0000540 if (Opcode == PPC::COND_BRANCH) {
Misha Brukmanab967902004-07-27 18:40:39 +0000541 std::cerr << "Error: untranslated conditional branch psuedo instruction!\n";
542 abort();
Misha Brukman5b570812004-08-10 22:47:03 +0000543 } else if (Opcode == PPC::IMPLICIT_DEF) {
Misha Brukman29188c62004-07-16 19:01:13 +0000544 O << "; IMPLICIT DEF ";
545 printOp(MI->getOperand(0));
546 O << "\n";
547 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000548 } else if (Opcode == PPC::CALLpcrel) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000549 O << TII.getName(Opcode) << " ";
Misha Brukman61114612004-07-20 00:42:19 +0000550 printOp(MI->getOperand(0));
551 O << "\n";
552 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000553 } else if (Opcode == PPC::CALLindirect) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000554 O << TII.getName(Opcode) << " ";
555 printImmOp(MI->getOperand(0), ArgType[0]);
Misha Brukman61114612004-07-20 00:42:19 +0000556 O << ", ";
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000557 printImmOp(MI->getOperand(1), ArgType[0]);
Misha Brukman61114612004-07-20 00:42:19 +0000558 O << "\n";
559 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000560 } else if (Opcode == PPC::MovePCtoLR) {
Misha Brukman61114612004-07-20 00:42:19 +0000561 // FIXME: should probably be converted to cout.width and cout.fill
Misha Brukmancf8d2442004-07-26 16:28:33 +0000562 O << "bl \"L0000" << LabelNumber << "$pb\"\n";
563 O << "\"L0000" << LabelNumber << "$pb\":\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000564 O << "\tmflr ";
565 printOp(MI->getOperand(0));
Misha Brukman218bec72004-06-29 17:13:26 +0000566 O << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000567 return;
568 }
569
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000570 O << TII.getName(Opcode) << " ";
Misha Brukman5b570812004-08-10 22:47:03 +0000571 if (Opcode == PPC::LOADLoDirect || Opcode == PPC::LOADLoIndirect) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000572 printOp(MI->getOperand(0));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000573 O << ", lo16(";
Nate Begemanb73a7112004-08-13 09:32:01 +0000574 printOp(MI->getOperand(2), true /* LoadAddrOp */);
Misha Brukmancf8d2442004-07-26 16:28:33 +0000575 O << "-\"L0000" << LabelNumber << "$pb\")";
Misha Brukman218bec72004-06-29 17:13:26 +0000576 O << "(";
Misha Brukman5b570812004-08-10 22:47:03 +0000577 if (MI->getOperand(1).getReg() == PPC::R0)
Misha Brukman218bec72004-06-29 17:13:26 +0000578 O << "0";
579 else
580 printOp(MI->getOperand(1));
581 O << ")\n";
Misha Brukman5b570812004-08-10 22:47:03 +0000582 } else if (Opcode == PPC::LOADHiAddr) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000583 printOp(MI->getOperand(0));
584 O << ", ";
Misha Brukman5b570812004-08-10 22:47:03 +0000585 if (MI->getOperand(1).getReg() == PPC::R0)
Misha Brukman218bec72004-06-29 17:13:26 +0000586 O << "0";
587 else
588 printOp(MI->getOperand(1));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000589 O << ", ha16(" ;
Nate Begemanb73a7112004-08-13 09:32:01 +0000590 printOp(MI->getOperand(2), true /* LoadAddrOp */);
Misha Brukmancf8d2442004-07-26 16:28:33 +0000591 O << "-\"L0000" << LabelNumber << "$pb\")\n";
Misha Brukman5b570812004-08-10 22:47:03 +0000592 } else if (ArgCount == 3 && ArgType[1] == PPCII::Disimm16) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000593 printOp(MI->getOperand(0));
594 O << ", ";
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000595 printImmOp(MI->getOperand(1), ArgType[1]);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000596 O << "(";
Misha Brukmanb9e8f972004-06-30 21:54:12 +0000597 if (MI->getOperand(2).hasAllocatedReg() &&
Misha Brukman5b570812004-08-10 22:47:03 +0000598 MI->getOperand(2).getReg() == PPC::R0)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000599 O << "0";
600 else
601 printOp(MI->getOperand(2));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000602 O << ")\n";
603 } else {
Misha Brukman7f484a52004-06-24 23:51:00 +0000604 for (i = 0; i < ArgCount; ++i) {
Misha Brukmanab967902004-07-27 18:40:39 +0000605 // addi and friends
Misha Brukman5b570812004-08-10 22:47:03 +0000606 if (i == 1 && ArgCount == 3 && ArgType[2] == PPCII::Simm16 &&
Misha Brukman4363bdb2004-07-01 21:09:12 +0000607 MI->getOperand(1).hasAllocatedReg() &&
Misha Brukman5b570812004-08-10 22:47:03 +0000608 MI->getOperand(1).getReg() == PPC::R0) {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000609 O << "0";
Misha Brukmanab967902004-07-27 18:40:39 +0000610 // for long branch support, bc $+8
611 } else if (i == 1 && ArgCount == 2 && MI->getOperand(1).isImmediate() &&
612 TII.isBranch(MI->getOpcode())) {
613 O << "$+8";
614 assert(8 == MI->getOperand(i).getImmedValue()
615 && "branch off PC not to pc+8?");
616 //printOp(MI->getOperand(i));
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000617 } else if (MI->getOperand(i).isImmediate()) {
618 printImmOp(MI->getOperand(i), ArgType[i]);
Misha Brukman218bec72004-06-29 17:13:26 +0000619 } else {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000620 printOp(MI->getOperand(i));
621 }
Misha Brukman7f484a52004-06-24 23:51:00 +0000622 if (ArgCount - 1 == i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000623 O << "\n";
624 else
625 O << ", ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000626 }
627 }
Nate Begemane59bf592004-08-14 22:09:10 +0000628 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000629}
630
Nate Begemane59bf592004-08-14 22:09:10 +0000631bool PowerPCAsmPrinter::doInitialization(Module &M) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000632 Mang = new Mangler(M, true);
633 return false; // success
634}
635
636// SwitchSection - Switch to the specified section of the executable if we are
637// not already in it!
638//
639static void SwitchSection(std::ostream &OS, std::string &CurSection,
640 const char *NewSection) {
641 if (CurSection != NewSection) {
642 CurSection = NewSection;
643 if (!CurSection.empty())
644 OS << "\t" << NewSection << "\n";
645 }
646}
647
Nate Begemane59bf592004-08-14 22:09:10 +0000648bool PowerPCAsmPrinter::doFinalization(Module &M) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000649 const TargetData &TD = TM.getTargetData();
650 std::string CurSection;
651
652 // Print out module-level global variables here.
653 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
654 if (I->hasInitializer()) { // External global require no code
655 O << "\n\n";
656 std::string name = Mang->getValueName(I);
657 Constant *C = I->getInitializer();
658 unsigned Size = TD.getTypeSize(C->getType());
659 unsigned Align = TD.getTypeAlignment(C->getType());
660
Misha Brukman97a296f2004-07-21 20:11:11 +0000661 if (C->isNullValue() && /* FIXME: Verify correct */
662 (I->hasInternalLinkage() || I->hasWeakLinkage())) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000663 SwitchSection(O, CurSection, ".data");
664 if (I->hasInternalLinkage())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000665 O << ".lcomm " << name << "," << TD.getTypeSize(C->getType())
Misha Brukman218bec72004-06-29 17:13:26 +0000666 << "," << (unsigned)TD.getTypeAlignment(C->getType());
667 else
Misha Brukmane2eceb52004-07-23 16:08:20 +0000668 O << ".comm " << name << "," << TD.getTypeSize(C->getType());
Misha Brukman218bec72004-06-29 17:13:26 +0000669 O << "\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000670 WriteAsOperand(O, I, true, true, &M);
671 O << "\n";
672 } else {
673 switch (I->getLinkage()) {
674 case GlobalValue::LinkOnceLinkage:
Misha Brukman97a296f2004-07-21 20:11:11 +0000675 O << ".section __TEXT,__textcoal_nt,coalesced,no_toc\n"
676 << ".weak_definition " << name << '\n'
677 << ".private_extern " << name << '\n'
678 << ".section __DATA,__datacoal_nt,coalesced,no_toc\n";
679 LinkOnceStubs.insert(name);
680 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000681 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
682 // Nonnull linkonce -> weak
683 O << "\t.weak " << name << "\n";
684 SwitchSection(O, CurSection, "");
685 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
686 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000687 case GlobalValue::AppendingLinkage:
688 // FIXME: appending linkage variables should go into a section of
689 // their name or something. For now, just emit them as external.
690 case GlobalValue::ExternalLinkage:
691 // If external or appending, declare as a global symbol
692 O << "\t.globl " << name << "\n";
693 // FALL THROUGH
694 case GlobalValue::InternalLinkage:
Misha Brukman61297ee2004-06-29 23:40:57 +0000695 SwitchSection(O, CurSection, ".data");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000696 break;
697 }
698
699 O << "\t.align " << Align << "\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000700 O << name << ":\t\t\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000701 WriteAsOperand(O, I, true, true, &M);
702 O << " = ";
703 WriteAsOperand(O, C, false, false, &M);
704 O << "\n";
705 emitGlobalConstant(C);
706 }
707 }
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000708
709 // Output stubs for dynamically-linked functions
710 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
711 i != e; ++i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000712 {
Misha Brukmane2eceb52004-07-23 16:08:20 +0000713 O << ".data\n";
714 O << ".section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n";
715 O << "\t.align 2\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000716 O << "L" << *i << "$stub:\n";
717 O << "\t.indirect_symbol " << *i << "\n";
718 O << "\tmflr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000719 O << "\tbcl 20,31,L0$" << *i << "\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000720 O << "L0$" << *i << ":\n";
721 O << "\tmflr r11\n";
722 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
723 O << "\tmtlr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000724 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000725 O << "\tmtctr r12\n";
726 O << "\tbctr\n";
727 O << ".data\n";
728 O << ".lazy_symbol_pointer\n";
729 O << "L" << *i << "$lazy_ptr:\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000730 O << "\t.indirect_symbol " << *i << "\n";
731 O << "\t.long dyld_stub_binding_helper\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000732 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000733
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000734 O << "\n";
735
736 // Output stubs for external global variables
737 if (GVStubs.begin() != GVStubs.end())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000738 O << ".data\n.non_lazy_symbol_pointer\n";
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000739 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
740 i != e; ++i) {
741 O << "L" << *i << "$non_lazy_ptr:\n";
742 O << "\t.indirect_symbol " << *i << "\n";
743 O << "\t.long\t0\n";
744 }
745
Nate Begemane59bf592004-08-14 22:09:10 +0000746 // Output stubs for link-once variables
747 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
748 O << ".data\n.align 2\n";
749 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
750 e = LinkOnceStubs.end(); i != e; ++i) {
751 O << "L" << *i << "$non_lazy_ptr:\n"
752 << "\t.long\t" << *i << '\n';
753 }
754
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000755 delete Mang;
756 return false; // success
757}
758
759} // End llvm namespace