From 045a9e8b93048379b60afc9992d6f245d5129a89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20K=C4=85dzio=C5=82ka?= Date: Mon, 12 Oct 2020 03:32:49 +0200 Subject: [PATCH 1/2] Report only one error when invalid shift has argument Not to mention that incrementing a variable in a loop is kinda dumb. --- include/asm/macro.h | 2 +- src/asm/macro.c | 9 ++++++--- src/asm/parser.y | 9 ++------- test/asm/shift-outside-macro.asm | 1 + test/asm/shift-outside-macro.err | 4 +++- 5 files changed, 13 insertions(+), 12 deletions(-) diff --git a/include/asm/macro.h b/include/asm/macro.h index 855133f8..6ec997b6 100644 --- a/include/asm/macro.h +++ b/include/asm/macro.h @@ -29,7 +29,7 @@ uint32_t macro_GetUniqueID(void); char const *macro_GetUniqueIDStr(void); void macro_SetUniqueID(uint32_t id); uint32_t macro_UseNewUniqueID(void); -void macro_ShiftCurrentArgs(void); +void macro_ShiftCurrentArgs(int32_t count); uint32_t macro_NbArgs(void); #endif diff --git a/src/asm/macro.c b/src/asm/macro.c index aa4903c4..49f38fc1 100644 --- a/src/asm/macro.c +++ b/src/asm/macro.c @@ -128,12 +128,15 @@ uint32_t macro_UseNewUniqueID(void) return maxUniqueID; } -void macro_ShiftCurrentArgs(void) +void macro_ShiftCurrentArgs(int32_t count) { if (!macroArgs) error("Cannot shift macro arguments outside of a macro\n"); - else if (macroArgs->shift != macroArgs->nbArgs) - macroArgs->shift++; + else if (macroArgs->shift < macroArgs->nbArgs) { + macroArgs->shift += count; + if (macroArgs->shift > macroArgs->nbArgs) + macroArgs->shift = macroArgs->nbArgs; + } } uint32_t macro_NbArgs(void) diff --git a/src/asm/parser.y b/src/asm/parser.y index 183adcbb..9ebf4775 100644 --- a/src/asm/parser.y +++ b/src/asm/parser.y @@ -579,13 +579,8 @@ assert : T_POP_ASSERT assert_type relocexpr } ; -shift : T_POP_SHIFT { macro_ShiftCurrentArgs(); } - | T_POP_SHIFT uconst - { - int32_t i = $2; - while (i--) - macro_ShiftCurrentArgs(); - } +shift : T_POP_SHIFT { macro_ShiftCurrentArgs(1); } + | T_POP_SHIFT uconst { macro_ShiftCurrentArgs($2); } ; load : T_POP_LOAD string ',' sectiontype sectorg sectattrs { diff --git a/test/asm/shift-outside-macro.asm b/test/asm/shift-outside-macro.asm index b46387d6..ca859616 100644 --- a/test/asm/shift-outside-macro.asm +++ b/test/asm/shift-outside-macro.asm @@ -1 +1,2 @@ shift +shift 3 diff --git a/test/asm/shift-outside-macro.err b/test/asm/shift-outside-macro.err index 5f050f3d..42cf6c9b 100644 --- a/test/asm/shift-outside-macro.err +++ b/test/asm/shift-outside-macro.err @@ -1,3 +1,5 @@ ERROR: shift-outside-macro.asm(1): Cannot shift macro arguments outside of a macro -error: Assembly aborted (1 errors)! +ERROR: shift-outside-macro.asm(2): + Cannot shift macro arguments outside of a macro +error: Assembly aborted (2 errors)! From 4e1d79081c3aba476daf38ba42180281752a8e3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20K=C4=85dzio=C5=82ka?= Date: Mon, 12 Oct 2020 22:54:38 +0200 Subject: [PATCH 2/2] Improve error message for negative shift arguments --- src/asm/macro.c | 6 ++++-- src/asm/parser.y | 2 +- test/asm/shift-negative.asm | 4 ++++ test/asm/shift-negative.err | 3 +++ test/asm/shift-negative.out | 0 5 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 test/asm/shift-negative.asm create mode 100644 test/asm/shift-negative.err create mode 100644 test/asm/shift-negative.out diff --git a/src/asm/macro.c b/src/asm/macro.c index 49f38fc1..e3d95200 100644 --- a/src/asm/macro.c +++ b/src/asm/macro.c @@ -130,9 +130,11 @@ uint32_t macro_UseNewUniqueID(void) void macro_ShiftCurrentArgs(int32_t count) { - if (!macroArgs) + if (!macroArgs) { error("Cannot shift macro arguments outside of a macro\n"); - else if (macroArgs->shift < macroArgs->nbArgs) { + } else if (count < 0) { + error("Cannot shift arguments by negative amount %" PRId32 "\n", count); + } else if (macroArgs->shift < macroArgs->nbArgs) { macroArgs->shift += count; if (macroArgs->shift > macroArgs->nbArgs) macroArgs->shift = macroArgs->nbArgs; diff --git a/src/asm/parser.y b/src/asm/parser.y index 9ebf4775..9e574620 100644 --- a/src/asm/parser.y +++ b/src/asm/parser.y @@ -580,7 +580,7 @@ assert : T_POP_ASSERT assert_type relocexpr ; shift : T_POP_SHIFT { macro_ShiftCurrentArgs(1); } - | T_POP_SHIFT uconst { macro_ShiftCurrentArgs($2); } + | T_POP_SHIFT const { macro_ShiftCurrentArgs($2); } ; load : T_POP_LOAD string ',' sectiontype sectorg sectattrs { diff --git a/test/asm/shift-negative.asm b/test/asm/shift-negative.asm new file mode 100644 index 00000000..4d41f406 --- /dev/null +++ b/test/asm/shift-negative.asm @@ -0,0 +1,4 @@ +m: MACRO + shift -3 +ENDM + m diff --git a/test/asm/shift-negative.err b/test/asm/shift-negative.err new file mode 100644 index 00000000..367f2e01 --- /dev/null +++ b/test/asm/shift-negative.err @@ -0,0 +1,3 @@ +ERROR: shift-negative.asm(4) -> shift-negative.asm::m(2): + Cannot shift arguments by negative amount -3 +error: Assembly aborted (1 errors)! diff --git a/test/asm/shift-negative.out b/test/asm/shift-negative.out new file mode 100644 index 00000000..e69de29b