blob: 8a4e59bf1d6436dff74b6f669026ae692dfd179e [file] [log] [blame]
Misha Brukman5dfe3a92004-06-21 16:55:25 +00001//===-- PPC32/Printer.cpp - Convert X86 LLVM code to Intel assembly ---------===//
2//
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//
10// This file contains a printer that converts from our internal
11// representation of machine-dependent LLVM code to Intel-format
12// assembly language. This printer is the output mechanism used
13// by `llc' and `lli -print-machineinstrs' on X86.
14//
15//===----------------------------------------------------------------------===//
16
Misha Brukman05794492004-06-24 17:31:42 +000017#define DEBUG_TYPE "asmprinter"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000018#include "PowerPC.h"
19#include "PowerPCInstrInfo.h"
20#include "llvm/Constants.h"
21#include "llvm/DerivedTypes.h"
22#include "llvm/Module.h"
23#include "llvm/Assembly/Writer.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000024#include "llvm/CodeGen/MachineConstantPool.h"
Misha Brukman05794492004-06-24 17:31:42 +000025#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000026#include "llvm/CodeGen/MachineInstr.h"
27#include "llvm/Target/TargetMachine.h"
28#include "llvm/Support/Mangler.h"
Misha Brukman05794492004-06-24 17:31:42 +000029#include "Support/CommandLine.h"
30#include "Support/Debug.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000031#include "Support/Statistic.h"
32#include "Support/StringExtras.h"
Misha Brukman05794492004-06-24 17:31:42 +000033#include <set>
Misha Brukman5dfe3a92004-06-21 16:55:25 +000034
35namespace llvm {
36
37namespace {
38 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
39
40 struct Printer : public MachineFunctionPass {
41 /// Output stream on which we're printing assembly code.
42 ///
43 std::ostream &O;
44
45 /// Target machine description which we query for reg. names, data
46 /// layout, etc.
47 ///
48 TargetMachine &TM;
49
50 /// Name-mangler for global names.
51 ///
52 Mangler *Mang;
53 std::set< std::string > Stubs;
54 std::set<std::string> Strings;
55
56 Printer(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { }
57
58 /// We name each basic block in a Function with a unique number, so
59 /// that we can consistently refer to them later. This is cleared
60 /// at the beginning of each call to runOnMachineFunction().
61 ///
62 typedef std::map<const Value *, unsigned> ValueMapTy;
63 ValueMapTy NumberForBB;
64
65 /// Cache of mangled name for current function. This is
66 /// recalculated at the beginning of each call to
67 /// runOnMachineFunction().
68 ///
69 std::string CurrentFnName;
70
71 virtual const char *getPassName() const {
72 return "PowerPC Assembly Printer";
73 }
74
75 void printMachineInstruction(const MachineInstr *MI);
76 void printOp(const MachineOperand &MO,
Misha Brukman46fd00a2004-06-24 23:04:11 +000077 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
87/// createPPCCodePrinterPass - Returns a pass that prints the X86
88/// 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///
92FunctionPass *createPPCCodePrinterPass(std::ostream &o,TargetMachine &tm){
93 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 {
134 switch(C) {
135 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();
164 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
165 // This is a constant address for a global variable or function. Use the
166 // name of the variable or function as the address value.
167 O << Mang->getValueName(CPR->getValue());
168 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
169 const TargetData &TD = TM.getTargetData();
170 switch(CE->getOpcode()) {
171 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
227 if (CV->isNullValue()) {
228 O << "\t.space\t " << TD.getTypeSize(CV->getType()) << "\n";
229 return;
230 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
231 if (isStringCompatible(CVA)) {
232 O << ".ascii";
233 printAsCString(O, CVA);
234 O << "\n";
235 } else { // Not a string. Print the values in successive locations
236 const std::vector<Use> &constValues = CVA->getValues();
237 for (unsigned i=0; i < constValues.size(); i++)
238 emitGlobalConstant(cast<Constant>(constValues[i].get()));
239 }
240 return;
241 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
242 // Print the fields in successive locations. Pad to align if needed!
243 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
244 const std::vector<Use>& constValues = CVS->getValues();
245 unsigned sizeSoFar = 0;
246 for (unsigned i=0, N = constValues.size(); i < N; i++) {
247 const Constant* field = cast<Constant>(constValues[i].get());
248
249 // Check if padding is needed and insert one or more 0s.
250 unsigned fieldSize = TD.getTypeSize(field->getType());
251 unsigned padSize = ((i == N-1? cvsLayout->StructSize
252 : cvsLayout->MemberOffsets[i+1])
253 - cvsLayout->MemberOffsets[i]) - fieldSize;
254 sizeSoFar += fieldSize + padSize;
255
256 // Now print the actual field value
257 emitGlobalConstant(field);
258
259 // Insert the field padding unless it's zero bytes...
260 if (padSize)
261 O << "\t.space\t " << padSize << "\n";
262 }
263 assert(sizeSoFar == cvsLayout->StructSize &&
264 "Layout of constant struct may be incorrect!");
265 return;
266 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
267 // FP Constants are printed as integer constants to avoid losing
268 // precision...
269 double Val = CFP->getValue();
Misha Brukmand71bd562004-06-21 17:19:08 +0000270 switch (CFP->getType()->getTypeID()) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000271 default: assert(0 && "Unknown floating point type!");
272 case Type::FloatTyID: {
273 union FU { // Abide by C TBAA rules
274 float FVal;
275 unsigned UVal;
276 } U;
277 U.FVal = Val;
278 O << ".long\t" << U.UVal << "\t# float " << Val << "\n";
279 return;
280 }
281 case Type::DoubleTyID: {
282 union DU { // Abide by C TBAA rules
283 double FVal;
284 uint64_t UVal;
285 struct {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000286 uint32_t MSWord;
287 uint32_t LSWord;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000288 } T;
289 } U;
290 U.FVal = Val;
291
Misha Brukman46fd00a2004-06-24 23:04:11 +0000292 O << ".long\t" << U.T.MSWord << "\t# double most significant word "
293 << Val << "\n";
294 O << ".long\t" << U.T.LSWord << "\t# double least significant word"
295 << Val << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000296 return;
297 }
298 }
299 } else if (CV->getType()->getPrimitiveSize() == 64) {
300 const ConstantInt *CI = dyn_cast<ConstantInt>(CV);
301 if(CI) {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000302 union DU { // Abide by C TBAA rules
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000303 int64_t UVal;
304 struct {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000305 uint32_t MSWord;
306 uint32_t LSWord;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000307 } T;
308 } U;
309 U.UVal = CI->getRawValue();
310
Misha Brukman46fd00a2004-06-24 23:04:11 +0000311 O << ".long\t" << U.T.MSWord << "\t# Double-word most significant word "
312 << U.UVal << "\n";
313 O << ".long\t" << U.T.LSWord << "\t# Double-word least significant word"
314 << U.UVal << "\n";
315 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000316 }
317 }
318
319 const Type *type = CV->getType();
320 O << "\t";
Misha Brukmand71bd562004-06-21 17:19:08 +0000321 switch (type->getTypeID()) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000322 case Type::UByteTyID: case Type::SByteTyID:
323 O << ".byte";
324 break;
325 case Type::UShortTyID: case Type::ShortTyID:
326 O << ".short";
327 break;
328 case Type::BoolTyID:
329 case Type::PointerTyID:
330 case Type::UIntTyID: case Type::IntTyID:
331 O << ".long";
332 break;
333 case Type::ULongTyID: case Type::LongTyID:
Misha Brukman46fd00a2004-06-24 23:04:11 +0000334 assert (0 && "Should have already output double-word constant.");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000335 case Type::FloatTyID: case Type::DoubleTyID:
336 assert (0 && "Should have already output floating point constant.");
337 default:
338 assert (0 && "Can't handle printing this type of thing");
339 break;
340 }
341 O << "\t";
342 emitConstantValueOnly(CV);
343 O << "\n";
344}
345
346/// printConstantPool - Print to the current output stream assembly
347/// representations of the constants in the constant pool MCP. This is
348/// used to print out constants which have been "spilled to memory" by
349/// the code generator.
350///
351void Printer::printConstantPool(MachineConstantPool *MCP) {
352 const std::vector<Constant*> &CP = MCP->getConstants();
353 const TargetData &TD = TM.getTargetData();
354
355 if (CP.empty()) return;
356
357 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
358 O << "\t.const\n";
359 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
360 << "\n";
361 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
362 << *CP[i] << "\n";
363 emitGlobalConstant(CP[i]);
364 }
365}
366
367/// runOnMachineFunction - This uses the printMachineInstruction()
368/// method to print assembly for each instruction.
369///
370bool Printer::runOnMachineFunction(MachineFunction &MF) {
371 // BBNumber is used here so that a given Printer will never give two
372 // BBs the same name. (If you have a better way, please let me know!)
373 static unsigned BBNumber = 0;
374
375 O << "\n\n";
376 // What's my mangled name?
377 CurrentFnName = Mang->getValueName(MF.getFunction());
378
379 // Print out constants referenced by the function
380 printConstantPool(MF.getConstantPool());
381
382 // Print out labels for the function.
383 O << "\t.text\n";
384 O << "\t.globl\t" << CurrentFnName << "\n";
385 O << "\t.align 5\n";
386 O << CurrentFnName << ":\n";
387
388 // Number each basic block so that we can consistently refer to them
389 // in PC-relative references.
390 NumberForBB.clear();
391 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
392 I != E; ++I) {
393 NumberForBB[I->getBasicBlock()] = BBNumber++;
394 }
395
396 // Print out code for the function.
397 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
398 I != E; ++I) {
399 // Print a label for the basic block.
400 O << "L" << NumberForBB[I->getBasicBlock()] << ":\t# "
401 << I->getBasicBlock()->getName() << "\n";
402 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukman46fd00a2004-06-24 23:04:11 +0000403 II != E; ++II) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000404 // Print the assembly for the instruction.
405 O << "\t";
406 printMachineInstruction(II);
407 }
408 }
409
410 // We didn't modify anything.
411 return false;
412}
413
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000414void Printer::printOp(const MachineOperand &MO,
Misha Brukman46fd00a2004-06-24 23:04:11 +0000415 bool elideOffsetKeyword /* = false */) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000416 const MRegisterInfo &RI = *TM.getRegisterInfo();
417 int new_symbol;
418
419 switch (MO.getType()) {
420 case MachineOperand::MO_VirtualRegister:
421 if (Value *V = MO.getVRegValueOrNull()) {
422 O << "<" << V->getName() << ">";
423 return;
424 }
425 // FALLTHROUGH
426 case MachineOperand::MO_MachineRegister:
Misha Brukman7f484a52004-06-24 23:51:00 +0000427 O << LowercaseString(RI.get(MO.getReg()).Name);
428 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000429
430 case MachineOperand::MO_SignExtendedImmed:
431 case MachineOperand::MO_UnextendedImmed:
432 O << (int)MO.getImmedValue();
433 return;
434 case MachineOperand::MO_MachineBasicBlock: {
435 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
436 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
437 << "_" << MBBOp->getNumber () << "\t# "
438 << MBBOp->getBasicBlock ()->getName ();
439 return;
440 }
441 case MachineOperand::MO_PCRelativeDisp:
442 std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
443 abort ();
444 return;
445 case MachineOperand::MO_GlobalAddress:
446 if (!elideOffsetKeyword) {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000447 if(isa<Function>(MO.getGlobal())) {
448 Stubs.insert(Mang->getValueName(MO.getGlobal()));
449 O << "L" << Mang->getValueName(MO.getGlobal()) << "$stub";
450 } else {
451 O << Mang->getValueName(MO.getGlobal());
452 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000453 }
454 return;
455 case MachineOperand::MO_ExternalSymbol:
456 O << MO.getSymbolName();
457 return;
458 default:
Misha Brukman22e12072004-06-25 15:11:34 +0000459 O << "<unknown operand type>";
460 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000461 }
462}
463
464#if 0
465static inline
466unsigned int ValidOpcodes(const MachineInstr *MI, unsigned int ArgType[5]) {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000467 int i;
468 unsigned int retval = 1;
469
470 for(i = 0; i<5; i++) {
471 switch(ArgType[i]) {
472 case none:
473 break;
474 case Gpr:
475 case Gpr0:
476 Type::UIntTy
477 case Simm16:
478 case Zimm16:
479 case PCRelimm24:
480 case Imm24:
481 case Imm5:
482 case PCRelimm14:
483 case Imm14:
484 case Imm2:
485 case Crf:
486 case Imm3:
487 case Imm1:
488 case Fpr:
489 case Imm4:
490 case Imm8:
491 case Disimm16:
492 case Spr:
493 case Sgr:
494 };
495
496 }
497 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000498}
499#endif
500
501/// printMachineInstruction -- Print out a single PPC32 LLVM instruction
502/// MI in Darwin syntax to the current output stream.
503///
504void Printer::printMachineInstruction(const MachineInstr *MI) {
505 unsigned Opcode = MI->getOpcode();
506 const TargetInstrInfo &TII = *TM.getInstrInfo();
507 const TargetInstrDescriptor &Desc = TII.get(Opcode);
508 unsigned int i;
509
510 unsigned int ArgCount = Desc.TSFlags & PPC32II::ArgCountMask;
Misha Brukman22e12072004-06-25 15:11:34 +0000511 unsigned int ArgType[] = {
512 (Desc.TSFlags >> PPC32II::Arg0TypeShift) & PPC32II::ArgTypeMask,
513 (Desc.TSFlags >> PPC32II::Arg1TypeShift) & PPC32II::ArgTypeMask,
514 (Desc.TSFlags >> PPC32II::Arg2TypeShift) & PPC32II::ArgTypeMask,
515 (Desc.TSFlags >> PPC32II::Arg3TypeShift) & PPC32II::ArgTypeMask,
516 (Desc.TSFlags >> PPC32II::Arg4TypeShift) & PPC32II::ArgTypeMask
517 };
Misha Brukman7f484a52004-06-24 23:51:00 +0000518 assert(((Desc.TSFlags & PPC32II::VMX) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000519 "Instruction requires VMX support");
Misha Brukman7f484a52004-06-24 23:51:00 +0000520 assert(((Desc.TSFlags & PPC32II::PPC64) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000521 "Instruction requires 64 bit support");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000522 //assert ( ValidOpcodes(MI, ArgType) && "Instruction has invalid inputs");
523 ++EmittedInsts;
524
Misha Brukman46fd00a2004-06-24 23:04:11 +0000525 if (Opcode == PPC32::MovePCtoLR) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000526 O << "mflr r0\n";
527 O << "bcl 20,31,L" << CurrentFnName << "$pb\n";
528 O << "L" << CurrentFnName << "$pb:\n";
529 return;
530 }
531
532 O << TII.getName(MI->getOpcode()) << " ";
Misha Brukman05794492004-06-24 17:31:42 +0000533 DEBUG(std::cerr << TII.getName(MI->getOpcode()) << " expects "
534 << ArgCount << " args\n");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000535
Misha Brukman46fd00a2004-06-24 23:04:11 +0000536 if (Opcode == PPC32::LOADLoAddr) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000537 printOp(MI->getOperand(0));
538 O << ", ";
539 printOp(MI->getOperand(1));
540 O << ", lo16(";
541 printOp(MI->getOperand(2));
542 O << "-L" << CurrentFnName << "$pb)\n";
543 return;
544 }
545
Misha Brukman46fd00a2004-06-24 23:04:11 +0000546 if (Opcode == PPC32::LOADHiAddr) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000547 printOp(MI->getOperand(0));
548 O << ", ";
549 printOp(MI->getOperand(1));
550 O << ", ha16(" ;
551 printOp(MI->getOperand(2));
552 O << "-L" << CurrentFnName << "$pb)\n";
553 return;
554 }
555
Misha Brukman46fd00a2004-06-24 23:04:11 +0000556 if (ArgCount == 3 && ArgType[1] == PPC32II::Disimm16) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000557 printOp(MI->getOperand(0));
558 O << ", ";
559 printOp(MI->getOperand(1));
560 O << "(";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000561 if (ArgType[2] == PPC32II::Gpr0 && MI->getOperand(2).getReg() == PPC32::R0)
562 O << "0";
563 else
564 printOp(MI->getOperand(2));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000565 O << ")\n";
566 } else {
Misha Brukman7f484a52004-06-24 23:51:00 +0000567 for (i = 0; i < ArgCount; ++i) {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000568 if (ArgType[i] == PPC32II::Gpr0 &&
569 MI->getOperand(i).getReg() == PPC32::R0)
570 O << "0";
571 else {
572 //std::cout << "DEBUG " << (*(TM.getRegisterInfo())).get(MI->getOperand(i).getReg()).Name << "\n";
573 printOp(MI->getOperand(i));
574 }
Misha Brukman7f484a52004-06-24 23:51:00 +0000575 if (ArgCount - 1 == i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000576 O << "\n";
577 else
578 O << ", ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000579 }
580 }
581
582 return;
583}
584
585bool Printer::doInitialization(Module &M) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000586 Mang = new Mangler(M, true);
587 return false; // success
588}
589
590// SwitchSection - Switch to the specified section of the executable if we are
591// not already in it!
592//
593static void SwitchSection(std::ostream &OS, std::string &CurSection,
594 const char *NewSection) {
595 if (CurSection != NewSection) {
596 CurSection = NewSection;
597 if (!CurSection.empty())
598 OS << "\t" << NewSection << "\n";
599 }
600}
601
602bool Printer::doFinalization(Module &M) {
603 const TargetData &TD = TM.getTargetData();
604 std::string CurSection;
605
606 // Print out module-level global variables here.
607 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
608 if (I->hasInitializer()) { // External global require no code
609 O << "\n\n";
610 std::string name = Mang->getValueName(I);
611 Constant *C = I->getInitializer();
612 unsigned Size = TD.getTypeSize(C->getType());
613 unsigned Align = TD.getTypeAlignment(C->getType());
614
615 if (C->isNullValue() &&
616 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
617 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
618 SwitchSection(O, CurSection, ".data");
619 if (I->hasInternalLinkage())
620 O << "\t.local " << name << "\n";
621
622 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
623 << "," << (unsigned)TD.getTypeAlignment(C->getType());
624 O << "\t\t# ";
625 WriteAsOperand(O, I, true, true, &M);
626 O << "\n";
627 } else {
628 switch (I->getLinkage()) {
629 case GlobalValue::LinkOnceLinkage:
630 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
631 // Nonnull linkonce -> weak
632 O << "\t.weak " << name << "\n";
633 SwitchSection(O, CurSection, "");
634 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
635 break;
636
637 case GlobalValue::AppendingLinkage:
638 // FIXME: appending linkage variables should go into a section of
639 // their name or something. For now, just emit them as external.
640 case GlobalValue::ExternalLinkage:
641 // If external or appending, declare as a global symbol
642 O << "\t.globl " << name << "\n";
643 // FALL THROUGH
644 case GlobalValue::InternalLinkage:
645 if (C->isNullValue())
646 SwitchSection(O, CurSection, ".bss");
647 else
648 SwitchSection(O, CurSection, ".data");
649 break;
650 }
651
652 O << "\t.align " << Align << "\n";
653 O << name << ":\t\t\t\t# ";
654 WriteAsOperand(O, I, true, true, &M);
655 O << " = ";
656 WriteAsOperand(O, C, false, false, &M);
657 O << "\n";
658 emitGlobalConstant(C);
659 }
660 }
661
Misha Brukman46fd00a2004-06-24 23:04:11 +0000662 for(std::set<std::string>::iterator i = Stubs.begin(); i != Stubs.end(); ++i)
663 {
664 O << ".data\n";
665 O<<".section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n";
666 O << "\t.align 2\n";
667 O << "L" << *i << "$stub:\n";
668 O << "\t.indirect_symbol " << *i << "\n";
669 O << "\tmflr r0\n";
670 O << "\tbcl 20,31,L0$" << *i << "\n";
671 O << "L0$" << *i << ":\n";
672 O << "\tmflr r11\n";
673 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
674 O << "\tmtlr r0\n";
675 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
676 O << "\tmtctr r12\n";
677 O << "\tbctr\n";
678 O << ".data\n";
679 O << ".lazy_symbol_pointer\n";
680 O << "L" << *i << "$lazy_ptr:\n";
681 O << ".indirect_symbol " << *i << "\n";
682 O << ".long dyld_stub_binding_helper\n";
683 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000684
685 delete Mang;
686 return false; // success
687}
688
689} // End llvm namespace