blob: 172ae9ec958b8a687ab37977c3553054bd020774 [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
41 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
42 bool MakeAbsolute = false);
43
44 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
45
Chris Lattner4e4db7a2009-07-07 20:30:46 +000046 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Chris Lattner1fc3d752009-07-09 17:25:12 +000047 unsigned Pow2Alignment, bool IsLocal);
Chris Lattner4e4db7a2009-07-07 20:30:46 +000048
Chris Lattner9be3fee2009-07-10 22:20:30 +000049 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL,
50 unsigned Size = 0, unsigned Pow2Alignment = 0);
51
Daniel Dunbara11af532009-06-24 01:03:06 +000052 virtual void EmitBytes(const char *Data, unsigned Length);
53
54 virtual void EmitValue(const MCValue &Value, unsigned Size);
55
Daniel Dunbar84a29262009-06-24 19:25:34 +000056 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
57 unsigned ValueSize = 1,
58 unsigned MaxBytesToEmit = 0);
59
60 virtual void EmitValueToOffset(const MCValue &Offset,
61 unsigned char Value = 0);
62
Daniel Dunbara11af532009-06-24 01:03:06 +000063 virtual void EmitInstruction(const MCInst &Inst);
64
65 virtual void Finish();
66
67 /// @}
68 };
69
70}
71
72/// Allow printing values directly to a raw_ostream.
Daniel Dunbar304f6a42009-06-25 21:03:18 +000073static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
Daniel Dunbara11af532009-06-24 01:03:06 +000074 if (Value.getSymA()) {
75 os << Value.getSymA()->getName();
76 if (Value.getSymB())
77 os << " - " << Value.getSymB()->getName();
Daniel Dunbar7908a6a2009-06-29 19:51:00 +000078 if (Value.getConstant())
79 os << " + " << Value.getConstant();
Daniel Dunbara11af532009-06-24 01:03:06 +000080 } else {
81 assert(!Value.getSymB() && "Invalid machine code value!");
Daniel Dunbar7908a6a2009-06-29 19:51:00 +000082 os << Value.getConstant();
Daniel Dunbara11af532009-06-24 01:03:06 +000083 }
84
85 return os;
86}
87
Daniel Dunbar304f6a42009-06-25 21:03:18 +000088static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
89 assert(Bytes && "Invalid size!");
90 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
91}
92
93static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
94 return MCValue::get(Value.getSymA(), Value.getSymB(),
Daniel Dunbar7908a6a2009-06-29 19:51:00 +000095 truncateToSize(Value.getConstant(), Bytes));
Daniel Dunbar304f6a42009-06-25 21:03:18 +000096}
97
Daniel Dunbara11af532009-06-24 01:03:06 +000098void MCAsmStreamer::SwitchSection(MCSection *Section) {
99 if (Section != CurSection) {
100 CurSection = Section;
101
102 // FIXME: Really we would like the segment, flags, etc. to be separate
103 // values instead of embedded in the name. Not all assemblers understand all
104 // this stuff though.
105 OS << ".section " << Section->getName() << "\n";
106 }
107}
108
109void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000110 assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
111 assert(CurSection && "Cannot emit before setting section!");
112 assert(!getContext().GetSymbolValue(Symbol) &&
113 "Cannot emit symbol which was directly assigned to!");
114
Daniel Dunbara11af532009-06-24 01:03:06 +0000115 OS << Symbol->getName() << ":\n";
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000116 Symbol->setSection(CurSection);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000117 Symbol->setExternal(false);
Daniel Dunbara11af532009-06-24 01:03:06 +0000118}
119
120void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
121 bool MakeAbsolute) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000122 assert(!Symbol->getSection() && "Cannot assign to a label!");
123
Daniel Dunbara11af532009-06-24 01:03:06 +0000124 if (MakeAbsolute) {
125 OS << ".set " << Symbol->getName() << ", " << Value << '\n';
126 } else {
127 OS << Symbol->getName() << " = " << Value << '\n';
128 }
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000129
130 getContext().SetSymbolValue(Symbol, Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000131}
132
133void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
134 SymbolAttr Attribute) {
135 switch (Attribute) {
136 case Global: OS << ".globl"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000137 case Hidden: OS << ".hidden"; break;
138 case IndirectSymbol: OS << ".indirect_symbol"; break;
139 case Internal: OS << ".internal"; break;
140 case LazyReference: OS << ".lazy_reference"; break;
141 case NoDeadStrip: OS << ".no_dead_strip"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000142 case PrivateExtern: OS << ".private_extern"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000143 case Protected: OS << ".protected"; break;
144 case Reference: OS << ".reference"; break;
145 case Weak: OS << ".weak"; break;
146 case WeakDefinition: OS << ".weak_definition"; break;
147 case WeakReference: OS << ".weak_reference"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000148 }
149
150 OS << ' ' << Symbol->getName() << '\n';
151}
152
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000153void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Chris Lattner1fc3d752009-07-09 17:25:12 +0000154 unsigned Pow2Alignment, bool IsLocal) {
155 if (IsLocal)
156 OS << ".lcomm";
157 else
158 OS << ".comm";
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000159 OS << ' ' << Symbol->getName() << ',' << Size;
160 if (Pow2Alignment != 0)
161 OS << ',' << Pow2Alignment;
162 OS << '\n';
163}
164
Chris Lattner9be3fee2009-07-10 22:20:30 +0000165void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
166 unsigned Size, unsigned Pow2Alignment) {
167 // Note: a .zerofill directive does not switch sections
168 // FIXME: Really we would like the segment and section names as well as the
169 // section type to be separate values instead of embedded in the name. Not
170 // all assemblers understand all this stuff though.
171 OS << ".zerofill " << Section->getName();
172 if (Symbol != NULL) {
173 OS << ',' << Symbol->getName() << ',' << Size;
174 if (Pow2Alignment != 0)
175 OS << ',' << Pow2Alignment;
176 }
177 OS << '\n';
178}
179
Daniel Dunbara11af532009-06-24 01:03:06 +0000180void MCAsmStreamer::EmitBytes(const char *Data, unsigned Length) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000181 assert(CurSection && "Cannot emit contents before setting section!");
182 for (unsigned i = 0; i != Length; ++i)
Daniel Dunbara11af532009-06-24 01:03:06 +0000183 OS << ".byte " << (unsigned) Data[i] << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000184}
185
186void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000187 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000188 // Need target hooks to know how to print this.
189 switch (Size) {
190 default:
Torok Edwinc25e7582009-07-11 20:10:48 +0000191 LLVM_UNREACHABLE("Invalid size for machine code value!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000192 case 1: OS << ".byte"; break;
Daniel Dunbarf5e75a12009-06-24 16:05:35 +0000193 case 2: OS << ".short"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000194 case 4: OS << ".long"; break;
195 case 8: OS << ".quad"; break;
196 }
197
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000198 OS << ' ' << truncateToSize(Value, Size) << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000199}
200
Daniel Dunbar84a29262009-06-24 19:25:34 +0000201void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
202 unsigned ValueSize,
203 unsigned MaxBytesToEmit) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000204 // Some assemblers don't support .balign, so we always emit as .p2align if
205 // this is a power of two. Otherwise we assume the client knows the target
206 // supports .balign and use that.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000207 unsigned Pow2 = Log2_32(ByteAlignment);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000208 bool IsPow2 = (1U << Pow2) == ByteAlignment;
Daniel Dunbar84a29262009-06-24 19:25:34 +0000209
210 switch (ValueSize) {
211 default:
Torok Edwinc25e7582009-07-11 20:10:48 +0000212 LLVM_UNREACHABLE("Invalid size for machine code value!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000213 case 8:
Torok Edwinc25e7582009-07-11 20:10:48 +0000214 LLVM_UNREACHABLE("Unsupported alignment size!");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000215 case 1: OS << (IsPow2 ? ".p2align" : ".balign"); break;
216 case 2: OS << (IsPow2 ? ".p2alignw" : ".balignw"); break;
217 case 4: OS << (IsPow2 ? ".p2alignl" : ".balignl"); break;
Daniel Dunbar84a29262009-06-24 19:25:34 +0000218 }
219
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000220 OS << ' ' << (IsPow2 ? Pow2 : ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000221
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000222 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000223 if (MaxBytesToEmit)
224 OS << ", " << MaxBytesToEmit;
225 OS << '\n';
226}
227
228void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
229 unsigned char Value) {
230 // FIXME: Verify that Offset is associated with the current section.
231 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
232}
233
Daniel Dunbarabde2982009-07-01 06:35:03 +0000234static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
235 if (Op.isReg())
236 return OS << "reg:" << Op.getReg();
237 if (Op.isImm())
238 return OS << "imm:" << Op.getImm();
239 if (Op.isMBBLabel())
240 return OS << "mbblabel:("
241 << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
242 assert(Op.isMCValue() && "Invalid operand!");
243 return OS << "val:" << Op.getMCValue();
244}
245
Daniel Dunbara11af532009-06-24 01:03:06 +0000246void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000247 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbarabde2982009-07-01 06:35:03 +0000248 // FIXME: Implement proper printing.
249 OS << "MCInst("
250 << "opcode=" << Inst.getOpcode() << ", "
251 << "operands=[";
252 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
253 if (i)
254 OS << ", ";
255 OS << Inst.getOperand(i);
256 }
257 OS << "])\n";
Daniel Dunbara11af532009-06-24 01:03:06 +0000258}
259
260void MCAsmStreamer::Finish() {
261 OS.flush();
262}
263
264MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
265 return new MCAsmStreamer(Context, OS);
266}