1 //===-- PPC32/Printer.cpp - Convert X86 LLVM code to Intel assembly ---------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
17 #define DEBUG_TYPE "asmprinter"
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"
24 #include "llvm/CodeGen/MachineConstantPool.h"
25 #include "llvm/CodeGen/MachineFunctionPass.h"
26 #include "llvm/CodeGen/MachineInstr.h"
27 #include "llvm/Target/TargetMachine.h"
28 #include "llvm/Support/Mangler.h"
29 #include "Support/CommandLine.h"
30 #include "Support/Debug.h"
31 #include "Support/Statistic.h"
32 #include "Support/StringExtras.h"
38 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
40 struct Printer : public MachineFunctionPass {
41 /// Output stream on which we're printing assembly code.
45 /// Target machine description which we query for reg. names, data
50 /// Name-mangler for global names.
53 std::set< std::string > Stubs;
54 std::set<std::string> Strings;
56 Printer(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { }
58 /// Cache of mangled name for current function. This is
59 /// recalculated at the beginning of each call to
60 /// runOnMachineFunction().
62 std::string CurrentFnName;
64 virtual const char *getPassName() const {
65 return "PowerPC Assembly Printer";
68 void printMachineInstruction(const MachineInstr *MI);
69 void printOp(const MachineOperand &MO,
70 bool elideOffsetKeyword = false);
71 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);
78 } // end of anonymous namespace
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.
85 FunctionPass *createPPCCodePrinterPass(std::ostream &o,TargetMachine &tm){
86 return new Printer(o, tm);
89 /// isStringCompatible - Can we treat the specified array as a string?
90 /// Only if it is an array of ubytes or non-negative sbytes.
92 static 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;
97 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
98 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
104 /// toOctal - Convert the low order bits of X into an octal digit.
106 static inline char toOctal(int X) {
110 /// getAsCString - Return the specified array as a C compatible
111 /// string, only if the predicate isStringCompatible is true.
113 static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
114 assert(isStringCompatible(CVA) && "Array is not string compatible!");
117 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
118 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
122 } else if (C == '\\') {
124 } else if (isprint(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;
135 O << toOctal(C >> 6);
136 O << toOctal(C >> 3);
137 O << toOctal(C >> 0);
145 // Print out the specified constant, without a storage class. Only the
146 // constants valid in constant expressions can occur here.
147 void Printer::emitConstantValueOnly(const Constant *CV) {
148 if (CV->isNullValue())
150 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
151 assert(CB == ConstantBool::True);
153 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
155 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
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)) {
170 emitConstantValueOnly(ptrVal);
171 O << ") + " << Offset;
173 emitConstantValueOnly(ptrVal);
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();
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");
196 emitConstantValueOnly(Op);
200 case Instruction::Add:
202 emitConstantValueOnly(CE->getOperand(0));
204 emitConstantValueOnly(CE->getOperand(1));
208 assert(0 && "Unsupported operator!");
211 assert(0 && "Unknown constant value!");
215 // Print a constant value or values, with the appropriate storage class as a
217 void Printer::emitGlobalConstant(const Constant *CV) {
218 const TargetData &TD = TM.getTargetData();
220 if (CV->isNullValue()) {
221 O << "\t.space\t " << TD.getTypeSize(CV->getType()) << "\n";
223 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
224 if (isStringCompatible(CVA)) {
226 printAsCString(O, CVA);
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()));
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());
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;
249 // Now print the actual field value
250 emitGlobalConstant(field);
252 // Insert the field padding unless it's zero bytes...
254 O << "\t.space\t " << padSize << "\n";
256 assert(sizeSoFar == cvsLayout->StructSize &&
257 "Layout of constant struct may be incorrect!");
259 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
260 // FP Constants are printed as integer constants to avoid losing
262 double Val = CFP->getValue();
263 switch (CFP->getType()->getTypeID()) {
264 default: assert(0 && "Unknown floating point type!");
265 case Type::FloatTyID: {
266 union FU { // Abide by C TBAA rules
271 O << ".long\t" << U.UVal << "\t# float " << Val << "\n";
274 case Type::DoubleTyID: {
275 union DU { // Abide by C TBAA rules
285 O << ".long\t" << U.T.MSWord << "\t# double most significant word "
287 O << ".long\t" << U.T.LSWord << "\t# double least significant word"
292 } else if (CV->getType()->getPrimitiveSize() == 64) {
293 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
294 union DU { // Abide by C TBAA rules
301 U.UVal = CI->getRawValue();
303 O << ".long\t" << U.T.MSWord << "\t# Double-word most significant word "
305 O << ".long\t" << U.T.LSWord << "\t# Double-word least significant word"
311 const Type *type = CV->getType();
313 switch (type->getTypeID()) {
314 case Type::UByteTyID: case Type::SByteTyID:
317 case Type::UShortTyID: case Type::ShortTyID:
321 case Type::PointerTyID:
322 case Type::UIntTyID: case Type::IntTyID:
325 case Type::ULongTyID: case Type::LongTyID:
326 assert (0 && "Should have already output double-word constant.");
327 case Type::FloatTyID: case Type::DoubleTyID:
328 assert (0 && "Should have already output floating point constant.");
330 assert (0 && "Can't handle printing this type of thing");
334 emitConstantValueOnly(CV);
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.
343 void Printer::printConstantPool(MachineConstantPool *MCP) {
344 const std::vector<Constant*> &CP = MCP->getConstants();
345 const TargetData &TD = TM.getTargetData();
347 if (CP.empty()) return;
349 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
351 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
353 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
355 emitGlobalConstant(CP[i]);
359 /// runOnMachineFunction - This uses the printMachineInstruction()
360 /// method to print assembly for each instruction.
362 bool 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;
368 // What's my mangled name?
369 CurrentFnName = Mang->getValueName(MF.getFunction());
371 // Print out constants referenced by the function
372 printConstantPool(MF.getConstantPool());
374 // Print out labels for the function.
376 O << "\t.globl\t" << CurrentFnName << "\n";
378 O << CurrentFnName << ":\n";
380 // Print out code for the function.
381 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
383 // Print a label for the basic block.
384 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t# "
385 << I->getBasicBlock()->getName() << "\n";
386 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
388 // Print the assembly for the instruction.
390 printMachineInstruction(II);
394 // We didn't modify anything.
398 void Printer::printOp(const MachineOperand &MO,
399 bool elideOffsetKeyword /* = false */) {
400 const MRegisterInfo &RI = *TM.getRegisterInfo();
403 switch (MO.getType()) {
404 case MachineOperand::MO_VirtualRegister:
405 if (Value *V = MO.getVRegValueOrNull()) {
406 O << "<" << V->getName() << ">";
410 case MachineOperand::MO_MachineRegister:
411 O << LowercaseString(RI.get(MO.getReg()).Name);
414 case MachineOperand::MO_SignExtendedImmed:
415 case MachineOperand::MO_UnextendedImmed:
416 O << (int)MO.getImmedValue();
418 case MachineOperand::MO_MachineBasicBlock: {
419 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
420 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
421 << "_" << MBBOp->getNumber() << "\t# "
422 << MBBOp->getBasicBlock()->getName();
425 case MachineOperand::MO_PCRelativeDisp:
426 std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
429 case MachineOperand::MO_GlobalAddress:
430 if (!elideOffsetKeyword) {
431 if(isa<Function>(MO.getGlobal())) {
432 Stubs.insert(Mang->getValueName(MO.getGlobal()));
433 O << "L" << Mang->getValueName(MO.getGlobal()) << "$stub";
435 O << Mang->getValueName(MO.getGlobal());
439 case MachineOperand::MO_ExternalSymbol:
440 O << MO.getSymbolName();
443 O << "<unknown operand type>";
450 unsigned int ValidOpcodes(const MachineInstr *MI, unsigned int ArgType[5]) {
452 unsigned int retval = 1;
454 for(i = 0; i<5; i++) {
485 /// printMachineInstruction -- Print out a single PPC32 LLVM instruction
486 /// MI in Darwin syntax to the current output stream.
488 void Printer::printMachineInstruction(const MachineInstr *MI) {
489 unsigned Opcode = MI->getOpcode();
490 const TargetInstrInfo &TII = *TM.getInstrInfo();
491 const TargetInstrDescriptor &Desc = TII.get(Opcode);
494 unsigned int ArgCount = Desc.TSFlags & PPC32II::ArgCountMask;
495 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
502 assert(((Desc.TSFlags & PPC32II::VMX) == 0) &&
503 "Instruction requires VMX support");
504 assert(((Desc.TSFlags & PPC32II::PPC64) == 0) &&
505 "Instruction requires 64 bit support");
506 //assert ( ValidOpcodes(MI, ArgType) && "Instruction has invalid inputs");
509 if (Opcode == PPC32::MovePCtoLR) {
511 O << "bcl 20,31,L" << CurrentFnName << "$pb\n";
512 O << "L" << CurrentFnName << "$pb:\n";
516 O << TII.getName(MI->getOpcode()) << " ";
517 DEBUG(std::cerr << TII.getName(MI->getOpcode()) << " expects "
518 << ArgCount << " args\n");
520 if (Opcode == PPC32::LOADLoAddr) {
521 printOp(MI->getOperand(0));
523 printOp(MI->getOperand(1));
525 printOp(MI->getOperand(2));
526 O << "-L" << CurrentFnName << "$pb)\n";
530 if (Opcode == PPC32::LOADHiAddr) {
531 printOp(MI->getOperand(0));
533 printOp(MI->getOperand(1));
535 printOp(MI->getOperand(2));
536 O << "-L" << CurrentFnName << "$pb)\n";
540 if (ArgCount == 3 && ArgType[1] == PPC32II::Disimm16) {
541 printOp(MI->getOperand(0));
543 printOp(MI->getOperand(1));
545 if (ArgType[2] == PPC32II::Gpr0 && MI->getOperand(2).getReg() == PPC32::R0)
548 printOp(MI->getOperand(2));
551 for (i = 0; i < ArgCount; ++i) {
552 if (ArgType[i] == PPC32II::Gpr0 &&
553 MI->getOperand(i).getReg() == PPC32::R0)
556 //std::cout << "DEBUG " << (*(TM.getRegisterInfo())).get(MI->getOperand(i).getReg()).Name << "\n";
557 printOp(MI->getOperand(i));
559 if (ArgCount - 1 == i)
569 bool Printer::doInitialization(Module &M) {
570 Mang = new Mangler(M, true);
571 return false; // success
574 // SwitchSection - Switch to the specified section of the executable if we are
575 // not already in it!
577 static void SwitchSection(std::ostream &OS, std::string &CurSection,
578 const char *NewSection) {
579 if (CurSection != NewSection) {
580 CurSection = NewSection;
581 if (!CurSection.empty())
582 OS << "\t" << NewSection << "\n";
586 bool Printer::doFinalization(Module &M) {
587 const TargetData &TD = TM.getTargetData();
588 std::string CurSection;
590 // Print out module-level global variables here.
591 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
592 if (I->hasInitializer()) { // External global require no code
594 std::string name = Mang->getValueName(I);
595 Constant *C = I->getInitializer();
596 unsigned Size = TD.getTypeSize(C->getType());
597 unsigned Align = TD.getTypeAlignment(C->getType());
599 if (C->isNullValue() &&
600 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
601 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
602 SwitchSection(O, CurSection, ".data");
603 if (I->hasInternalLinkage())
604 O << "\t.local " << name << "\n";
606 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
607 << "," << (unsigned)TD.getTypeAlignment(C->getType());
609 WriteAsOperand(O, I, true, true, &M);
612 switch (I->getLinkage()) {
613 case GlobalValue::LinkOnceLinkage:
614 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
615 // Nonnull linkonce -> weak
616 O << "\t.weak " << name << "\n";
617 SwitchSection(O, CurSection, "");
618 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
621 case GlobalValue::AppendingLinkage:
622 // FIXME: appending linkage variables should go into a section of
623 // their name or something. For now, just emit them as external.
624 case GlobalValue::ExternalLinkage:
625 // If external or appending, declare as a global symbol
626 O << "\t.globl " << name << "\n";
628 case GlobalValue::InternalLinkage:
629 if (C->isNullValue())
630 SwitchSection(O, CurSection, ".bss");
632 SwitchSection(O, CurSection, ".data");
636 O << "\t.align " << Align << "\n";
637 O << name << ":\t\t\t\t# ";
638 WriteAsOperand(O, I, true, true, &M);
640 WriteAsOperand(O, C, false, false, &M);
642 emitGlobalConstant(C);
646 for(std::set<std::string>::iterator i = Stubs.begin(); i != Stubs.end(); ++i)
649 O<<".section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n";
651 O << "L" << *i << "$stub:\n";
652 O << "\t.indirect_symbol " << *i << "\n";
654 O << "\tbcl 20,31,L0$" << *i << "\n";
655 O << "L0$" << *i << ":\n";
657 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
659 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
660 O << "\tmtctr r12\n";
663 O << ".lazy_symbol_pointer\n";
664 O << "L" << *i << "$lazy_ptr:\n";
665 O << ".indirect_symbol " << *i << "\n";
666 O << ".long dyld_stub_binding_helper\n";
670 return false; // success
673 } // End llvm namespace