From 4ce75e8f4b9e5ee67e00a3371cebc942bd3e7a3e Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Sun, 3 Dec 2017 16:33:35 +0100 Subject: [PATCH] mescc: Tinycc support: Respect word size better. * stage0/x86.M1 (movzwl_0x32(%ebp),%eax,movzwl_0x8(%ebp),%eax): New define. * module/mes/as-i386.mes (i386:push-word-local-de-ref, i386:word-local->accu): New function. * module/mes/as-i386.scm: Export them. * module/language/c99/compiler.mes (push-local-de-ref): Respect word size. (ident->accu): Likewise. --- module/language/c99/compiler.mes | 14 +++++++++----- module/mes/as-i386.mes | 14 ++++++++++++++ module/mes/as-i386.scm | 3 +++ stage0/x86.M1 | 2 ++ 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/module/language/c99/compiler.mes b/module/language/c99/compiler.mes index e45a6b46..f07edbb6 100644 --- a/module/language/c99/compiler.mes +++ b/module/language/c99/compiler.mes @@ -564,9 +564,11 @@ (ptr (local:pointer local)) (size (if (= ptr 1) (ast-type->size info (local:type o)) 4))) - (if (= size 1) - (wrap-as (i386:push-byte-local-de-ref (local:id o))) - (wrap-as (i386:push-local-de-ref (local:id o))))))) + (case size + ((1) (wrap-as (i386:push-byte-local-de-ref (local:id o)))) + ((2) (wrap-as (i386:push-word-local-de-ref (local:id o)))) + ((4) (wrap-as (i386:push-local-de-ref (local:id o)))) + (else (error (format #f "TODO: push size >4: ~a\n" size))))))) (define (push-local-de-de-ref info) (lambda (o) @@ -686,8 +688,10 @@ (size (if (= ptr 0) (ast-type->size info type) 4))) (cond ((< ptr 0) (wrap-as (i386:local-ptr->accu (local:id local)))) - (else (wrap-as (if (= size 1) (i386:byte-local->accu (local:id local)) - (i386:local->accu (local:id local))))))) + (else (wrap-as (case size + ((1) (i386:byte-local->accu (local:id local))) + ((2) (i386:word-local->accu (local:id local))) + (else (i386:local->accu (local:id local)))))))) (if global (let* ((ptr (ident->pointer info o))) (cond ((< ptr 0) (list (i386:label->accu `(#:address ,o)))) diff --git a/module/mes/as-i386.mes b/module/mes/as-i386.mes index c280fdd8..c4eca7c0 100644 --- a/module/mes/as-i386.mes +++ b/module/mes/as-i386.mes @@ -71,6 +71,14 @@ ("movzbl_(%eax),%eax") ("push___%eax")))) +(define (i386:push-word-local-de-ref n) + (or n (error "invalid value: push-word-local-de-ref: " n)) + (let ((n (- 0 (* 4 n)))) + `(,(if (< (abs n) #x80) `("mov____0x8(%ebp),%eax" (#:immediate1 ,n)) + `("mov____0x32(%ebp),%eax" (#:immediate ,n))) + ("movzwl_(%eax),%eax") + ("push___%eax")))) + (define (i386:push-byte-local-de-de-ref n) (or n (error "invalid value: push-byte-local-de-de-ref: " n)) (let ((n (- 0 (* 4 n)))) @@ -154,6 +162,12 @@ `(,(if (< (abs n) #x80) `("movzbl_0x8(%ebp),%eax" (#:immediate1 ,n)) `("movzbl_0x32(%ebp),%eax" (#:immediate ,n)))))) +(define (i386:word-local->accu n) + (or n (error "invalid value: word-local->accu: " n)) + (let ((n (- 0 (* 4 n)))) + `(,(if (< (abs n) #x80) `("movzwl_0x8(%ebp),%eax" (#:immediate1 ,n)) + `("movzwl_0x32(%ebp),%eax" (#:immediate ,n)))))) + (define (i386:byte-local->base n) (or n (error "invalid value: byte-local->base: " n)) (let ((n (- 0 (* 4 n)))) diff --git a/module/mes/as-i386.scm b/module/mes/as-i386.scm index ea6dd7e8..4df68864 100644 --- a/module/mes/as-i386.scm +++ b/module/mes/as-i386.scm @@ -47,6 +47,7 @@ i386:accu->local+n i386:byte-accu->local+n i386:word-accu->local+n + i386:accu->local+n i386:accu-and-base i386:accu-base i386:accu-cmp-value @@ -78,6 +79,7 @@ i386:byte-base-mem->accu i386:byte-base-sub i386:byte-local->accu + i386:word-local->accu i386:byte-local->base i386:byte-mem->accu i386:word-mem->accu @@ -125,6 +127,7 @@ i386:push-base i386:push-byte-local-de-de-ref i386:push-byte-local-de-ref + i386:push-word-local-de-ref i386:push-label i386:push-label-mem i386:push-local diff --git a/stage0/x86.M1 b/stage0/x86.M1 index c6f74522..ca7cf5d5 100644 --- a/stage0/x86.M1 +++ b/stage0/x86.M1 @@ -150,7 +150,9 @@ DEFINE movzbl_0x8(%ebp),%eax 0fb645 DEFINE movzbl_0x8(%ebp),%edx 0fb655 DEFINE movzwl_(%eax),%eax 0fb700 DEFINE movzwl_0x32(%eax),%eax 0fb780 +DEFINE movzwl_0x32(%ebp),%eax 0fb785 DEFINE movzwl_0x8(%eax),%eax 0fb740 +DEFINE movzwl_0x8(%ebp),%eax 0fb745 DEFINE mul____%edx f7e2 DEFINE nop 90 DEFINE not____%eax f7d0