DEBUG: Basic compiler undefined function warning
[jscl.git] / jscl.lisp
1 ;;; jscl.lisp ---
2
3 ;; Copyright (C) 2012, 2013 David Vazquez
4 ;; Copyright (C) 2012 Raimon Grau
5
6 ;; JSCL is free software: you can redistribute it and/or
7 ;; modify it under the terms of the GNU General Public License as
8 ;; published by the Free Software Foundation, either version 3 of the
9 ;; License, or (at your option) any later version.
10 ;;
11 ;; JSCL is distributed in the hope that it will be useful, but
12 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ;; General Public License for more details.
15 ;;
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with JSCL.  If not, see <http://www.gnu.org/licenses/>.
18
19 (defpackage :jscl
20   (:use :cl)
21   (:export #:bootstrap #:run-tests-in-host))
22
23 (in-package :jscl)
24
25 (defvar *base-directory*
26   (or #.*load-pathname* *default-pathname-defaults*))
27
28 ;;; List of all the source files that need to be compiled, and whether they
29 ;;; are to be compiled just by the host, by the target JSCL, or by both.
30 ;;; All files have a `.lisp' extension, and
31 ;;; are relative to src/
32 ;;; Subdirectories are indicated by the presence of a list rather than a
33 ;;; keyword in the second element of the list. For example, this list:
34 ;;;  (("foo"    :target)
35 ;;;   ("bar"
36 ;;;     ("baz"  :host)
37 ;;;     ("quux" :both)))
38 ;;; Means that src/foo.lisp and src/bar/quux.lisp need to be compiled in the
39 ;;; target, and that src/bar/baz.lisp and src/bar/quux.lisp need to be
40 ;;; compiled in the host
41 (defvar *source*
42   '(("boot"          :target)
43     ("compat"        :host)
44     ("setf"          :target)
45     ("utils"         :both)
46     ("numbers"       :target)
47     ("char"          :target)
48     ("list"          :target)
49     ("array"         :target)
50     ("string"        :target)
51     ("sequence"      :target)
52     ("stream"        :target)
53     ("hash-table"    :target)
54     ("print"         :target)
55     ("documentation" :target)
56     ("misc"          :target)
57     ("ffi"           :target)
58     ("symbol"        :target)
59     ("package"       :target)
60     ("read"          :both)
61     ("defstruct"     :both)
62     ("lambda-list"   :both)
63     ("backquote"     :both)
64     ("compiler"
65      ("codegen"      :both)
66      ("compiler"     :both))
67     ("toplevel"      :target)))
68
69 (defun get-files (file-list type dir)
70   "Traverse FILE-LIST and retrieve a list of the files within which match
71    either TYPE or :BOTH, processing subdirectories."
72   (let ((file (car file-list)))
73     (cond
74       ((null file-list)
75        ())
76       ((listp (cadr file))
77        (append
78          (get-files (cdr file)      type (append dir (list (car file))))
79          (get-files (cdr file-list) type dir)))
80       ((member (cadr file) (list type :both))
81        (cons (source-pathname (car file) :directory dir :type "lisp")
82              (get-files (cdr file-list) type dir)))
83       (t
84        (get-files (cdr file-list) type dir)))))
85
86 (defmacro do-source (name type &body body)
87   "Iterate over all the source files that need to be compiled in the host or
88    the target, depending on the TYPE argument."
89   (unless (member type '(:host :target))
90     (error "TYPE must be one of :HOST or :TARGET, not ~S" type))
91   `(dolist (,name (get-files *source* ,type '(:relative "src")))
92      ,@body))
93
94 (defun source-pathname (filename &key (directory '(:relative "src")) (type nil) (defaults filename))
95   (merge-pathnames
96    (if type
97        (make-pathname :type type :directory directory :defaults defaults)
98        (make-pathname            :directory directory :defaults defaults))
99    *base-directory*))
100
101 ;;; Compile jscl into the host
102 (with-compilation-unit ()
103   (do-source input :host
104     (multiple-value-bind (fasl warn fail) (compile-file input)
105       (declare (ignore fasl warn))
106       (when fail
107         (error "Compilation of ~A failed." input)))))
108
109 ;;; Load jscl into the host
110 (do-source input :host
111   (load input))
112
113 (defun read-whole-file (filename)
114   (with-open-file (in filename)
115     (let ((seq (make-array (file-length in) :element-type 'character)))
116       (read-sequence seq in)
117       seq)))
118
119 (defun !compile-file (filename out &key print)
120   (let ((*compiling-file* t)
121         (*compile-print-toplevels* print))
122     (let* ((source (read-whole-file filename))
123            (in (make-string-stream source)))
124       (format t "Compiling ~a...~%" (enough-namestring filename))
125       (loop
126          with eof-mark = (gensym)
127          for x = (ls-read in nil eof-mark)
128          until (eq x eof-mark)
129          do (let ((compilation (compile-toplevel x)))
130               (when (plusp (length compilation))
131                 (write-string compilation out)))))))
132
133 (defun dump-global-environment (stream)
134   (flet ((late-compile (form)
135            (let ((*standard-output* stream))
136              (write-string (compile-toplevel form)))))
137     ;; We assume that environments have a friendly list representation
138     ;; for the compiler and it can be dumped.
139     (dolist (b (lexenv-function *environment*))
140       (when (eq (binding-type b) 'macro)
141         (setf (binding-value b) `(,*magic-unquote-marker* ,(binding-value b)))))
142     (late-compile `(setq *environment* ',*environment*))
143     ;; Set some counter variable properly, so user compiled code will
144     ;; not collide with the compiler itself.
145     (late-compile
146      `(progn
147         (progn ,@(mapcar (lambda (s) `(%intern-symbol (%js-vref ,(string (cdr s)))))
148                          (remove-if-not #'symbolp *literal-table* :key #'car)))
149         (setq *literal-table* ',*literal-table*)
150         (setq *variable-counter* ,*variable-counter*)
151         (setq *gensym-counter* ,*gensym-counter*)))
152     (late-compile `(setq *literal-counter* ,*literal-counter*))))
153
154
155 (defun bootstrap ()
156   (let ((*features* (cons :jscl *features*))
157         (*package* (find-package "JSCL")))
158     (setq *environment* (make-lexenv))
159     (setq *literal-table* nil)
160     (setq *variable-counter* 0
161           *gensym-counter* 0
162           *literal-counter* 0)
163     (with-open-file (out (merge-pathnames "jscl.js" *base-directory*)
164                          :direction :output
165                          :if-exists :supersede)
166       (write-string (read-whole-file (source-pathname "prelude.js")) out)
167       (do-source input :target
168         (!compile-file input out))
169       (dump-global-environment out))
170     ;; Tests
171     (with-open-file (out (merge-pathnames "tests.js" *base-directory*)
172                          :direction :output
173                          :if-exists :supersede)
174       (dolist (input (append (directory "tests.lisp")
175                              (directory "tests/*.lisp")
176                              (directory "tests-report.lisp")))
177         (!compile-file input out)))
178     (report-undefined-functions)))
179
180
181 ;;; Run the tests in the host Lisp implementation. It is a quick way
182 ;;; to improve the level of trust of the tests.
183 (defun run-tests-in-host ()
184   (let ((*package* (find-package "JSCL")))
185     (load "tests.lisp")
186     (let ((*use-html-output-p* nil))
187       (declare (special *use-html-output-p*))
188       (dolist (input (directory "tests/*.lisp"))
189         (load input)))
190     (load "tests-report.lisp")))