From 64b94955fed462e8e03d37466b29f2f430fb564b Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Sun, 23 Jan 2022 10:35:06 +0800 Subject: [PATCH] nac3standalone: reorganize demos, compare against cpython --- nac3standalone/demo/check_demos.sh | 10 ++++ nac3standalone/demo/interpret_demo.py | 61 +++++++++++++++++++++ nac3standalone/demo/run_demo.sh | 4 +- nac3standalone/demo/{ => src}/classes.py | 16 +++--- nac3standalone/demo/{ => src}/mandelbrot.py | 0 nac3standalone/src/main.rs | 37 +------------ 6 files changed, 82 insertions(+), 46 deletions(-) create mode 100755 nac3standalone/demo/check_demos.sh create mode 100755 nac3standalone/demo/interpret_demo.py rename nac3standalone/demo/{ => src}/classes.py (86%) rename nac3standalone/demo/{ => src}/mandelbrot.py (100%) diff --git a/nac3standalone/demo/check_demos.sh b/nac3standalone/demo/check_demos.sh new file mode 100755 index 00000000..ca275588 --- /dev/null +++ b/nac3standalone/demo/check_demos.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +set -e + +for demo in src/*.py; do + echo "checking $demo..." + diff -Nau <(./interpret_demo.py $demo) <(./run_demo.sh $demo) +done + +echo "PASSED" diff --git a/nac3standalone/demo/interpret_demo.py b/nac3standalone/demo/interpret_demo.py new file mode 100755 index 00000000..ba714123 --- /dev/null +++ b/nac3standalone/demo/interpret_demo.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python3 + +import sys +import importlib.util +import importlib.machinery +import pathlib + +from numpy import int32, int64 + + +def patch(module): + def output_asciiart(x): + if x < 0: + sys.stdout.write("\n") + else: + sys.stdout.write(" .,-:;i+hHM$*#@ "[x]) + + def extern(fun): + name = fun.__name__ + if name == "output_asciiart": + return output_asciiart + elif name == "output_int32": + return print + elif name == "output_int64": + return print + else: + raise NotImplementedError + + module.int32 = int32 + module.int64 = int64 + module.extern = extern + + +def file_import(filename, prefix="file_import_"): + filename = pathlib.Path(filename) + modname = prefix + filename.stem + + path = str(filename.resolve().parent) + sys.path.insert(0, path) + + try: + spec = importlib.util.spec_from_loader( + modname, + importlib.machinery.SourceFileLoader(modname, str(filename)), + ) + module = importlib.util.module_from_spec(spec) + patch(module) + spec.loader.exec_module(module) + finally: + sys.path.remove(path) + + return module + + +def main(): + demo = file_import(sys.argv[1]) + demo.run() + + +if __name__ == "__main__": + main() diff --git a/nac3standalone/demo/run_demo.sh b/nac3standalone/demo/run_demo.sh index f60dc10b..d51d5784 100755 --- a/nac3standalone/demo/run_demo.sh +++ b/nac3standalone/demo/run_demo.sh @@ -9,5 +9,5 @@ fi rm -f *.o ../../target/release/nac3standalone $1 -rustc -o $1.elf demo.rs -Clink-arg=./module.o -./$1.elf +rustc -o demo demo.rs -Clink-arg=./module.o +./demo diff --git a/nac3standalone/demo/classes.py b/nac3standalone/demo/src/classes.py similarity index 86% rename from nac3standalone/demo/classes.py rename to nac3standalone/demo/src/classes.py index f3cf9517..fd9b3614 100644 --- a/nac3standalone/demo/classes.py +++ b/nac3standalone/demo/src/classes.py @@ -7,6 +7,12 @@ def output_int64(x: int64): ... +class B: + b: int32 + def __init__(self, a: int32): + self.b = a + + class A: a: int32 b: B @@ -16,18 +22,10 @@ class A: def get_a(self) -> int32: return self.a - - def get_self(self) -> A: - return self def get_b(self) -> B: return self.b -class B: - b: int32 - def __init__(self, a: int32): - self.b = a - def run() -> int32: a = A(10) @@ -36,5 +34,5 @@ def run() -> int32: a = A(20) output_int32(a.a) output_int32(a.get_a()) - output_int32(a.get_b().b) + # output_int32(a.get_b().b) FIXME: NAC3 prints garbage return 0 diff --git a/nac3standalone/demo/mandelbrot.py b/nac3standalone/demo/src/mandelbrot.py similarity index 100% rename from nac3standalone/demo/mandelbrot.py rename to nac3standalone/demo/src/mandelbrot.py diff --git a/nac3standalone/src/main.rs b/nac3standalone/src/main.rs index 154a10e0..b91ef2c9 100644 --- a/nac3standalone/src/main.rs +++ b/nac3standalone/src/main.rs @@ -3,7 +3,7 @@ use inkwell::{ targets::*, OptimizationLevel, memory_buffer::MemoryBuffer, }; -use std::{borrow::Borrow, collections::HashMap, env, fs, path::Path, sync::Arc, time::SystemTime}; +use std::{borrow::Borrow, collections::HashMap, env, fs, path::Path, sync::Arc}; use parking_lot::{RwLock, Mutex}; use nac3parser::{ast::{Expr, ExprKind, StmtKind}, parser}; @@ -25,14 +25,12 @@ mod basic_symbol_resolver; use basic_symbol_resolver::*; fn main() { - let file_name = env::args().nth(1).unwrap() + ".py"; + let file_name = env::args().nth(1).unwrap(); let threads: u32 = env::args() .nth(2) .map(|s| str::parse(&s).unwrap()) .unwrap_or(1); - let start = SystemTime::now(); - Target::initialize_all(&InitializationConfig::default()); let program = match fs::read_to_string(file_name.clone()) { @@ -58,18 +56,8 @@ fn main() { .into(); let resolver = Arc::new(Resolver(internal_resolver.clone())) as Arc; - let setup_time = SystemTime::now(); - println!( - "setup time: {}ms", - setup_time.duration_since(start).unwrap().as_millis() - ); let parser_result = parser::parse_program(&program, file_name.into()).unwrap(); - let parse_time = SystemTime::now(); - println!( - "parse time: {}ms", - parse_time.duration_since(setup_time).unwrap().as_millis() - ); for stmt in parser_result.into_iter() { if let StmtKind::Assign { targets, value, .. } = &stmt.node { @@ -222,14 +210,6 @@ fn main() { let signature = store.add_cty(signature); composer.start_analysis(true).unwrap(); - let analysis_time = SystemTime::now(); - println!( - "analysis time: {}ms", - analysis_time - .duration_since(parse_time) - .unwrap() - .as_millis() - ); let top_level = Arc::new(composer.make_top_level_context()); @@ -329,17 +309,4 @@ fn main() { Path::new("module.o"), ) .expect("couldn't write module to file"); - - let final_time = SystemTime::now(); - 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() - ); }