blob: 406910e255d7d6c8b3924d5d2ea7a842b9b45f54 [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
Rafael Espindola7768a9d2011-02-16 01:08:29 +000022MCStreamer::MCStreamer(MCContext &Ctx) : Context(Ctx) {
Joerg Sonnenberger7d0805d2011-02-21 01:07:42 +000023 const MCSection *section = NULL;
24 SectionStack.push_back(std::make_pair(section, section));
Daniel Dunbara11af532009-06-24 01:03:06 +000025}
26
27MCStreamer::~MCStreamer() {
28}
Chris Lattnerddf6bdd2010-01-19 18:45:47 +000029
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000030raw_ostream &MCStreamer::GetCommentOS() {
31 // By default, discard comments.
32 return nulls();
33}
34
Rafael Espindola32a006e2010-12-03 00:55:40 +000035void MCStreamer::EmitDwarfSetLineAddr(int64_t LineDelta,
36 const MCSymbol *Label, int PointerSize) {
37 // emit the sequence to set the address
38 EmitIntValue(dwarf::DW_LNS_extended_op, 1);
39 EmitULEB128IntValue(PointerSize + 1);
40 EmitIntValue(dwarf::DW_LNE_set_address, 1);
41 EmitSymbolValue(Label, PointerSize);
42
43 // emit the sequence for the LineDelta (from 1) and a zero address delta.
44 MCDwarfLineAddr::Emit(this, LineDelta, 0);
45}
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000046
Chris Lattner32ae3fe2010-01-19 22:03:38 +000047/// EmitIntValue - Special case of EmitValue that avoids the client having to
48/// pass in a MCExpr for constant integers.
49void MCStreamer::EmitIntValue(uint64_t Value, unsigned Size,
50 unsigned AddrSpace) {
Devang Pateldea914b2010-12-09 19:26:21 +000051 assert(Size <= 8 && "Invalid size");
Matt Beaumont-Gay818b5f52010-12-15 23:14:45 +000052 assert((isUIntN(8 * Size, Value) || isIntN(8 * Size, Value)) &&
53 "Invalid size");
Rafael Espindola2df042c2010-12-03 02:54:21 +000054 char buf[8];
55 // FIXME: Endianness assumption.
56 for (unsigned i = 0; i != Size; ++i)
57 buf[i] = uint8_t(Value >> (i * 8));
58 EmitBytes(StringRef(buf, Size), AddrSpace);
Chris Lattner32ae3fe2010-01-19 22:03:38 +000059}
60
Rafael Espindola3ff57092010-11-02 17:22:24 +000061/// EmitULEB128Value - Special case of EmitULEB128Value that avoids the
62/// client having to pass in a MCExpr for constant integers.
63void MCStreamer::EmitULEB128IntValue(uint64_t Value, unsigned AddrSpace) {
Rafael Espindola660b5fc2010-12-03 01:19:49 +000064 SmallString<32> Tmp;
65 raw_svector_ostream OSE(Tmp);
66 MCObjectWriter::EncodeULEB128(Value, OSE);
67 EmitBytes(OSE.str(), AddrSpace);
Kevin Enderbyc0957932010-09-30 16:52:03 +000068}
69
Rafael Espindola3ff57092010-11-02 17:22:24 +000070/// EmitSLEB128Value - Special case of EmitSLEB128Value that avoids the
71/// client having to pass in a MCExpr for constant integers.
72void MCStreamer::EmitSLEB128IntValue(int64_t Value, unsigned AddrSpace) {
Rafael Espindola660b5fc2010-12-03 01:19:49 +000073 SmallString<32> Tmp;
74 raw_svector_ostream OSE(Tmp);
75 MCObjectWriter::EncodeSLEB128(Value, OSE);
76 EmitBytes(OSE.str(), AddrSpace);
Kevin Enderbyc0957932010-09-30 16:52:03 +000077}
78
Rafael Espindola0bbe0b42010-12-06 17:27:56 +000079void MCStreamer::EmitAbsValue(const MCExpr *Value, unsigned Size,
80 unsigned AddrSpace) {
Rafael Espindola90a5a0c2010-12-22 21:51:29 +000081 if (getContext().getAsmInfo().hasAggressiveSymbolFolding()) {
Rafael Espindola0bbe0b42010-12-06 17:27:56 +000082 EmitValue(Value, Size, AddrSpace);
83 return;
84 }
85 MCSymbol *ABS = getContext().CreateTempSymbol();
86 EmitAssignment(ABS, Value);
87 EmitSymbolValue(ABS, Size, AddrSpace);
88}
89
Rafael Espindola89b93722010-12-10 07:39:47 +000090
91void MCStreamer::EmitValue(const MCExpr *Value, unsigned Size,
92 unsigned AddrSpace) {
93 EmitValueImpl(Value, Size, false, AddrSpace);
94}
95
96void MCStreamer::EmitPCRelValue(const MCExpr *Value, unsigned Size,
97 unsigned AddrSpace) {
98 EmitValueImpl(Value, Size, true, AddrSpace);
99}
100
101void MCStreamer::EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
102 bool isPCRel, unsigned AddrSpace) {
103 EmitValueImpl(MCSymbolRefExpr::Create(Sym, getContext()), Size, isPCRel,
104 AddrSpace);
105}
106
Chris Lattner6cde3e62010-03-09 00:39:24 +0000107void MCStreamer::EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
108 unsigned AddrSpace) {
Rafael Espindola89b93722010-12-10 07:39:47 +0000109 EmitSymbolValue(Sym, Size, false, AddrSpace);
110}
111
112void MCStreamer::EmitPCRelSymbolValue(const MCSymbol *Sym, unsigned Size,
113 unsigned AddrSpace) {
114 EmitSymbolValue(Sym, Size, true, AddrSpace);
Chris Lattner6cde3e62010-03-09 00:39:24 +0000115}
116
Rafael Espindola3e032112010-11-28 15:09:24 +0000117void MCStreamer::EmitGPRel32Value(const MCExpr *Value) {
118 report_fatal_error("unsupported directive in streamer");
119}
120
Chris Lattnerddf6bdd2010-01-19 18:45:47 +0000121/// EmitFill - Emit NumBytes bytes worth of the value specified by
122/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +0000123void MCStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
124 unsigned AddrSpace) {
Chris Lattnerddf6bdd2010-01-19 18:45:47 +0000125 const MCExpr *E = MCConstantExpr::Create(FillValue, getContext());
126 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
Chris Lattneraaec2052010-01-19 19:46:13 +0000127 EmitValue(E, 1, AddrSpace);
Chris Lattnerddf6bdd2010-01-19 18:45:47 +0000128}
Chris Lattner91bead72010-04-03 21:35:55 +0000129
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000130bool MCStreamer::EmitDwarfFileDirective(unsigned FileNo,
131 StringRef Filename) {
132 return getContext().GetDwarfFile(Filename, FileNo) == 0;
133}
134
135void MCStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
136 unsigned Column, unsigned Flags,
137 unsigned Isa,
Devang Patel3f3bf932011-04-18 20:26:49 +0000138 unsigned Discriminator,
139 StringRef FileName) {
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000140 getContext().setCurrentDwarfLoc(FileNo, Line, Column, Flags, Isa,
141 Discriminator);
142}
143
Rafael Espindola89b93722010-12-10 07:39:47 +0000144MCDwarfFrameInfo *MCStreamer::getCurrentFrameInfo() {
145 if (FrameInfos.empty())
146 return NULL;
147 return &FrameInfos.back();
148}
149
150void MCStreamer::EnsureValidFrame() {
151 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo();
152 if (!CurFrame || CurFrame->End)
153 report_fatal_error("No open frame");
154}
155
Rafael Espindola066c2f42011-04-12 23:59:07 +0000156void MCStreamer::EmitCFIStartProc() {
Rafael Espindola89b93722010-12-10 07:39:47 +0000157 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo();
Rafael Espindola066c2f42011-04-12 23:59:07 +0000158 if (CurFrame && !CurFrame->End)
Rafael Espindola89b93722010-12-10 07:39:47 +0000159 report_fatal_error("Starting a frame before finishing the previous one!");
Rafael Espindola5bba0842010-12-28 04:15:37 +0000160 MCDwarfFrameInfo Frame;
Rafael Espindola89b93722010-12-10 07:39:47 +0000161 Frame.Begin = getContext().CreateTempSymbol();
162 EmitLabel(Frame.Begin);
163 FrameInfos.push_back(Frame);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000164}
165
Rafael Espindola066c2f42011-04-12 23:59:07 +0000166void MCStreamer::EmitCFIEndProc() {
Rafael Espindola89b93722010-12-10 07:39:47 +0000167 EnsureValidFrame();
168 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo();
169 CurFrame->End = getContext().CreateTempSymbol();
170 EmitLabel(CurFrame->End);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000171}
172
Rafael Espindola066c2f42011-04-12 23:59:07 +0000173void MCStreamer::EmitCFIDefCfa(int64_t Register, int64_t Offset) {
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000174 EnsureValidFrame();
175 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo();
176 MCSymbol *Label = getContext().CreateTempSymbol();
177 EmitLabel(Label);
178 MachineLocation Dest(MachineLocation::VirtualFP);
179 MachineLocation Source(Register, -Offset);
180 MCCFIInstruction Instruction(Label, Dest, Source);
181 CurFrame->Instructions.push_back(Instruction);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000182}
183
Rafael Espindola066c2f42011-04-12 23:59:07 +0000184void MCStreamer::EmitCFIDefCfaOffset(int64_t Offset) {
Rafael Espindola89b93722010-12-10 07:39:47 +0000185 EnsureValidFrame();
Rafael Espindola5bba0842010-12-28 04:15:37 +0000186 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo();
187 MCSymbol *Label = getContext().CreateTempSymbol();
188 EmitLabel(Label);
189 MachineLocation Dest(MachineLocation::VirtualFP);
190 MachineLocation Source(MachineLocation::VirtualFP, -Offset);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000191 MCCFIInstruction Instruction(Label, Dest, Source);
192 CurFrame->Instructions.push_back(Instruction);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000193}
194
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000195void MCStreamer::EmitCFIAdjustCfaOffset(int64_t Adjustment) {
196 EnsureValidFrame();
197 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo();
198 MCSymbol *Label = getContext().CreateTempSymbol();
199 EmitLabel(Label);
200 MachineLocation Dest(MachineLocation::VirtualFP);
201 MachineLocation Source(MachineLocation::VirtualFP, Adjustment);
202 MCCFIInstruction Instruction(MCCFIInstruction::RelMove, Label, Dest, Source);
203 CurFrame->Instructions.push_back(Instruction);
204}
205
Rafael Espindola066c2f42011-04-12 23:59:07 +0000206void MCStreamer::EmitCFIDefCfaRegister(int64_t Register) {
Rafael Espindola89b93722010-12-10 07:39:47 +0000207 EnsureValidFrame();
Rafael Espindolab4601bd2010-12-29 00:26:06 +0000208 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo();
209 MCSymbol *Label = getContext().CreateTempSymbol();
210 EmitLabel(Label);
211 MachineLocation Dest(Register);
212 MachineLocation Source(MachineLocation::VirtualFP);
213 MCCFIInstruction Instruction(Label, Dest, Source);
214 CurFrame->Instructions.push_back(Instruction);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000215}
216
Rafael Espindola066c2f42011-04-12 23:59:07 +0000217void MCStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) {
Rafael Espindola89b93722010-12-10 07:39:47 +0000218 EnsureValidFrame();
Rafael Espindolab790a172010-12-29 00:09:59 +0000219 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo();
220 MCSymbol *Label = getContext().CreateTempSymbol();
221 EmitLabel(Label);
222 MachineLocation Dest(Register, Offset);
223 MachineLocation Source(Register, Offset);
224 MCCFIInstruction Instruction(Label, Dest, Source);
225 CurFrame->Instructions.push_back(Instruction);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000226}
227
Rafael Espindola25f492e2011-04-12 16:12:03 +0000228void MCStreamer::EmitCFIRelOffset(int64_t Register, int64_t Offset) {
229 EnsureValidFrame();
230 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo();
231 MCSymbol *Label = getContext().CreateTempSymbol();
232 EmitLabel(Label);
233 MachineLocation Dest(Register, Offset);
234 MachineLocation Source(Register, Offset);
235 MCCFIInstruction Instruction(MCCFIInstruction::RelMove, Label, Dest, Source);
236 CurFrame->Instructions.push_back(Instruction);
237}
238
Rafael Espindola066c2f42011-04-12 23:59:07 +0000239void MCStreamer::EmitCFIPersonality(const MCSymbol *Sym,
Rafael Espindola3a83c402010-12-27 00:36:05 +0000240 unsigned Encoding) {
Rafael Espindola89b93722010-12-10 07:39:47 +0000241 EnsureValidFrame();
242 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo();
243 CurFrame->Personality = Sym;
Rafael Espindola3a83c402010-12-27 00:36:05 +0000244 CurFrame->PersonalityEncoding = Encoding;
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000245}
246
Rafael Espindola066c2f42011-04-12 23:59:07 +0000247void MCStreamer::EmitCFILsda(const MCSymbol *Sym, unsigned Encoding) {
Rafael Espindola89b93722010-12-10 07:39:47 +0000248 EnsureValidFrame();
249 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo();
250 CurFrame->Lsda = Sym;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000251 CurFrame->LsdaEncoding = Encoding;
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000252}
253
Rafael Espindola066c2f42011-04-12 23:59:07 +0000254void MCStreamer::EmitCFIRememberState() {
Rafael Espindolafe024d02010-12-28 18:36:23 +0000255 EnsureValidFrame();
256 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo();
257 MCSymbol *Label = getContext().CreateTempSymbol();
258 EmitLabel(Label);
259 MCCFIInstruction Instruction(MCCFIInstruction::Remember, Label);
260 CurFrame->Instructions.push_back(Instruction);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000261}
262
Rafael Espindola066c2f42011-04-12 23:59:07 +0000263void MCStreamer::EmitCFIRestoreState() {
Rafael Espindolafe024d02010-12-28 18:36:23 +0000264 // FIXME: Error if there is no matching cfi_remember_state.
265 EnsureValidFrame();
266 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo();
267 MCSymbol *Label = getContext().CreateTempSymbol();
268 EmitLabel(Label);
269 MCCFIInstruction Instruction(MCCFIInstruction::Restore, Label);
270 CurFrame->Instructions.push_back(Instruction);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000271}
272
Rafael Espindolac5754392011-04-12 15:31:05 +0000273void MCStreamer::EmitCFISameValue(int64_t Register) {
274 EnsureValidFrame();
275 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo();
276 MCSymbol *Label = getContext().CreateTempSymbol();
277 EmitLabel(Label);
278 MCCFIInstruction Instruction(MCCFIInstruction::SameValue, Label, Register);
279 CurFrame->Instructions.push_back(Instruction);
280}
281
Anton Korobeynikovb5e16af2011-03-05 18:43:15 +0000282void MCStreamer::EmitFnStart() {
283 errs() << "Not implemented yet\n";
284 abort();
285}
286
287void MCStreamer::EmitFnEnd() {
288 errs() << "Not implemented yet\n";
289 abort();
290}
291
292void MCStreamer::EmitCantUnwind() {
293 errs() << "Not implemented yet\n";
294 abort();
295}
296
297void MCStreamer::EmitHandlerData() {
298 errs() << "Not implemented yet\n";
299 abort();
300}
301
302void MCStreamer::EmitPersonality(const MCSymbol *Personality) {
303 errs() << "Not implemented yet\n";
304 abort();
305}
306
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000307void MCStreamer::EmitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset) {
308 errs() << "Not implemented yet\n";
309 abort();
310}
311
312void MCStreamer::EmitPad(int64_t Offset) {
313 errs() << "Not implemented yet\n";
314 abort();
315}
316
317void MCStreamer::EmitRegSave(const SmallVectorImpl<unsigned> &RegList, bool) {
318 errs() << "Not implemented yet\n";
319 abort();
320}
321
Matt Flemingab3b3652010-05-20 19:45:09 +0000322/// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner91bead72010-04-03 21:35:55 +0000323/// the specified string in the output .s file. This capability is
324/// indicated by the hasRawTextSupport() predicate.
325void MCStreamer::EmitRawText(StringRef String) {
326 errs() << "EmitRawText called on an MCStreamer that doesn't support it, "
327 " something must not be fully mc'ized\n";
328 abort();
329}
Chris Lattner58bc4dd2010-04-03 22:12:35 +0000330
331void MCStreamer::EmitRawText(const Twine &T) {
332 SmallString<128> Str;
333 T.toVector(Str);
334 EmitRawText(Str.str());
335}