-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClipsExtensions.cc
More file actions
446 lines (414 loc) · 18.4 KB
/
ClipsExtensions.cc
File metadata and controls
446 lines (414 loc) · 18.4 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/**
* @file
* implementation of methods described in ClipsExtensions.h
* @copyright
* syn
* Copyright (c) 2013-2017, Joshua Scoggins and Contributors
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "BaseTypes.h"
#include "Base.h"
#include "BaseArithmetic.h"
#include "ClipsExtensions.h"
#include "functional.h"
#include "ExternalAddressWrapper.h"
#include <cstdint>
#include <climits>
#include <sstream>
#include <memory>
#include <map>
#include <iostream>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/trim.hpp>
extern "C" {
#include "clips.h"
}
namespace syn {
void
BinaryNot(Environment* env, UDFContext* context, UDFValue* ret) {
UDFValue val;
if (!UDFFirstArgument(context, INTEGER_BIT, &val)) {
setBoolean(env, ret, false);
return;
}
ret->integerValue = CreateInteger(env, ~(CVCoerceToInteger(&val)));
}
void
BinaryOr(Environment* env, UDFContext* context, UDFValue* ret) {
UDFValue a, b;
if (!UDFFirstArgument(context, INTEGER_BIT, &a)) {
setBoolean(env, ret, false);
return;
} else if (!UDFNextArgument(context, INTEGER_BIT, &b)) {
setBoolean(env, ret, false);
return;
}
ret->integerValue = CreateInteger(env, CVCoerceToInteger(&a) | CVCoerceToInteger(&b));
}
void
BinaryAnd(Environment* env, UDFContext* context, UDFValue* ret) {
UDFValue a, b;
if (!UDFFirstArgument(context, INTEGER_BIT, &a)) {
setBoolean(env, ret, false);
return;
} else if (!UDFNextArgument(context, INTEGER_BIT, &b)) {
setBoolean(env, ret, false);
return;
}
ret->integerValue = CreateInteger(env, CVCoerceToInteger(&a) & CVCoerceToInteger(&b));
}
void
BinaryXor(Environment* env, UDFContext* context, UDFValue* ret) {
UDFValue a, b;
if (!UDFFirstArgument(context, INTEGER_BIT, &a)) {
setBoolean(env, ret, false);
return;
} else if (!UDFNextArgument(context, INTEGER_BIT, &b)) {
setBoolean(env, ret, false);
return;
}
ret->integerValue = CreateInteger(env, CVCoerceToInteger(&a) ^ CVCoerceToInteger(&b));
}
void
BinaryNor(Environment* env, UDFContext* context, UDFValue* ret) {
UDFValue a, b;
if (!UDFFirstArgument(context, INTEGER_BIT, &a)) {
setBoolean(env, ret, false);
return;
} else if (!UDFNextArgument(context, INTEGER_BIT, &b)) {
setBoolean(env, ret, false);
return;
}
ret->integerValue = CreateInteger(env, ~(CVCoerceToInteger(&a) | CVCoerceToInteger(&b)));
}
void
BinaryNand(Environment* env, UDFContext* context, UDFValue* ret) {
UDFValue a, b;
if (!UDFFirstArgument(context, INTEGER_BIT, &a)) {
setBoolean(env, ret, false);
return;
} else if (!UDFNextArgument(context, INTEGER_BIT, &b)) {
setBoolean(env, ret, false);
return;
}
ret->integerValue = CreateInteger(env, ~(CVCoerceToInteger(&a) & CVCoerceToInteger(&b)));
}
void
ShiftLeft(Environment* env, UDFContext* context, UDFValue* ret) {
UDFValue value, by;
if (!UDFFirstArgument(context, INTEGER_BIT, &value)) {
setBoolean(env, ret, false);
return;
} else if (!UDFNextArgument(context, INTEGER_BIT, &by)) {
setBoolean(env, ret, false);
return;
}
ret->integerValue = CreateInteger(env, (CVCoerceToInteger(&value) << CVCoerceToInteger(&by)));
}
void
ShiftRight(Environment* env, UDFContext* context, UDFValue* ret) {
UDFValue value, by;
if (!UDFFirstArgument(context, INTEGER_BIT, &value)) {
setBoolean(env, ret, false);
return;
} else if (!UDFNextArgument(context, INTEGER_BIT, &by)) {
setBoolean(env, ret, false);
return;
}
ret->integerValue = CreateInteger(env, (CVCoerceToInteger(&value) >> CVCoerceToInteger(&by)));
}
void CLIPS_errorMessageGeneric(Environment* env, UDFContext* context, UDFValue* ret, const char* msg) noexcept {
UDFInvalidArgumentMessage(context, msg);
setBoolean(env, ret, false);
}
void CLIPS_errorMessageGeneric(Environment* env, UDFContext* context, UDFValue* ret, const std::string& msg) noexcept {
CLIPS_errorMessageGeneric(env, context, ret, msg.c_str());
}
void CLIPS_errorOverflowedNumber(Environment* env, UDFContext* context, UDFValue* ret) noexcept {
CLIPS_errorMessageGeneric(env, context, ret, "number is too large and overflowed");
}
void CLIPS_translateBitmask(Environment* env, UDFContext* context, UDFValue* ret) noexcept {
UDFValue value;
if (!UDFFirstArgument(context, LEXEME_BITS, &value)) {
setBoolean(env, ret, false);
} else {
std::string str(value.lexemeValue->contents);
if (boost::starts_with(str, "0m")) {
str.at(1) = '0';
auto tmp = strtoul(str.c_str(), NULL, 2);
if (tmp == ULONG_MAX && errno == ERANGE) {
CLIPS_errorOverflowedNumber(env, context, ret);
} else {
if (tmp > 0xFF) {
CLIPS_errorMessageGeneric(env, context, ret, "provided number is larger than 8-bits!");
} else {
ret->integerValue = CreateInteger(env, static_cast<int64_t>(static_cast<byte>(tmp)));
}
}
} else {
CLIPS_errorMessageGeneric(env, context, ret, "Bitmask must start with 0m");
}
}
}
void CLIPS_errorNumberLargerThan64Bits(Environment* env, UDFContext* context, UDFValue* ret) noexcept {
CLIPS_errorMessageGeneric(env, context, ret, "provided number is larger than 64-bits!");
}
void CLIPS_expandBit(Environment* env, UDFContext* context, UDFValue* ret) noexcept {
UDFValue number;
if (!UDFFirstArgument(context, NUMBER_BITS, &number)) {
setBoolean(env, ret, false);
return;
}
auto value = CVCoerceToInteger(&number);
ret->integerValue = CreateInteger(env, int64_t(expandBit(value != 0)));
}
void CLIPS_basePrintAddress(Environment* env, const char* logicalName, void* theValue, const char* func, const char* majorType) {
std::stringstream ss;
auto* ptr = static_cast<CLIPSExternalAddress*>(theValue);
ss << "<" << majorType << "-" << func << "-" << std::hex << ((ptr) ? ptr->contents : theValue) << ">";
auto str = ss.str();
clips::printRouter(env, logicalName, str);
}
//void CLIPS_basePrintAddress_Pointer(Environment* env, const char* logicalName, void* theValue, const char* func) noexcept {
// CLIPS_basePrintAddress(env, logicalName, theValue, func, "Pointer");
//}
void CLIPS_decodeBits(Environment* env, UDFContext* context, UDFValue* ret) {
UDFValue value, mask, shift;
if (!UDFFirstArgument(context, NUMBER_BITS, &value)) {
setBoolean(env, ret, false);
} else if (!UDFNextArgument(context, NUMBER_BITS, &mask)) {
setBoolean(env, ret, false);
} else if (!UDFNextArgument(context, NUMBER_BITS, &shift)) {
setBoolean(env, ret, false);
} else {
ret->integerValue = CreateInteger(env, decodeBits<int64_t, int64_t>(CVCoerceToInteger(&value), CVCoerceToInteger(&mask), CVCoerceToInteger(&shift)));
}
}
void CLIPS_encodeBits(Environment* env, UDFContext* context, UDFValue* ret) noexcept {
UDFValue input, value, mask, shift;
if (!UDFFirstArgument(context, NUMBER_BITS, &input)) {
setBoolean(env, ret, false);
} else if (!UDFNextArgument(context, NUMBER_BITS, &value)) {
setBoolean(env, ret, false);
} else if (!UDFNextArgument(context, NUMBER_BITS, &mask)) {
setBoolean(env, ret, false);
} else if (!UDFNextArgument(context, NUMBER_BITS, &shift)) {
setBoolean(env, ret, false);
} else {
auto i = CVCoerceToInteger(&input);
auto v = CVCoerceToInteger(&value);
auto m = CVCoerceToInteger(&mask);
auto s = CVCoerceToInteger(&shift);
ret->integerValue = CreateInteger(env, encodeBits<int64_t, int64_t>(i, v, m, s));
}
}
template<typename I, typename O, I mask, int index>
O performDecode(I input) noexcept {
return syn::decodeBits<I, O, mask << (8 * index), (8 * index)>(input);
}
void CLIPS_breakApartNumber(Environment* env, UDFContext* context, UDFValue* ret) {
UDFValue number;
if (!UDFFirstArgument(context, NUMBER_BITS, &number)) {
setBoolean(env, ret, false);
return;
}
auto integer = CVCoerceToInteger(&number);
constexpr int integerWidth = byteCount<decltype(integer)>;
constexpr auto baseMask = static_cast<decltype(integer)>(0xFF);
maya::MultifieldBuilder mb(env);
if (integerWidth == 8) {
mb.append(performDecode<int64_t, int64_t, baseMask, 0>(integer));
mb.append(performDecode<int64_t, int64_t, baseMask, 1>(integer));
mb.append(performDecode<int64_t, int64_t, baseMask, 2>(integer));
mb.append(performDecode<int64_t, int64_t, baseMask, 3>(integer));
mb.append(performDecode<int64_t, int64_t, baseMask, 4>(integer));
mb.append(performDecode<int64_t, int64_t, baseMask, 5>(integer));
mb.append(performDecode<int64_t, int64_t, baseMask, 6>(integer));
mb.append(performDecode<int64_t, int64_t, baseMask, 7>(integer));
} else {
for (int i = 0; i < integerWidth; ++i) {
mb.append(int64_t(syn::decodeBits<int64_t, int64_t>(integer, baseMask << (8 * i), (8 * i))));
}
}
ret->multifieldValue = mb.create();
}
bool errorMessage(Environment* env, const std::string& idClass, int idIndex, const std::string& msgPrefix, const std::string& msg) noexcept {
PrintErrorID(env, idClass.c_str(), idIndex, false);
clips::printRouter(env, STDERR, msgPrefix);
clips::printRouter(env, STDERR, msg);
clips::printLine(env, STDERR);
SetEvaluationError(env, true);
return false;
}
template<bool shiftLeft>
void CLIPS_circularShiftBase(Environment* env, UDFContext* context, UDFValue* ret) {
UDFValue a, b;
if (!UDFFirstArgument(context, INTEGER_BIT, &a)) {
setBoolean(env, ret, false);
} else if (!UDFNextArgument(context, INTEGER_BIT, &b)) {
setBoolean(env, ret, false);
} else {
auto firstValue = CVCoerceToInteger(&a);
auto secondValue = CVCoerceToInteger(&b);
auto result = shiftLeft ?
circularShiftLeft<int64_t>(firstValue, secondValue) :
circularShiftRight<int64_t>(firstValue, secondValue);
ret->integerValue = CreateInteger(env, result);
}
}
void CLIPS_circularShiftLeft(Environment* env, UDFContext* context, UDFValue* ret) {
CLIPS_circularShiftBase<true>(env, context, ret);
}
void CLIPS_circularShiftRight(Environment* env, UDFContext* context, UDFValue* ret) {
CLIPS_circularShiftBase<false>(env, context, ret);
}
void CLIPS_onesComplement(Environment* env, UDFContext* context, UDFValue* ret) {
UDFValue val;
if (!UDFFirstArgument(context, INTEGER_BIT, &val)) {
setBoolean(env, ret, false);
} else {
ret->integerValue = CreateInteger(env, onesComplement(CVCoerceToInteger(&val)));
}
}
void CLIPS_twosComplement(Environment* env, UDFContext* context, UDFValue* ret) {
UDFValue val;
if (!UDFFirstArgument(context, INTEGER_BIT, &val)) {
setBoolean(env, ret, false);
} else {
ret->integerValue = CreateInteger(env, twosComplement(CVCoerceToInteger(&val)));
}
}
void CLIPS_multiplyAdd(Environment* env, UDFContext* context, UDFValue* ret) {
UDFValue a, b, c;
if (!UDFFirstArgument(context, INTEGER_BIT, &a)) {
setBoolean(env, ret, false);
} else if (!UDFNextArgument(context, INTEGER_BIT, &b)) {
setBoolean(env, ret, false);
} else if (!UDFNextArgument(context, INTEGER_BIT, &c)) {
setBoolean(env, ret, false);
} else {
ret->integerValue = CreateInteger(env, multiplyAdd(CVCoerceToInteger(&a), CVCoerceToInteger(&b), CVCoerceToInteger(&c)));
}
}
void CLIPS_getEndianness(Environment* env, UDFContext* context, UDFValue* ret) {
static bool init = true;
static std::string storage;
if (init) {
init = false;
if (syn::isBigEndian()) {
storage = "big";
} else if (syn::isLittleEndian()) {
storage = "little";
} else {
storage = "unknown";
}
}
// only compute this once!
ret->lexemeValue = CreateSymbol(env, storage.c_str());
}
template<bool upperHalf>
void upperLowerHalfManip(Environment* env, UDFContext* context, UDFValue* ret) {
UDFValue num;
if (!UDFFirstArgument(context, INTEGER_BIT, &num)) {
setBoolean(env, ret, false);
} else {
int64_t n = num.integerValue->contents;
if (upperHalf) {
ret->integerValue = CreateInteger(env, syn::decodeBits<int64_t, int64_t, static_cast<int64_t>(0xFFFFFFFF00000000), getShiftCount<int64_t>()>(n));
} else {
ret->integerValue = CreateInteger(env, decodeBits<int64_t, int64_t, 0x00000000FFFFFFFF, 0>(n));
}
}
}
void CLIPS_getUpperHalf(Environment* env, UDFContext* c, UDFValue* ret) { upperLowerHalfManip<true>(env, c, ret); }
void CLIPS_getLowerHalf(Environment* env, UDFContext* c, UDFValue* ret) { upperLowerHalfManip<false>(env, c, ret); }
void CLIPS_translateBinary(Environment* env, UDFContext* context, UDFValue* ret) noexcept;
void CLIPS_translateHex(Environment* env, UDFContext* context, UDFValue* ret) noexcept;
void CLIPS_translateOctal(Environment* env, UDFContext* context, UDFValue* ret) noexcept;
void installExtensions(Environment* theEnv) {
AddUDF(theEnv, "bitmask->int", "l", 1, 1, "sy", CLIPS_translateBitmask, "CLIPS_translateBitmask", nullptr);
AddUDF(theEnv, "expand-bit", "l", 1, 1, nullptr, CLIPS_expandBit, "CLIPS_expandBit", nullptr);
AddUDF(theEnv, "decode-bits", "l", 3, 3, "l;l;l", CLIPS_decodeBits, "CLIPS_decodeBits",nullptr);
AddUDF(theEnv, "encode-bits", "l", 4, 4, "l;l;l;l", CLIPS_encodeBits, "CLIPS_encodeBits",nullptr);
AddUDF(theEnv, "break-apart-number", "m", 1, 1, "l", CLIPS_breakApartNumber, "CLIPS_breakApartNumber", nullptr);
AddUDF(theEnv, "circular-shift-right", "l", 2, 2, "l;l", CLIPS_circularShiftRight, "CLIPS_circularShiftRight",nullptr);
AddUDF(theEnv, "circular-shift-left", "l", 2, 2, "l;l", CLIPS_circularShiftLeft, "CLIPS_circularShiftLeft", nullptr);
AddUDF(theEnv, "ones-complement", "l", 1, 1, "l", CLIPS_onesComplement, "CLIPS_onesComplement",nullptr);
AddUDF(theEnv, "twos-complement", "l", 1, 1, "l", CLIPS_twosComplement, "CLIPS_twosComplement",nullptr);
AddUDF(theEnv, "multiply-add", "l", 3, 3, "l;l;l", CLIPS_multiplyAdd, "CLIPS_multiplyAdd", nullptr);
AddUDF(theEnv, "get-endian", "sy", 0, 0, nullptr, CLIPS_getEndianness, "CLIPS_getEndianness", nullptr);
AddUDF(theEnv, "upper-half", "l", 1, 1, "l", CLIPS_getUpperHalf, "CLIPS_getUpperHalf",nullptr);
AddUDF(theEnv, "lower-half", "l", 1, 1, "l", CLIPS_getLowerHalf, "CLIPS_getLowerHalf",nullptr);
AddUDF(theEnv, "binary-not", "l", 1, 1, "l", BinaryNot, "BinaryNot", nullptr);
AddUDF(theEnv, "binary-and", "l", 2, 2, "l;l", BinaryAnd, "BinaryAnd", nullptr);
AddUDF(theEnv, "binary-or", "l", 2, 2, "l;l", BinaryOr, "BinaryOr", nullptr);
AddUDF(theEnv, "binary-xor", "l", 2, 2, "l;l", BinaryXor, "BinaryXor", nullptr);
AddUDF(theEnv, "binary-nand", "l", 2, 2, "l;l", BinaryNand, "BinaryNand", nullptr);
AddUDF(theEnv, "binary-nor", "l", 2, 2, "l;l", BinaryNor, "BinaryNor", nullptr);
AddUDF(theEnv, "left-shift", "l", 2, 2, "l;l",ShiftLeft, "ShiftLeft", nullptr);
AddUDF(theEnv, "right-shift", "l", 2, 2, "l;l", ShiftRight, "ShiftRight", nullptr);
AddUDF(theEnv, "binary->int", "l", 1, 1, "sy", CLIPS_translateBinary, "CLIPS_translateBinary", nullptr);
AddUDF(theEnv, "hex->int", "l", 1, 1, "sy", CLIPS_translateHex, "CLIPS_translateHex", nullptr);
AddUDF(theEnv, "oct->int", "l", 1, 1, "sy", CLIPS_translateOctal, "CLIPS_translateOctal", nullptr);
}
void buildFunctionString(std::ostream& stream, const std::string& action, const std::string& name) noexcept {
stream << "Function " << action << " (" << name << ")";
}
void buildFunctionErrorString(std::ostream& stream, const std::string& action, const std::string& name) noexcept {
stream << "Function ";
buildFunctionString(stream, action, name);
}
void CLIPS_translateNumberBase(Environment* env, UDFContext* context, UDFValue* ret, const std::string& prefix, int base, const std::string& badPrefix, bool zeroPositionOne = false) noexcept {
constexpr uint64_t maximumIntegerValue = 0xFFFFFFFFFFFFFFFF;
UDFValue value;
if (!UDFFirstArgument(context, LEXEME_BITS, &value)) {
setBoolean(context, ret, false);
}
std::string str(getLexeme(value));
if (boost::starts_with(str, prefix)) {
if (zeroPositionOne) {
str.at(1) = '0';
}
auto tmp = strtoull(str.c_str(), nullptr, base);
if (tmp == ULLONG_MAX && errno == ERANGE) {
CLIPS_errorOverflowedNumber(env, context, ret);
} else {
if (tmp > maximumIntegerValue) {
CLIPS_errorNumberLargerThan64Bits(env, context, ret);
} else {
setInteger(env, ret, tmp);
}
}
} else {
CLIPS_errorMessageGeneric(env, context, ret, badPrefix);
}
}
void CLIPS_translateBinary(Environment* env, UDFContext* context, UDFValue* ret) noexcept {
CLIPS_translateNumberBase(env, context, ret, "0b", 2, "Binary must start with 0b", true);
}
void CLIPS_translateHex(Environment* env, UDFContext* context, UDFValue* ret) noexcept {
CLIPS_translateNumberBase(env, context, ret, "0x", 16, "Hex must start with 0x");
}
void CLIPS_translateOctal(Environment* env, UDFContext* context, UDFValue* ret) noexcept {
CLIPS_translateNumberBase(env, context, ret, "0q", 8, "Octal must start with 0q", true);
}
}