| @@ -0,0 +1,210 @@ |
| 1 | +//! The runtime's externs agree everywhere they are described (code-health |
| 2 | +//! H1): the table in `tilc_core::externs`, the standard library's |
| 3 | +//! `@extern` declarations, the Core that lowering produces, the |
| 4 | +//! interpreter, `libtilrt.a` and `tilrt.h`. Forgetting one of them when |
| 5 | +//! adding an extern fails here, rather than compiling into wrong code (a |
| 6 | +//! missing yield check) or failing at run time ("unknown extern"). |
| 7 | + |
| 8 | +mod common; |
| 9 | + |
| 10 | +use std::collections::BTreeSet; |
| 11 | +use std::path::{Path, PathBuf}; |
| 12 | +use std::process::Command; |
| 13 | + |
| 14 | +use common::build_runtime; |
| 15 | +use tilc_core::externs::{RUNTIME_EXTERNS, runtime_extern}; |
| 16 | +use tilc_core::ir::{Expr, Value}; |
| 17 | +use tilc_db::Db; |
| 18 | + |
| 19 | +const HEADER: &str = include_str!("../../../runtime/tilrt/include/tilrt.h"); |
| 20 | + |
| 21 | +/// Each `@extern` of the standard library: (module, symbol, blocking, |
| 22 | +/// owned). |
| 23 | +fn std_externs() -> Vec<(String, String, bool, bool)> { |
| 24 | + let analysis = Db::new().analyze(); |
| 25 | + analysis |
| 26 | + .hir |
| 27 | + .defs |
| 28 | + .iter() |
| 29 | + .filter_map(|(_, def)| { |
| 30 | + let ext = def.attrs.extern_.as_ref()?; |
| 31 | + let module = analysis.hir.modules[def.module].path.clone(); |
| 32 | + Some((module, ext.symbol.clone(), ext.blocking, ext.owned)) |
| 33 | + }) |
| 34 | + .collect() |
| 35 | +} |
| 36 | + |
| 37 | +/// The externs the Core of `tests/run`'s programs calls, std's included. |
| 38 | +fn called_externs() -> BTreeSet<String> { |
| 39 | + fn visit(e: &Expr, out: &mut BTreeSet<String>) { |
| 40 | + match e { |
| 41 | + Expr::Let(_, value, rest, _) => { |
| 42 | + match &**value { |
| 43 | + Value::CallExtern(sym, _) => { |
| 44 | + out.insert(sym.clone()); |
| 45 | + } |
| 46 | + Value::Block(inner) => visit(inner, out), |
| 47 | + _ => {} |
| 48 | + } |
| 49 | + visit(rest, out); |
| 50 | + } |
| 51 | + Expr::Switch { cases, default, .. } => { |
| 52 | + for (_, b) in cases { |
| 53 | + visit(b, out); |
| 54 | + } |
| 55 | + if let Some(d) = default { |
| 56 | + visit(d, out); |
| 57 | + } |
| 58 | + } |
| 59 | + Expr::SwitchLit { cases, default, .. } => { |
| 60 | + for (_, b) in cases { |
| 61 | + visit(b, out); |
| 62 | + } |
| 63 | + visit(default, out); |
| 64 | + } |
| 65 | + Expr::Dup(_, rest) | Expr::Drop(_, rest) | Expr::Release { rest, .. } => { |
| 66 | + visit(rest, out); |
| 67 | + } |
| 68 | + Expr::Return(_) | Expr::Panic(_) => {} |
| 69 | + } |
| 70 | + } |
| 71 | + let dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../tests/run"); |
| 72 | + let mut out = BTreeSet::new(); |
| 73 | + for entry in std::fs::read_dir(dir).unwrap() { |
| 74 | + let path = entry.unwrap().path(); |
| 75 | + if path.extension().is_none_or(|e| e != "til") { |
| 76 | + continue; |
| 77 | + } |
| 78 | + let mut db = Db::new(); |
| 79 | + let name = path.file_stem().unwrap().to_string_lossy().into_owned(); |
| 80 | + db.add_module( |
| 81 | + &name, |
| 82 | + &path.display().to_string(), |
| 83 | + std::fs::read_to_string(&path).unwrap(), |
| 84 | + ); |
| 85 | + let analysis = db.analyze(); |
| 86 | + let program = tilc_core::lower::lower(&analysis.hir, &analysis.typeck); |
| 87 | + for f in &program.fns { |
| 88 | + visit(&f.body, &mut out); |
| 89 | + } |
| 90 | + } |
| 91 | + out |
| 92 | +} |
| 93 | + |
| 94 | +#[test] |
| 95 | +fn std_externs_are_in_the_table_with_their_options() { |
| 96 | + let mut failures = Vec::new(); |
| 97 | + for (module, symbol, blocking, owned) in std_externs() { |
| 98 | + if !module.starts_with("std/") { |
| 99 | + continue; |
| 100 | + } |
| 101 | + match runtime_extern(&symbol) { |
| 102 | + None => failures.push(format!("{module}: `{symbol}` is not in tilc_core::externs")), |
| 103 | + Some(e) if (e.blocking, e.owned) != (blocking, owned) => failures.push(format!( |
| 104 | + "{module}: `{symbol}` is declared blocking={blocking}, owned={owned}, but the table says blocking={}, owned={}", |
| 105 | + e.blocking, e.owned |
| 106 | + )), |
| 107 | + Some(_) => {} |
| 108 | + } |
| 109 | + } |
| 110 | + assert!(failures.is_empty(), "{}", failures.join("\n")); |
| 111 | +} |
| 112 | + |
| 113 | +/// The table has the externs lowering calls itself (`++`), and only |
| 114 | +/// externs std declares or lowering calls (no stale entry). |
| 115 | +#[test] |
| 116 | +fn the_table_lists_the_externs_std_declares_or_lowering_calls() { |
| 117 | + let called = called_externs(); |
| 118 | + let missing: Vec<_> = called |
| 119 | + .iter() |
| 120 | + .filter(|s| s.starts_with("tl_") && runtime_extern(s).is_none()) |
| 121 | + .collect(); |
| 122 | + assert!( |
| 123 | + missing.is_empty(), |
| 124 | + "called but not in the table: {missing:?}" |
| 125 | + ); |
| 126 | + let declared: BTreeSet<String> = std_externs().into_iter().map(|(_, s, ..)| s).collect(); |
| 127 | + let stale: Vec<_> = RUNTIME_EXTERNS |
| 128 | + .iter() |
| 129 | + .filter(|e| !called.contains(e.symbol) && !declared.contains(e.symbol)) |
| 130 | + .map(|e| e.symbol) |
| 131 | + .collect(); |
| 132 | + assert!(stale.is_empty(), "neither declared nor called: {stale:?}"); |
| 133 | +} |
| 134 | + |
| 135 | +#[test] |
| 136 | +fn the_interpreter_implements_every_runtime_extern() { |
| 137 | + let missing: Vec<_> = RUNTIME_EXTERNS |
| 138 | + .iter() |
| 139 | + .filter(|e| !tilc_interp::implements_extern(e.symbol)) |
| 140 | + .map(|e| e.symbol) |
| 141 | + .collect(); |
| 142 | + assert!(missing.is_empty(), "not in the interpreter: {missing:?}"); |
| 143 | + assert!(!tilc_interp::implements_extern("tl_no_such_extern")); |
| 144 | +} |
| 145 | + |
| 146 | +/// The functions `tilrt.h` declares or defines (`tl_value tl_x(...)`), |
| 147 | +/// and those it defines `static inline`. |
| 148 | +fn header_functions() -> (BTreeSet<String>, BTreeSet<String>) { |
| 149 | + let mut declared = BTreeSet::new(); |
| 150 | + let mut inline = BTreeSet::new(); |
| 151 | + for line in HEADER.lines() { |
| 152 | + let line = line.trim_start(); |
| 153 | + let (is_inline, rest) = match line.strip_prefix("static inline ") { |
| 154 | + Some(rest) => (true, rest), |
| 155 | + None => (false, line), |
| 156 | + }; |
| 157 | + let Some(rest) = rest.strip_prefix("tl_value ") else { |
| 158 | + continue; |
| 159 | + }; |
| 160 | + let Some((name, _)) = rest.trim_start().split_once('(') else { |
| 161 | + continue; |
| 162 | + }; |
| 163 | + if name.starts_with("tl_") { |
| 164 | + declared.insert(name.to_owned()); |
| 165 | + if is_inline { |
| 166 | + inline.insert(name.to_owned()); |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + (declared, inline) |
| 171 | +} |
| 172 | + |
| 173 | +/// The functions `libtilrt.a` exports (`nm`). |
| 174 | +fn runtime_exports() -> BTreeSet<String> { |
| 175 | + build_runtime(); |
| 176 | + let exe = std::env::current_exe().unwrap(); |
| 177 | + let lib: PathBuf = exe |
| 178 | + .ancestors() |
| 179 | + .skip(1) |
| 180 | + .take(2) |
| 181 | + .map(|d| d.join("libtilrt.a")) |
| 182 | + .find(|p| p.is_file()) |
| 183 | + .expect("libtilrt.a next to the test binary"); |
| 184 | + let nm = Command::new("nm") |
| 185 | + .arg("--defined-only") |
| 186 | + .arg(&lib) |
| 187 | + .output() |
| 188 | + .expect("run nm"); |
| 189 | + assert!(nm.status.success(), "nm failed"); |
| 190 | + String::from_utf8_lossy(&nm.stdout) |
| 191 | + .lines() |
| 192 | + .filter_map(|l| Some(l.split_once(" T ")?.1.to_owned())) |
| 193 | + .collect() |
| 194 | +} |
| 195 | + |
| 196 | +#[test] |
| 197 | +fn the_runtime_exports_and_declares_every_runtime_extern() { |
| 198 | + let (declared, inline) = header_functions(); |
| 199 | + let exported = runtime_exports(); |
| 200 | + let mut failures = Vec::new(); |
| 201 | + for e in RUNTIME_EXTERNS { |
| 202 | + if !declared.contains(e.symbol) { |
| 203 | + failures.push(format!("`{}` is not declared in tilrt.h", e.symbol)); |
| 204 | + } |
| 205 | + if !inline.contains(e.symbol) && !exported.contains(e.symbol) { |
| 206 | + failures.push(format!("`{}` is not exported by libtilrt.a", e.symbol)); |
| 207 | + } |
| 208 | + } |
| 209 | + assert!(failures.is_empty(), "{}", failures.join("\n")); |
| 210 | +} |