forked from M-Labs/nac3
1
0
Fork 0

update kernel-only attribute annotation

Reviewed-on: M-Labs/nac3#127
Co-authored-by: ychenfo <yc@m-labs.hk>
Co-committed-by: ychenfo <yc@m-labs.hk>
This commit is contained in:
ychenfo 2021-12-19 11:04:53 +08:00 committed by sb10q
parent 7420ce185b
commit 91625dd327
4 changed files with 53 additions and 29 deletions

View File

@ -9,7 +9,7 @@ import nac3artiq
__all__ = [ __all__ = [
"KernelInvariant", "virtual", "Kernel", "KernelInvariant", "virtual",
"round64", "floor64", "ceil64", "round64", "floor64", "ceil64",
"extern", "kernel", "portable", "nac3", "extern", "kernel", "portable", "nac3",
"ms", "us", "ns", "ms", "us", "ns",
@ -21,6 +21,9 @@ __all__ = [
T = TypeVar('T') T = TypeVar('T')
class Kernel(Generic[T]):
pass
class KernelInvariant(Generic[T]): class KernelInvariant(Generic[T]):
pass pass

View File

@ -22,7 +22,7 @@ use parking_lot::{Mutex, RwLock};
use nac3core::{ use nac3core::{
codegen::{concrete_type::ConcreteTypeStore, CodeGenTask, WithCall, WorkerRegistry}, codegen::{concrete_type::ConcreteTypeStore, CodeGenTask, WithCall, WorkerRegistry},
symbol_resolver::SymbolResolver, symbol_resolver::SymbolResolver,
toplevel::{composer::TopLevelComposer, DefinitionId, GenCall, TopLevelDef}, toplevel::{composer::{TopLevelComposer, ComposerConfig}, DefinitionId, GenCall, TopLevelDef},
typecheck::typedef::{FunSignature, FuncArg}, typecheck::typedef::{FunSignature, FuncArg},
typecheck::{type_inferencer::PrimitiveStore, typedef::Type}, typecheck::{type_inferencer::PrimitiveStore, typedef::Type},
}; };
@ -239,7 +239,10 @@ impl Nac3 {
}))), }))),
), ),
]; ];
let (_, builtins_def, builtins_ty) = TopLevelComposer::new(builtins.clone()); let (_, builtins_def, builtins_ty) = TopLevelComposer::new(builtins.clone(), ComposerConfig {
kernel_ann: Some("Kernel"),
kernel_invariant_ann: "KernelInvariant"
});
let builtins_mod = PyModule::import(py, "builtins").unwrap(); let builtins_mod = PyModule::import(py, "builtins").unwrap();
let id_fn = builtins_mod.getattr("id").unwrap(); let id_fn = builtins_mod.getattr("id").unwrap();
@ -375,7 +378,10 @@ impl Nac3 {
filename: &str, filename: &str,
py: Python, py: Python,
) -> PyResult<()> { ) -> PyResult<()> {
let (mut composer, _, _) = TopLevelComposer::new(self.builtins.clone()); let (mut composer, _, _) = TopLevelComposer::new(self.builtins.clone(), ComposerConfig {
kernel_ann: Some("Kernel"),
kernel_invariant_ann: "KernelInvariant"
});
let mut id_to_def = HashMap::new(); let mut id_to_def = HashMap::new();
let mut id_to_type = HashMap::new(); let mut id_to_type = HashMap::new();

View File

@ -9,6 +9,20 @@ use crate::{
use super::*; use super::*;
pub struct ComposerConfig {
pub kernel_ann: Option<&'static str>,
pub kernel_invariant_ann: &'static str,
}
impl Default for ComposerConfig {
fn default() -> Self {
ComposerConfig {
kernel_ann: None,
kernel_invariant_ann: "Invariant"
}
}
}
type DefAst = (Arc<RwLock<TopLevelDef>>, Option<ast::Stmt<()>>); type DefAst = (Arc<RwLock<TopLevelDef>>, Option<ast::Stmt<()>>);
pub struct TopLevelComposer { pub struct TopLevelComposer {
// list of top level definitions, same as top level context // list of top level definitions, same as top level context
@ -25,11 +39,12 @@ pub struct TopLevelComposer {
pub method_class: HashMap<DefinitionId, DefinitionId>, pub method_class: HashMap<DefinitionId, DefinitionId>,
// number of built-in function and classes in the definition list, later skip // number of built-in function and classes in the definition list, later skip
pub builtin_num: usize, pub builtin_num: usize,
pub core_config: ComposerConfig,
} }
impl Default for TopLevelComposer { impl Default for TopLevelComposer {
fn default() -> Self { fn default() -> Self {
Self::new(vec![]).0 Self::new(vec![], Default::default()).0
} }
} }
@ -38,6 +53,7 @@ impl TopLevelComposer {
/// resolver can later figure out primitive type definitions when passed a primitive type name /// resolver can later figure out primitive type definitions when passed a primitive type name
pub fn new( pub fn new(
builtins: Vec<(StrRef, FunSignature, Arc<GenCall>)>, builtins: Vec<(StrRef, FunSignature, Arc<GenCall>)>,
core_config: ComposerConfig
) -> (Self, HashMap<StrRef, DefinitionId>, HashMap<StrRef, Type>) { ) -> (Self, HashMap<StrRef, DefinitionId>, HashMap<StrRef, Type>) {
let mut primitives = Self::make_primitives(); let mut primitives = Self::make_primitives();
let (mut definition_ast_list, builtin_name_list) = builtins::get_builtins(&mut primitives); let (mut definition_ast_list, builtin_name_list) = builtins::get_builtins(&mut primitives);
@ -108,6 +124,7 @@ impl TopLevelComposer {
keyword_list, keyword_list,
defined_names, defined_names,
method_class, method_class,
core_config,
}, },
builtin_id, builtin_id,
builtin_ty, builtin_ty,
@ -554,7 +571,7 @@ impl TopLevelComposer {
unifier, unifier,
primitives, primitives,
&mut type_var_to_concrete_def, &mut type_var_to_concrete_def,
&self.keyword_list, (&self.keyword_list, &self.core_config)
)? )?
} }
} }
@ -827,8 +844,9 @@ impl TopLevelComposer {
unifier: &mut Unifier, unifier: &mut Unifier,
primitives: &PrimitiveStore, primitives: &PrimitiveStore,
type_var_to_concrete_def: &mut HashMap<Type, TypeAnnotation>, type_var_to_concrete_def: &mut HashMap<Type, TypeAnnotation>,
keyword_list: &HashSet<StrRef>, core_info: (&HashSet<StrRef>, &ComposerConfig),
) -> Result<(), String> { ) -> Result<(), String> {
let (keyword_list, core_config) = core_info;
let mut class_def = class_def.write(); let mut class_def = class_def.write();
let ( let (
class_id, class_id,
@ -1058,20 +1076,17 @@ impl TopLevelComposer {
let dummy_field_type = unifier.get_fresh_var().0; let dummy_field_type = unifier.get_fresh_var().0;
// handle Kernel[T], KernelInvariant[T] // handle Kernel[T], KernelInvariant[T]
let (annotation, mutable) = { let (annotation, mutable) = match &annotation.node {
let mut result = None; ast::ExprKind::Subscript { value, slice, .. } if matches!(
if let ast::ExprKind::Subscript { value, slice, .. } = &annotation.as_ref().node { &value.node,
if let ast::ExprKind::Name { id, .. } = &value.node { ast::ExprKind::Name { id, .. } if id == &core_config.kernel_invariant_ann.into()
result = if id == &"Kernel".into() { ) => (slice, false),
Some((slice, true)) ast::ExprKind::Subscript { value, slice, .. } if matches!(
} else if id == &"KernelInvariant".into() { &value.node,
Some((slice, false)) ast::ExprKind::Name { id, .. } if core_config.kernel_ann.map_or(false, |c| id == &c.into())
} else { ) => (slice, true),
None _ if core_config.kernel_ann.is_none() => (annotation, true),
} _ => continue // ignore fields annotated otherwise
}
}
result.unwrap_or((annotation, true))
}; };
class_fields_def.push((*attr, dummy_field_type, mutable)); class_fields_def.push((*attr, dummy_field_type, mutable));

View File

@ -3,13 +3,10 @@ use inkwell::{
targets::*, targets::*,
OptimizationLevel, OptimizationLevel,
}; };
use nac3core::typecheck::{type_inferencer::PrimitiveStore, typedef::{Type, Unifier}}; use std::{borrow::Borrow, collections::HashMap, env, fs, path::Path, sync::Arc, time::SystemTime};
use nac3parser::{ast::{Expr, ExprKind, StmtKind}, parser};
use parking_lot::RwLock; use parking_lot::RwLock;
use std::{borrow::Borrow, env};
use std::fs;
use std::{collections::HashMap, path::Path, sync::Arc, time::SystemTime};
use nac3parser::{ast::{Expr, ExprKind, StmtKind}, parser};
use nac3core::{ use nac3core::{
codegen::{ codegen::{
concrete_type::ConcreteTypeStore, CodeGenTask, DefaultCodeGenerator, WithCall, concrete_type::ConcreteTypeStore, CodeGenTask, DefaultCodeGenerator, WithCall,
@ -17,11 +14,11 @@ use nac3core::{
}, },
symbol_resolver::SymbolResolver, symbol_resolver::SymbolResolver,
toplevel::{ toplevel::{
composer::TopLevelComposer, composer::{TopLevelComposer, ComposerConfig},
TopLevelDef, helper::parse_parameter_default_value, TopLevelDef, helper::parse_parameter_default_value,
type_annotation::*, type_annotation::*,
}, },
typecheck::typedef::FunSignature, typecheck::{type_inferencer::PrimitiveStore, typedef::{Type, Unifier, FunSignature}}
}; };
mod basic_symbol_resolver; mod basic_symbol_resolver;
@ -47,7 +44,10 @@ fn main() {
}; };
let primitive: PrimitiveStore = TopLevelComposer::make_primitives().0; let primitive: PrimitiveStore = TopLevelComposer::make_primitives().0;
let (mut composer, builtins_def, builtins_ty) = TopLevelComposer::new(vec![]); let (mut composer, builtins_def, builtins_ty) = TopLevelComposer::new(
vec![],
Default::default()
);
let internal_resolver: Arc<ResolverInternal> = ResolverInternal { let internal_resolver: Arc<ResolverInternal> = ResolverInternal {
id_to_type: builtins_ty.into(), id_to_type: builtins_ty.into(),