blob: 9640fd9121cbbf2401d264d695667d1c22434a81 [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"
17#include "llvm/Support/raw_ostream.h"
18using namespace llvm;
19
20namespace {
21
22 class MCAsmStreamer : public MCStreamer {
23 raw_ostream &OS;
24
25 MCSection *CurSection;
26
27 public:
28 MCAsmStreamer(MCContext &Context, raw_ostream &_OS)
Torok Edwin48de5d82009-06-29 19:59:10 +000029 : MCStreamer(Context), OS(_OS), CurSection(0) {}
Daniel Dunbara11af532009-06-24 01:03:06 +000030 ~MCAsmStreamer() {}
31
32 /// @name MCStreamer Interface
33 /// @{
34
35 virtual void SwitchSection(MCSection *Section);
36
37 virtual void EmitLabel(MCSymbol *Symbol);
38
39 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
40 bool MakeAbsolute = false);
41
42 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
43
Chris Lattner4e4db7a2009-07-07 20:30:46 +000044 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Chris Lattner1fc3d752009-07-09 17:25:12 +000045 unsigned Pow2Alignment, bool IsLocal);
Chris Lattner4e4db7a2009-07-07 20:30:46 +000046
Chris Lattner9be3fee2009-07-10 22:20:30 +000047 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL,
48 unsigned Size = 0, unsigned Pow2Alignment = 0);
49
Daniel Dunbara11af532009-06-24 01:03:06 +000050 virtual void EmitBytes(const char *Data, unsigned Length);
51
52 virtual void EmitValue(const MCValue &Value, unsigned Size);
53
Daniel Dunbar84a29262009-06-24 19:25:34 +000054 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
55 unsigned ValueSize = 1,
56 unsigned MaxBytesToEmit = 0);
57
58 virtual void EmitValueToOffset(const MCValue &Offset,
59 unsigned char Value = 0);
60
Daniel Dunbara11af532009-06-24 01:03:06 +000061 virtual void EmitInstruction(const MCInst &Inst);
62
63 virtual void Finish();
64
65 /// @}
66 };
67
68}
69
70/// Allow printing values directly to a raw_ostream.
Daniel Dunbar304f6a42009-06-25 21:03:18 +000071static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
Daniel Dunbara11af532009-06-24 01:03:06 +000072 if (Value.getSymA()) {
73 os << Value.getSymA()->getName();
74 if (Value.getSymB())
75 os << " - " << Value.getSymB()->getName();
Daniel Dunbar7908a6a2009-06-29 19:51:00 +000076 if (Value.getConstant())
77 os << " + " << Value.getConstant();
Daniel Dunbara11af532009-06-24 01:03:06 +000078 } else {
79 assert(!Value.getSymB() && "Invalid machine code value!");
Daniel Dunbar7908a6a2009-06-29 19:51:00 +000080 os << Value.getConstant();
Daniel Dunbara11af532009-06-24 01:03:06 +000081 }
82
83 return os;
84}
85
Daniel Dunbar304f6a42009-06-25 21:03:18 +000086static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
87 assert(Bytes && "Invalid size!");
88 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
89}
90
91static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
92 return MCValue::get(Value.getSymA(), Value.getSymB(),
Daniel Dunbar7908a6a2009-06-29 19:51:00 +000093 truncateToSize(Value.getConstant(), Bytes));
Daniel Dunbar304f6a42009-06-25 21:03:18 +000094}
95
Daniel Dunbara11af532009-06-24 01:03:06 +000096void MCAsmStreamer::SwitchSection(MCSection *Section) {
97 if (Section != CurSection) {
98 CurSection = Section;
99
100 // FIXME: Really we would like the segment, flags, etc. to be separate
101 // values instead of embedded in the name. Not all assemblers understand all
102 // this stuff though.
103 OS << ".section " << Section->getName() << "\n";
104 }
105}
106
107void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000108 assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
109 assert(CurSection && "Cannot emit before setting section!");
110 assert(!getContext().GetSymbolValue(Symbol) &&
111 "Cannot emit symbol which was directly assigned to!");
112
Daniel Dunbara11af532009-06-24 01:03:06 +0000113 OS << Symbol->getName() << ":\n";
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000114 Symbol->setSection(CurSection);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000115 Symbol->setExternal(false);
Daniel Dunbara11af532009-06-24 01:03:06 +0000116}
117
118void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
119 bool MakeAbsolute) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000120 assert(!Symbol->getSection() && "Cannot assign to a label!");
121
Daniel Dunbara11af532009-06-24 01:03:06 +0000122 if (MakeAbsolute) {
123 OS << ".set " << Symbol->getName() << ", " << Value << '\n';
124 } else {
125 OS << Symbol->getName() << " = " << Value << '\n';
126 }
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000127
128 getContext().SetSymbolValue(Symbol, Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000129}
130
131void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
132 SymbolAttr Attribute) {
133 switch (Attribute) {
134 case Global: OS << ".globl"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000135 case Hidden: OS << ".hidden"; break;
136 case IndirectSymbol: OS << ".indirect_symbol"; break;
137 case Internal: OS << ".internal"; break;
138 case LazyReference: OS << ".lazy_reference"; break;
139 case NoDeadStrip: OS << ".no_dead_strip"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000140 case PrivateExtern: OS << ".private_extern"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000141 case Protected: OS << ".protected"; break;
142 case Reference: OS << ".reference"; break;
143 case Weak: OS << ".weak"; break;
144 case WeakDefinition: OS << ".weak_definition"; break;
145 case WeakReference: OS << ".weak_reference"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000146 }
147
148 OS << ' ' << Symbol->getName() << '\n';
149}
150
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000151void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Chris Lattner1fc3d752009-07-09 17:25:12 +0000152 unsigned Pow2Alignment, bool IsLocal) {
153 if (IsLocal)
154 OS << ".lcomm";
155 else
156 OS << ".comm";
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000157 OS << ' ' << Symbol->getName() << ',' << Size;
158 if (Pow2Alignment != 0)
159 OS << ',' << Pow2Alignment;
160 OS << '\n';
161}
162
Chris Lattner9be3fee2009-07-10 22:20:30 +0000163void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
164 unsigned Size, unsigned Pow2Alignment) {
165 // Note: a .zerofill directive does not switch sections
166 // FIXME: Really we would like the segment and section names as well as the
167 // section type to be separate values instead of embedded in the name. Not
168 // all assemblers understand all this stuff though.
169 OS << ".zerofill " << Section->getName();
170 if (Symbol != NULL) {
171 OS << ',' << Symbol->getName() << ',' << Size;
172 if (Pow2Alignment != 0)
173 OS << ',' << Pow2Alignment;
174 }
175 OS << '\n';
176}
177
Daniel Dunbara11af532009-06-24 01:03:06 +0000178void MCAsmStreamer::EmitBytes(const char *Data, unsigned Length) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000179 assert(CurSection && "Cannot emit contents before setting section!");
180 for (unsigned i = 0; i != Length; ++i)
Daniel Dunbara11af532009-06-24 01:03:06 +0000181 OS << ".byte " << (unsigned) Data[i] << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000182}
183
184void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000185 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000186 // Need target hooks to know how to print this.
187 switch (Size) {
188 default:
189 assert(0 && "Invalid size for machine code value!");
190 case 1: OS << ".byte"; break;
Daniel Dunbarf5e75a12009-06-24 16:05:35 +0000191 case 2: OS << ".short"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000192 case 4: OS << ".long"; break;
193 case 8: OS << ".quad"; break;
194 }
195
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000196 OS << ' ' << truncateToSize(Value, Size) << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000197}
198
Daniel Dunbar84a29262009-06-24 19:25:34 +0000199void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
200 unsigned ValueSize,
201 unsigned MaxBytesToEmit) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000202 // Some assemblers don't support .balign, so we always emit as .p2align if
203 // this is a power of two. Otherwise we assume the client knows the target
204 // supports .balign and use that.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000205 unsigned Pow2 = Log2_32(ByteAlignment);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000206 bool IsPow2 = (1U << Pow2) == ByteAlignment;
Daniel Dunbar84a29262009-06-24 19:25:34 +0000207
208 switch (ValueSize) {
209 default:
210 assert(0 && "Invalid size for machine code value!");
211 case 8:
212 assert(0 && "Unsupported alignment size!");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000213 case 1: OS << (IsPow2 ? ".p2align" : ".balign"); break;
214 case 2: OS << (IsPow2 ? ".p2alignw" : ".balignw"); break;
215 case 4: OS << (IsPow2 ? ".p2alignl" : ".balignl"); break;
Daniel Dunbar84a29262009-06-24 19:25:34 +0000216 }
217
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000218 OS << ' ' << (IsPow2 ? Pow2 : ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000219
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000220 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000221 if (MaxBytesToEmit)
222 OS << ", " << MaxBytesToEmit;
223 OS << '\n';
224}
225
226void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
227 unsigned char Value) {
228 // FIXME: Verify that Offset is associated with the current section.
229 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
230}
231
Daniel Dunbarabde2982009-07-01 06:35:03 +0000232static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
233 if (Op.isReg())
234 return OS << "reg:" << Op.getReg();
235 if (Op.isImm())
236 return OS << "imm:" << Op.getImm();
237 if (Op.isMBBLabel())
238 return OS << "mbblabel:("
239 << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
240 assert(Op.isMCValue() && "Invalid operand!");
241 return OS << "val:" << Op.getMCValue();
242}
243
Daniel Dunbara11af532009-06-24 01:03:06 +0000244void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000245 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbarabde2982009-07-01 06:35:03 +0000246 // FIXME: Implement proper printing.
247 OS << "MCInst("
248 << "opcode=" << Inst.getOpcode() << ", "
249 << "operands=[";
250 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
251 if (i)
252 OS << ", ";
253 OS << Inst.getOperand(i);
254 }
255 OS << "])\n";
Daniel Dunbara11af532009-06-24 01:03:06 +0000256}
257
258void MCAsmStreamer::Finish() {
259 OS.flush();
260}
261
262MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
263 return new MCAsmStreamer(Context, OS);
264}