2005-07-01 22:44:09 +00:00
|
|
|
//===-- X86AsmPrinter.cpp - Convert X86 LLVM IR to X86 assembly -----------===//
|
2005-04-21 23:38:14 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-21 23:38:14 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-10-25 22:55:53 +00:00
|
|
|
//
|
2005-07-01 22:44:09 +00:00
|
|
|
// This file the shared super class printer that converts from our internal
|
|
|
|
// representation of machine-dependent LLVM code to Intel and AT&T format
|
|
|
|
// assembly language.
|
|
|
|
// This printer is the output mechanism used by `llc'.
|
2002-10-25 22:55:53 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2005-07-01 22:44:09 +00:00
|
|
|
#include "X86ATTAsmPrinter.h"
|
|
|
|
#include "X86IntelAsmPrinter.h"
|
2002-10-25 22:55:53 +00:00
|
|
|
#include "X86.h"
|
2003-08-11 20:06:16 +00:00
|
|
|
#include "llvm/Module.h"
|
2005-11-21 06:46:22 +00:00
|
|
|
#include "llvm/Type.h"
|
2003-08-11 20:06:16 +00:00
|
|
|
#include "llvm/Assembly/Writer.h"
|
2003-07-24 20:20:44 +00:00
|
|
|
#include "llvm/Support/Mangler.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2004-02-14 06:00:36 +00:00
|
|
|
using namespace llvm;
|
2005-07-01 22:44:09 +00:00
|
|
|
using namespace x86;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2005-07-27 06:12:32 +00:00
|
|
|
Statistic<> llvm::x86::EmittedInsts("asm-printer",
|
2005-07-01 23:56:38 +00:00
|
|
|
"Number of machine instrs printed");
|
2004-10-04 07:31:08 +00:00
|
|
|
|
2005-07-01 22:44:09 +00:00
|
|
|
enum AsmWriterFlavorTy { att, intel };
|
|
|
|
cl::opt<AsmWriterFlavorTy>
|
|
|
|
AsmWriterFlavor("x86-asm-syntax",
|
2005-07-11 06:25:47 +00:00
|
|
|
cl::desc("Choose style of code to emit from X86 backend:"),
|
|
|
|
cl::values(
|
|
|
|
clEnumVal(att, " Emit AT&T-style assembly"),
|
|
|
|
clEnumVal(intel, " Emit Intel-style assembly"),
|
|
|
|
clEnumValEnd),
|
|
|
|
cl::init(att));
|
2004-10-04 07:31:08 +00:00
|
|
|
|
2005-07-01 22:44:09 +00:00
|
|
|
/// doInitialization
|
2005-01-23 03:52:14 +00:00
|
|
|
bool X86SharedAsmPrinter::doInitialization(Module& M) {
|
2005-07-01 22:44:09 +00:00
|
|
|
bool leadingUnderscore = false;
|
|
|
|
forCygwin = false;
|
2005-01-23 03:52:14 +00:00
|
|
|
const std::string& TT = M.getTargetTriple();
|
2005-06-30 00:53:20 +00:00
|
|
|
if (TT.length() > 5) {
|
2005-04-21 23:38:14 +00:00
|
|
|
forCygwin = TT.find("cygwin") != std::string::npos ||
|
2005-03-08 17:02:05 +00:00
|
|
|
TT.find("mingw") != std::string::npos;
|
2005-06-30 00:53:20 +00:00
|
|
|
forDarwin = TT.find("darwin") != std::string::npos;
|
|
|
|
} else if (TT.empty()) {
|
2005-07-11 06:25:47 +00:00
|
|
|
#if defined(__CYGWIN__) || defined(__MINGW32__)
|
2005-01-23 03:52:14 +00:00
|
|
|
forCygwin = true;
|
2005-07-11 06:25:47 +00:00
|
|
|
#elif defined(__APPLE__)
|
2005-06-30 00:53:20 +00:00
|
|
|
forDarwin = true;
|
2005-07-11 06:25:47 +00:00
|
|
|
#elif defined(_WIN32)
|
2005-07-01 22:44:09 +00:00
|
|
|
leadingUnderscore = true;
|
2005-07-11 06:25:47 +00:00
|
|
|
#else
|
2005-07-01 22:44:09 +00:00
|
|
|
leadingUnderscore = false;
|
2005-07-11 06:25:47 +00:00
|
|
|
#endif
|
2005-01-23 03:52:14 +00:00
|
|
|
}
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-07-01 22:44:09 +00:00
|
|
|
if (leadingUnderscore || forCygwin || forDarwin)
|
2005-01-23 03:52:14 +00:00
|
|
|
GlobalPrefix = "_";
|
2005-07-01 22:44:09 +00:00
|
|
|
|
2005-07-16 01:59:47 +00:00
|
|
|
if (forDarwin) {
|
2005-06-30 00:53:20 +00:00
|
|
|
AlignmentIsInBytes = false;
|
2005-07-16 01:59:47 +00:00
|
|
|
Data64bitsDirective = 0; // we can't emit a 64-bit unit
|
|
|
|
ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
|
2005-11-21 06:46:22 +00:00
|
|
|
PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
|
2005-11-21 08:32:23 +00:00
|
|
|
ConstantPoolSection = "\t.const\n";
|
2005-07-16 01:59:47 +00:00
|
|
|
}
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-01-23 03:52:14 +00:00
|
|
|
return AsmPrinter::doInitialization(M);
|
|
|
|
}
|
|
|
|
|
2004-10-04 07:24:48 +00:00
|
|
|
bool X86SharedAsmPrinter::doFinalization(Module &M) {
|
|
|
|
const TargetData &TD = TM.getTargetData();
|
|
|
|
|
|
|
|
// Print out module-level global variables here.
|
2005-07-11 06:25:47 +00:00
|
|
|
for (Module::const_global_iterator I = M.global_begin(),
|
|
|
|
E = M.global_end(); I != E; ++I)
|
|
|
|
if (I->hasInitializer()) { // External global require no code
|
|
|
|
O << "\n\n";
|
|
|
|
std::string name = Mang->getValueName(I);
|
|
|
|
Constant *C = I->getInitializer();
|
|
|
|
unsigned Size = TD.getTypeSize(C->getType());
|
|
|
|
unsigned Align = TD.getTypeAlignmentShift(C->getType());
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-07-11 06:25:47 +00:00
|
|
|
if (C->isNullValue() &&
|
|
|
|
(I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
|
|
|
|
I->hasWeakLinkage() /* FIXME: Verify correct */)) {
|
2005-11-21 07:11:11 +00:00
|
|
|
SwitchSection(".data", I);
|
2005-07-11 06:25:47 +00:00
|
|
|
if (!forCygwin && !forDarwin && I->hasInternalLinkage())
|
|
|
|
O << "\t.local " << name << "\n";
|
|
|
|
if (forDarwin && I->hasInternalLinkage())
|
|
|
|
O << "\t.lcomm " << name << "," << Size << "," << Align;
|
2005-07-27 06:12:32 +00:00
|
|
|
else
|
2005-07-11 06:25:47 +00:00
|
|
|
O << "\t.comm " << name << "," << Size;
|
|
|
|
if (!forCygwin && !forDarwin)
|
|
|
|
O << "," << (1 << Align);
|
|
|
|
O << "\t\t# ";
|
|
|
|
WriteAsOperand(O, I, true, true, &M);
|
|
|
|
O << "\n";
|
|
|
|
} else {
|
|
|
|
switch (I->getLinkage()) {
|
|
|
|
default: assert(0 && "Unknown linkage type!");
|
|
|
|
case GlobalValue::LinkOnceLinkage:
|
|
|
|
case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
|
|
|
|
// Nonnull linkonce -> weak
|
|
|
|
O << "\t.weak " << name << "\n";
|
|
|
|
O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
|
2005-11-21 07:11:11 +00:00
|
|
|
SwitchSection("", I);
|
2005-07-11 06:25:47 +00:00
|
|
|
break;
|
|
|
|
case GlobalValue::AppendingLinkage:
|
|
|
|
// FIXME: appending linkage variables should go into a section of
|
|
|
|
// their name or something. For now, just emit them as external.
|
|
|
|
case GlobalValue::ExternalLinkage:
|
|
|
|
// If external or appending, declare as a global symbol
|
|
|
|
O << "\t.globl " << name << "\n";
|
|
|
|
// FALL THROUGH
|
|
|
|
case GlobalValue::InternalLinkage:
|
2005-11-21 07:11:11 +00:00
|
|
|
SwitchSection(C->isNullValue() ? ".bss" : ".data", I);
|
2005-07-11 06:25:47 +00:00
|
|
|
break;
|
|
|
|
}
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-11-21 07:51:23 +00:00
|
|
|
EmitAlignment(Align);
|
2005-07-11 06:25:47 +00:00
|
|
|
if (!forCygwin && !forDarwin) {
|
|
|
|
O << "\t.type " << name << ",@object\n";
|
|
|
|
O << "\t.size " << name << "," << Size << "\n";
|
|
|
|
}
|
|
|
|
O << name << ":\t\t\t\t# ";
|
|
|
|
WriteAsOperand(O, I, true, true, &M);
|
|
|
|
O << " = ";
|
|
|
|
WriteAsOperand(O, C, false, false, &M);
|
|
|
|
O << "\n";
|
2005-11-21 07:51:23 +00:00
|
|
|
EmitGlobalConstant(C);
|
2005-07-11 06:25:47 +00:00
|
|
|
}
|
2004-10-04 07:24:48 +00:00
|
|
|
}
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-07-08 00:23:26 +00:00
|
|
|
if (forDarwin) {
|
2005-11-21 07:16:34 +00:00
|
|
|
SwitchSection("", 0);
|
2005-07-12 01:37:28 +00:00
|
|
|
// Output stubs for external global variables
|
|
|
|
if (GVStubs.begin() != GVStubs.end())
|
|
|
|
O << "\t.non_lazy_symbol_pointer\n";
|
|
|
|
for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
|
|
|
|
i != e; ++i) {
|
|
|
|
O << "L" << *i << "$non_lazy_ptr:\n";
|
|
|
|
O << "\t.indirect_symbol " << *i << "\n";
|
|
|
|
O << "\t.long\t0\n";
|
|
|
|
}
|
|
|
|
|
2005-07-08 00:23:26 +00:00
|
|
|
// Output stubs for dynamically-linked functions
|
|
|
|
unsigned j = 1;
|
|
|
|
for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
|
2005-07-11 06:25:47 +00:00
|
|
|
i != e; ++i, ++j) {
|
2005-07-08 00:23:26 +00:00
|
|
|
O << "\t.symbol_stub\n";
|
|
|
|
O << "L" << *i << "$stub:\n";
|
|
|
|
O << "\t.indirect_symbol " << *i << "\n";
|
|
|
|
O << "\tjmp\t*L" << j << "$lz\n";
|
|
|
|
O << "L" << *i << "$stub_binder:\n";
|
|
|
|
O << "\tpushl\t$L" << j << "$lz\n";
|
|
|
|
O << "\tjmp\tdyld_stub_binding_helper\n";
|
|
|
|
O << "\t.section __DATA, __la_sym_ptr3,lazy_symbol_pointers\n";
|
|
|
|
O << "L" << j << "$lz:\n";
|
|
|
|
O << "\t.indirect_symbol " << *i << "\n";
|
|
|
|
O << "\t.long\tL" << *i << "$stub_binder\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
O << "\n";
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-07-08 00:23:26 +00:00
|
|
|
// Output stubs for link-once variables
|
|
|
|
if (LinkOnceStubs.begin() != LinkOnceStubs.end())
|
|
|
|
O << ".data\n.align 2\n";
|
|
|
|
for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
|
|
|
|
e = LinkOnceStubs.end(); i != e; ++i) {
|
|
|
|
O << "L" << *i << "$non_lazy_ptr:\n"
|
|
|
|
<< "\t.long\t" << *i << '\n';
|
|
|
|
}
|
|
|
|
}
|
2004-10-04 07:24:48 +00:00
|
|
|
|
|
|
|
AsmPrinter::doFinalization(M);
|
|
|
|
return false; // success
|
|
|
|
}
|
|
|
|
|
|
|
|
/// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
|
|
|
|
/// for a MachineFunction to the given output stream, using the given target
|
|
|
|
/// machine description.
|
|
|
|
///
|
|
|
|
FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
|
|
|
|
switch (AsmWriterFlavor) {
|
2005-04-21 23:38:14 +00:00
|
|
|
default:
|
2005-01-23 03:52:14 +00:00
|
|
|
assert(0 && "Unknown asm flavor!");
|
2004-10-04 07:24:48 +00:00
|
|
|
case intel:
|
|
|
|
return new X86IntelAsmPrinter(o, tm);
|
|
|
|
case att:
|
|
|
|
return new X86ATTAsmPrinter(o, tm);
|
|
|
|
}
|
2003-06-19 19:32:32 +00:00
|
|
|
}
|