blob: b3460080d73313d1556a81fdd98a8a005f51260b [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
Chris Lattner4e4db7a2009-07-07 20:30:46 +000050 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Chris Lattner1fc3d752009-07-09 17:25:12 +000051 unsigned Pow2Alignment, bool IsLocal);
Chris Lattner4e4db7a2009-07-07 20:30:46 +000052
Chris Lattner9be3fee2009-07-10 22:20:30 +000053 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL,
54 unsigned Size = 0, unsigned Pow2Alignment = 0);
55
Kevin Enderby5f1f0b82009-07-13 23:15:14 +000056 virtual void AbortAssembly(const char *AbortReason = NULL);
57
Daniel Dunbara11af532009-06-24 01:03:06 +000058 virtual void EmitBytes(const char *Data, unsigned Length);
59
60 virtual void EmitValue(const MCValue &Value, unsigned Size);
61
Daniel Dunbar84a29262009-06-24 19:25:34 +000062 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
63 unsigned ValueSize = 1,
64 unsigned MaxBytesToEmit = 0);
65
66 virtual void EmitValueToOffset(const MCValue &Offset,
67 unsigned char Value = 0);
68
Daniel Dunbara11af532009-06-24 01:03:06 +000069 virtual void EmitInstruction(const MCInst &Inst);
70
71 virtual void Finish();
72
73 /// @}
74 };
75
76}
77
78/// Allow printing values directly to a raw_ostream.
Daniel Dunbar304f6a42009-06-25 21:03:18 +000079static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
Daniel Dunbara11af532009-06-24 01:03:06 +000080 if (Value.getSymA()) {
81 os << Value.getSymA()->getName();
82 if (Value.getSymB())
83 os << " - " << Value.getSymB()->getName();
Daniel Dunbar7908a6a2009-06-29 19:51:00 +000084 if (Value.getConstant())
85 os << " + " << Value.getConstant();
Daniel Dunbara11af532009-06-24 01:03:06 +000086 } else {
87 assert(!Value.getSymB() && "Invalid machine code value!");
Daniel Dunbar7908a6a2009-06-29 19:51:00 +000088 os << Value.getConstant();
Daniel Dunbara11af532009-06-24 01:03:06 +000089 }
90
91 return os;
92}
93
Daniel Dunbar304f6a42009-06-25 21:03:18 +000094static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
95 assert(Bytes && "Invalid size!");
96 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
97}
98
99static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
100 return MCValue::get(Value.getSymA(), Value.getSymB(),
Daniel Dunbar7908a6a2009-06-29 19:51:00 +0000101 truncateToSize(Value.getConstant(), Bytes));
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000102}
103
Daniel Dunbara11af532009-06-24 01:03:06 +0000104void MCAsmStreamer::SwitchSection(MCSection *Section) {
105 if (Section != CurSection) {
106 CurSection = Section;
107
108 // FIXME: Really we would like the segment, flags, etc. to be separate
109 // values instead of embedded in the name. Not all assemblers understand all
110 // this stuff though.
111 OS << ".section " << Section->getName() << "\n";
112 }
113}
114
115void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000116 assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
117 assert(CurSection && "Cannot emit before setting section!");
118 assert(!getContext().GetSymbolValue(Symbol) &&
119 "Cannot emit symbol which was directly assigned to!");
120
Daniel Dunbara11af532009-06-24 01:03:06 +0000121 OS << Symbol->getName() << ":\n";
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000122 Symbol->setSection(CurSection);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000123 Symbol->setExternal(false);
Daniel Dunbara11af532009-06-24 01:03:06 +0000124}
125
Kevin Enderbya5c78322009-07-13 21:03:15 +0000126void MCAsmStreamer::SubsectionsViaSymbols(void) {
127 OS << ".subsections_via_symbols\n";
128}
129
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000130void MCAsmStreamer::AbortAssembly(const char *AbortReason) {
131 OS << ".abort";
132 if (AbortReason != NULL)
133 OS << ' ' << AbortReason;
134 OS << '\n';
135
136}
137
Daniel Dunbara11af532009-06-24 01:03:06 +0000138void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
139 bool MakeAbsolute) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000140 assert(!Symbol->getSection() && "Cannot assign to a label!");
141
Daniel Dunbara11af532009-06-24 01:03:06 +0000142 if (MakeAbsolute) {
143 OS << ".set " << Symbol->getName() << ", " << Value << '\n';
144 } else {
145 OS << Symbol->getName() << " = " << Value << '\n';
146 }
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000147
148 getContext().SetSymbolValue(Symbol, Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000149}
150
151void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
152 SymbolAttr Attribute) {
153 switch (Attribute) {
154 case Global: OS << ".globl"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000155 case Hidden: OS << ".hidden"; break;
156 case IndirectSymbol: OS << ".indirect_symbol"; break;
157 case Internal: OS << ".internal"; break;
158 case LazyReference: OS << ".lazy_reference"; break;
159 case NoDeadStrip: OS << ".no_dead_strip"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000160 case PrivateExtern: OS << ".private_extern"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000161 case Protected: OS << ".protected"; break;
162 case Reference: OS << ".reference"; break;
163 case Weak: OS << ".weak"; break;
164 case WeakDefinition: OS << ".weak_definition"; break;
165 case WeakReference: OS << ".weak_reference"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000166 }
167
168 OS << ' ' << Symbol->getName() << '\n';
169}
170
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000171void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
172 OS << ".desc" << ' ' << Symbol->getName() << ',' << DescValue << '\n';
173}
174
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000175void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Chris Lattner1fc3d752009-07-09 17:25:12 +0000176 unsigned Pow2Alignment, bool IsLocal) {
177 if (IsLocal)
178 OS << ".lcomm";
179 else
180 OS << ".comm";
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000181 OS << ' ' << Symbol->getName() << ',' << Size;
182 if (Pow2Alignment != 0)
183 OS << ',' << Pow2Alignment;
184 OS << '\n';
185}
186
Chris Lattner9be3fee2009-07-10 22:20:30 +0000187void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
188 unsigned Size, unsigned Pow2Alignment) {
189 // Note: a .zerofill directive does not switch sections
190 // FIXME: Really we would like the segment and section names as well as the
191 // section type to be separate values instead of embedded in the name. Not
192 // all assemblers understand all this stuff though.
193 OS << ".zerofill " << Section->getName();
194 if (Symbol != NULL) {
195 OS << ',' << Symbol->getName() << ',' << Size;
196 if (Pow2Alignment != 0)
197 OS << ',' << Pow2Alignment;
198 }
199 OS << '\n';
200}
201
Daniel Dunbara11af532009-06-24 01:03:06 +0000202void MCAsmStreamer::EmitBytes(const char *Data, unsigned Length) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000203 assert(CurSection && "Cannot emit contents before setting section!");
204 for (unsigned i = 0; i != Length; ++i)
Daniel Dunbara11af532009-06-24 01:03:06 +0000205 OS << ".byte " << (unsigned) Data[i] << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000206}
207
208void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000209 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000210 // Need target hooks to know how to print this.
211 switch (Size) {
212 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000213 llvm_unreachable("Invalid size for machine code value!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000214 case 1: OS << ".byte"; break;
Daniel Dunbarf5e75a12009-06-24 16:05:35 +0000215 case 2: OS << ".short"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000216 case 4: OS << ".long"; break;
217 case 8: OS << ".quad"; break;
218 }
219
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000220 OS << ' ' << truncateToSize(Value, Size) << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000221}
222
Daniel Dunbar84a29262009-06-24 19:25:34 +0000223void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
224 unsigned ValueSize,
225 unsigned MaxBytesToEmit) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000226 // Some assemblers don't support .balign, so we always emit as .p2align if
227 // this is a power of two. Otherwise we assume the client knows the target
228 // supports .balign and use that.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000229 unsigned Pow2 = Log2_32(ByteAlignment);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000230 bool IsPow2 = (1U << Pow2) == ByteAlignment;
Daniel Dunbar84a29262009-06-24 19:25:34 +0000231
232 switch (ValueSize) {
233 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000234 llvm_unreachable("Invalid size for machine code value!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000235 case 8:
Torok Edwinc23197a2009-07-14 16:55:14 +0000236 llvm_unreachable("Unsupported alignment size!");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000237 case 1: OS << (IsPow2 ? ".p2align" : ".balign"); break;
238 case 2: OS << (IsPow2 ? ".p2alignw" : ".balignw"); break;
239 case 4: OS << (IsPow2 ? ".p2alignl" : ".balignl"); break;
Daniel Dunbar84a29262009-06-24 19:25:34 +0000240 }
241
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000242 OS << ' ' << (IsPow2 ? Pow2 : ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000243
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000244 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000245 if (MaxBytesToEmit)
246 OS << ", " << MaxBytesToEmit;
247 OS << '\n';
248}
249
250void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
251 unsigned char Value) {
252 // FIXME: Verify that Offset is associated with the current section.
253 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
254}
255
Daniel Dunbarabde2982009-07-01 06:35:03 +0000256static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
257 if (Op.isReg())
258 return OS << "reg:" << Op.getReg();
259 if (Op.isImm())
260 return OS << "imm:" << Op.getImm();
261 if (Op.isMBBLabel())
262 return OS << "mbblabel:("
263 << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
264 assert(Op.isMCValue() && "Invalid operand!");
265 return OS << "val:" << Op.getMCValue();
266}
267
Daniel Dunbara11af532009-06-24 01:03:06 +0000268void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000269 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbarabde2982009-07-01 06:35:03 +0000270 // FIXME: Implement proper printing.
271 OS << "MCInst("
272 << "opcode=" << Inst.getOpcode() << ", "
273 << "operands=[";
274 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
275 if (i)
276 OS << ", ";
277 OS << Inst.getOperand(i);
278 }
279 OS << "])\n";
Daniel Dunbara11af532009-06-24 01:03:06 +0000280}
281
282void MCAsmStreamer::Finish() {
283 OS.flush();
284}
285
286MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
287 return new MCAsmStreamer(Context, OS);
288}