blob: e003a953adec380a823c5c90acffdec0ba7fab65 [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 Espindola0bbe0b42010-12-06 17:27:56 +000010#include "llvm/MC/MCAsmInfo.h"
Rafael Espindolaaf6b58082010-11-16 21:20:32 +000011#include "llvm/MC/MCContext.h"
Daniel Dunbara11af532009-06-24 01:03:06 +000012#include "llvm/MC/MCStreamer.h"
Chris Lattnerddf6bdd2010-01-19 18:45:47 +000013#include "llvm/MC/MCExpr.h"
Kevin Enderbyc0957932010-09-30 16:52:03 +000014#include "llvm/MC/MCObjectWriter.h"
Rafael Espindola484291c2010-11-01 14:28:48 +000015#include "llvm/Support/ErrorHandling.h"
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000016#include "llvm/Support/raw_ostream.h"
Chris Lattner58bc4dd2010-04-03 22:12:35 +000017#include "llvm/ADT/SmallString.h"
18#include "llvm/ADT/Twine.h"
Chris Lattner3580dea2010-04-03 21:48:59 +000019#include <cstdlib>
Daniel Dunbara11af532009-06-24 01:03:06 +000020using namespace llvm;
21
Benjamin Kramer1674b0b2010-09-02 18:53:37 +000022MCStreamer::MCStreamer(MCContext &Ctx) : Context(Ctx), CurSection(0),
23 PrevSection(0) {
Daniel Dunbara11af532009-06-24 01:03:06 +000024}
25
26MCStreamer::~MCStreamer() {
27}
Chris Lattnerddf6bdd2010-01-19 18:45:47 +000028
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000029raw_ostream &MCStreamer::GetCommentOS() {
30 // By default, discard comments.
31 return nulls();
32}
33
Rafael Espindola32a006e2010-12-03 00:55:40 +000034void 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}
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000045
Chris Lattner32ae3fe2010-01-19 22:03:38 +000046/// EmitIntValue - Special case of EmitValue that avoids the client having to
47/// pass in a MCExpr for constant integers.
48void MCStreamer::EmitIntValue(uint64_t Value, unsigned Size,
49 unsigned AddrSpace) {
Devang Pateldea914b2010-12-09 19:26:21 +000050 assert(Size <= 8 && "Invalid size");
51 assert(!(Size == 1 && (signed)Value > 255) && "Invalid size");
Rafael Espindola2df042c2010-12-03 02:54:21 +000052 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);
Chris Lattner32ae3fe2010-01-19 22:03:38 +000057}
58
Rafael Espindola3ff57092010-11-02 17:22:24 +000059/// EmitULEB128Value - Special case of EmitULEB128Value that avoids the
60/// client having to pass in a MCExpr for constant integers.
61void MCStreamer::EmitULEB128IntValue(uint64_t Value, unsigned AddrSpace) {
Rafael Espindola660b5fc2010-12-03 01:19:49 +000062 SmallString<32> Tmp;
63 raw_svector_ostream OSE(Tmp);
64 MCObjectWriter::EncodeULEB128(Value, OSE);
65 EmitBytes(OSE.str(), AddrSpace);
Kevin Enderbyc0957932010-09-30 16:52:03 +000066}
67
Rafael Espindola3ff57092010-11-02 17:22:24 +000068/// EmitSLEB128Value - Special case of EmitSLEB128Value that avoids the
69/// client having to pass in a MCExpr for constant integers.
70void MCStreamer::EmitSLEB128IntValue(int64_t Value, unsigned AddrSpace) {
Rafael Espindola660b5fc2010-12-03 01:19:49 +000071 SmallString<32> Tmp;
72 raw_svector_ostream OSE(Tmp);
73 MCObjectWriter::EncodeSLEB128(Value, OSE);
74 EmitBytes(OSE.str(), AddrSpace);
Kevin Enderbyc0957932010-09-30 16:52:03 +000075}
76
Rafael Espindola0bbe0b42010-12-06 17:27:56 +000077void 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
Chris Lattner6cde3e62010-03-09 00:39:24 +000088void MCStreamer::EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
89 unsigned AddrSpace) {
Rafael Espindolaf7fd4aa2010-12-10 04:01:09 +000090 EmitValue(MCSymbolRefExpr::Create(Sym, getContext()), Size, AddrSpace);
Chris Lattner6cde3e62010-03-09 00:39:24 +000091}
92
Rafael Espindola3e032112010-11-28 15:09:24 +000093void MCStreamer::EmitGPRel32Value(const MCExpr *Value) {
94 report_fatal_error("unsupported directive in streamer");
95}
96
Chris Lattnerddf6bdd2010-01-19 18:45:47 +000097/// EmitFill - Emit NumBytes bytes worth of the value specified by
98/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +000099void MCStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
100 unsigned AddrSpace) {
Chris Lattnerddf6bdd2010-01-19 18:45:47 +0000101 const MCExpr *E = MCConstantExpr::Create(FillValue, getContext());
102 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
Chris Lattneraaec2052010-01-19 19:46:13 +0000103 EmitValue(E, 1, AddrSpace);
Chris Lattnerddf6bdd2010-01-19 18:45:47 +0000104}
Chris Lattner91bead72010-04-03 21:35:55 +0000105
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000106bool MCStreamer::EmitDwarfFileDirective(unsigned FileNo,
107 StringRef Filename) {
108 return getContext().GetDwarfFile(Filename, FileNo) == 0;
109}
110
111void 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
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000119bool MCStreamer::EmitCFIStartProc() {
120 return false;
121}
122
123bool MCStreamer::EmitCFIEndProc() {
124 return false;
125}
126
127bool MCStreamer::EmitCFIDefCfaOffset(int64_t Offset) {
128 return false;
129}
130
131bool MCStreamer::EmitCFIDefCfaRegister(int64_t Register) {
132 return false;
133}
134
135bool MCStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) {
136 return false;
137}
138
139bool MCStreamer::EmitCFIPersonality(const MCSymbol *Sym) {
140 return false;
141}
142
143bool MCStreamer::EmitCFILsda(const MCSymbol *Sym) {
144 return false;
145}
146
Matt Flemingab3b3652010-05-20 19:45:09 +0000147/// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner91bead72010-04-03 21:35:55 +0000148/// the specified string in the output .s file. This capability is
149/// indicated by the hasRawTextSupport() predicate.
150void 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}
Chris Lattner58bc4dd2010-04-03 22:12:35 +0000155
156void MCStreamer::EmitRawText(const Twine &T) {
157 SmallString<128> Str;
158 T.toVector(Str);
159 EmitRawText(Str.str());
160}