1 //===- lib/Target/PTX/PTXMCAsmStreamer.cpp - PTX Text Assembly Output -----===//
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 #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"
22 /// TODO: Add appropriate implementation of Emit*() methods when needed
24 void PTXMCAsmStreamer::AddComment(const Twine &T) {
25 if (!IsVerboseAsm) return;
27 // Make sure that CommentStream is flushed.
28 CommentStream.flush();
30 T.toVector(CommentToEmit);
31 // Each comment goes on its own line.
32 CommentToEmit.push_back('\n');
34 // Tell the comment stream that the vector changed underneath it.
35 CommentStream.resync();
38 void PTXMCAsmStreamer::EmitCommentsAndEOL() {
39 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
44 CommentStream.flush();
45 StringRef Comments = CommentToEmit.str();
47 assert(Comments.back() == '\n' &&
48 "Comment array not newline terminated");
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';
55 Comments = Comments.substr(Position+1);
56 } while (!Comments.empty());
58 CommentToEmit.clear();
59 // Tell the comment stream that the vector changed underneath it.
60 CommentStream.resync();
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));
68 void PTXMCAsmStreamer::SwitchSection(const MCSection *Section) {
69 assert(Section && "Cannot switch to a null section!");
70 if (Section != CurSection) {
71 PrevSection = CurSection;
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!");
81 OS << *Symbol << MAI.getLabelSuffix();
83 Symbol->setSection(*CurSection);
86 void PTXMCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {}
88 void PTXMCAsmStreamer::EmitThumbFunc(MCSymbol *Func) {}
90 void PTXMCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
91 OS << *Symbol << " = " << *Value;
94 // FIXME: Lift context changes into super class.
95 Symbol->setVariableValue(Value);
98 void PTXMCAsmStreamer::EmitWeakReference(MCSymbol *Alias,
99 const MCSymbol *Symbol) {
100 OS << ".weakref " << *Alias << ", " << *Symbol;
104 void PTXMCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
105 MCSymbolAttr Attribute) {}
107 void PTXMCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {}
109 void PTXMCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {}
111 void PTXMCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {}
113 void PTXMCAsmStreamer::EmitCOFFSymbolType (int Type) {}
115 void PTXMCAsmStreamer::EndCOFFSymbolDef() {}
117 void PTXMCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {}
119 void PTXMCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
120 unsigned ByteAlignment) {}
122 void PTXMCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {}
124 void PTXMCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
125 unsigned Size, unsigned ByteAlignment) {}
127 void PTXMCAsmStreamer::EmitTBSSSymbol(const MCSection *Section,
129 uint64_t Size, unsigned ByteAlignment) {}
131 static inline char toOctal(int X) { return (X&7)+'0'; }
133 static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
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;
143 if (isprint((unsigned char)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;
156 OS << toOctal(C >> 6);
157 OS << toOctal(C >> 3);
158 OS << toOctal(C >> 0);
166 void PTXMCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
167 assert(CurSection && "Cannot emit contents before setting section!");
168 if (Data.empty()) return;
170 if (Data.size() == 1) {
171 OS << MAI.getData8bitsDirective(AddrSpace);
172 OS << (unsigned)(unsigned char)Data[0];
177 // If the data ends with 0 and the target supports .asciz, use it, otherwise
179 if (MAI.getAscizDirective() && Data.back() == 0) {
180 OS << MAI.getAscizDirective();
181 Data = Data.substr(0, Data.size()-1);
183 OS << MAI.getAsciiDirective();
187 PrintQuotedString(Data, OS);
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;
199 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
200 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
201 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
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);
210 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
211 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
216 assert(Directive && "Invalid size for machine code value!");
217 OS << Directive << truncateToSize(Value, Size);
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;
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;
233 assert(Directive && "Invalid size for machine code value!");
234 OS << Directive << *Value;
238 void PTXMCAsmStreamer::EmitULEB128Value(const MCExpr *Value,
239 unsigned AddrSpace) {
240 assert(MAI.hasLEB128() && "Cannot print a .uleb");
241 OS << ".uleb128 " << *Value;
245 void PTXMCAsmStreamer::EmitSLEB128Value(const MCExpr *Value,
246 unsigned AddrSpace) {
247 assert(MAI.hasLEB128() && "Cannot print a .sleb");
248 OS << ".sleb128 " << *Value;
252 void PTXMCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
253 assert(MAI.getGPRel32Directive() != 0);
254 OS << MAI.getGPRel32Directive() << *Value;
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;
266 if (const char *ZeroDirective = MAI.getZeroDirective()) {
267 OS << ZeroDirective << NumBytes;
269 OS << ',' << (int)FillValue;
274 // Emit a byte at a time.
275 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
278 void PTXMCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
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)) {
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!");
293 if (MAI.getAlignmentIsInBytes())
296 OS << Log2_32(ByteAlignment);
298 if (Value || MaxBytesToEmit) {
300 OS.write_hex(truncateToSize(Value, ValueSize));
303 OS << ", " << MaxBytesToEmit;
309 // Non-power of two alignment. This is not widely supported by assemblers.
310 // FIXME: Parameterize this based on MAI.
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!");
319 OS << ' ' << ByteAlignment;
320 OS << ", " << truncateToSize(Value, ValueSize);
322 OS << ", " << MaxBytesToEmit;
326 void PTXMCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
327 unsigned MaxBytesToEmit) {}
329 void PTXMCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
330 unsigned char Value) {}
333 void PTXMCAsmStreamer::EmitFileDirective(StringRef Filename) {
334 assert(MAI.hasSingleParameterDotFile());
336 PrintQuotedString(Filename, OS);
340 // FIXME: should we inherit from MCAsmStreamer?
341 bool PTXMCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo,
343 OS << "\t.file\t" << FileNo << ' ';
344 PrintQuotedString(Filename, OS);
346 return this->MCStreamer::EmitDwarfFileDirective(FileNo, Filename);
349 void PTXMCAsmStreamer::AddEncodingComment(const MCInst &Inst) {}
351 void PTXMCAsmStreamer::EmitInstruction(const MCInst &Inst) {
352 assert(CurSection && "Cannot emit contents before setting section!");
354 // Show the encoding in a comment if we have a code emitter.
356 AddEncodingComment(Inst);
358 // Show the MCInst if enabled.
360 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n ");
361 GetCommentOS() << "\n";
364 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
366 InstPrinter->printInst(&Inst, OS);
368 Inst.print(OS, &MAI);
372 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
373 /// the specified string in the output .s file. This capability is
374 /// indicated by the hasRawTextSupport() predicate.
375 void PTXMCAsmStreamer::EmitRawText(StringRef String) {
376 if (!String.empty() && String.back() == '\n')
377 String = String.substr(0, String.size()-1);
382 void PTXMCAsmStreamer::Finish() {}
384 MCStreamer *llvm::createPTXAsmStreamer(MCContext &Context,
385 formatted_raw_ostream &OS,
387 bool isVerboseAsm, MCInstPrinter *IP,
388 MCCodeEmitter *CE, bool ShowInst) {
389 return new PTXMCAsmStreamer(Context, OS, isLittleEndian, isVerboseAsm,