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
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "rcc/MC/MCFixup.h"
#include <algorithm>
#include <array>
#include <cstdint>
#include <limits>
#include <utility>
#include <vector>
namespace rcc::mc {
namespace {
[[nodiscard]] Expected<std::int64_t> checkedAdd(std::int64_t Left, std::int64_t Right) {
std::int64_t Result = 0;
if (__builtin_add_overflow(Left, Right, &Result))
return makeUnexpected(ErrorCode::RelocationFailed, "MC expression addition overflows");
return Result;
}
[[nodiscard]] Expected<std::int64_t> checkedSubtract(std::int64_t Left, std::int64_t Right) {
std::int64_t Result = 0;
if (__builtin_sub_overflow(Left, Right, &Result))
return makeUnexpected(ErrorCode::RelocationFailed, "MC expression subtraction overflows");
return Result;
}
[[nodiscard]] Expected<MCValue> combineLinearValues(const MCValue &Left, const MCValue &Right,
bool SubtractRight) {
struct SymbolTerm final {
MCSymbolRef Symbol;
int Coefficient = 0;
};
std::vector<SymbolTerm> Terms;
const auto AddTerm = [&Terms](const MCSymbolRef &Symbol, int Coefficient) -> Expected<void> {
if (!Symbol)
return {};
const auto Position = std::ranges::find(Terms, Symbol, &SymbolTerm::Symbol);
if (Position == Terms.end()) {
Terms.push_back(SymbolTerm{.Symbol = Symbol, .Coefficient = Coefficient});
return {};
}
Position->Coefficient += Coefficient;
if (Position->Coefficient < -1 || Position->Coefficient > 1) {
return makeUnexpected(ErrorCode::RelocationFailed,
"MC expression has a symbol coefficient outside -1 to 1");
}
if (Position->Coefficient == 0)
Terms.erase(Position);
return {};
};
for (const auto &[Symbol, Coefficient] : std::array<std::pair<MCSymbolRef, int>, 4>{
std::pair{Left.AddSymbol, 1},
std::pair{Left.SubtractSymbol, -1},
std::pair{Right.AddSymbol, SubtractRight ? -1 : 1},
std::pair{Right.SubtractSymbol, SubtractRight ? 1 : -1},
}) {
auto Added = AddTerm(Symbol, Coefficient);
if (!Added)
return std::unexpected(Added.error());
}
MCValue Result;
for (const SymbolTerm &Term : Terms) {
MCSymbolRef &Destination = Term.Coefficient > 0 ? Result.AddSymbol : Result.SubtractSymbol;
if (Destination) {
return makeUnexpected(ErrorCode::RelocationFailed,
"MC expression contains more than one symbol with the same sign");
}
Destination = Term.Symbol;
}
auto Addend = SubtractRight ? checkedSubtract(Left.Addend, Right.Addend)
: checkedAdd(Left.Addend, Right.Addend);
if (!Addend)
return std::unexpected(Addend.error());
Result.Addend = *Addend;
return Result;
}
[[nodiscard]] Expected<MCValue> evaluate(const MCExpr &Expression, std::size_t Depth) {
if (Depth > 256)
return makeUnexpected(ErrorCode::RelocationFailed, "MC expression exceeds its depth limit");
switch (Expression.kind()) {
case MCExpr::Kind::Constant:
return MCValue{.AddSymbol = {},
.SubtractSymbol = {},
.Addend = *Expression.getIf<std::int64_t>(),
.TargetKind = 0};
case MCExpr::Kind::SymbolRef:
return MCValue{.AddSymbol = Expression.getIf<MCSymbolRefExpr>()->Symbol,
.SubtractSymbol = {},
.Addend = 0,
.TargetKind = 0};
case MCExpr::Kind::Target: {
const auto &Target = *Expression.getIf<MCTargetExpr>();
auto Result = evaluate(*Target.Operand, Depth + 1);
if (!Result)
return std::unexpected(Result.error());
if (Result->TargetKind != 0) {
return makeUnexpected(ErrorCode::RelocationFailed,
"nested target MC expressions are not relocatable");
}
Result->TargetKind = Target.Kind;
return Result;
}
case MCExpr::Kind::Unary: {
const auto &Unary = *Expression.getIf<MCUnaryExpr>();
auto Operand = evaluate(*Unary.Operand, Depth + 1);
if (!Operand)
return std::unexpected(Operand.error());
if (Unary.Operator == MCUnaryOperator::Plus)
return Operand;
if (Unary.Operator == MCUnaryOperator::Minus && Operand->isAbsolute()) {
if (Operand->Addend == std::numeric_limits<std::int64_t>::min()) {
return makeUnexpected(ErrorCode::RelocationFailed, "MC expression negation overflows");
}
Operand->Addend = -Operand->Addend;
return Operand;
}
const auto Absolute = Expression.evaluateAbsolute();
if (!Absolute) {
return makeUnexpected(ErrorCode::RelocationFailed,
"symbolic unary MC expression is not relocatable");
}
return MCValue{.AddSymbol = {}, .SubtractSymbol = {}, .Addend = *Absolute, .TargetKind = 0};
}
case MCExpr::Kind::Binary: {
const auto &Binary = *Expression.getIf<MCBinaryExpr>();
if (Binary.Operator != MCBinaryOperator::Add && Binary.Operator != MCBinaryOperator::Subtract) {
const auto Absolute = Expression.evaluateAbsolute();
if (!Absolute) {
return makeUnexpected(ErrorCode::RelocationFailed,
"symbolic binary MC expression is not relocatable");
}
return MCValue{.AddSymbol = {}, .SubtractSymbol = {}, .Addend = *Absolute, .TargetKind = 0};
}
auto Left = evaluate(*Binary.Left, Depth + 1);
auto Right = evaluate(*Binary.Right, Depth + 1);
if (!Left)
return std::unexpected(Left.error());
if (!Right)
return std::unexpected(Right.error());
if (Left->TargetKind != 0 || Right->TargetKind != 0) {
return makeUnexpected(ErrorCode::RelocationFailed,
"target MC expression cannot participate in outer arithmetic");
}
return combineLinearValues(*Left, *Right, Binary.Operator == MCBinaryOperator::Subtract);
}
}
return makeUnexpected(ErrorCode::InternalError, "MC expression has an invalid kind");
}
} // namespace
Expected<void> MCFixup::verify() const {
if (Kind == fixup_kind::None)
return makeUnexpected(ErrorCode::InvalidArgument, "MC fixup has no kind");
if (!Value)
return makeUnexpected(ErrorCode::InvalidArgument, "MC fixup has no value expression");
auto Verification = Value->verify();
if (!Verification)
return std::unexpected(Verification.error());
return {};
}
Expected<MCValue> evaluateAsRelocatable(const MCExpr &Expression) {
auto Verification = Expression.verify();
if (!Verification)
return std::unexpected(Verification.error());
return evaluate(Expression, 0);
}
} // namespace rcc::mc
|