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