add PIC_ENABLE_STDIO flag

This commit is contained in:
Yuichi Nishiwaki 2015-06-19 14:03:52 +09:00
parent 20cb77bbbe
commit 3021e7f2b9
5 changed files with 67 additions and 4 deletions

View File

@ -76,7 +76,7 @@ test-r7rs: bin/picrin t/r7rs-tests.scm
test-contribs: bin/picrin $(CONTRIB_TESTS) test-contribs: bin/picrin $(CONTRIB_TESTS)
test-nostdlib: test-nostdlib:
$(CC) -I extlib/benz/include -D'PIC_ENABLE_LIBC=0' -D'PIC_ENABLE_FLOAT=0'-nostdlib -fPIC -shared -std=c89 -ansi -pedantic -Wall -Wextra -o lib/libbenz.so $(BENZ_SRCS) $(CC) -I extlib/benz/include -D'PIC_ENABLE_LIBC=0' -D'PIC_ENABLE_FLOAT=0' -D'PIC_ENABLE_STDIO=0' -nostdlib -fPIC -shared -std=c89 -ansi -pedantic -Wall -Wextra -o lib/libbenz.so $(BENZ_SRCS) etc/libc_polyfill.c -fno-stack-protector
rm -f lib/libbenz.so rm -f lib/libbenz.so
install: all install: all

20
etc/libc_polyfill.c Normal file
View File

@ -0,0 +1,20 @@
void abort()
{
while (1);
}
typedef char jmp_buf[1];
int setjmp(jmp_buf buf)
{
(void)buf;
return 0;
}
void longjmp(jmp_buf buf, int r)
{
(void)buf;
(void)r;
while (1);
}

View File

@ -209,6 +209,10 @@ strcpy(char *dst, const char *src)
# include <math.h> # include <math.h>
#endif #endif
#if PIC_ENABLE_STDIO
# include <stdio.h>
#endif
#if defined(__cplusplus) #if defined(__cplusplus)
} }
#endif #endif

View File

@ -17,6 +17,9 @@
/** no dependency on libc */ /** no dependency on libc */
/* #define PIC_ENABLE_LIBC 1 */ /* #define PIC_ENABLE_LIBC 1 */
/** use stdio or not */
/* #define PIC_ENABLE_STDIO 1 */
/** custom setjmp/longjmp */ /** custom setjmp/longjmp */
/* #define PIC_JMPBUF jmp_buf */ /* #define PIC_JMPBUF jmp_buf */
/* #define PIC_SETJMP(pic, buf) setjmp(buf) */ /* #define PIC_SETJMP(pic, buf) setjmp(buf) */
@ -93,6 +96,10 @@
# error cannot disable float support when nan boxing is on # error cannot disable float support when nan boxing is on
#endif #endif
#ifndef PIC_ENABLE_STDIO
# define PIC_ENABLE_STDIO 1
#endif
#ifndef PIC_JMPBUF #ifndef PIC_JMPBUF
# include <setjmp.h> # include <setjmp.h>
# define PIC_JMPBUF jmp_buf # define PIC_JMPBUF jmp_buf

View File

@ -4,8 +4,6 @@
#include "picrin.h" #include "picrin.h"
#include <stdio.h>
pic_value pic_value
pic_eof_object() pic_eof_object()
{ {
@ -28,6 +26,8 @@ pic_assert_port(pic_state *pic)
/* current-(input|output|error)-port */ /* current-(input|output|error)-port */
#if PIC_ENABLE_STDIO
static int static int
file_read(pic_state PIC_UNUSED(*pic), void *cookie, char *ptr, int size) { file_read(pic_state PIC_UNUSED(*pic), void *cookie, char *ptr, int size) {
FILE *file = cookie; FILE *file = cookie;
@ -118,6 +118,32 @@ pic_open_file(pic_state *pic, const char *name, int flags) {
return port; return port;
} }
#else
/* null file */
static int
null_read(pic_state PIC_UNUSED(*pic), void PIC_UNUSED(*cookie), char PIC_UNUSED(*ptr), int PIC_UNUSED(size)) {
return 0;
}
static int
null_write(pic_state PIC_UNUSED(*pic), void PIC_UNUSED(*cookie), const char PIC_UNUSED(*ptr), int size) {
return size;
}
static long
null_seek(pic_state PIC_UNUSED(*pic), void PIC_UNUSED(*cookie), long PIC_UNUSED(pos), int PIC_UNUSED(whence)) {
return 0;
}
static int
null_close(pic_state PIC_UNUSED(*pic), void PIC_UNUSED(*cookie)) {
return 0;
}
#endif
static void static void
pic_define_standard_port(pic_state *pic, const char *name, xFILE *file, int dir) pic_define_standard_port(pic_state *pic, const char *name, xFILE *file, int dir)
{ {
@ -854,7 +880,11 @@ pic_port_flush(pic_state *pic)
void void
pic_init_port(pic_state *pic) pic_init_port(pic_state *pic)
{ {
#define FILE_VTABLE { 0, file_read, file_write, file_seek, file_close } #if PIC_ENABLE_STDIO
# define FILE_VTABLE { 0, file_read, file_write, file_seek, file_close }
#else
# define FILE_VTABLE { 0, null_read, null_write, null_seek, null_close }
#endif
static const xFILE skel[3] = { static const xFILE skel[3] = {
{ { 0 }, 0, NULL, NULL, FILE_VTABLE, X_READ }, { { 0 }, 0, NULL, NULL, FILE_VTABLE, X_READ },
@ -866,9 +896,11 @@ pic_init_port(pic_state *pic)
pic->files[1] = skel[1]; pic->files[1] = skel[1];
pic->files[2] = skel[2]; pic->files[2] = skel[2];
#if PIC_ENABLE_STDIO
pic->files[0].vtable.cookie = stdin; pic->files[0].vtable.cookie = stdin;
pic->files[1].vtable.cookie = stdout; pic->files[1].vtable.cookie = stdout;
pic->files[2].vtable.cookie = stderr; pic->files[2].vtable.cookie = stderr;
#endif
pic_define_standard_port(pic, "current-input-port", xstdin, PIC_PORT_IN); pic_define_standard_port(pic, "current-input-port", xstdin, PIC_PORT_IN);
pic_define_standard_port(pic, "current-output-port", xstdout, PIC_PORT_OUT); pic_define_standard_port(pic, "current-output-port", xstdout, PIC_PORT_OUT);