blob: 465c9fba555513f30aeb644dad567562749d3d02 [file] [log] [blame]
swissChilica107a02021-04-14 12:07:30 -07001#pragma once
2
3#include "lisp.h"
swissChilif3e7f182021-04-20 13:57:22 -07004#include <dasm_proto.h>
swissChilica107a02021-04-14 12:07:30 -07005#include <stdbool.h>
swissChilib3ca4fb2021-04-20 10:33:00 -07006#include <stdint.h>
swissChilica107a02021-04-14 12:07:30 -07007
8struct function
9{
10 char *name;
11 int nargs; // number of arguments
12
13 union {
swissChili53472e82021-05-08 16:06:32 -070014 value_t (*def0)();
15 value_t (*def1)(value_t);
16 value_t (*def2)(value_t, value_t);
17 value_t (*def3)(value_t, value_t, value_t);
swissChilica107a02021-04-14 12:07:30 -070018 void *code_ptr;
swissChilif3e7f182021-04-20 13:57:22 -070019 uintptr_t code_addr;
swissChilica107a02021-04-14 12:07:30 -070020 };
21
22 struct function *prev;
23};
24
25struct environment
26{
27 struct function *first;
28};
29
swissChili923b5362021-05-09 20:31:43 -070030enum var_type
31{
32 V_BOUND, // Bound local variable
33 V_ARGUMENT, // Bound function argument
34 V_GLOBAL, // Global variable
35 V_FREE // Free (lexical) variable
36};
37
swissChilica107a02021-04-14 12:07:30 -070038struct variable
39{
40 char *name;
swissChili923b5362021-05-09 20:31:43 -070041 uintptr_t number;
42 enum var_type type;
swissChilica107a02021-04-14 12:07:30 -070043 struct variable *prev;
44};
45
46// local environment
47struct local
48{
swissChilica107a02021-04-14 12:07:30 -070049 int num_vars;
50 struct variable *first;
swissChili53472e82021-05-08 16:06:32 -070051 int npc;
52 int nextpc;
swissChili67bdf282021-06-06 18:46:08 -070053 bool *stack_slots;
54 int num_stack_slots, num_stack_entries;
swissChilica107a02021-04-14 12:07:30 -070055};
56
swissChili53472e82021-05-08 16:06:32 -070057void compile_expression(struct environment *env, struct local *local,
58 value_t val, dasm_State **Dst);
59void compile_expr_to_func(struct environment *env, char *name, value_t val,
60 dasm_State **Dst);
61int nextpc(struct local *local, dasm_State **Dst);
swissChili67bdf282021-06-06 18:46:08 -070062
63// Local utilities
64unsigned int local_alloc(struct local *local);
65void local_free(struct local *local, unsigned int slot);
66
67void walk_and_alloc(struct local *local, value_t body);
swissChili8fc5e2f2021-04-22 13:45:10 -070068// Compile top-level declaration
swissChili53472e82021-05-08 16:06:32 -070069void compile_tl(value_t val, struct environment *env);
70struct environment compile_all(struct istream *is);
71struct function *find_function(struct environment *env, char *name);
swissChili923b5362021-05-09 20:31:43 -070072struct variable *add_variable(struct local *local, enum var_type type,
73 char *name, int number);
74// Might return null
75struct variable *find_variable(struct local *local, char *name);
76void destroy_local(struct local *local);