From 06d2fbd25c130f8d29780a28047d5435a14ec926 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Thu, 17 Dec 2020 22:20:30 +0800 Subject: [PATCH] split off standalone compiler --- Cargo.lock | 15 ++++++++++--- Cargo.toml | 16 +++++-------- libnac3/Cargo.toml | 11 +++++++++ src/main.rs => libnac3/src/lib.rs | 37 ++++++------------------------- nac3standalone/Cargo.toml | 10 +++++++++ demo.c => nac3standalone/demo.c | 0 nac3standalone/src/main.rs | 30 +++++++++++++++++++++++++ test.py => nac3standalone/test.py | 0 8 files changed, 75 insertions(+), 44 deletions(-) create mode 100644 libnac3/Cargo.toml rename src/main.rs => libnac3/src/lib.rs (96%) create mode 100644 nac3standalone/Cargo.toml rename demo.c => nac3standalone/demo.c (100%) create mode 100644 nac3standalone/src/main.rs rename test.py => nac3standalone/test.py (100%) diff --git a/Cargo.lock b/Cargo.lock index a56ac5c2..f24e0a8a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -342,6 +342,16 @@ version = "0.2.81" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1482821306169ec4d07f6aca392a4681f66c75c9918aa49641a2595db64053cb" +[[package]] +name = "libnac3" +version = "0.1.0" +dependencies = [ + "inkwell", + "num-bigint", + "num-traits", + "rustpython-parser", +] + [[package]] name = "llvm-sys" version = "100.2.0" @@ -380,12 +390,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" [[package]] -name = "nac3" +name = "nac3standalone" version = "0.1.0" dependencies = [ "inkwell", - "num-bigint", - "num-traits", + "libnac3", "rustpython-parser", ] diff --git a/Cargo.toml b/Cargo.toml index 9ad0e4f1..8e5c931d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,11 +1,5 @@ -[package] -name = "nac3" -version = "0.1.0" -authors = ["Sebastien Bourdeauducq "] -edition = "2018" - -[dependencies] -num-bigint = "0.3" -num-traits = "0.2" -inkwell = { git = "https://github.com/TheDan64/inkwell", branch = "master", features = ["llvm10-0"] } -rustpython-parser = { git = "https://github.com/RustPython/RustPython", branch = "master" } +[workspace] +members = [ + "libnac3", + "nac3standalone", +] diff --git a/libnac3/Cargo.toml b/libnac3/Cargo.toml new file mode 100644 index 00000000..86d5842e --- /dev/null +++ b/libnac3/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "libnac3" +version = "0.1.0" +authors = ["M-Labs"] +edition = "2018" + +[dependencies] +num-bigint = "0.3" +num-traits = "0.2" +inkwell = { git = "https://github.com/TheDan64/inkwell", branch = "master", features = ["llvm10-0"] } +rustpython-parser = { git = "https://github.com/RustPython/RustPython", branch = "master" } diff --git a/src/main.rs b/libnac3/src/lib.rs similarity index 96% rename from src/main.rs rename to libnac3/src/lib.rs index 7afecd72..80a0a653 100644 --- a/src/main.rs +++ b/libnac3/src/lib.rs @@ -6,11 +6,10 @@ use std::error::Error; use std::fmt; use std::path::Path; use std::collections::HashMap; -use std::fs; use num_traits::cast::ToPrimitive; -use rustpython_parser::{ast, parser}; +use rustpython_parser::ast; use inkwell::OptimizationLevel; use inkwell::builder::Builder; @@ -58,7 +57,7 @@ impl fmt::Display for CompileErrorKind { } #[derive(Debug)] -struct CompileError { +pub struct CompileError { location: ast::Location, kind: CompileErrorKind, } @@ -73,7 +72,7 @@ impl Error for CompileError {} type CompileResult = Result; -struct CodeGen<'ctx> { +pub struct CodeGen<'ctx> { context: &'ctx Context, module: Module<'ctx>, pass_manager: passes::PassManager>, @@ -84,7 +83,7 @@ struct CodeGen<'ctx> { } impl<'ctx> CodeGen<'ctx> { - fn new(context: &'ctx Context) -> CodeGen<'ctx> { + pub fn new(context: &'ctx Context) -> CodeGen<'ctx> { let module = context.create_module("kernel"); let pass_manager = passes::PassManager::create(&module); @@ -538,7 +537,7 @@ impl<'ctx> CodeGen<'ctx> { Ok(()) } - fn compile_toplevel(&mut self, statement: &ast::Statement) -> CompileResult<()> { + pub fn compile_toplevel(&mut self, statement: &ast::Statement) -> CompileResult<()> { self.set_source_location(statement.location); if let ast::StatementType::FunctionDef { is_async, @@ -556,11 +555,11 @@ impl<'ctx> CodeGen<'ctx> { } } - fn print_ir(&self) { + pub fn print_ir(&self) { self.module.print_to_stderr(); } - fn output(&self) { + pub fn output(&self) { //let triple = TargetTriple::create("riscv32-none-linux-gnu"); let triple = TargetMachine::get_default_triple(); let target = Target::from_triple(&triple) @@ -582,25 +581,3 @@ impl<'ctx> CodeGen<'ctx> { .expect("couldn't write module to file"); } } - -fn main() { - Target::initialize_all(&InitializationConfig::default()); - - let program = match fs::read_to_string("test.py") { - Ok(program) => program, - Err(err) => { println!("Cannot open input file: {}", err); return; } - }; - let ast = match parser::parse_program(&program) { - Ok(ast) => ast, - Err(err) => { println!("Parse error: {}", err); return; } - }; - - let context = Context::create(); - let mut codegen = CodeGen::new(&context); - match codegen.compile_toplevel(&ast.statements[0]) { - Ok(_) => (), - Err(err) => { println!("Compilation error: {}", err); return; } - } - codegen.print_ir(); - codegen.output(); -} diff --git a/nac3standalone/Cargo.toml b/nac3standalone/Cargo.toml new file mode 100644 index 00000000..6dc0fc63 --- /dev/null +++ b/nac3standalone/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "nac3standalone" +version = "0.1.0" +authors = ["M-Labs"] +edition = "2018" + +[dependencies] +inkwell = { git = "https://github.com/TheDan64/inkwell", branch = "master", features = ["llvm10-0"] } +rustpython-parser = { git = "https://github.com/RustPython/RustPython", branch = "master" } +libnac3 = { path = "../libnac3" } diff --git a/demo.c b/nac3standalone/demo.c similarity index 100% rename from demo.c rename to nac3standalone/demo.c diff --git a/nac3standalone/src/main.rs b/nac3standalone/src/main.rs new file mode 100644 index 00000000..a2a80c29 --- /dev/null +++ b/nac3standalone/src/main.rs @@ -0,0 +1,30 @@ +use std::fs; + +use inkwell::context::Context; +use inkwell::targets::*; +use rustpython_parser::parser; + +use libnac3::CodeGen; + + +fn main() { + Target::initialize_all(&InitializationConfig::default()); + + let program = match fs::read_to_string("test.py") { + Ok(program) => program, + Err(err) => { println!("Cannot open input file: {}", err); return; } + }; + let ast = match parser::parse_program(&program) { + Ok(ast) => ast, + Err(err) => { println!("Parse error: {}", err); return; } + }; + + let context = Context::create(); + let mut codegen = CodeGen::new(&context); + match codegen.compile_toplevel(&ast.statements[0]) { + Ok(_) => (), + Err(err) => { println!("Compilation error: {}", err); return; } + } + codegen.print_ir(); + codegen.output(); +} diff --git a/test.py b/nac3standalone/test.py similarity index 100% rename from test.py rename to nac3standalone/test.py