From 4618afec945f4cd7ed7b871a5fb4a14a6f764788 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Tue, 25 Apr 2017 22:51:55 +0900 Subject: [PATCH] precompile library.scm --- Makefile | 2 +- lib/serialize.c | 8 +++++- tools/mklib.scm | 67 +++++++++---------------------------------------- 3 files changed, 20 insertions(+), 57 deletions(-) diff --git a/Makefile b/Makefile index 3571aa2c..ab36a943 100644 --- a/Makefile +++ b/Makefile @@ -51,7 +51,7 @@ picrin: $(PICRIN_OBJS) $(CONTRIB_OBJS) ext lib/libpicrin.a $(CC) $(CFLAGS) -o $@ $(PICRIN_OBJS) $(CONTRIB_OBJS) lib/libpicrin.a $(LDFLAGS) src/init_lib.c: piclib/library.scm - cat piclib/library.scm | bin/picrin-bootstrap tools/mklib.scm > src/init_lib.c + bin/picrin-bootstrap -c lib_rom piclib/library.scm | bin/picrin-bootstrap tools/mklib.scm > src/init_lib.c src/load_piclib.c: $(CONTRIB_LIBS) perl tools/mkloader.pl $(CONTRIB_LIBS) > $@ diff --git a/lib/serialize.c b/lib/serialize.c index df49b1b7..c5dffd75 100644 --- a/lib/serialize.c +++ b/lib/serialize.c @@ -80,6 +80,9 @@ dump_obj(pic_state *pic, pic_value obj, pic_value port) } dump1(pic, 0x03, port); dump_irep(pic, proc_ptr(pic, obj)->u.irep, port); + } else if (pic_char_p(pic, obj)) { + dump1(pic, 0x04, port); + dump1(pic, pic_char(pic, obj), port); } else { pic_error(pic, "dump: unsupported object", 1, obj); } @@ -160,7 +163,7 @@ load_obj(pic_state *pic, pic_value port) { int type, len; pic_value obj; - char *buf; + char *buf, c; struct irep *irep; struct proc *proc; type = load1(pic, port); @@ -187,6 +190,9 @@ load_obj(pic_state *pic, pic_value port) proc->u.irep = irep; proc->env = NULL; return obj_value(pic, proc); + case 0x04: + c = load1(pic, port); + return pic_char_value(pic, c); default: pic_error(pic, "load: unsupported object", 1, pic_int_value(pic, type)); } diff --git a/tools/mklib.scm b/tools/mklib.scm index 468385d4..48456c17 100644 --- a/tools/mklib.scm +++ b/tools/mklib.scm @@ -1,64 +1,21 @@ -(define (generate-rom) - - (define open-output-string open-output-bytevector) - (define (get-output-string port) - (list->string (map integer->char (bytevector->list (get-output-bytevector port))))) - - (define (with-output-to-string thunk) - (let ((port (open-output-string))) - (parameterize ((current-output-port port)) - (thunk) - (let ((s (get-output-string port))) - (close-port port) - s)))) - - (define text - (with-output-to-string - (lambda () - (write (read))))) - - (define (escape-string s) - (with-output-to-string - (lambda () - (string-for-each - (lambda (c) - (case c - ((#\\) (display "\\\\")) - ((#\") (display "\\\"")) - ((#\newline) (display "\\n")) - (else (display c)))) - s)))) - - (define (group-string i s) - (let loop ((t s) (n (string-length s)) (acc '())) - (if (= n 0) - (reverse acc) - (if (< n i) - (loop "" 0 (cons t acc)) - (loop (string-copy t i) (- n i) (cons (string-copy t 0 i) acc)))))) - - (define lines (map escape-string (group-string 80 text))) - - (let loop ((lines lines) (acc "")) - (if (null? lines) - acc - (loop (cdr lines) (string-append acc "\"" (car lines) "\",\n"))))) - - (for-each display `("#include \"picrin.h\"\n" "#include \"picrin/extra.h\"\n" "\n" - "static const char lib_rom[][80] = {\n" - ,(generate-rom) - "};\n" - "\n" + "static ")) + +(let loop () + (let ((c (read-u8))) + (unless (eof-object? c) + (write-u8 c) + (loop)))) + +(for-each + display + `("\n" "void\n" "pic_init_lib(pic_state *PIC_UNUSED(pic))\n" "{\n" - " pic_value port;\n" - " port = pic_fmemopen(pic, &lib_rom[0][0], strlen(&lib_rom[0][0]), \"r\");\n" - " pic_funcall(pic, \"eval\", 1, pic_funcall(pic, \"read\", 1, port));\n" + " pic_call(pic, pic_deserialize(pic, pic_blob_value(pic, lib_rom, sizeof lib_rom)), 0);\n" "}\n")) -