blob: 7d944644488957e95e6148eabfc6e3237218ca16 [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
44 virtual void EmitBytes(const char *Data, unsigned Length);
45
46 virtual void EmitValue(const MCValue &Value, unsigned Size);
47
Daniel Dunbar84a29262009-06-24 19:25:34 +000048 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
49 unsigned ValueSize = 1,
50 unsigned MaxBytesToEmit = 0);
51
52 virtual void EmitValueToOffset(const MCValue &Offset,
53 unsigned char Value = 0);
54
Daniel Dunbara11af532009-06-24 01:03:06 +000055 virtual void EmitInstruction(const MCInst &Inst);
56
57 virtual void Finish();
58
59 /// @}
60 };
61
62}
63
64/// Allow printing values directly to a raw_ostream.
Daniel Dunbar304f6a42009-06-25 21:03:18 +000065static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
Daniel Dunbara11af532009-06-24 01:03:06 +000066 if (Value.getSymA()) {
67 os << Value.getSymA()->getName();
68 if (Value.getSymB())
69 os << " - " << Value.getSymB()->getName();
Daniel Dunbar7908a6a2009-06-29 19:51:00 +000070 if (Value.getConstant())
71 os << " + " << Value.getConstant();
Daniel Dunbara11af532009-06-24 01:03:06 +000072 } else {
73 assert(!Value.getSymB() && "Invalid machine code value!");
Daniel Dunbar7908a6a2009-06-29 19:51:00 +000074 os << Value.getConstant();
Daniel Dunbara11af532009-06-24 01:03:06 +000075 }
76
77 return os;
78}
79
Daniel Dunbar304f6a42009-06-25 21:03:18 +000080static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
81 assert(Bytes && "Invalid size!");
82 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
83}
84
85static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
86 return MCValue::get(Value.getSymA(), Value.getSymB(),
Daniel Dunbar7908a6a2009-06-29 19:51:00 +000087 truncateToSize(Value.getConstant(), Bytes));
Daniel Dunbar304f6a42009-06-25 21:03:18 +000088}
89
Daniel Dunbara11af532009-06-24 01:03:06 +000090void MCAsmStreamer::SwitchSection(MCSection *Section) {
91 if (Section != CurSection) {
92 CurSection = Section;
93
94 // FIXME: Really we would like the segment, flags, etc. to be separate
95 // values instead of embedded in the name. Not all assemblers understand all
96 // this stuff though.
97 OS << ".section " << Section->getName() << "\n";
98 }
99}
100
101void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000102 assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
103 assert(CurSection && "Cannot emit before setting section!");
104 assert(!getContext().GetSymbolValue(Symbol) &&
105 "Cannot emit symbol which was directly assigned to!");
106
Daniel Dunbara11af532009-06-24 01:03:06 +0000107 OS << Symbol->getName() << ":\n";
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000108 Symbol->setSection(CurSection);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000109 Symbol->setExternal(false);
Daniel Dunbara11af532009-06-24 01:03:06 +0000110}
111
112void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
113 bool MakeAbsolute) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000114 assert(!Symbol->getSection() && "Cannot assign to a label!");
115
Daniel Dunbara11af532009-06-24 01:03:06 +0000116 if (MakeAbsolute) {
117 OS << ".set " << Symbol->getName() << ", " << Value << '\n';
118 } else {
119 OS << Symbol->getName() << " = " << Value << '\n';
120 }
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000121
122 getContext().SetSymbolValue(Symbol, Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000123}
124
125void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
126 SymbolAttr Attribute) {
127 switch (Attribute) {
128 case Global: OS << ".globl"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000129 case Hidden: OS << ".hidden"; break;
130 case IndirectSymbol: OS << ".indirect_symbol"; break;
131 case Internal: OS << ".internal"; break;
132 case LazyReference: OS << ".lazy_reference"; break;
133 case NoDeadStrip: OS << ".no_dead_strip"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000134 case PrivateExtern: OS << ".private_extern"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000135 case Protected: OS << ".protected"; break;
136 case Reference: OS << ".reference"; break;
137 case Weak: OS << ".weak"; break;
138 case WeakDefinition: OS << ".weak_definition"; break;
139 case WeakReference: OS << ".weak_reference"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000140 }
141
142 OS << ' ' << Symbol->getName() << '\n';
143}
144
145void MCAsmStreamer::EmitBytes(const char *Data, unsigned Length) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000146 assert(CurSection && "Cannot emit contents before setting section!");
147 for (unsigned i = 0; i != Length; ++i)
Daniel Dunbara11af532009-06-24 01:03:06 +0000148 OS << ".byte " << (unsigned) Data[i] << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000149}
150
151void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000152 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000153 // Need target hooks to know how to print this.
154 switch (Size) {
155 default:
156 assert(0 && "Invalid size for machine code value!");
157 case 1: OS << ".byte"; break;
Daniel Dunbarf5e75a12009-06-24 16:05:35 +0000158 case 2: OS << ".short"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000159 case 4: OS << ".long"; break;
160 case 8: OS << ".quad"; break;
161 }
162
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000163 OS << ' ' << truncateToSize(Value, Size) << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000164}
165
Daniel Dunbar84a29262009-06-24 19:25:34 +0000166void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
167 unsigned ValueSize,
168 unsigned MaxBytesToEmit) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000169 // Some assemblers don't support .balign, so we always emit as .p2align if
170 // this is a power of two. Otherwise we assume the client knows the target
171 // supports .balign and use that.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000172 unsigned Pow2 = Log2_32(ByteAlignment);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000173 bool IsPow2 = (1U << Pow2) == ByteAlignment;
Daniel Dunbar84a29262009-06-24 19:25:34 +0000174
175 switch (ValueSize) {
176 default:
177 assert(0 && "Invalid size for machine code value!");
178 case 8:
179 assert(0 && "Unsupported alignment size!");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000180 case 1: OS << (IsPow2 ? ".p2align" : ".balign"); break;
181 case 2: OS << (IsPow2 ? ".p2alignw" : ".balignw"); break;
182 case 4: OS << (IsPow2 ? ".p2alignl" : ".balignl"); break;
Daniel Dunbar84a29262009-06-24 19:25:34 +0000183 }
184
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000185 OS << ' ' << (IsPow2 ? Pow2 : ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000186
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000187 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000188 if (MaxBytesToEmit)
189 OS << ", " << MaxBytesToEmit;
190 OS << '\n';
191}
192
193void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
194 unsigned char Value) {
195 // FIXME: Verify that Offset is associated with the current section.
196 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
197}
198
Daniel Dunbarabde2982009-07-01 06:35:03 +0000199static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
200 if (Op.isReg())
201 return OS << "reg:" << Op.getReg();
202 if (Op.isImm())
203 return OS << "imm:" << Op.getImm();
204 if (Op.isMBBLabel())
205 return OS << "mbblabel:("
206 << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
207 assert(Op.isMCValue() && "Invalid operand!");
208 return OS << "val:" << Op.getMCValue();
209}
210
Daniel Dunbara11af532009-06-24 01:03:06 +0000211void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000212 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbarabde2982009-07-01 06:35:03 +0000213 // FIXME: Implement proper printing.
214 OS << "MCInst("
215 << "opcode=" << Inst.getOpcode() << ", "
216 << "operands=[";
217 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
218 if (i)
219 OS << ", ";
220 OS << Inst.getOperand(i);
221 }
222 OS << "])\n";
Daniel Dunbara11af532009-06-24 01:03:06 +0000223}
224
225void MCAsmStreamer::Finish() {
226 OS.flush();
227}
228
229MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
230 return new MCAsmStreamer(Context, OS);
231}