LLVM API Documentation
00001 //===- MCAssembler.h - Object File Generation -------------------*- C++ -*-===// 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 #ifndef LLVM_MC_MCASSEMBLER_H 00011 #define LLVM_MC_MCASSEMBLER_H 00012 00013 #include "llvm/ADT/SmallString.h" 00014 #include "llvm/ADT/ilist.h" 00015 #include "llvm/ADT/ilist_node.h" 00016 #include "llvm/MC/MCValue.h" 00017 #include "llvm/Support/Casting.h" 00018 #include "llvm/Support/DataTypes.h" 00019 00020 namespace llvm { 00021 class raw_ostream; 00022 class MCAssembler; 00023 class MCSection; 00024 class MCSectionData; 00025 00026 class MCFragment : public ilist_node<MCFragment> { 00027 MCFragment(const MCFragment&); // DO NOT IMPLEMENT 00028 void operator=(const MCFragment&); // DO NOT IMPLEMENT 00029 00030 public: 00031 enum FragmentType { 00032 FT_Data, 00033 FT_Align, 00034 FT_Fill, 00035 FT_Org 00036 }; 00037 00038 private: 00039 FragmentType Kind; 00040 00041 /// @name Assembler Backend Data 00042 /// @{ 00043 // 00044 // FIXME: This could all be kept private to the assembler implementation. 00045 00046 /// FileOffset - The offset of this section in the object file. This is ~0 00047 /// until initialized. 00048 uint64_t FileOffset; 00049 00050 /// FileSize - The size of this section in the object file. This is ~0 until 00051 /// initialized. 00052 uint64_t FileSize; 00053 00054 /// @} 00055 00056 protected: 00057 MCFragment(FragmentType _Kind, MCSectionData *SD = 0); 00058 00059 public: 00060 // Only for sentinel. 00061 MCFragment(); 00062 virtual ~MCFragment(); 00063 00064 FragmentType getKind() const { return Kind; } 00065 00066 // FIXME: This should be abstract, fix sentinel. 00067 virtual uint64_t getMaxFileSize() const { 00068 assert(0 && "Invalid getMaxFileSize call !"); 00069 return 0; 00070 }; 00071 00072 /// @name Assembler Backend Support 00073 /// @{ 00074 // 00075 // FIXME: This could all be kept private to the assembler implementation. 00076 00077 unsigned getFileSize() const { 00078 assert(FileSize != ~UINT64_C(0) && "File size not set!"); 00079 return FileSize; 00080 } 00081 void setFileSize(uint64_t Value) { 00082 assert(Value <= getMaxFileSize() && "Invalid file size!"); 00083 FileSize = Value; 00084 } 00085 00086 uint64_t getFileOffset() const { 00087 assert(FileOffset != ~UINT64_C(0) && "File offset not set!"); 00088 return FileOffset; 00089 } 00090 void setFileOffset(uint64_t Value) { FileOffset = Value; } 00091 00092 /// @} 00093 00094 static bool classof(const MCFragment *O) { return true; } 00095 }; 00096 00097 class MCDataFragment : public MCFragment { 00098 SmallString<32> Contents; 00099 00100 public: 00101 MCDataFragment(MCSectionData *SD = 0) : MCFragment(FT_Data, SD) {} 00102 00103 /// @name Accessors 00104 /// @{ 00105 00106 uint64_t getMaxFileSize() const { 00107 return Contents.size(); 00108 } 00109 00110 SmallString<32> &getContents() { return Contents; } 00111 const SmallString<32> &getContents() const { return Contents; } 00112 00113 /// @} 00114 00115 static bool classof(const MCFragment *F) { 00116 return F->getKind() == MCFragment::FT_Data; 00117 } 00118 static bool classof(const MCDataFragment *) { return true; } 00119 }; 00120 00121 class MCAlignFragment : public MCFragment { 00122 /// Alignment - The alignment to ensure, in bytes. 00123 unsigned Alignment; 00124 00125 /// Value - Value to use for filling padding bytes. 00126 int64_t Value; 00127 00128 /// ValueSize - The size of the integer (in bytes) of \arg Value. 00129 unsigned ValueSize; 00130 00131 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment 00132 /// cannot be satisfied in this width then this fragment is ignored. 00133 unsigned MaxBytesToEmit; 00134 00135 public: 00136 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize, 00137 unsigned _MaxBytesToEmit, MCSectionData *SD = 0) 00138 : MCFragment(FT_Align, SD), Alignment(_Alignment), 00139 Value(_Value),ValueSize(_ValueSize), 00140 MaxBytesToEmit(_MaxBytesToEmit) {} 00141 00142 /// @name Accessors 00143 /// @{ 00144 00145 uint64_t getMaxFileSize() const { 00146 return std::max(Alignment - 1, MaxBytesToEmit); 00147 } 00148 00149 unsigned getAlignment() const { return Alignment; } 00150 00151 int64_t getValue() const { return Value; } 00152 00153 unsigned getValueSize() const { return ValueSize; } 00154 00155 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; } 00156 00157 /// @} 00158 00159 static bool classof(const MCFragment *F) { 00160 return F->getKind() == MCFragment::FT_Align; 00161 } 00162 static bool classof(const MCAlignFragment *) { return true; } 00163 }; 00164 00165 class MCFillFragment : public MCFragment { 00166 /// Value - Value to use for filling bytes. 00167 MCValue Value; 00168 00169 /// ValueSize - The size (in bytes) of \arg Value to use when filling. 00170 unsigned ValueSize; 00171 00172 /// Count - The number of copies of \arg Value to insert. 00173 uint64_t Count; 00174 00175 public: 00176 MCFillFragment(MCValue _Value, unsigned _ValueSize, uint64_t _Count, 00177 MCSectionData *SD = 0) 00178 : MCFragment(FT_Fill, SD), 00179 Value(_Value), ValueSize(_ValueSize), Count(_Count) {} 00180 00181 /// @name Accessors 00182 /// @{ 00183 00184 uint64_t getMaxFileSize() const { 00185 return ValueSize * Count; 00186 } 00187 00188 MCValue getValue() const { return Value; } 00189 00190 unsigned getValueSize() const { return ValueSize; } 00191 00192 uint64_t getCount() const { return Count; } 00193 00194 /// @} 00195 00196 static bool classof(const MCFragment *F) { 00197 return F->getKind() == MCFragment::FT_Fill; 00198 } 00199 static bool classof(const MCFillFragment *) { return true; } 00200 }; 00201 00202 class MCOrgFragment : public MCFragment { 00203 /// Offset - The offset this fragment should start at. 00204 MCValue Offset; 00205 00206 /// Value - Value to use for filling bytes. 00207 int8_t Value; 00208 00209 public: 00210 MCOrgFragment(MCValue _Offset, int8_t _Value, MCSectionData *SD = 0) 00211 : MCFragment(FT_Org, SD), 00212 Offset(_Offset), Value(_Value) {} 00213 /// @name Accessors 00214 /// @{ 00215 00216 uint64_t getMaxFileSize() const { 00217 // FIXME: This doesn't make much sense. 00218 return ~UINT64_C(0); 00219 } 00220 00221 MCValue getOffset() const { return Offset; } 00222 00223 uint8_t getValue() const { return Value; } 00224 00225 /// @} 00226 00227 static bool classof(const MCFragment *F) { 00228 return F->getKind() == MCFragment::FT_Org; 00229 } 00230 static bool classof(const MCOrgFragment *) { return true; } 00231 }; 00232 00233 // FIXME: Should this be a separate class, or just merged into MCSection? Since 00234 // we anticipate the fast path being through an MCAssembler, the only reason to 00235 // keep it out is for API abstraction. 00236 class MCSectionData : public ilist_node<MCSectionData> { 00237 MCSectionData(const MCSectionData&); // DO NOT IMPLEMENT 00238 void operator=(const MCSectionData&); // DO NOT IMPLEMENT 00239 00240 public: 00241 typedef iplist<MCFragment> FragmentListType; 00242 00243 typedef FragmentListType::const_iterator const_iterator; 00244 typedef FragmentListType::iterator iterator; 00245 00246 private: 00247 iplist<MCFragment> Fragments; 00248 const MCSection &Section; 00249 00250 /// Alignment - The maximum alignment seen in this section. 00251 unsigned Alignment; 00252 00253 /// @name Assembler Backend Data 00254 /// @{ 00255 // 00256 // FIXME: This could all be kept private to the assembler implementation. 00257 00258 /// FileOffset - The offset of this section in the object file. This is ~0 00259 /// until initialized. 00260 uint64_t FileOffset; 00261 00262 /// FileSize - The size of this section in the object file. This is ~0 until 00263 /// initialized. 00264 uint64_t FileSize; 00265 00266 /// @} 00267 00268 public: 00269 // Only for use as sentinel. 00270 MCSectionData(); 00271 MCSectionData(const MCSection &Section, MCAssembler *A = 0); 00272 00273 const MCSection &getSection() const { return Section; } 00274 00275 unsigned getAlignment() const { return Alignment; } 00276 void setAlignment(unsigned Value) { Alignment = Value; } 00277 00278 00279 /// @name Section List Access 00280 /// @{ 00281 00282 const FragmentListType &getFragmentList() const { return Fragments; } 00283 FragmentListType &getFragmentList() { return Fragments; } 00284 00285 iterator begin() { return Fragments.begin(); } 00286 const_iterator begin() const { return Fragments.begin(); } 00287 00288 iterator end() { return Fragments.end(); } 00289 const_iterator end() const { return Fragments.end(); } 00290 00291 size_t size() const { return Fragments.size(); } 00292 00293 /// @} 00294 /// @name Assembler Backend Support 00295 /// @{ 00296 // 00297 // FIXME: This could all be kept private to the assembler implementation. 00298 00299 unsigned getFileSize() const { 00300 assert(FileSize != ~UINT64_C(0) && "File size not set!"); 00301 return FileSize; 00302 } 00303 void setFileSize(uint64_t Value) { FileSize = Value; } 00304 00305 uint64_t getFileOffset() const { 00306 assert(FileOffset != ~UINT64_C(0) && "File offset not set!"); 00307 return FileOffset; 00308 } 00309 void setFileOffset(uint64_t Value) { FileOffset = Value; } 00310 00311 /// @} 00312 }; 00313 00314 class MCAssembler { 00315 public: 00316 typedef iplist<MCSectionData> SectionDataListType; 00317 00318 typedef SectionDataListType::const_iterator const_iterator; 00319 typedef SectionDataListType::iterator iterator; 00320 00321 private: 00322 MCAssembler(const MCAssembler&); // DO NOT IMPLEMENT 00323 void operator=(const MCAssembler&); // DO NOT IMPLEMENT 00324 00325 raw_ostream &OS; 00326 00327 iplist<MCSectionData> Sections; 00328 00329 private: 00330 /// LayoutSection - Assign offsets and sizes to the fragments in the section 00331 /// \arg SD, and update the section size. The section file offset should 00332 /// already have been computed. 00333 void LayoutSection(MCSectionData &SD); 00334 00335 public: 00336 /// Construct a new assembler instance. 00337 /// 00338 /// \arg OS - The stream to output to. 00339 // 00340 // FIXME: How are we going to parameterize this? Two obvious options are stay 00341 // concrete and require clients to pass in a target like object. The other 00342 // option is to make this abstract, and have targets provide concrete 00343 // implementations as we do with AsmParser. 00344 MCAssembler(raw_ostream &OS); 00345 ~MCAssembler(); 00346 00347 /// Finish - Do final processing and write the object to the output stream. 00348 void Finish(); 00349 00350 /// @name Section List Access 00351 /// @{ 00352 00353 const SectionDataListType &getSectionList() const { return Sections; } 00354 SectionDataListType &getSectionList() { return Sections; } 00355 00356 iterator begin() { return Sections.begin(); } 00357 const_iterator begin() const { return Sections.begin(); } 00358 00359 iterator end() { return Sections.end(); } 00360 const_iterator end() const { return Sections.end(); } 00361 00362 size_t size() const { return Sections.size(); } 00363 00364 /// @} 00365 }; 00366 00367 } // end namespace llvm 00368 00369 #endif