1563bdd107fcaa814894e1db14a942e9b8781cdc
[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/MCObjectStreamer.h"
16 // FIXME: Remove this.
17 #include "llvm/MC/MCSectionMachO.h"
18 #include "llvm/MC/MCSymbol.h"
19 #include "llvm/Support/ErrorHandling.h"
20
21 using namespace llvm;
22
23 namespace {
24
25 class MCPureStreamer : public MCObjectStreamer {
26 private:
27   virtual void EmitInstToFragment(const MCInst &Inst);
28   virtual void EmitInstToData(const MCInst &Inst);
29
30 public:
31   MCPureStreamer(MCContext &Context, MCAsmBackend &TAB,
32                  raw_ostream &OS, MCCodeEmitter *Emitter)
33     : MCObjectStreamer(Context, TAB, OS, Emitter) {}
34
35   /// @name MCStreamer Interface
36   /// @{
37
38   virtual void InitSections();
39   virtual void EmitLabel(MCSymbol *Symbol);
40   virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
41                             uint64_t Size = 0, unsigned ByteAlignment = 0);
42   virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
43   virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
44                                     unsigned ValueSize = 1,
45                                     unsigned MaxBytesToEmit = 0);
46   virtual void EmitCodeAlignment(unsigned ByteAlignment,
47                                  unsigned MaxBytesToEmit = 0);
48   virtual bool EmitValueToOffset(const MCExpr *Offset,
49                                  unsigned char Value = 0);
50   virtual void FinishImpl();
51
52
53   virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
54     report_fatal_error("unsupported directive in pure streamer");
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 bool EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
96                                       StringRef Filename) {
97     report_fatal_error("unsupported directive in pure streamer");
98   }
99
100   /// @}
101 };
102
103 } // end anonymous namespace.
104
105 void MCPureStreamer::InitSections() {
106   // FIMXE: To what!?
107   SwitchSection(getContext().getMachOSection("__TEXT", "__text",
108                                     MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
109                                     0, SectionKind::getText()));
110
111 }
112
113 void MCPureStreamer::EmitLabel(MCSymbol *Symbol) {
114   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
115   assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
116   assert(getCurrentSection() && "Cannot emit before setting section!");
117
118   Symbol->setSection(*getCurrentSection());
119
120   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
121
122   // We have to create a new fragment if this is an atom defining symbol,
123   // fragments cannot span atoms.
124   if (getAssembler().isSymbolLinkerVisible(SD.getSymbol()))
125     new MCDataFragment(getCurrentSectionData());
126
127   // FIXME: This is wasteful, we don't necessarily need to create a data
128   // fragment. Instead, we should mark the symbol as pointing into the data
129   // fragment if it exists, otherwise we should just queue the label and set its
130   // fragment pointer when we emit the next fragment.
131   MCDataFragment *F = getOrCreateDataFragment();
132   assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
133   SD.setFragment(F);
134   SD.setOffset(F->getContents().size());
135 }
136
137 void MCPureStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
138                                   uint64_t Size, unsigned ByteAlignment) {
139   report_fatal_error("not yet implemented in pure streamer");
140 }
141
142 void MCPureStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
143   // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
144   // MCObjectStreamer.
145   getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
146 }
147
148 void MCPureStreamer::EmitValueToAlignment(unsigned ByteAlignment,
149                                           int64_t Value, unsigned ValueSize,
150                                           unsigned MaxBytesToEmit) {
151   // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
152   // MCObjectStreamer.
153   if (MaxBytesToEmit == 0)
154     MaxBytesToEmit = ByteAlignment;
155   new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
156                       getCurrentSectionData());
157
158   // Update the maximum alignment on the current section if necessary.
159   if (ByteAlignment > getCurrentSectionData()->getAlignment())
160     getCurrentSectionData()->setAlignment(ByteAlignment);
161 }
162
163 void MCPureStreamer::EmitCodeAlignment(unsigned ByteAlignment,
164                                        unsigned MaxBytesToEmit) {
165   // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
166   // MCObjectStreamer.
167   if (MaxBytesToEmit == 0)
168     MaxBytesToEmit = ByteAlignment;
169   MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
170                                            getCurrentSectionData());
171   F->setEmitNops(true);
172
173   // Update the maximum alignment on the current section if necessary.
174   if (ByteAlignment > getCurrentSectionData()->getAlignment())
175     getCurrentSectionData()->setAlignment(ByteAlignment);
176 }
177
178 bool MCPureStreamer::EmitValueToOffset(const MCExpr *Offset,
179                                        unsigned char Value) {
180   new MCOrgFragment(*Offset, Value, getCurrentSectionData());
181   return false;
182 }
183
184 void MCPureStreamer::EmitInstToFragment(const MCInst &Inst) {
185   MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
186
187   // Add the fixups and data.
188   //
189   // FIXME: Revisit this design decision when relaxation is done, we may be
190   // able to get away with not storing any extra data in the MCInst.
191   SmallVector<MCFixup, 4> Fixups;
192   SmallString<256> Code;
193   raw_svector_ostream VecOS(Code);
194   getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
195   VecOS.flush();
196
197   IF->getContents() = Code;
198   IF->getFixups() = Fixups;
199 }
200
201 void MCPureStreamer::EmitInstToData(const MCInst &Inst) {
202   MCDataFragment *DF = getOrCreateDataFragment();
203
204   SmallVector<MCFixup, 4> Fixups;
205   SmallString<256> Code;
206   raw_svector_ostream VecOS(Code);
207   getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
208   VecOS.flush();
209
210   // Add the fixups and data.
211   for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
212     Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
213     DF->getFixups().push_back(Fixups[i]);
214   }
215   DF->getContents().append(Code.begin(), Code.end());
216 }
217
218 void MCPureStreamer::FinishImpl() {
219   // FIXME: Handle DWARF tables?
220
221   this->MCObjectStreamer::FinishImpl();
222 }
223
224 MCStreamer *llvm::createPureStreamer(MCContext &Context, MCAsmBackend &MAB,
225                                      raw_ostream &OS, MCCodeEmitter *CE) {
226   return new MCPureStreamer(Context, MAB, OS, CE);
227 }