| 1 |
;;;; |
| 2 |
;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya |
| 3 |
;;;; Copyright (c) 1990, Giuseppe Attardi |
| 4 |
;;;; Copyright (c) 2010, Juan Jose Garcia-Ripoll |
| 5 |
;;;; Copyright (c) 2023, Daniel Kochmański |
| 6 |
;;;; |
| 7 |
;;;; See the file 'LICENSE' for the copyright details. |
| 8 |
;;;; |
| 9 |
|
| 10 |
(in-package #:compiler) |
| 11 |
|
| 12 |
(defun c2expr (form) |
| 13 |
(with-c1form-env (form form) |
| 14 |
(let* ((name (c1form-name form)) |
| 15 |
(args (c1form-args form)) |
| 16 |
(dispatch (gethash name *c2-dispatch-table*))) |
| 17 |
(if dispatch |
| 18 |
(apply dispatch form args) |
| 19 |
(cmperr "Unhandled C2FORM found at the:~%~4I~A" form))))) |
| 20 |
|
| 21 |
(defun c2expr* (form) |
| 22 |
;; C2EXPR* compiles the giving expression in a context in which |
| 23 |
;; other expressions will follow this one. We must thus create |
| 24 |
;; a possible label so that the compiled forms exit right at |
| 25 |
;; the point where the next form will be compiled. |
| 26 |
(with-exit-label (*exit*) |
| 27 |
(let (;;(*lex* *lex*) |
| 28 |
(*lcl* *lcl*) |
| 29 |
(*temp* *temp*)) |
| 30 |
(c2expr form)))) |
| 31 |
|
| 32 |
(defun c2progn (c1form forms) |
| 33 |
(declare (ignore c1form)) |
| 34 |
;; INV C1PROGN ensures that the length of forms is not less than 1. |
| 35 |
(loop with lex = *lex* |
| 36 |
for (form next . rest) on forms do |
| 37 |
(if (null next) |
| 38 |
(c2expr form) |
| 39 |
(let ((*destination* 'TRASH)) |
| 40 |
(c2expr* form) |
| 41 |
;; recycle lex locations |
| 42 |
(setq *lex* lex))) |
| 43 |
;; Since PROGN does not have tags, any transfer of control means leaving |
| 44 |
;; the current PROGN statement. |
| 45 |
until (member (c1form-name form) '(CL:GO CL:RETURN-FROM)))) |
| 46 |
|
| 47 |
(defun c2if (c1form fmla form1 form2) |
| 48 |
;; FIXME! Optimize when FORM1 or FORM2 are constants |
| 49 |
(cond ((type-true-p (c1form-primary-type fmla) *cmp-env*) |
| 50 |
;; The true branch is always taken |
| 51 |
(warn-dead-code form2 c1form "the test ~S always evaluates to true" fmla) |
| 52 |
(let ((*destination* 'TRASH)) |
| 53 |
(c2expr* fmla)) |
| 54 |
(c2expr form1)) |
| 55 |
((type-false-p (c1form-primary-type fmla) *cmp-env*) |
| 56 |
;; The false branch is always taken |
| 57 |
(warn-dead-code form1 c1form "the test ~S always evaluates to false" fmla) |
| 58 |
(let ((*destination* 'TRASH)) |
| 59 |
(c2expr* fmla)) |
| 60 |
(c2expr form2)) |
| 61 |
((and (eq *destination* 'TRASH) |
| 62 |
(eq (c1form-name form2) 'LOCATION)) |
| 63 |
;; The value produced by the false branch is not used |
| 64 |
(with-exit-label (false-label *exit*) |
| 65 |
(let ((*destination* `(JUMP-FALSE ,false-label))) |
| 66 |
(c2expr* fmla)) |
| 67 |
(c2expr form1))) |
| 68 |
((and (eq *destination* 'TRASH) |
| 69 |
(eq (c1form-name form1) 'LOCATION)) |
| 70 |
;; The value produced by the true branch is not used |
| 71 |
(with-exit-label (true-label *exit*) |
| 72 |
(let ((*destination* `(JUMP-TRUE ,true-label))) |
| 73 |
(c2expr* fmla)) |
| 74 |
(c2expr form2))) |
| 75 |
(t |
| 76 |
(with-exit-label (false-label) |
| 77 |
(let ((*destination* `(JUMP-FALSE ,false-label))) |
| 78 |
(c2expr* fmla)) |
| 79 |
(c2expr form1)) |
| 80 |
(c2expr form2)))) |
| 81 |
|
| 82 |
(defun jump-true-destination-p (dest) |
| 83 |
(declare (si::c-local)) |
| 84 |
(and (consp dest) (eq (si:cons-car dest) 'JUMP-TRUE))) |
| 85 |
|
| 86 |
(defun jump-false-destination-p (dest) |
| 87 |
(declare (si::c-local)) |
| 88 |
(and (consp dest) (eq (si:cons-car dest) 'JUMP-FALSE))) |
| 89 |
|
| 90 |
(defun c2fmla-not (c1form arg) |
| 91 |
(declare (ignore c1form)) |
| 92 |
(let ((dest *destination*)) |
| 93 |
(cond ((type-true-p (c1form-primary-type arg) *cmp-env*) |
| 94 |
(let ((*destination* 'TRASH)) |
| 95 |
(c2expr* arg)) |
| 96 |
(c2expr (c1nil))) |
| 97 |
((type-false-p (c1form-primary-type arg) *cmp-env*) |
| 98 |
(let ((*destination* 'TRASH)) |
| 99 |
(c2expr* arg)) |
| 100 |
(c2expr (c1t))) |
| 101 |
((jump-true-destination-p dest) |
| 102 |
(let ((*destination* `(JUMP-FALSE ,@(cdr dest)))) |
| 103 |
(c2expr arg))) |
| 104 |
((jump-false-destination-p dest) |
| 105 |
(let ((*destination* `(JUMP-TRUE ,@(cdr dest)))) |
| 106 |
(c2expr arg))) |
| 107 |
(t |
| 108 |
(with-inline-blocks () |
| 109 |
(unwind-exit (negate-argument arg dest))))))) |
| 110 |
|
| 111 |
(defun c2fmla-and (c1form butlast last) |
| 112 |
(flet ((c2expr-and-arguments (eval-dest exit-dest) |
| 113 |
(loop with *destination* = eval-dest |
| 114 |
for expr in butlast |
| 115 |
for remaining-exprs on butlast |
| 116 |
for type = (c1form-primary-type expr) |
| 117 |
do (cond ((type-false-p type *cmp-env*) |
| 118 |
(warn-dead-code (append (rest remaining-exprs) (list last)) c1form |
| 119 |
"the test ~S always evaluates to false" expr) |
| 120 |
(let ((*destination* exit-dest)) |
| 121 |
(c2expr* expr)) |
| 122 |
(return-from c2expr-and-arguments)) |
| 123 |
((type-true-p type *cmp-env*) |
| 124 |
(let ((*destination* 'TRASH)) |
| 125 |
(c2expr* expr))) |
| 126 |
(t |
| 127 |
(c2expr* expr))) |
| 128 |
finally |
| 129 |
(let ((*destination* exit-dest)) |
| 130 |
(c2expr last))))) |
| 131 |
(if (jump-false-destination-p *destination*) |
| 132 |
(c2expr-and-arguments *destination* *destination*) |
| 133 |
(with-exit-label (normal-exit) |
| 134 |
(with-exit-label (false-label) |
| 135 |
(c2expr-and-arguments `(JUMP-FALSE ,false-label) *destination*)) |
| 136 |
(unwind-exit *vv-nil*))))) |
| 137 |
|
| 138 |
(defun c2fmla-or (c1form butlast last) |
| 139 |
(flet ((c2expr-or-arguments (eval-dest exit-dest operation) |
| 140 |
(loop with *destination* = eval-dest |
| 141 |
for expr in butlast |
| 142 |
for remaining-exprs on butlast |
| 143 |
for type = (c1form-primary-type expr) |
| 144 |
do (cond ((type-true-p type *cmp-env*) |
| 145 |
(warn-dead-code (append (rest remaining-exprs) (list last)) c1form |
| 146 |
"the test ~S always evaluates to true" expr) |
| 147 |
(let ((*destination* 'VALUE0)) |
| 148 |
(c2expr* expr)) |
| 149 |
(return-from c2expr-or-arguments)) |
| 150 |
((type-false-p type *cmp-env*) |
| 151 |
(let ((*destination* 'TRASH)) |
| 152 |
(c2expr* expr))) |
| 153 |
(t |
| 154 |
(funcall operation expr))) |
| 155 |
finally |
| 156 |
(let ((*destination* exit-dest)) |
| 157 |
(c2expr last))))) |
| 158 |
(cond ((jump-true-destination-p *destination*) |
| 159 |
(c2expr-or-arguments *destination* *destination* #'c2expr*)) |
| 160 |
((jump-false-destination-p *destination*) |
| 161 |
(with-exit-label (true-label) |
| 162 |
(c2expr-or-arguments `(JUMP-TRUE ,true-label) *destination* #'c2expr*)) |
| 163 |
(unwind-exit *vv-t*)) |
| 164 |
(t |
| 165 |
(with-exit-label (common-exit) |
| 166 |
(with-exit-label (normal-exit) |
| 167 |
(c2expr-or-arguments *destination* *destination* |
| 168 |
(lambda (expr) |
| 169 |
(let ((*destination* 'VALUE0)) |
| 170 |
(c2expr* expr)) |
| 171 |
(unwind-cond normal-exit :jump-t 'VALUE0)))) |
| 172 |
(unwind-exit 'VALUE0)))))) |
| 173 |
|
| 174 |
(defun c2mv-prog1 (c1form form body) |
| 175 |
(declare (ignore c1form)) |
| 176 |
(with-stack-frame (frame) |
| 177 |
(let ((*destination* 'VALUEZ)) |
| 178 |
(c2expr* form)) |
| 179 |
(wt-nl "ecl_stack_frame_push_values(" frame ");") |
| 180 |
(let ((*destination* 'TRASH)) |
| 181 |
(mapc #'c2expr* body)) |
| 182 |
(wt-nl "ecl_stack_frame_pop_values(" frame ");") |
| 183 |
(unwind-exit 'VALUEZ))) |
| 184 |
|
| 185 |
(defun c2values (c1form forms) |
| 186 |
(declare (ignore c1form)) |
| 187 |
(cond |
| 188 |
;; When the values are not going to be used, then just process each form |
| 189 |
;; separately. |
| 190 |
((eq *destination* 'TRASH) |
| 191 |
(mapc #'c2expr* forms) |
| 192 |
;; We really pass no value, but we need UNWIND-EXIT to trigger all the |
| 193 |
;; frame-pop and all other exit forms. |
| 194 |
(unwind-exit 'VALUE0)) |
| 195 |
;; For (VALUES) we can replace the output with either NIL (if the value is |
| 196 |
;; actually used) and set only NVALUES when the value is the output of a |
| 197 |
;; function. |
| 198 |
((endp forms) |
| 199 |
(case *destination* |
| 200 |
(VALUEZ |
| 201 |
(wt-nl "cl_env_copy->values[0] = ECL_NIL;") |
| 202 |
(wt-nl "cl_env_copy->nvalues = 0;") |
| 203 |
(unwind-exit 'VALUEZ)) |
| 204 |
(LEAVE |
| 205 |
(wt-nl "value0 = ECL_NIL;") |
| 206 |
(wt-nl "cl_env_copy->nvalues = 0;") |
| 207 |
(unwind-exit 'LEAVE)) |
| 208 |
(otherwise (unwind-exit *vv-nil*)))) |
| 209 |
;; For a single form, we must simply ensure that we only take a single |
| 210 |
;; value of those that the function may output. |
| 211 |
((endp (rest forms)) |
| 212 |
(let ((form (first forms))) |
| 213 |
(if (or (not (member *destination* '(LEAVE VALUEZ))) |
| 214 |
(c1form-single-valued-p form)) |
| 215 |
(c2expr form) |
| 216 |
(progn |
| 217 |
(let ((*destination* 'VALUE0)) |
| 218 |
(c2expr* form)) |
| 219 |
(unwind-exit 'VALUE0))))) |
| 220 |
;; In all other cases, we store the values in the VALUES vector, |
| 221 |
;; and force the compiler to retrieve anything out of it. |
| 222 |
(t |
| 223 |
(with-inline-blocks () |
| 224 |
(let* ((nv (length forms)) |
| 225 |
(forms (nreverse (coerce-args (inline-args forms))))) |
| 226 |
;; By inlining arguments we make sure that VL has no call to funct. |
| 227 |
;; Reverse args to avoid clobbering VALUES(0) |
| 228 |
(wt-nl "cl_env_copy->nvalues = " nv ";") |
| 229 |
(do ((vl forms (rest vl)) |
| 230 |
(i (1- (length forms)) (1- i))) |
| 231 |
((null vl)) |
| 232 |
(declare (fixnum i)) |
| 233 |
(wt-nl "cl_env_copy->values[" i "] = " (first vl) ";")) |
| 234 |
(unwind-exit 'VALUEZ)))))) |