From 2e0c1c0aff17f4897e8baec76863370cb5793053 Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Fri, 28 Jul 2017 15:27:25 +0200 Subject: [PATCH] mescc: Tinycc support: bugfix *(cast)foo = bar. * module/language/c99/compiler.mes (expr->accu): Thinko for de-ref assign. * stage0/x86.M1: Fix typos. * module/mes/as-i386.mes: Update for typos. * scaffold/tests/77-pointer-assign.c: Test it. * make.scm (add-scaffold-test): Build it. --- make.scm | 3 +- module/language/c99/compiler.mes | 8 ++-- module/mes/as-i386.mes | 4 +- scaffold/tests/77-pointer-assign.c | 65 ++++++++++++++++++++++++++++++ stage0/x86.M1 | 4 +- 5 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 scaffold/tests/77-pointer-assign.c diff --git a/make.scm b/make.scm index 107cad67..e99935e3 100755 --- a/make.scm +++ b/make.scm @@ -156,7 +156,8 @@ exec ${GUILE-guile} --no-auto-compile -L . -L guile -C . -C guile -s "$0" ${1+"$ "73-union" "74-multi-line-string" "75-struct-union" - "76-pointer-arithmetic")) + "76-pointer-arithmetic" + "77-pointer-assign")) (add-target (group "check-scaffold-tests/7" #:dependencies (filter (target-prefix? "check-scaffold/tests/7") %targets))) diff --git a/module/language/c99/compiler.mes b/module/language/c99/compiler.mes index 7ba89c92..f410d3b8 100644 --- a/module/language/c99/compiler.mes +++ b/module/language/c99/compiler.mes @@ -924,10 +924,12 @@ (size (if (= ptr 1) (ast-type->size info type) 4))) (append-text info (append (wrap-as (i386:accu->base)) - ((base->ident-address info) name))))) + ((base->ident-address info) name))))) ; FIXME: size ((de-ref ,expr) - (let ((info ((expr->base info) expr))) - (append-text info (wrap-as (i386:mem->base))))) ;; FIXME: size + (let* ((info ((expr->base info) expr)) + (ptr (expr->pointer info expr)) + (size (expr->size info expr))) + (append-text info (wrap-as (i386:accu->base-address))))) ((array-ref ,index (d-sel (ident ,field) (p-expr (ident ,struct)))) (let* ((info (append-text info (wrap-as (i386:push-accu)))) (info ((expr->accu* info) a)) diff --git a/module/mes/as-i386.mes b/module/mes/as-i386.mes index 4ffb7923..a4394f83 100644 --- a/module/mes/as-i386.mes +++ b/module/mes/as-i386.mes @@ -332,11 +332,11 @@ `("mov____$i32,0x32(%eax)" (#:immediate ,n) (#:immediate ,v))))) (define (i386:base->accu-address) - '(("mov____%edx,%(eax)"))) ; mov %edx,(%eax) + '(("mov____%edx,(%eax)"))) ; mov %edx,(%eax) (define (i386:base-address->accu-address) '(("mov____(%edx),%ecx") ; mov (%edx),%ecx - ("mov____%ecx,%(eax)"))) ; mov %ecx,(%eax) + ("mov____%ecx,(%eax)"))) ; mov %ecx,(%eax) (define (i386:byte-base->accu-address) '(("mov____%dl,(%eax)"))) ; mov %dl,(%eax) diff --git a/scaffold/tests/77-pointer-assign.c b/scaffold/tests/77-pointer-assign.c new file mode 100644 index 00000000..d8f063c9 --- /dev/null +++ b/scaffold/tests/77-pointer-assign.c @@ -0,0 +1,65 @@ +/* -*-comment-start: "//";comment-end:""-*- + * Mes --- Maxwell Equations of Software + * Copyright © 2017 Jan Nieuwenhuizen + * + * 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 . + */ + +#include "30-test.i" + + +struct baz { + int i; +}; + +struct foo { + int **bar; +}; + +void +add (void *ptab) +{ + void ***x = (void***)ptab; + bla: + *(void***)ptab = 0x11223344; +} + +void +add2 (void *ptab) +{ + void ***x = (void***)ptab; + bla: + *x = 0x22334455; +} + +int +test () +{ + int i; + int *p = &i; + struct foo f; + f.bar = &p; + eputs ("f.bar:"); eputs (itoa (f.bar)); eputs ("\n"); + add (&f.bar); + eputs ("f.bar:"); eputs (itoa (f.bar)); eputs ("\n"); + if (f.bar != 0x11223344) return 1; + add2 (&f.bar); + // FIXME + // eputs ("f.bar:"); eputs (itoa (f.bar)); eputs ("\n"); + // if (f.bar != 0x22334455) return 2; + + return 0; +} diff --git a/stage0/x86.M1 b/stage0/x86.M1 index fc3fd41b..696d6ac9 100644 --- a/stage0/x86.M1 +++ b/stage0/x86.M1 @@ -86,11 +86,11 @@ DEFINE mov____%eax,0x8(%edx) 8942 DEFINE mov____%ebp,%eax 89e8 DEFINE mov____%ebp,%ecx 89e9 DEFINE mov____%ebp,%edx 89ea -DEFINE mov____%ecx,%(eax) 8908 -DEFINE mov____%edx,%(eax) 8910 +DEFINE mov____%ecx,(%eax) 8908 DEFINE mov____%edx,%eax 89d0 DEFINE mov____%edx,%ebx 86d3 DEFINE mov____%edx,%ecx 89d1 +DEFINE mov____%edx,(%eax) 8910 DEFINE mov____%edx,0x32(%ebp) 8995 DEFINE mov____%edx,0x8(%ebp) 8955 DEFINE mov____%esp,%ebp 89e5