Use %ull here.
[llvm/stm8.git] / lib / MC / MCObjectStreamer.cpp
blobef22eaaaa49836016cbaf1758d5def219c3a8cac
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 bool isPCRel, 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, isPCRel)));
106 DF->getContents().resize(DF->getContents().size() + Size, 0);
109 void MCObjectStreamer::EmitLabel(MCSymbol *Symbol) {
110 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
111 assert(getCurrentSection() && "Cannot emit before setting section!");
113 Symbol->setSection(*getCurrentSection());
115 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
117 // FIXME: This is wasteful, we don't necessarily need to create a data
118 // fragment. Instead, we should mark the symbol as pointing into the data
119 // fragment if it exists, otherwise we should just queue the label and set its
120 // fragment pointer when we emit the next fragment.
121 MCDataFragment *F = getOrCreateDataFragment();
122 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
123 SD.setFragment(F);
124 SD.setOffset(F->getContents().size());
127 void MCObjectStreamer::EmitULEB128Value(const MCExpr *Value,
128 unsigned AddrSpace) {
129 int64_t IntValue;
130 if (Value->EvaluateAsAbsolute(IntValue, getAssembler())) {
131 EmitULEB128IntValue(IntValue, AddrSpace);
132 return;
134 new MCLEBFragment(*Value, false, getCurrentSectionData());
137 void MCObjectStreamer::EmitSLEB128Value(const MCExpr *Value,
138 unsigned AddrSpace) {
139 int64_t IntValue;
140 if (Value->EvaluateAsAbsolute(IntValue, getAssembler())) {
141 EmitSLEB128IntValue(IntValue, AddrSpace);
142 return;
144 new MCLEBFragment(*Value, true, getCurrentSectionData());
147 void MCObjectStreamer::EmitWeakReference(MCSymbol *Alias,
148 const MCSymbol *Symbol) {
149 report_fatal_error("This file format doesn't support weak aliases.");
152 void MCObjectStreamer::ChangeSection(const MCSection *Section) {
153 assert(Section && "Cannot switch to a null section!");
155 CurSectionData = &getAssembler().getOrCreateSectionData(*Section);
158 void MCObjectStreamer::EmitInstruction(const MCInst &Inst) {
159 // Scan for values.
160 for (unsigned i = Inst.getNumOperands(); i--; )
161 if (Inst.getOperand(i).isExpr())
162 AddValueSymbols(Inst.getOperand(i).getExpr());
164 getCurrentSectionData()->setHasInstructions(true);
166 // Now that a machine instruction has been assembled into this section, make
167 // a line entry for any .loc directive that has been seen.
168 MCLineEntry::Make(this, getCurrentSection());
170 // If this instruction doesn't need relaxation, just emit it as data.
171 if (!getAssembler().getBackend().MayNeedRelaxation(Inst)) {
172 EmitInstToData(Inst);
173 return;
176 // Otherwise, if we are relaxing everything, relax the instruction as much as
177 // possible and emit it as data.
178 if (getAssembler().getRelaxAll()) {
179 MCInst Relaxed;
180 getAssembler().getBackend().RelaxInstruction(Inst, Relaxed);
181 while (getAssembler().getBackend().MayNeedRelaxation(Relaxed))
182 getAssembler().getBackend().RelaxInstruction(Relaxed, Relaxed);
183 EmitInstToData(Relaxed);
184 return;
187 // Otherwise emit to a separate fragment.
188 EmitInstToFragment(Inst);
191 void MCObjectStreamer::EmitInstToFragment(const MCInst &Inst) {
192 MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
194 raw_svector_ostream VecOS(IF->getCode());
195 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, IF->getFixups());
198 static const MCExpr *BuildSymbolDiff(MCContext &Context,
199 const MCSymbol *A, const MCSymbol *B) {
200 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
201 const MCExpr *ARef =
202 MCSymbolRefExpr::Create(A, Variant, Context);
203 const MCExpr *BRef =
204 MCSymbolRefExpr::Create(B, Variant, Context);
205 const MCExpr *AddrDelta =
206 MCBinaryExpr::Create(MCBinaryExpr::Sub, ARef, BRef, Context);
207 return AddrDelta;
210 static const MCExpr *ForceExpAbs(MCObjectStreamer *Streamer,
211 MCContext &Context, const MCExpr* Expr) {
212 if (Context.getAsmInfo().hasAggressiveSymbolFolding())
213 return Expr;
215 MCSymbol *ABS = Context.CreateTempSymbol();
216 Streamer->EmitAssignment(ABS, Expr);
217 return MCSymbolRefExpr::Create(ABS, Context);
220 void MCObjectStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
221 const MCSymbol *LastLabel,
222 const MCSymbol *Label) {
223 if (!LastLabel) {
224 int PointerSize = getContext().getTargetAsmInfo().getPointerSize();
225 EmitDwarfSetLineAddr(LineDelta, Label, PointerSize);
226 return;
228 const MCExpr *AddrDelta = BuildSymbolDiff(getContext(), Label, LastLabel);
229 int64_t Res;
230 if (AddrDelta->EvaluateAsAbsolute(Res, getAssembler())) {
231 MCDwarfLineAddr::Emit(this, LineDelta, Res);
232 return;
234 AddrDelta = ForceExpAbs(this, getContext(), AddrDelta);
235 new MCDwarfLineAddrFragment(LineDelta, *AddrDelta, getCurrentSectionData());
238 void MCObjectStreamer::EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
239 const MCSymbol *Label) {
240 const MCExpr *AddrDelta = BuildSymbolDiff(getContext(), Label, LastLabel);
241 int64_t Res;
242 if (AddrDelta->EvaluateAsAbsolute(Res, getAssembler())) {
243 MCDwarfFrameEmitter::EmitAdvanceLoc(*this, Res);
244 return;
246 AddrDelta = ForceExpAbs(this, getContext(), AddrDelta);
247 new MCDwarfCallFrameFragment(*AddrDelta, getCurrentSectionData());
250 void MCObjectStreamer::EmitValueToOffset(const MCExpr *Offset,
251 unsigned char Value) {
252 int64_t Res;
253 if (Offset->EvaluateAsAbsolute(Res, getAssembler())) {
254 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
255 return;
258 MCSymbol *CurrentPos = getContext().CreateTempSymbol();
259 EmitLabel(CurrentPos);
260 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
261 const MCExpr *Ref =
262 MCSymbolRefExpr::Create(CurrentPos, Variant, getContext());
263 const MCExpr *Delta =
264 MCBinaryExpr::Create(MCBinaryExpr::Sub, Offset, Ref, getContext());
266 if (!Delta->EvaluateAsAbsolute(Res, getAssembler()))
267 report_fatal_error("expected assembly-time absolute expression");
268 EmitFill(Res, Value, 0);
271 void MCObjectStreamer::Finish() {
272 // Dump out the dwarf file & directory tables and line tables.
273 if (getContext().hasDwarfFiles())
274 MCDwarfFileTable::Emit(this);
276 getAssembler().Finish();