-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMemoryBlock.cc
More file actions
390 lines (364 loc) · 14.6 KB
/
MemoryBlock.cc
File metadata and controls
390 lines (364 loc) · 14.6 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
/**
* @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 "ClipsExtensions.h"
#include "Base.h"
#include "ExternalAddressWrapper.h"
#include "MemoryBlock.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 {
template<typename T>
using Block = T[];
//bool Arg2IsInteger(Environment* env, UDFValue* storage, const std::string& funcStr) noexcept {
// return tryGetArgumentAsInteger(env, funcStr, 2, storage);
//}
//bool Arg2IsSymbol(Environment* env, UDFValue* storage, const std::string& funcStr) noexcept {
// return tryGetArgumentAsSymbol(env, funcStr, 2, storage);
//}
void handleProblem(Environment* env, UDFValue* ret, const syn::Problem& p, const std::string funcErrorPrefix) noexcept {
setBoolean(env, ret, false);
std::stringstream s;
s << "an exception was thrown: " << p.what();
auto str = s.str();
errorMessage(env, "CALL", 2, funcErrorPrefix, str);
}
template<typename Word>
class ManagedMemoryBlock : public ExternalAddressWrapper<Block<Word>> {
public:
static_assert(std::is_integral<Word>::value, "Expected an integral type to be for type Word");
using Address = int64_t;
using WordBlock = Block<Word>;
using Parent = ExternalAddressWrapper<WordBlock>;
using Self = ManagedMemoryBlock;
using Self_Ptr = Self*;
static ManagedMemoryBlock* make(int64_t capacity) noexcept {
return new ManagedMemoryBlock(capacity);
}
using ManagedMemoryBlock_Ptr = ManagedMemoryBlock*;
static void newFunction(UDFContext* context, UDFValue* ret) {
auto* env = context->environment;
try {
UDFValue capacity;
if (!UDFNextArgument(context, MayaType::INTEGER_BIT, &capacity)) {
setBoolean(env, ret, false);
errorMessage(env, "NEW", 1, getFunctionErrorPrefixNew<WordBlock>(), " expected an integer for capacity!");
}
auto cap = getInteger(capacity);
auto idIndex = Self::getAssociatedEnvironmentId(env);
setExternalAddress(env, ret, Self::make(cap), idIndex);
} catch(const syn::Problem& p) {
handleProblem(env, ret, p, getFunctionErrorPrefixNew<WordBlock>());
}
}
enum class MemoryBlockOp {
Populate,
Size,
Type,
Set,
Move,
Swap,
Decrement,
Increment,
Get,
MapWrite,
Count,
};
static std::tuple<MemoryBlockOp, int> getParameters(const std::string& op) noexcept {
static std::map<std::string, std::tuple<MemoryBlockOp, int>> opTranslation = {
{ "populate", std::make_tuple(MemoryBlockOp:: Populate , 1) },
{ "size", std::make_tuple(MemoryBlockOp:: Size , 0) },
{ "type", std::make_tuple(MemoryBlockOp:: Type , 0) },
{ "write", std::make_tuple(MemoryBlockOp:: Set , 2) },
{ "move", std::make_tuple(MemoryBlockOp:: Move , 2) },
{ "swap", std::make_tuple(MemoryBlockOp:: Swap , 2) },
{ "decrement", std::make_tuple(MemoryBlockOp:: Decrement , 1) },
{ "increment", std::make_tuple(MemoryBlockOp:: Increment , 1) },
{ "read", std::make_tuple(MemoryBlockOp:: Get , 1) },
{ "map-write", std::make_tuple(MemoryBlockOp::MapWrite, 2) },
};
static std::tuple<MemoryBlockOp, int> bad;
static bool init = false;
if (!init) {
init = true;
bad = std::make_tuple(syn::defaultErrorState<MemoryBlockOp>, -1);
}
auto result = opTranslation.find(op);
if (result == opTranslation.end()) {
return bad;
} else {
return result->second;
}
}
static bool callFunction(UDFContext* context, UDFValue* theValue, UDFValue* ret) {
UDFValue operation;
if (!UDFNextArgument(context, MayaType::SYMBOL_BIT, &operation)) {
//TODO: put error messages in here
return false;
}
std::string str(getLexeme(&operation));
// translate the op to an enumeration
auto* env = context->environment;
auto result = getParameters(str);
if (syn::isErrorState(std::get<0>(result))) {
setBoolean(context, ret, false);
return false;
//return Parent::callErrorMessageCode3(env, ret, str, " <- unknown operation requested!");
}
MemoryBlockOp op;
int aCount;
setBoolean(env, ret, true);
auto ptr = static_cast<Self_Ptr>(getExternalAddress(theValue));
std::tie(op, aCount) = result;
switch(op) {
case MemoryBlockOp::Type:
Self::setType(context, ret);
break;
case MemoryBlockOp::Size:
setInteger(context, ret, ptr->size());
break;
case MemoryBlockOp::Get:
return ptr->load(env, context, ret);
case MemoryBlockOp::Populate:
return ptr->populate(env, context, ret);
case MemoryBlockOp::Increment:
return ptr->increment(env, context, ret);
case MemoryBlockOp::Decrement:
return ptr->decrement(env, context, ret);
case MemoryBlockOp::Swap:
return ptr->swap(env, context, ret);
case MemoryBlockOp::Move:
return ptr->move(env, context, ret);
case MemoryBlockOp::Set:
return ptr->store(env, context, ret);
case MemoryBlockOp::MapWrite:
return ptr->mapWrite(env, context, ret);
default:
setBoolean(context, ret, false);
//return Parent::callErrorMessageCode3(env, ret, str, "<- legal but unimplemented operation!");
//TODO: add error message
return false;
}
return true;
}
static void registerWithEnvironment(Environment* env, const char* title) {
Parent::registerWithEnvironment(env, title, callFunction, newFunction);
}
static void registerWithEnvironment(Environment* env) {
registerWithEnvironment(env, Parent::getType().c_str());
}
public:
ManagedMemoryBlock(Address capacity) : Parent(std::move(std::make_unique<WordBlock>(capacity))), _capacity(capacity) { }
inline Address size() const noexcept { return _capacity; }
inline bool legalAddress(Address idx) const noexcept { return addressInRange<Address>(_capacity, idx); }
inline Word getMemoryCellValue(Address addr) noexcept { return this->_value.get()[addr]; }
inline void setMemoryCell(Address addr0, Word value) noexcept { this->_value.get()[addr0] = value; }
inline void swapMemoryCells(Address addr0, Address addr1) noexcept { syn::swap<Word>(this->_value.get()[addr0], this->_value.get()[addr1]); }
inline void decrementMemoryCell(Address address) noexcept { --this->_value.get()[address]; }
inline void incrementMemoryCell(Address address) noexcept { ++this->_value.get()[address]; }
inline void copyMemoryCell(Address from, Address to) noexcept {
auto ptr = this->_value.get();
ptr[to] = ptr[from];
}
inline void setMemoryToSingleValue(Word value) noexcept {
auto ptr = this->_value.get();
for (Address i = 0; i < _capacity; ++i) {
ptr[i] = value;
}
}
private:
bool extractInteger(UDFContext* context, UDFValue& storage) noexcept {
if (!UDFNextArgument(context, MayaType::INTEGER_BIT, &storage)) {
// TODO: put error message here
return false;
}
return true;
}
bool defaultSingleOperationBody(Environment* env, UDFContext* context, UDFValue* ret, std::function<bool(Environment*, UDFContext*, UDFValue*, Address)> body) noexcept {
UDFValue address;
if (!extractInteger(context, address)) {
setBoolean(env, ret, false);
return false;
}
auto value = static_cast<Address>(getInteger(address));
if (!legalAddress(value)) {
// TODO: insert error message here about illegal address
setBoolean(env, ret, false);
return false;
}
return body(env, context, ret, value);
}
bool defaultTwoOperationBody(Environment* env, UDFContext* context, UDFValue* ret, std::function<bool(Environment*, UDFContext*, UDFValue*, Address, Address)> body) noexcept {
UDFValue address, address2;
if (!extractInteger(context, address)) {
setBoolean(env, ret, false);
return false;
}
if (!extractInteger(context, address2)) {
setBoolean(env, ret, false);
return false;
}
auto addr0 = static_cast<Address>(getInteger(address));
auto addr1 = static_cast<Address>(getInteger(address2));
if (!legalAddress(addr0) || !legalAddress(addr1)) {
setBoolean(env, ret, false);
return false;
}
return body(env, context, ret, addr0, addr1);
}
public:
bool populate(Environment* env, UDFContext* context, UDFValue* ret) noexcept {
UDFValue value;
if (!extractInteger(context, value)) {
setBoolean(env, ret, false);
return false;
}
auto population = static_cast<Word>(getInteger(value));
setMemoryToSingleValue(population);
setBoolean(env, ret, true);
return true;
}
bool load(Environment* env, UDFContext* context, UDFValue* ret) noexcept {
return defaultSingleOperationBody(env, context, ret, [this](auto* env, auto* context, auto* ret, auto address) noexcept {
setInteger(env, ret, this->getMemoryCellValue(address));
return true;
});
}
bool increment(Environment* env, UDFContext* context, UDFValue* ret) noexcept {
return defaultSingleOperationBody(env, context, ret, [this](auto* env, auto* context, auto* ret, auto address) noexcept {
this->incrementMemoryCell(address);
setBoolean(env, ret, true);
return true;
});
}
bool decrement(Environment* env, UDFContext* context, UDFValue* ret) noexcept {
return defaultSingleOperationBody(env, context, ret, [this](auto* env, auto* context, auto* ret, auto address) noexcept {
this->decrementMemoryCell(address);
setBoolean(env, ret, true);
return true;
});
}
bool swap(Environment* env, UDFContext* context, UDFValue* ret) noexcept {
return defaultTwoOperationBody(env, context, ret, [this](auto* env, auto* context, auto* ret, auto addr0, auto addr1) noexcept {
this->swapMemoryCells(addr0, addr1);
setBoolean(env, ret, true);
return true;
});
}
bool move(Environment* env, UDFContext* context, UDFValue* ret) noexcept {
return defaultTwoOperationBody(env, context, ret, [this](auto* env, auto* context, auto* ret, auto from, auto to) noexcept {
this->copyMemoryCell(from, to);
setBoolean(env, ret, true);
return true;
});
}
bool store(Environment* env, UDFContext* context, UDFValue* ret) noexcept {
UDFValue address, value;
if (!extractInteger(context, address)) {
setBoolean(env, ret, false);
return false;
}
if (!extractInteger(context, value)) {
setBoolean(env, ret, false);
return false;
}
auto addr = static_cast<Address>(getInteger(address));
if (!legalAddress(addr)) {
setBoolean(env, ret, false);
return false;
}
auto data = static_cast<Word>(getInteger(value));
this->setMemoryCell(addr, data);
setBoolean(env, ret, true);
return true;
}
bool mapWrite(Environment* env, UDFContext* context, UDFValue* ret) noexcept {
UDFValue startingAddress;
if (!extractInteger(context, startingAddress)) {
setBoolean(env, ret, false);
return false;
}
auto addr = static_cast<Address>(getInteger(startingAddress));
while(UDFHasNextArgument(context)) {
if (!legalAddress(addr)) {
setBoolean(env, ret, false);
return false;
}
UDFValue currentItem;
if (!extractInteger(context, currentItem)) {
setBoolean(env, ret, false);
return false;
}
auto data = static_cast<Word>(getInteger(currentItem));
this->setMemoryCell(addr, data);
++addr;
}
setBoolean(env, ret, true);
return true;
}
private:
Address _capacity;
};
DefWrapperSymbolicName(Block<int64_t>, "memory-block");
using StandardManagedMemoryBlock = ManagedMemoryBlock<int64_t>;
#ifndef ENABLE_EXTENDED_MEMORY_BLOCKS
#define ENABLE_EXTENDED_MEMORY_BLOCKS 0
#endif // end ENABLE_EXTENDED_MEMORY_BLOCKS
#if ENABLE_EXTENDED_MEMORY_BLOCKS
#define DefMemoryBlock(name, type, alias) \
DefWrapperSymbolicName(Block< type > , name ); \
using alias = ManagedMemoryBlock< type >
DefMemoryBlock("memory-block:uint8", uint8, ManagedMemoryBlock_uint8);
DefMemoryBlock("memory-block:uint16", uint16, ManagedMemoryBlock_uint16);
DefMemoryBlock("memory-block:uint32", uint32, ManagedMemoryBlock_uint32);
DefMemoryBlock("memory-block:int32", int32, ManagedMemoryBlock_int32);
DefMemoryBlock("memory-block:int16", int16, ManagedMemoryBlock_int16);
DefMemoryBlock("memory-block:int8", int8, ManagedMemoryBlock_int8);
#undef DefMemoryBlock
#endif // end ENABLE_EXTENDED_MEMORY_BLOCKS
void installMemoryBlockTypes(Environment* theEnv) {
StandardManagedMemoryBlock::registerWithEnvironment(theEnv);
#if ENABLE_EXTENDED_MEMORY_BLOCKS
ManagedMemoryBlock_uint8::registerWithEnvironment(theEnv);
ManagedMemoryBlock_uint16::registerWithEnvironment(theEnv);
ManagedMemoryBlock_uint32::registerWithEnvironment(theEnv);
ManagedMemoryBlock_int8::registerWithEnvironment(theEnv);
ManagedMemoryBlock_int16::registerWithEnvironment(theEnv);
ManagedMemoryBlock_int32::registerWithEnvironment(theEnv);
#endif // end ENABLE_EXTENDED_MEMORY_BLOCKS
}
}