1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-04-30 03:40:34 +02:00
guile/module/system/base/compile.scm
Andy Wingo b41b92c9d1 compilation passes return third value: the continuation environment
* module/system/base/compile.scm: Expect compile passes to produce three
  values, not two. The third is the "continuation environment", the
  environment that can be used to compile a subsequent expression from
  the same source language. For example, expansion-time side effects can
  set the current module, which would be reflected appropriately in the
  continuation environment.

* module/language/assembly/compile-bytecode.scm:
* module/language/bytecode/spec.scm:
* module/language/ecmascript/compile-ghil.scm:
* module/language/ghil/compile-glil.scm:
* module/language/glil/spec.scm:
* module/language/objcode/spec.scm:
* module/language/scheme/compile-ghil.scm:
* module/system/base/compile.scm: Update compile passes to return a
  continuation environment.
2009-04-16 15:20:40 +02:00

204 lines
6.4 KiB
Scheme
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

;;; High-level compiler interface
;; Copyright (C) 2001, 2009 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)
#:use-module (system base syntax)
#:use-module (system base language)
#:use-module (system vm vm) ;; FIXME: there's a reason for this, can't remember why tho
#:use-module (ice-9 regex)
#:use-module (ice-9 optargs)
#:use-module (ice-9 receive)
#:export (syntax-error
*current-language*
compiled-file-name compile-file compile-and-load
compile compile-time-environment
decompile)
#:export-syntax (call-with-compile-error-catch))
;;;
;;; Compiler environment
;;;
(define (syntax-error loc msg exp)
(throw 'syntax-error-compile-time loc msg exp))
(define-macro (call-with-compile-error-catch thunk)
`(catch 'syntax-error-compile-time
,thunk
(lambda (key loc msg exp)
(if (pair? loc)
(let ((file (or (assq-ref loc 'filename) "unknown file"))
(line (assq-ref loc 'line))
(col (assq-ref loc 'column)))
(format (current-error-port)
"~A:~A:~A: ~A: ~A~%" file line col msg exp))
(format (current-error-port)
"unknown location: ~A: ~S~%" msg exp)))))
;;;
;;; Compiler
;;;
(define *current-language* (make-fluid))
(fluid-set! *current-language* 'scheme)
(define (current-language)
(fluid-ref *current-language*))
(define (call-once thunk)
(let ((entered #f))
(dynamic-wind
(lambda ()
(if entered
(error "thunk may only be entered once: ~a" thunk))
(set! entered #t))
thunk
(lambda () #t))))
(define (call-with-output-file/atomic filename proc)
(let* ((template (string-append filename ".XXXXXX"))
(tmp (mkstemp! template)))
(call-once
(lambda ()
(with-throw-handler #t
(lambda ()
(proc tmp)
(chmod tmp (logand #o0666 (lognot (umask))))
(close-port tmp)
(rename-file template filename))
(lambda args
(delete-file template)))))))
(define (ensure-language x)
(if (language? x)
x
(lookup-language x)))
(define* (compile-file file #:optional output-file
#:key (to 'objcode) (opts '()))
(let ((comp (or output-file (compiled-file-name file)))
(lang (ensure-language (current-language)))
(to (ensure-language to)))
(catch 'nothing-at-all
(lambda ()
(call-with-compile-error-catch
(lambda ()
(call-with-output-file/atomic comp
(lambda (port)
(let ((print (language-printer to)))
(print (compile (read-file-in file lang)
#:from lang #:to to #:opts opts)
port))))
(format #t "wrote `~A'\n" comp))))
(lambda (key . args)
(format #t "ERROR: during compilation of ~A:\n" file)
(display "ERROR: ")
(apply format #t (cadr args) (caddr args))
(newline)
(format #t "ERROR: ~A ~A ~A\n" key (car args) (cadddr args))
(delete-file comp)))))
(define* (compile-and-load file #:key (to 'value) (opts '()))
(let ((lang (ensure-language (current-language))))
(compile (read-file-in file lang) #:to 'value #:opts opts)))
(define (compiled-file-name file)
(let ((base (basename file))
(cext (cond ((or (null? %load-compiled-extensions)
(string-null? (car %load-compiled-extensions)))
(warn "invalid %load-compiled-extensions"
%load-compiled-extensions)
".go")
(else (car %load-compiled-extensions)))))
(let lp ((exts %load-extensions))
(cond ((null? exts) (string-append file cext))
((string-null? (car exts)) (lp (cdr exts)))
((string-suffix? (car exts) base)
(string-append
(dirname file) "/"
(substring base 0
(- (string-length base) (string-length (car exts))))
cext))
(else (lp (cdr exts)))))))
;;;
;;; Compiler interface
;;;
(define (read-file-in file lang)
(call-with-input-file file
(or (language-read-file lang)
(error "language has no #:read-file" lang))))
(define (compile-passes from to opts)
(map cdr
(or (lookup-compilation-order from to)
(error "no way to compile" from "to" to))))
(define (compile-fold passes exp env opts)
(if (null? passes)
exp
(receive (exp env cenv) ((car passes) exp env opts)
(compile-fold (cdr passes) exp env opts))))
(define (compile-time-environment)
"A special function known to the compiler that, when compiled, will
return a representation of the lexical environment in place at compile
time. Useful for supporting some forms of dynamic compilation. Returns
#f if called from the interpreter."
#f)
(define* (compile x #:key
(env #f)
(from (current-language))
(to 'value)
(opts '()))
(compile-fold (compile-passes from to opts)
x
env
opts))
;;;
;;; Decompiler interface
;;;
(define (decompile-passes from to opts)
(map cdr
(or (lookup-decompilation-order from to)
(error "no way to decompile" from "to" to))))
(define (decompile-fold passes exp env opts)
(if (null? passes)
(values exp env)
(receive (exp env) ((car passes) exp env opts)
(decompile-fold (cdr passes) exp env opts))))
(define* (decompile x #:key
(env #f)
(from 'value)
(to 'assembly)
(opts '()))
(decompile-fold (decompile-passes from to opts)
x
env
opts))