| 1 |
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: C -*- |
| 2 |
;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab: |
| 3 |
|
| 4 |
;;;; |
| 5 |
;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya. |
| 6 |
;;;; Copyright (c) 1990, Giuseppe Attardi. |
| 7 |
;;;; Copyright (c) 2001, Juan Jose Garcia Ripoll |
| 8 |
;;;; Copyright (c) 2015, Daniel Kochmański |
| 9 |
;;;; |
| 10 |
;;;; See file 'LICENSE' for the copyright details. |
| 11 |
|
| 12 |
;;;; CMPMAIN Compiler main program. |
| 13 |
|
| 14 |
(ext:package-lock "CL" nil) |
| 15 |
|
| 16 |
(in-package "COMPILER") |
| 17 |
|
| 18 |
(defun compile-file-pathname (name &key |
| 19 |
(output-file T) |
| 20 |
(type nil type-supplied-p) |
| 21 |
(system-p nil) |
| 22 |
&allow-other-keys) |
| 23 |
(setf name (pathname name)) |
| 24 |
(let* ((format '()) |
| 25 |
(extension '())) |
| 26 |
(unless type-supplied-p |
| 27 |
(setf type (if system-p :object :fasl))) |
| 28 |
(case type |
| 29 |
((:shared-library :dll) (setf format +shared-library-format+)) |
| 30 |
((:static-library :library :lib) (setf format +static-library-format+)) |
| 31 |
(:data (setf extension "data")) |
| 32 |
(:sdata (setf extension "sdat")) |
| 33 |
(:c (setf extension (if (member :cxx-core *features*) "cxx" "c"))) |
| 34 |
(:h (setf extension "eclh")) |
| 35 |
(:object (setf extension +object-file-extension+)) |
| 36 |
(:program (setf format +executable-file-format+)) |
| 37 |
#+msvc |
| 38 |
(:import-library (setf extension "implib")) |
| 39 |
(:precompiled-header (setf format #-msvc "~a.h.gch" #+msvc "~a.pch")) |
| 40 |
((:fasl :fas) (setf extension "fas"))) |
| 41 |
(cond ((not (member output-file '(T NIL))) |
| 42 |
output-file) |
| 43 |
(format |
| 44 |
(merge-pathnames (format nil format (pathname-name name)) name)) |
| 45 |
(t |
| 46 |
(make-pathname :type extension :defaults name))))) |
| 47 |
|
| 48 |
(defun compile-file |
| 49 |
(input-pathname &rest args |
| 50 |
&key |
| 51 |
((:verbose *compile-verbose*) *compile-verbose*) |
| 52 |
((:print *compile-print*) *compile-print*) |
| 53 |
(source-truename nil) |
| 54 |
(source-offset 0) |
| 55 |
(c-file nil) |
| 56 |
(h-file nil) |
| 57 |
(data-file nil) |
| 58 |
(system-p nil) |
| 59 |
(load nil) |
| 60 |
(external-format :default) |
| 61 |
output-file |
| 62 |
&aux |
| 63 |
(*standard-output* *standard-output*) |
| 64 |
(*error-output* *error-output*) |
| 65 |
(*compiler-in-use* *compiler-in-use*) |
| 66 |
(*package* *package*) |
| 67 |
(*readtable* *readtable*) |
| 68 |
(*print-pretty* nil) |
| 69 |
(*compile-file-pathname* nil) |
| 70 |
(*compile-file-truename* nil) |
| 71 |
(ext:*source-location* (cons source-truename 0)) |
| 72 |
(*suppress-compiler-messages* (or *suppress-compiler-messages* |
| 73 |
(not *compile-verbose*)))) |
| 74 |
(declare (notinline compiler-cc) |
| 75 |
(ignorable c-file h-file data-file)) |
| 76 |
"Compiles the file specified by INPUT-PATHNAME and generates a fasl file |
| 77 |
specified by OUTPUT-FILE. If the filetype is not specified in INPUT-PATHNAME, |
| 78 |
then \".lsp\" is used as the default file type for the source file. LOAD |
| 79 |
specifies whether to load the generated fasl file after compilation. The |
| 80 |
:O-FILE, :C-FILE, :H-FILE, and :DATA-FILE keyword parameters allow you to |
| 81 |
control the intermediate files generated by the ECL compiler.If the file was |
| 82 |
compiled successfully, returns the pathname of the compiled file." |
| 83 |
#-dlopen |
| 84 |
(unless system-p |
| 85 |
(format t "~%;;;~ |
| 86 |
~%;;; This system does not support loading dynamically linked libraries.~ |
| 87 |
~%;;; Therefore, COMPILE-FILE without :SYSTEM-P T is unsupported.~ |
| 88 |
~%;;;")) |
| 89 |
(setq *compile-file-pathname* (pathname (merge-pathnames input-pathname))) |
| 90 |
(unless (probe-file *compile-file-pathname*) |
| 91 |
(if (pathname-type input-pathname) |
| 92 |
(error 'file-error :pathname input-pathname) |
| 93 |
(dolist (ext '("lsp" "LSP" "lisp" "LISP") |
| 94 |
(error 'file-error :pathname input-pathname)) |
| 95 |
(setq *compile-file-pathname* (make-pathname :type ext :defaults input-pathname)) |
| 96 |
(when (probe-file *compile-file-pathname*) |
| 97 |
(return))))) |
| 98 |
(when (and system-p load) |
| 99 |
(error "Cannot load system files.")) |
| 100 |
(cmpprogress "~&;;;~%;;; Compiling ~a." (namestring input-pathname)) |
| 101 |
(let* ((input-file (truename *compile-file-pathname*)) |
| 102 |
(*compile-file-truename* input-file) |
| 103 |
(*compiler-in-use* *compiler-in-use*) |
| 104 |
(*load-time-values* nil) ; Load time values are compiled. |
| 105 |
(output-file (apply #'compile-file-pathname input-file :output-file output-file args)) |
| 106 |
(true-output-file nil) ; Will be set at the end. |
| 107 |
(compiler-conditions nil)) |
| 108 |
(with-compiler-env (compiler-conditions) |
| 109 |
(print-compiler-info) |
| 110 |
(when (probe-file "./cmpinit.lsp") |
| 111 |
(load "./cmpinit.lsp" :verbose *compile-verbose*)) |
| 112 |
(with-open-file (stream *compile-file-pathname* :external-format external-format) |
| 113 |
(unless source-truename |
| 114 |
(setf (car ext:*source-location*) *compile-file-pathname*)) |
| 115 |
(compiler-pass1 stream source-offset)) |
| 116 |
(compiler-pass/propagate-types) |
| 117 |
(apply #'compiler-pass/assemble-cxx input-file output-file args) |
| 118 |
(if (setf true-output-file (probe-file output-file)) |
| 119 |
(cmpprogress "~&;;; Finished compiling ~a.~%;;;~%" (namestring input-pathname)) |
| 120 |
(cmperr "The C compiler failed to compile the intermediate file.")) |
| 121 |
(when load |
| 122 |
(load true-output-file :verbose *compile-verbose*))) ; with-compiler-env |
| 123 |
(compiler-output-values true-output-file compiler-conditions))) |
| 124 |
|
| 125 |
(defun compiler-output-values (main-value conditions) |
| 126 |
(loop for i in conditions |
| 127 |
with warning-p = nil |
| 128 |
with failure-p = nil |
| 129 |
do (cond ((typep i 'style-warning) |
| 130 |
(setf warning-p t)) |
| 131 |
((typep i '(or compiler-error warning)) |
| 132 |
(setf warning-p t failure-p t))) |
| 133 |
finally (return (values main-value warning-p failure-p)))) |
| 134 |
|
| 135 |
#-dlopen |
| 136 |
(defun compile (name &optional (def nil supplied-p)) |
| 137 |
(format t "~%;;;~ |
| 138 |
~%;;; This system does not support loading dynamically linked libraries.~ |
| 139 |
~%;;; Therefore, COMPILE is unsupported.~ |
| 140 |
~%;;;")) |
| 141 |
|
| 142 |
#+dlopen |
| 143 |
(defvar *gazonk-counter* 0) |
| 144 |
|
| 145 |
#+dlopen |
| 146 |
(defun compile (name &optional (def nil supplied-p) |
| 147 |
&aux form data-pathname |
| 148 |
(lexenv nil) |
| 149 |
(*suppress-compiler-messages* (or *suppress-compiler-messages* |
| 150 |
(not *compile-verbose*))) |
| 151 |
(*compiler-in-use* *compiler-in-use*) |
| 152 |
(*standard-output* *standard-output*) |
| 153 |
(*error-output* *error-output*) |
| 154 |
(*package* *package*) |
| 155 |
(*compile-print* nil) |
| 156 |
(*print-pretty* nil) |
| 157 |
(si:*compiler-constants* t)) |
| 158 |
"Args: (name &optional definition) |
| 159 |
|
| 160 |
If DEFINITION is NIL, NAME must be the name of a not-yet-compiled function. |
| 161 |
In this case, COMPILE compiles the function, installs the compiled function as |
| 162 |
the global function definition of NAME, and returns NAME. If DEFINITION is |
| 163 |
non-NIL, it must be a lambda expression and NAME must be a symbol. COMPILE |
| 164 |
compiles the lambda expression, installs the compiled function as the function |
| 165 |
definition of NAME, and returns NAME. There is only one exception for this: |
| 166 |
If NAME is NIL, then the compiled function is not installed but is simply |
| 167 |
returned as the value of COMPILE. In any case, COMPILE creates temporary |
| 168 |
files, whose filenames begin with \"gazonk\", which are automatically deleted |
| 169 |
after compilation." |
| 170 |
(unless (si:valid-function-name-p name) |
| 171 |
(error "~s is not a valid function name." name)) |
| 172 |
|
| 173 |
(cond ((and supplied-p def) |
| 174 |
(when (functionp def) |
| 175 |
(unless (function-lambda-expression def) |
| 176 |
(return-from compile def)) |
| 177 |
(multiple-value-setq (def lexenv) |
| 178 |
(function-lambda-expression def)) |
| 179 |
(when (eq lexenv t) |
| 180 |
(warn "COMPILE can not compile C closures") |
| 181 |
(return-from compile (values def t nil)))) |
| 182 |
(setq form (if name |
| 183 |
`(setf (fdefinition ',name) #',def) |
| 184 |
`(set 'GAZONK #',def)))) |
| 185 |
((not (fboundp name)) |
| 186 |
(error "Symbol ~s is unbound." name)) |
| 187 |
((typep (setf def (fdefinition name)) 'standard-generic-function) |
| 188 |
(warn "COMPILE can not compile generic functions yet") |
| 189 |
(return-from compile (values def t nil))) |
| 190 |
((null (setq form (function-lambda-expression def))) |
| 191 |
(warn "We have lost the original function definition for ~s. Compilation to C failed" |
| 192 |
name) |
| 193 |
(return-from compile (values def t nil))) |
| 194 |
(t |
| 195 |
(setq form `(setf (fdefinition ',name) #',form)))) |
| 196 |
|
| 197 |
(let* ((*load-time-values* 'values) ;; Only the value is kept |
| 198 |
(tmp-names (safe-mkstemp (format nil "TMP:ECL~3,'0x" (incf *gazonk-counter*)))) |
| 199 |
(so-pathname (compile-file-pathname (first tmp-names))) |
| 200 |
(compiler-conditions nil) |
| 201 |
(*permanent-data* t) ; needed for literal objects in closures |
| 202 |
(*cmp-env-root* *cmp-env-root*)) |
| 203 |
|
| 204 |
(with-compiler-env (compiler-conditions) |
| 205 |
(setf form (set-closure-env form lexenv *cmp-env-root*)) |
| 206 |
(compiler-pass1 form) |
| 207 |
(compiler-pass/propagate-types) |
| 208 |
(let (#+(or mingw32 msvc cygwin)(*self-destructing-fasl* t)) |
| 209 |
(compiler-pass/assemble-cxx nil so-pathname)) |
| 210 |
(mapc 'cmp-delete-file tmp-names) |
| 211 |
(cond ((probe-file so-pathname) |
| 212 |
(load so-pathname :verbose nil) |
| 213 |
(cmp-delete-file so-pathname)) |
| 214 |
(t |
| 215 |
(setf name nil) |
| 216 |
(set 'GAZONK nil) |
| 217 |
(cmperr "The C compiler failed to compile the intermediate code for ~s." name))) |
| 218 |
) ; with-compiler-env |
| 219 |
(let ((output (or name (and (boundp 'GAZONK) (symbol-value 'GAZONK)) |
| 220 |
#'(lambda (&rest x) |
| 221 |
(declare (ignore x)) |
| 222 |
;; if compilation failed, we return this |
| 223 |
;; function which does nothing but resignal |
| 224 |
;; the compiler errors we got |
| 225 |
(loop for c in compiler-conditions |
| 226 |
if (typep c 'compiler-error) |
| 227 |
do (apply #'si::simple-program-error |
| 228 |
(simple-condition-format-control c) |
| 229 |
(simple-condition-format-arguments c))))))) |
| 230 |
;; By unsetting GAZONK we avoid spurious references to the |
| 231 |
;; loaded code. |
| 232 |
(set 'GAZONK nil) |
| 233 |
(si::gc t) |
| 234 |
(compiler-output-values output compiler-conditions)))) |
| 235 |
|
| 236 |
(defun disassemble (thing &key (h-file nil) (data-file nil) |
| 237 |
&aux lexenv disassembled-form |
| 238 |
(*compiler-in-use* *compiler-in-use*) |
| 239 |
(*print-pretty* nil)) |
| 240 |
"Compiles the form specified by THING and prints the intermediate C language |
| 241 |
code for that form. But does not install the result of compilation. If THING |
| 242 |
is NIL, then the previously DISASSEMBLEd form is re-DISASSEMBLEd. If THING is |
| 243 |
a symbol that names a function not yet compiled, the function definition is |
| 244 |
disassembled. If THING is a lambda expression, it is disassembled as a |
| 245 |
function definition. Otherwise, THING itself is disassembled as a top-level |
| 246 |
form. H-FILE and DATA-FILE specify intermediate files to build a fasl file |
| 247 |
from the C language code. NIL means \"do not create the file\"." |
| 248 |
(when (si:valid-function-name-p thing) |
| 249 |
(setq thing (fdefinition thing))) |
| 250 |
(when (and (functionp thing) (function-lambda-expression thing)) |
| 251 |
(multiple-value-setq (thing lexenv) |
| 252 |
(function-lambda-expression thing)) |
| 253 |
(when (eq lexenv t) |
| 254 |
(warn "DISASSEMBLE can not disassemble C closures.") |
| 255 |
(return-from disassemble nil))) |
| 256 |
(cond ((null thing)) |
| 257 |
((functionp thing) |
| 258 |
(unless (si:bc-disassemble thing) |
| 259 |
(warn "Cannot disassemble the binary function ~S because I do not have its source code." thing) |
| 260 |
(return-from disassemble nil))) |
| 261 |
((atom thing) |
| 262 |
(error 'simple-type-error |
| 263 |
:datum thing |
| 264 |
:expected-type '(OR FUNCTION (SATISFIES SI:VALID-FUNCTION-NAME-P)) |
| 265 |
:format-control "DISASSEMBLE cannot accept ~A." |
| 266 |
:format-arguments (list thing))) |
| 267 |
((eq (car thing) 'LAMBDA) |
| 268 |
(setq disassembled-form `(defun gazonk ,@(cdr thing)))) |
| 269 |
((eq (car thing) 'EXT:LAMBDA-BLOCK) |
| 270 |
(setq disassembled-form `(defun ,@(rest thing)))) |
| 271 |
(t |
| 272 |
(error 'simple-type-error |
| 273 |
:datum thing |
| 274 |
:expected-type '(OR FUNCTION (SATISFIES SI:VALID-FUNCTION-NAME-P)) |
| 275 |
:format-control "DISASSEMBLE cannot accept ~A." |
| 276 |
:format-arguments (list thing)))) |
| 277 |
|
| 278 |
(let* ((null-stream (make-broadcast-stream)) |
| 279 |
(*compiler-output1* null-stream) |
| 280 |
(*compiler-output2* (if h-file |
| 281 |
(open h-file :direction :output :external-format :default) |
| 282 |
null-stream)) |
| 283 |
(t3function (symbol-function 'T3FUNCTION)) |
| 284 |
(compiler-conditions nil) |
| 285 |
(*cmp-env-root* *cmp-env-root*)) |
| 286 |
(with-compiler-env (compiler-conditions) |
| 287 |
(with-cxx-env () |
| 288 |
(setf disassembled-form (set-closure-env disassembled-form lexenv *cmp-env-root*)) |
| 289 |
(unwind-protect |
| 290 |
(progn |
| 291 |
(setf (symbol-function 'T3FUNCTION) |
| 292 |
#'(lambda (&rest args) |
| 293 |
(let ((*compiler-output1* *standard-output*)) |
| 294 |
(apply t3function args)))) |
| 295 |
(compiler-pass1 disassembled-form) |
| 296 |
(compiler-pass/propagate-types) |
| 297 |
(optimize-cxx-data *referenced-objects*) |
| 298 |
(ctop-write (compute-init-name "foo" :kind :fasl) |
| 299 |
(if h-file h-file "") |
| 300 |
(if data-file data-file "")) |
| 301 |
(when data-file |
| 302 |
(data-c-dump data-file))) |
| 303 |
(setf (symbol-function 'T3FUNCTION) t3function) |
| 304 |
(when h-file (close *compiler-output2*)))))) |
| 305 |
nil) |
| 306 |
|
| 307 |
;;; FIXME source-offset and source-truename are used by swanks string |
| 308 |
;;; compilation. Revisit if it is truly needed. SBCL deals with that |
| 309 |
;;; using WITH-COMPILATION-UNIT macro what seems to be a much better |
| 310 |
;;; place to customize the source location. -- jd 2019-11-25 |
| 311 |
(defun compiler-pass1 (object &optional source-offset) |
| 312 |
(if (streamp object) |
| 313 |
(do* ((eof '(NIL)) |
| 314 |
(*compile-file-position* 0 (file-position object)) |
| 315 |
(form (si:read-object-or-ignore object eof) |
| 316 |
(si:read-object-or-ignore object eof))) |
| 317 |
((eq form eof)) |
| 318 |
(when form |
| 319 |
(setf (cdr ext:*source-location*) |
| 320 |
(+ source-offset *compile-file-position*)) |
| 321 |
(t1expr form))) |
| 322 |
(t1expr object)) |
| 323 |
(setq *top-level-forms* (nreverse *top-level-forms*)) |
| 324 |
(setq *make-forms* (nreverse *make-forms*))) |
| 325 |
|
| 326 |
(defun compiler-pass/propagate-types () |
| 327 |
;; Type propagation phase |
| 328 |
(when *do-type-propagation* |
| 329 |
(setq *compiler-phase* 'p1propagate) |
| 330 |
(dolist (form *top-level-forms*) |
| 331 |
(p1propagate form)))) |
| 332 |
|
| 333 |
(defun print-compiler-info () |
| 334 |
(cmpprogress "~&;;; OPTIMIZE levels: Safety=~d, Space=~d, Speed=~d, Debug=~d~%;;;~%" |
| 335 |
*safety* *space* *speed* *debug*)) |
| 336 |
|
| 337 |
(defmacro with-compilation-unit (options &rest body) |
| 338 |
(declare (ignore options)) |
| 339 |
`(progn ,@body)) |
| 340 |
|
| 341 |
(ext:package-lock "CL" t) |
| 342 |
|
| 343 |
(setf *features* (delete :ecl-bytecmp *features*)) |
| 344 |
|
| 345 |
(let* ((compile #'compile) |
| 346 |
(disassemble #'disassemble) |
| 347 |
(compile-file #'compile-file) |
| 348 |
(compile-file-pathname #'compile-file-pathname)) |
| 349 |
(defun ext:install-c-compiler () |
| 350 |
(ext:package-lock (find-package :cl) nil) |
| 351 |
(setf *features* (delete :ecl-bytecmp *features*)) |
| 352 |
(setf (fdefinition 'disassemble) disassemble |
| 353 |
(fdefinition 'compile) compile |
| 354 |
(fdefinition 'compile-file) compile-file |
| 355 |
(fdefinition 'compile-file-pathname) compile-file-pathname) |
| 356 |
(ext:package-lock (find-package :cl) t))) |
| 357 |
|
| 358 |
(provide 'cmp) |