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"
1450bee42b54cd9aec5f49566307df2b0cf23afcf6Craig Topper#include "llvm/Support/ErrorHandling.h"
1582f4ce5081fc9cfbf34bbe61eb0412e7ca4dc3dfJim Grosbach#include "llvm/Support/SMLoc.h"
1673c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar#include <cassert>
1773c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar
1873c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbarnamespace llvm {
195d5a1e13a129e18ee6031fe6354acd2ab4d39f37Daniel Dunbarclass MCExpr;
2073c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar
2173c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// MCFixupKind - Extensible enumeration to represent the type of a fixup.
2273c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbarenum MCFixupKind {
2373c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar  FK_Data_1 = 0, ///< A one-byte fixup.
2473c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar  FK_Data_2,     ///< A two-byte fixup.
2573c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar  FK_Data_4,     ///< A four-byte fixup.
2673c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar  FK_Data_8,     ///< A eight-byte fixup.
27e04ed7e45f194f14a7b28bbf3f55694d8e2bcf80Rafael Espindola  FK_PCRel_1,    ///< A one-byte pc relative fixup.
28e04ed7e45f194f14a7b28bbf3f55694d8e2bcf80Rafael Espindola  FK_PCRel_2,    ///< A two-byte pc relative fixup.
29e04ed7e45f194f14a7b28bbf3f55694d8e2bcf80Rafael Espindola  FK_PCRel_4,    ///< A four-byte pc relative fixup.
303a83c40ab61d5ca624f2bbadd70237c6adbdb304Rafael Espindola  FK_PCRel_8,    ///< A eight-byte pc relative fixup.
3184bfc2f090639f933df06cc675c4385511516befAkira Hatanaka  FK_GPRel_1,    ///< A one-byte gp relative fixup.
3284bfc2f090639f933df06cc675c4385511516befAkira Hatanaka  FK_GPRel_2,    ///< A two-byte gp relative fixup.
3384bfc2f090639f933df06cc675c4385511516befAkira Hatanaka  FK_GPRel_4,    ///< A four-byte gp relative fixup.
3484bfc2f090639f933df06cc675c4385511516befAkira Hatanaka  FK_GPRel_8,    ///< A eight-byte gp relative fixup.
35ce618af3e880ae0ec0ddd81aca8ed5bbd4096ae4Rafael Espindola  FK_SecRel_1,   ///< A one-byte section relative fixup.
36ce618af3e880ae0ec0ddd81aca8ed5bbd4096ae4Rafael Espindola  FK_SecRel_2,   ///< A two-byte section relative fixup.
37ce618af3e880ae0ec0ddd81aca8ed5bbd4096ae4Rafael Espindola  FK_SecRel_4,   ///< A four-byte section relative fixup.
38ce618af3e880ae0ec0ddd81aca8ed5bbd4096ae4Rafael Espindola  FK_SecRel_8,   ///< A eight-byte section relative fixup.
3973c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar
400dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar  FirstTargetFixupKind = 128,
4173c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar
420dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar  // Limit range of target fixups, in case we want to pack more efficiently
430dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar  // later.
440dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar  MaxTargetFixupKind = (1 << 8)
4573c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar};
4673c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar
470dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar/// MCFixup - Encode information on a single operation to perform on a byte
4873c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// sequence (e.g., an encoded instruction) which requires assemble- or run-
4973c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// time patching.
5073c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar///
5173c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// Fixups are used any time the target instruction encoder needs to represent
5273c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// some value in an instruction which is not yet concrete. The encoder will
5373c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// encode the instruction assuming the value is 0, and emit a fixup which
5473c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// communicates to the assembler backend how it should rewrite the encoded
5573c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// value.
5673c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar///
5773c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// During the process of relaxation, the assembler will apply fixups as
5873c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// symbolic values become concrete. When relaxation is complete, any remaining
5973c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// fixups become relocations in the object file (or errors, if the fixup cannot
6073c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// be encoded on the target).
6173c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbarclass MCFixup {
625d5a1e13a129e18ee6031fe6354acd2ab4d39f37Daniel Dunbar  /// The value to put into the fixup location. The exact interpretation of the
630dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar  /// expression is target dependent, usually it will be one of the operands to
640dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar  /// an instruction or an assembler directive.
655d5a1e13a129e18ee6031fe6354acd2ab4d39f37Daniel Dunbar  const MCExpr *Value;
665d5a1e13a129e18ee6031fe6354acd2ab4d39f37Daniel Dunbar
6773c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar  /// The byte index of start of the relocation inside the encoded instruction.
680dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar  uint32_t Offset;
6973c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar
7073c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar  /// The target dependent kind of fixup item this is. The kind is used to
7173c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar  /// determine how the operand value should be encoded into the instruction.
720dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar  unsigned Kind;
7373c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar
7482f4ce5081fc9cfbf34bbe61eb0412e7ca4dc3dfJim Grosbach  /// The source location which gave rise to the fixup, if any.
7582f4ce5081fc9cfbf34bbe61eb0412e7ca4dc3dfJim Grosbach  SMLoc Loc;
7673c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbarpublic:
770dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar  static MCFixup Create(uint32_t Offset, const MCExpr *Value,
7882f4ce5081fc9cfbf34bbe61eb0412e7ca4dc3dfJim Grosbach                        MCFixupKind Kind, SMLoc Loc = SMLoc()) {
790dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar    assert(unsigned(Kind) < MaxTargetFixupKind && "Kind out of range!");
8073c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar    MCFixup FI;
815d5a1e13a129e18ee6031fe6354acd2ab4d39f37Daniel Dunbar    FI.Value = Value;
8273c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar    FI.Offset = Offset;
8373c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar    FI.Kind = unsigned(Kind);
8482f4ce5081fc9cfbf34bbe61eb0412e7ca4dc3dfJim Grosbach    FI.Loc = Loc;
8573c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar    return FI;
8673c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar  }
8773c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar
885d5a1e13a129e18ee6031fe6354acd2ab4d39f37Daniel Dunbar  MCFixupKind getKind() const { return MCFixupKind(Kind); }
8973c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar
900dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar  uint32_t getOffset() const { return Offset; }
910dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar  void setOffset(uint32_t Value) { Offset = Value; }
9273c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar
935d5a1e13a129e18ee6031fe6354acd2ab4d39f37Daniel Dunbar  const MCExpr *getValue() const { return Value; }
942be2fd073003c0988723d2894dfb117ad90be11bDaniel Dunbar
952be2fd073003c0988723d2894dfb117ad90be11bDaniel Dunbar  /// getKindForSize - Return the generic fixup kind for a value with the given
962be2fd073003c0988723d2894dfb117ad90be11bDaniel Dunbar  /// size. It is an error to pass an unsupported size.
97e04ed7e45f194f14a7b28bbf3f55694d8e2bcf80Rafael Espindola  static MCFixupKind getKindForSize(unsigned Size, bool isPCRel) {
982be2fd073003c0988723d2894dfb117ad90be11bDaniel Dunbar    switch (Size) {
9950bee42b54cd9aec5f49566307df2b0cf23afcf6Craig Topper    default: llvm_unreachable("Invalid generic fixup size!");
100e04ed7e45f194f14a7b28bbf3f55694d8e2bcf80Rafael Espindola    case 1: return isPCRel ? FK_PCRel_1 : FK_Data_1;
101e04ed7e45f194f14a7b28bbf3f55694d8e2bcf80Rafael Espindola    case 2: return isPCRel ? FK_PCRel_2 : FK_Data_2;
102e04ed7e45f194f14a7b28bbf3f55694d8e2bcf80Rafael Espindola    case 4: return isPCRel ? FK_PCRel_4 : FK_Data_4;
1033a83c40ab61d5ca624f2bbadd70237c6adbdb304Rafael Espindola    case 8: return isPCRel ? FK_PCRel_8 : FK_Data_8;
1042be2fd073003c0988723d2894dfb117ad90be11bDaniel Dunbar    }
1052be2fd073003c0988723d2894dfb117ad90be11bDaniel Dunbar  }
10682f4ce5081fc9cfbf34bbe61eb0412e7ca4dc3dfJim Grosbach
10782f4ce5081fc9cfbf34bbe61eb0412e7ca4dc3dfJim Grosbach  SMLoc getLoc() const { return Loc; }
10873c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar};
10973c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar
11073c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar} // End llvm namespace
11173c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar
11273c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar#endif
113