blob: cb3642532c1c319e2a29220bda99fc1b565d730b [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"
Daniel Dunbarc22e0b22009-08-14 03:48:55 +000011#include "llvm/CodeGen/AsmPrinter.h"
Daniel Dunbara11af532009-06-24 01:03:06 +000012#include "llvm/MC/MCContext.h"
Daniel Dunbarabde2982009-07-01 06:35:03 +000013#include "llvm/MC/MCInst.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000014#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbara11af532009-06-24 01:03:06 +000015#include "llvm/MC/MCSymbol.h"
16#include "llvm/MC/MCValue.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000017#include "llvm/Support/ErrorHandling.h"
Daniel Dunbara11af532009-06-24 01:03:06 +000018#include "llvm/Support/raw_ostream.h"
19using namespace llvm;
20
21namespace {
22
Chris Lattner46a947d2009-08-17 04:17:34 +000023class MCAsmStreamer : public MCStreamer {
24 raw_ostream &OS;
Chris Lattnerf3ce0092009-08-17 04:23:44 +000025 const TargetAsmInfo &TAI;
Chris Lattner46a947d2009-08-17 04:17:34 +000026 AsmPrinter *Printer;
Chris Lattner46a947d2009-08-17 04:17:34 +000027public:
Chris Lattnerf3ce0092009-08-17 04:23:44 +000028 MCAsmStreamer(MCContext &Context, raw_ostream &_OS, const TargetAsmInfo &tai,
29 AsmPrinter *_AsmPrinter)
Chris Lattnerdabf07c2009-08-18 06:15:16 +000030 : MCStreamer(Context), OS(_OS), TAI(tai), Printer(_AsmPrinter) {}
Chris Lattner46a947d2009-08-17 04:17:34 +000031 ~MCAsmStreamer() {}
Daniel Dunbara11af532009-06-24 01:03:06 +000032
Chris Lattner46a947d2009-08-17 04:17:34 +000033 /// @name MCStreamer Interface
34 /// @{
Daniel Dunbara11af532009-06-24 01:03:06 +000035
Chris Lattner975780b2009-08-17 05:49:08 +000036 virtual void SwitchSection(const MCSection *Section);
Daniel Dunbara11af532009-06-24 01:03:06 +000037
Chris Lattner46a947d2009-08-17 04:17:34 +000038 virtual void EmitLabel(MCSymbol *Symbol);
Daniel Dunbara11af532009-06-24 01:03:06 +000039
Chris Lattner46a947d2009-08-17 04:17:34 +000040 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
Daniel Dunbara11af532009-06-24 01:03:06 +000041
Chris Lattner46a947d2009-08-17 04:17:34 +000042 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
43 bool MakeAbsolute = false);
Daniel Dunbara11af532009-06-24 01:03:06 +000044
Chris Lattner46a947d2009-08-17 04:17:34 +000045 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
Kevin Enderbya5c78322009-07-13 21:03:15 +000046
Chris Lattner46a947d2009-08-17 04:17:34 +000047 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Daniel Dunbara11af532009-06-24 01:03:06 +000048
Chris Lattner46a947d2009-08-17 04:17:34 +000049 virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value);
Daniel Dunbara11af532009-06-24 01:03:06 +000050
Chris Lattner46a947d2009-08-17 04:17:34 +000051 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
52 unsigned Pow2Alignment, bool IsLocal);
Kevin Enderby95cf30c2009-07-14 18:17:10 +000053
Chris Lattner46a947d2009-08-17 04:17:34 +000054 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL,
55 unsigned Size = 0, unsigned Pow2Alignment = 0);
Kevin Enderby71148242009-07-14 21:35:03 +000056
Chris Lattner46a947d2009-08-17 04:17:34 +000057 virtual void EmitBytes(const StringRef &Data);
Chris Lattner4e4db7a2009-07-07 20:30:46 +000058
Chris Lattner46a947d2009-08-17 04:17:34 +000059 virtual void EmitValue(const MCValue &Value, unsigned Size);
Chris Lattner9be3fee2009-07-10 22:20:30 +000060
Chris Lattner46a947d2009-08-17 04:17:34 +000061 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
62 unsigned ValueSize = 1,
63 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +000064
Chris Lattner46a947d2009-08-17 04:17:34 +000065 virtual void EmitValueToOffset(const MCValue &Offset,
66 unsigned char Value = 0);
67
68 virtual void EmitInstruction(const MCInst &Inst);
Daniel Dunbara11af532009-06-24 01:03:06 +000069
Chris Lattner46a947d2009-08-17 04:17:34 +000070 virtual void Finish();
71
72 /// @}
73};
Daniel Dunbar84a29262009-06-24 19:25:34 +000074
Chris Lattner46a947d2009-08-17 04:17:34 +000075} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +000076
Daniel Dunbarad4555c2009-07-31 23:04:32 +000077/// Allow printing symbols directly to a raw_ostream with proper quoting.
78static inline raw_ostream &operator<<(raw_ostream &os, const MCSymbol *S) {
Daniel Dunbarc22e0b22009-08-14 03:48:55 +000079 S->print(os);
Daniel Dunbarc22e0b22009-08-14 03:48:55 +000080 return os;
Daniel Dunbarad4555c2009-07-31 23:04:32 +000081}
82
Daniel Dunbara11af532009-06-24 01:03:06 +000083/// Allow printing values directly to a raw_ostream.
Daniel Dunbar304f6a42009-06-25 21:03:18 +000084static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
Daniel Dunbarc22e0b22009-08-14 03:48:55 +000085 Value.print(os);
Daniel Dunbara11af532009-06-24 01:03:06 +000086 return os;
87}
88
Daniel Dunbar304f6a42009-06-25 21:03:18 +000089static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
90 assert(Bytes && "Invalid size!");
91 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
92}
93
94static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
95 return MCValue::get(Value.getSymA(), Value.getSymB(),
Daniel Dunbar7908a6a2009-06-29 19:51:00 +000096 truncateToSize(Value.getConstant(), Bytes));
Daniel Dunbar304f6a42009-06-25 21:03:18 +000097}
98
Chris Lattner975780b2009-08-17 05:49:08 +000099void MCAsmStreamer::SwitchSection(const MCSection *Section) {
Daniel Dunbara11af532009-06-24 01:03:06 +0000100 if (Section != CurSection) {
101 CurSection = Section;
Chris Lattner975780b2009-08-17 05:49:08 +0000102 Section->PrintSwitchToSection(TAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000103 }
104}
105
106void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000107 assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
108 assert(CurSection && "Cannot emit before setting section!");
109 assert(!getContext().GetSymbolValue(Symbol) &&
110 "Cannot emit symbol which was directly assigned to!");
111
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000112 OS << Symbol << ":\n";
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000113 Symbol->setSection(CurSection);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000114 Symbol->setExternal(false);
Daniel Dunbara11af532009-06-24 01:03:06 +0000115}
116
Kevin Enderbyf96db462009-07-16 17:56:39 +0000117void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
118 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000119 default: assert(0 && "Invalid flag!");
Kevin Enderbyf96db462009-07-16 17:56:39 +0000120 case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
121 }
122 OS << '\n';
Kevin Enderbya5c78322009-07-13 21:03:15 +0000123}
124
Daniel Dunbara11af532009-06-24 01:03:06 +0000125void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
126 bool MakeAbsolute) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000127 assert(!Symbol->getSection() && "Cannot assign to a label!");
128
Daniel Dunbara11af532009-06-24 01:03:06 +0000129 if (MakeAbsolute) {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000130 OS << ".set " << Symbol << ", " << Value << '\n';
Daniel Dunbarb2d0b6b2009-08-14 19:10:46 +0000131
132 // HACK: If the value isn't already absolute, set the symbol value to
133 // itself, we want to use the .set absolute value, not the actual
134 // expression.
135 if (!Value.isAbsolute())
136 getContext().SetSymbolValue(Symbol, MCValue::get(Symbol));
137 else
138 getContext().SetSymbolValue(Symbol, Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000139 } else {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000140 OS << Symbol << " = " << Value << '\n';
Daniel Dunbarb2d0b6b2009-08-14 19:10:46 +0000141 getContext().SetSymbolValue(Symbol, Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000142 }
143}
144
145void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
146 SymbolAttr Attribute) {
147 switch (Attribute) {
148 case Global: OS << ".globl"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000149 case Hidden: OS << ".hidden"; break;
150 case IndirectSymbol: OS << ".indirect_symbol"; break;
151 case Internal: OS << ".internal"; break;
152 case LazyReference: OS << ".lazy_reference"; break;
153 case NoDeadStrip: OS << ".no_dead_strip"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000154 case PrivateExtern: OS << ".private_extern"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000155 case Protected: OS << ".protected"; break;
156 case Reference: OS << ".reference"; break;
157 case Weak: OS << ".weak"; break;
158 case WeakDefinition: OS << ".weak_definition"; break;
159 case WeakReference: OS << ".weak_reference"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000160 }
161
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000162 OS << ' ' << Symbol << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000163}
164
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000165void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000166 OS << ".desc" << ' ' << Symbol << ',' << DescValue << '\n';
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000167}
168
Kevin Enderby71148242009-07-14 21:35:03 +0000169void MCAsmStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000170 OS << ".lsym" << ' ' << Symbol << ',' << Value << '\n';
Kevin Enderby71148242009-07-14 21:35:03 +0000171}
172
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000173void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Chris Lattner1fc3d752009-07-09 17:25:12 +0000174 unsigned Pow2Alignment, bool IsLocal) {
175 if (IsLocal)
176 OS << ".lcomm";
177 else
178 OS << ".comm";
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000179 OS << ' ' << Symbol << ',' << Size;
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000180 if (Pow2Alignment != 0)
181 OS << ',' << Pow2Alignment;
182 OS << '\n';
183}
184
Chris Lattner9be3fee2009-07-10 22:20:30 +0000185void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
186 unsigned Size, unsigned Pow2Alignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000187 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000188 OS << ".zerofill ";
189
190 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000191 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000192 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Chris Lattner93b6db32009-08-08 23:39:42 +0000193
Chris Lattner9be3fee2009-07-10 22:20:30 +0000194 if (Symbol != NULL) {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000195 OS << ',' << Symbol << ',' << Size;
Chris Lattner9be3fee2009-07-10 22:20:30 +0000196 if (Pow2Alignment != 0)
197 OS << ',' << Pow2Alignment;
198 }
199 OS << '\n';
200}
201
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000202void MCAsmStreamer::EmitBytes(const StringRef &Data) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000203 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000204 for (unsigned i = 0, e = Data.size(); i != e; ++i)
Daniel Dunbare44313e2009-08-14 19:59:24 +0000205 OS << ".byte " << (unsigned) (unsigned char) Data[i] << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000206}
207
208void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000209 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000210 // Need target hooks to know how to print this.
211 switch (Size) {
212 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000213 llvm_unreachable("Invalid size for machine code value!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000214 case 1: OS << ".byte"; break;
Daniel Dunbarf5e75a12009-06-24 16:05:35 +0000215 case 2: OS << ".short"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000216 case 4: OS << ".long"; break;
217 case 8: OS << ".quad"; break;
218 }
219
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000220 OS << ' ' << truncateToSize(Value, Size) << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000221}
222
Daniel Dunbar84a29262009-06-24 19:25:34 +0000223void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
224 unsigned ValueSize,
225 unsigned MaxBytesToEmit) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000226 // Some assemblers don't support .balign, so we always emit as .p2align if
227 // this is a power of two. Otherwise we assume the client knows the target
228 // supports .balign and use that.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000229 unsigned Pow2 = Log2_32(ByteAlignment);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000230 bool IsPow2 = (1U << Pow2) == ByteAlignment;
Daniel Dunbar84a29262009-06-24 19:25:34 +0000231
232 switch (ValueSize) {
233 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000234 llvm_unreachable("Invalid size for machine code value!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000235 case 8:
Torok Edwinc23197a2009-07-14 16:55:14 +0000236 llvm_unreachable("Unsupported alignment size!");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000237 case 1: OS << (IsPow2 ? ".p2align" : ".balign"); break;
238 case 2: OS << (IsPow2 ? ".p2alignw" : ".balignw"); break;
239 case 4: OS << (IsPow2 ? ".p2alignl" : ".balignl"); break;
Daniel Dunbar84a29262009-06-24 19:25:34 +0000240 }
241
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000242 OS << ' ' << (IsPow2 ? Pow2 : ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000243
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000244 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000245 if (MaxBytesToEmit)
246 OS << ", " << MaxBytesToEmit;
247 OS << '\n';
248}
249
250void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
251 unsigned char Value) {
252 // FIXME: Verify that Offset is associated with the current section.
253 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
254}
255
Daniel Dunbarabde2982009-07-01 06:35:03 +0000256static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
257 if (Op.isReg())
258 return OS << "reg:" << Op.getReg();
259 if (Op.isImm())
260 return OS << "imm:" << Op.getImm();
261 if (Op.isMBBLabel())
262 return OS << "mbblabel:("
263 << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
264 assert(Op.isMCValue() && "Invalid operand!");
265 return OS << "val:" << Op.getMCValue();
266}
267
Daniel Dunbara11af532009-06-24 01:03:06 +0000268void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000269 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000270
271 // If we have an AsmPrinter, use that to print.
272 if (Printer) {
273 Printer->printMCInst(&Inst);
274 return;
275 }
276
277 // Otherwise fall back to a structural printing for now. Eventually we should
278 // always have access to the target specific printer.
Daniel Dunbarabde2982009-07-01 06:35:03 +0000279 OS << "MCInst("
280 << "opcode=" << Inst.getOpcode() << ", "
281 << "operands=[";
282 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
283 if (i)
284 OS << ", ";
285 OS << Inst.getOperand(i);
286 }
287 OS << "])\n";
Daniel Dunbara11af532009-06-24 01:03:06 +0000288}
289
290void MCAsmStreamer::Finish() {
291 OS.flush();
292}
293
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000294MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS,
Chris Lattnerf3ce0092009-08-17 04:23:44 +0000295 const TargetAsmInfo &TAI, AsmPrinter *AP) {
296 return new MCAsmStreamer(Context, OS, TAI, AP);
Daniel Dunbara11af532009-06-24 01:03:06 +0000297}