Che-Liang Chiou | d77f2a4 | 2010-11-08 02:58:44 +0000 | [diff] [blame^] | 1 | //===- lib/Target/PTX/PTXMCAsmStreamer.cpp - PTX Text Assembly Output -----===// |
| 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 | #include "PTXMCAsmStreamer.h" |
| 11 | #include "llvm/MC/MCAsmInfo.h" |
| 12 | #include "llvm/MC/MCExpr.h" |
| 13 | #include "llvm/MC/MCInst.h" |
| 14 | #include "llvm/MC/MCSymbol.h" |
| 15 | #include "llvm/ADT/Twine.h" |
| 16 | #include "llvm/Support/ErrorHandling.h" |
| 17 | #include "llvm/Support/MathExtras.h" |
| 18 | #include "llvm/Support/Format.h" |
| 19 | |
| 20 | using namespace llvm; |
| 21 | |
| 22 | /// TODO: Add appropriate implementation of Emit*() methods when needed |
| 23 | |
| 24 | void PTXMCAsmStreamer::AddComment(const Twine &T) { |
| 25 | if (!IsVerboseAsm) return; |
| 26 | |
| 27 | // Make sure that CommentStream is flushed. |
| 28 | CommentStream.flush(); |
| 29 | |
| 30 | T.toVector(CommentToEmit); |
| 31 | // Each comment goes on its own line. |
| 32 | CommentToEmit.push_back('\n'); |
| 33 | |
| 34 | // Tell the comment stream that the vector changed underneath it. |
| 35 | CommentStream.resync(); |
| 36 | } |
| 37 | |
| 38 | void PTXMCAsmStreamer::EmitCommentsAndEOL() { |
| 39 | if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) { |
| 40 | OS << '\n'; |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | CommentStream.flush(); |
| 45 | StringRef Comments = CommentToEmit.str(); |
| 46 | |
| 47 | assert(Comments.back() == '\n' && |
| 48 | "Comment array not newline terminated"); |
| 49 | do { |
| 50 | // Emit a line of comments. |
| 51 | OS.PadToColumn(MAI.getCommentColumn()); |
| 52 | size_t Position = Comments.find('\n'); |
| 53 | OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n'; |
| 54 | |
| 55 | Comments = Comments.substr(Position+1); |
| 56 | } while (!Comments.empty()); |
| 57 | |
| 58 | CommentToEmit.clear(); |
| 59 | // Tell the comment stream that the vector changed underneath it. |
| 60 | CommentStream.resync(); |
| 61 | } |
| 62 | |
| 63 | static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) { |
| 64 | assert(Bytes && "Invalid size!"); |
| 65 | return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8)); |
| 66 | } |
| 67 | |
| 68 | void PTXMCAsmStreamer::SwitchSection(const MCSection *Section) { |
| 69 | assert(Section && "Cannot switch to a null section!"); |
| 70 | if (Section != CurSection) { |
| 71 | PrevSection = CurSection; |
| 72 | CurSection = Section; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void PTXMCAsmStreamer::EmitLabel(MCSymbol *Symbol) { |
| 77 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
| 78 | assert(!Symbol->isVariable() && "Cannot emit a variable symbol!"); |
| 79 | assert(CurSection && "Cannot emit before setting section!"); |
| 80 | |
| 81 | OS << *Symbol << MAI.getLabelSuffix(); |
| 82 | EmitEOL(); |
| 83 | Symbol->setSection(*CurSection); |
| 84 | } |
| 85 | |
| 86 | void PTXMCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {} |
| 87 | |
| 88 | void PTXMCAsmStreamer::EmitThumbFunc(MCSymbol *Func) {} |
| 89 | |
| 90 | void PTXMCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) { |
| 91 | OS << *Symbol << " = " << *Value; |
| 92 | EmitEOL(); |
| 93 | |
| 94 | // FIXME: Lift context changes into super class. |
| 95 | Symbol->setVariableValue(Value); |
| 96 | } |
| 97 | |
| 98 | void PTXMCAsmStreamer::EmitWeakReference(MCSymbol *Alias, |
| 99 | const MCSymbol *Symbol) { |
| 100 | OS << ".weakref " << *Alias << ", " << *Symbol; |
| 101 | EmitEOL(); |
| 102 | } |
| 103 | |
| 104 | void PTXMCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol, |
| 105 | MCSymbolAttr Attribute) {} |
| 106 | |
| 107 | void PTXMCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {} |
| 108 | |
| 109 | void PTXMCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {} |
| 110 | |
| 111 | void PTXMCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {} |
| 112 | |
| 113 | void PTXMCAsmStreamer::EmitCOFFSymbolType (int Type) {} |
| 114 | |
| 115 | void PTXMCAsmStreamer::EndCOFFSymbolDef() {} |
| 116 | |
| 117 | void PTXMCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {} |
| 118 | |
| 119 | void PTXMCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
| 120 | unsigned ByteAlignment) {} |
| 121 | |
| 122 | void PTXMCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {} |
| 123 | |
| 124 | void PTXMCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol, |
| 125 | unsigned Size, unsigned ByteAlignment) {} |
| 126 | |
| 127 | void PTXMCAsmStreamer::EmitTBSSSymbol(const MCSection *Section, |
| 128 | MCSymbol *Symbol, |
| 129 | uint64_t Size, unsigned ByteAlignment) {} |
| 130 | |
| 131 | static inline char toOctal(int X) { return (X&7)+'0'; } |
| 132 | |
| 133 | static void PrintQuotedString(StringRef Data, raw_ostream &OS) { |
| 134 | OS << '"'; |
| 135 | |
| 136 | for (unsigned i = 0, e = Data.size(); i != e; ++i) { |
| 137 | unsigned char C = Data[i]; |
| 138 | if (C == '"' || C == '\\') { |
| 139 | OS << '\\' << (char)C; |
| 140 | continue; |
| 141 | } |
| 142 | |
| 143 | if (isprint((unsigned char)C)) { |
| 144 | OS << (char)C; |
| 145 | continue; |
| 146 | } |
| 147 | |
| 148 | switch (C) { |
| 149 | case '\b': OS << "\\b"; break; |
| 150 | case '\f': OS << "\\f"; break; |
| 151 | case '\n': OS << "\\n"; break; |
| 152 | case '\r': OS << "\\r"; break; |
| 153 | case '\t': OS << "\\t"; break; |
| 154 | default: |
| 155 | OS << '\\'; |
| 156 | OS << toOctal(C >> 6); |
| 157 | OS << toOctal(C >> 3); |
| 158 | OS << toOctal(C >> 0); |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | OS << '"'; |
| 164 | } |
| 165 | |
| 166 | void PTXMCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) { |
| 167 | assert(CurSection && "Cannot emit contents before setting section!"); |
| 168 | if (Data.empty()) return; |
| 169 | |
| 170 | if (Data.size() == 1) { |
| 171 | OS << MAI.getData8bitsDirective(AddrSpace); |
| 172 | OS << (unsigned)(unsigned char)Data[0]; |
| 173 | EmitEOL(); |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | // If the data ends with 0 and the target supports .asciz, use it, otherwise |
| 178 | // use .ascii |
| 179 | if (MAI.getAscizDirective() && Data.back() == 0) { |
| 180 | OS << MAI.getAscizDirective(); |
| 181 | Data = Data.substr(0, Data.size()-1); |
| 182 | } else { |
| 183 | OS << MAI.getAsciiDirective(); |
| 184 | } |
| 185 | |
| 186 | OS << ' '; |
| 187 | PrintQuotedString(Data, OS); |
| 188 | EmitEOL(); |
| 189 | } |
| 190 | |
| 191 | /// EmitIntValue - Special case of EmitValue that avoids the client having |
| 192 | /// to pass in a MCExpr for constant integers. |
| 193 | void PTXMCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size, |
| 194 | unsigned AddrSpace) { |
| 195 | assert(CurSection && "Cannot emit contents before setting section!"); |
| 196 | const char *Directive = 0; |
| 197 | switch (Size) { |
| 198 | default: break; |
| 199 | case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break; |
| 200 | case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break; |
| 201 | case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break; |
| 202 | case 8: |
| 203 | Directive = MAI.getData64bitsDirective(AddrSpace); |
| 204 | // If the target doesn't support 64-bit data, emit as two 32-bit halves. |
| 205 | if (Directive) break; |
| 206 | if (isLittleEndian()) { |
| 207 | EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace); |
| 208 | EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace); |
| 209 | } else { |
| 210 | EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace); |
| 211 | EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace); |
| 212 | } |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | assert(Directive && "Invalid size for machine code value!"); |
| 217 | OS << Directive << truncateToSize(Value, Size); |
| 218 | EmitEOL(); |
| 219 | } |
| 220 | |
| 221 | void PTXMCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size, |
| 222 | unsigned AddrSpace) { |
| 223 | assert(CurSection && "Cannot emit contents before setting section!"); |
| 224 | const char *Directive = 0; |
| 225 | switch (Size) { |
| 226 | default: break; |
| 227 | case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break; |
| 228 | case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break; |
| 229 | case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break; |
| 230 | case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break; |
| 231 | } |
| 232 | |
| 233 | assert(Directive && "Invalid size for machine code value!"); |
| 234 | OS << Directive << *Value; |
| 235 | EmitEOL(); |
| 236 | } |
| 237 | |
| 238 | void PTXMCAsmStreamer::EmitULEB128Value(const MCExpr *Value, |
| 239 | unsigned AddrSpace) { |
| 240 | assert(MAI.hasLEB128() && "Cannot print a .uleb"); |
| 241 | OS << ".uleb128 " << *Value; |
| 242 | EmitEOL(); |
| 243 | } |
| 244 | |
| 245 | void PTXMCAsmStreamer::EmitSLEB128Value(const MCExpr *Value, |
| 246 | unsigned AddrSpace) { |
| 247 | assert(MAI.hasLEB128() && "Cannot print a .sleb"); |
| 248 | OS << ".sleb128 " << *Value; |
| 249 | EmitEOL(); |
| 250 | } |
| 251 | |
| 252 | void PTXMCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) { |
| 253 | assert(MAI.getGPRel32Directive() != 0); |
| 254 | OS << MAI.getGPRel32Directive() << *Value; |
| 255 | EmitEOL(); |
| 256 | } |
| 257 | |
| 258 | |
| 259 | /// EmitFill - Emit NumBytes bytes worth of the value specified by |
| 260 | /// FillValue. This implements directives such as '.space'. |
| 261 | void PTXMCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue, |
| 262 | unsigned AddrSpace) { |
| 263 | if (NumBytes == 0) return; |
| 264 | |
| 265 | if (AddrSpace == 0) |
| 266 | if (const char *ZeroDirective = MAI.getZeroDirective()) { |
| 267 | OS << ZeroDirective << NumBytes; |
| 268 | if (FillValue != 0) |
| 269 | OS << ',' << (int)FillValue; |
| 270 | EmitEOL(); |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | // Emit a byte at a time. |
| 275 | MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace); |
| 276 | } |
| 277 | |
| 278 | void PTXMCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value, |
| 279 | unsigned ValueSize, |
| 280 | unsigned MaxBytesToEmit) { |
| 281 | // Some assemblers don't support non-power of two alignments, so we always |
| 282 | // emit alignments as a power of two if possible. |
| 283 | if (isPowerOf2_32(ByteAlignment)) { |
| 284 | switch (ValueSize) { |
| 285 | default: llvm_unreachable("Invalid size for machine code value!"); |
| 286 | case 1: OS << MAI.getAlignDirective(); break; |
| 287 | // FIXME: use MAI for this! |
| 288 | case 2: OS << ".p2alignw "; break; |
| 289 | case 4: OS << ".p2alignl "; break; |
| 290 | case 8: llvm_unreachable("Unsupported alignment size!"); |
| 291 | } |
| 292 | |
| 293 | if (MAI.getAlignmentIsInBytes()) |
| 294 | OS << ByteAlignment; |
| 295 | else |
| 296 | OS << Log2_32(ByteAlignment); |
| 297 | |
| 298 | if (Value || MaxBytesToEmit) { |
| 299 | OS << ", 0x"; |
| 300 | OS.write_hex(truncateToSize(Value, ValueSize)); |
| 301 | |
| 302 | if (MaxBytesToEmit) |
| 303 | OS << ", " << MaxBytesToEmit; |
| 304 | } |
| 305 | EmitEOL(); |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | // Non-power of two alignment. This is not widely supported by assemblers. |
| 310 | // FIXME: Parameterize this based on MAI. |
| 311 | switch (ValueSize) { |
| 312 | default: llvm_unreachable("Invalid size for machine code value!"); |
| 313 | case 1: OS << ".balign"; break; |
| 314 | case 2: OS << ".balignw"; break; |
| 315 | case 4: OS << ".balignl"; break; |
| 316 | case 8: llvm_unreachable("Unsupported alignment size!"); |
| 317 | } |
| 318 | |
| 319 | OS << ' ' << ByteAlignment; |
| 320 | OS << ", " << truncateToSize(Value, ValueSize); |
| 321 | if (MaxBytesToEmit) |
| 322 | OS << ", " << MaxBytesToEmit; |
| 323 | EmitEOL(); |
| 324 | } |
| 325 | |
| 326 | void PTXMCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment, |
| 327 | unsigned MaxBytesToEmit) {} |
| 328 | |
| 329 | void PTXMCAsmStreamer::EmitValueToOffset(const MCExpr *Offset, |
| 330 | unsigned char Value) {} |
| 331 | |
| 332 | |
| 333 | void PTXMCAsmStreamer::EmitFileDirective(StringRef Filename) { |
| 334 | assert(MAI.hasSingleParameterDotFile()); |
| 335 | OS << "\t.file\t"; |
| 336 | PrintQuotedString(Filename, OS); |
| 337 | EmitEOL(); |
| 338 | } |
| 339 | |
| 340 | void PTXMCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, |
| 341 | StringRef Filename){ |
| 342 | OS << "\t.file\t" << FileNo << ' '; |
| 343 | PrintQuotedString(Filename, OS); |
| 344 | EmitEOL(); |
| 345 | } |
| 346 | |
| 347 | void PTXMCAsmStreamer::AddEncodingComment(const MCInst &Inst) {} |
| 348 | |
| 349 | void PTXMCAsmStreamer::EmitInstruction(const MCInst &Inst) { |
| 350 | assert(CurSection && "Cannot emit contents before setting section!"); |
| 351 | |
| 352 | // Show the encoding in a comment if we have a code emitter. |
| 353 | if (Emitter) |
| 354 | AddEncodingComment(Inst); |
| 355 | |
| 356 | // Show the MCInst if enabled. |
| 357 | if (ShowInst) { |
| 358 | Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n "); |
| 359 | GetCommentOS() << "\n"; |
| 360 | } |
| 361 | |
| 362 | // If we have an AsmPrinter, use that to print, otherwise print the MCInst. |
| 363 | if (InstPrinter) |
| 364 | InstPrinter->printInst(&Inst, OS); |
| 365 | else |
| 366 | Inst.print(OS, &MAI); |
| 367 | EmitEOL(); |
| 368 | } |
| 369 | |
| 370 | /// EmitRawText - If this file is backed by an assembly streamer, this dumps |
| 371 | /// the specified string in the output .s file. This capability is |
| 372 | /// indicated by the hasRawTextSupport() predicate. |
| 373 | void PTXMCAsmStreamer::EmitRawText(StringRef String) { |
| 374 | if (!String.empty() && String.back() == '\n') |
| 375 | String = String.substr(0, String.size()-1); |
| 376 | OS << String; |
| 377 | EmitEOL(); |
| 378 | } |
| 379 | |
| 380 | void PTXMCAsmStreamer::Finish() {} |
| 381 | |
| 382 | MCStreamer *llvm::createPTXAsmStreamer(MCContext &Context, |
| 383 | formatted_raw_ostream &OS, |
| 384 | bool isLittleEndian, |
| 385 | bool isVerboseAsm, MCInstPrinter *IP, |
| 386 | MCCodeEmitter *CE, bool ShowInst) { |
| 387 | return new PTXMCAsmStreamer(Context, OS, isLittleEndian, isVerboseAsm, |
| 388 | IP, CE, ShowInst); |
| 389 | } |