mescc: Support open, read.

* module/mes/libc-i386.mes (i386:write): Fix comment.
  (i386:open, i386:read): New functions.
* module/mes/libc-i386.scm: Export them.
* module/language/c99/compiler.mes (i386:libc): Add them.
  (getchar, putchar): New libc functions.
  (libc): Add them.
This commit is contained in:
Jan Nieuwenhuizen 2017-01-10 20:35:52 +01:00
parent 4726a6c9ce
commit b0e0ab014f
3 changed files with 74 additions and 4 deletions

View file

@ -941,6 +941,39 @@ strlen (char const* s)
parse-c99)))
ast))
(define getchar
(let* ((ast (with-input-from-string
"
int
getchar ()
{
char c;
int r = read (g_stdin, &c, 1);
//int r = read (0, &c, 1);
if (r < 1) return -1;
return c;
}
"
;;paredit:"
parse-c99)))
ast))
(define putchar
(let* ((ast (with-input-from-string
"
int
putchar (int c)
{
//write (STDOUT, s, strlen (s));
//int i = write (STDOUT, s, strlen (s));
write (1, (char*)&c, 1);
return 0;
}
"
;;paredit:"
parse-c99)))
ast))
(define eputs
(let* ((ast (with-input-from-string
"
@ -964,7 +997,7 @@ eputs (char const* s)
int
fputs (char const* s, int fd)
{
int i = strlen (s);
int i = strlen (s);
write (fd, s, i);
return 0;
}
@ -1010,11 +1043,15 @@ strcmp (char const* a, char const* b)
(define i386:libc
(list
(cons "exit" (list i386:exit))
(cons "open" (list i386:open))
(cons "read" (list i386:read))
(cons "write" (list i386:write))))
(define libc
(list
strlen
getchar
putchar
eputs
fputs
puts

View file

@ -233,14 +233,45 @@
#xcd #x80 ; int $0x80
))
(define (i386:open f g t d)
`(
#x55 ; push %ebp
#x89 #xe5 ; mov %esp,%ebp
#x8b #x5d #x08 ; mov 0x8(%ebp),%ebx
#x8b #x4d #x0c ; mov 0xc(%ebp),%ecx
#xb8 #x05 #x00 #x00 #x00 ; mov $0x5,%eax
#xcd #x80 ; int $0x80
#xc9 ; leave
#xc3 ; ret
))
(define (i386:read f g t d)
`(
#x55 ; push %ebp
#x89 #xe5 ; mov %esp,%ebp
#x8b #x5d #x08 ; mov 0x8(%ebp),%ebx
#x8b #x4d #x0c ; mov 0xc(%ebp),%ecx
#x8b #x55 #x10 ; mov 0x10(%ebp),%edx
#xb8 #x03 #x00 #x00 #x00 ; mov $0x3,%eax
#xcd #x80 ; int $0x80
#xc9 ; leave
#xc3 ; ret
))
(define (i386:write f g t d)
`(
#x55 ; push %ebp
#x89 #xe5 ; mov %esp,%ebp
#x8b #x5d #x08 ; mov $0x8(%ebp),%ebx
#x8b #x4d #x0c ; mov $0xc(%ebp),%ecx
#x8b #x55 #x10 ; mov $0x4(%ebp),%edx
#x8b #x5d #x08 ; mov 0x8(%ebp),%ebx
#x8b #x4d #x0c ; mov 0xc(%ebp),%ecx
#x8b #x55 #x10 ; mov 0x10(%ebp),%edx
#xb8 #x04 #x00 #x00 #x00 ; mov $0x4,%eax
#xcd #x80 ; int $0x80

View file

@ -83,6 +83,8 @@
;; libc
i386:exit
i386:open
i386:read
i386:write
))