2012-05-04 20:18:50 +00:00
|
|
|
//===-- NVPTXAsmPrinter.h - NVPTX 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 NVPTX assembly language.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef NVPTXASMPRINTER_H
|
|
|
|
#define NVPTXASMPRINTER_H
|
|
|
|
|
|
|
|
#include "NVPTX.h"
|
|
|
|
#include "NVPTXSubtarget.h"
|
2012-12-04 07:12:27 +00:00
|
|
|
#include "NVPTXTargetMachine.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2012-05-04 20:18:50 +00:00
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Function.h"
|
2012-05-04 20:18:50 +00:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
|
|
#include "llvm/MC/MCExpr.h"
|
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2012-12-04 07:12:27 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2012-05-04 20:18:50 +00:00
|
|
|
#include "llvm/Support/FormattedStream.h"
|
|
|
|
#include "llvm/Target/Mangler.h"
|
2012-12-04 07:12:27 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2012-05-04 20:18:50 +00:00
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
// The ptx syntax and format is very different from that usually seem in a .s
|
|
|
|
// file,
|
|
|
|
// therefore we are not able to use the MCAsmStreamer interface here.
|
|
|
|
//
|
|
|
|
// We are handcrafting the output method here.
|
|
|
|
//
|
|
|
|
// A better approach is to clone the MCAsmStreamer to a MCPTXAsmStreamer
|
|
|
|
// (subclass of MCStreamer).
|
|
|
|
|
|
|
|
// This is defined in AsmPrinter.cpp.
|
|
|
|
// Used to process the constant expressions in initializers.
|
|
|
|
namespace nvptx {
|
2013-03-30 14:29:21 +00:00
|
|
|
const llvm::MCExpr *
|
|
|
|
LowerConstant(const llvm::Constant *CV, llvm::AsmPrinter &AP);
|
2012-05-04 20:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
class LineReader {
|
|
|
|
private:
|
2013-03-30 14:29:21 +00:00
|
|
|
unsigned theCurLine;
|
2012-05-04 20:18:50 +00:00
|
|
|
std::ifstream fstr;
|
|
|
|
char buff[512];
|
|
|
|
std::string theFileName;
|
|
|
|
SmallVector<unsigned, 32> lineOffset;
|
|
|
|
public:
|
|
|
|
LineReader(std::string filename) {
|
|
|
|
theCurLine = 0;
|
|
|
|
fstr.open(filename.c_str());
|
|
|
|
theFileName = filename;
|
|
|
|
}
|
|
|
|
std::string fileName() { return theFileName; }
|
2013-03-30 14:29:21 +00:00
|
|
|
~LineReader() { fstr.close(); }
|
2012-05-04 20:18:50 +00:00
|
|
|
std::string readLine(unsigned line);
|
|
|
|
};
|
|
|
|
|
|
|
|
class LLVM_LIBRARY_VISIBILITY NVPTXAsmPrinter : public AsmPrinter {
|
|
|
|
|
|
|
|
class AggBuffer {
|
|
|
|
// Used to buffer the emitted string for initializing global
|
|
|
|
// aggregates.
|
|
|
|
//
|
|
|
|
// Normally an aggregate (array, vector or structure) is emitted
|
|
|
|
// as a u8[]. However, if one element/field of the aggregate
|
|
|
|
// is a non-NULL address, then the aggregate is emitted as u32[]
|
|
|
|
// or u64[].
|
|
|
|
//
|
|
|
|
// We first layout the aggregate in 'buffer' in bytes, except for
|
|
|
|
// those symbol addresses. For the i-th symbol address in the
|
|
|
|
//aggregate, its corresponding 4-byte or 8-byte elements in 'buffer'
|
|
|
|
// are filled with 0s. symbolPosInBuffer[i-1] records its position
|
|
|
|
// in 'buffer', and Symbols[i-1] records the Value*.
|
|
|
|
//
|
|
|
|
// Once we have this AggBuffer setup, we can choose how to print
|
|
|
|
// it out.
|
|
|
|
public:
|
2013-03-30 14:29:21 +00:00
|
|
|
unsigned size; // size of the buffer in bytes
|
2012-05-04 20:18:50 +00:00
|
|
|
unsigned char *buffer; // the buffer
|
|
|
|
unsigned numSymbols; // number of symbol addresses
|
|
|
|
SmallVector<unsigned, 4> symbolPosInBuffer;
|
2013-05-20 12:13:32 +00:00
|
|
|
SmallVector<const Value *, 4> Symbols;
|
2012-05-04 20:18:50 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
unsigned curpos;
|
|
|
|
raw_ostream &O;
|
|
|
|
NVPTXAsmPrinter &AP;
|
|
|
|
|
|
|
|
public:
|
|
|
|
AggBuffer(unsigned _size, raw_ostream &_O, NVPTXAsmPrinter &_AP)
|
2013-03-30 14:29:21 +00:00
|
|
|
: O(_O), AP(_AP) {
|
2012-05-04 20:18:50 +00:00
|
|
|
buffer = new unsigned char[_size];
|
|
|
|
size = _size;
|
|
|
|
curpos = 0;
|
|
|
|
numSymbols = 0;
|
|
|
|
}
|
2013-03-30 14:29:21 +00:00
|
|
|
~AggBuffer() { delete[] buffer; }
|
2012-05-04 20:18:50 +00:00
|
|
|
unsigned addBytes(unsigned char *Ptr, int Num, int Bytes) {
|
2013-03-30 14:29:21 +00:00
|
|
|
assert((curpos + Num) <= size);
|
|
|
|
assert((curpos + Bytes) <= size);
|
|
|
|
for (int i = 0; i < Num; ++i) {
|
2012-05-04 20:18:50 +00:00
|
|
|
buffer[curpos] = Ptr[i];
|
2013-03-30 14:29:21 +00:00
|
|
|
curpos++;
|
2012-05-04 20:18:50 +00:00
|
|
|
}
|
2013-03-30 14:29:21 +00:00
|
|
|
for (int i = Num; i < Bytes; ++i) {
|
2012-05-04 20:18:50 +00:00
|
|
|
buffer[curpos] = 0;
|
2013-03-30 14:29:21 +00:00
|
|
|
curpos++;
|
2012-05-04 20:18:50 +00:00
|
|
|
}
|
|
|
|
return curpos;
|
|
|
|
}
|
|
|
|
unsigned addZeros(int Num) {
|
2013-03-30 14:29:21 +00:00
|
|
|
assert((curpos + Num) <= size);
|
|
|
|
for (int i = 0; i < Num; ++i) {
|
2012-05-04 20:18:50 +00:00
|
|
|
buffer[curpos] = 0;
|
2013-03-30 14:29:21 +00:00
|
|
|
curpos++;
|
2012-05-04 20:18:50 +00:00
|
|
|
}
|
|
|
|
return curpos;
|
|
|
|
}
|
2013-05-20 12:13:32 +00:00
|
|
|
void addSymbol(const Value *GVar) {
|
2012-05-04 20:18:50 +00:00
|
|
|
symbolPosInBuffer.push_back(curpos);
|
|
|
|
Symbols.push_back(GVar);
|
|
|
|
numSymbols++;
|
|
|
|
}
|
|
|
|
void print() {
|
|
|
|
if (numSymbols == 0) {
|
|
|
|
// print out in bytes
|
2013-03-30 14:29:21 +00:00
|
|
|
for (unsigned i = 0; i < size; i++) {
|
2012-05-04 20:18:50 +00:00
|
|
|
if (i)
|
|
|
|
O << ", ";
|
2013-03-30 14:29:21 +00:00
|
|
|
O << (unsigned int) buffer[i];
|
2012-05-04 20:18:50 +00:00
|
|
|
}
|
2012-05-24 07:02:50 +00:00
|
|
|
} else {
|
2012-05-04 20:18:50 +00:00
|
|
|
// print out in 4-bytes or 8-bytes
|
|
|
|
unsigned int pos = 0;
|
|
|
|
unsigned int nSym = 0;
|
|
|
|
unsigned int nextSymbolPos = symbolPosInBuffer[nSym];
|
|
|
|
unsigned int nBytes = 4;
|
|
|
|
if (AP.nvptxSubtarget.is64Bit())
|
|
|
|
nBytes = 8;
|
2013-03-30 14:29:21 +00:00
|
|
|
for (pos = 0; pos < size; pos += nBytes) {
|
2012-05-04 20:18:50 +00:00
|
|
|
if (pos)
|
|
|
|
O << ", ";
|
|
|
|
if (pos == nextSymbolPos) {
|
2013-05-20 12:13:32 +00:00
|
|
|
const Value *v = Symbols[nSym];
|
|
|
|
if (const GlobalValue *GVar = dyn_cast<GlobalValue>(v)) {
|
2013-10-29 17:07:16 +00:00
|
|
|
MCSymbol *Name = AP.getSymbol(GVar);
|
2012-05-04 20:18:50 +00:00
|
|
|
O << *Name;
|
2013-05-20 12:13:32 +00:00
|
|
|
} else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(v)) {
|
2012-05-04 20:18:50 +00:00
|
|
|
O << *nvptx::LowerConstant(Cexpr, AP);
|
2012-05-24 07:02:50 +00:00
|
|
|
} else
|
|
|
|
llvm_unreachable("symbol type unknown");
|
2012-05-04 20:18:50 +00:00
|
|
|
nSym++;
|
|
|
|
if (nSym >= numSymbols)
|
2013-03-30 14:29:21 +00:00
|
|
|
nextSymbolPos = size + 1;
|
2012-05-04 20:18:50 +00:00
|
|
|
else
|
|
|
|
nextSymbolPos = symbolPosInBuffer[nSym];
|
2013-03-30 14:29:21 +00:00
|
|
|
} else if (nBytes == 4)
|
|
|
|
O << *(unsigned int *)(buffer + pos);
|
|
|
|
else
|
|
|
|
O << *(unsigned long long *)(buffer + pos);
|
2012-05-04 20:18:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
friend class AggBuffer;
|
|
|
|
|
|
|
|
virtual void emitSrcInText(StringRef filename, unsigned line);
|
|
|
|
|
2013-03-30 14:29:21 +00:00
|
|
|
private:
|
|
|
|
virtual const char *getPassName() const { return "NVPTX Assembly Printer"; }
|
2012-05-04 20:18:50 +00:00
|
|
|
|
|
|
|
const Function *F;
|
|
|
|
std::string CurrentFnName;
|
|
|
|
|
|
|
|
void EmitFunctionEntryLabel();
|
|
|
|
void EmitFunctionBodyStart();
|
|
|
|
void EmitFunctionBodyEnd();
|
2013-10-11 12:39:36 +00:00
|
|
|
void emitImplicitDef(const MachineInstr *MI) const;
|
2012-05-04 20:18:50 +00:00
|
|
|
|
|
|
|
void EmitInstruction(const MachineInstr *);
|
2013-08-06 14:13:27 +00:00
|
|
|
void lowerToMCInst(const MachineInstr *MI, MCInst &OutMI);
|
|
|
|
bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp);
|
|
|
|
MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol);
|
|
|
|
unsigned encodeVirtualRegister(unsigned Reg);
|
2012-05-04 20:18:50 +00:00
|
|
|
|
|
|
|
void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const {}
|
|
|
|
|
|
|
|
void printGlobalVariable(const GlobalVariable *GVar);
|
2013-03-30 14:29:21 +00:00
|
|
|
void printVecModifiedImmediate(const MachineOperand &MO, const char *Modifier,
|
|
|
|
raw_ostream &O);
|
2012-05-04 20:18:50 +00:00
|
|
|
void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
|
2013-03-30 14:29:21 +00:00
|
|
|
const char *Modifier = 0);
|
2012-05-04 20:18:50 +00:00
|
|
|
void printImplicitDef(const MachineInstr *MI, raw_ostream &O) const;
|
2013-05-20 12:13:32 +00:00
|
|
|
void printModuleLevelGV(const GlobalVariable *GVar, raw_ostream &O,
|
|
|
|
bool = false);
|
2012-05-04 20:18:50 +00:00
|
|
|
void printParamName(int paramIndex, raw_ostream &O);
|
|
|
|
void printParamName(Function::const_arg_iterator I, int paramIndex,
|
|
|
|
raw_ostream &O);
|
2013-05-20 12:13:32 +00:00
|
|
|
void emitGlobals(const Module &M);
|
2012-05-04 20:18:50 +00:00
|
|
|
void emitHeader(Module &M, raw_ostream &O);
|
2013-03-30 14:29:21 +00:00
|
|
|
void emitKernelFunctionDirectives(const Function &F, raw_ostream &O) const;
|
2013-10-11 12:39:36 +00:00
|
|
|
void emitVirtualRegister(unsigned int vr, raw_ostream &);
|
2012-05-04 20:18:50 +00:00
|
|
|
void emitFunctionExternParamList(const MachineFunction &MF);
|
|
|
|
void emitFunctionParamList(const Function *, raw_ostream &O);
|
|
|
|
void emitFunctionParamList(const MachineFunction &MF, raw_ostream &O);
|
|
|
|
void setAndEmitFunctionVirtualRegisters(const MachineFunction &MF);
|
2013-03-30 14:29:21 +00:00
|
|
|
void emitFunctionTempData(const MachineFunction &MF, unsigned &FrameSize);
|
2012-05-04 20:18:50 +00:00
|
|
|
bool isImageType(const Type *Ty);
|
|
|
|
void printReturnValStr(const Function *, raw_ostream &O);
|
|
|
|
void printReturnValStr(const MachineFunction &MF, raw_ostream &O);
|
2013-08-24 01:17:23 +00:00
|
|
|
bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
|
|
|
|
unsigned AsmVariant, const char *ExtraCode,
|
|
|
|
raw_ostream &);
|
|
|
|
void printOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
|
|
|
|
const char *Modifier = 0);
|
|
|
|
bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
|
|
|
|
unsigned AsmVariant, const char *ExtraCode,
|
|
|
|
raw_ostream &);
|
2012-05-04 20:18:50 +00:00
|
|
|
protected:
|
|
|
|
bool doInitialization(Module &M);
|
|
|
|
bool doFinalization(Module &M);
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string CurrentBankselLabelInBasicBlock;
|
|
|
|
|
2013-05-20 12:13:32 +00:00
|
|
|
bool GlobalsEmitted;
|
|
|
|
|
2012-05-04 20:18:50 +00:00
|
|
|
// This is specific per MachineFunction.
|
|
|
|
const MachineRegisterInfo *MRI;
|
|
|
|
// The contents are specific for each
|
|
|
|
// MachineFunction. But the size of the
|
|
|
|
// array is not.
|
2013-05-31 12:14:49 +00:00
|
|
|
typedef DenseMap<unsigned, unsigned> VRegMap;
|
|
|
|
typedef DenseMap<const TargetRegisterClass *, VRegMap> VRegRCMap;
|
|
|
|
VRegRCMap VRegMapping;
|
2012-05-04 20:18:50 +00:00
|
|
|
// cache the subtarget here.
|
|
|
|
const NVPTXSubtarget &nvptxSubtarget;
|
|
|
|
// Build the map between type name and ID based on module's type
|
|
|
|
// symbol table.
|
|
|
|
std::map<const Type *, std::string> TypeNameMap;
|
|
|
|
|
|
|
|
// List of variables demoted to a function scope.
|
2013-05-20 12:13:32 +00:00
|
|
|
std::map<const Function *, std::vector<const GlobalVariable *> > localDecls;
|
2012-05-04 20:18:50 +00:00
|
|
|
|
|
|
|
// To record filename to ID mapping
|
|
|
|
std::map<std::string, unsigned> filenameMap;
|
|
|
|
void recordAndEmitFilenames(Module &);
|
|
|
|
|
|
|
|
void emitPTXGlobalVariable(const GlobalVariable *GVar, raw_ostream &O);
|
2013-03-30 14:29:21 +00:00
|
|
|
void emitPTXAddressSpace(unsigned int AddressSpace, raw_ostream &O) const;
|
|
|
|
std::string getPTXFundamentalTypeStr(const Type *Ty, bool = true) const;
|
2013-05-20 12:13:32 +00:00
|
|
|
void printScalarConstant(const Constant *CPV, raw_ostream &O);
|
2013-03-30 14:29:21 +00:00
|
|
|
void printFPConstant(const ConstantFP *Fp, raw_ostream &O);
|
2013-05-20 12:13:32 +00:00
|
|
|
void bufferLEByte(const Constant *CPV, int Bytes, AggBuffer *aggBuffer);
|
|
|
|
void bufferAggregateConstant(const Constant *CV, AggBuffer *aggBuffer);
|
2012-05-04 20:18:50 +00:00
|
|
|
|
|
|
|
void printOperandProper(const MachineOperand &MO);
|
|
|
|
|
2013-03-30 14:29:21 +00:00
|
|
|
void emitLinkageDirective(const GlobalValue *V, raw_ostream &O);
|
2013-05-20 12:13:32 +00:00
|
|
|
void emitDeclarations(const Module &, raw_ostream &O);
|
2012-05-04 20:18:50 +00:00
|
|
|
void emitDeclaration(const Function *, raw_ostream &O);
|
|
|
|
|
|
|
|
static const char *getRegisterName(unsigned RegNo);
|
|
|
|
void emitDemotedVars(const Function *, raw_ostream &);
|
|
|
|
|
|
|
|
LineReader *reader;
|
|
|
|
LineReader *getReader(std::string);
|
|
|
|
public:
|
2013-03-30 14:29:21 +00:00
|
|
|
NVPTXAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
|
|
|
|
: AsmPrinter(TM, Streamer),
|
|
|
|
nvptxSubtarget(TM.getSubtarget<NVPTXSubtarget>()) {
|
2012-05-04 20:18:50 +00:00
|
|
|
CurrentBankselLabelInBasicBlock = "";
|
|
|
|
reader = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
~NVPTXAsmPrinter() {
|
|
|
|
if (!reader)
|
|
|
|
delete reader;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ignoreLoc(const MachineInstr &);
|
|
|
|
|
2013-10-11 12:39:36 +00:00
|
|
|
std::string getVirtualRegisterName(unsigned) const;
|
2012-05-04 20:18:50 +00:00
|
|
|
|
|
|
|
DebugLoc prevDebugLoc;
|
|
|
|
void emitLineNumberAsDotLoc(const MachineInstr &);
|
|
|
|
};
|
|
|
|
} // end of namespace
|
|
|
|
|
|
|
|
#endif
|