From 956cca6ac8abd8a5cb8ec1c28aa38e31b1757392 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Wed, 22 Sep 2021 14:30:52 +0800 Subject: [PATCH] cleanup demos --- .../{mandelbrot.py => demo/classes.py} | 1 - nac3standalone/{ => demo}/demo.c | 15 +++++----- nac3standalone/demo/mandelbrot.py | 30 +++++++++++++++++++ nac3standalone/demo/run_demo.sh | 12 ++++++++ nac3standalone/src/main.rs | 22 +++++++++++--- 5 files changed, 67 insertions(+), 13 deletions(-) rename nac3standalone/{mandelbrot.py => demo/classes.py} (99%) rename nac3standalone/{ => demo}/demo.c (56%) create mode 100644 nac3standalone/demo/mandelbrot.py create mode 100755 nac3standalone/demo/run_demo.sh diff --git a/nac3standalone/mandelbrot.py b/nac3standalone/demo/classes.py similarity index 99% rename from nac3standalone/mandelbrot.py rename to nac3standalone/demo/classes.py index bc8f242b4..b50f09574 100644 --- a/nac3standalone/mandelbrot.py +++ b/nac3standalone/demo/classes.py @@ -29,4 +29,3 @@ def run() -> int32: output(a.get_a()) output(a.get_b().b) return 0 - diff --git a/nac3standalone/demo.c b/nac3standalone/demo/demo.c similarity index 56% rename from nac3standalone/demo.c rename to nac3standalone/demo/demo.c index 59a390376..fb9dbd1ff 100644 --- a/nac3standalone/demo.c +++ b/nac3standalone/demo/demo.c @@ -1,22 +1,21 @@ -// clang -Wall -o demo demo.c mandelbrot.o - #include #include -int output(int x) { +void output_int(int x) { + printf("%d\n", x); +} + +void output_asciiart(int x) { static char chars[] = " .,-:;i+hHM$*#@ "; if(x < 0) { putchar('\n'); } else { if(x < strlen(chars)) { - // putchar(chars[x]); - printf("%d\n", x); + putchar(chars[x]); } else { - // printf("ERROR\n"); - printf("%d\n", x); + printf("ERROR\n"); } } - return 0; } extern int run(); diff --git a/nac3standalone/demo/mandelbrot.py b/nac3standalone/demo/mandelbrot.py new file mode 100644 index 000000000..a2e00e898 --- /dev/null +++ b/nac3standalone/demo/mandelbrot.py @@ -0,0 +1,30 @@ +def run() -> int32: + minX = -2.0 + maxX = 1.0 + width = 78.0 + height = 36.0 + aspectRatio = 2.0 + + yScale = (maxX-minX)*(height/width)*aspectRatio + + y = 0.0 + while y < height: + x = 0.0 + while x < width: + c_r = minX+x*(maxX-minX)/width + c_i = y*yScale/height-yScale/2.0 + z_r = c_r + z_i = c_i + i = 0 + while i < 16: + if z_r*z_r + z_i*z_i > 4.0: + break + new_z_r = (z_r*z_r)-(z_i*z_i) + c_r + z_i = 2.0*z_r*z_i + c_i + z_r = new_z_r + i = i + 1 + output_asciiart(i) + x = x + 1.0 + output_asciiart(-1) + y = y + 1.0 + return 0 diff --git a/nac3standalone/demo/run_demo.sh b/nac3standalone/demo/run_demo.sh new file mode 100755 index 000000000..c3e606a46 --- /dev/null +++ b/nac3standalone/demo/run_demo.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +set -e + +if [ -z "$1" ]; then + echo "No argument supplied" + exit 1 +fi + +../../target/release/nac3standalone $1 +clang -Wall -O2 -o $1.elf demo.c $1.o +./$1.elf diff --git a/nac3standalone/src/main.rs b/nac3standalone/src/main.rs index 186c9d057..5f4eae663 100644 --- a/nac3standalone/src/main.rs +++ b/nac3standalone/src/main.rs @@ -1,4 +1,5 @@ use std::fs; +use std::env; use inkwell::{ passes::{PassManager, PassManagerBuilder}, targets::*, @@ -19,10 +20,13 @@ mod basic_symbol_resolver; use basic_symbol_resolver::*; fn main() { + let demo_name = env::args().nth(1).unwrap(); + let start = SystemTime::now(); + Target::initialize_all(&InitializationConfig::default()); - let program = match fs::read_to_string("mandelbrot.py") { + let program = match fs::read_to_string(demo_name.to_owned() + ".py") { Ok(program) => program, Err(err) => { println!("Cannot open input file: {}", err); @@ -32,9 +36,18 @@ fn main() { let primitive: PrimitiveStore = TopLevelComposer::make_primitives().0; let (mut composer, builtins_def, builtins_ty) = TopLevelComposer::new(vec![ - ("output".into(), FunSignature { + ("output_int".into(), FunSignature { args: vec![FuncArg { - name: "c".into(), + name: "x".into(), + ty: primitive.int32, + default_value: None, + }], + ret: primitive.none, + vars: HashMap::new(), + }), + ("output_asciiart".into(), FunSignature { + args: vec![FuncArg { + name: "x".into(), ty: primitive.int32, default_value: None, }], @@ -128,7 +141,7 @@ fn main() { ) .expect("couldn't create target machine"); target_machine - .write_to_file(module, FileType::Object, Path::new(&format!("{}.o", module.get_name().to_str().unwrap()))) + .write_to_file(module, FileType::Object, Path::new(&(demo_name.to_owned() + ".o"))) .expect("couldn't write module to file"); // println!("IR:\n{}", module.print_to_string().to_str().unwrap()); @@ -139,6 +152,7 @@ fn main() { let (registry, handles) = WorkerRegistry::create_workers(&threads, top_level, f); registry.add_task(task); registry.wait_tasks_complete(handles); + 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());