forked from M-Labs/nac3
1
0
Fork 0

nac3core: use Arc to reduce copy

This commit is contained in:
pca006132 2021-09-22 16:04:25 +08:00
parent 1b5ac3cd25
commit 891056631f
3 changed files with 28 additions and 21 deletions

View File

@ -42,7 +42,7 @@ pub struct CodeGenContext<'ctx, 'a> {
pub var_assignment: HashMap<String, PointerValue<'ctx>>, pub var_assignment: HashMap<String, PointerValue<'ctx>>,
pub type_cache: HashMap<Type, BasicTypeEnum<'ctx>>, pub type_cache: HashMap<Type, BasicTypeEnum<'ctx>>,
pub primitives: PrimitiveStore, pub primitives: PrimitiveStore,
pub calls: HashMap<CodeLocation, CallId>, pub calls: Arc<HashMap<CodeLocation, CallId>>,
pub registry: &'a WorkerRegistry, pub registry: &'a WorkerRegistry,
// stores the alloca for variables // stores the alloca for variables
pub init_bb: BasicBlock<'ctx>, pub init_bb: BasicBlock<'ctx>,
@ -187,8 +187,8 @@ pub struct CodeGenTask {
pub subst: Vec<(Type, Type)>, pub subst: Vec<(Type, Type)>,
pub symbol_name: String, pub symbol_name: String,
pub signature: FunSignature, pub signature: FunSignature,
pub body: Vec<Stmt<Option<Type>>>, pub body: Arc<Vec<Stmt<Option<Type>>>>,
pub calls: HashMap<CodeLocation, CallId>, pub calls: Arc<HashMap<CodeLocation, CallId>>,
pub unifier: (SharedUnifier, PrimitiveStore), pub unifier: (SharedUnifier, PrimitiveStore),
pub resolver: Arc<Box<dyn SymbolResolver + Send + Sync>>, pub resolver: Arc<Box<dyn SymbolResolver + Send + Sync>>,
} }

View File

@ -1344,22 +1344,24 @@ impl TopLevelComposer {
calls: &mut calls, calls: &mut calls,
}; };
let fun_body = if let ast::StmtKind::FunctionDef { body, decorator_list, .. } = let fun_body =
ast.clone().unwrap().node if let ast::StmtKind::FunctionDef { body, decorator_list, .. } =
{ ast.clone().unwrap().node
if !decorator_list.is_empty() && {
matches!(&decorator_list[0].node, if !decorator_list.is_empty()
ast::ExprKind::Name{ id, .. } if id == "syscall") { && matches!(&decorator_list[0].node,
instance_to_symbol.insert("".to_string(), simple_name.clone()); ast::ExprKind::Name{ id, .. } if id == "syscall")
continue {
instance_to_symbol.insert("".to_string(), simple_name.clone());
continue;
}
body
} else {
unreachable!("must be function def ast")
} }
body .into_iter()
} else { .map(|b| inferencer.fold_stmt(b))
unreachable!("must be function def ast") .collect::<Result<Vec<_>, _>>()?;
}
.into_iter()
.map(|b| inferencer.fold_stmt(b))
.collect::<Result<Vec<_>, _>>()?;
let returned = let returned =
inferencer.check_block(fun_body.as_slice(), &mut identifiers)?; inferencer.check_block(fun_body.as_slice(), &mut identifiers)?;
@ -1393,7 +1395,12 @@ impl TopLevelComposer {
}) })
.join(", ") .join(", ")
}, },
FunInstance { body: fun_body, unifier_id: 0, calls, subst }, FunInstance {
body: Arc::new(fun_body),
unifier_id: 0,
calls: Arc::new(calls),
subst,
},
); );
} }
} else { } else {

View File

@ -30,8 +30,8 @@ mod test;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct FunInstance { pub struct FunInstance {
pub body: Vec<Stmt<Option<Type>>>, pub body: Arc<Vec<Stmt<Option<Type>>>>,
pub calls: HashMap<CodeLocation, CallId>, pub calls: Arc<HashMap<CodeLocation, CallId>>,
pub subst: HashMap<u32, Type>, pub subst: HashMap<u32, Type>,
pub unifier_id: usize, pub unifier_id: usize,
} }