Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 1 | //===-- PTXAsmPrinter.cpp - PTX 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 PTX assembly language. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 15 | #define DEBUG_TYPE "ptx-asm-printer" |
| 16 | |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 17 | #include "PTX.h" |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 18 | #include "PTXMachineFunctionInfo.h" |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 19 | #include "PTXTargetMachine.h" |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 20 | #include "llvm/DerivedTypes.h" |
| 21 | #include "llvm/Module.h" |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallString.h" |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
| 24 | #include "llvm/ADT/Twine.h" |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/AsmPrinter.h" |
Justin Holewinski | df1c8d8 | 2011-06-20 15:56:20 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineInstr.h" |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCStreamer.h" |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 30 | #include "llvm/MC/MCSymbol.h" |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 31 | #include "llvm/Target/Mangler.h" |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetRegistry.h" |
Che-Liang Chiou | 21d8b9b | 2010-11-30 10:14:14 +0000 | [diff] [blame] | 34 | #include "llvm/Support/CommandLine.h" |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Debug.h" |
| 36 | #include "llvm/Support/ErrorHandling.h" |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 37 | #include "llvm/Support/MathExtras.h" |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 38 | #include "llvm/Support/raw_ostream.h" |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 39 | |
| 40 | using namespace llvm; |
| 41 | |
| 42 | namespace { |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 43 | class PTXAsmPrinter : public AsmPrinter { |
| 44 | public: |
| 45 | explicit PTXAsmPrinter(TargetMachine &TM, MCStreamer &Streamer) |
| 46 | : AsmPrinter(TM, Streamer) {} |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 47 | |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 48 | const char *getPassName() const { return "PTX Assembly Printer"; } |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 49 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 50 | bool doFinalization(Module &M); |
| 51 | |
Che-Liang Chiou | 21d8b9b | 2010-11-30 10:14:14 +0000 | [diff] [blame] | 52 | virtual void EmitStartOfAsmFile(Module &M); |
| 53 | |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 54 | virtual bool runOnMachineFunction(MachineFunction &MF); |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 55 | |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 56 | virtual void EmitFunctionBodyStart(); |
| 57 | virtual void EmitFunctionBodyEnd() { OutStreamer.EmitRawText(Twine("}")); } |
| 58 | |
| 59 | virtual void EmitInstruction(const MachineInstr *MI); |
| 60 | |
| 61 | void printOperand(const MachineInstr *MI, int opNum, raw_ostream &OS); |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 62 | void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &OS, |
| 63 | const char *Modifier = 0); |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 64 | void printParamOperand(const MachineInstr *MI, int opNum, raw_ostream &OS, |
| 65 | const char *Modifier = 0); |
Che-Liang Chiou | c2ec0f9 | 2011-03-13 17:26:00 +0000 | [diff] [blame] | 66 | void printPredicateOperand(const MachineInstr *MI, raw_ostream &O); |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 67 | |
| 68 | // autogen'd. |
| 69 | void printInstruction(const MachineInstr *MI, raw_ostream &OS); |
| 70 | static const char *getRegisterName(unsigned RegNo); |
| 71 | |
| 72 | private: |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 73 | void EmitVariableDeclaration(const GlobalVariable *gv); |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 74 | void EmitFunctionDeclaration(); |
| 75 | }; // class PTXAsmPrinter |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 76 | } // namespace |
| 77 | |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 78 | static const char PARAM_PREFIX[] = "__param_"; |
| 79 | |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 80 | static const char *getRegisterTypeName(unsigned RegNo) { |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 81 | #define TEST_REGCLS(cls, clsstr) \ |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 82 | if (PTX::cls ## RegisterClass->contains(RegNo)) return # clsstr; |
Justin Holewinski | 1b91bcd | 2011-06-16 17:49:58 +0000 | [diff] [blame] | 83 | TEST_REGCLS(RegPred, pred); |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 84 | TEST_REGCLS(RegI16, b16); |
| 85 | TEST_REGCLS(RegI32, b32); |
| 86 | TEST_REGCLS(RegI64, b64); |
| 87 | TEST_REGCLS(RegF32, b32); |
| 88 | TEST_REGCLS(RegF64, b64); |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 89 | #undef TEST_REGCLS |
| 90 | |
| 91 | llvm_unreachable("Not in any register class!"); |
| 92 | return NULL; |
| 93 | } |
| 94 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 95 | static const char *getStateSpaceName(unsigned addressSpace) { |
Che-Liang Chiou | d34f19f | 2010-12-30 10:41:27 +0000 | [diff] [blame] | 96 | switch (addressSpace) { |
| 97 | default: llvm_unreachable("Unknown state space"); |
| 98 | case PTX::GLOBAL: return "global"; |
| 99 | case PTX::CONSTANT: return "const"; |
| 100 | case PTX::LOCAL: return "local"; |
| 101 | case PTX::PARAMETER: return "param"; |
| 102 | case PTX::SHARED: return "shared"; |
| 103 | } |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 104 | return NULL; |
| 105 | } |
| 106 | |
Che-Liang Chiou | f717202 | 2011-02-28 06:34:09 +0000 | [diff] [blame] | 107 | static const char *getTypeName(const Type* type) { |
| 108 | while (true) { |
| 109 | switch (type->getTypeID()) { |
| 110 | default: llvm_unreachable("Unknown type"); |
| 111 | case Type::FloatTyID: return ".f32"; |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 112 | case Type::DoubleTyID: return ".f64"; |
| 113 | case Type::IntegerTyID: |
| 114 | switch (type->getPrimitiveSizeInBits()) { |
| 115 | default: llvm_unreachable("Unknown integer bit-width"); |
| 116 | case 16: return ".u16"; |
| 117 | case 32: return ".u32"; |
| 118 | case 64: return ".u64"; |
| 119 | } |
Che-Liang Chiou | f717202 | 2011-02-28 06:34:09 +0000 | [diff] [blame] | 120 | case Type::ArrayTyID: |
| 121 | case Type::PointerTyID: |
| 122 | type = dyn_cast<const SequentialType>(type)->getElementType(); |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | return NULL; |
| 127 | } |
| 128 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 129 | bool PTXAsmPrinter::doFinalization(Module &M) { |
| 130 | // XXX Temproarily remove global variables so that doFinalization() will not |
| 131 | // emit them again (global variables are emitted at beginning). |
| 132 | |
| 133 | Module::GlobalListType &global_list = M.getGlobalList(); |
| 134 | int i, n = global_list.size(); |
| 135 | GlobalVariable **gv_array = new GlobalVariable* [n]; |
| 136 | |
| 137 | // first, back-up GlobalVariable in gv_array |
| 138 | i = 0; |
| 139 | for (Module::global_iterator I = global_list.begin(), E = global_list.end(); |
| 140 | I != E; ++I) |
| 141 | gv_array[i++] = &*I; |
| 142 | |
| 143 | // second, empty global_list |
| 144 | while (!global_list.empty()) |
| 145 | global_list.remove(global_list.begin()); |
| 146 | |
| 147 | // call doFinalization |
| 148 | bool ret = AsmPrinter::doFinalization(M); |
| 149 | |
| 150 | // now we restore global variables |
| 151 | for (i = 0; i < n; i ++) |
| 152 | global_list.insert(global_list.end(), gv_array[i]); |
| 153 | |
| 154 | delete[] gv_array; |
| 155 | return ret; |
| 156 | } |
| 157 | |
Che-Liang Chiou | 21d8b9b | 2010-11-30 10:14:14 +0000 | [diff] [blame] | 158 | void PTXAsmPrinter::EmitStartOfAsmFile(Module &M) |
| 159 | { |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 160 | const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>(); |
| 161 | |
| 162 | OutStreamer.EmitRawText(Twine("\t.version " + ST.getPTXVersionString())); |
| 163 | OutStreamer.EmitRawText(Twine("\t.target " + ST.getTargetString() + |
Justin Holewinski | 12785e8 | 2011-03-03 13:34:29 +0000 | [diff] [blame] | 164 | (ST.supportsDouble() ? "" |
| 165 | : ", map_f64_to_f32"))); |
Justin Holewinski | a9c85f9 | 2011-06-22 00:43:56 +0000 | [diff] [blame] | 166 | // .address_size directive is optional, but it must immediately follow |
| 167 | // the .target directive if present within a module |
| 168 | if (ST.supportsPTX23()) { |
| 169 | std::string addrSize = ST.is64Bit() ? "64" : "32"; |
| 170 | OutStreamer.EmitRawText(Twine("\t.address_size " + addrSize)); |
| 171 | } |
| 172 | |
Che-Liang Chiou | 21d8b9b | 2010-11-30 10:14:14 +0000 | [diff] [blame] | 173 | OutStreamer.AddBlankLine(); |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 174 | |
| 175 | // declare global variables |
| 176 | for (Module::const_global_iterator i = M.global_begin(), e = M.global_end(); |
| 177 | i != e; ++i) |
| 178 | EmitVariableDeclaration(i); |
Che-Liang Chiou | 21d8b9b | 2010-11-30 10:14:14 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 181 | bool PTXAsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
| 182 | SetupMachineFunction(MF); |
| 183 | EmitFunctionDeclaration(); |
| 184 | EmitFunctionBody(); |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | void PTXAsmPrinter::EmitFunctionBodyStart() { |
| 189 | OutStreamer.EmitRawText(Twine("{")); |
| 190 | |
| 191 | const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>(); |
| 192 | |
| 193 | // Print local variable definition |
| 194 | for (PTXMachineFunctionInfo::reg_iterator |
| 195 | i = MFI->localVarRegBegin(), e = MFI->localVarRegEnd(); i != e; ++ i) { |
| 196 | unsigned reg = *i; |
| 197 | |
Che-Liang Chiou | 3f409f7 | 2010-11-17 08:08:49 +0000 | [diff] [blame] | 198 | std::string def = "\t.reg ."; |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 199 | def += getRegisterTypeName(reg); |
| 200 | def += ' '; |
| 201 | def += getRegisterName(reg); |
| 202 | def += ';'; |
| 203 | OutStreamer.EmitRawText(Twine(def)); |
| 204 | } |
Justin Holewinski | df1c8d8 | 2011-06-20 15:56:20 +0000 | [diff] [blame] | 205 | |
| 206 | const MachineFrameInfo* FrameInfo = MF->getFrameInfo(); |
Justin Holewinski | 08d0316 | 2011-06-22 16:07:03 +0000 | [diff] [blame^] | 207 | DEBUG(dbgs() << "Have " << FrameInfo->getNumObjects() |
| 208 | << " frame object(s)\n"); |
Justin Holewinski | df1c8d8 | 2011-06-20 15:56:20 +0000 | [diff] [blame] | 209 | for (unsigned i = 0, e = FrameInfo->getNumObjects(); i != e; ++i) { |
| 210 | DEBUG(dbgs() << "Size of object: " << FrameInfo->getObjectSize(i) << "\n"); |
Justin Holewinski | 08d0316 | 2011-06-22 16:07:03 +0000 | [diff] [blame^] | 211 | if (FrameInfo->getObjectSize(i) > 0) { |
| 212 | std::string def = "\t.reg .b"; |
| 213 | def += utostr(FrameInfo->getObjectSize(i)*8); // Convert to bits |
| 214 | def += " s"; |
| 215 | def += utostr(i); |
| 216 | def += ";"; |
| 217 | OutStreamer.EmitRawText(Twine(def)); |
| 218 | } |
Justin Holewinski | df1c8d8 | 2011-06-20 15:56:20 +0000 | [diff] [blame] | 219 | } |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 222 | void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) { |
Che-Liang Chiou | 3608d2a | 2010-12-01 11:45:53 +0000 | [diff] [blame] | 223 | std::string str; |
| 224 | str.reserve(64); |
| 225 | |
Che-Liang Chiou | 3608d2a | 2010-12-01 11:45:53 +0000 | [diff] [blame] | 226 | raw_string_ostream OS(str); |
Che-Liang Chiou | c2ec0f9 | 2011-03-13 17:26:00 +0000 | [diff] [blame] | 227 | |
| 228 | // Emit predicate |
| 229 | printPredicateOperand(MI, OS); |
| 230 | |
| 231 | // Write instruction to str |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 232 | printInstruction(MI, OS); |
| 233 | OS << ';'; |
Che-Liang Chiou | 3608d2a | 2010-12-01 11:45:53 +0000 | [diff] [blame] | 234 | OS.flush(); |
Che-Liang Chiou | 3f409f7 | 2010-11-17 08:08:49 +0000 | [diff] [blame] | 235 | |
Che-Liang Chiou | 3608d2a | 2010-12-01 11:45:53 +0000 | [diff] [blame] | 236 | StringRef strref = StringRef(str); |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 237 | OutStreamer.EmitRawText(strref); |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void PTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum, |
| 241 | raw_ostream &OS) { |
| 242 | const MachineOperand &MO = MI->getOperand(opNum); |
| 243 | |
| 244 | switch (MO.getType()) { |
| 245 | default: |
| 246 | llvm_unreachable("<unknown operand type>"); |
| 247 | break; |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 248 | case MachineOperand::MO_GlobalAddress: |
| 249 | OS << *Mang->getSymbol(MO.getGlobal()); |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 250 | break; |
| 251 | case MachineOperand::MO_Immediate: |
Justin Holewinski | 9583a86 | 2011-04-28 00:19:50 +0000 | [diff] [blame] | 252 | OS << (long) MO.getImm(); |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 253 | break; |
Che-Liang Chiou | 88d3367 | 2011-03-18 11:08:52 +0000 | [diff] [blame] | 254 | case MachineOperand::MO_MachineBasicBlock: |
| 255 | OS << *MO.getMBB()->getSymbol(); |
| 256 | break; |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 257 | case MachineOperand::MO_Register: |
| 258 | OS << getRegisterName(MO.getReg()); |
| 259 | break; |
Che-Liang Chiou | f717202 | 2011-02-28 06:34:09 +0000 | [diff] [blame] | 260 | case MachineOperand::MO_FPImmediate: |
| 261 | APInt constFP = MO.getFPImm()->getValueAPF().bitcastToAPInt(); |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 262 | bool isFloat = MO.getFPImm()->getType()->getTypeID() == Type::FloatTyID; |
| 263 | // Emit 0F for 32-bit floats and 0D for 64-bit doubles. |
| 264 | if (isFloat) { |
| 265 | OS << "0F"; |
Che-Liang Chiou | f717202 | 2011-02-28 06:34:09 +0000 | [diff] [blame] | 266 | } |
| 267 | else { |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 268 | OS << "0D"; |
| 269 | } |
| 270 | // Emit the encoded floating-point value. |
| 271 | if (constFP.getZExtValue() > 0) { |
| 272 | OS << constFP.toString(16, false); |
| 273 | } |
| 274 | else { |
| 275 | OS << "00000000"; |
| 276 | // If We have a double-precision zero, pad to 8-bytes. |
| 277 | if (!isFloat) { |
| 278 | OS << "00000000"; |
| 279 | } |
Che-Liang Chiou | f717202 | 2011-02-28 06:34:09 +0000 | [diff] [blame] | 280 | } |
| 281 | break; |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 282 | } |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 285 | void PTXAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum, |
| 286 | raw_ostream &OS, const char *Modifier) { |
| 287 | printOperand(MI, opNum, OS); |
| 288 | |
| 289 | if (MI->getOperand(opNum+1).isImm() && MI->getOperand(opNum+1).getImm() == 0) |
| 290 | return; // don't print "+0" |
| 291 | |
| 292 | OS << "+"; |
| 293 | printOperand(MI, opNum+1, OS); |
| 294 | } |
| 295 | |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 296 | void PTXAsmPrinter::printParamOperand(const MachineInstr *MI, int opNum, |
| 297 | raw_ostream &OS, const char *Modifier) { |
| 298 | OS << PARAM_PREFIX << (int) MI->getOperand(opNum).getImm() + 1; |
| 299 | } |
| 300 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 301 | void PTXAsmPrinter::EmitVariableDeclaration(const GlobalVariable *gv) { |
| 302 | // Check to see if this is a special global used by LLVM, if so, emit it. |
| 303 | if (EmitSpecialLLVMGlobal(gv)) |
| 304 | return; |
| 305 | |
| 306 | MCSymbol *gvsym = Mang->getSymbol(gv); |
| 307 | |
| 308 | assert(gvsym->isUndefined() && "Cannot define a symbol twice!"); |
| 309 | |
| 310 | std::string decl; |
| 311 | |
| 312 | // check if it is defined in some other translation unit |
| 313 | if (gv->isDeclaration()) |
| 314 | decl += ".extern "; |
| 315 | |
| 316 | // state space: e.g., .global |
| 317 | decl += "."; |
| 318 | decl += getStateSpaceName(gv->getType()->getAddressSpace()); |
| 319 | decl += " "; |
| 320 | |
| 321 | // alignment (optional) |
| 322 | unsigned alignment = gv->getAlignment(); |
| 323 | if (alignment != 0) { |
| 324 | decl += ".align "; |
| 325 | decl += utostr(Log2_32(gv->getAlignment())); |
| 326 | decl += " "; |
| 327 | } |
| 328 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 329 | |
Justin Holewinski | ae3ce17 | 2011-03-14 15:40:11 +0000 | [diff] [blame] | 330 | if (PointerType::classof(gv->getType())) { |
| 331 | const PointerType* pointerTy = dyn_cast<const PointerType>(gv->getType()); |
| 332 | const Type* elementTy = pointerTy->getElementType(); |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 333 | |
Justin Holewinski | ae3ce17 | 2011-03-14 15:40:11 +0000 | [diff] [blame] | 334 | decl += ".b8 "; |
| 335 | decl += gvsym->getName(); |
| 336 | decl += "["; |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 337 | |
Justin Holewinski | 9583a86 | 2011-04-28 00:19:50 +0000 | [diff] [blame] | 338 | if (elementTy->isArrayTy()) |
| 339 | { |
| 340 | assert(elementTy->isArrayTy() && "Only pointers to arrays are supported"); |
| 341 | |
| 342 | const ArrayType* arrayTy = dyn_cast<const ArrayType>(elementTy); |
| 343 | elementTy = arrayTy->getElementType(); |
| 344 | |
| 345 | unsigned numElements = arrayTy->getNumElements(); |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 346 | |
Justin Holewinski | 9583a86 | 2011-04-28 00:19:50 +0000 | [diff] [blame] | 347 | while (elementTy->isArrayTy()) { |
| 348 | |
| 349 | arrayTy = dyn_cast<const ArrayType>(elementTy); |
| 350 | elementTy = arrayTy->getElementType(); |
| 351 | |
| 352 | numElements *= arrayTy->getNumElements(); |
| 353 | } |
| 354 | |
| 355 | // FIXME: isPrimitiveType() == false for i16? |
| 356 | assert(elementTy->isSingleValueType() && |
| 357 | "Non-primitive types are not handled"); |
| 358 | |
| 359 | // Compute the size of the array, in bytes. |
| 360 | uint64_t arraySize = (elementTy->getPrimitiveSizeInBits() >> 3) |
| 361 | * numElements; |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 362 | |
Justin Holewinski | 9583a86 | 2011-04-28 00:19:50 +0000 | [diff] [blame] | 363 | decl += utostr(arraySize); |
| 364 | } |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 365 | |
Justin Holewinski | ae3ce17 | 2011-03-14 15:40:11 +0000 | [diff] [blame] | 366 | decl += "]"; |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 367 | |
Justin Holewinski | 9583a86 | 2011-04-28 00:19:50 +0000 | [diff] [blame] | 368 | // handle string constants (assume ConstantArray means string) |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 369 | |
Justin Holewinski | 9583a86 | 2011-04-28 00:19:50 +0000 | [diff] [blame] | 370 | if (gv->hasInitializer()) |
| 371 | { |
Jay Foad | 7d715df | 2011-06-19 18:37:11 +0000 | [diff] [blame] | 372 | const Constant *C = gv->getInitializer(); |
Justin Holewinski | 9583a86 | 2011-04-28 00:19:50 +0000 | [diff] [blame] | 373 | if (const ConstantArray *CA = dyn_cast<ConstantArray>(C)) |
| 374 | { |
| 375 | decl += " = {"; |
| 376 | |
| 377 | for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) |
| 378 | { |
| 379 | if (i > 0) decl += ","; |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 380 | |
| 381 | decl += "0x" + |
| 382 | utohexstr(cast<ConstantInt>(CA->getOperand(i))->getZExtValue()); |
Justin Holewinski | 9583a86 | 2011-04-28 00:19:50 +0000 | [diff] [blame] | 383 | } |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 384 | |
Justin Holewinski | 9583a86 | 2011-04-28 00:19:50 +0000 | [diff] [blame] | 385 | decl += "}"; |
| 386 | } |
| 387 | } |
Justin Holewinski | ae3ce17 | 2011-03-14 15:40:11 +0000 | [diff] [blame] | 388 | } |
| 389 | else { |
| 390 | // Note: this is currently the fall-through case and most likely generates |
| 391 | // incorrect code. |
| 392 | decl += getTypeName(gv->getType()); |
| 393 | decl += " "; |
| 394 | |
| 395 | decl += gvsym->getName(); |
| 396 | |
| 397 | if (ArrayType::classof(gv->getType()) || |
| 398 | PointerType::classof(gv->getType())) |
| 399 | decl += "[]"; |
| 400 | } |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 401 | |
| 402 | decl += ";"; |
| 403 | |
| 404 | OutStreamer.EmitRawText(Twine(decl)); |
| 405 | |
| 406 | OutStreamer.AddBlankLine(); |
| 407 | } |
| 408 | |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 409 | void PTXAsmPrinter::EmitFunctionDeclaration() { |
| 410 | // The function label could have already been emitted if two symbols end up |
| 411 | // conflicting due to asm renaming. Detect this and emit an error. |
| 412 | if (!CurrentFnSym->isUndefined()) { |
| 413 | report_fatal_error("'" + Twine(CurrentFnSym->getName()) + |
| 414 | "' label emitted multiple times to assembly file"); |
| 415 | return; |
| 416 | } |
| 417 | |
| 418 | const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>(); |
| 419 | const bool isKernel = MFI->isKernel(); |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 420 | |
| 421 | std::string decl = isKernel ? ".entry" : ".func"; |
| 422 | |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 423 | if (!isKernel) { |
| 424 | decl += " ("; |
| 425 | |
| 426 | for (PTXMachineFunctionInfo::ret_iterator |
| 427 | i = MFI->retRegBegin(), e = MFI->retRegEnd(), b = i; |
| 428 | i != e; ++i) { |
| 429 | if (i != b) { |
| 430 | decl += ", "; |
| 431 | } |
| 432 | decl += ".reg ."; |
| 433 | decl += getRegisterTypeName(*i); |
| 434 | decl += " "; |
| 435 | decl += getRegisterName(*i); |
| 436 | } |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 437 | decl += ")"; |
| 438 | } |
| 439 | |
| 440 | // Print function name |
| 441 | decl += " "; |
| 442 | decl += CurrentFnSym->getName().str(); |
| 443 | |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 444 | decl += " ("; |
| 445 | |
| 446 | unsigned cnt = 0; |
| 447 | |
| 448 | // Print parameters |
| 449 | for (PTXMachineFunctionInfo::reg_iterator |
| 450 | i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i; |
| 451 | i != e; ++i) { |
| 452 | if (i != b) { |
| 453 | decl += ", "; |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 454 | } |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 455 | if (isKernel) { |
| 456 | decl += ".param .b"; |
| 457 | decl += utostr(*i); |
| 458 | decl += " "; |
| 459 | decl += PARAM_PREFIX; |
| 460 | decl += utostr(++cnt); |
| 461 | } else { |
| 462 | decl += ".reg ."; |
| 463 | decl += getRegisterTypeName(*i); |
| 464 | decl += " "; |
| 465 | decl += getRegisterName(*i); |
| 466 | } |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 467 | } |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 468 | decl += ")"; |
| 469 | |
| 470 | // // Print parameter list |
| 471 | // if (!MFI->argRegEmpty()) { |
| 472 | // decl += " ("; |
| 473 | // if (isKernel) { |
| 474 | // unsigned cnt = 0; |
| 475 | // for(PTXMachineFunctionInfo::reg_iterator |
| 476 | // i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i; |
| 477 | // i != e; ++i) { |
| 478 | // reg = *i; |
| 479 | // assert(reg != PTX::NoRegister && "Not a valid register!"); |
| 480 | // if (i != b) |
| 481 | // decl += ", "; |
| 482 | // decl += ".param ."; |
| 483 | // decl += getRegisterTypeName(reg); |
| 484 | // decl += " "; |
| 485 | // decl += PARAM_PREFIX; |
| 486 | // decl += utostr(++cnt); |
| 487 | // } |
| 488 | // } else { |
| 489 | // for (PTXMachineFunctionInfo::reg_iterator |
| 490 | // i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i; |
| 491 | // i != e; ++i) { |
| 492 | // reg = *i; |
| 493 | // assert(reg != PTX::NoRegister && "Not a valid register!"); |
| 494 | // if (i != b) |
| 495 | // decl += ", "; |
| 496 | // decl += ".reg ."; |
| 497 | // decl += getRegisterTypeName(reg); |
| 498 | // decl += " "; |
| 499 | // decl += getRegisterName(reg); |
| 500 | // } |
| 501 | // } |
| 502 | // decl += ")"; |
| 503 | // } |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 504 | |
| 505 | OutStreamer.EmitRawText(Twine(decl)); |
| 506 | } |
| 507 | |
Che-Liang Chiou | c2ec0f9 | 2011-03-13 17:26:00 +0000 | [diff] [blame] | 508 | void PTXAsmPrinter:: |
| 509 | printPredicateOperand(const MachineInstr *MI, raw_ostream &O) { |
| 510 | int i = MI->findFirstPredOperandIdx(); |
| 511 | if (i == -1) |
| 512 | llvm_unreachable("missing predicate operand"); |
| 513 | |
| 514 | unsigned reg = MI->getOperand(i).getReg(); |
| 515 | int predOp = MI->getOperand(i+1).getImm(); |
| 516 | |
| 517 | DEBUG(dbgs() << "predicate: (" << reg << ", " << predOp << ")\n"); |
| 518 | |
Che-Liang Chiou | f78847e | 2011-03-14 11:26:01 +0000 | [diff] [blame] | 519 | if (reg != PTX::NoRegister) { |
Che-Liang Chiou | c2ec0f9 | 2011-03-13 17:26:00 +0000 | [diff] [blame] | 520 | O << '@'; |
| 521 | if (predOp == PTX::PRED_NEGATE) |
| 522 | O << '!'; |
| 523 | O << getRegisterName(reg); |
| 524 | } |
| 525 | } |
| 526 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 527 | #include "PTXGenAsmWriter.inc" |
| 528 | |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 529 | // Force static initialization. |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 530 | extern "C" void LLVMInitializePTXAsmPrinter() { |
Justin Holewinski | e1fee48 | 2011-04-20 15:37:17 +0000 | [diff] [blame] | 531 | RegisterAsmPrinter<PTXAsmPrinter> X(ThePTX32Target); |
| 532 | RegisterAsmPrinter<PTXAsmPrinter> Y(ThePTX64Target); |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 533 | } |