X86AsmPrinter.cpp revision 315421e77c1912038170762df85ea576f29371f6
1//===-- X86AsmPrinter.cpp - Convert X86 LLVM IR to X86 assembly -----------===//
2//
3//                     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.
7//
8//===----------------------------------------------------------------------===//
9//
10// 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'.
14//
15//===----------------------------------------------------------------------===//
16
17#include "X86ATTAsmPrinter.h"
18#include "X86IntelAsmPrinter.h"
19#include "X86Subtarget.h"
20#include "X86.h"
21#include "llvm/Constants.h"
22#include "llvm/Module.h"
23#include "llvm/Type.h"
24#include "llvm/Assembly/Writer.h"
25#include "llvm/Support/Mangler.h"
26#include "llvm/Support/CommandLine.h"
27using namespace llvm;
28using namespace x86;
29
30Statistic<> llvm::x86::EmittedInsts("asm-printer",
31                                    "Number of machine instrs printed");
32
33enum AsmWriterFlavorTy { att, intel };
34cl::opt<AsmWriterFlavorTy>
35AsmWriterFlavor("x86-asm-syntax",
36                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));
42
43/// doInitialization
44bool X86SharedAsmPrinter::doInitialization(Module &M) {
45  const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
46
47  forDarwin = false;
48
49  switch (Subtarget->TargetType) {
50  case X86Subtarget::isDarwin:
51    AlignmentIsInBytes = false;
52    GlobalPrefix = "_";
53    Data64bitsDirective = 0;       // we can't emit a 64-bit unit
54    ZeroDirective = "\t.space\t";  // ".space N" emits N zeros.
55    PrivateGlobalPrefix = "L";     // Marker for constant pool idxs
56    ConstantPoolSection = "\t.const\n";
57    LCOMMDirective = "\t.lcomm\t";
58    COMMDirectiveTakesAlignment = false;
59    HasDotTypeDotSizeDirective = false;
60    forDarwin = true;
61    StaticCtorsSection = ".mod_init_func";
62    StaticDtorsSection = ".mod_term_func";
63    break;
64  case X86Subtarget::isCygwin:
65    GlobalPrefix = "_";
66    COMMDirectiveTakesAlignment = false;
67    HasDotTypeDotSizeDirective = false;
68    break;
69  case X86Subtarget::isWindows:
70    GlobalPrefix = "_";
71    HasDotTypeDotSizeDirective = false;
72    break;
73  default: break;
74  }
75
76  return AsmPrinter::doInitialization(M);
77}
78
79bool X86SharedAsmPrinter::doFinalization(Module &M) {
80  const TargetData &TD = TM.getTargetData();
81
82  // Print out module-level global variables here.
83  for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
84       I != E; ++I) {
85    if (!I->hasInitializer()) continue;   // External global require no code
86
87    // Check to see if this is a special global used by LLVM, if so, emit it.
88    if (I->hasAppendingLinkage() && EmitSpecialLLVMGlobal(I))
89      continue;
90
91    std::string name = Mang->getValueName(I);
92    Constant *C = I->getInitializer();
93    unsigned Size = TD.getTypeSize(C->getType());
94    unsigned Align = getPreferredAlignmentLog(I);
95
96    if (C->isNullValue() && /* FIXME: Verify correct */
97        (I->hasInternalLinkage() || I->hasWeakLinkage() ||
98         I->hasLinkOnceLinkage())) {
99      if (Size == 0) Size = 1;   // .comm Foo, 0 is undefined, avoid it.
100      if (forDarwin) {
101        SwitchSection(".data", I);
102        if (I->hasInternalLinkage())
103          O << LCOMMDirective << name << "," << Size << "," << Align;
104        else
105          O << COMMDirective  << name << "," << Size;
106      } else {
107        SwitchSection(".local", I);
108        O << COMMDirective  << name << "," << Size;
109        if (COMMDirectiveTakesAlignment)
110          O << "," << (AlignmentIsInBytes ? (1 << Align) : Align);
111      }
112      O << "\t\t" << CommentString << " '" << I->getName() << "'\n";
113    } else {
114      switch (I->getLinkage()) {
115      case GlobalValue::LinkOnceLinkage:
116      case GlobalValue::WeakLinkage:
117        if (forDarwin) {
118          O << "\t.globl " << name << '\n'
119            << "\t.weak_definition " << name << '\n';
120          SwitchSection(".section __DATA,__datacoal_nt,coalesced", I);
121        } else {
122          O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
123          O << "\t.weak " << name << "\n";
124        }
125        break;
126      case GlobalValue::AppendingLinkage:
127        // FIXME: appending linkage variables should go into a section of
128        // their name or something.  For now, just emit them as external.
129      case GlobalValue::ExternalLinkage:
130        // If external or appending, declare as a global symbol
131        O << "\t.globl " << name << "\n";
132        // FALL THROUGH
133      case GlobalValue::InternalLinkage:
134        SwitchSection(".data", I);
135        break;
136      default:
137        assert(0 && "Unknown linkage type!");
138      }
139
140      EmitAlignment(Align, I);
141      O << name << ":\t\t\t\t" << CommentString << " '" << I->getName()
142        << "'\n";
143      EmitGlobalConstant(C);
144      O << '\n';
145    }
146  }
147
148  if (forDarwin) {
149    SwitchSection("", 0);
150
151    // Output stubs for dynamically-linked functions
152    unsigned j = 1;
153    for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
154         i != e; ++i, ++j) {
155      SwitchSection(".section __IMPORT,__jump_table,symbol_stubs,"
156                    "self_modifying_code+pure_instructions,5", 0);
157      O << "L" << *i << "$stub:\n";
158      O << "\t.indirect_symbol " << *i << "\n";
159      O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
160    }
161
162    O << "\n";
163
164    // Output stubs for external and common global variables.
165    if (GVStubs.begin() != GVStubs.end())
166      SwitchSection(".section __IMPORT,__pointers,non_lazy_symbol_pointers", 0);
167    for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
168         i != e; ++i) {
169      O << "L" << *i << "$non_lazy_ptr:\n";
170      O << "\t.indirect_symbol " << *i << "\n";
171      O << "\t.long\t0\n";
172    }
173  }
174
175  AsmPrinter::doFinalization(M);
176  return false; // success
177}
178
179/// 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///
183FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
184  switch (AsmWriterFlavor) {
185  default:
186    assert(0 && "Unknown asm flavor!");
187  case intel:
188    return new X86IntelAsmPrinter(o, tm);
189  case att:
190    return new X86ATTAsmPrinter(o, tm);
191  }
192}
193