Sketch streamer support for .align, .org functionality.
[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/MCSection.h"
14 #include "llvm/MC/MCSymbol.h"
15 #include "llvm/MC/MCValue.h"
16 #include "llvm/Support/raw_ostream.h"
17 using namespace llvm;
18
19 namespace {
20
21   class MCAsmStreamer : public MCStreamer {
22     raw_ostream &OS;
23
24     MCSection *CurSection;
25
26   public:
27     MCAsmStreamer(MCContext &Context, raw_ostream &_OS)
28       : MCStreamer(Context), OS(_OS) {}
29     ~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
47     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     
54     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.
64 inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
65   if (Value.getSymA()) {
66     os << Value.getSymA()->getName();
67     if (Value.getSymB())
68       os << " - " << Value.getSymB()->getName();
69     if (Value.getCst())
70       os << " + " << Value.getCst();
71   } else {
72     assert(!Value.getSymB() && "Invalid machine code value!");
73     os << Value.getCst();
74   }
75
76   return os;
77 }
78
79 void MCAsmStreamer::SwitchSection(MCSection *Section) {
80   if (Section != CurSection) {
81     CurSection = Section;
82
83     // FIXME: Really we would like the segment, flags, etc. to be separate
84     // values instead of embedded in the name. Not all assemblers understand all
85     // this stuff though.
86     OS << ".section " << Section->getName() << "\n";
87   }
88 }
89
90 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
91   assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
92   assert(CurSection && "Cannot emit before setting section!");
93   assert(!getContext().GetSymbolValue(Symbol) && 
94          "Cannot emit symbol which was directly assigned to!");
95
96   OS << Symbol->getName() << ":\n";
97   Symbol->setSection(CurSection);
98 }
99
100 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
101                                    bool MakeAbsolute) {
102   assert(!Symbol->getSection() && "Cannot assign to a label!");
103
104   if (MakeAbsolute) {
105     OS << ".set " << Symbol->getName() << ", " << Value << '\n';
106   } else {
107     OS << Symbol->getName() << " = " << Value << '\n';
108   }
109
110   getContext().SetSymbolValue(Symbol, Value);
111 }
112
113 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol, 
114                                         SymbolAttr Attribute) {
115   switch (Attribute) {
116   case Global: OS << ".globl"; break;
117   case Hidden: OS << ".hidden"; break;
118   case IndirectSymbol: OS << ".indirect_symbol"; break;
119   case Internal: OS << ".internal"; break;
120   case LazyReference: OS << ".lazy_reference"; break;
121   case NoDeadStrip: OS << ".no_dead_strip"; break;
122   case PrivateExtern: OS << ".private_extern"; break;
123   case Protected: OS << ".protected"; break;
124   case Reference: OS << ".reference"; break;
125   case Weak: OS << ".weak"; break;
126   case WeakDefinition: OS << ".weak_definition"; break;
127   case WeakReference: OS << ".weak_reference"; break;
128   }
129
130   OS << ' ' << Symbol->getName() << '\n';
131 }
132
133 void MCAsmStreamer::EmitBytes(const char *Data, unsigned Length) {
134   assert(CurSection && "Cannot emit contents before setting section!");
135   for (unsigned i = 0; i != Length; ++i)
136     OS << ".byte " << (unsigned) Data[i] << '\n';
137 }
138
139 void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
140   assert(CurSection && "Cannot emit contents before setting section!");
141   // Need target hooks to know how to print this.
142   switch (Size) {
143   default:
144     assert(0 && "Invalid size for machine code value!");
145   case 1: OS << ".byte"; break;
146   case 2: OS << ".short"; break;
147   case 4: OS << ".long"; break;
148   case 8: OS << ".quad"; break;
149   }
150
151   OS << ' ' << Value << '\n';
152 }
153
154 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
155                                          unsigned ValueSize,
156                                          unsigned MaxBytesToEmit) {
157   unsigned Pow2 = Log2_32(ByteAlignment);
158   assert((1U << Pow2) == ByteAlignment && "Invalid alignment!");
159
160   switch (ValueSize) {
161   default:
162     assert(0 && "Invalid size for machine code value!");
163   case 8:
164     assert(0 && "Unsupported alignment size!");
165   case 1: OS << ".p2align"; break;
166   case 2: OS << ".p2alignw"; break;
167   case 4: OS << ".p2alignl"; break;
168   }
169
170   OS << ' ' << Pow2;
171
172   OS << ", " << Value;
173   if (MaxBytesToEmit) 
174     OS << ", " << MaxBytesToEmit;
175   OS << '\n';
176 }
177
178 void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset, 
179                                       unsigned char Value) {
180   // FIXME: Verify that Offset is associated with the current section.
181   OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
182 }
183
184 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
185   assert(CurSection && "Cannot emit contents before setting section!");
186   // FIXME: Implement.
187   OS << "# FIXME: Implement instruction printing!\n";
188 }
189
190 void MCAsmStreamer::Finish() {
191   OS.flush();
192 }
193     
194 MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
195   return new MCAsmStreamer(Context, OS);
196 }