File: theme-d-target-compilation-common.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 (243 lines) | stat: -rw-r--r-- 6,993 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
;; Copyright (C) 2017 Tommi Höynälänmaa
;; Distributed under GNU General Public License version 3,
;; see file doc/GPL-3.


;; *** Common procedures for target compilation ***


(import (th-scheme-utilities stdutils))


(define gl-i-max-ebv-opt 10)


(define gl-l-prim-pred
  (list (cons tc-nil 'null?)
	(cons tc-symbol 'symbol?)
	(cons tc-boolean 'boolean?)
	(cons tc-real 'is-real?)
	(cons tc-integer 'is-integer?)
	(cons tc-string 'string?)
	(cons tc-char 'char?)))


(define gl-al-prim-equal
  (list (cons tc-nil 'eq?)
	(cons tc-boolean 'eq?)
	(cons tc-integer 'eqv?)
	(cons tc-real 'eqv?)
	(cons tc-symbol 'eq?)
	(cons tc-string 'equal?)
	(cons tc-char 'eqv?)))


(define gl-al-prim-equal-contents gl-al-prim-equal)


(define gl-al-prim-equal-objects
  (list (cons tc-nil 'eq?)
	(cons tc-boolean 'eq?)
	(cons tc-integer 'eqv?)
	(cons tc-real 'eqv?)
	(cons tc-symbol 'eq?)
	(cons tc-string 'eqv?)
	(cons tc-char 'eqv?)))


(define gl-al-target-eq-pred
  (list (cons 'equal-values? 'theme-equal?)
	(cons 'equal-objects? 'theme-equal-objects?)
	(cons 'equal-contents? 'theme-equal-contents?)))


(define (get-equiv-pred s-kind cl)
  (let* ((al
	  (case s-kind
	    ((equal-values?) gl-al-prim-equal)
	    ((equal-objects?) gl-al-prim-equal-objects)
	    ((equal-contents?) gl-al-prim-equal-contents)
	   (else (raise 'internal-error))))
	 (p (assq cl al)))
    (if p (cdr p) #f)))


(define (get-ht-raw-procs linker)
  (hfield-ref (hfield-ref linker 'binder-instantiation) 'ht-raw-procs))


(define (get-target-new-name linker module number source-name)
  (cond
   ((and (not (eqv? module #f)) (>= number 0))
    (let ((i-target-number
	   (let* ((x-actual-module-name (get-actual-module-name module))
		  (i-base (hash-ref
			   (hfield-ref linker 'ht-module-indices)
			   x-actual-module-name)))
	     (if (integer? i-base)
		 (+ i-base number)
		 (raise
		  (list 'variable-numbering-error
			source-name
			number
			x-actual-module-name))))))
      (string->symbol
       (if (not-null? source-name)
	   (string-append "_u_"
			  (number->string i-target-number)
			  "_"
			  (symbol->string source-name))
	   (string-append "_u_"
			  (number->string i-target-number))))))
   ((and (eqv? module #f) (>= number 0))
    (if (null? source-name)
	(string->symbol
	 (string-append "_l_" (number->string number)))
	(string->symbol
	 (string-append "_l_" (number->string number)
			"_" (symbol->string source-name)))))
   ((= number address-number-builtin)
    (assert (eq? module 'builtins))
    (assert (symbol? source-name))
    (string->symbol (string-append "_b_" (symbol->string source-name))))
   ((= number address-number-target)
    source-name)
   (else
    (write-error-info module)
    (write-error-info number)
    (write-error-info source-name)
    (raise 'internal-illegal-var-ref))))


(define (get-target-var-name linker address)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? address <address>))
  (let* ((ht-lexical-vars (hfield-ref linker 'ht-lexical-vars))
	 (sym-var-name (address-hash-ref ht-lexical-vars address)))
    (if sym-var-name
	sym-var-name
	(let ((module (hfield-ref address 'module))
	      (number (hfield-ref address 'number))
	      (source-name (hfield-ref address 'source-name)))
	  (assert (or (boolean? module) (eqv? module 'builtins)
		      (is-module-name? module)))
	  (assert (integer? number))
	  (assert (or (null? source-name) (symbol? source-name)))
	  (let ((sym-new-name (get-target-new-name linker module number
						   source-name)))
	    (address-hash-set! ht-lexical-vars address sym-new-name)
	    sym-new-name)))))


(set! get-target-var-name-fwd get-target-var-name)


(define (do-compile-value obj)
  (assert (is-target-object? obj))
  (cond
   ((eqv? obj to-nil) '())
   ((is-t-atomic-object? obj) (get-contents obj))
   ((target-type=? (get-entity-type (get-entity-type obj))
		   tpc-pair)
    (cons (do-compile-value (tno-field-ref obj 'first))
	  (do-compile-value (tno-field-ref obj 'second))))
   (else (raise 'invalid-value-expression))))


(define (param-class-inst-contains-tvars? instance)
  (let* ((to (list-ref instance 1))
	 (tvar-values (tno-field-ref to 'l-tvar-values)))
    (and (not-null? tvar-values)
	 (or-map? contains-type-variables? tvar-values))))


(define (get-let-keyword recursive? order?)
  (cond
   ((and recursive? order?) 'letrec*)
   ((and recursive? (not order?)) 'letrec)
   ;; let* is implemented as a macro so we should not have it here.
   ((and (not recursive?) order?)
    (raise 'invalid-let*)
    'let*)
   (else 'let)))


(define (get-initializer-arg-name index)
  (assert (integer? index))
  (string->symbol (string-append "arg" (number->string index))))


(define (get-constructor-field-args fields)
  (let ((len (length fields))
	(args '()))
    (do ((i 1 (+ i 1)) (cur-lst fields (cdr cur-lst)))
	((null? cur-lst) args)
      (let ((cur-field (car cur-lst)))
	(if (not (tno-field-ref cur-field 'has-init-value?))
	    (set! args
		  (append args (list (get-initializer-arg-name i)))))))))


(define (get-field-index field-name type)
  (let* ((all-fields (tno-field-ref type 'l-all-fields))
	 (index
	  (general-search
	   field-name all-fields
	   (lambda (a b) (eqv? a (tno-field-ref b 's-name))))))
    (if (= index -1)
	(raise (list 'nonexistent-field (cons 's-field-name field-name)))
	(+ index 1))))


(define (get-field field-name clas)
  (let* ((all-fields (tno-field-ref clas 'l-all-fields))
	 (field (find
		 (lambda (fld)
		   (eq? field-name (tno-field-ref fld 's-name)))
		 all-fields)))
    (if (eqv? field #f)
	(raise (list 'nonexistent-field (cons 's-field-name field-name)))
	field)))


(define (alloc-tvar-number-range linker nr-of-tvars)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (integer? nr-of-tvars))
  (let ((start (hfield-ref linker 'next-tvar-number)))
    (hfield-set! linker 'next-tvar-number
		 (+ start nr-of-tvars))
    start))


;; We use param-cache-parsing here because the target compilation
;; uses data structures created in implementation pseudocode reading.
;; Data structures created during instantiation are discarded.
(define (get-binder-for-tc linker)
  (hfield-ref linker 'binder-parsing))


(define (linker-entity-is-none? linker ent)
  (entity-is-none1? (get-binder-for-tc linker) ent))


(define (is-simple-arg-list2? arg-descs)
  ;; The following expression is #t for an empty list.
  (not (or-map? (lambda (ent)
		  (or (is-t-rest? ent) (is-t-splice? ent)))
		arg-descs)))


(define (is-simple-arg-list-with-tail? arg-descs)
  (and (not-null? arg-descs)
       (let ((ent-last (last arg-descs)))
	 (or (is-t-rest? ent-last) (is-t-splice? ent-last)))
       ;; The following expression is #t for a single element list.
       (not (or-map? (lambda (ent)
		       (or (is-t-rest? ent) (is-t-splice? ent)))
		     (drop-right arg-descs 1)))))


(define (get-gensyms linker lst-args)
  (map (lambda (arg)
	 (get-target-var-name linker (hfield-ref arg 'address)))
       lst-args))