Add assert to catch an attempt to emit .byte 256
[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 && "Invalid size");
51   assert(!(Size == 1 && (signed)Value > 255) && "Invalid size");
52   char buf[8];
53   // FIXME: Endianness assumption.
54   for (unsigned i = 0; i != Size; ++i)
55     buf[i] = uint8_t(Value >> (i * 8));
56   EmitBytes(StringRef(buf, Size), AddrSpace);
57 }
58
59 /// EmitULEB128Value - Special case of EmitULEB128Value that avoids the
60 /// client having to pass in a MCExpr for constant integers.
61 void MCStreamer::EmitULEB128IntValue(uint64_t Value, unsigned AddrSpace) {
62   SmallString<32> Tmp;
63   raw_svector_ostream OSE(Tmp);
64   MCObjectWriter::EncodeULEB128(Value, OSE);
65   EmitBytes(OSE.str(), AddrSpace);
66 }
67
68 /// EmitSLEB128Value - Special case of EmitSLEB128Value that avoids the
69 /// client having to pass in a MCExpr for constant integers.
70 void MCStreamer::EmitSLEB128IntValue(int64_t Value, unsigned AddrSpace) {
71   SmallString<32> Tmp;
72   raw_svector_ostream OSE(Tmp);
73   MCObjectWriter::EncodeSLEB128(Value, OSE);
74   EmitBytes(OSE.str(), AddrSpace);
75 }
76
77 void MCStreamer::EmitAbsValue(const MCExpr *Value, unsigned Size,
78                               unsigned AddrSpace) {
79   if (!getContext().getAsmInfo().needsSetToChangeDiffSize()) {
80     EmitValue(Value, Size, AddrSpace);
81     return;
82   }
83   MCSymbol *ABS = getContext().CreateTempSymbol();
84   EmitAssignment(ABS, Value);
85   EmitSymbolValue(ABS, Size, AddrSpace);
86 }
87
88 void MCStreamer::EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
89                                  unsigned AddrSpace) {
90   EmitValue(MCSymbolRefExpr::Create(Sym, getContext()), Size, AddrSpace);
91 }
92
93 void MCStreamer::EmitGPRel32Value(const MCExpr *Value) {
94   report_fatal_error("unsupported directive in streamer");
95 }
96
97 /// EmitFill - Emit NumBytes bytes worth of the value specified by
98 /// FillValue.  This implements directives such as '.space'.
99 void MCStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
100                           unsigned AddrSpace) {
101   const MCExpr *E = MCConstantExpr::Create(FillValue, getContext());
102   for (uint64_t i = 0, e = NumBytes; i != e; ++i)
103     EmitValue(E, 1, AddrSpace);
104 }
105
106 bool MCStreamer::EmitDwarfFileDirective(unsigned FileNo,
107                                         StringRef Filename) {
108   return getContext().GetDwarfFile(Filename, FileNo) == 0;
109 }
110
111 void MCStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
112                                        unsigned Column, unsigned Flags,
113                                        unsigned Isa,
114                                        unsigned Discriminator) {
115   getContext().setCurrentDwarfLoc(FileNo, Line, Column, Flags, Isa,
116                                   Discriminator);
117 }
118
119 bool MCStreamer::EmitCFIStartProc() {
120   return false;
121 }
122
123 bool MCStreamer::EmitCFIEndProc() {
124   return false;
125 }
126
127 bool MCStreamer::EmitCFIDefCfaOffset(int64_t Offset) {
128   return false;
129 }
130
131 bool MCStreamer::EmitCFIDefCfaRegister(int64_t Register) {
132   return false;
133 }
134
135 bool MCStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) {
136   return false;
137 }
138
139 bool MCStreamer::EmitCFIPersonality(const MCSymbol *Sym) {
140   return false;
141 }
142
143 bool MCStreamer::EmitCFILsda(const MCSymbol *Sym) {
144   return false;
145 }
146
147 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
148 /// the specified string in the output .s file.  This capability is
149 /// indicated by the hasRawTextSupport() predicate.
150 void MCStreamer::EmitRawText(StringRef String) {
151   errs() << "EmitRawText called on an MCStreamer that doesn't support it, "
152   " something must not be fully mc'ized\n";
153   abort();
154 }
155
156 void MCStreamer::EmitRawText(const Twine &T) {
157   SmallString<128> Str;
158   T.toVector(Str);
159   EmitRawText(Str.str());
160 }