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