blob: e1812cee59ff77df47b9fb7d988a3a19b01d2ef4 [file] [log] [blame]
Daniel Dunbara5cc0032009-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 Dunbara5cc0032009-06-24 01:03:06 +000012#include "llvm/MC/MCContext.h"
Daniel Dunbarfc387ba2009-07-01 06:35:03 +000013#include "llvm/MC/MCInst.h"
Daniel Dunbara5cc0032009-06-24 01:03:06 +000014#include "llvm/MC/MCSection.h"
15#include "llvm/MC/MCSymbol.h"
16#include "llvm/MC/MCValue.h"
Edwin Török675d5622009-07-11 20:10:48 +000017#include "llvm/Support/ErrorHandling.h"
Daniel Dunbara5cc0032009-06-24 01:03:06 +000018#include "llvm/Support/raw_ostream.h"
Edwin Török675d5622009-07-11 20:10:48 +000019
Daniel Dunbara5cc0032009-06-24 01:03:06 +000020using namespace llvm;
21
22namespace {
23
24 class MCAsmStreamer : public MCStreamer {
25 raw_ostream &OS;
26
27 MCSection *CurSection;
28
29 public:
30 MCAsmStreamer(MCContext &Context, raw_ostream &_OS)
Edwin Török89774d02009-06-29 19:59:10 +000031 : MCStreamer(Context), OS(_OS), CurSection(0) {}
Daniel Dunbara5cc0032009-06-24 01:03:06 +000032 ~MCAsmStreamer() {}
33
34 /// @name MCStreamer Interface
35 /// @{
36
37 virtual void SwitchSection(MCSection *Section);
38
39 virtual void EmitLabel(MCSymbol *Symbol);
40
Kevin Enderby68c81be2009-07-16 17:56:39 +000041 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
Kevin Enderby3694db22009-07-13 21:03:15 +000042
Daniel Dunbara5cc0032009-06-24 01:03:06 +000043 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
44 bool MakeAbsolute = false);
45
46 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
47
Kevin Enderby8ae702b2009-07-14 18:17:10 +000048 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
49
Kevin Enderbyd7895a12009-07-14 21:35:03 +000050 virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value);
51
Chris Lattnere29dbe32009-07-07 20:30:46 +000052 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Chris Lattner18359222009-07-09 17:25:12 +000053 unsigned Pow2Alignment, bool IsLocal);
Chris Lattnere29dbe32009-07-07 20:30:46 +000054
Chris Lattner349007a2009-07-10 22:20:30 +000055 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL,
56 unsigned Size = 0, unsigned Pow2Alignment = 0);
57
Kevin Enderby58773c72009-07-13 23:15:14 +000058 virtual void AbortAssembly(const char *AbortReason = NULL);
59
Kevin Enderby7495f5b2009-07-15 15:30:11 +000060 virtual void DumpSymbolsandMacros(const char *FileName);
61
62 virtual void LoadSymbolsandMacros(const char *FileName);
63
Daniel Dunbara5cc0032009-06-24 01:03:06 +000064 virtual void EmitBytes(const char *Data, unsigned Length);
65
66 virtual void EmitValue(const MCValue &Value, unsigned Size);
67
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +000068 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
69 unsigned ValueSize = 1,
70 unsigned MaxBytesToEmit = 0);
71
72 virtual void EmitValueToOffset(const MCValue &Offset,
73 unsigned char Value = 0);
74
Daniel Dunbara5cc0032009-06-24 01:03:06 +000075 virtual void EmitInstruction(const MCInst &Inst);
76
77 virtual void Finish();
78
79 /// @}
80 };
81
82}
83
84/// Allow printing values directly to a raw_ostream.
Daniel Dunbar7c48d622009-06-25 21:03:18 +000085static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
Daniel Dunbara5cc0032009-06-24 01:03:06 +000086 if (Value.getSymA()) {
87 os << Value.getSymA()->getName();
88 if (Value.getSymB())
89 os << " - " << Value.getSymB()->getName();
Daniel Dunbar586d4632009-06-29 19:51:00 +000090 if (Value.getConstant())
91 os << " + " << Value.getConstant();
Daniel Dunbara5cc0032009-06-24 01:03:06 +000092 } else {
93 assert(!Value.getSymB() && "Invalid machine code value!");
Daniel Dunbar586d4632009-06-29 19:51:00 +000094 os << Value.getConstant();
Daniel Dunbara5cc0032009-06-24 01:03:06 +000095 }
96
97 return os;
98}
99
Daniel Dunbar7c48d622009-06-25 21:03:18 +0000100static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
101 assert(Bytes && "Invalid size!");
102 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
103}
104
105static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
106 return MCValue::get(Value.getSymA(), Value.getSymB(),
Daniel Dunbar586d4632009-06-29 19:51:00 +0000107 truncateToSize(Value.getConstant(), Bytes));
Daniel Dunbar7c48d622009-06-25 21:03:18 +0000108}
109
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000110void MCAsmStreamer::SwitchSection(MCSection *Section) {
111 if (Section != CurSection) {
112 CurSection = Section;
113
114 // FIXME: Really we would like the segment, flags, etc. to be separate
115 // values instead of embedded in the name. Not all assemblers understand all
116 // this stuff though.
117 OS << ".section " << Section->getName() << "\n";
118 }
119}
120
121void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar75a52922009-06-24 17:00:42 +0000122 assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
123 assert(CurSection && "Cannot emit before setting section!");
124 assert(!getContext().GetSymbolValue(Symbol) &&
125 "Cannot emit symbol which was directly assigned to!");
126
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000127 OS << Symbol->getName() << ":\n";
Daniel Dunbar75a52922009-06-24 17:00:42 +0000128 Symbol->setSection(CurSection);
Daniel Dunbar631e12c2009-06-29 23:46:59 +0000129 Symbol->setExternal(false);
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000130}
131
Kevin Enderby68c81be2009-07-16 17:56:39 +0000132void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
133 switch (Flag) {
134 case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
135 }
136 OS << '\n';
Kevin Enderby3694db22009-07-13 21:03:15 +0000137}
138
Kevin Enderby58773c72009-07-13 23:15:14 +0000139void MCAsmStreamer::AbortAssembly(const char *AbortReason) {
140 OS << ".abort";
141 if (AbortReason != NULL)
142 OS << ' ' << AbortReason;
143 OS << '\n';
144
145}
146
Kevin Enderby7495f5b2009-07-15 15:30:11 +0000147void MCAsmStreamer::DumpSymbolsandMacros(const char *FileName) {
148 OS << ".dump" << ' ' << FileName << '\n';
149}
150
151void MCAsmStreamer::LoadSymbolsandMacros(const char *FileName) {
152 OS << ".load" << ' ' << FileName << '\n';
153}
154
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000155void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
156 bool MakeAbsolute) {
Daniel Dunbar75a52922009-06-24 17:00:42 +0000157 assert(!Symbol->getSection() && "Cannot assign to a label!");
158
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000159 if (MakeAbsolute) {
160 OS << ".set " << Symbol->getName() << ", " << Value << '\n';
161 } else {
162 OS << Symbol->getName() << " = " << Value << '\n';
163 }
Daniel Dunbar75a52922009-06-24 17:00:42 +0000164
165 getContext().SetSymbolValue(Symbol, Value);
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000166}
167
168void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
169 SymbolAttr Attribute) {
170 switch (Attribute) {
171 case Global: OS << ".globl"; break;
Daniel Dunbarf7dafd32009-06-24 16:36:52 +0000172 case Hidden: OS << ".hidden"; break;
173 case IndirectSymbol: OS << ".indirect_symbol"; break;
174 case Internal: OS << ".internal"; break;
175 case LazyReference: OS << ".lazy_reference"; break;
176 case NoDeadStrip: OS << ".no_dead_strip"; break;
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000177 case PrivateExtern: OS << ".private_extern"; break;
Daniel Dunbarf7dafd32009-06-24 16:36:52 +0000178 case Protected: OS << ".protected"; break;
179 case Reference: OS << ".reference"; break;
180 case Weak: OS << ".weak"; break;
181 case WeakDefinition: OS << ".weak_definition"; break;
182 case WeakReference: OS << ".weak_reference"; break;
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000183 }
184
185 OS << ' ' << Symbol->getName() << '\n';
186}
187
Kevin Enderby8ae702b2009-07-14 18:17:10 +0000188void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
189 OS << ".desc" << ' ' << Symbol->getName() << ',' << DescValue << '\n';
190}
191
Kevin Enderbyd7895a12009-07-14 21:35:03 +0000192void MCAsmStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
193 OS << ".lsym" << ' ' << Symbol->getName() << ',' << Value << '\n';
194}
195
Chris Lattnere29dbe32009-07-07 20:30:46 +0000196void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Chris Lattner18359222009-07-09 17:25:12 +0000197 unsigned Pow2Alignment, bool IsLocal) {
198 if (IsLocal)
199 OS << ".lcomm";
200 else
201 OS << ".comm";
Chris Lattnere29dbe32009-07-07 20:30:46 +0000202 OS << ' ' << Symbol->getName() << ',' << Size;
203 if (Pow2Alignment != 0)
204 OS << ',' << Pow2Alignment;
205 OS << '\n';
206}
207
Chris Lattner349007a2009-07-10 22:20:30 +0000208void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
209 unsigned Size, unsigned Pow2Alignment) {
210 // Note: a .zerofill directive does not switch sections
211 // FIXME: Really we would like the segment and section names as well as the
212 // section type to be separate values instead of embedded in the name. Not
213 // all assemblers understand all this stuff though.
214 OS << ".zerofill " << Section->getName();
215 if (Symbol != NULL) {
216 OS << ',' << Symbol->getName() << ',' << Size;
217 if (Pow2Alignment != 0)
218 OS << ',' << Pow2Alignment;
219 }
220 OS << '\n';
221}
222
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000223void MCAsmStreamer::EmitBytes(const char *Data, unsigned Length) {
Daniel Dunbar75a52922009-06-24 17:00:42 +0000224 assert(CurSection && "Cannot emit contents before setting section!");
225 for (unsigned i = 0; i != Length; ++i)
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000226 OS << ".byte " << (unsigned) Data[i] << '\n';
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000227}
228
229void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
Daniel Dunbar75a52922009-06-24 17:00:42 +0000230 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000231 // Need target hooks to know how to print this.
232 switch (Size) {
233 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000234 llvm_unreachable("Invalid size for machine code value!");
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000235 case 1: OS << ".byte"; break;
Daniel Dunbar79262862009-06-24 16:05:35 +0000236 case 2: OS << ".short"; break;
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000237 case 4: OS << ".long"; break;
238 case 8: OS << ".quad"; break;
239 }
240
Daniel Dunbar7c48d622009-06-25 21:03:18 +0000241 OS << ' ' << truncateToSize(Value, Size) << '\n';
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000242}
243
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +0000244void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
245 unsigned ValueSize,
246 unsigned MaxBytesToEmit) {
Daniel Dunbar631e12c2009-06-29 23:46:59 +0000247 // Some assemblers don't support .balign, so we always emit as .p2align if
248 // this is a power of two. Otherwise we assume the client knows the target
249 // supports .balign and use that.
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +0000250 unsigned Pow2 = Log2_32(ByteAlignment);
Daniel Dunbar631e12c2009-06-29 23:46:59 +0000251 bool IsPow2 = (1U << Pow2) == ByteAlignment;
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +0000252
253 switch (ValueSize) {
254 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000255 llvm_unreachable("Invalid size for machine code value!");
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +0000256 case 8:
Edwin Törökbd448e32009-07-14 16:55:14 +0000257 llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar631e12c2009-06-29 23:46:59 +0000258 case 1: OS << (IsPow2 ? ".p2align" : ".balign"); break;
259 case 2: OS << (IsPow2 ? ".p2alignw" : ".balignw"); break;
260 case 4: OS << (IsPow2 ? ".p2alignl" : ".balignl"); break;
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +0000261 }
262
Daniel Dunbar631e12c2009-06-29 23:46:59 +0000263 OS << ' ' << (IsPow2 ? Pow2 : ByteAlignment);
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +0000264
Daniel Dunbar7c48d622009-06-25 21:03:18 +0000265 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +0000266 if (MaxBytesToEmit)
267 OS << ", " << MaxBytesToEmit;
268 OS << '\n';
269}
270
271void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
272 unsigned char Value) {
273 // FIXME: Verify that Offset is associated with the current section.
274 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
275}
276
Daniel Dunbarfc387ba2009-07-01 06:35:03 +0000277static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
278 if (Op.isReg())
279 return OS << "reg:" << Op.getReg();
280 if (Op.isImm())
281 return OS << "imm:" << Op.getImm();
282 if (Op.isMBBLabel())
283 return OS << "mbblabel:("
284 << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
285 assert(Op.isMCValue() && "Invalid operand!");
286 return OS << "val:" << Op.getMCValue();
287}
288
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000289void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar75a52922009-06-24 17:00:42 +0000290 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbarfc387ba2009-07-01 06:35:03 +0000291 // FIXME: Implement proper printing.
292 OS << "MCInst("
293 << "opcode=" << Inst.getOpcode() << ", "
294 << "operands=[";
295 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
296 if (i)
297 OS << ", ";
298 OS << Inst.getOperand(i);
299 }
300 OS << "])\n";
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000301}
302
303void MCAsmStreamer::Finish() {
304 OS.flush();
305}
306
307MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
308 return new MCAsmStreamer(Context, OS);
309}