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"
|
2006-09-07 22:06:40 +00:00
|
|
|
#include "llvm/Target/TargetAsmInfo.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
|
|
|
|
2006-09-06 18:34:40 +00:00
|
|
|
/// doInitialization
|
2006-09-08 06:48:29 +00:00
|
|
|
bool X86SharedAsmPrinter::doInitialization(Module &M) {
|
2006-07-27 02:05:13 +00:00
|
|
|
if (Subtarget->isTargetDarwin()) {
|
2006-09-08 06:48:29 +00:00
|
|
|
const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
|
|
|
|
if (!Subtarget->is64Bit())
|
|
|
|
X86PICStyle = PICStyle::Stub;
|
|
|
|
|
2006-03-07 02:23:26 +00:00
|
|
|
// 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() ||
|
2006-07-27 02:05:13 +00:00
|
|
|
(Subtarget->isTargetDarwin() &&
|
2006-05-25 21:59:08 +00:00
|
|
|
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-09-06 18:34:40 +00:00
|
|
|
SwitchToDataSection(TAI->getDataSection(), I);
|
|
|
|
if (TAI->getLCOMMDirective() != NULL) {
|
2006-02-15 01:56:23 +00:00
|
|
|
if (I->hasInternalLinkage()) {
|
2006-09-06 18:34:40 +00:00
|
|
|
O << TAI->getLCOMMDirective() << name << "," << Size;
|
2006-07-27 02:05:13 +00:00
|
|
|
if (Subtarget->isTargetDarwin())
|
2006-09-06 18:34:40 +00:00
|
|
|
O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
|
2006-02-15 01:56:23 +00:00
|
|
|
} else
|
2006-09-06 18:34:40 +00:00
|
|
|
O << TAI->getCOMMDirective() << name << "," << Size;
|
2006-02-15 01:56:23 +00:00
|
|
|
} else {
|
2006-05-26 06:22:34 +00:00
|
|
|
if (Subtarget->TargetType != X86Subtarget::isCygwin) {
|
2006-05-25 21:59:08 +00:00
|
|
|
if (I->hasInternalLinkage())
|
|
|
|
O << "\t.local\t" << name << "\n";
|
|
|
|
}
|
2006-09-06 18:34:40 +00:00
|
|
|
O << TAI->getCOMMDirective() << name << "," << Size;
|
|
|
|
if (TAI->getCOMMDirectiveTakesAlignment())
|
|
|
|
O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
|
2006-02-15 01:56:23 +00:00
|
|
|
}
|
2006-02-07 08:38:37 +00:00
|
|
|
}
|
2006-09-06 18:34:40 +00:00
|
|
|
O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n";
|
2006-02-07 08:38:37 +00:00
|
|
|
} else {
|
|
|
|
switch (I->getLinkage()) {
|
|
|
|
case GlobalValue::LinkOnceLinkage:
|
|
|
|
case GlobalValue::WeakLinkage:
|
2006-07-27 02:05:13 +00:00
|
|
|
if (Subtarget->isTargetDarwin()) {
|
2006-02-07 23:32:58 +00:00
|
|
|
O << "\t.globl " << name << "\n"
|
|
|
|
<< "\t.weak_definition " << name << "\n";
|
2006-06-04 07:24:07 +00:00
|
|
|
SwitchToDataSection(".section __DATA,__const_coal,coalesced", I);
|
2006-05-25 21:59:08 +00:00
|
|
|
} else if (Subtarget->TargetType == X86Subtarget::isCygwin) {
|
|
|
|
O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\"\n"
|
|
|
|
<< "\t.weak " << name << "\n";
|
2005-11-21 22:48:18 +00:00
|
|
|
} else {
|
2006-05-25 21:59:08 +00:00
|
|
|
O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n"
|
|
|
|
<< "\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:
|
2006-09-06 18:34:40 +00:00
|
|
|
SwitchToDataSection(TAI->getDataSection(), I);
|
2006-02-07 08:38:37 +00:00
|
|
|
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-09-06 18:34:40 +00:00
|
|
|
O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName()
|
2006-02-07 23:32:58 +00:00
|
|
|
<< "\n";
|
2006-09-06 18:34:40 +00:00
|
|
|
if (TAI->hasDotTypeDotSizeDirective())
|
2006-02-07 23:32:58 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2006-07-27 02:05:13 +00:00
|
|
|
if (Subtarget->isTargetDarwin()) {
|
2006-05-09 04:59:56 +00:00
|
|
|
SwitchToDataSection("", 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-05-09 04:59:56 +00:00
|
|
|
SwitchToDataSection(".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())
|
2006-05-09 04:59:56 +00:00
|
|
|
SwitchToDataSection(
|
|
|
|
".section __IMPORT,__pointers,non_lazy_symbol_pointers", 0);
|
2006-02-07 08:38:37 +00: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 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,
|
2006-09-06 18:34:40 +00:00
|
|
|
X86TargetMachine &tm) {
|
2006-09-07 12:23:47 +00:00
|
|
|
const X86Subtarget *Subtarget = &tm.getSubtarget<X86Subtarget>();
|
2006-09-06 18:34:40 +00:00
|
|
|
|
2006-09-07 12:23:47 +00:00
|
|
|
if (Subtarget->isFlavorIntel()) {
|
2006-09-07 22:06:40 +00:00
|
|
|
return new X86IntelAsmPrinter(o, tm, tm.getTargetAsmInfo());
|
2006-09-07 12:23:47 +00:00
|
|
|
} else {
|
2006-09-07 22:06:40 +00:00
|
|
|
return new X86ATTAsmPrinter(o, tm, tm.getTargetAsmInfo());
|
2004-10-04 07:24:48 +00:00
|
|
|
}
|
2003-06-19 19:32:32 +00:00
|
|
|
}
|