Most streamers' InitSections just create a text section. Make that the default
[oota-llvm.git] / lib / MC / MCPureStreamer.cpp
1 //===- lib/MC/MCPureStreamer.cpp - MC "Pure" Object 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 #include "llvm/MC/MCAssembler.h"
12 #include "llvm/MC/MCCodeEmitter.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCExpr.h"
15 #include "llvm/MC/MCObjectFileInfo.h"
16 #include "llvm/MC/MCObjectStreamer.h"
17 #include "llvm/MC/MCSymbol.h"
18 #include "llvm/Support/ErrorHandling.h"
19
20 using namespace llvm;
21
22 namespace {
23
24 class MCPureStreamer : public MCObjectStreamer {
25 private:
26   virtual void EmitInstToFragment(const MCInst &Inst);
27   virtual void EmitInstToData(const MCInst &Inst);
28
29 public:
30   MCPureStreamer(MCContext &Context, MCAsmBackend &TAB, raw_ostream &OS,
31                  MCCodeEmitter *Emitter)
32       : MCObjectStreamer(Context, 0, TAB, OS, Emitter) {}
33
34   /// @name MCStreamer Interface
35   /// @{
36
37   virtual void EmitLabel(MCSymbol *Symbol);
38   virtual void EmitDebugLabel(MCSymbol *Symbol);
39   virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
40                             uint64_t Size = 0, unsigned ByteAlignment = 0);
41   virtual void EmitBytes(StringRef Data);
42   virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
43                                     unsigned ValueSize = 1,
44                                     unsigned MaxBytesToEmit = 0);
45   virtual void EmitCodeAlignment(unsigned ByteAlignment,
46                                  unsigned MaxBytesToEmit = 0);
47   virtual bool EmitValueToOffset(const MCExpr *Offset,
48                                  unsigned char Value = 0);
49   virtual void FinishImpl();
50
51
52   virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
53     report_fatal_error("unsupported directive in pure streamer");
54     return false;
55   }
56   virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
57     report_fatal_error("unsupported directive in pure streamer");
58   }
59   virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
60                               uint64_t Size, unsigned ByteAlignment = 0) {
61     report_fatal_error("unsupported directive in pure streamer");
62   }
63   virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
64     report_fatal_error("unsupported directive in pure streamer");
65   }
66   virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
67                                 unsigned ByteAlignment) {
68     report_fatal_error("unsupported directive in pure streamer");
69   }
70   virtual void EmitThumbFunc(MCSymbol *Func) {
71     report_fatal_error("unsupported directive in pure streamer");
72   }
73   virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
74     report_fatal_error("unsupported directive in pure streamer");
75   }
76   virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
77     report_fatal_error("unsupported directive in pure streamer");
78   }
79   virtual void EmitCOFFSymbolType(int Type) {
80     report_fatal_error("unsupported directive in pure streamer");
81   }
82   virtual void EndCOFFSymbolDef() {
83     report_fatal_error("unsupported directive in pure streamer");
84   }
85   virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
86     report_fatal_error("unsupported directive in pure streamer");
87   }
88   virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
89                                      unsigned ByteAlignment) {
90     report_fatal_error("unsupported directive in pure streamer");
91   }
92   virtual void EmitFileDirective(StringRef Filename) {
93     report_fatal_error("unsupported directive in pure streamer");
94   }
95   virtual void EmitIdent(StringRef IdentString) {
96     report_fatal_error("unsupported directive in pure streamer");
97   }
98   virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
99                                       StringRef Filename, unsigned CUID = 0) {
100     report_fatal_error("unsupported directive in pure streamer");
101   }
102 };
103
104 } // end anonymous namespace.
105
106 void MCPureStreamer::EmitLabel(MCSymbol *Symbol) {
107   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
108   assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
109   assert(getCurrentSection().first && "Cannot emit before setting section!");
110
111   AssignSection(Symbol, getCurrentSection().first);
112
113   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
114
115   // We have to create a new fragment if this is an atom defining symbol,
116   // fragments cannot span atoms.
117   if (getAssembler().isSymbolLinkerVisible(SD.getSymbol()))
118     insert(new MCDataFragment());
119
120   // FIXME: This is wasteful, we don't necessarily need to create a data
121   // fragment. Instead, we should mark the symbol as pointing into the data
122   // fragment if it exists, otherwise we should just queue the label and set its
123   // fragment pointer when we emit the next fragment.
124   MCDataFragment *F = getOrCreateDataFragment();
125   assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
126   SD.setFragment(F);
127   SD.setOffset(F->getContents().size());
128 }
129
130
131 void MCPureStreamer::EmitDebugLabel(MCSymbol *Symbol) {
132   EmitLabel(Symbol);
133 }
134
135 void MCPureStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
136                                   uint64_t Size, unsigned ByteAlignment) {
137   report_fatal_error("not yet implemented in pure streamer");
138 }
139
140 void MCPureStreamer::EmitBytes(StringRef Data) {
141   // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
142   // MCObjectStreamer.
143   getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
144 }
145
146 void MCPureStreamer::EmitValueToAlignment(unsigned ByteAlignment,
147                                           int64_t Value, unsigned ValueSize,
148                                           unsigned MaxBytesToEmit) {
149   // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
150   // MCObjectStreamer.
151   if (MaxBytesToEmit == 0)
152     MaxBytesToEmit = ByteAlignment;
153   insert(new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit));
154
155   // Update the maximum alignment on the current section if necessary.
156   if (ByteAlignment > getCurrentSectionData()->getAlignment())
157     getCurrentSectionData()->setAlignment(ByteAlignment);
158 }
159
160 void MCPureStreamer::EmitCodeAlignment(unsigned ByteAlignment,
161                                        unsigned MaxBytesToEmit) {
162   // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
163   // MCObjectStreamer.
164   if (MaxBytesToEmit == 0)
165     MaxBytesToEmit = ByteAlignment;
166   MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit);
167   insert(F);
168   F->setEmitNops(true);
169
170   // Update the maximum alignment on the current section if necessary.
171   if (ByteAlignment > getCurrentSectionData()->getAlignment())
172     getCurrentSectionData()->setAlignment(ByteAlignment);
173 }
174
175 bool MCPureStreamer::EmitValueToOffset(const MCExpr *Offset,
176                                        unsigned char Value) {
177   insert(new MCOrgFragment(*Offset, Value));
178   return false;
179 }
180
181 void MCPureStreamer::EmitInstToFragment(const MCInst &Inst) {
182   MCRelaxableFragment *IF = new MCRelaxableFragment(Inst);
183   insert(IF);
184
185   // Add the fixups and data.
186   //
187   // FIXME: Revisit this design decision when relaxation is done, we may be
188   // able to get away with not storing any extra data in the MCInst.
189   SmallVector<MCFixup, 4> Fixups;
190   SmallString<256> Code;
191   raw_svector_ostream VecOS(Code);
192   getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
193   VecOS.flush();
194
195   IF->getContents() = Code;
196   IF->getFixups() = Fixups;
197 }
198
199 void MCPureStreamer::EmitInstToData(const MCInst &Inst) {
200   MCDataFragment *DF = getOrCreateDataFragment();
201
202   SmallVector<MCFixup, 4> Fixups;
203   SmallString<256> Code;
204   raw_svector_ostream VecOS(Code);
205   getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
206   VecOS.flush();
207
208   // Add the fixups and data.
209   for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
210     Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
211     DF->getFixups().push_back(Fixups[i]);
212   }
213   DF->getContents().append(Code.begin(), Code.end());
214 }
215
216 void MCPureStreamer::FinishImpl() {
217   // FIXME: Handle DWARF tables?
218
219   this->MCObjectStreamer::FinishImpl();
220 }
221
222 MCStreamer *llvm::createPureStreamer(MCContext &Context, MCAsmBackend &MAB,
223                                      raw_ostream &OS, MCCodeEmitter *CE) {
224   return new MCPureStreamer(Context, MAB, OS, CE);
225 }