MCFixup.h revision 1f6efa3996dd1929fbc129203ce5009b620e6969
173c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar//===-- llvm/MC/MCFixup.h - Instruction Relocation and Patching -*- C++ -*-===//
273c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar//
373c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar//                     The LLVM Compiler Infrastructure
473c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar//
573c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar// This file is distributed under the University of Illinois Open Source
673c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar// License. See LICENSE.TXT for details.
773c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar//
873c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar//===----------------------------------------------------------------------===//
973c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar
1073c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar#ifndef LLVM_MC_MCFIXUP_H
1173c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar#define LLVM_MC_MCFIXUP_H
1273c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar
131f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/DataTypes.h"
1473c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar#include <cassert>
1573c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar
1673c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbarnamespace llvm {
175d5a1e13a129e18ee6031fe6354acd2ab4d39f37Daniel Dunbarclass MCExpr;
1873c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar
1973c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// MCFixupKind - Extensible enumeration to represent the type of a fixup.
2073c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbarenum MCFixupKind {
2173c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar  FK_Data_1 = 0, ///< A one-byte fixup.
2273c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar  FK_Data_2,     ///< A two-byte fixup.
2373c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar  FK_Data_4,     ///< A four-byte fixup.
2473c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar  FK_Data_8,     ///< A eight-byte fixup.
25e04ed7e45f194f14a7b28bbf3f55694d8e2bcf80Rafael Espindola  FK_PCRel_1,    ///< A one-byte pc relative fixup.
26e04ed7e45f194f14a7b28bbf3f55694d8e2bcf80Rafael Espindola  FK_PCRel_2,    ///< A two-byte pc relative fixup.
27e04ed7e45f194f14a7b28bbf3f55694d8e2bcf80Rafael Espindola  FK_PCRel_4,    ///< A four-byte pc relative fixup.
2873c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar
290dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar  FirstTargetFixupKind = 128,
3073c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar
310dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar  // Limit range of target fixups, in case we want to pack more efficiently
320dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar  // later.
330dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar  MaxTargetFixupKind = (1 << 8)
3473c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar};
3573c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar
360dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar/// MCFixup - Encode information on a single operation to perform on a byte
3773c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// sequence (e.g., an encoded instruction) which requires assemble- or run-
3873c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// time patching.
3973c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar///
4073c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// Fixups are used any time the target instruction encoder needs to represent
4173c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// some value in an instruction which is not yet concrete. The encoder will
4273c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// encode the instruction assuming the value is 0, and emit a fixup which
4373c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// communicates to the assembler backend how it should rewrite the encoded
4473c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// value.
4573c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar///
4673c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// During the process of relaxation, the assembler will apply fixups as
4773c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// symbolic values become concrete. When relaxation is complete, any remaining
4873c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// fixups become relocations in the object file (or errors, if the fixup cannot
4973c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// be encoded on the target).
5073c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbarclass MCFixup {
515d5a1e13a129e18ee6031fe6354acd2ab4d39f37Daniel Dunbar  /// The value to put into the fixup location. The exact interpretation of the
520dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar  /// expression is target dependent, usually it will be one of the operands to
530dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar  /// an instruction or an assembler directive.
545d5a1e13a129e18ee6031fe6354acd2ab4d39f37Daniel Dunbar  const MCExpr *Value;
555d5a1e13a129e18ee6031fe6354acd2ab4d39f37Daniel Dunbar
5673c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar  /// The byte index of start of the relocation inside the encoded instruction.
570dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar  uint32_t Offset;
5873c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar
5973c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar  /// The target dependent kind of fixup item this is. The kind is used to
6073c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar  /// determine how the operand value should be encoded into the instruction.
610dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar  unsigned Kind;
6273c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar
6373c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbarpublic:
640dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar  static MCFixup Create(uint32_t Offset, const MCExpr *Value,
655d5a1e13a129e18ee6031fe6354acd2ab4d39f37Daniel Dunbar                        MCFixupKind Kind) {
660dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar    assert(unsigned(Kind) < MaxTargetFixupKind && "Kind out of range!");
6773c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar    MCFixup FI;
685d5a1e13a129e18ee6031fe6354acd2ab4d39f37Daniel Dunbar    FI.Value = Value;
6973c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar    FI.Offset = Offset;
7073c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar    FI.Kind = unsigned(Kind);
7173c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar    return FI;
7273c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar  }
7373c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar
745d5a1e13a129e18ee6031fe6354acd2ab4d39f37Daniel Dunbar  MCFixupKind getKind() const { return MCFixupKind(Kind); }
7573c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar
760dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar  uint32_t getOffset() const { return Offset; }
770dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar  void setOffset(uint32_t Value) { Offset = Value; }
7873c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar
795d5a1e13a129e18ee6031fe6354acd2ab4d39f37Daniel Dunbar  const MCExpr *getValue() const { return Value; }
802be2fd073003c0988723d2894dfb117ad90be11bDaniel Dunbar
812be2fd073003c0988723d2894dfb117ad90be11bDaniel Dunbar  /// getKindForSize - Return the generic fixup kind for a value with the given
822be2fd073003c0988723d2894dfb117ad90be11bDaniel Dunbar  /// size. It is an error to pass an unsupported size.
83e04ed7e45f194f14a7b28bbf3f55694d8e2bcf80Rafael Espindola  static MCFixupKind getKindForSize(unsigned Size, bool isPCRel) {
842be2fd073003c0988723d2894dfb117ad90be11bDaniel Dunbar    switch (Size) {
852be2fd073003c0988723d2894dfb117ad90be11bDaniel Dunbar    default: assert(0 && "Invalid generic fixup size!");
86e04ed7e45f194f14a7b28bbf3f55694d8e2bcf80Rafael Espindola    case 1: return isPCRel ? FK_PCRel_1 : FK_Data_1;
87e04ed7e45f194f14a7b28bbf3f55694d8e2bcf80Rafael Espindola    case 2: return isPCRel ? FK_PCRel_2 : FK_Data_2;
88e04ed7e45f194f14a7b28bbf3f55694d8e2bcf80Rafael Espindola    case 4: return isPCRel ? FK_PCRel_4 : FK_Data_4;
89e04ed7e45f194f14a7b28bbf3f55694d8e2bcf80Rafael Espindola    case 8:
90e04ed7e45f194f14a7b28bbf3f55694d8e2bcf80Rafael Espindola      assert(!isPCRel && "8 byte pc relative fixup is not supported.");
91e04ed7e45f194f14a7b28bbf3f55694d8e2bcf80Rafael Espindola      return FK_Data_8;
922be2fd073003c0988723d2894dfb117ad90be11bDaniel Dunbar    }
932be2fd073003c0988723d2894dfb117ad90be11bDaniel Dunbar  }
9473c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar};
9573c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar
9673c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar} // End llvm namespace
9773c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar
9873c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar#endif
99