Second try at making direct object emission produce the same results
[oota-llvm.git] / lib / MC / MCStreamer.cpp
1 //===- lib/MC/MCStreamer.cpp - Streaming Machine Code Output --------------===//
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/MCAsmInfo.h"
11 #include "llvm/MC/MCContext.h"
12 #include "llvm/MC/MCStreamer.h"
13 #include "llvm/MC/MCExpr.h"
14 #include "llvm/MC/MCObjectWriter.h"
15 #include "llvm/Support/ErrorHandling.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/Twine.h"
19 #include <cstdlib>
20 using namespace llvm;
21
22 MCStreamer::MCStreamer(MCContext &Ctx) : Context(Ctx), CurSection(0),
23                                          PrevSection(0) {
24 }
25
26 MCStreamer::~MCStreamer() {
27 }
28
29 raw_ostream &MCStreamer::GetCommentOS() {
30   // By default, discard comments.
31   return nulls();
32 }
33
34 void MCStreamer::EmitDwarfSetLineAddr(int64_t LineDelta,
35                                       const MCSymbol *Label, int PointerSize) {
36   // emit the sequence to set the address
37   EmitIntValue(dwarf::DW_LNS_extended_op, 1);
38   EmitULEB128IntValue(PointerSize + 1);
39   EmitIntValue(dwarf::DW_LNE_set_address, 1);
40   EmitSymbolValue(Label, PointerSize);
41
42   // emit the sequence for the LineDelta (from 1) and a zero address delta.
43   MCDwarfLineAddr::Emit(this, LineDelta, 0);
44 }
45
46 /// EmitIntValue - Special case of EmitValue that avoids the client having to
47 /// pass in a MCExpr for constant integers.
48 void MCStreamer::EmitIntValue(uint64_t Value, unsigned Size,
49                               unsigned AddrSpace) {
50   assert(Size <= 8);
51   char buf[8];
52   // FIXME: Endianness assumption.
53   for (unsigned i = 0; i != Size; ++i)
54     buf[i] = uint8_t(Value >> (i * 8));
55   EmitBytes(StringRef(buf, Size), AddrSpace);
56 }
57
58 /// EmitULEB128Value - Special case of EmitULEB128Value that avoids the
59 /// client having to pass in a MCExpr for constant integers.
60 void MCStreamer::EmitULEB128IntValue(uint64_t Value, unsigned AddrSpace) {
61   SmallString<32> Tmp;
62   raw_svector_ostream OSE(Tmp);
63   MCObjectWriter::EncodeULEB128(Value, OSE);
64   EmitBytes(OSE.str(), AddrSpace);
65 }
66
67 /// EmitSLEB128Value - Special case of EmitSLEB128Value that avoids the
68 /// client having to pass in a MCExpr for constant integers.
69 void MCStreamer::EmitSLEB128IntValue(int64_t Value, unsigned AddrSpace) {
70   SmallString<32> Tmp;
71   raw_svector_ostream OSE(Tmp);
72   MCObjectWriter::EncodeSLEB128(Value, OSE);
73   EmitBytes(OSE.str(), AddrSpace);
74 }
75
76 void MCStreamer::EmitAbsValue(const MCExpr *Value, unsigned Size,
77                               unsigned AddrSpace) {
78   if (!getContext().getAsmInfo().needsSetToChangeDiffSize()) {
79     EmitValue(Value, Size, AddrSpace);
80     return;
81   }
82   MCSymbol *ABS = getContext().CreateTempSymbol();
83   EmitAssignment(ABS, Value);
84   EmitSymbolValue(ABS, Size, AddrSpace);
85 }
86
87 void MCStreamer::EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
88                                  unsigned AddrSpace) {
89   EmitValue(MCSymbolRefExpr::Create(Sym, getContext()), Size, AddrSpace);
90 }
91
92 void MCStreamer::EmitGPRel32Value(const MCExpr *Value) {
93   report_fatal_error("unsupported directive in streamer");
94 }
95
96 /// EmitFill - Emit NumBytes bytes worth of the value specified by
97 /// FillValue.  This implements directives such as '.space'.
98 void MCStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
99                           unsigned AddrSpace) {
100   const MCExpr *E = MCConstantExpr::Create(FillValue, getContext());
101   for (uint64_t i = 0, e = NumBytes; i != e; ++i)
102     EmitValue(E, 1, AddrSpace);
103 }
104
105 bool MCStreamer::EmitDwarfFileDirective(unsigned FileNo,
106                                         StringRef Filename) {
107   return getContext().GetDwarfFile(Filename, FileNo) == 0;
108 }
109
110 void MCStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
111                                        unsigned Column, unsigned Flags,
112                                        unsigned Isa,
113                                        unsigned Discriminator) {
114   getContext().setCurrentDwarfLoc(FileNo, Line, Column, Flags, Isa,
115                                   Discriminator);
116 }
117
118 bool MCStreamer::EmitCFIStartProc() {
119   return false;
120 }
121
122 bool MCStreamer::EmitCFIEndProc() {
123   return false;
124 }
125
126 bool MCStreamer::EmitCFIDefCfaOffset(int64_t Offset) {
127   return false;
128 }
129
130 bool MCStreamer::EmitCFIDefCfaRegister(int64_t Register) {
131   return false;
132 }
133
134 bool MCStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) {
135   return false;
136 }
137
138 bool MCStreamer::EmitCFIPersonality(const MCSymbol *Sym) {
139   return false;
140 }
141
142 bool MCStreamer::EmitCFILsda(const MCSymbol *Sym) {
143   return false;
144 }
145
146 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
147 /// the specified string in the output .s file.  This capability is
148 /// indicated by the hasRawTextSupport() predicate.
149 void MCStreamer::EmitRawText(StringRef String) {
150   errs() << "EmitRawText called on an MCStreamer that doesn't support it, "
151   " something must not be fully mc'ized\n";
152   abort();
153 }
154
155 void MCStreamer::EmitRawText(const Twine &T) {
156   SmallString<128> Str;
157   T.toVector(Str);
158   EmitRawText(Str.str());
159 }