From be3e8f50a2ce7413acce84406259f53ae7a648a3 Mon Sep 17 00:00:00 2001 From: David Mak Date: Fri, 29 Sep 2023 15:15:18 +0800 Subject: [PATCH] standalone: Refactor demo library to C Needed for use by lli. --- nac3standalone/demo/demo.c | 89 +++++++++++++++++++++ nac3standalone/demo/demo.rs | 111 -------------------------- nac3standalone/demo/interpret_demo.py | 6 +- nac3standalone/demo/run_demo.sh | 6 +- 4 files changed, 98 insertions(+), 114 deletions(-) create mode 100644 nac3standalone/demo/demo.c delete mode 100644 nac3standalone/demo/demo.rs diff --git a/nac3standalone/demo/demo.c b/nac3standalone/demo/demo.c new file mode 100644 index 00000000..4369b704 --- /dev/null +++ b/nac3standalone/demo/demo.c @@ -0,0 +1,89 @@ +#include +#include +#include +#include + +#if __SIZEOF_POINTER__ == 8 + #define usize uint64_t +#elif __SIZEOF_POINTER__ == 4 + #define usize uint32_t +#else + #error "Unsupported platform - Platform is not 32-bit or 64-bit" +#endif + +void output_int32(const int32_t x) { + printf("%d\n", x); +} + +void output_int64(const int64_t x) { + printf("%ld\n", x); +} + +void output_uint32(const uint32_t x) { + printf("%d\n", x); +} + +void output_uint64(const uint64_t x) { + printf("%ld\n", x); +} + +void output_float64(const double x) { + printf("%f\n", x); +} +void output_asciiart(const int32_t x) { + const char* chars = " .,-:;i+hHM$*#@ "; + if (x < 0) { + putchar('\n'); + } else { + putchar(chars[x]); + } +} + +struct cslice { + const void* data; + usize len; +}; + +void output_int32_list(struct cslice* slice) { + const int32_t* data = (const int32_t*) slice->data; + + putchar('['); + for (usize i = 0; i < slice->len; ++i) { + if (i == slice->len - 1) { + printf("%d", data[i]); + } else { + printf("%d, ", data[i]); + } + } + putchar(']'); + putchar('\n'); +} + +void output_str(struct cslice* slice) { + const char* data = (const char*) slice->data; + + for (usize i = 0; i < slice->len; ++i) { + putchar(data[i]); + } + putchar('\n'); +} + +uint32_t __nac3_personality(uint32_t state, uint32_t exception_object, uint32_t context) { + printf("__nac3_personality(state: %u, exception_object: %u, context: %u\n", state, exception_object, context); + exit(101); + __builtin_unreachable(); +} + +uint32_t __nac3_raise(uint32_t state, uint32_t exception_object, uint32_t context) { + printf("__nac3_raise(state: %u, exception_object: %u, context: %u\n", state, exception_object, context); + exit(101); + __builtin_unreachable(); +} + +void __nac3_end_catch(void) {} + +extern int32_t run(void); + +int main(void) { + run(); +} diff --git a/nac3standalone/demo/demo.rs b/nac3standalone/demo/demo.rs deleted file mode 100644 index 154236dc..00000000 --- a/nac3standalone/demo/demo.rs +++ /dev/null @@ -1,111 +0,0 @@ -use std::io; -use std::io::Write; -use std::process::exit; - -mod cslice { - // copied from https://github.com/dherman/cslice - use std::marker::PhantomData; - use std::slice; - - #[repr(C)] - #[derive(Clone, Copy)] - pub struct CSlice<'a, T> { - base: *const T, - len: usize, - marker: PhantomData<&'a ()>, - } - - impl<'a, T> AsRef<[T]> for CSlice<'a, T> { - fn as_ref(&self) -> &[T] { - unsafe { slice::from_raw_parts(self.base, self.len) } - } - } -} - -#[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_uint32(x: u32) { - println!("{}", x); -} - -#[no_mangle] -pub extern "C" fn output_uint64(x: u64) { - println!("{}", x); -} - -#[no_mangle] -pub extern "C" fn output_float64(x: f64) { - // debug output to preserve the digits after the decimal points - // to match python `print` function - 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()); - } -} - -#[no_mangle] -pub extern "C" fn output_str(x: &cslice::CSlice) { - for e in x.as_ref().iter() { - print!("{}", char::from(*e)); - } - println!(); -} - -#[no_mangle] -pub extern "C" fn output_int32_list(x: &cslice::CSlice) { - print!("["); - let mut it = x.as_ref().iter().peekable(); - while let Some(e) = it.next() { - if it.peek().is_none() { - print!("{}", e); - } else { - print!("{}, ", e); - } - } - println!("]"); -} - -#[no_mangle] -pub extern "C" fn __nac3_personality(_state: u32, _exception_object: u32, _context: u32) -> u32 { - unimplemented!(); -} - -#[no_mangle] -pub extern "C" fn __nac3_raise(state: u32, exception_object: u32, context: u32) -> u32 { - writeln!(io::stderr(), - "__nac3_raise(state: {:#010x}, exception_object: {:#010x}, context: {:#010x})", - state, - exception_object, - context - ).unwrap(); - exit(101); -} - -#[no_mangle] -pub extern "C" fn __nac3_end_catch() {} - -extern "C" { - fn run() -> i32; -} - -fn main() { - unsafe { - run(); - } -} diff --git a/nac3standalone/demo/interpret_demo.py b/nac3standalone/demo/interpret_demo.py index dcee2002..69c0cf05 100755 --- a/nac3standalone/demo/interpret_demo.py +++ b/nac3standalone/demo/interpret_demo.py @@ -48,17 +48,21 @@ def patch(module): else: sys.stdout.write(" .,-:;i+hHM$*#@ "[x]) + def output_float(x): + print("%f" % x) + def extern(fun): name = fun.__name__ if name == "output_asciiart": return output_asciiart + elif name == "output_float64": + return output_float elif name in { "output_int32", "output_int64", "output_int32_list", "output_uint32", "output_uint64", - "output_float64", "output_str", }: return print diff --git a/nac3standalone/demo/run_demo.sh b/nac3standalone/demo/run_demo.sh index 61b79d6c..cffa79e5 100755 --- a/nac3standalone/demo/run_demo.sh +++ b/nac3standalone/demo/run_demo.sh @@ -14,7 +14,9 @@ else nac3standalone=../../target/x86_64-unknown-linux-gnu/release/nac3standalone fi -rm -f *.o +rm -f "*.o" demo + $nac3standalone "$@" -rustc -o demo demo.rs -Crelocation-model=static -Clink-arg=./module.o +clang -c -std=gnu11 -Wall -Wextra -O3 -o demo.o demo.c +clang -o demo module.o demo.o ./demo