blob: c0a334adc9e527fe6791bb2a803ba7411db95950 [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
Daniel Dunbara11af532009-06-24 01:03:06 +000054 virtual void EmitBytes(const char *Data, unsigned Length);
55
56 virtual void EmitValue(const MCValue &Value, unsigned Size);
57
Daniel Dunbar84a29262009-06-24 19:25:34 +000058 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
59 unsigned ValueSize = 1,
60 unsigned MaxBytesToEmit = 0);
61
62 virtual void EmitValueToOffset(const MCValue &Offset,
63 unsigned char Value = 0);
64
Daniel Dunbara11af532009-06-24 01:03:06 +000065 virtual void EmitInstruction(const MCInst &Inst);
66
67 virtual void Finish();
68
69 /// @}
70 };
71
72}
73
74/// Allow printing values directly to a raw_ostream.
Daniel Dunbar304f6a42009-06-25 21:03:18 +000075static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
Daniel Dunbara11af532009-06-24 01:03:06 +000076 if (Value.getSymA()) {
77 os << Value.getSymA()->getName();
78 if (Value.getSymB())
79 os << " - " << Value.getSymB()->getName();
Daniel Dunbar7908a6a2009-06-29 19:51:00 +000080 if (Value.getConstant())
81 os << " + " << Value.getConstant();
Daniel Dunbara11af532009-06-24 01:03:06 +000082 } else {
83 assert(!Value.getSymB() && "Invalid machine code value!");
Daniel Dunbar7908a6a2009-06-29 19:51:00 +000084 os << Value.getConstant();
Daniel Dunbara11af532009-06-24 01:03:06 +000085 }
86
87 return os;
88}
89
Daniel Dunbar304f6a42009-06-25 21:03:18 +000090static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
91 assert(Bytes && "Invalid size!");
92 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
93}
94
95static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
96 return MCValue::get(Value.getSymA(), Value.getSymB(),
Daniel Dunbar7908a6a2009-06-29 19:51:00 +000097 truncateToSize(Value.getConstant(), Bytes));
Daniel Dunbar304f6a42009-06-25 21:03:18 +000098}
99
Daniel Dunbara11af532009-06-24 01:03:06 +0000100void MCAsmStreamer::SwitchSection(MCSection *Section) {
101 if (Section != CurSection) {
102 CurSection = Section;
103
104 // FIXME: Really we would like the segment, flags, etc. to be separate
105 // values instead of embedded in the name. Not all assemblers understand all
106 // this stuff though.
107 OS << ".section " << Section->getName() << "\n";
108 }
109}
110
111void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000112 assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
113 assert(CurSection && "Cannot emit before setting section!");
114 assert(!getContext().GetSymbolValue(Symbol) &&
115 "Cannot emit symbol which was directly assigned to!");
116
Daniel Dunbara11af532009-06-24 01:03:06 +0000117 OS << Symbol->getName() << ":\n";
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000118 Symbol->setSection(CurSection);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000119 Symbol->setExternal(false);
Daniel Dunbara11af532009-06-24 01:03:06 +0000120}
121
Kevin Enderbya5c78322009-07-13 21:03:15 +0000122void MCAsmStreamer::SubsectionsViaSymbols(void) {
123 OS << ".subsections_via_symbols\n";
124}
125
Daniel Dunbara11af532009-06-24 01:03:06 +0000126void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
127 bool MakeAbsolute) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000128 assert(!Symbol->getSection() && "Cannot assign to a label!");
129
Daniel Dunbara11af532009-06-24 01:03:06 +0000130 if (MakeAbsolute) {
131 OS << ".set " << Symbol->getName() << ", " << Value << '\n';
132 } else {
133 OS << Symbol->getName() << " = " << Value << '\n';
134 }
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000135
136 getContext().SetSymbolValue(Symbol, Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000137}
138
139void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
140 SymbolAttr Attribute) {
141 switch (Attribute) {
142 case Global: OS << ".globl"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000143 case Hidden: OS << ".hidden"; break;
144 case IndirectSymbol: OS << ".indirect_symbol"; break;
145 case Internal: OS << ".internal"; break;
146 case LazyReference: OS << ".lazy_reference"; break;
147 case NoDeadStrip: OS << ".no_dead_strip"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000148 case PrivateExtern: OS << ".private_extern"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000149 case Protected: OS << ".protected"; break;
150 case Reference: OS << ".reference"; break;
151 case Weak: OS << ".weak"; break;
152 case WeakDefinition: OS << ".weak_definition"; break;
153 case WeakReference: OS << ".weak_reference"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000154 }
155
156 OS << ' ' << Symbol->getName() << '\n';
157}
158
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000159void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Chris Lattner1fc3d752009-07-09 17:25:12 +0000160 unsigned Pow2Alignment, bool IsLocal) {
161 if (IsLocal)
162 OS << ".lcomm";
163 else
164 OS << ".comm";
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000165 OS << ' ' << Symbol->getName() << ',' << Size;
166 if (Pow2Alignment != 0)
167 OS << ',' << Pow2Alignment;
168 OS << '\n';
169}
170
Chris Lattner9be3fee2009-07-10 22:20:30 +0000171void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
172 unsigned Size, unsigned Pow2Alignment) {
173 // Note: a .zerofill directive does not switch sections
174 // FIXME: Really we would like the segment and section names as well as the
175 // section type to be separate values instead of embedded in the name. Not
176 // all assemblers understand all this stuff though.
177 OS << ".zerofill " << Section->getName();
178 if (Symbol != NULL) {
179 OS << ',' << Symbol->getName() << ',' << Size;
180 if (Pow2Alignment != 0)
181 OS << ',' << Pow2Alignment;
182 }
183 OS << '\n';
184}
185
Daniel Dunbara11af532009-06-24 01:03:06 +0000186void MCAsmStreamer::EmitBytes(const char *Data, unsigned Length) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000187 assert(CurSection && "Cannot emit contents before setting section!");
188 for (unsigned i = 0; i != Length; ++i)
Daniel Dunbara11af532009-06-24 01:03:06 +0000189 OS << ".byte " << (unsigned) Data[i] << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000190}
191
192void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000193 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000194 // Need target hooks to know how to print this.
195 switch (Size) {
196 default:
Torok Edwinc25e7582009-07-11 20:10:48 +0000197 LLVM_UNREACHABLE("Invalid size for machine code value!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000198 case 1: OS << ".byte"; break;
Daniel Dunbarf5e75a12009-06-24 16:05:35 +0000199 case 2: OS << ".short"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000200 case 4: OS << ".long"; break;
201 case 8: OS << ".quad"; break;
202 }
203
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000204 OS << ' ' << truncateToSize(Value, Size) << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000205}
206
Daniel Dunbar84a29262009-06-24 19:25:34 +0000207void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
208 unsigned ValueSize,
209 unsigned MaxBytesToEmit) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000210 // Some assemblers don't support .balign, so we always emit as .p2align if
211 // this is a power of two. Otherwise we assume the client knows the target
212 // supports .balign and use that.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000213 unsigned Pow2 = Log2_32(ByteAlignment);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000214 bool IsPow2 = (1U << Pow2) == ByteAlignment;
Daniel Dunbar84a29262009-06-24 19:25:34 +0000215
216 switch (ValueSize) {
217 default:
Torok Edwinc25e7582009-07-11 20:10:48 +0000218 LLVM_UNREACHABLE("Invalid size for machine code value!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000219 case 8:
Torok Edwinc25e7582009-07-11 20:10:48 +0000220 LLVM_UNREACHABLE("Unsupported alignment size!");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000221 case 1: OS << (IsPow2 ? ".p2align" : ".balign"); break;
222 case 2: OS << (IsPow2 ? ".p2alignw" : ".balignw"); break;
223 case 4: OS << (IsPow2 ? ".p2alignl" : ".balignl"); break;
Daniel Dunbar84a29262009-06-24 19:25:34 +0000224 }
225
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000226 OS << ' ' << (IsPow2 ? Pow2 : ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000227
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000228 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000229 if (MaxBytesToEmit)
230 OS << ", " << MaxBytesToEmit;
231 OS << '\n';
232}
233
234void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
235 unsigned char Value) {
236 // FIXME: Verify that Offset is associated with the current section.
237 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
238}
239
Daniel Dunbarabde2982009-07-01 06:35:03 +0000240static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
241 if (Op.isReg())
242 return OS << "reg:" << Op.getReg();
243 if (Op.isImm())
244 return OS << "imm:" << Op.getImm();
245 if (Op.isMBBLabel())
246 return OS << "mbblabel:("
247 << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
248 assert(Op.isMCValue() && "Invalid operand!");
249 return OS << "val:" << Op.getMCValue();
250}
251
Daniel Dunbara11af532009-06-24 01:03:06 +0000252void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000253 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbarabde2982009-07-01 06:35:03 +0000254 // FIXME: Implement proper printing.
255 OS << "MCInst("
256 << "opcode=" << Inst.getOpcode() << ", "
257 << "operands=[";
258 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
259 if (i)
260 OS << ", ";
261 OS << Inst.getOperand(i);
262 }
263 OS << "])\n";
Daniel Dunbara11af532009-06-24 01:03:06 +0000264}
265
266void MCAsmStreamer::Finish() {
267 OS.flush();
268}
269
270MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
271 return new MCAsmStreamer(Context, OS);
272}