2010-09-07 18:14:24 +00:00
|
|
|
//===-- PTXAsmPrinter.cpp - PTX LLVM assembly writer ----------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains a printer that converts from our internal representation
|
|
|
|
// of machine-dependent LLVM code to PTX assembly language.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-11-08 03:06:08 +00:00
|
|
|
#define DEBUG_TYPE "ptx-asm-printer"
|
|
|
|
|
2010-09-07 18:14:24 +00:00
|
|
|
#include "PTX.h"
|
2011-09-28 14:32:04 +00:00
|
|
|
#include "PTXAsmPrinter.h"
|
2010-11-08 03:06:08 +00:00
|
|
|
#include "PTXMachineFunctionInfo.h"
|
2011-09-23 14:18:22 +00:00
|
|
|
#include "PTXParamManager.h"
|
2011-09-22 16:45:40 +00:00
|
|
|
#include "PTXRegisterInfo.h"
|
2010-09-07 18:14:24 +00:00
|
|
|
#include "PTXTargetMachine.h"
|
2010-12-22 10:38:51 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Module.h"
|
2010-09-18 18:52:28 +00:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2010-11-08 03:06:08 +00:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
2011-06-24 19:19:18 +00:00
|
|
|
#include "llvm/Analysis/DebugInfo.h"
|
2010-09-07 18:14:24 +00:00
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
2011-06-20 15:56:20 +00:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2010-09-18 18:52:28 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2011-03-02 03:20:28 +00:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2011-06-24 19:19:18 +00:00
|
|
|
#include "llvm/MC/MCContext.h"
|
2011-09-28 14:32:04 +00:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
|
|
|
#include "llvm/MC/MCInst.h"
|
2010-09-18 18:52:28 +00:00
|
|
|
#include "llvm/MC/MCStreamer.h"
|
2010-11-08 03:06:08 +00:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2010-12-22 10:38:51 +00:00
|
|
|
#include "llvm/Target/Mangler.h"
|
2010-11-08 03:06:08 +00:00
|
|
|
#include "llvm/Target/TargetLoweringObjectFile.h"
|
2010-11-30 10:14:14 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2010-11-08 03:06:08 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2010-12-22 10:38:51 +00:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2011-06-24 19:19:18 +00:00
|
|
|
#include "llvm/Support/Path.h"
|
2011-08-24 18:08:43 +00:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2010-11-08 03:06:08 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-09-07 18:14:24 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2010-11-08 03:06:08 +00:00
|
|
|
static const char PARAM_PREFIX[] = "__param_";
|
2011-06-23 18:10:05 +00:00
|
|
|
static const char RETURN_PREFIX[] = "__ret_";
|
2010-11-08 03:06:08 +00:00
|
|
|
|
2011-09-22 16:45:46 +00:00
|
|
|
static const char *getRegisterTypeName(unsigned RegNo,
|
|
|
|
const MachineRegisterInfo& MRI) {
|
|
|
|
const TargetRegisterClass *TRC = MRI.getRegClass(RegNo);
|
|
|
|
|
|
|
|
#define TEST_REGCLS(cls, clsstr) \
|
|
|
|
if (PTX::cls ## RegisterClass == TRC) return # clsstr;
|
|
|
|
|
2011-06-16 17:49:58 +00:00
|
|
|
TEST_REGCLS(RegPred, pred);
|
2011-06-16 17:50:00 +00:00
|
|
|
TEST_REGCLS(RegI16, b16);
|
|
|
|
TEST_REGCLS(RegI32, b32);
|
|
|
|
TEST_REGCLS(RegI64, b64);
|
|
|
|
TEST_REGCLS(RegF32, b32);
|
|
|
|
TEST_REGCLS(RegF64, b64);
|
2010-11-08 03:06:08 +00:00
|
|
|
#undef TEST_REGCLS
|
|
|
|
|
|
|
|
llvm_unreachable("Not in any register class!");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-12-22 10:38:51 +00:00
|
|
|
static const char *getStateSpaceName(unsigned addressSpace) {
|
2010-12-30 10:41:27 +00:00
|
|
|
switch (addressSpace) {
|
|
|
|
default: llvm_unreachable("Unknown state space");
|
|
|
|
case PTX::GLOBAL: return "global";
|
|
|
|
case PTX::CONSTANT: return "const";
|
|
|
|
case PTX::LOCAL: return "local";
|
|
|
|
case PTX::PARAMETER: return "param";
|
|
|
|
case PTX::SHARED: return "shared";
|
|
|
|
}
|
2010-12-22 10:38:51 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
static const char *getTypeName(Type* type) {
|
2011-02-28 06:34:09 +00:00
|
|
|
while (true) {
|
|
|
|
switch (type->getTypeID()) {
|
|
|
|
default: llvm_unreachable("Unknown type");
|
|
|
|
case Type::FloatTyID: return ".f32";
|
2011-03-02 03:20:28 +00:00
|
|
|
case Type::DoubleTyID: return ".f64";
|
|
|
|
case Type::IntegerTyID:
|
|
|
|
switch (type->getPrimitiveSizeInBits()) {
|
|
|
|
default: llvm_unreachable("Unknown integer bit-width");
|
|
|
|
case 16: return ".u16";
|
|
|
|
case 32: return ".u32";
|
|
|
|
case 64: return ".u64";
|
|
|
|
}
|
2011-02-28 06:34:09 +00:00
|
|
|
case Type::ArrayTyID:
|
|
|
|
case Type::PointerTyID:
|
2011-07-18 04:54:35 +00:00
|
|
|
type = dyn_cast<SequentialType>(type)->getElementType();
|
2011-02-28 06:34:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-12-22 10:38:51 +00:00
|
|
|
bool PTXAsmPrinter::doFinalization(Module &M) {
|
|
|
|
// XXX Temproarily remove global variables so that doFinalization() will not
|
|
|
|
// emit them again (global variables are emitted at beginning).
|
|
|
|
|
|
|
|
Module::GlobalListType &global_list = M.getGlobalList();
|
|
|
|
int i, n = global_list.size();
|
|
|
|
GlobalVariable **gv_array = new GlobalVariable* [n];
|
|
|
|
|
|
|
|
// first, back-up GlobalVariable in gv_array
|
|
|
|
i = 0;
|
|
|
|
for (Module::global_iterator I = global_list.begin(), E = global_list.end();
|
|
|
|
I != E; ++I)
|
|
|
|
gv_array[i++] = &*I;
|
|
|
|
|
|
|
|
// second, empty global_list
|
|
|
|
while (!global_list.empty())
|
|
|
|
global_list.remove(global_list.begin());
|
|
|
|
|
|
|
|
// call doFinalization
|
|
|
|
bool ret = AsmPrinter::doFinalization(M);
|
|
|
|
|
|
|
|
// now we restore global variables
|
|
|
|
for (i = 0; i < n; i ++)
|
|
|
|
global_list.insert(global_list.end(), gv_array[i]);
|
|
|
|
|
|
|
|
delete[] gv_array;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-11-30 10:14:14 +00:00
|
|
|
void PTXAsmPrinter::EmitStartOfAsmFile(Module &M)
|
|
|
|
{
|
2011-03-02 03:20:28 +00:00
|
|
|
const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>();
|
|
|
|
|
2011-09-28 14:32:06 +00:00
|
|
|
// Emit the PTX .version and .target attributes
|
2011-03-02 03:20:28 +00:00
|
|
|
OutStreamer.EmitRawText(Twine("\t.version " + ST.getPTXVersionString()));
|
|
|
|
OutStreamer.EmitRawText(Twine("\t.target " + ST.getTargetString() +
|
2011-03-03 13:34:29 +00:00
|
|
|
(ST.supportsDouble() ? ""
|
|
|
|
: ", map_f64_to_f32")));
|
2011-06-22 00:43:56 +00:00
|
|
|
// .address_size directive is optional, but it must immediately follow
|
|
|
|
// the .target directive if present within a module
|
|
|
|
if (ST.supportsPTX23()) {
|
|
|
|
std::string addrSize = ST.is64Bit() ? "64" : "32";
|
|
|
|
OutStreamer.EmitRawText(Twine("\t.address_size " + addrSize));
|
|
|
|
}
|
|
|
|
|
2010-11-30 10:14:14 +00:00
|
|
|
OutStreamer.AddBlankLine();
|
2010-12-22 10:38:51 +00:00
|
|
|
|
2011-06-24 19:19:18 +00:00
|
|
|
// Define any .file directives
|
|
|
|
DebugInfoFinder DbgFinder;
|
|
|
|
DbgFinder.processModule(M);
|
|
|
|
|
|
|
|
for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
|
|
|
|
E = DbgFinder.compile_unit_end(); I != E; ++I) {
|
|
|
|
DICompileUnit DIUnit(*I);
|
|
|
|
StringRef FN = DIUnit.getFilename();
|
|
|
|
StringRef Dir = DIUnit.getDirectory();
|
|
|
|
GetOrCreateSourceID(FN, Dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
OutStreamer.AddBlankLine();
|
|
|
|
|
2010-12-22 10:38:51 +00:00
|
|
|
// declare global variables
|
|
|
|
for (Module::const_global_iterator i = M.global_begin(), e = M.global_end();
|
|
|
|
i != e; ++i)
|
|
|
|
EmitVariableDeclaration(i);
|
2010-11-30 10:14:14 +00:00
|
|
|
}
|
|
|
|
|
2010-11-08 03:06:08 +00:00
|
|
|
void PTXAsmPrinter::EmitFunctionBodyStart() {
|
|
|
|
OutStreamer.EmitRawText(Twine("{"));
|
|
|
|
|
|
|
|
const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
|
2011-09-23 14:31:12 +00:00
|
|
|
const PTXParamManager &PM = MFI->getParamManager();
|
2010-11-08 03:06:08 +00:00
|
|
|
|
2011-09-22 16:45:40 +00:00
|
|
|
// Print register definitions
|
|
|
|
std::string regDefs;
|
|
|
|
unsigned numRegs;
|
|
|
|
|
|
|
|
// pred
|
|
|
|
numRegs = MFI->getNumRegistersForClass(PTX::RegPredRegisterClass);
|
|
|
|
if(numRegs > 0) {
|
|
|
|
regDefs += "\t.reg .pred %p<";
|
|
|
|
regDefs += utostr(numRegs);
|
|
|
|
regDefs += ">;\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// i16
|
|
|
|
numRegs = MFI->getNumRegistersForClass(PTX::RegI16RegisterClass);
|
|
|
|
if(numRegs > 0) {
|
|
|
|
regDefs += "\t.reg .b16 %rh<";
|
|
|
|
regDefs += utostr(numRegs);
|
|
|
|
regDefs += ">;\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// i32
|
|
|
|
numRegs = MFI->getNumRegistersForClass(PTX::RegI32RegisterClass);
|
|
|
|
if(numRegs > 0) {
|
|
|
|
regDefs += "\t.reg .b32 %r<";
|
|
|
|
regDefs += utostr(numRegs);
|
|
|
|
regDefs += ">;\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// i64
|
|
|
|
numRegs = MFI->getNumRegistersForClass(PTX::RegI64RegisterClass);
|
|
|
|
if(numRegs > 0) {
|
|
|
|
regDefs += "\t.reg .b64 %rd<";
|
|
|
|
regDefs += utostr(numRegs);
|
|
|
|
regDefs += ">;\n";
|
2010-11-08 03:06:08 +00:00
|
|
|
}
|
2011-06-20 15:56:20 +00:00
|
|
|
|
2011-09-22 16:45:40 +00:00
|
|
|
// f32
|
|
|
|
numRegs = MFI->getNumRegistersForClass(PTX::RegF32RegisterClass);
|
|
|
|
if(numRegs > 0) {
|
|
|
|
regDefs += "\t.reg .f32 %f<";
|
|
|
|
regDefs += utostr(numRegs);
|
|
|
|
regDefs += ">;\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// f64
|
|
|
|
numRegs = MFI->getNumRegistersForClass(PTX::RegF64RegisterClass);
|
|
|
|
if(numRegs > 0) {
|
|
|
|
regDefs += "\t.reg .f64 %fd<";
|
|
|
|
regDefs += utostr(numRegs);
|
|
|
|
regDefs += ">;\n";
|
|
|
|
}
|
|
|
|
|
2011-09-23 14:31:12 +00:00
|
|
|
// Local params
|
|
|
|
for (PTXParamManager::param_iterator i = PM.local_begin(), e = PM.local_end();
|
|
|
|
i != e; ++i) {
|
|
|
|
regDefs += "\t.param .b";
|
|
|
|
regDefs += utostr(PM.getParamSize(*i));
|
|
|
|
regDefs += " ";
|
|
|
|
regDefs += PM.getParamName(*i);
|
|
|
|
regDefs += ";\n";
|
|
|
|
}
|
|
|
|
|
2011-09-22 16:45:40 +00:00
|
|
|
OutStreamer.EmitRawText(Twine(regDefs));
|
|
|
|
|
|
|
|
|
2011-06-20 15:56:20 +00:00
|
|
|
const MachineFrameInfo* FrameInfo = MF->getFrameInfo();
|
2011-06-22 16:07:03 +00:00
|
|
|
DEBUG(dbgs() << "Have " << FrameInfo->getNumObjects()
|
|
|
|
<< " frame object(s)\n");
|
2011-06-20 15:56:20 +00:00
|
|
|
for (unsigned i = 0, e = FrameInfo->getNumObjects(); i != e; ++i) {
|
|
|
|
DEBUG(dbgs() << "Size of object: " << FrameInfo->getObjectSize(i) << "\n");
|
2011-06-22 16:07:03 +00:00
|
|
|
if (FrameInfo->getObjectSize(i) > 0) {
|
2011-09-26 16:20:34 +00:00
|
|
|
std::string def = "\t.local .align ";
|
|
|
|
def += utostr(FrameInfo->getObjectAlignment(i));
|
2011-09-26 18:57:22 +00:00
|
|
|
def += " .b8";
|
2011-09-26 16:20:28 +00:00
|
|
|
def += " __local";
|
2011-06-22 16:07:03 +00:00
|
|
|
def += utostr(i);
|
2011-09-26 18:57:22 +00:00
|
|
|
def += "[";
|
|
|
|
def += utostr(FrameInfo->getObjectSize(i)); // Convert to bits
|
|
|
|
def += "]";
|
2011-06-22 16:07:03 +00:00
|
|
|
def += ";";
|
|
|
|
OutStreamer.EmitRawText(Twine(def));
|
|
|
|
}
|
2011-06-20 15:56:20 +00:00
|
|
|
}
|
2011-08-09 17:36:31 +00:00
|
|
|
|
2011-09-22 16:45:46 +00:00
|
|
|
//unsigned Index = 1;
|
2011-08-09 17:36:31 +00:00
|
|
|
// Print parameter passing params
|
2011-09-22 16:45:46 +00:00
|
|
|
//for (PTXMachineFunctionInfo::param_iterator
|
|
|
|
// i = MFI->paramBegin(), e = MFI->paramEnd(); i != e; ++i) {
|
|
|
|
// std::string def = "\t.param .b";
|
|
|
|
// def += utostr(*i);
|
|
|
|
// def += " __ret_";
|
|
|
|
// def += utostr(Index);
|
|
|
|
// Index++;
|
|
|
|
// def += ";";
|
|
|
|
// OutStreamer.EmitRawText(Twine(def));
|
|
|
|
//}
|
2010-11-08 03:06:08 +00:00
|
|
|
}
|
|
|
|
|
2011-09-28 14:32:04 +00:00
|
|
|
void PTXAsmPrinter::EmitFunctionBodyEnd() {
|
|
|
|
OutStreamer.EmitRawText(Twine("}"));
|
|
|
|
}
|
|
|
|
|
2010-09-18 18:52:28 +00:00
|
|
|
void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) {
|
2011-09-28 14:32:04 +00:00
|
|
|
#if 0
|
2010-12-01 11:45:53 +00:00
|
|
|
std::string str;
|
|
|
|
str.reserve(64);
|
|
|
|
|
|
|
|
raw_string_ostream OS(str);
|
2011-03-13 17:26:00 +00:00
|
|
|
|
2011-06-24 19:19:18 +00:00
|
|
|
DebugLoc DL = MI->getDebugLoc();
|
|
|
|
if (!DL.isUnknown()) {
|
|
|
|
|
|
|
|
const MDNode *S = DL.getScope(MF->getFunction()->getContext());
|
|
|
|
|
|
|
|
// This is taken from DwarfDebug.cpp, which is conveniently not a public
|
|
|
|
// LLVM class.
|
|
|
|
StringRef Fn;
|
|
|
|
StringRef Dir;
|
|
|
|
unsigned Src = 1;
|
|
|
|
if (S) {
|
|
|
|
DIDescriptor Scope(S);
|
|
|
|
if (Scope.isCompileUnit()) {
|
|
|
|
DICompileUnit CU(S);
|
|
|
|
Fn = CU.getFilename();
|
|
|
|
Dir = CU.getDirectory();
|
|
|
|
} else if (Scope.isFile()) {
|
|
|
|
DIFile F(S);
|
|
|
|
Fn = F.getFilename();
|
|
|
|
Dir = F.getDirectory();
|
|
|
|
} else if (Scope.isSubprogram()) {
|
|
|
|
DISubprogram SP(S);
|
|
|
|
Fn = SP.getFilename();
|
|
|
|
Dir = SP.getDirectory();
|
|
|
|
} else if (Scope.isLexicalBlock()) {
|
|
|
|
DILexicalBlock DB(S);
|
|
|
|
Fn = DB.getFilename();
|
|
|
|
Dir = DB.getDirectory();
|
|
|
|
} else
|
|
|
|
assert(0 && "Unexpected scope info");
|
|
|
|
|
|
|
|
Src = GetOrCreateSourceID(Fn, Dir);
|
|
|
|
}
|
|
|
|
OutStreamer.EmitDwarfLocDirective(Src, DL.getLine(), DL.getCol(),
|
|
|
|
0, 0, 0, Fn);
|
|
|
|
|
|
|
|
const MCDwarfLoc& MDL = OutContext.getCurrentDwarfLoc();
|
|
|
|
|
|
|
|
OS << "\t.loc ";
|
|
|
|
OS << utostr(MDL.getFileNum());
|
|
|
|
OS << " ";
|
|
|
|
OS << utostr(MDL.getLine());
|
|
|
|
OS << " ";
|
|
|
|
OS << utostr(MDL.getColumn());
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-13 17:26:00 +00:00
|
|
|
// Emit predicate
|
|
|
|
printPredicateOperand(MI, OS);
|
|
|
|
|
|
|
|
// Write instruction to str
|
2011-08-09 17:36:31 +00:00
|
|
|
if (MI->getOpcode() == PTX::CALL) {
|
|
|
|
printCall(MI, OS);
|
|
|
|
} else {
|
|
|
|
printInstruction(MI, OS);
|
|
|
|
}
|
2010-10-19 13:14:40 +00:00
|
|
|
OS << ';';
|
2010-12-01 11:45:53 +00:00
|
|
|
OS.flush();
|
2010-11-17 08:08:49 +00:00
|
|
|
|
2010-12-01 11:45:53 +00:00
|
|
|
StringRef strref = StringRef(str);
|
2010-11-30 07:34:44 +00:00
|
|
|
OutStreamer.EmitRawText(strref);
|
2011-09-28 14:32:04 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
MCInst TmpInst;
|
|
|
|
LowerPTXMachineInstrToMCInst(MI, TmpInst, *this);
|
|
|
|
OutStreamer.EmitInstruction(TmpInst);
|
2010-10-19 13:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
|
|
|
|
raw_ostream &OS) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(opNum);
|
2011-09-22 16:45:40 +00:00
|
|
|
const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
|
2010-10-19 13:14:40 +00:00
|
|
|
|
|
|
|
switch (MO.getType()) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("<unknown operand type>");
|
|
|
|
break;
|
2010-12-22 10:38:51 +00:00
|
|
|
case MachineOperand::MO_GlobalAddress:
|
|
|
|
OS << *Mang->getSymbol(MO.getGlobal());
|
2010-10-19 13:14:40 +00:00
|
|
|
break;
|
|
|
|
case MachineOperand::MO_Immediate:
|
2011-04-28 00:19:50 +00:00
|
|
|
OS << (long) MO.getImm();
|
2010-10-19 13:14:40 +00:00
|
|
|
break;
|
2011-03-18 11:08:52 +00:00
|
|
|
case MachineOperand::MO_MachineBasicBlock:
|
|
|
|
OS << *MO.getMBB()->getSymbol();
|
|
|
|
break;
|
2010-12-22 10:38:51 +00:00
|
|
|
case MachineOperand::MO_Register:
|
2011-09-22 16:45:40 +00:00
|
|
|
OS << MFI->getRegisterName(MO.getReg());
|
2010-12-22 10:38:51 +00:00
|
|
|
break;
|
2011-09-27 18:12:55 +00:00
|
|
|
case MachineOperand::MO_ExternalSymbol:
|
|
|
|
OS << MO.getSymbolName();
|
|
|
|
break;
|
2011-02-28 06:34:09 +00:00
|
|
|
case MachineOperand::MO_FPImmediate:
|
|
|
|
APInt constFP = MO.getFPImm()->getValueAPF().bitcastToAPInt();
|
2011-03-02 03:20:28 +00:00
|
|
|
bool isFloat = MO.getFPImm()->getType()->getTypeID() == Type::FloatTyID;
|
|
|
|
// Emit 0F for 32-bit floats and 0D for 64-bit doubles.
|
|
|
|
if (isFloat) {
|
|
|
|
OS << "0F";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
OS << "0D";
|
|
|
|
}
|
|
|
|
// Emit the encoded floating-point value.
|
2011-02-28 06:34:09 +00:00
|
|
|
if (constFP.getZExtValue() > 0) {
|
2011-03-02 03:20:28 +00:00
|
|
|
OS << constFP.toString(16, false);
|
2011-02-28 06:34:09 +00:00
|
|
|
}
|
|
|
|
else {
|
2011-03-02 03:20:28 +00:00
|
|
|
OS << "00000000";
|
|
|
|
// If We have a double-precision zero, pad to 8-bytes.
|
|
|
|
if (!isFloat) {
|
|
|
|
OS << "00000000";
|
|
|
|
}
|
2011-02-28 06:34:09 +00:00
|
|
|
}
|
|
|
|
break;
|
2010-10-19 13:14:40 +00:00
|
|
|
}
|
2010-09-18 18:52:28 +00:00
|
|
|
}
|
|
|
|
|
2010-11-30 07:34:44 +00:00
|
|
|
void PTXAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
|
|
|
|
raw_ostream &OS, const char *Modifier) {
|
|
|
|
printOperand(MI, opNum, OS);
|
|
|
|
|
|
|
|
if (MI->getOperand(opNum+1).isImm() && MI->getOperand(opNum+1).getImm() == 0)
|
|
|
|
return; // don't print "+0"
|
|
|
|
|
|
|
|
OS << "+";
|
|
|
|
printOperand(MI, opNum+1, OS);
|
|
|
|
}
|
|
|
|
|
2011-06-23 18:10:05 +00:00
|
|
|
void PTXAsmPrinter::printReturnOperand(const MachineInstr *MI, int opNum,
|
|
|
|
raw_ostream &OS, const char *Modifier) {
|
2011-09-22 16:45:46 +00:00
|
|
|
//OS << RETURN_PREFIX << (int) MI->getOperand(opNum).getImm() + 1;
|
|
|
|
OS << "__ret";
|
2011-06-23 18:10:05 +00:00
|
|
|
}
|
|
|
|
|
2010-12-22 10:38:51 +00:00
|
|
|
void PTXAsmPrinter::EmitVariableDeclaration(const GlobalVariable *gv) {
|
|
|
|
// Check to see if this is a special global used by LLVM, if so, emit it.
|
|
|
|
if (EmitSpecialLLVMGlobal(gv))
|
|
|
|
return;
|
|
|
|
|
|
|
|
MCSymbol *gvsym = Mang->getSymbol(gv);
|
|
|
|
|
|
|
|
assert(gvsym->isUndefined() && "Cannot define a symbol twice!");
|
|
|
|
|
|
|
|
std::string decl;
|
|
|
|
|
|
|
|
// check if it is defined in some other translation unit
|
|
|
|
if (gv->isDeclaration())
|
|
|
|
decl += ".extern ";
|
|
|
|
|
|
|
|
// state space: e.g., .global
|
|
|
|
decl += ".";
|
|
|
|
decl += getStateSpaceName(gv->getType()->getAddressSpace());
|
|
|
|
decl += " ";
|
|
|
|
|
|
|
|
// alignment (optional)
|
|
|
|
unsigned alignment = gv->getAlignment();
|
|
|
|
if (alignment != 0) {
|
|
|
|
decl += ".align ";
|
2011-09-28 18:24:58 +00:00
|
|
|
decl += utostr(gv->getAlignment());
|
2010-12-22 10:38:51 +00:00
|
|
|
decl += " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-14 15:40:11 +00:00
|
|
|
if (PointerType::classof(gv->getType())) {
|
2011-07-18 04:54:35 +00:00
|
|
|
PointerType* pointerTy = dyn_cast<PointerType>(gv->getType());
|
|
|
|
Type* elementTy = pointerTy->getElementType();
|
2011-03-14 15:40:11 +00:00
|
|
|
|
2011-04-28 00:19:50 +00:00
|
|
|
decl += ".b8 ";
|
|
|
|
decl += gvsym->getName();
|
|
|
|
decl += "[";
|
2011-06-16 15:17:11 +00:00
|
|
|
|
2011-04-28 00:19:50 +00:00
|
|
|
if (elementTy->isArrayTy())
|
|
|
|
{
|
|
|
|
assert(elementTy->isArrayTy() && "Only pointers to arrays are supported");
|
2011-03-18 19:24:28 +00:00
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
ArrayType* arrayTy = dyn_cast<ArrayType>(elementTy);
|
2011-03-18 19:24:28 +00:00
|
|
|
elementTy = arrayTy->getElementType();
|
|
|
|
|
2011-04-28 00:19:50 +00:00
|
|
|
unsigned numElements = arrayTy->getNumElements();
|
2011-06-16 15:17:11 +00:00
|
|
|
|
2011-04-28 00:19:50 +00:00
|
|
|
while (elementTy->isArrayTy()) {
|
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
arrayTy = dyn_cast<ArrayType>(elementTy);
|
2011-04-28 00:19:50 +00:00
|
|
|
elementTy = arrayTy->getElementType();
|
2011-03-18 19:24:28 +00:00
|
|
|
|
2011-04-28 00:19:50 +00:00
|
|
|
numElements *= arrayTy->getNumElements();
|
|
|
|
}
|
2011-03-14 15:40:11 +00:00
|
|
|
|
2011-04-28 00:19:50 +00:00
|
|
|
// FIXME: isPrimitiveType() == false for i16?
|
|
|
|
assert(elementTy->isSingleValueType() &&
|
|
|
|
"Non-primitive types are not handled");
|
2010-12-22 10:38:51 +00:00
|
|
|
|
2011-04-28 00:19:50 +00:00
|
|
|
// Compute the size of the array, in bytes.
|
|
|
|
uint64_t arraySize = (elementTy->getPrimitiveSizeInBits() >> 3)
|
|
|
|
* numElements;
|
2011-06-16 15:17:11 +00:00
|
|
|
|
2011-04-28 00:19:50 +00:00
|
|
|
decl += utostr(arraySize);
|
|
|
|
}
|
2011-06-16 15:17:11 +00:00
|
|
|
|
2011-03-14 15:40:11 +00:00
|
|
|
decl += "]";
|
2011-06-16 15:17:11 +00:00
|
|
|
|
2011-04-28 00:19:50 +00:00
|
|
|
// handle string constants (assume ConstantArray means string)
|
2011-06-16 15:17:11 +00:00
|
|
|
|
2011-04-28 00:19:50 +00:00
|
|
|
if (gv->hasInitializer())
|
|
|
|
{
|
2011-09-22 16:45:40 +00:00
|
|
|
const Constant *C = gv->getInitializer();
|
2011-04-28 00:19:50 +00:00
|
|
|
if (const ConstantArray *CA = dyn_cast<ConstantArray>(C))
|
|
|
|
{
|
|
|
|
decl += " = {";
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
|
|
|
|
{
|
|
|
|
if (i > 0) decl += ",";
|
2011-06-16 15:17:11 +00:00
|
|
|
|
|
|
|
decl += "0x" +
|
|
|
|
utohexstr(cast<ConstantInt>(CA->getOperand(i))->getZExtValue());
|
2011-04-28 00:19:50 +00:00
|
|
|
}
|
2011-06-16 15:17:11 +00:00
|
|
|
|
2011-04-28 00:19:50 +00:00
|
|
|
decl += "}";
|
|
|
|
}
|
|
|
|
}
|
2011-03-14 15:40:11 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Note: this is currently the fall-through case and most likely generates
|
|
|
|
// incorrect code.
|
|
|
|
decl += getTypeName(gv->getType());
|
|
|
|
decl += " ";
|
|
|
|
|
|
|
|
decl += gvsym->getName();
|
|
|
|
|
|
|
|
if (ArrayType::classof(gv->getType()) ||
|
|
|
|
PointerType::classof(gv->getType()))
|
|
|
|
decl += "[]";
|
|
|
|
}
|
2010-12-22 10:38:51 +00:00
|
|
|
|
|
|
|
decl += ";";
|
|
|
|
|
|
|
|
OutStreamer.EmitRawText(Twine(decl));
|
|
|
|
|
|
|
|
OutStreamer.AddBlankLine();
|
|
|
|
}
|
|
|
|
|
2011-09-28 14:32:06 +00:00
|
|
|
void PTXAsmPrinter::EmitFunctionEntryLabel() {
|
2010-11-08 03:06:08 +00:00
|
|
|
// The function label could have already been emitted if two symbols end up
|
|
|
|
// conflicting due to asm renaming. Detect this and emit an error.
|
|
|
|
if (!CurrentFnSym->isUndefined()) {
|
|
|
|
report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
|
|
|
|
"' label emitted multiple times to assembly file");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
|
2011-09-23 14:18:22 +00:00
|
|
|
const PTXParamManager &PM = MFI->getParamManager();
|
2010-11-08 03:06:08 +00:00
|
|
|
const bool isKernel = MFI->isKernel();
|
2011-06-23 18:10:03 +00:00
|
|
|
const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>();
|
2011-09-22 16:45:46 +00:00
|
|
|
const MachineRegisterInfo& MRI = MF->getRegInfo();
|
2010-11-08 03:06:08 +00:00
|
|
|
|
|
|
|
std::string decl = isKernel ? ".entry" : ".func";
|
|
|
|
|
2011-06-16 17:50:00 +00:00
|
|
|
if (!isKernel) {
|
|
|
|
decl += " (";
|
2011-09-23 14:18:22 +00:00
|
|
|
if (ST.useParamSpaceForDeviceArgs()) {
|
|
|
|
for (PTXParamManager::param_iterator i = PM.ret_begin(), e = PM.ret_end(),
|
|
|
|
b = i; i != e; ++i) {
|
|
|
|
if (i != b) {
|
|
|
|
decl += ", ";
|
|
|
|
}
|
|
|
|
|
|
|
|
decl += ".param .b";
|
|
|
|
decl += utostr(PM.getParamSize(*i));
|
|
|
|
decl += " ";
|
|
|
|
decl += PM.getParamName(*i);
|
|
|
|
}
|
2011-09-22 16:45:46 +00:00
|
|
|
} else {
|
2011-09-23 17:15:53 +00:00
|
|
|
for (PTXMachineFunctionInfo::reg_iterator
|
|
|
|
i = MFI->retreg_begin(), e = MFI->retreg_end(), b = i;
|
2011-09-22 16:45:46 +00:00
|
|
|
i != e; ++i) {
|
|
|
|
if (i != b) {
|
|
|
|
decl += ", ";
|
|
|
|
}
|
|
|
|
decl += ".reg .";
|
|
|
|
decl += getRegisterTypeName(*i, MRI);
|
|
|
|
decl += " ";
|
|
|
|
decl += MFI->getRegisterName(*i);
|
2011-06-16 17:50:00 +00:00
|
|
|
}
|
|
|
|
}
|
2010-11-08 03:06:08 +00:00
|
|
|
decl += ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print function name
|
|
|
|
decl += " ";
|
|
|
|
decl += CurrentFnSym->getName().str();
|
|
|
|
|
2011-06-16 17:50:00 +00:00
|
|
|
decl += " (";
|
|
|
|
|
|
|
|
// Print parameters
|
2011-09-22 16:45:46 +00:00
|
|
|
if (isKernel || ST.useParamSpaceForDeviceArgs()) {
|
2011-09-23 14:18:22 +00:00
|
|
|
for (PTXParamManager::param_iterator i = PM.arg_begin(), e = PM.arg_end(),
|
|
|
|
b = i; i != e; ++i) {
|
2011-09-22 16:45:46 +00:00
|
|
|
if (i != b) {
|
|
|
|
decl += ", ";
|
|
|
|
}
|
|
|
|
|
2011-06-16 17:50:00 +00:00
|
|
|
decl += ".param .b";
|
2011-09-23 14:18:22 +00:00
|
|
|
decl += utostr(PM.getParamSize(*i));
|
2011-06-16 17:50:00 +00:00
|
|
|
decl += " ";
|
2011-09-23 14:18:22 +00:00
|
|
|
decl += PM.getParamName(*i);
|
2011-09-22 16:45:46 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (PTXMachineFunctionInfo::reg_iterator
|
2011-09-23 17:15:53 +00:00
|
|
|
i = MFI->argreg_begin(), e = MFI->argreg_end(), b = i;
|
2011-09-22 16:45:46 +00:00
|
|
|
i != e; ++i) {
|
|
|
|
if (i != b) {
|
|
|
|
decl += ", ";
|
|
|
|
}
|
|
|
|
|
2011-06-16 17:50:00 +00:00
|
|
|
decl += ".reg .";
|
2011-09-22 16:45:46 +00:00
|
|
|
decl += getRegisterTypeName(*i, MRI);
|
2011-06-16 17:50:00 +00:00
|
|
|
decl += " ";
|
2011-09-22 16:45:46 +00:00
|
|
|
decl += MFI->getRegisterName(*i);
|
2010-11-08 03:06:08 +00:00
|
|
|
}
|
|
|
|
}
|
2011-06-16 17:50:00 +00:00
|
|
|
decl += ")";
|
|
|
|
|
2010-11-08 03:06:08 +00:00
|
|
|
OutStreamer.EmitRawText(Twine(decl));
|
|
|
|
}
|
|
|
|
|
2011-03-13 17:26:00 +00:00
|
|
|
void PTXAsmPrinter::
|
|
|
|
printPredicateOperand(const MachineInstr *MI, raw_ostream &O) {
|
|
|
|
int i = MI->findFirstPredOperandIdx();
|
|
|
|
if (i == -1)
|
|
|
|
llvm_unreachable("missing predicate operand");
|
|
|
|
|
|
|
|
unsigned reg = MI->getOperand(i).getReg();
|
|
|
|
int predOp = MI->getOperand(i+1).getImm();
|
2011-09-22 16:45:40 +00:00
|
|
|
const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
|
2011-03-13 17:26:00 +00:00
|
|
|
|
|
|
|
DEBUG(dbgs() << "predicate: (" << reg << ", " << predOp << ")\n");
|
|
|
|
|
2011-03-14 11:26:01 +00:00
|
|
|
if (reg != PTX::NoRegister) {
|
2011-03-13 17:26:00 +00:00
|
|
|
O << '@';
|
|
|
|
if (predOp == PTX::PRED_NEGATE)
|
|
|
|
O << '!';
|
2011-09-22 16:45:40 +00:00
|
|
|
O << MFI->getRegisterName(reg);
|
2011-03-13 17:26:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-09 17:36:31 +00:00
|
|
|
void PTXAsmPrinter::
|
|
|
|
printCall(const MachineInstr *MI, raw_ostream &O) {
|
|
|
|
O << "\tcall.uni\t";
|
2011-09-23 16:48:41 +00:00
|
|
|
// The first two operands are the predicate slot
|
|
|
|
unsigned Index = 2;
|
|
|
|
while (!MI->getOperand(Index).isGlobal()) {
|
|
|
|
if (Index == 2) {
|
|
|
|
O << "(";
|
|
|
|
} else {
|
|
|
|
O << ", ";
|
|
|
|
}
|
2011-09-27 18:12:55 +00:00
|
|
|
printOperand(MI, Index, O);
|
2011-09-23 16:48:41 +00:00
|
|
|
Index++;
|
|
|
|
}
|
2011-08-09 17:36:31 +00:00
|
|
|
|
2011-09-23 16:48:41 +00:00
|
|
|
if (Index != 2) {
|
|
|
|
O << "), ";
|
|
|
|
}
|
2011-08-09 17:36:31 +00:00
|
|
|
|
2011-09-23 16:48:41 +00:00
|
|
|
assert(MI->getOperand(Index).isGlobal() &&
|
|
|
|
"A GlobalAddress must follow the return arguments");
|
|
|
|
|
|
|
|
const GlobalValue *Address = MI->getOperand(Index).getGlobal();
|
|
|
|
O << Address->getName() << ", (";
|
|
|
|
Index++;
|
2011-08-09 17:36:31 +00:00
|
|
|
|
2011-09-23 16:48:41 +00:00
|
|
|
while (Index < MI->getNumOperands()) {
|
2011-09-27 18:12:55 +00:00
|
|
|
printOperand(MI, Index, O);
|
2011-09-23 16:48:41 +00:00
|
|
|
if (Index < MI->getNumOperands()-1) {
|
2011-08-09 17:36:31 +00:00
|
|
|
O << ", ";
|
|
|
|
}
|
2011-09-23 16:48:41 +00:00
|
|
|
Index++;
|
2011-08-09 17:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
O << ")";
|
|
|
|
}
|
|
|
|
|
2011-06-24 19:19:18 +00:00
|
|
|
unsigned PTXAsmPrinter::GetOrCreateSourceID(StringRef FileName,
|
|
|
|
StringRef DirName) {
|
|
|
|
// If FE did not provide a file name, then assume stdin.
|
|
|
|
if (FileName.empty())
|
|
|
|
return GetOrCreateSourceID("<stdin>", StringRef());
|
|
|
|
|
|
|
|
// MCStream expects full path name as filename.
|
|
|
|
if (!DirName.empty() && !sys::path::is_absolute(FileName)) {
|
|
|
|
SmallString<128> FullPathName = DirName;
|
|
|
|
sys::path::append(FullPathName, FileName);
|
|
|
|
// Here FullPathName will be copied into StringMap by GetOrCreateSourceID.
|
|
|
|
return GetOrCreateSourceID(StringRef(FullPathName), StringRef());
|
|
|
|
}
|
|
|
|
|
|
|
|
StringMapEntry<unsigned> &Entry = SourceIdMap.GetOrCreateValue(FileName);
|
|
|
|
if (Entry.getValue())
|
|
|
|
return Entry.getValue();
|
|
|
|
|
|
|
|
unsigned SrcId = SourceIdMap.size();
|
|
|
|
Entry.setValue(SrcId);
|
|
|
|
|
|
|
|
// Print out a .file directive to specify files for .loc directives.
|
|
|
|
OutStreamer.EmitDwarfFileDirective(SrcId, Entry.getKey());
|
|
|
|
|
|
|
|
return SrcId;
|
|
|
|
}
|
|
|
|
|
2011-09-28 14:32:04 +00:00
|
|
|
MCOperand PTXAsmPrinter::GetSymbolRef(const MachineOperand &MO,
|
|
|
|
const MCSymbol *Symbol) {
|
|
|
|
const MCExpr *Expr;
|
|
|
|
Expr = MCSymbolRefExpr::Create(Symbol, MCSymbolRefExpr::VK_None, OutContext);
|
|
|
|
return MCOperand::CreateExpr(Expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PTXAsmPrinter::lowerOperand(const MachineOperand &MO, MCOperand &MCOp) {
|
|
|
|
const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
|
|
|
|
const MCExpr *Expr;
|
|
|
|
const char *RegSymbolName;
|
|
|
|
switch (MO.getType()) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unknown operand type");
|
|
|
|
case MachineOperand::MO_Register:
|
|
|
|
// We create register operands as symbols, since the PTXInstPrinter class
|
|
|
|
// has no way to map virtual registers back to a name without some ugly
|
|
|
|
// hacks.
|
|
|
|
// FIXME: Figure out a better way to handle virtual register naming.
|
|
|
|
RegSymbolName = MFI->getRegisterName(MO.getReg());
|
|
|
|
Expr = MCSymbolRefExpr::Create(RegSymbolName, MCSymbolRefExpr::VK_None,
|
|
|
|
OutContext);
|
|
|
|
MCOp = MCOperand::CreateExpr(Expr);
|
|
|
|
break;
|
|
|
|
case MachineOperand::MO_Immediate:
|
|
|
|
MCOp = MCOperand::CreateImm(MO.getImm());
|
|
|
|
break;
|
|
|
|
case MachineOperand::MO_MachineBasicBlock:
|
|
|
|
MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
|
|
|
|
MO.getMBB()->getSymbol(), OutContext));
|
|
|
|
break;
|
|
|
|
case MachineOperand::MO_GlobalAddress:
|
|
|
|
MCOp = GetSymbolRef(MO, Mang->getSymbol(MO.getGlobal()));
|
|
|
|
break;
|
|
|
|
case MachineOperand::MO_ExternalSymbol:
|
|
|
|
MCOp = GetSymbolRef(MO, GetExternalSymbolSymbol(MO.getSymbolName()));
|
|
|
|
break;
|
|
|
|
case MachineOperand::MO_FPImmediate:
|
|
|
|
APFloat Val = MO.getFPImm()->getValueAPF();
|
|
|
|
bool ignored;
|
|
|
|
Val.convert(APFloat::IEEEdouble, APFloat::rmTowardZero, &ignored);
|
|
|
|
MCOp = MCOperand::CreateFPImm(Val.convertToDouble());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2010-09-18 18:52:28 +00:00
|
|
|
|
2010-09-07 18:14:24 +00:00
|
|
|
// Force static initialization.
|
2010-09-18 18:52:28 +00:00
|
|
|
extern "C" void LLVMInitializePTXAsmPrinter() {
|
2011-04-20 15:37:17 +00:00
|
|
|
RegisterAsmPrinter<PTXAsmPrinter> X(ThePTX32Target);
|
|
|
|
RegisterAsmPrinter<PTXAsmPrinter> Y(ThePTX64Target);
|
2010-09-07 18:14:24 +00:00
|
|
|
}
|
2011-09-28 14:32:04 +00:00
|
|
|
|