blob: 3ac322bfef4e83e2ef04dd788c46284d54307d5a [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 Lattnerb36cbd02005-07-01 22:44:09 +000047/// doInitialization
Chris Lattner5df14ca2005-11-21 22:19:48 +000048bool X86SharedAsmPrinter::doInitialization(Module &M) {
Chris Lattnera35a8e82005-11-21 22:39:40 +000049 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
50
Chris Lattner8fccc972005-11-21 19:50:31 +000051 forDarwin = false;
Chris Lattnere650a6b2006-02-23 05:25:02 +000052 PrivateGlobalPrefix = ".L";
Chris Lattnera35a8e82005-11-21 22:39:40 +000053
54 switch (Subtarget->TargetType) {
Chris Lattnera35a8e82005-11-21 22:39:40 +000055 case X86Subtarget::isDarwin:
Nate Begeman9d19eb42005-06-30 00:53:20 +000056 AlignmentIsInBytes = false;
Chris Lattner8fccc972005-11-21 19:50:31 +000057 GlobalPrefix = "_";
Nate Begeman9035b992005-07-16 01:59:47 +000058 Data64bitsDirective = 0; // we can't emit a 64-bit unit
59 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
Chris Lattnerc41cc832005-11-21 06:46:22 +000060 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
Chris Lattnerd939f6c2005-11-21 08:32:23 +000061 ConstantPoolSection = "\t.const\n";
Nate Begeman37efe672006-04-22 18:53:45 +000062 JumpTableSection = "\t.const\n"; // FIXME: depends on PIC mode
Chris Lattner8fccc972005-11-21 19:50:31 +000063 LCOMMDirective = "\t.lcomm\t";
64 COMMDirectiveTakesAlignment = false;
Chris Lattnerac2902b2005-11-21 23:06:54 +000065 HasDotTypeDotSizeDirective = false;
Chris Lattnera35a8e82005-11-21 22:39:40 +000066 forDarwin = true;
Chris Lattnerd1239b72005-12-13 06:32:50 +000067 StaticCtorsSection = ".mod_init_func";
68 StaticDtorsSection = ".mod_term_func";
Chris Lattner7660ebc2006-05-05 21:48:50 +000069 InlineAsmStart = "# InlineAsm Start";
70 InlineAsmEnd = "# InlineAsm End";
Chris Lattnera35a8e82005-11-21 22:39:40 +000071 break;
72 case X86Subtarget::isCygwin:
Chris Lattner8fccc972005-11-21 19:50:31 +000073 GlobalPrefix = "_";
74 COMMDirectiveTakesAlignment = false;
Chris Lattnerac2902b2005-11-21 23:06:54 +000075 HasDotTypeDotSizeDirective = false;
Chris Lattnera35a8e82005-11-21 22:39:40 +000076 break;
77 case X86Subtarget::isWindows:
Chris Lattner8fccc972005-11-21 19:50:31 +000078 GlobalPrefix = "_";
Chris Lattnerac2902b2005-11-21 23:06:54 +000079 HasDotTypeDotSizeDirective = false;
Chris Lattnera35a8e82005-11-21 22:39:40 +000080 break;
81 default: break;
82 }
Chris Lattner8fccc972005-11-21 19:50:31 +000083
Evan Chengd5948812006-03-07 02:23:26 +000084 if (forDarwin) {
85 // Emit initial debug information.
Jim Laskey99db0442006-03-23 18:09:44 +000086 DW.BeginModule(&M);
Evan Chengd5948812006-03-07 02:23:26 +000087 }
Evan Cheng3c992d22006-03-07 02:02:57 +000088
Reid Spencer5dc81f62005-01-23 03:52:14 +000089 return AsmPrinter::doInitialization(M);
90}
91
Chris Lattnerac5701c2004-10-04 07:24:48 +000092bool X86SharedAsmPrinter::doFinalization(Module &M) {
Jeff Cohend43b18d2006-05-06 21:27:14 +000093 // Note: this code is not shared by the Intel printer as it is too different
94 // from how MASM does things. When making changes here don't forget to look
95 // at X86IntelAsmPrinter::doFinalization().
Owen Andersona69571c2006-05-03 01:29:57 +000096 const TargetData *TD = TM.getTargetData();
Chris Lattnerac5701c2004-10-04 07:24:48 +000097
98 // Print out module-level global variables here.
Evan Cheng2338c5c2006-02-07 08:38:37 +000099 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
100 I != E; ++I) {
Chris Lattner9787c642005-11-21 22:48:18 +0000101 if (!I->hasInitializer()) continue; // External global require no code
102
Chris Lattnerd1239b72005-12-13 06:32:50 +0000103 // Check to see if this is a special global used by LLVM, if so, emit it.
Jim Laskey78098112006-03-07 22:00:35 +0000104 if (EmitSpecialLLVMGlobal(I))
Chris Lattnerd1239b72005-12-13 06:32:50 +0000105 continue;
Chris Lattnera046e0d2005-12-13 04:53:51 +0000106
Chris Lattner9787c642005-11-21 22:48:18 +0000107 std::string name = Mang->getValueName(I);
108 Constant *C = I->getInitializer();
Owen Andersona69571c2006-05-03 01:29:57 +0000109 unsigned Size = TD->getTypeSize(C->getType());
Chris Lattnerff805b52006-02-05 01:45:04 +0000110 unsigned Align = getPreferredAlignmentLog(I);
Jeff Cohen00b168892005-07-27 06:12:32 +0000111
Evan Cheng2338c5c2006-02-07 08:38:37 +0000112 if (C->isNullValue() && /* FIXME: Verify correct */
113 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
Evan Cheng17ef92e2006-02-15 01:56:23 +0000114 I->hasLinkOnceLinkage() ||
115 (forDarwin && I->hasExternalLinkage() && !I->hasSection()))) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000116 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Evan Cheng17ef92e2006-02-15 01:56:23 +0000117 if (I->hasExternalLinkage()) {
Evan Chenga0ea0532006-02-23 02:43:52 +0000118 O << "\t.globl\t" << name << "\n";
Evan Cheng17ef92e2006-02-15 01:56:23 +0000119 O << "\t.zerofill __DATA__, __common, " << name << ", "
120 << Size << ", " << Align;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000121 } else {
Evan Cheng17ef92e2006-02-15 01:56:23 +0000122 SwitchSection(".data", I);
123 if (LCOMMDirective != NULL) {
124 if (I->hasInternalLinkage()) {
125 O << LCOMMDirective << name << "," << Size;
126 if (forDarwin)
127 O << "," << (AlignmentIsInBytes ? (1 << Align) : Align);
128 } else
129 O << COMMDirective << name << "," << Size;
130 } else {
131 if (I->hasInternalLinkage())
132 O << "\t.local\t" << name << "\n";
133 O << COMMDirective << name << "," << Size;
134 if (COMMDirectiveTakesAlignment)
135 O << "," << (AlignmentIsInBytes ? (1 << Align) : Align);
136 }
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000137 }
Evan Chenga0ea0532006-02-23 02:43:52 +0000138 O << "\t\t" << CommentString << " " << I->getName() << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000139 } else {
140 switch (I->getLinkage()) {
141 case GlobalValue::LinkOnceLinkage:
142 case GlobalValue::WeakLinkage:
143 if (forDarwin) {
Evan Cheng5ada3702006-02-07 23:32:58 +0000144 O << "\t.globl " << name << "\n"
145 << "\t.weak_definition " << name << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000146 SwitchSection(".section __DATA,__datacoal_nt,coalesced", I);
147 } else {
148 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
149 O << "\t.weak " << name << "\n";
150 }
151 break;
152 case GlobalValue::AppendingLinkage:
153 // FIXME: appending linkage variables should go into a section of
154 // their name or something. For now, just emit them as external.
155 case GlobalValue::ExternalLinkage:
156 // If external or appending, declare as a global symbol
157 O << "\t.globl " << name << "\n";
158 // FALL THROUGH
159 case GlobalValue::InternalLinkage:
160 SwitchSection(".data", I);
161 break;
162 default:
163 assert(0 && "Unknown linkage type!");
164 }
Chris Lattner9787c642005-11-21 22:48:18 +0000165
Evan Cheng2338c5c2006-02-07 08:38:37 +0000166 EmitAlignment(Align, I);
Evan Cheng5ada3702006-02-07 23:32:58 +0000167 O << name << ":\t\t\t\t" << CommentString << " " << I->getName()
168 << "\n";
169 if (HasDotTypeDotSizeDirective)
170 O << "\t.size " << name << ", " << Size << "\n";
171
Evan Cheng2338c5c2006-02-07 08:38:37 +0000172 EmitGlobalConstant(C);
173 O << '\n';
Chris Lattner9787c642005-11-21 22:48:18 +0000174 }
Chris Lattnera35a8e82005-11-21 22:39:40 +0000175 }
176
Nate Begeman72b286b2005-07-08 00:23:26 +0000177 if (forDarwin) {
Chris Lattner7b6e53c2005-11-21 07:16:34 +0000178 SwitchSection("", 0);
Nate Begeman73213f62005-07-12 01:37:28 +0000179
Nate Begeman72b286b2005-07-08 00:23:26 +0000180 // Output stubs for dynamically-linked functions
181 unsigned j = 1;
182 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000183 i != e; ++i, ++j) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000184 SwitchSection(".section __IMPORT,__jump_table,symbol_stubs,"
185 "self_modifying_code+pure_instructions,5", 0);
Nate Begeman72b286b2005-07-08 00:23:26 +0000186 O << "L" << *i << "$stub:\n";
187 O << "\t.indirect_symbol " << *i << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000188 O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Nate Begeman72b286b2005-07-08 00:23:26 +0000189 }
190
191 O << "\n";
Jeff Cohen00b168892005-07-27 06:12:32 +0000192
Evan Cheng2338c5c2006-02-07 08:38:37 +0000193 // Output stubs for external and common global variables.
194 if (GVStubs.begin() != GVStubs.end())
195 SwitchSection(".section __IMPORT,__pointers,non_lazy_symbol_pointers", 0);
196 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
197 i != e; ++i) {
198 O << "L" << *i << "$non_lazy_ptr:\n";
199 O << "\t.indirect_symbol " << *i << "\n";
200 O << "\t.long\t0\n";
Nate Begeman72b286b2005-07-08 00:23:26 +0000201 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000202
Evan Chengd5948812006-03-07 02:23:26 +0000203 // Emit initial debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000204 DW.EndModule();
Evan Chengd5948812006-03-07 02:23:26 +0000205
206 // Funny Darwin hack: This flag tells the linker that no global symbols
207 // contain code that falls through to other global symbols (e.g. the obvious
208 // implementation of multiple entry points). If this doesn't occur, the
Jim Laskey99db0442006-03-23 18:09:44 +0000209 // linker can safely perform dead code stripping. Since LLVM never
210 // generates code that does this, it is always safe to set.
Evan Chengd5948812006-03-07 02:23:26 +0000211 O << "\t.subsections_via_symbols\n";
212 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000213
Chris Lattnerac5701c2004-10-04 07:24:48 +0000214 AsmPrinter::doFinalization(M);
215 return false; // success
216}
217
Chris Lattnerac5701c2004-10-04 07:24:48 +0000218/// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
219/// for a MachineFunction to the given output stream, using the given target
220/// machine description.
221///
Evan Chengc4c62572006-03-13 23:20:37 +0000222FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,
223 X86TargetMachine &tm){
Chris Lattnerac5701c2004-10-04 07:24:48 +0000224 switch (AsmWriterFlavor) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000225 default:
Reid Spencer5dc81f62005-01-23 03:52:14 +0000226 assert(0 && "Unknown asm flavor!");
Chris Lattnerac5701c2004-10-04 07:24:48 +0000227 case intel:
228 return new X86IntelAsmPrinter(o, tm);
229 case att:
230 return new X86ATTAsmPrinter(o, tm);
231 }
Brian Gaeke9e474c42003-06-19 19:32:32 +0000232}