From 2eae07de728129026fcaf93ad3999a40fb48fb19 Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Sat, 6 May 2017 17:30:14 +0200 Subject: [PATCH] mescc: Support ==, != as expression value. * module/mes/as-i386.scm: Export them. * module/language/c99/compiler.mes (expr->accu): Set accu to 0/1 for eq, ne. * module/mes/as-i386.mes (i386:nz->accu, i386:z->accu, i386:accu<->stack): New functions. * scaffold/t.c (math_test): Test it. --- module/language/c99/compiler.mes | 15 ++++++++++++--- module/mes/as-i386.mes | 11 +++++++++++ module/mes/as-i386.scm | 4 ++++ scaffold/t.c | 6 ++++++ 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/module/language/c99/compiler.mes b/module/language/c99/compiler.mes index 1bc6900c..2b554781 100644 --- a/module/language/c99/compiler.mes +++ b/module/language/c99/compiler.mes @@ -712,11 +712,20 @@ (wrap-as (i386:value->accu 0)) (wrap-as (i386:sub-base))))) - ((eq ,a ,b) ((binop->accu info) a b (i386:sub-base))) + ((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))) ((gt ,a ,b) ((binop->accu info) b a (i386:sub-base))) - ((ne ,a ,b) ((binop->accu info) a b (append (i386:sub-base) - (i386:xor-zf)))) + + ;; FIXME: set accu *and* flags + ((ne ,a ,b) ((binop->accu info) a b (append (i386:push-accu) + (i386:sub-base) + (i386:nz->accu) + (i386:accu<->stack) + (i386:sub-base) + (i386:xor-zf) + (i386:pop-accu)))) + + ((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))) ((lt ,a ,b) ((binop->accu info) b a (i386:base-sub))) diff --git a/module/mes/as-i386.mes b/module/mes/as-i386.mes index 3ffa6448..0de8c92b 100644 --- a/module/mes/as-i386.mes +++ b/module/mes/as-i386.mes @@ -483,3 +483,14 @@ (define (i386:base-sub) `(#x29 #xc2)) ; sub %eax,%edx +(define (i386:nz->accu) + '(#x0f #x95 #xc0 ; setne %al + #x0f #xb6 #xc0)) ; movzbl %al,%eax + +(define (i386:z->accu) + '(#x0f #x94 #xc0 ; sete %al + #x0f #xb6 #xc0)) ; movzbl %al,%eax + +(define (i386:accu<->stack) + '(#x87 #x04 #x24)) ; xchg %eax,(%esp) + diff --git a/module/mes/as-i386.scm b/module/mes/as-i386.scm index b5e1c8f8..a55ddac8 100644 --- a/module/mes/as-i386.scm +++ b/module/mes/as-i386.scm @@ -148,6 +148,10 @@ i386:accu+n i386:base+n i386:base-address->accu-address + + i386:nz->accu + i386:z->accu + i386:accu<->stack )) (include-from-path "mes/as-i386.mes") diff --git a/scaffold/t.c b/scaffold/t.c index 227efca1..8ad92369 100644 --- a/scaffold/t.c +++ b/scaffold/t.c @@ -341,6 +341,12 @@ math_test () puts ("t: ^ 1 \n"); if ((1 ^ 3) != 2) return 1; + puts ("t: 3 == 3\n"); + if ((3 == 3) != 1) return 1; + + puts ("t: 3 != 3\n"); + if ((3 != 3) != 0) return 1; + return array_test (env); }