Home | History | Annotate | Download | only in MC
      1 //===-- llvm/MC/MCFixup.h - Instruction Relocation and Patching -*- C++ -*-===//
      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 #ifndef LLVM_MC_MCFIXUP_H
     11 #define LLVM_MC_MCFIXUP_H
     12 
     13 #include "llvm/Support/DataTypes.h"
     14 #include <cassert>
     15 
     16 namespace llvm {
     17 class MCExpr;
     18 
     19 /// MCFixupKind - Extensible enumeration to represent the type of a fixup.
     20 enum MCFixupKind {
     21   FK_Data_1 = 0, ///< A one-byte fixup.
     22   FK_Data_2,     ///< A two-byte fixup.
     23   FK_Data_4,     ///< A four-byte fixup.
     24   FK_Data_8,     ///< A eight-byte fixup.
     25   FK_PCRel_1,    ///< A one-byte pc relative fixup.
     26   FK_PCRel_2,    ///< A two-byte pc relative fixup.
     27   FK_PCRel_4,    ///< A four-byte pc relative fixup.
     28   FK_PCRel_8,    ///< A eight-byte pc relative fixup.
     29 
     30   FirstTargetFixupKind = 128,
     31 
     32   // Limit range of target fixups, in case we want to pack more efficiently
     33   // later.
     34   MaxTargetFixupKind = (1 << 8)
     35 };
     36 
     37 /// MCFixup - Encode information on a single operation to perform on a byte
     38 /// sequence (e.g., an encoded instruction) which requires assemble- or run-
     39 /// time patching.
     40 ///
     41 /// Fixups are used any time the target instruction encoder needs to represent
     42 /// some value in an instruction which is not yet concrete. The encoder will
     43 /// encode the instruction assuming the value is 0, and emit a fixup which
     44 /// communicates to the assembler backend how it should rewrite the encoded
     45 /// value.
     46 ///
     47 /// During the process of relaxation, the assembler will apply fixups as
     48 /// symbolic values become concrete. When relaxation is complete, any remaining
     49 /// fixups become relocations in the object file (or errors, if the fixup cannot
     50 /// be encoded on the target).
     51 class MCFixup {
     52   /// The value to put into the fixup location. The exact interpretation of the
     53   /// expression is target dependent, usually it will be one of the operands to
     54   /// an instruction or an assembler directive.
     55   const MCExpr *Value;
     56 
     57   /// The byte index of start of the relocation inside the encoded instruction.
     58   uint32_t Offset;
     59 
     60   /// The target dependent kind of fixup item this is. The kind is used to
     61   /// determine how the operand value should be encoded into the instruction.
     62   unsigned Kind;
     63 
     64 public:
     65   static MCFixup Create(uint32_t Offset, const MCExpr *Value,
     66                         MCFixupKind Kind) {
     67     assert(unsigned(Kind) < MaxTargetFixupKind && "Kind out of range!");
     68     MCFixup FI;
     69     FI.Value = Value;
     70     FI.Offset = Offset;
     71     FI.Kind = unsigned(Kind);
     72     return FI;
     73   }
     74 
     75   MCFixupKind getKind() const { return MCFixupKind(Kind); }
     76 
     77   uint32_t getOffset() const { return Offset; }
     78   void setOffset(uint32_t Value) { Offset = Value; }
     79 
     80   const MCExpr *getValue() const { return Value; }
     81 
     82   /// getKindForSize - Return the generic fixup kind for a value with the given
     83   /// size. It is an error to pass an unsupported size.
     84   static MCFixupKind getKindForSize(unsigned Size, bool isPCRel) {
     85     switch (Size) {
     86     default: assert(0 && "Invalid generic fixup size!");
     87     case 1: return isPCRel ? FK_PCRel_1 : FK_Data_1;
     88     case 2: return isPCRel ? FK_PCRel_2 : FK_Data_2;
     89     case 4: return isPCRel ? FK_PCRel_4 : FK_Data_4;
     90     case 8: return isPCRel ? FK_PCRel_8 : FK_Data_8;
     91     }
     92   }
     93 };
     94 
     95 } // End llvm namespace
     96 
     97 #endif
     98