Repository navigation
Expand file tree
/
Copy pathexpression-processor.cpp
More file actions
346 lines (283 loc) · 11.1 KB
/
Copy pathexpression-processor.cpp
File metadata and controls
346 lines (283 loc) · 11.1 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
* Copyright (C) 2026 Clownacy
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "expression-processor.h"
#include <filesystem>
#include <optional>
#include "clowncommon/clowncommon.h"
#include "error.h"
#include "macro-state.h"
#include "options.h"
#include "symbol-manager.h"
#include "utilities/path.h"
std::optional<unsigned long> ExpressionProcessor::ResolveExpressionCommon(Expression &expression, bool fold, const std::string_view &message)
{
const auto &value = ResolveExpressionCommon(expression, fold);
if (!value.has_value() && !message.empty())
error_manager.Error(message);
return value;
}
std::optional<unsigned long> ExpressionProcessor::ResolveExpressionCommon(Expression &expression, const bool fold)
{
const auto &value = std::visit(
CC_VisitHelper{
[&](ExpressionArithmetic2Operand &expression) -> std::optional<unsigned long>
{
const auto &left_value_optional = ResolveExpressionCommon(expression.subexpressions[0], fold);
const auto &right_value_optional = ResolveExpressionCommon(expression.subexpressions[1], fold);
if (!left_value_optional.has_value() || !right_value_optional.has_value())
return std::nullopt;
auto left_value = *left_value_optional;
auto right_value = *right_value_optional;
unsigned long value;
switch (expression.type)
{
default:
CC_UNREACHABLE;
/* Fallthrough */
case ExpressionArithmetic2Operand::Type::SUBTRACT:
value = left_value - right_value;
break;
case ExpressionArithmetic2Operand::Type::ADD:
value = left_value + right_value;
break;
case ExpressionArithmetic2Operand::Type::MULTIPLY:
case ExpressionArithmetic2Operand::Type::DIVIDE:
case ExpressionArithmetic2Operand::Type::MODULO:
{
const bool left_value_was_negative = left_value >= 0x80000000;
const bool right_value_was_negative = right_value >= 0x80000000;
/* Make inputs absolute, so we can emulate signed arithmetic with unsigned arithmetic. */
left_value = (left_value ^ (-(unsigned long)left_value_was_negative & 0xFFFFFFFF)) + left_value_was_negative;
right_value = (right_value ^ (-(unsigned long)right_value_was_negative & 0xFFFFFFFF)) + right_value_was_negative;
switch (expression.type)
{
default:
CC_UNREACHABLE;
/* Fallthrough */
case ExpressionArithmetic2Operand::Type::MULTIPLY:
value = left_value * right_value;
if (left_value_was_negative != right_value_was_negative)
value = -value;
break;
case ExpressionArithmetic2Operand::Type::DIVIDE:
if (right_value == 0)
{
error_manager.Error("Cannot divide by zero.");
return std::nullopt;
}
value = left_value / right_value;
if (left_value_was_negative != right_value_was_negative)
value = -value;
break;
case ExpressionArithmetic2Operand::Type::MODULO:
if (right_value == 0)
{
error_manager.Error("Cannot modulo by zero.");
return std::nullopt;
}
value = left_value % right_value;
if (left_value_was_negative)
value = -value;
break;
}
break;
}
case ExpressionArithmetic2Operand::Type::LOGICAL_OR:
value = left_value != 0 || right_value != 0 ? -1 : 0;
break;
case ExpressionArithmetic2Operand::Type::LOGICAL_AND:
value = left_value != 0 && right_value != 0 ? -1 : 0;
break;
case ExpressionArithmetic2Operand::Type::BITWISE_OR:
value = left_value | right_value;
break;
case ExpressionArithmetic2Operand::Type::BITWISE_XOR:
value = left_value ^ right_value;
break;
case ExpressionArithmetic2Operand::Type::BITWISE_AND:
value = left_value & right_value;
break;
case ExpressionArithmetic2Operand::Type::EQUALITY:
value = left_value == right_value ? -1 : 0;
break;
case ExpressionArithmetic2Operand::Type::INEQUALITY:
value = left_value != right_value ? -1 : 0;
break;
case ExpressionArithmetic2Operand::Type::LESS_THAN:
case ExpressionArithmetic2Operand::Type::LESS_OR_EQUAL:
case ExpressionArithmetic2Operand::Type::MORE_THAN:
case ExpressionArithmetic2Operand::Type::MORE_OR_EQUAL:
/* Flip the sign bit so we can emulate signed comparisons with unsigned integers. */
left_value ^= 0x80000000;
right_value ^= 0x80000000;
switch (expression.type)
{
default:
CC_UNREACHABLE;
/* Fallthrough */
case ExpressionArithmetic2Operand::Type::LESS_THAN:
value = left_value < right_value;
break;
case ExpressionArithmetic2Operand::Type::LESS_OR_EQUAL:
value = left_value <= right_value;
break;
case ExpressionArithmetic2Operand::Type::MORE_THAN:
value = left_value > right_value;
break;
case ExpressionArithmetic2Operand::Type::MORE_OR_EQUAL:
value = left_value >= right_value;
break;
}
value = -value;
break;
/* asm68k does 32-bit arithmetic, so it masks the shift amount to 0-31 (though whether this is intentional or just a side-effect of using x86 shift instructions that do this is unknown).
(note: this is also needed to prevent undefined behaviour in C from shifting by an amount greater than or equal to the width of the type). */
/* TODO: Print a warning here. */
case ExpressionArithmetic2Operand::Type::LEFT_SHIFT:
value = left_value << (right_value % 32);
break;
case ExpressionArithmetic2Operand::Type::RIGHT_SHIFT:
value = left_value >> (right_value % 32);
break;
}
/* Prevent 'bleeding' out of the 68k's 32-bit range. */
return value & 0xFFFFFFFF;
},
[&](ExpressionArithmetic1Operand &expression) -> std::optional<unsigned long>
{
const auto &value_optional = ResolveExpressionCommon(*expression.subexpressions, fold);
if (!value_optional.has_value())
return std::nullopt;
auto value = *value_optional;
switch (expression.type)
{
default:
CC_UNREACHABLE;
/* Fallthrough */
case ExpressionArithmetic1Operand::Type::NEGATE:
value = 0 - value;
break;
case ExpressionArithmetic1Operand::Type::BITWISE_NOT:
value = ~value;
break;
case ExpressionArithmetic1Operand::Type::LOGICAL_NOT:
value = value == 0 ? -1 : 0;
break;
}
/* Prevent 'bleeding' out of the 68k's 32-bit range. */
return value & 0xFFFFFFFF;
},
[&](ExpressionNumber &expression) -> std::optional<unsigned long>
{
return expression.value;
},
[&](ExpressionIdentifier &expression) -> std::optional<unsigned long>
{
return symbol_manager.GetSymbolInteger(expression.string, false);
},
[&](ExpressionString &expression) -> std::optional<unsigned long>
{
const std::size_t length = expression.string.length();
if (length > 4)
{
error_manager.Error("Character literals cannot be more than 4 characters long.");
return std::nullopt;
}
unsigned long value = 0;
for (std::size_t i = 0; i < length; ++i)
{
value <<= 8;
value |= expression.string[i];
}
return value;
},
[&](ExpressionProgramCounterOfStatement&) -> std::optional<unsigned long>
{
return program_counter_of_statement;
},
[&](ExpressionProgramCounterOfExpression&) -> std::optional<unsigned long>
{
return program_counter_of_expression;
},
[&](ExpressionStrlen &expression) -> std::optional<unsigned long>
{
return expression.string.length();
},
[&](ExpressionStrcmp &expression) -> std::optional<unsigned long>
{
return expression.string_a == expression.string_b ? -1 : 0;
},
[&](ExpressionInstr &expression) -> std::optional<unsigned long>
{
const auto &position_optional = ResolveExpressionCommon(expression.subexpressions[0], fold);
if (!position_optional.has_value())
return std::nullopt;
const auto &position = *position_optional;
const auto expression_string_a = std::get_if<ExpressionString>(&expression.subexpressions[1]);
const auto expression_string_b = std::get_if<ExpressionString>(&expression.subexpressions[2]);
if (expression_string_a == nullptr || expression_string_b == nullptr)
{
error_manager.Error("Last two arguments to `INSTR` must be strings.");
return std::nullopt;
}
/* TODO: What if 'position' is 0? */
const std::size_t found_position = Utilities::CaseInsensitive::FindString(expression_string_a->string, expression_string_b->string, position - 1, options->case_insensitive);
return found_position == expression_string_a->string.npos ? 0 : found_position + 1;
},
[&](ExpressionDef &expression) -> std::optional<unsigned long>
{
return symbol_manager.LookupSymbol(expression.string, nullptr) != nullptr ? -1 : 0;
},
[&](ExpressionTypeWithIdentifier&) -> std::optional<unsigned long>
{
return 0; /* TODO: This is a placeholder! */
},
[&](ExpressionTypeWithNumber&) -> std::optional<unsigned long>
{
return 1; /* TODO: This is a placeholder! */
},
[&](ExpressionFilesize &expression) -> std::optional<unsigned long>
{
const auto &standard_path = Utilities::Path::ToStandardPath(expression.string);
if (!std::filesystem::exists(standard_path))
{
error_manager.Error("File '{}' does not exist.", expression.string);
return std::nullopt;
}
return std::filesystem::file_size(standard_path);
},
[&](ExpressionRs&) -> std::optional<unsigned long>
{
return rs;
},
[&](ExpressionNarg&) -> std::optional<unsigned long>
{
if (!macro.ExpansionInProgress())
{
error_manager.Error("`NARG` cannot be used outside of a macro.");
return std::nullopt;
}
return macro.expansion->total_arguments_at_start;
}
}, expression
);
/* Now that we have resolved the value, let's hardcode it here so that we don't ever have to calculate it again. */
/* This is especially useful for fix-ups, which may otherwise depend on identifiers that no longer exist at the time the value is resolved again. */
if (value.has_value() && fold)
expression.emplace<ExpressionNumber>(*value);
return value;
}