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.
This commit is contained in:
Jan Nieuwenhuizen 2017-03-12 11:05:20 +01:00
parent 3268027e46
commit 1322d99c22
5 changed files with 94 additions and 10 deletions

View file

@ -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))

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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 ();
}