blob: 331f72688797331736424e56bf523f4ffa88050a [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 Espindola59ff3c92010-09-22 22:27:05 +000022 raw_ostream &_OS, MCCodeEmitter *_Emitter,
23 bool _PadSectionToAlignment)
Daniel Dunbar83b46712010-06-16 20:04:25 +000024 : MCStreamer(Context), Assembler(new MCAssembler(Context, TAB,
Rafael Espindola59ff3c92010-09-22 22:27:05 +000025 *_Emitter,
26 _PadSectionToAlignment,
27 _OS)),
Daniel Dunbar83b46712010-06-16 20:04:25 +000028 CurSectionData(0)
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +000029{
30}
31
32MCObjectStreamer::~MCObjectStreamer() {
Benjamin Kramer1abcd062010-07-29 17:48:06 +000033 delete &Assembler->getBackend();
34 delete &Assembler->getEmitter();
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +000035 delete Assembler;
36}
Daniel Dunbar83b46712010-06-16 20:04:25 +000037
Michael J. Spencer8067adc2010-07-19 06:13:10 +000038MCFragment *MCObjectStreamer::getCurrentFragment() const {
39 assert(getCurrentSectionData() && "No current section!");
40
41 if (!getCurrentSectionData()->empty())
42 return &getCurrentSectionData()->getFragmentList().back();
43
44 return 0;
45}
46
47MCDataFragment *MCObjectStreamer::getOrCreateDataFragment() const {
48 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
49 if (!F)
50 F = new MCDataFragment(getCurrentSectionData());
51 return F;
52}
53
54const MCExpr *MCObjectStreamer::AddValueSymbols(const MCExpr *Value) {
55 switch (Value->getKind()) {
56 case MCExpr::Target: llvm_unreachable("Can't handle target exprs yet!");
57 case MCExpr::Constant:
58 break;
59
60 case MCExpr::Binary: {
61 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
62 AddValueSymbols(BE->getLHS());
63 AddValueSymbols(BE->getRHS());
64 break;
65 }
66
67 case MCExpr::SymbolRef:
68 Assembler->getOrCreateSymbolData(cast<MCSymbolRefExpr>(Value)->getSymbol());
69 break;
70
71 case MCExpr::Unary:
72 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
73 break;
74 }
75
76 return Value;
77}
78
Rafael Espindola6f950232010-11-28 23:08:47 +000079void MCObjectStreamer::EmitValue(const MCExpr *Value, unsigned Size,
80 unsigned AddrSpace) {
81 assert(AddrSpace == 0 && "Address space must be 0!");
82 MCDataFragment *DF = getOrCreateDataFragment();
83
84 // Avoid fixups when possible.
85 int64_t AbsValue;
86 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
87 // FIXME: Endianness assumption.
88 for (unsigned i = 0; i != Size; ++i)
89 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
90 } else {
91 DF->addFixup(MCFixup::Create(DF->getContents().size(),
92 AddValueSymbols(Value),
93 MCFixup::getKindForSize(Size, false)));
94 DF->getContents().resize(DF->getContents().size() + Size, 0);
95 }
96}
97
Rafael Espindolaea4afa92010-11-28 17:18:55 +000098void MCObjectStreamer::EmitLabel(MCSymbol *Symbol) {
99 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
100 assert(CurSection && "Cannot emit before setting section!");
101
102 Symbol->setSection(*CurSection);
103
104 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
105
106 // FIXME: This is wasteful, we don't necessarily need to create a data
107 // fragment. Instead, we should mark the symbol as pointing into the data
108 // fragment if it exists, otherwise we should just queue the label and set its
109 // fragment pointer when we emit the next fragment.
110 MCDataFragment *F = getOrCreateDataFragment();
111 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
112 SD.setFragment(F);
113 SD.setOffset(F->getContents().size());
114}
115
Rafael Espindola3ff57092010-11-02 17:22:24 +0000116void MCObjectStreamer::EmitULEB128Value(const MCExpr *Value,
117 unsigned AddrSpace) {
118 new MCLEBFragment(*Value, false, getCurrentSectionData());
119}
120
121void MCObjectStreamer::EmitSLEB128Value(const MCExpr *Value,
122 unsigned AddrSpace) {
123 new MCLEBFragment(*Value, true, getCurrentSectionData());
124}
125
Rafael Espindola484291c2010-11-01 14:28:48 +0000126void MCObjectStreamer::EmitWeakReference(MCSymbol *Alias,
127 const MCSymbol *Symbol) {
128 report_fatal_error("This file format doesn't support weak aliases.");
129}
130
Daniel Dunbar83b46712010-06-16 20:04:25 +0000131void MCObjectStreamer::SwitchSection(const MCSection *Section) {
132 assert(Section && "Cannot switch to a null section!");
133
134 // If already in this section, then this is a noop.
135 if (Section == CurSection) return;
136
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000137 PrevSection = CurSection;
Daniel Dunbar83b46712010-06-16 20:04:25 +0000138 CurSection = Section;
139 CurSectionData = &getAssembler().getOrCreateSectionData(*Section);
140}
141
Rafael Espindolaf89671d2010-11-01 16:27:31 +0000142void MCObjectStreamer::EmitInstruction(const MCInst &Inst) {
143 // Scan for values.
144 for (unsigned i = Inst.getNumOperands(); i--; )
145 if (Inst.getOperand(i).isExpr())
146 AddValueSymbols(Inst.getOperand(i).getExpr());
147
148 getCurrentSectionData()->setHasInstructions(true);
149
150 // Now that a machine instruction has been assembled into this section, make
151 // a line entry for any .loc directive that has been seen.
152 MCLineEntry::Make(this, getCurrentSection());
153
154 // If this instruction doesn't need relaxation, just emit it as data.
155 if (!getAssembler().getBackend().MayNeedRelaxation(Inst)) {
156 EmitInstToData(Inst);
157 return;
158 }
159
160 // Otherwise, if we are relaxing everything, relax the instruction as much as
161 // possible and emit it as data.
162 if (getAssembler().getRelaxAll()) {
163 MCInst Relaxed;
164 getAssembler().getBackend().RelaxInstruction(Inst, Relaxed);
165 while (getAssembler().getBackend().MayNeedRelaxation(Relaxed))
166 getAssembler().getBackend().RelaxInstruction(Relaxed, Relaxed);
167 EmitInstToData(Relaxed);
168 return;
169 }
170
171 // Otherwise emit to a separate fragment.
172 EmitInstToFragment(Inst);
173}
174
Rafael Espindoladedb0452010-12-02 05:44:06 +0000175void MCObjectStreamer::EmitInstToFragment(const MCInst &Inst) {
176 MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
177
178 raw_svector_ostream VecOS(IF->getCode());
179 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, IF->getFixups());
180}
181
Rafael Espindolae2393052010-12-02 05:59:38 +0000182void MCObjectStreamer::EmitValueToOffset(const MCExpr *Offset,
183 unsigned char Value) {
184 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
185}
186
Daniel Dunbar83b46712010-06-16 20:04:25 +0000187void MCObjectStreamer::Finish() {
188 getAssembler().Finish();
189}