2021-10-16 22:17:36 +08:00
|
|
|
use inkwell::{
|
2022-02-21 18:27:46 +08:00
|
|
|
memory_buffer::MemoryBuffer,
|
2021-10-16 22:17:36 +08:00
|
|
|
passes::{PassManager, PassManagerBuilder},
|
|
|
|
targets::*,
|
2022-02-21 18:27:46 +08:00
|
|
|
OptimizationLevel,
|
2021-10-16 22:17:36 +08:00
|
|
|
};
|
2022-02-21 18:27:46 +08:00
|
|
|
use parking_lot::{Mutex, RwLock};
|
2022-01-23 10:35:06 +08:00
|
|
|
use std::{borrow::Borrow, collections::HashMap, env, fs, path::Path, sync::Arc};
|
2020-12-17 22:20:30 +08:00
|
|
|
|
2021-08-19 15:30:52 +08:00
|
|
|
use nac3core::{
|
2021-10-17 13:02:18 +08:00
|
|
|
codegen::{
|
2022-02-21 18:27:46 +08:00
|
|
|
concrete_type::ConcreteTypeStore, irrt::load_irrt, CodeGenTask, DefaultCodeGenerator,
|
|
|
|
WithCall, WorkerRegistry,
|
2021-10-17 13:02:18 +08:00
|
|
|
},
|
2021-08-25 15:30:36 +08:00
|
|
|
symbol_resolver::SymbolResolver,
|
2021-12-01 22:44:53 +08:00
|
|
|
toplevel::{
|
2022-02-21 18:27:46 +08:00
|
|
|
composer::TopLevelComposer, helper::parse_parameter_default_value, type_annotation::*,
|
|
|
|
TopLevelDef,
|
2021-12-01 22:44:53 +08:00
|
|
|
},
|
2022-02-21 18:27:46 +08:00
|
|
|
typecheck::{
|
|
|
|
type_inferencer::PrimitiveStore,
|
|
|
|
typedef::{FunSignature, Type, Unifier},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
use nac3parser::{
|
|
|
|
ast::{Expr, ExprKind, StmtKind},
|
|
|
|
parser,
|
2021-08-19 15:30:52 +08:00
|
|
|
};
|
2020-12-17 22:20:30 +08:00
|
|
|
|
2021-08-19 15:30:52 +08:00
|
|
|
mod basic_symbol_resolver;
|
2021-09-19 16:19:16 +08:00
|
|
|
use basic_symbol_resolver::*;
|
2020-12-17 22:20:30 +08:00
|
|
|
|
|
|
|
fn main() {
|
2022-01-23 10:35:06 +08:00
|
|
|
let file_name = env::args().nth(1).unwrap();
|
2022-02-21 18:27:46 +08:00
|
|
|
let threads: u32 = env::args().nth(2).map(|s| str::parse(&s).unwrap()).unwrap_or(1);
|
2021-09-22 14:30:52 +08:00
|
|
|
|
2020-12-17 22:20:30 +08:00
|
|
|
Target::initialize_all(&InitializationConfig::default());
|
|
|
|
|
2021-12-28 10:59:17 +08:00
|
|
|
let program = match fs::read_to_string(file_name.clone()) {
|
2020-12-17 22:20:30 +08:00
|
|
|
Ok(program) => program,
|
2021-08-19 15:30:52 +08:00
|
|
|
Err(err) => {
|
|
|
|
println!("Cannot open input file: {}", err);
|
|
|
|
return;
|
|
|
|
}
|
2021-10-16 22:17:36 +08:00
|
|
|
};
|
2021-08-19 15:30:52 +08:00
|
|
|
|
2021-09-19 16:19:16 +08:00
|
|
|
let primitive: PrimitiveStore = TopLevelComposer::make_primitives().0;
|
2022-02-21 18:27:46 +08:00
|
|
|
let (mut composer, builtins_def, builtins_ty) =
|
|
|
|
TopLevelComposer::new(vec![], Default::default());
|
2021-08-27 16:25:59 +08:00
|
|
|
|
2021-09-19 16:19:16 +08:00
|
|
|
let internal_resolver: Arc<ResolverInternal> = ResolverInternal {
|
|
|
|
id_to_type: builtins_ty.into(),
|
|
|
|
id_to_def: builtins_def.into(),
|
2021-08-27 16:25:59 +08:00
|
|
|
class_names: Default::default(),
|
2021-11-23 07:32:09 +08:00
|
|
|
module_globals: Default::default(),
|
2022-02-13 17:21:42 +08:00
|
|
|
str_store: Default::default(),
|
2021-10-16 22:17:36 +08:00
|
|
|
}
|
|
|
|
.into();
|
|
|
|
let resolver =
|
|
|
|
Arc::new(Resolver(internal_resolver.clone())) as Arc<dyn SymbolResolver + Send + Sync>;
|
2021-09-22 14:45:56 +08:00
|
|
|
|
2021-12-28 10:59:17 +08:00
|
|
|
let parser_result = parser::parse_program(&program, file_name.into()).unwrap();
|
2021-08-27 16:25:59 +08:00
|
|
|
|
2021-09-22 14:45:56 +08:00
|
|
|
for stmt in parser_result.into_iter() {
|
2021-11-23 07:32:09 +08:00
|
|
|
if let StmtKind::Assign { targets, value, .. } = &stmt.node {
|
2021-12-01 22:44:53 +08:00
|
|
|
fn handle_typevar_definition(
|
|
|
|
var: &Expr,
|
|
|
|
resolver: &(dyn SymbolResolver + Send + Sync),
|
|
|
|
def_list: &[Arc<RwLock<TopLevelDef>>],
|
|
|
|
unifier: &mut Unifier,
|
|
|
|
primitives: &PrimitiveStore,
|
|
|
|
) -> Result<Type, String> {
|
|
|
|
if let ExprKind::Call { func, args, .. } = &var.node {
|
|
|
|
if matches!(&func.node, ExprKind::Name { id, .. } if id == &"TypeVar".into()) {
|
|
|
|
let constraints = args
|
|
|
|
.iter()
|
|
|
|
.skip(1)
|
|
|
|
.map(|x| -> Result<Type, String> {
|
|
|
|
let ty = parse_ast_to_type_annotation_kinds(
|
|
|
|
resolver,
|
|
|
|
def_list,
|
|
|
|
unifier,
|
|
|
|
primitives,
|
|
|
|
x,
|
|
|
|
Default::default(),
|
|
|
|
)?;
|
2022-02-21 18:27:46 +08:00
|
|
|
get_type_from_type_annotation_kinds(
|
2022-03-23 00:22:28 +08:00
|
|
|
def_list, unifier, primitives, &ty, &mut None
|
2022-02-21 18:27:46 +08:00
|
|
|
)
|
2021-12-01 22:44:53 +08:00
|
|
|
})
|
|
|
|
.collect::<Result<Vec<_>, _>>()?;
|
2022-02-21 17:52:34 +08:00
|
|
|
Ok(unifier.get_fresh_var_with_range(&constraints, None, None).0)
|
2021-12-01 22:44:53 +08:00
|
|
|
} else {
|
2022-02-21 18:27:46 +08:00
|
|
|
Err(format!(
|
|
|
|
"expression {:?} cannot be handled as a TypeVar in global scope",
|
|
|
|
var
|
|
|
|
))
|
2021-12-01 22:44:53 +08:00
|
|
|
}
|
|
|
|
} else {
|
2022-02-21 18:27:46 +08:00
|
|
|
Err(format!(
|
|
|
|
"expression {:?} cannot be handled as a TypeVar in global scope",
|
|
|
|
var
|
|
|
|
))
|
2021-12-01 22:44:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-23 07:32:09 +08:00
|
|
|
fn handle_assignment_pattern(
|
|
|
|
targets: &[Expr],
|
|
|
|
value: &Expr,
|
|
|
|
resolver: &(dyn SymbolResolver + Send + Sync),
|
|
|
|
internal_resolver: &ResolverInternal,
|
2021-12-01 22:44:53 +08:00
|
|
|
def_list: &[Arc<RwLock<TopLevelDef>>],
|
|
|
|
unifier: &mut Unifier,
|
|
|
|
primitives: &PrimitiveStore,
|
2021-11-23 07:32:09 +08:00
|
|
|
) -> Result<(), String> {
|
|
|
|
if targets.len() == 1 {
|
|
|
|
match &targets[0].node {
|
|
|
|
ExprKind::Name { id, .. } => {
|
2021-12-01 22:44:53 +08:00
|
|
|
if let Ok(var) = handle_typevar_definition(
|
|
|
|
value.borrow(),
|
|
|
|
resolver,
|
|
|
|
def_list,
|
|
|
|
unifier,
|
|
|
|
primitives,
|
|
|
|
) {
|
|
|
|
internal_resolver.add_id_type(*id, var);
|
|
|
|
Ok(())
|
2022-02-21 18:27:46 +08:00
|
|
|
} else if let Ok(val) =
|
|
|
|
parse_parameter_default_value(value.borrow(), resolver)
|
|
|
|
{
|
2021-12-01 22:44:53 +08:00
|
|
|
internal_resolver.add_module_global(*id, val);
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(format!("fails to evaluate this expression `{:?}` as a constant or TypeVar at {}",
|
|
|
|
targets[0].node,
|
|
|
|
targets[0].location,
|
|
|
|
))
|
|
|
|
}
|
2021-11-23 07:32:09 +08:00
|
|
|
}
|
2022-02-21 18:27:46 +08:00
|
|
|
ExprKind::List { elts, .. } | ExprKind::Tuple { elts, .. } => {
|
2021-12-01 22:44:53 +08:00
|
|
|
handle_assignment_pattern(
|
|
|
|
elts,
|
|
|
|
value,
|
|
|
|
resolver,
|
|
|
|
internal_resolver,
|
|
|
|
def_list,
|
|
|
|
unifier,
|
2022-02-21 18:27:46 +08:00
|
|
|
primitives,
|
2021-12-01 22:44:53 +08:00
|
|
|
)?;
|
2021-11-23 07:32:09 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-02-21 18:27:46 +08:00
|
|
|
_ => Err(format!(
|
|
|
|
"assignment to {:?} is not supported at {}",
|
|
|
|
targets[0], targets[0].location
|
|
|
|
)),
|
2021-11-23 07:32:09 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match &value.node {
|
2022-02-21 18:27:46 +08:00
|
|
|
ExprKind::List { elts, .. } | ExprKind::Tuple { elts, .. } => {
|
2021-11-23 07:32:09 +08:00
|
|
|
if elts.len() != targets.len() {
|
|
|
|
Err(format!(
|
|
|
|
"number of elements to unpack does not match (expect {}, found {}) at {}",
|
|
|
|
targets.len(),
|
|
|
|
elts.len(),
|
|
|
|
value.location
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
for (tar, val) in targets.iter().zip(elts) {
|
|
|
|
handle_assignment_pattern(
|
|
|
|
std::slice::from_ref(tar),
|
|
|
|
val,
|
|
|
|
resolver,
|
2021-12-01 22:44:53 +08:00
|
|
|
internal_resolver,
|
|
|
|
def_list,
|
|
|
|
unifier,
|
2022-02-21 18:27:46 +08:00
|
|
|
primitives,
|
2021-11-23 07:32:09 +08:00
|
|
|
)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-02-21 18:27:46 +08:00
|
|
|
}
|
|
|
|
_ => Err(format!(
|
|
|
|
"unpack of this expression is not supported at {}",
|
|
|
|
value.location
|
|
|
|
)),
|
2021-11-23 07:32:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-01 22:44:53 +08:00
|
|
|
|
|
|
|
let def_list = composer.extract_def_list();
|
|
|
|
let unifier = &mut composer.unifier;
|
|
|
|
let primitives = &composer.primitives_ty;
|
|
|
|
if let Err(err) = handle_assignment_pattern(
|
|
|
|
targets,
|
|
|
|
value,
|
|
|
|
resolver.as_ref(),
|
|
|
|
internal_resolver.as_ref(),
|
|
|
|
&def_list,
|
|
|
|
unifier,
|
|
|
|
primitives,
|
|
|
|
) {
|
2021-11-23 07:32:09 +08:00
|
|
|
eprintln!("{}", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-04-18 04:51:15 +08:00
|
|
|
// still needs to skip this `from __future__ import annotations` because this seems to be
|
|
|
|
// magic in python and there seems no way to patch it from another module..
|
|
|
|
if matches!(
|
|
|
|
&stmt.node,
|
|
|
|
StmtKind::ImportFrom { module, names, .. }
|
|
|
|
if module == &Some("__future__".into()) && names[0].name == "annotations".into()
|
|
|
|
) { continue; }
|
|
|
|
|
2022-02-21 18:27:46 +08:00
|
|
|
let (name, def_id, ty) =
|
|
|
|
composer.register_top_level(stmt, Some(resolver.clone()), "__main__".into()).unwrap();
|
2021-09-19 22:54:06 +08:00
|
|
|
|
2021-09-22 17:56:48 +08:00
|
|
|
internal_resolver.add_id_def(name, def_id);
|
2021-09-19 16:19:16 +08:00
|
|
|
if let Some(ty) = ty {
|
|
|
|
internal_resolver.add_id_type(name, ty);
|
2021-08-27 16:25:59 +08:00
|
|
|
}
|
2021-08-27 11:39:36 +08:00
|
|
|
}
|
2021-08-27 11:13:43 +08:00
|
|
|
|
2022-02-21 18:27:46 +08:00
|
|
|
let signature = FunSignature { args: vec![], ret: primitive.int32, vars: HashMap::new() };
|
2021-10-17 13:02:18 +08:00
|
|
|
let mut store = ConcreteTypeStore::new();
|
|
|
|
let mut cache = HashMap::new();
|
|
|
|
let signature = store.from_signature(&mut composer.unifier, &primitive, &signature, &mut cache);
|
|
|
|
let signature = store.add_cty(signature);
|
|
|
|
|
2021-09-19 16:19:16 +08:00
|
|
|
composer.start_analysis(true).unwrap();
|
2021-08-27 16:25:59 +08:00
|
|
|
|
2021-09-19 16:19:16 +08:00
|
|
|
let top_level = Arc::new(composer.make_top_level_context());
|
2021-08-25 15:30:36 +08:00
|
|
|
|
2021-08-27 16:25:59 +08:00
|
|
|
let instance = {
|
|
|
|
let defs = top_level.definitions.read();
|
2022-02-21 18:27:46 +08:00
|
|
|
let mut instance = defs[resolver
|
|
|
|
.get_identifier_def("run".into())
|
|
|
|
.unwrap_or_else(|_| panic!("cannot find run() entry point"))
|
|
|
|
.0]
|
|
|
|
.write();
|
|
|
|
if let TopLevelDef::Function { instance_to_stmt, instance_to_symbol, .. } = &mut *instance {
|
2021-08-27 16:25:59 +08:00
|
|
|
instance_to_symbol.insert("".to_string(), "run".to_string());
|
|
|
|
instance_to_stmt[""].clone()
|
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
};
|
2021-09-19 16:19:16 +08:00
|
|
|
|
2021-08-19 15:30:52 +08:00
|
|
|
let task = CodeGenTask {
|
|
|
|
subst: Default::default(),
|
|
|
|
symbol_name: "run".to_string(),
|
2021-08-27 16:25:59 +08:00
|
|
|
body: instance.body,
|
2021-08-19 15:30:52 +08:00
|
|
|
signature,
|
2021-08-27 16:25:59 +08:00
|
|
|
resolver,
|
2021-10-17 13:02:18 +08:00
|
|
|
store,
|
|
|
|
unifier_index: instance.unifier_id,
|
2021-08-27 16:25:59 +08:00
|
|
|
calls: instance.calls,
|
2021-11-20 19:50:25 +08:00
|
|
|
id: 0,
|
2021-08-19 15:30:52 +08:00
|
|
|
};
|
2021-08-27 16:25:59 +08:00
|
|
|
|
2022-01-09 02:11:33 +08:00
|
|
|
let membuffers: Arc<Mutex<Vec<Vec<u8>>>> = Default::default();
|
|
|
|
let membuffer = membuffers.clone();
|
2022-02-13 17:21:42 +08:00
|
|
|
|
2022-01-09 02:11:33 +08:00
|
|
|
let f = Arc::new(WithCall::new(Box::new(move |module| {
|
|
|
|
let buffer = module.write_bitcode_to_memory();
|
|
|
|
let buffer = buffer.as_slice().into();
|
|
|
|
membuffer.lock().push(buffer);
|
2021-08-19 15:30:52 +08:00
|
|
|
})));
|
2021-10-16 22:17:36 +08:00
|
|
|
let threads = (0..threads)
|
2021-12-27 22:55:51 +08:00
|
|
|
.map(|i| Box::new(DefaultCodeGenerator::new(format!("module{}", i), 64)))
|
2021-10-16 22:17:36 +08:00
|
|
|
.collect();
|
|
|
|
let (registry, handles) = WorkerRegistry::create_workers(threads, top_level, f);
|
2021-08-19 15:30:52 +08:00
|
|
|
registry.add_task(task);
|
|
|
|
registry.wait_tasks_complete(handles);
|
2021-09-22 14:30:52 +08:00
|
|
|
|
2022-01-09 02:11:33 +08:00
|
|
|
let buffers = membuffers.lock();
|
|
|
|
let context = inkwell::context::Context::create();
|
|
|
|
let main = context
|
|
|
|
.create_module_from_ir(MemoryBuffer::create_from_memory_range(&buffers[0], "main"))
|
|
|
|
.unwrap();
|
|
|
|
for buffer in buffers.iter().skip(1) {
|
|
|
|
let other = context
|
|
|
|
.create_module_from_ir(MemoryBuffer::create_from_memory_range(buffer, "main"))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
main.link_in_module(other).unwrap();
|
|
|
|
}
|
|
|
|
main.link_in_module(load_irrt(&context)).unwrap();
|
|
|
|
|
|
|
|
let mut function_iter = main.get_first_function();
|
|
|
|
while let Some(func) = function_iter {
|
|
|
|
if func.count_basic_blocks() > 0 && func.get_name().to_str().unwrap() != "run" {
|
|
|
|
func.set_linkage(inkwell::module::Linkage::Private);
|
|
|
|
}
|
|
|
|
function_iter = func.get_next_function();
|
|
|
|
}
|
|
|
|
|
|
|
|
let builder = PassManagerBuilder::create();
|
|
|
|
builder.set_optimization_level(OptimizationLevel::Aggressive);
|
|
|
|
let passes = PassManager::create(());
|
|
|
|
builder.set_inliner_with_threshold(255);
|
|
|
|
builder.populate_module_pass_manager(&passes);
|
|
|
|
passes.run_on(&main);
|
2022-02-13 17:21:42 +08:00
|
|
|
|
2022-01-09 02:11:33 +08:00
|
|
|
let triple = TargetMachine::get_default_triple();
|
2022-02-21 18:27:46 +08:00
|
|
|
let target = Target::from_triple(&triple).expect("couldn't create target from target triple");
|
2022-01-09 02:11:33 +08:00
|
|
|
let target_machine = target
|
|
|
|
.create_target_machine(
|
|
|
|
&triple,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
OptimizationLevel::Default,
|
|
|
|
RelocMode::Default,
|
|
|
|
CodeModel::Default,
|
|
|
|
)
|
|
|
|
.expect("couldn't create target machine");
|
|
|
|
target_machine
|
2022-02-21 18:27:46 +08:00
|
|
|
.write_to_file(&main, FileType::Object, Path::new("module.o"))
|
2022-01-09 02:11:33 +08:00
|
|
|
.expect("couldn't write module to file");
|
2020-12-17 22:20:30 +08:00
|
|
|
}
|