mescc: Compile specific for loop.

* module/language/c99/compiler.mes (ast->info): Handle for, pre-inc.
* GNUmakefile (main): New target.
* doc/examples/main.c:
  (exit, write, strlen,puts)[__GNUC__]: New functions; import from micro-mes.c
  (_start): New function.
This commit is contained in:
Jan Nieuwenhuizen 2017-01-05 21:24:56 +01:00
parent 4a3e419e30
commit 107795b13c
3 changed files with 122 additions and 2 deletions

View file

@ -45,6 +45,11 @@ micro-mes: doc/examples/micro-mes.c GNUmakefile
gcc -nostdlib --std=gnu99 -m32 -o micro-mes '-DVERSION="0.4"' $<
chmod +x $@
main: doc/examples/main.c GNUmakefile
rm -f $@
gcc -nostdlib --std=gnu99 -m32 -o main '-DVERSION="0.4"' $<
chmod +x $@
clean:
rm -f mes mes.o *.environment.i *.symbols.i *.environment.h *.cat a.out

View file

@ -1,3 +1,59 @@
#if __GNUC__
void
write (int fd, char const* s, int n)
{
int r;
//syscall (SYS_write, fd, s, n));
asm (
"mov %0, %%ebx\n\t"
"mov %1, %%ecx\n\t"
"mov %2, %%edx\n\t"
"mov $0x4, %%eax\n\t"
"int $0x80\n\t"
: // no outputs "=" (r)
: "" (fd), "" (s), "" (n)
: "eax", "ebx", "ecx", "edx"
);
}
void
exit (int code)
{
asm (
"movl %0, %%ebx\n\t"
"movl $1, %%eax\n\t"
"int $0x80"
: // no outputs "=" (r)
: "" (code)
);
// not reached
exit (0);
}
#define STDOUT 1
typedef long size_t;
size_t
strlen (char const* s)
{
int i = 0;
while (s[i]) i++;
return i;
}
int
puts (char const* s)
{
//write (STDOUT, s, strlen (s));
//int i = write (STDOUT, s, strlen (s));
int i = strlen (s);
write (1, s, i);
return 0;
}
#endif
int
main ()
{
@ -6,3 +62,12 @@ main ()
puts (" Hello, world!\n");
return 42;
}
#if __GNUC__
void
_start ()
{
int r=main ();
exit (r);
}
#endif

View file

@ -235,8 +235,7 @@
(append text (list (lambda (f g t d)
(i386:call f g t d
(+ t (function-offset name f))
(+ d (data-offset string globals
))))))
(+ d (data-offset string g))))))
#:globals globals))))
((expr-stmt (fctn-call (p-expr (ident ,name)) (expr-list . ,expr-list)))
@ -265,6 +264,50 @@
body-text)
#:globals (.globals body-info))))
(;;(for ,init ,test ,step ,body)
(for ,init
;; FIXME: ,test
(lt (p-expr (ident ,name)) (p-expr (fixed ,value)))
,step ,body)
(let* ((value (string->number value))
(info (clone info #:text '()))
(info ((ast->info info) init))
(init-text (.text info))
(init-locals (.locals info))
(info (clone info #:text '()))
(body-info ((ast->info info) body))
(body-text (.text body-info))
(body-length (length (text->list body-text)))
(step-info ((ast->info info) `(expr-stmt ,step)))
(step-text (.text step-info))
(step-length (length (text->list step-text)))
;; (test-info ((ast->info info) test))
;; (test-text (.text test-info))
;; (test-length (length (text->list test-text)))
)
(clone info #:text
(append text
init-text
(list (lambda (f g t d) (i386:jump body-length)))
body-text
step-text
;;test-text
;;(list (lambda (f g t d) (i386:jump-nz (- (+ body-length test-length)))))
(list (lambda (f g t d)
(append
(i386:local-test (assoc-ref init-locals name) value)
(i386:jump-le (- (+ body-length step-length 2) ;;test-length
)))))
)
#:globals (append globals (.globals body-info))
#:locals locals)))
((while ,test ,body)
(let* ((info (clone info #:text '()))
(body-info ((ast->info info) body))
@ -300,11 +343,18 @@
((ident->accu locals) index)
(i386:mem-byte->accu))))))) ; FIXME: type: char
;; i++
((expr-stmt (post-inc (p-expr (ident ,name))))
(clone info #:text
(append text (list (lambda (f g t d)
(i386:local-add (assoc-ref locals name) 1))))))
;; ++i -- same for now FIXME
((expr-stmt (pre-inc (p-expr (ident ,name))))
(clone info #:text
(append text (list (lambda (f g t d)
(i386:local-add (assoc-ref locals name) 1))))))
((return ,expr)
(clone info #:text
(append text (list (i386:ret ((expr->accu info) expr))))))