blob: edc2397e604ad3e57223f033cd79a9aa01095bd7 [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
Chris Lattnerb36cbd02005-07-01 22:44:09 +000017#include "X86ATTAsmPrinter.h"
18#include "X86IntelAsmPrinter.h"
Chris Lattnera35a8e82005-11-21 22:39:40 +000019#include "X86Subtarget.h"
Chris Lattner72614082002-10-25 22:55:53 +000020#include "X86.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;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000028using namespace x86;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Jeff Cohen00b168892005-07-27 06:12:32 +000030Statistic<> llvm::x86::EmittedInsts("asm-printer",
Nate Begeman4eb74ba2005-07-01 23:56:38 +000031 "Number of machine instrs printed");
Chris Lattner955f0962004-10-04 07:31:08 +000032
Chris Lattnerb36cbd02005-07-01 22:44:09 +000033enum AsmWriterFlavorTy { att, intel };
34cl::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),
41 cl::init(att));
Chris Lattner955f0962004-10-04 07:31:08 +000042
Chris Lattnerb36cbd02005-07-01 22:44:09 +000043/// doInitialization
Chris Lattner5df14ca2005-11-21 22:19:48 +000044bool X86SharedAsmPrinter::doInitialization(Module &M) {
Chris Lattnera35a8e82005-11-21 22:39:40 +000045 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
46
Chris Lattner8fccc972005-11-21 19:50:31 +000047 forDarwin = false;
Chris Lattnere650a6b2006-02-23 05:25:02 +000048 PrivateGlobalPrefix = ".L";
Chris Lattnera35a8e82005-11-21 22:39:40 +000049
50 switch (Subtarget->TargetType) {
Chris Lattnera35a8e82005-11-21 22:39:40 +000051 case X86Subtarget::isDarwin:
Nate Begeman9d19eb42005-06-30 00:53:20 +000052 AlignmentIsInBytes = false;
Chris Lattner8fccc972005-11-21 19:50:31 +000053 GlobalPrefix = "_";
Nate Begeman9035b992005-07-16 01:59:47 +000054 Data64bitsDirective = 0; // we can't emit a 64-bit unit
55 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
Chris Lattnerc41cc832005-11-21 06:46:22 +000056 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
Chris Lattnerd939f6c2005-11-21 08:32:23 +000057 ConstantPoolSection = "\t.const\n";
Chris Lattner8fccc972005-11-21 19:50:31 +000058 LCOMMDirective = "\t.lcomm\t";
59 COMMDirectiveTakesAlignment = false;
Chris Lattnerac2902b2005-11-21 23:06:54 +000060 HasDotTypeDotSizeDirective = false;
Chris Lattnera35a8e82005-11-21 22:39:40 +000061 forDarwin = true;
Chris Lattnerd1239b72005-12-13 06:32:50 +000062 StaticCtorsSection = ".mod_init_func";
63 StaticDtorsSection = ".mod_term_func";
Chris Lattner418caa12006-02-08 23:42:22 +000064 InlineAsmStart = InlineAsmEnd = ""; // Don't use #APP/#NO_APP
Chris Lattnera35a8e82005-11-21 22:39:40 +000065 break;
66 case X86Subtarget::isCygwin:
Chris Lattner8fccc972005-11-21 19:50:31 +000067 GlobalPrefix = "_";
68 COMMDirectiveTakesAlignment = false;
Chris Lattnerac2902b2005-11-21 23:06:54 +000069 HasDotTypeDotSizeDirective = false;
Chris Lattnera35a8e82005-11-21 22:39:40 +000070 break;
71 case X86Subtarget::isWindows:
Chris Lattner8fccc972005-11-21 19:50:31 +000072 GlobalPrefix = "_";
Chris Lattnerac2902b2005-11-21 23:06:54 +000073 HasDotTypeDotSizeDirective = false;
Chris Lattnera35a8e82005-11-21 22:39:40 +000074 break;
75 default: break;
76 }
Chris Lattner8fccc972005-11-21 19:50:31 +000077
Evan Cheng3c992d22006-03-07 02:02:57 +000078 // Emit initial debug information.
79 DW.BeginModule(M);
80
Reid Spencer5dc81f62005-01-23 03:52:14 +000081 return AsmPrinter::doInitialization(M);
82}
83
Chris Lattnerac5701c2004-10-04 07:24:48 +000084bool X86SharedAsmPrinter::doFinalization(Module &M) {
85 const TargetData &TD = TM.getTargetData();
Chris Lattnerac5701c2004-10-04 07:24:48 +000086
87 // Print out module-level global variables here.
Evan Cheng2338c5c2006-02-07 08:38:37 +000088 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
89 I != E; ++I) {
Chris Lattner9787c642005-11-21 22:48:18 +000090 if (!I->hasInitializer()) continue; // External global require no code
91
Chris Lattnerd1239b72005-12-13 06:32:50 +000092 // Check to see if this is a special global used by LLVM, if so, emit it.
93 if (I->hasAppendingLinkage() && EmitSpecialLLVMGlobal(I))
94 continue;
Chris Lattnera046e0d2005-12-13 04:53:51 +000095
Chris Lattner9787c642005-11-21 22:48:18 +000096 std::string name = Mang->getValueName(I);
97 Constant *C = I->getInitializer();
98 unsigned Size = TD.getTypeSize(C->getType());
Chris Lattnerff805b52006-02-05 01:45:04 +000099 unsigned Align = getPreferredAlignmentLog(I);
Jeff Cohen00b168892005-07-27 06:12:32 +0000100
Evan Cheng2338c5c2006-02-07 08:38:37 +0000101 if (C->isNullValue() && /* FIXME: Verify correct */
102 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
Evan Cheng17ef92e2006-02-15 01:56:23 +0000103 I->hasLinkOnceLinkage() ||
104 (forDarwin && I->hasExternalLinkage() && !I->hasSection()))) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000105 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Evan Cheng17ef92e2006-02-15 01:56:23 +0000106 if (I->hasExternalLinkage()) {
Evan Chenga0ea0532006-02-23 02:43:52 +0000107 O << "\t.globl\t" << name << "\n";
Evan Cheng17ef92e2006-02-15 01:56:23 +0000108 O << "\t.zerofill __DATA__, __common, " << name << ", "
109 << Size << ", " << Align;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000110 } else {
Evan Cheng17ef92e2006-02-15 01:56:23 +0000111 SwitchSection(".data", I);
112 if (LCOMMDirective != NULL) {
113 if (I->hasInternalLinkage()) {
114 O << LCOMMDirective << name << "," << Size;
115 if (forDarwin)
116 O << "," << (AlignmentIsInBytes ? (1 << Align) : Align);
117 } else
118 O << COMMDirective << name << "," << Size;
119 } else {
120 if (I->hasInternalLinkage())
121 O << "\t.local\t" << name << "\n";
122 O << COMMDirective << name << "," << Size;
123 if (COMMDirectiveTakesAlignment)
124 O << "," << (AlignmentIsInBytes ? (1 << Align) : Align);
125 }
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000126 }
Evan Chenga0ea0532006-02-23 02:43:52 +0000127 O << "\t\t" << CommentString << " " << I->getName() << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000128 } else {
129 switch (I->getLinkage()) {
130 case GlobalValue::LinkOnceLinkage:
131 case GlobalValue::WeakLinkage:
132 if (forDarwin) {
Evan Cheng5ada3702006-02-07 23:32:58 +0000133 O << "\t.globl " << name << "\n"
134 << "\t.weak_definition " << name << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000135 SwitchSection(".section __DATA,__datacoal_nt,coalesced", I);
136 } else {
137 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
138 O << "\t.weak " << name << "\n";
139 }
140 break;
141 case GlobalValue::AppendingLinkage:
142 // FIXME: appending linkage variables should go into a section of
143 // their name or something. For now, just emit them as external.
144 case GlobalValue::ExternalLinkage:
145 // If external or appending, declare as a global symbol
146 O << "\t.globl " << name << "\n";
147 // FALL THROUGH
148 case GlobalValue::InternalLinkage:
149 SwitchSection(".data", I);
150 break;
151 default:
152 assert(0 && "Unknown linkage type!");
153 }
Chris Lattner9787c642005-11-21 22:48:18 +0000154
Evan Cheng2338c5c2006-02-07 08:38:37 +0000155 EmitAlignment(Align, I);
Evan Cheng5ada3702006-02-07 23:32:58 +0000156 O << name << ":\t\t\t\t" << CommentString << " " << I->getName()
157 << "\n";
158 if (HasDotTypeDotSizeDirective)
159 O << "\t.size " << name << ", " << Size << "\n";
160
Evan Cheng2338c5c2006-02-07 08:38:37 +0000161 EmitGlobalConstant(C);
162 O << '\n';
Chris Lattner9787c642005-11-21 22:48:18 +0000163 }
Chris Lattnera35a8e82005-11-21 22:39:40 +0000164 }
165
Nate Begeman72b286b2005-07-08 00:23:26 +0000166 if (forDarwin) {
Chris Lattner7b6e53c2005-11-21 07:16:34 +0000167 SwitchSection("", 0);
Nate Begeman73213f62005-07-12 01:37:28 +0000168
Nate Begeman72b286b2005-07-08 00:23:26 +0000169 // Output stubs for dynamically-linked functions
170 unsigned j = 1;
171 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000172 i != e; ++i, ++j) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000173 SwitchSection(".section __IMPORT,__jump_table,symbol_stubs,"
174 "self_modifying_code+pure_instructions,5", 0);
Nate Begeman72b286b2005-07-08 00:23:26 +0000175 O << "L" << *i << "$stub:\n";
176 O << "\t.indirect_symbol " << *i << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000177 O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Nate Begeman72b286b2005-07-08 00:23:26 +0000178 }
179
180 O << "\n";
Jeff Cohen00b168892005-07-27 06:12:32 +0000181
Evan Cheng2338c5c2006-02-07 08:38:37 +0000182 // Output stubs for external and common global variables.
183 if (GVStubs.begin() != GVStubs.end())
184 SwitchSection(".section __IMPORT,__pointers,non_lazy_symbol_pointers", 0);
185 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
186 i != e; ++i) {
187 O << "L" << *i << "$non_lazy_ptr:\n";
188 O << "\t.indirect_symbol " << *i << "\n";
189 O << "\t.long\t0\n";
Nate Begeman72b286b2005-07-08 00:23:26 +0000190 }
191 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000192
Evan Cheng3c992d22006-03-07 02:02:57 +0000193 // Emit initial debug information.
194 DW.EndModule(M);
195
Chris Lattnerac5701c2004-10-04 07:24:48 +0000196 AsmPrinter::doFinalization(M);
197 return false; // success
198}
199
Chris Lattnerac5701c2004-10-04 07:24:48 +0000200/// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
201/// for a MachineFunction to the given output stream, using the given target
202/// machine description.
203///
204FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
205 switch (AsmWriterFlavor) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000206 default:
Reid Spencer5dc81f62005-01-23 03:52:14 +0000207 assert(0 && "Unknown asm flavor!");
Chris Lattnerac5701c2004-10-04 07:24:48 +0000208 case intel:
209 return new X86IntelAsmPrinter(o, tm);
210 case att:
211 return new X86ATTAsmPrinter(o, tm);
212 }
Brian Gaeke9e474c42003-06-19 19:32:32 +0000213}