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