LLVM API Documentation
00001 //===- lib/MC/MCMachOStreamer.cpp - Mach-O Object Output ------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #include "llvm/MC/MCStreamer.h" 00011 00012 #include "llvm/ADT/DenseMap.h" 00013 #include "llvm/MC/MCAssembler.h" 00014 #include "llvm/MC/MCContext.h" 00015 #include "llvm/MC/MCSection.h" 00016 #include "llvm/MC/MCSymbol.h" 00017 #include "llvm/MC/MCValue.h" 00018 #include "llvm/Support/ErrorHandling.h" 00019 using namespace llvm; 00020 00021 namespace { 00022 00023 class MCMachOStreamer : public MCStreamer { 00024 MCAssembler Assembler; 00025 00026 MCSectionData *CurSectionData; 00027 00028 DenseMap<const MCSection*, MCSectionData*> SectionMap; 00029 00030 public: 00031 MCMachOStreamer(MCContext &Context, raw_ostream &_OS) 00032 : MCStreamer(Context), Assembler(_OS), CurSectionData(0) {} 00033 ~MCMachOStreamer() {} 00034 00035 /// @name MCStreamer Interface 00036 /// @{ 00037 00038 virtual void SwitchSection(const MCSection *Section); 00039 00040 virtual void EmitLabel(MCSymbol *Symbol); 00041 00042 virtual void EmitAssemblerFlag(AssemblerFlag Flag); 00043 00044 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value, 00045 bool MakeAbsolute = false); 00046 00047 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute); 00048 00049 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue); 00050 00051 virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value); 00052 00053 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size, 00054 unsigned Pow2Alignment, bool IsLocal); 00055 00056 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL, 00057 unsigned Size = 0, unsigned Pow2Alignment = 0); 00058 00059 virtual void EmitBytes(const StringRef &Data); 00060 00061 virtual void EmitValue(const MCValue &Value, unsigned Size); 00062 00063 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, 00064 unsigned ValueSize = 1, 00065 unsigned MaxBytesToEmit = 0); 00066 00067 virtual void EmitValueToOffset(const MCValue &Offset, 00068 unsigned char Value = 0); 00069 00070 virtual void EmitInstruction(const MCInst &Inst); 00071 00072 virtual void Finish(); 00073 00074 /// @} 00075 }; 00076 00077 } // end anonymous namespace. 00078 00079 void MCMachOStreamer::SwitchSection(const MCSection *Section) { 00080 assert(Section && "Cannot switch to a null section!"); 00081 00082 if (Section != CurSection) { 00083 CurSection = Section; 00084 MCSectionData *&Entry = SectionMap[Section]; 00085 00086 if (!Entry) 00087 Entry = new MCSectionData(*Section, &Assembler); 00088 00089 CurSectionData = Entry; 00090 } 00091 } 00092 00093 void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) { 00094 assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!"); 00095 assert(CurSection && "Cannot emit before setting section!"); 00096 assert(!getContext().GetSymbolValue(Symbol) && 00097 "Cannot emit symbol which was directly assigned to!"); 00098 00099 llvm_unreachable("FIXME: Not yet implemented!"); 00100 00101 Symbol->setSection(CurSection); 00102 Symbol->setExternal(false); 00103 } 00104 00105 void MCMachOStreamer::EmitAssemblerFlag(AssemblerFlag Flag) { 00106 llvm_unreachable("FIXME: Not yet implemented!"); 00107 } 00108 00109 void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, 00110 const MCValue &Value, 00111 bool MakeAbsolute) { 00112 assert(!Symbol->getSection() && "Cannot assign to a label!"); 00113 00114 llvm_unreachable("FIXME: Not yet implemented!"); 00115 } 00116 00117 void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol, 00118 SymbolAttr Attribute) { 00119 llvm_unreachable("FIXME: Not yet implemented!"); 00120 } 00121 00122 void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { 00123 llvm_unreachable("FIXME: Not yet implemented!"); 00124 } 00125 00126 void MCMachOStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) { 00127 llvm_unreachable("FIXME: Not yet implemented!"); 00128 } 00129 00130 void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size, 00131 unsigned Pow2Alignment, 00132 bool IsLocal) { 00133 llvm_unreachable("FIXME: Not yet implemented!"); 00134 } 00135 00136 void MCMachOStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol, 00137 unsigned Size, unsigned Pow2Alignment) { 00138 llvm_unreachable("FIXME: Not yet implemented!"); 00139 } 00140 00141 void MCMachOStreamer::EmitBytes(const StringRef &Data) { 00142 MCDataFragment *DF = new MCDataFragment(CurSectionData); 00143 DF->getContents().append(Data.begin(), Data.end()); 00144 } 00145 00146 void MCMachOStreamer::EmitValue(const MCValue &Value, unsigned Size) { 00147 new MCFillFragment(Value, Size, 1, CurSectionData); 00148 } 00149 00150 void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment, 00151 int64_t Value, unsigned ValueSize, 00152 unsigned MaxBytesToEmit) { 00153 if (MaxBytesToEmit == 0) 00154 MaxBytesToEmit = ByteAlignment; 00155 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit, 00156 CurSectionData); 00157 00158 // Update the maximum alignment on the current section if necessary 00159 if (ByteAlignment > CurSectionData->getAlignment()) 00160 CurSectionData->setAlignment(ByteAlignment); 00161 } 00162 00163 void MCMachOStreamer::EmitValueToOffset(const MCValue &Offset, 00164 unsigned char Value) { 00165 new MCOrgFragment(Offset, Value, CurSectionData); 00166 } 00167 00168 void MCMachOStreamer::EmitInstruction(const MCInst &Inst) { 00169 llvm_unreachable("FIXME: Not yet implemented!"); 00170 } 00171 00172 void MCMachOStreamer::Finish() { 00173 Assembler.Finish(); 00174 } 00175 00176 MCStreamer *llvm::createMachOStreamer(MCContext &Context, raw_ostream &OS) { 00177 return new MCMachOStreamer(Context, OS); 00178 }