bug-mes
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 1/2] Do not overflow M1 immediates.


From: Andrius Štikonas
Subject: [PATCH 1/2] Do not overflow M1 immediates.
Date: Fri, 14 Jul 2023 01:22:19 +0100

M1 immediates are limited to 31-bit before they become negative.

* module/mescc/x86_64/as.scm: limit M1 immediates to #x80000000.
* lib/tests/scaffold/67-m1-overflow-check.c: add a new test.
---
 build-aux/check-mescc.sh                  |  1 +
 lib/tests/scaffold/67-m1-overflow-check.c | 23 +++++++++++++++++++++++
 module/mescc/x86_64/as.scm                |  8 ++++----
 3 files changed, 28 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 c80c14e3..8fd5743e 100755
--- a/build-aux/check-mescc.sh
+++ b/build-aux/check-mescc.sh
@@ -132,6 +132,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..60c22aee
--- /dev/null
+++ b/lib/tests/scaffold/67-m1-overflow-check.c
@@ -0,0 +1,23 @@
+/* -*-comment-start: "//";comment-end:""-*-
+ * GNU Mes --- Maxwell Equations of Software
+ * Copyright © 2023 Andrius Štikonas <andrius@stikonas.eu>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+int main() {
+  return (0xe5894855 >> 31) - 1;
+}
diff --git a/module/mescc/x86_64/as.scm b/module/mescc/x86_64/as.scm
index 661a5184..6e9cf6d0 100644
--- a/module/mescc/x86_64/as.scm
+++ b/module/mescc/x86_64/as.scm
@@ -82,7 +82,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 +520,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 +608,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 +638,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))))))
-- 
2.41.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]