blob: 9e862e8c6fa92e6a22101008e706262d70fb051e [file] [log] [blame]
Chris Lattnerb36cbd02005-07-01 22:44:09 +00001//===-- X86AsmPrinter.cpp - Convert X86 LLVM IR to X86 assembly -----------===//
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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 Brukman0e0a7a452005-04-21 23:38:14 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner72614082002-10-25 22:55:53 +00009//
Chris Lattnerb36cbd02005-07-01 22:44:09 +000010// 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 Lattner72614082002-10-25 22:55:53 +000014//
15//===----------------------------------------------------------------------===//
16
Evan Chengc4c62572006-03-13 23:20:37 +000017#include "X86AsmPrinter.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000018#include "X86ATTAsmPrinter.h"
19#include "X86IntelAsmPrinter.h"
Chris Lattnera35a8e82005-11-21 22:39:40 +000020#include "X86Subtarget.h"
Chris Lattnera046e0d2005-12-13 04:53:51 +000021#include "llvm/Constants.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000022#include "llvm/Module.h"
Chris Lattnerc41cc832005-11-21 06:46:22 +000023#include "llvm/Type.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000024#include "llvm/Assembly/Writer.h"
Brian Gaeked9fb37a2003-07-24 20:20:44 +000025#include "llvm/Support/Mangler.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/Support/CommandLine.h"
Chris Lattner300d0ed2004-02-14 06:00:36 +000027using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Evan Chengc4c62572006-03-13 23:20:37 +000029Statistic<> llvm::EmittedInsts("asm-printer",
30 "Number of machine instrs printed");
Chris Lattner955f0962004-10-04 07:31:08 +000031
Chris Lattnerb36cbd02005-07-01 22:44:09 +000032enum AsmWriterFlavorTy { att, intel };
33cl::opt<AsmWriterFlavorTy>
34AsmWriterFlavor("x86-asm-syntax",
Chris Lattnerb12c9fa2005-07-11 06:25:47 +000035 cl::desc("Choose style of code to emit from X86 backend:"),
36 cl::values(
37 clEnumVal(att, " Emit AT&T-style assembly"),
38 clEnumVal(intel, " Emit Intel-style assembly"),
39 clEnumValEnd),
Jeff Cohena6e24d82006-05-04 16:19:27 +000040#ifdef _MSC_VER
41 cl::init(intel)
42#else
43 cl::init(att)
44#endif
45 );
Chris Lattner955f0962004-10-04 07:31:08 +000046
Chris Lattnera3b59392006-07-14 23:05:05 +000047// Out of line virtual function to home classes.
48void X86DwarfWriter::virtfn() {}
49
50
Chris Lattnerb36cbd02005-07-01 22:44:09 +000051/// doInitialization
Chris Lattner5df14ca2005-11-21 22:19:48 +000052bool X86SharedAsmPrinter::doInitialization(Module &M) {
Chris Lattnere650a6b2006-02-23 05:25:02 +000053 PrivateGlobalPrefix = ".L";
Chris Lattnerdad9c5a2006-05-09 05:12:53 +000054 DefaultTextSection = ".text";
55 DefaultDataSection = ".data";
Chris Lattnera35a8e82005-11-21 22:39:40 +000056
57 switch (Subtarget->TargetType) {
Chris Lattnera35a8e82005-11-21 22:39:40 +000058 case X86Subtarget::isDarwin:
Nate Begeman9d19eb42005-06-30 00:53:20 +000059 AlignmentIsInBytes = false;
Chris Lattner8fccc972005-11-21 19:50:31 +000060 GlobalPrefix = "_";
Nate Begeman9035b992005-07-16 01:59:47 +000061 Data64bitsDirective = 0; // we can't emit a 64-bit unit
62 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
Chris Lattnerc41cc832005-11-21 06:46:22 +000063 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
Chris Lattnerd939f6c2005-11-21 08:32:23 +000064 ConstantPoolSection = "\t.const\n";
Nate Begeman2f1ae882006-07-27 01:13:04 +000065 JumpTableDataSection = "\t.const\n"; // FIXME: depends on PIC mode
Evan Chengd3f69812006-06-29 00:33:06 +000066 FourByteConstantSection = "\t.literal4\n";
67 EightByteConstantSection = "\t.literal8\n";
Chris Lattner8fccc972005-11-21 19:50:31 +000068 LCOMMDirective = "\t.lcomm\t";
69 COMMDirectiveTakesAlignment = false;
Chris Lattnerac2902b2005-11-21 23:06:54 +000070 HasDotTypeDotSizeDirective = false;
Chris Lattnerd1239b72005-12-13 06:32:50 +000071 StaticCtorsSection = ".mod_init_func";
72 StaticDtorsSection = ".mod_term_func";
Chris Lattner7660ebc2006-05-05 21:48:50 +000073 InlineAsmStart = "# InlineAsm Start";
74 InlineAsmEnd = "# InlineAsm End";
Nate Begeman52a51e382006-08-12 21:29:52 +000075 SetDirective = "\t.set";
Chris Lattnera35a8e82005-11-21 22:39:40 +000076 break;
77 case X86Subtarget::isCygwin:
Chris Lattner8fccc972005-11-21 19:50:31 +000078 GlobalPrefix = "_";
79 COMMDirectiveTakesAlignment = false;
Chris Lattnerac2902b2005-11-21 23:06:54 +000080 HasDotTypeDotSizeDirective = false;
Evan Cheng932ad512006-05-25 21:59:08 +000081 StaticCtorsSection = "\t.section .ctors,\"aw\"";
82 StaticDtorsSection = "\t.section .dtors,\"aw\"";
Chris Lattnera35a8e82005-11-21 22:39:40 +000083 break;
84 case X86Subtarget::isWindows:
Chris Lattner8fccc972005-11-21 19:50:31 +000085 GlobalPrefix = "_";
Chris Lattnerac2902b2005-11-21 23:06:54 +000086 HasDotTypeDotSizeDirective = false;
Chris Lattnera35a8e82005-11-21 22:39:40 +000087 break;
88 default: break;
89 }
Chris Lattner8fccc972005-11-21 19:50:31 +000090
Jim Laskeyea348582006-07-27 02:05:13 +000091 if (Subtarget->isTargetDarwin()) {
Evan Chengd5948812006-03-07 02:23:26 +000092 // Emit initial debug information.
Jim Laskey99db0442006-03-23 18:09:44 +000093 DW.BeginModule(&M);
Evan Chengd5948812006-03-07 02:23:26 +000094 }
Evan Cheng3c992d22006-03-07 02:02:57 +000095
Reid Spencer5dc81f62005-01-23 03:52:14 +000096 return AsmPrinter::doInitialization(M);
97}
98
Chris Lattnerac5701c2004-10-04 07:24:48 +000099bool X86SharedAsmPrinter::doFinalization(Module &M) {
Jeff Cohend43b18d2006-05-06 21:27:14 +0000100 // Note: this code is not shared by the Intel printer as it is too different
101 // from how MASM does things. When making changes here don't forget to look
102 // at X86IntelAsmPrinter::doFinalization().
Owen Andersona69571c2006-05-03 01:29:57 +0000103 const TargetData *TD = TM.getTargetData();
Chris Lattnerac5701c2004-10-04 07:24:48 +0000104
105 // Print out module-level global variables here.
Evan Cheng2338c5c2006-02-07 08:38:37 +0000106 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
107 I != E; ++I) {
Chris Lattner9787c642005-11-21 22:48:18 +0000108 if (!I->hasInitializer()) continue; // External global require no code
109
Chris Lattnerd1239b72005-12-13 06:32:50 +0000110 // Check to see if this is a special global used by LLVM, if so, emit it.
Jim Laskey78098112006-03-07 22:00:35 +0000111 if (EmitSpecialLLVMGlobal(I))
Chris Lattnerd1239b72005-12-13 06:32:50 +0000112 continue;
Chris Lattnera046e0d2005-12-13 04:53:51 +0000113
Chris Lattner9787c642005-11-21 22:48:18 +0000114 std::string name = Mang->getValueName(I);
115 Constant *C = I->getInitializer();
Owen Andersona69571c2006-05-03 01:29:57 +0000116 unsigned Size = TD->getTypeSize(C->getType());
Chris Lattnerff805b52006-02-05 01:45:04 +0000117 unsigned Align = getPreferredAlignmentLog(I);
Jeff Cohen00b168892005-07-27 06:12:32 +0000118
Evan Cheng2338c5c2006-02-07 08:38:37 +0000119 if (C->isNullValue() && /* FIXME: Verify correct */
120 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
Evan Cheng17ef92e2006-02-15 01:56:23 +0000121 I->hasLinkOnceLinkage() ||
Jim Laskeyea348582006-07-27 02:05:13 +0000122 (Subtarget->isTargetDarwin() &&
Evan Cheng932ad512006-05-25 21:59:08 +0000123 I->hasExternalLinkage() && !I->hasSection()))) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000124 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Evan Cheng17ef92e2006-02-15 01:56:23 +0000125 if (I->hasExternalLinkage()) {
Evan Chenga0ea0532006-02-23 02:43:52 +0000126 O << "\t.globl\t" << name << "\n";
Evan Cheng17ef92e2006-02-15 01:56:23 +0000127 O << "\t.zerofill __DATA__, __common, " << name << ", "
128 << Size << ", " << Align;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000129 } else {
Chris Lattnerdad9c5a2006-05-09 05:12:53 +0000130 SwitchToDataSection(DefaultDataSection, I);
Evan Cheng17ef92e2006-02-15 01:56:23 +0000131 if (LCOMMDirective != NULL) {
132 if (I->hasInternalLinkage()) {
133 O << LCOMMDirective << name << "," << Size;
Jim Laskeyea348582006-07-27 02:05:13 +0000134 if (Subtarget->isTargetDarwin())
Evan Cheng17ef92e2006-02-15 01:56:23 +0000135 O << "," << (AlignmentIsInBytes ? (1 << Align) : Align);
136 } else
137 O << COMMDirective << name << "," << Size;
138 } else {
Evan Cheng57c07882006-05-26 06:22:34 +0000139 if (Subtarget->TargetType != X86Subtarget::isCygwin) {
Evan Cheng932ad512006-05-25 21:59:08 +0000140 if (I->hasInternalLinkage())
141 O << "\t.local\t" << name << "\n";
142 }
Evan Cheng17ef92e2006-02-15 01:56:23 +0000143 O << COMMDirective << name << "," << Size;
144 if (COMMDirectiveTakesAlignment)
145 O << "," << (AlignmentIsInBytes ? (1 << Align) : Align);
146 }
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000147 }
Evan Chenga0ea0532006-02-23 02:43:52 +0000148 O << "\t\t" << CommentString << " " << I->getName() << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000149 } else {
150 switch (I->getLinkage()) {
151 case GlobalValue::LinkOnceLinkage:
152 case GlobalValue::WeakLinkage:
Jim Laskeyea348582006-07-27 02:05:13 +0000153 if (Subtarget->isTargetDarwin()) {
Evan Cheng5ada3702006-02-07 23:32:58 +0000154 O << "\t.globl " << name << "\n"
155 << "\t.weak_definition " << name << "\n";
Evan Chengf8614db2006-06-04 07:24:07 +0000156 SwitchToDataSection(".section __DATA,__const_coal,coalesced", I);
Evan Cheng932ad512006-05-25 21:59:08 +0000157 } else if (Subtarget->TargetType == X86Subtarget::isCygwin) {
158 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\"\n"
159 << "\t.weak " << name << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000160 } else {
Evan Cheng932ad512006-05-25 21:59:08 +0000161 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n"
162 << "\t.weak " << name << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000163 }
164 break;
165 case GlobalValue::AppendingLinkage:
166 // FIXME: appending linkage variables should go into a section of
167 // their name or something. For now, just emit them as external.
168 case GlobalValue::ExternalLinkage:
169 // If external or appending, declare as a global symbol
170 O << "\t.globl " << name << "\n";
171 // FALL THROUGH
172 case GlobalValue::InternalLinkage:
Chris Lattnerdad9c5a2006-05-09 05:12:53 +0000173 SwitchToDataSection(DefaultDataSection, I);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000174 break;
175 default:
176 assert(0 && "Unknown linkage type!");
177 }
Chris Lattner9787c642005-11-21 22:48:18 +0000178
Evan Cheng2338c5c2006-02-07 08:38:37 +0000179 EmitAlignment(Align, I);
Evan Cheng5ada3702006-02-07 23:32:58 +0000180 O << name << ":\t\t\t\t" << CommentString << " " << I->getName()
181 << "\n";
182 if (HasDotTypeDotSizeDirective)
183 O << "\t.size " << name << ", " << Size << "\n";
184
Evan Cheng2338c5c2006-02-07 08:38:37 +0000185 EmitGlobalConstant(C);
186 O << '\n';
Chris Lattner9787c642005-11-21 22:48:18 +0000187 }
Chris Lattnera35a8e82005-11-21 22:39:40 +0000188 }
189
Jim Laskeyea348582006-07-27 02:05:13 +0000190 if (Subtarget->isTargetDarwin()) {
Chris Lattner4632d7a2006-05-09 04:59:56 +0000191 SwitchToDataSection("", 0);
Nate Begeman73213f62005-07-12 01:37:28 +0000192
Nate Begeman72b286b2005-07-08 00:23:26 +0000193 // Output stubs for dynamically-linked functions
194 unsigned j = 1;
195 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000196 i != e; ++i, ++j) {
Chris Lattner4632d7a2006-05-09 04:59:56 +0000197 SwitchToDataSection(".section __IMPORT,__jump_table,symbol_stubs,"
198 "self_modifying_code+pure_instructions,5", 0);
Nate Begeman72b286b2005-07-08 00:23:26 +0000199 O << "L" << *i << "$stub:\n";
200 O << "\t.indirect_symbol " << *i << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000201 O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Nate Begeman72b286b2005-07-08 00:23:26 +0000202 }
203
204 O << "\n";
Jeff Cohen00b168892005-07-27 06:12:32 +0000205
Evan Cheng2338c5c2006-02-07 08:38:37 +0000206 // Output stubs for external and common global variables.
207 if (GVStubs.begin() != GVStubs.end())
Chris Lattner4632d7a2006-05-09 04:59:56 +0000208 SwitchToDataSection(
209 ".section __IMPORT,__pointers,non_lazy_symbol_pointers", 0);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000210 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
211 i != e; ++i) {
212 O << "L" << *i << "$non_lazy_ptr:\n";
213 O << "\t.indirect_symbol " << *i << "\n";
214 O << "\t.long\t0\n";
Nate Begeman72b286b2005-07-08 00:23:26 +0000215 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000216
Evan Chengd5948812006-03-07 02:23:26 +0000217 // Emit initial debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000218 DW.EndModule();
Evan Chengd5948812006-03-07 02:23:26 +0000219
220 // Funny Darwin hack: This flag tells the linker that no global symbols
221 // contain code that falls through to other global symbols (e.g. the obvious
222 // implementation of multiple entry points). If this doesn't occur, the
Jim Laskey99db0442006-03-23 18:09:44 +0000223 // linker can safely perform dead code stripping. Since LLVM never
224 // generates code that does this, it is always safe to set.
Evan Chengd5948812006-03-07 02:23:26 +0000225 O << "\t.subsections_via_symbols\n";
226 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000227
Chris Lattnerac5701c2004-10-04 07:24:48 +0000228 AsmPrinter::doFinalization(M);
229 return false; // success
230}
231
Chris Lattnerac5701c2004-10-04 07:24:48 +0000232/// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
233/// for a MachineFunction to the given output stream, using the given target
234/// machine description.
235///
Evan Chengc4c62572006-03-13 23:20:37 +0000236FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,
237 X86TargetMachine &tm){
Chris Lattnerac5701c2004-10-04 07:24:48 +0000238 switch (AsmWriterFlavor) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000239 default:
Reid Spencer5dc81f62005-01-23 03:52:14 +0000240 assert(0 && "Unknown asm flavor!");
Chris Lattnerac5701c2004-10-04 07:24:48 +0000241 case intel:
242 return new X86IntelAsmPrinter(o, tm);
243 case att:
244 return new X86ATTAsmPrinter(o, tm);
245 }
Brian Gaeke9e474c42003-06-19 19:32:32 +0000246}