X86AsmPrinter.cpp revision f9c197e022521a83f8876890b4241fc23e63572c
1//===-- X86AsmPrinter.cpp - Convert X86 LLVM IR to X86 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 the shared super class printer that converts from our internal 11// representation of machine-dependent LLVM code to Intel and AT&T format 12// assembly language. 13// This printer is the output mechanism used by `llc'. 14// 15//===----------------------------------------------------------------------===// 16 17#include "X86AsmPrinter.h" 18#include "X86ATTAsmPrinter.h" 19#include "X86IntelAsmPrinter.h" 20#include "X86MachineFunctionInfo.h" 21#include "X86Subtarget.h" 22#include "llvm/ADT/StringExtras.h" 23#include "llvm/CallingConv.h" 24#include "llvm/Constants.h" 25#include "llvm/Module.h" 26#include "llvm/Type.h" 27#include "llvm/Assembly/Writer.h" 28#include "llvm/Support/Mangler.h" 29#include "llvm/Target/TargetAsmInfo.h" 30 31using namespace llvm; 32 33Statistic<> llvm::EmittedInsts("asm-printer", 34 "Number of machine instrs printed"); 35 36static X86FunctionInfo calculateFunctionInfo(const Function *F, 37 const TargetData *TD) { 38 X86FunctionInfo Info; 39 uint64_t Size = 0; 40 41 switch (F->getCallingConv()) { 42 case CallingConv::X86_StdCall: 43 Info.setDecorationStyle(StdCall); 44 break; 45 case CallingConv::X86_FastCall: 46 Info.setDecorationStyle(FastCall); 47 break; 48 default: 49 return Info; 50 } 51 52 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end(); 53 AI != AE; ++AI) 54 Size += TD->getTypeSize(AI->getType()); 55 56 // Size should be aligned to DWORD boundary 57 Size = ((Size + 3)/4)*4; 58 59 // We're not supporting tooooo huge arguments :) 60 Info.setBytesToPopOnReturn((unsigned int)Size); 61 return Info; 62} 63 64 65/// decorateName - Query FunctionInfoMap and use this information for various 66/// name decoration. 67void X86SharedAsmPrinter::decorateName(std::string &Name, 68 const GlobalValue *GV) { 69 const Function *F = dyn_cast<Function>(GV); 70 if (!F) return; 71 72 // We don't want to decorate non-stdcall or non-fastcall functions right now 73 unsigned CC = F->getCallingConv(); 74 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall) 75 return; 76 77 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F); 78 79 const X86FunctionInfo *Info; 80 if (info_item == FunctionInfoMap.end()) { 81 // Calculate apropriate function info and populate map 82 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData()); 83 Info = &FunctionInfoMap[F]; 84 } else { 85 Info = &info_item->second; 86 } 87 88 switch (Info->getDecorationStyle()) { 89 case None: 90 break; 91 case StdCall: 92 if (!F->isVarArg()) // Variadic functions do not receive @0 suffix. 93 Name += '@' + utostr_32(Info->getBytesToPopOnReturn()); 94 break; 95 case FastCall: 96 if (!F->isVarArg()) // Variadic functions do not receive @0 suffix. 97 Name += '@' + utostr_32(Info->getBytesToPopOnReturn()); 98 99 if (Name[0] == '_') { 100 Name[0] = '@'; 101 } else { 102 Name = '@' + Name; 103 } 104 break; 105 default: 106 assert(0 && "Unsupported DecorationStyle"); 107 } 108} 109 110/// doInitialization 111bool X86SharedAsmPrinter::doInitialization(Module &M) { 112 if (Subtarget->isTargetDarwin()) { 113 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>(); 114 if (!Subtarget->is64Bit()) 115 X86PICStyle = PICStyle::Stub; 116 117 // Emit initial debug information. 118 DW.BeginModule(&M); 119 } 120 121 return AsmPrinter::doInitialization(M); 122} 123 124bool X86SharedAsmPrinter::doFinalization(Module &M) { 125 // Note: this code is not shared by the Intel printer as it is too different 126 // from how MASM does things. When making changes here don't forget to look 127 // at X86IntelAsmPrinter::doFinalization(). 128 const TargetData *TD = TM.getTargetData(); 129 130 // Print out module-level global variables here. 131 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); 132 I != E; ++I) { 133 if (!I->hasInitializer()) continue; // External global require no code 134 135 // Check to see if this is a special global used by LLVM, if so, emit it. 136 if (EmitSpecialLLVMGlobal(I)) 137 continue; 138 139 std::string name = Mang->getValueName(I); 140 Constant *C = I->getInitializer(); 141 unsigned Size = TD->getTypeSize(C->getType()); 142 unsigned Align = TD->getPreferredAlignmentLog(I); 143 144 if (C->isNullValue() && /* FIXME: Verify correct */ 145 (I->hasInternalLinkage() || I->hasWeakLinkage() || 146 I->hasLinkOnceLinkage() || 147 (Subtarget->isTargetDarwin() && 148 I->hasExternalLinkage() && !I->hasSection()))) { 149 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it. 150 if (I->hasExternalLinkage()) { 151 O << "\t.globl\t" << name << "\n"; 152 O << "\t.zerofill __DATA__, __common, " << name << ", " 153 << Size << ", " << Align; 154 } else { 155 SwitchToDataSection(TAI->getDataSection(), I); 156 if (TAI->getLCOMMDirective() != NULL) { 157 if (I->hasInternalLinkage()) { 158 O << TAI->getLCOMMDirective() << name << "," << Size; 159 if (Subtarget->isTargetDarwin()) 160 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align); 161 } else 162 O << TAI->getCOMMDirective() << name << "," << Size; 163 } else { 164 if (!Subtarget->isTargetCygwin()) { 165 if (I->hasInternalLinkage()) 166 O << "\t.local\t" << name << "\n"; 167 } 168 O << TAI->getCOMMDirective() << name << "," << Size; 169 if (TAI->getCOMMDirectiveTakesAlignment()) 170 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align); 171 } 172 } 173 O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n"; 174 } else { 175 switch (I->getLinkage()) { 176 case GlobalValue::LinkOnceLinkage: 177 case GlobalValue::WeakLinkage: 178 if (Subtarget->isTargetDarwin()) { 179 O << "\t.globl " << name << "\n" 180 << "\t.weak_definition " << name << "\n"; 181 SwitchToDataSection(".section __DATA,__const_coal,coalesced", I); 182 } else if (Subtarget->isTargetCygwin()) { 183 std::string SectionName(".section\t.data$linkonce." + 184 name + 185 ",\"aw\"\n"); 186 SwitchToDataSection(SectionName.c_str(), I); 187 O << "\t.globl " << name << "\n" 188 << "\t.linkonce same_size\n"; 189 } else { 190 std::string SectionName("\t.section\t.llvm.linkonce.d." + 191 name + 192 ",\"aw\",@progbits\n"); 193 SwitchToDataSection(SectionName.c_str(), I); 194 O << "\t.weak " << name << "\n"; 195 } 196 break; 197 case GlobalValue::AppendingLinkage: 198 // FIXME: appending linkage variables should go into a section of 199 // their name or something. For now, just emit them as external. 200 case GlobalValue::DLLExportLinkage: 201 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),"")); 202 // FALL THROUGH 203 case GlobalValue::ExternalLinkage: 204 // If external or appending, declare as a global symbol 205 O << "\t.globl " << name << "\n"; 206 // FALL THROUGH 207 case GlobalValue::InternalLinkage: 208 SwitchToDataSection(TAI->getDataSection(), I); 209 break; 210 default: 211 assert(0 && "Unknown linkage type!"); 212 } 213 214 EmitAlignment(Align, I); 215 O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName() 216 << "\n"; 217 if (TAI->hasDotTypeDotSizeDirective()) 218 O << "\t.size " << name << ", " << Size << "\n"; 219 220 EmitGlobalConstant(C); 221 O << '\n'; 222 } 223 } 224 225 // Output linker support code for dllexported globals 226 if (DLLExportedGVs.begin() != DLLExportedGVs.end()) { 227 SwitchToDataSection(".section .drectve", 0); 228 } 229 230 for (std::set<std::string>::iterator i = DLLExportedGVs.begin(), 231 e = DLLExportedGVs.end(); 232 i != e; ++i) { 233 O << "\t.ascii \" -export:" << *i << ",data\"\n"; 234 } 235 236 if (DLLExportedFns.begin() != DLLExportedFns.end()) { 237 SwitchToDataSection(".section .drectve", 0); 238 } 239 240 for (std::set<std::string>::iterator i = DLLExportedFns.begin(), 241 e = DLLExportedFns.end(); 242 i != e; ++i) { 243 O << "\t.ascii \" -export:" << *i << "\"\n"; 244 } 245 246 if (Subtarget->isTargetDarwin()) { 247 SwitchToDataSection("", 0); 248 249 // Output stubs for dynamically-linked functions 250 unsigned j = 1; 251 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end(); 252 i != e; ++i, ++j) { 253 SwitchToDataSection(".section __IMPORT,__jump_table,symbol_stubs," 254 "self_modifying_code+pure_instructions,5", 0); 255 O << "L" << *i << "$stub:\n"; 256 O << "\t.indirect_symbol " << *i << "\n"; 257 O << "\thlt ; hlt ; hlt ; hlt ; hlt\n"; 258 } 259 260 O << "\n"; 261 262 // Output stubs for external and common global variables. 263 if (GVStubs.begin() != GVStubs.end()) 264 SwitchToDataSection( 265 ".section __IMPORT,__pointers,non_lazy_symbol_pointers", 0); 266 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end(); 267 i != e; ++i) { 268 O << "L" << *i << "$non_lazy_ptr:\n"; 269 O << "\t.indirect_symbol " << *i << "\n"; 270 O << "\t.long\t0\n"; 271 } 272 273 // Emit initial debug information. 274 DW.EndModule(); 275 276 // Funny Darwin hack: This flag tells the linker that no global symbols 277 // contain code that falls through to other global symbols (e.g. the obvious 278 // implementation of multiple entry points). If this doesn't occur, the 279 // linker can safely perform dead code stripping. Since LLVM never 280 // generates code that does this, it is always safe to set. 281 O << "\t.subsections_via_symbols\n"; 282 } 283 284 AsmPrinter::doFinalization(M); 285 return false; // success 286} 287 288/// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code 289/// for a MachineFunction to the given output stream, using the given target 290/// machine description. 291/// 292FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o, 293 X86TargetMachine &tm) { 294 const X86Subtarget *Subtarget = &tm.getSubtarget<X86Subtarget>(); 295 296 if (Subtarget->isFlavorIntel()) { 297 return new X86IntelAsmPrinter(o, tm, tm.getTargetAsmInfo()); 298 } else { 299 return new X86ATTAsmPrinter(o, tm, tm.getTargetAsmInfo()); 300 } 301} 302