blob: 1a648ea5e4b9f19d53fe2c5e02a0d6bc313f9bbc [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;
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000029
30 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
31
32private:
33 MCFragment *getCurrentFragment() const {
34 assert(CurSectionData && "No current section!");
35
36 if (!CurSectionData->empty())
37 return &CurSectionData->getFragmentList().back();
38
39 return 0;
40 }
41
42 MCSymbolData &getSymbolData(MCSymbol &Symbol) {
43 MCSymbolData *&Entry = SymbolMap[&Symbol];
44
45 if (!Entry)
46 Entry = new MCSymbolData(Symbol, 0, 0, &Assembler);
47
48 return *Entry;
49 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000050
51public:
52 MCMachOStreamer(MCContext &Context, raw_ostream &_OS)
53 : MCStreamer(Context), Assembler(_OS), CurSectionData(0) {}
54 ~MCMachOStreamer() {}
55
56 /// @name MCStreamer Interface
57 /// @{
58
59 virtual void SwitchSection(const MCSection *Section);
60
61 virtual void EmitLabel(MCSymbol *Symbol);
62
63 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
64
65 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
66 bool MakeAbsolute = false);
67
68 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
69
70 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
71
72 virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value);
73
74 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
75 unsigned Pow2Alignment, bool IsLocal);
76
77 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL,
78 unsigned Size = 0, unsigned Pow2Alignment = 0);
79
80 virtual void EmitBytes(const StringRef &Data);
81
82 virtual void EmitValue(const MCValue &Value, unsigned Size);
83
84 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
85 unsigned ValueSize = 1,
86 unsigned MaxBytesToEmit = 0);
87
88 virtual void EmitValueToOffset(const MCValue &Offset,
89 unsigned char Value = 0);
90
91 virtual void EmitInstruction(const MCInst &Inst);
92
93 virtual void Finish();
94
95 /// @}
96};
97
98} // end anonymous namespace.
99
100void MCMachOStreamer::SwitchSection(const MCSection *Section) {
101 assert(Section && "Cannot switch to a null section!");
102
103 if (Section != CurSection) {
104 CurSection = Section;
105 MCSectionData *&Entry = SectionMap[Section];
106
107 if (!Entry)
108 Entry = new MCSectionData(*Section, &Assembler);
109
110 CurSectionData = Entry;
111 }
112}
113
114void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000115 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000116
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000117 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
118 if (!F)
119 F = new MCDataFragment(CurSectionData);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000120
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000121 MCSymbolData &SD = getSymbolData(*Symbol);
122 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
123 SD.setFragment(F);
124 SD.setOffset(F->getContents().size());
125
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000126 Symbol->setSection(*CurSection);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000127}
128
129void MCMachOStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
130 llvm_unreachable("FIXME: Not yet implemented!");
131}
132
133void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol,
134 const MCValue &Value,
135 bool MakeAbsolute) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000136 // Only absolute symbols can be redefined.
137 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
138 "Cannot define a symbol twice!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000139
140 llvm_unreachable("FIXME: Not yet implemented!");
141}
142
143void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
144 SymbolAttr Attribute) {
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000145 switch (Attribute) {
146 default:
147 llvm_unreachable("FIXME: Not yet implemented!");
148
149 case MCStreamer::Global:
150 getSymbolData(*Symbol).setExternal(true);
151 break;
152 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000153}
154
155void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
156 llvm_unreachable("FIXME: Not yet implemented!");
157}
158
159void MCMachOStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
160 llvm_unreachable("FIXME: Not yet implemented!");
161}
162
163void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
164 unsigned Pow2Alignment,
165 bool IsLocal) {
166 llvm_unreachable("FIXME: Not yet implemented!");
167}
168
169void MCMachOStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
170 unsigned Size, unsigned Pow2Alignment) {
171 llvm_unreachable("FIXME: Not yet implemented!");
172}
173
174void MCMachOStreamer::EmitBytes(const StringRef &Data) {
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000175 MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
176 if (!DF)
177 DF = new MCDataFragment(CurSectionData);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000178 DF->getContents().append(Data.begin(), Data.end());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000179}
180
181void MCMachOStreamer::EmitValue(const MCValue &Value, unsigned Size) {
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000182 new MCFillFragment(Value, Size, 1, CurSectionData);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000183}
184
185void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
186 int64_t Value, unsigned ValueSize,
187 unsigned MaxBytesToEmit) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000188 if (MaxBytesToEmit == 0)
189 MaxBytesToEmit = ByteAlignment;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000190 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
191 CurSectionData);
192
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000193 // Update the maximum alignment on the current section if necessary.
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000194 if (ByteAlignment > CurSectionData->getAlignment())
195 CurSectionData->setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000196}
197
198void MCMachOStreamer::EmitValueToOffset(const MCValue &Offset,
199 unsigned char Value) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000200 new MCOrgFragment(Offset, Value, CurSectionData);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000201}
202
203void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
204 llvm_unreachable("FIXME: Not yet implemented!");
205}
206
207void MCMachOStreamer::Finish() {
208 Assembler.Finish();
209}
210
211MCStreamer *llvm::createMachOStreamer(MCContext &Context, raw_ostream &OS) {
212 return new MCMachOStreamer(Context, OS);
213}