| 1 |
;;;; |
| 2 |
;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya |
| 3 |
;;;; Copyright (c) 1990, Giuseppe Attardi |
| 4 |
;;;; Copyright (c) 2023, Daniel Kochmański |
| 5 |
;;;; |
| 6 |
;;;; See the file 'LICENSE' for the copyright details. |
| 7 |
;;;; |
| 8 |
|
| 9 |
;;;; Open coding nested forms as C expressions while preserving the order of |
| 10 |
;;;; evaluation. Resulting locations may be used inline in C expressions. |
| 11 |
|
| 12 |
(in-package "COMPILER") |
| 13 |
|
| 14 |
(defun maybe-open-inline-block () |
| 15 |
(unless (plusp *inline-blocks*) |
| 16 |
(open-inline-block))) |
| 17 |
|
| 18 |
(defun open-inline-block () |
| 19 |
(wt-nl-open-brace) |
| 20 |
(incf *inline-blocks*)) |
| 21 |
|
| 22 |
(defun close-inline-blocks () |
| 23 |
(loop for i of-type fixnum from 0 below *inline-blocks* |
| 24 |
do (wt-nl-close-brace))) |
| 25 |
|
| 26 |
(defun coerce-loc (host-type location) |
| 27 |
(if (eq (loc-host-type location) host-type) |
| 28 |
location |
| 29 |
`(COERCE-LOC ,host-type ,location))) |
| 30 |
|
| 31 |
(defun coerce-args (inlined-args) |
| 32 |
(mapcar (lambda (loc) (coerce-loc :object loc)) inlined-args)) |
| 33 |
|
| 34 |
(defun coerce-locs (inlined-args types args-to-be-saved) |
| 35 |
;; INLINED-ARGS is a list of INLINED-ARG produced by the argument inliner. |
| 36 |
;; Each arg is a location, "inlined" means "evaluated in the correct order". |
| 37 |
;; |
| 38 |
;; ARGS-TO-BE-SAVED is a positional list created by C-INLINE, instructing that |
| 39 |
;; the value should be saved in a temporary variable. |
| 40 |
;; |
| 41 |
;; TYPES is a list of destination types, to which the former values are |
| 42 |
;; coerced. The destination type can be: |
| 43 |
;; |
| 44 |
;; - A host type (:OBJECT, :FIXNUM, :INT, :CHAR, ...) |
| 45 |
;; - A lisp type (T, INTEGER, STRING, CHARACTER, ...)) |
| 46 |
;; |
| 47 |
(loop with block-opened = nil |
| 48 |
for loc in inlined-args |
| 49 |
for type in types |
| 50 |
for i from 0 |
| 51 |
for host-type = (lisp-type->host-type type) |
| 52 |
collect |
| 53 |
(if (and (member i args-to-be-saved :test #'eql) |
| 54 |
(not (loc-movable-p loc))) |
| 55 |
(let ((lcl (make-lcl-var :host-type host-type))) |
| 56 |
(wt-nl) |
| 57 |
(unless block-opened |
| 58 |
(setf block-opened t) |
| 59 |
(open-inline-block)) |
| 60 |
(wt (host-type->c-name host-type) " " lcl "= " |
| 61 |
(coerce-loc host-type loc) ";") |
| 62 |
lcl) |
| 63 |
(coerce-loc host-type loc)))) |
| 64 |
|
| 65 |
;;; We could use VV to represent inlined args, but the most specific for our |
| 66 |
;;; purposes is the location (THE LISP-TYPE LOCATION) which is created when |
| 67 |
;;; necessary by the function PRECISE-LOC-TYPE. -- jd 2023-12-06 |
| 68 |
#+ (or) |
| 69 |
(defun make-inlined-arg (loc lisp-type) |
| 70 |
(make-vv :location loc |
| 71 |
:value *inline-loc* |
| 72 |
:type lisp-type |
| 73 |
:host-type (loc-host-type loc))) |
| 74 |
|
| 75 |
(defun make-inlined-temp-var (lisp-type host-type) |
| 76 |
(if (eq host-type :object) |
| 77 |
(make-temp-var lisp-type) |
| 78 |
(let ((var (make-lcl-var :host-type host-type |
| 79 |
:type lisp-type))) |
| 80 |
(open-inline-block) |
| 81 |
(wt-nl (host-type->c-name host-type) " " var ";") |
| 82 |
var))) |
| 83 |
|
| 84 |
(defun emit-inlined-temp-var (form lisp-type host-type) |
| 85 |
(let ((*destination* (make-inlined-temp-var lisp-type host-type))) |
| 86 |
(c2expr* form) |
| 87 |
*destination*)) |
| 88 |
|
| 89 |
(defun emit-inlined-variable (form rest-forms) |
| 90 |
(let ((var (c1form-arg 0 form)) |
| 91 |
(lisp-type (c1form-primary-type form))) |
| 92 |
(if (var-changed-in-form-list var rest-forms) |
| 93 |
(emit-inlined-temp-var form lisp-type (var-host-type var)) |
| 94 |
(precise-loc-lisp-type var lisp-type)))) |
| 95 |
|
| 96 |
(defun emit-inlined-setq (form rest-forms) |
| 97 |
(let ((var (c1form-arg 0 form)) |
| 98 |
(val-form (c1form-arg 1 form)) |
| 99 |
(lisp-type (c1form-primary-type form))) |
| 100 |
(let ((*destination* var)) |
| 101 |
(c2expr* val-form)) |
| 102 |
(cond |
| 103 |
((eq (c1form-name val-form) 'LOCATION) |
| 104 |
(precise-loc-lisp-type (c1form-arg 0 val-form) lisp-type)) |
| 105 |
((not (var-changed-in-form-list var rest-forms)) |
| 106 |
(precise-loc-lisp-type var lisp-type)) |
| 107 |
(t |
| 108 |
(let ((var-form (make-c1form 'VARIABLE form var nil))) |
| 109 |
(emit-inlined-temp-var var-form lisp-type (var-host-type var))))))) |
| 110 |
|
| 111 |
(defun emit-inlined-progn (form rest-forms) |
| 112 |
(let ((args (c1form-arg 0 form))) |
| 113 |
(loop with *destination* = 'TRASH |
| 114 |
while (rest args) |
| 115 |
do (c2expr* (pop args))) |
| 116 |
(emit-inline-form (first args) rest-forms))) |
| 117 |
|
| 118 |
(defun emit-inlined-values (form rest-forms) |
| 119 |
(let ((args (c1form-arg 0 form))) |
| 120 |
(prog1 (emit-inline-form (or (pop args) (c1nil)) |
| 121 |
;; The rest of the values args need to be added to |
| 122 |
;; the rest forms to execute side effects in the |
| 123 |
;; correct order. |
| 124 |
(append args rest-forms)) |
| 125 |
(loop with *destination* = 'TRASH |
| 126 |
for form in args |
| 127 |
do (c2expr* form))))) |
| 128 |
|
| 129 |
;;; |
| 130 |
;;; emit-inline-form: |
| 131 |
;;; returns a location that contains a moveable argument |
| 132 |
;;; side effects: emits code for a temporary variable |
| 133 |
;;; |
| 134 |
;;; Whoever calls this function must wrap the body in WITH-INLINE-BLOCKS. |
| 135 |
;;; |
| 136 |
(defun emit-inline-form (form forms) |
| 137 |
(with-c1form-env (form form) |
| 138 |
(precise-loc-lisp-type |
| 139 |
(case (c1form-name form) |
| 140 |
(LOCATION (c1form-arg 0 form) ) |
| 141 |
(VARIABLE (emit-inlined-variable form forms)) |
| 142 |
(SETQ (emit-inlined-setq form forms)) |
| 143 |
(PROGN (emit-inlined-progn form forms)) |
| 144 |
(VALUES (emit-inlined-values form forms)) |
| 145 |
(t (emit-inlined-temp-var form t :object))) |
| 146 |
(c1form-primary-type form)))) |
| 147 |
|
| 148 |
;;; |
| 149 |
;;; inline-args: |
| 150 |
;;; returns locations that contain results of evaluating forms |
| 151 |
;;; side effects: emits code for temporary variables |
| 152 |
;;; |
| 153 |
;;; Whoever calls this function must wrap the body in WITH-INLINE-BLOCKS. |
| 154 |
;;; |
| 155 |
(defun inline-args (forms) |
| 156 |
(loop for form-list on forms |
| 157 |
for form = (first form-list) |
| 158 |
collect (emit-inline-form form (rest form-list)))) |