From 05e945c0a8bb0ed9df5a6ce7a27f5078a14b90b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrius=20=C5=A0tikonas?= Date: Wed, 12 Jul 2023 23:19:09 +0100 Subject: [PATCH] mescc: Do not overflow M1 64bit immediates. M1 immediates are limited to 31-bit before they become negative. * module/mescc/x86_64/as.scm (x86_64:value->r, x86_64:r-cmp-value, x86_64:r-long-mem-add, x86_64:r-and): Limit M1 immediate to #x80000000. * lib/tests/scaffold/67-m1-overflow-check.c: New test. * build-aux/check-mescc.sh (mes_tests): Add it. --- build-aux/check-mescc.sh | 2 ++ lib/tests/scaffold/67-m1-overflow-check.c | 25 +++++++++++++++++++++++ module/mescc/compile.scm | 1 + module/mescc/x86_64/as.scm | 9 ++++---- 4 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 lib/tests/scaffold/67-m1-overflow-check.c diff --git a/build-aux/check-mescc.sh b/build-aux/check-mescc.sh index 0ac7216c..9789971b 100755 --- a/build-aux/check-mescc.sh +++ b/build-aux/check-mescc.sh @@ -2,6 +2,7 @@ # GNU Mes --- Maxwell Equations of Software # Copyright © 2017,2018,2019,2020,2022 Jan (janneke) Nieuwenhuizen +# Copyright © 2023 Andrius Štikonas # # This file is part of GNU Mes. # @@ -134,6 +135,7 @@ lib/tests/scaffold/63-struct-cell.c lib/tests/scaffold/64-make-cell.c lib/tests/scaffold/65-read.c lib/tests/scaffold/66-local-char-array.c +lib/tests/scaffold/67-m1-overflow-check.c " tcc_tests=" diff --git a/lib/tests/scaffold/67-m1-overflow-check.c b/lib/tests/scaffold/67-m1-overflow-check.c new file mode 100644 index 00000000..b3ea4803 --- /dev/null +++ b/lib/tests/scaffold/67-m1-overflow-check.c @@ -0,0 +1,25 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2023 Andrius Štikonas + * + * This file is part of GNU Mes. + * + * GNU Mes is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * GNU Mes is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Mes. If not, see . + */ + +int +main () +{ + return (0xe5894855 >> 31) - 1; +} diff --git a/module/mescc/compile.scm b/module/mescc/compile.scm index 65c16b32..06d5dc24 100644 --- a/module/mescc/compile.scm +++ b/module/mescc/compile.scm @@ -1,5 +1,6 @@ ;;; GNU Mes --- Maxwell Equations of Software ;;; Copyright © 2016,2017,2018,2019,2020,2021 Jan (janneke) Nieuwenhuizen +;;; Copyright © 2023 Andrius Štikonas ;;; ;;; This file is part of GNU Mes. ;;; diff --git a/module/mescc/x86_64/as.scm b/module/mescc/x86_64/as.scm index 7e438c6c..345399c2 100644 --- a/module/mescc/x86_64/as.scm +++ b/module/mescc/x86_64/as.scm @@ -1,5 +1,6 @@ ;;; GNU Mes --- Maxwell Equations of Software ;;; Copyright © 2018 Jan (janneke) Nieuwenhuizen +;;; Copyright © 2023 Andrius Štikonas ;;; ;;; This file is part of GNU Mes. ;;; @@ -82,7 +83,7 @@ (or v (error "invalid value: x86_64:value->r: " v)) (let ((r (get-r info))) (if (and (>= v 0) - (< v #xffffffff)) + (< v #x80000000)) `((,(string-append "mov____$i32,%" r) (#:immediate ,v))) `((,(string-append "mov____$i64,%" r) (#:immediate8 ,v)))))) @@ -520,7 +521,7 @@ (cond ((< (abs v) #x80) `((,(string-append "cmp____$i8,%" r) (#:immediate1 ,v)))) ((and (>= v 0) - (< v #xffffffff)) + (< v #x80000000)) `((,(string-append "cmp____$i32,%" r) (#:immediate ,v)))) (else `(,(string-append "mov____$i64,%r15") (#:immediate8 ,v) @@ -608,7 +609,7 @@ (cond ((< (abs v) #x80) `((,(string-append "addl___$i8,(%" r ")") (#:immediate1 ,v)))) ((and (>= v 0) - (< v #xffffffff)) + (< v #x80000000)) `((,(string-append "addl___$i32,(%" r ")") (#:immediate ,v)))) (else `((,(string-append "mov____$i64,%r15") (#:immediate8 ,v)) @@ -638,7 +639,7 @@ (define (x86_64:r-and info v) (let ((r (get-r info))) (if (and (>= v 0) - (< v #xffffffff)) + (< v #x80000000)) `((,(string-append "and____$i32,%" r) (#:immediate ,v))) `((,(string-append "mov____$i64,%r15") (#:immediate8 ,v)) (,(string-append "and____%r15,%" r))))))