llvm-mc/Mach-O: Support .o emission for .org and .align.
[oota-llvm.git] / lib / MC / MCMachOStreamer.cpp
1 //===- 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"
19 using namespace llvm;
20
21 namespace {
22
23 class MCMachOStreamer : public MCStreamer {
24   MCAssembler Assembler;
25
26   MCSectionData *CurSectionData;
27
28   DenseMap<const MCSection*, MCSectionData*> SectionMap;
29
30 public:
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
79 void 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
93 void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
94   assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
95   assert(CurSection && "Cannot emit before setting section!");
96   assert(!getContext().GetSymbolValue(Symbol) &&
97          "Cannot emit symbol which was directly assigned to!");
98
99   llvm_unreachable("FIXME: Not yet implemented!");
100
101   Symbol->setSection(CurSection);
102   Symbol->setExternal(false);
103 }
104
105 void MCMachOStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
106   llvm_unreachable("FIXME: Not yet implemented!");
107 }
108
109 void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol,
110                                      const MCValue &Value,
111                                      bool MakeAbsolute) {
112   assert(!Symbol->getSection() && "Cannot assign to a label!");
113
114   llvm_unreachable("FIXME: Not yet implemented!");
115 }
116
117 void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
118                                           SymbolAttr Attribute) {
119   llvm_unreachable("FIXME: Not yet implemented!");
120 }
121
122 void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
123   llvm_unreachable("FIXME: Not yet implemented!");
124 }
125
126 void MCMachOStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
127   llvm_unreachable("FIXME: Not yet implemented!");
128 }
129
130 void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
131                                        unsigned Pow2Alignment,
132                                        bool IsLocal) {
133   llvm_unreachable("FIXME: Not yet implemented!");
134 }
135
136 void MCMachOStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
137                                    unsigned Size, unsigned Pow2Alignment) {
138   llvm_unreachable("FIXME: Not yet implemented!");
139 }
140
141 void MCMachOStreamer::EmitBytes(const StringRef &Data) {
142   MCDataFragment *DF = new MCDataFragment(CurSectionData);
143   DF->getContents().append(Data.begin(), Data.end());
144 }
145
146 void MCMachOStreamer::EmitValue(const MCValue &Value, unsigned Size) {
147   new MCFillFragment(Value, Size, 1, CurSectionData);
148 }
149
150 void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
151                                            int64_t Value, unsigned ValueSize,
152                                            unsigned MaxBytesToEmit) {
153   if (MaxBytesToEmit == 0)
154     MaxBytesToEmit = ByteAlignment;
155   new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
156                       CurSectionData);
157
158   // Update the maximum alignment on the current section if necessary
159   if (ByteAlignment > CurSectionData->getAlignment())
160     CurSectionData->setAlignment(ByteAlignment);
161 }
162
163 void MCMachOStreamer::EmitValueToOffset(const MCValue &Offset,
164                                         unsigned char Value) {
165   new MCOrgFragment(Offset, Value, CurSectionData);
166 }
167
168 void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
169   llvm_unreachable("FIXME: Not yet implemented!");
170 }
171
172 void MCMachOStreamer::Finish() {
173   Assembler.Finish();
174 }
175
176 MCStreamer *llvm::createMachOStreamer(MCContext &Context, raw_ostream &OS) {
177   return new MCMachOStreamer(Context, OS);
178 }