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