summaryrefslogtreecommitdiff
path: root/books/workshops/2002/georgelin-borrione-ostier/support/generates-functions.lisp
blob: cfc6e8694447b6ff73e5cae05d49e9776531b280 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; This file generates functions for a formalization in ACL2
;                                                                          ;
;   Author : Philippe Georgelin                                            ;
;   Email : Philippe.Georgelin@st.com
;                                                                          ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(in-package "ACL2")

(include-book "utils")

(defun make-namep (x)
  (make-name "" x "p"))


;; indentification of type
(defconst *int* 0)
(defconst *nat* 0)
(defconst *bit* 1)
(defconst *signed* 2)
(defconst *unsigned* 3)
(defconst *bit_vector* 3)

;; identification of functions
(defconst *error* 0)
(defconst *and* 1)
(defconst *or* 2)
(defconst *not* 3)
(defconst *xor* 4)
(defconst *nor* 5)
(defconst *nand* 6)

(defconst *+* 7)
(defconst *x* 8)
(defconst *-* 9)
(defconst *=* 10)
(defconst *others* 11)
(defconst *<=* 12)
(defconst *>=* 13)
(defconst *<* 14)
(defconst *>* 15)

(defconst *bits* 16)
(defconst *bitn* 17)
(defconst *round* 18)

;; array of true functions
(defconst *true_function*
  '((error error error error error)
    (logand b-and logand logand vec-and)
    (logior b-ior logior logior vec-or)
    (lognot b-not lognot lognot vec-not)
    (logxor b-xor logxor logxor vec-xor)
    (lognor b-nor lognor lognor vec-nor)
    (lognand b-nand lognand lognand vec-nand)
    (+ "non-defined_plus_on_bits" + + "non-defined_plus_on_bitvec")
    (* "non-defined_mult_on_bits" * * "non-defined_mult_on_bitvec")
    (- "non-defined_minus_on_bits" - - "non-defined_minus_on_bitvec")
    (= equal = = equal)
    (others others "non-defined_others_on_bits" others others
	    "non-defined_others_on_bit_vector")
    (<= <= "non_defined_<=-on-bits" <= <= "non_defined_<=-on-bitsvec")
    (>= >= "non_defined_>=-on-bits" >= >= "non_defined_<=-on-bitsvec")
    (< < "non_defined_<-on-bits" < < "non_defined_<=-on-bitsvec")
    (> > "non_defined_>-on-bits" > > "non_defined_<=-on-bitsvec")
    (bits bits bits bits bits bits)
    (bitn bitn bitn bitn bitn bitn)
    (round round round round round round)))


;; to change name of function by his constant
(defun change-to-const (name_func_or_type)
  (let ((name (if (consp name_func_or_type)
		  (car name_func_or_type)
		 name_func_or_type)))

   (case name
           ('and *and*)
           ('or *or*)
           ('not *not*)
           ('xor *xor*)
           ('nor *nor*)
           ('nand *nand*)
	   ('+ *+*)
	   ('- *-*)
	   ('* *x*)
	   ('= *=*)
	   ('equal *=*)
	   ('<= *<=*)
	   ('>= *>=*)
	   ('< *<*)
	   ('> *>*)

	   ('bits *bits*)
	   ('bitn *bitn*)
	   ('round *round*)

           ('integer *int*)
	   ('natural *int*)
           ('bit *bit*)
           ('signed *signed*)
           ('unsigned *unsigned*)
           ('bit_vector *bit_vector*)

	   ('others *others*)

           (otherwise name))))



(defun make-list-predicate (name-ent name-arch list_of_mem_elmts list_of_types)
  (if (endp list_of_mem_elmts)
    nil
    (if (consp (car list_of_types))
	(let* ((type (car list_of_types))
	       (kind (car type))
	       (size (caddr type))
	       (type_elmts (caddr type)))

	  (case kind
	      ('array
	       `( (true-listp
		   (,(make-name name-ent
				  name-arch
				  "-getst")
		    (quote ,(car list_of_mem_elmts))
		    st)
		  )
		  ( ,(make-name "array-" type_elmts "p")
		     (,(make-name name-ent
				  name-arch
				  "-getst")
		      (quote ,(car list_of_mem_elmts)) st)
		  )
		  (equal (length (,(make-name name-ent
					      name-arch
					      "-getst")
				  (quote ,(car list_of_mem_elmts))
				  st))
				 ,size)

		   ,@(make-list-predicate
		      name-ent
		      name-arch
		      (cdr list_of_mem_elmts)
		      (cdr list_of_types)))
	       )
	      ('std_logic_vector
	       `( (true-listp
		   (,(make-name name-ent
				  name-arch
				  "-getst")
		    (quote ,(car list_of_mem_elmts))
		    st)
		  )
		  ( ,(make-name "array-" "bit" "p")
		     (,(make-name name-ent
				  name-arch
				  "-getst")
		      (quote ,(car list_of_mem_elmts)) st)
		  )
		  (equal (length (,(make-name name-ent
					      name-arch
					      "-getst")
				  (quote ,(car list_of_mem_elmts))
				  st))
				 ,size)

		   ,@(make-list-predicate
		      name-ent
		      name-arch
		      (cdr list_of_mem_elmts)
		      (cdr list_of_types))))

	      ('bit_vector
	       `( (true-listp
		   (,(make-name name-ent
				  name-arch
				  "-getst")
		    (quote ,(car list_of_mem_elmts))
		    st)
		  )
		  ( ,(make-name "array-" "bit" "p")
		     (,(make-name name-ent
				  name-arch
				  "-getst")
		      (quote ,(car list_of_mem_elmts)) st)
		  )
		  (equal (length (,(make-name name-ent
					      name-arch
					      "-getst")
				  (quote ,(car list_of_mem_elmts))
				  st))
				 ,size)

		   ,@(make-list-predicate
		      name-ent
		      name-arch
		      (cdr list_of_mem_elmts)
		      (cdr list_of_types))))
))



       `( ( ,(make-namep (car list_of_types))
	      (,(make-name name-ent
			   name-arch
			   "-getst")
	       (quote ,(car list_of_mem_elmts)) st)
	   )
	   ,@(make-list-predicate
	      name-ent
	      name-arch
	      (cdr list_of_mem_elmts)
	      (cdr list_of_types))))))



(defun make-state-p (name-ent name_architecture
		     list_of_mem_elmts
		     list_of_types
		     channel state)
  (declare (xargs :stobjs state
		 :mode :program))

(fms "~%
(defun ~x0 (st)
 (declare (xargs :guard t))
      ~x1)~%"
(list
(cons #\0 (make-name name-ent name_architecture
	      "-stp"))
(cons #\1 (cons `and (append
		      (list '(true-listp st))
		      (list `(= (length st)
			       ,(length list_of_mem_elmts)))
		(make-list-predicate
		 name-ent
		 name_architecture
		 list_of_mem_elmts
		 list_of_types)))))
channel
state
nil))

; ---------- generating accessor getst and updater putst ------------

(defun make-next (list)
; This function generates a new list with next-name of values
  (if (endp list)
      nil
    (cons (make-name "" (car list) "+")
	  (make-next (cdr list))))
)

(defun make-list_of_component (list_of_configurations)
  (if (endp list_of_configurations)
      nil
    (let* ((comp_conf (car list_of_configurations))
	   (name-instance (car comp_conf)))
    (cons (make-name "" name-instance "")
	  (make-list_of_component (cdr list_of_configurations))))))


(defun make-getst-putst (name-ent name_arch
			 list_of_inputs
			 list_of_locals_signals
			 list_of_variables
			 list_of_outputs
			 list_of_configurations
			 channel
			 state)
(declare (xargs :stobjs state
		:mode :program))

(let ((total_list (append list_of_inputs
			  list_of_locals_signals
			  (make-next list_of_locals_signals)
			  list_of_variables
			  list_of_outputs
			  (make-next list_of_outputs)
			  (make-list_of_component list_of_configurations))))

(fms "
;; Accessor of the memory state : (getst var st) -> value~%
(defun ~x0 (var st)
  (declare (xargs :guard t)
	   (type ~x4 var)
           (type (satisfies true-listp) st))
  (nth (~x1 var) st))~%~%
;; Updater of the memory state : (putst ver new st) -> st~%
(defun ~x2 (var new st)
  (declare (xargs :guard t)
	   (type ~x4 var)
           (type (satisfies true-listp) st))
  (update-nth (~x1 var) new st))~%~%
;;;;;;;;;;;;;;"

(list
(cons #\0 (make-name name-ent name_arch "-getst"))
(cons #\1 (make-name name-ent name_arch "-get-nth"))
(cons #\2 (make-name name-ent name_arch "-putst"))
(cons #\3 (make-name name-ent name_arch "-stp"))
(cons #\4 (append (list 'member) total_list))
)

channel
state
nil)))



; -------------- generating function get-nth ----------------


(defun make-elmts-of-get-nth (list number)
  (if (endp list)
      nil
    `(((quote ,(car list))
		,number)
	  ,@(make-elmts-of-get-nth (cdr list)
				 (1+ number)))))



(defun make-get-nth (name-ent name_architecture
		     list_of_inputs
		     list_of_locals_signals
		     list_of_variables
		     list_of_outputs
		     list_of_configurations
		     channel state)
  (declare (xargs :stobjs state
		 :mode :program))
  (let ((number_of_elemts (+ (length list_of_inputs)
			    (length list_of_locals_signals)
			    (length (make-next
				     list_of_locals_signals))
			    (length list_of_variables)
			    (length list_of_configurations)
			    (length list_of_outputs)
			    (length (make-next
				       list_of_outputs))))
	(total_list (append list_of_inputs
			    list_of_locals_signals
			    (make-next list_of_locals_signals)
			    list_of_variables
			    list_of_outputs
			    (make-next list_of_outputs)
			    (make-list_of_component list_of_configurations))))

(fms "~%
;;; Function get-nth : gives the position of an element
;;; in the memory state ~%
(defun ~x0 (var)
   (declare (type ~x1 var)
	    (xargs :guard t))
   (the (integer 0 ~x2)
      ~x3))~%"

(list
(cons #\0 (make-name name-ent name_architecture
	      "-get-nth"))
(cons #\1 (append (list 'member) total_list))
(cons #\2 (1- number_of_elemts))
(cons #\3 (append (list 'case 'var)
		  (make-elmts-of-get-nth total_list 0))))
channel
state
nil)))


; ------------- generating macro make-state -----------------

(defun make-state-key-arg (list_of_mem_elmts list_of_types)
  (if (endp list_of_mem_elmts)
      nil
    (case (car list_of_types)
	  (positive
	   `( ( ,(car list_of_mem_elmts)
		'1
		)
	      ,@(make-state-key-arg (cdr list_of_mem_elmts)
				    (cdr list_of_types))))
	   (otherwise
	    `( ( ,(car list_of_mem_elmts)
		'0
		)
	      ,@(make-state-key-arg (cdr list_of_mem_elmts)
				    (cdr list_of_types)))))))


(defun make-state-key-arg2 (par_gen default_value)
  (if (endp par_gen)
      nil
    `( (,(car par_gen)
	(quote ,(car default_value))
       )
       ,@(make-state-key-arg2 (cdr par_gen)
			      (cdr default_value)))))

; (defun make-state-body (list_of_mem_elmts
; 			list_of_types)
;   (if (endp list_of_mem_elmts)
;       nil
;    ; (if (consp (car list_of_types))
;    ;	`( (defarray ,(nth 2 (car list_of_types))
; ;	             :initial-element
; ;		     ,(car list_of_mem_elmts)
; ;	   )
; ;	   ,@(make-state-body (cdr list_of_mem_elmts)
; ;			      (cdr list_of_types)))

;       `( ,(car list_of_mem_elmts)
; 	 ,@(make-state-body (cdr list_of_mem_elmts)
; 			      (cdr list_of_types)))))



(defun make-state-body-for-components (list_of_configurations)
  (if (endp list_of_configurations)
      nil
    (let* ((comp_conf (car list_of_configurations))
	   (name-ent (caddr comp_conf))
	   (name-arch (cadddr comp_conf)))
    `( (,(make-name name-ent name-arch "-make-state")
       )
       ,@(make-state-body-for-components
	  (cdr list_of_configurations))))))



(defun make-make-state (name-ent name_architecture
			list_of_mem_elmts
			list_of_types
			list_of_gener_param
			list_of_default_gene_para
			list_of_configurations
			 channel state)
  (declare (xargs :stobjs state
		 :mode :program))

(fms "~%
;;;;;;;  This macro make-state creates an initial state ;;;;;~%
(defun ~x0 () (let ~x1
  ~x2))"

(list (cons #\0 (make-name name-ent
			   name_architecture
			   "-make-state"))
      (cons #\1 (append (make-state-key-arg2 list_of_gener_param
					     list_of_default_gene_para)
			(make-state-key-arg list_of_mem_elmts list_of_types)
		        ))
      (cons #\2 (append (list 'list)
			(append list_of_gener_param
				list_of_mem_elmts
				)
			(make-state-body-for-components
			 list_of_configurations))))
channel
state
nil))


(defun make-update-state (name-ent name_architecture
			  channel state)
  (declare (xargs :stobjs state
		 :mode :program))

(fms "~%
;;;;;  This function updates memory state ;;;;~%
(defun ~x0 (key-arg st)
  (update-state-body ~x1 ~x2 key-arg st))"

(list
 (cons #\0 (make-name name-ent
		      name_architecture
		      "-update-st"))
 (cons #\1 (string name-ent))
 (cons #\2 (string name_architecture)))

channel
state
nil))



(defun make_name_var_sig (name bool)
  (if bool
      (make-name "" name "+")
    name))


(defun slice-rec2 (name-ent name_arch
		   name_var1
		   signed
		   max1
		   int1
		   int2
		   bool_sig_var)
  (declare (xargs :measure (nfix (- max1 int1))))
  (if (and (integerp max1) (integerp int1) (>= (- max1 int1) 0)
	   (integerp int2))
      (if (= (nfix (- max1 int1)) 0)

	  `((,(make-name name-ent name_arch "-putst")
	      (quote ,(make_name_var_sig name_var1
					 bool_sig_var))
	        (setarrayi ,int1
		          (logbit ,int2 ,signed)
				  (,(make-name name-ent
					       name_arch
					       "-getst")
				    (quote ,name_var1)
				   ,(make-onename "st"))
		       ,(make-onename "st"))
		,(make-onename "st")))



	`((,(make-name name-ent name_arch "-putst")
	      (quote ,(make_name_var_sig name_var1
					 bool_sig_var))
	    (setarrayi ,int1
		       (logbit ,int2
			       ,signed)
				  (,(make-name name-ent
					       name_arch
					       "-getst")
				   (quote ,name_var1)
					 ,(make-onename "st")))
	    ,(make-onename "st"))

	  ,@(slice-rec2 name-ent name_arch
			 name_var1
			 signed
			 max1
			 (1+ int1)
			 (1+ int2)
			 bool_sig_var)))
    (cw "Error on slice-rec2")))


(defun slice-rec1 (name-ent name_arch
		   name_var1
		   name_var2
		       max1
		       int1
		       max2
		       int2
		       bool_sig_var)
  (declare (xargs :measure (nfix (+ (- max1 int1)
				    (- max2 int2)))))
  (if (and (integerp max1) (integerp int1) (>= (- max1 int1) 0)
	   (integerp max2) (integerp int2) (>= (- max2 int2) 0))
      (if (and (= (nfix (- max1 int1)) 0)
	       (= (nfix (- max2 int2)) 0))

	  `((,(make-name name-ent name_arch "-putst")
	         (quote ,(make_name_var_sig name_var1
		 		            bool_sig_var))
	    (setarrayi ,int1
		       (getarrayi (,(make-name name-ent
					       name_arch
					       "-getst")
				    (quote ,name_var2)
				   ,(make-onename "st"))
				  ,int2)
		       (,(make-name name-ent name_arch "-getst")
			 (quote ,name_var1)
			,(make-onename "st")))
            ,(make-onename "st")))


 `((,(make-name name-ent name_arch "-putst")
      (quote ,(make_name_var_sig name_var1
				 bool_sig_var))
	    (setarrayi ,int1
		       (getarrayi (,(make-name name-ent
					       name_arch
					       "-getst")
				    (quote ,name_var2)
				   ,(make-onename "st"))
				  ,int2)
		       (,(make-name name-ent
				    name_arch
				    "-getst")
			(quote ,name_var1)
			,(make-onename "st")))

     ,(make-onename "st"))
   ,@(slice-rec1 name-ent name_arch
	         name_var1
	         name_var2
	         max1
	         (1+ int1)
	         max2
	         (1+ int2)
	         bool_sig_var)))
(cw "Error on slice-rec1")))


(defun slice-rec3 (name-ent name_arch
		   name_var1
		   name_var2
		   max1
		   int1
		   int2
		   bool_sig_var)
  (declare (xargs :measure (nfix (- max1 int1))))

  (if (and (integerp max1) (integerp int1) (>= (- max1 int1) 0)
	   (integerp int2))
      (if (and (= (nfix (- max1 int1)) 0))

	  `((,(make-name name-ent name_arch "-putst")
	        (quote ,(make_name_var_sig name_var1
					   bool_sig_var))
	      (setarrayi ,int1
			 (getarrayi (quote ,name_var2)
				    ,int2)
			 (,(make-name name-ent name_arch  "-getst")
				   (quote ,name_var1)
					 ,(make-onename "st")))
	      ,(make-onename "st")))


 `((,(make-name name-ent name_arch  "-putst")
      (quote ,(make_name_var_sig name_var1
				 bool_sig_var))
     (setarrayi ,int1
		(getarrayi (quote ,name_var2)
			   ,int2)
		(,(make-name name-ent name_arch "-getst")
		  (quote ,name_var1)
			,(make-onename "st")))
     ,(make-onename "st"))

   ,@(slice-rec3 name-ent name_arch
		 name_var1
		 name_var2
		 max1
		 (1+ int1)
		 (1+ int2)
		 bool_sig_var)))
(cw "Error on slice-rec3")))


(defun slice-rec4 (name-ent name_arch
		   name_var1
		   name_var2
		   max1
		   int1
		   symb
		   bool_sig_var)
  (declare (xargs :measure (nfix (- max1 int1))))

  (if (and (integerp max1) (integerp int1) (>= (- max1 int1) 0))
      (if (and (= (nfix (- max1 int1)) 0))

	  `((,(make-name name-ent name_arch "-putst")
	        (quote ,(make_name_var_sig name_var1
					   bool_sig_var))
	      (setarrayi ,int1
			 (getarrayi (quote ,name_var2)
				    ,symb)
			 (,(make-name name-ent name_arch  "-getst")
				   (quote ,name_var1)
					 ,(make-onename "st")))
	      ,(make-onename "st")))


 `((,(make-name name-ent name_arch  "-putst")
      (quote ,(make_name_var_sig name_var1
				 bool_sig_var))
     (setarrayi ,int1
		(getarrayi (quote ,name_var2)
			   ,symb)
		(,(make-name name-ent name_arch "-getst")
		  (quote ,name_var1)
			,(make-onename "st")))
     ,(make-onename "st"))

   ,@(slice-rec4 name-ent name_arch
		 name_var1
		 name_var2
		 max1
		 (1+ int1)
		 symb
		 bool_sig_var)))
(cw "Error on slice-rec4")))





;; function type_of
;; to  find the type of one element

(defun type_of (element list_of_mem_element list_of_types)
  (cond ((endp list_of_mem_element) "error on type_of")
        ((and (symbolp element) (equal element (car list_of_mem_element)))
          (change-to-const (car list_of_types)))
        ((and (symbolp element) (not (equal element (car list_of_mem_element))))
           (type_of element (cdr list_of_mem_element)
		                        (cdr list_of_types)))
        (t "error on type_of")))




(defun true_func (func$ rst)
   (cond ((consp rst) ;(list func$ type)
	  (cond ((not (equal (caddr rst) nil))
		 ;; operator with 3 args
		 (cons (list (nth (change-to-const (cdr (car rst)))
                            (nth (change-to-const func$) *true_function*))
                        (caar rst)
                        (car (cadr rst))
			(car (caddr rst)))
		       (change-to-const (cdr (car rst)))))

		 ((and (equal (caddr rst) nil)
		       (not (equal (cadr rst) nil)))
		  (cons (list (nth (change-to-const (cdr (car rst)))
				   (nth (change-to-const func$) *true_function*))
			      (caar rst)
			      (car (cadr rst)))
			(change-to-const (cdr (car rst)))))

		  (t (cons (list (nth (change-to-const (cdr (car rst)))
				   (nth (change-to-const func$) *true_function*))
			      (caar rst))
			(change-to-const (cdr (car rst)))))))


         (t "error on true_func")))


(mutual-recursion
(defun expand-exp (name-ent name_arch e list_of_mem_elmt list_types)
  (cond

   ((symbolp e)
      (cons `(,(make-name name-ent
		  name_arch
		  "-getst")
         (quote ,e)
	    ,(make-onename "st"))
          (type_of e list_of_mem_elmt list_types)))

   ((integerp e)
     (cons e *int*))

   ((consp e)
      (cond
	 ((equal (car e) 'index)
	  (cons `(getarrayi (,(make-name name-ent name_arch "-getst")
			     (quote ,(nth 1 e)) st) ,(nth 2 e))
                ;;(type_of (nth 1 e) list_of_mem_elmt list_types)))
		*bit*))

         (t ;; call of function$
           (true_func (car e)
	      (expand-exp-list name-ent name_arch (cdr e) list_of_mem_elmt list_types)))))

    (t "error in expand-exp")))


(defun expand-exp-list (name-ent name_arch list list_of_mem_elmt list_types)
  (if (consp list)
      (cons (expand-exp name-ent name_arch (car list) list_of_mem_elmt list_types)
	    (expand-exp-list name-ent name_arch (cdr list) list_of_mem_elmt list_types))
      nil)))

#|
(mutual-recursion
(defun expand-exp (name-ent name_arch e)
  (cond
   ((symbolp e)
    `(,(make-name name-ent
		  name_arch
		  "-getst")
      (quote ,e)
	    ,(make-onename "st")))
   ((consp e)
    (if (equal (car e) 'quote)
	e
        (cons (car e)
	      (expand-exp-list name-ent name_arch (cdr e)))))
   (t e)))
(defun expand-exp-list (name-ent name_arch list)
  (if (consp list)
      (cons (expand-exp name-ent name_arch (car list))
	    (expand-exp-list name-ent name_arch (cdr list)))
      nil)))
|#

(defun call_or_single_element (elmt)
  (cond ((consp elmt)
	 (cond ((= (length elmt) 1) (car elmt))
	       (t elmt)))
	(t elmt)))


(defun assignment (name-ent name_arch list bool_sig_var list_mem_elmt list_type)
  (cond ((and (consp (nth 0 list)) (equal (len (nth 0 list)) 4)
	      (consp (member 'index (nth 2 list)))
	      (equal (len (nth 2 list)) 4))
	 ; OP1 (deb1 to fin1) <= OP2 (deb2 to fin2)
         ; (index OP1 deb1 fin1) <= (index OP2 deb2 fin2)
	 (let ((op1 (cadr (nth 0 list)))
	       (op2 (cadr (nth 2 list)))
	       (deb1 (nth 2 (nth 0 list)))
	       (fin1 (nth 3 (nth 0 list)))
	       (deb2 (nth 2 (nth 2 list)))
	       (fin2 (nth 3 (nth 2 list))))
	   (slice-rec1 name-ent name_arch
		       op1
		       op2
		       fin1
		       deb1
		       fin2
		       deb2
		       bool_sig_var)))

	((and (consp (nth 0 list)) (equal (len (nth 0 list)) 4)
	      (consp (member 'index (nth 0 list)))
	      (consp (member 'index (nth 2 list)))
	      (equal (len (nth 2 list)) 3))

	 ; OP1 (deb1 to fin1) <= OP2 (fn i ..)
	 ; (index OP1 deb1 fin1) <= (index OP2 (fn i..))
	 (let* ((op1 (cadr (nth 0 list)))
	        (op2 (cadr (nth 2 list)))
	        (deb1 (nth 2 (nth 0 list)))
	        (fin1 (nth 3 (nth 0 list)))
		(range2 (nth 2 (nth 2 list))))

	   (slice-rec4  name-ent name_arch
			op1
			op2
			fin1
			deb1
			(call_or_single_element
                            (car (expand-exp name-ent
					name_arch
					range2
					list_mem_elmt
					list_type)))
			bool_sig_var)))

	((and (consp (nth 0 list)) (equal (len (nth 0 list)) 4)
	      (consp (member 'index (nth 0 list)))
	      (not (consp (member 'index (nth 2 list)))))
	      ; OP1 (deb1 to fin1) <= OP2
	      ; (index OP1 deb1 fin1) <= OP2
	      (let ((deb1 (nth 2 (nth 0 list)))
		    (fin1 (nth 3 (nth 0 list))))
		     (slice-rec1 name-ent name_arch
				  (nth 1 (nth 0 list))
				  (nth 2 list)
				  fin1
				  deb1
				  (- fin1 deb1)
				  0
				  bool_sig_var)))


	((and (consp (nth 0 list))
	      (consp (member 'index (nth 0 list)))
	      (equal (len (nth 0 list)) 3)
	      (consp (member 'index (nth 2 list)))
	      (equal (len (nth 2 list)) 3))

	 ; OP1 (fn i..) <= OP2 (fn i ..)
	 ; (index OP1 (fn i..)) <= (index OP2 (fn i..))
	 (let ((op1 (nth 1 (nth 0 list)))
	       (op2 (nth 1 (nth 2 list)))
	       (range1
   (call_or_single_element (car (expand-exp name-ent
				       name_arch
				       (nth 2 (nth 0 list))
				       list_mem_elmt
				       list_type))))
	       (range2
  (call_or_single_element (car (expand-exp name-ent
				      name_arch
				      (nth 2 (nth 2 list))
				      list_mem_elmt
				      list_type)))))
	   `((,(make-name name-ent name_arch "-putst")
	      (quote ,(make_name_var_sig op1
					 bool_sig_var))
	      (setarrayi ,range1
			 (getarrayi (quote ,op2)
				    ,range2)
			 (,(make-name name-ent name_arch  "-getst")
			  (quote ,op1)
			  ,(make-onename "st")))
	      ,(make-onename "st")))))


	((and (consp (nth 0 list))
	      (consp (member 'index (nth 0 list)))
	      (equal (len (nth 0 list)) 3)
	      (not (consp (member 'index (nth 2 list)))))

	 ; OP1 (fn i ..) <= (fn2 ...)
         ; (index OP1 (fn i ..)) <= (fn2 i ..)
	 (let ((op1 (nth 1 (nth 0 list)))
	       (op2 (car (expand-exp name-ent
				name_arch
				(nth 2 list)
				list_mem_elmt
				list_type)))
	       (range1
  (call_or_single_element (car (expand-exp name-ent
				      name_arch
				      (nth 2 (nth 0 list))
				      list_mem_elmt
				      list_type)))))
	   `((,(make-name name-ent name_arch "-putst")
	    (quote ,(make_name_var_sig op1
				       bool_sig_var))
	    (setarrayi ,range1
		       ,op2
		       (,(make-name name-ent name_arch  "-getst")
			(quote ,op1)
			,(make-onename "st")))
	    ,(make-onename "st")))))

	((and (consp (nth 0 list))
	      (consp (member 'index (nth 0 list)))
	      (equal (len (nth 0 list)) 4)
	      (integerp (nth 2 list)))

	 ; OP1 (deb1 to fin1) <= 4; <- signed-byte
	 ; (index OP1 deb1 fin1) <= 4
	 (let ((op1 (nth 1 (nth 0 list)))
	       (op2 (nth 2 list))
	       (deb1 (nth 2 (nth 0 list)))
	       (fin1 (nth 3 (nth 0 list))))

	   (slice-rec2 name-ent name_arch
			op1
			op2
			fin1
			deb1
			0
			bool_sig_var)))

;;
	((and (consp (nth 0 list))
	      (consp (member 'index (nth 0 list)))
	      (equal (len (nth 0 list)) 4)
	      (integerp (nth 2 list)))

	 ; OP1 (deb1 to fin1) <= (fn ....)
	 ; (index OP1 deb1 fin1) <= (fn ..)
	 (let* ((op1 (nth 1 (nth 0 list)))
		(op2 (car (expand-exp name-ent
				 name_arch
				 (nth 2 list)
				 list_mem_elmt
				 list_type)))
		(deb1 (nth 2 (nth 0 list)))
		(fin1 (nth 3 (nth 0 list)))
		(instr (slice-rec3 name-ent name_arch
				   op1
				   (make-onename "temp")
				   fin1
				   deb1
				   0
				   t)))
	 `((let ((,(make-onename "temp")
		     ,op2))
		    (seq st,@instr)
		    )
	       ,(make-onename "st"))))


       ((and (consp (nth 0 list))
	     (consp (member 'index (nth 0 list)))
	      (equal (len (nth 0 list)) 3)
	      (not (consp (member 'index (nth 0 list)))))
	 ; OP1 (fn i..) <= OP2;
	 ; (index OP1 (fn i ..)) <= OP2
	 (let ((op1 (nth 1 (car list)))
	       (op2 (car (expand-exp name-ent
				name_arch
				(nth 2 list)
				list_mem_elmt
				list_type)))
	       (range1 (car (expand-exp name-ent
				   name_arch
				   (nth 2 (car list))
				   list_mem_elmt
				   list_type))))

	   `((,(make-name name-ent name_arch "-putst")
	      (quote ,(make_name_var_sig op1
					 bool_sig_var))
	      (setarrayi ,(call_or_single_element range1)
			 ,op2
			 (,(make-name name-ent name_arch "-getst")
			  (quote ,op1)
			  ,(make-onename "st")))
	     ,(make-onename "st")))))


	((and (not (consp (nth 0 list)))
	      (equal (len (nth 2 list)) 4)
	      (consp (member 'index (nth 2 list))))
	 ; OP1<= OP2 (deb to fin)
         ; OP1 <= (index OP2 deb fin)
	 (let* ((op1 (car list))
		(op2 (nth 1 (nth 2 list)))
		(deb2 (nth 2 (nth 2 list)))
		(fin2 (nth 3 (nth 2 list))))
	   (slice-rec1 name-ent name_arch
			op1
			op2
			(- fin2 deb2)
			0
			fin2
			deb2
			bool_sig_var)))


	((and (not (consp (nth 0 list)))
	      (equal (len (nth 2 list)) 3)
	      (consp (member 'index (nth 2 list))))
	 ; OP1 <= OP2 (fn i..)
         ; OP1 <= (index OP2 (fn i..))
	 `((,(make-name name-ent name_arch "-putst")
	    (quote ,(make_name_var_sig (car list)
				       bool_sig_var))
	    (getarrayi (,(make-name name-ent
				    name_arch
				    "-getst")
			(quote ,(nth 1 (nth 2 list)))
			,(make-onename "st"))
		       ,(call_or_single_element (nth 2 (nth 2 list)))
	    ,(make-onename "st")))))


	((and (symbolp (car list)) (equal (length list) 3)
	      (symbolp (nth 2 list)))
	 ; A <= B
	 (let ((op1 (nth 0 list))
	       (op2 (nth 2 list)))
	  `((,(make-name name-ent name_arch "-putst")
	     (quote ,(make_name_var_sig op1
					bool_sig_var))
	     (,(make-name name-ent name_arch "-getst")
	      (quote ,op2)
	      ,(make-onename "st"))
	     ,(make-onename "st")))))


	((and (symbolp (car list)) (equal (length list) 3))
	 ; A <= (fn ...)
	 (let ((op1 (nth 0 list))
	       (op2 (car (expand-exp name-ent
				name_arch
				(nth 2 list)
				list_mem_elmt
				list_type))))
	   `((,(make-name name-ent name_arch "-putst")
	      (quote ,(make_name_var_sig op1
					 bool_sig_var))
	      ,op2
	      ,(make-onename "st")))))

	(t (cw "Syntax error on assignment"))))


(mutual-recursion
(defun if-statement (name-ent name-arch instr list_mem_elmt list_type)
  (if (endp instr)
      nil
	 (let* ((instr_then (let-instructions name-ent
					      name-arch
					      (caddr instr)
					      list_mem_elmt list_type))
	       (instr_else (let-instructions name-ent
					     name-arch
					     (cadddr instr)
					     list_mem_elmt list_type))
	       (cond_stat (car (list (car (expand-exp-list name-ent name-arch
						      (list (nth 1 instr))
						      list_mem_elmt list_type
						      ))))))

	   (cond
	    ((equal instr_then nil)

	      `((if ,(car cond_stat)
		    ,(make-onename "st")
		  (seq ,(make-onename "st")
		     ,@instr_else)))

	      )

	    ((equal instr_else nil)

	     `((if ,(car cond_stat)
		 (seq ,(make-onename "st")
		     ,@instr_then)
		   ,(make-onename "st"))))

	    (t

	     `((if ,(car cond_stat)
		 (seq ,(make-onename "st")
		      ,@instr_then)
		 (seq ,(make-onename "st")
		     ,@instr_else)
		   )))))))


(defun let-instructions (name-ent name-arch list list_mem_elmt list_type)
 (if (endp list)
      nil
    (let ((instr (car list)))
      (cond

       ; clause0 : "for" statement
       ((not (equal (member-eq 'for-loop instr)
		    nil))
	(let ((num (cadr instr)))

	`((,(make-name ""
			"for-loop"
			(natural-to-string num))
	    ,(make-onename "st"))
	     ,@(let-instructions name-ent name-arch
				 (cdr list) list_mem_elmt list_type)))

       ) ; end clause0


       ; clause1 : "if"  statement

       ((not (equal (member-eq 'if instr)
		     nil))

	(let ((new_list (if-statement name-ent
				      name-arch
				      instr
				      list_mem_elmt list_type)))

	 `(,@new_list
	     ,@(let-instructions name-ent name-arch
				 (cdr list) list_mem_elmt list_type)))


       ) ; end clause1

         ; clause2 : variable assignment
        ((not (equal (member-eq ':= instr)
		     nil))

	 (let ((new_list (assignment name-ent
				     name-arch
				     instr
				     nil
				     list_mem_elmt list_type)))


	 `(,@new_list
	     ,@(let-instructions name-ent name-arch
				 (cdr list) list_mem_elmt list_type)))


	 ) ; end clause2

	; clause2bis : C  variable assignment
        ((not (equal (member-eq '= instr)
		     nil))

	 (let ((new_list (assignment name-ent
				     name-arch
				     instr
				     nil
				     list_mem_elmt list_type)))


	 `(,@new_list
	     ,@(let-instructions name-ent name-arch
				 (cdr list) list_mem_elmt list_type)))


	 ) ; end clause2bis


	; clause3 : signal assignment
	((not (equal (member-eq '<= instr)
		     nil))

	 (let ((new_list (assignment name-ent
				     name-arch
				     instr
				     t
				     list_mem_elmt list_type)))


	 `(,@new_list
	     ,@(let-instructions name-ent name-arch
				 (cdr list) list_mem_elmt list_type)))

	 ) ; end clause3



  (t (cw "error on instruction ~p0~%" instr)))))))


(defun update-st-for-function (name-ent arch_name args)
  (let ((name_putst (make-name name-ent arch_name "-putst"))
	(name_make-state (make-name name-ent arch_name "-make-state")))
    (if (endp args)
	`(,name_make-state)
      `(,name_putst (quote ,(car args))
		     (list ,(car args))
	(,@(update-st-for-function name-ent arch_name (cdr args)))))))


(defun make-functions (list_of_functions
		       channel list_mem_elmt list_type
		       state)
  (declare (xargs :stobjs state
		 :mode :program))
  (if (endp list_of_functions)
      state
    (let* ((func (car list_of_functions))
	   (name (cadr_assoc_keyword :name func))
	   (arg (cadr_assoc_keyword :arg func))
	   (type_arg (cadr_assoc_keyword :type_args func))
	   (loc_var (cadr_assoc_keyword :local_variables func))
	   (loc_var_type (cadr_assoc_keyword :local_variables_type func))
	   (type_return (list (cadr_assoc_keyword :type_return func)))
	   (body (cadr_assoc_keyword :body func))
	   (new_name (intern (string name) "ACL2"))
	   (state
	    (fms "~%;*****  Modelisation of the VHDL function ~x0 *****
;Definition of the local memory state"
		 (list (cons #\0 new_name))
		 channel
		 state
		 nil))

	   (state
	    (make-get-nth 'fct- new_name
			  arg
			  nil
			  (append loc_var
				  (list 'return))
			  nil
			  nil
			  channel
			  state))

	   (state
	    (make-getst-putst 'fct- new_name
			      arg
			      nil
			      (cons 'return loc_var)
			      nil
			      nil
			      channel
			      state))
	   (state
	    (make-state-p 'fct- new_name
			  (append arg
				  loc_var
				  '(return))
			  (append type_arg
				  loc_var_type
				  type_return)
			  channel
			  state))
	   (state
	    (make-update-state 'fct- new_name
			       channel
			       state))
	   (state
	    (make-make-state 'fct- new_name
			     (append arg
				     loc_var
				     (list 'return))
			     (append type_arg
				     loc_var_type
				     type_return)
			     nil
			     nil
			     nil
			     channel
			     state))
	   (new_instr (let-instructions 'fct- new_name
					body list_mem_elmt list_type))
	   (state
(fms
"~% ;;; Main function
(defun ~x0 ~x1
  (let* ((st ~x3)
	 (st ~x4))
    ~x5))~%"
(list (cons #\0 new_name)
      (cons #\1 arg)
      (cons #\3 (update-st-for-function 'fct- new_name
					arg))
      (cons #\4 `(seq st ,@new_instr))
      (cons #\5 `(,(make-name 'fct- new_name "-getst")
		 'return
		  st)))
channel
state
nil)))
(make-functions (cdr list_of_functions)
		channel list_mem_elmt list_type
		state))))





(defun make-for-loop (name-ent name_arch
		      list_of_for
		      channel list_mem_elmt list_type
		      state)
  (declare (xargs :stobjs state
		 :mode :program))

  (let* ((instr_for (car list_of_for))
	 (name_for (car instr_for))
	 (range (cadr instr_for))
	 (range1 (car range))
	 (range2 (cadr range))
	 (instr (caddr instr_for))
	 (new_instr (let-instructions name-ent name_arch
				      instr list_mem_elmt list_type)))

    (if (< range1 range2)  ;; ascending loop

	(fms "~%;; This function represents the for loop ~x0 ;;;;;;;~%
(defun ~x0 (i j st)
  (declare (type integer i)
	   (type integer j)
	   (type cons st)
	   (xargs :measure (nfix (- j i))))
  (if (zp (- j i))
      st
(~x0 (1+ i)
     j
     ~x1))) ~%"
(list
(cons #\0 (make-onename name_for))
(cons #\1 `(seq st ,@new_instr)))
channel
state
nil)


(fms "~%;; This function represents the for loop ~x0 ;;;;;;;~%
(defun ~x0 (i j st)
  (declare (type integer i)
	   (type integer j)
	   (type cons st)
	   (xargs :measure (nfix (- i j))))
  (if (zp (- i j))
      st
    (~x0 (1+ i)
	  j
	 ~x1))) ~%"
(list
(cons #\0 (make-onename name_for))
(cons #\1 `(seq st ,@new_instr)))
channel
state
nil))))


(defun process-functions (name-ent name_architecture
			  list_of_process
		     channel list_mem_elmt list_type state)
  (declare (xargs :stobjs state
		 :mode :program))

(if (endp list_of_process)
    state
  (let* ((new_instr (let-instructions name-ent name_architecture
				      (cadr (car list_of_process))
				      list_mem_elmt list_type))
	(state
(fms "~%;; This function represents the process ~x0 ;;;;;;;~%
(defun ~x1 (st) ~x2)"
(list
(cons #\0 (caar list_of_process))
(cons #\1 (make-name (concatenate 'string
				  (string name-ent)
				  (string name_architecture)
				  "-")
		     (caar list_of_process)
	             "-cycle"))
;(cons #\2 (make-name name-ent name_architecture "-stp"))

(cons #\2 (append '(seq st) new_instr)))


channel
state
nil)))

(process-functions name-ent name_architecture
		   (cdr list_of_process)
		   channel list_mem_elmt list_type
		   state))))





(defun make-rec-comb-process (name-ent
			      name_arch
			      list_of_comb_process
			      channel
			      state)
  (declare (xargs :stobjs state
		  :mode :program))

  (if (endp list_of_comb_process)
      state

    (let* ((name_process (make-name (concatenate 'string
						 (string name-ent)
						 (string name_arch)
						 "-")
				    (string (caar list_of_comb_process))
				    "-cycle"))
       (state
    (fms "~%~%;;======== Function rec-comb-process ~x0  =====~%~%
(defun ~x0 (i st) ~%
  (cond ((zp i) st)
	(t (~x0 (1- i) ~x1)))) ~%"

(list
(cons #\0 (make-name (concatenate 'string
				  (string name-ent)
				  (string name_arch)
				  "-")
		     (string (caar list_of_comb_process))
	             "-rec-cycle"))
(cons #\1 (append '(seq st) (list (list name_process 'st)))))
channel
state
nil)))
(make-rec-comb-process name-ent
			name_arch
			(cdr list_of_comb_process)
			channel
			state))))



(defun let-update-signals (name-ent name-arch list)
  (if (endp list)
      nil
    (let ((sig (car list)))
      `((,(make-name name-ent name-arch
		      "-putst")
	  (quote ,sig)
		(,(make-name name-ent
			     name-arch
			     "-getst")
		 (quote ,(make-name ""
				   sig
				   "+"))
		         ,(make-onename "st"))
		,(make-onename "st"))
	,@(let-update-signals name-ent name-arch (cdr list))))))


(defun comp-updt_sig-rec_in (name-ent name-arch
			  name-comp name_instance
			  links_inputs)
  (if (endp links_inputs)
      nil
    (let ((name-putst-arch (make-name name-ent
				 name-arch
				 "-putst"))
	  (name-getst-arch (make-name name-ent
				 name-arch
				 "-getst"))
	  (name-putst-comp (make-name ""
				 name-comp
				 "-putst"))
          (first_link (car links_inputs)))
      (if (not (consp (car first_link)))

  ; architecture -> input of component
	  (let ((src (make-name "" (string (car first_link)) ""))
                (dest (cadr first_link)))

   `((,name-putst-arch
      (quote ,name_instance)
       (,name-putst-comp (quote ,dest)
			 (,name-getst-arch (quote ,src)
					   ,(make-onename "st"))
			 (,name-getst-arch (quote ,name_instance)
					   ,(make-onename "st")))
       ,(make-onename "st"))
     ,@(comp-updt_sig-rec_in name-ent name-arch
			 name-comp name_instance
			 (cdr links_inputs))))


	     ;; array -> one element

	  (let ((src (make-name "" (string (nth 1 (car first_link))) ""))
                (dest (make-name "" (string (nth 1 first_link)) "")))



   `((,name-putst-arch
      (quote ,name_instance)
       (,name-putst-comp (quote ,dest)
			 (getarrayi (,name-getst-arch (quote ,src)
						      ,(make-onename "st"))
				    ,(nth 2 (car first_link)))
			 (,name-getst-arch (quote ,name_instance)
					   ,(make-onename "st")))
       ,(make-onename "st"))
     ,@(comp-updt_sig-rec_in name-ent name-arch
			 name-comp name_instance
			 (cdr links_inputs))))))))



(defun comp-updt_sig-rec_out (name-ent name-arch
			  name-comp name_instance
			  links_outputs)
  (if (endp links_outputs)
      nil
    (let ((name-putst-arch (make-name name-ent
				 name-arch
				 "-putst"))
	  (name-getst-arch (make-name name-ent
				 name-arch
				 "-getst"))
	  (name-getst-comp (make-name ""
				 name-comp
				 "-getst"))
          (first_link (car links_outputs)))
      (if (not (consp (car first_link)))

	  (let ((dest (make-name "" (string (car first_link)) ""))
                (src (make-name "" (string (cadr first_link)) "")))

   `((,name-putst-arch
     (quote ,src)
     (,name-getst-comp (quote ,dest)
		       (,name-getst-arch (quote ,name_instance)
					 ,(make-onename "st")))
      ,(make-onename "st"))
    ,@(comp-updt_sig-rec_out name-ent name-arch
			 name-comp name_instance
			 (cdr links_outputs))))


	     ;; array -> one element

	  (let ((dest (make-name "" (string (nth 1 (car first_link))) ""))
                (src (make-name "" (string (nth 1 first_link)) "")))

   `((,name-putst-arch (quote ,dest)
	               (setarrayi ,(nth 2 (car first_link))
                          (,name-getst-comp (quote ,src)
					    (,name-getst-arch (quote ,name_instance)
					                   ,(make-onename "st")))
                            (,name-getst-arch (quote ,dest) ,(make-onename "st")))
	        ,(make-onename "st"))
    ,@(comp-updt_sig-rec_out name-ent name-arch
			 name-comp name_instance
			 (cdr links_outputs))))))))



(defun find_links_input (inputs links)
  (cond ((endp links) nil)
	(t
	 (let* ((one_links (car links))
		(one_sig (cadr one_links)))
	   (if (member one_sig inputs)
	       (cons (car links)
		     (find_links_input inputs (cdr links)))

	      (find_links_input inputs (cdr links)))))))



(defun find_links_output (outputs links)
  (cond ((endp links) nil)
	(t
	 (let* ((one_links (car links))
		(one_sig (cadr one_links)))
	   (if (member one_sig outputs)
	       (cons (car links)
		     (find_links_output outputs (cdr links)))

	      (find_links_output outputs (cdr links)))))))


(defun search_input_comp (name_comp list_of_components)
  (let ((car_list_of_components (car list_of_components)))
  (cond ((endp list_of_components) nil)
	((equal name_comp (car car_list_of_components))
	 (append (cadr_assoc_keyword :inputs_signals (cdr car_list_of_components))
		 (cadr_assoc_keyword :generic_parameters (cdr car_list_of_components))))
	(t (search_input_comp name_comp (cdr list_of_components))))))


(defun search_output_comp (name_comp list_of_components)
  (let ((car_list_of_components (car list_of_components)))
  (cond ((endp list_of_components) nil)
	((equal name_comp (car car_list_of_components))
	 (cadr_assoc_keyword :outputs_signals (cdr car_list_of_components)))
	(t (search_output_comp name_comp (cdr list_of_components))))))


(defun components-update-signals (name-ent name-arch
				  list_of_components
				  list_of_configurations
				  list_of_links)
  (if (endp list_of_configurations)
      nil
    (let* ((comp (car list_of_configurations))
	   (name_comp (make-name (caddr comp) (cadddr comp) ""))
           (name_instance (make-name "" (car comp) ""))
	   (name_component (cadr comp))

	   (inputs_of_comp (search_input_comp name_component list_of_components))
	   (outputs_of_comp (search_output_comp name_component list_of_components))

	   (links_inputs (find_links_input inputs_of_comp (cdr (car list_of_links))))
	   (links_outputs (find_links_output outputs_of_comp (cdr (car list_of_links))))

	   (new_list1 (comp-updt_sig-rec_in name-ent name-arch
					name_comp name_instance
					links_inputs))
	  (new_list2 (comp-updt_sig-rec_out name-ent name-arch
					name_comp name_instance
					links_outputs)))
      `(,@new_list1 ,@new_list2
	 ,@(components-update-signals name-ent name-arch
				      list_of_components
				    (cdr list_of_configurations)
				    (cdr list_of_links))))))


#|
(defun components-update-signals (name-ent name-arch
				  list_of_configurations
				  list_of_links)
  (if (endp list_of_configurations)
      nil
    (let* ((comp (car list_of_configurations))
	   (name_comp (make-name (cadr comp) (caddr comp) ""))
           (name_instance (make-name "" (car comp) ""))
	   (links_inputs (car (cdr (car list_of_links))))
	   (links_outputs (cadr (cdr (car list_of_links))))
	   (new_list1 (comp-updt_sig-rec_in name-ent name-arch
					name_comp name_instance
					links_inputs))
	  (new_list2 (comp-updt_sig-rec_out name-ent name-arch
					name_comp name_instance
					links_outputs)))
      `(,@new_list1 ,@new_list2
	 ,@(components-update-signals name-ent name-arch
				    (cdr list_of_configurations)
				    (cdr list_of_links))))))
|#


(defun make-update-of-components (name-ent name_arch list_of_configurations)
  (if (endp list_of_configurations)
      nil
    (let* ((comp (car list_of_configurations))
         (name_comp (make-name (caddr comp) (cadddr comp) ""))
         (name_instance (make-name "" (car comp) "")))
      `((,(make-name name-ent name_arch "-putst")
	      (quote ,name_instance)
	      (,(make-name "" name_comp "-update-signals")
		     (,(make-name name-ent name_arch "-getst")
		      (quote ,name_instance)
		      st))
	      st)
	  ,@(make-update-of-components name-ent name_arch (cdr list_of_configurations))))))

(defun make-update-signals (name-ent name_architecture
			    list_of_locals_signals_and_outputs
			    list_of_components
		            list_of_configurations
			    list_of_links
		            channel state)
  (declare (xargs :stobjs state
		  :mode :program))

  (let* ((newinstr1 (let-update-signals name-ent name_architecture
				       list_of_locals_signals_and_outputs))
	 (newinstr2 (make-update-of-components name-ent name_architecture list_of_configurations))
	(newinstr3 (components-update-signals name-ent name_architecture
					      list_of_components
					      list_of_configurations
					      list_of_links)))

  (fms "~%~%;;======== Function update of signals  =====~%~%
(defun ~x0 (st) ~x2) ~%"
; (declare (xargs :guard (~x1 st)))
(list
(cons #\0 (make-name name-ent name_architecture "-update-signals"))
(cons #\1 (make-name name-ent name_architecture "-stp"))
(cons #\2 (append '(seq st)
		  newinstr1
	          newinstr2
		  newinstr3)))

channel
state
nil)))


(defun name_of_process (name-ent name-arch list_of_process)
  (if (endp list_of_process)
      nil
    `((,(make-name (concatenate 'string
    			   (string name-ent)
			       (string name-arch)
			       "-")
		  (caar list_of_process)
		  "-cycle")
       ,(make-onename "st"))
	  ,@(name_of_process name-ent name-arch (cdr list_of_process)))))



(defun name_of_conc_statem (name-ent name-arch list_of_conc_stat)
  (if (endp list_of_conc_stat)
      nil
    (let ((name-update-sig (make-name name-ent name-arch "-update-signals")))
    `((,name-update-sig (,(make-name (concatenate 'string
    			   (string name-ent)
			       (string name-arch)
			       "-")
		  (caar list_of_conc_stat)
		  "-cycle")
       ,(make-onename "st")))
	  ,@(name_of_conc_statem name-ent name-arch (cdr list_of_conc_stat))))))



(defun make-cycle-of-components (name-ent name_arch list_of_configurations)
  (if (endp list_of_configurations)
      nil
    (let* ((comp (car list_of_configurations))
	   (name_comp (make-name (caddr comp) (cadddr comp) ""))
           (name_instance (make-name "" (car comp) "")))
      `((,(make-name name-ent name_arch "-putst")
	      (quote ,name_instance)
	      (,(make-name "" name_comp "-cycle")
		     (,(make-name name-ent name_arch "-getst")
		      (quote ,name_instance)
		      st))
	      st)
	  ,@(make-cycle-of-components name-ent name_arch (cdr list_of_configurations))))))



(defun make-rec-concurrent-stat (name-ent name_architecture
				 concurrent-stat channel state)
     (declare (xargs :stobjs state
		  :mode :program))
  (cond ((endp concurrent-stat) state)
	(t (let ((name_conc (name_of_conc_statem name-ent name_architecture concurrent-stat)))

(fms "~%~%;;======== Function rec-concurrent-stat  =====~%~%
(defun ~x0 (i st) ~%
  (cond ((zp i) st)
	(t (~x0 (1- i) ~x1)))) ~%"

(list
(cons #\0 (make-name name-ent name_architecture "-rec-conc-stat"))
(cons #\1 (append '(seq st) name_conc)))
channel
state
nil)))))




(defun form-rec-concurrent-stat-name (name-ent name_architecture i)
  `((,(make-name "" (concatenate 'string
                   (string name-ent)
			       (string name_architecture)
			       "-")
		  "rec-conc-stat")
    ,i
       ,(make-onename "st"))))


(defun number_of_signal_ass (list)
  (cond ((endp list) 0)
	(t
	 (if (consp (member '<= (car list)))
	     (1+ (number_of_signal_ass (cdr list)))
	   (number_of_signal_ass (cdr list))))))

;; form call to recursive functions for comb process
(defun make-call-of-comp (name-ent name_arch list_of_comb_process)
  (cond ((endp list_of_comb_process)
	 nil)
	 (t
	  (let ((number_of_instr (number_of_signal_ass (cadar list_of_comb_process)))
	       (name-func (make-name (concatenate 'string
						  (string name-ent)
						  (string name_arch)
						  "-")
				     (caar list_of_comb_process)
				     "-rec-cycle")))
	   `((,name-func ,number_of_instr st)
	     ,@(make-call-of-comp name-ent
				  name_arch
				  (cdr list_of_comb_process)))))))



(defun make-cycle (name-ent name_architecture
		   list_of_process
		   list_of_comb_process
		   list_of_configurations
		   nat
		   channel
		   state)
    (declare (xargs :stobjs state
		  :mode :program))
    (let ((list (name_of_process name-ent name_architecture
				 list_of_process))
	  (comp (make-cycle-of-components name-ent name_architecture list_of_configurations))
	  (rec-conc-stat (if (equal nat nil)
			     nil
			   (form-rec-concurrent-stat-name name-ent
			  name_architecture nat)))
	  (make-call-of-comb-process (make-call-of-comp name-ent
							 name_architecture
							 list_of_comb_process))
	  )




(fms "~%~%;;======== Function cycle of simulation  =====~%~%
(defun ~x0 (st) ~% ~x2) ~%"

(list
(cons #\0 (make-name name-ent name_architecture "-cycle"))
(cons #\2 (append '(seq st) list rec-conc-stat make-call-of-comb-process comp)))
channel
state
nil)
))


(defun make-simul (name-ent name_architecture
		   channel
		   state)

  (declare (xargs :stobjs state
		  :mode :program))

  (let ((name-simul (make-name name-ent name_architecture "-simul"))
	(name-cycle (make-name name-ent name_architecture "-cycle"))
	(name-update-sig (make-name name-ent name_architecture "-update-signals")))

  (fms "~%~%;;======== Function for N simulation cycles  =====~%~%
(defun ~x0 (n st)
   (if (zp n)
      st
    ~x2)) ~%~%~%~% "
; (declare (xargs :guard (~x1 st)))
(list
(cons #\0 name-simul)
(cons #\1 (make-name name-ent name_architecture "-stp"))
(cons #\2 `(,name-simul (1- n)
			(,name-cycle
			 (,name-update-sig st)))))

channel
state
nil)))