blob: 3fdd7b16672e917be4a60abff47a3d23742ffc88 [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
Jim Laskey563321a2006-09-06 18:34:40 +000029enum AsmWriterFlavorTy { att, intel };
30
Evan Chengc4c62572006-03-13 23:20:37 +000031Statistic<> llvm::EmittedInsts("asm-printer",
32 "Number of machine instrs printed");
Chris Lattner955f0962004-10-04 07:31:08 +000033
Chris Lattnerb36cbd02005-07-01 22:44:09 +000034cl::opt<AsmWriterFlavorTy>
35AsmWriterFlavor("x86-asm-syntax",
Chris Lattnerb12c9fa2005-07-11 06:25:47 +000036 cl::desc("Choose style of code to emit from X86 backend:"),
37 cl::values(
38 clEnumVal(att, " Emit AT&T-style assembly"),
39 clEnumVal(intel, " Emit Intel-style assembly"),
40 clEnumValEnd),
Jeff Cohena6e24d82006-05-04 16:19:27 +000041#ifdef _MSC_VER
42 cl::init(intel)
43#else
44 cl::init(att)
45#endif
46 );
Chris Lattner955f0962004-10-04 07:31:08 +000047
Jim Laskey563321a2006-09-06 18:34:40 +000048X86TargetAsmInfo::X86TargetAsmInfo(X86TargetMachine &TM) {
49 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
Chris Lattnera35a8e82005-11-21 22:39:40 +000050
Jim Laskey563321a2006-09-06 18:34:40 +000051 //FIXME - Should to be simplified.
52
Chris Lattnera35a8e82005-11-21 22:39:40 +000053 switch (Subtarget->TargetType) {
Chris Lattnera35a8e82005-11-21 22:39:40 +000054 case X86Subtarget::isDarwin:
Nate Begeman9d19eb42005-06-30 00:53:20 +000055 AlignmentIsInBytes = false;
Chris Lattner8fccc972005-11-21 19:50:31 +000056 GlobalPrefix = "_";
Nate Begeman9035b992005-07-16 01:59:47 +000057 Data64bitsDirective = 0; // we can't emit a 64-bit unit
58 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
Chris Lattnerc41cc832005-11-21 06:46:22 +000059 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
Chris Lattnerd939f6c2005-11-21 08:32:23 +000060 ConstantPoolSection = "\t.const\n";
Nate Begeman2f1ae882006-07-27 01:13:04 +000061 JumpTableDataSection = "\t.const\n"; // FIXME: depends on PIC mode
Evan Chengd3f69812006-06-29 00:33:06 +000062 FourByteConstantSection = "\t.literal4\n";
63 EightByteConstantSection = "\t.literal8\n";
Chris Lattner8fccc972005-11-21 19:50:31 +000064 LCOMMDirective = "\t.lcomm\t";
65 COMMDirectiveTakesAlignment = false;
Chris Lattnerac2902b2005-11-21 23:06:54 +000066 HasDotTypeDotSizeDirective = false;
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";
Nate Begeman52a51e382006-08-12 21:29:52 +000071 SetDirective = "\t.set";
Jim Laskey563321a2006-09-06 18:34:40 +000072
73 NeedsSet = true;
74 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
75 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
76 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
77 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
78 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
79 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
80 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
81 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
82 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
83 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
84 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
Chris Lattnera35a8e82005-11-21 22:39:40 +000085 break;
86 case X86Subtarget::isCygwin:
Chris Lattner8fccc972005-11-21 19:50:31 +000087 GlobalPrefix = "_";
88 COMMDirectiveTakesAlignment = false;
Chris Lattnerac2902b2005-11-21 23:06:54 +000089 HasDotTypeDotSizeDirective = false;
Evan Cheng932ad512006-05-25 21:59:08 +000090 StaticCtorsSection = "\t.section .ctors,\"aw\"";
91 StaticDtorsSection = "\t.section .dtors,\"aw\"";
Chris Lattnera35a8e82005-11-21 22:39:40 +000092 break;
93 case X86Subtarget::isWindows:
Chris Lattner8fccc972005-11-21 19:50:31 +000094 GlobalPrefix = "_";
Chris Lattnerac2902b2005-11-21 23:06:54 +000095 HasDotTypeDotSizeDirective = false;
Chris Lattnera35a8e82005-11-21 22:39:40 +000096 break;
97 default: break;
98 }
Chris Lattner8fccc972005-11-21 19:50:31 +000099
Jim Laskey563321a2006-09-06 18:34:40 +0000100 if (AsmWriterFlavor == intel) {
101 GlobalPrefix = "_";
102 CommentString = ";";
103
104 PrivateGlobalPrefix = "$";
105 AlignDirective = "\talign\t";
106 ZeroDirective = "\tdb\t";
107 ZeroDirectiveSuffix = " dup(0)";
108 AsciiDirective = "\tdb\t";
109 AscizDirective = 0;
110 Data8bitsDirective = "\tdb\t";
111 Data16bitsDirective = "\tdw\t";
112 Data32bitsDirective = "\tdd\t";
113 Data64bitsDirective = "\tdq\t";
114 HasDotTypeDotSizeDirective = false;
115
116 TextSection = "_text";
117 DataSection = "_data";
118 SwitchToSectionDirective = "";
119 TextSectionStartSuffix = "\tsegment 'CODE'";
120 DataSectionStartSuffix = "\tsegment 'DATA'";
121 SectionEndDirectiveSuffix = "\tends\n";
122 }
123}
124
125/// doInitialization
126bool X86SharedAsmPrinter::doInitialization(Module &M) {
Jim Laskeyea348582006-07-27 02:05:13 +0000127 if (Subtarget->isTargetDarwin()) {
Evan Chengd5948812006-03-07 02:23:26 +0000128 // Emit initial debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000129 DW.BeginModule(&M);
Evan Chengd5948812006-03-07 02:23:26 +0000130 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000131
Reid Spencer5dc81f62005-01-23 03:52:14 +0000132 return AsmPrinter::doInitialization(M);
133}
134
Chris Lattnerac5701c2004-10-04 07:24:48 +0000135bool X86SharedAsmPrinter::doFinalization(Module &M) {
Jeff Cohend43b18d2006-05-06 21:27:14 +0000136 // Note: this code is not shared by the Intel printer as it is too different
137 // from how MASM does things. When making changes here don't forget to look
138 // at X86IntelAsmPrinter::doFinalization().
Owen Andersona69571c2006-05-03 01:29:57 +0000139 const TargetData *TD = TM.getTargetData();
Chris Lattnerac5701c2004-10-04 07:24:48 +0000140
141 // Print out module-level global variables here.
Evan Cheng2338c5c2006-02-07 08:38:37 +0000142 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
143 I != E; ++I) {
Chris Lattner9787c642005-11-21 22:48:18 +0000144 if (!I->hasInitializer()) continue; // External global require no code
145
Chris Lattnerd1239b72005-12-13 06:32:50 +0000146 // Check to see if this is a special global used by LLVM, if so, emit it.
Jim Laskey78098112006-03-07 22:00:35 +0000147 if (EmitSpecialLLVMGlobal(I))
Chris Lattnerd1239b72005-12-13 06:32:50 +0000148 continue;
Chris Lattnera046e0d2005-12-13 04:53:51 +0000149
Chris Lattner9787c642005-11-21 22:48:18 +0000150 std::string name = Mang->getValueName(I);
151 Constant *C = I->getInitializer();
Owen Andersona69571c2006-05-03 01:29:57 +0000152 unsigned Size = TD->getTypeSize(C->getType());
Chris Lattnerff805b52006-02-05 01:45:04 +0000153 unsigned Align = getPreferredAlignmentLog(I);
Jeff Cohen00b168892005-07-27 06:12:32 +0000154
Evan Cheng2338c5c2006-02-07 08:38:37 +0000155 if (C->isNullValue() && /* FIXME: Verify correct */
156 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
Evan Cheng17ef92e2006-02-15 01:56:23 +0000157 I->hasLinkOnceLinkage() ||
Jim Laskeyea348582006-07-27 02:05:13 +0000158 (Subtarget->isTargetDarwin() &&
Evan Cheng932ad512006-05-25 21:59:08 +0000159 I->hasExternalLinkage() && !I->hasSection()))) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000160 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Evan Cheng17ef92e2006-02-15 01:56:23 +0000161 if (I->hasExternalLinkage()) {
Evan Chenga0ea0532006-02-23 02:43:52 +0000162 O << "\t.globl\t" << name << "\n";
Evan Cheng17ef92e2006-02-15 01:56:23 +0000163 O << "\t.zerofill __DATA__, __common, " << name << ", "
164 << Size << ", " << Align;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000165 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000166 SwitchToDataSection(TAI->getDataSection(), I);
167 if (TAI->getLCOMMDirective() != NULL) {
Evan Cheng17ef92e2006-02-15 01:56:23 +0000168 if (I->hasInternalLinkage()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000169 O << TAI->getLCOMMDirective() << name << "," << Size;
Jim Laskeyea348582006-07-27 02:05:13 +0000170 if (Subtarget->isTargetDarwin())
Jim Laskey563321a2006-09-06 18:34:40 +0000171 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Evan Cheng17ef92e2006-02-15 01:56:23 +0000172 } else
Jim Laskey563321a2006-09-06 18:34:40 +0000173 O << TAI->getCOMMDirective() << name << "," << Size;
Evan Cheng17ef92e2006-02-15 01:56:23 +0000174 } else {
Evan Cheng57c07882006-05-26 06:22:34 +0000175 if (Subtarget->TargetType != X86Subtarget::isCygwin) {
Evan Cheng932ad512006-05-25 21:59:08 +0000176 if (I->hasInternalLinkage())
177 O << "\t.local\t" << name << "\n";
178 }
Jim Laskey563321a2006-09-06 18:34:40 +0000179 O << TAI->getCOMMDirective() << name << "," << Size;
180 if (TAI->getCOMMDirectiveTakesAlignment())
181 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Evan Cheng17ef92e2006-02-15 01:56:23 +0000182 }
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000183 }
Jim Laskey563321a2006-09-06 18:34:40 +0000184 O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000185 } else {
186 switch (I->getLinkage()) {
187 case GlobalValue::LinkOnceLinkage:
188 case GlobalValue::WeakLinkage:
Jim Laskeyea348582006-07-27 02:05:13 +0000189 if (Subtarget->isTargetDarwin()) {
Evan Cheng5ada3702006-02-07 23:32:58 +0000190 O << "\t.globl " << name << "\n"
191 << "\t.weak_definition " << name << "\n";
Evan Chengf8614db2006-06-04 07:24:07 +0000192 SwitchToDataSection(".section __DATA,__const_coal,coalesced", I);
Evan Cheng932ad512006-05-25 21:59:08 +0000193 } else if (Subtarget->TargetType == X86Subtarget::isCygwin) {
194 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\"\n"
195 << "\t.weak " << name << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000196 } else {
Evan Cheng932ad512006-05-25 21:59:08 +0000197 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n"
198 << "\t.weak " << name << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000199 }
200 break;
201 case GlobalValue::AppendingLinkage:
202 // FIXME: appending linkage variables should go into a section of
203 // their name or something. For now, just emit them as external.
204 case GlobalValue::ExternalLinkage:
205 // If external or appending, declare as a global symbol
206 O << "\t.globl " << name << "\n";
207 // FALL THROUGH
208 case GlobalValue::InternalLinkage:
Jim Laskey563321a2006-09-06 18:34:40 +0000209 SwitchToDataSection(TAI->getDataSection(), I);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000210 break;
211 default:
212 assert(0 && "Unknown linkage type!");
213 }
Chris Lattner9787c642005-11-21 22:48:18 +0000214
Evan Cheng2338c5c2006-02-07 08:38:37 +0000215 EmitAlignment(Align, I);
Jim Laskey563321a2006-09-06 18:34:40 +0000216 O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName()
Evan Cheng5ada3702006-02-07 23:32:58 +0000217 << "\n";
Jim Laskey563321a2006-09-06 18:34:40 +0000218 if (TAI->hasDotTypeDotSizeDirective())
Evan Cheng5ada3702006-02-07 23:32:58 +0000219 O << "\t.size " << name << ", " << Size << "\n";
220
Evan Cheng2338c5c2006-02-07 08:38:37 +0000221 EmitGlobalConstant(C);
222 O << '\n';
Chris Lattner9787c642005-11-21 22:48:18 +0000223 }
Chris Lattnera35a8e82005-11-21 22:39:40 +0000224 }
225
Jim Laskeyea348582006-07-27 02:05:13 +0000226 if (Subtarget->isTargetDarwin()) {
Chris Lattner4632d7a2006-05-09 04:59:56 +0000227 SwitchToDataSection("", 0);
Nate Begeman73213f62005-07-12 01:37:28 +0000228
Nate Begeman72b286b2005-07-08 00:23:26 +0000229 // Output stubs for dynamically-linked functions
230 unsigned j = 1;
231 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000232 i != e; ++i, ++j) {
Chris Lattner4632d7a2006-05-09 04:59:56 +0000233 SwitchToDataSection(".section __IMPORT,__jump_table,symbol_stubs,"
234 "self_modifying_code+pure_instructions,5", 0);
Nate Begeman72b286b2005-07-08 00:23:26 +0000235 O << "L" << *i << "$stub:\n";
236 O << "\t.indirect_symbol " << *i << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000237 O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Nate Begeman72b286b2005-07-08 00:23:26 +0000238 }
239
240 O << "\n";
Jeff Cohen00b168892005-07-27 06:12:32 +0000241
Evan Cheng2338c5c2006-02-07 08:38:37 +0000242 // Output stubs for external and common global variables.
243 if (GVStubs.begin() != GVStubs.end())
Chris Lattner4632d7a2006-05-09 04:59:56 +0000244 SwitchToDataSection(
245 ".section __IMPORT,__pointers,non_lazy_symbol_pointers", 0);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000246 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
247 i != e; ++i) {
248 O << "L" << *i << "$non_lazy_ptr:\n";
249 O << "\t.indirect_symbol " << *i << "\n";
250 O << "\t.long\t0\n";
Nate Begeman72b286b2005-07-08 00:23:26 +0000251 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000252
Evan Chengd5948812006-03-07 02:23:26 +0000253 // Emit initial debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000254 DW.EndModule();
Evan Chengd5948812006-03-07 02:23:26 +0000255
256 // Funny Darwin hack: This flag tells the linker that no global symbols
257 // contain code that falls through to other global symbols (e.g. the obvious
258 // implementation of multiple entry points). If this doesn't occur, the
Jim Laskey99db0442006-03-23 18:09:44 +0000259 // linker can safely perform dead code stripping. Since LLVM never
260 // generates code that does this, it is always safe to set.
Evan Chengd5948812006-03-07 02:23:26 +0000261 O << "\t.subsections_via_symbols\n";
262 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000263
Chris Lattnerac5701c2004-10-04 07:24:48 +0000264 AsmPrinter::doFinalization(M);
265 return false; // success
266}
267
Chris Lattnerac5701c2004-10-04 07:24:48 +0000268/// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
269/// for a MachineFunction to the given output stream, using the given target
270/// machine description.
271///
Evan Chengc4c62572006-03-13 23:20:37 +0000272FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,
Jim Laskey563321a2006-09-06 18:34:40 +0000273 X86TargetMachine &tm) {
274 TargetAsmInfo *TAI = new X86TargetAsmInfo(tm);
275
Chris Lattnerac5701c2004-10-04 07:24:48 +0000276 switch (AsmWriterFlavor) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000277 default:
Reid Spencer5dc81f62005-01-23 03:52:14 +0000278 assert(0 && "Unknown asm flavor!");
Jim Laskey563321a2006-09-06 18:34:40 +0000279 case intel: return new X86IntelAsmPrinter(o, tm, TAI);
280 case att: return new X86ATTAsmPrinter(o, tm, TAI);
Chris Lattnerac5701c2004-10-04 07:24:48 +0000281 }
Brian Gaeke9e474c42003-06-19 19:32:32 +0000282}