LispBM
eval_cps.h
Go to the documentation of this file.
1 /*
2  Copyright 2018, 2020, 2021, 2022 Joel Svensson svenssonjoel@yahoo.se
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
18 #ifndef EVAL_CPS_H_
19 #define EVAL_CPS_H_
20 
21 #include "lbm_types.h"
22 #include "stack.h"
23 #include "lbm_channel.h"
24 #include "lbm_flat_value.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #define EVAL_CPS_STATE_NONE 0
31 #define EVAL_CPS_STATE_PAUSED 1
32 #define EVAL_CPS_STATE_RUNNING 2
33 #define EVAL_CPS_STATE_KILL 4
34 #define EVAL_CPS_STATE_DEAD 8
35 #define EVAL_CPS_STATE_RESET 16
36 
37 #define EVAL_CPS_DEFAULT_MAILBOX_SIZE 10
38 
39 // Make sure the flags fit in an u28. (do not go beyond 27 flags)
40 #define EVAL_CPS_CONTEXT_FLAG_NOTHING (uint32_t)0x00
41 #define EVAL_CPS_CONTEXT_FLAG_TRAP (uint32_t)0x01
42 #define EVAL_CPS_CONTEXT_FLAG_CONST (uint32_t)0x02
43 #define EVAL_CPS_CONTEXT_FLAG_CONST_SYMBOL_STRINGS (uint32_t)0x04
44 #define EVAL_CPS_CONTEXT_FLAG_INCREMENTAL_READ (uint32_t)0x08
45 #define EVAL_CPS_CONTEXT_FLAG_TRAP_UNROLL_RETURN (uint32_t)0x10
46 #define EVAL_CPS_CONTEXT_READER_FLAGS_MASK (EVAL_CPS_CONTEXT_FLAG_CONST | EVAL_CPS_CONTEXT_FLAG_CONST_SYMBOL_STRINGS | EVAL_CPS_CONTEXT_FLAG_INCREMENTAL_READ)
47 
51 #define LBM_THREAD_STATE_READY (uint32_t)0
52 #define LBM_THREAD_STATE_BLOCKED (uint32_t)1
53 #define LBM_THREAD_STATE_TIMEOUT (uint32_t)2
54 #define LBM_THREAD_STATE_SLEEPING (uint32_t)4
55 #define LBM_THREAD_STATE_RECV_BL (uint32_t)8
56 #define LBM_THREAD_STATE_RECV_TO (uint32_t)16
57 #define LBM_THREAD_STATE_GC_BIT (uint32_t)(1 << 31)
58 
59 #define LBM_IS_STATE_TIMEOUT(X) (X & (LBM_THREAD_STATE_TIMEOUT | LBM_THREAD_STATE_RECV_TO))
60 #define LBM_IS_STATE_WAKE_UP_WAKABLE(X) (X & (LBM_THREAD_STATE_SLEEPING | LBM_IS_STATE_TIMEOUT(X)))
61 #define LBM_IS_STATE_UNBLOCKABLE(X) (X & (LBM_THREAD_STATE_BLOCKED | LBM_THREAD_STATE_TIMEOUT))
62 #define LBM_IS_STATE_RECV(X) (X & (LBM_THREAD_STATE_RECV_BL | LBM_THREAD_STATE_RECV_TO))
63 typedef struct eval_context_s{
67  lbm_value *mailbox; /* Message passing mailbox */
68  uint32_t mailbox_size;
69  uint32_t num_mail; /* Number of messages in mailbox */
70  uint32_t flags;
72  char *error_reason;
73  bool app_cont;
77  uint32_t state;
78  char *name;
81  /* while reading */
84  /* List structure */
88 
89 typedef enum {
95 
96 typedef struct {
101 } lbm_event_t;
102 
105 
106 extern const fundamental_fun fundamental_table[];
107 
114 typedef void (*ctx_fun)(eval_context_t *, void*, void*);
115 
116 /* Common interface */
122 
123 /* Concurrent interface */
128 int lbm_eval_init(void);
133 void lbm_set_eval_step_quota(uint32_t quota);
138 bool lbm_eval_init_events(unsigned int num_events);
150 bool lbm_event_handler_exists(void);
160 bool lbm_event_run_user_callback(void *arg);
171 bool lbm_event(lbm_flat_value_t *fv);
176 bool lbm_event_unboxed(lbm_value unboxed);
180 bool lbm_event_queue_is_empty(void);
189 
203 lbm_cid lbm_eval_program_ext(lbm_value lisp, unsigned int stack_size);
204 
208 void lbm_run_eval(void);
215 void lbm_reset_eval(void);
220 void lbm_pause_eval(void);
225 void lbm_pause_eval_with_gc(uint32_t num_free);
229 void lbm_continue_eval(void);
233 void lbm_kill_eval(void);
238 uint32_t lbm_get_eval_state(void);
249 void lbm_set_error_reason(char *error_str);
254 void lbm_set_error_suspect(lbm_value suspect);
258 void lbm_critical_error(void);
260 void lbm_set_user_callback(void (*fptr)(void *));
262 void lbm_set_critical_error_callback(void (*fptr)(void));
271 lbm_cid lbm_create_ctx(lbm_value program, lbm_value env, lbm_uint stack_size, char *name);
294 bool lbm_unblock_ctx_r(lbm_cid cid);
302 bool lbm_unblock_ctx_unboxed(lbm_cid cid, lbm_value unboxed);
309 void lbm_running_iterator(ctx_fun f, void*, void*);
316 void lbm_blocked_iterator(ctx_fun f, void*, void*);
319 void lbm_toggle_verbose(void);
324 void lbm_set_verbose(bool verbose);
329 void lbm_set_usleep_callback(void (*fptr)(uint32_t));
334 void lbm_set_timestamp_us_callback(uint32_t (*fptr)(void));
340 void lbm_set_ctx_done_callback(void (*fptr)(eval_context_t *));
346 void lbm_set_printf_callback(int (*prnt)(const char*, ...));
350 void lbm_set_dynamic_load_callback(bool (*fptr)(const char *, const char **));
356 
362 
368 bool lbm_mailbox_change_size(eval_context_t *ctx, lbm_uint new_size);
369 
370 bool create_string_channel(char *str, lbm_value *res);
371 
373 
375  //bool lift_array_flash(lbm_value flash_cell, char *data, lbm_uint num_elt);
376 
390 int lbm_perform_gc(void);
394 void lbm_request_gc(void);
395 #ifdef __cplusplus
396 }
397 #endif
398 #endif
lbm_set_verbose
void lbm_set_verbose(bool verbose)
Definition: eval_cps.c:423
eval_context_s::K
lbm_stack_t K
Definition: eval_cps.h:74
lbm_eval_program_ext
lbm_cid lbm_eval_program_ext(lbm_value lisp, unsigned int stack_size)
Definition: eval_cps.c:5234
lbm_unblock_ctx_r
bool lbm_unblock_ctx_r(lbm_cid cid)
Definition: eval_cps.c:1342
lbm_eval_init
int lbm_eval_init(void)
Definition: eval_cps.c:5238
lbm_perform_gc
int lbm_perform_gc(void)
Definition: eval_cps.c:1584
lbm_critical_error
void lbm_critical_error(void)
Definition: eval_cps.c:1059
lbm_event_t::buf_len
lbm_uint buf_len
Definition: eval_cps.h:100
eval_context_s::state
uint32_t state
Definition: eval_cps.h:77
eval_context_s::curr_exp
lbm_value curr_exp
Definition: eval_cps.h:65
lbm_set_user_callback
void lbm_set_user_callback(void(*fptr)(void *))
Definition: eval_cps.c:270
eval_context_s::row1
lbm_int row1
Definition: eval_cps.h:83
lbm_uint
uint32_t lbm_uint
Definition: lbm_types.h:48
eval_context_s::sleep_us
lbm_uint sleep_us
Definition: eval_cps.h:76
lbm_run_eval
void lbm_run_eval(void)
Definition: eval_cps.c:5147
lbm_eval_program
lbm_cid lbm_eval_program(lbm_value lisp)
Definition: eval_cps.c:5230
LBM_EVENT_FOR_HANDLER
@ LBM_EVENT_FOR_HANDLER
Definition: eval_cps.h:90
eval_context_s::error_reason
char * error_reason
Definition: eval_cps.h:72
lbm_request_gc
void lbm_request_gc(void)
Definition: eval_cps.c:197
lbm_event_unboxed
bool lbm_event_unboxed(lbm_value unboxed)
Definition: eval_cps.c:355
eval_context_s::mailbox_size
uint32_t mailbox_size
Definition: eval_cps.h:68
lbm_get_eval_state
uint32_t lbm_get_eval_state(void)
Definition: eval_cps.c:5061
lbm_channel.h
lbm_set_timestamp_us_callback
void lbm_set_timestamp_us_callback(uint32_t(*fptr)(void))
Definition: eval_cps.c:285
lbm_event_t::buf_ptr
lbm_uint buf_ptr
Definition: eval_cps.h:99
lbm_event
bool lbm_event(lbm_flat_value_t *fv)
Definition: eval_cps.c:368
lbm_get_current_cid
lbm_cid lbm_get_current_cid(void)
Definition: eval_cps.c:427
lbm_event_define
bool lbm_event_define(lbm_value key, lbm_flat_value_t *fv)
Definition: eval_cps.c:347
eval_context_s::timestamp
lbm_uint timestamp
Definition: eval_cps.h:75
fundamental_table
const fundamental_fun fundamental_table[]
Definition: fundamental.c:1383
lbm_get_event_handler_pid
lbm_cid lbm_get_event_handler_pid(void)
Definition: eval_cps.c:314
LBM_EVENT_UNBLOCK_CTX
@ LBM_EVENT_UNBLOCK_CTX
Definition: eval_cps.h:91
lbm_set_error_suspect
void lbm_set_error_suspect(lbm_value suspect)
Definition: eval_cps.c:987
lbm_stack_t
Definition: stack.h:33
eval_context_s::name
char * name
Definition: eval_cps.h:78
lbm_event_t::parameter
lbm_uint parameter
Definition: eval_cps.h:98
lbm_event_type_t
lbm_event_type_t
Definition: eval_cps.h:89
lbm_kill_eval
void lbm_kill_eval(void)
Definition: eval_cps.c:5056
eval_context_s::flags
uint32_t flags
Definition: eval_cps.h:70
lbm_set_error_reason
void lbm_set_error_reason(char *error_str)
Definition: eval_cps.c:992
lbm_pause_eval
void lbm_pause_eval(void)
Definition: eval_cps.c:5039
create_string_channel
bool create_string_channel(char *str, lbm_value *res)
Definition: eval_cps.c:826
request_flash_storage_cell
lbm_flash_status request_flash_storage_cell(lbm_value val, lbm_value *res)
Definition: eval_cps.c:4436
lbm_char_channel_s
Definition: lbm_channel.h:68
lbm_find_receiver_and_send
lbm_value lbm_find_receiver_and_send(lbm_cid cid, lbm_value msg)
Definition: eval_cps.c:1412
lbm_reset_eval
void lbm_reset_eval(void)
Definition: eval_cps.c:5033
eval_context_s::mailbox
lbm_value * mailbox
Definition: eval_cps.h:67
lbm_undo_block_ctx_from_extension
void lbm_undo_block_ctx_from_extension(void)
Definition: eval_cps.c:1405
lbm_get_current_context
eval_context_t * lbm_get_current_context(void)
Definition: eval_cps.c:434
lbm_remove_done_ctx
int lbm_remove_done_ctx(lbm_cid cid, lbm_value *v)
eval_context_s::prev
struct eval_context_s * prev
Definition: eval_cps.h:85
lbm_flat_value_t
Definition: lbm_flat_value.h:25
lift_char_channel
bool lift_char_channel(lbm_char_channel_t *ch, lbm_value *res)
Definition: eval_cps.c:853
lbm_set_printf_callback
void lbm_set_printf_callback(int(*prnt)(const char *,...))
Definition: eval_cps.c:295
LBM_EVENT_RUN_USER_CALLBACK
@ LBM_EVENT_RUN_USER_CALLBACK
Definition: eval_cps.h:93
lbm_block_ctx_from_extension
void lbm_block_ctx_from_extension(void)
Definition: eval_cps.c:1398
lbm_flash_status
lbm_flash_status
Definition: heap.h:194
lbm_event_queue_is_empty
bool lbm_event_queue_is_empty(void)
Definition: eval_cps.c:388
lbm_block_ctx_from_extension_timeout
void lbm_block_ctx_from_extension_timeout(float s)
Definition: eval_cps.c:1394
eval_context_s::app_cont
bool app_cont
Definition: eval_cps.h:73
eval_context_s::id
lbm_cid id
Definition: eval_cps.h:79
eval_context_s::r
lbm_value r
Definition: eval_cps.h:71
eval_context_t
struct eval_context_s eval_context_t
ctx_fun
void(* ctx_fun)(eval_context_t *, void *, void *)
Definition: eval_cps.h:114
lbm_flat_value.h
lbm_types.h
lbm_pause_eval_with_gc
void lbm_pause_eval_with_gc(uint32_t num_free)
Definition: eval_cps.c:5045
lbm_set_usleep_callback
void lbm_set_usleep_callback(void(*fptr)(uint32_t))
Definition: eval_cps.c:280
eval_context_s::curr_env
lbm_value curr_env
Definition: eval_cps.h:66
eval_cps_get_env
lbm_value eval_cps_get_env(void)
lbm_set_critical_error_callback
void lbm_set_critical_error_callback(void(*fptr)(void))
Definition: eval_cps.c:275
lbm_running_iterator
void lbm_running_iterator(ctx_fun f, void *, void *)
Definition: eval_cps.c:876
eval_context_s
Definition: eval_cps.h:63
eval_context_s::row0
lbm_int row0
Definition: eval_cps.h:82
lbm_event_run_user_callback
bool lbm_event_run_user_callback(void *arg)
Definition: eval_cps.c:351
eval_context_s::num_mail
uint32_t num_mail
Definition: eval_cps.h:69
lbm_toggle_verbose
void lbm_toggle_verbose(void)
Definition: eval_cps.c:419
lbm_event_handler_exists
bool lbm_event_handler_exists(void)
Definition: eval_cps.c:322
lbm_event_t::type
lbm_event_type_t type
Definition: eval_cps.h:97
lbm_value
uint32_t lbm_value
Definition: lbm_types.h:44
lbm_int
int32_t lbm_int
Definition: lbm_types.h:49
lbm_event_t
Definition: eval_cps.h:96
lbm_unblock_ctx_unboxed
bool lbm_unblock_ctx_unboxed(lbm_cid cid, lbm_value unboxed)
Definition: eval_cps.c:1360
lbm_set_dynamic_load_callback
void lbm_set_dynamic_load_callback(bool(*fptr)(const char *, const char **))
Definition: eval_cps.c:300
lbm_set_event_handler_pid
void lbm_set_event_handler_pid(lbm_cid pid)
Definition: eval_cps.c:318
lbm_continue_eval
void lbm_continue_eval(void)
Definition: eval_cps.c:5051
eval_context_s::next
struct eval_context_s * next
Definition: eval_cps.h:86
lbm_cid
int32_t lbm_cid
Definition: lbm_types.h:64
lbm_unblock_ctx
bool lbm_unblock_ctx(lbm_cid cid, lbm_flat_value_t *fv)
Definition: eval_cps.c:1338
lbm_eval_init_events
bool lbm_eval_init_events(unsigned int num_events)
Definition: eval_cps.c:5271
LBM_EVENT_DEFINE
@ LBM_EVENT_DEFINE
Definition: eval_cps.h:92
lbm_set_ctx_done_callback
void lbm_set_ctx_done_callback(void(*fptr)(eval_context_t *))
Definition: eval_cps.c:290
eval_context_s::program
lbm_value program
Definition: eval_cps.h:64
lbm_mailbox_change_size
bool lbm_mailbox_change_size(eval_context_t *ctx, lbm_uint new_size)
Definition: eval_cps.c:1278
lbm_set_eval_step_quota
void lbm_set_eval_step_quota(uint32_t quota)
Definition: eval_cps.c:221
fundamental_fun
lbm_value(* fundamental_fun)(lbm_value *, lbm_uint, eval_context_t *)
Definition: eval_cps.h:104
eval_context_s::parent
lbm_cid parent
Definition: eval_cps.h:80
lbm_create_ctx
lbm_cid lbm_create_ctx(lbm_value program, lbm_value env, lbm_uint stack_size, char *name)
Definition: eval_cps.c:1268
stack.h
lbm_blocked_iterator
void lbm_blocked_iterator(ctx_fun f, void *, void *)
Definition: eval_cps.c:882