blob: b634d13ea4a66ccc3a9f7d7110994c9c8b02914f [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"
Jim Laskeya0f3d172006-09-07 22:06:40 +000026#include "llvm/Target/TargetAsmInfo.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
Jim Laskey563321a2006-09-06 18:34:40 +000032/// doInitialization
Evan Cheng25ab6902006-09-08 06:48:29 +000033bool X86SharedAsmPrinter::doInitialization(Module &M) {
Jim Laskeyea348582006-07-27 02:05:13 +000034 if (Subtarget->isTargetDarwin()) {
Evan Cheng25ab6902006-09-08 06:48:29 +000035 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
36 if (!Subtarget->is64Bit())
37 X86PICStyle = PICStyle::Stub;
38
Evan Chengd5948812006-03-07 02:23:26 +000039 // Emit initial debug information.
Jim Laskey99db0442006-03-23 18:09:44 +000040 DW.BeginModule(&M);
Evan Chengd5948812006-03-07 02:23:26 +000041 }
Evan Cheng3c992d22006-03-07 02:02:57 +000042
Reid Spencer5dc81f62005-01-23 03:52:14 +000043 return AsmPrinter::doInitialization(M);
44}
45
Chris Lattnerac5701c2004-10-04 07:24:48 +000046bool X86SharedAsmPrinter::doFinalization(Module &M) {
Jeff Cohend43b18d2006-05-06 21:27:14 +000047 // Note: this code is not shared by the Intel printer as it is too different
48 // from how MASM does things. When making changes here don't forget to look
49 // at X86IntelAsmPrinter::doFinalization().
Owen Andersona69571c2006-05-03 01:29:57 +000050 const TargetData *TD = TM.getTargetData();
Chris Lattnerac5701c2004-10-04 07:24:48 +000051
52 // Print out module-level global variables here.
Evan Cheng2338c5c2006-02-07 08:38:37 +000053 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
54 I != E; ++I) {
Chris Lattner9787c642005-11-21 22:48:18 +000055 if (!I->hasInitializer()) continue; // External global require no code
56
Chris Lattnerd1239b72005-12-13 06:32:50 +000057 // Check to see if this is a special global used by LLVM, if so, emit it.
Jim Laskey78098112006-03-07 22:00:35 +000058 if (EmitSpecialLLVMGlobal(I))
Chris Lattnerd1239b72005-12-13 06:32:50 +000059 continue;
Chris Lattnera046e0d2005-12-13 04:53:51 +000060
Chris Lattner9787c642005-11-21 22:48:18 +000061 std::string name = Mang->getValueName(I);
62 Constant *C = I->getInitializer();
Owen Andersona69571c2006-05-03 01:29:57 +000063 unsigned Size = TD->getTypeSize(C->getType());
Chris Lattnerff805b52006-02-05 01:45:04 +000064 unsigned Align = getPreferredAlignmentLog(I);
Jeff Cohen00b168892005-07-27 06:12:32 +000065
Evan Cheng2338c5c2006-02-07 08:38:37 +000066 if (C->isNullValue() && /* FIXME: Verify correct */
67 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
Evan Cheng17ef92e2006-02-15 01:56:23 +000068 I->hasLinkOnceLinkage() ||
Jim Laskeyea348582006-07-27 02:05:13 +000069 (Subtarget->isTargetDarwin() &&
Evan Cheng932ad512006-05-25 21:59:08 +000070 I->hasExternalLinkage() && !I->hasSection()))) {
Evan Cheng2338c5c2006-02-07 08:38:37 +000071 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Evan Cheng17ef92e2006-02-15 01:56:23 +000072 if (I->hasExternalLinkage()) {
Evan Chenga0ea0532006-02-23 02:43:52 +000073 O << "\t.globl\t" << name << "\n";
Evan Cheng17ef92e2006-02-15 01:56:23 +000074 O << "\t.zerofill __DATA__, __common, " << name << ", "
75 << Size << ", " << Align;
Evan Cheng2338c5c2006-02-07 08:38:37 +000076 } else {
Jim Laskey563321a2006-09-06 18:34:40 +000077 SwitchToDataSection(TAI->getDataSection(), I);
78 if (TAI->getLCOMMDirective() != NULL) {
Evan Cheng17ef92e2006-02-15 01:56:23 +000079 if (I->hasInternalLinkage()) {
Jim Laskey563321a2006-09-06 18:34:40 +000080 O << TAI->getLCOMMDirective() << name << "," << Size;
Jim Laskeyea348582006-07-27 02:05:13 +000081 if (Subtarget->isTargetDarwin())
Jim Laskey563321a2006-09-06 18:34:40 +000082 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Evan Cheng17ef92e2006-02-15 01:56:23 +000083 } else
Jim Laskey563321a2006-09-06 18:34:40 +000084 O << TAI->getCOMMDirective() << name << "," << Size;
Evan Cheng17ef92e2006-02-15 01:56:23 +000085 } else {
Evan Cheng57c07882006-05-26 06:22:34 +000086 if (Subtarget->TargetType != X86Subtarget::isCygwin) {
Evan Cheng932ad512006-05-25 21:59:08 +000087 if (I->hasInternalLinkage())
88 O << "\t.local\t" << name << "\n";
89 }
Jim Laskey563321a2006-09-06 18:34:40 +000090 O << TAI->getCOMMDirective() << name << "," << Size;
91 if (TAI->getCOMMDirectiveTakesAlignment())
92 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Evan Cheng17ef92e2006-02-15 01:56:23 +000093 }
Chris Lattnerb12c9fa2005-07-11 06:25:47 +000094 }
Jim Laskey563321a2006-09-06 18:34:40 +000095 O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +000096 } else {
97 switch (I->getLinkage()) {
98 case GlobalValue::LinkOnceLinkage:
99 case GlobalValue::WeakLinkage:
Jim Laskeyea348582006-07-27 02:05:13 +0000100 if (Subtarget->isTargetDarwin()) {
Evan Cheng5ada3702006-02-07 23:32:58 +0000101 O << "\t.globl " << name << "\n"
102 << "\t.weak_definition " << name << "\n";
Evan Chengf8614db2006-06-04 07:24:07 +0000103 SwitchToDataSection(".section __DATA,__const_coal,coalesced", I);
Evan Cheng932ad512006-05-25 21:59:08 +0000104 } else if (Subtarget->TargetType == X86Subtarget::isCygwin) {
105 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\"\n"
106 << "\t.weak " << name << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000107 } else {
Evan Cheng932ad512006-05-25 21:59:08 +0000108 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n"
109 << "\t.weak " << name << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000110 }
111 break;
112 case GlobalValue::AppendingLinkage:
113 // FIXME: appending linkage variables should go into a section of
114 // their name or something. For now, just emit them as external.
115 case GlobalValue::ExternalLinkage:
116 // If external or appending, declare as a global symbol
117 O << "\t.globl " << name << "\n";
118 // FALL THROUGH
119 case GlobalValue::InternalLinkage:
Jim Laskey563321a2006-09-06 18:34:40 +0000120 SwitchToDataSection(TAI->getDataSection(), I);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000121 break;
122 default:
123 assert(0 && "Unknown linkage type!");
124 }
Chris Lattner9787c642005-11-21 22:48:18 +0000125
Evan Cheng2338c5c2006-02-07 08:38:37 +0000126 EmitAlignment(Align, I);
Jim Laskey563321a2006-09-06 18:34:40 +0000127 O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName()
Evan Cheng5ada3702006-02-07 23:32:58 +0000128 << "\n";
Jim Laskey563321a2006-09-06 18:34:40 +0000129 if (TAI->hasDotTypeDotSizeDirective())
Evan Cheng5ada3702006-02-07 23:32:58 +0000130 O << "\t.size " << name << ", " << Size << "\n";
131
Evan Cheng2338c5c2006-02-07 08:38:37 +0000132 EmitGlobalConstant(C);
133 O << '\n';
Chris Lattner9787c642005-11-21 22:48:18 +0000134 }
Chris Lattnera35a8e82005-11-21 22:39:40 +0000135 }
136
Jim Laskeyea348582006-07-27 02:05:13 +0000137 if (Subtarget->isTargetDarwin()) {
Chris Lattner4632d7a2006-05-09 04:59:56 +0000138 SwitchToDataSection("", 0);
Nate Begeman73213f62005-07-12 01:37:28 +0000139
Nate Begeman72b286b2005-07-08 00:23:26 +0000140 // Output stubs for dynamically-linked functions
141 unsigned j = 1;
142 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000143 i != e; ++i, ++j) {
Chris Lattner4632d7a2006-05-09 04:59:56 +0000144 SwitchToDataSection(".section __IMPORT,__jump_table,symbol_stubs,"
145 "self_modifying_code+pure_instructions,5", 0);
Nate Begeman72b286b2005-07-08 00:23:26 +0000146 O << "L" << *i << "$stub:\n";
147 O << "\t.indirect_symbol " << *i << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000148 O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Nate Begeman72b286b2005-07-08 00:23:26 +0000149 }
150
151 O << "\n";
Jeff Cohen00b168892005-07-27 06:12:32 +0000152
Evan Cheng2338c5c2006-02-07 08:38:37 +0000153 // Output stubs for external and common global variables.
154 if (GVStubs.begin() != GVStubs.end())
Chris Lattner4632d7a2006-05-09 04:59:56 +0000155 SwitchToDataSection(
156 ".section __IMPORT,__pointers,non_lazy_symbol_pointers", 0);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000157 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
158 i != e; ++i) {
159 O << "L" << *i << "$non_lazy_ptr:\n";
160 O << "\t.indirect_symbol " << *i << "\n";
161 O << "\t.long\t0\n";
Nate Begeman72b286b2005-07-08 00:23:26 +0000162 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000163
Evan Chengd5948812006-03-07 02:23:26 +0000164 // Emit initial debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000165 DW.EndModule();
Evan Chengd5948812006-03-07 02:23:26 +0000166
167 // Funny Darwin hack: This flag tells the linker that no global symbols
168 // contain code that falls through to other global symbols (e.g. the obvious
169 // implementation of multiple entry points). If this doesn't occur, the
Jim Laskey99db0442006-03-23 18:09:44 +0000170 // linker can safely perform dead code stripping. Since LLVM never
171 // generates code that does this, it is always safe to set.
Evan Chengd5948812006-03-07 02:23:26 +0000172 O << "\t.subsections_via_symbols\n";
173 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000174
Chris Lattnerac5701c2004-10-04 07:24:48 +0000175 AsmPrinter::doFinalization(M);
176 return false; // success
177}
178
Chris Lattnerac5701c2004-10-04 07:24:48 +0000179/// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
180/// for a MachineFunction to the given output stream, using the given target
181/// machine description.
182///
Evan Chengc4c62572006-03-13 23:20:37 +0000183FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,
Jim Laskey563321a2006-09-06 18:34:40 +0000184 X86TargetMachine &tm) {
Jim Laskey05a059d2006-09-07 12:23:47 +0000185 const X86Subtarget *Subtarget = &tm.getSubtarget<X86Subtarget>();
Jim Laskey563321a2006-09-06 18:34:40 +0000186
Jim Laskey05a059d2006-09-07 12:23:47 +0000187 if (Subtarget->isFlavorIntel()) {
Jim Laskeya0f3d172006-09-07 22:06:40 +0000188 return new X86IntelAsmPrinter(o, tm, tm.getTargetAsmInfo());
Jim Laskey05a059d2006-09-07 12:23:47 +0000189 } else {
Jim Laskeya0f3d172006-09-07 22:06:40 +0000190 return new X86ATTAsmPrinter(o, tm, tm.getTargetAsmInfo());
Chris Lattnerac5701c2004-10-04 07:24:48 +0000191 }
Brian Gaeke9e474c42003-06-19 19:32:32 +0000192}