-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwamcompiler.lisp
3016 lines (2796 loc) · 96.1 KB
/
wamcompiler.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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(declaim (ftype (function (t) t) set-to-trail))
(declaim (ftype (function (t) t) send-query))
(declaim (ftype (function (t) t) prolog-expr->string))
(declaim (ftype (function (t &optional t) t) compile-clause))
(declaim (ftype (function (t) t) divide-head-body))
(declaim (ftype (function (t t) t) optimize-wamcode))
(declaim (ftype (function (t) t) skip-comment))
(declaim (ftype (function (t) t) skip-whitespace))
(declaim (ftype (function (t) t) read-non-alphanum-atom))
(declaim (ftype (function (t) t) read-num))
(declaim (ftype (function (t) t) read-quoted-atom))
(declaim (ftype (function (t) t) read-alphanum-atom))
(declaim (optimize (speed 3) (space 0) (debug 0) (safety 0)))
(defvar *unbound-variable* (gensym))
(defvar *structure* (gensym))
(defvar *operator-list* nil)
(defvar *predicate-type-table* (make-hash-table :test #'equal)) ;;user-defined or builtin
(defvar *clause-code-table* (make-hash-table :test #'equal)) ;; key (functor . arity).
(defvar *dispatching-code-table* (make-hash-table :test #'equal))
(defvar *builtin-predicate-table* (make-hash-table :test #'equal))
(define-condition prolog-escape-repl (simple-condition) ())
(define-condition prolog-query-failed (simple-condition) ())
(define-condition prolog-found-solution (simple-condition)
((vars :initarg :vars
:accessor vars)
(can-backtrack? :initarg :can-backtrack?
:accessor can-backtrack?)))
(define-condition prolog-syntax-error (simple-error)
((cause :initarg :cause
:initform nil
:accessor cause))
(:report (lambda (condition stream)
(format stream "Syntax error!")
(when (cause condition)
(format stream " (~A)" (cause condition))))))
(define-condition prolog-compile-error (simple-error)
((cause :initarg :cause
:initform nil
:accessor cause))
(:report (lambda (condition stream)
(format stream "Compile error!")
(when (cause condition)
(format stream " (~A)" (cause condition))))))
(define-condition prolog-bad-instruction-error (simple-error)
((inst :initarg :inst
:initform nil
:accessor bad-inst))
(:report (lambda (condition stream)
(format stream "Bad instruction: ~A" (bad-inst condition)))))
(define-condition prolog-builtin-predicate-error (simple-error)
((predicate :initarg :predicate
:initform nil
:accessor predicate)
(cause :initarg :cause
:initform nil
:accessor cause))
(:report (lambda (condition stream)
(format stream "Error in builtin-predicate(~A/~A): ~A"
(car (predicate condition))
(cdr (predicate condition))
(cause condition)))))
(defmacro op-atom (x) `(car ,x))
(defmacro op-prec (x) `(cadr ,x))
(defmacro op-assoc (x) `(caddr ,x))
(defmacro dostream (bind &body body)
`(loop (let ((,(car bind) (read-char ,(cadr bind) nil)))
(if (null ,(car bind)) (return nil)) ,@body)))
(defvar *non-alphanum-chars*
'(#\# #\$ #\& #\* #\+ #\- #\. #\/ #\: #\< #\= #\> #\? #\@ #\^ #\~ #\\))
(defun get-token (s)
(skip-whitespace s)
(let ((c (read-char s)))
(cond ((null c) nil)
((eq c #\%) (skip-comment s) (get-token s))
((eq c #\)) '(rparen))
((eq c #\() '(lparen))
((eq c #\]) '(rbracket))
((eq c #\[) '(lbracket))
((eq c #\|) '(vertical-bar))
((eq c #\,) (cons 'atom '|,|))
((eq c #\;) (cons 'atom '|;|))
((eq c #\!) (cons 'atom '|!|))
((eq c #\') (read-quoted-atom s))
((digit-char-p c) (unread-char c s) (cons 'atom (read-num s)))
((member c *non-alphanum-chars*)
(unread-char c s) (read-non-alphanum-atom s))
(t (unread-char c s) (read-alphanum-atom s)))))
(defun skip-whitespace (s)
(dostream (c s)
(unless (or (not (graphic-char-p c)) (eq c #\Space))
(unread-char c s) (return nil))))
(defun skip-comment (s)
(dostream (c s)
(if (eq c #\Newline) (return nil))))
(defun parse-string-to-float (line)
(with-input-from-string (s line)
(read s nil nil)))
(defun read-num (s)
(let* ( (acc)
(point t))
(dostream (c s)
(unless (or (digit-char-p c)
(and point (equal #\. c)))
(unread-char c s) (return nil))
(setq acc (cons c acc))
(when (equal c #\.) (setq point nil)))
(cond ( (equal (car acc) #\.) (unread-char (car acc) s)
(parse-integer (concatenate 'string (reverse (cdr acc))) ))
(point (parse-integer (concatenate 'string (reverse acc)) ))
(t (parse-string-to-float
(concatenate 'string (reverse acc))) ))))
(defun read-alphanum-atom (s)
(let ((acc))
(dostream (c s)
(unless (or (alphanumericp c) (eq #\_ c))
(unread-char c s) (return nil))
(setq acc (cons c acc)))
(let ((str (concatenate 'string (reverse acc))))
(cond ((or (upper-case-p (char str 0)) (equal (char str 0) #\_))
(cons 'variable (intern str)))
(t (cons 'atom (intern str)))))))
(defun read-non-alphanum-atom (s)
(let ((acc))
(dostream (c s)
(unless (member c *non-alphanum-chars*)
(unread-char c s) (return nil))
(setq acc (cons c acc)))
(let ((str (concatenate 'string (reverse acc))))
(cond ((equal str ".") '(dot))
(t (cons 'atom (intern str)))))))
(defun read-quoted-atom (s)
(let ((acc))
(dostream (c s)
(when (eq #\' c)
(return nil))
(setq acc (cons c acc)))
(let ((str (concatenate 'string (reverse acc))))
(cons 'atom (intern str)))))
(defun register-operator (op)
(labels ((op< (x y)
(if (eq (op-prec x) (op-prec y))
(string< (symbol-name (op-assoc x)) (symbol-name (op-assoc y)))
(> (op-prec x) (op-prec y))))
(insert-op-sorted (head x)
(cond ((null head) (list x))
((op< (car head) x)
(cons (car head) (insert-op-sorted (cdr head) x)))
(t (cons x head)))))
(setf (get (op-atom op) 'op) t)
(setq *operator-list* (insert-op-sorted *operator-list* op))))
(defun commap (x)
(and (eq (car x) 'atom) (eq (cdr x) '|,|)))
(defun init-operators (oplist)
(setq *operator-list* nil)
(dolist (op oplist)
(register-operator (cons (intern (car op)) (cdr op)))))
(init-operators
'(("?-" 1200 fx)
(":-" 1200 fx)
(":-" 1200 xfx)
("-->" 1200 xfx)
("public" 1150 fx)
("mode" 1150 fx)
("index" 1150 fx)
("extern" 1150 fx)
("dynamic" 1150 fx)
("bltin" 1150 fx)
("###" 1150 fx)
("module" 1150 fy)
("help" 1150 fy)
(";" 1100 xfy)
("->" 1050 xfy)
("," 1000 xfy)
("spy" 900 fy)
("nospy" 900 fy)
("\\+" 900 fy)
("is" 700 xfx)
("\\==" 700 xfx)
("\\=" 700 xfx)
("@>=" 700 xfx)
("@>" 700 xfx)
("@=<" 700 xfx)
("@<" 700 xfx)
(">=" 700 xfx)
(">" 700 xfx)
("=\\=" 700 xfx)
("==" 700 xfx)
("=<" 700 xfx)
("=:=" 700 xfx)
("=/=" 700 xfx)
("=.." 700 xfx)
("=" 700 xfx)
("<" 700 xfx)
(":=" 700 xfx)
("/==" 700 xfx)
("#\\=" 700 xfx)
("#>=" 700 xfx)
("#>" 700 xfx)
("#=<" 700 xfx)
("#=" 700 xfx)
("#<" 700 xfx)
("notin" 580 xfx)
("in" 580 xfx)
("::" 580 xfy)
(".." 560 yfx)
(":" 550 xfy)
("or" 500 yfx)
("and" 500 yfx)
("\\/" 500 yfx)
("/\\" 500 yfx)
("-" 500 yfx)
("+" 500 yfx)
("rem" 400 yfx)
("mod" 400 yfx)
(">>" 400 yfx)
("<<" 400 yfx)
("//" 400 yfx)
("/" 400 yfx)
("*" 400 yfx)
("\\" 200 fy)
("-" 200 fy)
("+" 200 fy)
("**" 200 xfx)
("^" 200 xfy)))
(defun parse (stream)
(let ((tokenstack)) ;;used by unget-tokens
(labels
((next-token ()
(if (null tokenstack) (get-token stream) (pop tokenstack)))
(unget-token (tok)
(push tok tokenstack))
(next-prec-head (head)
(let ((current-prec (op-prec (car head))))
(labels ((scan (h)
(if (and (not (null h)) (eq (op-prec (car h)) current-prec))
(scan (cdr h)) h)))
(scan head))))
(arrange (tree)
(case (car tree)
(struct (cons (cadr tree) (mapcar #'arrange (cddr tree))))
(list (make-listterm (mapcar #'arrange (cdr tree))))
(int (cdr tree))
(variable (cdr tree))
(otherwise
(princ tree)
(error (make-condition 'prolog-syntax-error)))))
(make-listterm (elements)
(if (= (length elements) 2)
`(|.| ,@elements)
`(|.| ,(car elements) ,(make-listterm (cdr elements)))))
(parse-arguments ()
(let ((next (next-token)))
(if (eq (car next) 'lparen)
(labels ((get-args (acc)
(let ((arg (parse-sub *operator-list* t))
(n (next-token)))
(cond ((eq (car n) 'rparen) (cons arg acc))
((commap n)
(get-args (cons arg acc)))
(t
(error (make-condition
'prolog-syntax-error
:cause "invalid argument list")))))))
(reverse (get-args nil)))
(progn (unget-token next) nil))))
(parse-list ()
(let ((next (next-token)))
(if (eq (car next) 'rbracket)
(list 'struct '|[]|)
(labels
((get-args (acc)
(let ((arg (parse-sub *operator-list* t))
(n (next-token)))
(cond ((eq (car n) 'rbracket)
(cons (list 'struct '|[]|) (cons arg acc)))
((eq (car n) 'vertical-bar)
(cons
(prog1
(parse-sub *operator-list* t)
(unless (eq (car (next-token)) 'rbracket)
(error (make-condition
'prolog-syntax-error
:cause "mismatched brackets"))))
(cons arg acc)))
((commap n)
(get-args (cons arg acc)))
(t (error (make-condition
'prolog-syntax-error
:cause "near argument list")))))))
(unget-token next)
`(list ,@(reverse (get-args nil)))))))
(parse-prim (ignore-comma)
(let ((next (next-token)))
(cond ((eq (car next) 'lparen)
(let ((inside (parse-sub *operator-list* ignore-comma)))
(if (eq (car (next-token)) 'rparen)
inside
(error (make-condition 'prolog-syntax-error
:cause "mismatched parentheses")))))
((eq (car next) 'atom)
`(struct ,(cdr next) ,@(parse-arguments)))
((eq (car next) 'lbracket)
(parse-list))
(t next))))
(separatorp (tok ignore-comma)
(or (and ignore-comma (commap tok))
(member (car tok) '(rparen dot rbracket vertical-bar))))
(operatorp (tok)
(and (eq (car tok) 'atom) (symbolp (cdr tok)) (get (cdr tok) 'op)))
(parse-sub (head &optional (ignore-comma nil))
(block exit
(let ((current-prec (op-prec (car head)))
(current-head head)
(next-head (next-prec-head head)))
(let ((gottok (next-token)))
(cond
((separatorp gottok ignore-comma)
(error (make-condition 'prolog-syntax-error
:cause "unexpected end of clause")))
((null head)
(unget-token gottok)
(return-from exit (parse-prim ignore-comma)))
((operatorp gottok)
(let ((next (next-token)))
(if (separatorp next ignore-comma)
(progn
(unget-token next)
(return-from exit `(struct ,(cdr gottok))))
(progn
(unget-token next)
(do
((op (car head) (progn (setq head (cdr head)) (car head))))
((or (null op) (not (eq current-prec (op-prec op)))
(not (member (op-assoc op) '(fx fy))))
(unget-token gottok))
(if (eq (cdr gottok) (op-atom op))
(case (op-assoc op)
(fx
(return-from exit
`(struct ,(op-atom op)
,(parse-sub next-head ignore-comma))))
(fy
(return-from exit
`(struct ,(op-atom op)
,(parse-sub
current-head ignore-comma)))))))))))
(t (unget-token gottok))))
(let ((operand-1 (parse-sub next-head ignore-comma))
(gottok (next-token)))
(cond
((separatorp gottok ignore-comma)
(unget-token gottok) (return-from exit operand-1))
((operatorp gottok)
(let* ((gottok-2 (let ((n (next-token))) (unget-token n) n))
(next-sep? (separatorp gottok-2 ignore-comma)))
(do ((op (car head) (progn (setq head (cdr head)) (car head))))
((or (null op) (not (eq current-prec (op-prec op))))
(progn (unget-token gottok) (return-from exit operand-1)))
(if (eq (cdr gottok) (op-atom op))
(if (not next-sep?)
(case (op-assoc op)
(xfy
(return-from exit
`(struct ,(op-atom op)
,operand-1
,(parse-sub current-head ignore-comma))))
(xfx
(return-from exit
`(struct ,(op-atom op)
,operand-1
,(parse-sub next-head ignore-comma))))
(yfx
(unget-token
`(struct
,(op-atom op)
,operand-1
,(parse-sub next-head ignore-comma)))
(return-from exit
(parse-sub current-head ignore-comma))))
(case (op-assoc op)
(xf
(return-from exit
`(struct ,(op-atom op) ,operand-1)))
(yf
(unget-token `(struct ,(op-atom op) ,operand-1))
(return-from exit
(parse-sub
current-head ignore-comma)))))))))))))))
(let ((result (arrange (parse-sub *operator-list*))))
(if (not (eq (car (next-token)) 'dot))
(error (make-condition 'prolog-syntax-error
:cause "operator arity error")) result)))))
(defun arity (term) (length (cdr term)))
(defmacro variablep (x) `(and (atom ,x)))
(defmacro anonymousvar-p (x) `(eq ,x '|_|))
(defmacro termp (x) `(listp ,x))
(defmacro listterm-p (x) `(and (listp ,x) (eq (car ,x) '|.|)))
(defmacro cut-operator-p (x) `(and (listp ,x) (eq (car ,x) '|!|) (= (arity ,x) 0)))
(defun flatten-comma (tree)
(cond ((and (consp tree) (eq '|,| (car tree)))
(append (flatten-comma (cadr tree)) (flatten-comma (caddr tree))))
((and (consp tree) (symbolp (car tree))) (list tree))
(t (error (make-condition 'prolog-compile-error
:cause "predicate expected on body")))))
(defun collect-vars (goal)
(cond ((anonymousvar-p goal) nil)
((variablep goal) (list goal))
((cut-operator-p goal) (list '|!|))
((termp goal) (mapcan #'collect-vars (cdr goal)))
(t nil)))
;;In variable assignment, head term is treated as the part of first body term.
;;(VAR . (FIRST . LAST))
(defun make-varposition-table (goals)
(let ((tbl nil) (cnt 0))
(dolist (g goals)
(dolist (v (collect-vars g))
(if (assoc v tbl)
(rplacd (assoc v tbl) (cons (cadr (assoc v tbl)) cnt))
(if (eq v '|!|)
(setq tbl (cons (cons v (cons 0 cnt)) tbl))
(setq tbl (cons (cons v (cons cnt cnt)) tbl)))))
(incf cnt))
tbl))
(defun make-arity-list (head body)
(let ((head-arity (arity head))
(body1-arity (arity (car body))))
(cons (max head-arity body1-arity)
(mapcar (lambda (term) (arity term)) (cdr body)))))
(defun head-firstbody-conj (head body)
(cond ((null head) (car body))
(t (append head (cdar body)))))
(defun assign-variables (head body)
(let* ((Y-counter 0)
(arity-list (make-arity-list head body))
(A-counter (apply #'max arity-list))
(postbl
(remove-if
(lambda (vardata)
(and (eq (car vardata) '|!|) (= (cddr vardata) 0)))
(make-varposition-table (cons (head-firstbody-conj head body) (cdr body)))))
(sorted-tbl (sort postbl (lambda (x y) (> (cddr x) (cddr y)))))
(assigned-tbl
(mapcar
(lambda (v)
(let ((var (car v))
(first-appear (cadr v)) (last-appear (cddr v)))
(if (= first-appear last-appear)
(cons var (cons 'temporary (incf A-counter)))
(cons var (cons 'permanent (incf Y-counter)))))) sorted-tbl))
(remain-list (make-list (length body) :initial-element 0)))
;;make remain-list
(dolist (v sorted-tbl)
(loop for i from 0 to (1- (cddr v)) do
(incf (nth i remain-list))))
(when (consp remain-list)
(setf (car (last remain-list)) -1))
(values assigned-tbl A-counter remain-list)))
(defun assign-variables-query (head body)
(let* ((Y-counter 0)
(arity-list (make-arity-list head body))
(A-counter (apply #'max arity-list))
(postbl
(remove-if
(lambda (vardata)
(and (eq (car vardata) '|!|) (= (cddr vardata) 0)))
(make-varposition-table (cons (head-firstbody-conj head body) (cdr body)))))
(sorted-tbl (sort postbl (lambda (x y) (> (cddr x) (cddr y)))))
(assigned-tbl
(mapcar
(lambda (v)
(let ((var (car v)))
(cons var (cons 'permanent (incf Y-counter))))) sorted-tbl))
(remain-list (make-list (length body) :initial-element (length assigned-tbl))))
(values assigned-tbl A-counter remain-list)))
(defun assign-test ()
(let ((expr (parse *standard-input*)))
(cond ((and (eq (car expr) '|:-|) (= (arity expr) 2))
(assign-variables (cadr expr) (flatten-comma (caddr expr))))
((and (eq (car expr) '|:-|) (= (arity expr) 1))
(assign-variables nil (flatten-comma (cadr expr))))
(t
(assign-variables expr nil)))))
(defun print-hashtable (ht)
(princ "{")
(let ((counter 0) (len (hash-table-count ht)))
(maphash (lambda (k v)
(incf counter)
(if (consp k)
(format t "~A/~A => ~A" (car k) (cdr k) v)
(format t "~A => ~A" k v))
(when (/= counter len)
(princ ", "))) ht)
(princ "}")))
(defun print-wamcode (code)
(dolist (inst code)
(case (car inst)
(put-variable-temporary
(format t "~10Tput-variable X~A,A~A~%" (cadr inst) (caddr inst)))
(put-variable-permanent
(format t "~10Tput-variable Y~A,A~A~%" (cadr inst) (caddr inst)))
(put-value-temporary
(format t "~10Tput-value X~A,A~A~%" (cadr inst) (caddr inst)))
(put-value-permanent
(format t "~10Tput-value Y~A,A~A~%" (cadr inst) (caddr inst)))
(put-unsafe-value
(format t "~10Tput-unsafe-value Y~A,A~A~%" (cadr inst) (caddr inst)))
(put-structure
(format t "~10Tput-structure ~A/~A,A~A~%" (caadr inst) (cdadr inst) (caddr inst)))
(put-list
(format t "~10Tput-list A~A~%" (cadr inst)))
(put-constant
(format t "~10Tput-constant ~A,A~A~%" (cadr inst) (caddr inst)))
(set-variable-temporary
(format t "~10Tset-variable X~A~%" (cadr inst)))
(set-variable-permanent
(format t "~10Tset-variable Y~A~%" (cadr inst)))
(set-value-temporary
(format t "~10Tset-value X~A~%" (cadr inst)))
(set-value-permanent
(format t "~10Tset-value Y~A~%" (cadr inst)))
(set-local-value-temporary
(format t "~10Tset-local-value X~A~%" (cadr inst)))
(set-local-value-permanent
(format t "~10Tset-local-value Y~A~%" (cadr inst)))
(set-constant
(format t "~10Tset-constant ~A~%" (cadr inst)))
(set-void
(format t "~10Tset-void ~A~%" (cadr inst)))
(get-variable-temporary
(format t "~10Tget-variable X~A,A~A~%" (cadr inst) (caddr inst)))
(get-variable-permanent
(format t "~10Tget-variable Y~A,A~A~%" (cadr inst) (caddr inst)))
(get-value-temporary
(format t "~10Tget-value X~A,A~A~%" (cadr inst) (caddr inst)))
(get-value-permanent
(format t "~10Tget-value Y~A,A~A~%" (cadr inst) (caddr inst)))
(get-structure
(format t "~10Tget-structure ~A/~A,A~A~%" (caadr inst) (cdadr inst) (caddr inst)))
(get-list
(format t "~10Tget-list A~A~%" (cadr inst)))
(get-constant
(format t "~10Tget-constant ~A,A~A~%" (cadr inst) (caddr inst)))
(unify-variable-temporary
(format t "~10Tunify-variable X~A~%" (cadr inst)))
(unify-variable-permanent
(format t "~10Tunify-variable Y~A~%" (cadr inst)))
(unify-value-temporary
(format t "~10Tunify-value X~A~%" (cadr inst)))
(unify-value-permanent
(format t "~10Tunify-value Y~A~%" (cadr inst)))
(unify-local-value-temporary
(format t "~10Tunify-local-value X~A~%" (cadr inst)))
(unify-local-value-permanent
(format t "~10Tunify-local-value Y~A~%" (cadr inst)))
(unify-constant
(format t "~10Tunify-constant ~A~%" (cadr inst)))
(unify-void
(format t "~10Tunify-void ~A~%" (cadr inst)))
(allocate
(format t "~10Tallocate~%"))
(deallocate
(format t "~10Tdeallocate~%"))
(allocate-for-query
(format t "~10Tallocate-for-query ~A~%" (cadr inst)))
(call
(format t "~10Tcall ~A/~A,~A~%" (caadr inst) (cdadr inst) (caddr inst)))
(execute
(format t "~10Texecute ~A/~A~%" (caadr inst) (cdadr inst)))
(proceed
(format t "~10Tproceed~%"))
(try-me-else
(format t "~10Ttry-me-else ~A~%" (cadr inst)))
(retry-me-else
(format t "~10Tretry-me-else ~A~%" (cadr inst)))
(trust-me
(format t "~10Ttrust-me~%"))
(try
(format t "~10Ttry ~A~%" (cadr inst)))
(retry
(format t "~10Tretry ~A~%" (cadr inst)))
(trust
(format t "~10Ttrust ~A~%" (cadr inst)))
(switch-on-term
(format t "~10Tswitch-on-term ~A,~A,~A,~A~%"
(cadr inst) (caddr inst) (cadddr inst) (car (cddddr inst))))
(switch-on-constant
(format t "~10Tswitch-on-constant ")
(print-hashtable (cadr inst)) (princ #\Newline))
(switch-on-structure
(format t "~10Tswitch-on-structure ")
(print-hashtable (cadr inst)) (princ #\Newline))
(neck-cut
(format t "~10Tneck-cut~%"))
(get-level
(format t "~10Tget-level Y~A~%" (cadr inst)))
(cut
(format t "~10Tcut Y~A~%" (cadr inst)))
(label
(format t "~A:" (cadr inst)))
(notify-solution
(format t "~10Tnotify-solution~%"))
(function-call
(format t "~10Tfunction-call~%"))
(backtrack-if-fail
(format t "backtrack-if-fail~%"))
(t (error (make-condition 'prolog-bad-instruction-error :inst (car inst)))))))
(defun have-key? (key ht)
(multiple-value-bind (val exist) (gethash key ht)
(declare (ignore val))
exist))
(defmacro awhen (condition &body body)
`(let ((it ,condition))
(when it
,@body)))
(defmacro awhile (condition &body body)
`(loop
(let ((it ,condition))
(if it
(progn ,@body)
(return)))))
(defmacro aif (condition true-part &optional false-part)
`(let ((it ,condition))
(if it ,true-part ,false-part)))
(defun sublis-temporary! (code replace-alist)
(dolist (inst code)
(case (car inst)
((put-variable-temporary
put-value-temporary
put-list
set-variable-temporary
set-value-temporary
get-variable-temporary
get-value-temporary
get-list
unify-variable-temporary
unify-value-temporary)
(awhen (assoc (cadr inst) replace-alist)
(setf (cadr inst) (cdr it))))
((put-structure
put-constant
get-structure
get-constant)
(awhen (assoc (caddr inst) replace-alist)
(setf (caddr inst) (cdr it))))))
code)
(defun make-replace-alist (partial-code A-start)
(let (modified-temporary-list modified-register-list replace-alist)
(labels ((x? (n)
(>= n A-start))
(can-replace (tempvar register)
(unless (assoc tempvar replace-alist)
(push (cons tempvar register) replace-alist)))
(modify-register (r)
(mapc (lambda (p)
(when (eq (cdr p) r)
(pushnew (car p) modified-temporary-list))) replace-alist))
(modify-temporary (temp)
(awhen (assoc temp replace-alist)
(pushnew (cdr it) modified-register-list)))
(use-temporary (temp-var)
(when (member temp-var modified-temporary-list)
(setq replace-alist (remove-if (lambda (p)
(eq (car p) temp-var))
replace-alist))))
(use-register (reg)
(when (member reg modified-register-list)
(setq replace-alist (remove-if (lambda (p)
(eq (cdr p) reg))
replace-alist)))))
(dolist (inst partial-code)
(case (car inst)
(put-variable-temporary
(can-replace (second inst) (third inst)))
(put-value-temporary
(can-replace (second inst) (third inst)))
(get-variable-temporary
(can-replace (second inst) (third inst)))
(get-value-temporary
(can-replace (second inst) (third inst)))))
(dolist (inst partial-code)
(case (car inst)
(put-variable-temporary
(modify-temporary (second inst))
(modify-register (third inst)))
(put-variable-permanent
(modify-register (third inst)))
(put-value-temporary
(use-temporary (second inst))
(modify-register (third inst)))
(put-value-permanent
(modify-register (third inst)))
(put-structure
(if (x? (third inst))
(modify-temporary (third inst))
(modify-register (third inst))))
(put-list
(if (x? (second inst))
(modify-temporary (second inst))
(modify-register (second inst))))
(put-constant
(if (x? (third inst))
(modify-temporary (third inst))
(modify-register (third inst))))
(get-variable-temporary
(use-register (third inst))
(modify-temporary (second inst)))
(get-variable-permanent
(use-register (third inst)))
(get-value-temporary
(use-register (third inst))
(use-temporary (second inst)))
(get-value-permanent
(use-register (third inst)))
(get-structure
(if (x? (third inst))
(use-temporary (third inst))
(use-register (third inst))))
(get-list
(if (x? (second inst))
(use-temporary (second inst))
(use-register (second inst))))
(get-constant
(if (x? (third inst))
(use-temporary (third inst))
(use-register (third inst))))
(set-variable-temporary
(if (x? (second inst))
(modify-temporary (second inst))
(modify-register (second inst))))
(set-value-temporary
(if (x? (second inst))
(use-temporary (second inst))
(use-register (second inst))))
(unify-variable-temporary
(if (x? (second inst))
(modify-temporary (second inst))
(modify-register (second inst))))
(unify-value-temporary
(if (x? (second inst))
(use-temporary (second inst))
(use-register (second inst))))))
replace-alist)))
(defun make-unnecessary-temporary-list (partial-code A-start)
(let (temporary-occur-alist)
(labels ((x? (n)
(>= n A-start))
(occur-temporary (temp-var)
(aif (assoc temp-var temporary-occur-alist)
(incf (cdr it))
(push (cons temp-var 1) temporary-occur-alist))))
(dolist (inst partial-code)
(case (car inst)
((put-variable-temporary
put-value-temporary)
(occur-temporary (second inst)))
(put-structure
(when (x? (third inst))
(occur-temporary (third inst))))
(put-list
(when (x? (second inst))
(occur-temporary (second inst))))
(put-constant
(when (x? (third inst))
(occur-temporary (third inst))))
((get-variable-temporary
get-value-temporary)
(occur-temporary (second inst)))
(get-structure
(when (x? (third inst))
(occur-temporary (third inst))))
(get-list
(when (x? (second inst))
(occur-temporary (second inst))))
(get-constant
(when (x? (third inst))
(occur-temporary (third inst))))
(set-variable-temporary
(when (x? (second inst))
(occur-temporary (second inst))))
(set-value-temporary
(when (x? (second inst))
(occur-temporary (second inst))))
(unify-variable-temporary
(when (x? (second inst))
(occur-temporary (second inst))))
(unify-value-temporary
(when (x? (second inst))
(occur-temporary (second inst))))))
(mapcar
#'car (remove-if (lambda (pair) (> (cdr pair) 1)) temporary-occur-alist)))))
(defun optimize-head (partial-code A-start)
(sublis-temporary! partial-code (make-replace-alist partial-code A-start)))
(defun analyze-body (partial-code)
(let ((replace-alist nil))
(dolist (inst partial-code)
(case (car inst)
((put-variable-temporary
put-value-temporary)
(if (assoc (cadr inst) replace-alist)
(rplacd (assoc (cadr inst) replace-alist) (caddr inst))
(push (cons (cadr inst) (caddr inst)) replace-alist)))))
replace-alist))
(defun optimize-body (partial-code A-start)
(sublis-temporary! partial-code (make-replace-alist partial-code A-start)))
(defun remove-unnecessary-code (code)
(remove nil
(maplist (lambda (current-code)
(let ((inst (car current-code)))
(case (car inst)
((put-value-temporary
get-variable-temporary get-value-temporary)
(unless (= (cadr inst) (caddr inst))
inst))
(t inst))))
code)))
;;must be called before calling reallocate-registers!
(defun set-unsafe-and-local! (code)
(let ((temporary-state-table (make-hash-table :test #'eq))
(permanent-state-table (make-hash-table :test #'eq))
(permanent-lastgoal-table (make-hash-table :test #'eq)))
(let ((body-num 0))
(dolist (inst code)
(case (car inst)
((put-variable-permanent
put-value-permanent
set-variable-permanent
set-value-permanent
get-variable-permanent
get-value-permanent
unify-variable-permanent
unify-value-permanent)
(setf (gethash (cadr inst) permanent-lastgoal-table) body-num))
((call execute proceed)
(incf body-num)))))
(let ((body-num 0))
(dolist (inst code)
(case (car inst)
(put-variable-temporary
(unless (have-key? (cadr inst) temporary-state-table)
(setf (gethash (cadr inst) temporary-state-table) 'on-heap)))
(put-variable-permanent
(unless (have-key? (cadr inst) permanent-state-table)
(setf (gethash (cadr inst) permanent-state-table) 'initialized)))
(put-value-temporary
(unless (have-key? (cadr inst) temporary-state-table)
(setf (gethash (cadr inst) temporary-state-table) 'initialized)))
(put-value-permanent
(when (and
(have-key? (cadr inst) permanent-state-table)
(eq 'initialized (gethash (cadr inst) permanent-state-table))
(eq body-num (gethash (cadr inst) permanent-lastgoal-table)))
(setf (car inst) 'put-unsafe-value)
(setf (gethash (cadr inst) permanent-state-table) 'on-heap))
(unless (have-key? (cadr inst) permanent-state-table)
(setf (gethash (cadr inst) permanent-state-table) 'initialized)))
(set-variable-temporary
(unless (have-key? (cadr inst) temporary-state-table)
(setf (gethash (cadr inst) temporary-state-table) 'on-heap)))
(set-variable-permanent
(unless (have-key? (cadr inst) permanent-state-table)
(setf (gethash (cadr inst) permanent-state-table) 'on-heap)))
(set-value-temporary
(when (and
(have-key? (cadr inst) temporary-state-table)
(eq 'initialized (gethash (cadr inst) temporary-state-table)))
(setf (car inst) 'set-local-value-temporary)
(setf (gethash (cadr inst) temporary-state-table) 'on-heap)))
(set-value-permanent
(when (and
(have-key? (cadr inst) permanent-state-table)
(eq 'initialized (gethash (cadr inst) permanent-state-table)))
(setf (car inst) 'set-local-value-permanent)
(setf (gethash (cadr inst) permanent-state-table) 'on-heap)))
(get-variable-temporary
(unless (have-key? (cadr inst) temporary-state-table)
(setf (gethash (cadr inst) temporary-state-table) 'initialized)))
(get-variable-permanent
(unless (have-key? (cadr inst) permanent-state-table)
(setf (gethash (cadr inst) permanent-state-table) 'initialized)))
(get-value-temporary
(unless (have-key? (cadr inst) temporary-state-table)
(setf (gethash (cadr inst) temporary-state-table) 'initialized)))
(get-value-permanent
(unless (have-key? (cadr inst) permanent-state-table)
(setf (gethash (cadr inst) permanent-state-table) 'initialized)))
(unify-variable-temporary
(unless (have-key? (cadr inst) temporary-state-table)
(setf (gethash (cadr inst) temporary-state-table) 'on-heap)))
(unify-variable-permanent
(unless (have-key? (cadr inst) permanent-state-table)
(setf (gethash (cadr inst) permanent-state-table) 'on-heap)))
(unify-value-temporary
(when (and
(have-key? (cadr inst) temporary-state-table)
(eq 'initialized (gethash (cadr inst) temporary-state-table)))
(setf (car inst) 'unify-local-value-temporary)
(setf (gethash (cadr inst) temporary-state-table) 'on-heap))
(unless (have-key? (cadr inst) temporary-state-table)
(setf (gethash (cadr inst) temporary-state-table) 'on-heap)))
(unify-value-permanent
(when (and
(have-key? (cadr inst) permanent-state-table)
(eq 'initialized (gethash (cadr inst) permanent-state-table)))
(setf (car inst) 'unify-local-value-permanent)
(setf (gethash (cadr inst) permanent-state-table) 'on-heap))
(unless (have-key? (cadr inst) permanent-state-table)
(setf (gethash (cadr inst) permanent-state-table) 'on-heap)))
((call execute proceed)
(incf body-num)))))
code))
(defun reallocate-registers! (code A-start arity-list)
(let ((using-register-list (list nil)))
;;collect temporary variables...
(dolist (inst code)
(case (car inst)
((put-variable-temporary
put-value-temporary
put-list
get-variable-temporary
get-value-temporary
get-list
set-variable-temporary
set-value-temporary
set-local-value-temporary
unify-variable-temporary
unify-value-temporary
unify-local-value-temporary)
(when (>= (cadr inst) A-start)
(pushnew (cadr inst) (car using-register-list))))
((put-structure
put-constant
get-structure
get-constant)
(when (>= (caddr inst) A-start)