blob: 0eed1206aa1be539b0bafb8225088e0d93247bf7 [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;
25 AsmPrinter *Printer;
26 MCSection *CurSection;
27public:
28 MCAsmStreamer(MCContext &Context, raw_ostream &_OS, AsmPrinter *_AsmPrinter)
29 : MCStreamer(Context), OS(_OS), Printer(_AsmPrinter), CurSection(0) {}
30 ~MCAsmStreamer() {}
Daniel Dunbara11af532009-06-24 01:03:06 +000031
Chris Lattner46a947d2009-08-17 04:17:34 +000032 /// @name MCStreamer Interface
33 /// @{
Daniel Dunbara11af532009-06-24 01:03:06 +000034
Chris Lattner46a947d2009-08-17 04:17:34 +000035 virtual void SwitchSection(MCSection *Section);
Daniel Dunbara11af532009-06-24 01:03:06 +000036
Chris Lattner46a947d2009-08-17 04:17:34 +000037 virtual void EmitLabel(MCSymbol *Symbol);
Daniel Dunbara11af532009-06-24 01:03:06 +000038
Chris Lattner46a947d2009-08-17 04:17:34 +000039 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
Daniel Dunbara11af532009-06-24 01:03:06 +000040
Chris Lattner46a947d2009-08-17 04:17:34 +000041 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
42 bool MakeAbsolute = false);
Daniel Dunbara11af532009-06-24 01:03:06 +000043
Chris Lattner46a947d2009-08-17 04:17:34 +000044 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
Kevin Enderbya5c78322009-07-13 21:03:15 +000045
Chris Lattner46a947d2009-08-17 04:17:34 +000046 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Daniel Dunbara11af532009-06-24 01:03:06 +000047
Chris Lattner46a947d2009-08-17 04:17:34 +000048 virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value);
Daniel Dunbara11af532009-06-24 01:03:06 +000049
Chris Lattner46a947d2009-08-17 04:17:34 +000050 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
51 unsigned Pow2Alignment, bool IsLocal);
Kevin Enderby95cf30c2009-07-14 18:17:10 +000052
Chris Lattner46a947d2009-08-17 04:17:34 +000053 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL,
54 unsigned Size = 0, unsigned Pow2Alignment = 0);
Kevin Enderby71148242009-07-14 21:35:03 +000055
Chris Lattner46a947d2009-08-17 04:17:34 +000056 virtual void EmitBytes(const StringRef &Data);
Chris Lattner4e4db7a2009-07-07 20:30:46 +000057
Chris Lattner46a947d2009-08-17 04:17:34 +000058 virtual void EmitValue(const MCValue &Value, unsigned Size);
Chris Lattner9be3fee2009-07-10 22:20:30 +000059
Chris Lattner46a947d2009-08-17 04:17:34 +000060 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
61 unsigned ValueSize = 1,
62 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +000063
Chris Lattner46a947d2009-08-17 04:17:34 +000064 virtual void EmitValueToOffset(const MCValue &Offset,
65 unsigned char Value = 0);
66
67 virtual void EmitInstruction(const MCInst &Inst);
Daniel Dunbara11af532009-06-24 01:03:06 +000068
Chris Lattner46a947d2009-08-17 04:17:34 +000069 virtual void Finish();
70
71 /// @}
72};
Daniel Dunbar84a29262009-06-24 19:25:34 +000073
Chris Lattner46a947d2009-08-17 04:17:34 +000074} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +000075
Daniel Dunbarad4555c2009-07-31 23:04:32 +000076/// Allow printing symbols directly to a raw_ostream with proper quoting.
77static inline raw_ostream &operator<<(raw_ostream &os, const MCSymbol *S) {
Daniel Dunbarc22e0b22009-08-14 03:48:55 +000078 S->print(os);
Daniel Dunbarc22e0b22009-08-14 03:48:55 +000079 return os;
Daniel Dunbarad4555c2009-07-31 23:04:32 +000080}
81
Daniel Dunbara11af532009-06-24 01:03:06 +000082/// Allow printing values directly to a raw_ostream.
Daniel Dunbar304f6a42009-06-25 21:03:18 +000083static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
Daniel Dunbarc22e0b22009-08-14 03:48:55 +000084 Value.print(os);
Daniel Dunbara11af532009-06-24 01:03:06 +000085 return os;
86}
87
Daniel Dunbar304f6a42009-06-25 21:03:18 +000088static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
89 assert(Bytes && "Invalid size!");
90 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
91}
92
93static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
94 return MCValue::get(Value.getSymA(), Value.getSymB(),
Daniel Dunbar7908a6a2009-06-29 19:51:00 +000095 truncateToSize(Value.getConstant(), Bytes));
Daniel Dunbar304f6a42009-06-25 21:03:18 +000096}
97
Daniel Dunbara11af532009-06-24 01:03:06 +000098void MCAsmStreamer::SwitchSection(MCSection *Section) {
99 if (Section != CurSection) {
100 CurSection = Section;
101
Chris Lattner93b6db32009-08-08 23:39:42 +0000102 // FIXME: Needs TargetAsmInfo!
103 Section->PrintSwitchToSection(*(const TargetAsmInfo*)0, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000104 }
105}
106
107void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000108 assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
109 assert(CurSection && "Cannot emit before setting section!");
110 assert(!getContext().GetSymbolValue(Symbol) &&
111 "Cannot emit symbol which was directly assigned to!");
112
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000113 OS << Symbol << ":\n";
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000114 Symbol->setSection(CurSection);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000115 Symbol->setExternal(false);
Daniel Dunbara11af532009-06-24 01:03:06 +0000116}
117
Kevin Enderbyf96db462009-07-16 17:56:39 +0000118void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
119 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000120 default: assert(0 && "Invalid flag!");
Kevin Enderbyf96db462009-07-16 17:56:39 +0000121 case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
122 }
123 OS << '\n';
Kevin Enderbya5c78322009-07-13 21:03:15 +0000124}
125
Daniel Dunbara11af532009-06-24 01:03:06 +0000126void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
127 bool MakeAbsolute) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000128 assert(!Symbol->getSection() && "Cannot assign to a label!");
129
Daniel Dunbara11af532009-06-24 01:03:06 +0000130 if (MakeAbsolute) {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000131 OS << ".set " << Symbol << ", " << Value << '\n';
Daniel Dunbarb2d0b6b2009-08-14 19:10:46 +0000132
133 // HACK: If the value isn't already absolute, set the symbol value to
134 // itself, we want to use the .set absolute value, not the actual
135 // expression.
136 if (!Value.isAbsolute())
137 getContext().SetSymbolValue(Symbol, MCValue::get(Symbol));
138 else
139 getContext().SetSymbolValue(Symbol, Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000140 } else {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000141 OS << Symbol << " = " << Value << '\n';
Daniel Dunbarb2d0b6b2009-08-14 19:10:46 +0000142 getContext().SetSymbolValue(Symbol, Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000143 }
144}
145
146void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
147 SymbolAttr Attribute) {
148 switch (Attribute) {
149 case Global: OS << ".globl"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000150 case Hidden: OS << ".hidden"; break;
151 case IndirectSymbol: OS << ".indirect_symbol"; break;
152 case Internal: OS << ".internal"; break;
153 case LazyReference: OS << ".lazy_reference"; break;
154 case NoDeadStrip: OS << ".no_dead_strip"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000155 case PrivateExtern: OS << ".private_extern"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000156 case Protected: OS << ".protected"; break;
157 case Reference: OS << ".reference"; break;
158 case Weak: OS << ".weak"; break;
159 case WeakDefinition: OS << ".weak_definition"; break;
160 case WeakReference: OS << ".weak_reference"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000161 }
162
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000163 OS << ' ' << Symbol << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000164}
165
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000166void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000167 OS << ".desc" << ' ' << Symbol << ',' << DescValue << '\n';
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000168}
169
Kevin Enderby71148242009-07-14 21:35:03 +0000170void MCAsmStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000171 OS << ".lsym" << ' ' << Symbol << ',' << Value << '\n';
Kevin Enderby71148242009-07-14 21:35:03 +0000172}
173
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000174void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Chris Lattner1fc3d752009-07-09 17:25:12 +0000175 unsigned Pow2Alignment, bool IsLocal) {
176 if (IsLocal)
177 OS << ".lcomm";
178 else
179 OS << ".comm";
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000180 OS << ' ' << Symbol << ',' << Size;
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000181 if (Pow2Alignment != 0)
182 OS << ',' << Pow2Alignment;
183 OS << '\n';
184}
185
Chris Lattner9be3fee2009-07-10 22:20:30 +0000186void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
187 unsigned Size, unsigned Pow2Alignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000188 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000189 OS << ".zerofill ";
190
191 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000192 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000193 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Chris Lattner93b6db32009-08-08 23:39:42 +0000194
Chris Lattner9be3fee2009-07-10 22:20:30 +0000195 if (Symbol != NULL) {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000196 OS << ',' << Symbol << ',' << Size;
Chris Lattner9be3fee2009-07-10 22:20:30 +0000197 if (Pow2Alignment != 0)
198 OS << ',' << Pow2Alignment;
199 }
200 OS << '\n';
201}
202
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000203void MCAsmStreamer::EmitBytes(const StringRef &Data) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000204 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000205 for (unsigned i = 0, e = Data.size(); i != e; ++i)
Daniel Dunbare44313e2009-08-14 19:59:24 +0000206 OS << ".byte " << (unsigned) (unsigned char) Data[i] << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000207}
208
209void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000210 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000211 // Need target hooks to know how to print this.
212 switch (Size) {
213 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000214 llvm_unreachable("Invalid size for machine code value!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000215 case 1: OS << ".byte"; break;
Daniel Dunbarf5e75a12009-06-24 16:05:35 +0000216 case 2: OS << ".short"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000217 case 4: OS << ".long"; break;
218 case 8: OS << ".quad"; break;
219 }
220
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000221 OS << ' ' << truncateToSize(Value, Size) << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000222}
223
Daniel Dunbar84a29262009-06-24 19:25:34 +0000224void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
225 unsigned ValueSize,
226 unsigned MaxBytesToEmit) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000227 // Some assemblers don't support .balign, so we always emit as .p2align if
228 // this is a power of two. Otherwise we assume the client knows the target
229 // supports .balign and use that.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000230 unsigned Pow2 = Log2_32(ByteAlignment);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000231 bool IsPow2 = (1U << Pow2) == ByteAlignment;
Daniel Dunbar84a29262009-06-24 19:25:34 +0000232
233 switch (ValueSize) {
234 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000235 llvm_unreachable("Invalid size for machine code value!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000236 case 8:
Torok Edwinc23197a2009-07-14 16:55:14 +0000237 llvm_unreachable("Unsupported alignment size!");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000238 case 1: OS << (IsPow2 ? ".p2align" : ".balign"); break;
239 case 2: OS << (IsPow2 ? ".p2alignw" : ".balignw"); break;
240 case 4: OS << (IsPow2 ? ".p2alignl" : ".balignl"); break;
Daniel Dunbar84a29262009-06-24 19:25:34 +0000241 }
242
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000243 OS << ' ' << (IsPow2 ? Pow2 : ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000244
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000245 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000246 if (MaxBytesToEmit)
247 OS << ", " << MaxBytesToEmit;
248 OS << '\n';
249}
250
251void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
252 unsigned char Value) {
253 // FIXME: Verify that Offset is associated with the current section.
254 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
255}
256
Daniel Dunbarabde2982009-07-01 06:35:03 +0000257static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
258 if (Op.isReg())
259 return OS << "reg:" << Op.getReg();
260 if (Op.isImm())
261 return OS << "imm:" << Op.getImm();
262 if (Op.isMBBLabel())
263 return OS << "mbblabel:("
264 << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
265 assert(Op.isMCValue() && "Invalid operand!");
266 return OS << "val:" << Op.getMCValue();
267}
268
Daniel Dunbara11af532009-06-24 01:03:06 +0000269void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000270 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000271
272 // If we have an AsmPrinter, use that to print.
273 if (Printer) {
274 Printer->printMCInst(&Inst);
275 return;
276 }
277
278 // Otherwise fall back to a structural printing for now. Eventually we should
279 // always have access to the target specific printer.
Daniel Dunbarabde2982009-07-01 06:35:03 +0000280 OS << "MCInst("
281 << "opcode=" << Inst.getOpcode() << ", "
282 << "operands=[";
283 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
284 if (i)
285 OS << ", ";
286 OS << Inst.getOperand(i);
287 }
288 OS << "])\n";
Daniel Dunbara11af532009-06-24 01:03:06 +0000289}
290
291void MCAsmStreamer::Finish() {
292 OS.flush();
293}
294
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000295MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS,
296 AsmPrinter *AP) {
297 return new MCAsmStreamer(Context, OS, AP);
Daniel Dunbara11af532009-06-24 01:03:06 +0000298}