blob: 683f2c6adc684428e96ac842b9c62d3afa35251a [file] [log] [blame]
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +00001//===- 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
Michael J. Spencer8067adc2010-07-19 06:13:10 +000012#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +000013#include "llvm/MC/MCAssembler.h"
Benjamin Kramer1abcd062010-07-29 17:48:06 +000014#include "llvm/MC/MCCodeEmitter.h"
Rafael Espindolaf89671d2010-11-01 16:27:31 +000015#include "llvm/MC/MCDwarf.h"
Michael J. Spencer8067adc2010-07-19 06:13:10 +000016#include "llvm/MC/MCExpr.h"
Rafael Espindolaea4afa92010-11-28 17:18:55 +000017#include "llvm/MC/MCSymbol.h"
Benjamin Kramer1abcd062010-07-29 17:48:06 +000018#include "llvm/Target/TargetAsmBackend.h"
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +000019using namespace llvm;
20
21MCObjectStreamer::MCObjectStreamer(MCContext &Context, TargetAsmBackend &TAB,
Rafael Espindola85f2ecc2010-12-07 00:27:36 +000022 raw_ostream &_OS, MCCodeEmitter *_Emitter)
Daniel Dunbar83b46712010-06-16 20:04:25 +000023 : MCStreamer(Context), Assembler(new MCAssembler(Context, TAB,
Rafael Espindola59ff3c92010-09-22 22:27:05 +000024 *_Emitter,
Rafael Espindola59ff3c92010-09-22 22:27:05 +000025 _OS)),
Daniel Dunbar83b46712010-06-16 20:04:25 +000026 CurSectionData(0)
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +000027{
28}
29
30MCObjectStreamer::~MCObjectStreamer() {
Benjamin Kramer1abcd062010-07-29 17:48:06 +000031 delete &Assembler->getBackend();
32 delete &Assembler->getEmitter();
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +000033 delete Assembler;
34}
Daniel Dunbar83b46712010-06-16 20:04:25 +000035
Michael J. Spencer8067adc2010-07-19 06:13:10 +000036MCFragment *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
45MCDataFragment *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
52const 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
Rafael Espindolaf7fd4aa2010-12-10 04:01:09 +000077void MCObjectStreamer::EmitValue(const MCExpr *Value, unsigned Size,
78 unsigned AddrSpace) {
Rafael Espindola6f950232010-11-28 23:08:47 +000079 assert(AddrSpace == 0 && "Address space must be 0!");
80 MCDataFragment *DF = getOrCreateDataFragment();
81
82 // Avoid fixups when possible.
83 int64_t AbsValue;
Rafael Espindola22373b22010-12-06 22:30:54 +000084 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue, getAssembler())) {
Rafael Espindola2df042c2010-12-03 02:54:21 +000085 EmitIntValue(AbsValue, Size, AddrSpace);
86 return;
Rafael Espindola6f950232010-11-28 23:08:47 +000087 }
Rafael Espindola2df042c2010-12-03 02:54:21 +000088 DF->addFixup(MCFixup::Create(DF->getContents().size(),
89 AddValueSymbols(Value),
Rafael Espindolaf7fd4aa2010-12-10 04:01:09 +000090 MCFixup::getKindForSize(Size, false)));
Rafael Espindola2df042c2010-12-03 02:54:21 +000091 DF->getContents().resize(DF->getContents().size() + Size, 0);
Rafael Espindola6f950232010-11-28 23:08:47 +000092}
93
Rafael Espindolaea4afa92010-11-28 17:18:55 +000094void 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
Rafael Espindola3ff57092010-11-02 17:22:24 +0000112void MCObjectStreamer::EmitULEB128Value(const MCExpr *Value,
113 unsigned AddrSpace) {
Rafael Espindola660b5fc2010-12-03 01:19:49 +0000114 int64_t IntValue;
Rafael Espindola22373b22010-12-06 22:30:54 +0000115 if (Value->EvaluateAsAbsolute(IntValue, getAssembler())) {
Rafael Espindola660b5fc2010-12-03 01:19:49 +0000116 EmitULEB128IntValue(IntValue, AddrSpace);
117 return;
118 }
Rafael Espindola3ff57092010-11-02 17:22:24 +0000119 new MCLEBFragment(*Value, false, getCurrentSectionData());
120}
121
122void MCObjectStreamer::EmitSLEB128Value(const MCExpr *Value,
123 unsigned AddrSpace) {
Rafael Espindola660b5fc2010-12-03 01:19:49 +0000124 int64_t IntValue;
Rafael Espindola22373b22010-12-06 22:30:54 +0000125 if (Value->EvaluateAsAbsolute(IntValue, getAssembler())) {
Rafael Espindola660b5fc2010-12-03 01:19:49 +0000126 EmitSLEB128IntValue(IntValue, AddrSpace);
127 return;
128 }
Rafael Espindola3ff57092010-11-02 17:22:24 +0000129 new MCLEBFragment(*Value, true, getCurrentSectionData());
130}
131
Rafael Espindola484291c2010-11-01 14:28:48 +0000132void MCObjectStreamer::EmitWeakReference(MCSymbol *Alias,
133 const MCSymbol *Symbol) {
134 report_fatal_error("This file format doesn't support weak aliases.");
135}
136
Daniel Dunbar83b46712010-06-16 20:04:25 +0000137void 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
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000143 PrevSection = CurSection;
Daniel Dunbar83b46712010-06-16 20:04:25 +0000144 CurSection = Section;
145 CurSectionData = &getAssembler().getOrCreateSectionData(*Section);
146}
147
Rafael Espindolaf89671d2010-11-01 16:27:31 +0000148void 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
Rafael Espindoladedb0452010-12-02 05:44:06 +0000181void 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
Rafael Espindola32a006e2010-12-03 00:55:40 +0000188void MCObjectStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
189 const MCSymbol *LastLabel,
190 const MCSymbol *Label) {
191 if (!LastLabel) {
Rafael Espindolaf7fd4aa2010-12-10 04:01:09 +0000192 int PointerSize = getAssembler().getBackend().getPointerSize();
Rafael Espindola32a006e2010-12-03 00:55:40 +0000193 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;
Rafael Espindola22373b22010-12-06 22:30:54 +0000205 if (AddrDelta->EvaluateAsAbsolute(Res, getAssembler())) {
Rafael Espindola32a006e2010-12-03 00:55:40 +0000206 MCDwarfLineAddr::Emit(this, LineDelta, Res);
207 return;
208 }
209 new MCDwarfLineAddrFragment(LineDelta, *AddrDelta, getCurrentSectionData());
210}
211
Rafael Espindolae2393052010-12-02 05:59:38 +0000212void MCObjectStreamer::EmitValueToOffset(const MCExpr *Offset,
213 unsigned char Value) {
214 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
215}
216
Daniel Dunbar83b46712010-06-16 20:04:25 +0000217void MCObjectStreamer::Finish() {
218 getAssembler().Finish();
219}