1 //===-- NVPTXAsmPrinter.h - NVPTX LLVM assembly writer --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to NVPTX assembly language.
13 //===----------------------------------------------------------------------===//
15 #ifndef NVPTXASMPRINTER_H
16 #define NVPTXASMPRINTER_H
19 #include "NVPTXSubtarget.h"
20 #include "NVPTXTargetMachine.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/CodeGen/AsmPrinter.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/MC/MCAsmInfo.h"
26 #include "llvm/MC/MCExpr.h"
27 #include "llvm/MC/MCSymbol.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/FormattedStream.h"
30 #include "llvm/Target/Mangler.h"
31 #include "llvm/Target/TargetMachine.h"
34 // The ptx syntax and format is very different from that usually seem in a .s
36 // therefore we are not able to use the MCAsmStreamer interface here.
38 // We are handcrafting the output method here.
40 // A better approach is to clone the MCAsmStreamer to a MCPTXAsmStreamer
41 // (subclass of MCStreamer).
43 // This is defined in AsmPrinter.cpp.
44 // Used to process the constant expressions in initializers.
47 LowerConstant(const llvm::Constant *CV, llvm::AsmPrinter &AP);
57 std::string theFileName;
58 SmallVector<unsigned, 32> lineOffset;
60 LineReader(std::string filename) {
62 fstr.open(filename.c_str());
63 theFileName = filename;
65 std::string fileName() { return theFileName; }
66 ~LineReader() { fstr.close(); }
67 std::string readLine(unsigned line);
70 class LLVM_LIBRARY_VISIBILITY NVPTXAsmPrinter : public AsmPrinter {
73 // Used to buffer the emitted string for initializing global
76 // Normally an aggregate (array, vector or structure) is emitted
77 // as a u8[]. However, if one element/field of the aggregate
78 // is a non-NULL address, then the aggregate is emitted as u32[]
81 // We first layout the aggregate in 'buffer' in bytes, except for
82 // those symbol addresses. For the i-th symbol address in the
83 //aggregate, its corresponding 4-byte or 8-byte elements in 'buffer'
84 // are filled with 0s. symbolPosInBuffer[i-1] records its position
85 // in 'buffer', and Symbols[i-1] records the Value*.
87 // Once we have this AggBuffer setup, we can choose how to print
90 unsigned size; // size of the buffer in bytes
91 unsigned char *buffer; // the buffer
92 unsigned numSymbols; // number of symbol addresses
93 SmallVector<unsigned, 4> symbolPosInBuffer;
94 SmallVector<const Value *, 4> Symbols;
102 AggBuffer(unsigned _size, raw_ostream &_O, NVPTXAsmPrinter &_AP)
104 buffer = new unsigned char[_size];
109 ~AggBuffer() { delete[] buffer; }
110 unsigned addBytes(unsigned char *Ptr, int Num, int Bytes) {
111 assert((curpos + Num) <= size);
112 assert((curpos + Bytes) <= size);
113 for (int i = 0; i < Num; ++i) {
114 buffer[curpos] = Ptr[i];
117 for (int i = Num; i < Bytes; ++i) {
123 unsigned addZeros(int Num) {
124 assert((curpos + Num) <= size);
125 for (int i = 0; i < Num; ++i) {
131 void addSymbol(const Value *GVar) {
132 symbolPosInBuffer.push_back(curpos);
133 Symbols.push_back(GVar);
137 if (numSymbols == 0) {
138 // print out in bytes
139 for (unsigned i = 0; i < size; i++) {
142 O << (unsigned int) buffer[i];
145 // print out in 4-bytes or 8-bytes
146 unsigned int pos = 0;
147 unsigned int nSym = 0;
148 unsigned int nextSymbolPos = symbolPosInBuffer[nSym];
149 unsigned int nBytes = 4;
150 if (AP.nvptxSubtarget.is64Bit())
152 for (pos = 0; pos < size; pos += nBytes) {
155 if (pos == nextSymbolPos) {
156 const Value *v = Symbols[nSym];
157 if (const GlobalValue *GVar = dyn_cast<GlobalValue>(v)) {
158 MCSymbol *Name = AP.Mang->getSymbol(GVar);
160 } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(v)) {
161 O << *nvptx::LowerConstant(Cexpr, AP);
163 llvm_unreachable("symbol type unknown");
165 if (nSym >= numSymbols)
166 nextSymbolPos = size + 1;
168 nextSymbolPos = symbolPosInBuffer[nSym];
169 } else if (nBytes == 4)
170 O << *(unsigned int *)(buffer + pos);
172 O << *(unsigned long long *)(buffer + pos);
178 friend class AggBuffer;
180 virtual void emitSrcInText(StringRef filename, unsigned line);
183 virtual const char *getPassName() const { return "NVPTX Assembly Printer"; }
186 std::string CurrentFnName;
188 void EmitFunctionEntryLabel();
189 void EmitFunctionBodyStart();
190 void EmitFunctionBodyEnd();
192 void EmitInstruction(const MachineInstr *);
193 void lowerToMCInst(const MachineInstr *MI, MCInst &OutMI);
194 bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp);
195 MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol);
196 unsigned encodeVirtualRegister(unsigned Reg);
198 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const {}
200 void printGlobalVariable(const GlobalVariable *GVar);
201 void printVecModifiedImmediate(const MachineOperand &MO, const char *Modifier,
203 void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
204 const char *Modifier = 0);
205 void printImplicitDef(const MachineInstr *MI, raw_ostream &O) const;
206 // definition autogenerated.
207 void printInstruction(const MachineInstr *MI, raw_ostream &O);
208 void printModuleLevelGV(const GlobalVariable *GVar, raw_ostream &O,
210 void printParamName(int paramIndex, raw_ostream &O);
211 void printParamName(Function::const_arg_iterator I, int paramIndex,
213 void emitGlobals(const Module &M);
214 void emitHeader(Module &M, raw_ostream &O);
215 void emitKernelFunctionDirectives(const Function &F, raw_ostream &O) const;
216 void emitVirtualRegister(unsigned int vr, bool isVec, raw_ostream &O);
217 void emitFunctionExternParamList(const MachineFunction &MF);
218 void emitFunctionParamList(const Function *, raw_ostream &O);
219 void emitFunctionParamList(const MachineFunction &MF, raw_ostream &O);
220 void setAndEmitFunctionVirtualRegisters(const MachineFunction &MF);
221 void emitFunctionTempData(const MachineFunction &MF, unsigned &FrameSize);
222 bool isImageType(const Type *Ty);
223 void printReturnValStr(const Function *, raw_ostream &O);
224 void printReturnValStr(const MachineFunction &MF, raw_ostream &O);
227 bool doInitialization(Module &M);
228 bool doFinalization(Module &M);
231 std::string CurrentBankselLabelInBasicBlock;
235 // This is specific per MachineFunction.
236 const MachineRegisterInfo *MRI;
237 // The contents are specific for each
238 // MachineFunction. But the size of the
240 typedef DenseMap<unsigned, unsigned> VRegMap;
241 typedef DenseMap<const TargetRegisterClass *, VRegMap> VRegRCMap;
242 VRegRCMap VRegMapping;
243 // cache the subtarget here.
244 const NVPTXSubtarget &nvptxSubtarget;
245 // Build the map between type name and ID based on module's type
247 std::map<const Type *, std::string> TypeNameMap;
249 // List of variables demoted to a function scope.
250 std::map<const Function *, std::vector<const GlobalVariable *> > localDecls;
252 // To record filename to ID mapping
253 std::map<std::string, unsigned> filenameMap;
254 void recordAndEmitFilenames(Module &);
256 void emitPTXGlobalVariable(const GlobalVariable *GVar, raw_ostream &O);
257 void emitPTXAddressSpace(unsigned int AddressSpace, raw_ostream &O) const;
258 std::string getPTXFundamentalTypeStr(const Type *Ty, bool = true) const;
259 void printScalarConstant(const Constant *CPV, raw_ostream &O);
260 void printFPConstant(const ConstantFP *Fp, raw_ostream &O);
261 void bufferLEByte(const Constant *CPV, int Bytes, AggBuffer *aggBuffer);
262 void bufferAggregateConstant(const Constant *CV, AggBuffer *aggBuffer);
264 void printOperandProper(const MachineOperand &MO);
266 void emitLinkageDirective(const GlobalValue *V, raw_ostream &O);
267 void emitDeclarations(const Module &, raw_ostream &O);
268 void emitDeclaration(const Function *, raw_ostream &O);
270 static const char *getRegisterName(unsigned RegNo);
271 void emitDemotedVars(const Function *, raw_ostream &);
274 LineReader *getReader(std::string);
276 NVPTXAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
277 : AsmPrinter(TM, Streamer),
278 nvptxSubtarget(TM.getSubtarget<NVPTXSubtarget>()) {
279 CurrentBankselLabelInBasicBlock = "";
288 bool ignoreLoc(const MachineInstr &);
290 virtual void getVirtualRegisterName(unsigned, bool, raw_ostream &);
292 DebugLoc prevDebugLoc;
293 void emitLineNumberAsDotLoc(const MachineInstr &);
295 } // end of namespace