blob: c79625517d4b4c9cf0affd95041256d502c6eccf [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
Chris Lattner4e4db7a2009-07-07 20:30:46 +000048 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Chris Lattner1fc3d752009-07-09 17:25:12 +000049 unsigned Pow2Alignment, bool IsLocal);
Chris Lattner4e4db7a2009-07-07 20:30:46 +000050
Chris Lattner9be3fee2009-07-10 22:20:30 +000051 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL,
52 unsigned Size = 0, unsigned Pow2Alignment = 0);
53
Kevin Enderby5f1f0b82009-07-13 23:15:14 +000054 virtual void AbortAssembly(const char *AbortReason = NULL);
55
Daniel Dunbara11af532009-06-24 01:03:06 +000056 virtual void EmitBytes(const char *Data, unsigned Length);
57
58 virtual void EmitValue(const MCValue &Value, unsigned Size);
59
Daniel Dunbar84a29262009-06-24 19:25:34 +000060 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
61 unsigned ValueSize = 1,
62 unsigned MaxBytesToEmit = 0);
63
64 virtual void EmitValueToOffset(const MCValue &Offset,
65 unsigned char Value = 0);
66
Daniel Dunbara11af532009-06-24 01:03:06 +000067 virtual void EmitInstruction(const MCInst &Inst);
68
69 virtual void Finish();
70
71 /// @}
72 };
73
74}
75
76/// Allow printing values directly to a raw_ostream.
Daniel Dunbar304f6a42009-06-25 21:03:18 +000077static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
Daniel Dunbara11af532009-06-24 01:03:06 +000078 if (Value.getSymA()) {
79 os << Value.getSymA()->getName();
80 if (Value.getSymB())
81 os << " - " << Value.getSymB()->getName();
Daniel Dunbar7908a6a2009-06-29 19:51:00 +000082 if (Value.getConstant())
83 os << " + " << Value.getConstant();
Daniel Dunbara11af532009-06-24 01:03:06 +000084 } else {
85 assert(!Value.getSymB() && "Invalid machine code value!");
Daniel Dunbar7908a6a2009-06-29 19:51:00 +000086 os << Value.getConstant();
Daniel Dunbara11af532009-06-24 01:03:06 +000087 }
88
89 return os;
90}
91
Daniel Dunbar304f6a42009-06-25 21:03:18 +000092static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
93 assert(Bytes && "Invalid size!");
94 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
95}
96
97static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
98 return MCValue::get(Value.getSymA(), Value.getSymB(),
Daniel Dunbar7908a6a2009-06-29 19:51:00 +000099 truncateToSize(Value.getConstant(), Bytes));
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000100}
101
Daniel Dunbara11af532009-06-24 01:03:06 +0000102void MCAsmStreamer::SwitchSection(MCSection *Section) {
103 if (Section != CurSection) {
104 CurSection = Section;
105
106 // FIXME: Really we would like the segment, flags, etc. to be separate
107 // values instead of embedded in the name. Not all assemblers understand all
108 // this stuff though.
109 OS << ".section " << Section->getName() << "\n";
110 }
111}
112
113void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000114 assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
115 assert(CurSection && "Cannot emit before setting section!");
116 assert(!getContext().GetSymbolValue(Symbol) &&
117 "Cannot emit symbol which was directly assigned to!");
118
Daniel Dunbara11af532009-06-24 01:03:06 +0000119 OS << Symbol->getName() << ":\n";
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000120 Symbol->setSection(CurSection);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000121 Symbol->setExternal(false);
Daniel Dunbara11af532009-06-24 01:03:06 +0000122}
123
Kevin Enderbya5c78322009-07-13 21:03:15 +0000124void MCAsmStreamer::SubsectionsViaSymbols(void) {
125 OS << ".subsections_via_symbols\n";
126}
127
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000128void MCAsmStreamer::AbortAssembly(const char *AbortReason) {
129 OS << ".abort";
130 if (AbortReason != NULL)
131 OS << ' ' << AbortReason;
132 OS << '\n';
133
134}
135
Daniel Dunbara11af532009-06-24 01:03:06 +0000136void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
137 bool MakeAbsolute) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000138 assert(!Symbol->getSection() && "Cannot assign to a label!");
139
Daniel Dunbara11af532009-06-24 01:03:06 +0000140 if (MakeAbsolute) {
141 OS << ".set " << Symbol->getName() << ", " << Value << '\n';
142 } else {
143 OS << Symbol->getName() << " = " << Value << '\n';
144 }
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000145
146 getContext().SetSymbolValue(Symbol, Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000147}
148
149void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
150 SymbolAttr Attribute) {
151 switch (Attribute) {
152 case Global: OS << ".globl"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000153 case Hidden: OS << ".hidden"; break;
154 case IndirectSymbol: OS << ".indirect_symbol"; break;
155 case Internal: OS << ".internal"; break;
156 case LazyReference: OS << ".lazy_reference"; break;
157 case NoDeadStrip: OS << ".no_dead_strip"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000158 case PrivateExtern: OS << ".private_extern"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000159 case Protected: OS << ".protected"; break;
160 case Reference: OS << ".reference"; break;
161 case Weak: OS << ".weak"; break;
162 case WeakDefinition: OS << ".weak_definition"; break;
163 case WeakReference: OS << ".weak_reference"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000164 }
165
166 OS << ' ' << Symbol->getName() << '\n';
167}
168
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000169void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Chris Lattner1fc3d752009-07-09 17:25:12 +0000170 unsigned Pow2Alignment, bool IsLocal) {
171 if (IsLocal)
172 OS << ".lcomm";
173 else
174 OS << ".comm";
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000175 OS << ' ' << Symbol->getName() << ',' << Size;
176 if (Pow2Alignment != 0)
177 OS << ',' << Pow2Alignment;
178 OS << '\n';
179}
180
Chris Lattner9be3fee2009-07-10 22:20:30 +0000181void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
182 unsigned Size, unsigned Pow2Alignment) {
183 // Note: a .zerofill directive does not switch sections
184 // FIXME: Really we would like the segment and section names as well as the
185 // section type to be separate values instead of embedded in the name. Not
186 // all assemblers understand all this stuff though.
187 OS << ".zerofill " << Section->getName();
188 if (Symbol != NULL) {
189 OS << ',' << Symbol->getName() << ',' << Size;
190 if (Pow2Alignment != 0)
191 OS << ',' << Pow2Alignment;
192 }
193 OS << '\n';
194}
195
Daniel Dunbara11af532009-06-24 01:03:06 +0000196void MCAsmStreamer::EmitBytes(const char *Data, unsigned Length) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000197 assert(CurSection && "Cannot emit contents before setting section!");
198 for (unsigned i = 0; i != Length; ++i)
Daniel Dunbara11af532009-06-24 01:03:06 +0000199 OS << ".byte " << (unsigned) Data[i] << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000200}
201
202void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000203 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000204 // Need target hooks to know how to print this.
205 switch (Size) {
206 default:
Torok Edwinc25e7582009-07-11 20:10:48 +0000207 LLVM_UNREACHABLE("Invalid size for machine code value!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000208 case 1: OS << ".byte"; break;
Daniel Dunbarf5e75a12009-06-24 16:05:35 +0000209 case 2: OS << ".short"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000210 case 4: OS << ".long"; break;
211 case 8: OS << ".quad"; break;
212 }
213
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000214 OS << ' ' << truncateToSize(Value, Size) << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000215}
216
Daniel Dunbar84a29262009-06-24 19:25:34 +0000217void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
218 unsigned ValueSize,
219 unsigned MaxBytesToEmit) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000220 // Some assemblers don't support .balign, so we always emit as .p2align if
221 // this is a power of two. Otherwise we assume the client knows the target
222 // supports .balign and use that.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000223 unsigned Pow2 = Log2_32(ByteAlignment);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000224 bool IsPow2 = (1U << Pow2) == ByteAlignment;
Daniel Dunbar84a29262009-06-24 19:25:34 +0000225
226 switch (ValueSize) {
227 default:
Torok Edwinc25e7582009-07-11 20:10:48 +0000228 LLVM_UNREACHABLE("Invalid size for machine code value!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000229 case 8:
Torok Edwinc25e7582009-07-11 20:10:48 +0000230 LLVM_UNREACHABLE("Unsupported alignment size!");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000231 case 1: OS << (IsPow2 ? ".p2align" : ".balign"); break;
232 case 2: OS << (IsPow2 ? ".p2alignw" : ".balignw"); break;
233 case 4: OS << (IsPow2 ? ".p2alignl" : ".balignl"); break;
Daniel Dunbar84a29262009-06-24 19:25:34 +0000234 }
235
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000236 OS << ' ' << (IsPow2 ? Pow2 : ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000237
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000238 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000239 if (MaxBytesToEmit)
240 OS << ", " << MaxBytesToEmit;
241 OS << '\n';
242}
243
244void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
245 unsigned char Value) {
246 // FIXME: Verify that Offset is associated with the current section.
247 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
248}
249
Daniel Dunbarabde2982009-07-01 06:35:03 +0000250static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
251 if (Op.isReg())
252 return OS << "reg:" << Op.getReg();
253 if (Op.isImm())
254 return OS << "imm:" << Op.getImm();
255 if (Op.isMBBLabel())
256 return OS << "mbblabel:("
257 << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
258 assert(Op.isMCValue() && "Invalid operand!");
259 return OS << "val:" << Op.getMCValue();
260}
261
Daniel Dunbara11af532009-06-24 01:03:06 +0000262void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000263 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbarabde2982009-07-01 06:35:03 +0000264 // FIXME: Implement proper printing.
265 OS << "MCInst("
266 << "opcode=" << Inst.getOpcode() << ", "
267 << "operands=[";
268 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
269 if (i)
270 OS << ", ";
271 OS << Inst.getOperand(i);
272 }
273 OS << "])\n";
Daniel Dunbara11af532009-06-24 01:03:06 +0000274}
275
276void MCAsmStreamer::Finish() {
277 OS.flush();
278}
279
280MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
281 return new MCAsmStreamer(Context, OS);
282}