LLVM API Documentation
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_MachineRegister) { 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 SwitchSection("\n\t.section .text, \"ax\", \"progbits\"\n", MF.getFunction()); 00146 // ^^ means "Allocated instruXions in mem, initialized" 00147 EmitAlignment(5); 00148 O << "\t.global\t" << CurrentFnName << "\n"; 00149 O << "\t.type\t" << CurrentFnName << ", @function\n"; 00150 O << CurrentFnName << ":\n"; 00151 00152 // Print out code for the function. 00153 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); 00154 I != E; ++I) { 00155 // Print a label for the basic block if there are any predecessors. 00156 if (I->pred_begin() != I->pred_end()) 00157 O << PrivateGlobalPrefix << "LBB" << CurrentFnName << "_" 00158 << I->getNumber() << ":\t" 00159 << CommentString << " " << I->getBasicBlock()->getName() << "\n"; 00160 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); 00161 II != E; ++II) { 00162 // Print the assembly for the instruction. 00163 O << "\t"; 00164 printMachineInstruction(II); 00165 } 00166 } 00167 00168 // We didn't modify anything. 00169 return false; 00170 } 00171 00172 void IA64AsmPrinter::printOp(const MachineOperand &MO, 00173 bool isBRCALLinsn /* = false */) { 00174 const MRegisterInfo &RI = *TM.getRegisterInfo(); 00175 switch (MO.getType()) { 00176 case MachineOperand::MO_VirtualRegister: 00177 if (Value *V = MO.getVRegValueOrNull()) { 00178 O << "<" << V->getName() << ">"; 00179 return; 00180 } 00181 // FALLTHROUGH 00182 case MachineOperand::MO_MachineRegister: 00183 case MachineOperand::MO_CCRegister: { 00184 O << RI.get(MO.getReg()).Name; 00185 return; 00186 } 00187 00188 case MachineOperand::MO_SignExtendedImmed: 00189 case MachineOperand::MO_UnextendedImmed: 00190 O << /*(unsigned int)*/MO.getImmedValue(); 00191 return; 00192 case MachineOperand::MO_MachineBasicBlock: { 00193 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock(); 00194 O << PrivateGlobalPrefix << "LBB" 00195 << Mang->getValueName(MBBOp->getParent()->getFunction()) 00196 << "_" << MBBOp->getNumber () << "\t// " 00197 << MBBOp->getBasicBlock ()->getName (); 00198 return; 00199 } 00200 case MachineOperand::MO_PCRelativeDisp: 00201 std::cerr << "Shouldn't use addPCDisp() when building IA64 MachineInstrs"; 00202 abort (); 00203 return; 00204 00205 case MachineOperand::MO_ConstantPoolIndex: { 00206 O << "@gprel(" << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_" 00207 << MO.getConstantPoolIndex() << ")"; 00208 return; 00209 } 00210 00211 case MachineOperand::MO_GlobalAddress: { 00212 00213 // functions need @ltoff(@fptr(fn_name)) form 00214 GlobalValue *GV = MO.getGlobal(); 00215 Function *F = dyn_cast<Function>(GV); 00216 00217 bool Needfptr=false; // if we're computing an address @ltoff(X), do 00218 // we need to decorate it so it becomes 00219 // @ltoff(@fptr(X)) ? 00220 if (F && !isBRCALLinsn /*&& F->isExternal()*/) 00221 Needfptr=true; 00222 00223 // if this is the target of a call instruction, we should define 00224 // the function somewhere (GNU gas has no problem without this, but 00225 // Intel ias rightly complains of an 'undefined symbol') 00226 00227 if (F /*&& isBRCALLinsn*/ && F->isExternal()) 00228 ExternalFunctionNames.insert(Mang->getValueName(MO.getGlobal())); 00229 else 00230 if (GV->isExternal()) // e.g. stuff like 'stdin' 00231 ExternalObjectNames.insert(Mang->getValueName(MO.getGlobal())); 00232 00233 if (!isBRCALLinsn) 00234 O << "@ltoff("; 00235 if (Needfptr) 00236 O << "@fptr("; 00237 O << Mang->getValueName(MO.getGlobal()); 00238 00239 if (Needfptr && !isBRCALLinsn) 00240 O << "#))"; // close both fptr( and ltoff( 00241 else { 00242 if (Needfptr) 00243 O << "#)"; // close only fptr( 00244 if (!isBRCALLinsn) 00245 O << "#)"; // close only ltoff( 00246 } 00247 00248 int Offset = MO.getOffset(); 00249 if (Offset > 0) 00250 O << " + " << Offset; 00251 else if (Offset < 0) 00252 O << " - " << -Offset; 00253 return; 00254 } 00255 case MachineOperand::MO_ExternalSymbol: 00256 O << MO.getSymbolName(); 00257 ExternalFunctionNames.insert(MO.getSymbolName()); 00258 return; 00259 default: 00260 O << "<AsmPrinter: unknown operand type: " << MO.getType() << " >"; return; 00261 } 00262 } 00263 00264 /// printMachineInstruction -- Print out a single IA64 LLVM instruction 00265 /// MI to the current output stream. 00266 /// 00267 void IA64AsmPrinter::printMachineInstruction(const MachineInstr *MI) { 00268 ++EmittedInsts; 00269 00270 // Call the autogenerated instruction printer routines. 00271 printInstruction(MI); 00272 } 00273 00274 bool IA64AsmPrinter::doInitialization(Module &M) { 00275 AsmPrinter::doInitialization(M); 00276 00277 O << "\n.ident \"LLVM-ia64\"\n\n" 00278 << "\t.psr lsb\n" // should be "msb" on HP-UX, for starters 00279 << "\t.radix C\n" 00280 << "\t.psr abi64\n"; // we only support 64 bits for now 00281 return false; 00282 } 00283 00284 bool IA64AsmPrinter::doFinalization(Module &M) { 00285 const TargetData &TD = TM.getTargetData(); 00286 00287 // Print out module-level global variables here. 00288 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); 00289 I != E; ++I) 00290 if (I->hasInitializer()) { // External global require no code 00291 // Check to see if this is a special global used by LLVM, if so, emit it. 00292 if (EmitSpecialLLVMGlobal(I)) 00293 continue; 00294 00295 O << "\n\n"; 00296 std::string name = Mang->getValueName(I); 00297 Constant *C = I->getInitializer(); 00298 unsigned Size = TD.getTypeSize(C->getType()); 00299 unsigned Align = TD.getTypeAlignmentShift(C->getType()); 00300 00301 if (C->isNullValue() && 00302 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() || 00303 I->hasWeakLinkage() /* FIXME: Verify correct */)) { 00304 SwitchSection(".data", I); 00305 if (I->hasInternalLinkage()) { 00306 O << "\t.lcomm " << name << "#," << TD.getTypeSize(C->getType()) 00307 << "," << (1 << Align); 00308 O << "\t\t// "; 00309 } else { 00310 O << "\t.common " << name << "#," << TD.getTypeSize(C->getType()) 00311 << "," << (1 << Align); 00312 O << "\t\t// "; 00313 } 00314 WriteAsOperand(O, I, true, true, &M); 00315 O << "\n"; 00316 } else { 00317 switch (I->getLinkage()) { 00318 case GlobalValue::LinkOnceLinkage: 00319 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak. 00320 // Nonnull linkonce -> weak 00321 O << "\t.weak " << name << "\n"; 00322 O << "\t.section\t.llvm.linkonce.d." << name 00323 << ", \"aw\", \"progbits\"\n"; 00324 SwitchSection("", I); 00325 break; 00326 case GlobalValue::AppendingLinkage: 00327 // FIXME: appending linkage variables should go into a section of 00328 // their name or something. For now, just emit them as external. 00329 case GlobalValue::ExternalLinkage: 00330 // If external or appending, declare as a global symbol 00331 O << "\t.global " << name << "\n"; 00332 // FALL THROUGH 00333 case GlobalValue::InternalLinkage: 00334 SwitchSection(C->isNullValue() ? ".bss" : ".data", I); 00335 break; 00336 case GlobalValue::GhostLinkage: 00337 std::cerr << "GhostLinkage cannot appear in IA64AsmPrinter!\n"; 00338 abort(); 00339 } 00340 00341 EmitAlignment(Align); 00342 O << "\t.type " << name << ",@object\n"; 00343 O << "\t.size " << name << "," << Size << "\n"; 00344 O << name << ":\t\t\t\t// "; 00345 WriteAsOperand(O, I, true, true, &M); 00346 O << " = "; 00347 WriteAsOperand(O, C, false, false, &M); 00348 O << "\n"; 00349 EmitGlobalConstant(C); 00350 } 00351 } 00352 00353 // we print out ".global X \n .type X, @function" for each external function 00354 O << "\n\n// br.call targets referenced (and not defined) above: \n"; 00355 for (std::set<std::string>::iterator i = ExternalFunctionNames.begin(), 00356 e = ExternalFunctionNames.end(); i!=e; ++i) { 00357 O << "\t.global " << *i << "\n\t.type " << *i << ", @function\n"; 00358 } 00359 O << "\n\n"; 00360 00361 // we print out ".global X \n .type X, @object" for each external object 00362 O << "\n\n// (external) symbols referenced (and not defined) above: \n"; 00363 for (std::set<std::string>::iterator i = ExternalObjectNames.begin(), 00364 e = ExternalObjectNames.end(); i!=e; ++i) { 00365 O << "\t.global " << *i << "\n\t.type " << *i << ", @object\n"; 00366 } 00367 O << "\n\n"; 00368 00369 AsmPrinter::doFinalization(M); 00370 return false; // success 00371 } 00372 00373 /// createIA64CodePrinterPass - Returns a pass that prints the IA64 00374 /// assembly code for a MachineFunction to the given output stream, using 00375 /// the given target machine description. 00376 /// 00377 FunctionPass *llvm::createIA64CodePrinterPass(std::ostream &o, 00378 IA64TargetMachine &tm) { 00379 return new IA64AsmPrinter(o, tm); 00380 } 00381 00382