blob: f4aeaf39f338bebfaf4f586b8e781f067f6297f0 [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,
45 unsigned Pow2Alignment);
46
Daniel Dunbara11af532009-06-24 01:03:06 +000047 virtual void EmitBytes(const char *Data, unsigned Length);
48
49 virtual void EmitValue(const MCValue &Value, unsigned Size);
50
Daniel Dunbar84a29262009-06-24 19:25:34 +000051 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
52 unsigned ValueSize = 1,
53 unsigned MaxBytesToEmit = 0);
54
55 virtual void EmitValueToOffset(const MCValue &Offset,
56 unsigned char Value = 0);
57
Daniel Dunbara11af532009-06-24 01:03:06 +000058 virtual void EmitInstruction(const MCInst &Inst);
59
60 virtual void Finish();
61
62 /// @}
63 };
64
65}
66
67/// Allow printing values directly to a raw_ostream.
Daniel Dunbar304f6a42009-06-25 21:03:18 +000068static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
Daniel Dunbara11af532009-06-24 01:03:06 +000069 if (Value.getSymA()) {
70 os << Value.getSymA()->getName();
71 if (Value.getSymB())
72 os << " - " << Value.getSymB()->getName();
Daniel Dunbar7908a6a2009-06-29 19:51:00 +000073 if (Value.getConstant())
74 os << " + " << Value.getConstant();
Daniel Dunbara11af532009-06-24 01:03:06 +000075 } else {
76 assert(!Value.getSymB() && "Invalid machine code value!");
Daniel Dunbar7908a6a2009-06-29 19:51:00 +000077 os << Value.getConstant();
Daniel Dunbara11af532009-06-24 01:03:06 +000078 }
79
80 return os;
81}
82
Daniel Dunbar304f6a42009-06-25 21:03:18 +000083static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
84 assert(Bytes && "Invalid size!");
85 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
86}
87
88static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
89 return MCValue::get(Value.getSymA(), Value.getSymB(),
Daniel Dunbar7908a6a2009-06-29 19:51:00 +000090 truncateToSize(Value.getConstant(), Bytes));
Daniel Dunbar304f6a42009-06-25 21:03:18 +000091}
92
Daniel Dunbara11af532009-06-24 01:03:06 +000093void MCAsmStreamer::SwitchSection(MCSection *Section) {
94 if (Section != CurSection) {
95 CurSection = Section;
96
97 // FIXME: Really we would like the segment, flags, etc. to be separate
98 // values instead of embedded in the name. Not all assemblers understand all
99 // this stuff though.
100 OS << ".section " << Section->getName() << "\n";
101 }
102}
103
104void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000105 assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
106 assert(CurSection && "Cannot emit before setting section!");
107 assert(!getContext().GetSymbolValue(Symbol) &&
108 "Cannot emit symbol which was directly assigned to!");
109
Daniel Dunbara11af532009-06-24 01:03:06 +0000110 OS << Symbol->getName() << ":\n";
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000111 Symbol->setSection(CurSection);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000112 Symbol->setExternal(false);
Daniel Dunbara11af532009-06-24 01:03:06 +0000113}
114
115void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
116 bool MakeAbsolute) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000117 assert(!Symbol->getSection() && "Cannot assign to a label!");
118
Daniel Dunbara11af532009-06-24 01:03:06 +0000119 if (MakeAbsolute) {
120 OS << ".set " << Symbol->getName() << ", " << Value << '\n';
121 } else {
122 OS << Symbol->getName() << " = " << Value << '\n';
123 }
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000124
125 getContext().SetSymbolValue(Symbol, Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000126}
127
128void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
129 SymbolAttr Attribute) {
130 switch (Attribute) {
131 case Global: OS << ".globl"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000132 case Hidden: OS << ".hidden"; break;
133 case IndirectSymbol: OS << ".indirect_symbol"; break;
134 case Internal: OS << ".internal"; break;
135 case LazyReference: OS << ".lazy_reference"; break;
136 case NoDeadStrip: OS << ".no_dead_strip"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000137 case PrivateExtern: OS << ".private_extern"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000138 case Protected: OS << ".protected"; break;
139 case Reference: OS << ".reference"; break;
140 case Weak: OS << ".weak"; break;
141 case WeakDefinition: OS << ".weak_definition"; break;
142 case WeakReference: OS << ".weak_reference"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000143 }
144
145 OS << ' ' << Symbol->getName() << '\n';
146}
147
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000148void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
149 unsigned Pow2Alignment) {
150 OS << ".comm";
151 OS << ' ' << Symbol->getName() << ',' << Size;
152 if (Pow2Alignment != 0)
153 OS << ',' << Pow2Alignment;
154 OS << '\n';
155}
156
Daniel Dunbara11af532009-06-24 01:03:06 +0000157void MCAsmStreamer::EmitBytes(const char *Data, unsigned Length) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000158 assert(CurSection && "Cannot emit contents before setting section!");
159 for (unsigned i = 0; i != Length; ++i)
Daniel Dunbara11af532009-06-24 01:03:06 +0000160 OS << ".byte " << (unsigned) Data[i] << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000161}
162
163void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000164 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000165 // Need target hooks to know how to print this.
166 switch (Size) {
167 default:
168 assert(0 && "Invalid size for machine code value!");
169 case 1: OS << ".byte"; break;
Daniel Dunbarf5e75a12009-06-24 16:05:35 +0000170 case 2: OS << ".short"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000171 case 4: OS << ".long"; break;
172 case 8: OS << ".quad"; break;
173 }
174
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000175 OS << ' ' << truncateToSize(Value, Size) << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000176}
177
Daniel Dunbar84a29262009-06-24 19:25:34 +0000178void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
179 unsigned ValueSize,
180 unsigned MaxBytesToEmit) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000181 // Some assemblers don't support .balign, so we always emit as .p2align if
182 // this is a power of two. Otherwise we assume the client knows the target
183 // supports .balign and use that.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000184 unsigned Pow2 = Log2_32(ByteAlignment);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000185 bool IsPow2 = (1U << Pow2) == ByteAlignment;
Daniel Dunbar84a29262009-06-24 19:25:34 +0000186
187 switch (ValueSize) {
188 default:
189 assert(0 && "Invalid size for machine code value!");
190 case 8:
191 assert(0 && "Unsupported alignment size!");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000192 case 1: OS << (IsPow2 ? ".p2align" : ".balign"); break;
193 case 2: OS << (IsPow2 ? ".p2alignw" : ".balignw"); break;
194 case 4: OS << (IsPow2 ? ".p2alignl" : ".balignl"); break;
Daniel Dunbar84a29262009-06-24 19:25:34 +0000195 }
196
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000197 OS << ' ' << (IsPow2 ? Pow2 : ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000198
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000199 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000200 if (MaxBytesToEmit)
201 OS << ", " << MaxBytesToEmit;
202 OS << '\n';
203}
204
205void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
206 unsigned char Value) {
207 // FIXME: Verify that Offset is associated with the current section.
208 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
209}
210
Daniel Dunbarabde2982009-07-01 06:35:03 +0000211static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
212 if (Op.isReg())
213 return OS << "reg:" << Op.getReg();
214 if (Op.isImm())
215 return OS << "imm:" << Op.getImm();
216 if (Op.isMBBLabel())
217 return OS << "mbblabel:("
218 << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
219 assert(Op.isMCValue() && "Invalid operand!");
220 return OS << "val:" << Op.getMCValue();
221}
222
Daniel Dunbara11af532009-06-24 01:03:06 +0000223void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000224 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbarabde2982009-07-01 06:35:03 +0000225 // FIXME: Implement proper printing.
226 OS << "MCInst("
227 << "opcode=" << Inst.getOpcode() << ", "
228 << "operands=[";
229 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
230 if (i)
231 OS << ", ";
232 OS << Inst.getOperand(i);
233 }
234 OS << "])\n";
Daniel Dunbara11af532009-06-24 01:03:06 +0000235}
236
237void MCAsmStreamer::Finish() {
238 OS.flush();
239}
240
241MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
242 return new MCAsmStreamer(Context, OS);
243}