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