[e90b2f]: src / new-cmp / cmptranslate.lsp Maximize Restore History

Download this file

cmptranslate.lsp    444 lines (365 with data), 14.9 kB

  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
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; Package: C -*-
;;;;
;;;; Copyright (c) 2009, Juan Jose Garcia-Ripoll.
;;;;
;;;; This program 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 of the License, or (at your option) any later version.
;;;;
;;;; See file '../Copyright' for full details.
(in-package "COMPILER")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; MAIN DRIVERS
;;;
(defun c1translate (destination value)
(enforce-destination destination (c1expr destination value)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; SPECIALIZED OPERATIONS
;;;
;;;
;;; HANDLING OF TEMPORARIES
;;;
(defvar *c1-temps* '())
(defun make-c1-temp ()
(let ((v (make-var :kind :object :type t :ref 1
:name (gensym "X")
:loc `(LCL ,(- (length *c1-temps*))))))
(push v *c1-temps*)
v))
;;;
;;; HIGHER ORDER TRANSLATION CONSTRUCTS
;;;
(defun enforce-destination (destination expr)
(loop with last-form = nil
for i in expr
when (c1form-p last-form)
do (setf last-form i)
finally (let ((actual-dest destination))
(unless (or (eq actual-dest 'TRASH)
(eq actual-dest destination))
(error "Wrong destination in forms list ~S" expr))))
expr)
(labels ((recursively-process (function var-or-loc forms)
(loop (cond ((var-p var-or-loc)
(return (funcall function var-or-loc forms)))
((atom var-or-loc)
(return))
((consp var-or-loc)
(recursively-process function (pop var-or-loc) forms))))))
(defun maybe-add-to-read-nodes (var-or-loc forms)
(recursively-process 'add-to-read-nodes var-or-loc forms)
forms)
(defun maybe-add-to-set-nodes (var-or-loc forms)
(recursively-process 'add-to-set-nodes var-or-loc forms)
forms))
(defun maybe-add-to-read/set-nodes (var-or-loc forms)
(maybe-add-to-set-nodes var-or-loc forms)
(maybe-add-to-read-nodes var-or-loc forms))
(defun c1save-one-value (value)
(if (constantp value)
(values nil (build-constant-value-loc (cmp-eval value) :always t))
(let ((v (make-c1-temp)))
(values (c1translate v value) v))))
(defmacro c1with-temps ((prefix postfix &rest temps) &rest body)
`(let* ((*c1-temps* *c1-temps*)
(*cmp-env* (cmp-env-copy))
,@(loop for name in temps
collect `(,name (make-c1-temp)))
(,prefix (c1bind (list ,@temps)))
(,postfix (c1unbind (list ,@temps))))
,@(loop for name in temps
collect `(cmp-env-register-var ,name *cmp-env*))
,@body))
(defmacro c1with-saved-one-value ((prefix postfix location expression) &rest body)
`(let* ((*c1-temps* *c1-temps*)
(,location (make-c1-temp))
(,prefix (nconc
(c1bind (list ,location))
(c1translate ,location ,expression)))
(,postfix (c1unbind (list ,location))))
,@body))
(defmacro c1with-saved-value ((prefix postfix temp value) &rest body)
(let ((forms (gensym))
(vars (gensym)))
`(let* ((*c1-temps* *c1-temps*))
(multiple-value-bind (,forms ,temp)
(c1save-one-value ,value)
(let* ((,vars (and ,forms (list ,temp)))
(,prefix (nconc (c1bind ,vars) ,forms))
(,postfix (c1unbind ,vars)))
,@body)))))
(defmacro c1with-saved-values ((prefix postfix temps values) &rest body)
(let ((forms (gensym))
(vars (gensym)))
`(let* ((*c1-temps* *c1-temps*)
(,temps '())
(,vars '())
(,prefix (loop for v in ,values
nconc (multiple-value-bind (forms temp)
(c1save-one-value v)
(when forms
(push temp ,vars))
(push temp ,temps)
forms)))
(,postfix (c1unbind (setf ,vars (nreverse ,vars)))))
(setf prefix (nconc (c1bind ,vars) prefix)
,temps (nreverse ,temps))
,@body)))
(defun maybe-create-temp (destination)
(case destination
((VALUES VALUE0 VALUES+VALUE0) destination)
(RETURN 'VALUES+VALUE0)
(ACTUAL-RETURN (baboon))
(t (make-c1-temp))))
(defmacro c1with-saved-output ((prefix postfix new-destination old-destination)
&rest body)
`(let* ((*c1-temps* *c1-temps*)
(temp-var (maybe-create-temp ,old-destination))
(,new-destination (or temp-var ,old-destination))
(,prefix (and (var-p temp-var) (c1bind (list temp-var))))
(,postfix (and (var-p temp-var)
(nconc (c1set-loc ,old-destination temp-var)
(c1unbind (list temp-var))))))
,@body))
;;;
;;; CLEANUP STATEMENTS
;;;
(defun c1cleanup-forms (env)
(multiple-value-bind (specials other-statements)
(cmp-env-cleanups env)
(nconc (c1unbind specials nil) other-statements)))
;;;
;;; VARIABLE BINDINGS
;;;
(defun c1bind (vars)
(and vars
(make-c1form* 'BIND :args vars)))
(defun c1unbind (vars &optional (close-block t))
(when vars
(make-c1form* 'UNBIND :args vars close-block)))
(defun c1progv-op (ndx-loc vars-loc values-loc)
(let ((form (make-c1form* 'PROGV :args ndx-loc vars-loc values-loc)))
(setf (var-kind ndx-loc) :CL-INDEX
(var-type ndx-loc) 'SI::INDEX)
(maybe-add-to-set-nodes ndx-loc form)
(maybe-add-to-read-nodes vars-loc form)
(maybe-add-to-read-nodes values-loc form)))
(defun c1progv-exit-op (ndx-loc)
(maybe-add-to-read-nodes ndx-loc (make-c1form* 'PROGV-EXIT :args ndx-loc)))
;;;
;;; ASSIGNMENTS
;;;
(defun update-destination-type (destination type)
(cond ((not (var-p destination)))
((eq (setf type (values-type-primary-type type)) t))
((var-read-only-p destination)
(setf (var-type destination) type))
(t
(let* ((type2 (var-type destination))
(type1 (type-and type type2)))
(unless type1
(let* ((*print-length* 4)
(*print-level* 3))
(cmpwarn "Variable ~A was declared to have type ~A~%and is assigned a value of type ~A"
(var-name destination) type2 type)))))))
(defun c1set-loc (dest value-loc)
(unless (eq dest value-loc)
(let* ((type (location-type value-loc))
(form (make-c1form* 'SET :args dest value-loc)))
(update-destination-type dest type)
(maybe-add-to-set-nodes dest form)
(maybe-add-to-read-nodes value-loc form))))
(defun c1bind-special-op (dest value-loc)
(let* ((type (location-type value-loc))
(form (make-c1form* 'BIND-SPECIAL :args dest value-loc)))
(update-destination-type dest type)
(maybe-add-to-set-nodes dest form)
(maybe-add-to-read-nodes value-loc form)))
(defun c1maybe-bind-special (dest-var forms)
(if (global-var-p dest-var)
(c1bind-special dest-var forms)
(c1translate dest-var forms)))
(defun c1maybe-bind-special-op (dest-var loc)
(if (global-var-p dest-var)
(c1bind-special-op dest-var loc)
(c1set-loc dest-var loc)))
(defun c1bind-special (dest-var forms)
(c1with-saved-value (prefix postfix temp forms)
(nconc prefix
(c1bind-special-op dest-var temp)
postfix)))
(defun c1set-from-values (new-destination)
(maybe-add-to-set-nodes new-destination
(c1set-loc new-destination 'VALUES)))
(defun c1set-mv (locations &optional (min-args 0) (max-args multiple-values-limit))
(maybe-add-to-set-nodes locations
(make-c1form* 'SET-MV :args locations min-args max-args)))
(defun c1values-op (locations)
(let ((form (make-c1form* 'VALUES
:args locations)))
(maybe-add-to-read-nodes locations form)))
;;;
;;; FUNCTION ARGUMENTS AND BLOCKS
;;;
(defun c1function-prologue (fun)
(make-c1form* 'FUNCTION-PROLOGUE :args fun))
(defun c1function-epilogue (fun)
(make-c1form* 'FUNCTION-EPILOGUE :args fun))
(defun c1bind-requireds (var-loc-pairs)
(make-c1form* 'BIND-REQUIREDS :args var-loc-pairs))
(defun c1varargs-bind-op (nargs-loc varargs-loc minargs maxargs nkeywords check)
(make-c1form* 'VARARGS-BIND
:args nargs-loc varargs-loc minargs maxargs nkeywords check))
(defun c1varargs-pop-op (dest nargs-loc varargs-loc)
(if (global-var-p dest)
(nconc (c1varargs-pop-op 'VALUE0 nargs-loc varargs-loc)
(c1bind-special-op dest 'VALUE0))
(let ((form (make-c1form* 'VARARGS-POP :args dest nargs-loc varargs-loc)))
(maybe-add-to-read/set-nodes nargs-loc form)
(maybe-add-to-read/set-nodes varargs-loc form)
(maybe-add-to-set-nodes dest form))))
(defun c1varargs-rest-op (dest-loc nargs-loc varargs-loc nkeys
keywords-list allow-other-keys)
(let ((form (make-c1form* 'VARARGS-REST :args dest-loc
nargs-loc varargs-loc
nkeys keywords-list allow-other-keys)))
(update-destination-type dest-loc 'list)
(maybe-add-to-read/set-nodes nargs-loc form)
(maybe-add-to-read/set-nodes varargs-loc form)
(maybe-add-to-set-nodes dest-loc form)))
(defun c1varargs-unbind-op (nargs-loc varargs-loc minargs maxargs nkeywords)
(make-c1form* 'VARARGS-UNBIND
:args nargs-loc varargs-loc minargs maxargs nkeywords))
;;;
;;; LOCATIONS, VARIABLES, ASSIGNMENTS
;;;
(defun c1values-ref (destination args)
(maybe-add-to-set-nodes
destination
(make-c1form* 'SET :args destination `(VALUE ,(first args)))))
;;;
;;; JUMP FRAMES
;;;
(defun c1frame-set (id-loc no-label)
(incf (tag-ref no-label))
(maybe-add-to-read-nodes id-loc
(make-c1form* 'FRAME-SET :args id-loc no-label)))
(defun c1frame-pop (&optional var)
(make-c1form* 'FRAME-POP :args var))
(defun c1frame-save-next (var)
(make-c1form* 'FRAME-SAVE-NEXT :args var))
(defun c1frame-jmp-next (var)
(make-c1form* 'FRAME-JMP-NEXT :args var))
(defun c1frame-id (var)
(maybe-add-to-set-nodes var (make-c1form* 'FRAME-ID :args var)))
;;;
;;; STACK FRAMES
;;;
(defun c1stack-frame-open (var)
(maybe-add-to-set-nodes var (make-c1form* 'STACK-FRAME-OPEN :args var)))
(defun c1stack-frame-push (frame-var value-loc)
(maybe-add-to-read/set-nodes
frame-var
(make-c1form* 'STACK-FRAME-PUSH :args frame-var value-loc)))
(defun c1stack-frame-push-values (frame-var)
(maybe-add-to-read/set-nodes
frame-var
(make-c1form* 'STACK-FRAME-PUSH-VALUES :args frame-var)))
(defun c1stack-frame-pop-values (frame-var &optional (dest 'trash))
(maybe-add-to-read/set-nodes
frame-var
(make-c1form* 'STACK-FRAME-POP-VALUES :args frame-var dest)))
(defun c1stack-frame-apply (frame-var function-loc)
(maybe-add-to-read-nodes
frame-var
(make-c1form* 'STACK-FRAME-APPLY :args frame-var function-loc)))
(defun c1stack-frame-close (frame-var)
(maybe-add-to-read-nodes
frame-var
(make-c1form* 'STACK-FRAME-CLOSE :args frame-var)))
;;;
;;; LOCAL AND NONLOCAL CONTROL TRANSFER
;;;
(defun add-jmp-cleanups (tag forms)
(let ((env (tag-env tag)))
(if env
(nconc (c1cleanup-forms env) forms)
forms)))
(defun c1jmp (tag)
(incf (tag-ref tag)) ;; Only local jumps, so no -ccb or -clb
(add-jmp-cleanups tag (make-c1form* 'JMP :args tag)))
(defun c1jmp-true (tag loc)
(incf (tag-ref tag))
(add-jmp-cleanups tag (c1set-loc `(JMP-TRUE ,tag) loc)))
(defun c1jmp-false (tag loc)
(incf (tag-ref tag))
(add-jmp-cleanups tag (c1set-loc `(JMP-FALSE ,tag) loc)))
(defun c1jmp-zero (tag loc)
(incf (tag-ref tag))
(add-jmp-cleanups tag (c1set-loc `(JMP-ZERO ,tag) loc)))
(defun c1jmp-nonzero (tag loc)
(incf (tag-ref tag))
(add-jmp-cleanups tag (c1set-loc `(JMP-NONZERO ,tag) loc)))
(defun c1return-from-op (var name)
(maybe-add-to-read-nodes var (make-c1form* 'RETURN-FROM :args var name)))
(defun c1throw-op (tag-loc)
(maybe-add-to-read-nodes tag-loc (make-c1form* 'THROW :args tag-loc)))
(defun c1go-op (tag)
(maybe-add-to-read-nodes tag (make-c1form* 'GO :args tag)))
;;;
;;; FUNCTION CALLS, CLOSURES AND THE LIKE
;;;
(defun c1do-flet/labels-op (function-list)
(make-c1form* 'DO-FLET/LABELS :args function-list))
(defun c1funcall-op (destination arguments)
(let ((form (make-c1form* 'FUNCALL :args destination arguments)))
(maybe-add-to-read-nodes arguments form)
(maybe-add-to-set-nodes destination form)))
(defun c1call-local-op (destination fun args-loc)
(let* ((return-type (or (get-local-return-type fun) 'T))
(arg-types (get-local-arg-types fun))
(form (make-c1form* 'CALL-LOCAL
:args destination fun args-loc)))
;; Add type information to the arguments.
(maybe-add-to-read-nodes args-loc form)
(loop for arg in args-loc
for type in arg-types
do (and-form-type (car arg-types) form (car args)
:safe "In a call to ~a" fname))
(update-destination-type destination return-type)
(maybe-add-to-set-nodes destination form)))
(defun c1call-global-op (destination fname arguments)
(let* ((return-type (propagate-types fname arguments))
(form (make-c1form* 'CALL-GLOBAL
:sp-change (function-may-change-sp fname)
:args destination fname arguments
(values-type-primary-type return-type))))
(maybe-add-to-read-nodes arguments form)
(update-destination-type destination return-type)
(maybe-add-to-set-nodes destination form)))
(defun c1c-inline-op (output-type destination temps arg-types
output-rep-type c-expression side-effects
one-liner)
(let ((form (make-c1form* 'C-INLINE
:args destination temps arg-types
output-rep-type c-expression side-effects
one-liner)))
(maybe-add-to-read-nodes temps form)
(update-destination-type destination output-type)
(maybe-add-to-set-nodes destination form)))
;;;
;;; DEBUG INFORMATION
;;;
(defun c1debug-env-open (fname)
(make-c1form* 'DEBUG-ENV-OPEN :args fname))
(defun c1debug-env-push-vars (variables)
(when variables
(make-c1form* 'DEBUG-ENV-PUSH-VARS :args variables)))
(defun c1debug-env-pop-vars (variables &optional close-block)
(when variables
(make-c1form* 'DEBUG-ENV-POP-VARS :args variables close-block)))
(defun c1debug-env-close (fname)
(make-c1form* 'DEBUG-ENV-CLOSE :args fname))