llvm-mc: Implement .abort fully in the front end
[oota-llvm.git] / lib / MC / MCAsmStreamer.cpp
1 //===- 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
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCInst.h"
14 #include "llvm/MC/MCSection.h"
15 #include "llvm/MC/MCSymbol.h"
16 #include "llvm/MC/MCValue.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/raw_ostream.h"
19
20 using namespace llvm;
21
22 namespace {
23
24   class MCAsmStreamer : public MCStreamer {
25     raw_ostream &OS;
26
27     MCSection *CurSection;
28
29   public:
30     MCAsmStreamer(MCContext &Context, raw_ostream &_OS)
31       : MCStreamer(Context), OS(_OS), CurSection(0) {}
32     ~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 EmitAssemblerFlag(AssemblerFlag Flag);
42
43     virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
44                                 bool MakeAbsolute = false);
45
46     virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
47
48     virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
49
50     virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value);
51
52     virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
53                                   unsigned Pow2Alignment, bool IsLocal);
54
55     virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL,
56                               unsigned Size = 0, unsigned Pow2Alignment = 0);
57
58     virtual void EmitBytes(const StringRef &Data);
59
60     virtual void EmitValue(const MCValue &Value, unsigned Size);
61
62     virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
63                                       unsigned ValueSize = 1,
64                                       unsigned MaxBytesToEmit = 0);
65
66     virtual void EmitValueToOffset(const MCValue &Offset, 
67                                    unsigned char Value = 0);
68     
69     virtual void EmitInstruction(const MCInst &Inst);
70
71     virtual void Finish();
72     
73     /// @}
74   };
75
76 }
77
78 /// Allow printing values directly to a raw_ostream.
79 static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
80   if (Value.getSymA()) {
81     os << Value.getSymA()->getName();
82     if (Value.getSymB())
83       os << " - " << Value.getSymB()->getName();
84     if (Value.getConstant())
85       os << " + " << Value.getConstant();
86   } else {
87     assert(!Value.getSymB() && "Invalid machine code value!");
88     os << Value.getConstant();
89   }
90
91   return os;
92 }
93
94 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
95   assert(Bytes && "Invalid size!");
96   return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
97 }
98
99 static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
100   return MCValue::get(Value.getSymA(), Value.getSymB(), 
101                       truncateToSize(Value.getConstant(), Bytes));
102 }
103
104 void MCAsmStreamer::SwitchSection(MCSection *Section) {
105   if (Section != CurSection) {
106     CurSection = Section;
107
108     // FIXME: Really we would like the segment, flags, etc. to be separate
109     // values instead of embedded in the name. Not all assemblers understand all
110     // this stuff though.
111     OS << ".section " << Section->getName() << "\n";
112   }
113 }
114
115 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
116   assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
117   assert(CurSection && "Cannot emit before setting section!");
118   assert(!getContext().GetSymbolValue(Symbol) && 
119          "Cannot emit symbol which was directly assigned to!");
120
121   OS << Symbol->getName() << ":\n";
122   Symbol->setSection(CurSection);
123   Symbol->setExternal(false);
124 }
125
126 void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
127   switch (Flag) {
128   case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
129   }
130   OS << '\n';
131 }
132
133 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
134                                    bool MakeAbsolute) {
135   assert(!Symbol->getSection() && "Cannot assign to a label!");
136
137   if (MakeAbsolute) {
138     OS << ".set " << Symbol->getName() << ", " << Value << '\n';
139   } else {
140     OS << Symbol->getName() << " = " << Value << '\n';
141   }
142
143   getContext().SetSymbolValue(Symbol, Value);
144 }
145
146 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol, 
147                                         SymbolAttr Attribute) {
148   switch (Attribute) {
149   case Global: OS << ".globl"; break;
150   case Hidden: OS << ".hidden"; break;
151   case IndirectSymbol: OS << ".indirect_symbol"; break;
152   case Internal: OS << ".internal"; break;
153   case LazyReference: OS << ".lazy_reference"; break;
154   case NoDeadStrip: OS << ".no_dead_strip"; break;
155   case PrivateExtern: OS << ".private_extern"; break;
156   case Protected: OS << ".protected"; break;
157   case Reference: OS << ".reference"; break;
158   case Weak: OS << ".weak"; break;
159   case WeakDefinition: OS << ".weak_definition"; break;
160   case WeakReference: OS << ".weak_reference"; break;
161   }
162
163   OS << ' ' << Symbol->getName() << '\n';
164 }
165
166 void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
167   OS << ".desc" << ' ' << Symbol->getName() << ',' << DescValue << '\n';
168 }
169
170 void MCAsmStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
171   OS << ".lsym" << ' ' << Symbol->getName() << ',' << Value << '\n';
172 }
173
174 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
175                                      unsigned Pow2Alignment, bool IsLocal) {
176   if (IsLocal)
177     OS << ".lcomm";
178   else
179     OS << ".comm";
180   OS << ' ' << Symbol->getName() << ',' << Size;
181   if (Pow2Alignment != 0)
182     OS << ',' << Pow2Alignment;
183   OS << '\n';
184 }
185
186 void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
187                                  unsigned Size, unsigned Pow2Alignment) {
188   // Note: a .zerofill directive does not switch sections
189   // FIXME: Really we would like the segment and section names as well as the
190   // section type to be separate values instead of embedded in the name. Not
191   // all assemblers understand all this stuff though.
192   OS << ".zerofill " << Section->getName();
193   if (Symbol != NULL) {
194     OS << ',' << Symbol->getName() << ',' << Size;
195     if (Pow2Alignment != 0)
196       OS << ',' << Pow2Alignment;
197   }
198   OS << '\n';
199 }
200
201 void MCAsmStreamer::EmitBytes(const StringRef &Data) {
202   assert(CurSection && "Cannot emit contents before setting section!");
203   for (unsigned i = 0, e = Data.size(); i != e; ++i)
204     OS << ".byte " << (unsigned) Data[i] << '\n';
205 }
206
207 void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
208   assert(CurSection && "Cannot emit contents before setting section!");
209   // Need target hooks to know how to print this.
210   switch (Size) {
211   default:
212     llvm_unreachable("Invalid size for machine code value!");
213   case 1: OS << ".byte"; break;
214   case 2: OS << ".short"; break;
215   case 4: OS << ".long"; break;
216   case 8: OS << ".quad"; break;
217   }
218
219   OS << ' ' << truncateToSize(Value, Size) << '\n';
220 }
221
222 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
223                                          unsigned ValueSize,
224                                          unsigned MaxBytesToEmit) {
225   // Some assemblers don't support .balign, so we always emit as .p2align if
226   // this is a power of two. Otherwise we assume the client knows the target
227   // supports .balign and use that.
228   unsigned Pow2 = Log2_32(ByteAlignment);
229   bool IsPow2 = (1U << Pow2) == ByteAlignment;
230
231   switch (ValueSize) {
232   default:
233     llvm_unreachable("Invalid size for machine code value!");
234   case 8:
235     llvm_unreachable("Unsupported alignment size!");
236   case 1: OS << (IsPow2 ? ".p2align" : ".balign"); break;
237   case 2: OS << (IsPow2 ? ".p2alignw" : ".balignw"); break;
238   case 4: OS << (IsPow2 ? ".p2alignl" : ".balignl"); break;
239   }
240
241   OS << ' ' << (IsPow2 ? Pow2 : ByteAlignment);
242
243   OS << ", " << truncateToSize(Value, ValueSize);
244   if (MaxBytesToEmit) 
245     OS << ", " << MaxBytesToEmit;
246   OS << '\n';
247 }
248
249 void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset, 
250                                       unsigned char Value) {
251   // FIXME: Verify that Offset is associated with the current section.
252   OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
253 }
254
255 static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
256   if (Op.isReg())
257     return OS << "reg:" << Op.getReg();
258   if (Op.isImm())
259     return OS << "imm:" << Op.getImm();
260   if (Op.isMBBLabel())
261     return OS << "mbblabel:(" 
262               << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
263   assert(Op.isMCValue() && "Invalid operand!");
264   return OS << "val:" << Op.getMCValue();
265 }
266
267 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
268   assert(CurSection && "Cannot emit contents before setting section!");
269   // FIXME: Implement proper printing.
270   OS << "MCInst("
271      << "opcode=" << Inst.getOpcode() << ", "
272      << "operands=[";
273   for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
274     if (i)
275       OS << ", ";
276     OS << Inst.getOperand(i);
277   }
278   OS << "])\n";
279 }
280
281 void MCAsmStreamer::Finish() {
282   OS.flush();
283 }
284     
285 MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
286   return new MCAsmStreamer(Context, OS);
287 }