guile/module/system/base/compile.scm

146 lines
4.2 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)
2001-04-16 03:43:48 +00:00
:use-module (system vm core)
:use-module (system vm assemble)
:use-module (ice-9 regex))
;;;
;;; Compiler environment
;;;
2001-04-22 02:13:48 +00:00
(define-record (<cenv> vm language module))
2001-04-16 03:43:48 +00:00
(define-public (make-cenv . rest)
2001-04-22 02:13:48 +00:00
(apply <cenv> rest))
2001-04-16 03:43:48 +00:00
(define-public (syntax-error loc msg exp)
(throw 'syntax-error loc msg exp))
(define-public (call-with-compile-error-catch thunk)
2001-04-22 02:13:48 +00:00
(try (thunk)
((syntax-error loc msg exp)
(format #t "~A:~A: ~A: ~A" (car loc) (cdr loc) msg exp))))
2001-04-16 03:43:48 +00:00
;;;
;;; Compiler
;;;
2001-04-15 15:02:41 +00:00
(define scheme (lookup-language 'scheme))
2001-04-16 03:43:48 +00:00
(define-public (compile-file file . opts)
(let ((comp (compiled-file-name file)))
(catch #t
(lambda ()
(call-with-compile-error-catch
2001-04-15 15:02:41 +00:00
(lambda ()
(call-with-output-file comp
(lambda (port)
2001-04-16 03:43:48 +00:00
(let* ((source (read-file-in file scheme))
(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-array-write (objcode->string objcode) port)))))
2001-04-16 03:43:48 +00:00
(format #t "Wrote ~A\n" comp))))
(lambda (key . args)
2001-04-19 03:09:27 +00:00
(format #t "ERROR: During compiling ~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)))))
(define-public (load-source-file file . opts)
(let ((source (read-file-in file scheme)))
2001-04-19 03:09:27 +00:00
(apply compile-in source (current-module) scheme opts)))
2001-04-16 03:43:48 +00:00
(define-public (load-file file . opts)
(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-public (compiled-file-name file)
2001-04-15 15:02:41 +00:00
(let ((m (string-match "\\.[^.]*$" file)))
(string-append (if m (match:prefix m) file) ".go")))
2001-04-16 03:43:48 +00:00
;;;
;;; Scheme compiler interface
;;;
(define-public (read-file-in file lang)
(call-with-input-file file lang.read-file))
(define-public (compile-in x e lang . opts)
(catch 'result
(lambda ()
;; expand
(set! x (lang.expander x e))
(if (memq :e opts) (throw 'result x))
;; translate
(set! x (lang.translator 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)))
;;;
;;;
;;;
(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)))