guile/module/system/base/compile.scm

168 lines
4.9 KiB
Scheme
Raw Normal View History

2001-04-15 15:02:41 +00:00
;;; High-level compiler interface
;; Copyright (C) 2001 Free Software Foundation, Inc.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Code:
(define-module (system base compile)
2001-04-16 03:43:48 +00:00
:use-syntax (system base syntax)
2001-04-15 15:02:41 +00:00
:use-module (system base language)
2001-04-16 03:43:48 +00:00
:use-module (system il compile)
2001-04-22 02:13:48 +00:00
:use-module (system il glil)
:use-module ((system vm core)
:select (the-vm vm-load objcode->u8vector load-objcode))
2001-04-16 03:43:48 +00:00
:use-module (system vm assemble)
:use-module (ice-9 regex)
:export (syntax-error compile-file load-source-file load-file
compiled-file-name
enable inlining; speed! * module/system/il/inline.scm: New module, implements generic inlining of scheme functions. It even does the right thing regarding (define arity:nopt caddr) and such. So now there are many more inlines: the arithmetics, `apply', the caddr family, etc. This makes the benchmarks *much* faster. * module/language/scheme/translate.scm (trans): Remove the %scheme-primitives code in favor of the generic (scheme il inline) code. Adds inlining for +, -, =, etc. * src/vm.c (vm_puts): Fix to work. * module/system/base/compile.scm (system): Export load/compile also. * module/system/il/compile.scm (optimize): Further debitrotting, but I haven't tried this function yet. It seems that <ghil-inst> was what <ghil-inline> is. * module/system/il/ghil.scm (*core-primitives*, *macro-module*) (ghil-primitive-macro?, ghil-macro-expander, ghil-primitive?): Remove these unused things. * module/system/il/macros.scm: Removed, replaced with inline.scm. * module/system/vm/assemble.scm (stack->bytes): Before, the final serialization code did an (apply u8vector (apply append (map u8vector->list ...))). Aside from the misspelling of append-map, this ends up pushing all elements of the u8vector on the stack -- assuredly not what you want. But besides even that, I think that pushing more than 32k arguments on the stack brings out some other bug that I think was hidden before, because now we actually use the `apply' VM instruction. Further testing is needed here, I think. Fixed the code to be more efficient, which fixes the manifestation of this particular bug: a failure to self-compile after inlining was enabled. * module/system/vm/bootstrap.scm: New module, serves to bootstrap boot-9's `load-compiled'. That way when we load (system vm core), we're loading compiled code already. * module/system/vm/core.scm: Use (system vm bootstrap). * src/guilec.in: Use the bootstrap code, so that we really are compiling with an entirely compiled compiler. * module/system/repl/repl.scm (default-catch-handler): An attempt at making the repl print a backtrace; more work needed here. * module/system/vm/frame.scm (make-frame-chain): Fix some misspellings -- I think, anyway.
2008-05-25 13:13:15 +02:00
scheme-eval read-file-in compile-in
load/compile))
2001-04-16 03:43:48 +00:00
;;;
;;; Compiler environment
;;;
(define (syntax-error loc msg exp)
2001-04-16 03:43:48 +00:00
(throw 'syntax-error loc msg exp))
(define-macro (call-with-compile-error-catch thunk)
`(catch 'syntax-error
,thunk
(lambda (key loc msg exp)
(if (pair? loc)
(format #t "~A:~A: ~A: ~A~%" (car loc) (cdr loc) msg exp)
(format #t "unknown location: ~A: ~A~%" msg exp)))))
(export-syntax call-with-compile-error-catch)
2001-04-16 03:43:48 +00:00
;;;
;;; Compiler
;;;
2001-04-15 15:02:41 +00:00
(define (scheme) (lookup-language 'scheme))
2001-04-15 15:02:41 +00:00
(define (compile-file file . opts)
(let ((comp (compiled-file-name file))
(scheme (scheme)))
(catch 'nothing-at-all
2001-04-16 03:43:48 +00:00
(lambda ()
(call-with-compile-error-catch
(lambda ()
2001-04-15 15:02:41 +00:00
(call-with-output-file comp
(lambda (port)
(let* ((source (read-file-in file scheme))
2001-04-16 03:43:48 +00:00
(objcode (apply compile-in source (current-module)
scheme opts)))
2001-04-22 02:13:48 +00:00
(if (memq :c opts)
(pprint-glil objcode port)
(uniform-vector-write (objcode->u8vector objcode) port)))))
(format #t "wrote `~A'\n" comp))))
2001-04-16 03:43:48 +00:00
(lambda (key . args)
(format #t "ERROR: during compilation of ~A:\n" file)
2001-04-16 03:43:48 +00:00
(display "ERROR: ")
2001-04-19 03:09:27 +00:00
(apply format #t (cadr args) (caddr args))
2001-04-16 03:43:48 +00:00
(newline)
2001-04-19 03:09:27 +00:00
(format #t "ERROR: ~A ~A ~A\n" key (car args) (cadddr args))
2001-04-16 03:43:48 +00:00
(delete-file comp)))))
; (let ((c-f compile-file))
; ;; XXX: Debugging output
; (set! compile-file
; (lambda (file . opts)
; (format #t "compile-file: ~a ~a~%" file opts)
; (let ((result (apply c-f (cons file opts))))
; (format #t "compile-file: returned ~a~%" result)
; result))))
(define (load-source-file file . opts)
(let ((source (read-file-in file (scheme))))
(apply compile-in source (current-module) (scheme) opts)))
2001-04-16 03:43:48 +00:00
(define (load-file file . opts)
2001-04-16 03:43:48 +00:00
(let ((comp (compiled-file-name file)))
(if (file-exists? comp)
2001-04-19 03:09:27 +00:00
(load-objcode comp)
2001-04-16 03:43:48 +00:00
(apply load-source-file file opts))))
(define (compiled-file-name file)
(let ((base (basename file)))
(let ((m (string-match "\\.scm$" base)))
(string-append (if m (match:prefix m) base) ".go"))))
2001-04-16 03:43:48 +00:00
(define (scheme-eval x e)
(vm-load (the-vm) (compile-in x e (scheme))))
2001-05-02 15:05:05 +00:00
2001-04-16 03:43:48 +00:00
;;;
;;; Scheme compiler interface
;;;
(define (read-file-in file lang)
(call-with-input-file file (language-read-file lang)))
2001-04-16 03:43:48 +00:00
(define (compile-in x e lang . opts)
(save-module-excursion
(lambda ()
(catch 'result
(lambda ()
;; expand
(set! x ((language-expander lang) x e))
(if (memq :e opts) (throw 'result x))
;; translate
(set! x ((language-translator lang) x e))
(if (memq :t opts) (throw 'result x))
;; compile
(set! x (apply compile x e opts))
(if (memq :c opts) (throw 'result x))
;; assemble
(apply assemble x e opts))
(lambda (key val) val)))))
2001-04-16 03:43:48 +00:00
;;;
;;;
2001-04-16 03:43:48 +00:00
;;;
(define (compile-and-load file . opts)
(let ((comp (object-file-name file)))
(if (or (not (file-exists? comp))
(> (stat:mtime (stat file)) (stat:mtime (stat comp))))
(compile-file file))
(load-compiled-file comp)))
(define (load/compile file . opts)
(let* ((file (file-full-name file))
(compiled (object-file-name file)))
(if (or (not (file-exists? compiled))
(> (stat:mtime (stat file)) (stat:mtime (stat compiled))))
(apply compile-file file #f opts))
(if (memq #:b opts)
(apply vm-trace (the-vm) (load-objcode compiled) opts)
((the-vm) (load-objcode compiled)))))
(define (file-full-name filename)
(let* ((port (current-load-port))
(oldname (and port (port-filename port))))
(if (and oldname
(> (string-length filename) 0)
(not (char=? (string-ref filename 0) #\/))
(not (string=? (dirname oldname) ".")))
(string-append (dirname oldname) "/" filename)
filename)))