Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1 | //===-- 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 Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 19 | #include "NVPTXSubtarget.h" |
Chandler Carruth | a1514e2 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 20 | #include "NVPTXTargetMachine.h" |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallString.h" |
| 22 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | a1514e2 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/AsmPrinter.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Function.h" |
Chandler Carruth | a1514e2 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 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" |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 32 | #include <fstream> |
| 33 | |
| 34 | // The ptx syntax and format is very different from that usually seem in a .s |
| 35 | // file, |
| 36 | // therefore we are not able to use the MCAsmStreamer interface here. |
| 37 | // |
| 38 | // We are handcrafting the output method here. |
| 39 | // |
| 40 | // A better approach is to clone the MCAsmStreamer to a MCPTXAsmStreamer |
| 41 | // (subclass of MCStreamer). |
| 42 | |
| 43 | // This is defined in AsmPrinter.cpp. |
| 44 | // Used to process the constant expressions in initializers. |
| 45 | namespace nvptx { |
Justin Holewinski | 3639ce2 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 46 | const llvm::MCExpr * |
| 47 | LowerConstant(const llvm::Constant *CV, llvm::AsmPrinter &AP); |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | namespace llvm { |
| 51 | |
| 52 | class LineReader { |
| 53 | private: |
Justin Holewinski | 3639ce2 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 54 | unsigned theCurLine; |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 55 | std::ifstream fstr; |
| 56 | char buff[512]; |
| 57 | std::string theFileName; |
| 58 | SmallVector<unsigned, 32> lineOffset; |
| 59 | public: |
| 60 | LineReader(std::string filename) { |
| 61 | theCurLine = 0; |
| 62 | fstr.open(filename.c_str()); |
| 63 | theFileName = filename; |
| 64 | } |
| 65 | std::string fileName() { return theFileName; } |
Justin Holewinski | 3639ce2 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 66 | ~LineReader() { fstr.close(); } |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 67 | std::string readLine(unsigned line); |
| 68 | }; |
| 69 | |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 70 | class LLVM_LIBRARY_VISIBILITY NVPTXAsmPrinter : public AsmPrinter { |
| 71 | |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 72 | class AggBuffer { |
| 73 | // Used to buffer the emitted string for initializing global |
| 74 | // aggregates. |
| 75 | // |
| 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[] |
| 79 | // or u64[]. |
| 80 | // |
| 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*. |
| 86 | // |
| 87 | // Once we have this AggBuffer setup, we can choose how to print |
| 88 | // it out. |
| 89 | public: |
Justin Holewinski | 3639ce2 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 90 | unsigned size; // size of the buffer in bytes |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 91 | unsigned char *buffer; // the buffer |
| 92 | unsigned numSymbols; // number of symbol addresses |
| 93 | SmallVector<unsigned, 4> symbolPosInBuffer; |
Justin Holewinski | 7536ecf | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 94 | SmallVector<const Value *, 4> Symbols; |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 95 | |
| 96 | private: |
| 97 | unsigned curpos; |
| 98 | raw_ostream &O; |
| 99 | NVPTXAsmPrinter &AP; |
| 100 | |
| 101 | public: |
| 102 | AggBuffer(unsigned _size, raw_ostream &_O, NVPTXAsmPrinter &_AP) |
Justin Holewinski | 3639ce2 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 103 | : O(_O), AP(_AP) { |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 104 | buffer = new unsigned char[_size]; |
| 105 | size = _size; |
| 106 | curpos = 0; |
| 107 | numSymbols = 0; |
| 108 | } |
Justin Holewinski | 3639ce2 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 109 | ~AggBuffer() { delete[] buffer; } |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 110 | unsigned addBytes(unsigned char *Ptr, int Num, int Bytes) { |
Justin Holewinski | 3639ce2 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 111 | assert((curpos + Num) <= size); |
| 112 | assert((curpos + Bytes) <= size); |
| 113 | for (int i = 0; i < Num; ++i) { |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 114 | buffer[curpos] = Ptr[i]; |
Justin Holewinski | 3639ce2 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 115 | curpos++; |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 116 | } |
Justin Holewinski | 3639ce2 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 117 | for (int i = Num; i < Bytes; ++i) { |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 118 | buffer[curpos] = 0; |
Justin Holewinski | 3639ce2 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 119 | curpos++; |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 120 | } |
| 121 | return curpos; |
| 122 | } |
| 123 | unsigned addZeros(int Num) { |
Justin Holewinski | 3639ce2 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 124 | assert((curpos + Num) <= size); |
| 125 | for (int i = 0; i < Num; ++i) { |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 126 | buffer[curpos] = 0; |
Justin Holewinski | 3639ce2 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 127 | curpos++; |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 128 | } |
| 129 | return curpos; |
| 130 | } |
Justin Holewinski | 7536ecf | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 131 | void addSymbol(const Value *GVar) { |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 132 | symbolPosInBuffer.push_back(curpos); |
| 133 | Symbols.push_back(GVar); |
| 134 | numSymbols++; |
| 135 | } |
| 136 | void print() { |
| 137 | if (numSymbols == 0) { |
| 138 | // print out in bytes |
Justin Holewinski | 3639ce2 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 139 | for (unsigned i = 0; i < size; i++) { |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 140 | if (i) |
| 141 | O << ", "; |
Justin Holewinski | 3639ce2 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 142 | O << (unsigned int) buffer[i]; |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 143 | } |
Craig Topper | 6366361 | 2012-05-24 07:02:50 +0000 | [diff] [blame] | 144 | } else { |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 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()) |
| 151 | nBytes = 8; |
Justin Holewinski | 3639ce2 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 152 | for (pos = 0; pos < size; pos += nBytes) { |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 153 | if (pos) |
| 154 | O << ", "; |
| 155 | if (pos == nextSymbolPos) { |
Justin Holewinski | 7536ecf | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 156 | const Value *v = Symbols[nSym]; |
| 157 | if (const GlobalValue *GVar = dyn_cast<GlobalValue>(v)) { |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 158 | MCSymbol *Name = AP.Mang->getSymbol(GVar); |
| 159 | O << *Name; |
Justin Holewinski | 7536ecf | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 160 | } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(v)) { |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 161 | O << *nvptx::LowerConstant(Cexpr, AP); |
Craig Topper | 6366361 | 2012-05-24 07:02:50 +0000 | [diff] [blame] | 162 | } else |
| 163 | llvm_unreachable("symbol type unknown"); |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 164 | nSym++; |
| 165 | if (nSym >= numSymbols) |
Justin Holewinski | 3639ce2 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 166 | nextSymbolPos = size + 1; |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 167 | else |
| 168 | nextSymbolPos = symbolPosInBuffer[nSym]; |
Justin Holewinski | 3639ce2 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 169 | } else if (nBytes == 4) |
| 170 | O << *(unsigned int *)(buffer + pos); |
| 171 | else |
| 172 | O << *(unsigned long long *)(buffer + pos); |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | } |
| 176 | }; |
| 177 | |
| 178 | friend class AggBuffer; |
| 179 | |
| 180 | virtual void emitSrcInText(StringRef filename, unsigned line); |
| 181 | |
Justin Holewinski | 3639ce2 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 182 | private: |
| 183 | virtual const char *getPassName() const { return "NVPTX Assembly Printer"; } |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 184 | |
| 185 | const Function *F; |
| 186 | std::string CurrentFnName; |
| 187 | |
| 188 | void EmitFunctionEntryLabel(); |
| 189 | void EmitFunctionBodyStart(); |
| 190 | void EmitFunctionBodyEnd(); |
| 191 | |
| 192 | void EmitInstruction(const MachineInstr *); |
| 193 | |
| 194 | void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const {} |
| 195 | |
| 196 | void printGlobalVariable(const GlobalVariable *GVar); |
| 197 | void printOperand(const MachineInstr *MI, int opNum, raw_ostream &O, |
Justin Holewinski | 3639ce2 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 198 | const char *Modifier = 0); |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 199 | void printLdStCode(const MachineInstr *MI, int opNum, raw_ostream &O, |
Justin Holewinski | 3639ce2 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 200 | const char *Modifier = 0); |
| 201 | void printVecModifiedImmediate(const MachineOperand &MO, const char *Modifier, |
| 202 | raw_ostream &O); |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 203 | void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O, |
Justin Holewinski | 3639ce2 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 204 | const char *Modifier = 0); |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 205 | void printImplicitDef(const MachineInstr *MI, raw_ostream &O) const; |
| 206 | // definition autogenerated. |
| 207 | void printInstruction(const MachineInstr *MI, raw_ostream &O); |
Justin Holewinski | 7536ecf | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 208 | void printModuleLevelGV(const GlobalVariable *GVar, raw_ostream &O, |
| 209 | bool = false); |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 210 | void printParamName(int paramIndex, raw_ostream &O); |
| 211 | void printParamName(Function::const_arg_iterator I, int paramIndex, |
| 212 | raw_ostream &O); |
Justin Holewinski | 7536ecf | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 213 | void emitGlobals(const Module &M); |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 214 | void emitHeader(Module &M, raw_ostream &O); |
Justin Holewinski | 3639ce2 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 215 | void emitKernelFunctionDirectives(const Function &F, raw_ostream &O) const; |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 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); |
Justin Holewinski | 3639ce2 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 221 | void emitFunctionTempData(const MachineFunction &MF, unsigned &FrameSize); |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 222 | bool isImageType(const Type *Ty); |
| 223 | bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
| 224 | unsigned AsmVariant, const char *ExtraCode, |
| 225 | raw_ostream &); |
| 226 | bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, |
| 227 | unsigned AsmVariant, const char *ExtraCode, |
| 228 | raw_ostream &); |
| 229 | void printReturnValStr(const Function *, raw_ostream &O); |
| 230 | void printReturnValStr(const MachineFunction &MF, raw_ostream &O); |
| 231 | |
| 232 | protected: |
| 233 | bool doInitialization(Module &M); |
| 234 | bool doFinalization(Module &M); |
| 235 | |
| 236 | private: |
| 237 | std::string CurrentBankselLabelInBasicBlock; |
| 238 | |
Justin Holewinski | 7536ecf | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 239 | bool GlobalsEmitted; |
| 240 | |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 241 | // This is specific per MachineFunction. |
| 242 | const MachineRegisterInfo *MRI; |
| 243 | // The contents are specific for each |
| 244 | // MachineFunction. But the size of the |
| 245 | // array is not. |
| 246 | std::map<unsigned, unsigned> *VRidGlobal2LocalMap; |
| 247 | // 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 Holewinski | 7536ecf | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 254 | std::map<const Function *, std::vector<const GlobalVariable *> > localDecls; |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 255 | |
| 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 Holewinski | 3639ce2 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 261 | void emitPTXAddressSpace(unsigned int AddressSpace, raw_ostream &O) const; |
| 262 | std::string getPTXFundamentalTypeStr(const Type *Ty, bool = true) const; |
Justin Holewinski | 7536ecf | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 263 | void printScalarConstant(const Constant *CPV, raw_ostream &O); |
Justin Holewinski | 3639ce2 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 264 | void printFPConstant(const ConstantFP *Fp, raw_ostream &O); |
Justin Holewinski | 7536ecf | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 265 | void bufferLEByte(const Constant *CPV, int Bytes, AggBuffer *aggBuffer); |
| 266 | void bufferAggregateConstant(const Constant *CV, AggBuffer *aggBuffer); |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 267 | |
| 268 | void printOperandProper(const MachineOperand &MO); |
| 269 | |
Justin Holewinski | 3639ce2 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 270 | void emitLinkageDirective(const GlobalValue *V, raw_ostream &O); |
Justin Holewinski | 7536ecf | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 271 | void emitDeclarations(const Module &, raw_ostream &O); |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 272 | 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); |
| 279 | public: |
Justin Holewinski | 3639ce2 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 280 | NVPTXAsmPrinter(TargetMachine &TM, MCStreamer &Streamer) |
| 281 | : AsmPrinter(TM, Streamer), |
| 282 | nvptxSubtarget(TM.getSubtarget<NVPTXSubtarget>()) { |
Justin Holewinski | 49683f3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 283 | CurrentBankselLabelInBasicBlock = ""; |
| 284 | VRidGlobal2LocalMap = NULL; |
| 285 | reader = NULL; |
| 286 | } |
| 287 | |
| 288 | ~NVPTXAsmPrinter() { |
| 289 | if (!reader) |
| 290 | delete reader; |
| 291 | } |
| 292 | |
| 293 | bool ignoreLoc(const MachineInstr &); |
| 294 | |
| 295 | virtual void getVirtualRegisterName(unsigned, bool, raw_ostream &); |
| 296 | |
| 297 | DebugLoc prevDebugLoc; |
| 298 | void emitLineNumberAsDotLoc(const MachineInstr &); |
| 299 | }; |
| 300 | } // end of namespace |
| 301 | |
| 302 | #endif |