blob: 421faef6aa41186e6f5b6aa2f2a31cc9b1db5490 [file] [log] [blame]
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +00001//===- lib/MC/MCMachOStreamer.cpp - Mach-O Object 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/MCStreamer.h"
11
12#include "llvm/ADT/DenseMap.h"
13#include "llvm/MC/MCAssembler.h"
14#include "llvm/MC/MCContext.h"
15#include "llvm/MC/MCSection.h"
16#include "llvm/MC/MCSymbol.h"
17#include "llvm/MC/MCValue.h"
18#include "llvm/Support/ErrorHandling.h"
19using namespace llvm;
20
21namespace {
22
23class MCMachOStreamer : public MCStreamer {
24 MCAssembler Assembler;
25
26 MCSectionData *CurSectionData;
27
28 DenseMap<const MCSection*, MCSectionData*> SectionMap;
29
30public:
31 MCMachOStreamer(MCContext &Context, raw_ostream &_OS)
32 : MCStreamer(Context), Assembler(_OS), CurSectionData(0) {}
33 ~MCMachOStreamer() {}
34
35 /// @name MCStreamer Interface
36 /// @{
37
38 virtual void SwitchSection(const MCSection *Section);
39
40 virtual void EmitLabel(MCSymbol *Symbol);
41
42 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
43
44 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
45 bool MakeAbsolute = false);
46
47 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
48
49 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
50
51 virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value);
52
53 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
54 unsigned Pow2Alignment, bool IsLocal);
55
56 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL,
57 unsigned Size = 0, unsigned Pow2Alignment = 0);
58
59 virtual void EmitBytes(const StringRef &Data);
60
61 virtual void EmitValue(const MCValue &Value, unsigned Size);
62
63 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
64 unsigned ValueSize = 1,
65 unsigned MaxBytesToEmit = 0);
66
67 virtual void EmitValueToOffset(const MCValue &Offset,
68 unsigned char Value = 0);
69
70 virtual void EmitInstruction(const MCInst &Inst);
71
72 virtual void Finish();
73
74 /// @}
75};
76
77} // end anonymous namespace.
78
79void MCMachOStreamer::SwitchSection(const MCSection *Section) {
80 assert(Section && "Cannot switch to a null section!");
81
82 if (Section != CurSection) {
83 CurSection = Section;
84 MCSectionData *&Entry = SectionMap[Section];
85
86 if (!Entry)
87 Entry = new MCSectionData(*Section, &Assembler);
88
89 CurSectionData = Entry;
90 }
91}
92
93void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +000094 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000095 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000096
97 llvm_unreachable("FIXME: Not yet implemented!");
98
Daniel Dunbar8906ff12009-08-22 07:22:36 +000099 Symbol->setSection(*CurSection);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000100}
101
102void MCMachOStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
103 llvm_unreachable("FIXME: Not yet implemented!");
104}
105
106void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol,
107 const MCValue &Value,
108 bool MakeAbsolute) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000109 // Only absolute symbols can be redefined.
110 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
111 "Cannot define a symbol twice!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000112
113 llvm_unreachable("FIXME: Not yet implemented!");
114}
115
116void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
117 SymbolAttr Attribute) {
118 llvm_unreachable("FIXME: Not yet implemented!");
119}
120
121void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
122 llvm_unreachable("FIXME: Not yet implemented!");
123}
124
125void MCMachOStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
126 llvm_unreachable("FIXME: Not yet implemented!");
127}
128
129void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
130 unsigned Pow2Alignment,
131 bool IsLocal) {
132 llvm_unreachable("FIXME: Not yet implemented!");
133}
134
135void MCMachOStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
136 unsigned Size, unsigned Pow2Alignment) {
137 llvm_unreachable("FIXME: Not yet implemented!");
138}
139
140void MCMachOStreamer::EmitBytes(const StringRef &Data) {
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000141 MCDataFragment *DF = new MCDataFragment(CurSectionData);
142 DF->getContents().append(Data.begin(), Data.end());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000143}
144
145void MCMachOStreamer::EmitValue(const MCValue &Value, unsigned Size) {
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000146 new MCFillFragment(Value, Size, 1, CurSectionData);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000147}
148
149void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
150 int64_t Value, unsigned ValueSize,
151 unsigned MaxBytesToEmit) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000152 if (MaxBytesToEmit == 0)
153 MaxBytesToEmit = ByteAlignment;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000154 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
155 CurSectionData);
156
157 // Update the maximum alignment on the current section if necessary
158 if (ByteAlignment > CurSectionData->getAlignment())
159 CurSectionData->setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000160}
161
162void MCMachOStreamer::EmitValueToOffset(const MCValue &Offset,
163 unsigned char Value) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000164 new MCOrgFragment(Offset, Value, CurSectionData);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000165}
166
167void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
168 llvm_unreachable("FIXME: Not yet implemented!");
169}
170
171void MCMachOStreamer::Finish() {
172 Assembler.Finish();
173}
174
175MCStreamer *llvm::createMachOStreamer(MCContext &Context, raw_ostream &OS) {
176 return new MCMachOStreamer(Context, OS);
177}