mescc: Tinycc support: fix accu value after comparison.

* stage0/x86.M1 (setg___%al,, setge__%al, setl___%al, setle__%al): New
  defines.
* module/mes/as-i386.mes (i386:g?->accu, i386:ge?->accu,
  i386:l?->accu, i386:le?->accu, i386:jumpl, i386:jumple): New functions.
* module/mes/as-i386.scm: Export them.
* module/language/c99/compiler.mes (expr->accu): Use them to fix accu
  value after comparisons.
  (test-jump-label->info): Update comparison jumps.
This commit is contained in:
Jan Nieuwenhuizen 2017-07-29 20:46:16 +02:00
parent f29479dfa2
commit 042022419e
5 changed files with 135 additions and 10 deletions

View file

@ -851,8 +851,8 @@
(wrap-as (i386:sub-base)))))) (wrap-as (i386:sub-base))))))
((eq ,a ,b) ((binop->accu info) a b (append (i386:sub-base) (i386:z->accu)))) ((eq ,a ,b) ((binop->accu info) a b (append (i386:sub-base) (i386:z->accu))))
((ge ,a ,b) ((binop->accu info) b a (i386:sub-base))) ((ge ,a ,b) ((binop->accu info) a b (append (i386:sub-base) (i386:ge?->accu))))
((gt ,a ,b) ((binop->accu info) b a (i386:sub-base))) ((gt ,a ,b) ((binop->accu info) a b (append (i386:sub-base) (i386:g?->accu) (i386:accu-test))))
;; FIXME: set accu *and* flags ;; FIXME: set accu *and* flags
((ne ,a ,b) ((binop->accu info) a b (append (i386:push-accu) ((ne ,a ,b) ((binop->accu info) a b (append (i386:push-accu)
@ -864,8 +864,8 @@
(i386:pop-accu)))) (i386:pop-accu))))
((ne ,a ,b) ((binop->accu info) a b (append (i386:sub-base) (i386:xor-zf)))) ((ne ,a ,b) ((binop->accu info) a b (append (i386:sub-base) (i386:xor-zf))))
((le ,a ,b) ((binop->accu info) b a (i386:base-sub))) ((le ,a ,b) ((binop->accu info) a b (append (i386:sub-base) (i386:le?->accu))))
((lt ,a ,b) ((binop->accu info) b a (i386:base-sub))) ((lt ,a ,b) ((binop->accu info) a b (append (i386:sub-base) (i386:l?->accu))))
((or ,a ,b) ((or ,a ,b)
(let* ((info ((expr->accu info) a)) (let* ((info ((expr->accu info) a))
@ -1426,8 +1426,8 @@
((le ,a ,b) ((jump i386:jump-g) o)) ((le ,a ,b) ((jump i386:jump-g) o))
((lt ,a ,b) ((jump i386:jump-ge) o)) ((lt ,a ,b) ((jump i386:jump-ge) o))
((ge ,a ,b) ((jump i386:jump-g) o)) ((ge ,a ,b) ((jump i386:jump-l) o))
((gt ,a ,b) ((jump i386:jump-ge) o)) ((gt ,a ,b) ((jump i386:jump-le) o))
((ne ,a ,b) ((jump i386:jump-nz) o)) ((ne ,a ,b) ((jump i386:jump-nz) o))
((eq ,a ,b) ((jump i386:jump-nz) o)) ((eq ,a ,b) ((jump i386:jump-nz) o))

View file

@ -210,9 +210,6 @@
(define (i386:accu->label label) (define (i386:accu->label label)
`(("mov____%eax,0x32" (#:address ,label)))) ; mov %eax,0x<label> `(("mov____%eax,0x32" (#:address ,label)))) ; mov %eax,0x<label>
(define (i386:accu-zero?)
'(("test___%eax,%eax")))
(define (i386:accu-shl n) (define (i386:accu-shl n)
(or n (error "invalid value: accu:shl n: " n)) (or n (error "invalid value: accu:shl n: " n))
`(("shl____$i8,%eax" (#:immediate1 ,n)))) ; shl $0x8,%eax `(("shl____$i8,%eax" (#:immediate1 ,n)))) ; shl $0x8,%eax
@ -373,6 +370,9 @@
("call___*%eax") ; call *%eax ("call___*%eax") ; call *%eax
("add____$i8,%esp" (#:immediate1 ,(* n 4))))) ; add $00,%esp ("add____$i8,%esp" (#:immediate1 ,(* n 4))))) ; add $00,%esp
(define (i386:accu-zero?)
'(("test___%eax,%eax")))
(define (i386:accu-negate) (define (i386:accu-negate)
'(("sete___%al") ; sete %al '(("sete___%al") ; sete %al
("movzbl_%al,%eax"))) ; movzbl %al,%eax ("movzbl_%al,%eax"))) ; movzbl %al,%eax
@ -411,6 +411,14 @@
(define (i386:jump-ge label) (define (i386:jump-ge label)
`(("jge32 " (#:offset ,label)))) ; jge/jnl <n> `(("jge32 " (#:offset ,label)))) ; jge/jnl <n>
;; signed
(define (i386:jump-l label)
`(("jl32 " (#:offset ,label))))
;; signed
(define (i386:jump-le label)
`(("jle32 " (#:offset ,label))))
(define (i386:jump-nz label) (define (i386:jump-nz label)
`(("jne32 " (#:offset ,label)))) ; jnz . + <n> `(("jne32 " (#:offset ,label)))) ; jnz . + <n>
@ -432,6 +440,22 @@
(define (i386:base-sub) (define (i386:base-sub)
`(("sub____%eax,%edx"))) ; sub %eax,%edx `(("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) (define (i386:nz->accu)
'(("setne__%al") ; setne %al '(("setne__%al") ; setne %al
("movzbl_%al,%eax"))) ; movzbl %al,%eax ("movzbl_%al,%eax"))) ; movzbl %al,%eax

View file

@ -84,6 +84,8 @@
i386:jump-byte-z i386:jump-byte-z
i386:jump-g i386:jump-g
i386:jump-ge i386:jump-ge
i386:jump-l
i386:jump-le
i386:jump-nz i386:jump-nz
i386:jump-z i386:jump-z
i386:label->accu i386:label->accu
@ -128,6 +130,10 @@
i386:value->local i386:value->local
i386:xor-accu i386:xor-accu
i386:xor-zf i386:xor-zf
i386:g?->accu
i386:ge?->accu
i386:l?->accu
i386:le?->accu
i386:z->accu i386:z->accu
)) ))

View file

@ -21,6 +21,10 @@
#include "30-test.i" #include "30-test.i"
#include <stdio.h> #include <stdio.h>
int isid(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
}
int int
test () test ()
{ {
@ -117,6 +121,91 @@ test ()
if (one != 0) goto ok11; if (one != 0) goto ok11;
return 1; return 1;
ok11: ok11:
;
int m1 = -1;
int i;
puts ("t: i = one > 0\n");
i = one > 0;
if (!i) return 1;
puts ("t: i = one >= 1\n");
i = one >= 1;
if (!i) return 2;
puts ("t: i = one < 2\n");
i = one < 2;
if (!i) return 3;
puts ("t: i = one <= 1\n");
i = one <= 1;
if (!i) return 4;
puts ("t: i = 0 > one\n");
i = 0 > one;
if (i) return 5;
puts ("t: i = 0 >= one\n");
i = 0 >= one;
if (i) return 6;
puts ("t: i = 1 < one \n");
i = 1 < one;
if (i) return 7;
puts ("t: i = 2 <= one\n");
i = 2 <= one;
if (i) return 8;
puts ("t: i = m1 > -2\n");
i = m1 > -2;
if (!i) return 9;
puts ("t: i = m1 >= -1\n");
i = m1 >= -1;
if (!i) return 10;
puts ("t: i = m1 < 0\n");
i = m1 < 0;
if (!i) return 11;
puts ("t: i = m1 <= -1\n");
i = m1 <= -1;
if (!i) return 12;
puts ("t: i = -1 > m1\n");
i = -1 > m1;
if (i) return 13;
puts ("t: i = -2 >= m1\n");
i = -2 >= m1;
if (i) return 14;
puts ("t: i = -1 < m1 \n");
i = -1 < m1;
if (i) return 15;
puts ("t: i = -2 <= m1\n");
i = 0 <= m1;
if (i) return 16;
puts ("t: isid (0)\n");
if (isid (0)) return 17;
puts ("t: isid (6)\n");
if (isid (6)) return 18;
puts ("t: isid (a)\n");
if (isid ('a') != 1) return 19;
puts ("t: isid ( )\n");
if (isid (' ')) return 20;
return 0; return 0;
} }

View file

@ -52,11 +52,13 @@ DEFINE cmp____%edx,%eax 39d0
DEFINE idiv___%ebx f7fb DEFINE idiv___%ebx f7fb
DEFINE int____$0x80 cd80 DEFINE int____$0x80 cd80
DEFINE je32 0f84 DEFINE je32 0f84
DEFINE je8 74
DEFINE jg32 0f8f DEFINE jg32 0f8f
DEFINE jge32 0f8d DEFINE jge32 0f8d
DEFINE jl32 0f8c
DEFINE jle32 0f8e
DEFINE jmp32 e9 DEFINE jmp32 e9
DEFINE jne32 0f85 DEFINE jne32 0f85
DEFINE je8 74
DEFINE lahf 9f DEFINE lahf 9f
DEFINE lea____0x32(%ebp),%eax 8d85 DEFINE lea____0x32(%ebp),%eax 8d85
DEFINE lea____0x32(%ebp),%edx 8d95 DEFINE lea____0x32(%ebp),%edx 8d95
@ -136,6 +138,10 @@ DEFINE push___0x8(%ebp) ff75
DEFINE ret c3 DEFINE ret c3
DEFINE sahf 9e DEFINE sahf 9e
DEFINE sete___%al 0f94c0 DEFINE sete___%al 0f94c0
DEFINE setg___%al 0f9fc0
DEFINE setge__%al 0f9dc0
DEFINE setl___%al 0f9cc0
DEFINE setle__%al 0f9ec0
DEFINE setne__%al 0f95c0 DEFINE setne__%al 0f95c0
DEFINE shl____$i8,%eax c1e0 DEFINE shl____$i8,%eax c1e0
DEFINE shl____%cl,%eax d3e0 DEFINE shl____%cl,%eax d3e0