blob: b06eb4bcc5915f60c4165dcd1afb7d6ac96be459 [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//
Misha Brukman218bec72004-06-29 17:13:26 +000015// Documentation at
16// http://developer.apple.com/documentation/DeveloperTools/
17// Reference/Assembler/ASMIntroduction/chapter_1_section_1.html
18//
Misha Brukman5dfe3a92004-06-21 16:55:25 +000019//===----------------------------------------------------------------------===//
20
Misha Brukman05794492004-06-24 17:31:42 +000021#define DEBUG_TYPE "asmprinter"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000022#include "PowerPC.h"
23#include "PowerPCInstrInfo.h"
24#include "llvm/Constants.h"
25#include "llvm/DerivedTypes.h"
26#include "llvm/Module.h"
27#include "llvm/Assembly/Writer.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000028#include "llvm/CodeGen/MachineConstantPool.h"
Misha Brukman05794492004-06-24 17:31:42 +000029#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000030#include "llvm/CodeGen/MachineInstr.h"
31#include "llvm/Target/TargetMachine.h"
32#include "llvm/Support/Mangler.h"
Misha Brukman05794492004-06-24 17:31:42 +000033#include "Support/CommandLine.h"
34#include "Support/Debug.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000035#include "Support/Statistic.h"
36#include "Support/StringExtras.h"
Misha Brukman05794492004-06-24 17:31:42 +000037#include <set>
Misha Brukman5dfe3a92004-06-21 16:55:25 +000038
39namespace llvm {
40
41namespace {
42 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
43
44 struct Printer : public MachineFunctionPass {
45 /// Output stream on which we're printing assembly code.
46 ///
47 std::ostream &O;
48
49 /// Target machine description which we query for reg. names, data
50 /// layout, etc.
51 ///
52 TargetMachine &TM;
53
54 /// Name-mangler for global names.
55 ///
56 Mangler *Mang;
Misha Brukmanbb4a9082004-06-28 15:53:27 +000057 std::set<std::string> Stubs;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000058 std::set<std::string> Strings;
59
Misha Brukman218bec72004-06-29 17:13:26 +000060 Printer(std::ostream &o, TargetMachine &tm) : O(o), TM(tm), labelNumber(0)
61 { }
Misha Brukman5dfe3a92004-06-21 16:55:25 +000062
Misha Brukman5dfe3a92004-06-21 16:55:25 +000063 /// Cache of mangled name for current function. This is
64 /// recalculated at the beginning of each call to
65 /// runOnMachineFunction().
66 ///
67 std::string CurrentFnName;
68
Misha Brukman218bec72004-06-29 17:13:26 +000069 /// Unique incrementer for label values for referencing
70 /// Global values.
71 ///
72 unsigned int labelNumber;
73
Misha Brukman5dfe3a92004-06-21 16:55:25 +000074 virtual const char *getPassName() const {
75 return "PowerPC Assembly Printer";
76 }
77
78 void printMachineInstruction(const MachineInstr *MI);
Misha Brukmanbb4a9082004-06-28 15:53:27 +000079 void printOp(const MachineOperand &MO, bool elideOffsetKeyword = false);
Misha Brukman5dfe3a92004-06-21 16:55:25 +000080 void printConstantPool(MachineConstantPool *MCP);
81 bool runOnMachineFunction(MachineFunction &F);
82 bool doInitialization(Module &M);
83 bool doFinalization(Module &M);
84 void emitGlobalConstant(const Constant* CV);
85 void emitConstantValueOnly(const Constant *CV);
86 };
87} // end of anonymous namespace
88
Misha Brukman218bec72004-06-29 17:13:26 +000089/// createPPCCodePrinterPass - Returns a pass that prints the PPC
Misha Brukman5dfe3a92004-06-21 16:55:25 +000090/// assembly code for a MachineFunction to the given output stream,
91/// using the given target machine description. This should work
92/// regardless of whether the function is in SSA form.
93///
Misha Brukmanbb4a9082004-06-28 15:53:27 +000094FunctionPass *createPPCCodePrinterPass(std::ostream &o,TargetMachine &tm) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +000095 return new Printer(o, tm);
96}
97
98/// isStringCompatible - Can we treat the specified array as a string?
99/// Only if it is an array of ubytes or non-negative sbytes.
100///
101static bool isStringCompatible(const ConstantArray *CVA) {
102 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
103 if (ETy == Type::UByteTy) return true;
104 if (ETy != Type::SByteTy) return false;
105
106 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
107 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
108 return false;
109
110 return true;
111}
112
113/// toOctal - Convert the low order bits of X into an octal digit.
114///
115static inline char toOctal(int X) {
116 return (X&7)+'0';
117}
118
119/// getAsCString - Return the specified array as a C compatible
120/// string, only if the predicate isStringCompatible is true.
121///
122static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
123 assert(isStringCompatible(CVA) && "Array is not string compatible!");
124
125 O << "\"";
126 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
127 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
128
129 if (C == '"') {
130 O << "\\\"";
131 } else if (C == '\\') {
132 O << "\\\\";
133 } else if (isprint(C)) {
134 O << C;
135 } else {
136 switch(C) {
137 case '\b': O << "\\b"; break;
138 case '\f': O << "\\f"; break;
139 case '\n': O << "\\n"; break;
140 case '\r': O << "\\r"; break;
141 case '\t': O << "\\t"; break;
142 default:
143 O << '\\';
144 O << toOctal(C >> 6);
145 O << toOctal(C >> 3);
146 O << toOctal(C >> 0);
147 break;
148 }
149 }
150 }
151 O << "\"";
152}
153
154// Print out the specified constant, without a storage class. Only the
155// constants valid in constant expressions can occur here.
156void Printer::emitConstantValueOnly(const Constant *CV) {
157 if (CV->isNullValue())
158 O << "0";
159 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
160 assert(CB == ConstantBool::True);
161 O << "1";
162 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
163 O << CI->getValue();
164 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
165 O << CI->getValue();
166 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
167 // This is a constant address for a global variable or function. Use the
168 // name of the variable or function as the address value.
169 O << Mang->getValueName(CPR->getValue());
170 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
171 const TargetData &TD = TM.getTargetData();
172 switch(CE->getOpcode()) {
173 case Instruction::GetElementPtr: {
174 // generate a symbolic expression for the byte address
175 const Constant *ptrVal = CE->getOperand(0);
176 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
177 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
178 O << "(";
179 emitConstantValueOnly(ptrVal);
180 O << ") + " << Offset;
181 } else {
182 emitConstantValueOnly(ptrVal);
183 }
184 break;
185 }
186 case Instruction::Cast: {
187 // Support only non-converting or widening casts for now, that is, ones
188 // that do not involve a change in value. This assertion is really gross,
189 // and may not even be a complete check.
190 Constant *Op = CE->getOperand(0);
191 const Type *OpTy = Op->getType(), *Ty = CE->getType();
192
193 // Remember, kids, pointers on x86 can be losslessly converted back and
194 // forth into 32-bit or wider integers, regardless of signedness. :-P
195 assert(((isa<PointerType>(OpTy)
196 && (Ty == Type::LongTy || Ty == Type::ULongTy
197 || Ty == Type::IntTy || Ty == Type::UIntTy))
198 || (isa<PointerType>(Ty)
199 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
200 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
201 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
202 && OpTy->isLosslesslyConvertibleTo(Ty))))
203 && "FIXME: Don't yet support this kind of constant cast expr");
204 O << "(";
205 emitConstantValueOnly(Op);
206 O << ")";
207 break;
208 }
209 case Instruction::Add:
210 O << "(";
211 emitConstantValueOnly(CE->getOperand(0));
212 O << ") + (";
213 emitConstantValueOnly(CE->getOperand(1));
214 O << ")";
215 break;
216 default:
217 assert(0 && "Unsupported operator!");
218 }
219 } else {
220 assert(0 && "Unknown constant value!");
221 }
222}
223
224// Print a constant value or values, with the appropriate storage class as a
225// prefix.
226void Printer::emitGlobalConstant(const Constant *CV) {
227 const TargetData &TD = TM.getTargetData();
228
229 if (CV->isNullValue()) {
230 O << "\t.space\t " << TD.getTypeSize(CV->getType()) << "\n";
231 return;
232 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
233 if (isStringCompatible(CVA)) {
Misha Brukman218bec72004-06-29 17:13:26 +0000234 O << "\t.ascii ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000235 printAsCString(O, CVA);
236 O << "\n";
237 } else { // Not a string. Print the values in successive locations
238 const std::vector<Use> &constValues = CVA->getValues();
239 for (unsigned i=0; i < constValues.size(); i++)
240 emitGlobalConstant(cast<Constant>(constValues[i].get()));
241 }
242 return;
243 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
244 // Print the fields in successive locations. Pad to align if needed!
245 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
246 const std::vector<Use>& constValues = CVS->getValues();
247 unsigned sizeSoFar = 0;
248 for (unsigned i=0, N = constValues.size(); i < N; i++) {
249 const Constant* field = cast<Constant>(constValues[i].get());
250
251 // Check if padding is needed and insert one or more 0s.
252 unsigned fieldSize = TD.getTypeSize(field->getType());
253 unsigned padSize = ((i == N-1? cvsLayout->StructSize
254 : cvsLayout->MemberOffsets[i+1])
255 - cvsLayout->MemberOffsets[i]) - fieldSize;
256 sizeSoFar += fieldSize + padSize;
257
258 // Now print the actual field value
259 emitGlobalConstant(field);
260
261 // Insert the field padding unless it's zero bytes...
262 if (padSize)
263 O << "\t.space\t " << padSize << "\n";
264 }
265 assert(sizeSoFar == cvsLayout->StructSize &&
266 "Layout of constant struct may be incorrect!");
267 return;
268 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
269 // FP Constants are printed as integer constants to avoid losing
270 // precision...
271 double Val = CFP->getValue();
Misha Brukmand71bd562004-06-21 17:19:08 +0000272 switch (CFP->getType()->getTypeID()) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000273 default: assert(0 && "Unknown floating point type!");
274 case Type::FloatTyID: {
275 union FU { // Abide by C TBAA rules
276 float FVal;
277 unsigned UVal;
278 } U;
279 U.FVal = Val;
Misha Brukman218bec72004-06-29 17:13:26 +0000280 O << ".long\t" << U.UVal << "\t; float " << Val << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000281 return;
282 }
283 case Type::DoubleTyID: {
284 union DU { // Abide by C TBAA rules
285 double FVal;
286 uint64_t UVal;
287 struct {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000288 uint32_t MSWord;
289 uint32_t LSWord;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000290 } T;
291 } U;
292 U.FVal = Val;
293
Misha Brukman218bec72004-06-29 17:13:26 +0000294 O << ".long\t" << U.T.MSWord << "\t; double most significant word "
Misha Brukman46fd00a2004-06-24 23:04:11 +0000295 << Val << "\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000296 O << ".long\t" << U.T.LSWord << "\t; double least significant word"
Misha Brukman46fd00a2004-06-24 23:04:11 +0000297 << Val << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000298 return;
299 }
300 }
301 } else if (CV->getType()->getPrimitiveSize() == 64) {
Misha Brukman2bf183c2004-06-25 15:42:10 +0000302 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
303 union DU { // Abide by C TBAA rules
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000304 int64_t UVal;
305 struct {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000306 uint32_t MSWord;
307 uint32_t LSWord;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000308 } T;
309 } U;
310 U.UVal = CI->getRawValue();
311
Misha Brukman218bec72004-06-29 17:13:26 +0000312 O << ".long\t" << U.T.MSWord << "\t; Double-word most significant word "
Misha Brukman46fd00a2004-06-24 23:04:11 +0000313 << U.UVal << "\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000314 O << ".long\t" << U.T.LSWord << "\t; Double-word least significant word"
Misha Brukman46fd00a2004-06-24 23:04:11 +0000315 << U.UVal << "\n";
316 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000317 }
318 }
319
320 const Type *type = CV->getType();
321 O << "\t";
Misha Brukmand71bd562004-06-21 17:19:08 +0000322 switch (type->getTypeID()) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000323 case Type::UByteTyID: case Type::SByteTyID:
324 O << ".byte";
325 break;
326 case Type::UShortTyID: case Type::ShortTyID:
327 O << ".short";
328 break;
329 case Type::BoolTyID:
330 case Type::PointerTyID:
331 case Type::UIntTyID: case Type::IntTyID:
332 O << ".long";
333 break;
334 case Type::ULongTyID: case Type::LongTyID:
Misha Brukman46fd00a2004-06-24 23:04:11 +0000335 assert (0 && "Should have already output double-word constant.");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000336 case Type::FloatTyID: case Type::DoubleTyID:
337 assert (0 && "Should have already output floating point constant.");
338 default:
339 assert (0 && "Can't handle printing this type of thing");
340 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 }
398
399 // We didn't modify anything.
400 return false;
401}
402
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000403void Printer::printOp(const MachineOperand &MO,
Misha Brukman46fd00a2004-06-24 23:04:11 +0000404 bool elideOffsetKeyword /* = false */) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000405 const MRegisterInfo &RI = *TM.getRegisterInfo();
406 int new_symbol;
407
408 switch (MO.getType()) {
409 case MachineOperand::MO_VirtualRegister:
410 if (Value *V = MO.getVRegValueOrNull()) {
411 O << "<" << V->getName() << ">";
412 return;
413 }
414 // FALLTHROUGH
415 case MachineOperand::MO_MachineRegister:
Misha Brukman7f484a52004-06-24 23:51:00 +0000416 O << LowercaseString(RI.get(MO.getReg()).Name);
417 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000418
419 case MachineOperand::MO_SignExtendedImmed:
420 case MachineOperand::MO_UnextendedImmed:
421 O << (int)MO.getImmedValue();
422 return;
423 case MachineOperand::MO_MachineBasicBlock: {
424 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
425 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
Misha Brukman218bec72004-06-29 17:13:26 +0000426 << "_" << MBBOp->getNumber() << "\t; "
Misha Brukman2bf183c2004-06-25 15:42:10 +0000427 << MBBOp->getBasicBlock()->getName();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000428 return;
429 }
430 case MachineOperand::MO_PCRelativeDisp:
431 std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
Misha Brukman9c661c82004-06-28 17:57:40 +0000432 abort();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000433 return;
434 case MachineOperand::MO_GlobalAddress:
435 if (!elideOffsetKeyword) {
Misha Brukmana6e58b32004-06-28 18:03:37 +0000436 // Dynamically-resolved functions need a stub for the function
437 Function *F = dyn_cast<Function>(MO.getGlobal());
438 if (F && F->isExternal()) {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000439 Stubs.insert(Mang->getValueName(MO.getGlobal()));
440 O << "L" << Mang->getValueName(MO.getGlobal()) << "$stub";
441 } else {
442 O << Mang->getValueName(MO.getGlobal());
443 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000444 }
445 return;
446 case MachineOperand::MO_ExternalSymbol:
447 O << MO.getSymbolName();
448 return;
449 default:
Misha Brukman22e12072004-06-25 15:11:34 +0000450 O << "<unknown operand type>";
451 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000452 }
453}
454
455#if 0
456static inline
457unsigned int ValidOpcodes(const MachineInstr *MI, unsigned int ArgType[5]) {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000458 int i;
459 unsigned int retval = 1;
460
461 for(i = 0; i<5; i++) {
462 switch(ArgType[i]) {
463 case none:
464 break;
465 case Gpr:
466 case Gpr0:
467 Type::UIntTy
468 case Simm16:
469 case Zimm16:
470 case PCRelimm24:
471 case Imm24:
472 case Imm5:
473 case PCRelimm14:
474 case Imm14:
475 case Imm2:
476 case Crf:
477 case Imm3:
478 case Imm1:
479 case Fpr:
480 case Imm4:
481 case Imm8:
482 case Disimm16:
483 case Spr:
484 case Sgr:
485 };
486
487 }
488 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000489}
490#endif
491
492/// printMachineInstruction -- Print out a single PPC32 LLVM instruction
493/// MI in Darwin syntax to the current output stream.
494///
495void Printer::printMachineInstruction(const MachineInstr *MI) {
496 unsigned Opcode = MI->getOpcode();
497 const TargetInstrInfo &TII = *TM.getInstrInfo();
498 const TargetInstrDescriptor &Desc = TII.get(Opcode);
499 unsigned int i;
Misha Brukmanc6cc10f2004-06-25 19:24:52 +0000500
Misha Brukmanb9e8f972004-06-30 21:54:12 +0000501 unsigned int ArgCount = MI->getNumOperands();
502 //Desc.TSFlags & PPC32II::ArgCountMask;
Misha Brukman22e12072004-06-25 15:11:34 +0000503 unsigned int ArgType[] = {
504 (Desc.TSFlags >> PPC32II::Arg0TypeShift) & PPC32II::ArgTypeMask,
505 (Desc.TSFlags >> PPC32II::Arg1TypeShift) & PPC32II::ArgTypeMask,
506 (Desc.TSFlags >> PPC32II::Arg2TypeShift) & PPC32II::ArgTypeMask,
507 (Desc.TSFlags >> PPC32II::Arg3TypeShift) & PPC32II::ArgTypeMask,
508 (Desc.TSFlags >> PPC32II::Arg4TypeShift) & PPC32II::ArgTypeMask
509 };
Misha Brukman7f484a52004-06-24 23:51:00 +0000510 assert(((Desc.TSFlags & PPC32II::VMX) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000511 "Instruction requires VMX support");
Misha Brukman7f484a52004-06-24 23:51:00 +0000512 assert(((Desc.TSFlags & PPC32II::PPC64) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000513 "Instruction requires 64 bit support");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000514 //assert ( ValidOpcodes(MI, ArgType) && "Instruction has invalid inputs");
515 ++EmittedInsts;
516
Misha Brukman218bec72004-06-29 17:13:26 +0000517 // FIXME: should probably be converted to cout.width and cout.fill
Misha Brukman46fd00a2004-06-24 23:04:11 +0000518 if (Opcode == PPC32::MovePCtoLR) {
Misha Brukman218bec72004-06-29 17:13:26 +0000519 O << "bcl 20,31,\"L0000" << labelNumber << "$pb\"\n";
520 O << "\"L0000" << labelNumber << "$pb\":\n";
521 O << "\tmflr ";
522 printOp(MI->getOperand(0));
Misha Brukman218bec72004-06-29 17:13:26 +0000523 O << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000524 return;
525 }
526
527 O << TII.getName(MI->getOpcode()) << " ";
Misha Brukman05794492004-06-24 17:31:42 +0000528 DEBUG(std::cerr << TII.getName(MI->getOpcode()) << " expects "
529 << ArgCount << " args\n");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000530
Misha Brukman46fd00a2004-06-24 23:04:11 +0000531 if (Opcode == PPC32::LOADLoAddr) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000532 printOp(MI->getOperand(0));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000533 O << ", lo16(";
534 printOp(MI->getOperand(2));
Misha Brukman218bec72004-06-29 17:13:26 +0000535 O << "-\"L0000" << labelNumber << "$pb\")";
536 labelNumber++;
537 O << "(";
538 if (MI->getOperand(1).getReg() == PPC32::R0)
539 O << "0";
540 else
541 printOp(MI->getOperand(1));
542 O << ")\n";
Misha Brukmanc6cc10f2004-06-25 19:24:52 +0000543 } else if (Opcode == PPC32::LOADHiAddr) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000544 printOp(MI->getOperand(0));
545 O << ", ";
Misha Brukman218bec72004-06-29 17:13:26 +0000546 if (MI->getOperand(1).getReg() == PPC32::R0)
547 O << "0";
548 else
549 printOp(MI->getOperand(1));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000550 O << ", ha16(" ;
551 printOp(MI->getOperand(2));
Misha Brukman218bec72004-06-29 17:13:26 +0000552 O << "-\"L0000" << labelNumber << "$pb\")\n";
Misha Brukmanc6cc10f2004-06-25 19:24:52 +0000553 } else if (ArgCount == 3 && ArgType[1] == PPC32II::Disimm16) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000554 printOp(MI->getOperand(0));
555 O << ", ";
556 printOp(MI->getOperand(1));
557 O << "(";
Misha Brukmanb9e8f972004-06-30 21:54:12 +0000558 if (MI->getOperand(2).hasAllocatedReg() &&
559 MI->getOperand(2).getReg() == PPC32::R0)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000560 O << "0";
561 else
562 printOp(MI->getOperand(2));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000563 O << ")\n";
564 } else {
Misha Brukman7f484a52004-06-24 23:51:00 +0000565 for (i = 0; i < ArgCount; ++i) {
Misha Brukman218bec72004-06-29 17:13:26 +0000566 if (i == 1 && ArgCount == 3 && ArgType[2] == PPC32II::Simm16 &&
567 MI->getOperand(1).getReg() == PPC32::R0) {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000568 O << "0";
Misha Brukman218bec72004-06-29 17:13:26 +0000569 } else {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000570 //std::cout << "DEBUG " << (*(TM.getRegisterInfo())).get(MI->getOperand(i).getReg()).Name << "\n";
571 printOp(MI->getOperand(i));
572 }
Misha Brukman7f484a52004-06-24 23:51:00 +0000573 if (ArgCount - 1 == i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000574 O << "\n";
575 else
576 O << ", ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000577 }
578 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000579}
580
581bool Printer::doInitialization(Module &M) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000582 Mang = new Mangler(M, true);
583 return false; // success
584}
585
586// SwitchSection - Switch to the specified section of the executable if we are
587// not already in it!
588//
589static void SwitchSection(std::ostream &OS, std::string &CurSection,
590 const char *NewSection) {
591 if (CurSection != NewSection) {
592 CurSection = NewSection;
593 if (!CurSection.empty())
594 OS << "\t" << NewSection << "\n";
595 }
596}
597
598bool Printer::doFinalization(Module &M) {
599 const TargetData &TD = TM.getTargetData();
600 std::string CurSection;
601
602 // Print out module-level global variables here.
603 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
604 if (I->hasInitializer()) { // External global require no code
605 O << "\n\n";
606 std::string name = Mang->getValueName(I);
607 Constant *C = I->getInitializer();
608 unsigned Size = TD.getTypeSize(C->getType());
609 unsigned Align = TD.getTypeAlignment(C->getType());
610
611 if (C->isNullValue() &&
612 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
613 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
614 SwitchSection(O, CurSection, ".data");
615 if (I->hasInternalLinkage())
Misha Brukman218bec72004-06-29 17:13:26 +0000616 O << "\t.lcomm " << name << "," << TD.getTypeSize(C->getType())
617 << "," << (unsigned)TD.getTypeAlignment(C->getType());
618 else
619 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType());
620 O << "\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000621 WriteAsOperand(O, I, true, true, &M);
622 O << "\n";
623 } else {
624 switch (I->getLinkage()) {
625 case GlobalValue::LinkOnceLinkage:
626 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
627 // Nonnull linkonce -> weak
628 O << "\t.weak " << name << "\n";
629 SwitchSection(O, CurSection, "");
630 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
631 break;
632
633 case GlobalValue::AppendingLinkage:
634 // FIXME: appending linkage variables should go into a section of
635 // their name or something. For now, just emit them as external.
636 case GlobalValue::ExternalLinkage:
637 // If external or appending, declare as a global symbol
638 O << "\t.globl " << name << "\n";
639 // FALL THROUGH
640 case GlobalValue::InternalLinkage:
Misha Brukman61297ee2004-06-29 23:40:57 +0000641 SwitchSection(O, CurSection, ".data");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000642 break;
643 }
644
645 O << "\t.align " << Align << "\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000646 O << name << ":\t\t\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000647 WriteAsOperand(O, I, true, true, &M);
648 O << " = ";
649 WriteAsOperand(O, C, false, false, &M);
650 O << "\n";
651 emitGlobalConstant(C);
652 }
653 }
654
Misha Brukman46fd00a2004-06-24 23:04:11 +0000655 for(std::set<std::string>::iterator i = Stubs.begin(); i != Stubs.end(); ++i)
656 {
Misha Brukman218bec72004-06-29 17:13:26 +0000657 O << "\t.picsymbol_stub\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000658 O << "L" << *i << "$stub:\n";
659 O << "\t.indirect_symbol " << *i << "\n";
660 O << "\tmflr r0\n";
661 O << "\tbcl 20,31,L0$" << *i << "\n";
662 O << "L0$" << *i << ":\n";
663 O << "\tmflr r11\n";
664 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
665 O << "\tmtlr r0\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000666 O << "\tlwz r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000667 O << "\tmtctr r12\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000668 O << "\taddi r11,r11,lo16(L" << *i << "$lazy_ptr - L0$" << *i << ")\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000669 O << "\tbctr\n";
670 O << ".data\n";
671 O << ".lazy_symbol_pointer\n";
672 O << "L" << *i << "$lazy_ptr:\n";
673 O << ".indirect_symbol " << *i << "\n";
674 O << ".long dyld_stub_binding_helper\n";
675 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000676
677 delete Mang;
678 return false; // success
679}
680
681} // End llvm namespace