When promoting an alloca to registers discard any lifetime intrinsics.
[llvm/stm8.git] / lib / MC / MCObjectStreamer.cpp
blobe230c53302039471345f4ec929c828205d2e4bce
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 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCAsmInfo.h"
11 #include "llvm/MC/MCObjectStreamer.h"
13 #include "llvm/Support/ErrorHandling.h"
14 #include "llvm/MC/MCAssembler.h"
15 #include "llvm/MC/MCCodeEmitter.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCDwarf.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/MC/MCSymbol.h"
20 #include "llvm/Target/TargetAsmBackend.h"
21 #include "llvm/Target/TargetAsmInfo.h"
22 using namespace llvm;
24 MCObjectStreamer::MCObjectStreamer(MCContext &Context, TargetAsmBackend &TAB,
25 raw_ostream &OS, MCCodeEmitter *Emitter_)
26 : MCStreamer(Context),
27 Assembler(new MCAssembler(Context, TAB,
28 *Emitter_, *TAB.createObjectWriter(OS),
29 OS)),
30 CurSectionData(0)
34 MCObjectStreamer::MCObjectStreamer(MCContext &Context, TargetAsmBackend &TAB,
35 raw_ostream &OS, MCCodeEmitter *Emitter_,
36 MCAssembler *_Assembler)
37 : MCStreamer(Context), Assembler(_Assembler), CurSectionData(0)
41 MCObjectStreamer::~MCObjectStreamer() {
42 delete &Assembler->getBackend();
43 delete &Assembler->getEmitter();
44 delete &Assembler->getWriter();
45 delete Assembler;
48 MCFragment *MCObjectStreamer::getCurrentFragment() const {
49 assert(getCurrentSectionData() && "No current section!");
51 if (!getCurrentSectionData()->empty())
52 return &getCurrentSectionData()->getFragmentList().back();
54 return 0;
57 MCDataFragment *MCObjectStreamer::getOrCreateDataFragment() const {
58 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
59 if (!F)
60 F = new MCDataFragment(getCurrentSectionData());
61 return F;
64 const MCExpr *MCObjectStreamer::AddValueSymbols(const MCExpr *Value) {
65 switch (Value->getKind()) {
66 case MCExpr::Target:
67 cast<MCTargetExpr>(Value)->AddValueSymbols(Assembler);
68 break;
70 case MCExpr::Constant:
71 break;
73 case MCExpr::Binary: {
74 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
75 AddValueSymbols(BE->getLHS());
76 AddValueSymbols(BE->getRHS());
77 break;
80 case MCExpr::SymbolRef:
81 Assembler->getOrCreateSymbolData(cast<MCSymbolRefExpr>(Value)->getSymbol());
82 break;
84 case MCExpr::Unary:
85 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
86 break;
89 return Value;
92 void MCObjectStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
93 unsigned AddrSpace) {
94 assert(AddrSpace == 0 && "Address space must be 0!");
95 MCDataFragment *DF = getOrCreateDataFragment();
97 // Avoid fixups when possible.
98 int64_t AbsValue;
99 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue, getAssembler())) {
100 EmitIntValue(AbsValue, Size, AddrSpace);
101 return;
103 DF->addFixup(MCFixup::Create(DF->getContents().size(),
104 Value,
105 MCFixup::getKindForSize(Size, false)));
106 DF->getContents().resize(DF->getContents().size() + Size, 0);
109 void MCObjectStreamer::EmitLabel(MCSymbol *Symbol) {
110 MCStreamer::EmitLabel(Symbol);
112 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
114 // FIXME: This is wasteful, we don't necessarily need to create a data
115 // fragment. Instead, we should mark the symbol as pointing into the data
116 // fragment if it exists, otherwise we should just queue the label and set its
117 // fragment pointer when we emit the next fragment.
118 MCDataFragment *F = getOrCreateDataFragment();
119 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
120 SD.setFragment(F);
121 SD.setOffset(F->getContents().size());
124 void MCObjectStreamer::EmitULEB128Value(const MCExpr *Value) {
125 int64_t IntValue;
126 if (Value->EvaluateAsAbsolute(IntValue, getAssembler())) {
127 EmitULEB128IntValue(IntValue);
128 return;
130 Value = ForceExpAbs(Value);
131 new MCLEBFragment(*Value, false, getCurrentSectionData());
134 void MCObjectStreamer::EmitSLEB128Value(const MCExpr *Value) {
135 int64_t IntValue;
136 if (Value->EvaluateAsAbsolute(IntValue, getAssembler())) {
137 EmitSLEB128IntValue(IntValue);
138 return;
140 Value = ForceExpAbs(Value);
141 new MCLEBFragment(*Value, true, getCurrentSectionData());
144 void MCObjectStreamer::EmitWeakReference(MCSymbol *Alias,
145 const MCSymbol *Symbol) {
146 report_fatal_error("This file format doesn't support weak aliases.");
149 void MCObjectStreamer::ChangeSection(const MCSection *Section) {
150 assert(Section && "Cannot switch to a null section!");
152 CurSectionData = &getAssembler().getOrCreateSectionData(*Section);
155 void MCObjectStreamer::EmitInstruction(const MCInst &Inst) {
156 // Scan for values.
157 for (unsigned i = Inst.getNumOperands(); i--; )
158 if (Inst.getOperand(i).isExpr())
159 AddValueSymbols(Inst.getOperand(i).getExpr());
161 getCurrentSectionData()->setHasInstructions(true);
163 // Now that a machine instruction has been assembled into this section, make
164 // a line entry for any .loc directive that has been seen.
165 MCLineEntry::Make(this, getCurrentSection());
167 // If this instruction doesn't need relaxation, just emit it as data.
168 if (!getAssembler().getBackend().MayNeedRelaxation(Inst)) {
169 EmitInstToData(Inst);
170 return;
173 // Otherwise, if we are relaxing everything, relax the instruction as much as
174 // possible and emit it as data.
175 if (getAssembler().getRelaxAll()) {
176 MCInst Relaxed;
177 getAssembler().getBackend().RelaxInstruction(Inst, Relaxed);
178 while (getAssembler().getBackend().MayNeedRelaxation(Relaxed))
179 getAssembler().getBackend().RelaxInstruction(Relaxed, Relaxed);
180 EmitInstToData(Relaxed);
181 return;
184 // Otherwise emit to a separate fragment.
185 EmitInstToFragment(Inst);
188 void MCObjectStreamer::EmitInstToFragment(const MCInst &Inst) {
189 MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
191 SmallString<128> Code;
192 raw_svector_ostream VecOS(Code);
193 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, IF->getFixups());
194 VecOS.flush();
195 IF->getCode().append(Code.begin(), Code.end());
198 void MCObjectStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
199 const MCSymbol *LastLabel,
200 const MCSymbol *Label) {
201 if (!LastLabel) {
202 int PointerSize = getContext().getTargetAsmInfo().getPointerSize();
203 EmitDwarfSetLineAddr(LineDelta, Label, PointerSize);
204 return;
206 const MCExpr *AddrDelta = BuildSymbolDiff(getContext(), Label, LastLabel);
207 int64_t Res;
208 if (AddrDelta->EvaluateAsAbsolute(Res, getAssembler())) {
209 MCDwarfLineAddr::Emit(this, LineDelta, Res);
210 return;
212 AddrDelta = ForceExpAbs(AddrDelta);
213 new MCDwarfLineAddrFragment(LineDelta, *AddrDelta, getCurrentSectionData());
216 void MCObjectStreamer::EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
217 const MCSymbol *Label) {
218 const MCExpr *AddrDelta = BuildSymbolDiff(getContext(), Label, LastLabel);
219 int64_t Res;
220 if (AddrDelta->EvaluateAsAbsolute(Res, getAssembler())) {
221 MCDwarfFrameEmitter::EmitAdvanceLoc(*this, Res);
222 return;
224 AddrDelta = ForceExpAbs(AddrDelta);
225 new MCDwarfCallFrameFragment(*AddrDelta, getCurrentSectionData());
228 void MCObjectStreamer::EmitValueToOffset(const MCExpr *Offset,
229 unsigned char Value) {
230 int64_t Res;
231 if (Offset->EvaluateAsAbsolute(Res, getAssembler())) {
232 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
233 return;
236 MCSymbol *CurrentPos = getContext().CreateTempSymbol();
237 EmitLabel(CurrentPos);
238 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
239 const MCExpr *Ref =
240 MCSymbolRefExpr::Create(CurrentPos, Variant, getContext());
241 const MCExpr *Delta =
242 MCBinaryExpr::Create(MCBinaryExpr::Sub, Offset, Ref, getContext());
244 if (!Delta->EvaluateAsAbsolute(Res, getAssembler()))
245 report_fatal_error("expected assembly-time absolute expression");
246 EmitFill(Res, Value, 0);
249 void MCObjectStreamer::Finish() {
250 // Dump out the dwarf file & directory tables and line tables.
251 if (getContext().hasDwarfFiles())
252 MCDwarfFileTable::Emit(this);
254 getAssembler().Finish();