blob: a20525c814a52c23543ce4d595743f80e485fb59 [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 Brukmanaf313fb2004-07-28 00:00:48 +0000503 } else {
504 O << Imm;
505 }
506}
507
Nate Begemane59bf592004-08-14 22:09:10 +0000508/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
509/// the current output stream.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000510///
Nate Begemane59bf592004-08-14 22:09:10 +0000511void PowerPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
512 ++EmittedInsts;
513 if (printInstruction(MI))
514 return; // Printer was automatically generated
515
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000516 unsigned Opcode = MI->getOpcode();
517 const TargetInstrInfo &TII = *TM.getInstrInfo();
518 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000519 unsigned i;
Misha Brukmanc6cc10f2004-06-25 19:24:52 +0000520
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000521 unsigned ArgCount = MI->getNumOperands();
522 unsigned ArgType[] = {
Misha Brukman5b570812004-08-10 22:47:03 +0000523 (Desc.TSFlags >> PPCII::Arg0TypeShift) & PPCII::ArgTypeMask,
524 (Desc.TSFlags >> PPCII::Arg1TypeShift) & PPCII::ArgTypeMask,
525 (Desc.TSFlags >> PPCII::Arg2TypeShift) & PPCII::ArgTypeMask,
526 (Desc.TSFlags >> PPCII::Arg3TypeShift) & PPCII::ArgTypeMask,
527 (Desc.TSFlags >> PPCII::Arg4TypeShift) & PPCII::ArgTypeMask
Misha Brukman22e12072004-06-25 15:11:34 +0000528 };
Misha Brukman5b570812004-08-10 22:47:03 +0000529 assert(((Desc.TSFlags & PPCII::VMX) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000530 "Instruction requires VMX support");
Misha Brukman5b570812004-08-10 22:47:03 +0000531 assert(((Desc.TSFlags & PPCII::PPC64) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000532 "Instruction requires 64 bit support");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000533
Misha Brukman61114612004-07-20 00:42:19 +0000534 // CALLpcrel and CALLindirect are handled specially here to print only the
535 // appropriate number of args that the assembler expects. This is because
536 // may have many arguments appended to record the uses of registers that are
537 // holding arguments to the called function.
Misha Brukman5b570812004-08-10 22:47:03 +0000538 if (Opcode == PPC::COND_BRANCH) {
Misha Brukmanab967902004-07-27 18:40:39 +0000539 std::cerr << "Error: untranslated conditional branch psuedo instruction!\n";
540 abort();
Misha Brukman5b570812004-08-10 22:47:03 +0000541 } else if (Opcode == PPC::IMPLICIT_DEF) {
Misha Brukman29188c62004-07-16 19:01:13 +0000542 O << "; IMPLICIT DEF ";
543 printOp(MI->getOperand(0));
544 O << "\n";
545 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000546 } else if (Opcode == PPC::CALLpcrel) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000547 O << TII.getName(Opcode) << " ";
Misha Brukman61114612004-07-20 00:42:19 +0000548 printOp(MI->getOperand(0));
549 O << "\n";
550 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000551 } else if (Opcode == PPC::CALLindirect) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000552 O << TII.getName(Opcode) << " ";
553 printImmOp(MI->getOperand(0), ArgType[0]);
Misha Brukman61114612004-07-20 00:42:19 +0000554 O << ", ";
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000555 printImmOp(MI->getOperand(1), ArgType[0]);
Misha Brukman61114612004-07-20 00:42:19 +0000556 O << "\n";
557 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000558 } else if (Opcode == PPC::MovePCtoLR) {
Misha Brukman61114612004-07-20 00:42:19 +0000559 // FIXME: should probably be converted to cout.width and cout.fill
Misha Brukmancf8d2442004-07-26 16:28:33 +0000560 O << "bl \"L0000" << LabelNumber << "$pb\"\n";
561 O << "\"L0000" << LabelNumber << "$pb\":\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000562 O << "\tmflr ";
563 printOp(MI->getOperand(0));
Misha Brukman218bec72004-06-29 17:13:26 +0000564 O << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000565 return;
566 }
567
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000568 O << TII.getName(Opcode) << " ";
Misha Brukman5b570812004-08-10 22:47:03 +0000569 if (Opcode == PPC::LOADLoDirect || Opcode == PPC::LOADLoIndirect) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000570 printOp(MI->getOperand(0));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000571 O << ", lo16(";
Nate Begemanb73a7112004-08-13 09:32:01 +0000572 printOp(MI->getOperand(2), true /* LoadAddrOp */);
Misha Brukmancf8d2442004-07-26 16:28:33 +0000573 O << "-\"L0000" << LabelNumber << "$pb\")";
Misha Brukman218bec72004-06-29 17:13:26 +0000574 O << "(";
Misha Brukman5b570812004-08-10 22:47:03 +0000575 if (MI->getOperand(1).getReg() == PPC::R0)
Misha Brukman218bec72004-06-29 17:13:26 +0000576 O << "0";
577 else
578 printOp(MI->getOperand(1));
579 O << ")\n";
Misha Brukman5b570812004-08-10 22:47:03 +0000580 } else if (Opcode == PPC::LOADHiAddr) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000581 printOp(MI->getOperand(0));
582 O << ", ";
Misha Brukman5b570812004-08-10 22:47:03 +0000583 if (MI->getOperand(1).getReg() == PPC::R0)
Misha Brukman218bec72004-06-29 17:13:26 +0000584 O << "0";
585 else
586 printOp(MI->getOperand(1));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000587 O << ", ha16(" ;
Nate Begemanb73a7112004-08-13 09:32:01 +0000588 printOp(MI->getOperand(2), true /* LoadAddrOp */);
Misha Brukmancf8d2442004-07-26 16:28:33 +0000589 O << "-\"L0000" << LabelNumber << "$pb\")\n";
Misha Brukman5b570812004-08-10 22:47:03 +0000590 } else if (ArgCount == 3 && ArgType[1] == PPCII::Disimm16) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000591 printOp(MI->getOperand(0));
592 O << ", ";
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000593 printImmOp(MI->getOperand(1), ArgType[1]);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000594 O << "(";
Misha Brukmanb9e8f972004-06-30 21:54:12 +0000595 if (MI->getOperand(2).hasAllocatedReg() &&
Misha Brukman5b570812004-08-10 22:47:03 +0000596 MI->getOperand(2).getReg() == PPC::R0)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000597 O << "0";
598 else
599 printOp(MI->getOperand(2));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000600 O << ")\n";
601 } else {
Misha Brukman7f484a52004-06-24 23:51:00 +0000602 for (i = 0; i < ArgCount; ++i) {
Misha Brukmanab967902004-07-27 18:40:39 +0000603 // addi and friends
Misha Brukman5b570812004-08-10 22:47:03 +0000604 if (i == 1 && ArgCount == 3 && ArgType[2] == PPCII::Simm16 &&
Misha Brukman4363bdb2004-07-01 21:09:12 +0000605 MI->getOperand(1).hasAllocatedReg() &&
Misha Brukman5b570812004-08-10 22:47:03 +0000606 MI->getOperand(1).getReg() == PPC::R0) {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000607 O << "0";
Misha Brukmanab967902004-07-27 18:40:39 +0000608 // for long branch support, bc $+8
609 } else if (i == 1 && ArgCount == 2 && MI->getOperand(1).isImmediate() &&
610 TII.isBranch(MI->getOpcode())) {
611 O << "$+8";
612 assert(8 == MI->getOperand(i).getImmedValue()
613 && "branch off PC not to pc+8?");
614 //printOp(MI->getOperand(i));
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000615 } else if (MI->getOperand(i).isImmediate()) {
616 printImmOp(MI->getOperand(i), ArgType[i]);
Misha Brukman218bec72004-06-29 17:13:26 +0000617 } else {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000618 printOp(MI->getOperand(i));
619 }
Misha Brukman7f484a52004-06-24 23:51:00 +0000620 if (ArgCount - 1 == i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000621 O << "\n";
622 else
623 O << ", ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000624 }
625 }
Nate Begemane59bf592004-08-14 22:09:10 +0000626 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000627}
628
Nate Begemane59bf592004-08-14 22:09:10 +0000629bool PowerPCAsmPrinter::doInitialization(Module &M) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000630 Mang = new Mangler(M, true);
631 return false; // success
632}
633
634// SwitchSection - Switch to the specified section of the executable if we are
635// not already in it!
636//
637static void SwitchSection(std::ostream &OS, std::string &CurSection,
638 const char *NewSection) {
639 if (CurSection != NewSection) {
640 CurSection = NewSection;
641 if (!CurSection.empty())
642 OS << "\t" << NewSection << "\n";
643 }
644}
645
Nate Begemane59bf592004-08-14 22:09:10 +0000646bool PowerPCAsmPrinter::doFinalization(Module &M) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000647 const TargetData &TD = TM.getTargetData();
648 std::string CurSection;
649
650 // Print out module-level global variables here.
651 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
652 if (I->hasInitializer()) { // External global require no code
653 O << "\n\n";
654 std::string name = Mang->getValueName(I);
655 Constant *C = I->getInitializer();
656 unsigned Size = TD.getTypeSize(C->getType());
657 unsigned Align = TD.getTypeAlignment(C->getType());
658
Misha Brukman97a296f2004-07-21 20:11:11 +0000659 if (C->isNullValue() && /* FIXME: Verify correct */
660 (I->hasInternalLinkage() || I->hasWeakLinkage())) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000661 SwitchSection(O, CurSection, ".data");
662 if (I->hasInternalLinkage())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000663 O << ".lcomm " << name << "," << TD.getTypeSize(C->getType())
Misha Brukman218bec72004-06-29 17:13:26 +0000664 << "," << (unsigned)TD.getTypeAlignment(C->getType());
665 else
Misha Brukmane2eceb52004-07-23 16:08:20 +0000666 O << ".comm " << name << "," << TD.getTypeSize(C->getType());
Misha Brukman218bec72004-06-29 17:13:26 +0000667 O << "\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000668 WriteAsOperand(O, I, true, true, &M);
669 O << "\n";
670 } else {
671 switch (I->getLinkage()) {
672 case GlobalValue::LinkOnceLinkage:
Misha Brukman97a296f2004-07-21 20:11:11 +0000673 O << ".section __TEXT,__textcoal_nt,coalesced,no_toc\n"
674 << ".weak_definition " << name << '\n'
675 << ".private_extern " << name << '\n'
676 << ".section __DATA,__datacoal_nt,coalesced,no_toc\n";
677 LinkOnceStubs.insert(name);
678 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000679 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
680 // Nonnull linkonce -> weak
681 O << "\t.weak " << name << "\n";
682 SwitchSection(O, CurSection, "");
683 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
684 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000685 case GlobalValue::AppendingLinkage:
686 // FIXME: appending linkage variables should go into a section of
687 // their name or something. For now, just emit them as external.
688 case GlobalValue::ExternalLinkage:
689 // If external or appending, declare as a global symbol
690 O << "\t.globl " << name << "\n";
691 // FALL THROUGH
692 case GlobalValue::InternalLinkage:
Misha Brukman61297ee2004-06-29 23:40:57 +0000693 SwitchSection(O, CurSection, ".data");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000694 break;
695 }
696
697 O << "\t.align " << Align << "\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000698 O << name << ":\t\t\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000699 WriteAsOperand(O, I, true, true, &M);
700 O << " = ";
701 WriteAsOperand(O, C, false, false, &M);
702 O << "\n";
703 emitGlobalConstant(C);
704 }
705 }
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000706
707 // Output stubs for dynamically-linked functions
708 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
709 i != e; ++i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000710 {
Misha Brukmane2eceb52004-07-23 16:08:20 +0000711 O << ".data\n";
712 O << ".section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n";
713 O << "\t.align 2\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000714 O << "L" << *i << "$stub:\n";
715 O << "\t.indirect_symbol " << *i << "\n";
716 O << "\tmflr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000717 O << "\tbcl 20,31,L0$" << *i << "\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000718 O << "L0$" << *i << ":\n";
719 O << "\tmflr r11\n";
720 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
721 O << "\tmtlr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000722 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000723 O << "\tmtctr r12\n";
724 O << "\tbctr\n";
725 O << ".data\n";
726 O << ".lazy_symbol_pointer\n";
727 O << "L" << *i << "$lazy_ptr:\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000728 O << "\t.indirect_symbol " << *i << "\n";
729 O << "\t.long dyld_stub_binding_helper\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000730 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000731
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000732 O << "\n";
733
734 // Output stubs for external global variables
735 if (GVStubs.begin() != GVStubs.end())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000736 O << ".data\n.non_lazy_symbol_pointer\n";
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000737 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
738 i != e; ++i) {
739 O << "L" << *i << "$non_lazy_ptr:\n";
740 O << "\t.indirect_symbol " << *i << "\n";
741 O << "\t.long\t0\n";
742 }
743
Nate Begemane59bf592004-08-14 22:09:10 +0000744 // Output stubs for link-once variables
745 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
746 O << ".data\n.align 2\n";
747 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
748 e = LinkOnceStubs.end(); i != e; ++i) {
749 O << "L" << *i << "$non_lazy_ptr:\n"
750 << "\t.long\t" << *i << '\n';
751 }
752
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000753 delete Mang;
754 return false; // success
755}
756
757} // End llvm namespace