blob: 3dfd8e947484cd0ce55187eb10d0018e4828625a [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"
Chris Lattner663c2d22009-08-19 06:12:02 +000017#include "llvm/Target/TargetAsmInfo.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000018#include "llvm/Support/ErrorHandling.h"
Chris Lattner663c2d22009-08-19 06:12:02 +000019#include "llvm/Support/MathExtras.h"
Daniel Dunbara11af532009-06-24 01:03:06 +000020#include "llvm/Support/raw_ostream.h"
21using namespace llvm;
22
23namespace {
24
Chris Lattner46a947d2009-08-17 04:17:34 +000025class MCAsmStreamer : public MCStreamer {
26 raw_ostream &OS;
Chris Lattnerf3ce0092009-08-17 04:23:44 +000027 const TargetAsmInfo &TAI;
Chris Lattner46a947d2009-08-17 04:17:34 +000028 AsmPrinter *Printer;
Chris Lattner46a947d2009-08-17 04:17:34 +000029public:
Chris Lattnerf3ce0092009-08-17 04:23:44 +000030 MCAsmStreamer(MCContext &Context, raw_ostream &_OS, const TargetAsmInfo &tai,
31 AsmPrinter *_AsmPrinter)
Chris Lattnerdabf07c2009-08-18 06:15:16 +000032 : MCStreamer(Context), OS(_OS), TAI(tai), Printer(_AsmPrinter) {}
Chris Lattner46a947d2009-08-17 04:17:34 +000033 ~MCAsmStreamer() {}
Daniel Dunbara11af532009-06-24 01:03:06 +000034
Chris Lattner46a947d2009-08-17 04:17:34 +000035 /// @name MCStreamer Interface
36 /// @{
Daniel Dunbara11af532009-06-24 01:03:06 +000037
Chris Lattner975780b2009-08-17 05:49:08 +000038 virtual void SwitchSection(const MCSection *Section);
Daniel Dunbara11af532009-06-24 01:03:06 +000039
Chris Lattner46a947d2009-08-17 04:17:34 +000040 virtual void EmitLabel(MCSymbol *Symbol);
Daniel Dunbara11af532009-06-24 01:03:06 +000041
Chris Lattner46a947d2009-08-17 04:17:34 +000042 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
Daniel Dunbara11af532009-06-24 01:03:06 +000043
Chris Lattner46a947d2009-08-17 04:17:34 +000044 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
45 bool MakeAbsolute = false);
Daniel Dunbara11af532009-06-24 01:03:06 +000046
Chris Lattner46a947d2009-08-17 04:17:34 +000047 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
Kevin Enderbya5c78322009-07-13 21:03:15 +000048
Chris Lattner46a947d2009-08-17 04:17:34 +000049 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Daniel Dunbara11af532009-06-24 01:03:06 +000050
Chris Lattner46a947d2009-08-17 04:17:34 +000051 virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value);
Daniel Dunbara11af532009-06-24 01:03:06 +000052
Chris Lattner46a947d2009-08-17 04:17:34 +000053 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
54 unsigned Pow2Alignment, bool IsLocal);
Kevin Enderby95cf30c2009-07-14 18:17:10 +000055
Chris Lattner46a947d2009-08-17 04:17:34 +000056 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL,
57 unsigned Size = 0, unsigned Pow2Alignment = 0);
Kevin Enderby71148242009-07-14 21:35:03 +000058
Chris Lattner46a947d2009-08-17 04:17:34 +000059 virtual void EmitBytes(const StringRef &Data);
Chris Lattner4e4db7a2009-07-07 20:30:46 +000060
Chris Lattner46a947d2009-08-17 04:17:34 +000061 virtual void EmitValue(const MCValue &Value, unsigned Size);
Chris Lattner9be3fee2009-07-10 22:20:30 +000062
Chris Lattner46a947d2009-08-17 04:17:34 +000063 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
64 unsigned ValueSize = 1,
65 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +000066
Chris Lattner46a947d2009-08-17 04:17:34 +000067 virtual void EmitValueToOffset(const MCValue &Offset,
68 unsigned char Value = 0);
69
70 virtual void EmitInstruction(const MCInst &Inst);
Daniel Dunbara11af532009-06-24 01:03:06 +000071
Chris Lattner46a947d2009-08-17 04:17:34 +000072 virtual void Finish();
73
74 /// @}
75};
Daniel Dunbar84a29262009-06-24 19:25:34 +000076
Chris Lattner46a947d2009-08-17 04:17:34 +000077} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +000078
Daniel Dunbarad4555c2009-07-31 23:04:32 +000079/// Allow printing symbols directly to a raw_ostream with proper quoting.
80static inline raw_ostream &operator<<(raw_ostream &os, const MCSymbol *S) {
Daniel Dunbarc22e0b22009-08-14 03:48:55 +000081 S->print(os);
Daniel Dunbarc22e0b22009-08-14 03:48:55 +000082 return os;
Daniel Dunbarad4555c2009-07-31 23:04:32 +000083}
84
Daniel Dunbara11af532009-06-24 01:03:06 +000085/// Allow printing values directly to a raw_ostream.
Daniel Dunbar304f6a42009-06-25 21:03:18 +000086static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
Daniel Dunbarc22e0b22009-08-14 03:48:55 +000087 Value.print(os);
Daniel Dunbara11af532009-06-24 01:03:06 +000088 return os;
89}
90
Daniel Dunbar304f6a42009-06-25 21:03:18 +000091static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
92 assert(Bytes && "Invalid size!");
93 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
94}
95
96static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
97 return MCValue::get(Value.getSymA(), Value.getSymB(),
Daniel Dunbar7908a6a2009-06-29 19:51:00 +000098 truncateToSize(Value.getConstant(), Bytes));
Daniel Dunbar304f6a42009-06-25 21:03:18 +000099}
100
Chris Lattner975780b2009-08-17 05:49:08 +0000101void MCAsmStreamer::SwitchSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000102 assert(Section && "Cannot switch to a null section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000103 if (Section != CurSection) {
104 CurSection = Section;
Chris Lattner975780b2009-08-17 05:49:08 +0000105 Section->PrintSwitchToSection(TAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000106 }
107}
108
109void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000110 assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
111 assert(CurSection && "Cannot emit before setting section!");
112 assert(!getContext().GetSymbolValue(Symbol) &&
113 "Cannot emit symbol which was directly assigned to!");
114
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000115 OS << Symbol << ":\n";
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000116 Symbol->setSection(CurSection);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000117 Symbol->setExternal(false);
Daniel Dunbara11af532009-06-24 01:03:06 +0000118}
119
Kevin Enderbyf96db462009-07-16 17:56:39 +0000120void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
121 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000122 default: assert(0 && "Invalid flag!");
Kevin Enderbyf96db462009-07-16 17:56:39 +0000123 case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
124 }
125 OS << '\n';
Kevin Enderbya5c78322009-07-13 21:03:15 +0000126}
127
Daniel Dunbara11af532009-06-24 01:03:06 +0000128void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
129 bool MakeAbsolute) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000130 assert(!Symbol->getSection() && "Cannot assign to a label!");
131
Daniel Dunbara11af532009-06-24 01:03:06 +0000132 if (MakeAbsolute) {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000133 OS << ".set " << Symbol << ", " << Value << '\n';
Daniel Dunbarb2d0b6b2009-08-14 19:10:46 +0000134
135 // HACK: If the value isn't already absolute, set the symbol value to
136 // itself, we want to use the .set absolute value, not the actual
137 // expression.
138 if (!Value.isAbsolute())
139 getContext().SetSymbolValue(Symbol, MCValue::get(Symbol));
140 else
141 getContext().SetSymbolValue(Symbol, Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000142 } else {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000143 OS << Symbol << " = " << Value << '\n';
Daniel Dunbarb2d0b6b2009-08-14 19:10:46 +0000144 getContext().SetSymbolValue(Symbol, Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000145 }
146}
147
148void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
149 SymbolAttr Attribute) {
150 switch (Attribute) {
151 case Global: OS << ".globl"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000152 case Hidden: OS << ".hidden"; break;
153 case IndirectSymbol: OS << ".indirect_symbol"; break;
154 case Internal: OS << ".internal"; break;
155 case LazyReference: OS << ".lazy_reference"; break;
156 case NoDeadStrip: OS << ".no_dead_strip"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000157 case PrivateExtern: OS << ".private_extern"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000158 case Protected: OS << ".protected"; break;
159 case Reference: OS << ".reference"; break;
160 case Weak: OS << ".weak"; break;
161 case WeakDefinition: OS << ".weak_definition"; break;
162 case WeakReference: OS << ".weak_reference"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000163 }
164
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000165 OS << ' ' << Symbol << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000166}
167
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000168void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000169 OS << ".desc" << ' ' << Symbol << ',' << DescValue << '\n';
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000170}
171
Kevin Enderby71148242009-07-14 21:35:03 +0000172void MCAsmStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000173 OS << ".lsym" << ' ' << Symbol << ',' << Value << '\n';
Kevin Enderby71148242009-07-14 21:35:03 +0000174}
175
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000176void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Chris Lattner1fc3d752009-07-09 17:25:12 +0000177 unsigned Pow2Alignment, bool IsLocal) {
178 if (IsLocal)
179 OS << ".lcomm";
180 else
181 OS << ".comm";
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000182 OS << ' ' << Symbol << ',' << Size;
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000183 if (Pow2Alignment != 0)
184 OS << ',' << Pow2Alignment;
185 OS << '\n';
186}
187
Chris Lattner9be3fee2009-07-10 22:20:30 +0000188void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
189 unsigned Size, unsigned Pow2Alignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000190 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000191 OS << ".zerofill ";
192
193 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000194 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000195 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Chris Lattner93b6db32009-08-08 23:39:42 +0000196
Chris Lattner9be3fee2009-07-10 22:20:30 +0000197 if (Symbol != NULL) {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000198 OS << ',' << Symbol << ',' << Size;
Chris Lattner9be3fee2009-07-10 22:20:30 +0000199 if (Pow2Alignment != 0)
200 OS << ',' << Pow2Alignment;
201 }
202 OS << '\n';
203}
204
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000205void MCAsmStreamer::EmitBytes(const StringRef &Data) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000206 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000207 for (unsigned i = 0, e = Data.size(); i != e; ++i)
Daniel Dunbare44313e2009-08-14 19:59:24 +0000208 OS << ".byte " << (unsigned) (unsigned char) Data[i] << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000209}
210
211void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000212 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000213 // Need target hooks to know how to print this.
214 switch (Size) {
215 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000216 llvm_unreachable("Invalid size for machine code value!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000217 case 1: OS << ".byte"; break;
Daniel Dunbarf5e75a12009-06-24 16:05:35 +0000218 case 2: OS << ".short"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000219 case 4: OS << ".long"; break;
220 case 8: OS << ".quad"; break;
221 }
222
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000223 OS << ' ' << truncateToSize(Value, Size) << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000224}
225
Daniel Dunbar84a29262009-06-24 19:25:34 +0000226void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
227 unsigned ValueSize,
228 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000229 // Some assemblers don't support non-power of two alignments, so we always
230 // emit alignments as a power of two if possible.
231 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000232 switch (ValueSize) {
233 default: llvm_unreachable("Invalid size for machine code value!");
234 case 1: OS << TAI.getAlignDirective(); break;
235 // FIXME: use TAI for this!
236 case 2: OS << ".p2alignw "; break;
237 case 4: OS << ".p2alignl "; break;
238 case 8: llvm_unreachable("Unsupported alignment size!");
239 }
Chris Lattner663c2d22009-08-19 06:12:02 +0000240
241 if (TAI.getAlignmentIsInBytes())
242 OS << ByteAlignment;
243 else
244 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000245
Chris Lattner663c2d22009-08-19 06:12:02 +0000246 if (Value || MaxBytesToEmit) {
247 OS << ", " << truncateToSize(Value, ValueSize);
248
249 if (MaxBytesToEmit)
250 OS << ", " << MaxBytesToEmit;
251 }
252 OS << '\n';
253 return;
254 }
255
256 // Non-power of two alignment. This is not widely supported by assemblers.
257 // FIXME: Parameterize this based on TAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000258 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000259 default: llvm_unreachable("Invalid size for machine code value!");
260 case 1: OS << ".balign"; break;
261 case 2: OS << ".balignw"; break;
262 case 4: OS << ".balignl"; break;
263 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000264 }
265
Chris Lattner663c2d22009-08-19 06:12:02 +0000266 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000267 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000268 if (MaxBytesToEmit)
269 OS << ", " << MaxBytesToEmit;
270 OS << '\n';
271}
272
273void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
274 unsigned char Value) {
275 // FIXME: Verify that Offset is associated with the current section.
276 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
277}
278
Daniel Dunbarabde2982009-07-01 06:35:03 +0000279static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
280 if (Op.isReg())
281 return OS << "reg:" << Op.getReg();
282 if (Op.isImm())
283 return OS << "imm:" << Op.getImm();
284 if (Op.isMBBLabel())
285 return OS << "mbblabel:("
286 << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
287 assert(Op.isMCValue() && "Invalid operand!");
288 return OS << "val:" << Op.getMCValue();
289}
290
Daniel Dunbara11af532009-06-24 01:03:06 +0000291void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000292 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000293
294 // If we have an AsmPrinter, use that to print.
295 if (Printer) {
296 Printer->printMCInst(&Inst);
297 return;
298 }
299
300 // Otherwise fall back to a structural printing for now. Eventually we should
301 // always have access to the target specific printer.
Daniel Dunbarabde2982009-07-01 06:35:03 +0000302 OS << "MCInst("
303 << "opcode=" << Inst.getOpcode() << ", "
304 << "operands=[";
305 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
306 if (i)
307 OS << ", ";
308 OS << Inst.getOperand(i);
309 }
310 OS << "])\n";
Daniel Dunbara11af532009-06-24 01:03:06 +0000311}
312
313void MCAsmStreamer::Finish() {
314 OS.flush();
315}
316
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000317MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS,
Chris Lattnerf3ce0092009-08-17 04:23:44 +0000318 const TargetAsmInfo &TAI, AsmPrinter *AP) {
319 return new MCAsmStreamer(Context, OS, TAI, AP);
Daniel Dunbara11af532009-06-24 01:03:06 +0000320}