blob: 0b49bcc43f6266b41bdec43ac8161cd7bb09ac63 [file] [log] [blame]
Che-Liang Chioud77f2a42010-11-08 02:58:44 +00001//===- 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
Rafael Espindolaa484f2c2010-11-28 14:48:34 +000010#include "llvm/ADT/OwningPtr.h"
11#include "llvm/ADT/SmallString.h"
12#include "llvm/ADT/Twine.h"
Che-Liang Chioud77f2a42010-11-08 02:58:44 +000013#include "llvm/MC/MCAsmInfo.h"
Rafael Espindolaa484f2c2010-11-28 14:48:34 +000014#include "llvm/MC/MCContext.h"
15#include "llvm/MC/MCCodeEmitter.h"
Che-Liang Chioud77f2a42010-11-08 02:58:44 +000016#include "llvm/MC/MCExpr.h"
17#include "llvm/MC/MCInst.h"
Rafael Espindolaa484f2c2010-11-28 14:48:34 +000018#include "llvm/MC/MCInstPrinter.h"
19#include "llvm/MC/MCStreamer.h"
Che-Liang Chioud77f2a42010-11-08 02:58:44 +000020#include "llvm/MC/MCSymbol.h"
Che-Liang Chioud77f2a42010-11-08 02:58:44 +000021#include "llvm/Support/ErrorHandling.h"
22#include "llvm/Support/MathExtras.h"
23#include "llvm/Support/Format.h"
Rafael Espindolaa484f2c2010-11-28 14:48:34 +000024#include "llvm/Support/FormattedStream.h"
25#include "llvm/Support/raw_ostream.h"
Che-Liang Chioud77f2a42010-11-08 02:58:44 +000026
27using namespace llvm;
28
Rafael Espindolaa484f2c2010-11-28 14:48:34 +000029namespace {
30class PTXMCAsmStreamer : public MCStreamer {
31 formatted_raw_ostream &OS;
32 const MCAsmInfo &MAI;
33 OwningPtr<MCInstPrinter> InstPrinter;
34 OwningPtr<MCCodeEmitter> Emitter;
35
36 SmallString<128> CommentToEmit;
37 raw_svector_ostream CommentStream;
38
39 unsigned IsLittleEndian : 1;
40 unsigned IsVerboseAsm : 1;
41 unsigned ShowInst : 1;
42
43public:
44 PTXMCAsmStreamer(MCContext &Context,
45 formatted_raw_ostream &os,
46 bool isLittleEndian,
47 bool isVerboseAsm,
48 MCInstPrinter *printer,
49 MCCodeEmitter *emitter,
50 bool showInst)
51 : MCStreamer(Context), OS(os), MAI(Context.getAsmInfo()),
52 InstPrinter(printer), Emitter(emitter), CommentStream(CommentToEmit),
53 IsLittleEndian(isLittleEndian), IsVerboseAsm(isVerboseAsm),
54 ShowInst(showInst) {
55 if (InstPrinter && IsVerboseAsm)
56 InstPrinter->setCommentStream(CommentStream);
57 }
58
59 ~PTXMCAsmStreamer() {}
60
61 bool isLittleEndian() const { return IsLittleEndian; }
62
63 inline void EmitEOL() {
64 // If we don't have any comments, just emit a \n.
65 if (!IsVerboseAsm) {
66 OS << '\n';
67 return;
68 }
69 EmitCommentsAndEOL();
70 }
71 void EmitCommentsAndEOL();
72
73 /// isVerboseAsm - Return true if this streamer supports verbose assembly at
74 /// all.
75 virtual bool isVerboseAsm() const { return IsVerboseAsm; }
76
77 /// hasRawTextSupport - We support EmitRawText.
78 virtual bool hasRawTextSupport() const { return true; }
79
80 /// AddComment - Add a comment that can be emitted to the generated .s
81 /// file if applicable as a QoI issue to make the output of the compiler
82 /// more readable. This only affects the MCAsmStreamer, and only when
83 /// verbose assembly output is enabled.
84 virtual void AddComment(const Twine &T);
85
86 /// AddEncodingComment - Add a comment showing the encoding of an instruction.
87 virtual void AddEncodingComment(const MCInst &Inst);
88
89 /// GetCommentOS - Return a raw_ostream that comments can be written to.
90 /// Unlike AddComment, you are required to terminate comments with \n if you
91 /// use this method.
92 virtual raw_ostream &GetCommentOS() {
93 if (!IsVerboseAsm)
94 return nulls(); // Discard comments unless in verbose asm mode.
95 return CommentStream;
96 }
97
98 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
99 virtual void AddBlankLine() {
100 EmitEOL();
101 }
102
103 /// @name MCStreamer Interface
104 /// @{
105
106 virtual void SwitchSection(const MCSection *Section);
107
108 virtual void InitSections() {
109 }
110
111 virtual void EmitLabel(MCSymbol *Symbol);
112
113 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
114
115 virtual void EmitThumbFunc(MCSymbol *Func);
116
117 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
118
119 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
120
121 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
122
123 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
124 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
125 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
126 virtual void EmitCOFFSymbolType(int Type);
127 virtual void EndCOFFSymbolDef();
128 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
129 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
130 unsigned ByteAlignment);
131
132 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
133 ///
134 /// @param Symbol - The common symbol to emit.
135 /// @param Size - The size of the common symbol.
136 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
137
138 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
139 unsigned Size = 0, unsigned ByteAlignment = 0);
140
141 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
142 uint64_t Size, unsigned ByteAlignment = 0);
143
144 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
145
Devang Patelee4854f2010-12-02 21:32:30 +0000146 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace,
147 bool UseSet = false);
Rafael Espindolaa484f2c2010-11-28 14:48:34 +0000148 virtual void EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace = 0);
149 virtual void EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace = 0);
150 virtual void EmitGPRel32Value(const MCExpr *Value);
151
152
153 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
154 unsigned AddrSpace);
155
156 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
157 unsigned ValueSize = 1,
158 unsigned MaxBytesToEmit = 0);
159
160 virtual void EmitCodeAlignment(unsigned ByteAlignment,
161 unsigned MaxBytesToEmit = 0);
162
163 virtual void EmitValueToOffset(const MCExpr *Offset,
164 unsigned char Value = 0);
165
166 virtual void EmitFileDirective(StringRef Filename);
167 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
168
169 virtual void EmitInstruction(const MCInst &Inst);
170
171 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
172 /// the specified string in the output .s file. This capability is
173 /// indicated by the hasRawTextSupport() predicate.
174 virtual void EmitRawText(StringRef String);
175
176 virtual void Finish();
177
178 /// @}
179
180}; // class PTXMCAsmStreamer
181
182}
183
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000184/// TODO: Add appropriate implementation of Emit*() methods when needed
185
186void PTXMCAsmStreamer::AddComment(const Twine &T) {
187 if (!IsVerboseAsm) return;
188
189 // Make sure that CommentStream is flushed.
190 CommentStream.flush();
191
192 T.toVector(CommentToEmit);
193 // Each comment goes on its own line.
194 CommentToEmit.push_back('\n');
195
196 // Tell the comment stream that the vector changed underneath it.
197 CommentStream.resync();
198}
199
200void PTXMCAsmStreamer::EmitCommentsAndEOL() {
201 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
202 OS << '\n';
203 return;
204 }
205
206 CommentStream.flush();
207 StringRef Comments = CommentToEmit.str();
208
209 assert(Comments.back() == '\n' &&
210 "Comment array not newline terminated");
211 do {
212 // Emit a line of comments.
213 OS.PadToColumn(MAI.getCommentColumn());
214 size_t Position = Comments.find('\n');
215 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
216
217 Comments = Comments.substr(Position+1);
218 } while (!Comments.empty());
219
220 CommentToEmit.clear();
221 // Tell the comment stream that the vector changed underneath it.
222 CommentStream.resync();
223}
224
225static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
226 assert(Bytes && "Invalid size!");
227 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
228}
229
230void PTXMCAsmStreamer::SwitchSection(const MCSection *Section) {
231 assert(Section && "Cannot switch to a null section!");
232 if (Section != CurSection) {
233 PrevSection = CurSection;
234 CurSection = Section;
235 }
236}
237
238void PTXMCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
239 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
240 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
241 assert(CurSection && "Cannot emit before setting section!");
242
243 OS << *Symbol << MAI.getLabelSuffix();
244 EmitEOL();
245 Symbol->setSection(*CurSection);
246}
247
248void PTXMCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {}
249
250void PTXMCAsmStreamer::EmitThumbFunc(MCSymbol *Func) {}
251
252void PTXMCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
253 OS << *Symbol << " = " << *Value;
254 EmitEOL();
255
256 // FIXME: Lift context changes into super class.
257 Symbol->setVariableValue(Value);
258}
259
260void PTXMCAsmStreamer::EmitWeakReference(MCSymbol *Alias,
261 const MCSymbol *Symbol) {
262 OS << ".weakref " << *Alias << ", " << *Symbol;
263 EmitEOL();
264}
265
266void PTXMCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
267 MCSymbolAttr Attribute) {}
268
269void PTXMCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {}
270
271void PTXMCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {}
272
273void PTXMCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {}
274
275void PTXMCAsmStreamer::EmitCOFFSymbolType (int Type) {}
276
277void PTXMCAsmStreamer::EndCOFFSymbolDef() {}
278
279void PTXMCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {}
280
281void PTXMCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
282 unsigned ByteAlignment) {}
283
284void PTXMCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {}
285
286void PTXMCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
287 unsigned Size, unsigned ByteAlignment) {}
288
289void PTXMCAsmStreamer::EmitTBSSSymbol(const MCSection *Section,
290 MCSymbol *Symbol,
291 uint64_t Size, unsigned ByteAlignment) {}
292
293static inline char toOctal(int X) { return (X&7)+'0'; }
294
295static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
296 OS << '"';
297
298 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
299 unsigned char C = Data[i];
300 if (C == '"' || C == '\\') {
301 OS << '\\' << (char)C;
302 continue;
303 }
304
305 if (isprint((unsigned char)C)) {
306 OS << (char)C;
307 continue;
308 }
309
310 switch (C) {
311 case '\b': OS << "\\b"; break;
312 case '\f': OS << "\\f"; break;
313 case '\n': OS << "\\n"; break;
314 case '\r': OS << "\\r"; break;
315 case '\t': OS << "\\t"; break;
316 default:
317 OS << '\\';
318 OS << toOctal(C >> 6);
319 OS << toOctal(C >> 3);
320 OS << toOctal(C >> 0);
321 break;
322 }
323 }
324
325 OS << '"';
326}
327
328void PTXMCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
329 assert(CurSection && "Cannot emit contents before setting section!");
330 if (Data.empty()) return;
331
332 if (Data.size() == 1) {
333 OS << MAI.getData8bitsDirective(AddrSpace);
334 OS << (unsigned)(unsigned char)Data[0];
335 EmitEOL();
336 return;
337 }
338
339 // If the data ends with 0 and the target supports .asciz, use it, otherwise
340 // use .ascii
341 if (MAI.getAscizDirective() && Data.back() == 0) {
342 OS << MAI.getAscizDirective();
343 Data = Data.substr(0, Data.size()-1);
344 } else {
345 OS << MAI.getAsciiDirective();
346 }
347
348 OS << ' ';
349 PrintQuotedString(Data, OS);
350 EmitEOL();
351}
352
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000353void PTXMCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
Devang Patelee4854f2010-12-02 21:32:30 +0000354 unsigned AddrSpace, bool UseSet) {
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000355 assert(CurSection && "Cannot emit contents before setting section!");
356 const char *Directive = 0;
357 switch (Size) {
358 default: break;
359 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
360 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
361 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
362 case 8:
363 Directive = MAI.getData64bitsDirective(AddrSpace);
364 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
365 if (Directive) break;
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000366 int64_t IntValue;
367 if (!Value->EvaluateAsAbsolute(IntValue))
368 report_fatal_error("Don't know how to emit this value.");
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000369 if (isLittleEndian()) {
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000370 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
371 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000372 } else {
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000373 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
374 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000375 }
376 return;
377 }
378
379 assert(Directive && "Invalid size for machine code value!");
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000380 OS << Directive << *Value;
381 EmitEOL();
382}
383
384void PTXMCAsmStreamer::EmitULEB128Value(const MCExpr *Value,
385 unsigned AddrSpace) {
386 assert(MAI.hasLEB128() && "Cannot print a .uleb");
387 OS << ".uleb128 " << *Value;
388 EmitEOL();
389}
390
391void PTXMCAsmStreamer::EmitSLEB128Value(const MCExpr *Value,
392 unsigned AddrSpace) {
393 assert(MAI.hasLEB128() && "Cannot print a .sleb");
394 OS << ".sleb128 " << *Value;
395 EmitEOL();
396}
397
398void PTXMCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
399 assert(MAI.getGPRel32Directive() != 0);
400 OS << MAI.getGPRel32Directive() << *Value;
401 EmitEOL();
402}
403
404
405/// EmitFill - Emit NumBytes bytes worth of the value specified by
406/// FillValue. This implements directives such as '.space'.
407void PTXMCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
408 unsigned AddrSpace) {
409 if (NumBytes == 0) return;
410
411 if (AddrSpace == 0)
412 if (const char *ZeroDirective = MAI.getZeroDirective()) {
413 OS << ZeroDirective << NumBytes;
414 if (FillValue != 0)
415 OS << ',' << (int)FillValue;
416 EmitEOL();
417 return;
418 }
419
420 // Emit a byte at a time.
421 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
422}
423
424void PTXMCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
425 unsigned ValueSize,
426 unsigned MaxBytesToEmit) {
427 // Some assemblers don't support non-power of two alignments, so we always
428 // emit alignments as a power of two if possible.
429 if (isPowerOf2_32(ByteAlignment)) {
430 switch (ValueSize) {
431 default: llvm_unreachable("Invalid size for machine code value!");
432 case 1: OS << MAI.getAlignDirective(); break;
433 // FIXME: use MAI for this!
434 case 2: OS << ".p2alignw "; break;
435 case 4: OS << ".p2alignl "; break;
436 case 8: llvm_unreachable("Unsupported alignment size!");
437 }
438
439 if (MAI.getAlignmentIsInBytes())
440 OS << ByteAlignment;
441 else
442 OS << Log2_32(ByteAlignment);
443
444 if (Value || MaxBytesToEmit) {
445 OS << ", 0x";
446 OS.write_hex(truncateToSize(Value, ValueSize));
447
448 if (MaxBytesToEmit)
449 OS << ", " << MaxBytesToEmit;
450 }
451 EmitEOL();
452 return;
453 }
454
455 // Non-power of two alignment. This is not widely supported by assemblers.
456 // FIXME: Parameterize this based on MAI.
457 switch (ValueSize) {
458 default: llvm_unreachable("Invalid size for machine code value!");
459 case 1: OS << ".balign"; break;
460 case 2: OS << ".balignw"; break;
461 case 4: OS << ".balignl"; break;
462 case 8: llvm_unreachable("Unsupported alignment size!");
463 }
464
465 OS << ' ' << ByteAlignment;
466 OS << ", " << truncateToSize(Value, ValueSize);
467 if (MaxBytesToEmit)
468 OS << ", " << MaxBytesToEmit;
469 EmitEOL();
470}
471
472void PTXMCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
473 unsigned MaxBytesToEmit) {}
474
475void PTXMCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
476 unsigned char Value) {}
477
478
479void PTXMCAsmStreamer::EmitFileDirective(StringRef Filename) {
480 assert(MAI.hasSingleParameterDotFile());
481 OS << "\t.file\t";
482 PrintQuotedString(Filename, OS);
483 EmitEOL();
484}
485
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000486// FIXME: should we inherit from MCAsmStreamer?
487bool PTXMCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo,
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000488 StringRef Filename){
489 OS << "\t.file\t" << FileNo << ' ';
490 PrintQuotedString(Filename, OS);
491 EmitEOL();
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000492 return this->MCStreamer::EmitDwarfFileDirective(FileNo, Filename);
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000493}
494
495void PTXMCAsmStreamer::AddEncodingComment(const MCInst &Inst) {}
496
497void PTXMCAsmStreamer::EmitInstruction(const MCInst &Inst) {
498 assert(CurSection && "Cannot emit contents before setting section!");
499
500 // Show the encoding in a comment if we have a code emitter.
501 if (Emitter)
502 AddEncodingComment(Inst);
503
504 // Show the MCInst if enabled.
505 if (ShowInst) {
506 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n ");
507 GetCommentOS() << "\n";
508 }
509
510 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
511 if (InstPrinter)
512 InstPrinter->printInst(&Inst, OS);
513 else
514 Inst.print(OS, &MAI);
515 EmitEOL();
516}
517
518/// EmitRawText - If this file is backed by an assembly streamer, this dumps
519/// the specified string in the output .s file. This capability is
520/// indicated by the hasRawTextSupport() predicate.
521void PTXMCAsmStreamer::EmitRawText(StringRef String) {
522 if (!String.empty() && String.back() == '\n')
523 String = String.substr(0, String.size()-1);
524 OS << String;
525 EmitEOL();
526}
527
528void PTXMCAsmStreamer::Finish() {}
529
Rafael Espindolaa484f2c2010-11-28 14:48:34 +0000530namespace llvm {
531 MCStreamer *createPTXAsmStreamer(MCContext &Context,
532 formatted_raw_ostream &OS,
533 bool isLittleEndian,
534 bool isVerboseAsm, MCInstPrinter *IP,
535 MCCodeEmitter *CE, bool ShowInst) {
536 return new PTXMCAsmStreamer(Context, OS, isLittleEndian, isVerboseAsm,
537 IP, CE, ShowInst);
538 }
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000539}