demo: rewrite in Rust

escape-analysis
Sebastien Bourdeauducq 2022-01-09 10:51:10 +08:00
parent 4760851638
commit 096193f7ab
3 changed files with 30 additions and 31 deletions

View File

@ -1,30 +0,0 @@
#include <stdio.h>
#include <string.h>
void output_int32(int x) {
printf("%d\n", x);
}
void output_int64(long x) {
printf("%ld\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]);
} else {
printf("ERROR\n");
}
}
}
extern int run();
int main() {
run();
return 0;
}

View File

@ -0,0 +1,29 @@
#[no_mangle]
pub extern "C" fn output_int32(x: i32) {
println!("{}", x);
}
#[no_mangle]
pub extern "C" fn output_int64(x: i64) {
println!("{}", x);
}
#[no_mangle]
pub extern "C" fn output_asciiart(x: i32) {
let chars = " .,-:;i+hHM$*#@ ";
if x < 0 {
println!("");
} else {
print!("{}", chars.chars().nth(x as usize).unwrap());
}
}
extern "C" {
fn run() -> i32;
}
fn main() {
unsafe {
run();
}
}

View File

@ -9,5 +9,5 @@ fi
rm -f *.o
../../target/release/nac3standalone $1
clang -Wall -O2 -o $1.elf demo.c module.o -lm
rustc -o $1.elf demo.rs -Clink-arg=./module.o
./$1.elf