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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "rcc/MC/MCExpr.h"
#include <cstddef>
#include <cstdint>
#include <limits>
#include <memory>
#include <optional>
#include <utility>
namespace rcc::mc {
namespace {
[[nodiscard]] std::optional<std::int64_t> checkedUnary(MCUnaryOperator Operator,
std::int64_t Value) noexcept {
switch (Operator) {
case MCUnaryOperator::Plus:
return Value;
case MCUnaryOperator::Minus:
if (Value == std::numeric_limits<std::int64_t>::min())
return std::nullopt;
return -Value;
case MCUnaryOperator::BitwiseNot:
return ~Value;
case MCUnaryOperator::LogicalNot:
return Value == 0 ? 1 : 0;
}
return std::nullopt;
}
[[nodiscard]] std::optional<std::int64_t>
checkedBinary(MCBinaryOperator Operator, std::int64_t Left, std::int64_t Right) noexcept {
std::int64_t Result = 0;
switch (Operator) {
case MCBinaryOperator::Add:
return __builtin_add_overflow(Left, Right, &Result) ? std::nullopt : std::optional(Result);
case MCBinaryOperator::Subtract:
return __builtin_sub_overflow(Left, Right, &Result) ? std::nullopt : std::optional(Result);
case MCBinaryOperator::Multiply:
return __builtin_mul_overflow(Left, Right, &Result) ? std::nullopt : std::optional(Result);
case MCBinaryOperator::Divide:
if (Right == 0 || (Left == std::numeric_limits<std::int64_t>::min() && Right == -1))
return std::nullopt;
return Left / Right;
case MCBinaryOperator::Modulo:
if (Right == 0 || (Left == std::numeric_limits<std::int64_t>::min() && Right == -1))
return std::nullopt;
return Left % Right;
case MCBinaryOperator::ShiftLeft:
if (Right < 0 || Right >= 64)
return std::nullopt;
if (Right == 63) {
if (Left == 0)
return 0;
return Left == -1 ? std::optional(std::numeric_limits<std::int64_t>::min()) : std::nullopt;
}
if (__builtin_mul_overflow(Left, std::int64_t{1} << Right, &Result))
return std::nullopt;
return Result;
case MCBinaryOperator::ShiftRight:
if (Right < 0 || Right >= 64)
return std::nullopt;
return Left >> Right;
case MCBinaryOperator::BitwiseAnd:
return Left & Right;
case MCBinaryOperator::BitwiseOr:
return Left | Right;
case MCBinaryOperator::BitwiseXor:
return Left ^ Right;
case MCBinaryOperator::LogicalAnd:
return Left != 0 && Right != 0 ? 1 : 0;
case MCBinaryOperator::LogicalOr:
return Left != 0 || Right != 0 ? 1 : 0;
case MCBinaryOperator::Equal:
return Left == Right ? 1 : 0;
case MCBinaryOperator::NotEqual:
return Left != Right ? 1 : 0;
case MCBinaryOperator::Less:
return Left < Right ? 1 : 0;
case MCBinaryOperator::LessEqual:
return Left <= Right ? 1 : 0;
case MCBinaryOperator::Greater:
return Left > Right ? 1 : 0;
case MCBinaryOperator::GreaterEqual:
return Left >= Right ? 1 : 0;
}
return std::nullopt;
}
[[nodiscard]] Expected<void> verifyExpression(const MCExpr &Expression, std::size_t Depth,
std::size_t MaximumDepth) {
if (Depth > MaximumDepth)
return makeUnexpected(ErrorCode::InvalidArgument, "MC expression exceeds its depth limit");
switch (Expression.kind()) {
case MCExpr::Kind::Constant:
if (Expression.getIf<std::int64_t>() == nullptr)
return makeUnexpected(ErrorCode::InternalError, "MC constant expression storage is invalid");
return {};
case MCExpr::Kind::SymbolRef: {
const auto *Reference = Expression.getIf<MCSymbolRefExpr>();
if (Reference == nullptr || !Reference->Symbol || Reference->Symbol->name().empty()) {
return makeUnexpected(ErrorCode::InvalidArgument,
"MC symbol expression requires a valid symbol");
}
return {};
}
case MCExpr::Kind::Unary: {
const auto *Unary = Expression.getIf<MCUnaryExpr>();
if (Unary == nullptr || !Unary->Operand)
return makeUnexpected(ErrorCode::InvalidArgument, "MC unary expression has no operand");
return verifyExpression(*Unary->Operand, Depth + 1, MaximumDepth);
}
case MCExpr::Kind::Binary: {
const auto *Binary = Expression.getIf<MCBinaryExpr>();
if (Binary == nullptr || !Binary->Left || !Binary->Right)
return makeUnexpected(ErrorCode::InvalidArgument, "MC binary expression has a null operand");
auto Left = verifyExpression(*Binary->Left, Depth + 1, MaximumDepth);
if (!Left)
return std::unexpected(Left.error());
return verifyExpression(*Binary->Right, Depth + 1, MaximumDepth);
}
case MCExpr::Kind::Target: {
const auto *Target = Expression.getIf<MCTargetExpr>();
if (Target == nullptr || Target->Kind == 0 || !Target->Operand) {
return makeUnexpected(ErrorCode::InvalidArgument,
"target MC expression requires a nonzero kind and an operand");
}
return verifyExpression(*Target->Operand, Depth + 1, MaximumDepth);
}
}
return makeUnexpected(ErrorCode::InternalError, "MC expression kind is invalid");
}
} // namespace
MCExprRef MCExpr::constant(std::int64_t Value) {
return std::shared_ptr<const MCExpr>(new MCExpr(Kind::Constant, Value));
}
Expected<MCExprRef> MCExpr::symbol(MCSymbolRef Symbol) {
if (!Symbol || Symbol->name().empty())
return makeUnexpected(ErrorCode::InvalidArgument, "MC symbol expression requires a symbol");
return std::shared_ptr<const MCExpr>(
new MCExpr(Kind::SymbolRef, MCSymbolRefExpr{.Symbol = std::move(Symbol)}));
}
Expected<MCExprRef> MCExpr::unary(MCUnaryOperator Operator, MCExprRef Operand) {
if (!Operand)
return makeUnexpected(ErrorCode::InvalidArgument, "MC unary expression requires an operand");
return std::shared_ptr<const MCExpr>(
new MCExpr(Kind::Unary, MCUnaryExpr{.Operator = Operator, .Operand = std::move(Operand)}));
}
Expected<MCExprRef> MCExpr::binary(MCBinaryOperator Operator, MCExprRef Left, MCExprRef Right) {
if (!Left || !Right)
return makeUnexpected(ErrorCode::InvalidArgument, "MC binary expression requires two operands");
return std::shared_ptr<const MCExpr>(new MCExpr(
Kind::Binary,
MCBinaryExpr{.Operator = Operator, .Left = std::move(Left), .Right = std::move(Right)}));
}
Expected<MCExprRef> MCExpr::target(std::uint32_t Kind, MCExprRef Operand) {
if (Kind == 0 || !Operand) {
return makeUnexpected(ErrorCode::InvalidArgument,
"target MC expression requires a nonzero kind and an operand");
}
return std::shared_ptr<const MCExpr>(
new MCExpr(MCExpr::Kind::Target, MCTargetExpr{.Kind = Kind, .Operand = std::move(Operand)}));
}
Expected<void> MCExpr::verify(std::size_t MaximumDepth) const {
if (MaximumDepth == 0)
return makeUnexpected(ErrorCode::InvalidArgument, "MC expression depth limit cannot be zero");
return verifyExpression(*this, 1, MaximumDepth);
}
std::optional<std::int64_t> MCExpr::evaluateAbsolute() const noexcept {
switch (ExpressionKind) {
case Kind::Constant:
return *getIf<std::int64_t>();
case Kind::SymbolRef:
case Kind::Target:
return std::nullopt;
case Kind::Unary: {
const auto *Unary = getIf<MCUnaryExpr>();
if (Unary == nullptr || !Unary->Operand)
return std::nullopt;
const auto Value = Unary->Operand->evaluateAbsolute();
return Value ? checkedUnary(Unary->Operator, *Value) : std::nullopt;
}
case Kind::Binary: {
const auto *Binary = getIf<MCBinaryExpr>();
if (Binary == nullptr || !Binary->Left || !Binary->Right)
return std::nullopt;
const auto Left = Binary->Left->evaluateAbsolute();
const auto Right = Binary->Right->evaluateAbsolute();
return Left && Right ? checkedBinary(Binary->Operator, *Left, *Right) : std::nullopt;
}
}
return std::nullopt;
}
} // namespace rcc::mc
|