blob: 314e525681423c92138eb61ba01a081b5f12527e [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"
13#include "llvm/MC/MCSection.h"
14#include "llvm/MC/MCSymbol.h"
15#include "llvm/MC/MCValue.h"
16#include "llvm/Support/raw_ostream.h"
17using namespace llvm;
18
19namespace {
20
21 class MCAsmStreamer : public MCStreamer {
22 raw_ostream &OS;
23
24 MCSection *CurSection;
25
26 public:
27 MCAsmStreamer(MCContext &Context, raw_ostream &_OS)
Torok Edwin48de5d82009-06-29 19:59:10 +000028 : MCStreamer(Context), OS(_OS), CurSection(0) {}
Daniel Dunbara11af532009-06-24 01:03:06 +000029 ~MCAsmStreamer() {}
30
31 /// @name MCStreamer Interface
32 /// @{
33
34 virtual void SwitchSection(MCSection *Section);
35
36 virtual void EmitLabel(MCSymbol *Symbol);
37
38 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
39 bool MakeAbsolute = false);
40
41 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
42
43 virtual void EmitBytes(const char *Data, unsigned Length);
44
45 virtual void EmitValue(const MCValue &Value, unsigned Size);
46
Daniel Dunbar84a29262009-06-24 19:25:34 +000047 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
48 unsigned ValueSize = 1,
49 unsigned MaxBytesToEmit = 0);
50
51 virtual void EmitValueToOffset(const MCValue &Offset,
52 unsigned char Value = 0);
53
Daniel Dunbara11af532009-06-24 01:03:06 +000054 virtual void EmitInstruction(const MCInst &Inst);
55
56 virtual void Finish();
57
58 /// @}
59 };
60
61}
62
63/// Allow printing values directly to a raw_ostream.
Daniel Dunbar304f6a42009-06-25 21:03:18 +000064static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
Daniel Dunbara11af532009-06-24 01:03:06 +000065 if (Value.getSymA()) {
66 os << Value.getSymA()->getName();
67 if (Value.getSymB())
68 os << " - " << Value.getSymB()->getName();
Daniel Dunbar7908a6a2009-06-29 19:51:00 +000069 if (Value.getConstant())
70 os << " + " << Value.getConstant();
Daniel Dunbara11af532009-06-24 01:03:06 +000071 } else {
72 assert(!Value.getSymB() && "Invalid machine code value!");
Daniel Dunbar7908a6a2009-06-29 19:51:00 +000073 os << Value.getConstant();
Daniel Dunbara11af532009-06-24 01:03:06 +000074 }
75
76 return os;
77}
78
Daniel Dunbar304f6a42009-06-25 21:03:18 +000079static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
80 assert(Bytes && "Invalid size!");
81 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
82}
83
84static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
85 return MCValue::get(Value.getSymA(), Value.getSymB(),
Daniel Dunbar7908a6a2009-06-29 19:51:00 +000086 truncateToSize(Value.getConstant(), Bytes));
Daniel Dunbar304f6a42009-06-25 21:03:18 +000087}
88
Daniel Dunbara11af532009-06-24 01:03:06 +000089void MCAsmStreamer::SwitchSection(MCSection *Section) {
90 if (Section != CurSection) {
91 CurSection = Section;
92
93 // FIXME: Really we would like the segment, flags, etc. to be separate
94 // values instead of embedded in the name. Not all assemblers understand all
95 // this stuff though.
96 OS << ".section " << Section->getName() << "\n";
97 }
98}
99
100void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000101 assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
102 assert(CurSection && "Cannot emit before setting section!");
103 assert(!getContext().GetSymbolValue(Symbol) &&
104 "Cannot emit symbol which was directly assigned to!");
105
Daniel Dunbara11af532009-06-24 01:03:06 +0000106 OS << Symbol->getName() << ":\n";
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000107 Symbol->setSection(CurSection);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000108 Symbol->setExternal(false);
Daniel Dunbara11af532009-06-24 01:03:06 +0000109}
110
111void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
112 bool MakeAbsolute) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000113 assert(!Symbol->getSection() && "Cannot assign to a label!");
114
Daniel Dunbara11af532009-06-24 01:03:06 +0000115 if (MakeAbsolute) {
116 OS << ".set " << Symbol->getName() << ", " << Value << '\n';
117 } else {
118 OS << Symbol->getName() << " = " << Value << '\n';
119 }
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000120
121 getContext().SetSymbolValue(Symbol, Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000122}
123
124void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
125 SymbolAttr Attribute) {
126 switch (Attribute) {
127 case Global: OS << ".globl"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000128 case Hidden: OS << ".hidden"; break;
129 case IndirectSymbol: OS << ".indirect_symbol"; break;
130 case Internal: OS << ".internal"; break;
131 case LazyReference: OS << ".lazy_reference"; break;
132 case NoDeadStrip: OS << ".no_dead_strip"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000133 case PrivateExtern: OS << ".private_extern"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000134 case Protected: OS << ".protected"; break;
135 case Reference: OS << ".reference"; break;
136 case Weak: OS << ".weak"; break;
137 case WeakDefinition: OS << ".weak_definition"; break;
138 case WeakReference: OS << ".weak_reference"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000139 }
140
141 OS << ' ' << Symbol->getName() << '\n';
142}
143
144void MCAsmStreamer::EmitBytes(const char *Data, unsigned Length) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000145 assert(CurSection && "Cannot emit contents before setting section!");
146 for (unsigned i = 0; i != Length; ++i)
Daniel Dunbara11af532009-06-24 01:03:06 +0000147 OS << ".byte " << (unsigned) Data[i] << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000148}
149
150void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000151 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000152 // Need target hooks to know how to print this.
153 switch (Size) {
154 default:
155 assert(0 && "Invalid size for machine code value!");
156 case 1: OS << ".byte"; break;
Daniel Dunbarf5e75a12009-06-24 16:05:35 +0000157 case 2: OS << ".short"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000158 case 4: OS << ".long"; break;
159 case 8: OS << ".quad"; break;
160 }
161
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000162 OS << ' ' << truncateToSize(Value, Size) << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000163}
164
Daniel Dunbar84a29262009-06-24 19:25:34 +0000165void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
166 unsigned ValueSize,
167 unsigned MaxBytesToEmit) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000168 // Some assemblers don't support .balign, so we always emit as .p2align if
169 // this is a power of two. Otherwise we assume the client knows the target
170 // supports .balign and use that.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000171 unsigned Pow2 = Log2_32(ByteAlignment);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000172 bool IsPow2 = (1U << Pow2) == ByteAlignment;
Daniel Dunbar84a29262009-06-24 19:25:34 +0000173
174 switch (ValueSize) {
175 default:
176 assert(0 && "Invalid size for machine code value!");
177 case 8:
178 assert(0 && "Unsupported alignment size!");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000179 case 1: OS << (IsPow2 ? ".p2align" : ".balign"); break;
180 case 2: OS << (IsPow2 ? ".p2alignw" : ".balignw"); break;
181 case 4: OS << (IsPow2 ? ".p2alignl" : ".balignl"); break;
Daniel Dunbar84a29262009-06-24 19:25:34 +0000182 }
183
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000184 OS << ' ' << (IsPow2 ? Pow2 : ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000185
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000186 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000187 if (MaxBytesToEmit)
188 OS << ", " << MaxBytesToEmit;
189 OS << '\n';
190}
191
192void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
193 unsigned char Value) {
194 // FIXME: Verify that Offset is associated with the current section.
195 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
196}
197
Daniel Dunbara11af532009-06-24 01:03:06 +0000198void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000199 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000200 // FIXME: Implement.
201 OS << "# FIXME: Implement instruction printing!\n";
202}
203
204void MCAsmStreamer::Finish() {
205 OS.flush();
206}
207
208MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
209 return new MCAsmStreamer(Context, OS);
210}