swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "lisp.h" |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 4 | #include <dasm_proto.h> |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 5 | #include <stdbool.h> |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 6 | #include <stdint.h> |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 7 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 8 | enum namespace |
| 9 | { |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 10 | /// A function |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 11 | NS_FUNCTION, |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 12 | /// A macro |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 13 | NS_MACRO, |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 14 | /// An anonymous function (a lambda/closure) |
| 15 | NS_ANONYMOUS, |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 16 | }; |
| 17 | |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 18 | struct function |
| 19 | { |
| 20 | char *name; |
| 21 | int nargs; // number of arguments |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 22 | enum namespace namespace; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 23 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 24 | union |
| 25 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 26 | value_t (*def0)(); |
| 27 | value_t (*def1)(value_t); |
| 28 | value_t (*def2)(value_t, value_t); |
| 29 | value_t (*def3)(value_t, value_t, value_t); |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 30 | void *code_ptr; |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 31 | uintptr_t code_addr; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | struct function *prev; |
| 35 | }; |
| 36 | |
| 37 | struct environment |
| 38 | { |
| 39 | struct function *first; |
| 40 | }; |
| 41 | |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 42 | enum var_type |
| 43 | { |
| 44 | V_BOUND, // Bound local variable |
| 45 | V_ARGUMENT, // Bound function argument |
| 46 | V_GLOBAL, // Global variable |
| 47 | V_FREE // Free (lexical) variable |
| 48 | }; |
| 49 | |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 50 | struct variable |
| 51 | { |
| 52 | char *name; |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 53 | uintptr_t number; |
| 54 | enum var_type type; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 55 | struct variable *prev; |
| 56 | }; |
| 57 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 58 | /// Local environment |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 59 | struct local |
| 60 | { |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 61 | /// Parent environment, NULL if none (root). |
| 62 | struct local *parent; |
| 63 | |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame^] | 64 | /// Name that the current function should be referred to by, e.g. `recurse` |
| 65 | /// for a lambda. |
| 66 | char *current_function_name; |
| 67 | |
| 68 | int num_vars, num_args; |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 69 | /// Most recently defined variable |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 70 | struct variable *first; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 71 | int npc; |
| 72 | int nextpc; |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 73 | bool *stack_slots; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 74 | /// Number of slots allocated in `stack_slots` |
| 75 | int num_stack_slots; |
| 76 | /// Number of entries used in `stack_slots` |
| 77 | int num_stack_entries; |
| 78 | /// Number of closure slots total (allocated as V_FREE variables) |
| 79 | int num_closure_slots; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 82 | void compile_expression(struct environment *env, struct local *local, |
| 83 | value_t val, dasm_State **Dst); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 84 | |
| 85 | /** |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 86 | * Compile a function |
| 87 | * @param args The function args and body, e.g. `((b c) d)` |
| 88 | * @param namespace The function namespace. |
| 89 | * @param env The environment. |
| 90 | * @param local_out The local environment generated for this function will be |
| 91 | * returned here. NULL if you do not care about it being returned (you probably |
| 92 | * should since you need to free the stack slot allocation map). |
| 93 | * @param local_parent Parent local environment, only needed for closures. NULL |
| 94 | * if no parent. |
| 95 | * @param nargs The number of arguments for this function will be returned here. |
| 96 | * NULL if you don't care about it. |
| 97 | * @returns The compiled function state. You should probably give this to |
| 98 | * `add_function` or something similar. |
| 99 | */ |
| 100 | struct dasm_State *compile_function(value_t args, enum namespace namespace, |
| 101 | struct environment *env, struct local *local_out, |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame^] | 102 | struct local *local_parent, int *nargs, char *name); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 103 | |
| 104 | void compile_variable(struct variable *v, dasm_State *Dst); |
| 105 | |
| 106 | /** |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 107 | * Compile a backquoted expression |
| 108 | */ |
| 109 | void compile_backquote(struct environment *env, struct local *local, |
| 110 | value_t val, dasm_State **Dst); |
| 111 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 112 | void compile_expr_to_func(struct environment *env, char *name, value_t val, |
| 113 | dasm_State **Dst); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 114 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 115 | int nextpc(struct local *local, dasm_State **Dst); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 116 | |
| 117 | // Local utilities |
| 118 | unsigned int local_alloc(struct local *local); |
| 119 | void local_free(struct local *local, unsigned int slot); |
| 120 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 121 | /** |
| 122 | * Walk `body` and reserve space in `local` for any variable declarations. |
| 123 | */ |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 124 | void walk_and_alloc(struct local *local, value_t body); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 125 | // Compile top-level declaration |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 126 | void compile_tl(value_t val, struct environment *env); |
| 127 | struct environment compile_all(struct istream *is); |
| 128 | struct function *find_function(struct environment *env, char *name); |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 129 | struct variable *add_variable(struct local *local, enum var_type type, |
| 130 | char *name, int number); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * Find a variable in `local` with name `name`. |
| 134 | * @returns The variable, NULL if not found. |
| 135 | */ |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 136 | struct variable *find_variable(struct local *local, char *name); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 137 | |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 138 | void destroy_local(struct local *local); |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 139 | |
| 140 | /** |
| 141 | * Like `apply` in lisp, calls func with list args and returns the result. |
| 142 | */ |
| 143 | value_t call_list(struct function *func, value_t list); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 144 | value_t call_list_closure(struct closure *c, value_t list); |