From 286964d5f81ee7393a24b74447ddf48d435ac89a Mon Sep 17 00:00:00 2001 From: Jeremiah Orians Date: Sat, 10 Dec 2022 20:55:41 -0500 Subject: [PATCH] Properly remove blocks that aren't executed --- cc.c | 9 ++++++++- cc.h | 32 ++++++++++++++++---------------- cc_macro.c | 25 ++++++++++++++++++++++--- makefile | 4 ++-- test/test.answers | 14 +++++++------- test/test1000/proof.answer | 2 +- 6 files changed, 56 insertions(+), 30 deletions(-) diff --git a/cc.c b/cc.c index 87fb0d4..80d2d92 100644 --- a/cc.c +++ b/cc.c @@ -45,7 +45,7 @@ int main(int argc, char** argv) int DEBUG = FALSE; FILE* in = stdin; FILE* destination_file = stdout; - Architecture = KNIGHT_NATIVE; /* Assume Knight-native */ + Architecture = 0; /* catch unset */ init_macro_env("__M2__", "42", "__INTERNAL_M2__", 0); /* Setup __M2__ */ char* arch; char* name; @@ -225,6 +225,13 @@ int main(int argc, char** argv) } } + /* Deal with special case of architecture not being set */ + if(0 == Architecture) + { + Architecture = KNIGHT_NATIVE; + init_macro_env("__knight__", "1", "--architecture", env); + } + /* Deal with special case of wanting to read from standard input */ if(stdin == in) { diff --git a/cc.h b/cc.h index aa3c95a..c7514e6 100644 --- a/cc.h +++ b/cc.h @@ -25,22 +25,22 @@ // CONSTANT TRUE 1 #define TRUE 1 -// CONSTANT KNIGHT_NATIVE 0 -#define KNIGHT_NATIVE 0 -// CONSTANT KNIGHT_POSIX 1 -#define KNIGHT_POSIX 1 -// CONSTANT X86 2 -#define X86 2 -// CONSTANT AMD64 3 -#define AMD64 3 -// CONSTANT ARMV7L 4 -#define ARMV7L 4 -// CONSTANT AARCH64 5 -#define AARCH64 5 -// CONSTANT RISCV32 6 -#define RISCV32 6 -// CONSTANT RISCV64 7 -#define RISCV64 7 +// CONSTANT KNIGHT_NATIVE 1 +#define KNIGHT_NATIVE 1 +// CONSTANT KNIGHT_POSIX 2 +#define KNIGHT_POSIX 2 +// CONSTANT X86 3 +#define X86 3 +// CONSTANT AMD64 4 +#define AMD64 4 +// CONSTANT ARMV7L 5 +#define ARMV7L 5 +// CONSTANT AARCH64 6 +#define AARCH64 6 +// CONSTANT RISCV32 7 +#define RISCV32 7 +// CONSTANT RISCV64 8 +#define RISCV64 8 void copy_string(char* target, char* source, int max); diff --git a/cc_macro.c b/cc_macro.c index cab300b..6e9c544 100644 --- a/cc_macro.c +++ b/cc_macro.c @@ -537,6 +537,7 @@ void handle_error(int warning_p) } } +void eat_block(); void macro_directive() { struct conditional_inclusion *t; @@ -557,6 +558,7 @@ void macro_directive() if(FALSE == result) { t->include = FALSE; + eat_block(); } t->previous_condition_matched = t->include; @@ -568,12 +570,13 @@ void macro_directive() if (NULL != lookup_macro(macro_token)) { result = TRUE; + eat_current_token(); } else { result = FALSE; + eat_block(); } - eat_current_token(); /* push conditional inclusion */ t = calloc(1, sizeof(struct conditional_inclusion)); @@ -599,8 +602,8 @@ void macro_directive() else { result = TRUE; + eat_current_token(); } - eat_current_token(); /* push conditional inclusion */ t = calloc(1, sizeof(struct conditional_inclusion)); @@ -611,6 +614,7 @@ void macro_directive() if(FALSE == result) { t->include = FALSE; + eat_block(); } t->previous_condition_matched = t->include; @@ -623,12 +627,20 @@ void macro_directive() conditional_inclusion_top->include = result && !conditional_inclusion_top->previous_condition_matched; conditional_inclusion_top->previous_condition_matched = conditional_inclusion_top->previous_condition_matched || conditional_inclusion_top->include; + if(FALSE == result) + { + eat_block(); + } } else if(match("#else", macro_token->s)) { eat_current_token(); require(NULL != conditional_inclusion_top, "#else without leading #if\n"); conditional_inclusion_top->include = !conditional_inclusion_top->previous_condition_matched; + if(FALSE == conditional_inclusion_top->include) + { + eat_block(); + } } else if(match("#endif", macro_token->s)) { @@ -722,7 +734,14 @@ void eat_block() eat_current_token(); require(NULL != macro_token, "Unterminated #if block\n"); - } while(!match("#elif", macro_token->s) && !match("#else", macro_token->s) && !match("#endif", macro_token->s)); + if(match("#elif", macro_token->s)) break; + if(match("#else", macro_token->s)) break; + if(match("#endif", macro_token->s)) break; + } while(TRUE); + require(NULL != macro_token->prev, "impossible #if block\n"); + + /* rewind the newline */ + if(match("\n", macro_token->prev->s)) macro_token = macro_token->prev; } diff --git a/makefile b/makefile index 50e25ac..d0c3c95 100644 --- a/makefile +++ b/makefile @@ -100,11 +100,11 @@ clean: bin: mkdir -p bin -results: +test/results: mkdir -p test/results # tests -test: M2-Planet | bin results +test: M2-Planet | bin test/results +make -f makefile-tests --output-sync sha256sum -c test/test.answers diff --git a/test/test.answers b/test/test.answers index 7b3b257..2f7e118 100644 --- a/test/test.answers +++ b/test/test.answers @@ -280,10 +280,10 @@ a9ef59d773b6ee9874343288313ee60dd1f9bf9a40e39fc72774353c80355d51 test/results/t 270011d2e8e69a4189b6000aa941f6dde68e9a9ff56e5306817b9d216f3061b9 test/results/test0106-riscv32-binary 99984c18dd4458c01a352619b0f2fbc97b892a698eae68ff9fe5aa0941ab7db3 test/results/test0106-riscv64-binary 832d344e0f60e8cd0ef37b11ff2b305950b15acad750cfe2e9cf079bd3788c92 test/results/test0106-x86-binary -4386585209d6e31ee38aaa2a18ffae1940fc1f50f97bf8db8df0244bcf7ce81c test/results/test1000-aarch64-binary -1b3ce0ef1e6b105286c463510736e423ee5b75e7373311aef1bb7d1334ba9792 test/results/test1000-amd64-binary -e4d348333b0c86bab2c45a35b5bd08d91dcc6353eeebaf870e2bea1c2e361480 test/results/test1000-armv7l-binary -70020886a51f59ac5f772127ba15a51481c1ab37bb49969f8a0a82fa9611fcdb test/results/test1000-knight-posix-binary -e661da4c9ac9b8e37667e2ad7ee0e0ffc644b7dafbd1201a3656f884f90c54d8 test/results/test1000-riscv32-binary -cae2d2ab1e5dcaeddc863327ed59c7490addf7fd2dbe80c99d9851f8813a4955 test/results/test1000-riscv64-binary -b814a6b280da0113b8a94098166086783017f290a9086922e7736529e4169227 test/results/test1000-x86-binary +9d8aa61dab395e71bd426f0a51dc8c61ef74b931213f7a0a69275519f2c0a55c test/results/test1000-aarch64-binary +b2d0388982b41e8198665e432524cf9a5697d595348f08aa407e0d5bd890141c test/results/test1000-amd64-binary +66f28931a06620caaed1011bcd9213f7c85a993c40cf100ff254eb52dae34a27 test/results/test1000-armv7l-binary +a7235952e40fa1ad53db909056d369344bc4f8f60063ee2d599e7825abbeb8c9 test/results/test1000-knight-posix-binary +c5dea9438154c6662e76dbb4e76f1894e53fc070a82d135f0eb01c7d7c738c1e test/results/test1000-riscv32-binary +599783f1643982bd1311de52e95e231797cfe2afc17a5b02cf97e9916ccc3ea9 test/results/test1000-riscv64-binary +9e755620983c84fe9fb491b0e3db20aa11331b559b22cce0810d1d3b4ae54584 test/results/test1000-x86-binary diff --git a/test/test1000/proof.answer b/test/test1000/proof.answer index cdef49e..7ed1703 100644 --- a/test/test1000/proof.answer +++ b/test/test1000/proof.answer @@ -1 +1 @@ -b5ff28e05396fb9eb892d83ccb09277c68b882d2a47fcb7ad80e6207fb587fed test/test1000/proof +c1159fc89bbf570f5df71f3d293e168eb2b590ea94ae3da4317a2948e3764b9a test/test1000/proof