Boron 2.1.0
boron_internal.h
1#ifndef BORON_INTERNAL_H
2#define BORON_INTERNAL_H
3
4
5#include "env.h"
6
7#ifdef CONFIG_RANDOM
8#include "well512.h"
9#endif
10#ifdef CONFIG_ASSEMBLE
11#include <jit/jit.h>
12#endif
13
14
15#define MAX_OPT 8 // LIMIT: 8 options per func/cfunc.
16#define OPT_BITS(c) (c)->id._pad0
17
18#define PORT_DEVICE(dev,pbuf) \
19 UPortDevice* dev = (pbuf->form == UR_PORT_SIMPLE) ? \
20 (UPortDevice*) pbuf->ptr.v : \
21 (pbuf->ptr.v ? *((UPortDevice**) pbuf->ptr.v) : 0)
22
23#define PORT_SITE(dev,pbuf,portC) \
24 UBuffer* pbuf = ur_buffer(portC->port.buf); \
25 PORT_DEVICE(dev,pbuf)
26
27
28typedef struct
29{
30 UEnv env;
31 UBuffer ports;
32 UStatus (*funcRead)( UThread*, UCell*, UCell* );
33 uint16_t argStackSize;
34 uint16_t evalStackSize;
35 UAtom compileAtoms[5];
36 UAtom atomThreadPort;
37}
38BoronEnv;
39
40#define BENV ((BoronEnv*) ut->env)
41
42
43enum EvaluatorOpcodes
44{
45 EOP_NOP,
46 EOP_DO_BLOCK,
47 EOP_DO_BLOCK1,
48 EOP_FUNC_BODY,
49 EOP_SET,
50 EOP_REDUCE,
51 EOP_BLOCK_ITER,
52 EOP_CATCH,
53 EOP_RUN_RECURSE,
54 EOP_INVOKE_LOOP,
55 EOP_INVOKE,
56 EOP_OPTION_IT,
57 EOP_CALL_CFUNC,
58 EOP_CALL_FUNC,
59 EOP_WAIT
60};
61
62#define MASK_EOP_DO (1<<EOP_DO_BLOCK | 1<<EOP_DO_BLOCK1 | 1<<EOP_FUNC_BODY | 1<<EOP_REDUCE)
63#define MASK_EOP_CATCH (1<<EOP_CATCH | 1<<EOP_INVOKE_LOOP)
64#define MASK_EOP_FFLAGS (1<<EOP_DO_BLOCK | 1<<EOP_FUNC_BODY)
65
66
67struct EvalFrameBlock {
68 uint8_t eop;
69 uint8_t funcFlags;
70 uint16_t origStack;
71 UIndex codeBlk;
72 const UCell* it;
73 const UCell* end;
74 UCell* result;
75};
76
77struct EvalFrameReduce {
78 uint8_t eop;
79 uint8_t _pad;
80 uint16_t origStack;
81 UIndex codeBlk;
82 const UCell* it;
83 const UCell* end;
84 UIndex resBlk;
85};
86
87typedef union EvalFrame EvalFrame;
88
89typedef struct EvalFrameInvoke EvalFrameInvoke;
90
91struct EvalFrameInvoke {
92 uint8_t eop;
93 uint8_t state; // opCount for EOP_CATCH & EOP_INVOKE_LOOP
94 uint16_t origStack;
95 UIndex userBuf;
96 UStatus (*func)(UThread*, EvalFrameInvoke*);
97 union {
98 UStatus (*catchf)(UThread*, EvalFrame*);
99 void* ptr;
100 UIndex i;
101 } dat;
102 UCell* result;
103};
104
105enum DoBlock1State {
106 DO_BLOCK1_START,
107 DO_BLOCK1_VALUE,
108 DO_BLOCK1_COMPLETE
109};
110
111struct EvalFrameCall {
112 uint8_t eop;
113 uint8_t funcFlags; // For func! only
114 uint16_t origStack;
115 uint16_t argsPos;
116 int16_t tracePos; // Code block UCell index, cfunc! only.
117 const uint8_t* pc;
118 const UCell* funC;
119 UCell* result;
120};
121
122struct EvalFrameSet {
123 uint8_t eop;
124 const UCell* it;
125 const UCell* end;
126 UCell* result;
127};
128
129struct EvalFrameData {
130 uint8_t eop; // EOP_NOP
131 uint8_t state;
132 uint16_t origStack;
133 UIndex index;
134 union {
135 int32_t i32[6];
136 uint64_t u64[3];
137 void* ptr[3];
138 } var;
139};
140
141union EvalFrame {
142 struct EvalFrameBlock block;
143 struct EvalFrameReduce reduce;
144 struct EvalFrameInvoke invoke;
145 struct EvalFrameCall call;
146 struct EvalFrameSet set;
147 struct EvalFrameData data;
148};
149
150
151typedef struct BoronFibre BoronThread;
152
153struct BoronFibre
154{
155 UThread thread;
156 UBuffer tbin; // Temporary binary buffer.
157 int (*requestAccess)( UThread*, const char* );
158 UIndex stackLimit; // End of available UThread::stack.
159 UBuffer frames; // Function body & locals stack position.
160 UBuffer evalOp; // EvalFrame stack.
161 UCell optionCell;
162#ifdef CONFIG_FIBRE
163 BoronThread* nextFibre; // Circular, singly-linked list.
164 UCell* yieldResult;
165 UIndex waitPorts; // Block of port! values.
166 int64_t timeout;
167#endif
168#ifdef CONFIG_RANDOM
169 Well512 rand;
170#endif
171#ifdef CONFIG_ASSEMBLE
172 jit_context_t jit;
173 UAtomEntry* insTable;
174#endif
175};
176
177#define BT ((BoronThread*) ut)
178#define RESULT (BT->evalData + BT_RESULT)
179
180extern EvalFrame* boron_reuseFrame(UThread*, int extraFrames, int* keepStack);
181extern EvalFrame* boron_pushEvalFrame(UThread*);
182extern EvalFrame* boron_findEvalFrame(UThread*, int op);
183extern int boron_resetEvalFrame(UThread*, const EvalFrame*);
184extern void boron_initEvalCatch(EvalFrame* ef,
185 UStatus (*handler)(UThread*, EvalFrame*),
186 int origStack, UCell* result);
187extern void boron_initEvalBlock(EvalFrame*, UThread*, UIndex blkN, UCell*);
188extern int boron_reframeDoBlock1(UThread*, UIndex blkN,
189 UStatus (*valueFunc)(UThread*, EvalFrameInvoke*),
190 UCell* res);
191extern int boron_breakDoBlock1(UThread*, EvalFrameInvoke*);
192extern EvalFrame* boron_reframeReduce(UThread*, const UCell* a1, UCell* res,
193 UStatus (*complete)(UThread*, EvalFrameInvoke*));
194extern UStatus boron_yield(UThread*, UCell* res, UIndex portsBlkN, int timeout);
195extern UIndex boron_seriesEnd( UThread* ut, const UCell* cell );
196
197
198#endif // BORON_INTERNAL_H
The UBuffer struct holds information about a resource, usually a chunk of memory.
Definition urlan.h:266
The UThread struct stores the data specific to a thread of execution.
Definition urlan.h:309
A cell holds a single value of a simple type or a reference (often to a UBuffer) for a complex type.
Definition urlan.h:248
UStatus
Definition urlan.h:116
int32_t UIndex
This is an index into an array.
Definition urlan.h:150