src/code: add stackhandling examples in C and Rust
This commit is contained in:
parent
b4f23fdd2f
commit
12b71b3744
6 changed files with 192 additions and 0 deletions
28
src/code/stack_handling.c/makefile
Normal file
28
src/code/stack_handling.c/makefile
Normal file
|
@ -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) > $@
|
48
src/code/stack_handling.c/stack_handling.c
Normal file
48
src/code/stack_handling.c/stack_handling.c
Normal file
|
@ -0,0 +1,48 @@
|
|||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
4
src/code/stack_handling.rs/Cargo.lock
generated
Normal file
4
src/code/stack_handling.rs/Cargo.lock
generated
Normal file
|
@ -0,0 +1,4 @@
|
|||
[root]
|
||||
name = "stack_handling"
|
||||
version = "0.1.0"
|
||||
|
10
src/code/stack_handling.rs/Cargo.toml
Normal file
10
src/code/stack_handling.rs/Cargo.toml
Normal file
|
@ -0,0 +1,10 @@
|
|||
[package]
|
||||
name = "stack_handling"
|
||||
version = "0.1.0"
|
||||
authors = ["Stefan Junker <mail@stefanjunker.de>"]
|
||||
build = "build.rs"
|
||||
|
||||
[dependencies]
|
||||
|
||||
[profile.dev]
|
||||
panic = "abort"
|
10
src/code/stack_handling.rs/build.rs
Normal file
10
src/code/stack_handling.rs/build.rs
Normal file
|
@ -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");
|
||||
}
|
92
src/code/stack_handling.rs/src/main.rs
Normal file
92
src/code/stack_handling.rs/src/main.rs
Normal file
|
@ -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();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue