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>
escape-analysis
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__ = [
"KernelInvariant", "virtual",
"Kernel", "KernelInvariant", "virtual",
"round64", "floor64", "ceil64",
"extern", "kernel", "portable", "nac3",
"ms", "us", "ns",
@ -21,6 +21,9 @@ __all__ = [
T = TypeVar('T')
class Kernel(Generic[T]):
pass
class KernelInvariant(Generic[T]):
pass

View File

@ -22,7 +22,7 @@ use parking_lot::{Mutex, RwLock};
use nac3core::{
codegen::{concrete_type::ConcreteTypeStore, CodeGenTask, WithCall, WorkerRegistry},
symbol_resolver::SymbolResolver,
toplevel::{composer::TopLevelComposer, DefinitionId, GenCall, TopLevelDef},
toplevel::{composer::{TopLevelComposer, ComposerConfig}, DefinitionId, GenCall, TopLevelDef},
typecheck::typedef::{FunSignature, FuncArg},
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 id_fn = builtins_mod.getattr("id").unwrap();
@ -375,7 +378,10 @@ impl Nac3 {
filename: &str,
py: Python,
) -> 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_type = HashMap::new();

View File

@ -9,6 +9,20 @@ use crate::{
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<()>>);
pub struct TopLevelComposer {
// list of top level definitions, same as top level context
@ -25,11 +39,12 @@ pub struct TopLevelComposer {
pub method_class: HashMap<DefinitionId, DefinitionId>,
// 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![]).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
pub fn new(
builtins: Vec<(StrRef, FunSignature, Arc<GenCall>)>,
core_config: ComposerConfig
) -> (Self, HashMap<StrRef, DefinitionId>, HashMap<StrRef, Type>) {
let mut primitives = Self::make_primitives();
let (mut definition_ast_list, builtin_name_list) = builtins::get_builtins(&mut primitives);
@ -108,6 +124,7 @@ impl TopLevelComposer {
keyword_list,
defined_names,
method_class,
core_config,
},
builtin_id,
builtin_ty,
@ -554,7 +571,7 @@ impl TopLevelComposer {
unifier,
primitives,
&mut type_var_to_concrete_def,
&self.keyword_list,
(&self.keyword_list, &self.core_config)
)?
}
}
@ -827,8 +844,9 @@ impl TopLevelComposer {
unifier: &mut Unifier,
primitives: &PrimitiveStore,
type_var_to_concrete_def: &mut HashMap<Type, TypeAnnotation>,
keyword_list: &HashSet<StrRef>,
core_info: (&HashSet<StrRef>, &ComposerConfig),
) -> Result<(), String> {
let (keyword_list, core_config) = core_info;
let mut class_def = class_def.write();
let (
class_id,
@ -1058,20 +1076,17 @@ impl TopLevelComposer {
let dummy_field_type = unifier.get_fresh_var().0;
// handle Kernel[T], KernelInvariant[T]
let (annotation, mutable) = {
let mut result = None;
if let ast::ExprKind::Subscript { value, slice, .. } = &annotation.as_ref().node {
if let ast::ExprKind::Name { id, .. } = &value.node {
result = if id == &"Kernel".into() {
Some((slice, true))
} else if id == &"KernelInvariant".into() {
Some((slice, false))
} else {
None
}
}
}
result.unwrap_or((annotation, true))
let (annotation, mutable) = match &annotation.node {
ast::ExprKind::Subscript { value, slice, .. } if matches!(
&value.node,
ast::ExprKind::Name { id, .. } if id == &core_config.kernel_invariant_ann.into()
) => (slice, false),
ast::ExprKind::Subscript { value, slice, .. } if matches!(
&value.node,
ast::ExprKind::Name { id, .. } if core_config.kernel_ann.map_or(false, |c| id == &c.into())
) => (slice, true),
_ if core_config.kernel_ann.is_none() => (annotation, true),
_ => continue // ignore fields annotated otherwise
};
class_fields_def.push((*attr, dummy_field_type, mutable));

View File

@ -3,13 +3,10 @@ use inkwell::{
targets::*,
OptimizationLevel,
};
use nac3core::typecheck::{type_inferencer::PrimitiveStore, typedef::{Type, Unifier}};
use nac3parser::{ast::{Expr, ExprKind, StmtKind}, parser};
use std::{borrow::Borrow, collections::HashMap, env, fs, path::Path, sync::Arc, time::SystemTime};
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::{
codegen::{
concrete_type::ConcreteTypeStore, CodeGenTask, DefaultCodeGenerator, WithCall,
@ -17,11 +14,11 @@ use nac3core::{
},
symbol_resolver::SymbolResolver,
toplevel::{
composer::TopLevelComposer,
composer::{TopLevelComposer, ComposerConfig},
TopLevelDef, helper::parse_parameter_default_value,
type_annotation::*,
},
typecheck::typedef::FunSignature,
typecheck::{type_inferencer::PrimitiveStore, typedef::{Type, Unifier, FunSignature}}
};
mod basic_symbol_resolver;
@ -47,7 +44,10 @@ fn main() {
};
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 {
id_to_type: builtins_ty.into(),