Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 1 | //===-- X86AsmPrinter.cpp - Convert X86 LLVM IR to X86 assembly -----------===// |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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. |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 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'. |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 17 | #include "X86ATTAsmPrinter.h" |
| 18 | #include "X86IntelAsmPrinter.h" |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 19 | #include "X86.h" |
Chris Lattner | d59414f | 2003-08-11 20:06:16 +0000 | [diff] [blame] | 20 | #include "llvm/Module.h" |
| 21 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineConstantPool.h" |
Brian Gaeke | d9fb37a | 2003-07-24 20:20:44 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Mangler.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 24 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | 300d0ed | 2004-02-14 06:00:36 +0000 | [diff] [blame] | 25 | using namespace llvm; |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 26 | using namespace x86; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 27 | |
Nate Begeman | 4eb74ba | 2005-07-01 23:56:38 +0000 | [diff] [blame] | 28 | Statistic<> llvm::x86::EmittedInsts("asm-printer", |
| 29 | "Number of machine instrs printed"); |
Chris Lattner | 955f096 | 2004-10-04 07:31:08 +0000 | [diff] [blame] | 30 | |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 31 | enum AsmWriterFlavorTy { att, intel }; |
| 32 | cl::opt<AsmWriterFlavorTy> |
| 33 | AsmWriterFlavor("x86-asm-syntax", |
| 34 | cl::desc("Choose style of code to emit from X86 backend:"), |
| 35 | cl::values( |
| 36 | clEnumVal(att, " Emit AT&T-style assembly"), |
| 37 | clEnumVal(intel, " Emit Intel-style assembly"), |
| 38 | clEnumValEnd), |
| 39 | cl::init(att)); |
Chris Lattner | 955f096 | 2004-10-04 07:31:08 +0000 | [diff] [blame] | 40 | |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 41 | /// doInitialization |
Reid Spencer | 5dc81f6 | 2005-01-23 03:52:14 +0000 | [diff] [blame] | 42 | bool X86SharedAsmPrinter::doInitialization(Module& M) { |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 43 | bool leadingUnderscore = false; |
| 44 | forCygwin = false; |
Reid Spencer | 5dc81f6 | 2005-01-23 03:52:14 +0000 | [diff] [blame] | 45 | const std::string& TT = M.getTargetTriple(); |
Nate Begeman | 9d19eb4 | 2005-06-30 00:53:20 +0000 | [diff] [blame] | 46 | if (TT.length() > 5) { |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 47 | forCygwin = TT.find("cygwin") != std::string::npos || |
Reid Spencer | d632f49 | 2005-03-08 17:02:05 +0000 | [diff] [blame] | 48 | TT.find("mingw") != std::string::npos; |
Nate Begeman | 9d19eb4 | 2005-06-30 00:53:20 +0000 | [diff] [blame] | 49 | forDarwin = TT.find("darwin") != std::string::npos; |
| 50 | } else if (TT.empty()) { |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 51 | #if defined(__CYGWIN__) || defined(__MINGW32__) |
Reid Spencer | 5dc81f6 | 2005-01-23 03:52:14 +0000 | [diff] [blame] | 52 | forCygwin = true; |
Nate Begeman | 72b286b | 2005-07-08 00:23:26 +0000 | [diff] [blame] | 53 | #elif defined(__APPLE__) |
Nate Begeman | 9d19eb4 | 2005-06-30 00:53:20 +0000 | [diff] [blame] | 54 | forDarwin = true; |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 55 | #elif defined(_WIN32) |
| 56 | leadingUnderscore = true; |
| 57 | #else |
| 58 | leadingUnderscore = false; |
| 59 | #endif |
Reid Spencer | 5dc81f6 | 2005-01-23 03:52:14 +0000 | [diff] [blame] | 60 | } |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 61 | if (leadingUnderscore || forCygwin || forDarwin) |
Reid Spencer | 5dc81f6 | 2005-01-23 03:52:14 +0000 | [diff] [blame] | 62 | GlobalPrefix = "_"; |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 63 | |
Nate Begeman | 9d19eb4 | 2005-06-30 00:53:20 +0000 | [diff] [blame] | 64 | if (forDarwin) |
| 65 | AlignmentIsInBytes = false; |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 66 | |
Reid Spencer | 5dc81f6 | 2005-01-23 03:52:14 +0000 | [diff] [blame] | 67 | return AsmPrinter::doInitialization(M); |
| 68 | } |
| 69 | |
Chris Lattner | ac5701c | 2004-10-04 07:24:48 +0000 | [diff] [blame] | 70 | /// printConstantPool - Print to the current output stream assembly |
| 71 | /// representations of the constants in the constant pool MCP. This is |
| 72 | /// used to print out constants which have been "spilled to memory" by |
| 73 | /// the code generator. |
| 74 | /// |
| 75 | void X86SharedAsmPrinter::printConstantPool(MachineConstantPool *MCP) { |
| 76 | const std::vector<Constant*> &CP = MCP->getConstants(); |
| 77 | const TargetData &TD = TM.getTargetData(); |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 78 | |
Chris Lattner | ac5701c | 2004-10-04 07:24:48 +0000 | [diff] [blame] | 79 | if (CP.empty()) return; |
| 80 | |
| 81 | for (unsigned i = 0, e = CP.size(); i != e; ++i) { |
Nate Begeman | 72b286b | 2005-07-08 00:23:26 +0000 | [diff] [blame] | 82 | if (forDarwin) |
| 83 | O << "\t.data\n"; |
| 84 | else |
| 85 | O << "\t.section .rodata\n"; |
Chris Lattner | ac5701c | 2004-10-04 07:24:48 +0000 | [diff] [blame] | 86 | emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType())); |
| 87 | O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString |
| 88 | << *CP[i] << "\n"; |
| 89 | emitGlobalConstant(CP[i]); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | bool X86SharedAsmPrinter::doFinalization(Module &M) { |
| 94 | const TargetData &TD = TM.getTargetData(); |
| 95 | std::string CurSection; |
| 96 | |
| 97 | // Print out module-level global variables here. |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 98 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 99 | if (I->hasInitializer()) { // External global require no code |
| 100 | O << "\n\n"; |
| 101 | std::string name = Mang->getValueName(I); |
| 102 | Constant *C = I->getInitializer(); |
| 103 | unsigned Size = TD.getTypeSize(C->getType()); |
| 104 | unsigned Align = TD.getTypeAlignmentShift(C->getType()); |
Chris Lattner | ac5701c | 2004-10-04 07:24:48 +0000 | [diff] [blame] | 105 | |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 106 | if (C->isNullValue() && |
| 107 | (I->hasLinkOnceLinkage() || I->hasInternalLinkage() || |
| 108 | I->hasWeakLinkage() /* FIXME: Verify correct */)) { |
| 109 | SwitchSection(O, CurSection, ".data"); |
Nate Begeman | 72b286b | 2005-07-08 00:23:26 +0000 | [diff] [blame] | 110 | if (!forCygwin && !forDarwin && I->hasInternalLinkage()) |
| 111 | O << "\t.local " << name << "\n"; |
| 112 | if (forDarwin && I->hasInternalLinkage()) |
| 113 | O << "\t.lcomm " << name << "," << Size << "," << Align; |
| 114 | else |
| 115 | O << "\t.comm " << name << "," << Size; |
| 116 | if (!forCygwin && !forDarwin) |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 117 | O << "," << (1 << Align); |
| 118 | O << "\t\t# "; |
| 119 | WriteAsOperand(O, I, true, true, &M); |
| 120 | O << "\n"; |
| 121 | } else { |
| 122 | switch (I->getLinkage()) { |
| 123 | case GlobalValue::LinkOnceLinkage: |
| 124 | case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak. |
| 125 | // Nonnull linkonce -> weak |
| 126 | O << "\t.weak " << name << "\n"; |
| 127 | SwitchSection(O, CurSection, ""); |
| 128 | O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n"; |
| 129 | break; |
| 130 | case GlobalValue::AppendingLinkage: |
| 131 | // FIXME: appending linkage variables should go into a section of |
| 132 | // their name or something. For now, just emit them as external. |
| 133 | case GlobalValue::ExternalLinkage: |
| 134 | // If external or appending, declare as a global symbol |
| 135 | O << "\t.globl " << name << "\n"; |
| 136 | // FALL THROUGH |
| 137 | case GlobalValue::InternalLinkage: |
| 138 | if (C->isNullValue()) |
| 139 | SwitchSection(O, CurSection, ".bss"); |
| 140 | else |
| 141 | SwitchSection(O, CurSection, ".data"); |
| 142 | break; |
| 143 | case GlobalValue::GhostLinkage: |
| 144 | std::cerr << "GhostLinkage cannot appear in X86AsmPrinter!\n"; |
| 145 | abort(); |
| 146 | } |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 147 | |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 148 | emitAlignment(Align); |
| 149 | if (!forCygwin && !forDarwin) { |
| 150 | O << "\t.type " << name << ",@object\n"; |
| 151 | O << "\t.size " << name << "," << Size << "\n"; |
| 152 | } |
| 153 | O << name << ":\t\t\t\t# "; |
| 154 | WriteAsOperand(O, I, true, true, &M); |
| 155 | O << " = "; |
| 156 | WriteAsOperand(O, C, false, false, &M); |
| 157 | O << "\n"; |
| 158 | emitGlobalConstant(C); |
| 159 | } |
Chris Lattner | ac5701c | 2004-10-04 07:24:48 +0000 | [diff] [blame] | 160 | } |
Nate Begeman | 72b286b | 2005-07-08 00:23:26 +0000 | [diff] [blame] | 161 | |
| 162 | if (forDarwin) { |
| 163 | // Output stubs for dynamically-linked functions |
| 164 | unsigned j = 1; |
| 165 | for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end(); |
| 166 | i != e; ++i, ++j) |
| 167 | { |
| 168 | O << "\t.symbol_stub\n"; |
| 169 | O << "L" << *i << "$stub:\n"; |
| 170 | O << "\t.indirect_symbol " << *i << "\n"; |
| 171 | O << "\tjmp\t*L" << j << "$lz\n"; |
| 172 | O << "L" << *i << "$stub_binder:\n"; |
| 173 | O << "\tpushl\t$L" << j << "$lz\n"; |
| 174 | O << "\tjmp\tdyld_stub_binding_helper\n"; |
| 175 | O << "\t.section __DATA, __la_sym_ptr3,lazy_symbol_pointers\n"; |
| 176 | O << "L" << j << "$lz:\n"; |
| 177 | O << "\t.indirect_symbol " << *i << "\n"; |
| 178 | O << "\t.long\tL" << *i << "$stub_binder\n"; |
| 179 | } |
| 180 | |
| 181 | O << "\n"; |
| 182 | |
| 183 | // Output stubs for external global variables |
| 184 | if (GVStubs.begin() != GVStubs.end()) |
| 185 | O << ".data\n.non_lazy_symbol_pointer\n"; |
| 186 | for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end(); |
| 187 | i != e; ++i) { |
| 188 | O << "L" << *i << "$non_lazy_ptr:\n"; |
| 189 | O << "\t.indirect_symbol " << *i << "\n"; |
| 190 | O << "\t.long\t0\n"; |
| 191 | } |
| 192 | |
| 193 | // Output stubs for link-once variables |
| 194 | if (LinkOnceStubs.begin() != LinkOnceStubs.end()) |
| 195 | O << ".data\n.align 2\n"; |
| 196 | for (std::set<std::string>::iterator i = LinkOnceStubs.begin(), |
| 197 | e = LinkOnceStubs.end(); i != e; ++i) { |
| 198 | O << "L" << *i << "$non_lazy_ptr:\n" |
| 199 | << "\t.long\t" << *i << '\n'; |
| 200 | } |
| 201 | } |
Chris Lattner | ac5701c | 2004-10-04 07:24:48 +0000 | [diff] [blame] | 202 | |
| 203 | AsmPrinter::doFinalization(M); |
| 204 | return false; // success |
| 205 | } |
| 206 | |
Chris Lattner | ac5701c | 2004-10-04 07:24:48 +0000 | [diff] [blame] | 207 | /// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code |
| 208 | /// for a MachineFunction to the given output stream, using the given target |
| 209 | /// machine description. |
| 210 | /// |
| 211 | FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){ |
| 212 | switch (AsmWriterFlavor) { |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 213 | default: |
Reid Spencer | 5dc81f6 | 2005-01-23 03:52:14 +0000 | [diff] [blame] | 214 | assert(0 && "Unknown asm flavor!"); |
Chris Lattner | ac5701c | 2004-10-04 07:24:48 +0000 | [diff] [blame] | 215 | case intel: |
| 216 | return new X86IntelAsmPrinter(o, tm); |
| 217 | case att: |
| 218 | return new X86ATTAsmPrinter(o, tm); |
| 219 | } |
Brian Gaeke | 9e474c4 | 2003-06-19 19:32:32 +0000 | [diff] [blame] | 220 | } |