2021-08-27 12:35:21 +08:00
|
|
|
use std::time::SystemTime;
|
2021-08-27 16:25:59 +08:00
|
|
|
use std::{collections::HashSet, fs};
|
2020-12-17 22:20:30 +08:00
|
|
|
|
2021-08-27 16:25:59 +08:00
|
|
|
use inkwell::{
|
|
|
|
passes::{PassManager, PassManagerBuilder},
|
|
|
|
targets::*,
|
|
|
|
OptimizationLevel,
|
|
|
|
};
|
2021-08-19 15:30:52 +08:00
|
|
|
use parking_lot::RwLock;
|
|
|
|
use rustpython_parser::{
|
|
|
|
ast::{fold::Fold, StmtKind},
|
|
|
|
parser,
|
|
|
|
};
|
|
|
|
use std::{cell::RefCell, collections::HashMap, path::Path, sync::Arc};
|
2020-12-17 22:20:30 +08:00
|
|
|
|
2021-08-19 15:30:52 +08:00
|
|
|
use nac3core::{
|
|
|
|
codegen::{CodeGenTask, WithCall, WorkerRegistry},
|
2021-08-25 15:30:36 +08:00
|
|
|
symbol_resolver::SymbolResolver,
|
2021-08-27 16:25:59 +08:00
|
|
|
toplevel::{DefinitionId, FunInstance, TopLevelComposer, TopLevelContext, TopLevelDef},
|
2021-08-19 15:30:52 +08:00
|
|
|
typecheck::{
|
|
|
|
type_inferencer::{FunctionData, Inferencer},
|
|
|
|
typedef::{FunSignature, FuncArg, TypeEnum},
|
|
|
|
},
|
|
|
|
};
|
2020-12-17 22:20:30 +08:00
|
|
|
|
2021-08-19 15:30:52 +08:00
|
|
|
mod basic_symbol_resolver;
|
2020-12-17 22:20:30 +08:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
Target::initialize_all(&InitializationConfig::default());
|
|
|
|
|
2020-12-18 22:35:59 +08:00
|
|
|
let program = match fs::read_to_string("mandelbrot.py") {
|
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;
|
|
|
|
}
|
2020-12-17 22:20:30 +08:00
|
|
|
};
|
2021-08-19 15:30:52 +08:00
|
|
|
|
2021-08-27 12:35:21 +08:00
|
|
|
let start = SystemTime::now();
|
2021-08-19 15:30:52 +08:00
|
|
|
|
2021-08-25 13:39:55 +08:00
|
|
|
let composer = TopLevelComposer::new();
|
2021-08-19 15:30:52 +08:00
|
|
|
let mut unifier = composer.unifier.clone();
|
2021-08-25 15:30:36 +08:00
|
|
|
let primitives = composer.primitives_ty;
|
2021-08-23 10:34:11 +08:00
|
|
|
let top_level = Arc::new(composer.make_top_level_context());
|
2021-08-21 14:51:46 +08:00
|
|
|
unifier.top_level = Some(top_level.clone());
|
2021-08-25 15:30:36 +08:00
|
|
|
let output_fun = unifier.add_ty(TypeEnum::TFunc(RefCell::new(FunSignature {
|
2021-08-19 15:30:52 +08:00
|
|
|
args: vec![FuncArg {
|
|
|
|
name: "c".into(),
|
|
|
|
ty: primitives.int32,
|
|
|
|
default_value: None,
|
|
|
|
}],
|
|
|
|
ret: primitives.none,
|
|
|
|
vars: HashMap::new(),
|
|
|
|
})));
|
2021-08-27 16:25:59 +08:00
|
|
|
let output_id = top_level.definitions.read().len();
|
2021-08-19 15:30:52 +08:00
|
|
|
top_level
|
|
|
|
.definitions
|
|
|
|
.write()
|
|
|
|
.push(Arc::new(RwLock::new(TopLevelDef::Function {
|
|
|
|
name: "output".into(),
|
2021-08-25 15:30:36 +08:00
|
|
|
signature: output_fun,
|
2021-08-19 15:30:52 +08:00
|
|
|
instance_to_stmt: HashMap::new(),
|
|
|
|
instance_to_symbol: [("".to_string(), "output".to_string())]
|
|
|
|
.iter()
|
|
|
|
.cloned()
|
|
|
|
.collect(),
|
2021-08-25 15:30:36 +08:00
|
|
|
var_id: Default::default(),
|
2021-08-19 15:30:52 +08:00
|
|
|
resolver: None,
|
|
|
|
})));
|
2021-08-25 15:30:36 +08:00
|
|
|
|
2021-08-27 16:25:59 +08:00
|
|
|
// dummy resolver...
|
2021-08-25 15:30:36 +08:00
|
|
|
let resolver = Arc::new(Box::new(basic_symbol_resolver::Resolver {
|
2021-08-27 16:25:59 +08:00
|
|
|
id_to_type: HashMap::new(),
|
|
|
|
id_to_def: HashMap::new(),
|
2021-08-19 15:30:52 +08:00
|
|
|
class_names: Default::default(),
|
2021-08-25 15:30:36 +08:00
|
|
|
}) as Box<dyn SymbolResolver + Send + Sync>);
|
2021-08-27 16:25:59 +08:00
|
|
|
let mut functions = HashMap::new();
|
2021-08-19 15:30:52 +08:00
|
|
|
|
2021-08-27 16:25:59 +08:00
|
|
|
for stmt in parser::parse_program(&program).unwrap().into_iter() {
|
|
|
|
if let StmtKind::FunctionDef {
|
|
|
|
name,
|
|
|
|
body,
|
|
|
|
args,
|
|
|
|
returns,
|
|
|
|
..
|
|
|
|
} = stmt.node
|
|
|
|
{
|
|
|
|
let args = args
|
|
|
|
.args
|
|
|
|
.into_iter()
|
|
|
|
.map(|arg| FuncArg {
|
|
|
|
name: arg.node.arg.to_string(),
|
|
|
|
ty: resolver
|
|
|
|
.parse_type_annotation(
|
|
|
|
&top_level.definitions.read(),
|
|
|
|
&mut unifier,
|
|
|
|
&primitives,
|
|
|
|
&arg.node
|
|
|
|
.annotation
|
|
|
|
.expect("expected type annotation in parameters"),
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
default_value: None,
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let ret = returns
|
|
|
|
.map(|r| {
|
|
|
|
resolver
|
|
|
|
.parse_type_annotation(
|
|
|
|
&top_level.definitions.read(),
|
|
|
|
&mut unifier,
|
|
|
|
&primitives,
|
|
|
|
&r,
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
})
|
|
|
|
.unwrap_or(primitives.none);
|
|
|
|
let signature = FunSignature {
|
|
|
|
args,
|
|
|
|
ret,
|
|
|
|
vars: Default::default(),
|
|
|
|
};
|
|
|
|
let fun_ty = unifier.add_ty(TypeEnum::TFunc(RefCell::new(signature.clone())));
|
|
|
|
let id = top_level.definitions.read().len();
|
|
|
|
top_level
|
|
|
|
.definitions
|
|
|
|
.write()
|
|
|
|
.push(Arc::new(RwLock::new(TopLevelDef::Function {
|
|
|
|
name: name.clone(),
|
|
|
|
signature: fun_ty,
|
|
|
|
var_id: vec![],
|
|
|
|
instance_to_stmt: HashMap::new(),
|
|
|
|
instance_to_symbol: HashMap::new(),
|
|
|
|
resolver: None,
|
|
|
|
})));
|
|
|
|
functions.insert(name, (id, body, signature));
|
|
|
|
} else {
|
|
|
|
panic!("unsupported statement type");
|
|
|
|
}
|
|
|
|
}
|
2021-08-19 15:30:52 +08:00
|
|
|
|
2021-08-27 12:35:21 +08:00
|
|
|
let setup_time = SystemTime::now();
|
|
|
|
println!(
|
|
|
|
"Setup time: {}ms",
|
2021-08-27 16:25:59 +08:00
|
|
|
setup_time
|
|
|
|
.duration_since(start)
|
|
|
|
.unwrap()
|
|
|
|
.as_millis()
|
2021-08-27 12:35:21 +08:00
|
|
|
);
|
2021-08-27 16:25:59 +08:00
|
|
|
|
|
|
|
let mut id_to_def: HashMap<_, _> = functions
|
|
|
|
.iter()
|
|
|
|
.map(|(k, v)| (k.clone(), DefinitionId(v.0)))
|
|
|
|
.collect();
|
|
|
|
id_to_def.insert("output".into(), DefinitionId(output_id));
|
|
|
|
let mut id_to_type: HashMap<_, _> = functions
|
|
|
|
.iter()
|
|
|
|
.map(|(k, v)| {
|
|
|
|
(
|
|
|
|
k.clone(),
|
|
|
|
unifier.add_ty(TypeEnum::TFunc(RefCell::new(v.2.clone()))),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
id_to_type.insert("output".into(), output_fun);
|
|
|
|
|
|
|
|
let resolver = Arc::new(Box::new(basic_symbol_resolver::Resolver {
|
|
|
|
class_names: Default::default(),
|
|
|
|
id_to_type,
|
|
|
|
id_to_def,
|
|
|
|
}) as Box<dyn SymbolResolver + Send + Sync>);
|
|
|
|
|
|
|
|
for (_, (id, ast, signature)) in functions.into_iter() {
|
|
|
|
if let TopLevelDef::Function {
|
|
|
|
resolver: r,
|
|
|
|
instance_to_stmt,
|
|
|
|
..
|
|
|
|
} = &mut *top_level.definitions.read()[id].write()
|
|
|
|
{
|
|
|
|
*r = Some(resolver.clone());
|
|
|
|
|
|
|
|
let return_type = if unifier.unioned(primitives.none, signature.ret) {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(signature.ret)
|
|
|
|
};
|
|
|
|
let mut function_data = FunctionData {
|
|
|
|
resolver: resolver.clone(),
|
|
|
|
bound_variables: Vec::new(),
|
|
|
|
return_type,
|
|
|
|
};
|
|
|
|
let mut virtual_checks = Vec::new();
|
|
|
|
let mut calls = HashMap::new();
|
|
|
|
let mut identifiers = HashSet::new();
|
|
|
|
let mut variable_mapping = HashMap::new();
|
|
|
|
for arg in signature.args.iter() {
|
|
|
|
identifiers.insert(arg.name.clone());
|
|
|
|
variable_mapping.insert(arg.name.clone(), arg.ty);
|
|
|
|
}
|
|
|
|
let mut inferencer = Inferencer {
|
|
|
|
top_level: &top_level,
|
|
|
|
function_data: &mut function_data,
|
|
|
|
unifier: &mut unifier,
|
|
|
|
variable_mapping,
|
|
|
|
primitives: &primitives,
|
|
|
|
virtual_checks: &mut virtual_checks,
|
|
|
|
calls: &mut calls,
|
|
|
|
defined_identifiers: identifiers.clone(),
|
|
|
|
};
|
|
|
|
let statements = ast
|
|
|
|
.into_iter()
|
|
|
|
.map(|v| inferencer.fold_stmt(v))
|
|
|
|
.collect::<Result<Vec<_>, _>>()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let returned = inferencer
|
|
|
|
.check_block(&statements, &mut identifiers)
|
|
|
|
.unwrap();
|
|
|
|
if return_type.is_some() && !returned {
|
|
|
|
panic!("expected return");
|
|
|
|
}
|
|
|
|
|
|
|
|
instance_to_stmt.insert(
|
|
|
|
"".to_string(),
|
|
|
|
FunInstance {
|
|
|
|
body: statements,
|
|
|
|
unifier_id: 0,
|
|
|
|
calls,
|
|
|
|
subst: Default::default(),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2021-08-27 11:39:36 +08:00
|
|
|
}
|
2021-08-27 11:13:43 +08:00
|
|
|
|
2021-08-27 16:25:59 +08:00
|
|
|
let inference_time = SystemTime::now();
|
|
|
|
println!(
|
|
|
|
"Type inference time: {}ms",
|
|
|
|
inference_time
|
|
|
|
.duration_since(setup_time)
|
|
|
|
.unwrap()
|
|
|
|
.as_millis()
|
|
|
|
);
|
|
|
|
|
2021-08-19 15:30:52 +08:00
|
|
|
let top_level = Arc::new(TopLevelContext {
|
|
|
|
definitions: Arc::new(RwLock::new(std::mem::take(
|
|
|
|
&mut *top_level.definitions.write(),
|
|
|
|
))),
|
|
|
|
unifiers: Arc::new(RwLock::new(vec![(
|
|
|
|
unifier.get_shared_unifier(),
|
2021-08-25 15:30:36 +08:00
|
|
|
primitives,
|
2021-08-19 15:30:52 +08:00
|
|
|
)])),
|
|
|
|
});
|
2021-08-25 15:30:36 +08:00
|
|
|
|
2021-08-27 16:25:59 +08:00
|
|
|
let instance = {
|
|
|
|
let defs = top_level.definitions.read();
|
|
|
|
let mut instance = defs[resolver.get_identifier_def("run").unwrap().0].write();
|
|
|
|
if let TopLevelDef::Function {
|
|
|
|
instance_to_stmt,
|
|
|
|
instance_to_symbol,
|
|
|
|
..
|
|
|
|
} = &mut *instance
|
|
|
|
{
|
|
|
|
instance_to_symbol.insert("".to_string(), "run".to_string());
|
|
|
|
instance_to_stmt[""].clone()
|
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let signature = FunSignature {
|
|
|
|
args: vec![],
|
|
|
|
ret: primitives.int32,
|
|
|
|
vars: HashMap::new(),
|
|
|
|
};
|
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,
|
|
|
|
unifier: top_level.unifiers.read()[instance.unifier_id].clone(),
|
|
|
|
calls: instance.calls,
|
2021-08-19 15:30:52 +08:00
|
|
|
};
|
2021-08-27 12:35:21 +08:00
|
|
|
let f = Arc::new(WithCall::new(Box::new(move |module| {
|
|
|
|
let codegen_time = SystemTime::now();
|
|
|
|
println!(
|
|
|
|
"Code generation time: {}ms",
|
|
|
|
codegen_time
|
|
|
|
.duration_since(inference_time)
|
|
|
|
.unwrap()
|
|
|
|
.as_millis()
|
|
|
|
);
|
|
|
|
let builder = PassManagerBuilder::create();
|
|
|
|
builder.set_optimization_level(OptimizationLevel::Aggressive);
|
|
|
|
let passes = PassManager::create(());
|
|
|
|
builder.populate_module_pass_manager(&passes);
|
|
|
|
passes.run_on(module);
|
|
|
|
|
2021-08-19 15:30:52 +08:00
|
|
|
let triple = TargetMachine::get_default_triple();
|
|
|
|
let target =
|
|
|
|
Target::from_triple(&triple).expect("couldn't create target from target triple");
|
|
|
|
let target_machine = target
|
|
|
|
.create_target_machine(
|
|
|
|
&triple,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
OptimizationLevel::Default,
|
|
|
|
RelocMode::Default,
|
|
|
|
CodeModel::Default,
|
|
|
|
)
|
|
|
|
.expect("couldn't create target machine");
|
|
|
|
target_machine
|
|
|
|
.write_to_file(module, FileType::Object, Path::new("mandelbrot.o"))
|
|
|
|
.expect("couldn't write module to file");
|
2021-08-27 16:25:59 +08:00
|
|
|
|
2021-08-27 12:35:21 +08:00
|
|
|
println!(
|
|
|
|
"LLVM time: {}ms",
|
|
|
|
SystemTime::now()
|
|
|
|
.duration_since(codegen_time)
|
|
|
|
.unwrap()
|
|
|
|
.as_millis()
|
|
|
|
);
|
2021-08-27 16:25:59 +08:00
|
|
|
println!("IR:\n{}", module.print_to_string().to_str().unwrap());
|
|
|
|
|
2021-08-19 15:30:52 +08:00
|
|
|
})));
|
2021-08-27 16:25:59 +08:00
|
|
|
let threads = ["test"];
|
2021-08-19 15:30:52 +08:00
|
|
|
let (registry, handles) = WorkerRegistry::create_workers(&threads, top_level, f);
|
|
|
|
registry.add_task(task);
|
|
|
|
registry.wait_tasks_complete(handles);
|
2021-08-27 16:25:59 +08:00
|
|
|
println!("object file is in mandelbrot.o");
|
2020-12-17 22:20:30 +08:00
|
|
|
}
|