From bd792904f90e8abb2cd56593798b255ea720f4b5 Mon Sep 17 00:00:00 2001 From: David Mak Date: Fri, 15 Dec 2023 14:02:30 +0800 Subject: [PATCH] core: Add size_t to primitive store Used for ndims in ndarray. --- nac3artiq/src/lib.rs | 15 ++++++++++++++- nac3core/src/codegen/mod.rs | 1 + nac3core/src/toplevel/composer.rs | 12 +++++------- nac3core/src/toplevel/helper.rs | 3 ++- nac3core/src/typecheck/type_inferencer/mod.rs | 12 ++++++++++++ nac3standalone/src/main.rs | 7 ++++--- 6 files changed, 38 insertions(+), 12 deletions(-) diff --git a/nac3artiq/src/lib.rs b/nac3artiq/src/lib.rs index 3e146842..9a0ad861 100644 --- a/nac3artiq/src/lib.rs +++ b/nac3artiq/src/lib.rs @@ -63,6 +63,17 @@ enum Isa { CortexA9, } +impl Isa { + /// Returns the number of bits in `size_t` for the [`Isa`]. + fn get_size_type(&self) -> u32 { + if self == &Isa::Host { + 64u32 + } else { + 32u32 + } + } +} + #[derive(Clone)] pub struct PrimitivePythonId { int: u64, @@ -277,9 +288,11 @@ impl Nac3 { py: Python, link_fn: &dyn Fn(&Module) -> PyResult, ) -> PyResult { + let size_t = self.isa.get_size_type(); let (mut composer, mut builtins_def, mut builtins_ty) = TopLevelComposer::new( self.builtins.clone(), ComposerConfig { kernel_ann: Some("Kernel"), kernel_invariant_ann: "KernelInvariant" }, + size_t, ); let builtins = PyModule::import(py, "builtins")?; @@ -792,7 +805,7 @@ impl Nac3 { Isa::RiscV32IMA => &timeline::NOW_PINNING_TIME_FNS, Isa::CortexA9 | Isa::Host => &timeline::EXTERN_TIME_FNS, }; - let primitive: PrimitiveStore = TopLevelComposer::make_primitives().0; + let primitive: PrimitiveStore = TopLevelComposer::make_primitives(isa.get_size_type()).0; let builtins = vec![ ( "now_mu".into(), diff --git a/nac3core/src/codegen/mod.rs b/nac3core/src/codegen/mod.rs index 51496e69..41bd2a82 100644 --- a/nac3core/src/codegen/mod.rs +++ b/nac3core/src/codegen/mod.rs @@ -614,6 +614,7 @@ pub fn gen_func_impl<'ctx, G: CodeGenerator, F: FnOnce(&mut G, &mut CodeGenConte str: unifier.get_representative(primitives.str), exception: unifier.get_representative(primitives.exception), option: unifier.get_representative(primitives.option), + ..primitives }; let mut type_cache: HashMap<_, _> = [ diff --git a/nac3core/src/toplevel/composer.rs b/nac3core/src/toplevel/composer.rs index 8e612a63..48f7c1ac 100644 --- a/nac3core/src/toplevel/composer.rs +++ b/nac3core/src/toplevel/composer.rs @@ -37,12 +37,8 @@ pub struct TopLevelComposer { // number of built-in function and classes in the definition list, later skip pub builtin_num: usize, pub core_config: ComposerConfig, -} - -impl Default for TopLevelComposer { - fn default() -> Self { - Self::new(vec![], ComposerConfig::default()).0 - } + /// The size of a native word on the target platform. + pub size_t: u32, } impl TopLevelComposer { @@ -52,8 +48,9 @@ impl TopLevelComposer { pub fn new( builtins: Vec<(StrRef, FunSignature, Arc)>, core_config: ComposerConfig, + size_t: u32, ) -> (Self, HashMap, HashMap) { - let mut primitives = Self::make_primitives(); + let mut primitives = Self::make_primitives(size_t); let mut definition_ast_list = builtins::get_builtins(&mut primitives); let primitives_ty = primitives.0; let mut unifier = primitives.1; @@ -146,6 +143,7 @@ impl TopLevelComposer { defined_names, method_class, core_config, + size_t, }, builtin_id, builtin_ty, diff --git a/nac3core/src/toplevel/helper.rs b/nac3core/src/toplevel/helper.rs index 0f4d3755..8e440c18 100644 --- a/nac3core/src/toplevel/helper.rs +++ b/nac3core/src/toplevel/helper.rs @@ -44,7 +44,7 @@ impl TopLevelDef { impl TopLevelComposer { #[must_use] - pub fn make_primitives() -> (PrimitiveStore, Unifier) { + pub fn make_primitives(size_t: u32) -> (PrimitiveStore, Unifier) { let mut unifier = Unifier::new(); let int32 = unifier.add_ty(TypeEnum::TObj { obj_id: DefinitionId(0), @@ -144,6 +144,7 @@ impl TopLevelComposer { str, exception, option, + size_t, }; unifier.put_primitive_store(&primitives); crate::typecheck::magic_methods::set_primitives_magic_methods(&primitives, &mut unifier); diff --git a/nac3core/src/typecheck/type_inferencer/mod.rs b/nac3core/src/typecheck/type_inferencer/mod.rs index 0b9b5a0c..c4360765 100644 --- a/nac3core/src/typecheck/type_inferencer/mod.rs +++ b/nac3core/src/typecheck/type_inferencer/mod.rs @@ -41,6 +41,18 @@ pub struct PrimitiveStore { pub str: Type, pub exception: Type, pub option: Type, + pub size_t: u32, +} + +impl PrimitiveStore { + /// Returns a [Type] representing `size_t`. + pub fn usize(&self) -> Type { + match self.size_t { + 32 => self.uint32, + 64 => self.uint64, + _ => unreachable!(), + } + } } pub struct FunctionData { diff --git a/nac3standalone/src/main.rs b/nac3standalone/src/main.rs index eb746a8a..8973cf31 100644 --- a/nac3standalone/src/main.rs +++ b/nac3standalone/src/main.rs @@ -286,6 +286,7 @@ fn main() { // The default behavior for -O where n>3 defaults to O3 for both Clang and GCC _ => OptimizationLevel::Aggressive, }; + const SIZE_T: u32 = 64; let program = match fs::read_to_string(file_name.clone()) { Ok(program) => program, @@ -295,9 +296,9 @@ fn main() { } }; - let primitive: PrimitiveStore = TopLevelComposer::make_primitives().0; + let primitive: PrimitiveStore = TopLevelComposer::make_primitives(SIZE_T).0; let (mut composer, builtins_def, builtins_ty) = - TopLevelComposer::new(vec![], ComposerConfig::default()); + TopLevelComposer::new(vec![], ComposerConfig::default(), SIZE_T); let internal_resolver: Arc = ResolverInternal { id_to_type: builtins_ty.into(), @@ -400,7 +401,7 @@ fn main() { membuffer.lock().push(buffer); }))); let threads = (0..threads) - .map(|i| Box::new(DefaultCodeGenerator::new(format!("module{i}"), 64))) + .map(|i| Box::new(DefaultCodeGenerator::new(format!("module{i}"), SIZE_T))) .collect(); let (registry, handles) = WorkerRegistry::create_workers(threads, top_level, &llvm_options, &f); registry.add_task(task);