blob: 6df4ae44e40e2b5b3b3e69a8374936726fe9b5dd [file] [log] [blame]
Daniel Dunbara11af532009-06-24 01:03:06 +00001//===- 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
Rafael Espindolaaf6b58082010-11-16 21:20:32 +000010#include "llvm/MC/MCContext.h"
Daniel Dunbara11af532009-06-24 01:03:06 +000011#include "llvm/MC/MCStreamer.h"
Chris Lattnerddf6bdd2010-01-19 18:45:47 +000012#include "llvm/MC/MCExpr.h"
Kevin Enderbyc0957932010-09-30 16:52:03 +000013#include "llvm/MC/MCObjectWriter.h"
Rafael Espindola484291c2010-11-01 14:28:48 +000014#include "llvm/Support/ErrorHandling.h"
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000015#include "llvm/Support/raw_ostream.h"
Chris Lattner58bc4dd2010-04-03 22:12:35 +000016#include "llvm/ADT/SmallString.h"
17#include "llvm/ADT/Twine.h"
Chris Lattner3580dea2010-04-03 21:48:59 +000018#include <cstdlib>
Daniel Dunbara11af532009-06-24 01:03:06 +000019using namespace llvm;
20
Benjamin Kramer1674b0b2010-09-02 18:53:37 +000021MCStreamer::MCStreamer(MCContext &Ctx) : Context(Ctx), CurSection(0),
22 PrevSection(0) {
Daniel Dunbara11af532009-06-24 01:03:06 +000023}
24
25MCStreamer::~MCStreamer() {
26}
Chris Lattnerddf6bdd2010-01-19 18:45:47 +000027
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000028raw_ostream &MCStreamer::GetCommentOS() {
29 // By default, discard comments.
30 return nulls();
31}
32
Rafael Espindola32a006e2010-12-03 00:55:40 +000033void 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}
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000044
Chris Lattner32ae3fe2010-01-19 22:03:38 +000045/// EmitIntValue - Special case of EmitValue that avoids the client having to
46/// pass in a MCExpr for constant integers.
47void MCStreamer::EmitIntValue(uint64_t Value, unsigned Size,
48 unsigned AddrSpace) {
Rafael Espindola2df042c2010-12-03 02:54:21 +000049 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);
Chris Lattner32ae3fe2010-01-19 22:03:38 +000055}
56
Rafael Espindola3ff57092010-11-02 17:22:24 +000057/// EmitULEB128Value - Special case of EmitULEB128Value that avoids the
58/// client having to pass in a MCExpr for constant integers.
59void MCStreamer::EmitULEB128IntValue(uint64_t Value, unsigned AddrSpace) {
Rafael Espindola660b5fc2010-12-03 01:19:49 +000060 SmallString<32> Tmp;
61 raw_svector_ostream OSE(Tmp);
62 MCObjectWriter::EncodeULEB128(Value, OSE);
63 EmitBytes(OSE.str(), AddrSpace);
Kevin Enderbyc0957932010-09-30 16:52:03 +000064}
65
Rafael Espindola3ff57092010-11-02 17:22:24 +000066/// EmitSLEB128Value - Special case of EmitSLEB128Value that avoids the
67/// client having to pass in a MCExpr for constant integers.
68void MCStreamer::EmitSLEB128IntValue(int64_t Value, unsigned AddrSpace) {
Rafael Espindola660b5fc2010-12-03 01:19:49 +000069 SmallString<32> Tmp;
70 raw_svector_ostream OSE(Tmp);
71 MCObjectWriter::EncodeSLEB128(Value, OSE);
72 EmitBytes(OSE.str(), AddrSpace);
Kevin Enderbyc0957932010-09-30 16:52:03 +000073}
74
Chris Lattner6cde3e62010-03-09 00:39:24 +000075void MCStreamer::EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
76 unsigned AddrSpace) {
77 EmitValue(MCSymbolRefExpr::Create(Sym, getContext()), Size, AddrSpace);
78}
79
Rafael Espindola3e032112010-11-28 15:09:24 +000080void MCStreamer::EmitGPRel32Value(const MCExpr *Value) {
81 report_fatal_error("unsupported directive in streamer");
82}
83
Chris Lattnerddf6bdd2010-01-19 18:45:47 +000084/// EmitFill - Emit NumBytes bytes worth of the value specified by
85/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +000086void MCStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
87 unsigned AddrSpace) {
Chris Lattnerddf6bdd2010-01-19 18:45:47 +000088 const MCExpr *E = MCConstantExpr::Create(FillValue, getContext());
89 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
Chris Lattneraaec2052010-01-19 19:46:13 +000090 EmitValue(E, 1, AddrSpace);
Chris Lattnerddf6bdd2010-01-19 18:45:47 +000091}
Chris Lattner91bead72010-04-03 21:35:55 +000092
Rafael Espindolaaf6b58082010-11-16 21:20:32 +000093bool MCStreamer::EmitDwarfFileDirective(unsigned FileNo,
94 StringRef Filename) {
95 return getContext().GetDwarfFile(Filename, FileNo) == 0;
96}
97
98void MCStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
99 unsigned Column, unsigned Flags,
100 unsigned Isa,
101 unsigned Discriminator) {
102 getContext().setCurrentDwarfLoc(FileNo, Line, Column, Flags, Isa,
103 Discriminator);
104}
105
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000106bool MCStreamer::EmitCFIStartProc() {
107 return false;
108}
109
110bool MCStreamer::EmitCFIEndProc() {
111 return false;
112}
113
114bool MCStreamer::EmitCFIDefCfaOffset(int64_t Offset) {
115 return false;
116}
117
118bool MCStreamer::EmitCFIDefCfaRegister(int64_t Register) {
119 return false;
120}
121
122bool MCStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) {
123 return false;
124}
125
126bool MCStreamer::EmitCFIPersonality(const MCSymbol *Sym) {
127 return false;
128}
129
130bool MCStreamer::EmitCFILsda(const MCSymbol *Sym) {
131 return false;
132}
133
Matt Flemingab3b3652010-05-20 19:45:09 +0000134/// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner91bead72010-04-03 21:35:55 +0000135/// the specified string in the output .s file. This capability is
136/// indicated by the hasRawTextSupport() predicate.
137void MCStreamer::EmitRawText(StringRef String) {
138 errs() << "EmitRawText called on an MCStreamer that doesn't support it, "
139 " something must not be fully mc'ized\n";
140 abort();
141}
Chris Lattner58bc4dd2010-04-03 22:12:35 +0000142
143void MCStreamer::EmitRawText(const Twine &T) {
144 SmallString<128> Str;
145 T.toVector(Str);
146 EmitRawText(Str.str());
147}