Sorry for such a large commit. The summary is that only MachO cares about the
[oota-llvm.git] / lib / MC / MCObjectStreamer.cpp
1 //===- lib/MC/MCObjectStreamer.cpp - Object File MCStreamer Interface -----===//
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/MCObjectStreamer.h"
11
12 #include "llvm/Support/ErrorHandling.h"
13 #include "llvm/MC/MCAssembler.h"
14 #include "llvm/MC/MCCodeEmitter.h"
15 #include "llvm/MC/MCDwarf.h"
16 #include "llvm/MC/MCExpr.h"
17 #include "llvm/MC/MCSymbol.h"
18 #include "llvm/Target/TargetAsmBackend.h"
19 using namespace llvm;
20
21 MCObjectStreamer::MCObjectStreamer(MCContext &Context, TargetAsmBackend &TAB,
22                                    raw_ostream &_OS, MCCodeEmitter *_Emitter)
23   : MCStreamer(Context), Assembler(new MCAssembler(Context, TAB,
24                                                    *_Emitter,
25                                                    _OS)),
26     CurSectionData(0)
27 {
28 }
29
30 MCObjectStreamer::~MCObjectStreamer() {
31   delete &Assembler->getBackend();
32   delete &Assembler->getEmitter();
33   delete Assembler;
34 }
35
36 MCFragment *MCObjectStreamer::getCurrentFragment() const {
37   assert(getCurrentSectionData() && "No current section!");
38
39   if (!getCurrentSectionData()->empty())
40     return &getCurrentSectionData()->getFragmentList().back();
41
42   return 0;
43 }
44
45 MCDataFragment *MCObjectStreamer::getOrCreateDataFragment() const {
46   MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
47   if (!F)
48     F = new MCDataFragment(getCurrentSectionData());
49   return F;
50 }
51
52 const MCExpr *MCObjectStreamer::AddValueSymbols(const MCExpr *Value) {
53   switch (Value->getKind()) {
54   case MCExpr::Target: llvm_unreachable("Can't handle target exprs yet!");
55   case MCExpr::Constant:
56     break;
57
58   case MCExpr::Binary: {
59     const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
60     AddValueSymbols(BE->getLHS());
61     AddValueSymbols(BE->getRHS());
62     break;
63   }
64
65   case MCExpr::SymbolRef:
66     Assembler->getOrCreateSymbolData(cast<MCSymbolRefExpr>(Value)->getSymbol());
67     break;
68
69   case MCExpr::Unary:
70     AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
71     break;
72   }
73
74   return Value;
75 }
76
77 void MCObjectStreamer::EmitValue(const MCExpr *Value, unsigned Size,
78                                  unsigned AddrSpace) {
79   assert(AddrSpace == 0 && "Address space must be 0!");
80   MCDataFragment *DF = getOrCreateDataFragment();
81
82   // Avoid fixups when possible.
83   int64_t AbsValue;
84   if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue, getAssembler())) {
85     EmitIntValue(AbsValue, Size, AddrSpace);
86     return;
87   }
88   DF->addFixup(MCFixup::Create(DF->getContents().size(),
89                                AddValueSymbols(Value),
90                                MCFixup::getKindForSize(Size, false)));
91   DF->getContents().resize(DF->getContents().size() + Size, 0);
92 }
93
94 void MCObjectStreamer::EmitLabel(MCSymbol *Symbol) {
95   assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
96   assert(CurSection && "Cannot emit before setting section!");
97
98   Symbol->setSection(*CurSection);
99
100   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
101
102   // FIXME: This is wasteful, we don't necessarily need to create a data
103   // fragment. Instead, we should mark the symbol as pointing into the data
104   // fragment if it exists, otherwise we should just queue the label and set its
105   // fragment pointer when we emit the next fragment.
106   MCDataFragment *F = getOrCreateDataFragment();
107   assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
108   SD.setFragment(F);
109   SD.setOffset(F->getContents().size());
110 }
111
112 void MCObjectStreamer::EmitULEB128Value(const MCExpr *Value,
113                                         unsigned AddrSpace) {
114   int64_t IntValue;
115   if (Value->EvaluateAsAbsolute(IntValue, getAssembler())) {
116     EmitULEB128IntValue(IntValue, AddrSpace);
117     return;
118   }
119   new MCLEBFragment(*Value, false, getCurrentSectionData());
120 }
121
122 void MCObjectStreamer::EmitSLEB128Value(const MCExpr *Value,
123                                         unsigned AddrSpace) {
124   int64_t IntValue;
125   if (Value->EvaluateAsAbsolute(IntValue, getAssembler())) {
126     EmitSLEB128IntValue(IntValue, AddrSpace);
127     return;
128   }
129   new MCLEBFragment(*Value, true, getCurrentSectionData());
130 }
131
132 void MCObjectStreamer::EmitWeakReference(MCSymbol *Alias,
133                                          const MCSymbol *Symbol) {
134   report_fatal_error("This file format doesn't support weak aliases.");
135 }
136
137 void MCObjectStreamer::SwitchSection(const MCSection *Section) {
138   assert(Section && "Cannot switch to a null section!");
139
140   // If already in this section, then this is a noop.
141   if (Section == CurSection) return;
142
143   PrevSection = CurSection;
144   CurSection = Section;
145   CurSectionData = &getAssembler().getOrCreateSectionData(*Section);
146 }
147
148 void MCObjectStreamer::EmitInstruction(const MCInst &Inst) {
149   // Scan for values.
150   for (unsigned i = Inst.getNumOperands(); i--; )
151     if (Inst.getOperand(i).isExpr())
152       AddValueSymbols(Inst.getOperand(i).getExpr());
153
154   getCurrentSectionData()->setHasInstructions(true);
155
156   // Now that a machine instruction has been assembled into this section, make
157   // a line entry for any .loc directive that has been seen.
158   MCLineEntry::Make(this, getCurrentSection());
159
160   // If this instruction doesn't need relaxation, just emit it as data.
161   if (!getAssembler().getBackend().MayNeedRelaxation(Inst)) {
162     EmitInstToData(Inst);
163     return;
164   }
165
166   // Otherwise, if we are relaxing everything, relax the instruction as much as
167   // possible and emit it as data.
168   if (getAssembler().getRelaxAll()) {
169     MCInst Relaxed;
170     getAssembler().getBackend().RelaxInstruction(Inst, Relaxed);
171     while (getAssembler().getBackend().MayNeedRelaxation(Relaxed))
172       getAssembler().getBackend().RelaxInstruction(Relaxed, Relaxed);
173     EmitInstToData(Relaxed);
174     return;
175   }
176
177   // Otherwise emit to a separate fragment.
178   EmitInstToFragment(Inst);
179 }
180
181 void MCObjectStreamer::EmitInstToFragment(const MCInst &Inst) {
182   MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
183
184   raw_svector_ostream VecOS(IF->getCode());
185   getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, IF->getFixups());
186 }
187
188 void MCObjectStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
189                                                 const MCSymbol *LastLabel,
190                                                 const MCSymbol *Label) {
191   if (!LastLabel) {
192     int PointerSize = getAssembler().getBackend().getPointerSize();
193     EmitDwarfSetLineAddr(LineDelta, Label, PointerSize);
194     return;
195   }
196   MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
197   const MCExpr *LabelRef =
198     MCSymbolRefExpr::Create(Label, Variant, getContext());
199   const MCExpr *LastLabelRef =
200     MCSymbolRefExpr::Create(LastLabel, Variant, getContext());
201   const MCExpr *AddrDelta =
202     MCBinaryExpr::Create(MCBinaryExpr::Sub, LabelRef, LastLabelRef,
203                          getContext());
204   int64_t Res;
205   if (AddrDelta->EvaluateAsAbsolute(Res, getAssembler())) {
206     MCDwarfLineAddr::Emit(this, LineDelta, Res);
207     return;
208   }
209   new MCDwarfLineAddrFragment(LineDelta, *AddrDelta, getCurrentSectionData());
210 }
211
212 void MCObjectStreamer::EmitValueToOffset(const MCExpr *Offset,
213                                         unsigned char Value) {
214   new MCOrgFragment(*Offset, Value, getCurrentSectionData());
215 }
216
217 void MCObjectStreamer::Finish() {
218   getAssembler().Finish();
219 }