chiark / gitweb /
src/method-proto.lisp: Fix varargs trampoline method construction.
[sod] / src / codegen-proto.lisp
CommitLineData
1f1d88f5
MW
1;;; -*-lisp-*-
2;;;
dea4d055 3;;; Code generation protocol
1f1d88f5
MW
4;;;
5;;; (c) 2009 Straylight/Edgeware
6;;;
7
8;;;----- Licensing notice ---------------------------------------------------
9;;;
dea4d055 10;;; This file is part of the Sensble Object Design, an object system for C.
1f1d88f5
MW
11;;;
12;;; SOD is free software; you can redistribute it and/or modify
13;;; it under the terms of the GNU General Public License as published by
14;;; the Free Software Foundation; either version 2 of the License, or
15;;; (at your option) any later version.
16;;;
17;;; SOD is distributed in the hope that it will be useful,
18;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;;; GNU General Public License for more details.
21;;;
22;;; You should have received a copy of the GNU General Public License
23;;; along with SOD; if not, write to the Free Software Foundation,
24;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
26(cl:in-package #:sod)
27
28;;;--------------------------------------------------------------------------
29;;; Temporary names.
30
dea4d055
MW
31;; Protocol.
32
33(export 'format-temporary-name)
34(defgeneric format-temporary-name (var stream)
1f1d88f5 35 (:documentation
dea4d055 36 "Write the name of a temporary variable VAR to STREAM."))
1f1d88f5 37
dea4d055
MW
38(export 'var-in-use-p)
39(defgeneric var-in-use-p (var)
40 (:documentation
3109662a 41 "Answer whether VAR is currently being used. See `with-temporary-var'.")
dea4d055
MW
42 (:method (var)
43 "Non-temporary variables are always in use."
1d8cc67a 44 (declare (ignore var))
dea4d055
MW
45 t))
46(defgeneric (setf var-in-use-p) (value var)
47 (:documentation
3109662a 48 "Record whether VAR is currently being used. See `with-temporary-var'."))
1f1d88f5 49
dea4d055 50;; Root class.
1f1d88f5 51
dea4d055
MW
52(export 'temporary-name)
53(defclass temporary-name ()
54 ((tag :initarg :tag :reader temp-tag))
55 (:documentation
56 "Base class for temporary variable and argument names."))
1f1d88f5 57
dea4d055 58;; Important variables.
1f1d88f5
MW
59
60(defparameter *temporary-index* 0
61 "Index for temporary name generation.
62
63 This is automatically reset to zero before the output functions are
64 invoked to write a file. This way, we can ensure that the same output
9ec578d9
MW
65 file is always produced from the same input.")
66
67(define-clear-the-decks reset-codegen-index
68 (setf *temporary-index* 0))
1f1d88f5 69
dea4d055 70;; Important temporary names.
1f1d88f5 71
dea4d055 72(export '(*sod-ap* *sod-master-ap*))
1f1d88f5
MW
73(defparameter *sod-ap*
74 (make-instance 'temporary-name :tag "sod__ap"))
75(defparameter *sod-master-ap*
76 (make-instance 'temporary-name :tag "sod__master_ap"))
77
78;;;--------------------------------------------------------------------------
79;;; Instructions.
80
dea4d055
MW
81;; Classes.
82
83(export 'inst)
1f1d88f5
MW
84(defclass inst () ()
85 (:documentation
86 "A base class for instructions.
87
88 An `instruction' is anything which might be useful to string into a code
9ec578d9
MW
89 generator. Both statements and expressions can be represented by trees of
90 instructions. The `definst' macro is a convenient way of defining new
91 instructions.
1f1d88f5
MW
92
93 The only important protocol for instructions is output, which is achieved
3109662a 94 by calling `print-object' with `*print-escape*' nil.
1f1d88f5
MW
95
96 This doesn't really do very much, but it acts as a handy marker for
97 instruction subclasses."))
98
dea4d055 99(export 'inst-metric)
1f1d88f5
MW
100(defgeneric inst-metric (inst)
101 (:documentation
102 "Returns a `metric' describing how complicated INST is.
103
3109662a
MW
104 The default metric of an inst node is simply 1; `inst' subclasses
105 generated by `definst' (q.v.) have an automatically generated method which
106 returns one plus the sum of the metrics of the node's children.
1f1d88f5
MW
107
108 This isn't intended to be a particularly rigorous definition. Its purpose
109 is to allow code generators to make decisions about inlining or calling
110 code fairly simply.")
9ec578d9
MW
111 (:method ((inst t))
112 (declare (ignore inst))
113 1)
114 (:method ((inst null))
1d8cc67a 115 (declare (ignore inst))
9ec578d9
MW
116 1)
117 (:method ((inst list))
118 (reduce #'+ inst :key #'inst-metric)))
1f1d88f5 119
dea4d055
MW
120;; Instruction definition.
121
122(export 'definst)
418752c5 123(defmacro definst (code (streamvar &key export) args &body body)
1f1d88f5
MW
124 "Define an instruction type and describe how to output it.
125
3109662a
MW
126 An `inst' can represent any structured piece of output syntax: a
127 statement, expression or declaration, for example. This macro defines the
128 following things:
1f1d88f5 129
3109662a 130 * A class `CODE-inst' to represent the instruction.
1f1d88f5
MW
131
132 * Instance slots named after the ARGS, with matching keyword initargs,
3109662a 133 and `inst-ARG' readers.
1f1d88f5 134
3109662a 135 * A constructor `make-CODE-inst' which accepts the ARGS (in order, not
1f1d88f5
MW
136 with keywords) as arguments and returns a fresh instance.
137
3109662a 138 * A print method, which prints a diagnostic dump if `*print-escape*' is
1f1d88f5
MW
139 set, or invokes the BODY (with STREAMVAR bound to the output stream)
140 otherwise. The BODY is expected to produce target code at this
418752c5
MW
141 point.
142
143 If EXPORT is non-nil, then export the `CODE-inst' and `make-CODE-inst'
144 symbols."
1f1d88f5
MW
145
146 (let ((inst-var (gensym "INST"))
147 (class-name (symbolicate code '-inst))
418752c5 148 (constructor-name (symbolicate 'make- code '-inst))
1f1d88f5
MW
149 (keys (mapcar (lambda (arg) (intern (symbol-name arg) :keyword))
150 args)))
151 `(progn
152 (defclass ,class-name (inst)
153 ,(mapcar (lambda (arg key)
154 `(,arg :initarg ,key :reader ,(symbolicate 'inst- arg)))
155 args keys))
418752c5 156 (defun ,constructor-name (,@args)
1f1d88f5
MW
157 (make-instance ',class-name ,@(mappend #'list keys args)))
158 (defmethod inst-metric ((,inst-var ,class-name))
159 (with-slots (,@args) ,inst-var
160 (+ 1 ,@(mapcar (lambda (arg) `(inst-metric ,arg)) args))))
161 (defmethod print-object ((,inst-var ,class-name) ,streamvar)
162 (with-slots (,@args) ,inst-var
163 (if *print-escape*
164 (print-unreadable-object (,inst-var ,streamvar :type t)
165 (format stream "~@<~@{~S ~@_~S~^ ~_~}~:>"
166 ,@(mappend #'list keys args)))
418752c5
MW
167 (progn ,@body))))
168 ,@(and export `((export '(,class-name ,constructor-name))))
169 ',code)))
1f1d88f5 170
dea4d055
MW
171;; Important instruction classes.
172
418752c5 173(definst var (stream :export t) (name type init)
dea4d055
MW
174 (pprint-c-type type stream name)
175 (when init
75dbacfc
MW
176 (format stream " = ~A" init))
177 (write-char #\; stream))
418752c5 178(definst set (stream :export t) (var expr)
dea4d055 179 (format stream "~@<~A = ~@_~2I~A;~:>" var expr))
b559689b
MW
180(definst update (stream :export t) (var op expr)
181 (format stream "~@<~A ~A= ~@_~2I~A;~:>" var op expr))
418752c5 182(definst return (stream :export t) (expr)
dea4d055 183 (format stream "return~@[ (~A)~];" expr))
b559689b
MW
184(definst break (stream :export t) ()
185 (format stream "break;"))
186(definst continue (stream :export t) ()
187 (format stream "continue;"))
418752c5 188(definst expr (stream :export t) (expr)
dea4d055 189 (format stream "~A;" expr))
418752c5 190(definst block (stream :export t) (decls body)
75dbacfc 191 (format stream "{~:@_~@< ~2I~@[~{~A~:@_~}~:@_~]~{~A~^~:@_~}~:>~:@_}"
dea4d055 192 decls body))
418752c5 193(definst function (stream :export t) (name type body)
dea4d055
MW
194 (pprint-logical-block (stream nil)
195 (princ "static " stream)
196 (pprint-c-type type stream name)
197 (format stream "~:@_~A~:@_~:@_" body)))
198
199;; Formatting utilities.
200
1f1d88f5 201(defun format-compound-statement* (stream child morep thunk)
3109662a 202 "Underlying function for `format-compound-statement'."
1f1d88f5
MW
203 (cond ((typep child 'block-inst)
204 (funcall thunk stream)
205 (write-char #\space stream)
206 (princ child stream)
207 (when morep (write-char #\space stream)))
208 (t
209 (pprint-logical-block (stream nil)
210 (funcall thunk stream)
211 (write-char #\space stream)
212 (pprint-indent :block 2 stream)
213 (pprint-newline :linear stream)
214 (princ child stream)
215 (pprint-indent :block 0 stream)
216 (case morep
217 (:space
218 (write-char #\space stream)
219 (pprint-newline :linear stream))
dea4d055 220 ((t)
1f1d88f5
MW
221 (pprint-newline :mandatory stream)))))))
222
dea4d055 223(export 'format-compound-statement)
1f1d88f5
MW
224(defmacro format-compound-statement
225 ((stream child &optional morep) &body body)
226 "Format a compound statement to STREAM.
227
228 The introductory material is printed by BODY. The CHILD is formatted
3109662a 229 properly according to whether it's a `block-inst'. If MOREP is true, then
1f1d88f5
MW
230 allow for more stuff following the child."
231 `(format-compound-statement* ,stream ,child ,morep
232 (lambda (,stream) ,@body)))
233
234;;;--------------------------------------------------------------------------
dea4d055 235;;; Code generation.
1f1d88f5 236
dea4d055 237;; Accessors.
1f1d88f5 238
dea4d055
MW
239(export 'codegen-functions)
240(defgeneric codegen-functions (codegen)
1f1d88f5 241 (:documentation
3109662a 242 "Return the list of `function-inst's of completed functions."))
1f1d88f5 243
dea4d055 244(export 'ensure-var)
1f1d88f5
MW
245(defgeneric ensure-var (codegen name type &optional init)
246 (:documentation
247 "Add a variable to CODEGEN's list.
248
3109662a 249 The variable is called NAME (which should be comparable using `equal' and
1f1d88f5 250 print to an identifier) and has the given TYPE. If INIT is present and
3109662a 251 non-nil it is an expression `inst' used to provide the variable with an
dea4d055 252 initial value."))
1f1d88f5 253
dea4d055
MW
254(export '(emit-inst emit-insts))
255(defgeneric emit-inst (codegen inst)
256 (:documentation
257 "Add INST to the end of CODEGEN's list of instructions."))
258(defgeneric emit-insts (codegen insts)
259 (:documentation
260 "Add a list of INSTS to the end of CODEGEN's list of instructions.")
261 (:method (codegen insts)
262 (dolist (inst insts) (emit-inst codegen inst))))
1f1d88f5 263
3f4ac959
MW
264(export '(emit-decl emit-decls))
265(defgeneric emit-decl (codegen inst)
266 (:documentation
267 "Add INST to the end of CODEGEN's list of declarations."))
268(defgeneric emit-decls (codegen insts)
269 (:documentation
270 "Add a list of INSTS to the end of CODEGEN's list of declarations."))
271
dea4d055 272(export 'codegen-push)
1f1d88f5
MW
273(defgeneric codegen-push (codegen)
274 (:documentation
275 "Pushes the current code generation state onto a stack.
276
dea4d055 277 The state consists of the accumulated variables and instructions."))
1f1d88f5 278
dea4d055 279(export 'codegen-pop)
1f1d88f5
MW
280(defgeneric codegen-pop (codegen)
281 (:documentation
282 "Pops a saved state off of the CODEGEN's stack.
283
284 Returns the newly accumulated variables and instructions as lists, as
dea4d055 285 separate values."))
1f1d88f5 286
dea4d055 287(export 'codegen-add-function)
1f1d88f5
MW
288(defgeneric codegen-add-function (codegen function)
289 (:documentation
290 "Adds a function to CODEGEN's list.
291
292 Actually, we're not picky: FUNCTION can be any kind of object that you're
3109662a 293 willing to find in the list returned by `codegen-functions'."))
dea4d055
MW
294
295(export 'temporary-var)
296(defgeneric temporary-var (codegen type)
297 (:documentation
298 "Return the name of a temporary variable.
299
300 The temporary variable will have the given TYPE, and will be marked
301 in-use. You should clear the in-use flag explicitly when you've finished
3109662a
MW
302 with the variable -- or, better, use `with-temporary-var' to do the
303 cleanup automatically."))
1f1d88f5 304
dea4d055 305(export 'codegen-build-function)
1f1d88f5
MW
306(defun codegen-build-function (codegen name type vars insts)
307 "Build a function and add it to CODEGEN's list.
308
309 Returns the function's name."
310 (codegen-add-function codegen
311 (make-function-inst name type
312 (make-block-inst vars insts)))
313 name)
314
dea4d055
MW
315(export 'codegen-pop-block)
316(defgeneric codegen-pop-block (codegen)
317 (:documentation
3109662a 318 "Makes a block (`block-inst') out of the completed code in CODEGEN.")
dea4d055
MW
319 (:method (codegen)
320 (multiple-value-bind (vars insts) (codegen-pop codegen)
321 (make-block-inst vars insts))))
322
323(export 'codegen-pop-function)
1f1d88f5
MW
324(defgeneric codegen-pop-function (codegen name type)
325 (:documentation
326 "Makes a function out of the completed code in CODEGEN.
327
328 The NAME can be any object you like. The TYPE should be a function type
329 object which includes argument names. The return value is the NAME.")
dea4d055 330 (:method (codegen name type)
1f1d88f5
MW
331 (multiple-value-bind (vars insts) (codegen-pop codegen)
332 (codegen-build-function codegen name type vars insts))))
333
dea4d055 334(export 'with-temporary-var)
1f1d88f5
MW
335(defmacro with-temporary-var ((codegen var type) &body body)
336 "Evaluate BODY with VAR bound to a temporary variable name.
337
338 During BODY, VAR will be marked in-use; when BODY ends, VAR will be marked
9ec578d9 339 available for re-use."
1f1d88f5
MW
340 `(let ((,var (temporary-var ,codegen ,type)))
341 (unwind-protect
342 (progn ,@body)
343 (setf (var-in-use-p ,var) nil))))
344
345;;;--------------------------------------------------------------------------
346;;; Code generation idioms.
347
dea4d055 348(export 'deliver-expr)
1f1d88f5
MW
349(defun deliver-expr (codegen target expr)
350 "Emit code to deliver the value of EXPR to the TARGET.
351
352 The TARGET may be one of the following.
353
3109662a 354 * `:void', indicating that the value is to be discarded. The expression
1f1d88f5
MW
355 will still be evaluated.
356
3109662a
MW
357 * `:void-return', indicating that the value is to be discarded (as for
358 `:void') and furthermore a `return' from the current function should
359 be forced after computing the value.
1f1d88f5 360
3109662a
MW
361 * `:return', indicating that the value is to be returned from the
362 current function.
1f1d88f5
MW
363
364 * A variable name, indicating that the value is to be stored in the
365 variable.
366
3109662a
MW
367 In the cases of `:return', `:void' and `:void-return' targets, it is valid
368 for EXPR to be nil; this signifies that no computation needs to be
369 performed. Variable-name targets require an expression."
1f1d88f5
MW
370
371 (case target
372 (:return (emit-inst codegen (make-return-inst expr)))
373 (:void (when expr (emit-inst codegen (make-expr-inst expr))))
374 (:void-return (when expr (emit-inst codegen (make-expr-inst expr)))
375 (emit-inst codegen (make-return-inst nil)))
376 (t (emit-inst codegen (make-set-inst target expr)))))
377
dea4d055 378(export 'convert-stmts)
1f1d88f5 379(defun convert-stmts (codegen target type func)
3109662a 380 "Invoke FUNC to deliver a value to a non-`:return' target.
1f1d88f5 381
3109662a
MW
382 FUNC is a function which accepts a single argument, a non-`:return'
383 target, and generates statements which deliver a value (see
384 `deliver-expr') of the specified TYPE to this target. In general, the
385 generated code will have the form
1f1d88f5
MW
386
387 setup instructions...
3109662a 388 (deliver-expr CODEGEN TARGET (compute value...))
1f1d88f5
MW
389 cleanup instructions...
390
391 where the cleanup instructions are essential to the proper working of the
392 generated program.
393
3109662a
MW
394 The `convert-stmts' function will call FUNC to generate code, and arrange
395 that its value is correctly delivered to TARGET, regardless of what the
396 TARGET is -- i.e., it lifts the restriction to non-`:return' targets. It
397 does this by inventing a new temporary variable."
1f1d88f5
MW
398
399 (case target
400 (:return (with-temporary-var (codegen var type)
401 (funcall func var)
402 (deliver-expr codegen target var)))
403 (:void-return (funcall func :void)
404 (emit-inst codegen (make-return-inst nil)))
405 (t (funcall func target))))
406
407;;;----- That's all, folks --------------------------------------------------