blob: 2a15783478b278a971183477c06893898a064b22 [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 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"
Daniel Dunbara11af532009-06-24 01:03:06 +000014#include "llvm/MC/MCSection.h"
15#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"
Torok Edwinc25e7582009-07-11 20:10:48 +000019
Daniel Dunbara11af532009-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)
Torok Edwin48de5d82009-06-29 19:59:10 +000031 : MCStreamer(Context), OS(_OS), CurSection(0) {}
Daniel Dunbara11af532009-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 Enderbya5c78322009-07-13 21:03:15 +000041 virtual void SubsectionsViaSymbols(void);
42
Daniel Dunbara11af532009-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 Enderby95cf30c2009-07-14 18:17:10 +000048 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
49
Kevin Enderby71148242009-07-14 21:35:03 +000050 virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value);
51
Chris Lattner4e4db7a2009-07-07 20:30:46 +000052 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Chris Lattner1fc3d752009-07-09 17:25:12 +000053 unsigned Pow2Alignment, bool IsLocal);
Chris Lattner4e4db7a2009-07-07 20:30:46 +000054
Chris Lattner9be3fee2009-07-10 22:20:30 +000055 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL,
56 unsigned Size = 0, unsigned Pow2Alignment = 0);
57
Kevin Enderby5f1f0b82009-07-13 23:15:14 +000058 virtual void AbortAssembly(const char *AbortReason = NULL);
59
Kevin Enderby1f049b22009-07-14 23:21:55 +000060 virtual void SwitchInputAssemblyFile(const char *FileName);
61
Daniel Dunbara11af532009-06-24 01:03:06 +000062 virtual void EmitBytes(const char *Data, unsigned Length);
63
64 virtual void EmitValue(const MCValue &Value, unsigned Size);
65
Daniel Dunbar84a29262009-06-24 19:25:34 +000066 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
67 unsigned ValueSize = 1,
68 unsigned MaxBytesToEmit = 0);
69
70 virtual void EmitValueToOffset(const MCValue &Offset,
71 unsigned char Value = 0);
72
Daniel Dunbara11af532009-06-24 01:03:06 +000073 virtual void EmitInstruction(const MCInst &Inst);
74
75 virtual void Finish();
76
77 /// @}
78 };
79
80}
81
82/// 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 Dunbara11af532009-06-24 01:03:06 +000084 if (Value.getSymA()) {
85 os << Value.getSymA()->getName();
86 if (Value.getSymB())
87 os << " - " << Value.getSymB()->getName();
Daniel Dunbar7908a6a2009-06-29 19:51:00 +000088 if (Value.getConstant())
89 os << " + " << Value.getConstant();
Daniel Dunbara11af532009-06-24 01:03:06 +000090 } else {
91 assert(!Value.getSymB() && "Invalid machine code value!");
Daniel Dunbar7908a6a2009-06-29 19:51:00 +000092 os << Value.getConstant();
Daniel Dunbara11af532009-06-24 01:03:06 +000093 }
94
95 return os;
96}
97
Daniel Dunbar304f6a42009-06-25 21:03:18 +000098static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
99 assert(Bytes && "Invalid size!");
100 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
101}
102
103static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
104 return MCValue::get(Value.getSymA(), Value.getSymB(),
Daniel Dunbar7908a6a2009-06-29 19:51:00 +0000105 truncateToSize(Value.getConstant(), Bytes));
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000106}
107
Daniel Dunbara11af532009-06-24 01:03:06 +0000108void MCAsmStreamer::SwitchSection(MCSection *Section) {
109 if (Section != CurSection) {
110 CurSection = Section;
111
112 // FIXME: Really we would like the segment, flags, etc. to be separate
113 // values instead of embedded in the name. Not all assemblers understand all
114 // this stuff though.
115 OS << ".section " << Section->getName() << "\n";
116 }
117}
118
119void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000120 assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
121 assert(CurSection && "Cannot emit before setting section!");
122 assert(!getContext().GetSymbolValue(Symbol) &&
123 "Cannot emit symbol which was directly assigned to!");
124
Daniel Dunbara11af532009-06-24 01:03:06 +0000125 OS << Symbol->getName() << ":\n";
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000126 Symbol->setSection(CurSection);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000127 Symbol->setExternal(false);
Daniel Dunbara11af532009-06-24 01:03:06 +0000128}
129
Kevin Enderbya5c78322009-07-13 21:03:15 +0000130void MCAsmStreamer::SubsectionsViaSymbols(void) {
131 OS << ".subsections_via_symbols\n";
132}
133
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000134void MCAsmStreamer::AbortAssembly(const char *AbortReason) {
135 OS << ".abort";
136 if (AbortReason != NULL)
137 OS << ' ' << AbortReason;
138 OS << '\n';
139
140}
141
Kevin Enderby1f049b22009-07-14 23:21:55 +0000142void MCAsmStreamer::SwitchInputAssemblyFile(const char *FileName) {
143 OS << ".include" << ' ' << FileName << '\n';
144}
145
Daniel Dunbara11af532009-06-24 01:03:06 +0000146void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
147 bool MakeAbsolute) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000148 assert(!Symbol->getSection() && "Cannot assign to a label!");
149
Daniel Dunbara11af532009-06-24 01:03:06 +0000150 if (MakeAbsolute) {
151 OS << ".set " << Symbol->getName() << ", " << Value << '\n';
152 } else {
153 OS << Symbol->getName() << " = " << Value << '\n';
154 }
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000155
156 getContext().SetSymbolValue(Symbol, Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000157}
158
159void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
160 SymbolAttr Attribute) {
161 switch (Attribute) {
162 case Global: OS << ".globl"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000163 case Hidden: OS << ".hidden"; break;
164 case IndirectSymbol: OS << ".indirect_symbol"; break;
165 case Internal: OS << ".internal"; break;
166 case LazyReference: OS << ".lazy_reference"; break;
167 case NoDeadStrip: OS << ".no_dead_strip"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000168 case PrivateExtern: OS << ".private_extern"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000169 case Protected: OS << ".protected"; break;
170 case Reference: OS << ".reference"; break;
171 case Weak: OS << ".weak"; break;
172 case WeakDefinition: OS << ".weak_definition"; break;
173 case WeakReference: OS << ".weak_reference"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000174 }
175
176 OS << ' ' << Symbol->getName() << '\n';
177}
178
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000179void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
180 OS << ".desc" << ' ' << Symbol->getName() << ',' << DescValue << '\n';
181}
182
Kevin Enderby71148242009-07-14 21:35:03 +0000183void MCAsmStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
184 OS << ".lsym" << ' ' << Symbol->getName() << ',' << Value << '\n';
185}
186
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000187void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Chris Lattner1fc3d752009-07-09 17:25:12 +0000188 unsigned Pow2Alignment, bool IsLocal) {
189 if (IsLocal)
190 OS << ".lcomm";
191 else
192 OS << ".comm";
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000193 OS << ' ' << Symbol->getName() << ',' << Size;
194 if (Pow2Alignment != 0)
195 OS << ',' << Pow2Alignment;
196 OS << '\n';
197}
198
Chris Lattner9be3fee2009-07-10 22:20:30 +0000199void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
200 unsigned Size, unsigned Pow2Alignment) {
201 // Note: a .zerofill directive does not switch sections
202 // FIXME: Really we would like the segment and section names as well as the
203 // section type to be separate values instead of embedded in the name. Not
204 // all assemblers understand all this stuff though.
205 OS << ".zerofill " << Section->getName();
206 if (Symbol != NULL) {
207 OS << ',' << Symbol->getName() << ',' << Size;
208 if (Pow2Alignment != 0)
209 OS << ',' << Pow2Alignment;
210 }
211 OS << '\n';
212}
213
Daniel Dunbara11af532009-06-24 01:03:06 +0000214void MCAsmStreamer::EmitBytes(const char *Data, unsigned Length) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000215 assert(CurSection && "Cannot emit contents before setting section!");
216 for (unsigned i = 0; i != Length; ++i)
Daniel Dunbara11af532009-06-24 01:03:06 +0000217 OS << ".byte " << (unsigned) Data[i] << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000218}
219
220void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000221 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000222 // Need target hooks to know how to print this.
223 switch (Size) {
224 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000225 llvm_unreachable("Invalid size for machine code value!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000226 case 1: OS << ".byte"; break;
Daniel Dunbarf5e75a12009-06-24 16:05:35 +0000227 case 2: OS << ".short"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000228 case 4: OS << ".long"; break;
229 case 8: OS << ".quad"; break;
230 }
231
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000232 OS << ' ' << truncateToSize(Value, Size) << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000233}
234
Daniel Dunbar84a29262009-06-24 19:25:34 +0000235void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
236 unsigned ValueSize,
237 unsigned MaxBytesToEmit) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000238 // Some assemblers don't support .balign, so we always emit as .p2align if
239 // this is a power of two. Otherwise we assume the client knows the target
240 // supports .balign and use that.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000241 unsigned Pow2 = Log2_32(ByteAlignment);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000242 bool IsPow2 = (1U << Pow2) == ByteAlignment;
Daniel Dunbar84a29262009-06-24 19:25:34 +0000243
244 switch (ValueSize) {
245 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000246 llvm_unreachable("Invalid size for machine code value!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000247 case 8:
Torok Edwinc23197a2009-07-14 16:55:14 +0000248 llvm_unreachable("Unsupported alignment size!");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000249 case 1: OS << (IsPow2 ? ".p2align" : ".balign"); break;
250 case 2: OS << (IsPow2 ? ".p2alignw" : ".balignw"); break;
251 case 4: OS << (IsPow2 ? ".p2alignl" : ".balignl"); break;
Daniel Dunbar84a29262009-06-24 19:25:34 +0000252 }
253
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000254 OS << ' ' << (IsPow2 ? Pow2 : ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000255
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000256 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000257 if (MaxBytesToEmit)
258 OS << ", " << MaxBytesToEmit;
259 OS << '\n';
260}
261
262void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
263 unsigned char Value) {
264 // FIXME: Verify that Offset is associated with the current section.
265 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
266}
267
Daniel Dunbarabde2982009-07-01 06:35:03 +0000268static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
269 if (Op.isReg())
270 return OS << "reg:" << Op.getReg();
271 if (Op.isImm())
272 return OS << "imm:" << Op.getImm();
273 if (Op.isMBBLabel())
274 return OS << "mbblabel:("
275 << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
276 assert(Op.isMCValue() && "Invalid operand!");
277 return OS << "val:" << Op.getMCValue();
278}
279
Daniel Dunbara11af532009-06-24 01:03:06 +0000280void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000281 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbarabde2982009-07-01 06:35:03 +0000282 // FIXME: Implement proper printing.
283 OS << "MCInst("
284 << "opcode=" << Inst.getOpcode() << ", "
285 << "operands=[";
286 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
287 if (i)
288 OS << ", ";
289 OS << Inst.getOperand(i);
290 }
291 OS << "])\n";
Daniel Dunbara11af532009-06-24 01:03:06 +0000292}
293
294void MCAsmStreamer::Finish() {
295 OS.flush();
296}
297
298MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
299 return new MCAsmStreamer(Context, OS);
300}