2005-07-02 00:44:09 +02:00
|
|
|
//===-- X86AsmPrinter.cpp - Convert X86 LLVM IR to X86 assembly -----------===//
|
2005-04-22 01:38:14 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02: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-22 01:38:14 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-10-26 00:55:53 +02:00
|
|
|
//
|
2005-07-02 00:44:09 +02: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-26 00:55:53 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-03-14 00:20:37 +01:00
|
|
|
#include "X86AsmPrinter.h"
|
2005-07-02 00:44:09 +02:00
|
|
|
#include "X86ATTAsmPrinter.h"
|
|
|
|
#include "X86IntelAsmPrinter.h"
|
2005-11-21 23:39:40 +01:00
|
|
|
#include "X86Subtarget.h"
|
2005-12-13 05:53:51 +01:00
|
|
|
#include "llvm/Constants.h"
|
2003-08-11 22:06:16 +02:00
|
|
|
#include "llvm/Module.h"
|
2005-11-21 07:46:22 +01:00
|
|
|
#include "llvm/Type.h"
|
2003-08-11 22:06:16 +02:00
|
|
|
#include "llvm/Assembly/Writer.h"
|
2003-07-24 22:20:44 +02:00
|
|
|
#include "llvm/Support/Mangler.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2004-02-14 07:00:36 +01:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2006-03-14 00:20:37 +01:00
|
|
|
Statistic<> llvm::EmittedInsts("asm-printer",
|
|
|
|
"Number of machine instrs printed");
|
2004-10-04 09:31:08 +02:00
|
|
|
|
2005-07-02 00:44:09 +02:00
|
|
|
enum AsmWriterFlavorTy { att, intel };
|
|
|
|
cl::opt<AsmWriterFlavorTy>
|
|
|
|
AsmWriterFlavor("x86-asm-syntax",
|
2005-07-11 08:25:47 +02: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 18:19:27 +02:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
cl::init(intel)
|
|
|
|
#else
|
|
|
|
cl::init(att)
|
|
|
|
#endif
|
|
|
|
);
|
2004-10-04 09:31:08 +02:00
|
|
|
|
2005-07-02 00:44:09 +02:00
|
|
|
/// doInitialization
|
2005-11-21 23:19:48 +01:00
|
|
|
bool X86SharedAsmPrinter::doInitialization(Module &M) {
|
2005-11-21 23:39:40 +01:00
|
|
|
const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
|
|
|
|
|
2005-11-21 20:50:31 +01:00
|
|
|
forDarwin = false;
|
2006-02-23 06:25:02 +01:00
|
|
|
PrivateGlobalPrefix = ".L";
|
2006-05-09 07:12:53 +02:00
|
|
|
DefaultTextSection = ".text";
|
|
|
|
DefaultDataSection = ".data";
|
2005-11-21 23:39:40 +01:00
|
|
|
|
|
|
|
switch (Subtarget->TargetType) {
|
|
|
|
case X86Subtarget::isDarwin:
|
2005-06-30 02:53:20 +02:00
|
|
|
AlignmentIsInBytes = false;
|
2005-11-21 20:50:31 +01:00
|
|
|
GlobalPrefix = "_";
|
2005-07-16 03:59:47 +02:00
|
|
|
Data64bitsDirective = 0; // we can't emit a 64-bit unit
|
|
|
|
ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
|
2005-11-21 07:46:22 +01:00
|
|
|
PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
|
2005-11-21 09:32:23 +01:00
|
|
|
ConstantPoolSection = "\t.const\n";
|
2006-04-22 20:53:45 +02:00
|
|
|
JumpTableSection = "\t.const\n"; // FIXME: depends on PIC mode
|
2005-11-21 20:50:31 +01:00
|
|
|
LCOMMDirective = "\t.lcomm\t";
|
|
|
|
COMMDirectiveTakesAlignment = false;
|
2005-11-22 00:06:54 +01:00
|
|
|
HasDotTypeDotSizeDirective = false;
|
2005-11-21 23:39:40 +01:00
|
|
|
forDarwin = true;
|
2005-12-13 07:32:50 +01:00
|
|
|
StaticCtorsSection = ".mod_init_func";
|
|
|
|
StaticDtorsSection = ".mod_term_func";
|
2006-05-05 23:48:50 +02:00
|
|
|
InlineAsmStart = "# InlineAsm Start";
|
|
|
|
InlineAsmEnd = "# InlineAsm End";
|
2005-11-21 23:39:40 +01:00
|
|
|
break;
|
|
|
|
case X86Subtarget::isCygwin:
|
2005-11-21 20:50:31 +01:00
|
|
|
GlobalPrefix = "_";
|
|
|
|
COMMDirectiveTakesAlignment = false;
|
2005-11-22 00:06:54 +01:00
|
|
|
HasDotTypeDotSizeDirective = false;
|
2005-11-21 23:39:40 +01:00
|
|
|
break;
|
|
|
|
case X86Subtarget::isWindows:
|
2005-11-21 20:50:31 +01:00
|
|
|
GlobalPrefix = "_";
|
2005-11-22 00:06:54 +01:00
|
|
|
HasDotTypeDotSizeDirective = false;
|
2005-11-21 23:39:40 +01:00
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
2005-11-21 20:50:31 +01:00
|
|
|
|
2006-03-07 03:23:26 +01:00
|
|
|
if (forDarwin) {
|
|
|
|
// Emit initial debug information.
|
2006-03-23 19:09:44 +01:00
|
|
|
DW.BeginModule(&M);
|
2006-03-07 03:23:26 +01:00
|
|
|
}
|
2006-03-07 03:02:57 +01:00
|
|
|
|
2005-01-23 04:52:14 +01:00
|
|
|
return AsmPrinter::doInitialization(M);
|
|
|
|
}
|
|
|
|
|
2004-10-04 09:24:48 +02:00
|
|
|
bool X86SharedAsmPrinter::doFinalization(Module &M) {
|
2006-05-06 23:27:14 +02: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 03:29:57 +02:00
|
|
|
const TargetData *TD = TM.getTargetData();
|
2004-10-04 09:24:48 +02:00
|
|
|
|
|
|
|
// Print out module-level global variables here.
|
2006-02-07 09:38:37 +01:00
|
|
|
for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
|
|
|
|
I != E; ++I) {
|
2005-11-21 23:48:18 +01:00
|
|
|
if (!I->hasInitializer()) continue; // External global require no code
|
|
|
|
|
2005-12-13 07:32:50 +01:00
|
|
|
// Check to see if this is a special global used by LLVM, if so, emit it.
|
2006-03-07 23:00:35 +01:00
|
|
|
if (EmitSpecialLLVMGlobal(I))
|
2005-12-13 07:32:50 +01:00
|
|
|
continue;
|
2005-12-13 05:53:51 +01:00
|
|
|
|
2005-11-21 23:48:18 +01:00
|
|
|
std::string name = Mang->getValueName(I);
|
|
|
|
Constant *C = I->getInitializer();
|
2006-05-03 03:29:57 +02:00
|
|
|
unsigned Size = TD->getTypeSize(C->getType());
|
2006-02-05 02:45:04 +01:00
|
|
|
unsigned Align = getPreferredAlignmentLog(I);
|
2005-07-27 08:12:32 +02:00
|
|
|
|
2006-02-07 09:38:37 +01:00
|
|
|
if (C->isNullValue() && /* FIXME: Verify correct */
|
|
|
|
(I->hasInternalLinkage() || I->hasWeakLinkage() ||
|
2006-02-15 02:56:23 +01:00
|
|
|
I->hasLinkOnceLinkage() ||
|
|
|
|
(forDarwin && I->hasExternalLinkage() && !I->hasSection()))) {
|
2006-02-07 09:38:37 +01:00
|
|
|
if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
|
2006-02-15 02:56:23 +01:00
|
|
|
if (I->hasExternalLinkage()) {
|
2006-02-23 03:43:52 +01:00
|
|
|
O << "\t.globl\t" << name << "\n";
|
2006-02-15 02:56:23 +01:00
|
|
|
O << "\t.zerofill __DATA__, __common, " << name << ", "
|
|
|
|
<< Size << ", " << Align;
|
2006-02-07 09:38:37 +01:00
|
|
|
} else {
|
2006-05-09 07:12:53 +02:00
|
|
|
SwitchToDataSection(DefaultDataSection, I);
|
2006-02-15 02:56:23 +01:00
|
|
|
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 09:38:37 +01:00
|
|
|
}
|
2006-02-23 03:43:52 +01:00
|
|
|
O << "\t\t" << CommentString << " " << I->getName() << "\n";
|
2006-02-07 09:38:37 +01:00
|
|
|
} else {
|
|
|
|
switch (I->getLinkage()) {
|
|
|
|
case GlobalValue::LinkOnceLinkage:
|
|
|
|
case GlobalValue::WeakLinkage:
|
|
|
|
if (forDarwin) {
|
2006-02-08 00:32:58 +01:00
|
|
|
O << "\t.globl " << name << "\n"
|
|
|
|
<< "\t.weak_definition " << name << "\n";
|
2006-05-09 06:59:56 +02:00
|
|
|
SwitchToDataSection(".section __DATA,__datacoal_nt,coalesced", I);
|
2005-11-21 23:48:18 +01:00
|
|
|
} else {
|
2006-02-07 09:38:37 +01:00
|
|
|
O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
|
|
|
|
O << "\t.weak " << name << "\n";
|
2005-07-11 08:25:47 +02:00
|
|
|
}
|
2006-02-07 09:38:37 +01: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:
|
2006-05-09 07:12:53 +02:00
|
|
|
SwitchToDataSection(DefaultDataSection, I);
|
2006-02-07 09:38:37 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0 && "Unknown linkage type!");
|
2005-11-21 20:50:31 +01:00
|
|
|
}
|
|
|
|
|
2006-02-07 09:38:37 +01:00
|
|
|
EmitAlignment(Align, I);
|
2006-02-08 00:32:58 +01:00
|
|
|
O << name << ":\t\t\t\t" << CommentString << " " << I->getName()
|
|
|
|
<< "\n";
|
|
|
|
if (HasDotTypeDotSizeDirective)
|
|
|
|
O << "\t.size " << name << ", " << Size << "\n";
|
|
|
|
|
2006-02-07 09:38:37 +01:00
|
|
|
EmitGlobalConstant(C);
|
|
|
|
O << '\n';
|
2004-10-04 09:24:48 +02:00
|
|
|
}
|
2005-11-21 23:39:40 +01:00
|
|
|
}
|
|
|
|
|
2005-07-08 02:23:26 +02:00
|
|
|
if (forDarwin) {
|
2006-05-09 06:59:56 +02:00
|
|
|
SwitchToDataSection("", 0);
|
2005-07-12 03:37:28 +02:00
|
|
|
|
2005-07-08 02:23:26 +02: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 08:25:47 +02:00
|
|
|
i != e; ++i, ++j) {
|
2006-05-09 06:59:56 +02:00
|
|
|
SwitchToDataSection(".section __IMPORT,__jump_table,symbol_stubs,"
|
|
|
|
"self_modifying_code+pure_instructions,5", 0);
|
2005-07-08 02:23:26 +02:00
|
|
|
O << "L" << *i << "$stub:\n";
|
|
|
|
O << "\t.indirect_symbol " << *i << "\n";
|
2006-02-07 09:38:37 +01:00
|
|
|
O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
|
2005-07-08 02:23:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
O << "\n";
|
2005-07-27 08:12:32 +02:00
|
|
|
|
2006-02-07 09:38:37 +01:00
|
|
|
// Output stubs for external and common global variables.
|
|
|
|
if (GVStubs.begin() != GVStubs.end())
|
2006-05-09 06:59:56 +02:00
|
|
|
SwitchToDataSection(
|
|
|
|
".section __IMPORT,__pointers,non_lazy_symbol_pointers", 0);
|
2006-02-07 09:38:37 +01:00
|
|
|
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 02:23:26 +02:00
|
|
|
}
|
2004-10-04 09:24:48 +02:00
|
|
|
|
2006-03-07 03:23:26 +01:00
|
|
|
// Emit initial debug information.
|
2006-03-23 19:09:44 +01:00
|
|
|
DW.EndModule();
|
2006-03-07 03:23:26 +01: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 19:09:44 +01: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 03:23:26 +01:00
|
|
|
O << "\t.subsections_via_symbols\n";
|
|
|
|
}
|
2006-03-07 03:02:57 +01:00
|
|
|
|
2004-10-04 09:24:48 +02: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-14 00:20:37 +01:00
|
|
|
FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,
|
|
|
|
X86TargetMachine &tm){
|
2004-10-04 09:24:48 +02:00
|
|
|
switch (AsmWriterFlavor) {
|
2005-04-22 01:38:14 +02:00
|
|
|
default:
|
2005-01-23 04:52:14 +01:00
|
|
|
assert(0 && "Unknown asm flavor!");
|
2004-10-04 09:24:48 +02:00
|
|
|
case intel:
|
|
|
|
return new X86IntelAsmPrinter(o, tm);
|
|
|
|
case att:
|
|
|
|
return new X86ATTAsmPrinter(o, tm);
|
|
|
|
}
|
2003-06-19 21:32:32 +02:00
|
|
|
}
|