mescc: Parse mlibc early, show progress.

* module/mes/libc.mes (_start, strlen, getchar, assert_fail, ungetc,
  putchar, fputc, eputs, fputs, puts, strcmp, itoa, isdigit, atoi,
  malloc, realloc, strncmp, c:getenv): Change to function, add
  progress.  Update callers.
* module/language/c99/compiler.mes (c99-input->info): Compile libc separately.
* guile/mescc.scm: Update progress.
* scripts/mescc.mes: Update progress.
This commit is contained in:
Jan Nieuwenhuizen 2017-04-24 19:09:54 +02:00
parent 8e6ae9ea5f
commit 26dcf7136b
4 changed files with 121 additions and 148 deletions

View File

@ -61,6 +61,9 @@ GUILE='~/src/guile-1.8/build/pre-inst-guile --debug -q' guile/mescc.scm
((equal? file "--version") ((equal? file "--version")
(format (current-error-port) "mescc.scm (mes) ~a\n" %version) (format (current-error-port) "mescc.scm (mes) ~a\n" %version)
(exit 0))) (exit 0)))
(format (current-error-port) "compiling: ~a\n" file) (format (current-error-port) "input: ~a\n" file)
(with-input-from-file file (with-input-from-file file
c99-input->elf))) c99-input->elf)))
(format (current-error-port) "compiler loaded\n")
(format (current-error-port) "calling ~s\n" (cons 'main (command-line)))

View File

@ -1824,7 +1824,7 @@
(formals (.formals o)) (formals (.formals o))
(text (formals->text formals)) (text (formals->text formals))
(locals (formals->locals formals))) (locals (formals->locals formals)))
(format (current-error-port) "compiling ~s\n" name) (format (current-error-port) "compiling: ~a\n" name)
(let loop ((statements (.statements o)) (let loop ((statements (.statements o))
(info (clone info #:locals locals #:function (.name o) #:text text))) (info (clone info #:locals locals #:function (.name o) #:text text)))
(if (null? statements) (clone info (if (null? statements) (clone info
@ -1841,14 +1841,18 @@
(loop (cdr elements) ((ast->info info) (car elements))))))) (loop (cdr elements) ((ast->info info) (car elements)))))))
(define (c99-input->info) (define (c99-input->info)
(stderr "COMPILE\n") (let* ((info (make <info>
(let* ((ast (c99-input->ast))
(info (make <info>
#:functions i386:libc #:functions i386:libc
#:types i386:type-alist)) #:types i386:type-alist))
(ast (append libc ast)) (foo (stderr "compiling: mlibc\n"))
(info (let loop ((info info) (libc libc))
(if (null? libc) info
(loop ((ast->info info) ((car libc))) (cdr libc)))))
(foo (stderr "parsing: input\n"))
(ast (c99-input->ast))
(foo (stderr "compiling: input\n"))
(info ((ast->info info) ast)) (info ((ast->info info) ast))
(info ((ast->info info) _start))) (info ((ast->info info) (_start))))
info)) info))
(define (write-any x) (define (write-any x)

View File

@ -31,9 +31,10 @@
(mes-use-module (nyacc lang c99 parser)) (mes-use-module (nyacc lang c99 parser))
(mes-use-module (mes libc-i386)))) (mes-use-module (mes libc-i386))))
(define _start (define (_start)
(let* ((argc-argv (i386:_start)) (let ((argc-argv (i386:_start)))
(ast (with-input-from-string (format (current-error-port) "parsing: _start\n")
(with-input-from-string
(string-append " (string-append "
char **g_environment; char **g_environment;
char ** char **
@ -51,12 +52,11 @@ _start ()
int r = main (); int r = main ();
exit (r); exit (r);
} }
") ") parse-c99)))
parse-c99)))
ast))
(define strlen (define (strlen)
(let* ((ast (with-input-from-string (format (current-error-port) "parsing: strlen\n")
(with-input-from-string
" "
int int
strlen (char const* s) strlen (char const* s)
@ -65,13 +65,11 @@ strlen (char const* s)
while (s[i]) i++; while (s[i]) i++;
return i; return i;
} }
" " parse-c99))
;;paredit:"
parse-c99)))
ast))
(define getchar (define (getchar)
(let* ((ast (with-input-from-string (format (current-error-port) "parsing: getchar\n")
(with-input-from-string
" "
int g_stdin = 0; int g_stdin = 0;
int ungetc_char = -1; int ungetc_char = -1;
@ -99,13 +97,11 @@ getchar ()
return i; return i;
} }
" " parse-c99))
;;paredit:"
parse-c99)))
ast))
(define assert_fail (define (assert_fail)
(let* ((ast (with-input-from-string (format (current-error-port) "parsing: assert_fail\n")
(with-input-from-string
" "
void void
assert_fail (char* s) assert_fail (char* s)
@ -118,13 +114,11 @@ assert_fail (char* s)
fail = 0; fail = 0;
*fail = 0; *fail = 0;
} }
" " parse-c99))
;;paredit:"
parse-c99)))
ast))
(define ungetc (define (ungetc)
(let* ((ast (with-input-from-string (format (current-error-port) "parsing: ungetc\n")
(with-input-from-string
" "
//#define assert(x) ((x) ? (void)0 : assert_fail (#x)) //#define assert(x) ((x) ? (void)0 : assert_fail (#x))
int int
@ -139,13 +133,11 @@ ungetc (int c, int fd)
ungetc_buf[ungetc_char] = c; ungetc_buf[ungetc_char] = c;
return c; return c;
} }
" " parse-c99))
;;paredit:"
parse-c99)))
ast))
(define putchar (define (putchar)
(let* ((ast (with-input-from-string (format (current-error-port) "parsing: putchar\n")
(with-input-from-string
" "
int int
putchar (int c) putchar (int c)
@ -153,13 +145,11 @@ putchar (int c)
write (1, (char*)&c, 1); write (1, (char*)&c, 1);
return 0; return 0;
} }
" " parse-c99))
;;paredit:"
parse-c99)))
ast))
(define fputc (define (fputc)
(let* ((ast (with-input-from-string (format (current-error-port) "parsing: fputc\n")
(with-input-from-string
" "
int int
fputc (int c, int fd) fputc (int c, int fd)
@ -167,13 +157,11 @@ fputc (int c, int fd)
write (fd, (char*)&c, 1); write (fd, (char*)&c, 1);
return 0; return 0;
} }
" " parse-c99))
;;paredit:"
parse-c99)))
ast))
(define eputs (define (eputs)
(let* ((ast (with-input-from-string (format (current-error-port) "parsing: eputs\n")
(with-input-from-string
" "
int int
eputs (char const* s) eputs (char const* s)
@ -182,13 +170,12 @@ eputs (char const* s)
write (2, s, i); write (2, s, i);
return 0; return 0;
} }
" " parse-c99))
;;paredit:"
parse-c99)))
ast))
(define fputs
(let* ((ast (with-input-from-string (define (fputs)
(format (current-error-port) "parsing: fputs\n")
(with-input-from-string
" "
int int
fputs (char const* s, int fd) fputs (char const* s, int fd)
@ -197,13 +184,11 @@ fputs (char const* s, int fd)
write (fd, s, i); write (fd, s, i);
return 0; return 0;
} }
" " parse-c99))
;;paredit:"
parse-c99)))
ast))
(define puts (define (puts)
(let* ((ast (with-input-from-string (format (current-error-port) "parsing: puts\n")
(with-input-from-string
" "
int int
puts (char const* s) puts (char const* s)
@ -212,13 +197,11 @@ puts (char const* s)
write (1, s, i); write (1, s, i);
return 0; return 0;
} }
" " parse-c99))
;;paredit:"
parse-c99)))
ast))
(define strcmp (define (strcmp)
(let* ((ast (with-input-from-string (format (current-error-port) "parsing: strcmp\n")
(with-input-from-string
" "
int int
strcmp (char const* a, char const* b) strcmp (char const* a, char const* b)
@ -229,13 +212,11 @@ strcmp (char const* a, char const* b)
} }
return *a - *b; return *a - *b;
} }
" " parse-c99))
;;paredit:"
parse-c99)))
ast))
(define itoa (define (itoa)
(let* ((ast (with-input-from-string (format (current-error-port) "parsing: itoa\n")
(with-input-from-string
" "
char itoa_buf[10]; char itoa_buf[10];
@ -265,13 +246,11 @@ itoa (int x)
return p+1; return p+1;
} }
" " parse-c99))
;;paredit:"
parse-c99)))
ast))
(define isdigit (define (isdigit)
(let* ((ast (with-input-from-string (format (current-error-port) "parsing: isdigit\n")
(with-input-from-string
" "
int int
isdigit (char c) isdigit (char c)
@ -280,13 +259,11 @@ isdigit (char c)
if (c>='0' && c<='9') return 1; if (c>='0' && c<='9') return 1;
return 0; return 0;
} }
" " parse-c99))
;;paredit:"
parse-c99)))
ast))
(define atoi (define (atoi)
(let* ((ast (with-input-from-string (format (current-error-port) "parsing: atoi\n")
(with-input-from-string
" "
int int
atoi (char const *s) atoi (char const *s)
@ -306,13 +283,11 @@ atoi (char const *s)
} }
return i * sign; return i * sign;
} }
" " parse-c99))
;;paredit:"
parse-c99)))
ast))
(define malloc (define (malloc)
(let* ((ast (with-input-from-string (format (current-error-port) "parsing: malloc\n")
(with-input-from-string
" "
//void *g_malloc_base = 0; //void *g_malloc_base = 0;
char *g_malloc_base = 0; char *g_malloc_base = 0;
@ -328,13 +303,11 @@ malloc (int size)
brk (p+size); brk (p+size);
return p; return p;
} }
" " parse-c99))
;;paredit:"
parse-c99)))
ast))
(define realloc (define (realloc)
(let* ((ast (with-input-from-string (format (current-error-port) "parsing: realloc\n")
(with-input-from-string
" "
//void * //void *
int * int *
@ -344,13 +317,11 @@ realloc (int *p, int size)
brk (g_malloc_base + size); brk (g_malloc_base + size);
return g_malloc_base; return g_malloc_base;
} }
" " parse-c99))
;;paredit:"
parse-c99)))
ast))
(define strncmp (define (strncmp)
(let* ((ast (with-input-from-string (format (current-error-port) "parsing: strncmp\n")
(with-input-from-string
" "
int int
strncmp (char const* a, char const* b, int length) strncmp (char const* a, char const* b, int length)
@ -358,13 +329,11 @@ strncmp (char const* a, char const* b, int length)
while (*a && *b && *a == *b && --length) {a++;b++;} while (*a && *b && *a == *b && --length) {a++;b++;}
return *a - *b; return *a - *b;
} }
" " parse-c99))
;;paredit:"
parse-c99)))
ast))
(define c:getenv (define (c:getenv)
(let* ((ast (with-input-from-string (format (current-error-port) "parsing: getenv\n")
(with-input-from-string
" "
char **g_environment; char **g_environment;
char const* char const*
@ -380,11 +349,7 @@ getenv (char const* s)
} }
return 0; return 0;
} }
" " parse-c99))
;;paredit:"
parse-c99)))
ast))
(define libc (define libc
(list (list

View File

@ -63,10 +63,11 @@ exit $r
(cdr mfiles))) (cdr mfiles)))
(mfile (if (null? mfiles) (string-append %docdir "examples/main.c") (mfile (if (null? mfiles) (string-append %docdir "examples/main.c")
(car mfiles)))) (car mfiles))))
(format (current-error-port) "compiling: ~a\n" mfile) (format (current-error-port) "input: ~a\n" mfile)
(with-input-from-file mfile (with-input-from-file mfile
c99-input->elf))) c99-input->elf)))
(format (current-error-port) "calling main, command-line=~s\n" (command-line)) (format (current-error-port) "compiler loaded\n")
(format (current-error-port) "calling ~s\n" (cons 'main (command-line)))
(main (command-line)) (main (command-line))
() ()