1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
|
;;; CMPMULT Multiple-value-call and Multiple-value-prog1.
;;;
;; Copyright (C) 1994 M. Hagiya, W. Schelter, T. Yuasa
;; Copyright (C) 2024 Camm Maguire
;; This file is part of GNU Common Lisp, herein referred to as GCL
;;
;; GCL is free software; you can redistribute it and/or modify it under
;; the terms of the GNU LIBRARY GENERAL PUBLIC LICENSE as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;;
;; GCL is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
;; License for more details.
;;
;; You should have received a copy of the GNU Library General Public License
;; along with GCL; see the file COPYING. If not, write to the Free Software
;; Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
(in-package :compiler)
(si:putprop 'multiple-value-call 'c1multiple-value-call 'c1special)
(si:putprop 'multiple-value-call 'c2multiple-value-call 'c2)
(si:putprop 'multiple-value-prog1 'c1multiple-value-prog1 'c1special)
(si:putprop 'multiple-value-prog1 'c2multiple-value-prog1 'c2)
(si:putprop 'values 'c1values 'c1)
(si:putprop 'values 'c2values 'c2)
(si:putprop 'multiple-value-bind 'c1multiple-value-bind 'c1)
(si:putprop 'multiple-value-bind 'c2multiple-value-bind 'c2)
(defun nval (x)
(cond ;((type>= #t(returns-exactly) x) 0)
((single-type-p x) 1)
((when (consp x) (eq (car x) 'returns-exactly)) (1- (length x)))))
(defun c1multiple-value-call (args
&aux (tsyms (load-time-value
(mapl (lambda (x) (setf (car x) (gensym "MV-CALL")))
(make-list 50)))))
(when (endp args) (too-few-args 'multiple-value-call 1 0))
(let* ((info (make-info))
(nargs (c1args args info))
(tps (mapcar (lambda (x) (info-type (cadr x))) (cdr nargs)))
(vals (mapcar 'nval tps))
(n (if (member nil vals) -1 (reduce '+ vals))))
(cond ((endp (cdr args)) (c1funcall args))
((and (>= (length tsyms) n 0) (inline-possible 'multiple-value-bind))
(let* ((syms (mapcar (lambda (x) (declare (ignore x)) (pop tsyms)) (make-list n)))
(r syms))
(c1expr
(reduce (lambda (x y)
(cond ((= 1 (length (car x)))
`(let ((,(caar x) ,(cadr x))) ,y))
(`(multiple-value-bind ,@x ,y))))
(mapcar (lambda (x y) (let* ((n (nval x)) syms)
(dotimes (i n) (push (pop r) syms))
(list (nreverse syms) y))) tps (cdr args))
:from-end t :initial-value `(funcall ,(car args) ,@syms)))))
((list 'multiple-value-call info (pop nargs) nargs)))))
(defun c2multiple-value-call (funob forms &aux (*vs* *vs*) (loc (list 'vs (vs-push))) top sup)
(let ((*value-to-go* loc))
(c2expr* funob))
(cond ((endp (cdr forms))
(let ((*value-to-go* 'top)) (c2expr* (car forms))))
((setq top (cs-push t t))
(setq sup (cs-push t t))
(base-used)
;; Add (sup .var) handling in unwind-exit -- in
;; c2multiple-value-prog1 and c2-multiple-value-call, apparently
;; alone, c2expr-top is used to evaluate arguments, presumably to
;; preserve certain states of the value stack for the purposes of
;; retrieving the final results. c2exprt-top rebinds sup, and
;; vs_top in turn to the new sup, causing non-local exits to lose
;; the true top of the stack vital for subsequent function
;; evaluations. We unwind this stack supremum variable change here
;; when necessary. CM 20040301
(wt-nl "{object *V" top "=base+" *vs* ",*V" sup "=sup;")
(dolist (form forms)
(let ((*value-to-go* 'top)
(*unwind-exit* (cons (cons 'sup sup) *unwind-exit*)))
(c2expr-top* form top))
(wt-nl "while(vs_base<vs_top)")
(wt-nl "{V" top "[0]=vs_base[0];V" top "++;vs_base++;}"))
(wt-nl "vs_base=base+" *vs* ";vs_top=V" top ";sup=V" sup ";")))
(if *compiler-push-events*
(wt-nl "super_funcall(" loc ");")
(if *super-funcall*
(funcall *super-funcall* loc)
(wt-nl "super_funcall_no_event(" loc ");")))
(unwind-exit 'fun-val)
(when (cdr forms)
(wt "}")))
;; (defun c2multiple-value-call (funob forms &aux (*vs* *vs*) loc top sup)
;; (cond ((endp (cdr forms))
;; (setq loc (save-funob funob))
;; (let ((*value-to-go* 'top)) (c2expr* (car forms)))
;; (c2funcall funob 'args-pushed loc))
;; (t
;; (setq top (cs-push t t))
;; (setq sup (cs-push t t))
;; (setq loc (save-funob funob))
;; (base-used)
;; ;; Add (sup .var) handling in unwind-exit -- in
;; ;; c2multiple-value-prog1 and c2-multiple-value-call, apparently
;; ;; alone, c2expr-top is used to evaluate arguments, presumably to
;; ;; preserve certain states of the value stack for the purposes of
;; ;; retrieving the final results. c2exprt-top rebinds sup, and
;; ;; vs_top in turn to the new sup, causing non-local exits to lose
;; ;; the true top of the stack vital for subsequent function
;; ;; evaluations. We unwind this stack supremum variable change here
;; ;; when necessary. CM 20040301
;; (wt-nl "{object *V" top "=base+" *vs* ",*V" sup "=sup;")
;; (dolist (form forms)
;; (let ((*value-to-go* 'top)
;; (*unwind-exit* (cons (cons 'sup sup) *unwind-exit*)))
;; (c2expr-top* form top))
;; (wt-nl "while(vs_base<vs_top)")
;; (wt-nl "{V" top "[0]=vs_base[0];V" top "++;vs_base++;}"))
;; (wt-nl "vs_base=base+" *vs* ";vs_top=V" top ";sup=V" sup ";")
;; (c2funcall funob 'args-pushed loc)
;; (wt "}"))))
(defun c1multiple-value-prog1 (args
&aux form info tp (tsyms (load-time-value
(mapl (lambda (x) (setf (car x) (gensym "MV-PROG1")))
(make-list 50)))))
(when (endp args) (too-few-args 'multiple-value-prog1 1 0))
(with-restore-vars
(setq form (c1expr (car args)) info (copy-info (cadr form)) tp (info-type info))
(unless (or (single-type-p tp)
(and (consp tp) (eq (car tp) 'returns-exactly) (>= (length tsyms) (length (cdr tp)))))
(keep-vars)))
(cond ((single-type-p tp)
(let ((s (pop tsyms)))
(c1expr `(let ((,s ,(car args))) ,@(cdr args) ,s))))
((and (consp tp) (eq (car tp) 'returns-exactly) (>= (length tsyms) (length (cdr tp))))
(let ((syms (mapcar (lambda (x) (declare (ignore x)) (pop tsyms)) (cdr tp))))
(c1expr `(multiple-value-bind (,@syms) ,(car args) ,@(cdr args) (values ,@syms)))))
(t
(setq args (c1args (cdr args) info))
; (setf (info-type info) (info-type (cadr form)))
(list 'multiple-value-prog1 info form args))))
;; (defun c1multiple-value-prog1 (args &aux (info (make-info)) form)
;; (when (endp args) (too-few-args 'multiple-value-prog1 1 0))
;; (setq form (c1expr* (car args) info))
;; (let ((tp (info-type (cadr form))))
;; (cond ((single-type-p tp) (let ((s (tmpsym))) (c1expr `(let ((,s ,(car args))) ,@(cdr args) ,s))))
;; ((and (consp tp) (eq (car tp) 'returns-exactly))
;; (let ((syms (mapcar (lambda (x) (declare (ignore x)) (tmpsym)) (cdr tp))))
;; (c1expr `(multiple-value-bind (,@syms) ,(car args) ,@(cdr args) (values ,@syms)))))
;; (t
;; (setq args (c1args (cdr args) info))
;; (setf (info-type info) (info-type (cadr form)))
;; (list 'multiple-value-prog1 info form args)))))
;; We may record information here when *value-to-go* = 'top
(defvar *top-data* nil)
(defun c2multiple-value-prog1 (form forms &aux (base (cs-push t t))
(top (cs-push t t))
(sup (cs-push t t))
top-data)
(let ((*value-to-go* 'top)
*top-data*)
(c2expr* form)
(setq top-data *top-data*))
;; Add (sup .var) handling in unwind-exit -- in
;; c2multiple-value-prog1 and c2-multiple-value-call, apparently
;; alone, c2expr-top is used to evaluate arguments, presumably to
;; preserve certain states of the value stack for the purposes of
;; retrieving the final results. c2exprt-top rebinds sup, and
;; vs_top in turn to the new sup, causing non-local exits to lose
;; the true top of the stack vital for subsequent function
;; evaluations. We unwind this stack supremum variable change here
;; when necessary. CM 20040301
(wt-nl "{object *V" top "=vs_top,*V" base "=vs_base,*V" sup "=sup;")
(setq *sup-used* t)
(wt-nl "vs_base=V" top ";")
(dolist (form forms)
(let ((*value-to-go* 'trash)
(*unwind-exit* (cons (cons 'sup sup) *unwind-exit*)))
(c2expr-top* form top)))
(wt-nl "vs_base=V" base ";vs_top=V" top ";sup=V" sup ";}")
(unwind-exit 'fun-val nil (if top-data (car top-data))))
(defun c1values (args &aux (info (make-info))(a (mapcar (lambda (x) (c1expr* x info)) args)))
(when (and a (not (cdr a)) (single-type-p (info-type (cadar a))))
(return-from c1values (car a)))
(setf (info-type info)
(let ((x (mapcar (lambda (x) (coerce-to-one-value (info-type (cadr x)))) a)))
(if (unless (cdr x) x) (car x) (cons 'returns-exactly x))));FIXME
(list 'values info a))
;; (defun c1values (args &aux (info (make-info)))
;; (cond ((and args (not (cdr args)))
;; (let ((nargs (c1args args info)))
;; (if (type>= t (info-type (cadar nargs)))
;; (c1expr (car args))
;; (c1expr (let ((s (tmpsym))) `(let ((,s ,(car args))) ,s))))))
;; (t
;; (setq args (c1args args info))
;; (setf (info-type info)
;; (cmp-norm-tp
;; (cons 'returns-exactly
;; (mapcar (lambda (x) (coerce-to-one-value (info-type (cadr x)))) args))))
;; (list 'values info args))))
(defun c2values (forms)
(let* ((*inline-blocks* 0)
(types (mapcar (lambda (x) (let ((x (coerce-to-one-value (info-type (cadr x))))) (if (type>= #tboolean x) t x))) forms))
(i -1)
;FIXME all of this unnecessary, just avoid valp[i]=base[0]
(r (mapcar (lambda (x y &aux (x (when x (write-to-string (incf i))))) (strcat (rep-type y) " _t" x "=#" x ";")) (or forms (list (c1nil))) (or types (list #tnull))))
(i 0)
(s (mapcar (lambda (x &aux (x (when x (write-to-string (incf i))))) (strcat "@" x "(_t" x ")@")) (cdr forms)))
(s (strcat "({" (apply 'strcat (nconc r s)) "_t0;})"));FIXME
(s (cons s (mapcar 'inline-type (cdr types))))
(in (list (inline-type (car types)) (flags) s (inline-args forms types))))
(unwind-exit in nil (cons 'values (length forms)))
(close-inline-blocks)))
(defun c1multiple-value-bind (args &aux (info (make-info))
(vars nil) (vnames nil) init-form
ss is ts body other-decls
(*vars* *vars*))
(when (or (endp args) (endp (cdr args)))
(too-few-args 'multiple-value-bind 2 (length args)))
(when (and (caar args) (not (cdar args)))
(return-from c1multiple-value-bind
(c1expr `(let ((,(caar args) ,(cadr args))) ,@(cddr args)))))
(multiple-value-setq (body ss ts is other-decls) (c1body (cddr args) nil))
(dolist (s (car args))
(let ((v (c1make-var s ss is ts)))
(push s vnames)
(push v vars)))
(c1add-globals (set-difference ss vnames))
(setq init-form (c1arg (cadr args) info))
(unless (let ((x (info-type (cadr init-form))))
(if (cmpt x) (not (member nil x)) x))
(eliminate-src body)
(return-from c1multiple-value-bind init-form))
(when (single-type-p (info-type (cadr init-form)))
(return-from c1multiple-value-bind
(c1let-* (cons (cons (list (caar args) (cadr args)) (cdar args)) (cddr args)) t
(cons init-form (mapcar (lambda (x) (declare (ignore x)) (c1nil)) (cdar args))))))
(setq vars (nreverse vars))
(let* ((tp (info-type (second init-form)))
(tp (if (eq tp '*) (make-list (length vars) :initial-element t) (cdr tp))))
(do ((v vars (cdr v)) (t1 tp (cdr t1)))
((not v))
(set-var-init-type (car v) (if t1 (car t1) #tnull))))
(dolist (v vars) (push-var v init-form))
(check-vdecl vnames ts is)
(setq body (c1decl-body other-decls body))
(add-info info (cadr body))
(setf (info-type info) (info-type (cadr body)))
(ref-vars body vars)
(dolist (var vars) (check-vref var))
;; (let* ((*vars* ov));FIXME
;; (print (setq fff (trim-vars vars (make-list (length vars) :initial-element init-form) body nil)))
;; (break))
(list 'multiple-value-bind info vars init-form body))
(defun max-stack-space (form) (abs (vald (info-type (cadr form)))))
(defun stack-space (form)
(let* ((tp (info-type (cadr form)))
(vd (vald tp)))
(cond ((< vd 0) (- vd))
((equal tp #t(returns-exactly)) 0))))
(defvar *mvb-vals* nil)
(defvar *vals-set* nil)
(defun c2multiple-value-bind (vars init-form body
&aux (labels nil)
(*unwind-exit* *unwind-exit*)
(*vs* *vs*) (*clink* *clink*) (*ccb-vs* *ccb-vs*)
top-data lbs)
(let* ((mv (make-var :type #tfixnum :kind 'lexical :loc (cs-push #tfixnum t)))
(nv (1- (length vars)))
(ns1 (stack-space init-form))
(ns (max nv (or ns1 (max-stack-space init-form))))
(*mvb-vals* t)
*vals-set*)
(setf (var-kind mv) (c2var-kind mv) (var-space mv) nv (var-known-init mv) (or ns1 -1))
(setq lbs
(mapcar (lambda (x)
(let ((kind (c2var-kind x))(f (eq x (car vars))))
(if kind (setf (var-kind x) (if f kind 'object)
(var-loc x) (cs-push (if f (var-type x) t) t))
(setf (var-ref x) (vs-push) x (cs-push (if f (var-type x) t) t)))))
vars))
; (wt-nl "{")
; (wt-nl "int vals_set=0;")
(when vars
(wt-nl "register " (rep-type (var-type (car vars))) " V" (car lbs) ";")
(wt-nl "object V" (var-loc mv) "[" ns "];"))
(let ((i -1)) (mapc (lambda (x) (wt-nl "#define V" x " V" (var-loc mv) "[" (incf i) "]")) (cdr lbs)))
(wt-nl);FIXME
(dotimes (i (1+ (length vars))) (push (next-label) labels))
(wt-nl "{")
;; (wt-nl "int vals_set=0;")
(let ((*mv-var* mv)
(*value-to-go* (or (mapcar (lambda (x) (list 'cvar x)) lbs) 'trash))
*top-data*)
(c2expr* init-form)
(setq top-data *top-data*))
(and *record-call-info* (record-call-info nil (car top-data)))
(when lbs (unless *vals-set* (baboon)))
;; (wt-nl "if (!vals_set) {")
;; (setq labels (nreverse labels))
;; (do ((lb lbs (cdr lb))
;; (lab labels (cdr lab)))
;; ((endp lb)(reset-top)(wt-go (car lab)))
;; (wt-nl "if(vs_base>=vs_top){")
;; (reset-top)
;; (wt-go (car lab))
;; (wt "}")
;; (set-cvar '(vs-base 0) (car lb))
;; (when (cdr lb)
;; (wt-nl "vs_base++;")))
;; (do ((lb lbs (cdr lb))
;; (lab labels (cdr lab)))
;; ((endp lb)(wt-label (car lab)))
;; (wt-label (car lab))
;; (set-cvar nil (car lb)))
;; (wt-nl "}}")
(do ((vs vars (cdr vs)) (lb lbs (cdr lb)))
((endp vs))
(when (member (var-kind (car vs)) '(lexical special down))
(c2bind-loc (car vs) (list 'cvar (car lb))))))
(c2expr body)
(mapc (lambda (x) (wt-nl "#undef V" x)) (cdr lbs))
(wt-nl "")
(wt-nl "}"))
;; (defun c2multiple-value-bind (vars init-form body
;; &aux (labels nil)
;; (*unwind-exit* *unwind-exit*)
;; (*vs* *vs*) (*clink* *clink*) (*ccb-vs* *ccb-vs*)
;; top-data lbs)
;; (multiple-value-check vars init-form)
;; (let* ((mv (make-var :type #tfixnum :kind 'lexical :loc (cs-push #tfixnum t)))
;; (nv (1- (length vars)))
;; (ns1 (stack-space init-form))
;; (ns (max nv (or ns1 (max-stack-space init-form))))
;; (*mvb-vals* t))
;; (setf (var-kind mv) (c2var-kind mv) (var-space mv) nv (var-known-init mv) (or ns1 -1))
;; (setq lbs
;; (mapcar (lambda (x)
;; (let ((kind (c2var-kind x))(f (eq x (car vars))))
;; (if kind (setf (var-kind x) (if f kind 'object)
;; (var-loc x) (cs-push (if f (var-type x) t) t))
;; (setf (var-ref x) (vs-push) x (cs-push (if f (var-type x) t) t)))))
;; vars))
;; (wt-nl "{")
;; ; (wt-nl "int vals_set=0;")
;; (when vars
;; (wt-nl "register " (rep-type (var-type (car vars))) " V" (car lbs) ";")
;; (wt-nl "object V" (var-loc mv) "[" ns "];"))
;; (let ((i -1)) (mapc (lambda (x) (wt-nl "#define V" x " V" (var-loc mv) "[" (incf i) "]")) (cdr lbs)))
;; (wt-nl);FIXME
;; (dotimes (i (1+ (length vars))) (push (next-label) labels))
;; (wt-nl "{")
;; (wt-nl "int vals_set=0;")
;; (let ((*mv-var* mv)
;; (*value-to-go* (or (mapcar (lambda (x) (list 'cvar x)) lbs) 'trash))
;; *top-data*)
;; (c2expr* init-form)
;; (setq top-data *top-data*))
;; (and *record-call-info* (record-call-info nil (car top-data)))
;; (wt-nl "if (!vals_set) {")
;; (setq labels (nreverse labels))
;; (do ((lb lbs (cdr lb))
;; (lab labels (cdr lab)))
;; ((endp lb)(reset-top)(wt-go (car lab)))
;; (wt-nl "if(vs_base>=vs_top){")
;; (reset-top)
;; (wt-go (car lab))
;; (wt "}")
;; (set-cvar '(vs-base 0) (car lb))
;; (when (cdr lb)
;; (wt-nl "vs_base++;")))
;; (do ((lb lbs (cdr lb))
;; (lab labels (cdr lab)))
;; ((endp lb)(wt-label (car lab)))
;; (wt-label (car lab))
;; (set-cvar nil (car lb)))
;; (wt-nl "}}")
;; (do ((vs vars (cdr vs)) (lb lbs (cdr lb)))
;; ((endp vs))
;; (when (member (var-kind (car vs)) '(lexical special down))
;; (c2bind-loc (car vs) (list 'cvar (car lb))))))
;; (c2expr body)
;; (mapc (lambda (x) (wt-nl "#undef V" x)) (cdr lbs))
;; (wt-nl "")
;; (wt-nl "}"))
|