From a66c85675226c82d1ec0e9cf22fdcff552e406d7 Mon Sep 17 00:00:00 2001 From: Andrew Chambers Date: Thu, 14 Oct 2021 22:39:39 +1300 Subject: Use short address displacement when possible. --- main.c | 4 ++-- test/test.sh | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/main.c b/main.c index dd3c3db..1d88fe8 100644 --- a/main.c +++ b/main.c @@ -372,11 +372,11 @@ static void assemblemem(const Memarg *memarg, uint8_t rexw, VarBytes prefix, base = rm; rm = 4; - // TODO: if our disp fits in a +disp8, use that instead. if (memarg->disp.c == 0 && memarg->disp.l == 0 && ((base & 7) != 5)) { mod = 0; /* +0 */ } else { - if (memarg->disp.c == 0 && memarg->disp.l == 0) { + if (memarg->disp.l == NULL && memarg->disp.c >= -128 && + memarg->disp.c <= 127) { mod = 1; /* +disp8 */ } else { mod = 2; /* +disp32 */ diff --git a/test/test.sh b/test/test.sh index 08db8b8..9734915 100644 --- a/test/test.sh +++ b/test/test.sh @@ -144,6 +144,11 @@ do t "lea (%rsp), %r${r}x" t "lea (%rbp), %r${r}x" t "lea (%r${r}x), %r${r}x" + t "lea 1(%rsp), %r${r}x" + t "lea 127(%rsp), %r${r}x" + t "lea 128(%rsp), %r${r}x" + t "lea -129(%rsp), %r${r}x" + t "lea -127(%rsp), %r${r}x" t "lea 2147483647(%rsp), %r${r}x" t "lea 2147483647(%rbp), %r${r}x" t "lea 2147483647(%r${r}x), %r${r}x" @@ -152,6 +157,11 @@ do t "lea (%rsp, %r${r}x), %r${r}x" t "lea (%rbp, %r${r}x), %r${r}x" t "lea (%r${r}x, %r${r}x), %r${r}x" + t "lea 1(%rsp, %r${r}x), %r${r}x" + t "lea 127(%rbp, %r${r}x), %r${r}x" + t "lea 128(%rbp, %r${r}x), %r${r}x" + t "lea -129(%rbp, %r${r}x), %r${r}x" + t "lea -127(%r${r}x, %r${r}x), %r${r}x" t "lea 2147483647(%rsp, %r${r}x), %r${r}x" t "lea 2147483647(%rbp, %r${r}x), %r${r}x" t "lea 2147483647(%r${r}x, %r${r}x), %r${r}x" @@ -159,6 +169,11 @@ do t "lea (%rsp, %r${r}x, 4), %r${r}x" t "lea (%rbp, %r${r}x, 4), %r${r}x" t "lea (%r${r}x, %r${r}x, 4), %r${r}x" + t "lea 1(%rsp, %r${r}x, 4), %r${r}x" + t "lea 127(%rbp, %r${r}x, 4), %r${r}x" + t "lea 128(%r${r}x, %r${r}x, 4), %r${r}x" + t "lea -129(%r${r}x, %r${r}x, 4), %r${r}x" + t "lea -127(%r${r}x, %r${r}x, 4), %r${r}x" t "lea 2147483647(%rsp, %r${r}x, 4), %r${r}x" t "lea 2147483647(%rbp, %r${r}x, 4), %r${r}x" t "lea 2147483647(%r${r}x, %r${r}x, 4), %r${r}x" @@ -181,7 +196,6 @@ for cc in $conditioncodes do t "set${cc} %al" t "set${cc} (%rax)" - t "j${cc} $0xefffffff" done for op in sal sar shl shr -- cgit v1.2.3