forked from M-Labs/nac3
1
0
Fork 0

split off standalone compiler

This commit is contained in:
Sebastien Bourdeauducq 2020-12-17 22:20:30 +08:00
parent c54e5994d7
commit 06d2fbd25c
8 changed files with 75 additions and 44 deletions

15
Cargo.lock generated
View File

@ -342,6 +342,16 @@ version = "0.2.81"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1482821306169ec4d07f6aca392a4681f66c75c9918aa49641a2595db64053cb" checksum = "1482821306169ec4d07f6aca392a4681f66c75c9918aa49641a2595db64053cb"
[[package]]
name = "libnac3"
version = "0.1.0"
dependencies = [
"inkwell",
"num-bigint",
"num-traits",
"rustpython-parser",
]
[[package]] [[package]]
name = "llvm-sys" name = "llvm-sys"
version = "100.2.0" version = "100.2.0"
@ -380,12 +390,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525"
[[package]] [[package]]
name = "nac3" name = "nac3standalone"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"inkwell", "inkwell",
"num-bigint", "libnac3",
"num-traits",
"rustpython-parser", "rustpython-parser",
] ]

View File

@ -1,11 +1,5 @@
[package] [workspace]
name = "nac3" members = [
version = "0.1.0" "libnac3",
authors = ["Sebastien Bourdeauducq <sb@m-labs.hk>"] "nac3standalone",
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" }

11
libnac3/Cargo.toml Normal file
View File

@ -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" }

View File

@ -6,11 +6,10 @@ use std::error::Error;
use std::fmt; use std::fmt;
use std::path::Path; use std::path::Path;
use std::collections::HashMap; use std::collections::HashMap;
use std::fs;
use num_traits::cast::ToPrimitive; use num_traits::cast::ToPrimitive;
use rustpython_parser::{ast, parser}; use rustpython_parser::ast;
use inkwell::OptimizationLevel; use inkwell::OptimizationLevel;
use inkwell::builder::Builder; use inkwell::builder::Builder;
@ -58,7 +57,7 @@ impl fmt::Display for CompileErrorKind {
} }
#[derive(Debug)] #[derive(Debug)]
struct CompileError { pub struct CompileError {
location: ast::Location, location: ast::Location,
kind: CompileErrorKind, kind: CompileErrorKind,
} }
@ -73,7 +72,7 @@ impl Error for CompileError {}
type CompileResult<T> = Result<T, CompileError>; type CompileResult<T> = Result<T, CompileError>;
struct CodeGen<'ctx> { pub struct CodeGen<'ctx> {
context: &'ctx Context, context: &'ctx Context,
module: Module<'ctx>, module: Module<'ctx>,
pass_manager: passes::PassManager<values::FunctionValue<'ctx>>, pass_manager: passes::PassManager<values::FunctionValue<'ctx>>,
@ -84,7 +83,7 @@ struct CodeGen<'ctx> {
} }
impl<'ctx> 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 module = context.create_module("kernel");
let pass_manager = passes::PassManager::create(&module); let pass_manager = passes::PassManager::create(&module);
@ -538,7 +537,7 @@ impl<'ctx> CodeGen<'ctx> {
Ok(()) 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); self.set_source_location(statement.location);
if let ast::StatementType::FunctionDef { if let ast::StatementType::FunctionDef {
is_async, is_async,
@ -556,11 +555,11 @@ impl<'ctx> CodeGen<'ctx> {
} }
} }
fn print_ir(&self) { pub fn print_ir(&self) {
self.module.print_to_stderr(); self.module.print_to_stderr();
} }
fn output(&self) { pub fn output(&self) {
//let triple = TargetTriple::create("riscv32-none-linux-gnu"); //let triple = TargetTriple::create("riscv32-none-linux-gnu");
let triple = TargetMachine::get_default_triple(); let triple = TargetMachine::get_default_triple();
let target = Target::from_triple(&triple) let target = Target::from_triple(&triple)
@ -582,25 +581,3 @@ impl<'ctx> CodeGen<'ctx> {
.expect("couldn't write module to file"); .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();
}

10
nac3standalone/Cargo.toml Normal file
View File

@ -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" }

View File

@ -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();
}