LLVM API Documentation
00001 //===-- SystemZAsmPrinter.cpp - SystemZ LLVM assembly writer ---------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // 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 the SystemZ assembly language. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #define DEBUG_TYPE "asm-printer" 00016 #include "SystemZ.h" 00017 #include "SystemZInstrInfo.h" 00018 #include "SystemZTargetMachine.h" 00019 #include "llvm/Constants.h" 00020 #include "llvm/DerivedTypes.h" 00021 #include "llvm/Module.h" 00022 #include "llvm/Assembly/Writer.h" 00023 #include "llvm/CodeGen/AsmPrinter.h" 00024 #include "llvm/CodeGen/DwarfWriter.h" 00025 #include "llvm/CodeGen/MachineModuleInfo.h" 00026 #include "llvm/CodeGen/MachineFunctionPass.h" 00027 #include "llvm/CodeGen/MachineConstantPool.h" 00028 #include "llvm/CodeGen/MachineInstr.h" 00029 #include "llvm/MC/MCStreamer.h" 00030 #include "llvm/Target/TargetAsmInfo.h" 00031 #include "llvm/Target/TargetData.h" 00032 #include "llvm/Target/TargetLoweringObjectFile.h" 00033 #include "llvm/Target/TargetRegistry.h" 00034 #include "llvm/ADT/Statistic.h" 00035 #include "llvm/Support/Compiler.h" 00036 #include "llvm/Support/FormattedStream.h" 00037 #include "llvm/Support/Mangler.h" 00038 00039 using namespace llvm; 00040 00041 STATISTIC(EmittedInsts, "Number of machine instrs printed"); 00042 00043 namespace { 00044 class VISIBILITY_HIDDEN SystemZAsmPrinter : public AsmPrinter { 00045 public: 00046 SystemZAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM, 00047 const TargetAsmInfo *TAI, bool V) 00048 : AsmPrinter(O, TM, TAI, V) {} 00049 00050 virtual const char *getPassName() const { 00051 return "SystemZ Assembly Printer"; 00052 } 00053 00054 void printOperand(const MachineInstr *MI, int OpNum, 00055 const char* Modifier = 0); 00056 void printPCRelImmOperand(const MachineInstr *MI, int OpNum); 00057 void printRIAddrOperand(const MachineInstr *MI, int OpNum, 00058 const char* Modifier = 0); 00059 void printRRIAddrOperand(const MachineInstr *MI, int OpNum, 00060 const char* Modifier = 0); 00061 void printS16ImmOperand(const MachineInstr *MI, int OpNum) { 00062 O << (int16_t)MI->getOperand(OpNum).getImm(); 00063 } 00064 void printS32ImmOperand(const MachineInstr *MI, int OpNum) { 00065 O << (int32_t)MI->getOperand(OpNum).getImm(); 00066 } 00067 00068 void printInstruction(const MachineInstr *MI); // autogenerated. 00069 void printMachineInstruction(const MachineInstr * MI); 00070 00071 void emitFunctionHeader(const MachineFunction &MF); 00072 bool runOnMachineFunction(MachineFunction &F); 00073 void PrintGlobalVariable(const GlobalVariable* GVar); 00074 00075 void getAnalysisUsage(AnalysisUsage &AU) const { 00076 AsmPrinter::getAnalysisUsage(AU); 00077 AU.setPreservesAll(); 00078 } 00079 }; 00080 } // end of anonymous namespace 00081 00082 #include "SystemZGenAsmWriter.inc" 00083 00084 void SystemZAsmPrinter::emitFunctionHeader(const MachineFunction &MF) { 00085 unsigned FnAlign = MF.getAlignment(); 00086 const Function *F = MF.getFunction(); 00087 00088 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM)); 00089 00090 EmitAlignment(FnAlign, F); 00091 00092 switch (F->getLinkage()) { 00093 default: assert(0 && "Unknown linkage type!"); 00094 case Function::InternalLinkage: // Symbols default to internal. 00095 case Function::PrivateLinkage: 00096 case Function::LinkerPrivateLinkage: 00097 break; 00098 case Function::ExternalLinkage: 00099 O << "\t.globl\t" << CurrentFnName << '\n'; 00100 break; 00101 case Function::LinkOnceAnyLinkage: 00102 case Function::LinkOnceODRLinkage: 00103 case Function::WeakAnyLinkage: 00104 case Function::WeakODRLinkage: 00105 O << "\t.weak\t" << CurrentFnName << '\n'; 00106 break; 00107 } 00108 00109 printVisibility(CurrentFnName, F->getVisibility()); 00110 00111 O << "\t.type\t" << CurrentFnName << ",@function\n" 00112 << CurrentFnName << ":\n"; 00113 } 00114 00115 bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) { 00116 SetupMachineFunction(MF); 00117 O << "\n\n"; 00118 00119 // Print out constants referenced by the function 00120 EmitConstantPool(MF.getConstantPool()); 00121 00122 // Print the 'header' of function 00123 emitFunctionHeader(MF); 00124 00125 // Print out code for the function. 00126 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); 00127 I != E; ++I) { 00128 // Print a label for the basic block. 00129 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) { 00130 // This is an entry block or a block that's only reachable via a 00131 // fallthrough edge. In non-VerboseAsm mode, don't print the label. 00132 } else { 00133 printBasicBlockLabel(I, true, true, VerboseAsm); 00134 O << '\n'; 00135 } 00136 00137 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); 00138 II != E; ++II) 00139 // Print the assembly for the instruction. 00140 printMachineInstruction(II); 00141 } 00142 00143 if (TAI->hasDotTypeDotSizeDirective()) 00144 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n'; 00145 00146 // Print out jump tables referenced by the function. 00147 EmitJumpTableInfo(MF.getJumpTableInfo(), MF); 00148 00149 // We didn't modify anything 00150 return false; 00151 } 00152 00153 void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) { 00154 ++EmittedInsts; 00155 00156 // Call the autogenerated instruction printer routines. 00157 printInstruction(MI); 00158 } 00159 00160 void SystemZAsmPrinter::printPCRelImmOperand(const MachineInstr *MI, int OpNum) { 00161 const MachineOperand &MO = MI->getOperand(OpNum); 00162 switch (MO.getType()) { 00163 case MachineOperand::MO_Immediate: 00164 O << MO.getImm(); 00165 return; 00166 case MachineOperand::MO_MachineBasicBlock: 00167 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm); 00168 return; 00169 case MachineOperand::MO_GlobalAddress: { 00170 const GlobalValue *GV = MO.getGlobal(); 00171 std::string Name = Mang->getMangledName(GV); 00172 00173 O << Name; 00174 00175 // Assemble calls via PLT for externally visible symbols if PIC. 00176 if (TM.getRelocationModel() == Reloc::PIC_ && 00177 !GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() && 00178 !GV->hasLocalLinkage()) 00179 O << "@PLT"; 00180 00181 printOffset(MO.getOffset()); 00182 return; 00183 } 00184 case MachineOperand::MO_ExternalSymbol: { 00185 std::string Name(TAI->getGlobalPrefix()); 00186 Name += MO.getSymbolName(); 00187 O << Name; 00188 00189 if (TM.getRelocationModel() == Reloc::PIC_) 00190 O << "@PLT"; 00191 00192 return; 00193 } 00194 default: 00195 assert(0 && "Not implemented yet!"); 00196 } 00197 } 00198 00199 00200 void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum, 00201 const char* Modifier) { 00202 const MachineOperand &MO = MI->getOperand(OpNum); 00203 switch (MO.getType()) { 00204 case MachineOperand::MO_Register: { 00205 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) && 00206 "Virtual registers should be already mapped!"); 00207 unsigned Reg = MO.getReg(); 00208 if (Modifier && strncmp(Modifier, "subreg", 6) == 0) { 00209 if (strncmp(Modifier + 7, "even", 4) == 0) 00210 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_EVEN); 00211 else if (strncmp(Modifier + 7, "odd", 3) == 0) 00212 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_ODD); 00213 else 00214 assert(0 && "Invalid subreg modifier"); 00215 } 00216 00217 O << '%' << TRI->getAsmName(Reg); 00218 return; 00219 } 00220 case MachineOperand::MO_Immediate: 00221 O << MO.getImm(); 00222 return; 00223 case MachineOperand::MO_MachineBasicBlock: 00224 printBasicBlockLabel(MO.getMBB()); 00225 return; 00226 case MachineOperand::MO_JumpTableIndex: 00227 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_' 00228 << MO.getIndex(); 00229 00230 return; 00231 case MachineOperand::MO_ConstantPoolIndex: 00232 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_' 00233 << MO.getIndex(); 00234 00235 printOffset(MO.getOffset()); 00236 break; 00237 case MachineOperand::MO_GlobalAddress: { 00238 const GlobalValue *GV = MO.getGlobal(); 00239 std::string Name = Mang->getMangledName(GV); 00240 00241 O << Name; 00242 break; 00243 } 00244 case MachineOperand::MO_ExternalSymbol: { 00245 std::string Name(TAI->getGlobalPrefix()); 00246 Name += MO.getSymbolName(); 00247 O << Name; 00248 break; 00249 } 00250 default: 00251 assert(0 && "Not implemented yet!"); 00252 } 00253 00254 switch (MO.getTargetFlags()) { 00255 default: 00256 llvm_unreachable("Unknown target flag on GV operand"); 00257 case SystemZII::MO_NO_FLAG: 00258 break; 00259 case SystemZII::MO_GOTENT: O << "@GOTENT"; break; 00260 case SystemZII::MO_PLT: O << "@PLT"; break; 00261 } 00262 00263 printOffset(MO.getOffset()); 00264 } 00265 00266 void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum, 00267 const char* Modifier) { 00268 const MachineOperand &Base = MI->getOperand(OpNum); 00269 00270 // Print displacement operand. 00271 printOperand(MI, OpNum+1); 00272 00273 // Print base operand (if any) 00274 if (Base.getReg()) { 00275 O << '('; 00276 printOperand(MI, OpNum); 00277 O << ')'; 00278 } 00279 } 00280 00281 void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum, 00282 const char* Modifier) { 00283 const MachineOperand &Base = MI->getOperand(OpNum); 00284 const MachineOperand &Index = MI->getOperand(OpNum+2); 00285 00286 // Print displacement operand. 00287 printOperand(MI, OpNum+1); 00288 00289 // Print base operand (if any) 00290 if (Base.getReg()) { 00291 O << '('; 00292 printOperand(MI, OpNum); 00293 if (Index.getReg()) { 00294 O << ','; 00295 printOperand(MI, OpNum+2); 00296 } 00297 O << ')'; 00298 } else 00299 assert(!Index.getReg() && "Should allocate base register first!"); 00300 } 00301 00302 void SystemZAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) { 00303 const TargetData *TD = TM.getTargetData(); 00304 00305 if (!GVar->hasInitializer()) 00306 return; // External global require no code 00307 00308 // Check to see if this is a special global used by LLVM, if so, emit it. 00309 if (EmitSpecialLLVMGlobal(GVar)) 00310 return; 00311 00312 std::string name = Mang->getMangledName(GVar); 00313 Constant *C = GVar->getInitializer(); 00314 unsigned Size = TD->getTypeAllocSize(C->getType()); 00315 unsigned Align = std::max(1U, TD->getPreferredAlignmentLog(GVar)); 00316 00317 printVisibility(name, GVar->getVisibility()); 00318 00319 O << "\t.type\t" << name << ",@object\n"; 00320 00321 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang, 00322 TM)); 00323 00324 if (C->isNullValue() && !GVar->hasSection() && 00325 !GVar->isThreadLocal() && 00326 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) { 00327 00328 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it. 00329 00330 if (GVar->hasLocalLinkage()) 00331 O << "\t.local\t" << name << '\n'; 00332 00333 O << TAI->getCOMMDirective() << name << ',' << Size; 00334 if (TAI->getCOMMDirectiveTakesAlignment()) 00335 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align); 00336 00337 if (VerboseAsm) { 00338 O << "\t\t" << TAI->getCommentString() << ' '; 00339 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent()); 00340 } 00341 O << '\n'; 00342 return; 00343 } 00344 00345 switch (GVar->getLinkage()) { 00346 case GlobalValue::CommonLinkage: 00347 case GlobalValue::LinkOnceAnyLinkage: 00348 case GlobalValue::LinkOnceODRLinkage: 00349 case GlobalValue::WeakAnyLinkage: 00350 case GlobalValue::WeakODRLinkage: 00351 O << "\t.weak\t" << name << '\n'; 00352 break; 00353 case GlobalValue::DLLExportLinkage: 00354 case GlobalValue::AppendingLinkage: 00355 // FIXME: appending linkage variables should go into a section of 00356 // their name or something. For now, just emit them as external. 00357 case GlobalValue::ExternalLinkage: 00358 // If external or appending, declare as a global symbol 00359 O << "\t.globl " << name << '\n'; 00360 // FALL THROUGH 00361 case GlobalValue::PrivateLinkage: 00362 case GlobalValue::LinkerPrivateLinkage: 00363 case GlobalValue::InternalLinkage: 00364 break; 00365 default: 00366 assert(0 && "Unknown linkage type!"); 00367 } 00368 00369 // Use 16-bit alignment by default to simplify bunch of stuff 00370 EmitAlignment(Align, GVar, 1); 00371 O << name << ":"; 00372 if (VerboseAsm) { 00373 O << "\t\t\t\t" << TAI->getCommentString() << ' '; 00374 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent()); 00375 } 00376 O << '\n'; 00377 if (TAI->hasDotTypeDotSizeDirective()) 00378 O << "\t.size\t" << name << ", " << Size << '\n'; 00379 00380 EmitGlobalConstant(C); 00381 } 00382 00383 // Force static initialization. 00384 extern "C" void LLVMInitializeSystemZAsmPrinter() { 00385 RegisterAsmPrinter<SystemZAsmPrinter> X(TheSystemZTarget); 00386 }