-
Notifications
You must be signed in to change notification settings - Fork 1
/
compile.lisp
2014 lines (1866 loc) · 91.3 KB
/
compile.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
(in-package :cl-meld)
(define-condition compile-invalid-error (error)
((text :initarg :text :reader text)))
(defun has-reg-p (regs reg)
(find-if #'(lambda (x) (reg-eq-p x reg)) regs))
(defun extend-regs (regs reg)
(if (has-reg-p regs reg)
regs
(cons reg regs)))
(defun free-reg (regs reg)
(remove reg regs :count 1 :test #'reg-eq-p))
(defun find-unused-reg (regs)
(loop for i upto (1- *num-regs*)
do (if (not (has-reg-p regs (make-reg i)))
(return-from find-unused-reg (make-reg i)))))
(defun find-unused-n-regs (regs n)
(let ((c 0))
(loop for i upto (1- *num-regs*)
while (< c n)
append (unless (has-reg-p regs (make-reg i))
(incf c)
`(,(make-reg i))))))
(defun alloc-reg (regs)
(assert (< (length regs) *num-regs*))
(let ((new-reg (find-unused-reg regs)))
(values new-reg (extend-regs regs new-reg))))
(defun alloc-n-reg (regs n)
(assert (< (+ n (length regs)) *num-regs*))
(let ((new-regs (find-unused-n-regs regs n)))
(values new-regs (append regs new-regs))))
(defparameter *compiling-consts* nil)
(defparameter *compiling-axioms* nil)
(defparameter *compilation-clause* nil)
(defparameter *compiling-rule* nil)
(defparameter *compilation-rule-id* 0)
(defparameter *starting-subgoal* nil)
(defparameter *vars-places* nil)
(defparameter *used-regs* nil)
(defmacro with-empty-compile-context (&body body)
"Initiates compile context."
`(let ((*vars-places* (make-hash-table))
(*used-regs* nil))
,@body))
(defmacro with-compile-context (&body body)
`(let ((*vars-places* (copy-hash-table *vars-places*))
(*used-regs* (copy-list *used-regs*)))
,@body))
(defun alloc-new-reg () (alloc-reg *used-regs*))
(defmacro with-reg ((reg) &body body)
`(multiple-value-bind (,reg *used-regs*) (alloc-new-reg)
,@body))
(defmacro with-n-regs (n (regs) &body body)
`(multiple-value-bind (,regs *used-regs*) (alloc-n-reg *used-regs* ,n)
,@body))
(defmacro with-old-reg ((reg) &body body)
"Adds a new register to the context so it is not allocated inside body."
`(cond
((reg-p ,reg)
(let ((*used-regs* (extend-regs *used-regs* ,reg)))
,@body))
(t
,@body)))
(defun lookup-used-var (var-name)
(multiple-value-bind (data found) (gethash var-name *vars-places*)
(when found data)))
(defun add-used-var (var-name data)
(setf (gethash var-name *vars-places*) data))
(defun remove-used-var (var-name)
(remhash var-name *vars-places*))
(defun all-used-var-names () (alexandria:hash-table-keys *vars-places*))
(defun hash-table-to-stack (hash)
(let ((new-hash (make-hash-table)))
(loop for key in (all-used-var-names)
do (let ((now (gethash key hash)))
(setf (gethash key new-hash)
(if (reg-p now)
(let ((stack-off (reg-num now)))
(make-vm-stack stack-off))
now))))
new-hash))
(defmacro with-stack-compile-context (&body body)
`(let ((*vars-places* (hash-table-to-stack *vars-places*)))
,@body))
(defun valid-constraint-p (all-vars)
"Returns a function that returns true if constraint argument uses only variables from 'all-vars'."
#L(subsetp (all-variable-names !1) all-vars))
(defun equal-ass-constraint-p (all-vars)
"Returns a function that returns constraints of the form NewVar = Expr and Expr uses only variables from 'all-vars'."
#'(lambda (constr)
(let ((expr (constraint-expr constr)))
(when (equal-p expr)
(let ((op1 (op-op1 expr))
(op2 (op-op2 expr)))
(flet ((check-arguments (op1 op2)
(and (var-p op1) (not (member (var-name op1) all-vars)) (subsetp (all-variable-names op2) all-vars))))
(or (check-arguments op1 op2)
(check-arguments op2 op1))))))))
(defun create-new-assignment-from-equal (all-vars)
"From a constraint of the form NewVar = Expr, create a new assignment."
#'(lambda (constraint)
(let* ((cexpr (constraint-expr constraint))
(v1 (op-op1 cexpr)) (v2 (op-op2 cexpr))
(is-v1-p (and (var-p v1) (not (member (var-name v1) all-vars))))
(var (if is-v1-p v1 v2))
(expr (if is-v1-p v2 v1)))
(make-assignment var expr))))
(defun get-compile-constraints-and-assignments (body)
"Return constraints and assignments that can be used from the body."
(let* ((assignments (select-valid-assignments body nil (all-used-var-names)))
(vars-ass (mapcar #L(var-name (assignment-var !1)) assignments))
(all-vars (append vars-ass (all-used-var-names)))
(constraints (filter (valid-constraint-p all-vars) (get-constraints body)))
(equal-constraints (filter (equal-ass-constraint-p all-vars) (get-constraints body)))
(new-ass (mapcar (create-new-assignment-from-equal all-vars) equal-constraints))
(new-body (remove-all body `(,@assignments ,@constraints ,@equal-constraints))))
(values constraints
`(,@assignments ,@new-ass)
new-body)))
(defun make-low-constraint (typ v1 v2) `(,typ ,v1 ,v2))
(defun low-constraint-type (lc) (first lc))
(defun low-constraint-v1 (lc) (second lc))
(defun low-constraint-v2 (lc) (third lc))
(defmacro return-expr (place &optional code) `(values ,place ,code (if (reg-p ,place) (extend-regs *used-regs* ,place) *used-regs*)))
(defmacro with-compiled-expr ((place code &key (force-dest nil) (top-level nil)) expr &body body)
`(multiple-value-bind (,place ,code *used-regs*) (compile-expr ,expr :dest ,force-dest :top-level ,top-level)
(cond
((and ,force-dest (not (equalp ,place ,force-dest)))
(setf ,code (append ,code (list (make-move ,place ,force-dest))))
(setf ,place ,force-dest)
,@body)
(t
,@body))))
(defmacro with-compilation ((place code &key (top-level nil)) expr &body body)
`(multiple-value-bind (,place ,code *used-regs*) (compile-expr ,expr :top-level ,top-level)
,@body))
(defmacro with-compilation-on-reg ((place code) expr &body body)
"Ensures that place is a register."
(alexandria:with-gensyms (new-reg)
`(with-compilation (,place ,code) ,expr
(if (not (reg-p ,place))
(with-reg (,new-reg)
(setf ,code (append ,code (list (make-move ,place ,new-reg))))
(setf ,place ,new-reg)
,@body)
(progn ,@body)))))
(defmacro with-compilation-on-rf ((place code &key top-level) expr &body body)
"Ensures that place is either a field or register."
(alexandria:with-gensyms (new-reg)
`(with-compilation (,place ,code :top-level ,top-level) ,expr
(if (not (or (reg-p ,place) (reg-dot-p ,place)))
(with-reg (,new-reg)
(setf ,code (append ,code (list (make-move ,place ,new-reg))))
(setf ,place ,new-reg)
,@body)
(progn ,@body)))))
(defmacro with-dest-or-new-reg ((dest) &body body)
`(if (null ,dest)
(with-reg (,dest) ,@body)
(progn ,@body)))
(defmacro compile-expr-to (expr place &key (top-level nil) (update-p nil))
(alexandria:with-gensyms (new-place code)
`(multiple-value-bind (,new-place ,code *used-regs*) (compile-expr ,expr :dest ,place :top-level ,top-level)
(if (not (equal ,new-place ,place))
(append ,code (list (make-move ,new-place ,place (expr-type ,expr))))
,code))))
(defmacro compile-expr-to-reg (expr &key try-place (top-level nil))
(alexandria:with-gensyms (new-place new-code)
`(cond
((or (null ,try-place) (not (reg-p ,try-place)))
(with-reg (,new-place)
(values ,new-place (compile-expr-to ,expr ,new-place :top-level ,top-level))))
(t
(values ,try-place (compile-expr-to ,expr ,try-place :top-level ,top-level))))))
(defun decide-external-function (name new-reg regs gc call)
"Decides if external function is pre-defined or not."
(if (lookup-standard-external-function name)
(make-vm-call name new-reg regs (make-vm-bool gc) (expr-type call))
(make-vm-calle name new-reg regs (make-vm-bool gc) (expr-type call))))
(defun get-external-function-ret-type (name)
(let ((fun (lookup-standard-external-function name)))
(when fun
(extern-ret-type fun))))
(defun find-polymorphic-type (extern concrete-types)
(let ((template-types `(,(extern-ret-type extern) ,@(extern-types extern))))
(when (and (extern-poly-p extern) (has-all-type-p template-types))
(loop for template in template-types
for concrete in concrete-types
when (has-all-type-p template)
do (progn
(let ((typ (find-all-type template concrete)))
(when typ
(return-from find-polymorphic-type typ))))))))
(defun compile-call-args (args call)
(cond
((null args)
(let ((poly-typ (find-polymorphic-type (lookup-external-definition (call-name call))
`(,(expr-type call) ,@(loop for arg in (call-args call) collect (expr-type arg))))))
(cond
(poly-typ
(with-reg (type-reg)
(values `(,type-reg) `(,(make-move (make-vm-type poly-typ) type-reg)))))
(t (values nil nil)))))
(t
(with-compilation-on-reg (arg-place arg-code) (first args)
(multiple-value-bind (regs codes) (compile-call-args (rest args) call)
(values (cons arg-place regs) `(,@arg-code ,@codes)))))))
(defun compile-call (name args dest call gc)
(multiple-value-bind (regs codes) (compile-call-args args call)
(cond
((null dest)
(with-reg (new-dest)
(return-expr new-dest `(,@codes ,(decide-external-function name new-dest regs gc call)))))
((and dest (reg-p dest))
(return-expr dest `(,@codes ,(decide-external-function name dest regs gc call))))
(t
(with-reg (new-dest)
(return-expr dest `(,@codes ,(decide-external-function name new-dest regs gc call)
,(make-move new-dest dest (expr-type call)))))))))
(defun compile-callf-args (args args-code n)
(if (null args)
args-code
(with-compiled-expr (arg-place arg-code :force-dest (make-reg n)) (first args)
(compile-callf-args (rest args) `(,@args-code ,@arg-code) (1+ n)))))
(defun compile-expr (expr &key dest top-level)
(cond
((bool-p expr)
(return-expr (make-vm-bool (bool-val expr))))
((int-p expr) ;; Int expression in the previous phases maybe coerced into a float
(let ((typ (expr-type expr)))
(return-expr (funcall (if (type-int-p typ) #'make-vm-int #'make-vm-float)
(int-val expr)))))
((float-p expr) (return-expr (make-vm-float (float-val expr))))
((string-constant-p expr) (return-expr (make-vm-string-constant (string-constant-val expr))))
((addr-p expr) (return-expr (make-vm-addr (addr-num expr))))
((or (host-p expr) (host-id-p expr))
(cond
(dest
(return-expr dest `(,(make-move (make-vm-host-id) dest))))
(t
(return-expr (make-vm-host-id)))))
((thread-id-p expr) (return-expr (make-vm-thread-id)))
((argument-p expr)
(return-expr (make-vm-argument (argument-id expr))))
((get-constant-p expr)
(return-expr (make-vm-constant (get-constant-name expr))))
((var-p expr)
(let ((look (lookup-used-var (var-name expr))))
(unless look
(error 'compile-invalid-error :text (tostring "Cannot find variable ~a" (var-name expr))))
(return-expr look)))
((call-p expr)
(let ((name (call-name expr))
(args (call-args expr)))
(cond
((string-equal name "fabs")
(with-dest-or-new-reg (dest)
(multiple-value-bind (arg-place arg-code) (compile-expr-to-reg (first args) :try-place dest :top-level top-level)
(cond
((reg-p dest)
(return-expr dest `(,@arg-code ,(make-vm-fabs arg-place dest))))
(t
(return-expr dest `(,@arg-code ,(make-vm-fabs arg-place arg-place) ,(make-move arg-place dest))))))))
((string-equal name "cpu-id")
(cond
((or (null dest) (reg-p dest))
(with-dest-or-new-reg (dest)
(with-compiled-expr (arg-place arg-code :force-dest dest) (first args)
(return-expr dest `(,@arg-code ,(make-vm-cpu-id arg-place dest))))))
(t
(with-reg (new-dest)
(with-compiled-expr (arg-place arg-code :force-dest new-dest) (first args)
(return-expr dest `(,@arg-code ,(make-vm-cpu-id arg-place new-dest) ,(make-move new-dest dest))))))))
((string-equal name "is-moving")
(with-dest-or-new-reg (dest)
(with-compiled-expr (arg-place arg-code :force-dest dest) (first args)
(return-expr dest `(,@arg-code ,(make-vm-is-moving arg-place dest))))))
((string-equal name "is-static")
(with-dest-or-new-reg (dest)
(with-compiled-expr (arg-place arg-code :force-dest dest) (first args)
(return-expr dest `(,@arg-code ,(make-vm-is-static arg-place dest))))))
((string-equal name "priority")
(with-dest-or-new-reg (dest)
(with-compiled-expr (arg-place arg-code :force-dest dest) (first args)
(return-expr dest `(,@arg-code ,(make-vm-node-priority arg-place dest))))))
((string-equal name "cpu-static")
(with-dest-or-new-reg (dest)
(with-compiled-expr (arg-place arg-code :force-dest dest) (first args)
(return-expr dest `(,@arg-code ,(make-vm-cpu-static arg-place dest))))))
((string-equal name "facts-proved")
(with-dest-or-new-reg (dest)
(with-compiled-expr (arg-place arg-code :force-dest dest) (first args)
(return-expr dest `(,@arg-code ,(make-vm-facts-proved arg-place dest))))))
((string-equal name "facts-consumed")
(with-dest-or-new-reg (dest)
(with-compiled-expr (arg-place arg-code :force-dest dest) (first args)
(return-expr dest `(,@arg-code ,(make-vm-facts-consumed arg-place dest))))))
(t
(compile-call name args dest expr (not top-level))))))
((struct-val-p expr)
(with-dest-or-new-reg (dest)
(let ((look (lookup-used-var (var-name (struct-val-var expr)))))
(assert look)
(return-expr dest `(,(make-vm-struct-val (struct-val-idx expr) look dest (expr-type expr)))))))
((struct-p expr)
(with-dest-or-new-reg (dest)
(let ((ls (struct-list expr))
instrs)
(return-expr dest
`(,(make-vm-push-n (length ls))
,@(loop for x in ls
for i from 0
append (compile-expr-to x (make-vm-stack i)))
,(make-vm-make-struct (expr-type expr) dest))))))
((callf-p expr)
(with-dest-or-new-reg (dest)
(with-stack-compile-context
(return-expr dest `(,(make-vm-push) ;; for pcounter
,(make-vm-push) ;; for return value
,(make-vm-push-registers)
,@(compile-callf-args (callf-args expr) (list) 0)
,(make-move (make-vm-pcounter) (make-vm-stack (1+ *num-regs*)))
,(make-vm-callf (callf-name expr))
,(make-vm-pop-registers)
,(make-move (make-vm-stack 0) dest)
,(make-vm-pop)
,(make-vm-pop))))))
((convert-float-p expr)
(let (p c)
(with-compilation-on-reg (place code) (convert-float-expr expr)
(setf p place
c code))
(with-dest-or-new-reg (dest)
(cond
((reg-p dest)
(return-expr dest `(,@c ,(make-vm-convert-float p dest))))
(t
(with-reg (new-reg)
(return-expr dest `(,@c ,(make-vm-convert-float p new-reg) ,(make-move new-reg dest)))))))))
((let-p expr)
(with-dest-or-new-reg (dest)
(with-compiled-expr (place-expr code-expr) (let-expr expr)
(add-used-var (var-name (let-var expr)) place-expr)
(with-compiled-expr (place-body code-body :force-dest dest) (let-body expr)
(remove-used-var (var-name (let-var expr)))
(return-expr place-body `(,@code-expr ,@code-body))))))
((if-p expr)
(with-dest-or-new-reg (dest)
(let ((code1 (compile-expr-to (if-e1 expr) dest))
(code2 (compile-expr-to (if-e2 expr) dest)))
(multiple-value-bind (place-cmp code-cmp) (compile-expr-to-reg (if-cmp expr) :try-place dest :top-level top-level)
(return-expr dest `(,@code-cmp ,(make-vm-if-else place-cmp code1 code2 (make-vm-if-spec expr dest))))))))
((tail-p expr)
(let (p c)
(with-compilation-on-rf (place code) (head-list expr)
(setf p place
c code))
(with-dest-or-new-reg (dest)
(return-expr dest `(,@c ,(make-vm-tail p dest (expr-type expr)))))))
((head-p expr)
(let (p c)
(with-compilation-on-rf (place code) (head-list expr)
(setf p place
c code))
(with-dest-or-new-reg (dest)
(return-expr dest `(,@c ,(make-vm-head p dest (expr-type (vm-head-cons expr))))))))
((cons-p expr)
(cond
((and *compiling-consts* (constant-cons-p expr))
(with-dest-or-new-reg (dest)
(return-expr dest `(,(make-vm-literal-cons expr dest)))))
(t
(let (ptail ctail phead chead d)
(with-compilation-on-rf (place-tail code-tail :top-level top-level) (cons-tail expr)
(setf ptail place-tail
ctail code-tail)
(with-compilation-on-rf (place-head code-head :top-level top-level) (cons-head expr)
(setf phead place-head
chead code-head)))
(with-dest-or-new-reg (dest)
(return-expr dest `(,@ctail ,@chead ,(make-vm-cons phead ptail dest (expr-type expr)
(make-vm-bool (not top-level))))))))))
((not-p expr)
(let (p c)
(with-compilation-on-reg (place-expr code-expr) (not-expr expr)
(setf p place-expr
c code-expr))
(with-dest-or-new-reg (dest)
(return-expr dest `(,@c ,(make-vm-not p dest))))))
((test-nil-p expr)
(let (p c)
(with-compilation-on-reg (place-expr code-expr) (test-nil-expr expr)
(setf p place-expr
c code-expr))
(with-dest-or-new-reg (dest)
(return-expr dest `(,@c ,(make-vm-test-nil p dest))))))
((nil-p expr) (return-expr (make-vm-nil)))
((world-p expr) (return-expr (make-vm-world)))
((cpus-p expr) (return-expr (make-vm-cpus)))
((and-p expr)
(with-dest-or-new-reg (dest)
(let ((code1 (compile-expr-to (op-op1 expr) dest))
(code2 (compile-expr-to (op-op2 expr) dest)))
(return-expr dest `(,@code1 ,(make-vm-if dest code2))))))
((or-p expr)
(with-dest-or-new-reg (dest)
(let ((code1 (compile-expr-to (op-op1 expr) dest))
(code2 (compile-expr-to (op-op2 expr) dest)))
(let ((new-reg (alloc-new-reg)))
(return-expr dest
`(,@code1 ,(make-vm-not dest new-reg)
,(make-vm-if new-reg code2)))))))
((op-p expr)
(let (p1 c1 p2 c2)
(with-compilation-on-reg (place1 code1) (op-op1 expr)
(with-compilation-on-reg (place2 code2) (op-op2 expr)
(setf p1 place1
c1 code1
p2 place2
c2 code2)))
(with-dest-or-new-reg (dest)
(compile-op-expr expr p1 p2 c1 c2 dest))))
(t (assert nil)
(error 'compile-invalid-error :text (tostring "Unknown expression to compile: ~a" expr)))))
(defun compile-op-expr (expr new-place1 new-place2 new-code1 new-code2 dest)
(cond
((and dest (or (reg-dot-p dest) (vm-stack-p dest)))
(with-reg (new-dest)
(return-expr dest `(,@(generate-op-instr expr new-dest new-place1 new-place2 new-code1 new-code2)
,(make-move new-dest dest)))))
((and dest (reg-p dest))
(return-expr dest (generate-op-instr expr dest new-place1 new-place2 new-code1 new-code2)))
((null dest)
(with-reg (new-dest)
(return-expr new-dest (generate-op-instr expr new-dest new-place1 new-place2 new-code1 new-code2))))
(t (error 'compile-invalid-error :text (tostring "can't compile operation to ~a" dest)))))
(defun generate-op-instr (expr dest place1 place2 code1 code2)
(let* ((base-op (op-op expr))
(op1 (op-op1 expr)) (op2 (op-op2 expr))
(ret-type (expr-type expr)) (operand-type (expr-type op1)))
(cond
((and (equal-p expr) (or (nil-p op1) (nil-p op2)))
(let ((place (if (nil-p op1) place2 place1))
(code (if (nil-p op1) code2 code1)))
`(,@code ,(make-vm-test-nil place dest))))
((and (not-equal-p expr) (or (nil-p op1) (nil-p op2)))
(let ((place (if (nil-p op1) place2 place1))
(code (if (nil-p op1) code2 code1)))
`(,@code ,(make-vm-test-nil place dest) ,(make-vm-not dest dest))))
(t
(let ((vm-op (set-type-to-op operand-type ret-type base-op)))
(assert (not (null vm-op)) (expr) "Cannot find operator for expression ~a" expr)
`(,@code1 ,@code2 ,(make-vm-op dest place1 vm-op place2)))))))
(defun get-remote-dest (subgoal)
(let ((dest (subgoal-get-remote-dest subgoal)))
(cond
((var-p dest)
(lookup-used-var (var-name dest)))
((addr-p dest)
(make-vm-addr (addr-num dest)))
(t (assert nil)))))
(defun get-remote-reg-and-code (subgoal default)
(if (subgoal-is-remote-p subgoal)
(let ((arg (get-remote-dest subgoal)))
(if (reg-p arg)
arg
(with-reg (new-reg)
(values new-reg `(,(make-move arg new-reg))))))
default))
(defmacro with-remote-reg-and-code ((reg code) subgoal default &body body)
(alexandria:with-gensyms (arg)
`(if (subgoal-is-remote-p ,subgoal)
(let ((,arg (get-remote-dest ,subgoal)))
(if (reg-p ,arg)
(let ((,reg ,arg)
(,code nil))
,@body)
(with-reg (,reg)
(let ((,code (list (make-move ,arg ,reg))))
,@body))))
(let ((,reg ,default)
(,code nil))
,@body))))
(defun find-matchable-constraint-for-var (body var reg level &optional i)
(cond
((int-p var) (values (make-vm-int (int-val var)) nil))
((float-p var) (values (make-vm-float (float-val var)) nil))
((var-p var)
(let ((already-defined (lookup-used-var (var-name var))))
(multiple-value-bind (cs other-var) (when (zerop level)
(find-first-assignment-constraint-to-var body var))
(cond
(already-defined
(cond
((zerop level)
(values already-defined nil))
((vm-host-id-p already-defined)
(values already-defined nil))
((not (reg-eq-p (reg-dot-reg already-defined) reg))
(values already-defined nil))))
((and (not already-defined) (zerop level) cs (lookup-used-var (var-name other-var)))
(assert other-var)
(assert i)
(setf already-defined (lookup-used-var (var-name other-var)))
(assert (reg-dot-p already-defined))
(add-used-var (var-name var) (make-reg-dot reg i))
(values already-defined (list cs)))
(t
(let ((literal-constr (find-if #L(and (op-p (constraint-expr !1)) (literal-p (op-op2 (constraint-expr !1))))
(find-assignment-constraints body var)))
(non-nil-constr (find-if #'(lambda (cs)
(let ((note (not-expr (constraint-expr cs))))
(when (test-nil-p note)
(var-eq-p var (test-nil-expr note)))))
(find-not-constraints body)))
(nil-constr (find-if #'(lambda (cs)
(let ((v (test-nil-expr (constraint-expr cs))))
(var-eq-p var v)))
(find-test-nil-constraints body))))
(cond
(literal-constr
(values (op-op2 (constraint-expr literal-constr)) (list literal-constr)))
(non-nil-constr
(multiple-value-bind (ls new-constraints) (get-possible-list-constraint body var reg level)
(if ls
(values ls (cons non-nil-constr new-constraints))
(values (make-vm-non-nil) (list non-nil-constr)))))
(nil-constr
(values (make-vm-nil) (list nil-constr))))))))))))
(defun get-possible-list-constraint (body arg reg level)
"Looks into body if the list variable arg has constraints in relation to its structure."
(let ((head-ass (find-if #'(lambda (ass) (let ((e (assignment-expr ass)))
(when (head-p e)
(var-eq-p (head-list e) arg))))
(get-assignments body)))
(head-eqs (find-constraints body #L(and (equal-p !1) (head-p (op-op2 !1)) (var-eq-p (head-list (op-op2 !1)) arg))))
(tail-ass (find-if #'(lambda (ass) (let ((e (assignment-expr ass)))
(when (tail-p e)
(var-eq-p (tail-list e) arg))))
(get-assignments body)))
(tail-eq-nil (find-constraints body #L(and (equal-p !1) (nil-p (op-op1 !1)) (tail-p (op-op2 !1)) (var-eq-p (tail-list (op-op2 !1)) arg))))
head-constraints head-value tail-constraints tail-value)
(cond
(head-ass
(let ((head-var (assignment-var head-ass)))
(multiple-value-bind (val head-consts) (find-matchable-constraint-for-var body head-var reg (1+ level))
(setf head-constraints head-consts
head-value val))))
(head-eqs
(let ((head-expr (op-op1 (constraint-expr (first head-eqs)))))
(multiple-value-bind (val head-consts) (find-matchable-constraint-for-var body head-expr reg (1+ level))
(when val
(setf head-constraints head-eqs
head-value val))))))
(cond
(tail-ass
(let ((tail-var (assignment-var tail-ass)))
(multiple-value-bind (val tail-consts) (find-matchable-constraint-for-var body tail-var reg (1+ level))
(setf tail-constraints tail-consts
tail-value val))))
(tail-eq-nil
(setf tail-constraints tail-eq-nil
tail-value (make-vm-nil))))
(when (or head-value tail-value)
(unless tail-value
(setf tail-value (make-vm-any)))
(unless head-value
(setf head-value (make-vm-any)))
(values (make-vm-list head-value tail-value) (append head-constraints tail-constraints)))))
(defun add-subgoal (subgoal reg body &optional (in-c reg))
"Adds subgoal to the compilation context and returns several low constraints plus the updated body of the rule."
(with-subgoal subgoal (:args args)
(let ((low-constraints (loop for arg in args
for i upto (length args)
append (multiple-value-bind (val constrs-to-remove) (find-matchable-constraint-for-var body arg reg 0 i)
(when (or (not val) (not (reg-dot-p val)))
(add-used-var (var-name arg) (make-reg-dot reg i)))
(when val
(delete-all body constrs-to-remove)
(list (make-low-constraint (expr-type arg) (make-reg-dot in-c i) val)))))))
(values low-constraints body))))
(defun compile-remain-delete-args (n ls)
(if (null ls)
nil
(with-compiled-expr (place instrs) (first ls)
(multiple-value-bind (code places) (compile-remain-delete-args (1+ n) (rest ls))
(values `(,@instrs ,@code)
`(,(cons n place) ,@places))))))
(defun compile-delete (delete-option subgoal)
(let* ((args (delete-option-args delete-option))
(mapped (mapcar #L(nth (1- !1) (subgoal-args subgoal)) args))
(iter (first mapped))
(remain (rest mapped))
(minus-1 (make-minus iter '- (make-forced-int 2))))
(with-compiled-expr (place instrs) minus-1
(with-reg (reg)
(let ((greater-instr `(,(make-move (make-vm-int 0) reg) (make-vm-op reg place :int-greater-equal reg))))
(multiple-value-bind (remain-instrs places) (compile-remain-delete-args 2 remain)
;(format t "remain-instrs ~a places ~a~%" remain-instrs places)
(let* ((delete-code (make-vm-delete (subgoal-name subgoal) `(,(cons 1 place) ,@places)))
(if-instr (make-vm-if reg `(,delete-code))))
`(,@instrs ,@remain-instrs ,@greater-instr ,if-instr))))))))
(defun compile-inner-delete (clause)
(when (clause-has-delete-p clause)
(let ((all (clause-get-all-deletes clause)))
(loop for delete-option in all
append (compile-delete delete-option
(find-if (subgoal-by-name (delete-option-name delete-option))
(get-subgoals (clause-body clause))))))))
(defun compile-head-move (arg i tuple-reg &key (update-p nil))
(let ((reg-dot (make-reg-dot tuple-reg i :update-p update-p)))
(compile-expr-to arg reg-dot :top-level t)))
(defun general-make-send (sub name tuple-reg send-to appears-body-p)
(let ((def (lookup-definition name)))
(cond
((and (is-linear-p def) (subgoal-is-thread-p sub))
;; send to current thread.
(make-vm-enqueue-linear tuple-reg))
((and (not (is-linear-p def)) (subgoal-is-thread-p sub))
(make-vm-add-thread-persistent tuple-reg))
((and (subgoal-is-thread-p sub))
(assert nil (sub) "Cannot send subgoal ~a to thread." sub))
((definition-is-thread-p def)
(make-vm-send-thread tuple-reg send-to))
((and (not (is-reused-p def)) (is-linear-p def)
(reg-eq-p tuple-reg send-to) (not appears-body-p) (not (is-action-p def)))
(make-vm-add-linear tuple-reg))
((and (or (and (is-reused-p def) (is-linear-p def)) (not (is-linear-p def)))
(reg-eq-p tuple-reg send-to) (not (is-action-p def)))
(make-vm-add-persistent tuple-reg))
((and (is-action-p def) (reg-eq-p tuple-reg send-to))
(make-vm-run-action tuple-reg))
((and (is-linear-p def) (reg-eq-p tuple-reg send-to) appears-body-p)
(make-vm-enqueue-linear tuple-reg))
(t
(assert (not (reg-eq-p tuple-reg send-to)))
(make-send tuple-reg send-to)))))
(defun subgoal-appears-in-any-body-p (clause name)
(do-comprehensions (clause-head clause) (:left body)
(when (subgoal-appears-code-p body name)
(return-from subgoal-appears-in-any-body-p t)))
(do-agg-constructs (clause-head clause) (:body body)
(when (subgoal-appears-code-p body name)
(return-from subgoal-appears-in-any-body-p t)))
(clause-body-matches-subgoal-p clause name))
(defun expression-is-the-same-p (arg1 arg2) (equal arg1 arg2))
(defun do-compile-reused-subgoal (sub name args sub-regs)
(let* ((reused-sub (subgoal-get-reused sub))
(reg (cdr (assoc reused-sub sub-regs))))
(assert reg)
`(,@(loop for new-arg in args
for old-arg in (subgoal-args reused-sub)
for i upto (length args)
append (unless (expression-is-the-same-p old-arg new-arg)
(compile-head-move new-arg i reg :update-p t))))))
(defun do-compile-normal-subgoal (sub name args)
(with-reg (tuple-reg)
(with-remote-reg-and-code (send-to extra-code) sub tuple-reg
`(,@extra-code
,(make-vm-alloc name tuple-reg send-to)
,@(loop for arg in args
for i upto (length args)
append (compile-head-move arg i tuple-reg))
,(if (subgoal-has-delay-p sub)
(make-vm-send-delay tuple-reg send-to (subgoal-delay-value sub))
(general-make-send sub name tuple-reg send-to
(subgoal-appears-in-any-body-p *compilation-clause* name)))))))
(defun subgoal-is-set-priority-p (name) (string-equal name "set-priority"))
(defun subgoal-is-add-priority-p (name) (string-equal name "add-priority"))
(defun subgoal-is-schedule-next-p (name) (string-equal name "schedule-next"))
(defun subgoal-is-stop-program-p (name) (string-equal name "stop-program"))
(defun subgoal-is-set-default-priority-p (name) (string-equal name "set-default-priority"))
(defun subgoal-is-set-static-p (name) (string-equal name "set-static"))
(defun subgoal-is-set-moving-p (name) (string-equal name "set-moving"))
(defun subgoal-is-set-affinity-p (name) (string-equal name "set-affinity"))
(defun subgoal-is-set-cpu-p (name) (string-equal name "set-cpu"))
(defun subgoal-is-remove-priority-p (name) (string-equal name "remove-priority"))
(defun do-compile-set-priority (sub args)
(assert (= (length args) 1))
(with-compilation-on-reg (priority priority-instrs) (first args)
(with-old-reg (priority)
(multiple-value-bind (send-to extra-code) (get-remote-reg-and-code sub nil)
(if (null send-to)
`(,@priority-instrs ,(make-vm-set-priority-here priority))
`(,@priority-instrs ,@extra-code ,(make-vm-set-priority priority send-to)))))))
(defun do-compile-set-cpu (sub args)
(assert (= (length args) 1))
(with-compilation-on-reg (cpu cpu-instrs) (first args)
(with-old-reg (cpu)
(multiple-value-bind (send-to extra-code) (get-remote-reg-and-code sub nil)
(if (null send-to)
`(,@cpu-instrs ,(make-vm-set-cpu-here cpu))
`(,@cpu-instrs ,@extra-code ,(make-vm-set-cpu cpu send-to)))))))
(defun do-compile-add-priority (sub args)
(assert (= (length args) 1))
(with-compilation-on-reg (priority priority-instrs) (first args)
(with-old-reg (priority)
(multiple-value-bind (send-to extra-code) (get-remote-reg-and-code sub nil)
(if (null send-to)
`(,@priority-instrs ,(make-vm-add-priority-here priority))
`(,@priority-instrs ,@extra-code ,(make-vm-add-priority priority send-to)))))))
(defun do-compile-remove-priority (sub args)
(assert (= (length args) 0))
(multiple-value-bind (send-to extra-code) (get-remote-reg-and-code sub nil)
(if (null send-to)
`(,(make-vm-remove-priority-here))
`(,@extra-code ,(make-vm-remove-priority send-to)))))
(defun do-compile-schedule-next (sub args)
(assert (= (length args) 0))
(multiple-value-bind (send-to extra-code) (get-remote-reg-and-code sub nil)
(if (null send-to)
(with-reg (dest)
`(,(make-move (make-vm-host-id) dest) ,(make-vm-schedule-next dest)))
`(,@extra-code ,(make-vm-schedule-next send-to)))))
(defun do-compile-set-default-priority (sub args)
(assert (= (length args) 1))
(with-compilation-on-reg (priority priority-instrs) (first args)
(with-old-reg (priority)
(multiple-value-bind (send-to extra-code) (get-remote-reg-and-code sub nil)
(if (null send-to)
`(,@priority-instrs ,(make-vm-set-default-priority-here priority))
`(,@priority-instrs ,@extra-code ,(make-vm-set-default-priority priority send-to)))))))
(defun do-compile-set-static (sub args)
(assert (= (length args) 0))
(multiple-value-bind (send-to extra-code) (get-remote-reg-and-code sub nil)
(if (null send-to)
`(,(make-vm-set-static-here))
`(,@extra-code ,(make-vm-set-static send-to)))))
(defun do-compile-set-moving (sub args)
(assert (= (length args) 0))
(multiple-value-bind (send-to extra-code) (get-remote-reg-and-code sub nil)
(if (null send-to)
`(,(make-vm-set-moving-here))
`(,@extra-code ,(make-vm-set-moving send-to)))))
(defun do-compile-set-affinity (sub args)
(assert (= (length args) 1))
(with-compilation-on-reg (target target-instrs) (first args)
(with-old-reg (target)
(multiple-value-bind (send-to extra-code) (get-remote-reg-and-code sub nil)
(if (null send-to)
`(,@target-instrs ,(make-vm-set-affinity-here target))
`(,@target-instrs ,@extra-code ,(make-vm-set-affinity target send-to)))))))
(defun do-compile-remote-update-subgoal (def sub name args)
(let* ((target-def (definition-get-update-definition def))
(count (definition-get-update-count def))
(regs-needed (- (length (subgoal-args sub)) count)))
(with-n-regs (length (subgoal-args sub)) (regs)
(let ((arg-code (loop for arg in (subgoal-args sub)
for reg in regs
append (compile-expr-to arg reg :top-level t))))
(multiple-value-bind (send-to extra-code) (get-remote-reg-and-code sub nil)
(assert send-to)
`(,@arg-code ,@extra-code ,(make-vm-remote-update send-to def target-def regs count)))))))
(defun do-compile-head-subgoal (sub sub-regs)
(let ((def (lookup-subgoal-definition sub)))
(with-subgoal sub (:name name :args args)
(cond
((definition-is-update-p def)
(do-compile-remote-update-subgoal def sub name args))
((subgoal-will-reuse-other-p sub)
(do-compile-reused-subgoal sub name args sub-regs))
((subgoal-is-set-priority-p name)
(do-compile-set-priority sub args))
((subgoal-is-add-priority-p name)
(do-compile-add-priority sub args))
((subgoal-is-schedule-next-p name)
(do-compile-schedule-next sub args))
((subgoal-is-set-default-priority-p name)
(do-compile-set-default-priority sub args))
((subgoal-is-set-static-p name)
(do-compile-set-static sub args))
((subgoal-is-set-moving-p name)
(do-compile-set-moving sub args))
((subgoal-is-set-cpu-p name)
(do-compile-set-cpu sub args))
((subgoal-is-set-affinity-p name)
(do-compile-set-affinity sub args))
((subgoal-is-stop-program-p name)
`(,(make-vm-stop-program)))
((subgoal-is-remove-priority-p name)
(do-compile-remove-priority sub args))
(t
(do-compile-normal-subgoal sub name args))))))
(defun do-compile-head-subgoals-rec (subgoals sub-regs)
(cond
((null subgoals) nil)
(t
(let ((f (first subgoals))
(r (rest subgoals)))
(cond
((subgoal-will-reuse-other-p f)
(let ((other (subgoal-get-reused f))
(saved-vars nil))
(with-subgoal other (:args args-other)
(with-subgoal f (:args args-this)
(let ((rest-args-this args-this))
(dolist2 (argo args-other) (argt args-this)
(setf rest-args-this (cdr rest-args-this))
(when (and (not (equal argo argt))
(or (expr-uses-var-p r argo)
(expr-uses-var-p rest-args-this argo)))
(assert (var-p argo))
(assert (not (reference-type-p (expr-type argo))))
(push argo saved-vars))))))
(with-n-regs (length saved-vars) (regs)
(let ((backup-code (loop for var in saved-vars
for r in regs
append (let ((place (lookup-used-var (var-name var))))
(add-used-var (var-name var) r)
(assert place)
`(,(make-move place r))))))
`(,@backup-code ,@(do-compile-head-subgoal f sub-regs)
,@(do-compile-head-subgoals-rec r sub-regs))))))
(t
(append (do-compile-head-subgoal f sub-regs)
(do-compile-head-subgoals-rec r sub-regs))))))))
(defun do-compile-head-subgoals (subgoals sub-regs)
(multiple-value-bind (reuse non-reuse) (split-mult-return #'subgoal-will-reuse-other-p subgoals)
(let ((sorted-subgoals (append non-reuse reuse)))
(do-compile-head-subgoals-rec sorted-subgoals sub-regs))))
(defconstant +plus-infinity+ 2147483647)
(defun agg-construct-start (op acc spec)
(case op
(:custom
(with-agg-spec spec (:args args)
(let ((start (second args)))
(compile-expr-to start acc))))
(:collect
`(,(make-move (make-vm-nil) acc)))
((:count :sum)
(with-agg-spec spec (:var var)
(let ((typ (var-type var)))
(cond
((type-int-p typ) `(,(make-move (make-vm-int 0) acc)))
((type-float-p typ) `(,(make-move (make-vm-float 0.0) acc)))
(t (assert nil))))))
(:min
(with-agg-spec spec (:var var)
(let ((typ (var-type var)))
(cond
((type-int-p typ) `(,(make-move (make-vm-int +plus-infinity+) acc)))
((type-float-p typ) (assert nil))
(t (assert nil))))))
(otherwise (error 'compile-invalid-error :text (tostring "agg-construct-start: op ~a not recognized" op)))))
(defun agg-construct-end (op acc)
(case op
(:collect nil)
(:count nil)
(:sum nil)
(:min nil)
(:custom nil)
(otherwise (error 'compile-invalid-error :text (tostring "agg-construct-end: op ~a not recognized" op)))))
(defun agg-construct-post (op acc var)
(case op
(:collect nil)
(:count nil)
(:sum nil)
(:min nil)
(:custom nil)
(otherwise (error 'compile-invalid-error :text (tostring "agg-construct-post op ~a not recognized" op)))))
(defun agg-construct-step (op acc var gc spec)
(case op
(:custom
(with-agg-spec spec (:args args)
(let* ((fun (first args))
(extern (lookup-external-definition fun))
(dest (lookup-used-var (var-name var)))
(poly-typ (find-polymorphic-type extern (list (expr-type var) (expr-type var) (expr-type var)))))
(cond
(poly-typ
(with-reg (arg-place)
(let ((arg-code `(,(make-move (make-vm-type poly-typ) arg-place))))
(if (reg-p dest)
`(,@arg-code ,(make-vm-call fun acc (list acc dest arg-place) (make-vm-bool gc) (expr-type var)))
(with-reg (reg)
`(,@arg-code ,(make-move dest reg) ,(make-vm-call fun acc (list acc reg arg-place) (make-vm-bool gc) (expr-type var))))))))
(t
(if (reg-p dest)
`(,(make-vm-call fun acc (list acc dest) (make-vm-bool gc) (expr-type var)))
(with-reg (reg)
`(,(make-move dest reg) ,(make-vm-call fun acc (list acc reg) (make-vm-bool gc) (expr-type var))))))))))
(:collect
(let ((dest (lookup-used-var (var-name var))))
`(,(make-vm-cons dest acc acc (expr-type var) (make-vm-bool gc)))))
(:sum
(with-agg-spec spec (:var var)
(let ((src (lookup-used-var (var-name var)))
(typ (var-type var)))
(assert src)
(cond
((reg-p src)
`(,(make-vm-op acc acc (if (type-int-p typ) :int-plus :float-plus) src)))
(t
(with-reg (new)
`(,(make-move src new) ,(make-vm-op acc acc (if (type-int-p typ) :int-plus :float-plus) new))))))))
(:count
(with-reg (new)
`(,(make-move (make-vm-int 1) new) ,(make-vm-op acc acc :int-plus new))))
(:min
(let ((src (lookup-used-var (var-name var))))
(assert (reg-dot-p src))
(with-reg (new)
(with-reg (tmp)
`(,(make-move src tmp) ,(make-vm-op new tmp :int-lesser acc) ,(make-vm-if new `(,(make-move tmp acc))))))))
(otherwise (error 'compile-invalid-error
:text (tostring "agg-construct-step: op ~a not recognized" op)))))
(defun compile-agg-construct (c sub-regs)
(with-agg-construct c (:specs specs)
(compile-agg-construct-specs c specs nil nil nil nil sub-regs)))
(defun compile-agg-construct-specs (c specs end post vars-regs gcs sub-regs)
(cond
((null specs)
(let ((inner-code (compile-iterate (agg-construct-body c) (agg-construct-body c) nil nil
:head-compiler #'(lambda (h d sr s)
(declare (ignore h d sr s))
(let ((step-code (loop for var-reg in (reverse vars-regs)
for spec in (agg-construct-specs c)
for gc in gcs
append (agg-construct-step (third var-reg) (second var-reg) (first var-reg) gc spec))))
`(,@step-code ,@(do-compile-head-code (agg-construct-head0 c) nil nil nil)))))))
(dolist (var-reg vars-regs)
(add-used-var (var-name (first var-reg)) (second var-reg)))
(let ((head-code (do-compile-head-code (agg-construct-head c) sub-regs nil nil)))
(dolist (var-reg vars-regs)
(remove-used-var (var-name (first var-reg))))
`(,(make-vm-reset-linear `(,@inner-code ,@end ,(make-vm-reset-linear-end))) ,@head-code ,@post))))
(t