logicaffeine_compile/codegen_tt/
wrap.rs1use super::runtime::{TT_CMAKE_TEMPLATE, TT_HOST_TEMPLATE, TT_KERNEL_PREAMBLE};
6use super::{TtArtifact, KERNEL_FILE_NAME};
7use crate::codegen_c::runtime::C_GC_COLLECTOR;
8
9const C_MAIN_SIG: &str = "int main(int argc, char **argv) {";
12const KERNEL_MAIN_SIG: &str = "static int logos_main(int argc, char **argv) {";
13
14pub fn wrap_as_tt(oracle_c: &str) -> TtArtifact {
19 let device_c = oracle_c.replace(C_GC_COLLECTOR, "");
22 let body = strip_system_includes(&device_c).replace(C_MAIN_SIG, KERNEL_MAIN_SIG);
23
24 let kernel_cpp = format!(
25 "{preamble}{body}\n\nvoid kernel_main() {{\n (void) logos_main(0, (char **)0);\n}}\n",
26 preamble = TT_KERNEL_PREAMBLE,
27 body = body,
28 );
29
30 let host_cpp = TT_HOST_TEMPLATE.replace("{KERNEL_FILE}", KERNEL_FILE_NAME);
31 let cmake = TT_CMAKE_TEMPLATE.replace("{KERNEL_FILE}", KERNEL_FILE_NAME);
32
33 TtArtifact {
34 kernel_cpp,
35 host_cpp,
36 cmake,
37 oracle_c: oracle_c.to_string(),
38 }
39}
40
41fn strip_system_includes(c: &str) -> String {
45 let mut out = String::with_capacity(c.len());
46 for line in c.lines() {
47 let t = line.trim_start();
48 if t.starts_with("#include <") && t.ends_with('>') {
49 continue;
50 }
51 out.push_str(line);
52 out.push('\n');
53 }
54 out
55}
56
57#[cfg(test)]
58#[path = "../../../logicaffeine_tests/unit/logicaffeine_compile/codegen_tt/wrap/tests.rs"]
59mod tests;