Debian Codegraph @

src/cmp/cmpbackend-cxx/cmppass2-cont.lsp

raw
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) 2021, Daniel Kochmański
6 ;;;;
7 ;;;; This program is free software; you can redistribute it and/or
8 ;;;; modify it under the terms of the GNU Library General Public
9 ;;;; License as published by the Free Software Foundation; either
10 ;;;; version 2 of the License, or (at your option) any later version.
11 ;;;;
12 ;;;; See file '../Copyright' for full details.
13 ;;;;
14
15 (in-package #:compiler)
16
17
18 (defun c2block (c1form blk body)
19 (declare (ignore c1form))
20 (setf (blk-exit blk) *exit*
21 (blk-destination blk) *destination*)
22 (if (plusp (var-ref (blk-var blk)))
23 (let* ((blk-var (blk-var blk))
24 (*env-lvl* *env-lvl*))
25 (check-vref blk-var)
26 (with-lexical-scope ()
27 (when (eq :object (var-kind blk-var))
28 (setf (var-loc blk-var) (next-lcl))
29 (wt-nl "cl_object " blk-var ";"))
30 (when (env-grows (var-ref-ccb blk-var))
31 ;; var is referenced from a closure which may escape.
32 (let ((env-lvl *env-lvl*))
33 (wt-nl "cl_object " *volatile* "env" (incf *env-lvl*) " = env" env-lvl ";")))
34 (bind 'FRAME++ blk-var)
35 (with-unwind-frame (blk-var)
36 (unwind-exit 'VALUEZ)
37 (c2expr body))
38 (when (var-ref-ccb blk-var)
39 (decf *env*))))
40 (c2expr body)))
41
42 (defun c2return-from (c1form blk nonlocal val)
43 (declare (ignore c1form))
44 (if nonlocal
45 (progn
46 (let ((*destination* 'VALUEZ))
47 (c2expr* val))
48 (unwind-flee blk :return-from))
49 (let ((*destination* (blk-destination blk))
50 (*exit* (blk-exit blk)))
51 (c2expr val))))
52
53
54 (defun c2tagbody (c1form tag-loc body)
55 (declare (type var tag-loc)
56 (ignore c1form))
57 (macrolet ((do-tags ((tag forms result) &body body)
58 ;; Allocate labels.
59 `(dolist (,tag ,forms ,result)
60 (when (and (tag-p ,tag) (plusp (tag-ref ,tag)))
61 (setf (tag-jump ,tag) (next-label t))
62 ,@body))))
63 (if (null (var-kind tag-loc))
64 ;; only local goto's
65 (do-tags (tag body (c2tagbody-body body)))
66 ;; some tag used non locally or inside an unwind-protect
67 (let ((*env* *env*) (*env-lvl* *env-lvl*)
68 (*lex* *lex*) (*lcl* *lcl*)
69 (*inline-blocks* 0)
70 (env-grows (env-grows (var-ref-ccb tag-loc))))
71 (when env-grows
72 (let ((env-lvl *env-lvl*))
73 (maybe-open-inline-block)
74 (wt-nl "volatile cl_object env" (incf *env-lvl*)
75 " = env" env-lvl ";")))
76 (when (eq :OBJECT (var-kind tag-loc))
77 (setf (var-loc tag-loc) (next-lcl))
78 (maybe-open-inline-block)
79 (wt-nl "cl_object " tag-loc ";"))
80 (bind 'FRAME++ tag-loc)
81 (with-unwind-frame (tag-loc)
82 (progn
83 (do-tags (tag body nil)
84 (unwind-cond (tag-jump tag) :jump-eq
85 'VALUEZ (tag-index tag)))
86 (when (var-ref-ccb tag-loc)
87 (wt-nl "ecl_internal_error(\"GO found an inexistent tag\");")))
88 (c2tagbody-body body))
89 (close-inline-blocks)))))
90
91 (defun c2tagbody-body (body)
92 ;;; INV: BODY is a list of tags and forms. We have processed the body
93 ;;; so that the last element is always a form producing NIL.
94 (loop for (this-form next-form . rest) on body do
95 (cond ((tag-p this-form)
96 (wt-label (tag-jump this-form)))
97 ((tag-p next-form)
98 (with-exit-label (*exit* (tag-jump next-form))
99 (let ((*destination* 'TRASH))
100 (c2expr this-form))))
101 (t
102 (c2expr this-form)))))
103
104 (defun c2go (c1form tag nonlocal)
105 (declare (ignore c1form))
106 (if nonlocal
107 (unwind-flee tag :go)
108 (unwind-jump (tag-jump tag))))
109
110
111 (defun c2throw (c1form tag val &aux loc)
112 (declare (ignore c1form))
113 (case (c1form-name tag)
114 ((VARIABLE LOCATION)
115 (setq loc (c1form-arg 0 tag)))
116 (t
117 (setq loc (make-temp-var))
118 (let ((*destination* loc))
119 (c2expr* tag))))
120 (let ((*destination* 'VALUEZ))
121 (c2expr* val))
122 (unwind-flee loc :throw))
123
124 (defun c2catch (c1form tag body)
125 (declare (ignore c1form))
126 (let* ((new-destination (tmp-destination *destination*))
127 (code (gensym "CATCH")))
128 (let ((*destination* 'VALUE0))
129 (c2expr* tag))
130 (let ((*destination* new-destination))
131 (wt-comment "BEGIN CATCH ~A" code)
132 (with-unwind-frame ('VALUE0)
133 (unless (member new-destination '(TRASH VALUEZ))
134 (with-indentation
135 (with-exit-label (*exit*)
136 (unwind-exit 'VALUEZ))))
137 (with-indentation
138 (c2expr* body)))
139 (wt-nl "ecl_frs_pop(cl_env_copy);")
140 (wt-comment "END CATCH ~A" code))
141 (unwind-exit new-destination)))
142
143 (defun c2unwind-protect (c1form form body)
144 (declare (ignore c1form))
145 (with-stack-frame (frame)
146 ;; Here we compile the form which is protected. When this form is aborted,
147 ;; it continues with unwinding=TRUE. We call ecl_frs_pop() manually because
148 ;; we use C2EXPR* in the body.
149 (wt-nl "volatile bool unwinding = FALSE;")
150 (wt-nl "ecl_frame_ptr next_fr;")
151 (with-unwind-frame ("ECL_PROTECT_TAG")
152 (wt-nl " unwinding = TRUE; next_fr=cl_env_copy->frs_stack.nlj_fr;")
153 (let ((*destination* 'VALUEZ))
154 (c2expr* form)))
155 (wt-nl "ecl_frs_pop(cl_env_copy);")
156 ;; Here we save the values of the form which might have been
157 ;; aborted, and execute some cleanup code. This code may also
158 ;; be aborted by some control structure, but is not protected.
159 (wt-nl "ecl_stack_frame_push_values(" frame ");")
160 (let ((*destination* 'TRASH))
161 (c2expr* body))
162 (wt-nl "ecl_stack_frame_pop_values(" frame ");")
163 ;; Finally, if the protected form was aborted, jump to the
164 ;; next catch point...
165 (wt-nl "if (unwinding) ecl_unwind(cl_env_copy,next_fr);")
166 ;; ... or simply return the values of the protected form.
167 (unwind-exit 'VALUEZ)))