MCAssembler.h revision f0d17d2461a6458568672f611ba4a590d26b1e1a
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===- MCAssembler.h - Object File Generation -------------------*- C++ -*-===// 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The LLVM Compiler Infrastructure 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file is distributed under the University of Illinois Open Source 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// License. See LICENSE.TXT for details. 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===// 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef LLVM_MC_MCASSEMBLER_H 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LLVM_MC_MCASSEMBLER_H 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/ADT/DenseMap.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/ADT/SmallString.h" 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/ADT/ilist.h" 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/ADT/ilist_node.h" 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/Casting.h" 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/MC/MCFixup.h" 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/MC/MCInst.h" 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/System/DataTypes.h" 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector> // FIXME: Shouldn't be needed. 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace llvm { 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class raw_ostream; 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MCAsmLayout; 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MCAssembler; 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MCContext; 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MCCodeEmitter; 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MCExpr; 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MCFragment; 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MCObjectWriter; 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MCSection; 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MCSectionData; 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MCSymbol; 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MCSymbolData; 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MCValue; 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class TargetAsmBackend; 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// MCAsmFixup - Represent a fixed size region of bytes inside some fragment 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// which needs to be rewritten. This region will either be rewritten by the 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// assembler or cause a relocation entry to be generated. 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// FIXME: This should probably just be merged with MCFixup. 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MCAsmFixup { 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public: 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// Offset - The offset inside the fragment which needs to be rewritten. 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) uint64_t Offset; 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// Value - The expression to eventually write into the fragment. 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const MCExpr *Value; 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// Kind - The fixup kind. 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) MCFixupKind Kind; 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public: 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) MCAsmFixup(uint64_t _Offset, const MCExpr &_Value, MCFixupKind _Kind) 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) : Offset(_Offset), Value(&_Value), Kind(_Kind) {} 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MCFragment : public ilist_node<MCFragment> { 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) friend class MCAsmLayout; 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) MCFragment(const MCFragment&); // DO NOT IMPLEMENT 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void operator=(const MCFragment&); // DO NOT IMPLEMENT 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public: 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) enum FragmentType { 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FT_Align, 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FT_Data, 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FT_Fill, 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FT_Inst, 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FT_Org, 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FT_ZeroFill 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) }; 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)private: 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FragmentType Kind; 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// Parent - The data for the section this fragment is in. 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) MCSectionData *Parent; 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// Atom - The atom this fragment is in, as represented by it's defining 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// symbol. Atom's are only used by backends which set 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// \see MCAsmBackend::hasReliableSymbolDifference(). 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) MCSymbolData *Atom; 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// @name Assembler Backend Data 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// @{ 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // FIXME: This could all be kept private to the assembler implementation. 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// Offset - The offset of this fragment in its section. This is ~0 until 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// initialized. 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) uint64_t Offset; 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// EffectiveSize - The compute size of this section. This is ~0 until 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// initialized. 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) uint64_t EffectiveSize; 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// Ordinal - The global index of this fragment. This is the index across all 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// sections, not just the parent section. 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned Ordinal; 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// @} 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)protected: 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0); 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public: 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Only for sentinel. 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) MCFragment(); 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual ~MCFragment(); 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FragmentType getKind() const { return Kind; } 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) MCSectionData *getParent() const { return Parent; } 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void setParent(MCSectionData *Value) { Parent = Value; } 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) MCSymbolData *getAtom() const { return Atom; } 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void setAtom(MCSymbolData *Value) { Atom = Value; } 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned getOrdinal() const { return Ordinal; } 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void setOrdinal(unsigned Value) { Ordinal = Value; } 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static bool classof(const MCFragment *O) { return true; } 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void dump(); 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 129 130class MCDataFragment : public MCFragment { 131 SmallString<32> Contents; 132 133 /// Fixups - The list of fixups in this fragment. 134 std::vector<MCAsmFixup> Fixups; 135 136public: 137 typedef std::vector<MCAsmFixup>::const_iterator const_fixup_iterator; 138 typedef std::vector<MCAsmFixup>::iterator fixup_iterator; 139 140public: 141 MCDataFragment(MCSectionData *SD = 0) : MCFragment(FT_Data, SD) {} 142 143 /// @name Accessors 144 /// @{ 145 146 SmallString<32> &getContents() { return Contents; } 147 const SmallString<32> &getContents() const { return Contents; } 148 149 /// @} 150 /// @name Fixup Access 151 /// @{ 152 153 void addFixup(MCAsmFixup Fixup) { 154 // Enforce invariant that fixups are in offset order. 155 assert((Fixups.empty() || Fixup.Offset > Fixups.back().Offset) && 156 "Fixups must be added in order!"); 157 Fixups.push_back(Fixup); 158 } 159 160 std::vector<MCAsmFixup> &getFixups() { return Fixups; } 161 const std::vector<MCAsmFixup> &getFixups() const { return Fixups; } 162 163 fixup_iterator fixup_begin() { return Fixups.begin(); } 164 const_fixup_iterator fixup_begin() const { return Fixups.begin(); } 165 166 fixup_iterator fixup_end() {return Fixups.end();} 167 const_fixup_iterator fixup_end() const {return Fixups.end();} 168 169 size_t fixup_size() const { return Fixups.size(); } 170 171 /// @} 172 173 static bool classof(const MCFragment *F) { 174 return F->getKind() == MCFragment::FT_Data; 175 } 176 static bool classof(const MCDataFragment *) { return true; } 177 178 virtual void dump(); 179}; 180 181// FIXME: This current incarnation of MCInstFragment doesn't make much sense, as 182// it is almost entirely a duplicate of MCDataFragment. If we decide to stick 183// with this approach (as opposed to making MCInstFragment a very light weight 184// object with just the MCInst and a code size, then we should just change 185// MCDataFragment to have an optional MCInst at its end. 186class MCInstFragment : public MCFragment { 187 /// Inst - The instruction this is a fragment for. 188 MCInst Inst; 189 190 /// InstSize - The size of the currently encoded instruction. 191 SmallString<8> Code; 192 193 /// Fixups - The list of fixups in this fragment. 194 SmallVector<MCAsmFixup, 1> Fixups; 195 196public: 197 typedef SmallVectorImpl<MCAsmFixup>::const_iterator const_fixup_iterator; 198 typedef SmallVectorImpl<MCAsmFixup>::iterator fixup_iterator; 199 200public: 201 MCInstFragment(MCInst _Inst, MCSectionData *SD = 0) 202 : MCFragment(FT_Inst, SD), Inst(_Inst) { 203 } 204 205 /// @name Accessors 206 /// @{ 207 208 SmallVectorImpl<char> &getCode() { return Code; } 209 const SmallVectorImpl<char> &getCode() const { return Code; } 210 211 unsigned getInstSize() const { return Code.size(); } 212 213 MCInst &getInst() { return Inst; } 214 const MCInst &getInst() const { return Inst; } 215 216 void setInst(MCInst Value) { Inst = Value; } 217 218 /// @} 219 /// @name Fixup Access 220 /// @{ 221 222 SmallVectorImpl<MCAsmFixup> &getFixups() { return Fixups; } 223 const SmallVectorImpl<MCAsmFixup> &getFixups() const { return Fixups; } 224 225 fixup_iterator fixup_begin() { return Fixups.begin(); } 226 const_fixup_iterator fixup_begin() const { return Fixups.begin(); } 227 228 fixup_iterator fixup_end() {return Fixups.end();} 229 const_fixup_iterator fixup_end() const {return Fixups.end();} 230 231 size_t fixup_size() const { return Fixups.size(); } 232 233 /// @} 234 235 static bool classof(const MCFragment *F) { 236 return F->getKind() == MCFragment::FT_Inst; 237 } 238 static bool classof(const MCInstFragment *) { return true; } 239 240 virtual void dump(); 241}; 242 243class MCAlignFragment : public MCFragment { 244 /// Alignment - The alignment to ensure, in bytes. 245 unsigned Alignment; 246 247 /// Value - Value to use for filling padding bytes. 248 int64_t Value; 249 250 /// ValueSize - The size of the integer (in bytes) of \arg Value. 251 unsigned ValueSize; 252 253 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment 254 /// cannot be satisfied in this width then this fragment is ignored. 255 unsigned MaxBytesToEmit; 256 257 /// EmitNops - true when aligning code and optimal nops to be used for 258 /// filling. 259 bool EmitNops; 260 261public: 262 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize, 263 unsigned _MaxBytesToEmit, bool _EmitNops, 264 MCSectionData *SD = 0) 265 : MCFragment(FT_Align, SD), Alignment(_Alignment), 266 Value(_Value),ValueSize(_ValueSize), 267 MaxBytesToEmit(_MaxBytesToEmit), EmitNops(_EmitNops) {} 268 269 /// @name Accessors 270 /// @{ 271 272 unsigned getAlignment() const { return Alignment; } 273 274 int64_t getValue() const { return Value; } 275 276 unsigned getValueSize() const { return ValueSize; } 277 278 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; } 279 280 unsigned getEmitNops() const { return EmitNops; } 281 282 /// @} 283 284 static bool classof(const MCFragment *F) { 285 return F->getKind() == MCFragment::FT_Align; 286 } 287 static bool classof(const MCAlignFragment *) { return true; } 288 289 virtual void dump(); 290}; 291 292class MCFillFragment : public MCFragment { 293 /// Value - Value to use for filling bytes. 294 int64_t Value; 295 296 /// ValueSize - The size (in bytes) of \arg Value to use when filling. 297 unsigned ValueSize; 298 299 /// Count - The number of copies of \arg Value to insert. 300 uint64_t Count; 301 302public: 303 MCFillFragment(int64_t _Value, unsigned _ValueSize, uint64_t _Count, 304 MCSectionData *SD = 0) 305 : MCFragment(FT_Fill, SD), 306 Value(_Value), ValueSize(_ValueSize), Count(_Count) {} 307 308 /// @name Accessors 309 /// @{ 310 311 int64_t getValue() const { return Value; } 312 313 unsigned getValueSize() const { return ValueSize; } 314 315 uint64_t getCount() const { return Count; } 316 317 /// @} 318 319 static bool classof(const MCFragment *F) { 320 return F->getKind() == MCFragment::FT_Fill; 321 } 322 static bool classof(const MCFillFragment *) { return true; } 323 324 virtual void dump(); 325}; 326 327class MCOrgFragment : public MCFragment { 328 /// Offset - The offset this fragment should start at. 329 const MCExpr *Offset; 330 331 /// Value - Value to use for filling bytes. 332 int8_t Value; 333 334public: 335 MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0) 336 : MCFragment(FT_Org, SD), 337 Offset(&_Offset), Value(_Value) {} 338 339 /// @name Accessors 340 /// @{ 341 342 const MCExpr &getOffset() const { return *Offset; } 343 344 uint8_t getValue() const { return Value; } 345 346 /// @} 347 348 static bool classof(const MCFragment *F) { 349 return F->getKind() == MCFragment::FT_Org; 350 } 351 static bool classof(const MCOrgFragment *) { return true; } 352 353 virtual void dump(); 354}; 355 356/// MCZeroFillFragment - Represent data which has a fixed size and alignment, 357/// but requires no physical space in the object file. 358class MCZeroFillFragment : public MCFragment { 359 /// Size - The size of this fragment. 360 uint64_t Size; 361 362 /// Alignment - The alignment for this fragment. 363 unsigned Alignment; 364 365public: 366 MCZeroFillFragment(uint64_t _Size, unsigned _Alignment, MCSectionData *SD = 0) 367 : MCFragment(FT_ZeroFill, SD), 368 Size(_Size), Alignment(_Alignment) {} 369 370 /// @name Accessors 371 /// @{ 372 373 uint64_t getSize() const { return Size; } 374 375 unsigned getAlignment() const { return Alignment; } 376 377 /// @} 378 379 static bool classof(const MCFragment *F) { 380 return F->getKind() == MCFragment::FT_ZeroFill; 381 } 382 static bool classof(const MCZeroFillFragment *) { return true; } 383 384 virtual void dump(); 385}; 386 387// FIXME: Should this be a separate class, or just merged into MCSection? Since 388// we anticipate the fast path being through an MCAssembler, the only reason to 389// keep it out is for API abstraction. 390class MCSectionData : public ilist_node<MCSectionData> { 391 friend class MCAsmLayout; 392 393 MCSectionData(const MCSectionData&); // DO NOT IMPLEMENT 394 void operator=(const MCSectionData&); // DO NOT IMPLEMENT 395 396public: 397 typedef iplist<MCFragment> FragmentListType; 398 399 typedef FragmentListType::const_iterator const_iterator; 400 typedef FragmentListType::iterator iterator; 401 402 typedef FragmentListType::const_reverse_iterator const_reverse_iterator; 403 typedef FragmentListType::reverse_iterator reverse_iterator; 404 405private: 406 iplist<MCFragment> Fragments; 407 const MCSection *Section; 408 409 /// Ordinal - The section index in the assemblers section list. 410 unsigned Ordinal; 411 412 /// Alignment - The maximum alignment seen in this section. 413 unsigned Alignment; 414 415 /// @name Assembler Backend Data 416 /// @{ 417 // 418 // FIXME: This could all be kept private to the assembler implementation. 419 420 /// Address - The computed address of this section. This is ~0 until 421 /// initialized. 422 uint64_t Address; 423 424 /// Size - The content size of this section. This is ~0 until initialized. 425 uint64_t Size; 426 427 /// FileSize - The size of this section in the object file. This is ~0 until 428 /// initialized. 429 uint64_t FileSize; 430 431 /// HasInstructions - Whether this section has had instructions emitted into 432 /// it. 433 unsigned HasInstructions : 1; 434 435 /// @} 436 437public: 438 // Only for use as sentinel. 439 MCSectionData(); 440 MCSectionData(const MCSection &Section, MCAssembler *A = 0); 441 442 const MCSection &getSection() const { return *Section; } 443 444 unsigned getAlignment() const { return Alignment; } 445 void setAlignment(unsigned Value) { Alignment = Value; } 446 447 bool hasInstructions() const { return HasInstructions; } 448 void setHasInstructions(bool Value) { HasInstructions = Value; } 449 450 unsigned getOrdinal() const { return Ordinal; } 451 void setOrdinal(unsigned Value) { Ordinal = Value; } 452 453 /// @name Fragment Access 454 /// @{ 455 456 const FragmentListType &getFragmentList() const { return Fragments; } 457 FragmentListType &getFragmentList() { return Fragments; } 458 459 iterator begin() { return Fragments.begin(); } 460 const_iterator begin() const { return Fragments.begin(); } 461 462 iterator end() { return Fragments.end(); } 463 const_iterator end() const { return Fragments.end(); } 464 465 reverse_iterator rbegin() { return Fragments.rbegin(); } 466 const_reverse_iterator rbegin() const { return Fragments.rbegin(); } 467 468 reverse_iterator rend() { return Fragments.rend(); } 469 const_reverse_iterator rend() const { return Fragments.rend(); } 470 471 size_t size() const { return Fragments.size(); } 472 473 bool empty() const { return Fragments.empty(); } 474 475 void dump(); 476 477 /// @} 478}; 479 480// FIXME: Same concerns as with SectionData. 481class MCSymbolData : public ilist_node<MCSymbolData> { 482public: 483 const MCSymbol *Symbol; 484 485 /// Fragment - The fragment this symbol's value is relative to, if any. 486 MCFragment *Fragment; 487 488 /// Offset - The offset to apply to the fragment address to form this symbol's 489 /// value. 490 uint64_t Offset; 491 492 /// IsExternal - True if this symbol is visible outside this translation 493 /// unit. 494 unsigned IsExternal : 1; 495 496 /// IsPrivateExtern - True if this symbol is private extern. 497 unsigned IsPrivateExtern : 1; 498 499 /// CommonSize - The size of the symbol, if it is 'common', or 0. 500 // 501 // FIXME: Pack this in with other fields? We could put it in offset, since a 502 // common symbol can never get a definition. 503 uint64_t CommonSize; 504 505 /// CommonAlign - The alignment of the symbol, if it is 'common'. 506 // 507 // FIXME: Pack this in with other fields? 508 unsigned CommonAlign; 509 510 /// Flags - The Flags field is used by object file implementations to store 511 /// additional per symbol information which is not easily classified. 512 uint32_t Flags; 513 514 /// Index - Index field, for use by the object file implementation. 515 uint64_t Index; 516 517public: 518 // Only for use as sentinel. 519 MCSymbolData(); 520 MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset, 521 MCAssembler *A = 0); 522 523 /// @name Accessors 524 /// @{ 525 526 const MCSymbol &getSymbol() const { return *Symbol; } 527 528 MCFragment *getFragment() const { return Fragment; } 529 void setFragment(MCFragment *Value) { Fragment = Value; } 530 531 uint64_t getOffset() const { return Offset; } 532 void setOffset(uint64_t Value) { Offset = Value; } 533 534 /// @} 535 /// @name Symbol Attributes 536 /// @{ 537 538 bool isExternal() const { return IsExternal; } 539 void setExternal(bool Value) { IsExternal = Value; } 540 541 bool isPrivateExtern() const { return IsPrivateExtern; } 542 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; } 543 544 /// isCommon - Is this a 'common' symbol. 545 bool isCommon() const { return CommonSize != 0; } 546 547 /// setCommon - Mark this symbol as being 'common'. 548 /// 549 /// \param Size - The size of the symbol. 550 /// \param Align - The alignment of the symbol. 551 void setCommon(uint64_t Size, unsigned Align) { 552 CommonSize = Size; 553 CommonAlign = Align; 554 } 555 556 /// getCommonSize - Return the size of a 'common' symbol. 557 uint64_t getCommonSize() const { 558 assert(isCommon() && "Not a 'common' symbol!"); 559 return CommonSize; 560 } 561 562 /// getCommonAlignment - Return the alignment of a 'common' symbol. 563 unsigned getCommonAlignment() const { 564 assert(isCommon() && "Not a 'common' symbol!"); 565 return CommonAlign; 566 } 567 568 /// getFlags - Get the (implementation defined) symbol flags. 569 uint32_t getFlags() const { return Flags; } 570 571 /// setFlags - Set the (implementation defined) symbol flags. 572 void setFlags(uint32_t Value) { Flags = Value; } 573 574 /// modifyFlags - Modify the flags via a mask 575 void modifyFlags(uint32_t Value, uint32_t Mask) { 576 Flags = (Flags & ~Mask) | Value; 577 } 578 579 /// getIndex - Get the (implementation defined) index. 580 uint64_t getIndex() const { return Index; } 581 582 /// setIndex - Set the (implementation defined) index. 583 void setIndex(uint64_t Value) { Index = Value; } 584 585 /// @} 586 587 void dump(); 588}; 589 590// FIXME: This really doesn't belong here. See comments below. 591struct IndirectSymbolData { 592 MCSymbol *Symbol; 593 MCSectionData *SectionData; 594}; 595 596class MCAssembler { 597 friend class MCAsmLayout; 598 599public: 600 typedef iplist<MCSectionData> SectionDataListType; 601 typedef iplist<MCSymbolData> SymbolDataListType; 602 603 typedef SectionDataListType::const_iterator const_iterator; 604 typedef SectionDataListType::iterator iterator; 605 606 typedef SymbolDataListType::const_iterator const_symbol_iterator; 607 typedef SymbolDataListType::iterator symbol_iterator; 608 609 typedef std::vector<IndirectSymbolData>::const_iterator 610 const_indirect_symbol_iterator; 611 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator; 612 613private: 614 MCAssembler(const MCAssembler&); // DO NOT IMPLEMENT 615 void operator=(const MCAssembler&); // DO NOT IMPLEMENT 616 617 MCContext &Context; 618 619 TargetAsmBackend &Backend; 620 621 MCCodeEmitter &Emitter; 622 623 raw_ostream &OS; 624 625 iplist<MCSectionData> Sections; 626 627 iplist<MCSymbolData> Symbols; 628 629 /// The map of sections to their associated assembler backend data. 630 // 631 // FIXME: Avoid this indirection? 632 DenseMap<const MCSection*, MCSectionData*> SectionMap; 633 634 /// The map of symbols to their associated assembler backend data. 635 // 636 // FIXME: Avoid this indirection? 637 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap; 638 639 std::vector<IndirectSymbolData> IndirectSymbols; 640 641 unsigned RelaxAll : 1; 642 unsigned SubsectionsViaSymbols : 1; 643 644private: 645 /// Evaluate a fixup to a relocatable expression and the value which should be 646 /// placed into the fixup. 647 /// 648 /// \param Layout The layout to use for evaluation. 649 /// \param Fixup The fixup to evaluate. 650 /// \param DF The fragment the fixup is inside. 651 /// \param Target [out] On return, the relocatable expression the fixup 652 /// evaluates to. 653 /// \param Value [out] On return, the value of the fixup as currently layed 654 /// out. 655 /// \return Whether the fixup value was fully resolved. This is true if the 656 /// \arg Value result is fixed, otherwise the value may change due to 657 /// relocation. 658 bool EvaluateFixup(const MCAsmLayout &Layout, 659 const MCAsmFixup &Fixup, const MCFragment *DF, 660 MCValue &Target, uint64_t &Value) const; 661 662 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed 663 /// (increased in size, in order to hold its value correctly). 664 bool FixupNeedsRelaxation(const MCAsmFixup &Fixup, const MCFragment *DF, 665 const MCAsmLayout &Layout) const; 666 667 /// Check whether the given fragment needs relaxation. 668 bool FragmentNeedsRelaxation(const MCInstFragment *IF, 669 const MCAsmLayout &Layout) const; 670 671 /// LayoutFragment - Performs layout of the given \arg Fragment; assuming that 672 /// the previous fragment has already been layed out correctly, and the parent 673 /// section has been initialized. 674 void LayoutFragment(MCAsmLayout &Layout, MCFragment &Fragment); 675 676 /// LayoutSection - Performs layout of the section referenced by the given 677 /// \arg SectionOrderIndex. The layout assumes that the previous section has 678 /// already been layed out correctly. 679 void LayoutSection(MCAsmLayout &Layout, unsigned SectionOrderIndex); 680 681 /// LayoutOnce - Perform one layout iteration and return true if any offsets 682 /// were adjusted. 683 bool LayoutOnce(MCAsmLayout &Layout); 684 685 /// FinishLayout - Finalize a layout, including fragment lowering. 686 void FinishLayout(MCAsmLayout &Layout); 687 688public: 689 /// Find the symbol which defines the atom containing the given symbol, or 690 /// null if there is no such symbol. 691 const MCSymbolData *getAtom(const MCAsmLayout &Layout, 692 const MCSymbolData *Symbol) const; 693 694 /// Check whether a particular symbol is visible to the linker and is required 695 /// in the symbol table, or whether it can be discarded by the assembler. This 696 /// also effects whether the assembler treats the label as potentially 697 /// defining a separate atom. 698 bool isSymbolLinkerVisible(const MCSymbolData *SD) const; 699 700 /// Emit the section contents using the given object writer. 701 // 702 // FIXME: Should MCAssembler always have a reference to the object writer? 703 void WriteSectionData(const MCSectionData *Section, const MCAsmLayout &Layout, 704 MCObjectWriter *OW) const; 705 706public: 707 /// Construct a new assembler instance. 708 /// 709 /// \arg OS - The stream to output to. 710 // 711 // FIXME: How are we going to parameterize this? Two obvious options are stay 712 // concrete and require clients to pass in a target like object. The other 713 // option is to make this abstract, and have targets provide concrete 714 // implementations as we do with AsmParser. 715 MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend, 716 MCCodeEmitter &_Emitter, raw_ostream &OS); 717 ~MCAssembler(); 718 719 MCContext &getContext() const { return Context; } 720 721 TargetAsmBackend &getBackend() const { return Backend; } 722 723 MCCodeEmitter &getEmitter() const { return Emitter; } 724 725 /// Finish - Do final processing and write the object to the output stream. 726 void Finish(); 727 728 // FIXME: This does not belong here. 729 bool getSubsectionsViaSymbols() const { 730 return SubsectionsViaSymbols; 731 } 732 void setSubsectionsViaSymbols(bool Value) { 733 SubsectionsViaSymbols = Value; 734 } 735 736 bool getRelaxAll() const { return RelaxAll; } 737 void setRelaxAll(bool Value) { RelaxAll = Value; } 738 739 /// @name Section List Access 740 /// @{ 741 742 const SectionDataListType &getSectionList() const { return Sections; } 743 SectionDataListType &getSectionList() { return Sections; } 744 745 iterator begin() { return Sections.begin(); } 746 const_iterator begin() const { return Sections.begin(); } 747 748 iterator end() { return Sections.end(); } 749 const_iterator end() const { return Sections.end(); } 750 751 size_t size() const { return Sections.size(); } 752 753 /// @} 754 /// @name Symbol List Access 755 /// @{ 756 757 const SymbolDataListType &getSymbolList() const { return Symbols; } 758 SymbolDataListType &getSymbolList() { return Symbols; } 759 760 symbol_iterator symbol_begin() { return Symbols.begin(); } 761 const_symbol_iterator symbol_begin() const { return Symbols.begin(); } 762 763 symbol_iterator symbol_end() { return Symbols.end(); } 764 const_symbol_iterator symbol_end() const { return Symbols.end(); } 765 766 size_t symbol_size() const { return Symbols.size(); } 767 768 /// @} 769 /// @name Indirect Symbol List Access 770 /// @{ 771 772 // FIXME: This is a total hack, this should not be here. Once things are 773 // factored so that the streamer has direct access to the .o writer, it can 774 // disappear. 775 std::vector<IndirectSymbolData> &getIndirectSymbols() { 776 return IndirectSymbols; 777 } 778 779 indirect_symbol_iterator indirect_symbol_begin() { 780 return IndirectSymbols.begin(); 781 } 782 const_indirect_symbol_iterator indirect_symbol_begin() const { 783 return IndirectSymbols.begin(); 784 } 785 786 indirect_symbol_iterator indirect_symbol_end() { 787 return IndirectSymbols.end(); 788 } 789 const_indirect_symbol_iterator indirect_symbol_end() const { 790 return IndirectSymbols.end(); 791 } 792 793 size_t indirect_symbol_size() const { return IndirectSymbols.size(); } 794 795 /// @} 796 /// @name Backend Data Access 797 /// @{ 798 799 MCSectionData &getSectionData(const MCSection &Section) const { 800 MCSectionData *Entry = SectionMap.lookup(&Section); 801 assert(Entry && "Missing section data!"); 802 return *Entry; 803 } 804 805 MCSectionData &getOrCreateSectionData(const MCSection &Section, 806 bool *Created = 0) { 807 MCSectionData *&Entry = SectionMap[&Section]; 808 809 if (Created) *Created = !Entry; 810 if (!Entry) 811 Entry = new MCSectionData(Section, this); 812 813 return *Entry; 814 } 815 816 MCSymbolData &getSymbolData(const MCSymbol &Symbol) const { 817 MCSymbolData *Entry = SymbolMap.lookup(&Symbol); 818 assert(Entry && "Missing symbol data!"); 819 return *Entry; 820 } 821 822 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol, 823 bool *Created = 0) { 824 MCSymbolData *&Entry = SymbolMap[&Symbol]; 825 826 if (Created) *Created = !Entry; 827 if (!Entry) 828 Entry = new MCSymbolData(Symbol, 0, 0, this); 829 830 return *Entry; 831 } 832 833 /// @} 834 835 void dump(); 836}; 837 838} // end namespace llvm 839 840#endif 841