-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreel_std.c
512 lines (411 loc) · 16.9 KB
/
reel_std.c
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#include <string.h>
/* setpos */
#define reelfunc_setpos_uintptr(ctx, ev, idx, val)\
if (*(val) < num_events) { evidx = *(val); goto loopstart; }\
else { return REEL_SETPOS_OUT_OF_BOUNDS; }
#define reelfunc_setpos_uint(ctx, ev, idx, val)\
if (val < num_events) { evidx = val; goto loopstart; }\
else { return REEL_SETPOS_OUT_OF_BOUNDS; }
/* num_events */
static inline void reelfunc_numevents_uintptr(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t *lval){
*lval = ctx->num_events;
}
/* inc */
static inline void reelfunc_inc_uintptr_uint(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t *lval, uint64_t rval){
*lval += rval;
}
static inline void reelfunc_inc_tableptr_tableptr(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, reel_var *lval, reel_var *rval){
if (!(lval->table_value_type == REEL_UINT &&
lval->table_field == rval->table_field &&
lval->table_value_type == rval->table_value_type)){
ctx->error = REEL_TABLE_MISMATCH;
}else{
uint64_t *dst = (uint64_t*)lval->value;
const uint64_t *src = (uint64_t*)rval->value;
uint64_t i;
for (i = 0; i < lval->table_length; i++)
dst[i] += src[i];
}
}
static inline void reelfunc_inc_tableitem_uint(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, reel_var *lval, uint64_t rval){
if (lval->table_value_type != REEL_UINT){
ctx->error = REEL_TABLE_MISMATCH;
}else if (lval->table_field){
uint64_t *dst = (uint64_t*)lval->value;
dst[tdb_item_val(ev->items[lval->table_field - 1])] += rval;
}
}
static inline void reelfunc_inc_tableitem_tableitem(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, reel_var *lval, reel_var *rval){
if (!(lval->table_value_type == REEL_UINT &&
rval->table_value_type == REEL_UINT &&
lval->table_field == rval->table_field))
ctx->error = REEL_TABLE_MISMATCH;
else if (lval->table_field){
const uint64_t *src = (const uint64_t*)rval->value;
uint64_t *dst = (uint64_t*)lval->value;
uint64_t idx = tdb_item_val(ev->items[lval->table_field - 1]);
dst[idx] += src[idx];
}
}
/* dec */
static inline void safe_dec(uint64_t *dst, uint64_t src)
{
if (*dst > src)
*dst -= src;
else
*dst = 0;
}
static inline void reelfunc_dec_uintptr_uint(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t *lval, uint64_t rval){
safe_dec(lval, rval);
}
static inline void reelfunc_dec_tableptr_tableptr(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, reel_var *lval, reel_var *rval){
if (!(lval->table_value_type == REEL_UINT &&
lval->table_field == rval->table_field &&
lval->table_value_type == rval->table_value_type)){
ctx->error = REEL_TABLE_MISMATCH;
}else{
uint64_t *dst = (uint64_t*)lval->value;
const uint64_t *src = (uint64_t*)rval->value;
uint64_t i;
for (i = 0; i < lval->table_length; i++)
safe_dec(&dst[i], src[i]);
}
}
static inline void reelfunc_dec_tableitem_uint(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, reel_var *lval, uint64_t rval){
if (lval->table_value_type != REEL_UINT){
ctx->error = REEL_TABLE_MISMATCH;
}else if (lval->table_field){
uint64_t *dst = (uint64_t*)lval->value;
safe_dec(&dst[tdb_item_val(ev->items[lval->table_field - 1])], rval);
}
}
static inline void reelfunc_dec_tableitem_tableitem(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, reel_var *lval, reel_var *rval){
if (!(lval->table_value_type == REEL_UINT &&
rval->table_value_type == REEL_UINT &&
lval->table_field == rval->table_field))
ctx->error = REEL_TABLE_MISMATCH;
else if (lval->table_field){
const uint64_t *src = (const uint64_t*)rval->value;
uint64_t *dst = (uint64_t*)lval->value;
uint64_t idx = tdb_item_val(ev->items[lval->table_field - 1]);
safe_dec(&dst[idx], src[idx]);
}
}
/* set */
static inline void reelfunc_set_uintptr_uint(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t *lval, uint64_t rval){
*lval = rval;
}
static inline void reelfunc_set_uintptr_uintptr(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t *lval, uint64_t *rval){
*lval = *rval;
}
static inline void reelfunc_set_itemptr_item(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, tdb_item *lval, tdb_item rval){
*lval = rval;
}
static inline void reelfunc_set_itemptr_itemptr(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, tdb_item *lval, tdb_item *rval){
*lval = *rval;
}
static inline void reelfunc_set_tableitem_uint(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, reel_var *lval, uint64_t rval){
if (lval->table_value_type != REEL_UINT){
ctx->error = REEL_TABLE_MISMATCH;
}else if (lval->table_field){
uint64_t *dst = (uint64_t*)lval->value;
dst[tdb_item_val(ev->items[lval->table_field - 1])] = rval;
}
}
static inline void reelfunc_set_tableitem_uintptr(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, reel_var *lval, uint64_t *rval){
if (lval->table_value_type != REEL_UINT){
ctx->error = REEL_TABLE_MISMATCH;
}else if (lval->table_field){
uint64_t *dst = (uint64_t*)lval->value;
dst[tdb_item_val(ev->items[lval->table_field - 1])] = *rval;
}
}
static inline void reelfunc_set_uintptr_tableitem(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t *lval, reel_var *rval){
if (rval->table_value_type != REEL_UINT){
ctx->error = REEL_TABLE_MISMATCH;
}else if (rval->table_field){
const uint64_t *src = (uint64_t*)rval->value;
*lval = src[tdb_item_val(ev->items[rval->table_field - 1])];
}else
*lval = 0;
}
/* unset */
static inline void reelfunc_unset_tableptr(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, reel_var *val){
if (val->table_field){
uint64_t *dst = (uint64_t*)val->value;
memset(dst, 0, val->table_length * 8);
}
}
static inline void reelfunc_unset_tableitem(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, reel_var *val){
if (val->table_field){
uint64_t *dst = (uint64_t*)val->value;
dst[tdb_item_val(ev->items[val->table_field - 1])] = 0;
}
}
static inline void reelfunc_unset_itemptr(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, tdb_item *val){
*val = 0;
}
static inline void reelfunc_unset_uintptr(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, tdb_item *val){
*val = 0;
}
/* if */
static inline int reelfunc_if_item(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, tdb_item item){
return tdb_item_val(item) != 0;
}
static inline int reelfunc_if_itemptr(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, tdb_item *item){
return tdb_item_val(*item) != 0;
}
static inline int reelfunc_if_item_itemptr(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, tdb_item lval, tdb_item *rval){
return lval == *rval;
}
static inline int reelfunc_if_itemptr_item(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, tdb_item *lval, tdb_item rval){
return *lval == rval;
}
static inline int reelfunc_if_item_item(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, tdb_item lval, tdb_item rval){
return lval == rval;
}
static inline int reelfunc_if_uint(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t val){
return val != 0;
}
static inline int reelfunc_if_tableitem(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, reel_var *val){
if (val->table_value_type != REEL_UINT)
ctx->error = REEL_TABLE_MISMATCH;
else if (val->table_field){
const uint64_t *src = (const uint64_t*)val->value;
return src[tdb_item_val(ev->items[val->table_field - 1])] != 0;
}
return 0;
}
static inline int reelfunc_if_uintptr(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t *val){
return *val != 0;
}
static inline int reelfunc_if_uintptr_uintptr(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t *lval, uint64_t *rval){
return *lval == *rval;
}
static inline int reelfunc_if_uintptr_uint(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t *lval, uint64_t rval){
return *lval == rval;
}
/* identity */
#define MAX_ID_ITEMS 6
static int reel_identity(reel_ctx *ctx, uint64_t *dst, const uint64_t args[MAX_ID_ITEMS])
{
Word_t *ptr;
JHSI(ptr, ctx->identities, (uint8_t*)args, MAX_ID_ITEMS * sizeof(uint64_t))
if (!*ptr){
*ptr = *dst = ++ctx->identity_counter;
return 1;
}else{
*dst = *ptr;
return 0;
}
}
static inline int reelfunc_id_uintptr_item(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t *lval, uint64_t v1){
uint64_t args[MAX_ID_ITEMS] = {v1, 0, 0, 0, 0, 0};
return reel_identity(ctx, lval, args);
}
static inline int reelfunc_id_uintptr_item_item(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t *lval, uint64_t v1, uint64_t v2){
uint64_t args[MAX_ID_ITEMS] = {v1, v2, 0, 0, 0, 0};
return reel_identity(ctx, lval, args);
}
static inline int reelfunc_id_uintptr_item_item_item(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t *lval, uint64_t v1, uint64_t v2, uint64_t v3){
uint64_t args[MAX_ID_ITEMS] = {v1, v2, v3, 0, 0, 0};
return reel_identity(ctx, lval, args);
}
static inline int reelfunc_id_uintptr_item_item_item_item(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t *lval, uint64_t v1, uint64_t v2, uint64_t v3, uint64_t v4){
uint64_t args[MAX_ID_ITEMS] = {v1, v2, v3, v4, 0, 0};
return reel_identity(ctx, lval, args);
}
static inline int reelfunc_id_uintptr_item_item_item_item_item(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t *lval, uint64_t v1, uint64_t v2, uint64_t v3, uint64_t v4, uint64_t v5){
uint64_t args[MAX_ID_ITEMS] = {v1, v2, v3, v4, v5, 0};
return reel_identity(ctx, lval, args);
}
static inline int reelfunc_id_uintptr_item_item_item_item_item_item(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t *lval, uint64_t v1, uint64_t v2, uint64_t v3, uint64_t v4, uint64_t v5, uint64_t v6){
uint64_t args[MAX_ID_ITEMS] = {v1, v2, v3, v4, v5, v6};
return reel_identity(ctx, lval, args);
}
/* time_before */
static inline int reelfunc_time_before_uintptr_uint(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t *lval, uint64_t rval){
return ev->timestamp <= *lval + rval;
}
static inline int reelfunc_time_before_uintptr_uintptr(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t *lval, uint64_t *rval){
return ev->timestamp <= *lval + *rval;
}
/* time_after */
static inline int reelfunc_time_after_uintptr_uint(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t *lval, uint64_t rval){
return ev->timestamp >= *lval + rval;
}
static inline int reelfunc_time_after_uintptr_uintptr(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t *lval, uint64_t *rval){
return ev->timestamp >= *lval + *rval;
}
static inline int reelfunc_time_after_uintptr(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t *val){
return ev->timestamp >= *val;
}
/* if_greater */
static inline int reelfunc_if_greater_uint_uintptr(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t lval, uint64_t *rval){
return lval > *rval;
}
static inline int reelfunc_if_greater_uintptr_uint(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t *lval, uint64_t rval){
return *lval > rval;
}
static inline int reelfunc_if_greater_or_equal_uint_uintptr(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t lval, uint64_t *rval){
return lval >= *rval;
}
static inline int reelfunc_if_greater_or_equal_uintptr_uint(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t *lval, uint64_t rval){
return *lval >= rval;
}
/* print */
static inline void reelfunc_print_uintptr(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t *val){
fprintf(stderr, "debug %lu\n", *val);
}
static inline void reelfunc_print_itemptr(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t *val){
fprintf(stderr, "debug %lu\n", *val);
}
static inline void reelfunc_print_item(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t val){
fprintf(stderr, "debug %lu\n", val);
}
static inline void reelfunc_print_uint(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t val){
fprintf(stderr, "debug %lu\n", val);
}
/* tables */
static int reel_init_table(reel_var *var, const tdb *db, const char *field_name)
{
uintptr_t *p;
if (tdb_get_field(db, field_name, &var->table_field)){
var->value = var->table_field = var->table_length = 0;
return 0;
}
var->table_length = tdb_lexicon_size(db, var->table_field);
if (!(p = calloc(1, var->table_length * sizeof(uintptr_t))))
return -1;
var->value = (uintptr_t)p;
return 0;
}
/* fork */
static reel_ctx *reel_clone(const reel_ctx *src,
tdb *db,
int do_reset,
int do_deep_copy)
{
uint64_t i;
reel_ctx *ctx;
if (!(ctx = malloc(sizeof(reel_ctx))))
return NULL;
memcpy(ctx, src, sizeof(reel_ctx));
ctx->trail_id = 0;
if (db)
ctx->db = db;
/* handle variables */
for (i = 0; i < sizeof(ctx->vars) / sizeof(reel_var); i++){
reel_var *v = &ctx->vars[i];
if (v->table_field){
/* const tables are shared */
if (!(v->flags & REEL_FLAG_IS_CONST)){
char *p;
if (!(p = calloc(v->table_length, sizeof(uintptr_t))))
goto out_of_mem;
v->value = (uintptr_t)p;
if (!do_reset)
memcpy(p,
(const char*)v->value,
v->table_length * sizeof(uintptr_t));
}
}else if (do_reset)
/* zero scalar variables */
v->value = 0;
}
/*
Make the new context a parent context.
This may be changed later (see below).
*/
ctx->root = ctx;
ctx->child_contexts = NULL;
ctx->evaluated_contexts = NULL;
if (do_deep_copy){
Word_t *ptr;
Word_t key = 0;
JLF(ptr, src->child_contexts, key);
while (ptr){
const reel_ctx *src_child = (const reel_ctx*)*ptr;
reel_ctx *dst_child = reel_clone(src_child, db, do_reset, 0);
if (!dst_child)
goto out_of_mem;
dst_child->root = ctx;
JLI(ptr, ctx->child_contexts, key);
*ptr = (Word_t)dst_child;
JLN(ptr, src->child_contexts, key);
}
}
return ctx;
out_of_mem:
/* XXX we leak some memory here, tables and children are not freed */
free(ctx);
return NULL;
}
static int reel_fork(reel_ctx *ctx, Word_t key)
{
int not_exists;
J1S(not_exists, ctx->root->evaluated_contexts, key);
if (!not_exists){
return 0;
}else{
Word_t *ptr;
JLI(ptr, ctx->root->child_contexts, key);
if (!*ptr){
reel_ctx *child = reel_clone(ctx, NULL, 1, 0);
if (!child){
ctx->error = REEL_FORK_FAILED;
return 0;
}
*ptr = (Word_t)child;
}
ctx->child = (reel_ctx*)*ptr;
return 1;
}
}
static inline int reelfunc_fork_item(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t val){
return reel_fork(ctx, (Word_t)val);
}
static inline int reelfunc_fork_uint(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t val){
return reel_fork(ctx, (Word_t)val);
}
static inline int reelfunc_fork_uintptr(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx, uint64_t *val){
return reel_fork(ctx, (Word_t)*val);
}
static inline void reelfunc_fork_reset_active(reel_ctx *ctx, const tdb_event *ev, uint32_t func_idx){
Word_t tmp;
J1FA(tmp, ctx->root->evaluated_contexts);
}
static int reel_get_forks(const reel_ctx *ctx, reel_ctx **ctxs, uint64_t *num_ctxs, uint64_t *ctxs_size)
{
Word_t num, key = 0;
Word_t *ptr;
JLC(num, ctx->child_contexts, 0, -1);
if (num > *ctxs_size){
if (!(ctxs = realloc(ctxs, num * sizeof(reel_ctx*))))
return -1;
*ctxs_size = num;
}
*num_ctxs = num;
num = 0;
JLF(ptr, ctx->child_contexts, key);
while (ptr){
ctxs[num++] = (reel_ctx*)*ptr;
JLN(ptr, ctx->child_contexts, key);
}
return 0;
}
/* utilities */
static tdb_item reel_resolve_item_literal(const tdb *db, const char *field_name, const char *value)
{
tdb_field field;
if (tdb_get_field(db, field_name, &field))
return 0;
return tdb_get_item(db, field, value, strlen(value));
}
static tdb_field reel_resolve_field(const tdb *db, const char *field_name)
{
tdb_field field;
if (tdb_get_field(db, field_name, &field))
return 0;
return field;
}