diff options
author | Matthew Sotoudeh <matthew@masot.net> | 2023-07-30 14:38:43 -0700 |
---|---|---|
committer | Matthew Sotoudeh <matthew@masot.net> | 2023-07-30 14:38:43 -0700 |
commit | 4de60a709d3af497781b7467f2c6fe7e09b39595 (patch) | |
tree | a9c4b70c6e87f37f9b69d77153c7cda10a60e043 /c/examples/zero_init/dietpass.c | |
parent | 678b0d4a2ec15db0735d08ebafec55605ce0a688 (diff) |
basic libdietc for writing passes in C
Diffstat (limited to 'c/examples/zero_init/dietpass.c')
-rw-r--r-- | c/examples/zero_init/dietpass.c | 25 |
1 files changed, 25 insertions, 0 deletions
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 <assert.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <libdietc.h> + +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; +} |