-
Notifications
You must be signed in to change notification settings - Fork 3
/
genfunctions.lisp
548 lines (482 loc) · 21.4 KB
/
genfunctions.lisp
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
;; -*- Mode:Lisp; Syntax:Common-Lisp; Package: (*SIM-I COMMON-LISP-GLOBAL); Muser: yes -*-
(in-package :*sim-i)
;;;> *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+
;;;>
;;;> The Thinking Machines *Lisp Simulator is in the public domain.
;;;> You are free to do whatever you like with it, including but
;;;> not limited to distributing, modifying, and copying.
;;;> Bugs, comments and revisions due to porting can be sent to:
;;;> bug-starlisp@think.com. Other than to Thinking Machines'
;;;> customers, no promise of support is intended or implied.
;;;>
;;;> *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+
;;; Author: JP Massar.
;;; This file contains macros that expand into function definitions. These
;;; functions are simple, parallel extensions of Common Lisp functions.
(defun set-vector-conditionally (vector-to-set value)
(let ((any-set nil))
(do-for-selected-processors-internal (j) (setq any-set t) (setf (aref vector-to-set j) value))
any-set
))
(defun trivial-name (function-name)
(intern
(concatenate 'string "TRIVIAL-" (symbol-name function-name))
(find-package '*sim-i)
))
(defmacro note-trivial-macro (&rest symbols)
`(eval-when (:compile-toplevel :load-toplevel :execute) (mapcar 'note-trivial '(,@symbols)))
)
(defun note-trivial (symbol) (setf (get symbol 'trivialp) t))
(defun symbol-trivial-p (symbol) (get symbol 'trivialp))
(defmacro def-trivial-one-arg-*lisp-functions (names-and-lisp-functions)
`(progn
(defun execute-trivial-one-arg-*lisp-function (name internal-name arg)
(safety-check (new-pvar-check arg name))
(let* ((return-pvar (allocate-temp-general-pvar))
(return-array (pvar-array return-pvar))
(arg-array (pvar-array arg))
)
(when (funcall internal-name arg-array return-array)
(make-non-void return-pvar)
)
return-pvar
))
,@(mapcar
#'(lambda (name-and-lisp-function)
(let* ((name (car name-and-lisp-function))
(lisp-function (cadr name-and-lisp-function))
(internal-name (trivial-name name))
)
`(defun ,internal-name (pvar-array return-array)
(let ((pvar-array pvar-array) (return-array return-array))
(with-simple-vectors (pvar-array return-array)
(let ((any-set nil))
(do-for-selected-processors-internal (j)
(setq any-set t)
(setf (aref return-array j) (,lisp-function (aref pvar-array j)))
)
any-set
))))))
names-and-lisp-functions
)
,@(mapcar
#'(lambda (name-and-lisp-function)
(let* ((name (car name-and-lisp-function))
;;(lisp-function (cadr name-and-lisp-function))
(internal-name (trivial-name name))
)
`(defun ,name (pvar)
;;(declare-pvar-arglist ,lisp-function)
,(if (or (eql name 'byte-position!!)
(eql name 'byte-size!!))
'(pvar-argument!! pvar *bytespec)
'(simple-pvar-argument!! pvar))
(execute-trivial-one-arg-*lisp-function ',name ',internal-name pvar))
))
names-and-lisp-functions
)
(note-trivial-macro ,@(mapcar #'car names-and-lisp-functions))
))
(defmacro def-trivial-two-arg-*lisp-functions (names-and-lisp-functions)
`(progn
(defun execute-trivial-two-arg-*lisp-function (name internal-name arg1 arg2)
(safety-check (new-two-pvar-check arg1 arg2 name))
(let* ((return-pvar (allocate-temp-general-pvar))
(return-array (pvar-array return-pvar))
(arg1-array (pvar-array arg1))
(arg2-array (pvar-array arg2))
)
(when (funcall internal-name arg1-array arg2-array return-array)
(make-non-void return-pvar)
)
return-pvar
))
,@(mapcar
#'(lambda (name-and-lisp-function)
(let* ((name (car name-and-lisp-function))
(lisp-function (cadr name-and-lisp-function))
(internal-name (trivial-name name))
)
`(defun ,internal-name (pvar1-array pvar2-array return-array)
(let ((pvar1-array pvar1-array)
(pvar2-array pvar2-array)
(return-array return-array)
)
(with-simple-vectors (pvar1-array pvar2-array return-array)
(let ((any-set nil))
(do-for-selected-processors-internal (j)
(setq any-set t)
(setf (aref return-array j)
(,lisp-function (aref pvar1-array j) (aref pvar2-array j)))
)
any-set
))))))
names-and-lisp-functions
)
,@(mapcar
#'(lambda (name-and-lisp-function)
(let* ((name (car name-and-lisp-function))
;;(lisp-function (cadr name-and-lisp-function))
(internal-name (trivial-name name))
)
`(defun ,name (pvar1 pvar2)
;; (declare-pvar-arglist ,lisp-function)
,(case name
((ldb!! ldb-test!! mask-field!!)
'(pvar-argument!! pvar1 *bytespec pvar2 legal-pvar-value))
(otherwise '(simple-pvar-argument!! (pvar1 pvar2))))
(execute-trivial-two-arg-*lisp-function ',name ',internal-name pvar1 pvar2))
))
names-and-lisp-functions
)
(note-trivial-macro ,@(mapcar #'car names-and-lisp-functions))
))
;; Functions like truncate which take one argument and optionally
;; a second.
(defmacro def-trivial-optional-two-arg-*lisp-functions (names-and-lisp-functions)
`(progn
(defun execute-trivial-optional-two-arg-*lisp-function
(name internal-name arg1 &optional arg2)
(safety-check (new-pvar-check arg1 name))
(safety-check (and arg2 (new-pvar-check arg2 name)))
(let* ((return-pvar (allocate-temp-general-pvar))
(return-array (pvar-array return-pvar))
(arg1-array (pvar-array arg1))
(arg2-array (and arg2 (pvar-array arg2)))
)
(when (funcall internal-name arg1-array arg2-array return-array)
(make-non-void return-pvar)
)
return-pvar
))
,@(mapcar
#'(lambda (name-and-lisp-function)
(let* ((name (car name-and-lisp-function))
(lisp-function (cadr name-and-lisp-function))
(internal-name (trivial-name name))
)
`(defun ,internal-name (pvar1-array pvar2-array return-array)
(let ((pvar1-array pvar1-array)
(pvar2-array pvar2-array)
(return-array return-array)
)
(with-simple-vectors (pvar1-array return-array)
(let ((any-set nil))
(do-for-selected-processors-internal (j)
(setq any-set t)
(setf (aref return-array j)
(if pvar2-array
(,lisp-function (aref pvar1-array j) (aref pvar2-array j))
(,lisp-function (aref pvar1-array j))
)))
any-set
))))))
names-and-lisp-functions
)
,@(mapcar
#'(lambda (name-and-lisp-function)
(let* ((name (car name-and-lisp-function))
;;(lisp-function (cadr name-and-lisp-function))
(internal-name (trivial-name name))
)
`(defun ,name (pvar1 &optional pvar2)
;;(declare-pvar-arglist ,lisp-function)
(simple-pvar-argument!! pvar1 &opt pvar2)
(execute-trivial-optional-two-arg-*lisp-function
',name ',internal-name pvar1 pvar2))
))
names-and-lisp-functions
)
(note-trivial-macro ,@(mapcar #'car names-and-lisp-functions))
))
(defmacro def-trivial-three-arg-*lisp-functions (names-and-lisp-functions)
`(progn
(defun execute-trivial-three-arg-*lisp-function (name internal-name arg1 arg2 arg3)
(safety-check (new-two-pvar-check arg1 arg2 name))
(let* ((return-pvar (allocate-temp-general-pvar))
(return-array (pvar-array return-pvar))
(arg1-array (pvar-array arg1))
(arg2-array (pvar-array arg2))
(arg3-array (pvar-array arg3))
)
(when (funcall internal-name arg1-array arg2-array arg3-array return-array)
(make-non-void return-pvar)
)
return-pvar
))
,@(mapcar
#'(lambda (name-and-lisp-function)
(let* ((name (car name-and-lisp-function))
(lisp-function (cadr name-and-lisp-function))
(internal-name (trivial-name name))
)
`(defun ,internal-name (pvar1-array pvar2-array pvar3-array return-array)
(let ((pvar1-array pvar1-array)
(pvar2-array pvar2-array)
(pvar3-array pvar3-array)
(return-array return-array)
)
(with-simple-vectors (pvar1-array pvar2-array pvar3-array return-array)
(let ((any-set nil))
(do-for-selected-processors-internal (j)
(setq any-set t)
(setf (aref return-array j)
(,lisp-function (aref pvar1-array j) (aref pvar2-array j) (aref pvar3-array j))
))
any-set
))))))
names-and-lisp-functions
)
,@(mapcar
#'(lambda (name-and-lisp-function)
(let* ((name (car name-and-lisp-function))
;;(lisp-function (cadr name-and-lisp-function))
(internal-name (trivial-name name))
)
`(defun ,name (pvar1 pvar2 pvar3)
;;(declare-pvar-arglist ,lisp-function)
,(case name
((deposit-field!! dpb!!)
'(pvar-argument!! pvar2 *bytespec (pvar1 pvar3) integer))
(otherwise '(simple-pvar-argument!! (pvar1 pvar2 pvar3))))
(execute-trivial-three-arg-*lisp-function ',name ',internal-name pvar1 pvar2 pvar3))
))
names-and-lisp-functions
)
(note-trivial-macro ,@(mapcar #'car names-and-lisp-functions))
))
;; functions which take an arbitrary number of arguments.
;; Whether they allow none is a flag to the macro.
;; If they allow none, what the function returns given that
;; no arguments are provided is a parameter of the macro.
(defmacro def-trivial-nary-*lisp-functions
(name-lispname-zeroargsp-zeroargsdefault-list)
`(progn
(defun execute-trivial-nary-*lisp-function
(name internal-name lisp-function allow-zero-args zero-args-default &rest args)
(safety-check (new-multiple-pvar-check args name))
(let* ((nargs (length args))
(return-pvar (allocate-temp-general-pvar))
(return-array (pvar-array return-pvar))
(arg-arrays (mapcar #'pvar-array args))
(any-set nil)
)
(cond
((eql 0 nargs)
(if allow-zero-args
(setq any-set (set-vector-conditionally return-array zero-args-default))
(error "Zero arguments not allowed to ~S~%" name)
))
;; this is inefficient since it uses funcall, but
;; presumably calling an n-ary function with one argument
;; is a rare event
((eql 1 nargs)
(let ((x-array (car arg-arrays)))
(with-simple-vectors (x-array)
(do-for-selected-processors-internal (j)
(setq any-set t)
(let ((element (aref x-array j)))
(setf (aref return-array j) (funcall lisp-function element))
)))))
;; for multiple arguments set up the return array with
;; values from the first argument, then call the internal
;; function to combine all the rest of the arguments with
;; the return array values.
(t
(let* ((first-array (car arg-arrays)))
(with-simple-vectors (first-array)
(do-for-selected-processors-internal (j)
(setq any-set t)
(setf (aref return-array j) (aref first-array j))
)
(apply internal-name return-array (cdr arg-arrays))
))))
(when any-set (make-non-void return-pvar))
return-pvar
))
,@(mapcar
#'(lambda (name lisp-function)
(let ((internal-name (trivial-name name)))
`(defun ,internal-name (return-array &rest pvar-arrays)
(let ((return-array return-array))
(when pvar-arrays
(let ((next-array (car pvar-arrays)))
(with-simple-vectors (return-array next-array)
(do-for-selected-processors (j)
(setf (aref return-array j)
(,lisp-function (aref return-array j) (aref next-array j)))
)))
(apply ',internal-name return-array (cdr pvar-arrays))
)))))
(mapcar #'first name-lispname-zeroargsp-zeroargsdefault-list)
(mapcar #'second name-lispname-zeroargsp-zeroargsdefault-list)
)
,@(mapcar
#'(lambda (name lisp-function allow-zero-args zero-args-default)
(let ((internal-name (trivial-name name)))
`(defun ,name (&rest pvars)
;;(declare-pvar-arglist ,lisp-function)
(simple-pvar-argument!! &rest pvars)
(apply #'execute-trivial-nary-*lisp-function
',name ',internal-name ',lisp-function
,allow-zero-args ,zero-args-default pvars
))))
(mapcar #'first name-lispname-zeroargsp-zeroargsdefault-list)
(mapcar #'second name-lispname-zeroargsp-zeroargsdefault-list)
(mapcar #'third name-lispname-zeroargsp-zeroargsdefault-list)
(mapcar #'fourth name-lispname-zeroargsp-zeroargsdefault-list)
)
(note-trivial-macro ,@(mapcar #'car name-lispname-zeroargsp-zeroargsdefault-list))
))
;; Functions that take pvars and combine all the active elements of the
;; pvar in some way to produce a single result. The initial-value is
;; usually the identity element for the operation. rval-if-none-active
;; is what is returned if no processors are active.
(defmacro def-trivial-*lisp-reduction-function-using-initial-value
(name lisp-function initial-value rval-if-none-active)
`(*defun
,name (pvar)
;;(declare-pvar-arglist ,lisp-function)
(simple-pvar-argument!! pvar)
(safety-check (new-pvar-check pvar ',name))
(let ((any-active nil)
(return-value ,initial-value)
(x-array (pvar-array pvar))
)
(with-simple-vectors (x-array)
(do-for-selected-processors-internal (j)
(setq any-active t)
(setq return-value (,lisp-function return-value (aref x-array j)))
)
(if any-active return-value ,rval-if-none-active)
))))
;; Similar to def-trivial-*lisp-reduction-function-using-initial-value
;; except that no initial-value is provided. Instead, the first
;; active element is used.
(defmacro def-trivial-*lisp-reduction-function-using-first-element
(name lisp-function rval-if-none-active)
`(*defun
,name (pvar)
;;(declare-pvar-arglist ,lisp-function)
(simple-pvar-argument!! pvar)
(safety-check (new-pvar-check pvar ',name))
(let ((first t)
(return-value nil)
(x-array (pvar-array pvar))
)
(with-simple-vectors (x-array)
(do-for-selected-processors (j)
(if first
(progn (setq first nil) (setq return-value (aref x-array j)))
(setq return-value (,lisp-function return-value (aref x-array j)))
))
(if first ,rval-if-none-active return-value)
))))
;; Comparision functions like < and >.
;; Zero arguments are illegal, one argument always produces t!!.
(defmacro def-trivial-*lisp-comparision-functions (name-lisp-function-list)
`(progn
(defun execute-trivial-*lisp-comparision-function
(name internal-name arglist)
(safety-check (new-multiple-pvar-check arglist name))
(let* ((nargs (length arglist))
(return-pvar (allocate-temp-general-pvar))
(return-array (pvar-array return-pvar))
(argument-arrays (mapcar #'pvar-array arglist))
(any-set nil)
)
(cond
((eql 0 nargs)
(error "~S: This function does not allow 0 arguments." name))
((eql 1 nargs)
(setq any-set (set-vector-conditionally return-array t)))
(t
(setq any-set (set-vector-conditionally return-array t))
(funcall internal-name return-array argument-arrays nargs))
)
(when any-set (make-non-void return-pvar))
return-pvar
))
,@(mapcar
#'(lambda (name lisp-function)
(let ((internal-name (trivial-name name)))
`(defun ,internal-name (return-array argument-arrays nargs)
(if (< nargs 2)
return-array
(let ((return-array return-array)
(arg1-array (car argument-arrays))
(arg2-array (cadr argument-arrays))
)
(with-simple-vectors (return-array arg1-array arg2-array)
(do-for-selected-processors (j)
(and (aref return-array j)
(setf (aref return-array j)
(,lisp-function (aref arg1-array j) (aref arg2-array j))
)))
(funcall ',internal-name return-array (cdr argument-arrays) (1- nargs))
))))))
(mapcar #'car name-lisp-function-list)
(mapcar #'cadr name-lisp-function-list)
)
,@(mapcar
#'(lambda (name lisp-function)
(declare (ignore lisp-function))
(let ((internal-name (trivial-name name)))
`(defun ,name (&rest pvars)
;;(declare-pvar-arglist ,lisp-function)
(simple-pvar-argument!! &rest pvars)
(execute-trivial-*lisp-comparision-function
',name ',internal-name pvars
))))
(mapcar #'car name-lisp-function-list)
(mapcar #'cadr name-lisp-function-list)
)
(note-trivial-macro ,@(mapcar #'car name-lisp-function-list))
))
;;; Given a list of pvar function specifications (see trivfunctions.lisp)
;;; This macro macroexpands into a list of 'def-trivial...' macro
;;; definitions. These definitions then macroexpand into the proper
;;; defun or *defun for each function. Ah, the wonders of Lisp!
(defmacro define-trivial-functions (def-trivial-macro-name flist)
(let ((macrolist nil))
(dolist (j (eval flist))
(push (cons def-trivial-macro-name j) macrolist)
)
`(progn ,@macrolist)
))
(defun arglist-symbol-to-pvar-symbol (symbol)
(cond
((member symbol lambda-list-keywords :test #'eq) symbol)
((listp symbol) (arglist-symbol-to-pvar-symbol (car symbol)))
(t
(let ((string (symbol-name symbol)))
(let ((length (length string)))
(if (char-equal (char string (1- length)) #\S)
(intern (concatenate 'string (subseq string 0 (1- length)) "-PVARS") '*sim)
(intern (concatenate 'string string "-PVAR") '*sim)
))))))
;;; (defun common-lisp-arglist-to-starlisp-arglist (common-lisp-function)
;;; #-(OR SYMBOLICS LCL3.0)
;;; (declare (ignore common-lisp-function))
;;; (mapcar
;;; 'arglist-symbol-to-pvar-symbol
;;; #+SYMBOLICS
;;; (scl:arglist common-lisp-function)
;;; #+LCL3.0
;;; (sys::arglist common-lisp-function)
;;; #-(OR SYMBOLICS LCL3.0)
;;; nil
;;; ))
;;;
;;; (defmacro declare-pvar-arglist (common-lisp-function)
;;; (let ((arglist (common-lisp-arglist-to-starlisp-arglist common-lisp-function)))
;;; #+SYMBOLICS
;;; `(declare (scl:arglist ,@arglist))
;;; #+LCL3.0
;;; `(declare (sys::arglist ,arglist))
;;; #-(OR SYMBOLICS LCL3.0)
;;; (declare (ignore arglist))
;;; #-(OR SYMBOLICS LCL3.0)
;;; nil
;;; ))
;;;