LLVM API Documentation

IA64AsmPrinter.cpp

Go to the documentation of this file.
00001 //===-- IA64AsmPrinter.cpp - Print out IA64 LLVM as assembly --------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by Duraid Madina and is distributed under the
00006 // University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains a printer that converts from our internal representation
00011 // of machine-dependent LLVM code to assembly accepted by the GNU binutils 'gas'
00012 // assembler. The Intel 'ias' and HP-UX 'as' assemblers *may* choke on this
00013 // output, but if so that's a bug I'd like to hear about: please file a bug
00014 // report in bugzilla. FYI, the not too bad 'ias' assembler is bundled with
00015 // the Intel C/C++ compiler for Itanium Linux.
00016 //
00017 //===----------------------------------------------------------------------===//
00018 
00019 #include "IA64.h"
00020 #include "IA64TargetMachine.h"
00021 #include "llvm/Module.h"
00022 #include "llvm/Type.h"
00023 #include "llvm/Assembly/Writer.h"
00024 #include "llvm/CodeGen/AsmPrinter.h"
00025 #include "llvm/CodeGen/MachineFunctionPass.h"
00026 #include "llvm/Target/TargetMachine.h"
00027 #include "llvm/Support/Mangler.h"
00028 #include "llvm/ADT/Statistic.h"
00029 #include <iostream>
00030 using namespace llvm;
00031 
00032 namespace {
00033   Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
00034 
00035   struct IA64AsmPrinter : public AsmPrinter {
00036     std::set<std::string> ExternalFunctionNames, ExternalObjectNames;
00037 
00038     IA64AsmPrinter(std::ostream &O, TargetMachine &TM) : AsmPrinter(O, TM) {
00039       CommentString = "//";
00040       Data8bitsDirective = "\tdata1\t";     // FIXME: check that we are
00041       Data16bitsDirective = "\tdata2.ua\t"; // disabling auto-alignment
00042       Data32bitsDirective = "\tdata4.ua\t"; // properly
00043       Data64bitsDirective = "\tdata8.ua\t";
00044       ZeroDirective = "\t.skip\t";
00045       AsciiDirective = "\tstring\t";
00046 
00047       GlobalVarAddrPrefix="";
00048       GlobalVarAddrSuffix="";
00049       FunctionAddrPrefix="@fptr(";
00050       FunctionAddrSuffix=")";
00051       
00052       // FIXME: would be nice to have rodata (no 'w') when appropriate?
00053       ConstantPoolSection = "\n\t.section .data, \"aw\", \"progbits\"\n";
00054     }
00055 
00056     virtual const char *getPassName() const {
00057       return "IA64 Assembly Printer";
00058     }
00059 
00060     /// printInstruction - This method is automatically generated by tablegen
00061     /// from the instruction set description.  This method returns true if the
00062     /// machine instruction was sufficiently described to print it, otherwise it
00063     /// returns false.
00064     bool printInstruction(const MachineInstr *MI);
00065 
00066     // This method is used by the tablegen'erated instruction printer.
00067     void printOperand(const MachineInstr *MI, unsigned OpNo){
00068       const MachineOperand &MO = MI->getOperand(OpNo);
00069       if (MO.getType() == MachineOperand::MO_Register) {
00070         assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physref??");
00071         //XXX Bug Workaround: See note in Printer::doInitialization about %.
00072         O << TM.getRegisterInfo()->get(MO.getReg()).Name;
00073       } else {
00074         printOp(MO);
00075       }
00076     }
00077 
00078     void printS8ImmOperand(const MachineInstr *MI, unsigned OpNo) {
00079       int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
00080       if(val>=128) val=val-256; // if negative, flip sign
00081       O << val;
00082     }
00083     void printS14ImmOperand(const MachineInstr *MI, unsigned OpNo) {
00084       int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
00085       if(val>=8192) val=val-16384; // if negative, flip sign
00086       O << val;
00087     }
00088     void printS22ImmOperand(const MachineInstr *MI, unsigned OpNo) {
00089       int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
00090       if(val>=2097152) val=val-4194304; // if negative, flip sign
00091       O << val;
00092     }
00093     void printU64ImmOperand(const MachineInstr *MI, unsigned OpNo) {
00094       O << (uint64_t)MI->getOperand(OpNo).getImmedValue();
00095     }
00096     void printS64ImmOperand(const MachineInstr *MI, unsigned OpNo) {
00097 // XXX : nasty hack to avoid GPREL22 "relocation truncated to fit" linker
00098 // errors - instead of add rX = @gprel(CPI<whatever>), r1;; we now
00099 // emit movl rX = @gprel(CPI<whatever);;
00100 //      add  rX = rX, r1; 
00101 // this gives us 64 bits instead of 22 (for the add long imm) to play
00102 // with, which shuts up the linker. The problem is that the constant
00103 // pool entries aren't immediates at this stage, so we check here. 
00104 // If it's an immediate, print it the old fashioned way. If it's
00105 // not, we print it as a constant pool index. 
00106       if(MI->getOperand(OpNo).isImmediate()) {
00107         O << (int64_t)MI->getOperand(OpNo).getImmedValue();
00108       } else { // this is a constant pool reference: FIXME: assert this
00109         printOp(MI->getOperand(OpNo));
00110       }
00111     }
00112 
00113     void printGlobalOperand(const MachineInstr *MI, unsigned OpNo) {
00114       printOp(MI->getOperand(OpNo), false); // this is NOT a br.call instruction
00115     }
00116 
00117     void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
00118       printOp(MI->getOperand(OpNo), true); // this is a br.call instruction
00119     }
00120 
00121     void printMachineInstruction(const MachineInstr *MI);
00122     void printOp(const MachineOperand &MO, bool isBRCALLinsn= false);
00123     bool runOnMachineFunction(MachineFunction &F);
00124     bool doInitialization(Module &M);
00125     bool doFinalization(Module &M);
00126   };
00127 } // end of anonymous namespace
00128 
00129 
00130 // Include the auto-generated portion of the assembly writer.
00131 #include "IA64GenAsmWriter.inc"
00132 
00133 
00134 /// runOnMachineFunction - This uses the printMachineInstruction()
00135 /// method to print assembly for each instruction.
00136 ///
00137 bool IA64AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
00138   SetupMachineFunction(MF);
00139   O << "\n\n";
00140 
00141   // Print out constants referenced by the function
00142   EmitConstantPool(MF.getConstantPool());
00143 
00144   // Print out labels for the function.
00145   SwitchToTextSection("\n\t.section .text, \"ax\", \"progbits\"\n", 
00146                       MF.getFunction());
00147   // ^^  means "Allocated instruXions in mem, initialized"
00148   EmitAlignment(5);
00149   O << "\t.global\t" << CurrentFnName << "\n";
00150   O << "\t.type\t" << CurrentFnName << ", @function\n";
00151   O << CurrentFnName << ":\n";
00152 
00153   // Print out code for the function.
00154   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
00155        I != E; ++I) {
00156     // Print a label for the basic block if there are any predecessors.
00157     if (I->pred_begin() != I->pred_end()) {
00158       printBasicBlockLabel(I, true);
00159       O << '\n';
00160     }
00161     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
00162          II != E; ++II) {
00163       // Print the assembly for the instruction.
00164       O << "\t";
00165       printMachineInstruction(II);
00166     }
00167   }
00168 
00169   // We didn't modify anything.
00170   return false;
00171 }
00172 
00173 void IA64AsmPrinter::printOp(const MachineOperand &MO,
00174                              bool isBRCALLinsn /* = false */) {
00175   const MRegisterInfo &RI = *TM.getRegisterInfo();
00176   switch (MO.getType()) {
00177   case MachineOperand::MO_Register:
00178     O << RI.get(MO.getReg()).Name;
00179     return;
00180 
00181   case MachineOperand::MO_Immediate:
00182     O << MO.getImmedValue();
00183     return;
00184   case MachineOperand::MO_MachineBasicBlock:
00185     printBasicBlockLabel(MO.getMachineBasicBlock());
00186     return;
00187   case MachineOperand::MO_ConstantPoolIndex: {
00188     O << "@gprel(" << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
00189       << MO.getConstantPoolIndex() << ")";
00190     return;
00191   }
00192 
00193   case MachineOperand::MO_GlobalAddress: {
00194 
00195     // functions need @ltoff(@fptr(fn_name)) form
00196     GlobalValue *GV = MO.getGlobal();
00197     Function *F = dyn_cast<Function>(GV);
00198 
00199     bool Needfptr=false; // if we're computing an address @ltoff(X), do
00200                          // we need to decorate it so it becomes
00201                          // @ltoff(@fptr(X)) ?
00202     if (F && !isBRCALLinsn /*&& F->isExternal()*/)
00203       Needfptr=true;
00204 
00205     // if this is the target of a call instruction, we should define
00206     // the function somewhere (GNU gas has no problem without this, but
00207     // Intel ias rightly complains of an 'undefined symbol')
00208 
00209     if (F /*&& isBRCALLinsn*/ && F->isExternal())
00210       ExternalFunctionNames.insert(Mang->getValueName(MO.getGlobal()));
00211     else
00212       if (GV->isExternal()) // e.g. stuff like 'stdin'
00213         ExternalObjectNames.insert(Mang->getValueName(MO.getGlobal()));
00214 
00215     if (!isBRCALLinsn)
00216       O << "@ltoff(";
00217     if (Needfptr)
00218       O << "@fptr(";
00219     O << Mang->getValueName(MO.getGlobal());
00220     
00221     if (Needfptr && !isBRCALLinsn)
00222       O << "#))"; // close both fptr( and ltoff(
00223     else {
00224       if (Needfptr)
00225         O << "#)"; // close only fptr(
00226       if (!isBRCALLinsn)
00227         O << "#)"; // close only ltoff(
00228     }
00229     
00230     int Offset = MO.getOffset();
00231     if (Offset > 0)
00232       O << " + " << Offset;
00233     else if (Offset < 0)
00234       O << " - " << -Offset;
00235     return;
00236   }
00237   case MachineOperand::MO_ExternalSymbol:
00238     O << MO.getSymbolName();
00239     ExternalFunctionNames.insert(MO.getSymbolName());
00240     return;
00241   default:
00242     O << "<AsmPrinter: unknown operand type: " << MO.getType() << " >"; return;
00243   }
00244 }
00245 
00246 /// printMachineInstruction -- Print out a single IA64 LLVM instruction
00247 /// MI to the current output stream.
00248 ///
00249 void IA64AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
00250   ++EmittedInsts;
00251 
00252   // Call the autogenerated instruction printer routines.
00253   printInstruction(MI);
00254 }
00255 
00256 bool IA64AsmPrinter::doInitialization(Module &M) {
00257   AsmPrinter::doInitialization(M);
00258 
00259   O << "\n.ident \"LLVM-ia64\"\n\n"
00260     << "\t.psr    lsb\n"  // should be "msb" on HP-UX, for starters
00261     << "\t.radix  C\n"
00262     << "\t.psr    abi64\n"; // we only support 64 bits for now
00263   return false;
00264 }
00265 
00266 bool IA64AsmPrinter::doFinalization(Module &M) {
00267   const TargetData *TD = TM.getTargetData();
00268   
00269   // Print out module-level global variables here.
00270   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
00271        I != E; ++I)
00272     if (I->hasInitializer()) {   // External global require no code
00273       // Check to see if this is a special global used by LLVM, if so, emit it.
00274       if (EmitSpecialLLVMGlobal(I))
00275         continue;
00276       
00277       O << "\n\n";
00278       std::string name = Mang->getValueName(I);
00279       Constant *C = I->getInitializer();
00280       unsigned Size = TD->getTypeSize(C->getType());
00281       unsigned Align = TD->getTypeAlignmentShift(C->getType());
00282       
00283       if (C->isNullValue() &&
00284           (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
00285            I->hasWeakLinkage() /* FIXME: Verify correct */)) {
00286         SwitchToDataSection(".data", I);
00287         if (I->hasInternalLinkage()) {
00288           O << "\t.lcomm " << name << "#," << TD->getTypeSize(C->getType())
00289           << "," << (1 << Align);
00290           O << "\t\t// ";
00291         } else {
00292           O << "\t.common " << name << "#," << TD->getTypeSize(C->getType())
00293           << "," << (1 << Align);
00294           O << "\t\t// ";
00295         }
00296         WriteAsOperand(O, I, true, true, &M);
00297         O << "\n";
00298       } else {
00299         switch (I->getLinkage()) {
00300           case GlobalValue::LinkOnceLinkage:
00301           case GlobalValue::WeakLinkage:   // FIXME: Verify correct for weak.
00302                                            // Nonnull linkonce -> weak
00303             O << "\t.weak " << name << "\n";
00304             O << "\t.section\t.llvm.linkonce.d." << name
00305               << ", \"aw\", \"progbits\"\n";
00306             SwitchToDataSection("", I);
00307             break;
00308           case GlobalValue::AppendingLinkage:
00309             // FIXME: appending linkage variables should go into a section of
00310             // their name or something.  For now, just emit them as external.
00311           case GlobalValue::ExternalLinkage:
00312             // If external or appending, declare as a global symbol
00313             O << "\t.global " << name << "\n";
00314             // FALL THROUGH
00315           case GlobalValue::InternalLinkage:
00316             SwitchToDataSection(C->isNullValue() ? ".bss" : ".data", I);
00317             break;
00318           case GlobalValue::GhostLinkage:
00319             std::cerr << "GhostLinkage cannot appear in IA64AsmPrinter!\n";
00320             abort();
00321         }
00322         
00323         EmitAlignment(Align);
00324         O << "\t.type " << name << ",@object\n";
00325         O << "\t.size " << name << "," << Size << "\n";
00326         O << name << ":\t\t\t\t// ";
00327         WriteAsOperand(O, I, true, true, &M);
00328         O << " = ";
00329         WriteAsOperand(O, C, false, false, &M);
00330         O << "\n";
00331         EmitGlobalConstant(C);
00332       }
00333     }
00334       
00335       // we print out ".global X \n .type X, @function" for each external function
00336       O << "\n\n// br.call targets referenced (and not defined) above: \n";
00337   for (std::set<std::string>::iterator i = ExternalFunctionNames.begin(),
00338        e = ExternalFunctionNames.end(); i!=e; ++i) {
00339     O << "\t.global " << *i << "\n\t.type " << *i << ", @function\n";
00340   }
00341   O << "\n\n";
00342   
00343   // we print out ".global X \n .type X, @object" for each external object
00344   O << "\n\n// (external) symbols referenced (and not defined) above: \n";
00345   for (std::set<std::string>::iterator i = ExternalObjectNames.begin(),
00346        e = ExternalObjectNames.end(); i!=e; ++i) {
00347     O << "\t.global " << *i << "\n\t.type " << *i << ", @object\n";
00348   }
00349   O << "\n\n";
00350   
00351   AsmPrinter::doFinalization(M);
00352   return false; // success
00353 }
00354 
00355 /// createIA64CodePrinterPass - Returns a pass that prints the IA64
00356 /// assembly code for a MachineFunction to the given output stream, using
00357 /// the given target machine description.
00358 ///
00359 FunctionPass *llvm::createIA64CodePrinterPass(std::ostream &o,
00360                                               IA64TargetMachine &tm) {
00361   return new IA64AsmPrinter(o, tm);
00362 }
00363 
00364