blob: 2da7a521c095d56ad358f338e305e2cc5653042a [file] [log] [blame]
Misha Brukman05fcd0c2004-07-08 17:58:04 +00001//===-- Printer.cpp - Convert LLVM code 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
12// the output mechanism used by `llc' and `lli -print-machineinstrs'.
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"
Misha Brukmane2eceb52004-07-23 16:08:20 +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"
30#include "llvm/Target/TargetMachine.h"
31#include "llvm/Support/Mangler.h"
Misha Brukman05794492004-06-24 17:31:42 +000032#include "Support/CommandLine.h"
33#include "Support/Debug.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000034#include "Support/Statistic.h"
35#include "Support/StringExtras.h"
Misha Brukman05794492004-06-24 17:31:42 +000036#include <set>
Misha Brukman5dfe3a92004-06-21 16:55:25 +000037
38namespace llvm {
39
40namespace {
41 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
42
43 struct Printer : public MachineFunctionPass {
44 /// Output stream on which we're printing assembly code.
45 ///
46 std::ostream &O;
47
48 /// Target machine description which we query for reg. names, data
49 /// layout, etc.
50 ///
Misha Brukmane2eceb52004-07-23 16:08:20 +000051 PowerPCTargetMachine &TM;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000052
53 /// Name-mangler for global names.
54 ///
55 Mangler *Mang;
Misha Brukman97a296f2004-07-21 20:11:11 +000056 std::set<std::string> FnStubs, GVStubs, LinkOnceStubs;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000057 std::set<std::string> Strings;
58
Misha Brukmancf8d2442004-07-26 16:28:33 +000059 Printer(std::ostream &o, TargetMachine &tm) : O(o),
60 TM(reinterpret_cast<PowerPCTargetMachine&>(tm)), LabelNumber(0) {}
Misha Brukman5dfe3a92004-06-21 16:55:25 +000061
Misha Brukman5dfe3a92004-06-21 16:55:25 +000062 /// Cache of mangled name for current function. This is
63 /// recalculated at the beginning of each call to
64 /// runOnMachineFunction().
65 ///
66 std::string CurrentFnName;
67
Misha Brukmancf8d2442004-07-26 16:28:33 +000068 /// Unique incrementer for label values for referencing Global values.
Misha Brukman218bec72004-06-29 17:13:26 +000069 ///
Misha Brukmancf8d2442004-07-26 16:28:33 +000070 unsigned LabelNumber;
71
Misha Brukman5dfe3a92004-06-21 16:55:25 +000072 virtual const char *getPassName() const {
73 return "PowerPC Assembly Printer";
74 }
75
76 void printMachineInstruction(const MachineInstr *MI);
Misha Brukmanbb4a9082004-06-28 15:53:27 +000077 void printOp(const MachineOperand &MO, bool elideOffsetKeyword = false);
Misha Brukman5dfe3a92004-06-21 16:55:25 +000078 void printConstantPool(MachineConstantPool *MCP);
79 bool runOnMachineFunction(MachineFunction &F);
80 bool doInitialization(Module &M);
81 bool doFinalization(Module &M);
82 void emitGlobalConstant(const Constant* CV);
83 void emitConstantValueOnly(const Constant *CV);
84 };
85} // end of anonymous namespace
86
Misha Brukman218bec72004-06-29 17:13:26 +000087/// createPPCCodePrinterPass - Returns a pass that prints the PPC
Misha Brukman5dfe3a92004-06-21 16:55:25 +000088/// assembly code for a MachineFunction to the given output stream,
89/// using the given target machine description. This should work
90/// regardless of whether the function is in SSA form.
91///
Misha Brukmanbb4a9082004-06-28 15:53:27 +000092FunctionPass *createPPCCodePrinterPass(std::ostream &o,TargetMachine &tm) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +000093 return new Printer(o, tm);
94}
95
96/// isStringCompatible - Can we treat the specified array as a string?
97/// Only if it is an array of ubytes or non-negative sbytes.
98///
99static bool isStringCompatible(const ConstantArray *CVA) {
100 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
101 if (ETy == Type::UByteTy) return true;
102 if (ETy != Type::SByteTy) return false;
103
104 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
105 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
106 return false;
107
108 return true;
109}
110
111/// toOctal - Convert the low order bits of X into an octal digit.
112///
113static inline char toOctal(int X) {
114 return (X&7)+'0';
115}
116
117/// getAsCString - Return the specified array as a C compatible
118/// string, only if the predicate isStringCompatible is true.
119///
120static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
121 assert(isStringCompatible(CVA) && "Array is not string compatible!");
122
123 O << "\"";
124 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
125 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
126
127 if (C == '"') {
128 O << "\\\"";
129 } else if (C == '\\') {
130 O << "\\\\";
131 } else if (isprint(C)) {
132 O << C;
133 } else {
Misha Brukmane2eceb52004-07-23 16:08:20 +0000134 switch (C) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000135 case '\b': O << "\\b"; break;
136 case '\f': O << "\\f"; break;
137 case '\n': O << "\\n"; break;
138 case '\r': O << "\\r"; break;
139 case '\t': O << "\\t"; break;
140 default:
141 O << '\\';
142 O << toOctal(C >> 6);
143 O << toOctal(C >> 3);
144 O << toOctal(C >> 0);
145 break;
146 }
147 }
148 }
149 O << "\"";
150}
151
152// Print out the specified constant, without a storage class. Only the
153// constants valid in constant expressions can occur here.
154void Printer::emitConstantValueOnly(const Constant *CV) {
155 if (CV->isNullValue())
156 O << "0";
157 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
158 assert(CB == ConstantBool::True);
159 O << "1";
160 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
161 O << CI->getValue();
162 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
163 O << CI->getValue();
Chris Lattner67910e12004-07-18 07:29:35 +0000164 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000165 // This is a constant address for a global variable or function. Use the
166 // name of the variable or function as the address value.
Chris Lattner67910e12004-07-18 07:29:35 +0000167 O << Mang->getValueName(GV);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000168 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
169 const TargetData &TD = TM.getTargetData();
Misha Brukmane2eceb52004-07-23 16:08:20 +0000170 switch (CE->getOpcode()) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000171 case Instruction::GetElementPtr: {
172 // generate a symbolic expression for the byte address
173 const Constant *ptrVal = CE->getOperand(0);
174 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
175 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
176 O << "(";
177 emitConstantValueOnly(ptrVal);
178 O << ") + " << Offset;
179 } else {
180 emitConstantValueOnly(ptrVal);
181 }
182 break;
183 }
184 case Instruction::Cast: {
185 // Support only non-converting or widening casts for now, that is, ones
186 // that do not involve a change in value. This assertion is really gross,
187 // and may not even be a complete check.
188 Constant *Op = CE->getOperand(0);
189 const Type *OpTy = Op->getType(), *Ty = CE->getType();
190
191 // Remember, kids, pointers on x86 can be losslessly converted back and
192 // forth into 32-bit or wider integers, regardless of signedness. :-P
193 assert(((isa<PointerType>(OpTy)
194 && (Ty == Type::LongTy || Ty == Type::ULongTy
195 || Ty == Type::IntTy || Ty == Type::UIntTy))
196 || (isa<PointerType>(Ty)
197 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
198 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
199 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
200 && OpTy->isLosslesslyConvertibleTo(Ty))))
201 && "FIXME: Don't yet support this kind of constant cast expr");
202 O << "(";
203 emitConstantValueOnly(Op);
204 O << ")";
205 break;
206 }
207 case Instruction::Add:
208 O << "(";
209 emitConstantValueOnly(CE->getOperand(0));
210 O << ") + (";
211 emitConstantValueOnly(CE->getOperand(1));
212 O << ")";
213 break;
214 default:
215 assert(0 && "Unsupported operator!");
216 }
217 } else {
218 assert(0 && "Unknown constant value!");
219 }
220}
221
222// Print a constant value or values, with the appropriate storage class as a
223// prefix.
224void Printer::emitGlobalConstant(const Constant *CV) {
225 const TargetData &TD = TM.getTargetData();
226
Misha Brukmane48178e2004-07-20 15:45:27 +0000227 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000228 if (isStringCompatible(CVA)) {
Misha Brukman218bec72004-06-29 17:13:26 +0000229 O << "\t.ascii ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000230 printAsCString(O, CVA);
231 O << "\n";
232 } else { // Not a string. Print the values in successive locations
233 const std::vector<Use> &constValues = CVA->getValues();
234 for (unsigned i=0; i < constValues.size(); i++)
235 emitGlobalConstant(cast<Constant>(constValues[i].get()));
236 }
237 return;
238 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
239 // Print the fields in successive locations. Pad to align if needed!
240 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
241 const std::vector<Use>& constValues = CVS->getValues();
242 unsigned sizeSoFar = 0;
243 for (unsigned i=0, N = constValues.size(); i < N; i++) {
244 const Constant* field = cast<Constant>(constValues[i].get());
245
246 // Check if padding is needed and insert one or more 0s.
247 unsigned fieldSize = TD.getTypeSize(field->getType());
248 unsigned padSize = ((i == N-1? cvsLayout->StructSize
249 : cvsLayout->MemberOffsets[i+1])
250 - cvsLayout->MemberOffsets[i]) - fieldSize;
251 sizeSoFar += fieldSize + padSize;
252
253 // Now print the actual field value
254 emitGlobalConstant(field);
255
256 // Insert the field padding unless it's zero bytes...
257 if (padSize)
258 O << "\t.space\t " << padSize << "\n";
259 }
260 assert(sizeSoFar == cvsLayout->StructSize &&
261 "Layout of constant struct may be incorrect!");
262 return;
263 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
264 // FP Constants are printed as integer constants to avoid losing
265 // precision...
266 double Val = CFP->getValue();
Misha Brukmand71bd562004-06-21 17:19:08 +0000267 switch (CFP->getType()->getTypeID()) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000268 default: assert(0 && "Unknown floating point type!");
269 case Type::FloatTyID: {
270 union FU { // Abide by C TBAA rules
271 float FVal;
272 unsigned UVal;
273 } U;
274 U.FVal = Val;
Misha Brukman218bec72004-06-29 17:13:26 +0000275 O << ".long\t" << U.UVal << "\t; float " << Val << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000276 return;
277 }
278 case Type::DoubleTyID: {
279 union DU { // Abide by C TBAA rules
280 double FVal;
281 uint64_t UVal;
282 struct {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000283 uint32_t MSWord;
284 uint32_t LSWord;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000285 } T;
286 } U;
287 U.FVal = Val;
288
Misha Brukman218bec72004-06-29 17:13:26 +0000289 O << ".long\t" << U.T.MSWord << "\t; double most significant word "
Misha Brukman46fd00a2004-06-24 23:04:11 +0000290 << Val << "\n";
Misha Brukman29188c62004-07-16 19:01:13 +0000291 O << ".long\t" << U.T.LSWord << "\t; double least significant word "
Misha Brukman46fd00a2004-06-24 23:04:11 +0000292 << Val << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000293 return;
294 }
295 }
296 } else if (CV->getType()->getPrimitiveSize() == 64) {
Misha Brukman2bf183c2004-06-25 15:42:10 +0000297 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
298 union DU { // Abide by C TBAA rules
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000299 int64_t UVal;
300 struct {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000301 uint32_t MSWord;
302 uint32_t LSWord;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000303 } T;
304 } U;
305 U.UVal = CI->getRawValue();
306
Misha Brukman218bec72004-06-29 17:13:26 +0000307 O << ".long\t" << U.T.MSWord << "\t; Double-word most significant word "
Misha Brukman46fd00a2004-06-24 23:04:11 +0000308 << U.UVal << "\n";
Misha Brukman29188c62004-07-16 19:01:13 +0000309 O << ".long\t" << U.T.LSWord << "\t; Double-word least significant word "
Misha Brukman46fd00a2004-06-24 23:04:11 +0000310 << U.UVal << "\n";
311 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000312 }
313 }
314
315 const Type *type = CV->getType();
316 O << "\t";
Misha Brukmand71bd562004-06-21 17:19:08 +0000317 switch (type->getTypeID()) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000318 case Type::UByteTyID: case Type::SByteTyID:
319 O << ".byte";
320 break;
321 case Type::UShortTyID: case Type::ShortTyID:
322 O << ".short";
323 break;
324 case Type::BoolTyID:
325 case Type::PointerTyID:
326 case Type::UIntTyID: case Type::IntTyID:
327 O << ".long";
328 break;
329 case Type::ULongTyID: case Type::LongTyID:
Misha Brukman46fd00a2004-06-24 23:04:11 +0000330 assert (0 && "Should have already output double-word constant.");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000331 case Type::FloatTyID: case Type::DoubleTyID:
332 assert (0 && "Should have already output floating point constant.");
333 default:
Misha Brukman97a296f2004-07-21 20:11:11 +0000334 if (CV == Constant::getNullValue(type)) { // Zero initializer?
335 O << ".space\t" << TD.getTypeSize(type) << "\n";
336 return;
337 }
338 std::cerr << "Can't handle printing: " << *CV;
339 abort();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000340 break;
341 }
342 O << "\t";
343 emitConstantValueOnly(CV);
344 O << "\n";
345}
346
347/// printConstantPool - Print to the current output stream assembly
348/// representations of the constants in the constant pool MCP. This is
349/// used to print out constants which have been "spilled to memory" by
350/// the code generator.
351///
352void Printer::printConstantPool(MachineConstantPool *MCP) {
353 const std::vector<Constant*> &CP = MCP->getConstants();
354 const TargetData &TD = TM.getTargetData();
355
356 if (CP.empty()) return;
357
358 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
359 O << "\t.const\n";
360 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
361 << "\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000362 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t;"
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000363 << *CP[i] << "\n";
364 emitGlobalConstant(CP[i]);
365 }
366}
367
368/// runOnMachineFunction - This uses the printMachineInstruction()
369/// method to print assembly for each instruction.
370///
371bool Printer::runOnMachineFunction(MachineFunction &MF) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000372 O << "\n\n";
373 // What's my mangled name?
374 CurrentFnName = Mang->getValueName(MF.getFunction());
375
376 // Print out constants referenced by the function
377 printConstantPool(MF.getConstantPool());
378
379 // Print out labels for the function.
380 O << "\t.text\n";
381 O << "\t.globl\t" << CurrentFnName << "\n";
Misha Brukman61297ee2004-06-29 23:40:57 +0000382 O << "\t.align 2\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000383 O << CurrentFnName << ":\n";
384
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000385 // Print out code for the function.
386 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
387 I != E; ++I) {
388 // Print a label for the basic block.
Misha Brukman218bec72004-06-29 17:13:26 +0000389 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t; "
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000390 << I->getBasicBlock()->getName() << "\n";
391 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukman46fd00a2004-06-24 23:04:11 +0000392 II != E; ++II) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000393 // Print the assembly for the instruction.
394 O << "\t";
395 printMachineInstruction(II);
396 }
397 }
Misha Brukmancf8d2442004-07-26 16:28:33 +0000398 ++LabelNumber;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000399
400 // We didn't modify anything.
401 return false;
402}
403
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000404void Printer::printOp(const MachineOperand &MO,
Misha Brukman46fd00a2004-06-24 23:04:11 +0000405 bool elideOffsetKeyword /* = false */) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000406 const MRegisterInfo &RI = *TM.getRegisterInfo();
407 int new_symbol;
408
409 switch (MO.getType()) {
410 case MachineOperand::MO_VirtualRegister:
411 if (Value *V = MO.getVRegValueOrNull()) {
412 O << "<" << V->getName() << ">";
413 return;
414 }
415 // FALLTHROUGH
416 case MachineOperand::MO_MachineRegister:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000417 case MachineOperand::MO_CCRegister:
Misha Brukman7f484a52004-06-24 23:51:00 +0000418 O << LowercaseString(RI.get(MO.getReg()).Name);
419 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000420
421 case MachineOperand::MO_SignExtendedImmed:
Misha Brukman97a296f2004-07-21 20:11:11 +0000422 O << (short)MO.getImmedValue();
423 return;
424
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000425 case MachineOperand::MO_UnextendedImmed:
Misha Brukman97a296f2004-07-21 20:11:11 +0000426 O << (unsigned short)MO.getImmedValue();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000427 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000428
429 case MachineOperand::MO_PCRelativeDisp:
430 std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
431 abort();
432 return;
433
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000434 case MachineOperand::MO_MachineBasicBlock: {
435 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
436 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
Misha Brukman218bec72004-06-29 17:13:26 +0000437 << "_" << MBBOp->getNumber() << "\t; "
Misha Brukman2bf183c2004-06-25 15:42:10 +0000438 << MBBOp->getBasicBlock()->getName();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000439 return;
440 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000441
442 case MachineOperand::MO_ConstantPoolIndex:
443 O << ".CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000444 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000445
446 case MachineOperand::MO_ExternalSymbol:
447 O << MO.getSymbolName();
448 return;
449
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000450 case MachineOperand::MO_GlobalAddress:
451 if (!elideOffsetKeyword) {
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000452 GlobalValue *GV = MO.getGlobal();
453 std::string Name = Mang->getValueName(GV);
Misha Brukmane2eceb52004-07-23 16:08:20 +0000454
Misha Brukmana6e58b32004-06-28 18:03:37 +0000455 // Dynamically-resolved functions need a stub for the function
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000456 Function *F = dyn_cast<Function>(GV);
Misha Brukmane2eceb52004-07-23 16:08:20 +0000457 if (F && F->isExternal() &&
458 TM.CalledFunctions.find(F) != TM.CalledFunctions.end()) {
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000459 FnStubs.insert(Name);
460 O << "L" << Name << "$stub";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000461 return;
Misha Brukman46fd00a2004-06-24 23:04:11 +0000462 }
Misha Brukmane2eceb52004-07-23 16:08:20 +0000463
464 // External global variables need a non-lazily-resolved stub
Misha Brukman1245c352004-07-23 21:43:26 +0000465 if (!GV->hasInternalLinkage() &&
Misha Brukmane2eceb52004-07-23 16:08:20 +0000466 TM.AddressTaken.find(GV) != TM.AddressTaken.end()) {
467 GVStubs.insert(Name);
468 O << "L" << Name << "$non_lazy_ptr";
469 return;
470 }
471
472 O << Mang->getValueName(GV);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000473 }
474 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000475
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000476 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000477 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000478 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000479 }
480}
481
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000482/// printMachineInstruction -- Print out a single PPC32 LLVM instruction
483/// MI in Darwin syntax to the current output stream.
484///
485void Printer::printMachineInstruction(const MachineInstr *MI) {
486 unsigned Opcode = MI->getOpcode();
487 const TargetInstrInfo &TII = *TM.getInstrInfo();
488 const TargetInstrDescriptor &Desc = TII.get(Opcode);
489 unsigned int i;
Misha Brukmanc6cc10f2004-06-25 19:24:52 +0000490
Misha Brukmanb9e8f972004-06-30 21:54:12 +0000491 unsigned int ArgCount = MI->getNumOperands();
492 //Desc.TSFlags & PPC32II::ArgCountMask;
Misha Brukman22e12072004-06-25 15:11:34 +0000493 unsigned int ArgType[] = {
494 (Desc.TSFlags >> PPC32II::Arg0TypeShift) & PPC32II::ArgTypeMask,
495 (Desc.TSFlags >> PPC32II::Arg1TypeShift) & PPC32II::ArgTypeMask,
496 (Desc.TSFlags >> PPC32II::Arg2TypeShift) & PPC32II::ArgTypeMask,
497 (Desc.TSFlags >> PPC32II::Arg3TypeShift) & PPC32II::ArgTypeMask,
498 (Desc.TSFlags >> PPC32II::Arg4TypeShift) & PPC32II::ArgTypeMask
499 };
Misha Brukman7f484a52004-06-24 23:51:00 +0000500 assert(((Desc.TSFlags & PPC32II::VMX) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000501 "Instruction requires VMX support");
Misha Brukman7f484a52004-06-24 23:51:00 +0000502 assert(((Desc.TSFlags & PPC32II::PPC64) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000503 "Instruction requires 64 bit support");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000504 ++EmittedInsts;
505
Misha Brukman61114612004-07-20 00:42:19 +0000506 // CALLpcrel and CALLindirect are handled specially here to print only the
507 // appropriate number of args that the assembler expects. This is because
508 // may have many arguments appended to record the uses of registers that are
509 // holding arguments to the called function.
Misha Brukmanab967902004-07-27 18:40:39 +0000510 if (Opcode == PPC32::COND_BRANCH) {
511 std::cerr << "Error: untranslated conditional branch psuedo instruction!\n";
512 abort();
513 } else if (Opcode == PPC32::IMPLICIT_DEF) {
Misha Brukman29188c62004-07-16 19:01:13 +0000514 O << "; IMPLICIT DEF ";
515 printOp(MI->getOperand(0));
516 O << "\n";
517 return;
Misha Brukman61114612004-07-20 00:42:19 +0000518 } else if (Opcode == PPC32::CALLpcrel) {
519 O << TII.getName(MI->getOpcode()) << " ";
520 printOp(MI->getOperand(0));
521 O << "\n";
522 return;
523 } else if (Opcode == PPC32::CALLindirect) {
524 O << TII.getName(MI->getOpcode()) << " ";
525 printOp(MI->getOperand(0));
526 O << ", ";
527 printOp(MI->getOperand(1));
528 O << "\n";
529 return;
530 } else if (Opcode == PPC32::MovePCtoLR) {
531 // FIXME: should probably be converted to cout.width and cout.fill
Misha Brukmancf8d2442004-07-26 16:28:33 +0000532 O << "bl \"L0000" << LabelNumber << "$pb\"\n";
533 O << "\"L0000" << LabelNumber << "$pb\":\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000534 O << "\tmflr ";
535 printOp(MI->getOperand(0));
Misha Brukman218bec72004-06-29 17:13:26 +0000536 O << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000537 return;
538 }
539
540 O << TII.getName(MI->getOpcode()) << " ";
Misha Brukmane48178e2004-07-20 15:45:27 +0000541 if (Opcode == PPC32::LOADLoDirect || Opcode == PPC32::LOADLoIndirect) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000542 printOp(MI->getOperand(0));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000543 O << ", lo16(";
544 printOp(MI->getOperand(2));
Misha Brukmancf8d2442004-07-26 16:28:33 +0000545 O << "-\"L0000" << LabelNumber << "$pb\")";
Misha Brukman218bec72004-06-29 17:13:26 +0000546 O << "(";
547 if (MI->getOperand(1).getReg() == PPC32::R0)
548 O << "0";
549 else
550 printOp(MI->getOperand(1));
551 O << ")\n";
Misha Brukmanc6cc10f2004-06-25 19:24:52 +0000552 } else if (Opcode == PPC32::LOADHiAddr) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000553 printOp(MI->getOperand(0));
554 O << ", ";
Misha Brukman218bec72004-06-29 17:13:26 +0000555 if (MI->getOperand(1).getReg() == PPC32::R0)
556 O << "0";
557 else
558 printOp(MI->getOperand(1));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000559 O << ", ha16(" ;
560 printOp(MI->getOperand(2));
Misha Brukmancf8d2442004-07-26 16:28:33 +0000561 O << "-\"L0000" << LabelNumber << "$pb\")\n";
Misha Brukmanc6cc10f2004-06-25 19:24:52 +0000562 } else if (ArgCount == 3 && ArgType[1] == PPC32II::Disimm16) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000563 printOp(MI->getOperand(0));
564 O << ", ";
565 printOp(MI->getOperand(1));
566 O << "(";
Misha Brukmanb9e8f972004-06-30 21:54:12 +0000567 if (MI->getOperand(2).hasAllocatedReg() &&
568 MI->getOperand(2).getReg() == PPC32::R0)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000569 O << "0";
570 else
571 printOp(MI->getOperand(2));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000572 O << ")\n";
573 } else {
Misha Brukman7f484a52004-06-24 23:51:00 +0000574 for (i = 0; i < ArgCount; ++i) {
Misha Brukmanab967902004-07-27 18:40:39 +0000575 // addi and friends
Misha Brukman218bec72004-06-29 17:13:26 +0000576 if (i == 1 && ArgCount == 3 && ArgType[2] == PPC32II::Simm16 &&
Misha Brukman4363bdb2004-07-01 21:09:12 +0000577 MI->getOperand(1).hasAllocatedReg() &&
Misha Brukman218bec72004-06-29 17:13:26 +0000578 MI->getOperand(1).getReg() == PPC32::R0) {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000579 O << "0";
Misha Brukmanab967902004-07-27 18:40:39 +0000580 // for long branch support, bc $+8
581 } else if (i == 1 && ArgCount == 2 && MI->getOperand(1).isImmediate() &&
582 TII.isBranch(MI->getOpcode())) {
583 O << "$+8";
584 assert(8 == MI->getOperand(i).getImmedValue()
585 && "branch off PC not to pc+8?");
586 //printOp(MI->getOperand(i));
Misha Brukman218bec72004-06-29 17:13:26 +0000587 } else {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000588 printOp(MI->getOperand(i));
589 }
Misha Brukman7f484a52004-06-24 23:51:00 +0000590 if (ArgCount - 1 == i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000591 O << "\n";
592 else
593 O << ", ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000594 }
595 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000596}
597
598bool Printer::doInitialization(Module &M) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000599 Mang = new Mangler(M, true);
600 return false; // success
601}
602
603// SwitchSection - Switch to the specified section of the executable if we are
604// not already in it!
605//
606static void SwitchSection(std::ostream &OS, std::string &CurSection,
607 const char *NewSection) {
608 if (CurSection != NewSection) {
609 CurSection = NewSection;
610 if (!CurSection.empty())
611 OS << "\t" << NewSection << "\n";
612 }
613}
614
615bool Printer::doFinalization(Module &M) {
616 const TargetData &TD = TM.getTargetData();
617 std::string CurSection;
618
619 // Print out module-level global variables here.
620 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
621 if (I->hasInitializer()) { // External global require no code
622 O << "\n\n";
623 std::string name = Mang->getValueName(I);
624 Constant *C = I->getInitializer();
625 unsigned Size = TD.getTypeSize(C->getType());
626 unsigned Align = TD.getTypeAlignment(C->getType());
627
Misha Brukman97a296f2004-07-21 20:11:11 +0000628 if (C->isNullValue() && /* FIXME: Verify correct */
629 (I->hasInternalLinkage() || I->hasWeakLinkage())) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000630 SwitchSection(O, CurSection, ".data");
631 if (I->hasInternalLinkage())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000632 O << ".lcomm " << name << "," << TD.getTypeSize(C->getType())
Misha Brukman218bec72004-06-29 17:13:26 +0000633 << "," << (unsigned)TD.getTypeAlignment(C->getType());
634 else
Misha Brukmane2eceb52004-07-23 16:08:20 +0000635 O << ".comm " << name << "," << TD.getTypeSize(C->getType());
Misha Brukman218bec72004-06-29 17:13:26 +0000636 O << "\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000637 WriteAsOperand(O, I, true, true, &M);
638 O << "\n";
639 } else {
640 switch (I->getLinkage()) {
641 case GlobalValue::LinkOnceLinkage:
Misha Brukman97a296f2004-07-21 20:11:11 +0000642 O << ".section __TEXT,__textcoal_nt,coalesced,no_toc\n"
643 << ".weak_definition " << name << '\n'
644 << ".private_extern " << name << '\n'
645 << ".section __DATA,__datacoal_nt,coalesced,no_toc\n";
646 LinkOnceStubs.insert(name);
647 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000648 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
649 // Nonnull linkonce -> weak
650 O << "\t.weak " << name << "\n";
651 SwitchSection(O, CurSection, "");
652 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
653 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000654 case GlobalValue::AppendingLinkage:
655 // FIXME: appending linkage variables should go into a section of
656 // their name or something. For now, just emit them as external.
657 case GlobalValue::ExternalLinkage:
658 // If external or appending, declare as a global symbol
659 O << "\t.globl " << name << "\n";
660 // FALL THROUGH
661 case GlobalValue::InternalLinkage:
Misha Brukman61297ee2004-06-29 23:40:57 +0000662 SwitchSection(O, CurSection, ".data");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000663 break;
664 }
665
666 O << "\t.align " << Align << "\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000667 O << name << ":\t\t\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000668 WriteAsOperand(O, I, true, true, &M);
669 O << " = ";
670 WriteAsOperand(O, C, false, false, &M);
671 O << "\n";
672 emitGlobalConstant(C);
673 }
674 }
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000675
Misha Brukman97a296f2004-07-21 20:11:11 +0000676 // Output stubs for link-once variables
677 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
678 O << ".data\n.align 2\n";
679 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
Misha Brukmane2eceb52004-07-23 16:08:20 +0000680 e = LinkOnceStubs.end(); i != e; ++i) {
Misha Brukman97a296f2004-07-21 20:11:11 +0000681 O << *i << "$non_lazy_ptr:\n"
682 << "\t.long\t" << *i << '\n';
683 }
684
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000685 // Output stubs for dynamically-linked functions
686 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
687 i != e; ++i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000688 {
Misha Brukmane2eceb52004-07-23 16:08:20 +0000689 O << ".data\n";
690 O << ".section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n";
691 O << "\t.align 2\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000692 O << "L" << *i << "$stub:\n";
693 O << "\t.indirect_symbol " << *i << "\n";
694 O << "\tmflr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000695 O << "\tbcl 20,31,L0$" << *i << "\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000696 O << "L0$" << *i << ":\n";
697 O << "\tmflr r11\n";
698 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
699 O << "\tmtlr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000700 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000701 O << "\tmtctr r12\n";
702 O << "\tbctr\n";
703 O << ".data\n";
704 O << ".lazy_symbol_pointer\n";
705 O << "L" << *i << "$lazy_ptr:\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000706 O << "\t.indirect_symbol " << *i << "\n";
707 O << "\t.long dyld_stub_binding_helper\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000708 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000709
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000710 O << "\n";
711
712 // Output stubs for external global variables
713 if (GVStubs.begin() != GVStubs.end())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000714 O << ".data\n.non_lazy_symbol_pointer\n";
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000715 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
716 i != e; ++i) {
717 O << "L" << *i << "$non_lazy_ptr:\n";
718 O << "\t.indirect_symbol " << *i << "\n";
719 O << "\t.long\t0\n";
720 }
721
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000722 delete Mang;
723 return false; // success
724}
725
726} // End llvm namespace