blob: 41f88334e2b76440e4c4e3690f63a194a4780268 [file] [log] [blame]
Daniel Dunbara11af532009-06-24 01:03:06 +00001//===- lib/MC/MCAsmStreamer.cpp - 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 "llvm/MC/MCStreamer.h"
11
Daniel Dunbarc22e0b22009-08-14 03:48:55 +000012#include "llvm/CodeGen/AsmPrinter.h"
Daniel Dunbara11af532009-06-24 01:03:06 +000013#include "llvm/MC/MCContext.h"
Daniel Dunbarabde2982009-07-01 06:35:03 +000014#include "llvm/MC/MCInst.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000015#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbara11af532009-06-24 01:03:06 +000016#include "llvm/MC/MCSymbol.h"
17#include "llvm/MC/MCValue.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000018#include "llvm/Support/ErrorHandling.h"
Daniel Dunbara11af532009-06-24 01:03:06 +000019#include "llvm/Support/raw_ostream.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000020
Daniel Dunbara11af532009-06-24 01:03:06 +000021using namespace llvm;
22
23namespace {
24
25 class MCAsmStreamer : public MCStreamer {
26 raw_ostream &OS;
Daniel Dunbarc22e0b22009-08-14 03:48:55 +000027
28 AsmPrinter *Printer;
Daniel Dunbara11af532009-06-24 01:03:06 +000029
30 MCSection *CurSection;
31
32 public:
Daniel Dunbarc22e0b22009-08-14 03:48:55 +000033 MCAsmStreamer(MCContext &Context, raw_ostream &_OS, AsmPrinter *_AsmPrinter)
34 : MCStreamer(Context), OS(_OS), Printer(_AsmPrinter), CurSection(0) {}
Daniel Dunbara11af532009-06-24 01:03:06 +000035 ~MCAsmStreamer() {}
36
37 /// @name MCStreamer Interface
38 /// @{
39
40 virtual void SwitchSection(MCSection *Section);
41
42 virtual void EmitLabel(MCSymbol *Symbol);
43
Kevin Enderbyf96db462009-07-16 17:56:39 +000044 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
Kevin Enderbya5c78322009-07-13 21:03:15 +000045
Daniel Dunbara11af532009-06-24 01:03:06 +000046 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
47 bool MakeAbsolute = false);
48
49 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
50
Kevin Enderby95cf30c2009-07-14 18:17:10 +000051 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
52
Kevin Enderby71148242009-07-14 21:35:03 +000053 virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value);
54
Chris Lattner4e4db7a2009-07-07 20:30:46 +000055 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Chris Lattner1fc3d752009-07-09 17:25:12 +000056 unsigned Pow2Alignment, bool IsLocal);
Chris Lattner4e4db7a2009-07-07 20:30:46 +000057
Chris Lattner9be3fee2009-07-10 22:20:30 +000058 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL,
59 unsigned Size = 0, unsigned Pow2Alignment = 0);
60
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +000061 virtual void EmitBytes(const StringRef &Data);
Daniel Dunbara11af532009-06-24 01:03:06 +000062
63 virtual void EmitValue(const MCValue &Value, unsigned Size);
64
Daniel Dunbar84a29262009-06-24 19:25:34 +000065 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
66 unsigned ValueSize = 1,
67 unsigned MaxBytesToEmit = 0);
68
69 virtual void EmitValueToOffset(const MCValue &Offset,
70 unsigned char Value = 0);
71
Daniel Dunbara11af532009-06-24 01:03:06 +000072 virtual void EmitInstruction(const MCInst &Inst);
73
74 virtual void Finish();
75
76 /// @}
77 };
78
79}
80
Daniel Dunbarad4555c2009-07-31 23:04:32 +000081/// Allow printing symbols directly to a raw_ostream with proper quoting.
82static inline raw_ostream &operator<<(raw_ostream &os, const MCSymbol *S) {
Daniel Dunbarc22e0b22009-08-14 03:48:55 +000083 S->print(os);
84
85 return os;
Daniel Dunbarad4555c2009-07-31 23:04:32 +000086}
87
Daniel Dunbara11af532009-06-24 01:03:06 +000088/// Allow printing values directly to a raw_ostream.
Daniel Dunbar304f6a42009-06-25 21:03:18 +000089static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
Daniel Dunbarc22e0b22009-08-14 03:48:55 +000090 Value.print(os);
Daniel Dunbara11af532009-06-24 01:03:06 +000091
92 return os;
93}
94
Daniel Dunbar304f6a42009-06-25 21:03:18 +000095static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
96 assert(Bytes && "Invalid size!");
97 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
98}
99
100static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
101 return MCValue::get(Value.getSymA(), Value.getSymB(),
Daniel Dunbar7908a6a2009-06-29 19:51:00 +0000102 truncateToSize(Value.getConstant(), Bytes));
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000103}
104
Daniel Dunbara11af532009-06-24 01:03:06 +0000105void MCAsmStreamer::SwitchSection(MCSection *Section) {
106 if (Section != CurSection) {
107 CurSection = Section;
108
Chris Lattner93b6db32009-08-08 23:39:42 +0000109 // FIXME: Needs TargetAsmInfo!
110 Section->PrintSwitchToSection(*(const TargetAsmInfo*)0, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000111 }
112}
113
114void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000115 assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
116 assert(CurSection && "Cannot emit before setting section!");
117 assert(!getContext().GetSymbolValue(Symbol) &&
118 "Cannot emit symbol which was directly assigned to!");
119
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000120 OS << Symbol << ":\n";
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000121 Symbol->setSection(CurSection);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000122 Symbol->setExternal(false);
Daniel Dunbara11af532009-06-24 01:03:06 +0000123}
124
Kevin Enderbyf96db462009-07-16 17:56:39 +0000125void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
126 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000127 default: assert(0 && "Invalid flag!");
Kevin Enderbyf96db462009-07-16 17:56:39 +0000128 case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
129 }
130 OS << '\n';
Kevin Enderbya5c78322009-07-13 21:03:15 +0000131}
132
Daniel Dunbara11af532009-06-24 01:03:06 +0000133void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
134 bool MakeAbsolute) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000135 assert(!Symbol->getSection() && "Cannot assign to a label!");
136
Daniel Dunbara11af532009-06-24 01:03:06 +0000137 if (MakeAbsolute) {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000138 OS << ".set " << Symbol << ", " << Value << '\n';
Daniel Dunbarb2d0b6b2009-08-14 19:10:46 +0000139
140 // HACK: If the value isn't already absolute, set the symbol value to
141 // itself, we want to use the .set absolute value, not the actual
142 // expression.
143 if (!Value.isAbsolute())
144 getContext().SetSymbolValue(Symbol, MCValue::get(Symbol));
145 else
146 getContext().SetSymbolValue(Symbol, Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000147 } else {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000148 OS << Symbol << " = " << Value << '\n';
Daniel Dunbarb2d0b6b2009-08-14 19:10:46 +0000149 getContext().SetSymbolValue(Symbol, Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000150 }
151}
152
153void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
154 SymbolAttr Attribute) {
155 switch (Attribute) {
156 case Global: OS << ".globl"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000157 case Hidden: OS << ".hidden"; break;
158 case IndirectSymbol: OS << ".indirect_symbol"; break;
159 case Internal: OS << ".internal"; break;
160 case LazyReference: OS << ".lazy_reference"; break;
161 case NoDeadStrip: OS << ".no_dead_strip"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000162 case PrivateExtern: OS << ".private_extern"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000163 case Protected: OS << ".protected"; break;
164 case Reference: OS << ".reference"; break;
165 case Weak: OS << ".weak"; break;
166 case WeakDefinition: OS << ".weak_definition"; break;
167 case WeakReference: OS << ".weak_reference"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000168 }
169
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000170 OS << ' ' << Symbol << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000171}
172
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000173void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000174 OS << ".desc" << ' ' << Symbol << ',' << DescValue << '\n';
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000175}
176
Kevin Enderby71148242009-07-14 21:35:03 +0000177void MCAsmStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000178 OS << ".lsym" << ' ' << Symbol << ',' << Value << '\n';
Kevin Enderby71148242009-07-14 21:35:03 +0000179}
180
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000181void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Chris Lattner1fc3d752009-07-09 17:25:12 +0000182 unsigned Pow2Alignment, bool IsLocal) {
183 if (IsLocal)
184 OS << ".lcomm";
185 else
186 OS << ".comm";
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000187 OS << ' ' << Symbol << ',' << Size;
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000188 if (Pow2Alignment != 0)
189 OS << ',' << Pow2Alignment;
190 OS << '\n';
191}
192
Chris Lattner9be3fee2009-07-10 22:20:30 +0000193void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
194 unsigned Size, unsigned Pow2Alignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000195 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000196 OS << ".zerofill ";
197
198 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000199 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000200 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Chris Lattner93b6db32009-08-08 23:39:42 +0000201
Chris Lattner9be3fee2009-07-10 22:20:30 +0000202 if (Symbol != NULL) {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000203 OS << ',' << Symbol << ',' << Size;
Chris Lattner9be3fee2009-07-10 22:20:30 +0000204 if (Pow2Alignment != 0)
205 OS << ',' << Pow2Alignment;
206 }
207 OS << '\n';
208}
209
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000210void MCAsmStreamer::EmitBytes(const StringRef &Data) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000211 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000212 for (unsigned i = 0, e = Data.size(); i != e; ++i)
Daniel Dunbara11af532009-06-24 01:03:06 +0000213 OS << ".byte " << (unsigned) Data[i] << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000214}
215
216void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000217 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000218 // Need target hooks to know how to print this.
219 switch (Size) {
220 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000221 llvm_unreachable("Invalid size for machine code value!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000222 case 1: OS << ".byte"; break;
Daniel Dunbarf5e75a12009-06-24 16:05:35 +0000223 case 2: OS << ".short"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000224 case 4: OS << ".long"; break;
225 case 8: OS << ".quad"; break;
226 }
227
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000228 OS << ' ' << truncateToSize(Value, Size) << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000229}
230
Daniel Dunbar84a29262009-06-24 19:25:34 +0000231void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
232 unsigned ValueSize,
233 unsigned MaxBytesToEmit) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000234 // Some assemblers don't support .balign, so we always emit as .p2align if
235 // this is a power of two. Otherwise we assume the client knows the target
236 // supports .balign and use that.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000237 unsigned Pow2 = Log2_32(ByteAlignment);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000238 bool IsPow2 = (1U << Pow2) == ByteAlignment;
Daniel Dunbar84a29262009-06-24 19:25:34 +0000239
240 switch (ValueSize) {
241 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000242 llvm_unreachable("Invalid size for machine code value!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000243 case 8:
Torok Edwinc23197a2009-07-14 16:55:14 +0000244 llvm_unreachable("Unsupported alignment size!");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000245 case 1: OS << (IsPow2 ? ".p2align" : ".balign"); break;
246 case 2: OS << (IsPow2 ? ".p2alignw" : ".balignw"); break;
247 case 4: OS << (IsPow2 ? ".p2alignl" : ".balignl"); break;
Daniel Dunbar84a29262009-06-24 19:25:34 +0000248 }
249
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000250 OS << ' ' << (IsPow2 ? Pow2 : ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000251
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000252 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000253 if (MaxBytesToEmit)
254 OS << ", " << MaxBytesToEmit;
255 OS << '\n';
256}
257
258void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
259 unsigned char Value) {
260 // FIXME: Verify that Offset is associated with the current section.
261 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
262}
263
Daniel Dunbarabde2982009-07-01 06:35:03 +0000264static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
265 if (Op.isReg())
266 return OS << "reg:" << Op.getReg();
267 if (Op.isImm())
268 return OS << "imm:" << Op.getImm();
269 if (Op.isMBBLabel())
270 return OS << "mbblabel:("
271 << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
272 assert(Op.isMCValue() && "Invalid operand!");
273 return OS << "val:" << Op.getMCValue();
274}
275
Daniel Dunbara11af532009-06-24 01:03:06 +0000276void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000277 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000278
279 // If we have an AsmPrinter, use that to print.
280 if (Printer) {
281 Printer->printMCInst(&Inst);
282 return;
283 }
284
285 // Otherwise fall back to a structural printing for now. Eventually we should
286 // always have access to the target specific printer.
Daniel Dunbarabde2982009-07-01 06:35:03 +0000287 OS << "MCInst("
288 << "opcode=" << Inst.getOpcode() << ", "
289 << "operands=[";
290 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
291 if (i)
292 OS << ", ";
293 OS << Inst.getOperand(i);
294 }
295 OS << "])\n";
Daniel Dunbara11af532009-06-24 01:03:06 +0000296}
297
298void MCAsmStreamer::Finish() {
299 OS.flush();
300}
301
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000302MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS,
303 AsmPrinter *AP) {
304 return new MCAsmStreamer(Context, OS, AP);
Daniel Dunbara11af532009-06-24 01:03:06 +0000305}