blob: 3f9e8b4f329a0452654deaf46415afb225573248 [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
146 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
147 virtual void EmitIntValue(uint64_t Value, unsigned Size, unsigned AddrSpace);
148 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
353/// EmitIntValue - Special case of EmitValue that avoids the client having
354/// to pass in a MCExpr for constant integers.
355void PTXMCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
356 unsigned AddrSpace) {
357 assert(CurSection && "Cannot emit contents before setting section!");
358 const char *Directive = 0;
359 switch (Size) {
360 default: break;
361 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
362 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
363 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
364 case 8:
365 Directive = MAI.getData64bitsDirective(AddrSpace);
366 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
367 if (Directive) break;
368 if (isLittleEndian()) {
369 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
370 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
371 } else {
372 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
373 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
374 }
375 return;
376 }
377
378 assert(Directive && "Invalid size for machine code value!");
379 OS << Directive << truncateToSize(Value, Size);
380 EmitEOL();
381}
382
383void PTXMCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
384 unsigned AddrSpace) {
385 assert(CurSection && "Cannot emit contents before setting section!");
386 const char *Directive = 0;
387 switch (Size) {
388 default: break;
389 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
390 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
391 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
392 case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
393 }
394
395 assert(Directive && "Invalid size for machine code value!");
396 OS << Directive << *Value;
397 EmitEOL();
398}
399
400void PTXMCAsmStreamer::EmitULEB128Value(const MCExpr *Value,
401 unsigned AddrSpace) {
402 assert(MAI.hasLEB128() && "Cannot print a .uleb");
403 OS << ".uleb128 " << *Value;
404 EmitEOL();
405}
406
407void PTXMCAsmStreamer::EmitSLEB128Value(const MCExpr *Value,
408 unsigned AddrSpace) {
409 assert(MAI.hasLEB128() && "Cannot print a .sleb");
410 OS << ".sleb128 " << *Value;
411 EmitEOL();
412}
413
414void PTXMCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
415 assert(MAI.getGPRel32Directive() != 0);
416 OS << MAI.getGPRel32Directive() << *Value;
417 EmitEOL();
418}
419
420
421/// EmitFill - Emit NumBytes bytes worth of the value specified by
422/// FillValue. This implements directives such as '.space'.
423void PTXMCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
424 unsigned AddrSpace) {
425 if (NumBytes == 0) return;
426
427 if (AddrSpace == 0)
428 if (const char *ZeroDirective = MAI.getZeroDirective()) {
429 OS << ZeroDirective << NumBytes;
430 if (FillValue != 0)
431 OS << ',' << (int)FillValue;
432 EmitEOL();
433 return;
434 }
435
436 // Emit a byte at a time.
437 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
438}
439
440void PTXMCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
441 unsigned ValueSize,
442 unsigned MaxBytesToEmit) {
443 // Some assemblers don't support non-power of two alignments, so we always
444 // emit alignments as a power of two if possible.
445 if (isPowerOf2_32(ByteAlignment)) {
446 switch (ValueSize) {
447 default: llvm_unreachable("Invalid size for machine code value!");
448 case 1: OS << MAI.getAlignDirective(); break;
449 // FIXME: use MAI for this!
450 case 2: OS << ".p2alignw "; break;
451 case 4: OS << ".p2alignl "; break;
452 case 8: llvm_unreachable("Unsupported alignment size!");
453 }
454
455 if (MAI.getAlignmentIsInBytes())
456 OS << ByteAlignment;
457 else
458 OS << Log2_32(ByteAlignment);
459
460 if (Value || MaxBytesToEmit) {
461 OS << ", 0x";
462 OS.write_hex(truncateToSize(Value, ValueSize));
463
464 if (MaxBytesToEmit)
465 OS << ", " << MaxBytesToEmit;
466 }
467 EmitEOL();
468 return;
469 }
470
471 // Non-power of two alignment. This is not widely supported by assemblers.
472 // FIXME: Parameterize this based on MAI.
473 switch (ValueSize) {
474 default: llvm_unreachable("Invalid size for machine code value!");
475 case 1: OS << ".balign"; break;
476 case 2: OS << ".balignw"; break;
477 case 4: OS << ".balignl"; break;
478 case 8: llvm_unreachable("Unsupported alignment size!");
479 }
480
481 OS << ' ' << ByteAlignment;
482 OS << ", " << truncateToSize(Value, ValueSize);
483 if (MaxBytesToEmit)
484 OS << ", " << MaxBytesToEmit;
485 EmitEOL();
486}
487
488void PTXMCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
489 unsigned MaxBytesToEmit) {}
490
491void PTXMCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
492 unsigned char Value) {}
493
494
495void PTXMCAsmStreamer::EmitFileDirective(StringRef Filename) {
496 assert(MAI.hasSingleParameterDotFile());
497 OS << "\t.file\t";
498 PrintQuotedString(Filename, OS);
499 EmitEOL();
500}
501
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000502// FIXME: should we inherit from MCAsmStreamer?
503bool PTXMCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo,
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000504 StringRef Filename){
505 OS << "\t.file\t" << FileNo << ' ';
506 PrintQuotedString(Filename, OS);
507 EmitEOL();
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000508 return this->MCStreamer::EmitDwarfFileDirective(FileNo, Filename);
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000509}
510
511void PTXMCAsmStreamer::AddEncodingComment(const MCInst &Inst) {}
512
513void PTXMCAsmStreamer::EmitInstruction(const MCInst &Inst) {
514 assert(CurSection && "Cannot emit contents before setting section!");
515
516 // Show the encoding in a comment if we have a code emitter.
517 if (Emitter)
518 AddEncodingComment(Inst);
519
520 // Show the MCInst if enabled.
521 if (ShowInst) {
522 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n ");
523 GetCommentOS() << "\n";
524 }
525
526 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
527 if (InstPrinter)
528 InstPrinter->printInst(&Inst, OS);
529 else
530 Inst.print(OS, &MAI);
531 EmitEOL();
532}
533
534/// EmitRawText - If this file is backed by an assembly streamer, this dumps
535/// the specified string in the output .s file. This capability is
536/// indicated by the hasRawTextSupport() predicate.
537void PTXMCAsmStreamer::EmitRawText(StringRef String) {
538 if (!String.empty() && String.back() == '\n')
539 String = String.substr(0, String.size()-1);
540 OS << String;
541 EmitEOL();
542}
543
544void PTXMCAsmStreamer::Finish() {}
545
Rafael Espindolaa484f2c2010-11-28 14:48:34 +0000546namespace llvm {
547 MCStreamer *createPTXAsmStreamer(MCContext &Context,
548 formatted_raw_ostream &OS,
549 bool isLittleEndian,
550 bool isVerboseAsm, MCInstPrinter *IP,
551 MCCodeEmitter *CE, bool ShowInst) {
552 return new PTXMCAsmStreamer(Context, OS, isLittleEndian, isVerboseAsm,
553 IP, CE, ShowInst);
554 }
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000555}