blob: 2e32f34506b6253d0adfbf44e68cc28935df5259 [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"
Chris Lattner300d0ed2004-02-14 06:00:36 +000026using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Evan Chengc4c62572006-03-13 23:20:37 +000028Statistic<> llvm::EmittedInsts("asm-printer",
29 "Number of machine instrs printed");
Chris Lattner955f0962004-10-04 07:31:08 +000030
Jim Laskey563321a2006-09-06 18:34:40 +000031X86TargetAsmInfo::X86TargetAsmInfo(X86TargetMachine &TM) {
32 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
Chris Lattnera35a8e82005-11-21 22:39:40 +000033
Jim Laskey563321a2006-09-06 18:34:40 +000034 //FIXME - Should to be simplified.
35
Chris Lattnera35a8e82005-11-21 22:39:40 +000036 switch (Subtarget->TargetType) {
Chris Lattnera35a8e82005-11-21 22:39:40 +000037 case X86Subtarget::isDarwin:
Nate Begeman9d19eb42005-06-30 00:53:20 +000038 AlignmentIsInBytes = false;
Chris Lattner8fccc972005-11-21 19:50:31 +000039 GlobalPrefix = "_";
Nate Begeman9035b992005-07-16 01:59:47 +000040 Data64bitsDirective = 0; // we can't emit a 64-bit unit
41 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
Chris Lattnerc41cc832005-11-21 06:46:22 +000042 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
Chris Lattnerd939f6c2005-11-21 08:32:23 +000043 ConstantPoolSection = "\t.const\n";
Nate Begeman2f1ae882006-07-27 01:13:04 +000044 JumpTableDataSection = "\t.const\n"; // FIXME: depends on PIC mode
Evan Chengd3f69812006-06-29 00:33:06 +000045 FourByteConstantSection = "\t.literal4\n";
46 EightByteConstantSection = "\t.literal8\n";
Chris Lattner8fccc972005-11-21 19:50:31 +000047 LCOMMDirective = "\t.lcomm\t";
48 COMMDirectiveTakesAlignment = false;
Chris Lattnerac2902b2005-11-21 23:06:54 +000049 HasDotTypeDotSizeDirective = false;
Chris Lattnerd1239b72005-12-13 06:32:50 +000050 StaticCtorsSection = ".mod_init_func";
51 StaticDtorsSection = ".mod_term_func";
Chris Lattner7660ebc2006-05-05 21:48:50 +000052 InlineAsmStart = "# InlineAsm Start";
53 InlineAsmEnd = "# InlineAsm End";
Nate Begeman52a51e382006-08-12 21:29:52 +000054 SetDirective = "\t.set";
Jim Laskey563321a2006-09-06 18:34:40 +000055
56 NeedsSet = true;
57 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
58 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
59 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
60 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
61 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
62 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
63 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
64 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
65 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
66 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
67 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
Chris Lattnera35a8e82005-11-21 22:39:40 +000068 break;
69 case X86Subtarget::isCygwin:
Chris Lattner8fccc972005-11-21 19:50:31 +000070 GlobalPrefix = "_";
71 COMMDirectiveTakesAlignment = false;
Chris Lattnerac2902b2005-11-21 23:06:54 +000072 HasDotTypeDotSizeDirective = false;
Evan Cheng932ad512006-05-25 21:59:08 +000073 StaticCtorsSection = "\t.section .ctors,\"aw\"";
74 StaticDtorsSection = "\t.section .dtors,\"aw\"";
Chris Lattnera35a8e82005-11-21 22:39:40 +000075 break;
76 case X86Subtarget::isWindows:
Chris Lattner8fccc972005-11-21 19:50:31 +000077 GlobalPrefix = "_";
Chris Lattnerac2902b2005-11-21 23:06:54 +000078 HasDotTypeDotSizeDirective = false;
Chris Lattnera35a8e82005-11-21 22:39:40 +000079 break;
80 default: break;
81 }
Chris Lattner8fccc972005-11-21 19:50:31 +000082
Jim Laskey05a059d2006-09-07 12:23:47 +000083 if (Subtarget->isFlavorIntel()) {
Jim Laskey563321a2006-09-06 18:34:40 +000084 GlobalPrefix = "_";
85 CommentString = ";";
86
87 PrivateGlobalPrefix = "$";
88 AlignDirective = "\talign\t";
89 ZeroDirective = "\tdb\t";
90 ZeroDirectiveSuffix = " dup(0)";
91 AsciiDirective = "\tdb\t";
92 AscizDirective = 0;
93 Data8bitsDirective = "\tdb\t";
94 Data16bitsDirective = "\tdw\t";
95 Data32bitsDirective = "\tdd\t";
96 Data64bitsDirective = "\tdq\t";
97 HasDotTypeDotSizeDirective = false;
98
99 TextSection = "_text";
100 DataSection = "_data";
101 SwitchToSectionDirective = "";
102 TextSectionStartSuffix = "\tsegment 'CODE'";
103 DataSectionStartSuffix = "\tsegment 'DATA'";
104 SectionEndDirectiveSuffix = "\tends\n";
105 }
106}
107
108/// doInitialization
109bool X86SharedAsmPrinter::doInitialization(Module &M) {
Jim Laskeyea348582006-07-27 02:05:13 +0000110 if (Subtarget->isTargetDarwin()) {
Evan Chengd5948812006-03-07 02:23:26 +0000111 // Emit initial debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000112 DW.BeginModule(&M);
Evan Chengd5948812006-03-07 02:23:26 +0000113 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000114
Reid Spencer5dc81f62005-01-23 03:52:14 +0000115 return AsmPrinter::doInitialization(M);
116}
117
Chris Lattnerac5701c2004-10-04 07:24:48 +0000118bool X86SharedAsmPrinter::doFinalization(Module &M) {
Jeff Cohend43b18d2006-05-06 21:27:14 +0000119 // Note: this code is not shared by the Intel printer as it is too different
120 // from how MASM does things. When making changes here don't forget to look
121 // at X86IntelAsmPrinter::doFinalization().
Owen Andersona69571c2006-05-03 01:29:57 +0000122 const TargetData *TD = TM.getTargetData();
Chris Lattnerac5701c2004-10-04 07:24:48 +0000123
124 // Print out module-level global variables here.
Evan Cheng2338c5c2006-02-07 08:38:37 +0000125 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
126 I != E; ++I) {
Chris Lattner9787c642005-11-21 22:48:18 +0000127 if (!I->hasInitializer()) continue; // External global require no code
128
Chris Lattnerd1239b72005-12-13 06:32:50 +0000129 // Check to see if this is a special global used by LLVM, if so, emit it.
Jim Laskey78098112006-03-07 22:00:35 +0000130 if (EmitSpecialLLVMGlobal(I))
Chris Lattnerd1239b72005-12-13 06:32:50 +0000131 continue;
Chris Lattnera046e0d2005-12-13 04:53:51 +0000132
Chris Lattner9787c642005-11-21 22:48:18 +0000133 std::string name = Mang->getValueName(I);
134 Constant *C = I->getInitializer();
Owen Andersona69571c2006-05-03 01:29:57 +0000135 unsigned Size = TD->getTypeSize(C->getType());
Chris Lattnerff805b52006-02-05 01:45:04 +0000136 unsigned Align = getPreferredAlignmentLog(I);
Jeff Cohen00b168892005-07-27 06:12:32 +0000137
Evan Cheng2338c5c2006-02-07 08:38:37 +0000138 if (C->isNullValue() && /* FIXME: Verify correct */
139 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
Evan Cheng17ef92e2006-02-15 01:56:23 +0000140 I->hasLinkOnceLinkage() ||
Jim Laskeyea348582006-07-27 02:05:13 +0000141 (Subtarget->isTargetDarwin() &&
Evan Cheng932ad512006-05-25 21:59:08 +0000142 I->hasExternalLinkage() && !I->hasSection()))) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000143 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Evan Cheng17ef92e2006-02-15 01:56:23 +0000144 if (I->hasExternalLinkage()) {
Evan Chenga0ea0532006-02-23 02:43:52 +0000145 O << "\t.globl\t" << name << "\n";
Evan Cheng17ef92e2006-02-15 01:56:23 +0000146 O << "\t.zerofill __DATA__, __common, " << name << ", "
147 << Size << ", " << Align;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000148 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000149 SwitchToDataSection(TAI->getDataSection(), I);
150 if (TAI->getLCOMMDirective() != NULL) {
Evan Cheng17ef92e2006-02-15 01:56:23 +0000151 if (I->hasInternalLinkage()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000152 O << TAI->getLCOMMDirective() << name << "," << Size;
Jim Laskeyea348582006-07-27 02:05:13 +0000153 if (Subtarget->isTargetDarwin())
Jim Laskey563321a2006-09-06 18:34:40 +0000154 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Evan Cheng17ef92e2006-02-15 01:56:23 +0000155 } else
Jim Laskey563321a2006-09-06 18:34:40 +0000156 O << TAI->getCOMMDirective() << name << "," << Size;
Evan Cheng17ef92e2006-02-15 01:56:23 +0000157 } else {
Evan Cheng57c07882006-05-26 06:22:34 +0000158 if (Subtarget->TargetType != X86Subtarget::isCygwin) {
Evan Cheng932ad512006-05-25 21:59:08 +0000159 if (I->hasInternalLinkage())
160 O << "\t.local\t" << name << "\n";
161 }
Jim Laskey563321a2006-09-06 18:34:40 +0000162 O << TAI->getCOMMDirective() << name << "," << Size;
163 if (TAI->getCOMMDirectiveTakesAlignment())
164 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Evan Cheng17ef92e2006-02-15 01:56:23 +0000165 }
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000166 }
Jim Laskey563321a2006-09-06 18:34:40 +0000167 O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000168 } else {
169 switch (I->getLinkage()) {
170 case GlobalValue::LinkOnceLinkage:
171 case GlobalValue::WeakLinkage:
Jim Laskeyea348582006-07-27 02:05:13 +0000172 if (Subtarget->isTargetDarwin()) {
Evan Cheng5ada3702006-02-07 23:32:58 +0000173 O << "\t.globl " << name << "\n"
174 << "\t.weak_definition " << name << "\n";
Evan Chengf8614db2006-06-04 07:24:07 +0000175 SwitchToDataSection(".section __DATA,__const_coal,coalesced", I);
Evan Cheng932ad512006-05-25 21:59:08 +0000176 } else if (Subtarget->TargetType == X86Subtarget::isCygwin) {
177 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\"\n"
178 << "\t.weak " << name << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000179 } else {
Evan Cheng932ad512006-05-25 21:59:08 +0000180 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n"
181 << "\t.weak " << name << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000182 }
183 break;
184 case GlobalValue::AppendingLinkage:
185 // FIXME: appending linkage variables should go into a section of
186 // their name or something. For now, just emit them as external.
187 case GlobalValue::ExternalLinkage:
188 // If external or appending, declare as a global symbol
189 O << "\t.globl " << name << "\n";
190 // FALL THROUGH
191 case GlobalValue::InternalLinkage:
Jim Laskey563321a2006-09-06 18:34:40 +0000192 SwitchToDataSection(TAI->getDataSection(), I);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000193 break;
194 default:
195 assert(0 && "Unknown linkage type!");
196 }
Chris Lattner9787c642005-11-21 22:48:18 +0000197
Evan Cheng2338c5c2006-02-07 08:38:37 +0000198 EmitAlignment(Align, I);
Jim Laskey563321a2006-09-06 18:34:40 +0000199 O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName()
Evan Cheng5ada3702006-02-07 23:32:58 +0000200 << "\n";
Jim Laskey563321a2006-09-06 18:34:40 +0000201 if (TAI->hasDotTypeDotSizeDirective())
Evan Cheng5ada3702006-02-07 23:32:58 +0000202 O << "\t.size " << name << ", " << Size << "\n";
203
Evan Cheng2338c5c2006-02-07 08:38:37 +0000204 EmitGlobalConstant(C);
205 O << '\n';
Chris Lattner9787c642005-11-21 22:48:18 +0000206 }
Chris Lattnera35a8e82005-11-21 22:39:40 +0000207 }
208
Jim Laskeyea348582006-07-27 02:05:13 +0000209 if (Subtarget->isTargetDarwin()) {
Chris Lattner4632d7a2006-05-09 04:59:56 +0000210 SwitchToDataSection("", 0);
Nate Begeman73213f62005-07-12 01:37:28 +0000211
Nate Begeman72b286b2005-07-08 00:23:26 +0000212 // Output stubs for dynamically-linked functions
213 unsigned j = 1;
214 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000215 i != e; ++i, ++j) {
Chris Lattner4632d7a2006-05-09 04:59:56 +0000216 SwitchToDataSection(".section __IMPORT,__jump_table,symbol_stubs,"
217 "self_modifying_code+pure_instructions,5", 0);
Nate Begeman72b286b2005-07-08 00:23:26 +0000218 O << "L" << *i << "$stub:\n";
219 O << "\t.indirect_symbol " << *i << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000220 O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Nate Begeman72b286b2005-07-08 00:23:26 +0000221 }
222
223 O << "\n";
Jeff Cohen00b168892005-07-27 06:12:32 +0000224
Evan Cheng2338c5c2006-02-07 08:38:37 +0000225 // Output stubs for external and common global variables.
226 if (GVStubs.begin() != GVStubs.end())
Chris Lattner4632d7a2006-05-09 04:59:56 +0000227 SwitchToDataSection(
228 ".section __IMPORT,__pointers,non_lazy_symbol_pointers", 0);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000229 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
230 i != e; ++i) {
231 O << "L" << *i << "$non_lazy_ptr:\n";
232 O << "\t.indirect_symbol " << *i << "\n";
233 O << "\t.long\t0\n";
Nate Begeman72b286b2005-07-08 00:23:26 +0000234 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000235
Evan Chengd5948812006-03-07 02:23:26 +0000236 // Emit initial debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000237 DW.EndModule();
Evan Chengd5948812006-03-07 02:23:26 +0000238
239 // Funny Darwin hack: This flag tells the linker that no global symbols
240 // contain code that falls through to other global symbols (e.g. the obvious
241 // implementation of multiple entry points). If this doesn't occur, the
Jim Laskey99db0442006-03-23 18:09:44 +0000242 // linker can safely perform dead code stripping. Since LLVM never
243 // generates code that does this, it is always safe to set.
Evan Chengd5948812006-03-07 02:23:26 +0000244 O << "\t.subsections_via_symbols\n";
245 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000246
Chris Lattnerac5701c2004-10-04 07:24:48 +0000247 AsmPrinter::doFinalization(M);
248 return false; // success
249}
250
Chris Lattnerac5701c2004-10-04 07:24:48 +0000251/// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
252/// for a MachineFunction to the given output stream, using the given target
253/// machine description.
254///
Evan Chengc4c62572006-03-13 23:20:37 +0000255FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,
Jim Laskey563321a2006-09-06 18:34:40 +0000256 X86TargetMachine &tm) {
Jim Laskey05a059d2006-09-07 12:23:47 +0000257 const X86Subtarget *Subtarget = &tm.getSubtarget<X86Subtarget>();
Jim Laskey563321a2006-09-06 18:34:40 +0000258 TargetAsmInfo *TAI = new X86TargetAsmInfo(tm);
259
Jim Laskey05a059d2006-09-07 12:23:47 +0000260 if (Subtarget->isFlavorIntel()) {
261 return new X86IntelAsmPrinter(o, tm, TAI);
262 } else {
263 return new X86ATTAsmPrinter(o, tm, TAI);
Chris Lattnerac5701c2004-10-04 07:24:48 +0000264 }
Brian Gaeke9e474c42003-06-19 19:32:32 +0000265}