blob: feec9ed5ceb94a0f5e9197192aef66aebe49544e [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 Brukmane2eceb52004-07-23 16:08:20 +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 Brukman218bec72004-06-29 17:13:26 +000068 /// Unique incrementer for label values for referencing
69 /// Global values.
70 ///
71 unsigned int labelNumber;
72
Misha Brukman5dfe3a92004-06-21 16:55:25 +000073 virtual const char *getPassName() const {
74 return "PowerPC Assembly Printer";
75 }
76
77 void printMachineInstruction(const MachineInstr *MI);
Misha Brukmanbb4a9082004-06-28 15:53:27 +000078 void printOp(const MachineOperand &MO, bool elideOffsetKeyword = false);
Misha Brukman5dfe3a92004-06-21 16:55:25 +000079 void printConstantPool(MachineConstantPool *MCP);
80 bool runOnMachineFunction(MachineFunction &F);
81 bool doInitialization(Module &M);
82 bool doFinalization(Module &M);
83 void emitGlobalConstant(const Constant* CV);
84 void emitConstantValueOnly(const Constant *CV);
85 };
86} // end of anonymous namespace
87
Misha Brukman218bec72004-06-29 17:13:26 +000088/// createPPCCodePrinterPass - Returns a pass that prints the PPC
Misha Brukman5dfe3a92004-06-21 16:55:25 +000089/// assembly code for a MachineFunction to the given output stream,
90/// using the given target machine description. This should work
91/// regardless of whether the function is in SSA form.
92///
Misha Brukmanbb4a9082004-06-28 15:53:27 +000093FunctionPass *createPPCCodePrinterPass(std::ostream &o,TargetMachine &tm) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +000094 return new Printer(o, tm);
95}
96
97/// isStringCompatible - Can we treat the specified array as a string?
98/// Only if it is an array of ubytes or non-negative sbytes.
99///
100static bool isStringCompatible(const ConstantArray *CVA) {
101 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
102 if (ETy == Type::UByteTy) return true;
103 if (ETy != Type::SByteTy) return false;
104
105 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
106 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
107 return false;
108
109 return true;
110}
111
112/// toOctal - Convert the low order bits of X into an octal digit.
113///
114static inline char toOctal(int X) {
115 return (X&7)+'0';
116}
117
118/// getAsCString - Return the specified array as a C compatible
119/// string, only if the predicate isStringCompatible is true.
120///
121static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
122 assert(isStringCompatible(CVA) && "Array is not string compatible!");
123
124 O << "\"";
125 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
126 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
127
128 if (C == '"') {
129 O << "\\\"";
130 } else if (C == '\\') {
131 O << "\\\\";
132 } else if (isprint(C)) {
133 O << C;
134 } else {
Misha Brukmane2eceb52004-07-23 16:08:20 +0000135 switch (C) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000136 case '\b': O << "\\b"; break;
137 case '\f': O << "\\f"; break;
138 case '\n': O << "\\n"; break;
139 case '\r': O << "\\r"; break;
140 case '\t': O << "\\t"; break;
141 default:
142 O << '\\';
143 O << toOctal(C >> 6);
144 O << toOctal(C >> 3);
145 O << toOctal(C >> 0);
146 break;
147 }
148 }
149 }
150 O << "\"";
151}
152
153// Print out the specified constant, without a storage class. Only the
154// constants valid in constant expressions can occur here.
155void Printer::emitConstantValueOnly(const Constant *CV) {
156 if (CV->isNullValue())
157 O << "0";
158 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
159 assert(CB == ConstantBool::True);
160 O << "1";
161 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
162 O << CI->getValue();
163 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
164 O << CI->getValue();
Chris Lattner67910e12004-07-18 07:29:35 +0000165 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000166 // This is a constant address for a global variable or function. Use the
167 // name of the variable or function as the address value.
Chris Lattner67910e12004-07-18 07:29:35 +0000168 O << Mang->getValueName(GV);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000169 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
170 const TargetData &TD = TM.getTargetData();
Misha Brukmane2eceb52004-07-23 16:08:20 +0000171 switch (CE->getOpcode()) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000172 case Instruction::GetElementPtr: {
173 // generate a symbolic expression for the byte address
174 const Constant *ptrVal = CE->getOperand(0);
175 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
176 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
177 O << "(";
178 emitConstantValueOnly(ptrVal);
179 O << ") + " << Offset;
180 } else {
181 emitConstantValueOnly(ptrVal);
182 }
183 break;
184 }
185 case Instruction::Cast: {
186 // Support only non-converting or widening casts for now, that is, ones
187 // that do not involve a change in value. This assertion is really gross,
188 // and may not even be a complete check.
189 Constant *Op = CE->getOperand(0);
190 const Type *OpTy = Op->getType(), *Ty = CE->getType();
191
192 // Remember, kids, pointers on x86 can be losslessly converted back and
193 // forth into 32-bit or wider integers, regardless of signedness. :-P
194 assert(((isa<PointerType>(OpTy)
195 && (Ty == Type::LongTy || Ty == Type::ULongTy
196 || Ty == Type::IntTy || Ty == Type::UIntTy))
197 || (isa<PointerType>(Ty)
198 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
199 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
200 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
201 && OpTy->isLosslesslyConvertibleTo(Ty))))
202 && "FIXME: Don't yet support this kind of constant cast expr");
203 O << "(";
204 emitConstantValueOnly(Op);
205 O << ")";
206 break;
207 }
208 case Instruction::Add:
209 O << "(";
210 emitConstantValueOnly(CE->getOperand(0));
211 O << ") + (";
212 emitConstantValueOnly(CE->getOperand(1));
213 O << ")";
214 break;
215 default:
216 assert(0 && "Unsupported operator!");
217 }
218 } else {
219 assert(0 && "Unknown constant value!");
220 }
221}
222
223// Print a constant value or values, with the appropriate storage class as a
224// prefix.
225void Printer::emitGlobalConstant(const Constant *CV) {
226 const TargetData &TD = TM.getTargetData();
227
Misha Brukmane48178e2004-07-20 15:45:27 +0000228 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000229 if (isStringCompatible(CVA)) {
Misha Brukman218bec72004-06-29 17:13:26 +0000230 O << "\t.ascii ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000231 printAsCString(O, CVA);
232 O << "\n";
233 } else { // Not a string. Print the values in successive locations
234 const std::vector<Use> &constValues = CVA->getValues();
235 for (unsigned i=0; i < constValues.size(); i++)
236 emitGlobalConstant(cast<Constant>(constValues[i].get()));
237 }
238 return;
239 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
240 // Print the fields in successive locations. Pad to align if needed!
241 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
242 const std::vector<Use>& constValues = CVS->getValues();
243 unsigned sizeSoFar = 0;
244 for (unsigned i=0, N = constValues.size(); i < N; i++) {
245 const Constant* field = cast<Constant>(constValues[i].get());
246
247 // Check if padding is needed and insert one or more 0s.
248 unsigned fieldSize = TD.getTypeSize(field->getType());
249 unsigned padSize = ((i == N-1? cvsLayout->StructSize
250 : cvsLayout->MemberOffsets[i+1])
251 - cvsLayout->MemberOffsets[i]) - fieldSize;
252 sizeSoFar += fieldSize + padSize;
253
254 // Now print the actual field value
255 emitGlobalConstant(field);
256
257 // Insert the field padding unless it's zero bytes...
258 if (padSize)
259 O << "\t.space\t " << padSize << "\n";
260 }
261 assert(sizeSoFar == cvsLayout->StructSize &&
262 "Layout of constant struct may be incorrect!");
263 return;
264 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
265 // FP Constants are printed as integer constants to avoid losing
266 // precision...
267 double Val = CFP->getValue();
Misha Brukmand71bd562004-06-21 17:19:08 +0000268 switch (CFP->getType()->getTypeID()) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000269 default: assert(0 && "Unknown floating point type!");
270 case Type::FloatTyID: {
271 union FU { // Abide by C TBAA rules
272 float FVal;
273 unsigned UVal;
274 } U;
275 U.FVal = Val;
Misha Brukman218bec72004-06-29 17:13:26 +0000276 O << ".long\t" << U.UVal << "\t; float " << Val << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000277 return;
278 }
279 case Type::DoubleTyID: {
280 union DU { // Abide by C TBAA rules
281 double FVal;
282 uint64_t UVal;
283 struct {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000284 uint32_t MSWord;
285 uint32_t LSWord;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000286 } T;
287 } U;
288 U.FVal = Val;
289
Misha Brukman218bec72004-06-29 17:13:26 +0000290 O << ".long\t" << U.T.MSWord << "\t; double most significant word "
Misha Brukman46fd00a2004-06-24 23:04:11 +0000291 << Val << "\n";
Misha Brukman29188c62004-07-16 19:01:13 +0000292 O << ".long\t" << U.T.LSWord << "\t; double least significant word "
Misha Brukman46fd00a2004-06-24 23:04:11 +0000293 << Val << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000294 return;
295 }
296 }
297 } else if (CV->getType()->getPrimitiveSize() == 64) {
Misha Brukman2bf183c2004-06-25 15:42:10 +0000298 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
299 union DU { // Abide by C TBAA rules
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000300 int64_t UVal;
301 struct {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000302 uint32_t MSWord;
303 uint32_t LSWord;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000304 } T;
305 } U;
306 U.UVal = CI->getRawValue();
307
Misha Brukman218bec72004-06-29 17:13:26 +0000308 O << ".long\t" << U.T.MSWord << "\t; Double-word most significant word "
Misha Brukman46fd00a2004-06-24 23:04:11 +0000309 << U.UVal << "\n";
Misha Brukman29188c62004-07-16 19:01:13 +0000310 O << ".long\t" << U.T.LSWord << "\t; Double-word least significant word "
Misha Brukman46fd00a2004-06-24 23:04:11 +0000311 << U.UVal << "\n";
312 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000313 }
314 }
315
316 const Type *type = CV->getType();
317 O << "\t";
Misha Brukmand71bd562004-06-21 17:19:08 +0000318 switch (type->getTypeID()) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000319 case Type::UByteTyID: case Type::SByteTyID:
320 O << ".byte";
321 break;
322 case Type::UShortTyID: case Type::ShortTyID:
323 O << ".short";
324 break;
325 case Type::BoolTyID:
326 case Type::PointerTyID:
327 case Type::UIntTyID: case Type::IntTyID:
328 O << ".long";
329 break;
330 case Type::ULongTyID: case Type::LongTyID:
Misha Brukman46fd00a2004-06-24 23:04:11 +0000331 assert (0 && "Should have already output double-word constant.");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000332 case Type::FloatTyID: case Type::DoubleTyID:
333 assert (0 && "Should have already output floating point constant.");
334 default:
Misha Brukman97a296f2004-07-21 20:11:11 +0000335 if (CV == Constant::getNullValue(type)) { // Zero initializer?
336 O << ".space\t" << TD.getTypeSize(type) << "\n";
337 return;
338 }
339 std::cerr << "Can't handle printing: " << *CV;
340 abort();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000341 break;
342 }
343 O << "\t";
344 emitConstantValueOnly(CV);
345 O << "\n";
346}
347
348/// printConstantPool - Print to the current output stream assembly
349/// representations of the constants in the constant pool MCP. This is
350/// used to print out constants which have been "spilled to memory" by
351/// the code generator.
352///
353void Printer::printConstantPool(MachineConstantPool *MCP) {
354 const std::vector<Constant*> &CP = MCP->getConstants();
355 const TargetData &TD = TM.getTargetData();
356
357 if (CP.empty()) return;
358
359 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
360 O << "\t.const\n";
361 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
362 << "\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000363 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t;"
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000364 << *CP[i] << "\n";
365 emitGlobalConstant(CP[i]);
366 }
367}
368
369/// runOnMachineFunction - This uses the printMachineInstruction()
370/// method to print assembly for each instruction.
371///
372bool Printer::runOnMachineFunction(MachineFunction &MF) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000373 O << "\n\n";
374 // What's my mangled name?
375 CurrentFnName = Mang->getValueName(MF.getFunction());
376
377 // Print out constants referenced by the function
378 printConstantPool(MF.getConstantPool());
379
380 // Print out labels for the function.
381 O << "\t.text\n";
382 O << "\t.globl\t" << CurrentFnName << "\n";
Misha Brukman61297ee2004-06-29 23:40:57 +0000383 O << "\t.align 2\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000384 O << CurrentFnName << ":\n";
385
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000386 // Print out code for the function.
387 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
388 I != E; ++I) {
389 // Print a label for the basic block.
Misha Brukman218bec72004-06-29 17:13:26 +0000390 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t; "
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000391 << I->getBasicBlock()->getName() << "\n";
392 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukman46fd00a2004-06-24 23:04:11 +0000393 II != E; ++II) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000394 // Print the assembly for the instruction.
395 O << "\t";
396 printMachineInstruction(II);
397 }
398 }
399
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
465 if (GV->hasInternalLinkage() == false &&
466 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 Brukman29188c62004-07-16 19:01:13 +0000510 if (Opcode == PPC32::IMPLICIT_DEF) {
511 O << "; IMPLICIT DEF ";
512 printOp(MI->getOperand(0));
513 O << "\n";
514 return;
Misha Brukman61114612004-07-20 00:42:19 +0000515 } else if (Opcode == PPC32::CALLpcrel) {
516 O << TII.getName(MI->getOpcode()) << " ";
517 printOp(MI->getOperand(0));
518 O << "\n";
519 return;
520 } else if (Opcode == PPC32::CALLindirect) {
521 O << TII.getName(MI->getOpcode()) << " ";
522 printOp(MI->getOperand(0));
523 O << ", ";
524 printOp(MI->getOperand(1));
525 O << "\n";
526 return;
527 } else if (Opcode == PPC32::MovePCtoLR) {
528 // FIXME: should probably be converted to cout.width and cout.fill
Misha Brukman4cf51122004-07-06 22:40:34 +0000529 O << "bl \"L0000" << labelNumber << "$pb\"\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000530 O << "\"L0000" << labelNumber << "$pb\":\n";
531 O << "\tmflr ";
532 printOp(MI->getOperand(0));
Misha Brukman218bec72004-06-29 17:13:26 +0000533 O << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000534 return;
535 }
536
537 O << TII.getName(MI->getOpcode()) << " ";
Misha Brukmane48178e2004-07-20 15:45:27 +0000538 if (Opcode == PPC32::LOADLoDirect || Opcode == PPC32::LOADLoIndirect) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000539 printOp(MI->getOperand(0));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000540 O << ", lo16(";
541 printOp(MI->getOperand(2));
Misha Brukman218bec72004-06-29 17:13:26 +0000542 O << "-\"L0000" << labelNumber << "$pb\")";
543 labelNumber++;
544 O << "(";
545 if (MI->getOperand(1).getReg() == PPC32::R0)
546 O << "0";
547 else
548 printOp(MI->getOperand(1));
549 O << ")\n";
Misha Brukmanc6cc10f2004-06-25 19:24:52 +0000550 } else if (Opcode == PPC32::LOADHiAddr) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000551 printOp(MI->getOperand(0));
552 O << ", ";
Misha Brukman218bec72004-06-29 17:13:26 +0000553 if (MI->getOperand(1).getReg() == PPC32::R0)
554 O << "0";
555 else
556 printOp(MI->getOperand(1));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000557 O << ", ha16(" ;
558 printOp(MI->getOperand(2));
Misha Brukman218bec72004-06-29 17:13:26 +0000559 O << "-\"L0000" << labelNumber << "$pb\")\n";
Misha Brukmanc6cc10f2004-06-25 19:24:52 +0000560 } else if (ArgCount == 3 && ArgType[1] == PPC32II::Disimm16) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000561 printOp(MI->getOperand(0));
562 O << ", ";
563 printOp(MI->getOperand(1));
564 O << "(";
Misha Brukmanb9e8f972004-06-30 21:54:12 +0000565 if (MI->getOperand(2).hasAllocatedReg() &&
566 MI->getOperand(2).getReg() == PPC32::R0)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000567 O << "0";
568 else
569 printOp(MI->getOperand(2));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000570 O << ")\n";
571 } else {
Misha Brukman7f484a52004-06-24 23:51:00 +0000572 for (i = 0; i < ArgCount; ++i) {
Misha Brukman218bec72004-06-29 17:13:26 +0000573 if (i == 1 && ArgCount == 3 && ArgType[2] == PPC32II::Simm16 &&
Misha Brukman4363bdb2004-07-01 21:09:12 +0000574 MI->getOperand(1).hasAllocatedReg() &&
Misha Brukman218bec72004-06-29 17:13:26 +0000575 MI->getOperand(1).getReg() == PPC32::R0) {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000576 O << "0";
Misha Brukman218bec72004-06-29 17:13:26 +0000577 } else {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000578 printOp(MI->getOperand(i));
579 }
Misha Brukman7f484a52004-06-24 23:51:00 +0000580 if (ArgCount - 1 == i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000581 O << "\n";
582 else
583 O << ", ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000584 }
585 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000586}
587
588bool Printer::doInitialization(Module &M) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000589 Mang = new Mangler(M, true);
590 return false; // success
591}
592
593// SwitchSection - Switch to the specified section of the executable if we are
594// not already in it!
595//
596static void SwitchSection(std::ostream &OS, std::string &CurSection,
597 const char *NewSection) {
598 if (CurSection != NewSection) {
599 CurSection = NewSection;
600 if (!CurSection.empty())
601 OS << "\t" << NewSection << "\n";
602 }
603}
604
605bool Printer::doFinalization(Module &M) {
606 const TargetData &TD = TM.getTargetData();
607 std::string CurSection;
608
609 // Print out module-level global variables here.
610 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
611 if (I->hasInitializer()) { // External global require no code
612 O << "\n\n";
613 std::string name = Mang->getValueName(I);
614 Constant *C = I->getInitializer();
615 unsigned Size = TD.getTypeSize(C->getType());
616 unsigned Align = TD.getTypeAlignment(C->getType());
617
Misha Brukman97a296f2004-07-21 20:11:11 +0000618 if (C->isNullValue() && /* FIXME: Verify correct */
619 (I->hasInternalLinkage() || I->hasWeakLinkage())) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000620 SwitchSection(O, CurSection, ".data");
621 if (I->hasInternalLinkage())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000622 O << ".lcomm " << name << "," << TD.getTypeSize(C->getType())
Misha Brukman218bec72004-06-29 17:13:26 +0000623 << "," << (unsigned)TD.getTypeAlignment(C->getType());
624 else
Misha Brukmane2eceb52004-07-23 16:08:20 +0000625 O << ".comm " << name << "," << TD.getTypeSize(C->getType());
Misha Brukman218bec72004-06-29 17:13:26 +0000626 O << "\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000627 WriteAsOperand(O, I, true, true, &M);
628 O << "\n";
629 } else {
630 switch (I->getLinkage()) {
631 case GlobalValue::LinkOnceLinkage:
Misha Brukman97a296f2004-07-21 20:11:11 +0000632 O << ".section __TEXT,__textcoal_nt,coalesced,no_toc\n"
633 << ".weak_definition " << name << '\n'
634 << ".private_extern " << name << '\n'
635 << ".section __DATA,__datacoal_nt,coalesced,no_toc\n";
636 LinkOnceStubs.insert(name);
637 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000638 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
639 // Nonnull linkonce -> weak
640 O << "\t.weak " << name << "\n";
641 SwitchSection(O, CurSection, "");
642 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
643 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000644 case GlobalValue::AppendingLinkage:
645 // FIXME: appending linkage variables should go into a section of
646 // their name or something. For now, just emit them as external.
647 case GlobalValue::ExternalLinkage:
648 // If external or appending, declare as a global symbol
649 O << "\t.globl " << name << "\n";
650 // FALL THROUGH
651 case GlobalValue::InternalLinkage:
Misha Brukman61297ee2004-06-29 23:40:57 +0000652 SwitchSection(O, CurSection, ".data");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000653 break;
654 }
655
656 O << "\t.align " << Align << "\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000657 O << name << ":\t\t\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000658 WriteAsOperand(O, I, true, true, &M);
659 O << " = ";
660 WriteAsOperand(O, C, false, false, &M);
661 O << "\n";
662 emitGlobalConstant(C);
663 }
664 }
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000665
Misha Brukman97a296f2004-07-21 20:11:11 +0000666 // Output stubs for link-once variables
667 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
668 O << ".data\n.align 2\n";
669 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
Misha Brukmane2eceb52004-07-23 16:08:20 +0000670 e = LinkOnceStubs.end(); i != e; ++i) {
Misha Brukman97a296f2004-07-21 20:11:11 +0000671 O << *i << "$non_lazy_ptr:\n"
672 << "\t.long\t" << *i << '\n';
673 }
674
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000675 // Output stubs for dynamically-linked functions
676 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
677 i != e; ++i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000678 {
Misha Brukmane2eceb52004-07-23 16:08:20 +0000679 O << ".data\n";
680 O << ".section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n";
681 O << "\t.align 2\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000682 O << "L" << *i << "$stub:\n";
683 O << "\t.indirect_symbol " << *i << "\n";
684 O << "\tmflr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000685 O << "\tbcl 20,31,L0$" << *i << "\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000686 O << "L0$" << *i << ":\n";
687 O << "\tmflr r11\n";
688 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
689 O << "\tmtlr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000690 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000691 O << "\tmtctr r12\n";
692 O << "\tbctr\n";
693 O << ".data\n";
694 O << ".lazy_symbol_pointer\n";
695 O << "L" << *i << "$lazy_ptr:\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000696 O << "\t.indirect_symbol " << *i << "\n";
697 O << "\t.long dyld_stub_binding_helper\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000698 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000699
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000700 O << "\n";
701
702 // Output stubs for external global variables
703 if (GVStubs.begin() != GVStubs.end())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000704 O << ".data\n.non_lazy_symbol_pointer\n";
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000705 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
706 i != e; ++i) {
707 O << "L" << *i << "$non_lazy_ptr:\n";
708 O << "\t.indirect_symbol " << *i << "\n";
709 O << "\t.long\t0\n";
710 }
711
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000712 delete Mang;
713 return false; // success
714}
715
716} // End llvm namespace