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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-03-13 23:20:37 +00:00
|
|
|
#include "X86AsmPrinter.h"
|
2005-07-01 22:44:09 +00:00
|
|
|
#include "X86ATTAsmPrinter.h"
|
|
|
|
#include "X86IntelAsmPrinter.h"
|
2005-11-21 22:39:40 +00:00
|
|
|
#include "X86Subtarget.h"
|
2005-12-13 04:53:51 +00:00
|
|
|
#include "llvm/Constants.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;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2006-03-13 23:20:37 +00:00
|
|
|
Statistic<> llvm::EmittedInsts("asm-printer",
|
|
|
|
"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),
|
2006-05-04 16:19:27 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
cl::init(intel)
|
|
|
|
#else
|
|
|
|
cl::init(att)
|
|
|
|
#endif
|
|
|
|
);
|
2004-10-04 07:31:08 +00:00
|
|
|
|
2005-07-01 22:44:09 +00:00
|
|
|
/// doInitialization
|
2005-11-21 22:19:48 +00:00
|
|
|
bool X86SharedAsmPrinter::doInitialization(Module &M) {
|
2005-11-21 22:39:40 +00:00
|
|
|
const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
|
|
|
|
|
2005-11-21 19:50:31 +00:00
|
|
|
forDarwin = false;
|
2006-02-23 05:25:02 +00:00
|
|
|
PrivateGlobalPrefix = ".L";
|
2005-11-21 22:39:40 +00:00
|
|
|
|
|
|
|
switch (Subtarget->TargetType) {
|
|
|
|
case X86Subtarget::isDarwin:
|
2005-06-30 00:53:20 +00:00
|
|
|
AlignmentIsInBytes = false;
|
2005-11-21 19:50:31 +00:00
|
|
|
GlobalPrefix = "_";
|
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";
|
2006-04-22 18:53:45 +00:00
|
|
|
JumpTableSection = "\t.const\n"; // FIXME: depends on PIC mode
|
2005-11-21 19:50:31 +00:00
|
|
|
LCOMMDirective = "\t.lcomm\t";
|
|
|
|
COMMDirectiveTakesAlignment = false;
|
2005-11-21 23:06:54 +00:00
|
|
|
HasDotTypeDotSizeDirective = false;
|
2005-11-21 22:39:40 +00:00
|
|
|
forDarwin = true;
|
2005-12-13 06:32:50 +00:00
|
|
|
StaticCtorsSection = ".mod_init_func";
|
|
|
|
StaticDtorsSection = ".mod_term_func";
|
2006-05-05 21:48:50 +00:00
|
|
|
InlineAsmStart = "# InlineAsm Start";
|
|
|
|
InlineAsmEnd = "# InlineAsm End";
|
2005-11-21 22:39:40 +00:00
|
|
|
break;
|
|
|
|
case X86Subtarget::isCygwin:
|
2005-11-21 19:50:31 +00:00
|
|
|
GlobalPrefix = "_";
|
|
|
|
COMMDirectiveTakesAlignment = false;
|
2005-11-21 23:06:54 +00:00
|
|
|
HasDotTypeDotSizeDirective = false;
|
2005-11-21 22:39:40 +00:00
|
|
|
break;
|
|
|
|
case X86Subtarget::isWindows:
|
2005-11-21 19:50:31 +00:00
|
|
|
GlobalPrefix = "_";
|
2005-11-21 23:06:54 +00:00
|
|
|
HasDotTypeDotSizeDirective = false;
|
2005-11-21 22:39:40 +00:00
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
2005-11-21 19:50:31 +00:00
|
|
|
|
2006-03-07 02:23:26 +00:00
|
|
|
if (forDarwin) {
|
|
|
|
// Emit initial debug information.
|
2006-03-23 18:09:44 +00:00
|
|
|
DW.BeginModule(&M);
|
2006-03-07 02:23:26 +00:00
|
|
|
}
|
2006-03-07 02:02:57 +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) {
|
2006-05-06 21:27:14 +00:00
|
|
|
// Note: this code is not shared by the Intel printer as it is too different
|
|
|
|
// from how MASM does things. When making changes here don't forget to look
|
|
|
|
// at X86IntelAsmPrinter::doFinalization().
|
2006-05-03 01:29:57 +00:00
|
|
|
const TargetData *TD = TM.getTargetData();
|
2004-10-04 07:24:48 +00:00
|
|
|
|
|
|
|
// Print out module-level global variables here.
|
2006-02-07 08:38:37 +00:00
|
|
|
for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
|
|
|
|
I != E; ++I) {
|
2005-11-21 22:48:18 +00:00
|
|
|
if (!I->hasInitializer()) continue; // External global require no code
|
|
|
|
|
2005-12-13 06:32:50 +00:00
|
|
|
// Check to see if this is a special global used by LLVM, if so, emit it.
|
2006-03-07 22:00:35 +00:00
|
|
|
if (EmitSpecialLLVMGlobal(I))
|
2005-12-13 06:32:50 +00:00
|
|
|
continue;
|
2005-12-13 04:53:51 +00:00
|
|
|
|
2005-11-21 22:48:18 +00:00
|
|
|
std::string name = Mang->getValueName(I);
|
|
|
|
Constant *C = I->getInitializer();
|
2006-05-03 01:29:57 +00:00
|
|
|
unsigned Size = TD->getTypeSize(C->getType());
|
2006-02-05 01:45:04 +00:00
|
|
|
unsigned Align = getPreferredAlignmentLog(I);
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2006-02-07 08:38:37 +00:00
|
|
|
if (C->isNullValue() && /* FIXME: Verify correct */
|
|
|
|
(I->hasInternalLinkage() || I->hasWeakLinkage() ||
|
2006-02-15 01:56:23 +00:00
|
|
|
I->hasLinkOnceLinkage() ||
|
|
|
|
(forDarwin && I->hasExternalLinkage() && !I->hasSection()))) {
|
2006-02-07 08:38:37 +00:00
|
|
|
if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
|
2006-02-15 01:56:23 +00:00
|
|
|
if (I->hasExternalLinkage()) {
|
2006-02-23 02:43:52 +00:00
|
|
|
O << "\t.globl\t" << name << "\n";
|
2006-02-15 01:56:23 +00:00
|
|
|
O << "\t.zerofill __DATA__, __common, " << name << ", "
|
|
|
|
<< Size << ", " << Align;
|
2006-02-07 08:38:37 +00:00
|
|
|
} else {
|
2006-02-15 01:56:23 +00:00
|
|
|
SwitchSection(".data", I);
|
|
|
|
if (LCOMMDirective != NULL) {
|
|
|
|
if (I->hasInternalLinkage()) {
|
|
|
|
O << LCOMMDirective << name << "," << Size;
|
|
|
|
if (forDarwin)
|
|
|
|
O << "," << (AlignmentIsInBytes ? (1 << Align) : Align);
|
|
|
|
} else
|
|
|
|
O << COMMDirective << name << "," << Size;
|
|
|
|
} else {
|
|
|
|
if (I->hasInternalLinkage())
|
|
|
|
O << "\t.local\t" << name << "\n";
|
|
|
|
O << COMMDirective << name << "," << Size;
|
|
|
|
if (COMMDirectiveTakesAlignment)
|
|
|
|
O << "," << (AlignmentIsInBytes ? (1 << Align) : Align);
|
|
|
|
}
|
2006-02-07 08:38:37 +00:00
|
|
|
}
|
2006-02-23 02:43:52 +00:00
|
|
|
O << "\t\t" << CommentString << " " << I->getName() << "\n";
|
2006-02-07 08:38:37 +00:00
|
|
|
} else {
|
|
|
|
switch (I->getLinkage()) {
|
|
|
|
case GlobalValue::LinkOnceLinkage:
|
|
|
|
case GlobalValue::WeakLinkage:
|
|
|
|
if (forDarwin) {
|
2006-02-07 23:32:58 +00:00
|
|
|
O << "\t.globl " << name << "\n"
|
|
|
|
<< "\t.weak_definition " << name << "\n";
|
2006-02-07 08:38:37 +00:00
|
|
|
SwitchSection(".section __DATA,__datacoal_nt,coalesced", I);
|
2005-11-21 22:48:18 +00:00
|
|
|
} else {
|
2006-02-07 08:38:37 +00:00
|
|
|
O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
|
|
|
|
O << "\t.weak " << name << "\n";
|
2005-07-11 06:25:47 +00:00
|
|
|
}
|
2006-02-07 08:38:37 +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:
|
|
|
|
SwitchSection(".data", I);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0 && "Unknown linkage type!");
|
2005-11-21 19:50:31 +00:00
|
|
|
}
|
|
|
|
|
2006-02-07 08:38:37 +00:00
|
|
|
EmitAlignment(Align, I);
|
2006-02-07 23:32:58 +00:00
|
|
|
O << name << ":\t\t\t\t" << CommentString << " " << I->getName()
|
|
|
|
<< "\n";
|
|
|
|
if (HasDotTypeDotSizeDirective)
|
|
|
|
O << "\t.size " << name << ", " << Size << "\n";
|
|
|
|
|
2006-02-07 08:38:37 +00:00
|
|
|
EmitGlobalConstant(C);
|
|
|
|
O << '\n';
|
2004-10-04 07:24:48 +00:00
|
|
|
}
|
2005-11-21 22:39:40 +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
|
|
|
|
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) {
|
2006-02-07 08:38:37 +00:00
|
|
|
SwitchSection(".section __IMPORT,__jump_table,symbol_stubs,"
|
|
|
|
"self_modifying_code+pure_instructions,5", 0);
|
2005-07-08 00:23:26 +00:00
|
|
|
O << "L" << *i << "$stub:\n";
|
|
|
|
O << "\t.indirect_symbol " << *i << "\n";
|
2006-02-07 08:38:37 +00:00
|
|
|
O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
|
2005-07-08 00:23:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
O << "\n";
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2006-02-07 08:38:37 +00:00
|
|
|
// Output stubs for external and common global variables.
|
|
|
|
if (GVStubs.begin() != GVStubs.end())
|
|
|
|
SwitchSection(".section __IMPORT,__pointers,non_lazy_symbol_pointers", 0);
|
|
|
|
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
|
|
|
}
|
2004-10-04 07:24:48 +00:00
|
|
|
|
2006-03-07 02:23:26 +00:00
|
|
|
// Emit initial debug information.
|
2006-03-23 18:09:44 +00:00
|
|
|
DW.EndModule();
|
2006-03-07 02:23:26 +00:00
|
|
|
|
|
|
|
// Funny Darwin hack: This flag tells the linker that no global symbols
|
|
|
|
// contain code that falls through to other global symbols (e.g. the obvious
|
|
|
|
// implementation of multiple entry points). If this doesn't occur, the
|
2006-03-23 18:09:44 +00:00
|
|
|
// linker can safely perform dead code stripping. Since LLVM never
|
|
|
|
// generates code that does this, it is always safe to set.
|
2006-03-07 02:23:26 +00:00
|
|
|
O << "\t.subsections_via_symbols\n";
|
|
|
|
}
|
2006-03-07 02:02:57 +00:00
|
|
|
|
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.
|
|
|
|
///
|
2006-03-13 23:20:37 +00:00
|
|
|
FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,
|
|
|
|
X86TargetMachine &tm){
|
2004-10-04 07:24:48 +00:00
|
|
|
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
|
|
|
}
|