blob: d778550b5722e9b3d1a2300e502aa444e033a36d [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
Misha Brukman5dfe3a92004-06-21 16:55:25 +000058 /// Cache of mangled name for current function. This is
59 /// recalculated at the beginning of each call to
60 /// runOnMachineFunction().
61 ///
62 std::string CurrentFnName;
63
64 virtual const char *getPassName() const {
65 return "PowerPC Assembly Printer";
66 }
67
68 void printMachineInstruction(const MachineInstr *MI);
69 void printOp(const MachineOperand &MO,
Misha Brukman46fd00a2004-06-24 23:04:11 +000070 bool elideOffsetKeyword = false);
Misha Brukman5dfe3a92004-06-21 16:55:25 +000071 void printConstantPool(MachineConstantPool *MCP);
72 bool runOnMachineFunction(MachineFunction &F);
73 bool doInitialization(Module &M);
74 bool doFinalization(Module &M);
75 void emitGlobalConstant(const Constant* CV);
76 void emitConstantValueOnly(const Constant *CV);
77 };
78} // end of anonymous namespace
79
80/// createPPCCodePrinterPass - Returns a pass that prints the X86
81/// assembly code for a MachineFunction to the given output stream,
82/// using the given target machine description. This should work
83/// regardless of whether the function is in SSA form.
84///
85FunctionPass *createPPCCodePrinterPass(std::ostream &o,TargetMachine &tm){
86 return new Printer(o, tm);
87}
88
89/// isStringCompatible - Can we treat the specified array as a string?
90/// Only if it is an array of ubytes or non-negative sbytes.
91///
92static bool isStringCompatible(const ConstantArray *CVA) {
93 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
94 if (ETy == Type::UByteTy) return true;
95 if (ETy != Type::SByteTy) return false;
96
97 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
98 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
99 return false;
100
101 return true;
102}
103
104/// toOctal - Convert the low order bits of X into an octal digit.
105///
106static inline char toOctal(int X) {
107 return (X&7)+'0';
108}
109
110/// getAsCString - Return the specified array as a C compatible
111/// string, only if the predicate isStringCompatible is true.
112///
113static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
114 assert(isStringCompatible(CVA) && "Array is not string compatible!");
115
116 O << "\"";
117 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
118 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
119
120 if (C == '"') {
121 O << "\\\"";
122 } else if (C == '\\') {
123 O << "\\\\";
124 } else if (isprint(C)) {
125 O << C;
126 } else {
127 switch(C) {
128 case '\b': O << "\\b"; break;
129 case '\f': O << "\\f"; break;
130 case '\n': O << "\\n"; break;
131 case '\r': O << "\\r"; break;
132 case '\t': O << "\\t"; break;
133 default:
134 O << '\\';
135 O << toOctal(C >> 6);
136 O << toOctal(C >> 3);
137 O << toOctal(C >> 0);
138 break;
139 }
140 }
141 }
142 O << "\"";
143}
144
145// Print out the specified constant, without a storage class. Only the
146// constants valid in constant expressions can occur here.
147void Printer::emitConstantValueOnly(const Constant *CV) {
148 if (CV->isNullValue())
149 O << "0";
150 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
151 assert(CB == ConstantBool::True);
152 O << "1";
153 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
154 O << CI->getValue();
155 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
156 O << CI->getValue();
157 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
158 // This is a constant address for a global variable or function. Use the
159 // name of the variable or function as the address value.
160 O << Mang->getValueName(CPR->getValue());
161 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
162 const TargetData &TD = TM.getTargetData();
163 switch(CE->getOpcode()) {
164 case Instruction::GetElementPtr: {
165 // generate a symbolic expression for the byte address
166 const Constant *ptrVal = CE->getOperand(0);
167 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
168 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
169 O << "(";
170 emitConstantValueOnly(ptrVal);
171 O << ") + " << Offset;
172 } else {
173 emitConstantValueOnly(ptrVal);
174 }
175 break;
176 }
177 case Instruction::Cast: {
178 // Support only non-converting or widening casts for now, that is, ones
179 // that do not involve a change in value. This assertion is really gross,
180 // and may not even be a complete check.
181 Constant *Op = CE->getOperand(0);
182 const Type *OpTy = Op->getType(), *Ty = CE->getType();
183
184 // Remember, kids, pointers on x86 can be losslessly converted back and
185 // forth into 32-bit or wider integers, regardless of signedness. :-P
186 assert(((isa<PointerType>(OpTy)
187 && (Ty == Type::LongTy || Ty == Type::ULongTy
188 || Ty == Type::IntTy || Ty == Type::UIntTy))
189 || (isa<PointerType>(Ty)
190 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
191 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
192 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
193 && OpTy->isLosslesslyConvertibleTo(Ty))))
194 && "FIXME: Don't yet support this kind of constant cast expr");
195 O << "(";
196 emitConstantValueOnly(Op);
197 O << ")";
198 break;
199 }
200 case Instruction::Add:
201 O << "(";
202 emitConstantValueOnly(CE->getOperand(0));
203 O << ") + (";
204 emitConstantValueOnly(CE->getOperand(1));
205 O << ")";
206 break;
207 default:
208 assert(0 && "Unsupported operator!");
209 }
210 } else {
211 assert(0 && "Unknown constant value!");
212 }
213}
214
215// Print a constant value or values, with the appropriate storage class as a
216// prefix.
217void Printer::emitGlobalConstant(const Constant *CV) {
218 const TargetData &TD = TM.getTargetData();
219
220 if (CV->isNullValue()) {
221 O << "\t.space\t " << TD.getTypeSize(CV->getType()) << "\n";
222 return;
223 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
224 if (isStringCompatible(CVA)) {
225 O << ".ascii";
226 printAsCString(O, CVA);
227 O << "\n";
228 } else { // Not a string. Print the values in successive locations
229 const std::vector<Use> &constValues = CVA->getValues();
230 for (unsigned i=0; i < constValues.size(); i++)
231 emitGlobalConstant(cast<Constant>(constValues[i].get()));
232 }
233 return;
234 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
235 // Print the fields in successive locations. Pad to align if needed!
236 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
237 const std::vector<Use>& constValues = CVS->getValues();
238 unsigned sizeSoFar = 0;
239 for (unsigned i=0, N = constValues.size(); i < N; i++) {
240 const Constant* field = cast<Constant>(constValues[i].get());
241
242 // Check if padding is needed and insert one or more 0s.
243 unsigned fieldSize = TD.getTypeSize(field->getType());
244 unsigned padSize = ((i == N-1? cvsLayout->StructSize
245 : cvsLayout->MemberOffsets[i+1])
246 - cvsLayout->MemberOffsets[i]) - fieldSize;
247 sizeSoFar += fieldSize + padSize;
248
249 // Now print the actual field value
250 emitGlobalConstant(field);
251
252 // Insert the field padding unless it's zero bytes...
253 if (padSize)
254 O << "\t.space\t " << padSize << "\n";
255 }
256 assert(sizeSoFar == cvsLayout->StructSize &&
257 "Layout of constant struct may be incorrect!");
258 return;
259 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
260 // FP Constants are printed as integer constants to avoid losing
261 // precision...
262 double Val = CFP->getValue();
Misha Brukmand71bd562004-06-21 17:19:08 +0000263 switch (CFP->getType()->getTypeID()) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000264 default: assert(0 && "Unknown floating point type!");
265 case Type::FloatTyID: {
266 union FU { // Abide by C TBAA rules
267 float FVal;
268 unsigned UVal;
269 } U;
270 U.FVal = Val;
271 O << ".long\t" << U.UVal << "\t# float " << Val << "\n";
272 return;
273 }
274 case Type::DoubleTyID: {
275 union DU { // Abide by C TBAA rules
276 double FVal;
277 uint64_t UVal;
278 struct {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000279 uint32_t MSWord;
280 uint32_t LSWord;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000281 } T;
282 } U;
283 U.FVal = Val;
284
Misha Brukman46fd00a2004-06-24 23:04:11 +0000285 O << ".long\t" << U.T.MSWord << "\t# double most significant word "
286 << Val << "\n";
287 O << ".long\t" << U.T.LSWord << "\t# double least significant word"
288 << Val << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000289 return;
290 }
291 }
292 } else if (CV->getType()->getPrimitiveSize() == 64) {
Misha Brukman2bf183c2004-06-25 15:42:10 +0000293 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
294 union DU { // Abide by C TBAA rules
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000295 int64_t UVal;
296 struct {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000297 uint32_t MSWord;
298 uint32_t LSWord;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000299 } T;
300 } U;
301 U.UVal = CI->getRawValue();
302
Misha Brukman46fd00a2004-06-24 23:04:11 +0000303 O << ".long\t" << U.T.MSWord << "\t# Double-word most significant word "
304 << U.UVal << "\n";
305 O << ".long\t" << U.T.LSWord << "\t# Double-word least significant word"
306 << U.UVal << "\n";
307 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000308 }
309 }
310
311 const Type *type = CV->getType();
312 O << "\t";
Misha Brukmand71bd562004-06-21 17:19:08 +0000313 switch (type->getTypeID()) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000314 case Type::UByteTyID: case Type::SByteTyID:
315 O << ".byte";
316 break;
317 case Type::UShortTyID: case Type::ShortTyID:
318 O << ".short";
319 break;
320 case Type::BoolTyID:
321 case Type::PointerTyID:
322 case Type::UIntTyID: case Type::IntTyID:
323 O << ".long";
324 break;
325 case Type::ULongTyID: case Type::LongTyID:
Misha Brukman46fd00a2004-06-24 23:04:11 +0000326 assert (0 && "Should have already output double-word constant.");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000327 case Type::FloatTyID: case Type::DoubleTyID:
328 assert (0 && "Should have already output floating point constant.");
329 default:
330 assert (0 && "Can't handle printing this type of thing");
331 break;
332 }
333 O << "\t";
334 emitConstantValueOnly(CV);
335 O << "\n";
336}
337
338/// printConstantPool - Print to the current output stream assembly
339/// representations of the constants in the constant pool MCP. This is
340/// used to print out constants which have been "spilled to memory" by
341/// the code generator.
342///
343void Printer::printConstantPool(MachineConstantPool *MCP) {
344 const std::vector<Constant*> &CP = MCP->getConstants();
345 const TargetData &TD = TM.getTargetData();
346
347 if (CP.empty()) return;
348
349 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
350 O << "\t.const\n";
351 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
352 << "\n";
353 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
354 << *CP[i] << "\n";
355 emitGlobalConstant(CP[i]);
356 }
357}
358
359/// runOnMachineFunction - This uses the printMachineInstruction()
360/// method to print assembly for each instruction.
361///
362bool Printer::runOnMachineFunction(MachineFunction &MF) {
363 // BBNumber is used here so that a given Printer will never give two
364 // BBs the same name. (If you have a better way, please let me know!)
365 static unsigned BBNumber = 0;
366
367 O << "\n\n";
368 // What's my mangled name?
369 CurrentFnName = Mang->getValueName(MF.getFunction());
370
371 // Print out constants referenced by the function
372 printConstantPool(MF.getConstantPool());
373
374 // Print out labels for the function.
375 O << "\t.text\n";
376 O << "\t.globl\t" << CurrentFnName << "\n";
377 O << "\t.align 5\n";
378 O << CurrentFnName << ":\n";
379
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000380 // Print out code for the function.
381 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
382 I != E; ++I) {
383 // Print a label for the basic block.
Misha Brukman2bf183c2004-06-25 15:42:10 +0000384 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t# "
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000385 << I->getBasicBlock()->getName() << "\n";
386 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukman46fd00a2004-06-24 23:04:11 +0000387 II != E; ++II) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000388 // Print the assembly for the instruction.
389 O << "\t";
390 printMachineInstruction(II);
391 }
392 }
393
394 // We didn't modify anything.
395 return false;
396}
397
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000398void Printer::printOp(const MachineOperand &MO,
Misha Brukman46fd00a2004-06-24 23:04:11 +0000399 bool elideOffsetKeyword /* = false */) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000400 const MRegisterInfo &RI = *TM.getRegisterInfo();
401 int new_symbol;
402
403 switch (MO.getType()) {
404 case MachineOperand::MO_VirtualRegister:
405 if (Value *V = MO.getVRegValueOrNull()) {
406 O << "<" << V->getName() << ">";
407 return;
408 }
409 // FALLTHROUGH
410 case MachineOperand::MO_MachineRegister:
Misha Brukman7f484a52004-06-24 23:51:00 +0000411 O << LowercaseString(RI.get(MO.getReg()).Name);
412 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000413
414 case MachineOperand::MO_SignExtendedImmed:
415 case MachineOperand::MO_UnextendedImmed:
416 O << (int)MO.getImmedValue();
417 return;
418 case MachineOperand::MO_MachineBasicBlock: {
419 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
420 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
Misha Brukman2bf183c2004-06-25 15:42:10 +0000421 << "_" << MBBOp->getNumber() << "\t# "
422 << MBBOp->getBasicBlock()->getName();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000423 return;
424 }
425 case MachineOperand::MO_PCRelativeDisp:
426 std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
427 abort ();
428 return;
429 case MachineOperand::MO_GlobalAddress:
430 if (!elideOffsetKeyword) {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000431 if(isa<Function>(MO.getGlobal())) {
432 Stubs.insert(Mang->getValueName(MO.getGlobal()));
433 O << "L" << Mang->getValueName(MO.getGlobal()) << "$stub";
434 } else {
435 O << Mang->getValueName(MO.getGlobal());
436 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000437 }
438 return;
439 case MachineOperand::MO_ExternalSymbol:
440 O << MO.getSymbolName();
441 return;
442 default:
Misha Brukman22e12072004-06-25 15:11:34 +0000443 O << "<unknown operand type>";
444 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000445 }
446}
447
448#if 0
449static inline
450unsigned int ValidOpcodes(const MachineInstr *MI, unsigned int ArgType[5]) {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000451 int i;
452 unsigned int retval = 1;
453
454 for(i = 0; i<5; i++) {
455 switch(ArgType[i]) {
456 case none:
457 break;
458 case Gpr:
459 case Gpr0:
460 Type::UIntTy
461 case Simm16:
462 case Zimm16:
463 case PCRelimm24:
464 case Imm24:
465 case Imm5:
466 case PCRelimm14:
467 case Imm14:
468 case Imm2:
469 case Crf:
470 case Imm3:
471 case Imm1:
472 case Fpr:
473 case Imm4:
474 case Imm8:
475 case Disimm16:
476 case Spr:
477 case Sgr:
478 };
479
480 }
481 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000482}
483#endif
484
485/// printMachineInstruction -- Print out a single PPC32 LLVM instruction
486/// MI in Darwin syntax to the current output stream.
487///
488void Printer::printMachineInstruction(const MachineInstr *MI) {
489 unsigned Opcode = MI->getOpcode();
490 const TargetInstrInfo &TII = *TM.getInstrInfo();
491 const TargetInstrDescriptor &Desc = TII.get(Opcode);
492 unsigned int i;
Misha Brukmanc6cc10f2004-06-25 19:24:52 +0000493
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000494 unsigned int ArgCount = Desc.TSFlags & PPC32II::ArgCountMask;
Misha Brukman22e12072004-06-25 15:11:34 +0000495 unsigned int ArgType[] = {
496 (Desc.TSFlags >> PPC32II::Arg0TypeShift) & PPC32II::ArgTypeMask,
497 (Desc.TSFlags >> PPC32II::Arg1TypeShift) & PPC32II::ArgTypeMask,
498 (Desc.TSFlags >> PPC32II::Arg2TypeShift) & PPC32II::ArgTypeMask,
499 (Desc.TSFlags >> PPC32II::Arg3TypeShift) & PPC32II::ArgTypeMask,
500 (Desc.TSFlags >> PPC32II::Arg4TypeShift) & PPC32II::ArgTypeMask
501 };
Misha Brukman7f484a52004-06-24 23:51:00 +0000502 assert(((Desc.TSFlags & PPC32II::VMX) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000503 "Instruction requires VMX support");
Misha Brukman7f484a52004-06-24 23:51:00 +0000504 assert(((Desc.TSFlags & PPC32II::PPC64) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000505 "Instruction requires 64 bit support");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000506 //assert ( ValidOpcodes(MI, ArgType) && "Instruction has invalid inputs");
507 ++EmittedInsts;
508
Misha Brukman46fd00a2004-06-24 23:04:11 +0000509 if (Opcode == PPC32::MovePCtoLR) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000510 O << "mflr r0\n";
Misha Brukmanc6cc10f2004-06-25 19:24:52 +0000511 O << "\tbcl 20,31,L" << CurrentFnName << "$pb\n";
Misha Brukman2bf183c2004-06-25 15:42:10 +0000512 O << "L" << CurrentFnName << "$pb:\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000513 return;
514 }
515
516 O << TII.getName(MI->getOpcode()) << " ";
Misha Brukman05794492004-06-24 17:31:42 +0000517 DEBUG(std::cerr << TII.getName(MI->getOpcode()) << " expects "
518 << ArgCount << " args\n");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000519
Misha Brukman46fd00a2004-06-24 23:04:11 +0000520 if (Opcode == PPC32::LOADLoAddr) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000521 printOp(MI->getOperand(0));
522 O << ", ";
523 printOp(MI->getOperand(1));
524 O << ", lo16(";
525 printOp(MI->getOperand(2));
526 O << "-L" << CurrentFnName << "$pb)\n";
Misha Brukmanc6cc10f2004-06-25 19:24:52 +0000527 } else if (Opcode == PPC32::LOADHiAddr) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000528 printOp(MI->getOperand(0));
529 O << ", ";
530 printOp(MI->getOperand(1));
531 O << ", ha16(" ;
532 printOp(MI->getOperand(2));
533 O << "-L" << CurrentFnName << "$pb)\n";
Misha Brukmanc6cc10f2004-06-25 19:24:52 +0000534 } else if (ArgCount == 3 && ArgType[1] == PPC32II::Disimm16) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000535 printOp(MI->getOperand(0));
536 O << ", ";
537 printOp(MI->getOperand(1));
538 O << "(";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000539 if (ArgType[2] == PPC32II::Gpr0 && MI->getOperand(2).getReg() == PPC32::R0)
540 O << "0";
541 else
542 printOp(MI->getOperand(2));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000543 O << ")\n";
544 } else {
Misha Brukman7f484a52004-06-24 23:51:00 +0000545 for (i = 0; i < ArgCount; ++i) {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000546 if (ArgType[i] == PPC32II::Gpr0 &&
547 MI->getOperand(i).getReg() == PPC32::R0)
548 O << "0";
549 else {
550 //std::cout << "DEBUG " << (*(TM.getRegisterInfo())).get(MI->getOperand(i).getReg()).Name << "\n";
551 printOp(MI->getOperand(i));
552 }
Misha Brukman7f484a52004-06-24 23:51:00 +0000553 if (ArgCount - 1 == i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000554 O << "\n";
555 else
556 O << ", ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000557 }
558 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000559}
560
561bool Printer::doInitialization(Module &M) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000562 Mang = new Mangler(M, true);
563 return false; // success
564}
565
566// SwitchSection - Switch to the specified section of the executable if we are
567// not already in it!
568//
569static void SwitchSection(std::ostream &OS, std::string &CurSection,
570 const char *NewSection) {
571 if (CurSection != NewSection) {
572 CurSection = NewSection;
573 if (!CurSection.empty())
574 OS << "\t" << NewSection << "\n";
575 }
576}
577
578bool Printer::doFinalization(Module &M) {
579 const TargetData &TD = TM.getTargetData();
580 std::string CurSection;
581
582 // Print out module-level global variables here.
583 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
584 if (I->hasInitializer()) { // External global require no code
585 O << "\n\n";
586 std::string name = Mang->getValueName(I);
587 Constant *C = I->getInitializer();
588 unsigned Size = TD.getTypeSize(C->getType());
589 unsigned Align = TD.getTypeAlignment(C->getType());
590
591 if (C->isNullValue() &&
592 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
593 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
594 SwitchSection(O, CurSection, ".data");
595 if (I->hasInternalLinkage())
596 O << "\t.local " << name << "\n";
597
598 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
599 << "," << (unsigned)TD.getTypeAlignment(C->getType());
600 O << "\t\t# ";
601 WriteAsOperand(O, I, true, true, &M);
602 O << "\n";
603 } else {
604 switch (I->getLinkage()) {
605 case GlobalValue::LinkOnceLinkage:
606 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
607 // Nonnull linkonce -> weak
608 O << "\t.weak " << name << "\n";
609 SwitchSection(O, CurSection, "");
610 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
611 break;
612
613 case GlobalValue::AppendingLinkage:
614 // FIXME: appending linkage variables should go into a section of
615 // their name or something. For now, just emit them as external.
616 case GlobalValue::ExternalLinkage:
617 // If external or appending, declare as a global symbol
618 O << "\t.globl " << name << "\n";
619 // FALL THROUGH
620 case GlobalValue::InternalLinkage:
621 if (C->isNullValue())
622 SwitchSection(O, CurSection, ".bss");
623 else
624 SwitchSection(O, CurSection, ".data");
625 break;
626 }
627
628 O << "\t.align " << Align << "\n";
629 O << name << ":\t\t\t\t# ";
630 WriteAsOperand(O, I, true, true, &M);
631 O << " = ";
632 WriteAsOperand(O, C, false, false, &M);
633 O << "\n";
634 emitGlobalConstant(C);
635 }
636 }
637
Misha Brukman46fd00a2004-06-24 23:04:11 +0000638 for(std::set<std::string>::iterator i = Stubs.begin(); i != Stubs.end(); ++i)
639 {
640 O << ".data\n";
641 O<<".section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n";
642 O << "\t.align 2\n";
643 O << "L" << *i << "$stub:\n";
644 O << "\t.indirect_symbol " << *i << "\n";
645 O << "\tmflr r0\n";
646 O << "\tbcl 20,31,L0$" << *i << "\n";
647 O << "L0$" << *i << ":\n";
648 O << "\tmflr r11\n";
649 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
650 O << "\tmtlr r0\n";
651 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
652 O << "\tmtctr r12\n";
653 O << "\tbctr\n";
654 O << ".data\n";
655 O << ".lazy_symbol_pointer\n";
656 O << "L" << *i << "$lazy_ptr:\n";
657 O << ".indirect_symbol " << *i << "\n";
658 O << ".long dyld_stub_binding_helper\n";
659 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000660
661 delete Mang;
662 return false; // success
663}
664
665} // End llvm namespace