Newer
Older
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; Package: C -*-
;;;;
;;; ----------------------------------------------------------------------
;;; Macros only used in the code of the compiler itself:
;;;; Copyright by a number of previous anonymous authors
;;;; presumed to be the same as for the rest of MKCL.
;;;; Copyright (c) 2012. Jean-Claude Beaudoin.
;;;;
;;;; MKCL is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;;
;;;; See file '../../Copyright' for full details.
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
(in-package "COMPILER")
(defun same-fname-p (name1 name2) (equal name1 name2))
;;; from cmpenv.lsp
(defmacro next-cmacro () '(incf *next-cmacro*))
;;; from cmplabel.lsp
(defmacro next-label () `(cons (incf *last-label*) nil))
(defmacro next-label* () `(cons (incf *last-label*) t))
(defmacro wt-go (label)
`(progn (rplacd ,label t) (wt "goto L" (car ,label) ";")))
;;; from cmplam.lsp
(defmacro ck-spec (condition)
`(unless ,condition
(cmperr "The parameter specification ~s is illegal." spec)))
(defmacro ck-vl (condition)
`(unless ,condition
(cmperr "The lambda list ~s is illegal." vl)))
;;; fromcmputil.sp
(defmacro cmpck (condition string &rest args)
`(if ,condition (cmperr ,string ,@args)))
(defmacro cmpassert (condition string &rest args)
`(unless ,condition (cmperr ,string ,@args)))
;;; from cmpwt.lsp
(defmacro wt (&rest forms &aux (fl nil))
(dolist (form forms `(progn ,@(nreverse (cons nil fl))))
(if (stringp form)
(push `(princ ,form *compiler-output1*) fl)
(push `(wt1 ,form) fl))))
(defmacro wt-h (&rest forms &aux (fl nil))
(dolist (form forms `(progn ,@(nreverse (cons nil fl))))
(if (stringp form)
(push `(princ ,form *compiler-output2*) fl)
(push `(wt-h1 ,form) fl))))
(defmacro wt-nl-h (&rest forms)
`(progn (terpri *compiler-output2*) (wt-h ,@forms)))
(defmacro princ-h (form) `(princ ,form *compiler-output2*))
(defmacro wt-nl (&rest forms)
`(wt #\Newline #\Tab ,@forms))
(defmacro wt-nl1 (&rest forms)
`(wt #\Newline ,@forms))
(defmacro safe-compile ()
`(>= (cmp-env-optimization 'safety) 1) ;; Let's err on the side of safety. JCB
)
(defmacro compiler-check-args ()
`(>= (cmp-env-optimization 'safety) 1))
(defmacro compiler-push-events ()
`(>= (cmp-env-optimization 'safety) 3))
(defmacro with-clean-symbols (symbols &body body)
"Rewrites the given forms replacing the given symbols with uninterned
ones, which is useful for creating hygienic macros."
`(progn ,@(sublis (loop for s in symbols
collect (cons s (make-symbol (symbol-name s))))
body)))
;; ----------------------------------------------------------------------
;; C1-FORMS
;;
(defstruct (c1form (:include info)
(:print-object print-c1form)
(:constructor do-make-c1form))
(name nil)
(parent nil)
(args '())
(file nil)
(file-end-position 0))
(defun print-c1form (form stream)
(format stream "#<form ~A ~X>" (c1form-name form) (si::pointer form)))
(defun make-c1form (name subform &rest args)
(let ((form (do-make-c1form :name name :args args
:type (info-type subform)
:sp-change (info-sp-change subform)
:volatile (info-volatile subform)
:file *compile-file-truename*
:file-end-position *compile-file-end-position*)))
(c1form-add-info form args)
form))
(defun make-c1form* (name &rest args)
(let ((info-args '())
(form-args '()))
(do ((l args (cdr l)))
((endp l))
(let ((key (first l)))
(cond ((not (keywordp key))
(baboon))
((eq key ':args)
(setf form-args (rest l))
(return))
(t
(setf info-args (list* key (second l) info-args)
l (cdr l))))))
(let ((form (apply #'do-make-c1form :name name :args form-args
info-args)))
(c1form-add-info form form-args)
form)))
(defun c1form-add-info (form dependents)
(dolist (subform dependents form)
(cond ((c1form-p subform)
(when (info-sp-change subform)
(setf (info-sp-change form) t))
(setf (c1form-parent subform) form))
((consp subform)
(c1form-add-info form subform)))))
(defmacro c1form-arg (nth form)
(case nth
(0 `(first (c1form-args ,form)))
(1 `(second (c1form-args ,form)))
(otherwise `(nth ,nth (c1form-args ,form)))))
(defun c1form-volatile* (form)
(if (c1form-volatile form) "volatile " ""))
(defun c1form-primary-type (form)
(let ((type (c1form-type form)))
(when (and (consp type) (eq (first type) 'VALUES))
(let ((subtype (second type)))
(when (or (eq subtype '&optional) (eq subtype '&rest))
(setf subtype (third (c1form-type form)))
(when (eq subtype '&optional)
(cmperr "Syntax error in type expression ~S" type)))
(when (eq subtype '&rest)
(cmperr "Syntax error in type expression ~S" type))
(setf type subtype)))
type))
(defun find-node-in-list (home-node list)
(flet ((parent-node-p (node presumed-child)
(loop
(cond ((null presumed-child) (return nil))
((eq node presumed-child) (return t))
(t (setf presumed-child (c1form-parent presumed-child)))))))
(member home-node list :test #'parent-node-p)))