-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
rt_types.h
475 lines (409 loc) · 12.8 KB
/
rt_types.h
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
/* Copyright 2012-2020 Dustin DeWeese
This file is part of PoprC.
PoprC is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PoprC 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with PoprC. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __RT_TYPES__
#define __RT_TYPES__
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#include <time.h>
#include "startle/macros.h"
#include "startle/types.h"
#include "macros.h"
typedef unsigned int uint;
typedef uint16_t csize_t;
// base types
typedef enum __attribute__((packed)) type_t {
T_ANY = 0, /**< type variable */
T_INT,
T_LIST, /**< a list/quote */
T_SYMBOL,
T_MAP,
T_STRING,
T_RETURN, /**< a list of return values, trace ONLY */
T_FLOAT,
T_OPAQUE,
T_MODULE, /**< module definition ONLY */
T_BOTTOM, /**< the uninhabited type, or unknown */
T_FAIL // TODO use T_BOTTOM
} type_t;
// declare parts of a cell_t
typedef union cell cell_t;
typedef struct expr expr_t;
typedef struct value value_t;
typedef struct tok_list tok_list_t;
typedef struct entry entry_t;
typedef struct mem mem_t;
typedef struct context context_t;
typedef struct tcell tcell_t;
typedef uintptr_t alt_set_t;
typedef int16_t refcount_t;
typedef intptr_t val_t;
typedef uintptr_t attr_t;
#ifdef __clang__
#pragma clang diagnostic ignored "-Warray-bounds"
#pragma clang diagnostic ignored "-Wzero-length-array"
#pragma clang diagnostic ignored "-Wgnu-anonymous-struct"
#pragma clang diagnostic ignored "-Wnested-anon-types"
#pragma clang diagnostic ignored "-Wgnu-folding-constant"
#pragma clang diagnostic ignored "-Wgnu-empty-initializer"
#endif
// quote size info
typedef struct qsize {
csize_t in, out;
} qsize_t;
// define the file_id enum
#define FILE_ID(id, str) \
FILE_ID_##id,
typedef enum __attribute__((packed)) file_id {
FILE_ID_NONE = 0,
#include "file_ids.h"
FILE_ID_COUNT
} file_id_t;
// location within PoprC source code
typedef union location {
uintptr_t raw;
struct {
file_id_t file;
uint16_t line;
};
} location_t;
#undef FILE_ID
static_assert(sizeof(location_t) == sizeof_field(location_t, raw), "location_t wrong size");
typedef enum __attribute__((packed)) priority {
PRIORITY_SIMPLIFY = 0,
PRIORITY_VAR,
PRIORITY_ASSERT,
PRIORITY_DELAY,
PRIORITY_EXEC_SELF,
PRIORITY_UNLESS,
PRIORITY_MAX
} priority_t;
#define PRIORITY_TOP (PRIORITY_MAX - 1)
static_assert(sizeof(priority_t) == 1, "priority_t too big");
// context used for reduction
// allows both downward and upward information transfer
struct context {
context_t *up; // outer context [down]
cell_t **src; // closure for this context [down]
range_t bound; // requested bound [down]
alt_set_t alt_set; // outside constraints [up/down]
seg_t text; // text from source [up]
priority_t priority; // used to control reduction stages [down]
location_t loc; // reported location (for debug) [up]
qsize_t s; // required quote size [down]
type_t t; // required type [down]
uint8_t depth; // limits nesting depth [down]
uint8_t flags; // see below [up/down]
};
#define CONTEXT_RETRY 0x01
#define CONTEXT_INV 0x02
#define CONTEXT_SEQ 0x04
#define CONTEXT_REDUCE_LISTS 0x08
#define CONTEXT_DOWN (CONTEXT_INV | CONTEXT_SEQ | CONTEXT_REDUCE_LISTS)
typedef enum response {
SUCCESS = 0, // continue reduction
DELAY, // halt reduction for this priority
RETRY, // trampoline
FAIL // abort and mark failure
} response;
#define TR_FINAL 0x01
// trace argument
typedef union tr {
cell_t *ptr;
int entry;
struct __attribute__((packed)) {
int16_t index;
uint8_t flags;
};
} tr;
#define FLAG_expr (expr, ., EXPR)
#define EXPR_NEEDS_ARG 0x02
#define EXPR_TRACE 0x04
#define EXPR_PARTIAL 0x10
#define EXPR_RECURSIVE 0x20 // exec
#define EXPR_NO_UNIFY 0x40 // exec
#define EXPR_ROW 0x80 // placeholder
/* unevaluated expression */
struct __attribute__((packed)) expr {
uint8_t out, flags;
union {
cell_t *arg[2];
val_t idx[2];
struct {
cell_t *arg0; // padding
alt_set_t alt_set;
};
};
};
// value flags
#define FLAG_value (value, ., VALUE)
#define VALUE_ABBREV 0x01
#define VALUE_TRACED 0x02
#define VALUE_INLINE 0x04
#define VALUE_DEP 0x08
#define VALUE_BOUNDED 0x10
#define VALUE_LINEAR 0x20
#define VALUE_ROW 0x40
#define VALUE_VAR 0x80
/* reduced value */
struct __attribute__((packed)) value {
type_t type;
uint8_t flags;
alt_set_t alt_set;
union {
struct {
union {
tcell_t *var; /* variable */
attr_t attributes; /* for definitions */
};
union {
val_t integer; /* integer */
double flt; /* float */
char str[0]; /* string */
cell_t *ptr[2]; /* list */
range_t range; /* range */
struct { /* symbol | opaque */
val_t symbol;
union {
val_t id;
void *opaque;
};
};
};
};
pair_t map[1]; /* map */
};
};
// attributes
#define ATTR_HIDE 0x00000001
/* token list */
struct __attribute__((packed)) tok_list {
csize_t length;
const char *location, *line;
cell_t *next;
uintptr_t attributes;
};
/* unallocated memory */
struct __attribute__((packed)) mem {
csize_t __padding;
cell_t *prev, *next;
location_t loc; // for debug
};
#define FLAG_specialize (specialize, ->, SPECIALIZE)
#define SPECIALIZE_UNWRAPPED 0x01
#define SPECIALIZE_ROW 0x02
typedef struct specialize_data {
cell_t *initial;
cell_t *expand;
tcell_t *entry;
uintptr_t dep_mask;
uint8_t flags;
} specialize_data;
#define FLAG_entry (entry, ., ENTRY)
#define ENTRY_PRIMITIVE 0x0001
#define ENTRY_TRACE 0x0002
#define ENTRY_SYNC 0x0004
#define ENTRY_QUOTE 0x0008
#define ENTRY_PARTIAL 0x0010
#define ENTRY_RAM 0x0020
#define ENTRY_BLOCK 0x0040
#define ENTRY_COMPLETE 0x0080
#define ENTRY_RECURSIVE 0x0100
#define ENTRY_MUTUAL 0x0200
#define ENTRY_FORCED_INLINE 0x0400
#define ENTRY_STACK 0x0800
#define ENTRY_RETURN_ADDR 0x1000
#define ENTRY_ROW 0x2000
/* word entry */
struct __attribute__((packed)) entry {
uint16_t flags;
uint8_t alts, sub_id;
csize_t in, out, len;
tcell_t *parent;
union {
specialize_data *specialize;
tcell_t *compact;
};
};
typedef enum char_class_t {
CC_NONE,
CC_NUMERIC,
CC_FLOAT,
CC_ALPHA,
CC_SYMBOL,
CC_BRACKET,
CC_VAR,
CC_COMMENT,
CC_DOT,
CC_STRING,
CC_COMMA
} char_class_t;
// define the op enum
#define OP__ITEM(file, line, name) \
OP_##name,
typedef enum __attribute__((packed)) op {
OP__ITEM(FILEBASE, __LINE__, null)
#include "op_list.h"
OP_COUNT
} op;
#undef OP__ITEM
union cell {
/* op indicates the type:
* OP_null -> mem
* OP_value -> value
* otherwise -> expr
*/
uintptr_t c[10];
#define CELL_STRUCT_CONTENTS \
struct { \
union { \
cell_t *alt; \
const char *word_name; /* entry */ \
}; \
union { \
cell_t *tmp; \
val_t tmp_val; \
const char *module_name; /* entry */ \
char_class_t char_class; /* tok_list */ \
}; \
enum op op; \
seg_t src; \
union { \
uint8_t pos; /* see below */ \
uint8_t arg_index; /* arg index (for dep vars) */ \
uint8_t var_index; /* final index for vars in trace */ \
priority_t priority; /* used in func_list() & delay_branch() */ \
}; \
refcount_t n; \
csize_t size; \
union { \
expr_t expr; \
value_t value; \
tok_list_t tok_list; \
entry_t entry; \
mem_t mem; \
}; \
}
/* end of CELL_STRUCT_CONTENTS */
CELL_STRUCT_CONTENTS;
};
/* An explanation of pos:
*
* pos is used to mark the boundary between entries within the graph.
* Dependent vars should be created within the entry indicated by pos,
* even if they otherwise would not be, while dependencies should *not*
* be created within the entry, and switch_entry is used to lift vars.
*
* For expressions, this means that setting pos will prevent them from
* escaping, because they follow their dependencies.
* For values, this will lift them out, because they follow their consumer.
*/
#define VALUE_OFFSET(f) ((offsetof(cell_t, value.f) - offsetof(cell_t, expr.arg)) / sizeof(cell_t *))
#ifndef EMSCRIPTEN
static_assert(sizeof(cell_t) == sizeof_field(cell_t, c), "cell_t wrong size");
#endif
static_assert(sizeof(tr) == sizeof_field(cell_t, expr.arg[0]), "tr wrong size");
#define ASSERT_ALIAS(s, f0, f1) \
static_assert(offsetof(s, f0) == offsetof(s, f1), #f0 " should alias " #f1 " in " #s)
#define ASSERT_VALUE_OFFSET(f) ASSERT_ALIAS(cell_t, expr.arg[VALUE_OFFSET(f)], value.f)
ASSERT_VALUE_OFFSET(integer);
ASSERT_VALUE_OFFSET(flt);
ASSERT_VALUE_OFFSET(str);
ASSERT_VALUE_OFFSET(ptr);
ASSERT_VALUE_OFFSET(map);
ASSERT_ALIAS(cell_t, expr.flags, value.flags);
ASSERT_ALIAS(cell_t, expr.arg[1], expr.alt_set);
static_assert(offsetof(cell_t, c) == 0, "offset of cell_t.c should be 0");
typedef struct stats_t {
int reduce_cnt, fail_cnt, alloc_cnt, max_alloc_cnt, trace_cnt;
clock_t start, stop;
uint8_t alt_cnt;
} stats_t;
#ifdef EMSCRIPTEN
#define strnlen(s, n) strlen(s)
#endif
// Maximum number of alts
#define AS_SIZE (sizeof(alt_set_t) * 4)
#define AS_MASK ((alt_set_t)0x5555555555555555)
#define ALT_SET_IDS AS_SIZE
#define SYM_False 0
#define SYM_True 1
#define SYM_IO 2
#define SYM_Dict 3
#define SYM_Something 4
#define SYM_Array 5
#define SYM_File 6
#define PERSISTENT ((refcount_t)-15)
#define PRIMITIVE_MODULE_PREFIX __primitive
#define PRIMITIVE_MODULE_NAME STRINGIFY(PRIMITIVE_MODULE_PREFIX)
typedef struct list_iterator {
cell_t **array;
csize_t index, size;
bool row;
} list_iterator_t;
void breakpoint();
#define COMMAND(name, desc) void command_##name(UNUSED cell_t *rest)
// trace flags
#define FLAG_trace (trace, ., TRACE)
#define TRACE_INCOMPLETE 0x0001
#define TRACE_TRACED 0x0002
#define TRACE_USED 0x0004
#define TRACE_IMMEDIATE 0x0008
#define TRACE_CHANGES 0x0010
#define TRACE_DECL 0x0020
#define TRACE_NO_SKIP 0x0040
#define TRACE_JUMP 0x0080
// extra data about functions stored in the trace
typedef struct trace {
union {
range_t range;
uint8_t symbol_set[sizeof(range_t)];
uintptr_t first_return;
};
uint16_t flags;
union {
csize_t prev_cells;
csize_t extension;
};
uint32_t hash;
uint16_t bit_width;
uint8_t addr_width;
type_t type;
} trace_t;
struct tcell {
trace_t trace;
union { // inline fields so that they can be accessed as in a cell_t
CELL_STRUCT_CONTENTS;
cell_t c;
};
};
typedef struct scratch {
range_t range;
} scratch_t;
ASSERT_ALIAS(tcell_t, c.alt, alt);
ASSERT_ALIAS(tcell_t, c.tmp, tmp);
ASSERT_ALIAS(tcell_t, c.op, op);
ASSERT_ALIAS(tcell_t, c.pos, pos);
ASSERT_ALIAS(tcell_t, c.n, n);
ASSERT_ALIAS(tcell_t, c.size, size);
ASSERT_ALIAS(tcell_t, c.expr, expr);
struct file;
typedef struct mmap_array {
uintptr_t id;
size_t size;
unsigned int width;
struct file *file;
char *data;
} mmap_array_t;
#endif