2024-10-03 12:37:56 +08:00
|
|
|
use std::{
|
|
|
|
collections::{HashMap, HashSet},
|
|
|
|
sync::Arc,
|
|
|
|
};
|
|
|
|
|
|
|
|
use parking_lot::{Mutex, RwLock};
|
|
|
|
|
2021-10-16 18:08:13 +08:00
|
|
|
use nac3core::{
|
2024-10-04 12:51:38 +08:00
|
|
|
codegen::{CodeGenContext, CodeGenerator},
|
2024-10-04 13:16:04 +08:00
|
|
|
inkwell::{module::Linkage, values::BasicValue},
|
2024-10-03 12:37:56 +08:00
|
|
|
nac3parser::ast::{self, StrRef},
|
2021-11-27 21:29:27 +08:00
|
|
|
symbol_resolver::{SymbolResolver, SymbolValue, ValueEnum},
|
2021-10-16 18:08:13 +08:00
|
|
|
toplevel::{DefinitionId, TopLevelDef},
|
|
|
|
typecheck::{
|
2021-08-19 15:30:52 +08:00
|
|
|
type_inferencer::PrimitiveStore,
|
|
|
|
typedef::{Type, Unifier},
|
2021-10-16 18:08:13 +08:00
|
|
|
},
|
|
|
|
};
|
2021-08-19 15:30:52 +08:00
|
|
|
|
2021-09-19 16:19:16 +08:00
|
|
|
pub struct ResolverInternal {
|
2021-09-22 17:19:27 +08:00
|
|
|
pub id_to_type: Mutex<HashMap<StrRef, Type>>,
|
|
|
|
pub id_to_def: Mutex<HashMap<StrRef, DefinitionId>>,
|
2021-11-23 07:32:09 +08:00
|
|
|
pub module_globals: Mutex<HashMap<StrRef, SymbolValue>>,
|
2022-02-13 17:21:42 +08:00
|
|
|
pub str_store: Mutex<HashMap<String, i32>>,
|
2021-08-19 15:30:52 +08:00
|
|
|
}
|
|
|
|
|
2021-09-19 16:19:16 +08:00
|
|
|
impl ResolverInternal {
|
2021-09-22 17:19:27 +08:00
|
|
|
pub fn add_id_def(&self, id: StrRef, def: DefinitionId) {
|
2021-09-19 16:19:16 +08:00
|
|
|
self.id_to_def.lock().insert(id, def);
|
|
|
|
}
|
|
|
|
|
2021-09-22 17:19:27 +08:00
|
|
|
pub fn add_id_type(&self, id: StrRef, ty: Type) {
|
2021-09-19 16:19:16 +08:00
|
|
|
self.id_to_type.lock().insert(id, ty);
|
|
|
|
}
|
2021-11-23 07:32:09 +08:00
|
|
|
|
|
|
|
pub fn add_module_global(&self, id: StrRef, val: SymbolValue) {
|
|
|
|
self.module_globals.lock().insert(id, val);
|
|
|
|
}
|
2021-09-19 16:19:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Resolver(pub Arc<ResolverInternal>);
|
|
|
|
|
2021-08-19 15:30:52 +08:00
|
|
|
impl SymbolResolver for Resolver {
|
2021-11-23 07:32:09 +08:00
|
|
|
fn get_default_param_value(&self, expr: &ast::Expr) -> Option<SymbolValue> {
|
|
|
|
match &expr.node {
|
2022-02-21 18:27:46 +08:00
|
|
|
ast::ExprKind::Name { id, .. } => self.0.module_globals.lock().get(id).cloned(),
|
|
|
|
_ => unimplemented!("other type of expr not supported at {}", expr.location),
|
2021-11-23 07:32:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-16 18:08:13 +08:00
|
|
|
fn get_symbol_type(
|
|
|
|
&self,
|
2024-10-04 13:16:04 +08:00
|
|
|
unifier: &mut Unifier,
|
2021-10-16 18:08:13 +08:00
|
|
|
_: &[Arc<RwLock<TopLevelDef>>],
|
2024-10-04 13:16:04 +08:00
|
|
|
primitives: &PrimitiveStore,
|
2021-10-16 18:08:13 +08:00
|
|
|
str: StrRef,
|
2022-01-13 03:14:06 +08:00
|
|
|
) -> Result<Type, String> {
|
2024-10-04 13:16:04 +08:00
|
|
|
self.0
|
|
|
|
.id_to_type
|
|
|
|
.lock()
|
|
|
|
.get(&str)
|
|
|
|
.copied()
|
|
|
|
.or_else(|| {
|
|
|
|
self.0
|
|
|
|
.module_globals
|
|
|
|
.lock()
|
|
|
|
.get(&str)
|
|
|
|
.cloned()
|
|
|
|
.map(|v| v.get_type(primitives, unifier))
|
|
|
|
})
|
|
|
|
.ok_or(format!("cannot get type of {str}"))
|
2021-08-19 15:30:52 +08:00
|
|
|
}
|
|
|
|
|
2023-12-06 11:49:02 +08:00
|
|
|
fn get_symbol_value<'ctx>(
|
2021-10-16 18:08:13 +08:00
|
|
|
&self,
|
2024-10-04 13:16:04 +08:00
|
|
|
str: StrRef,
|
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
|
|
generator: &mut dyn CodeGenerator,
|
2021-11-20 19:50:25 +08:00
|
|
|
) -> Option<ValueEnum<'ctx>> {
|
2024-10-04 13:16:04 +08:00
|
|
|
self.0.module_globals.lock().get(&str).cloned().map(|v| {
|
|
|
|
ctx.module
|
|
|
|
.get_global(&str.to_string())
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
let ty = v.get_type(&ctx.primitives, &mut ctx.unifier);
|
|
|
|
|
|
|
|
let init_val = ctx.gen_symbol_val(generator, &v, ty);
|
|
|
|
let llvm_ty = init_val.get_type();
|
|
|
|
|
|
|
|
let global = ctx.module.add_global(llvm_ty, None, &str.to_string());
|
|
|
|
global.set_linkage(Linkage::LinkOnceAny);
|
|
|
|
global.set_initializer(&init_val);
|
|
|
|
|
|
|
|
global
|
|
|
|
})
|
|
|
|
.as_basic_value_enum()
|
|
|
|
.into()
|
|
|
|
})
|
2021-08-19 15:30:52 +08:00
|
|
|
}
|
|
|
|
|
2023-11-15 17:30:26 +08:00
|
|
|
fn get_identifier_def(&self, id: StrRef) -> Result<DefinitionId, HashSet<String>> {
|
2024-06-12 14:45:03 +08:00
|
|
|
self.0
|
|
|
|
.id_to_def
|
|
|
|
.lock()
|
|
|
|
.get(&id)
|
|
|
|
.copied()
|
|
|
|
.ok_or_else(|| HashSet::from([format!("Undefined identifier `{id}`")]))
|
2021-08-19 15:30:52 +08:00
|
|
|
}
|
2022-02-12 21:21:56 +08:00
|
|
|
|
2022-02-13 17:21:42 +08:00
|
|
|
fn get_string_id(&self, s: &str) -> i32 {
|
|
|
|
let mut str_store = self.0.str_store.lock();
|
|
|
|
if let Some(id) = str_store.get(s) {
|
|
|
|
*id
|
|
|
|
} else {
|
2024-03-11 14:25:37 +08:00
|
|
|
let id = i32::try_from(str_store.len())
|
|
|
|
.expect("Symbol resolver string store size exceeds max capacity (i32::MAX)");
|
2022-02-13 17:21:42 +08:00
|
|
|
str_store.insert(s.to_string(), id);
|
|
|
|
id
|
|
|
|
}
|
2022-02-12 21:21:56 +08:00
|
|
|
}
|
2022-03-05 00:27:51 +08:00
|
|
|
|
2022-03-17 21:23:38 +08:00
|
|
|
fn get_exception_id(&self, _: usize) -> usize {
|
2022-03-05 00:27:51 +08:00
|
|
|
unimplemented!()
|
|
|
|
}
|
2021-08-19 15:30:52 +08:00
|
|
|
}
|