forked from M-Labs/nac3
1
0
Fork 0

cleanup demos

This commit is contained in:
Sebastien Bourdeauducq 2021-09-22 14:30:52 +08:00
parent 4a5f2d495e
commit 956cca6ac8
5 changed files with 67 additions and 13 deletions

View File

@ -29,4 +29,3 @@ def run() -> int32:
output(a.get_a()) output(a.get_a())
output(a.get_b().b) output(a.get_b().b)
return 0 return 0

View File

@ -1,22 +1,21 @@
// clang -Wall -o demo demo.c mandelbrot.o
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
int output(int x) { void output_int(int x) {
printf("%d\n", x);
}
void output_asciiart(int x) {
static char chars[] = " .,-:;i+hHM$*#@ "; static char chars[] = " .,-:;i+hHM$*#@ ";
if(x < 0) { if(x < 0) {
putchar('\n'); putchar('\n');
} else { } else {
if(x < strlen(chars)) { if(x < strlen(chars)) {
// putchar(chars[x]); putchar(chars[x]);
printf("%d\n", x);
} else { } else {
// printf("ERROR\n"); printf("ERROR\n");
printf("%d\n", x);
} }
} }
return 0;
} }
extern int run(); extern int run();

View File

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

12
nac3standalone/demo/run_demo.sh Executable file
View File

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

View File

@ -1,4 +1,5 @@
use std::fs; use std::fs;
use std::env;
use inkwell::{ use inkwell::{
passes::{PassManager, PassManagerBuilder}, passes::{PassManager, PassManagerBuilder},
targets::*, targets::*,
@ -19,10 +20,13 @@ mod basic_symbol_resolver;
use basic_symbol_resolver::*; use basic_symbol_resolver::*;
fn main() { fn main() {
let demo_name = env::args().nth(1).unwrap();
let start = SystemTime::now(); let start = SystemTime::now();
Target::initialize_all(&InitializationConfig::default()); 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, Ok(program) => program,
Err(err) => { Err(err) => {
println!("Cannot open input file: {}", err); println!("Cannot open input file: {}", err);
@ -32,9 +36,18 @@ fn main() {
let primitive: PrimitiveStore = TopLevelComposer::make_primitives().0; let primitive: PrimitiveStore = TopLevelComposer::make_primitives().0;
let (mut composer, builtins_def, builtins_ty) = TopLevelComposer::new(vec![ let (mut composer, builtins_def, builtins_ty) = TopLevelComposer::new(vec![
("output".into(), FunSignature { ("output_int".into(), FunSignature {
args: vec![FuncArg { 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, ty: primitive.int32,
default_value: None, default_value: None,
}], }],
@ -128,7 +141,7 @@ fn main() {
) )
.expect("couldn't create target machine"); .expect("couldn't create target machine");
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"); .expect("couldn't write module to file");
// println!("IR:\n{}", module.print_to_string().to_str().unwrap()); // 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); let (registry, handles) = WorkerRegistry::create_workers(&threads, top_level, f);
registry.add_task(task); registry.add_task(task);
registry.wait_tasks_complete(handles); registry.wait_tasks_complete(handles);
let final_time = SystemTime::now(); let final_time = SystemTime::now();
println!("codegen time (including LLVM): {}ms", final_time.duration_since(analysis_time).unwrap().as_millis()); 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()); println!("total time: {}ms", final_time.duration_since(start).unwrap().as_millis());