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/TargetMachine.h"
33 // The ptx syntax and format is very different from that usually seem in a .s
35 // therefore we are not able to use the MCAsmStreamer interface here.
37 // We are handcrafting the output method here.
39 // A better approach is to clone the MCAsmStreamer to a MCPTXAsmStreamer
40 // (subclass of MCStreamer).
42 // This is defined in AsmPrinter.cpp.
43 // Used to process the constant expressions in initializers.
46 LowerConstant(const llvm::Constant *CV, llvm::AsmPrinter &AP);
56 std::string theFileName;
57 SmallVector<unsigned, 32> lineOffset;
59 LineReader(std::string filename) {
61 fstr.open(filename.c_str());
62 theFileName = filename;
64 std::string fileName() { return theFileName; }
65 ~LineReader() { fstr.close(); }
66 std::string readLine(unsigned line);
69 class LLVM_LIBRARY_VISIBILITY NVPTXAsmPrinter : public AsmPrinter {
72 // Used to buffer the emitted string for initializing global
75 // Normally an aggregate (array, vector or structure) is emitted
76 // as a u8[]. However, if one element/field of the aggregate
77 // is a non-NULL address, then the aggregate is emitted as u32[]
80 // We first layout the aggregate in 'buffer' in bytes, except for
81 // those symbol addresses. For the i-th symbol address in the
82 //aggregate, its corresponding 4-byte or 8-byte elements in 'buffer'
83 // are filled with 0s. symbolPosInBuffer[i-1] records its position
84 // in 'buffer', and Symbols[i-1] records the Value*.
86 // Once we have this AggBuffer setup, we can choose how to print
89 unsigned size; // size of the buffer in bytes
90 unsigned char *buffer; // the buffer
91 unsigned numSymbols; // number of symbol addresses
92 SmallVector<unsigned, 4> symbolPosInBuffer;
93 SmallVector<const Value *, 4> Symbols;
101 AggBuffer(unsigned _size, raw_ostream &_O, NVPTXAsmPrinter &_AP)
103 buffer = new unsigned char[_size];
108 ~AggBuffer() { delete[] buffer; }
109 unsigned addBytes(unsigned char *Ptr, int Num, int Bytes) {
110 assert((curpos + Num) <= size);
111 assert((curpos + Bytes) <= size);
112 for (int i = 0; i < Num; ++i) {
113 buffer[curpos] = Ptr[i];
116 for (int i = Num; i < Bytes; ++i) {
122 unsigned addZeros(int Num) {
123 assert((curpos + Num) <= size);
124 for (int i = 0; i < Num; ++i) {
130 void addSymbol(const Value *GVar) {
131 symbolPosInBuffer.push_back(curpos);
132 Symbols.push_back(GVar);
136 if (numSymbols == 0) {
137 // print out in bytes
138 for (unsigned i = 0; i < size; i++) {
141 O << (unsigned int) buffer[i];
144 // print out in 4-bytes or 8-bytes
145 unsigned int pos = 0;
146 unsigned int nSym = 0;
147 unsigned int nextSymbolPos = symbolPosInBuffer[nSym];
148 unsigned int nBytes = 4;
149 if (AP.nvptxSubtarget.is64Bit())
151 for (pos = 0; pos < size; pos += nBytes) {
154 if (pos == nextSymbolPos) {
155 const Value *v = Symbols[nSym];
156 if (const GlobalValue *GVar = dyn_cast<GlobalValue>(v)) {
157 MCSymbol *Name = AP.getSymbol(GVar);
159 } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(v)) {
160 O << *nvptx::LowerConstant(Cexpr, AP);
162 llvm_unreachable("symbol type unknown");
164 if (nSym >= numSymbols)
165 nextSymbolPos = size + 1;
167 nextSymbolPos = symbolPosInBuffer[nSym];
168 } else if (nBytes == 4)
169 O << *(unsigned int *)(buffer + pos);
171 O << *(unsigned long long *)(buffer + pos);
177 friend class AggBuffer;
179 virtual void emitSrcInText(StringRef filename, unsigned line);
182 virtual const char *getPassName() const { return "NVPTX Assembly Printer"; }
185 std::string CurrentFnName;
187 void EmitFunctionEntryLabel();
188 void EmitFunctionBodyStart();
189 void EmitFunctionBodyEnd();
190 void emitImplicitDef(const MachineInstr *MI) const;
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 void printModuleLevelGV(const GlobalVariable *GVar, raw_ostream &O,
208 void printParamName(int paramIndex, raw_ostream &O);
209 void printParamName(Function::const_arg_iterator I, int paramIndex,
211 void emitGlobals(const Module &M);
212 void emitHeader(Module &M, raw_ostream &O);
213 void emitKernelFunctionDirectives(const Function &F, raw_ostream &O) const;
214 void emitVirtualRegister(unsigned int vr, raw_ostream &);
215 void emitFunctionExternParamList(const MachineFunction &MF);
216 void emitFunctionParamList(const Function *, raw_ostream &O);
217 void emitFunctionParamList(const MachineFunction &MF, raw_ostream &O);
218 void setAndEmitFunctionVirtualRegisters(const MachineFunction &MF);
219 void emitFunctionTempData(const MachineFunction &MF, unsigned &FrameSize);
220 bool isImageType(const Type *Ty);
221 void printReturnValStr(const Function *, raw_ostream &O);
222 void printReturnValStr(const MachineFunction &MF, raw_ostream &O);
223 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
224 unsigned AsmVariant, const char *ExtraCode,
226 void printOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
227 const char *Modifier = 0);
228 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
229 unsigned AsmVariant, const char *ExtraCode,
232 bool doInitialization(Module &M);
233 bool doFinalization(Module &M);
236 std::string CurrentBankselLabelInBasicBlock;
240 // This is specific per MachineFunction.
241 const MachineRegisterInfo *MRI;
242 // The contents are specific for each
243 // MachineFunction. But the size of the
245 typedef DenseMap<unsigned, unsigned> VRegMap;
246 typedef DenseMap<const TargetRegisterClass *, VRegMap> VRegRCMap;
247 VRegRCMap VRegMapping;
248 // cache the subtarget here.
249 const NVPTXSubtarget &nvptxSubtarget;
250 // Build the map between type name and ID based on module's type
252 std::map<const Type *, std::string> TypeNameMap;
254 // List of variables demoted to a function scope.
255 std::map<const Function *, std::vector<const GlobalVariable *> > localDecls;
257 // To record filename to ID mapping
258 std::map<std::string, unsigned> filenameMap;
259 void recordAndEmitFilenames(Module &);
261 void emitPTXGlobalVariable(const GlobalVariable *GVar, raw_ostream &O);
262 void emitPTXAddressSpace(unsigned int AddressSpace, raw_ostream &O) const;
263 std::string getPTXFundamentalTypeStr(const Type *Ty, bool = true) const;
264 void printScalarConstant(const Constant *CPV, raw_ostream &O);
265 void printFPConstant(const ConstantFP *Fp, raw_ostream &O);
266 void bufferLEByte(const Constant *CPV, int Bytes, AggBuffer *aggBuffer);
267 void bufferAggregateConstant(const Constant *CV, AggBuffer *aggBuffer);
269 void printOperandProper(const MachineOperand &MO);
271 void emitLinkageDirective(const GlobalValue *V, raw_ostream &O);
272 void emitDeclarations(const Module &, raw_ostream &O);
273 void emitDeclaration(const Function *, raw_ostream &O);
275 static const char *getRegisterName(unsigned RegNo);
276 void emitDemotedVars(const Function *, raw_ostream &);
279 LineReader *getReader(std::string);
281 NVPTXAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
282 : AsmPrinter(TM, Streamer),
283 nvptxSubtarget(TM.getSubtarget<NVPTXSubtarget>()) {
284 CurrentBankselLabelInBasicBlock = "";
293 bool ignoreLoc(const MachineInstr &);
295 std::string getVirtualRegisterName(unsigned) const;
297 DebugLoc prevDebugLoc;
298 void emitLineNumberAsDotLoc(const MachineInstr &);
300 } // end of namespace