MCFixup.h revision 73c557458c0e28899f37c557bcaf36c2b6701260
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 <cassert>
14
15namespace llvm {
16
17// Private constants, do not use.
18//
19// This is currently layed out so that the MCFixup fields can be efficiently
20// accessed, while keeping the offset field large enought that the assembler
21// backend can reasonably use the MCFixup representation for an entire fragment
22// (splitting any overly large fragments).
23//
24// The division of bits between the kind and the opindex can be tweaked if we
25// end up needing more bits for target dependent kinds.
26enum {
27  MCFIXUP_NUM_GENERIC_KINDS = 128,
28  MCFIXUP_NUM_KIND_BITS = 8,
29  MCFIXUP_NUM_OPINDEX_BITS = 8,
30  MCFIXUP_NUM_OFFSET_BITS = (32 - MCFIXUP_NUM_OPINDEX_BITS -
31                             MCFIXUP_NUM_OPINDEX_BITS)
32};
33
34/// MCFixupKind - Extensible enumeration to represent the type of a fixup.
35enum MCFixupKind {
36  FK_Data_1 = 0, ///< A one-byte fixup.
37  FK_Data_2,     ///< A two-byte fixup.
38  FK_Data_4,     ///< A four-byte fixup.
39  FK_Data_8,     ///< A eight-byte fixup.
40
41  FirstTargetFixupKind = MCFIXUP_NUM_GENERIC_KINDS,
42
43  MaxTargetFixupKind = (1 << MCFIXUP_NUM_KIND_BITS)
44};
45
46/// MCFixup - Encode information on a single operation to perform on an byte
47/// sequence (e.g., an encoded instruction) which requires assemble- or run-
48/// time patching.
49///
50/// Fixups are used any time the target instruction encoder needs to represent
51/// some value in an instruction which is not yet concrete. The encoder will
52/// encode the instruction assuming the value is 0, and emit a fixup which
53/// communicates to the assembler backend how it should rewrite the encoded
54/// value.
55///
56/// During the process of relaxation, the assembler will apply fixups as
57/// symbolic values become concrete. When relaxation is complete, any remaining
58/// fixups become relocations in the object file (or errors, if the fixup cannot
59/// be encoded on the target).
60class MCFixup {
61  static const unsigned MaxOffset = 1 << MCFIXUP_NUM_KIND_BITS;
62
63  /// The byte index of start of the relocation inside the encoded instruction.
64  unsigned Offset : MCFIXUP_NUM_OFFSET_BITS;
65
66  /// The index of the operand to encode into the instruction.
67  unsigned OpIndex : MCFIXUP_NUM_OPINDEX_BITS;
68
69  /// The target dependent kind of fixup item this is. The kind is used to
70  /// determine how the operand value should be encoded into the instruction.
71  unsigned Kind : MCFIXUP_NUM_KIND_BITS;
72
73public:
74  static MCFixup Create(unsigned Offset, unsigned OpIndex, MCFixupKind Kind) {
75    MCFixup FI;
76    FI.Offset = Offset;
77    FI.OpIndex = OpIndex;
78    FI.Kind = unsigned(Kind);
79
80    assert(Offset == FI.Offset && "Offset out of range!");
81    assert(OpIndex == FI.OpIndex && "Operand index out of range!");
82    assert(Kind == FI.Kind && "Kind out of range!");
83    return FI;
84  }
85
86  unsigned getOffset() const { return Offset; }
87
88  unsigned getOpIndex() const { return OpIndex; }
89
90  MCFixupKind getKind() const { return MCFixupKind(Kind); }
91};
92
93} // End llvm namespace
94
95#endif
96