add llvm-mc support for parsing the .lcomm directive, patch by Kevin Enderby!
[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/raw_ostream.h"
18 using namespace llvm;
19
20 namespace {
21
22   class MCAsmStreamer : public MCStreamer {
23     raw_ostream &OS;
24
25     MCSection *CurSection;
26
27   public:
28     MCAsmStreamer(MCContext &Context, raw_ostream &_OS)
29       : MCStreamer(Context), OS(_OS), CurSection(0) {}
30     ~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 EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
45                                   unsigned Pow2Alignment, bool IsLocal);
46
47     virtual void EmitBytes(const char *Data, unsigned Length);
48
49     virtual void EmitValue(const MCValue &Value, unsigned Size);
50
51     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     
58     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.
68 static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
69   if (Value.getSymA()) {
70     os << Value.getSymA()->getName();
71     if (Value.getSymB())
72       os << " - " << Value.getSymB()->getName();
73     if (Value.getConstant())
74       os << " + " << Value.getConstant();
75   } else {
76     assert(!Value.getSymB() && "Invalid machine code value!");
77     os << Value.getConstant();
78   }
79
80   return os;
81 }
82
83 static 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
88 static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
89   return MCValue::get(Value.getSymA(), Value.getSymB(), 
90                       truncateToSize(Value.getConstant(), Bytes));
91 }
92
93 void 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
104 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
105   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
110   OS << Symbol->getName() << ":\n";
111   Symbol->setSection(CurSection);
112   Symbol->setExternal(false);
113 }
114
115 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
116                                    bool MakeAbsolute) {
117   assert(!Symbol->getSection() && "Cannot assign to a label!");
118
119   if (MakeAbsolute) {
120     OS << ".set " << Symbol->getName() << ", " << Value << '\n';
121   } else {
122     OS << Symbol->getName() << " = " << Value << '\n';
123   }
124
125   getContext().SetSymbolValue(Symbol, Value);
126 }
127
128 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol, 
129                                         SymbolAttr Attribute) {
130   switch (Attribute) {
131   case Global: OS << ".globl"; break;
132   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;
137   case PrivateExtern: OS << ".private_extern"; break;
138   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;
143   }
144
145   OS << ' ' << Symbol->getName() << '\n';
146 }
147
148 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
149                                      unsigned Pow2Alignment, bool IsLocal) {
150   if (IsLocal)
151     OS << ".lcomm";
152   else
153     OS << ".comm";
154   OS << ' ' << Symbol->getName() << ',' << Size;
155   if (Pow2Alignment != 0)
156     OS << ',' << Pow2Alignment;
157   OS << '\n';
158 }
159
160 void MCAsmStreamer::EmitBytes(const char *Data, unsigned Length) {
161   assert(CurSection && "Cannot emit contents before setting section!");
162   for (unsigned i = 0; i != Length; ++i)
163     OS << ".byte " << (unsigned) Data[i] << '\n';
164 }
165
166 void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
167   assert(CurSection && "Cannot emit contents before setting section!");
168   // Need target hooks to know how to print this.
169   switch (Size) {
170   default:
171     assert(0 && "Invalid size for machine code value!");
172   case 1: OS << ".byte"; break;
173   case 2: OS << ".short"; break;
174   case 4: OS << ".long"; break;
175   case 8: OS << ".quad"; break;
176   }
177
178   OS << ' ' << truncateToSize(Value, Size) << '\n';
179 }
180
181 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
182                                          unsigned ValueSize,
183                                          unsigned MaxBytesToEmit) {
184   // Some assemblers don't support .balign, so we always emit as .p2align if
185   // this is a power of two. Otherwise we assume the client knows the target
186   // supports .balign and use that.
187   unsigned Pow2 = Log2_32(ByteAlignment);
188   bool IsPow2 = (1U << Pow2) == ByteAlignment;
189
190   switch (ValueSize) {
191   default:
192     assert(0 && "Invalid size for machine code value!");
193   case 8:
194     assert(0 && "Unsupported alignment size!");
195   case 1: OS << (IsPow2 ? ".p2align" : ".balign"); break;
196   case 2: OS << (IsPow2 ? ".p2alignw" : ".balignw"); break;
197   case 4: OS << (IsPow2 ? ".p2alignl" : ".balignl"); break;
198   }
199
200   OS << ' ' << (IsPow2 ? Pow2 : ByteAlignment);
201
202   OS << ", " << truncateToSize(Value, ValueSize);
203   if (MaxBytesToEmit) 
204     OS << ", " << MaxBytesToEmit;
205   OS << '\n';
206 }
207
208 void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset, 
209                                       unsigned char Value) {
210   // FIXME: Verify that Offset is associated with the current section.
211   OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
212 }
213
214 static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
215   if (Op.isReg())
216     return OS << "reg:" << Op.getReg();
217   if (Op.isImm())
218     return OS << "imm:" << Op.getImm();
219   if (Op.isMBBLabel())
220     return OS << "mbblabel:(" 
221               << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
222   assert(Op.isMCValue() && "Invalid operand!");
223   return OS << "val:" << Op.getMCValue();
224 }
225
226 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
227   assert(CurSection && "Cannot emit contents before setting section!");
228   // FIXME: Implement proper printing.
229   OS << "MCInst("
230      << "opcode=" << Inst.getOpcode() << ", "
231      << "operands=[";
232   for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
233     if (i)
234       OS << ", ";
235     OS << Inst.getOperand(i);
236   }
237   OS << "])\n";
238 }
239
240 void MCAsmStreamer::Finish() {
241   OS.flush();
242 }
243     
244 MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
245   return new MCAsmStreamer(Context, OS);
246 }