From 12b71b37449593e0f37884245b1451084774cdfc Mon Sep 17 00:00:00 2001 From: Stefan Junker Date: Tue, 19 Sep 2017 10:41:39 +0200 Subject: [PATCH] src/code: add stackhandling examples in C and Rust --- src/code/stack_handling.c/makefile | 28 +++++++ src/code/stack_handling.c/stack_handling.c | 48 +++++++++++ src/code/stack_handling.rs/Cargo.lock | 4 + src/code/stack_handling.rs/Cargo.toml | 10 +++ src/code/stack_handling.rs/build.rs | 10 +++ src/code/stack_handling.rs/src/main.rs | 92 ++++++++++++++++++++++ 6 files changed, 192 insertions(+) create mode 100644 src/code/stack_handling.c/makefile create mode 100644 src/code/stack_handling.c/stack_handling.c create mode 100644 src/code/stack_handling.rs/Cargo.lock create mode 100644 src/code/stack_handling.rs/Cargo.toml create mode 100644 src/code/stack_handling.rs/build.rs create mode 100644 src/code/stack_handling.rs/src/main.rs diff --git a/src/code/stack_handling.c/makefile b/src/code/stack_handling.c/makefile new file mode 100644 index 0000000..c8b1fa9 --- /dev/null +++ b/src/code/stack_handling.c/makefile @@ -0,0 +1,28 @@ +TARGET = stack_handling +LIBS = +CC = clang +CFLAGS = -Wall -fomit-frame-pointer + +.PHONY: default all clean + +default: $(TARGET) +all: default objdump + +OBJECTS = $(patsubst %.c, %.o, $(wildcard *.c)) +HEADERS = $(wildcard *.h) + +%.o: %.c $(HEADERS) + $(CC) $(CFLAGS) -c $< -o $@ + +.PRECIOUS: $(TARGET) $(OBJECTS) + +$(TARGET): $(OBJECTS) + $(CC) $(OBJECTS) $(CFLAGS) $(LIBS) -o $@ + +clean: + -rm -f *.o + -rm -f $(TARGET) + -rm -f objdump + +objdump: $(TARGET) + objdump --no-show-raw-insn --disassembler-options=intel-nmemonic -d $(TARGET) > $@ \ No newline at end of file diff --git a/src/code/stack_handling.c/stack_handling.c b/src/code/stack_handling.c/stack_handling.c new file mode 100644 index 0000000..2cf0629 --- /dev/null +++ b/src/code/stack_handling.c/stack_handling.c @@ -0,0 +1,48 @@ +#include +#include + +static int64_t passthrough(int64_t a) { return a; } + +static int64_t neg(int64_t a) { return -a; } + +static int64_t neg_extravar(int64_t a) { + int64_t neg = -a; + return neg; +} + +static int64_t rec_many_args(int64_t rdi, int64_t rsi, int64_t rdx, int64_t rcx, + int64_t r8, int64_t r9, int64_t s1) { + if (s1 == INT64_MIN) { + return INT64_MIN; + } else { + return rec_many_args(rdi, rsi, rdx, rcx, r8, r9, s1 - 1); + } +} + +static int64_t many_args(int64_t rdi, int64_t rsi, int64_t rdx, int64_t rcx, + int64_t r8, int64_t r9, int64_t s1) { + + int64_t s2 = + rec_many_args(0xfffffffffffffff0, 0xfffffffffffffff1, 0xfffffffffffffff3, + 0xfffffffffffffff4, 0xfffffffffffffff5, 0xfffffffffffffff6, + INT64_MIN + 80000); + return s2; +} + +static void printer(int64_t *a, int64_t *b) { + fprintf(stdout, "%" PRId64 " = -%" PRId64 "\n", *a, *b); +} + +static void caller(void) { + int64_t a = passthrough(INT64_MAX); + int64_t b = neg(a); + printer(&a, &b); +} + +int main(void) { + caller(); + many_args(0xfffffffffffffff0, 0xfffffffffffffff1, 0xfffffffffffffff3, + 0xfffffffffffffff4, 0xfffffffffffffff5, 0xfffffffffffffff6, + 0xfffffffffffffff7); + return 0; +} \ No newline at end of file diff --git a/src/code/stack_handling.rs/Cargo.lock b/src/code/stack_handling.rs/Cargo.lock new file mode 100644 index 0000000..ace96fe --- /dev/null +++ b/src/code/stack_handling.rs/Cargo.lock @@ -0,0 +1,4 @@ +[root] +name = "stack_handling" +version = "0.1.0" + diff --git a/src/code/stack_handling.rs/Cargo.toml b/src/code/stack_handling.rs/Cargo.toml new file mode 100644 index 0000000..cd78041 --- /dev/null +++ b/src/code/stack_handling.rs/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "stack_handling" +version = "0.1.0" +authors = ["Stefan Junker "] +build = "build.rs" + +[dependencies] + +[profile.dev] +panic = "abort" \ No newline at end of file diff --git a/src/code/stack_handling.rs/build.rs b/src/code/stack_handling.rs/build.rs new file mode 100644 index 0000000..220a697 --- /dev/null +++ b/src/code/stack_handling.rs/build.rs @@ -0,0 +1,10 @@ +// build.rs + +use std::env; +use std::fs::File; +use std::io::Write; +use std::path::Path; + +fn main() { + println!("build.rs ran"); +} \ No newline at end of file diff --git a/src/code/stack_handling.rs/src/main.rs b/src/code/stack_handling.rs/src/main.rs new file mode 100644 index 0000000..2054f6c --- /dev/null +++ b/src/code/stack_handling.rs/src/main.rs @@ -0,0 +1,92 @@ +// #[derive(Debug)] +// struct Stat { +// sum: isize, +// count: isize, +// avg: isize, +// min: isize, +// max: isize, +// } +// +// macro_rules! sum { +// () => (0); +// ($e:expr) => ( $e ); +// ($head:expr, $($tail:expr),*) => { $head + sum!($($tail),*)}; +// } +// +// macro_rules! count { +// () => (0); +// ($e:expr) => ( 1 ); +// ($head:expr, $($tail:expr),*) => { 1 + count!($($tail),*) }; +// } +// +// #[inline(never)] +// fn push(a: isize, b: isize, c: isize) -> isize { +// sum!(a, b, c) +// } +// +// macro_rules! avg{ +// ($($all:expr),+) => { +// sum!($($all),+) / count!($($all),+) +// }; +// } +// +// macro_rules! max { +// ($x:expr) => ( $x ); +// ($x:expr, $($xs:expr),+) => { +// { +// use std::cmp::max; +// max($x, max!( $($xs),+ )) +// } +// }; +// } +// +// macro_rules! min { +// ($x:expr) => ( $x ); +// ($x:expr, $($xs:expr),+) => { +// { +// use std::cmp::min; +// min($x, min!( $($xs),+ )) +// } +// }; +// } +// #[inline(never)] +// fn stats(a: isize, b: isize, c: isize) -> Stat { +// let sum = sum(a, b, c); +// let count = count!(a, b, c); +// let avg = avg!(a, b, c); +// let min = min!(a, b, c); +// let max = max!(a, b, c); +// Stat { +// sum, +// count, +// avg, +// min, +// max, +// } +// } +// +// #[inline(never)] +// fn sum(a: isize, b: isize, c: isize) -> isize { +// sum!(a, b, c) +// } +#[inline(never)] +fn passthrough(a: isize) -> isize { + let b = a; + b +} + +#[inline(never)] +fn caller() { + let a = passthrough(1); + let b = a * 2; + printer(&a, &b); +} + +#[inline(never)] +fn printer(a: &isize, b: &isize) { + println!("2*{}={}", a, b); +} + +fn main() { + caller(); +} \ No newline at end of file