File: theme-d-linker-instantiation.scm

package info (click to toggle)
theme-d 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 12,784 kB
  • sloc: lisp: 47,684; sh: 4,200; makefile: 455; ansic: 319
file content (376 lines) | stat: -rw-r--r-- 12,128 bytes parent folder | download
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
; -*-scheme-*-

;; Copyright (C) 2008-2015 Tommi Höynälänmaa
;; Distributed under GNU General Public License version 3
;; see file doc/GPL-3.



;; *** Linker instantiation and factorization ***


(import (rnrs exceptions)
	(srfi srfi-1)
	(th-scheme-utilities stdutils)
	(th-scheme-utilities hrecord))


(define theme-target-compile-instance-predef-fwd '())
(define theme-target-compile-instance-fwd '())
(define compile-factorized-expr-fwd '())
(define tc-tree-il-instance-predef-fwd '())
(define tc-tree-il-instance-fwd '())
(define tc-tree-il-factorized-expr-fwd '())


(define gl-ctr11 0)


(define-hrecord-type <linker-instance-predef> ()
  lst-instance)


(define-hrecord-type <linker-instance> ()
  lst-instance)


(define-hrecord-type <factorized-expr> ()
  address to)


(define (get-binder-for-inst linker)
  (hfield-ref linker 'binder-instantiation))


(define (inst-handle-type-vars linker repr)
  (let ((prev-state (hfield-ref linker 'state))
	(binder (get-binder-for-inst linker)))
    (hfield-set! linker 'state 'binding)
    (if gl-test8 (raise 'inst-test-error))
    (dwl4 (hfield-ref binder 'type-check?))
    (let ((result
	   (inst-bind-type-vars binder '() repr)))
      (hfield-set! linker 'state prev-state)
      result)))


(define (can-be-factorized? repr l-visited)
  (cond
   ;; Objects containing nulls or primitive values can be factorized.
   ((or (is-null-obj? repr) (is-null-class-entity? repr)) #t)
   ((and (is-target-object? repr) (hfield-ref repr 'primitive?)) #t)
   ((memq repr l-visited) #t)
   ((is-normal-variable? repr) #f)
   ((is-t-type-variable? repr) #f)
   ;; Definitions of signatures and parametrized signatures involve
   ;; corresponding objects. Those definitions may not be factorized (?).
   ((is-t-signature? repr) #f)
   ((is-t-param-signature? repr) #f)
   ((not (is-target-object? repr)) #f)
   (else
    ;; Parametrized procedures and parametrized logical types
    ;; may contain expressions in field value-expr.
    ;; OTOH do-tcomp-object seems not to be able to
    ;; compile parametrized procedures or parametrized logical
    ;; types without address.
    (let ((repr-type (get-entity-type repr)))
      (if (or (is-tc-param-proc? repr-type)
	      (is-t-param-logical-type? repr-type))
	  #f
	  (let ((l-subreprs (get-subexpressions repr))
		(l-new-visited (cons repr l-visited)))
	    (and-map? (lambda (ent) (can-be-factorized? ent l-new-visited))
		      l-subreprs)))))))


(define (factorize-pair linker repr fact visited)
  (let* ((new-visited (cons repr visited))
	 (x-head (factorize-subexprs linker (car repr)
				     fact new-visited))
	 (x-tail (factorize-subexprs linker (cdr repr)
				     fact new-visited)))
    (if (or (not (eqv? x-head (car repr)))
	    (not (eqv? x-tail (cdr repr))))
	(cons x-head x-tail)
	repr)))


(define (factorize-object linker repr fact)
  (if (and (null? (hfield-ref repr 'address))
	   (can-be-factorized? repr '()))
      ;; Factorized variables are toplevel.
      (let ((address (linker-alloc-loc linker 'f #t)))
	(hfield-set! fact 'element
		     (append (hfield-ref fact 'element)
			     (list (cons address repr))))
	(let ((to-new (make-object-with-address repr address)))
	  (hashq-set! (hfield-ref linker 'ht-fact) repr to-new)
	  to-new))
      repr))


(define (factorize-normal-entity linker repr fact visited)
  (let* ((new-visited (cons repr visited))
	 (subreprs (get-subexpressions repr))
	 (translated-subreprs
	  (map* (lambda (subrepr)
		  (factorize-subexprs linker subrepr fact new-visited))
		subreprs))
	 ;; There should be no need to do type checking here.
	 (new-repr
	  (let* ((binder (get-binder-for-inst linker))
		 (old-type-check? (hfield-ref binder 'type-check?))
		 (old-preserve-types? (hfield-ref binder 'preserve-types?))
		 (old-instantiation? (hfield-ref binder 'instantiation?))
		 (old-make-instances? (hfield-ref binder 'make-instances?)))
	    (hfield-set! binder 'type-check? #f)
	    (hfield-set! binder 'preserve-types? #t)
	    (hfield-set! binder 'instantiation? #f)
	    (hfield-set! binder 'make-instances? #f)
	    (let ((result
		   (clone-with-branches
		    binder
		    repr
		    translated-subreprs #f)))
	      (hfield-set! binder 'type-check? old-type-check?)
	      (hfield-set! binder 'preserve-types? old-preserve-types?)
	      (hfield-set! binder 'instantiation? old-instantiation?)
	      (hfield-set! binder 'make-instances? old-make-instances?)
	      result))))
    new-repr))


(define (factorize-subexprs linker repr fact visited)
  (assert (is-linker? linker))
  (assert (or (null? repr) (pair? repr) (is-entity? repr)))
  (assert (hrecord-is-instance? fact <singleton>))
  (assert (list? visited))

  ;; (set! gl-counter25 (+ gl-counter25 1))
  ;; (dwli gl-counter25)
  ;; (dwi "fact ")
  ;; (dwc gl-counter25)
  ;; (dwc " ")
  ;; (dwc gl-indent)
  ;; (dwc " ")
  ;; (if (hrecord? repr)
  ;;     (begin
  ;; 	(dwc (hrecord-type-name-of repr))
  ;; 	(dwc " ")))
  ;; (if (is-target-object? repr)
  ;;     (dwc (debug-get-string repr)))
  ;; (dwli-newline)

  (let ((old-indent gl-indent))
    (set! gl-indent (+ gl-indent 1))
    (let ((result
	   (cond
	    ((or (is-null-obj? repr) (is-null-class-entity? repr)) repr)
	    ;; Primitive atomic objects are not factorized but other
	    ;; primitive objects are.
	    ((is-t-atomic-object? repr) repr)
	    ((hashq-ref (hfield-ref linker 'ht-fact) repr)
	     =>
	     (lambda (x) x))
	    ((memv repr visited) repr)
	    ((pair? repr)
	     (factorize-pair linker repr fact visited))
	    ((is-normal-variable? repr) repr)
	    ((is-t-type-variable? repr) repr)
	    ((is-target-object? repr)
	     (factorize-object linker repr fact))
	    (else
	     (factorize-normal-entity linker repr fact visited)))))
      (set! gl-indent old-indent)
      result)))


(define (factorize-instance linker inst fact)
  (assert (list? inst))
  (assert (memv (car inst) '(class ltype proc raw-proc)))
  (if (eq? (car inst) 'proc)
      (let* ((old-expr (caddr inst))
	     (new-expr (factorize-subexprs linker old-expr fact '())))
	(list 'proc (cadr inst) new-expr))
      inst))


(define (make-linker-instance-predef linker lst-instance)
  (make-hrecord <linker-instance-predef> lst-instance))


(define (make-linker-instance-predefs linker lst-instances)
  (map (lambda (lst-inst) (make-linker-instance-predef linker lst-inst))
       lst-instances))


(define (make-linker-instance linker lst-instance)
  (let ((result
	 (make-hrecord <linker-instance> lst-instance)))
    result))


(define (make-linker-instances linker lst-instances)
  (map (lambda (lst-inst) (make-linker-instance linker lst-inst))
       lst-instances))


(define (make-linker-factorized-exprs linker fact)
  (let ((exprs (hfield-ref fact 'element)))
    (map (lambda (expr)
	   (make-hrecord <factorized-expr> (car expr) (cdr expr)))
	 exprs)))


(define (determine-instance-cov linker inst)
  (let ((lst (hfield-ref inst 'lst-instance)))
    ;; Should we do something for parametrized class and type instances, too?
    (if (eq? (car lst) 'proc)
	(determine-coverage linker (list-ref lst 2) '()))))


(define (determine-cov-for-instances linker lst-instances)
  (for-each (lambda (inst) (determine-instance-cov linker inst))
	    lst-instances))


(define (theme-do-target-instantiate linker repr)
  ;; We clear the ht-fact hash table when we start to process a new toplevel
  ;; expression.
  (hash-clear! (hfield-ref linker 'ht-fact))
  (let ((result
	 (if (hfield-ref linker 'factorize?)
	     (let* ((bind-result (inst-handle-type-vars linker repr))
		    (inst (cdr bind-result))
		    (fact (make-hrecord <singleton> '()))
		    (preinst (make-linker-instance-predefs
			      linker
			      inst))
		    (inst2 (map* (lambda (tp-instance)
				   (factorize-instance linker tp-instance fact))
				 inst))
		    (inst3 (make-linker-instances linker inst2))
		    (repr1 (car bind-result))
		    (repr2 (factorize-subexprs linker repr1 fact '()))
		    (lst-fact (make-linker-factorized-exprs linker fact)))
	       (if (hfield-ref linker 'strip?)
		   (begin
		     (determine-cov-for-instances linker inst3)
		     (determine-coverage linker repr2 '())
		     (hashq-set! (hfield-ref linker 'ht-rebound) repr2 #t)))
	       (append preinst lst-fact inst3 (list repr2)))
	     (begin
	       (let* ((bind-result (inst-handle-type-vars linker repr))
		      (preinst (make-linker-instance-predefs
				linker
				(cdr bind-result)))
		      (inst (make-linker-instances linker (cdr bind-result)))
		      (bound-expr (car bind-result)))
		 (if (hfield-ref linker 'strip?)
		     (begin
		       (determine-cov-for-instances linker inst)
		       (determine-coverage linker bound-expr '())
		       (hashq-set! (hfield-ref linker 'ht-rebound) bound-expr
				   #t)))
	       (append preinst inst (list bound-expr)))))))
    result))


(define (theme-target-instantiate linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (hfield-set! linker 'current-toplevel-repr repr)
  (let ((result
	 (cond
	  ((or 
	    (hrecord-is-instance?
	     repr <param-class-definition>)
	    (hrecord-is-instance?
	     repr <param-logical-type-def>))
	   (list repr))
	  ((and (hrecord-is-instance?
	  	 repr <variable-definition>)
		(or
		 (hrecord-is-instance?
		  (hfield-ref repr 'value-expr)
		  <param-proc-expr>)
		 (hrecord-is-instance?
		  (hfield-ref repr 'value-expr)
		  <prim-proc-ref>)
		 (hrecord-is-instance?
		  (hfield-ref repr 'value-expr)
		  <checked-prim-proc>)))
	   (if (or (not (hfield-ref linker 'strip?))
		   (address-hash-ref
		    (hfield-ref linker 'ht-used)
		    (hfield-ref (hfield-ref repr 'variable) 'address)))
	       (list repr)
	       ;; (if (hfield-ref linker 'factorize?)
	       ;; 	   (let* ((fact (make-hrecord <singleton> '()))
	       ;; 		  (repr-new (factorize-subexprs linker repr fact '()))
	       ;; 		  (lst-fact (make-linker-factorized-exprs linker fact)))
	       ;; 	     (append lst-fact (list repr-new)))
	       ;; 	   (list repr))
	       '()))
	  ((hrecord-is-instance? repr <forward-declaration>)
	   (list repr))
	  ((hrecord-is-instance? repr <variable-definition>)
	   (if (or (not (hfield-ref linker 'strip?))
		   (address-hash-ref
		    (hfield-ref linker 'ht-used)
		    (hfield-ref (hfield-ref repr 'variable) 'address)))
	       (theme-do-target-instantiate linker repr)
	       '()))
	  ((hrecord-is-instance? repr <method-definition>)
	   (if (or (not (hfield-ref linker 'strip?))
		   (hfield-ref repr 'include?))
	       (begin
		 (theme-do-target-instantiate linker repr))
	       '()))
	  ((hrecord-is-instance? repr <prevent-stripping-expr>)
	   '())
	  ((hrecord-is-instance? repr <method-declaration>)
	   (list repr))
	  (else
	   (theme-do-target-instantiate linker repr)))))
    (hfield-set! linker 'current-toplevel-repr '())
    result))


(define (theme-instantiate-program linker lst-reprs)
  (let ((lst-reprs1 (reverse lst-reprs))
	(binder (hfield-ref linker 'binder-instantiation)))
    (let ((result '()))
      (do ((lst-cur lst-reprs1 (cdr lst-cur))
	   (i 0 (+ i 1)))
	  ((null? lst-cur))
	(dw2 "Instantiating expression number ")
	(dwl2 i)
	(let ((expr (car lst-cur)))
	  (if (hrecord-is-instance? expr <variable-definition>)
	      (dwl2 (hfield-ref (hfield-ref (hfield-ref expr 'variable)
					    'address)
				'source-name))))
	;; (let ((expr (car lst-cur)))
	;;   (if (and (hrecord-is-instance? expr <variable-definition>)
	;; 	   (or
	;; 	    (hrecord-is-instance? (hfield-ref expr 'value-expr)
	;; 				  <procedure-expression>)
	;; 	    (hrecord-is-instance? (hfield-ref expr 'value-expr)
	;; 				  <param-proc-expr>)))
	;;       (begin
	;;	(dwl2 "addr HEP")
	(let ((expr (car lst-cur)))
	  (if (hrecord-is-instance? expr <variable-definition>)
	      (hfield-set! binder 's-cur-toplevel
			   (let ((address
				  (hfield-ref (hfield-ref expr 'variable)
					      'address)))
			     (if (not-null? address)
				 (hfield-ref address 'source-name))
				 '()))))
		(set! result
		      (append
		       result
		       (theme-target-instantiate linker (car lst-cur))))
		(hfield-set! binder 's-cur-toplevel '()))
    result)))