From 1322d99c22f53a93c7287408b47fa825c02d0de5 Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Sun, 12 Mar 2017 11:05:20 +0100 Subject: [PATCH] mescc: Support [for] itoa. * module/mes/libc-i386.mes (i386:accu%base): New function. * module/mes/libc-i386.scm: Export it. * module/language/c99/compiler.mes (expr->accu): Use it to support mod. * doc/examples/t.c (itoa): New function. (test): Test it. * doc/examples/mini-mes.c (itoa)[!__GNUC__]: New function. --- module/language/c99/compiler.mes | 46 ++++++++++++++++++++++++++++++++ module/mes/libc-i386.mes | 9 ++++--- module/mes/libc-i386.scm | 1 + scaffold/mini-mes.c | 14 +++++++--- scaffold/t.c | 34 ++++++++++++++++++++--- 5 files changed, 94 insertions(+), 10 deletions(-) diff --git a/module/language/c99/compiler.mes b/module/language/c99/compiler.mes index e007fd97..339bbdae 100644 --- a/module/language/c99/compiler.mes +++ b/module/language/c99/compiler.mes @@ -698,6 +698,38 @@ (list (lambda (f g ta t d) (i386:accu/base))))))) + ((mod ,a ,b) + (let* ((empty (clone info #:text '())) + (accu ((expr->accu empty) a)) + (base ((expr->base empty) b))) + (clone info #:text + (append text + (.text accu) + (.text base) + (list (lambda (f g ta t d) + (i386:accu%base))))))) + + ;; FIXME: c/p ast->info + ((lt ,a ,b) + (let* ((base ((expr->base info) a)) + (empty (clone base #:text '())) + (accu ((expr->accu empty) b))) + (clone info #:text + (append text + (.text base) + (.text accu) + (list (lambda (f g ta t d) + (i386:base-sub))))))) + + ;; FIXME: ...c/p ast->info + ((neg (p-expr (ident ,name))) + (clone info #:text (append text + ((ident->base info) name) + (list (lambda (f g ta t d) + (i386:value->accu 0))) + (list (lambda (f g ta t d) + (i386:base-sub)))))) + ;;((cast (type-name (decl-spec-list (type-spec (typename "SCM"))) (abs-declr (declr-fctn (declr-scope (abs-declr (pointer))) (param-list (param-decl (decl-spec-list (type-spec (typename "SCM")))))))) (d-sel (ident "function") (array-ref (d-sel (ident "cdr") (array-ref (p-expr (ident "fn")) (p-expr (ident "g_cells")))) (p-expr (ident "functions")))))) ((cast ,cast ,o) ((expr->accu info) o)) @@ -1272,6 +1304,7 @@ #:globals (append globals (list-tail (.globals body-info) (length globals))) #:locals locals))) + ;; FIXME: support break statement (see switch/case) ((while ,test ,body) (let* ((skip-info (lambda (body-length) (clone info #:text (append text @@ -1796,6 +1829,19 @@ ((base->ident-address info) name) ((ident-add info) name 1))))) + ;; *p-- = b; + ((expr-stmt (assn-expr (de-ref (post-dec (p-expr (ident ,name)))) (op ,op) ,b)) + (when (not (equal? op "=")) + (stderr "OOOPS0.0: op=~s\n" op) + barf) + (let* ((empty (clone info #:text '())) + (base ((expr->base empty) b))) + (clone info #:text + (append text + (.text base) + ((base->ident-address info) name) + ((ident-add info) name -1))))) + ;; CAR (x) = 0 ;; TYPE (x) = PAIR; ((expr-stmt (assn-expr (d-sel (ident ,field) . ,d-sel) (op ,op) ,b)) diff --git a/module/mes/libc-i386.mes b/module/mes/libc-i386.mes index f6ae7c54..70bc4935 100644 --- a/module/mes/libc-i386.mes +++ b/module/mes/libc-i386.mes @@ -140,14 +140,17 @@ (define (i386:accu-base) `(#x29 #xd0)) ; sub %edx,%eax -;; (define (i386:accu/base) -;; '(#xf7 #xf2)) ; div %edx,%eax - (define (i386:accu/base) '(#x86 #xd3 ; mov %edx,%ebx #x31 #xd2 ; xor %edx,%edx #xf7 #xf3)) ; div %ebx +(define (i386:accu%base) + '(#x86 #xd3 ; mov %edx,%ebx + #x31 #xd2 ; xor %edx,%edx + #xf7 #xf3 ; div %ebx + #x89 #xd0)) ; mov %edx,%eax + (define (i386:base->accu) '(#x89 #xd0)) ; mov %edx,%eax diff --git a/module/mes/libc-i386.scm b/module/mes/libc-i386.scm index 3f2762b7..a710b166 100644 --- a/module/mes/libc-i386.scm +++ b/module/mes/libc-i386.scm @@ -43,6 +43,7 @@ i386:accu+base i386:accu+value i386:accu/base + i386:accu%base i386:accu-base i386:accu-shl i386:base-sub diff --git a/scaffold/mini-mes.c b/scaffold/mini-mes.c index a1349ac9..62245d08 100644 --- a/scaffold/mini-mes.c +++ b/scaffold/mini-mes.c @@ -198,15 +198,22 @@ eputs (char const* s) write (2, s, i); return 0; } +#endif + +char itoa_buf[10]; char const* itoa (int x) { - static char buf[10]; - char *p = buf+9; + //static char itoa_buf[10]; + //char *p = buf+9; + char *p = itoa_buf; + p += 9; *p-- = 0; - int sign = x < 0; + //int sign = x < 0; + int sign; + sign = x < 0; if (sign) x = -x; @@ -221,7 +228,6 @@ itoa (int x) return p+1; } -#endif void assert_fail (char* s) diff --git a/scaffold/t.c b/scaffold/t.c index 7ba9cc84..5121c969 100644 --- a/scaffold/t.c +++ b/scaffold/t.c @@ -135,6 +135,35 @@ SCM cell_fun; #if 1 +char itoa_buf[10]; + +char const* +itoa (int x) +{ + //static char itoa_buf[10]; + //char *p = buf+9; + char *p = itoa_buf; + p += 9; + *p-- = 0; + + //int sign = x < 0; + int sign; + sign = x < 0; + if (sign) + x = -x; + + do + { + *p-- = '0' + (x % 10); + x = x / 10; + } while (x); + + if (sign) + *p-- = '-'; + + return p+1; +} + int add (int a, int b) { @@ -663,9 +692,8 @@ test (char *p) return 1; ok15: - puts ("t: for (i=1; i<5; ++i)\n"); - for (i=1; i<5; ++i); - if (i != 5) return i; + puts ("t: itoa (33) == \"33\"\n"); + if (strcmp (itoa (33), "33")) return 1; return struct_test (); }