From 9a36cf22d0c9ec051ec69387f8219e462887a478 Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Sun, 7 Oct 2018 15:15:45 +0200 Subject: [PATCH] mescc: Another attempt at divide. * module/mescc/i386/as.scm (i386:r0/r1): Never be signed. Use div for unsigned. (i386:r0%r1): Likewise. * module/mescc/x86_64/as.scm (x86_64:r0/r1): Likewise. (x86_64:r0%r1): Likewise. * lib/x86-mes/x86.M1: Add div. * lib/x86_64-mes/x86_64.M1: Likewise. --- lib/x86-mes/x86.M1 | 3 ++- lib/x86_64-mes/x86_64.M1 | 3 ++- module/mescc/i386/as.scm | 14 ++++++++------ module/mescc/x86_64/as.scm | 14 ++++++++------ 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/lib/x86-mes/x86.M1 b/lib/x86-mes/x86.M1 index 4e850b34..c36e46ee 100644 --- a/lib/x86-mes/x86.M1 +++ b/lib/x86-mes/x86.M1 @@ -17,7 +17,7 @@ ### along with GNU Mes. If not, see . # reduced instruction set: eax, ebx (some ecx for shift, edx for mul, div) -# 191 instructions +# 193 instructions DEFINE add____$i32,%eax 05 DEFINE add____$i32,%ebx 81c3 @@ -53,6 +53,7 @@ DEFINE cltd 99 DEFINE cmp____$0x32,%eax 3d DEFINE cmp____$i32,%eax 3d DEFINE cmp____$i8,%eax 83f8 +DEFINE div___%ebx f7f3 DEFINE hlt f4 DEFINE idiv___%ebx f7fb DEFINE int cd diff --git a/lib/x86_64-mes/x86_64.M1 b/lib/x86_64-mes/x86_64.M1 index a7b26bcc..364adb7d 100644 --- a/lib/x86_64-mes/x86_64.M1 +++ b/lib/x86_64-mes/x86_64.M1 @@ -18,7 +18,7 @@ # reduced instruction set: rax, rdi (some rcx for shift, rdx for mul, div) # and r10 as i64 immediate helper -# 211 instructions +# 212 instructions DEFINE add____$i32,%rax 4805 DEFINE add____$i32,%rbp 4881c5 @@ -57,6 +57,7 @@ DEFINE cmp____$i8,%rax 4883f8 DEFINE cmp____%r15,%rax 4c39f8 DEFINE cmp____%r15,%rdi 4c39ff DEFINE cqto 4899 +DEFINE div___%rdi 48f7f7 DEFINE hlt f4 DEFINE idiv___%rdi 48f7ff DEFINE ja32 0f87 diff --git a/module/mescc/i386/as.scm b/module/mescc/i386/as.scm index 178ab98e..3256a323 100644 --- a/module/mescc/i386/as.scm +++ b/module/mescc/i386/as.scm @@ -355,7 +355,8 @@ `((,(string-append "and____%" r1 ",%" r0))))) (define (i386:r0/r1 info signed?) - (let ((allocated (.allocated info)) + (let ((signed? #f) ; nobody knows, -- all advice are belong to us? + (allocated (.allocated info)) (r0 (get-r0 info)) (r1 (get-r1 info))) (if (not (member "edx" allocated)) @@ -363,7 +364,7 @@ `(("push___%eax") (,(string-append "mov____%" r0 ",%eax")))) ,(if signed? '("cltd") '("xor____%edx,%edx")) - (,(string-append "idiv___%" r1)) + ,(if signed? `(,(string-append "idiv___%" r1)) `(,(string-append "div___%" r1))) ,@(if (equal? r0 "eax") '() `((,(string-append "mov____%eax,%" r0)) ("pop____%eax")))) @@ -373,14 +374,15 @@ (,(string-append "mov____%" r1 ",%ebx")) (,(string-append "mov____%" r0 ",%eax")) ,(if signed? '("cltd") '("xor____%edx,%edx")) - (,(string-append "idiv___%ebx")) + ,(if signed? `(,(string-append "idiv___%ebx")) `(,(string-append "div___%ebx"))) ("pop____%edx") ("pop____%ebx") (,(string-append "mov____%eax,%" r0)) ("pop____%eax"))))) (define (i386:r0%r1 info signed?) - (let ((allocated (.allocated info)) + (let ((signed? #f) ; nobody knows, -- all advice are belong to us? + (allocated (.allocated info)) (r0 (get-r0 info)) (r1 (get-r1 info))) (if (not (member "edx" allocated)) @@ -388,7 +390,7 @@ `(("push___%eax") (,(string-append "mov____%" r0 ",%eax")))) ,(if signed? '("cltd") '("xor____%edx,%edx")) - (,(string-append "idiv___%" r1)) + ,(if signed? `(,(string-append "idiv___%" r1)) `(,(string-append "div___%" r1))) (,(string-append "mov____%edx,%" r0))) `(("push___%eax") ("push___%ebx") @@ -396,7 +398,7 @@ (,(string-append "mov____%" r1 ",%ebx")) (,(string-append "mov____%" r0 ",%eax")) ,(if signed? '("cltd") '("xor____%edx,%edx")) - (,(string-append "idiv___%ebx")) + ,(if signed? `(,(string-append "idiv___%ebx")) `(,(string-append "div___%ebx"))) ("pop____%edx") ("pop____%ebx") (,(string-append "mov____%edx,%" r0)) diff --git a/module/mescc/x86_64/as.scm b/module/mescc/x86_64/as.scm index 968d7c4b..9bc6d1f7 100644 --- a/module/mescc/x86_64/as.scm +++ b/module/mescc/x86_64/as.scm @@ -435,7 +435,8 @@ `((,(string-append "and____%" r1 ",%" r0))))) (define (x86_64:r0/r1 info signed?) - (let ((allocated (.allocated info)) + (let ((signed? #f) ; nobody knows, -- all advice are belong to us? + (allocated (.allocated info)) (r0 (get-r0 info)) (r1 (get-r1 info))) (if (not (member "rdx" allocated)) @@ -443,7 +444,7 @@ `(("push___%rax") (,(string-append "mov____%" r0 ",%rax")))) ,(if signed? '("cqto") '("xor____%rdx,%rdx")) - (,(string-append "idiv___%" r1)) + ,(if signed? `(,(string-append "idiv___%" r1)) `(,(string-append "div___%" r1))) ,@(if (equal? r0 "rax") '() `((,(string-append "mov____%rax,%" r0)) ("pop____%rax")))) @@ -453,14 +454,15 @@ (,(string-append "mov____%" r1 ",%rdi")) (,(string-append "mov____%" r0 ",%rax")) ,(if signed? '("cqto") '("xor____%rdx,%rdx")) - (,(string-append "idiv___%rdi")) + ,(if signed? `(,(string-append "idiv___%rdi")) `(,(string-append "div___%rdi"))) ("pop____%rdx") ("pop____%rdi") (,(string-append "mov____%rax,%" r0)) ("pop____%rax"))))) (define (x86_64:r0%r1 info signed?) - (let ((allocated (.allocated info)) + (let ((signed? #f) ; nobody knows, -- all advice are belong to us? + (allocated (.allocated info)) (r0 (get-r0 info)) (r1 (get-r1 info))) (if (not (member "rdx" allocated)) @@ -468,7 +470,7 @@ `(("push___%rax") (,(string-append "mov____%" r0 ",%rax")))) ,(if signed? '("cqto") '("xor____%rdx,%rdx")) - (,(string-append "idiv___%" r1)) + ,(if signed? `(,(string-append "idiv___%" r1)) `(,(string-append "div___%" r1))) (,(string-append "mov____%rdx,%" r0))) `(("push___%rax") ("push___%rdi") @@ -476,7 +478,7 @@ (,(string-append "mov____%" r1 ",%rdi")) (,(string-append "mov____%" r0 ",%rax")) ,(if signed? '("cqto") '("xor____%rdx,%rdx")) - (,(string-append "idiv___%rdi")) + ,(if signed? `(,(string-append "idiv___%rdi")) `(,(string-append "div___%rdi"))) ("pop____%rdx") ("pop____%rdi") (,(string-append "mov____%rdx,%" r0))