| @@ -0,0 +1,175 @@ |
| 1 | +//! The runtime's externs: the `tilrt` functions the standard library |
| 2 | +//! declares with `@extern(c, "tl_...")`, or lowering calls, and what the |
| 3 | +//! compiler must know about each. |
| 4 | +//! |
| 5 | +//! This table is the single list of them. The native backend reads it to |
| 6 | +//! decide which calls need a yield check ([`may_unwind`]) and which |
| 7 | +//! symbols `tilrt.h` declares; the interpreter must implement every one. |
| 8 | +//! `tilc_codegen_c/tests/externs.rs` checks that the table, the standard |
| 9 | +//! library, the interpreter, `libtilrt.a` and `tilrt.h` agree, so adding a |
| 10 | +//! runtime extern without updating one of them fails a test. |
| 11 | +//! |
| 12 | +//! **Adding a runtime extern** `tl_foo`: declare it in `std/<module>.til`, |
| 13 | +//! export it from `tilrt` (`#[unsafe(no_mangle)] extern "C"`), declare it |
| 14 | +//! in `tilrt.h`, implement it in the interpreter (`tilc_interp`), and add |
| 15 | +//! it here, marked `may_unwind` if it may panic or park the actor. |
| 16 | +//! |
| 17 | +//! The prefix `tl_` is reserved for the runtime (ADR 0015). An extern that |
| 18 | +//! is not in the table belongs to a binding, and may always unwind. |
| 19 | + |
| 20 | +/// What the compiler knows about a runtime extern. |
| 21 | +#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 22 | +pub struct Extern { |
| 23 | + /// Its C symbol. |
| 24 | + pub symbol: &'static str, |
| 25 | + /// It may panic, or park the actor (which may be stopped meanwhile): |
| 26 | + /// generated code tests `ctx->yielding` after the call. |
| 27 | + pub may_unwind: bool, |
| 28 | + /// It runs on the blocking pool while the actor waits (ADR 0005). Its |
| 29 | + /// std declaration must say `blocking`. |
| 30 | + pub blocking: bool, |
| 31 | + /// It consumes its arguments instead of borrowing them (ADR 0014). Its |
| 32 | + /// std declaration must say `owned`. |
| 33 | + pub owned: bool, |
| 34 | +} |
| 35 | + |
| 36 | +impl Extern { |
| 37 | + const fn new(symbol: &'static str) -> Extern { |
| 38 | + Extern { |
| 39 | + symbol, |
| 40 | + may_unwind: false, |
| 41 | + blocking: false, |
| 42 | + owned: false, |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + const fn unwinds(self) -> Extern { |
| 47 | + Extern { |
| 48 | + may_unwind: true, |
| 49 | + ..self |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + const fn owned(self) -> Extern { |
| 54 | + Extern { |
| 55 | + owned: true, |
| 56 | + ..self |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +const fn ext(symbol: &'static str) -> Extern { |
| 62 | + Extern::new(symbol) |
| 63 | +} |
| 64 | + |
| 65 | +/// Every runtime extern, by std module. |
| 66 | +pub const RUNTIME_EXTERNS: &[Extern] = &[ |
| 67 | + // std/prelude |
| 68 | + ext("tl_panic").unwinds(), |
| 69 | + ext("tl_int_eq"), |
| 70 | + ext("tl_int_compare"), |
| 71 | + ext("tl_int_add"), |
| 72 | + ext("tl_int_sub"), |
| 73 | + ext("tl_int_mul"), |
| 74 | + ext("tl_int_div").unwinds(), |
| 75 | + ext("tl_int_rem").unwinds(), |
| 76 | + ext("tl_int_show"), |
| 77 | + ext("tl_float_eq"), |
| 78 | + ext("tl_float_compare"), |
| 79 | + ext("tl_float_add"), |
| 80 | + ext("tl_float_sub"), |
| 81 | + ext("tl_float_mul"), |
| 82 | + ext("tl_float_div"), |
| 83 | + ext("tl_float_rem"), |
| 84 | + ext("tl_float_show"), |
| 85 | + ext("tl_string_eq"), |
| 86 | + ext("tl_string_compare"), |
| 87 | + ext("tl_bytes_eq"), |
| 88 | + ext("tl_bytes_compare"), |
| 89 | + ext("tl_hash_int"), |
| 90 | + ext("tl_hash_float"), |
| 91 | + ext("tl_hash_string"), |
| 92 | + ext("tl_hash_bytes"), |
| 93 | + ext("tl_hash_combine"), |
| 94 | + ext("tl_string_debug"), |
| 95 | + ext("tl_bytes_debug"), |
| 96 | + // std/io |
| 97 | + ext("tl_println"), |
| 98 | + ext("tl_print"), |
| 99 | + ext("tl_eprintln"), |
| 100 | + // std/string |
| 101 | + ext("tl_string_length"), |
| 102 | + ext("tl_string_join"), |
| 103 | + ext("tl_string_starts_with"), |
| 104 | + ext("tl_string_ends_with"), |
| 105 | + ext("tl_string_contains"), |
| 106 | + ext("tl_string_byte_length"), |
| 107 | + ext("tl_string_to_lower"), |
| 108 | + ext("tl_string_trim"), |
| 109 | + ext("tl_string_split"), |
| 110 | + ext("tl_string_parse_int"), |
| 111 | + // std/bytes |
| 112 | + ext("tl_bytes_length"), |
| 113 | + ext("tl_bytes_at"), |
| 114 | + ext("tl_bytes_slice"), |
| 115 | + ext("tl_bytes_concat"), |
| 116 | + ext("tl_bytes_concat_all"), |
| 117 | + ext("tl_bytes_from_string"), |
| 118 | + ext("tl_bytes_to_string"), |
| 119 | + ext("tl_bytes_to_string_lossy"), |
| 120 | + ext("tl_bytes_from_list").unwinds(), |
| 121 | + ext("tl_bytes_to_list"), |
| 122 | + ext("tl_bytes_builder"), |
| 123 | + ext("tl_bytes_append").owned(), |
| 124 | + ext("tl_bytes_append_string").owned(), |
| 125 | + ext("tl_bytes_append_byte").owned().unwinds(), |
| 126 | + ext("tl_bytes_build").owned(), |
| 127 | + // std/array |
| 128 | + ext("tl_array_with_capacity"), |
| 129 | + ext("tl_array_from_list"), |
| 130 | + ext("tl_array_to_list"), |
| 131 | + ext("tl_array_length"), |
| 132 | + ext("tl_array_get"), |
| 133 | + ext("tl_array_set").owned().unwinds(), |
| 134 | + ext("tl_array_push").owned(), |
| 135 | + ext("tl_array_slice"), |
| 136 | + ext("tl_array_at").unwinds(), |
| 137 | + // std/html |
| 138 | + ext("tl_html_escape"), |
| 139 | + // std/process |
| 140 | + ext("tl_coerce"), |
| 141 | + ext("tl_pid_eq"), |
| 142 | + // std/random |
| 143 | + ext("tl_random_int").unwinds(), |
| 144 | + ext("tl_random_token"), |
| 145 | + // std/net: the blocking operations park the actor. |
| 146 | + ext("tl_net_listen"), |
| 147 | + ext("tl_net_port"), |
| 148 | + ext("tl_net_accept").unwinds(), |
| 149 | + ext("tl_net_connect").unwinds(), |
| 150 | + ext("tl_net_read").unwinds(), |
| 151 | + ext("tl_net_write").unwinds(), |
| 152 | + ext("tl_net_write_string").unwinds(), |
| 153 | + ext("tl_net_close"), |
| 154 | + // std/http |
| 155 | + ext("tl_http_parse_request"), |
| 156 | + ext("tl_http_parse_chunk"), |
| 157 | + ext("tl_url_decode"), |
| 158 | + // std/os |
| 159 | + ext("tl_os_env"), |
| 160 | + // Called by lowering only: `++` on strings. |
| 161 | + ext("tl_string_concat"), |
| 162 | +]; |
| 163 | + |
| 164 | +/// The runtime extern `symbol`, if it is one. |
| 165 | +pub fn runtime_extern(symbol: &str) -> Option<&'static Extern> { |
| 166 | + RUNTIME_EXTERNS.iter().find(|e| e.symbol == symbol) |
| 167 | +} |
| 168 | + |
| 169 | +/// Whether a call of the extern `symbol` may start unwinding (ADR 0003, |
| 170 | +/// Revision 2): a runtime extern if the table says so; a `blocking` one |
| 171 | +/// (it parks the actor); and any extern of a binding, which may panic or |
| 172 | +/// call back into tilcayo (ADR 0015). |
| 173 | +pub fn may_unwind(symbol: &str, blocking: bool) -> bool { |
| 174 | + blocking || runtime_extern(symbol).is_none_or(|e| e.may_unwind) |
| 175 | +} |