blob: 71624200d0ec1d2d75e561504e6a549f8ef7f764 [file] [log] [blame]
Justin Holewinskiae556d32012-05-04 20:18:50 +00001//===-- NVPTXAsmPrinter.h - NVPTX LLVM assembly writer --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to NVPTX assembly language.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef NVPTXASMPRINTER_H
16#define NVPTXASMPRINTER_H
17
18#include "NVPTX.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000019#include "NVPTXSubtarget.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000020#include "NVPTXTargetMachine.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000021#include "llvm/ADT/SmallString.h"
22#include "llvm/ADT/StringExtras.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000023#include "llvm/CodeGen/AsmPrinter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Function.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000025#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"
Chandler Carruth802d7552012-12-04 07:12:27 +000030#include "llvm/Target/TargetMachine.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000031#include <fstream>
32
33// The ptx syntax and format is very different from that usually seem in a .s
34// file,
35// therefore we are not able to use the MCAsmStreamer interface here.
36//
37// We are handcrafting the output method here.
38//
39// A better approach is to clone the MCAsmStreamer to a MCPTXAsmStreamer
40// (subclass of MCStreamer).
41
42// This is defined in AsmPrinter.cpp.
43// Used to process the constant expressions in initializers.
44namespace nvptx {
Justin Holewinski0497ab12013-03-30 14:29:21 +000045const llvm::MCExpr *
46LowerConstant(const llvm::Constant *CV, llvm::AsmPrinter &AP);
Justin Holewinskiae556d32012-05-04 20:18:50 +000047}
48
49namespace llvm {
50
51class LineReader {
52private:
Justin Holewinski0497ab12013-03-30 14:29:21 +000053 unsigned theCurLine;
Justin Holewinskiae556d32012-05-04 20:18:50 +000054 std::ifstream fstr;
55 char buff[512];
56 std::string theFileName;
57 SmallVector<unsigned, 32> lineOffset;
58public:
59 LineReader(std::string filename) {
60 theCurLine = 0;
61 fstr.open(filename.c_str());
62 theFileName = filename;
63 }
64 std::string fileName() { return theFileName; }
Justin Holewinski0497ab12013-03-30 14:29:21 +000065 ~LineReader() { fstr.close(); }
Justin Holewinskiae556d32012-05-04 20:18:50 +000066 std::string readLine(unsigned line);
67};
68
Justin Holewinskiae556d32012-05-04 20:18:50 +000069class LLVM_LIBRARY_VISIBILITY NVPTXAsmPrinter : public AsmPrinter {
70
Justin Holewinskiae556d32012-05-04 20:18:50 +000071 class AggBuffer {
72 // Used to buffer the emitted string for initializing global
73 // aggregates.
74 //
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[]
78 // or u64[].
79 //
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*.
85 //
86 // Once we have this AggBuffer setup, we can choose how to print
87 // it out.
88 public:
Justin Holewinski0497ab12013-03-30 14:29:21 +000089 unsigned size; // size of the buffer in bytes
Justin Holewinskiae556d32012-05-04 20:18:50 +000090 unsigned char *buffer; // the buffer
91 unsigned numSymbols; // number of symbol addresses
92 SmallVector<unsigned, 4> symbolPosInBuffer;
Justin Holewinski01f89f02013-05-20 12:13:32 +000093 SmallVector<const Value *, 4> Symbols;
Justin Holewinskiae556d32012-05-04 20:18:50 +000094
95 private:
96 unsigned curpos;
97 raw_ostream &O;
98 NVPTXAsmPrinter &AP;
99
100 public:
101 AggBuffer(unsigned _size, raw_ostream &_O, NVPTXAsmPrinter &_AP)
Justin Holewinski0497ab12013-03-30 14:29:21 +0000102 : O(_O), AP(_AP) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000103 buffer = new unsigned char[_size];
104 size = _size;
105 curpos = 0;
106 numSymbols = 0;
107 }
Justin Holewinski0497ab12013-03-30 14:29:21 +0000108 ~AggBuffer() { delete[] buffer; }
Justin Holewinskiae556d32012-05-04 20:18:50 +0000109 unsigned addBytes(unsigned char *Ptr, int Num, int Bytes) {
Justin Holewinski0497ab12013-03-30 14:29:21 +0000110 assert((curpos + Num) <= size);
111 assert((curpos + Bytes) <= size);
112 for (int i = 0; i < Num; ++i) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000113 buffer[curpos] = Ptr[i];
Justin Holewinski0497ab12013-03-30 14:29:21 +0000114 curpos++;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000115 }
Justin Holewinski0497ab12013-03-30 14:29:21 +0000116 for (int i = Num; i < Bytes; ++i) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000117 buffer[curpos] = 0;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000118 curpos++;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000119 }
120 return curpos;
121 }
122 unsigned addZeros(int Num) {
Justin Holewinski0497ab12013-03-30 14:29:21 +0000123 assert((curpos + Num) <= size);
124 for (int i = 0; i < Num; ++i) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000125 buffer[curpos] = 0;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000126 curpos++;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000127 }
128 return curpos;
129 }
Justin Holewinski01f89f02013-05-20 12:13:32 +0000130 void addSymbol(const Value *GVar) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000131 symbolPosInBuffer.push_back(curpos);
132 Symbols.push_back(GVar);
133 numSymbols++;
134 }
135 void print() {
136 if (numSymbols == 0) {
137 // print out in bytes
Justin Holewinski0497ab12013-03-30 14:29:21 +0000138 for (unsigned i = 0; i < size; i++) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000139 if (i)
140 O << ", ";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000141 O << (unsigned int) buffer[i];
Justin Holewinskiae556d32012-05-04 20:18:50 +0000142 }
Craig Topperbdf39a42012-05-24 07:02:50 +0000143 } else {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000144 // 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())
150 nBytes = 8;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000151 for (pos = 0; pos < size; pos += nBytes) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000152 if (pos)
153 O << ", ";
154 if (pos == nextSymbolPos) {
Justin Holewinski01f89f02013-05-20 12:13:32 +0000155 const Value *v = Symbols[nSym];
156 if (const GlobalValue *GVar = dyn_cast<GlobalValue>(v)) {
Rafael Espindola79858aa2013-10-29 17:07:16 +0000157 MCSymbol *Name = AP.getSymbol(GVar);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000158 O << *Name;
Justin Holewinski01f89f02013-05-20 12:13:32 +0000159 } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(v)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000160 O << *nvptx::LowerConstant(Cexpr, AP);
Craig Topperbdf39a42012-05-24 07:02:50 +0000161 } else
162 llvm_unreachable("symbol type unknown");
Justin Holewinskiae556d32012-05-04 20:18:50 +0000163 nSym++;
164 if (nSym >= numSymbols)
Justin Holewinski0497ab12013-03-30 14:29:21 +0000165 nextSymbolPos = size + 1;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000166 else
167 nextSymbolPos = symbolPosInBuffer[nSym];
Justin Holewinski0497ab12013-03-30 14:29:21 +0000168 } else if (nBytes == 4)
169 O << *(unsigned int *)(buffer + pos);
170 else
171 O << *(unsigned long long *)(buffer + pos);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000172 }
173 }
174 }
175 };
176
177 friend class AggBuffer;
178
179 virtual void emitSrcInText(StringRef filename, unsigned line);
180
Justin Holewinski0497ab12013-03-30 14:29:21 +0000181private:
182 virtual const char *getPassName() const { return "NVPTX Assembly Printer"; }
Justin Holewinskiae556d32012-05-04 20:18:50 +0000183
184 const Function *F;
185 std::string CurrentFnName;
186
187 void EmitFunctionEntryLabel();
188 void EmitFunctionBodyStart();
189 void EmitFunctionBodyEnd();
Justin Holewinski660597d2013-10-11 12:39:36 +0000190 void emitImplicitDef(const MachineInstr *MI) const;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000191
192 void EmitInstruction(const MachineInstr *);
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000193 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);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000197
198 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const {}
199
Justin Holewinski0497ab12013-03-30 14:29:21 +0000200 void printVecModifiedImmediate(const MachineOperand &MO, const char *Modifier,
201 raw_ostream &O);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000202 void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
Justin Holewinski0497ab12013-03-30 14:29:21 +0000203 const char *Modifier = 0);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000204 void printImplicitDef(const MachineInstr *MI, raw_ostream &O) const;
Justin Holewinski01f89f02013-05-20 12:13:32 +0000205 void printModuleLevelGV(const GlobalVariable *GVar, raw_ostream &O,
206 bool = false);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000207 void printParamName(int paramIndex, raw_ostream &O);
208 void printParamName(Function::const_arg_iterator I, int paramIndex,
209 raw_ostream &O);
Justin Holewinski01f89f02013-05-20 12:13:32 +0000210 void emitGlobals(const Module &M);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000211 void emitHeader(Module &M, raw_ostream &O);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000212 void emitKernelFunctionDirectives(const Function &F, raw_ostream &O) const;
Justin Holewinski660597d2013-10-11 12:39:36 +0000213 void emitVirtualRegister(unsigned int vr, raw_ostream &);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000214 void emitFunctionExternParamList(const MachineFunction &MF);
215 void emitFunctionParamList(const Function *, raw_ostream &O);
216 void emitFunctionParamList(const MachineFunction &MF, raw_ostream &O);
217 void setAndEmitFunctionVirtualRegisters(const MachineFunction &MF);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000218 void emitFunctionTempData(const MachineFunction &MF, unsigned &FrameSize);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000219 bool isImageType(const Type *Ty);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000220 void printReturnValStr(const Function *, raw_ostream &O);
221 void printReturnValStr(const MachineFunction &MF, raw_ostream &O);
Justin Holewinskiaaa8b6e2013-08-24 01:17:23 +0000222 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
223 unsigned AsmVariant, const char *ExtraCode,
224 raw_ostream &);
225 void printOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
226 const char *Modifier = 0);
227 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
228 unsigned AsmVariant, const char *ExtraCode,
229 raw_ostream &);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000230protected:
231 bool doInitialization(Module &M);
232 bool doFinalization(Module &M);
233
234private:
235 std::string CurrentBankselLabelInBasicBlock;
236
Justin Holewinski01f89f02013-05-20 12:13:32 +0000237 bool GlobalsEmitted;
238
Justin Holewinskiae556d32012-05-04 20:18:50 +0000239 // This is specific per MachineFunction.
240 const MachineRegisterInfo *MRI;
241 // The contents are specific for each
242 // MachineFunction. But the size of the
243 // array is not.
Justin Holewinskidbb3b2f2013-05-31 12:14:49 +0000244 typedef DenseMap<unsigned, unsigned> VRegMap;
245 typedef DenseMap<const TargetRegisterClass *, VRegMap> VRegRCMap;
246 VRegRCMap VRegMapping;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000247 // cache the subtarget here.
248 const NVPTXSubtarget &nvptxSubtarget;
249 // Build the map between type name and ID based on module's type
250 // symbol table.
251 std::map<const Type *, std::string> TypeNameMap;
252
253 // List of variables demoted to a function scope.
Justin Holewinski01f89f02013-05-20 12:13:32 +0000254 std::map<const Function *, std::vector<const GlobalVariable *> > localDecls;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000255
256 // To record filename to ID mapping
257 std::map<std::string, unsigned> filenameMap;
258 void recordAndEmitFilenames(Module &);
259
260 void emitPTXGlobalVariable(const GlobalVariable *GVar, raw_ostream &O);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000261 void emitPTXAddressSpace(unsigned int AddressSpace, raw_ostream &O) const;
262 std::string getPTXFundamentalTypeStr(const Type *Ty, bool = true) const;
Justin Holewinski01f89f02013-05-20 12:13:32 +0000263 void printScalarConstant(const Constant *CPV, raw_ostream &O);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000264 void printFPConstant(const ConstantFP *Fp, raw_ostream &O);
Justin Holewinski01f89f02013-05-20 12:13:32 +0000265 void bufferLEByte(const Constant *CPV, int Bytes, AggBuffer *aggBuffer);
266 void bufferAggregateConstant(const Constant *CV, AggBuffer *aggBuffer);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000267
268 void printOperandProper(const MachineOperand &MO);
269
Justin Holewinski0497ab12013-03-30 14:29:21 +0000270 void emitLinkageDirective(const GlobalValue *V, raw_ostream &O);
Justin Holewinski01f89f02013-05-20 12:13:32 +0000271 void emitDeclarations(const Module &, raw_ostream &O);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000272 void emitDeclaration(const Function *, raw_ostream &O);
273
274 static const char *getRegisterName(unsigned RegNo);
275 void emitDemotedVars(const Function *, raw_ostream &);
276
277 LineReader *reader;
278 LineReader *getReader(std::string);
279public:
Justin Holewinski0497ab12013-03-30 14:29:21 +0000280 NVPTXAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
281 : AsmPrinter(TM, Streamer),
282 nvptxSubtarget(TM.getSubtarget<NVPTXSubtarget>()) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000283 CurrentBankselLabelInBasicBlock = "";
Justin Holewinskiae556d32012-05-04 20:18:50 +0000284 reader = NULL;
285 }
286
287 ~NVPTXAsmPrinter() {
288 if (!reader)
289 delete reader;
290 }
291
292 bool ignoreLoc(const MachineInstr &);
293
Justin Holewinski660597d2013-10-11 12:39:36 +0000294 std::string getVirtualRegisterName(unsigned) const;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000295
296 DebugLoc prevDebugLoc;
297 void emitLineNumberAsDotLoc(const MachineInstr &);
298};
299} // end of namespace
300
301#endif