blob: b77e6efac39a4ab6240379ed79cfd43a74bc3d85 [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 Enderbyf96db462009-07-16 17:56:39 +000041 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
Kevin Enderbya5c78322009-07-13 21:03:15 +000042
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
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +000060 virtual void EmitBytes(const StringRef &Data);
Daniel Dunbara11af532009-06-24 01:03:06 +000061
62 virtual void EmitValue(const MCValue &Value, unsigned Size);
63
Daniel Dunbar84a29262009-06-24 19:25:34 +000064 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
65 unsigned ValueSize = 1,
66 unsigned MaxBytesToEmit = 0);
67
68 virtual void EmitValueToOffset(const MCValue &Offset,
69 unsigned char Value = 0);
70
Daniel Dunbara11af532009-06-24 01:03:06 +000071 virtual void EmitInstruction(const MCInst &Inst);
72
73 virtual void Finish();
74
75 /// @}
76 };
77
78}
79
80/// Allow printing values directly to a raw_ostream.
Daniel Dunbar304f6a42009-06-25 21:03:18 +000081static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
Daniel Dunbara11af532009-06-24 01:03:06 +000082 if (Value.getSymA()) {
83 os << Value.getSymA()->getName();
84 if (Value.getSymB())
85 os << " - " << Value.getSymB()->getName();
Daniel Dunbar7908a6a2009-06-29 19:51:00 +000086 if (Value.getConstant())
87 os << " + " << Value.getConstant();
Daniel Dunbara11af532009-06-24 01:03:06 +000088 } else {
89 assert(!Value.getSymB() && "Invalid machine code value!");
Daniel Dunbar7908a6a2009-06-29 19:51:00 +000090 os << Value.getConstant();
Daniel Dunbara11af532009-06-24 01:03:06 +000091 }
92
93 return os;
94}
95
Daniel Dunbar304f6a42009-06-25 21:03:18 +000096static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
97 assert(Bytes && "Invalid size!");
98 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
99}
100
101static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
102 return MCValue::get(Value.getSymA(), Value.getSymB(),
Daniel Dunbar7908a6a2009-06-29 19:51:00 +0000103 truncateToSize(Value.getConstant(), Bytes));
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000104}
105
Daniel Dunbara11af532009-06-24 01:03:06 +0000106void MCAsmStreamer::SwitchSection(MCSection *Section) {
107 if (Section != CurSection) {
108 CurSection = Section;
109
110 // FIXME: Really we would like the segment, flags, etc. to be separate
111 // values instead of embedded in the name. Not all assemblers understand all
112 // this stuff though.
113 OS << ".section " << Section->getName() << "\n";
114 }
115}
116
117void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000118 assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
119 assert(CurSection && "Cannot emit before setting section!");
120 assert(!getContext().GetSymbolValue(Symbol) &&
121 "Cannot emit symbol which was directly assigned to!");
122
Daniel Dunbara11af532009-06-24 01:03:06 +0000123 OS << Symbol->getName() << ":\n";
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000124 Symbol->setSection(CurSection);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000125 Symbol->setExternal(false);
Daniel Dunbara11af532009-06-24 01:03:06 +0000126}
127
Kevin Enderbyf96db462009-07-16 17:56:39 +0000128void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
129 switch (Flag) {
130 case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
131 }
132 OS << '\n';
Kevin Enderbya5c78322009-07-13 21:03:15 +0000133}
134
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000135void MCAsmStreamer::AbortAssembly(const char *AbortReason) {
136 OS << ".abort";
137 if (AbortReason != NULL)
138 OS << ' ' << AbortReason;
139 OS << '\n';
140
141}
142
Daniel Dunbara11af532009-06-24 01:03:06 +0000143void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
144 bool MakeAbsolute) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000145 assert(!Symbol->getSection() && "Cannot assign to a label!");
146
Daniel Dunbara11af532009-06-24 01:03:06 +0000147 if (MakeAbsolute) {
148 OS << ".set " << Symbol->getName() << ", " << Value << '\n';
149 } else {
150 OS << Symbol->getName() << " = " << Value << '\n';
151 }
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000152
153 getContext().SetSymbolValue(Symbol, Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000154}
155
156void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
157 SymbolAttr Attribute) {
158 switch (Attribute) {
159 case Global: OS << ".globl"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000160 case Hidden: OS << ".hidden"; break;
161 case IndirectSymbol: OS << ".indirect_symbol"; break;
162 case Internal: OS << ".internal"; break;
163 case LazyReference: OS << ".lazy_reference"; break;
164 case NoDeadStrip: OS << ".no_dead_strip"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000165 case PrivateExtern: OS << ".private_extern"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000166 case Protected: OS << ".protected"; break;
167 case Reference: OS << ".reference"; break;
168 case Weak: OS << ".weak"; break;
169 case WeakDefinition: OS << ".weak_definition"; break;
170 case WeakReference: OS << ".weak_reference"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000171 }
172
173 OS << ' ' << Symbol->getName() << '\n';
174}
175
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000176void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
177 OS << ".desc" << ' ' << Symbol->getName() << ',' << DescValue << '\n';
178}
179
Kevin Enderby71148242009-07-14 21:35:03 +0000180void MCAsmStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
181 OS << ".lsym" << ' ' << Symbol->getName() << ',' << Value << '\n';
182}
183
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000184void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Chris Lattner1fc3d752009-07-09 17:25:12 +0000185 unsigned Pow2Alignment, bool IsLocal) {
186 if (IsLocal)
187 OS << ".lcomm";
188 else
189 OS << ".comm";
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000190 OS << ' ' << Symbol->getName() << ',' << Size;
191 if (Pow2Alignment != 0)
192 OS << ',' << Pow2Alignment;
193 OS << '\n';
194}
195
Chris Lattner9be3fee2009-07-10 22:20:30 +0000196void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
197 unsigned Size, unsigned Pow2Alignment) {
198 // Note: a .zerofill directive does not switch sections
199 // FIXME: Really we would like the segment and section names as well as the
200 // section type to be separate values instead of embedded in the name. Not
201 // all assemblers understand all this stuff though.
202 OS << ".zerofill " << Section->getName();
203 if (Symbol != NULL) {
204 OS << ',' << Symbol->getName() << ',' << Size;
205 if (Pow2Alignment != 0)
206 OS << ',' << Pow2Alignment;
207 }
208 OS << '\n';
209}
210
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000211void MCAsmStreamer::EmitBytes(const StringRef &Data) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000212 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000213 for (unsigned i = 0, e = Data.size(); i != e; ++i)
Daniel Dunbara11af532009-06-24 01:03:06 +0000214 OS << ".byte " << (unsigned) Data[i] << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000215}
216
217void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000218 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000219 // Need target hooks to know how to print this.
220 switch (Size) {
221 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000222 llvm_unreachable("Invalid size for machine code value!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000223 case 1: OS << ".byte"; break;
Daniel Dunbarf5e75a12009-06-24 16:05:35 +0000224 case 2: OS << ".short"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000225 case 4: OS << ".long"; break;
226 case 8: OS << ".quad"; break;
227 }
228
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000229 OS << ' ' << truncateToSize(Value, Size) << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000230}
231
Daniel Dunbar84a29262009-06-24 19:25:34 +0000232void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
233 unsigned ValueSize,
234 unsigned MaxBytesToEmit) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000235 // Some assemblers don't support .balign, so we always emit as .p2align if
236 // this is a power of two. Otherwise we assume the client knows the target
237 // supports .balign and use that.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000238 unsigned Pow2 = Log2_32(ByteAlignment);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000239 bool IsPow2 = (1U << Pow2) == ByteAlignment;
Daniel Dunbar84a29262009-06-24 19:25:34 +0000240
241 switch (ValueSize) {
242 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000243 llvm_unreachable("Invalid size for machine code value!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000244 case 8:
Torok Edwinc23197a2009-07-14 16:55:14 +0000245 llvm_unreachable("Unsupported alignment size!");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000246 case 1: OS << (IsPow2 ? ".p2align" : ".balign"); break;
247 case 2: OS << (IsPow2 ? ".p2alignw" : ".balignw"); break;
248 case 4: OS << (IsPow2 ? ".p2alignl" : ".balignl"); break;
Daniel Dunbar84a29262009-06-24 19:25:34 +0000249 }
250
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000251 OS << ' ' << (IsPow2 ? Pow2 : ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000252
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000253 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000254 if (MaxBytesToEmit)
255 OS << ", " << MaxBytesToEmit;
256 OS << '\n';
257}
258
259void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
260 unsigned char Value) {
261 // FIXME: Verify that Offset is associated with the current section.
262 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
263}
264
Daniel Dunbarabde2982009-07-01 06:35:03 +0000265static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
266 if (Op.isReg())
267 return OS << "reg:" << Op.getReg();
268 if (Op.isImm())
269 return OS << "imm:" << Op.getImm();
270 if (Op.isMBBLabel())
271 return OS << "mbblabel:("
272 << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
273 assert(Op.isMCValue() && "Invalid operand!");
274 return OS << "val:" << Op.getMCValue();
275}
276
Daniel Dunbara11af532009-06-24 01:03:06 +0000277void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000278 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbarabde2982009-07-01 06:35:03 +0000279 // FIXME: Implement proper printing.
280 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
295MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
296 return new MCAsmStreamer(Context, OS);
297}