forked from M-Labs/nac3
1
0
Fork 0
nac3/nac3embedded/src/lib.rs

192 lines
7.0 KiB
Rust
Raw Normal View History

2020-12-19 15:29:39 +08:00
use std::collections::HashMap;
2021-09-23 19:30:03 +08:00
use std::sync::Arc;
use std::path::Path;
2021-09-23 21:30:13 +08:00
use std::process::Command;
2020-12-19 15:29:39 +08:00
2020-12-18 10:09:35 +08:00
use pyo3::prelude::*;
2020-12-19 00:52:06 +08:00
use pyo3::exceptions;
2021-09-23 19:30:03 +08:00
use rustpython_parser::parser;
use inkwell::{
passes::{PassManager, PassManagerBuilder},
targets::*,
OptimizationLevel,
};
2020-12-19 16:23:12 +08:00
2021-09-23 19:30:03 +08:00
use nac3core::typecheck::type_inferencer::PrimitiveStore;
use nac3core::{
codegen::{CodeGenTask, WithCall, WorkerRegistry},
symbol_resolver::SymbolResolver,
toplevel::{composer::TopLevelComposer, TopLevelContext, TopLevelDef},
typecheck::typedef::{FunSignature, FuncArg},
};
2020-12-19 15:29:39 +08:00
2021-09-23 19:30:03 +08:00
mod symbol_resolver;
use symbol_resolver::*;
2020-12-19 15:29:39 +08:00
2021-09-23 19:30:03 +08:00
// TODO: do we really want unsendable?
// TopLevelComposer causes a lot of problems for Send.
#[pyclass(unsendable,name="NAC3")]
2020-12-19 15:29:39 +08:00
struct Nac3 {
2021-09-23 19:30:03 +08:00
primitive: PrimitiveStore,
internal_resolver: Arc<ResolverInternal>,
resolver: Arc<Box<dyn SymbolResolver + Send + Sync>>,
composer: TopLevelComposer,
top_level: Option<Arc<TopLevelContext>>
2020-12-19 15:29:39 +08:00
}
#[pymethods]
impl Nac3 {
#[new]
fn new() -> Self {
2021-09-23 19:30:03 +08:00
let primitive: PrimitiveStore = TopLevelComposer::make_primitives().0;
let (composer, builtins_def, builtins_ty) = TopLevelComposer::new(vec![
("output_int".into(), FunSignature {
args: vec![FuncArg {
name: "x".into(),
ty: primitive.int32,
default_value: None,
}],
ret: primitive.none,
vars: HashMap::new(),
}),
]);
let internal_resolver: Arc<ResolverInternal> = ResolverInternal {
id_to_type: builtins_ty.into(),
id_to_def: builtins_def.into(),
class_names: Default::default(),
}.into();
let resolver = Arc::new(
Box::new(Resolver(internal_resolver.clone())) as Box<dyn SymbolResolver + Send + Sync>
);
2020-12-19 15:29:39 +08:00
Nac3 {
2021-09-23 19:30:03 +08:00
primitive: primitive,
internal_resolver: internal_resolver,
resolver: resolver,
composer: composer,
top_level: None
2020-12-19 15:29:39 +08:00
}
}
2021-09-23 19:30:03 +08:00
fn register_class(&mut self, obj: PyObject) -> PyResult<()> {
2020-12-19 15:29:39 +08:00
Python::with_gil(|py| -> PyResult<()> {
let obj: &PyAny = obj.extract(py)?;
2021-09-23 19:30:03 +08:00
let source = PyModule::import(py, "inspect")?.getattr("getsource")?.call1((obj, ))?.extract()?;
let parser_result = parser::parse_program(source).map_err(|e|
exceptions::PySyntaxError::new_err(format!("failed to parse host object source: {}", e)))?;
for stmt in parser_result.into_iter() {
let (name, def_id, ty) = self.composer.register_top_level(
stmt,
Some(self.resolver.clone()),
"__main__".into(),
).unwrap();
self.internal_resolver.add_id_def(name.clone(), def_id);
if let Some(ty) = ty {
self.internal_resolver.add_id_type(name, ty);
}
}
2020-12-19 15:29:39 +08:00
Ok(())
})
}
2021-09-23 19:30:03 +08:00
fn analyze(&mut self) -> PyResult<()> {
self.composer.start_analysis(true).unwrap();
self.top_level = Some(Arc::new(self.composer.make_top_level_context()));
Ok(())
}
fn compile_method(&mut self, class_name: String, method_name: String) -> PyResult<()> {
let top_level = self.top_level.as_ref().unwrap();
let instance = {
let defs = top_level.definitions.read();
2021-09-24 09:58:58 +08:00
let class_def = defs[self.resolver.get_identifier_def(class_name.into()).unwrap().0].write();
2021-09-23 19:30:03 +08:00
let mut method_def = if let TopLevelDef::Class { methods, .. } = &*class_def {
2021-09-24 09:58:58 +08:00
println!("{:?}", methods);
if let Some((_name, _unification_key, definition_id)) = methods.iter().find(|method| method.0.to_string() == method_name) {
2021-09-23 19:30:03 +08:00
defs[definition_id.0].write()
} else {
return Err(exceptions::PyValueError::new_err("method not found"));
2020-12-19 15:29:39 +08:00
}
} else {
2021-09-23 19:30:03 +08:00
return Err(exceptions::PyTypeError::new_err("parent object is not a class"));
};
// FIXME: what is this for? What happens if the kernel is called twice?
if let TopLevelDef::Function {
instance_to_stmt,
instance_to_symbol,
..
} = &mut *method_def
{
instance_to_symbol.insert("".to_string(), method_name.clone());
instance_to_stmt[""].clone()
} else {
unreachable!()
2020-12-19 15:29:39 +08:00
}
2021-09-23 19:30:03 +08:00
};
let signature = FunSignature {
args: vec![],
ret: self.primitive.none,
vars: HashMap::new(),
};
let task = CodeGenTask {
subst: Default::default(),
symbol_name: method_name,
body: instance.body,
signature,
resolver: self.resolver.clone(),
unifier: top_level.unifiers.read()[instance.unifier_id].clone(),
calls: instance.calls,
};
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);
2020-12-19 15:29:39 +08:00
let triple = TargetTriple::create("riscv32-unknown-linux");
2021-09-23 19:30:03 +08:00
let target =
Target::from_triple(&triple).expect("couldn't create target from target triple");
let target_machine = target
.create_target_machine(
&triple,
"",
"+a,+m",
2021-09-23 19:30:03 +08:00
OptimizationLevel::Default,
RelocMode::PIC,
2021-09-23 19:30:03 +08:00
CodeModel::Default,
)
.expect("couldn't create target machine");
target_machine
.write_to_file(module, FileType::Object, Path::new(&format!("{}.o", module.get_name().to_str().unwrap())))
.expect("couldn't write module to file");
})));
2021-09-23 21:30:13 +08:00
let thread_names: Vec<String> = (0..4).map(|i| format!("module{}", i)).collect();
let threads: Vec<_> = thread_names.iter().map(|s| s.as_str()).collect();
2021-09-23 19:30:03 +08:00
let (registry, handles) = WorkerRegistry::create_workers(&threads, top_level.clone(), f);
registry.add_task(task);
registry.wait_tasks_complete(handles);
2021-09-23 21:30:13 +08:00
if let Ok(linker_status) = Command::new("ld.lld").args(&["-shared", "--eh-frame-hdr", "-Tkernel.ld"]).status() {
if !linker_status.success() {
return Err(exceptions::PyRuntimeError::new_err("failed to start linker"));
}
} else {
return Err(exceptions::PyRuntimeError::new_err("linker returned non-zero status code"));
}
2021-09-23 19:30:03 +08:00
Ok(())
2020-12-19 15:29:39 +08:00
}
2020-12-18 10:09:35 +08:00
}
#[pymodule]
2020-12-18 23:44:45 +08:00
fn nac3embedded(_py: Python, m: &PyModule) -> PyResult<()> {
2020-12-19 16:23:12 +08:00
Target::initialize_all(&InitializationConfig::default());
2020-12-19 15:29:39 +08:00
m.add_class::<Nac3>()?;
2020-12-18 10:09:35 +08:00
Ok(())
}