2021-10-16 22:17:36 +08:00
|
|
|
use inkwell::{
|
|
|
|
passes::{PassManager, PassManagerBuilder},
|
|
|
|
targets::*,
|
|
|
|
OptimizationLevel,
|
|
|
|
};
|
2021-09-19 16:19:16 +08:00
|
|
|
use nac3core::typecheck::type_inferencer::PrimitiveStore;
|
2021-11-03 17:11:00 +08:00
|
|
|
use nac3parser::parser;
|
2021-10-16 22:17:36 +08:00
|
|
|
use std::env;
|
|
|
|
use std::fs;
|
2021-09-22 14:45:56 +08:00
|
|
|
use std::{collections::HashMap, path::Path, sync::Arc, time::SystemTime};
|
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::{
|
|
|
|
concrete_type::ConcreteTypeStore, CodeGenTask, DefaultCodeGenerator, WithCall,
|
|
|
|
WorkerRegistry,
|
|
|
|
},
|
2021-08-25 15:30:36 +08:00
|
|
|
symbol_resolver::SymbolResolver,
|
2021-09-19 16:19:16 +08:00
|
|
|
toplevel::{composer::TopLevelComposer, TopLevelDef},
|
2021-10-02 19:05:35 +08:00
|
|
|
typecheck::typedef::FunSignature,
|
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() {
|
2021-09-22 14:30:52 +08:00
|
|
|
let demo_name = env::args().nth(1).unwrap();
|
2021-10-16 22:17:36 +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
|
|
|
|
2021-09-22 14:45:56 +08:00
|
|
|
let start = SystemTime::now();
|
2021-09-22 14:30:52 +08:00
|
|
|
|
2020-12-17 22:20:30 +08:00
|
|
|
Target::initialize_all(&InitializationConfig::default());
|
|
|
|
|
2021-09-23 20:02:56 +08:00
|
|
|
let program = match fs::read_to_string(demo_name + ".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;
|
|
|
|
}
|
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;
|
2021-10-02 19:05:35 +08:00
|
|
|
let (mut composer, builtins_def, builtins_ty) = TopLevelComposer::new(vec![]);
|
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-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
|
|
|
let setup_time = SystemTime::now();
|
2021-10-16 22:17:36 +08:00
|
|
|
println!(
|
|
|
|
"setup time: {}ms",
|
|
|
|
setup_time.duration_since(start).unwrap().as_millis()
|
|
|
|
);
|
2021-09-22 14:45:56 +08:00
|
|
|
|
|
|
|
let parser_result = parser::parse_program(&program).unwrap();
|
|
|
|
let parse_time = SystemTime::now();
|
2021-10-16 22:17:36 +08:00
|
|
|
println!(
|
|
|
|
"parse time: {}ms",
|
|
|
|
parse_time.duration_since(setup_time).unwrap().as_millis()
|
|
|
|
);
|
2021-08-27 16:25:59 +08:00
|
|
|
|
2021-09-22 14:45:56 +08:00
|
|
|
for stmt in parser_result.into_iter() {
|
2021-10-16 22:17:36 +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
|
|
|
|
2021-10-17 13:02:18 +08:00
|
|
|
let signature = FunSignature {
|
|
|
|
args: vec![],
|
|
|
|
ret: primitive.int32,
|
|
|
|
vars: HashMap::new(),
|
|
|
|
};
|
|
|
|
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-09-22 14:45:56 +08:00
|
|
|
let analysis_time = SystemTime::now();
|
2021-10-16 22:17:36 +08:00
|
|
|
println!(
|
|
|
|
"analysis time: {}ms",
|
|
|
|
analysis_time
|
|
|
|
.duration_since(parse_time)
|
|
|
|
.unwrap()
|
|
|
|
.as_millis()
|
|
|
|
);
|
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();
|
2021-09-22 17:19:27 +08:00
|
|
|
let mut instance = defs[resolver.get_identifier_def("run".into()).unwrap().0].write();
|
2021-08-27 16:25:59 +08:00
|
|
|
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!()
|
|
|
|
}
|
|
|
|
};
|
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-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 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
|
2021-10-16 22:17:36 +08:00
|
|
|
.write_to_file(
|
|
|
|
module,
|
|
|
|
FileType::Object,
|
|
|
|
Path::new(&format!("{}.o", module.get_name().to_str().unwrap())),
|
|
|
|
)
|
2021-08-19 15:30:52 +08:00
|
|
|
.expect("couldn't write module to file");
|
2021-08-27 16:25:59 +08:00
|
|
|
|
2021-09-22 14:45:56 +08:00
|
|
|
// println!("IR:\n{}", module.print_to_string().to_str().unwrap());
|
2021-08-19 15:30:52 +08:00
|
|
|
})));
|
2021-10-16 22:17:36 +08:00
|
|
|
let threads = (0..threads)
|
|
|
|
.map(|i| Box::new(DefaultCodeGenerator::new(format!("module{}", i))))
|
|
|
|
.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
|
|
|
|
2021-09-22 14:45:56 +08:00
|
|
|
let final_time = SystemTime::now();
|
2021-10-16 22:17:36 +08:00
|
|
|
println!(
|
|
|
|
"codegen time (including LLVM): {}ms",
|
|
|
|
final_time
|
|
|
|
.duration_since(analysis_time)
|
|
|
|
.unwrap()
|
|
|
|
.as_millis()
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"total time: {}ms",
|
|
|
|
final_time.duration_since(start).unwrap().as_millis()
|
|
|
|
);
|
2020-12-17 22:20:30 +08:00
|
|
|
}
|