aboutsummaryrefslogtreecommitdiff
path: root/backend/lib/CodeGen/MachineJumpTableInfo.cpp
blob: 5ba0853e9c9030de8097ac7e4366fe918acfcd33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// SPDX-License-Identifier: GPL-3.0-or-later

#include "rcc/CodeGen/MachineJumpTableInfo.h"

#include "rcc/CodeGen/MachineBasicBlock.h"
#include "rcc/CodeGen/MachineFunction.h"

#include <bit>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <utility>

namespace rcc {

Expected<void>
MachineJumpTableInfo::verifyDescription(const MachineJumpTableDesc &Description) const {
  if (Description.Encoding >= JumpTableEntryEncoding::Count)
    return makeUnexpected(ErrorCode::VerificationError, "jump table encoding is invalid");
  if (Description.EntrySize == 0 || Description.EntrySize > 16 ||
      !std::has_single_bit(Description.EntrySize)) {
    return makeUnexpected(ErrorCode::VerificationError,
                          "jump table entry size must be a power of two no larger than 16 bytes");
  }
  if (Description.Targets.empty())
    return makeUnexpected(ErrorCode::VerificationError, "jump table must have at least one target");
  if (Description.Placement.MaximumDistance && *Description.Placement.MaximumDistance == 0) {
    return makeUnexpected(ErrorCode::VerificationError,
                          "jump table maximum distance must be nonzero");
  }
  if ((Description.Encoding == JumpTableEntryEncoding::Target) !=
      (Description.TargetEncoding != 0)) {
    return makeUnexpected(ErrorCode::VerificationError,
                          "target jump table encoding and target encoding ID are inconsistent");
  }
  if ((Description.Encoding == JumpTableEntryEncoding::GPRelative32BlockAddress ||
       Description.Encoding == JumpTableEntryEncoding::LabelDifference32 ||
       Description.Encoding == JumpTableEntryEncoding::Custom32) &&
      Description.EntrySize != 4) {
    return makeUnexpected(ErrorCode::VerificationError,
                          "32-bit jump table encodings require four-byte entries");
  }
  if ((Description.Encoding == JumpTableEntryEncoding::GPRelative64BlockAddress ||
       Description.Encoding == JumpTableEntryEncoding::LabelDifference64) &&
      Description.EntrySize != 8) {
    return makeUnexpected(ErrorCode::VerificationError,
                          "64-bit jump table encodings require eight-byte entries");
  }

  for (const MachineBasicBlock *Target : Description.Targets) {
    if (Target == nullptr || &Target->function() != Owner || !Target->isLinked()) {
      return makeUnexpected(ErrorCode::VerificationError,
                            "jump table target must be linked to its owning function");
    }
  }
  return {};
}

Expected<JumpTableIndex> MachineJumpTableInfo::createTable(MachineJumpTableDesc Description) {
  if (Frozen)
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "frozen jump table info cannot create tables");
  auto Verification = verifyDescription(Description);
  if (!Verification)
    return std::unexpected(Verification.error());
  if (Tables.size() > std::numeric_limits<std::uint32_t>::max())
    return makeUnexpected(ErrorCode::Unsupported, "jump table index space is exhausted");

  const JumpTableIndex Index = JumpTableIndex::fromValue(static_cast<std::uint32_t>(Tables.size()));
  Tables.push_back(MachineJumpTableEntry(Index, std::move(Description)));
  return Index;
}

Expected<JumpTableIndex> MachineJumpTableInfo::reserveTable() {
  if (Frozen) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "frozen jump table info cannot reserve tables");
  }
  if (Tables.size() > std::numeric_limits<std::uint32_t>::max())
    return makeUnexpected(ErrorCode::Unsupported, "jump table index space is exhausted");

  const JumpTableIndex Index = JumpTableIndex::fromValue(static_cast<std::uint32_t>(Tables.size()));
  Tables.push_back(MachineJumpTableEntry(Index));
  return Index;
}

Expected<void>
MachineJumpTableInfo::defineReservedTable(JumpTableIndex Index,
                                          MachineJumpTableDesc Description) {
  if (Frozen) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "frozen jump table info cannot define reserved tables");
  }
  if (Index.value() >= Tables.size() || !Tables[Index.value()].isPending()) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "jump table index is not an unresolved reservation");
  }
  auto Verification = verifyDescription(Description);
  if (!Verification)
    return std::unexpected(Verification.error());
  Tables[Index.value()].Description.emplace<MachineJumpTableDesc>(std::move(Description));
  return {};
}

Expected<MachineJumpTableEntry *> MachineJumpTableInfo::mutableTable(JumpTableIndex Index) {
  if (Frozen)
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "frozen jump table info cannot modify tables");
  if (Index.value() >= Tables.size())
    return makeUnexpected(ErrorCode::InvalidArgument, "jump table index is out of range");
  MachineJumpTableEntry &Entry = Tables[Index.value()];
  if (Entry.isTombstone() || Entry.isPending()) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "jump table index has no defined description");
  }
  return &Entry;
}

Expected<void> MachineJumpTableInfo::updateTable(JumpTableIndex Index,
                                                 MachineJumpTableDesc Description) {
  auto Verification = verifyDescription(Description);
  if (!Verification)
    return std::unexpected(Verification.error());
  auto Entry = mutableTable(Index);
  if (!Entry)
    return std::unexpected(Entry.error());
  (*Entry)->Description.emplace<MachineJumpTableDesc>(std::move(Description));
  return {};
}

Expected<void> MachineJumpTableInfo::eraseTable(JumpTableIndex Index) {
  auto Entry = mutableTable(Index);
  if (!Entry)
    return std::unexpected(Entry.error());
  (*Entry)->Description.emplace<std::monostate>();
  return {};
}

const MachineJumpTableEntry *MachineJumpTableInfo::table(JumpTableIndex Index) const noexcept {
  if (Index.value() >= Tables.size() || Tables[Index.value()].isTombstone())
    return nullptr;
  return &Tables[Index.value()];
}

std::size_t MachineJumpTableInfo::liveTableCount() const noexcept {
  std::size_t Count = 0;
  for (const MachineJumpTableEntry &Entry : Tables) {
    if (!Entry.isTombstone())
      ++Count;
  }
  return Count;
}

Expected<void> MachineJumpTableInfo::freeze() {
  if (Frozen)
    return makeUnexpected(ErrorCode::InvalidArgument, "jump table info is already frozen");
  auto Verification = verify();
  if (!Verification)
    return std::unexpected(Verification.error());
  Frozen = true;
  return {};
}

Expected<void> MachineJumpTableInfo::verify() const {
  if (Owner == nullptr)
    return makeUnexpected(ErrorCode::VerificationError, "jump table info has no owner");
  for (std::size_t Position = 0; Position < Tables.size(); ++Position) {
    const MachineJumpTableEntry &Entry = Tables[Position];
    if (Entry.index() != JumpTableIndex::fromValue(static_cast<std::uint32_t>(Position))) {
      return makeUnexpected(ErrorCode::VerificationError,
                            "jump table index does not match its stable table position");
    }
    const MachineJumpTableDesc *Description = Entry.description();
    if (Entry.isPending()) {
      return makeUnexpected(ErrorCode::VerificationError,
                            "jump table contains an unresolved reservation");
    }
    if (Description == nullptr)
      continue;
    auto Verification = verifyDescription(*Description);
    if (!Verification)
      return std::unexpected(Verification.error());
  }
  return {};
}

} // namespace rcc