From 4de60a709d3af497781b7467f2c6fe7e09b39595 Mon Sep 17 00:00:00 2001 From: Matthew Sotoudeh Date: Sun, 30 Jul 2023 14:38:43 -0700 Subject: basic libdietc for writing passes in C --- c/examples/zero_init/dietpass.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 c/examples/zero_init/dietpass.c (limited to 'c/examples/zero_init/dietpass.c') diff --git a/c/examples/zero_init/dietpass.c b/c/examples/zero_init/dietpass.c new file mode 100644 index 0000000..e145a91 --- /dev/null +++ b/c/examples/zero_init/dietpass.c @@ -0,0 +1,25 @@ +#include +#include +#include +#include +#include + +int main(int argc, char **argv) { + assert(argc == 2); + struct program program = libdietc_parse(argv[1]); + + for (struct function *f = program.functions; f; f = f->next) { + for (struct instruction *i = f->instructions; i; i = i->next) { + if (strncmp(i->line, "\tType_", strlen("\tType_"))) continue; + char *name = libdietc_tokdup(strchrnul(i->line, ' ') + 1); + struct instruction *n = calloc(1, sizeof(*n)); + n->line = malloc(strlen(name) + 512); + sprintf(n->line, "memset(&%s, 0, sizeof(%s));", name, name); + n->next = i->next; + i->next = n; + i = n; + } + } + libdietc_print(program); + return 0; +} -- cgit v1.2.3