2005-07-02 06:44:09 +08:00
|
|
|
//===-- X86AsmPrinter.cpp - Convert X86 LLVM IR to X86 assembly -----------===//
|
2005-04-22 07:38:14 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08: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 07:38:14 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-10-26 06:55:53 +08:00
|
|
|
//
|
2005-07-02 06:44:09 +08: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 06:55:53 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-03-14 07:20:37 +08:00
|
|
|
#include "X86AsmPrinter.h"
|
2005-07-02 06:44:09 +08:00
|
|
|
#include "X86ATTAsmPrinter.h"
|
|
|
|
#include "X86IntelAsmPrinter.h"
|
2006-09-21 06:03:51 +08:00
|
|
|
#include "X86MachineFunctionInfo.h"
|
2005-11-22 06:39:40 +08:00
|
|
|
#include "X86Subtarget.h"
|
2006-09-21 06:03:51 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/CallingConv.h"
|
2005-12-13 12:53:51 +08:00
|
|
|
#include "llvm/Constants.h"
|
2003-08-12 04:06:16 +08:00
|
|
|
#include "llvm/Module.h"
|
2005-11-21 14:46:22 +08:00
|
|
|
#include "llvm/Type.h"
|
2003-08-12 04:06:16 +08:00
|
|
|
#include "llvm/Assembly/Writer.h"
|
2003-07-25 04:20:44 +08:00
|
|
|
#include "llvm/Support/Mangler.h"
|
2006-09-08 06:06:40 +08:00
|
|
|
#include "llvm/Target/TargetAsmInfo.h"
|
2006-09-21 06:03:51 +08:00
|
|
|
|
2004-02-14 14:00:36 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2006-03-14 07:20:37 +08:00
|
|
|
Statistic<> llvm::EmittedInsts("asm-printer",
|
|
|
|
"Number of machine instrs printed");
|
2004-10-04 15:31:08 +08:00
|
|
|
|
2006-09-26 11:57:53 +08:00
|
|
|
static X86FunctionInfo calculateFunctionInfo(const Function *F,
|
|
|
|
const TargetData *TD) {
|
2006-09-21 06:03:51 +08:00
|
|
|
X86FunctionInfo Info;
|
2006-09-26 11:57:53 +08:00
|
|
|
uint64_t Size = 0;
|
2006-09-21 06:03:51 +08:00
|
|
|
|
|
|
|
switch (F->getCallingConv()) {
|
2006-09-26 11:57:53 +08:00
|
|
|
case CallingConv::X86_StdCall:
|
2006-09-21 06:03:51 +08:00
|
|
|
Info.setDecorationStyle(StdCall);
|
|
|
|
break;
|
2006-09-26 11:57:53 +08:00
|
|
|
case CallingConv::X86_FastCall:
|
2006-09-21 06:03:51 +08:00
|
|
|
Info.setDecorationStyle(FastCall);
|
|
|
|
break;
|
2006-09-26 11:57:53 +08:00
|
|
|
default:
|
2006-09-21 06:03:51 +08:00
|
|
|
return Info;
|
|
|
|
}
|
|
|
|
|
2006-09-26 11:57:53 +08:00
|
|
|
for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
|
|
|
|
AI != AE; ++AI)
|
|
|
|
Size += TD->getTypeSize(AI->getType());
|
2006-09-21 06:03:51 +08:00
|
|
|
|
2006-10-15 04:53:35 +08:00
|
|
|
// Size should be aligned to DWORD boundary
|
|
|
|
Size = ((Size + 3)/4)*4;
|
|
|
|
|
2006-09-21 06:03:51 +08:00
|
|
|
// We're not supporting tooooo huge arguments :)
|
2006-09-26 11:57:53 +08:00
|
|
|
Info.setBytesToPopOnReturn((unsigned int)Size);
|
2006-09-21 06:03:51 +08:00
|
|
|
return Info;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-26 11:57:53 +08:00
|
|
|
/// decorateName - Query FunctionInfoMap and use this information for various
|
|
|
|
/// name decoration.
|
|
|
|
void X86SharedAsmPrinter::decorateName(std::string &Name,
|
|
|
|
const GlobalValue *GV) {
|
|
|
|
const Function *F = dyn_cast<Function>(GV);
|
|
|
|
if (!F) return;
|
2006-09-21 06:03:51 +08:00
|
|
|
|
|
|
|
// We don't want to decorate non-stdcall or non-fastcall functions right now
|
2006-09-26 11:57:53 +08:00
|
|
|
unsigned CC = F->getCallingConv();
|
|
|
|
if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
|
2006-09-21 06:03:51 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
|
|
|
|
|
2006-09-26 11:57:53 +08:00
|
|
|
const X86FunctionInfo *Info;
|
2006-09-21 06:03:51 +08:00
|
|
|
if (info_item == FunctionInfoMap.end()) {
|
|
|
|
// Calculate apropriate function info and populate map
|
|
|
|
FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
|
|
|
|
Info = &FunctionInfoMap[F];
|
|
|
|
} else {
|
2006-09-26 11:57:53 +08:00
|
|
|
Info = &info_item->second;
|
2006-09-21 06:03:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (Info->getDecorationStyle()) {
|
2006-09-26 11:57:53 +08:00
|
|
|
case None:
|
2006-09-21 06:03:51 +08:00
|
|
|
break;
|
2006-09-26 11:57:53 +08:00
|
|
|
case StdCall:
|
|
|
|
if (!F->isVarArg()) // Variadic functions do not receive @0 suffix.
|
2006-09-21 06:03:51 +08:00
|
|
|
Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
|
|
|
|
break;
|
2006-09-26 11:57:53 +08:00
|
|
|
case FastCall:
|
|
|
|
if (!F->isVarArg()) // Variadic functions do not receive @0 suffix.
|
2006-09-21 06:03:51 +08:00
|
|
|
Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
|
2006-09-26 11:57:53 +08:00
|
|
|
|
2006-09-21 06:03:51 +08:00
|
|
|
if (Name[0] == '_') {
|
|
|
|
Name[0] = '@';
|
|
|
|
} else {
|
|
|
|
Name = '@' + Name;
|
|
|
|
}
|
|
|
|
break;
|
2006-09-26 11:57:53 +08:00
|
|
|
default:
|
2006-09-21 06:03:51 +08:00
|
|
|
assert(0 && "Unsupported DecorationStyle");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-07 02:34:40 +08:00
|
|
|
/// doInitialization
|
2006-09-08 14:48:29 +08:00
|
|
|
bool X86SharedAsmPrinter::doInitialization(Module &M) {
|
2006-07-27 10:05:13 +08:00
|
|
|
if (Subtarget->isTargetDarwin()) {
|
2006-09-08 14:48:29 +08:00
|
|
|
const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
|
|
|
|
if (!Subtarget->is64Bit())
|
|
|
|
X86PICStyle = PICStyle::Stub;
|
|
|
|
|
2006-03-07 10:23:26 +08:00
|
|
|
// Emit initial debug information.
|
2006-03-24 02:09:44 +08:00
|
|
|
DW.BeginModule(&M);
|
2006-03-07 10:23:26 +08:00
|
|
|
}
|
2006-03-07 10:02:57 +08:00
|
|
|
|
2005-01-23 11:52:14 +08:00
|
|
|
return AsmPrinter::doInitialization(M);
|
|
|
|
}
|
|
|
|
|
2004-10-04 15:24:48 +08:00
|
|
|
bool X86SharedAsmPrinter::doFinalization(Module &M) {
|
2006-05-07 05:27:14 +08: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 09:29:57 +08:00
|
|
|
const TargetData *TD = TM.getTargetData();
|
2004-10-04 15:24:48 +08:00
|
|
|
|
|
|
|
// Print out module-level global variables here.
|
2006-02-07 16:38:37 +08:00
|
|
|
for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
|
|
|
|
I != E; ++I) {
|
2005-11-22 06:48:18 +08:00
|
|
|
if (!I->hasInitializer()) continue; // External global require no code
|
|
|
|
|
2005-12-13 14:32:50 +08:00
|
|
|
// Check to see if this is a special global used by LLVM, if so, emit it.
|
2006-03-08 06:00:35 +08:00
|
|
|
if (EmitSpecialLLVMGlobal(I))
|
2005-12-13 14:32:50 +08:00
|
|
|
continue;
|
2005-12-13 12:53:51 +08:00
|
|
|
|
2005-11-22 06:48:18 +08:00
|
|
|
std::string name = Mang->getValueName(I);
|
|
|
|
Constant *C = I->getInitializer();
|
2006-05-03 09:29:57 +08:00
|
|
|
unsigned Size = TD->getTypeSize(C->getType());
|
2006-10-25 04:32:14 +08:00
|
|
|
unsigned Align = TD->getPreferredAlignmentLog(I);
|
2005-07-27 14:12:32 +08:00
|
|
|
|
2006-02-07 16:38:37 +08:00
|
|
|
if (C->isNullValue() && /* FIXME: Verify correct */
|
|
|
|
(I->hasInternalLinkage() || I->hasWeakLinkage() ||
|
2006-02-15 09:56:23 +08:00
|
|
|
I->hasLinkOnceLinkage() ||
|
2006-07-27 10:05:13 +08:00
|
|
|
(Subtarget->isTargetDarwin() &&
|
2006-05-26 05:59:08 +08:00
|
|
|
I->hasExternalLinkage() && !I->hasSection()))) {
|
2006-02-07 16:38:37 +08:00
|
|
|
if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
|
2006-02-15 09:56:23 +08:00
|
|
|
if (I->hasExternalLinkage()) {
|
2006-02-23 10:43:52 +08:00
|
|
|
O << "\t.globl\t" << name << "\n";
|
2006-02-15 09:56:23 +08:00
|
|
|
O << "\t.zerofill __DATA__, __common, " << name << ", "
|
|
|
|
<< Size << ", " << Align;
|
2006-02-07 16:38:37 +08:00
|
|
|
} else {
|
2006-09-07 02:34:40 +08:00
|
|
|
SwitchToDataSection(TAI->getDataSection(), I);
|
|
|
|
if (TAI->getLCOMMDirective() != NULL) {
|
2006-02-15 09:56:23 +08:00
|
|
|
if (I->hasInternalLinkage()) {
|
2006-09-07 02:34:40 +08:00
|
|
|
O << TAI->getLCOMMDirective() << name << "," << Size;
|
2006-07-27 10:05:13 +08:00
|
|
|
if (Subtarget->isTargetDarwin())
|
2006-09-07 02:34:40 +08:00
|
|
|
O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
|
2006-02-15 09:56:23 +08:00
|
|
|
} else
|
2006-09-07 02:34:40 +08:00
|
|
|
O << TAI->getCOMMDirective() << name << "," << Size;
|
2006-02-15 09:56:23 +08:00
|
|
|
} else {
|
2006-09-18 04:25:45 +08:00
|
|
|
if (!Subtarget->isTargetCygwin()) {
|
2006-05-26 05:59:08 +08:00
|
|
|
if (I->hasInternalLinkage())
|
|
|
|
O << "\t.local\t" << name << "\n";
|
|
|
|
}
|
2006-09-07 02:34:40 +08:00
|
|
|
O << TAI->getCOMMDirective() << name << "," << Size;
|
|
|
|
if (TAI->getCOMMDirectiveTakesAlignment())
|
|
|
|
O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
|
2006-02-15 09:56:23 +08:00
|
|
|
}
|
2006-02-07 16:38:37 +08:00
|
|
|
}
|
2006-09-07 02:34:40 +08:00
|
|
|
O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n";
|
2006-02-07 16:38:37 +08:00
|
|
|
} else {
|
|
|
|
switch (I->getLinkage()) {
|
|
|
|
case GlobalValue::LinkOnceLinkage:
|
2006-10-18 17:12:29 +08:00
|
|
|
case GlobalValue::WeakLinkage:
|
2006-10-18 04:29:49 +08:00
|
|
|
if (Subtarget->isTargetDarwin()) {
|
|
|
|
O << "\t.globl " << name << "\n"
|
|
|
|
<< "\t.weak_definition " << name << "\n";
|
|
|
|
SwitchToDataSection(".section __DATA,__const_coal,coalesced", I);
|
|
|
|
} else if (Subtarget->isTargetCygwin()) {
|
2006-10-23 05:37:13 +08:00
|
|
|
std::string SectionName(".section\t.data$linkonce." +
|
|
|
|
name +
|
|
|
|
",\"aw\"\n");
|
|
|
|
SwitchToDataSection(SectionName.c_str(), I);
|
|
|
|
O << "\t.globl " << name << "\n"
|
2006-10-18 04:29:49 +08:00
|
|
|
<< "\t.linkonce same_size\n";
|
|
|
|
} else {
|
2006-10-23 05:37:13 +08:00
|
|
|
std::string SectionName("\t.section\t.llvm.linkonce.d." +
|
|
|
|
name +
|
|
|
|
",\"aw\",@progbits\n");
|
|
|
|
SwitchToDataSection(SectionName.c_str(), I);
|
|
|
|
O << "\t.weak " << name << "\n";
|
2006-10-18 04:29:49 +08:00
|
|
|
}
|
|
|
|
break;
|
2006-02-07 16:38:37 +08:00
|
|
|
case GlobalValue::AppendingLinkage:
|
|
|
|
// FIXME: appending linkage variables should go into a section of
|
|
|
|
// their name or something. For now, just emit them as external.
|
2006-09-15 02:23:27 +08:00
|
|
|
case GlobalValue::DLLExportLinkage:
|
|
|
|
DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
|
|
|
|
// FALL THROUGH
|
2006-02-07 16:38:37 +08:00
|
|
|
case GlobalValue::ExternalLinkage:
|
|
|
|
// If external or appending, declare as a global symbol
|
|
|
|
O << "\t.globl " << name << "\n";
|
|
|
|
// FALL THROUGH
|
2006-10-27 03:18:18 +08:00
|
|
|
case GlobalValue::InternalLinkage: {
|
|
|
|
if (TAI->getCStringSection()) {
|
|
|
|
const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
|
|
|
|
if (CVA && CVA->isCString()) {
|
|
|
|
SwitchToDataSection(TAI->getCStringSection(), I);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-09-07 02:34:40 +08:00
|
|
|
SwitchToDataSection(TAI->getDataSection(), I);
|
2006-02-07 16:38:37 +08:00
|
|
|
break;
|
2006-10-27 03:18:18 +08:00
|
|
|
}
|
2006-02-07 16:38:37 +08:00
|
|
|
default:
|
|
|
|
assert(0 && "Unknown linkage type!");
|
2005-11-22 03:50:31 +08:00
|
|
|
}
|
|
|
|
|
2006-02-07 16:38:37 +08:00
|
|
|
EmitAlignment(Align, I);
|
2006-09-07 02:34:40 +08:00
|
|
|
O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName()
|
2006-02-08 07:32:58 +08:00
|
|
|
<< "\n";
|
2006-09-07 02:34:40 +08:00
|
|
|
if (TAI->hasDotTypeDotSizeDirective())
|
2006-02-08 07:32:58 +08:00
|
|
|
O << "\t.size " << name << ", " << Size << "\n";
|
|
|
|
|
2006-02-07 16:38:37 +08:00
|
|
|
EmitGlobalConstant(C);
|
|
|
|
O << '\n';
|
2004-10-04 15:24:48 +08:00
|
|
|
}
|
2005-11-22 06:39:40 +08:00
|
|
|
}
|
|
|
|
|
2006-09-15 02:23:27 +08:00
|
|
|
// Output linker support code for dllexported globals
|
|
|
|
if (DLLExportedGVs.begin() != DLLExportedGVs.end()) {
|
|
|
|
SwitchToDataSection(".section .drectve", 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::set<std::string>::iterator i = DLLExportedGVs.begin(),
|
|
|
|
e = DLLExportedGVs.end();
|
|
|
|
i != e; ++i) {
|
|
|
|
O << "\t.ascii \" -export:" << *i << ",data\"\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DLLExportedFns.begin() != DLLExportedFns.end()) {
|
|
|
|
SwitchToDataSection(".section .drectve", 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::set<std::string>::iterator i = DLLExportedFns.begin(),
|
|
|
|
e = DLLExportedFns.end();
|
|
|
|
i != e; ++i) {
|
|
|
|
O << "\t.ascii \" -export:" << *i << "\"\n";
|
|
|
|
}
|
|
|
|
|
2006-07-27 10:05:13 +08:00
|
|
|
if (Subtarget->isTargetDarwin()) {
|
2006-05-09 12:59:56 +08:00
|
|
|
SwitchToDataSection("", 0);
|
2005-07-12 09:37:28 +08:00
|
|
|
|
2005-07-08 08:23:26 +08: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 14:25:47 +08:00
|
|
|
i != e; ++i, ++j) {
|
2006-05-09 12:59:56 +08:00
|
|
|
SwitchToDataSection(".section __IMPORT,__jump_table,symbol_stubs,"
|
|
|
|
"self_modifying_code+pure_instructions,5", 0);
|
2005-07-08 08:23:26 +08:00
|
|
|
O << "L" << *i << "$stub:\n";
|
|
|
|
O << "\t.indirect_symbol " << *i << "\n";
|
2006-02-07 16:38:37 +08:00
|
|
|
O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
|
2005-07-08 08:23:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
O << "\n";
|
2005-07-27 14:12:32 +08:00
|
|
|
|
2006-02-07 16:38:37 +08:00
|
|
|
// Output stubs for external and common global variables.
|
|
|
|
if (GVStubs.begin() != GVStubs.end())
|
2006-05-09 12:59:56 +08:00
|
|
|
SwitchToDataSection(
|
|
|
|
".section __IMPORT,__pointers,non_lazy_symbol_pointers", 0);
|
2006-02-07 16:38:37 +08: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 08:23:26 +08:00
|
|
|
}
|
2004-10-04 15:24:48 +08:00
|
|
|
|
2006-03-07 10:23:26 +08:00
|
|
|
// Emit initial debug information.
|
2006-03-24 02:09:44 +08:00
|
|
|
DW.EndModule();
|
2006-03-07 10:23:26 +08: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-24 02:09:44 +08: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 10:23:26 +08:00
|
|
|
O << "\t.subsections_via_symbols\n";
|
|
|
|
}
|
2006-03-07 10:02:57 +08:00
|
|
|
|
2004-10-04 15:24:48 +08: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 07:20:37 +08:00
|
|
|
FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,
|
2006-09-07 02:34:40 +08:00
|
|
|
X86TargetMachine &tm) {
|
2006-09-07 20:23:47 +08:00
|
|
|
const X86Subtarget *Subtarget = &tm.getSubtarget<X86Subtarget>();
|
2006-09-07 02:34:40 +08:00
|
|
|
|
2006-09-07 20:23:47 +08:00
|
|
|
if (Subtarget->isFlavorIntel()) {
|
2006-09-08 06:06:40 +08:00
|
|
|
return new X86IntelAsmPrinter(o, tm, tm.getTargetAsmInfo());
|
2006-09-07 20:23:47 +08:00
|
|
|
} else {
|
2006-09-08 06:06:40 +08:00
|
|
|
return new X86ATTAsmPrinter(o, tm, tm.getTargetAsmInfo());
|
2004-10-04 15:24:48 +08:00
|
|
|
}
|
2003-06-20 03:32:32 +08:00
|
|
|
}
|