From d512b6b8012c4bdbddc1dd81587bd8e0159e3d67 Mon Sep 17 00:00:00 2001 From: David Given Date: Wed, 17 Jan 2024 20:24:19 +0100 Subject: [PATCH] Fix incorrect casting of 16 bit to 32 bit signed values on the Z80. Add tests for this. --HG-- branch : bazel --- src/cowbe/archz80.cow.ng | 2 +- tests/casts.good | 8 ++++++++ tests/casts.test.cow | 10 ++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/cowbe/archz80.cow.ng b/src/cowbe/archz80.cow.ng index ffc07bf8..632dbeea 100644 --- a/src/cowbe/archz80.cow.ng +++ b/src/cowbe/archz80.cow.ng @@ -1881,7 +1881,7 @@ gen hlhl := CAST24(hl, sext==0) gen hlhl := CAST24(hl, sext!=0) uses a { - E_mov(REG_A, REG_L); + E_mov(REG_A, REG_H); E_add(REG_A, REG_A); E_sbc(REG_A, REG_A); E_exx(); diff --git a/tests/casts.good b/tests/casts.good index 10bf83d6..bcb1e87e 100644 --- a/tests/casts.good +++ b/tests/casts.good @@ -25,6 +25,10 @@ mone as uint8: yes mone as uint32: yes mone as int8: yes mone as int32: yes +twohundred as uint8: yes +twohundred as uint32: yes +twohundred as int8: yes +twohundred as int32: yes casts2 one as uint8: yes one as uint32: yes @@ -34,6 +38,10 @@ mone as uint8: yes mone as uint32: yes mone as int8: yes mone as int32: yes +twohundred as uint8: yes +twohundred as uint32: yes +twohundred as int8: yes +twohundred as int32: yes castu4 one as uint8: yes one as uint16: yes diff --git a/tests/casts.test.cow b/tests/casts.test.cow index 03234c1e..e95c08dd 100644 --- a/tests/casts.test.cow +++ b/tests/casts.test.cow @@ -36,6 +36,7 @@ casts1(); sub castu2() is var one: uint16 := 1; var mone: uint16 := -1; + var twohundred: uint16 := 200; print("castu2\n"); print("one as uint8"); if (one as uint8) == 1 then yes(); else no(); end if; @@ -46,12 +47,17 @@ sub castu2() is print("mone as uint32"); if (mone as uint32) == 0xffff then yes(); else no(); end if; print("mone as int8"); if (mone as int8) == -1 then yes(); else no(); end if; print("mone as int32"); if (mone as int32) == 0xffff then yes(); else no(); end if; + print("twohundred as uint8"); if (twohundred as uint8) == 200 then yes(); else no(); end if; + print("twohundred as uint32"); if (twohundred as uint32) == 200 then yes(); else no(); end if; + print("twohundred as int8"); if (twohundred as int8) == 200 then yes(); else no(); end if; + print("twohundred as int32"); if (twohundred as int32) == 200 then yes(); else no(); end if; end sub; castu2(); sub casts2() is var one: int16 := 1; var mone: int16 := -1; + var twohundred: int16 := 200; print("casts2\n"); print("one as uint8"); if (one as uint8) == 1 then yes(); else no(); end if; @@ -62,6 +68,10 @@ sub casts2() is print("mone as uint32"); if (mone as uint32) == 0xffffffff then yes(); else no(); end if; print("mone as int8"); if (mone as int8) == -1 then yes(); else no(); end if; print("mone as int32"); if (mone as int32) == -1 then yes(); else no(); end if; + print("twohundred as uint8"); if (twohundred as uint8) == 200 then yes(); else no(); end if; + print("twohundred as uint32"); if (twohundred as uint32) == 200 then yes(); else no(); end if; + print("twohundred as int8"); if (twohundred as int8) == 200 then yes(); else no(); end if; + print("twohundred as int32"); if (twohundred as int32) == 200 then yes(); else no(); end if; end sub; casts2();