From dbb88ad79d487f9da0a4d479b4bf3f9c7077975a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Bourdeauducq?= Date: Wed, 11 Sep 2024 15:22:50 +0800 Subject: [PATCH] support multiple cells --- .cargo/config.toml | 4 ++-- src/main.rs | 23 +++++++++++++++-------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index d8ec41e..ccb524c 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,5 +1,5 @@ [target.x86_64-unknown-openbsd] -rustflags = [ "-C", "link-args=-Wl,--export-dynamic-symbol=__nac3_personality", "-C", "link-args=-Wl,-z,nobtcfi" ] +rustflags = [ "-C", "link-args=-Wl,--export-dynamic-symbol=__nac3_*", "-C", "link-args=-Wl,-z,nobtcfi" ] [target.x86_64-unknown-linux-gnu] -rustflags = [ "-C", "link-args=-Wl,--export-dynamic-symbol=__nac3_personality" ] +rustflags = [ "-C", "link-args=-Wl,--export-dynamic-symbol=__nac3_*" ] diff --git a/src/main.rs b/src/main.rs index 030cfd0..883f92c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,7 +18,7 @@ use nac3core::typecheck::{type_inferencer, typedef}; mod basic_symbol_resolver; use basic_symbol_resolver::{Resolver, ResolverInternal}; -fn compile(code: &String, output_filename: &Path) -> Result<(), String> { +fn compile(code: &String, run_symbol: &String, output_filename: &Path) -> Result<(), String> { let mut target_machine_options = codegen::CodeGenTargetMachineOptions::from_host(); target_machine_options.reloc_mode = inkwell::targets::RelocMode::PIC; let llvm_options = codegen::CodeGenLLVMOptions { @@ -59,7 +59,12 @@ fn compile(code: &String, output_filename: &Path) -> Result<(), String> { return Err(format!("parse error: {}", err)); } }; - for stmt in parser_result { + for mut stmt in parser_result { + if let nac3parser::ast::StmtKind::FunctionDef { name, .. } = &mut stmt.node { + if *name == "run".into() { + *name = run_symbol.as_str().into(); + } + } match composer.register_top_level(stmt, Some(resolver.clone()), "__main__", true) { Ok((name, def_id, ty)) => { internal_resolver.add_id_def(name, def_id); @@ -98,7 +103,7 @@ fn compile(code: &String, output_filename: &Path) -> Result<(), String> { let top_level = Arc::new(composer.make_top_level_context()); - let run_id_def = match resolver.get_identifier_def("run".into()) { + let run_id_def = match resolver.get_identifier_def(run_symbol.as_str().into()) { Ok(run_id_def) => run_id_def, Err(_) => { return Err(format!("no run() entry point")); @@ -115,13 +120,13 @@ fn compile(code: &String, output_filename: &Path) -> Result<(), String> { else { unreachable!() }; - instance_to_symbol.insert(String::new(), "run".to_string()); + instance_to_symbol.insert(String::new(), run_symbol.clone()); instance_to_stmt[""].clone() }; let task = codegen::CodeGenTask { subst: Vec::default(), - symbol_name: "run".to_string(), + symbol_name: run_symbol.clone(), body: instance.body, signature, resolver, @@ -180,7 +185,8 @@ fn compile(code: &String, output_filename: &Path) -> Result<(), String> { // Private all functions except "run" let mut function_iter = main.get_first_function(); while let Some(func) = function_iter { - if func.count_basic_blocks() > 0 && func.get_name().to_str().unwrap() != "run" { + println!("{}", func.get_name().to_str().unwrap()); + if func.count_basic_blocks() > 0 && func.get_name().to_str().unwrap() != run_symbol { func.set_linkage(inkwell::module::Linkage::Private); } function_iter = func.get_next_function(); @@ -244,7 +250,8 @@ impl CellBin { assert!(self.library.is_none()); let object = self.directory.path().join("module.o"); let library = self.directory.path().join("module.so"); - compile(code, &object)?; + let run_symbol = format!("__cells_run_{}", self.cell_id); + compile(code, &run_symbol, &object)?; link_with_lld(&library, &object)?; unsafe { self.library = Some(libloading::Library::new(library).or_else(|e| Err(e.to_string()))?); @@ -252,7 +259,7 @@ impl CellBin { .library .as_ref() .unwrap() - .get::(b"run") + .get::(run_symbol.as_bytes()) .unwrap() .try_as_raw_ptr() .unwrap();