mescc: Tinycc support: Unsigned comparison.

* stage0/x86.M1 (ja32, jae32, jb32, jbe32): New macro.
* module/mes/as-i386.mes (i386:jump-a, i386:jump-ae, i386:jump-b,
  i386:jump-be): Use it in new function.
* module/mes/as-i386.scm: Export them.
* module/language/c99/compiler.mes (test-jump-label->info): Use them to
  support unsigned comparison.
* scaffold/tests/7s-unsigned-compare.c: Test it.
* build-aux/check-mescc.sh (tests): Run it.
This commit is contained in:
Jan Nieuwenhuizen 2018-05-19 11:41:53 +02:00
parent 4faeece057
commit b4d1c40aa8
No known key found for this signature in database
GPG key ID: F3C1A0D9C1D65273
6 changed files with 264 additions and 84 deletions

View file

@ -152,6 +152,7 @@
("short" . ,(make-type 'signed 2 #f))
("int" . ,(make-type 'signed 4 #f))
("long" . ,(make-type 'signed 4 #f))
("default" . ,(make-type 'signed 4 #f))
;;("long long" . ,(make-type 'signed 8 #f))
;;("long long int" . ,(make-type 'signed 8 #f))
@ -210,8 +211,8 @@
(,b (guard (bit-field? b)) b)
((char ,value) (get-type "char" info))
((enum-ref . _) (get-type "int" info))
((fixed ,value) (get-type "int" info))
((enum-ref . _) (get-type "default" info))
((fixed ,value) (get-type "default" info))
((float ,float) (get-type "float" info))
((void) (get-type "void" info))
@ -240,11 +241,11 @@
((fctn-call (p-expr (ident ,name)) . _)
(or (and=> (assoc-ref (.functions info) name) function:type)
(get-type "int" info)))
(get-type "default" info)))
((fctn-call (de-ref (p-expr (ident ,name))) . _)
(or (and=> (assoc-ref (.functions info) name) function:type)
(get-type "int" info)))
(get-type "default" info)))
((fixed-type ,type) (ast->type type info))
((float-type ,type) (ast->type type info))
@ -280,7 +281,7 @@
(let ((fields (append-map (struct-field info) fields)))
(make-type 'union (apply + (map field:size fields)) fields)))
((enum-def (enum-def-list . ,fields))
(get-type "int" info))
(get-type "default" info))
((d-sel (ident ,field) ,struct)
(let ((type0 (ast->type struct info)))
@ -487,7 +488,7 @@
(cond ((global? var) (global:type var))
((local? var) (local:type var))
((function? var) (function:type var))
((assoc-ref (.constants info) o) (assoc-ref (.types info) "int"))
((assoc-ref (.constants info) o) (assoc-ref (.types info) "default"))
((pair? var) (car var))
(else (stderr "ident->type ~s => ~s\n" o var)
#f))))
@ -1207,8 +1208,18 @@
(wrap-as (i386:sub-base))))))
((eq ,a ,b) ((binop->accu info) a b (append (i386:sub-base) (i386:z->accu))))
((ge ,a ,b) ((binop->accu info) a b (append (i386:sub-base) (i386:ge?->accu))))
((gt ,a ,b) ((binop->accu info) a b (append (i386:sub-base) (i386:g?->accu) (i386:accu-test))))
((ge ,a ,b)
(let* ((type-a (ast->type a info))
(type-b (ast->type b info))
(test->accu (if (or (unsigned? type-a) (unsigned? type-b)) i386:ae?->accu i386:ge?->accu)))
((binop->accu info) a b (append (i386:sub-base) (test->accu) (i386:accu-test)))))
((gt ,a ,b)
(let* ((type-a (ast->type a info))
(type-b (ast->type b info))
(test->accu (if (or (unsigned? type-a) (unsigned? type-b)) i386:a?->accu i386:g?->accu)))
((binop->accu info) a b (append (i386:sub-base) (test->accu) (i386:accu-test)))))
;; FIXME: set accu *and* flags
((ne ,a ,b) ((binop->accu info) a b (append (i386:push-accu)
@ -1220,8 +1231,18 @@
(i386:pop-accu))))
((ne ,a ,b) ((binop->accu info) a b (append (i386:sub-base) (i386:xor-zf))))
((le ,a ,b) ((binop->accu info) a b (append (i386:sub-base) (i386:le?->accu))))
((lt ,a ,b) ((binop->accu info) a b (append (i386:sub-base) (i386:l?->accu))))
((le ,a ,b)
(let* ((type-a (ast->type a info))
(type-b (ast->type b info))
(test->accu (if (or (unsigned? type-a) (unsigned? type-b)) i386:be?->accu i386:le?->accu)))
((binop->accu info) a b (append (i386:sub-base) (test->accu) (i386:accu-test)))))
((lt ,a ,b)
(let* ((type-a (ast->type a info))
(type-b (ast->type b info))
(test->accu (if (or (unsigned? type-a) (unsigned? type-b)) i386:b?->accu i386:l?->accu)))
((binop->accu info) a b (append (i386:sub-base) (test->accu) (i386:accu-test)))))
((or ,a ,b)
(let* ((info (expr->accu a info))
@ -1411,17 +1432,10 @@
(lambda (o)
(pmatch o
((expr) info)
;; unsigned
;; ((le ,a ,b) ((jump i386:jump-ncz) o)) ; ja
;; ((lt ,a ,b) ((jump i386:jump-nc) o)) ; jae
;; ((ge ,a ,b) ((jump i386:jump-ncz) o))
;; ((gt ,a ,b) ((jump i386:jump-nc) o))
((le ,a ,b) ((jump i386:jump-g) o))
((lt ,a ,b) ((jump i386:jump-ge) o))
((ge ,a ,b) ((jump i386:jump-l) o))
((gt ,a ,b) ((jump i386:jump-le) o))
((le ,a ,b) ((jump i386:jump-z) o))
((lt ,a ,b) ((jump i386:jump-z) o))
((ge ,a ,b) ((jump i386:jump-z) o))
((gt ,a ,b) ((jump i386:jump-z) o))
((ne ,a ,b) ((jump i386:jump-nz) o))
((eq ,a ,b) ((jump i386:jump-nz) o))
((not _) ((jump i386:jump-z) o))

View file

@ -459,20 +459,62 @@
;; signed
(define (i386:jump-g label)
`(("jg32 " (#:offset ,label)))) ; jg/jnle <n>
`(("jg32 " (#:offset ,label))))
;; signed
(define (i386:jump-ge label)
`(("jge32 " (#:offset ,label)))) ; jge/jnl <n>
`(("jge32 " (#:offset ,label))))
;; signed
(define (i386:jump-l label)
`(("jl32 " (#:offset ,label))))
;; signed
(define (i386:jump-le label)
`(("jle32 " (#:offset ,label))))
(define (i386:g?->accu)
'(("setg___%al")
("movzbl_%al,%eax")))
(define (i386:ge?->accu)
'(("setge__%al")
("movzbl_%al,%eax")))
(define (i386:l?->accu)
'(("setl___%al")
("movzbl_%al,%eax")))
(define (i386:le?->accu)
'(("setle__%al")
("movzbl_%al,%eax")))
;; unsigned
(define (i386:jump-a label)
`(("ja32 " (#:offset ,label))))
(define (i386:jump-ae label)
`(("jae32 " (#:offset ,label))))
(define (i386:jump-b label)
`(("jb32 " (#:offset ,label))))
(define (i386:jump-be label)
`(("jbe32 " (#:offset ,label))))
(define (i386:a?->accu)
'(("seta___%al")
("movzbl_%al,%eax")))
(define (i386:ae?->accu)
'(("setae__%al")
("movzbl_%al,%eax")))
(define (i386:b?->accu)
'(("setb___%al")
("movzbl_%al,%eax")))
(define (i386:be?->accu)
'(("setbe__%al")
("movzbl_%al,%eax")))
(define (i386:jump-nz label)
`(("jne32 " (#:offset ,label)))) ; jnz . + <n>
@ -494,22 +536,6 @@
(define (i386:base-sub)
`(("sub____%eax,%edx"))) ; sub %eax,%edx
(define (i386:g?->accu)
'(("setg___%al")
("movzbl_%al,%eax")))
(define (i386:ge?->accu)
'(("setge__%al")
("movzbl_%al,%eax")))
(define (i386:l?->accu)
'(("setl___%al")
("movzbl_%al,%eax")))
(define (i386:le?->accu)
'(("setle__%al")
("movzbl_%al,%eax")))
(define (i386:nz->accu)
'(("setne__%al") ; setne %al
("movzbl_%al,%eax"))) ; movzbl %al,%eax

View file

@ -92,6 +92,10 @@
i386:function-preamble
i386:jump
i386:jump
i386:jump-a
i386:jump-ae
i386:jump-b
i386:jump-be
i386:jump-byte-z
i386:jump-g
i386:jump-ge
@ -148,6 +152,10 @@
i386:ge?->accu
i386:l?->accu
i386:le?->accu
i386:a?->accu
i386:ae?->accu
i386:b?->accu
i386:be?->accu
i386:z->accu
i386:byte-accu
i386:signed-byte-accu

View file

@ -1,6 +1,6 @@
/* -*-comment-start: "//";comment-end:""-*-
* Mes --- Maxwell Equations of Software
* Copyright © 2017 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
* Copyright © 2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of Mes.
*
@ -37,89 +37,89 @@ test ()
if (f) return 1;
puts ("t: if (one != 1)\n");
if (one != 1) return 1;
if (one != 1) return 2;
puts ("t: if (1 != one)\n");
if (1 != one) return 1;
if (1 != one) return 3;
puts ("t: if (one > 1)\n");
if (one > 1) return 1;
if (one > 1) return 4;
puts ("t: if (one < 0)\n");
if (one < 0) return 1;
if (one < 0) return 5;
puts ("t: if (one <= 0)\n");
if (one <= 0) return 1;
if (one <= 0) return 6;
puts ("t: if (one >= 2)\n");
if (one >= 2) return 1;
if (one >= 2) return 7;
puts ("t: if (!1)\n");
if (!1) return 1;
if (!1) return 8;
puts ("t: if (one == 0)\n");
if (one == 0) return 1;
if (one == 0) return 9;
puts ("t: if (f != 0)\n");
if (one != 1) return 1;
if (one != 1) return 10;
puts ("t: if (1)\n");
if (1) goto ok0;
return 1;
return 111;
ok0:
puts ("t: if (0); return 1; else;\n");
if (0) return 1; else goto ok1;
if (0) return 12; else goto ok1;
ok1:
puts ("t: if (t)\n");
if (t) goto ok2;
return 1;
return 13;
ok2:
puts ("t: if (one > 0)\n");
if (one > 0) goto ok3;
return 1;
return 14;
ok3:
puts ("t: if (one < 2)\n");
if (one < 2) goto ok4;
return 1;
return 15;
ok4:
puts ("t: if (one >= 0)\n");
if (one >= 0) goto ok5;
return 1;
return 16;
ok5:
puts ("t: if (one >= 1)\n");
if (one >= 0) goto ok6;
return 1;
return 17;
ok6:
puts ("t: if (one <= 2)\n");
if (one <= 2) goto ok7;
return 1;
return 18;
ok7:
puts ("t: if (one <= 1)\n");
if (one <= 1) goto ok8;
return 1;
return 19;
ok8:
puts ("t: if (!0)\n");
if (!0) goto ok9;
return 1;
return 20;
ok9:
puts ("t: if (one == 1)\n");
if (one == 1) goto ok10;
return 1;
return 21;
ok10:
puts ("t: if (one != 0)\n");
if (one != 0) goto ok11;
return 1;
return 22;
ok11:
;
@ -128,84 +128,84 @@ test ()
puts ("t: i = one > 0\n");
i = one > 0;
if (!i) return 1;
if (!i) return 23;
puts ("t: i = one >= 1\n");
i = one >= 1;
if (!i) return 2;
if (!i) return 24;
puts ("t: i = one < 2\n");
i = one < 2;
if (!i) return 3;
if (!i) return 25;
puts ("t: i = one <= 1\n");
i = one <= 1;
if (!i) return 4;
if (!i) return 26;
puts ("t: i = 0 > one\n");
i = 0 > one;
if (i) return 5;
if (i) return 27;
puts ("t: i = 0 >= one\n");
i = 0 >= one;
if (i) return 6;
if (i) return 28;
puts ("t: i = 1 < one \n");
i = 1 < one;
if (i) return 7;
if (i) return 29;
puts ("t: i = 2 <= one\n");
i = 2 <= one;
if (i) return 8;
if (i) return 30;
puts ("t: i = m1 > -2\n");
i = m1 > -2;
if (!i) return 9;
if (!i) return 31;
puts ("t: i = m1 >= -1\n");
i = m1 >= -1;
if (!i) return 10;
if (!i) return 32;
puts ("t: i = m1 < 0\n");
i = m1 < 0;
if (!i) return 11;
if (!i) return 33;
puts ("t: i = m1 <= -1\n");
i = m1 <= -1;
if (!i) return 12;
if (!i) return 34;
puts ("t: i = -1 > m1\n");
i = -1 > m1;
if (i) return 13;
if (i) return 35;
puts ("t: i = -2 >= m1\n");
i = -2 >= m1;
if (i) return 14;
if (i) return 36;
puts ("t: i = -1 < m1 \n");
i = -1 < m1;
if (i) return 15;
if (i) return 37;
puts ("t: i = -2 <= m1\n");
i = 0 <= m1;
if (i) return 16;
if (i) return 38;
puts ("t: isid (0)\n");
if (isid (0)) return 17;
if (isid (0)) return 39;
puts ("t: isid (6)\n");
if (isid (6)) return 18;
if (isid (6)) return 40;
puts ("t: isid (a)\n");
if (isid ('a') != 1) return 19;
if (isid ('a') != 1) return 41;
puts ("t: isid ( )\n");
if (isid (' ')) return 20;
if (isid (' ')) return 42;
return 0;
}

View file

@ -0,0 +1,124 @@
/* -*-comment-start: "//";comment-end:""-*-
* Mes --- Maxwell Equations of Software
* Copyright © 2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of Mes.
*
* 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.
*
* 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 Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <limits.h>
int
main ()
{
{
int i = 0;
if (i < -1)
return 1;
}
{
unsigned u = 0;
if (-1 < u)
return 2;
}
{
int i = INT_MAX + 2;
if (INT_MAX < i)
return 3;
}
{
unsigned u = INT_MAX + 2;
if (u < INT_MAX)
return 4;
}
{
int i = -1;
if (-1 > 0)
return 5;
}
{
unsigned u = INT_MAX + 2;
if (INT_MAX > u)
return 6;
}
{
int i = INT_MAX + 2;
if (i > 0)
return 7;
}
{
unsigned u = 0;
if (u > -1)
return 8;
}
{
int i = 0;
if (i <= -1)
return 9;
}
{
unsigned u = 0;
if (-1 <= u)
return 10;
}
{
int i = INT_MAX + 2;
if (INT_MAX <= i)
return 11;
}
{
unsigned u = INT_MAX + 2;
if (u <= INT_MAX)
return 12;
}
{
int i = -1;
if (-1 >= 0)
return 13;
}
{
unsigned u = INT_MAX + 2;
if (INT_MAX >= u)
return 14;
}
{
int i = INT_MAX + 2;
if (i >= 0)
return 15;
}
{
unsigned u = 0;
if (u >= -1)
return 16;
}
return 0;
}

View file

@ -55,6 +55,10 @@ DEFINE hlt f4
DEFINE idiv___%ebx f7fb
DEFINE int cd
DEFINE int____$0x80 cd80
DEFINE ja32 0f87
DEFINE jae32 0f83
DEFINE jb32 0f82
DEFINE jbe32 0f86
DEFINE je32 0f84
DEFINE je8 74
DEFINE jg32 0f8f
@ -169,6 +173,10 @@ DEFINE push___0x32(%ebp) ffb5
DEFINE push___0x8(%ebp) ff75
DEFINE ret c3
DEFINE sahf 9e
DEFINE seta___%al 0f97c0
DEFINE setae__%al 0f93c0
DEFINE setb___%al 0f92c0
DEFINE setbe__%al 0f96c0
DEFINE sete___%al 0f94c0
DEFINE setg___%al 0f9fc0
DEFINE setge__%al 0f9dc0