Debian Codegraph @

contrib/bytecmp/bytecmp.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; Package: C -*-
2 ;;;;
3 ;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya.
4 ;;;; Copyright (c) 1990, Giuseppe Attardi.
5 ;;;; Copyright (c) 2001, Juan Jose Garcia Ripoll
6 ;;;;
7 ;;;; This program is free software; you can redistribute it and/or
8 ;;;; modify it under the terms of the GNU Library General Public
9 ;;;; License as published by the Free Software Foundation; either
10 ;;;; version 2 of the License, or (at your option) any later version.
11 ;;;;
12 ;;;; See file '../Copyright' for full details.
13
14 ;;;; BYTECMP Fake compiler which is used as a replacement when we do not
15 ;;;; want or can have the real native compiler.
16
17 (in-package "EXT")
18
19 (defpackage "C"
20 (:use "CL")
21 (:export
22 "*SUPPRESS-COMPILER-WARNINGS*"
23 "*SUPPRESS-COMPILER-NOTES*"))
24
25 (defun bc-disassemble (thing)
26 (when (si::valid-function-name-p thing)
27 (setq thing (fdefinition thing)))
28 (cond ((null thing))
29 ((functionp thing)
30 (si::bc-disassemble thing))
31 ((and (consp thing)
32 (member (car thing) '(LAMBDA 'EXT:LAMBDA-BLOCK)))
33 (disassemble (compile nil thing)))
34 (t
35 (error 'simple-type-error
36 :datum thing
37 :expected-type '(OR FUNCTION (SATISFIES SI:VALID-FUNCTION-NAME-P))
38 :format-control "DISASSEMBLE cannot accept ~A."
39 :format-arguments (list thing))))
40 nil)
41
42 (defun bc-compile (name &optional (definition nil def-p) &aux (*print-pretty* nil))
43 (check-type name (or (satisfies si:valid-function-name-p) nil))
44 (when def-p (check-type definition (or function cons)))
45 (cond ((functionp definition)
46 (multiple-value-bind (form lexenv) (function-lambda-expression definition)
47 (when form
48 (cond ((eq lexenv t)
49 (warn "COMPILE can not compile C closures")
50 (return-from bc-compile (values definition t nil)))
51 (lexenv (setf definition (si:eval-with-env form lexenv)))
52 (t (setf definition (si:eval-with-env form nil nil nil :execute))))))
53 (when name (setf (fdefinition name) definition))
54 (return-from bc-compile (values (or name definition) nil nil)))
55 ((not (null definition))
56 (unless (member (car definition) '(LAMBDA EXT:LAMBDA-BLOCK))
57 (format t "~&;;; Error: Not a valid lambda expression: ~s." definition)
58 (return-from bc-compile (values nil t t)))
59 (setq definition (si:eval-with-env definition nil nil nil :execute))
60 (when name (setf (fdefinition name) definition))
61 (return-from bc-compile (values (or name definition) nil nil)))
62 ((not (fboundp name))
63 (error "Function name ~s is unbound." name))
64 ((typep (fdefinition name) 'standard-generic-function)
65 (warn "COMPILE can not compile generic functions yet.")
66 (return-from bc-compile (values name t nil)))
67 (T
68 (multiple-value-bind (form lexenv)
69 (function-lambda-expression (fdefinition name))
70 (when form
71 (cond ((eq lexenv t)
72 (warn "The bytecodes compiler can not compile C closures")
73 (return-from bc-compile (values definition t nil)))
74 (lexenv (setf definition (si:eval-with-env form lexenv)))
75 (t (setf definition (si:eval-with-env form nil nil nil :execute))))))
76 (when (null definition)
77 (warn "We have lost the original function definition for ~s." name)
78 (return-from bc-compile (values name t nil)))
79 (return-from bc-compile (values name nil nil)))))
80
81 (defun bc-compile-file-pathname (name &key (output-file name) (type :fasl)
82 verbose print c-file h-file data-file
83 shared-data-file system-p load external-format)
84 (declare (ignore load c-file h-file data-file shared-data-file system-p verbose print external-format))
85 (let ((extension "fasc"))
86 (case type
87 ((:fasl :fas) (setf extension "fasc"))
88 (t (error "In COMPILE-FILE-PATHNAME, the type ~A is unsupported." type)))
89 (make-pathname :type extension :defaults output-file)))
90
91 (defun bc-compile-file (input
92 &key
93 ((:verbose *compile-verbose*) *compile-verbose*)
94 ((:print *compile-print*) *compile-print*)
95 (load nil)
96 (external-format :default)
97 (output-file nil output-file-p)
98 &allow-other-keys &aux foo)
99 (setf output-file (if (and output-file-p (not (eql output-file t)))
100 (pathname output-file)
101 (bc-compile-file-pathname input)))
102 (when *compile-verbose*
103 (format t "~&;;; Compiling ~A." input))
104 (cond ((not (streamp input))
105 (let* ((ext:*source-location* (cons (truename input) 0))
106 (*compile-file-pathname* (pathname (merge-pathnames input)))
107 (*compile-file-truename* (truename input)))
108 (with-open-file (sin input :direction :input)
109 (bc-compile-file sin :output-file output-file))))
110 ((not output-file-p)
111 (error "COMPILE-FILE invoked with a stream input and no :OUTPUT-FILE"))
112 (t
113 (with-open-file (sout output-file :direction :output :if-exists :supersede
114 :if-does-not-exist :create
115 :external-format external-format)
116 (let ((binary
117 (let ((*package* *package*)
118 (*readtable* *readtable*)
119 (ext:*bytecodes-compiler* t))
120 (si::bc-compile-from-stream input))))
121 (sys:with-ecl-io-syntax
122 (write binary :stream sout :circle t :escape t :readably t :pretty nil))
123 (terpri sout)))))
124 (when load
125 (si::load-bytecodes output-file *compile-verbose* *load-print* :default))
126 (values (truename output-file) nil nil))
127
128 (defun install-bytecodes-compiler ()
129 (ext::package-lock (find-package :cl) nil)
130 (pushnew :ecl-bytecmp *features*)
131 (setf (fdefinition 'disassemble) #'bc-disassemble
132 (fdefinition 'compile) #'bc-compile
133 (fdefinition 'compile-file) #'bc-compile-file
134 (fdefinition 'compile-file-pathname) #'bc-compile-file-pathname)
135 (ext::package-lock (find-package :cl) t))
136
137 (defun install-c-compiler ()
138 (require :cmp)
139 (locally (declare (notinline install-c-compiler))
140 (funcall 'install-c-compiler)))
141
142 #-ecl-min
143 (progn
144 #+(and dlopen (not windows))
145 (sys::autoload "SYS:cmp" 'compile-file 'compile 'compile-file-pathname 'disassemble)
146 #-(and dlopen (not windows))
147 (install-bytecodes-compiler))
148
149 (provide '#:BYTECMP)
150
151 #-ecl-min
152 (package-lock "COMMON-LISP" t)