Source file michelson.ml
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
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
open Ident
open Tools
type 'a with_annot = {
node: 'a;
annotation: ident option;
}
[@@deriving show {with_path = false}]
type prim = {
prim: ident;
args: obj_micheline list;
annots: ident list;
}
[@@deriving yojson, show {with_path = false}]
and obj_micheline =
| Oprim of prim
| Ostring of string
| Obytes of string
| Oint of string
| Oarray of obj_micheline list
| Ovar of obj_micheline_var
[@@deriving yojson, show {with_path = false}]
and obj_micheline_var =
| OMVfree of ident
| OMVint of ident * bool
| OMVstring of ident
| OMVbytes of ident
| OMVif of ident * obj_micheline * obj_micheline
[@@deriving yojson, show {with_path = false}]
type type_node =
| Taddress
| Tbig_map of type_ * type_
| Tbool
| Tbytes
| Tchain_id
| Tcontract of type_
| Tint
| Tkey
| Tkey_hash
| Tlambda of type_ * type_
| Tlist of type_
| Tmap of type_ * type_
| Tmutez
| Tnat
| Toperation
| Toption of type_
| Tor of type_ * type_
| Tpair of type_ list
| Tset of type_
| Tsignature
| Tstring
| Ttimestamp
| Tunit
| Tticket of type_
| Tsapling_state of int
| Tsapling_transaction of int
| Tbls12_381_fr
| Tbls12_381_g1
| Tbls12_381_g2
| Tnever
| Tchest
| Tchest_key
| Ttx_rollup_l2_address
[@@deriving show {with_path = false}]
and type_ = type_node with_annot
[@@deriving show {with_path = false}]
type data =
| Dint of Core.big_int
| Dstring of string
| Dbytes of string
| Dunit
| Dtrue
| Dfalse
| Dpair of data list
| Dleft of data
| Dright of data
| Dsome of data
| Dnone
| Dlist of data list
| Delt of data * data
| Dvar of ident * type_ * bool
| DIrCode of ident * instruction
| Dcode of code
| Dlambda_rec of code
[@@deriving show {with_path = false}]
and code_node =
| SEQ of code list
| APPLY
| EXEC
| FAILWITH
| IF of code list * code list
| IF_CONS of code list * code list
| IF_LEFT of code list * code list
| IF_NONE of code list * code list
| ITER of code list
| LAMBDA of type_ * type_ * code list
| LOOP of code list
| LOOP_LEFT of code list
| DIG of int
| DIP of int * code list
| DROP of int
| DUG of int
| DUP
| DUP_N of int
| PUSH of type_* data
| SWAP
| ABS
| ADD
| COMPARE
| EDIV
| EQ
| GE
| GT
| NAT
| INT
| BYTES
| ISNAT
| LE
| LSL
| LSR
| LT
| MUL
| NEG
| NEQ
| SUB
| SUB_MUTEZ
| AND
| NOT
| OR
| XOR
| BLAKE2B
| CHECK_SIGNATURE
| HASH_KEY
| KECCAK
| PAIRING_CHECK
| SAPLING_EMPTY_STATE of int
| SAPLING_VERIFY_UPDATE
| SHA256
| SHA512
| SHA3
| ADDRESS
| AMOUNT
| BALANCE
| CHAIN_ID
| CONTRACT of type_ * ident option
| CREATE_CONTRACT of obj_micheline
| EMIT of type_ * ident option
| IMPLICIT_ACCOUNT
| LEVEL
| MIN_BLOCK_TIME
| NOW
| SELF of ident option
| SELF_ADDRESS
| SENDER
| SET_DELEGATE
| SOURCE
| TOTAL_VOTING_POWER
| TRANSFER_TOKENS
| VOTING_POWER
| CAR
| CDR
| CONCAT
| CONS
| EMPTY_BIG_MAP of type_ * type_
| EMPTY_MAP of type_ * type_
| EMPTY_SET of type_
| GET
| GET_N of int
| GET_AND_UPDATE
| LEFT of type_
| MAP of code list
| MEM
| NEVER
| NIL of type_
| NONE of type_
| PACK
| PAIR
| PAIR_N of int
| RIGHT of type_
| SIZE
| SLICE
| SOME
| UNIT
| UNPACK of type_
| UNPAIR
| UNPAIR_N of int
| UPDATE
| UPDATE_N of int
| JOIN_TICKETS
| READ_TICKET
| SPLIT_TICKET
| TICKET
| CAST of type_
| RENAME
| VIEW of ident * type_
| OPEN_CHEST
| CAR_N of int
| CDR_N of int
[@@deriving show {with_path = false}]
and code = { node : code_node; type_: (type_ list) option ref; }
and z_operator =
| Znow
| Zamount
| Zbalance
| Zsource
| Zsender
| Zaddress
| Zchain_id
| Zself of ident option
| Zself_address
| Znone of type_
| Zunit
| Znil of type_
| Zemptyset of type_
| Zemptymap of type_ * type_
| Zemptybigmap of type_ * type_
| Ztotalvotingpower
| Zlevel
| Zsapling_empty_state of int
| Zmin_block_time
[@@deriving show {with_path = false}]
and un_operator =
| Ucar
| Ucdr
| Uleft of type_
| Uright of type_
| Uneg
| Unat
| Uint
| Ubytes
| Unot
| Uabs
| Uisnat
| Usome
| Usize
| Upack
| Uunpack of type_
| Ublake2b
| Usha256
| Usha512
| Usha3
| Ukeccak
| Uhash_key
| Ufail
| Ucontract of type_ * ident option
| Usetdelegate
| Uimplicitaccount
| Ueq
| Une
| Ugt
| Uge
| Ult
| Ule
| Uvotingpower
| Ureadticket
| Ujointickets
| Upairing_check
| Uconcat
| Uaddress
| UcarN of int
| UcdrN of int
| UforcePair
| Uemit of type_ * ident option
[@@deriving show {with_path = false}]
and bin_operator =
| Badd
| Bsub
| Bmul
| Bediv
| Blsl
| Blsr
| Bor
| Band
| Bxor
| Bcompare
| Bget
| Bmem
| Bconcat
| Bcons
| Bpair
| Bexec
| Bapply
| Bcreateticket
| Bsplitticket
| Bsapling_verify_update
| Bview of ident * type_
| Bsubmutez
[@@deriving show {with_path = false}]
and ter_operator =
| Tcheck_signature
| Tslice
| Tupdate
| Ttransfer_tokens
| Topen_chest
| Tcreate_contract of obj_micheline
[@@deriving show {with_path = false}]
and g_operator = [`Zop of z_operator | `Uop of un_operator | `Bop of bin_operator | `Top of ter_operator ]
[@@deriving show {with_path = false}]
and cmp_operator =
| Ceq
| Cne
| Cgt
| Cge
| Clt
| Cle
[@@deriving show {with_path = false}]
and builtin =
| Bmin of type_
| Bmax of type_
| Bfloor
| Bceil
| BlistContains of type_
| BlistNth of type_
| BlistHead of type_
| BlistTail of type_
| Bnattostring
| Bbytestonat
| Bnattobytes
| Bratcmp
| Bratnorm
| Brataddsub
| Bratdiv
| Bratmul
| Bratuminus
| Bratabs
| Brattez
| Bratdur
| Bmuteztonat
| Bsimplify_rational
[@@deriving show {with_path = false}]
and klv =
| KLVoption of type_
| KLVmap of (type_ * instruction)
[@@deriving show {with_path = false}]
and access_item = {
ai_index: int;
ai_length: int
}
[@@deriving show {with_path = false}]
and access_value = {
av_ident : ident;
av_path: access_item list;
av_source_no_dup: bool;
av_value_no_dup: bool;
}
[@@deriving show {with_path = false}]
and instruction =
| Iseq of instruction list
| IletIn of ident * instruction * instruction * bool
| Ivar_access of access_value
| Icall of ident * instruction list * bool
| Iassign of ident * instruction
| Iassigntuple of ident * int * int * instruction
| Iif of instruction * instruction * instruction * type_
| Iifnone of instruction * instruction * ident * instruction * type_
| Iifleft of instruction * ident * instruction * ident * instruction * type_
| Iifcons of instruction * ident * ident * instruction * instruction * type_
| Iloop of instruction * instruction
| Iiter of ident list * instruction * instruction
| Iloopleft of instruction * ident * instruction
| Ilambda of type_ * ident * type_ * instruction
| Izop of z_operator
| Iunop of un_operator * instruction
| Ibinop of bin_operator * instruction * instruction
| Iterop of ter_operator * instruction * instruction * instruction
| Iupdate of ukind * aoperator
| Iconst of type_ * data
| Icompare of cmp_operator * instruction * instruction
| Iset of type_ * instruction list
| Ilist of type_ * instruction list
| Imap of bool * type_ * type_ * (instruction * instruction) list
| Irecord of ritem
| Irecupdate of instruction * ruitem
| Imap_ of instruction * ident * instruction
| Ifold of ident * ident option * ident * instruction * instruction * instruction
| Ireverse of type_ * instruction
| Imichelson of instruction list * code * ident list
| Iwildcard of type_ * ident
| Ireplace of ident * ident * klv * instruction
| Ireadticket of instruction
[@@deriving show {with_path = false}]
and ritem =
| Rtuple of instruction list
| Rnodes of ritem list
[@@deriving show {with_path = false}]
and ruitem =
| RUnodes of int * (int * ruitem) list
| RUassign of int * (int * instruction) list
[@@deriving show {with_path = false}]
and aoperator =
| Aunop of un_operator
| Abinop of bin_operator * instruction
| Aterop of ter_operator * instruction * instruction
[@@deriving show {with_path = false}]
and ukind =
| Uvar of ident
| Urec of ident * (int * int) list
type implem =
| Concrete of (ident * type_) list * instruction
| Abstract of builtin
[@@deriving show {with_path = false}]
type ctx_func = {
args: (ident * type_) list;
stovars: ident list;
}
[@@deriving show {with_path = false}]
type func = {
name: ident;
targ: type_;
tret: type_;
ctx: ctx_func;
body: implem;
}
[@@deriving show {with_path = false}]
type entry = {
name: ident;
args: (ident * type_) list;
eargs: (ident * type_) list;
body: instruction;
}
[@@deriving show {with_path = false}]
type ir = {
name: ident;
storage_type: type_;
storage_data : data;
storage_list: (ident * type_ * data) list;
with_operations: bool;
parameter: type_;
funs: func list;
views: func list;
offchain_views: func list;
entries: entry list;
parameters: ident list;
}
[@@deriving show {with_path = false}]
type view_struct = {
id: ident;
param: type_;
ret: type_;
body: code;
}
[@@deriving show {with_path = false}]
type michelson = {
storage: type_;
parameter: type_;
code: code;
views: view_struct list;
parameters: ident list;
}
[@@deriving show {with_path = false}]
type micheline = {
code: obj_micheline list;
storage: obj_micheline;
parameters: ident list;
views: obj_micheline list;
}
[@@deriving show {with_path = false}]
type annotation_struct = {
name: string;
description: string;
}
[@@deriving show {with_path = false}]
type michelson_storage_view_struct = {
code: obj_micheline;
parameter: obj_micheline option;
returnType: obj_micheline option;
annotations: annotation_struct list;
version: string option;
}
[@@deriving show {with_path = false}]
type rest_api_query_struct = {
specificationUri: string;
baseUri: string;
path: string;
}
[@@deriving show {with_path = false}]
type offchain_view_implem_kind =
| OVIKMichelsonStorageView of michelson_storage_view_struct
| OVIKRestApiQuery of rest_api_query_struct
[@@deriving show {with_path = false}]
type offchain_view = {
name: ident;
implementations: offchain_view_implem_kind list;
}
[@@deriving show {with_path = false}]
type dvar = [`VLocal of int | `VGlobal of ident]
and dexpr =
| Dvar of dvar
| Depair of dexpr * dexpr
| Ddata of type_ * data
| Dfun of g_operator * dexpr list
[@@deriving show {with_path = false}]
type dinstr =
| DIAssign of dvar * dexpr
| DIIf of dexpr * (dcode * dcode)
| DIMatch of dexpr * (ident * dpattern list * dcode) list
| DIFailwith of dexpr
| DIWhile of dexpr * dcode
| DIIter of dtyvar * dexpr * dcode
| DILoop of dtyvar * dcode
and dtyvar = dvar
and dpattern =
| DVar of int
| DPair of dpattern * dpattern
and dcode = dinstr list
[@@deriving show {with_path = false}]
type dprogram = {
name: ident;
storage: type_;
parameter: type_;
storage_data: data;
code: dcode;
procs: (string * (string * type_) list * dcode) list;
}
[@@deriving show {with_path = false}]
type rstack1 = [dvar | `Paired of rstack1 * rstack1]
[@@deriving show {with_path = false}]
type rstack = rstack1 list
[@@deriving show {with_path = false}]
let mk_type ?annotation node : type_ =
{ node; annotation }
let mk_ctx_func ?(args = []) ?(stovars = []) _ : ctx_func =
{ args; stovars }
let mk_code ?type_ node : code =
{ node; type_ = ref type_ }
let mk_func name targ tret ctx body : func =
{ name; targ; tret; ctx; body }
let mk_entry name args eargs body : entry =
{ name; args; eargs; body }
let mk_ir ?(parameters = []) name storage_type storage_data storage_list ?(with_operations = false) parameter funs views offchain_views entries : ir =
{ name; storage_type; storage_data; storage_list; with_operations; parameter; funs; views; offchain_views; entries; parameters }
let mk_view_struct id param ret body : view_struct =
{ id; param; ret; body }
let mk_michelson ?(parameters = []) storage parameter ?(views = []) code : michelson =
{ storage; parameter; code; views; parameters }
let mk_prim ?(args=[]) ?(annots=[]) prim : prim =
{ prim; args; annots }
let mk_micheline ?(parameters = []) ?(views = []) code storage : micheline =
{ code; storage; parameters; views }
let mk_dprogram ?(procs = []) storage parameter storage_data name code =
{ name; storage; parameter; storage_data; code; procs }
let toperation = mk_type Toperation
let tunit = mk_type Tunit
let tstring = mk_type Tstring
let tnat = mk_type Tnat
let tint = mk_type Tint
let tbool = mk_type Tbool
let tmutez = mk_type Tmutez
let taddress = mk_type Taddress
let ttimestamp = mk_type Ttimestamp
let tchain_id = mk_type Tchain_id
let tbytes = mk_type Tbytes
let tpair l = mk_type (Tpair l)
let tor t1 t2 = mk_type (Tor (t1, t2))
let trat = tpair [tint; tnat]
let tlist t = mk_type (Tlist t)
let tset t = mk_type (Tlist t)
let tmap t1 t2 = mk_type (Tmap (t1, t2))
let tlambda t1 t2 = mk_type (Tlambda (t1, t2))
let toption t = mk_type (Toption t)
let tcontract t = mk_type (Tcontract t)
let tkey_hash = mk_type (Tkey_hash)
let tticket t = mk_type (Tticket t)
let ivar id = Ivar_access {av_ident = id; av_path = []; av_source_no_dup = false; av_value_no_dup = false}
let itrue = Iconst (tbool, Dtrue)
let ifalse = Iconst (tbool, Dfalse)
let iint n = Iconst (tint, Dint n)
let inat n = Iconst (tnat, Dint n)
let istring s = Iconst (tstring, Dstring s)
let imutez v = Iconst (tmutez, Dint v)
let isome s = Iunop (Usome, s)
let inone t = Izop (Znone t)
let iunit = Izop Zunit
let inil t = Izop (Znil t)
let iemptyset t = Izop (Zemptyset t)
let iemptymap k v = Izop (Zemptymap (k, v))
let iemptybigmap k v = Izop (Zemptybigmap (k, v))
let icar x = Iunop (Ucar, x)
let icdr x = Iunop (Ucdr, x)
let ifail msg = Iunop (Ufail, istring msg)
let ifaild data = Iunop (Ufail, data)
let iskip = Iseq []
let ileft t x = Iunop (Uleft t, x)
let iright t x = Iunop (Uright t, x)
let ieq l r = Icompare (Ceq, l, r)
let iadd l r = Ibinop (Badd, l, r)
let isub l r = Ibinop (Bsub, l, r)
let isub_mutez l r = Ibinop (Bsubmutez, l, r)
let imul l r = Ibinop (Bmul, l, r)
let idiv l r = Iifnone (Ibinop (Bediv, l, r), ifail "DIV_BY_ZERO", "_var_ifnone", icar (ivar "_var_ifnone"), tint )
let imod l r = Iifnone (Ibinop (Bediv, l, r), ifail "DIV_BY_ZERO", "_var_ifnone", icdr (ivar "_var_ifnone"), tnat )
let irecord ir = Irecord ir
let isrecord l = irecord (Rtuple l)
let ipair x y = Ibinop (Bpair, x, y)
let icarn n x = Iunop (UcarN n, x)
let icdrn n x = Iunop (UcdrN n, x)
let ctrue = mk_code (PUSH (mk_type Tbool, Dtrue))
let cfalse = mk_code (PUSH (mk_type Tbool, Dfalse))
let cint n = mk_code (PUSH (mk_type Tint, Dint n))
let cnat n = mk_code (PUSH (mk_type Tnat, Dint n))
let cstring s = mk_code (PUSH (mk_type Tstring, Dstring s))
let cfail msg = mk_code (SEQ [mk_code (PUSH (mk_type Tstring, Dstring msg)); mk_code FAILWITH])
let cskip = mk_code (SEQ [])
let cseq a = mk_code (SEQ a)
let capply = mk_code APPLY
let cexec = mk_code EXEC
let cfailwith = mk_code FAILWITH
let cif (a, b) = mk_code (IF (a, b))
let cifcons (a, b) = mk_code (IF_CONS (a, b))
let cifleft (a, b) = mk_code (IF_LEFT (a, b))
let cifnone (a, b) = mk_code (IF_NONE (a, b))
let citer a = mk_code (ITER a)
let clambda (a, b, c) = mk_code (LAMBDA (a, b, c))
let cloop a = mk_code (LOOP a)
let cloop_left a = mk_code (LOOP_LEFT a)
let cdig a = mk_code (DIG a)
let cdip (a, b) = mk_code (DIP (a, b))
let cdrop a = mk_code (DROP a)
let cdug a = mk_code (DUG a)
let cdup = mk_code DUP
let cdup_n a = mk_code (DUP_N a)
let cpush (a, b) = mk_code (PUSH (a, b))
let cswap = mk_code SWAP
let cabs = mk_code ABS
let cadd = mk_code ADD
let ccompare = mk_code COMPARE
let cediv = mk_code EDIV
let ceq = mk_code EQ
let cge = mk_code GE
let cgt = mk_code GT
let cnat = mk_code NAT
let cint = mk_code INT
let cbytes = mk_code BYTES
let cisnat = mk_code ISNAT
let cle = mk_code LE
let clsl = mk_code LSL
let clsr = mk_code LSR
let clt = mk_code LT
let cmul = mk_code MUL
let cneg = mk_code NEG
let cneq = mk_code NEQ
let csub = mk_code SUB
let csub_mutez = mk_code SUB_MUTEZ
let cand = mk_code AND
let cnot = mk_code NOT
let cor = mk_code OR
let cxor = mk_code XOR
let cblake2b = mk_code BLAKE2B
let ccheck_signature = mk_code CHECK_SIGNATURE
let chash_key = mk_code HASH_KEY
let ckeccak = mk_code KECCAK
let cpairing_check = mk_code PAIRING_CHECK
let csapling_empty_state a = mk_code (SAPLING_EMPTY_STATE a)
let csapling_verify_update = mk_code SAPLING_VERIFY_UPDATE
let csha256 = mk_code SHA256
let csha512 = mk_code SHA512
let csha3 = mk_code SHA3
let caddress = mk_code ADDRESS
let camount = mk_code AMOUNT
let cbalance = mk_code BALANCE
let cchain_id = mk_code CHAIN_ID
let ccontract (a, b) = mk_code (CONTRACT (a, b))
let ccreate_contract c = mk_code (CREATE_CONTRACT c)
let cemit (a, b) = mk_code (EMIT (a, b))
let cimplicit_account = mk_code IMPLICIT_ACCOUNT
let clevel = mk_code LEVEL
let cmin_block_time = mk_code MIN_BLOCK_TIME
let cnow = mk_code NOW
let cself a = mk_code (SELF a)
let cself_address = mk_code SELF_ADDRESS
let csender = mk_code SENDER
let cset_delegate = mk_code SET_DELEGATE
let csource = mk_code SOURCE
let ctotal_voting_power = mk_code TOTAL_VOTING_POWER
let ctransfer_tokens = mk_code TRANSFER_TOKENS
let cvoting_power = mk_code VOTING_POWER
let ccar = mk_code CAR
let ccdr = mk_code CDR
let cconcat = mk_code CONCAT
let ccons = mk_code CONS
let cempty_big_map (a, b) = mk_code (EMPTY_BIG_MAP (a, b))
let cempty_map (a, b) = mk_code (EMPTY_MAP (a, b))
let cempty_set a = mk_code (EMPTY_SET a)
let cget = mk_code GET
let cget_n n = mk_code (GET_N n)
let cget_and_update = mk_code GET_AND_UPDATE
let cleft a = mk_code (LEFT a)
let cmap a = mk_code (MAP a)
let cmem = mk_code MEM
let cnever = mk_code NEVER
let cnil a = mk_code (NIL a)
let cnone a = mk_code (NONE a)
let cpack = mk_code PACK
let cpair = mk_code PAIR
let cpair_n n = mk_code (PAIR_N n)
let cright a = mk_code (RIGHT a)
let csize = mk_code SIZE
let cslice = mk_code SLICE
let csome = mk_code SOME
let cunit = mk_code UNIT
let cunpair = mk_code UNPAIR
let cunpair_n n = mk_code (UNPAIR_N n)
let cunpack a = mk_code (UNPACK a)
let cupdate = mk_code UPDATE
let cupdate_n n = mk_code (UPDATE_N n)
let cjoin_tickets = mk_code JOIN_TICKETS
let cread_ticket = mk_code READ_TICKET
let csplit_ticket = mk_code SPLIT_TICKET
let cticket = mk_code TICKET
let ccast a = mk_code (CAST a)
let crename = mk_code RENAME
let cview (c, t) = mk_code (VIEW (c, t))
let copen_chest = mk_code OPEN_CHEST
let ccreate_contract c = mk_code (CREATE_CONTRACT c)
let ccarn k = mk_code (CAR_N k)
let ccdrn k = mk_code (CDR_N k)
let cmp_ident = String.equal
let cmp_type (lhs : type_) (rhs : type_) =
let rec f (lhs : type_) (rhs : type_) =
match lhs.node, rhs.node with
| Taddress, Taddress -> true
| Tbig_map (a1, b1), Tbig_map (a2, b2) -> f a1 a2 && f b1 b2
| Tbool, Tbool -> true
| Tbytes, Tbytes -> true
| Tchain_id, Tchain_id -> true
| Tcontract a1, Tcontract a2 -> f a1 a2
| Tint, Tint -> true
| Tkey, Tkey -> true
| Tkey_hash, Tkey_hash -> true
| Tlambda (a1, b1), Tlambda (a2, b2) -> f a1 a2 && f b1 b2
| Tlist a1, Tlist a2 -> f a1 a2
| Tmap (a1, b1), Tmap (a2, b2) -> f a1 a2 && f b1 b2
| Tmutez, Tmutez -> true
| Tnat, Tnat -> true
| Toperation, Toperation -> true
| Toption a1, Toption a2 -> f a1 a2
| Tor (a1, b1), Tor (a2, b2) -> f a1 a2 && f b1 b2
| Tpair l1, Tpair l2 -> List.for_all2 f l1 l2
| Tset a1, Tset a2 -> f a1 a2
| Tsignature, Tsignature -> true
| Tstring, Tstring -> true
| Ttimestamp, Ttimestamp -> true
| Tunit, Tunit -> true
| Tticket a1, Tticket a2 -> f a1 a2
| Tsapling_state n1, Tsapling_state n2 -> n1 = n2
| Tsapling_transaction n1, Tsapling_transaction n2 -> n1 = n2
| Tbls12_381_g1, Tbls12_381_g1 -> true
| Tbls12_381_g2, Tbls12_381_g2 -> true
| Tbls12_381_fr, Tbls12_381_fr -> true
| Tnever, Tnever -> true
| Tchest, Tchest -> true
| Tchest_key, Tchest_key -> true
| _ -> false
in
f lhs rhs
let cmp_data lhs rhs =
let rec f lhs rhs =
match lhs, rhs with
| Dint n1, Dint n2 -> Big_int.eq_big_int n1 n2
| Dstring s1, Dstring s2 -> String.equal s1 s2
| Dbytes s1, Dbytes s2 -> String.equal s1 s2
| Dunit, Dunit -> true
| Dtrue, Dtrue -> true
| Dfalse, Dfalse -> true
| Dpair l1, Dpair l2 -> List.for_all2 f l1 l2
| Dleft d1, Dleft d2 -> f d1 d2
| Dright d1, Dright d2 -> f d1 d2
| Dsome d1, Dsome d2 -> f d1 d2
| Dnone, Dnone -> true
| Dlist ds1, Dlist ds2 -> List.for_all2 f ds1 ds2
| Delt (a1, b1), Delt (a2, b2) -> f a1 a2 && f b1 b2
| _ -> false
in
f lhs rhs
let cmp_z_operator lhs rhs =
match lhs, rhs with
| Znow, Znow -> true
| Zamount, Zamount -> true
| Zbalance, Zbalance -> true
| Zsource, Zsource -> true
| Zsender, Zsender -> true
| Zaddress, Zaddress -> true
| Zchain_id, Zchain_id -> true
| Zself a1, Zself a2 -> Option.cmp String.equal a1 a2
| Zself_address, Zself_address -> true
| Znone t1, Znone t2 -> cmp_type t1 t2
| Zunit, Zunit -> true
| Znil t1, Znil t2 -> cmp_type t1 t2
| Zemptyset t1, Zemptyset t2 -> cmp_type t1 t2
| Zemptymap (k1, v1), Zemptymap (k2, v2) -> cmp_type k1 k2 && cmp_type v1 v2
| Zemptybigmap (k1, v1), Zemptybigmap (k2, v2) -> cmp_type k1 k2 && cmp_type v1 v2
| _ -> false
let cmp_un_operator lhs rhs =
match lhs, rhs with
| Ucar, Ucar -> true
| Ucdr, Ucdr -> true
| Uleft t1, Uleft t2 -> cmp_type t1 t2
| Uright t1, Uright t2 -> cmp_type t1 t2
| Uneg, Uneg -> true
| Unat, Unat -> true
| Uint, Uint -> true
| Ubytes, Ubytes -> true
| Unot, Unot -> true
| Uabs, Uabs -> true
| Uisnat, Uisnat -> true
| Usome, Usome -> true
| Usize, Usize -> true
| Upack, Upack -> true
| Uunpack t1, Uunpack t2 -> cmp_type t1 t2
| Ublake2b, Ublake2b -> true
| Usha256, Usha256 -> true
| Usha512, Usha512 -> true
| Uhash_key, Uhash_key -> true
| Ufail, Ufail -> true
| Ucontract (t1, i1), Ucontract (t2, i2) -> cmp_type t1 t2 && Option.cmp String.equal i1 i2
| Usetdelegate, Usetdelegate -> true
| Uimplicitaccount, Uimplicitaccount -> true
| Ueq, Ueq -> true
| Une, Une -> true
| Ugt, Ugt -> true
| Uge, Uge -> true
| Ult, Ult -> true
| Ule, Ule -> true
| UcarN n1, UcarN n2 -> n1 = n2
| UcdrN n1, UcdrN n2 -> n1 = n2
| _ -> false
let cmp_bin_operator lhs rhs =
match lhs, rhs with
| Badd, Badd -> true
| Bsub, Bsub -> true
| Bsubmutez, Bsubmutez -> true
| Bmul, Bmul -> true
| Bediv, Bediv -> true
| Blsl, Blsl -> true
| Blsr, Blsr -> true
| Bor, Bor -> true
| Band, Band -> true
| Bxor, Bxor -> true
| Bcompare, Bcompare -> true
| Bget, Bget -> true
| Bmem, Bmem -> true
| Bconcat, Bconcat -> true
| Bcons, Bcons -> true
| Bpair, Bpair -> true
| Bexec, Bexec -> true
| Bapply, Bapply -> true
| _ -> false
let cmp_ter_operator lhs rhs =
match lhs, rhs with
| Tcheck_signature, Tcheck_signature -> true
| Tslice, Tslice -> true
| Tupdate, Tupdate -> true
| Ttransfer_tokens, Ttransfer_tokens -> true
| _ -> false
let cmp_code (lhs : code) (rhs : code) =
let rec f (lhs : code) (rhs : code) =
match lhs.node, rhs.node with
| SEQ l1, SEQ l2 -> List.for_all2 f l1 l2
| APPLY, APPLY -> true
| EXEC, EXEC -> true
| FAILWITH, FAILWITH -> true
| IF (t1, e1), IF (t2, e2) -> List.for_all2 f t1 t2 && List.for_all2 f e1 e2
| IF_CONS (t1, e1), IF_CONS (t2, e2) -> List.for_all2 f t1 t2 && List.for_all2 f e1 e2
| IF_LEFT (t1, e1), IF_LEFT (t2, e2) -> List.for_all2 f t1 t2 && List.for_all2 f e1 e2
| IF_NONE (t1, e1), IF_NONE (t2, e2) -> List.for_all2 f t1 t2 && List.for_all2 f e1 e2
| ITER l1, ITER l2 -> List.for_all2 f l1 l2
| LAMBDA (a1, r1, b1), LAMBDA (a2, r2, b2) -> cmp_type a1 a2 && cmp_type r1 r2 && List.for_all2 f b1 b2
| LOOP l1, LOOP l2 -> List.for_all2 f l1 l2
| LOOP_LEFT l1, LOOP_LEFT l2 -> List.for_all2 f l1 l2
| DIG n1, DIG n2 -> n1 = n2
| DIP (n1, l1), DIP (n2, l2) -> n1 = n2 && List.for_all2 f l1 l2
| DROP n1, DROP n2 -> n1 = n2
| DUG n1, DUG n2 -> n1 = n2
| DUP, DUP -> true
| DUP_N n1, DUP_N n2 -> n1 = n2
| PUSH (t1, d1), PUSH (t2, d2) -> cmp_type t1 t2 && cmp_data d1 d2
| SWAP, SWAP -> true
| ABS, ABS -> true
| ADD, ADD -> true
| COMPARE, COMPARE -> true
| EDIV, EDIV -> true
| EQ, EQ -> true
| GE, GE -> true
| GT, GT -> true
| NAT, NAT -> true
| INT, INT -> true
| BYTES, BYTES -> true
| ISNAT, ISNAT -> true
| LE, LE -> true
| LSL, LSL -> true
| LSR, LSR -> true
| LT, LT -> true
| MUL, MUL -> true
| NEG, NEG -> true
| NEQ, NEQ -> true
| SUB, SUB -> true
| SUB_MUTEZ, SUB_MUTEZ -> true
| AND, AND -> true
| NOT, NOT -> true
| OR, OR -> true
| XOR, XOR -> true
| BLAKE2B, BLAKE2B -> true
| CHECK_SIGNATURE, CHECK_SIGNATURE -> true
| HASH_KEY, HASH_KEY -> true
| KECCAK, KECCAK -> true
| PAIRING_CHECK, PAIRING_CHECK -> true
| SAPLING_EMPTY_STATE n1, SAPLING_EMPTY_STATE n2 -> n1 = n2
| SAPLING_VERIFY_UPDATE, SAPLING_VERIFY_UPDATE -> true
| SHA256, SHA256 -> true
| SHA512, SHA512 -> true
| SHA3, SHA3 -> true
| ADDRESS, ADDRESS -> true
| AMOUNT, AMOUNT -> true
| BALANCE, BALANCE -> true
| CHAIN_ID, CHAIN_ID -> true
| CONTRACT (t1, a1), CONTRACT (t2, a2) -> cmp_type t1 t2 && Option.cmp cmp_ident a1 a2
| CREATE_CONTRACT _c1, CREATE_CONTRACT _c2 -> true
| IMPLICIT_ACCOUNT, IMPLICIT_ACCOUNT -> true
| LEVEL, LEVEL -> true
| MIN_BLOCK_TIME, MIN_BLOCK_TIME -> true
| NOW, NOW -> true
| SELF a1, SELF a2 -> Option.cmp String.equal a1 a2
| SELF_ADDRESS, SELF_ADDRESS -> true
| SENDER, SENDER -> true
| SET_DELEGATE, SET_DELEGATE -> true
| SOURCE, SOURCE -> true
| TOTAL_VOTING_POWER, TOTAL_VOTING_POWER -> true
| TRANSFER_TOKENS, TRANSFER_TOKENS -> true
| EMIT (t1, a1), EMIT (t2, a2) -> cmp_type t1 t2 && Option.cmp cmp_ident a1 a2
| VOTING_POWER, VOTING_POWER -> true
| CAR, CAR -> true
| CDR, CDR -> true
| CONCAT, CONCAT -> true
| CONS, CONS -> true
| EMPTY_BIG_MAP (k1, v1), EMPTY_BIG_MAP (k2, v2) -> cmp_type k1 k2 && cmp_type v1 v2
| EMPTY_MAP (k1, v1), EMPTY_MAP (k2, v2) -> cmp_type k1 k2 && cmp_type v1 v2
| EMPTY_SET t1, EMPTY_SET t2 -> cmp_type t1 t2
| GET, GET -> true
| GET_N n1, GET_N n2 -> n1 = n2
| GET_AND_UPDATE, GET_AND_UPDATE -> true
| LEFT t1, LEFT t2 -> cmp_type t1 t2
| MAP l1, MAP l2 -> List.for_all2 f l1 l2
| MEM, MEM -> true
| NEVER, NEVER -> true
| NIL t1, NIL t2 -> cmp_type t1 t2
| NONE t1, NONE t2 -> cmp_type t1 t2
| PACK, PACK -> true
| PAIR, PAIR -> true
| PAIR_N n1, PAIR_N n2 -> n1 = n2
| RIGHT t1, RIGHT t2 -> cmp_type t1 t2
| SIZE, SIZE -> true
| SLICE, SLICE -> true
| SOME, SOME -> true
| UNIT, UNIT -> true
| UNPACK t1, UNPACK t2 -> cmp_type t1 t2
| UNPAIR, UNPAIR -> true
| UNPAIR_N n1, UNPAIR_N n2 -> n1 = n2
| UPDATE, UPDATE -> true
| UPDATE_N n1, UPDATE_N n2 -> n1 = n2
| JOIN_TICKETS, JOIN_TICKETS -> true
| READ_TICKET, READ_TICKET -> true
| SPLIT_TICKET, SPLIT_TICKET -> true
| TICKET, TICKET -> true
| CAST t1, CAST t2 -> cmp_type t1 t2
| RENAME, RENAME -> true
| VIEW (i1, t1), VIEW (i2, t2) -> cmp_ident i1 i2 && cmp_type t1 t2
| OPEN_CHEST, OPEN_CHEST -> true
| CAR_N n1, CAR_N n2 -> n1 = n2
| CDR_N n1, CDR_N n2 -> n1 = n2
| _ -> false
in
f lhs rhs
let cmp_builtin lhs rhs =
match lhs, rhs with
| Bmin t1, Bmin t2 -> cmp_type t1 t2
| Bmax t1, Bmax t2 -> cmp_type t1 t2
| Bfloor, Bfloor -> true
| Bceil, Bceil -> true
| BlistContains t1, BlistContains t2 -> cmp_type t1 t2
| BlistNth t1, BlistNth t2 -> cmp_type t1 t2
| BlistHead t1, BlistHead t2 -> cmp_type t1 t2
| BlistTail t1, BlistTail t2 -> cmp_type t1 t2
| Bnattostring, Bnattostring -> true
| Bbytestonat, Bbytestonat -> true
| Bnattobytes, Bnattobytes -> true
| Bratcmp, Bratcmp -> true
| Bratnorm, Bratnorm -> true
| Brataddsub, Brataddsub -> true
| Bratdiv, Bratdiv -> true
| Bratmul, Bratmul -> true
| Bratuminus, Bratuminus -> true
| Bratabs, Bratabs -> true
| Brattez, Brattez -> true
| Bratdur, Bratdur -> true
| Bsimplify_rational, Bsimplify_rational -> true
| _ -> false
let map_type (f : type_ -> type_) (t : type_) : type_ =
let node =
match t.node with
| Taddress -> Taddress
| Tbig_map (k, v) -> Tbig_map (f k, f v)
| Tbool -> Tbool
| Tbytes -> Tbytes
| Tchain_id -> Tchain_id
| Tcontract t -> Tcontract (f t)
| Tint -> Tint
| Tkey -> Tkey
| Tkey_hash -> Tkey_hash
| Tlambda (a, r) -> Tlambda (f a, f r)
| Tlist t -> Tlist (f t)
| Tmap (k, v) -> Tmap (f k, f v)
| Tmutez -> Tmutez
| Tnat -> Tnat
| Toperation -> Toperation
| Toption t -> Toption (f t)
| Tor (l, r) -> Tor (f l, f r)
| Tpair l -> Tpair (List.map f l)
| Tset t -> Tset (f t)
| Tsignature -> Tsignature
| Tstring -> Tstring
| Ttimestamp -> Ttimestamp
| Tunit -> Tunit
| Tticket t -> Tticket (f t)
| Tsapling_state n -> Tsapling_state n
| Tsapling_transaction n -> Tsapling_transaction n
| Tbls12_381_fr -> Tbls12_381_fr
| Tbls12_381_g1 -> Tbls12_381_g1
| Tbls12_381_g2 -> Tbls12_381_g2
| Tnever -> Tnever
| Tchest -> Tchest
| Tchest_key -> Tchest_key
| Ttx_rollup_l2_address -> Ttx_rollup_l2_address
in
{node = node; annotation = t.annotation}
let rec cmp_dvar (v1 : dvar) (v2 : dvar) =
match v1, v2 with
| `VLocal l1, `VLocal l2 -> l1 = l2
| `VGlobal g1, `VGlobal g2 -> String.equal g1 g2
| _ -> false
and cmp_dlocal l1 l2 =
Option.cmp cmp_dexpr !l1 !l2
and cmp_dexpr e1 e2 =
match e1, e2 with
| Dvar v1, Dvar v2 -> cmp_dvar v1 v2
| Ddata (_, d1), Ddata (_, d2) -> cmp_data d1 d2
| Depair (e1, e'1), Depair (e2, e'2) -> cmp_dexpr e1 e2 && cmp_dexpr e'1 e'2
| Dfun (op1, l1), Dfun (op2, l2) -> op1 = op2 && List.for_all2 cmp_dexpr l1 l2
| _ -> false
let map_data (f : data -> data) = function
| Dint n -> Dint n
| Dstring v -> Dstring v
| Dbytes v -> Dbytes v
| Dunit -> Dunit
| Dtrue -> Dtrue
| Dfalse -> Dfalse
| Dpair l -> Dpair (List.map f l)
| Dleft v -> Dleft (f v)
| Dright v -> Dright (f v)
| Dsome v -> Dsome (f v)
| Dnone -> Dnone
| Dlist l -> Dlist (List.map f l)
| Delt (l, r) -> Delt (f l, f r)
| Dvar (c, t, b) -> Dvar (c, t, b)
| DIrCode (id, c) -> DIrCode (id, c)
| Dcode c -> Dcode c
| Dlambda_rec c -> Dlambda_rec c
let map_code_gen (fc : code -> code) (fd : data -> data) (ft : type_ -> type_) (x : code) : code =
let node =
match x.node with
| SEQ l -> SEQ (List.map fc l)
| APPLY -> APPLY
| EXEC -> EXEC
| FAILWITH -> FAILWITH
| IF (then_, else_) -> IF (List.map fc then_, List.map fc else_)
| IF_CONS (then_, else_) -> IF_CONS (List.map fc then_, List.map fc else_)
| IF_LEFT (then_, else_) -> IF_LEFT (List.map fc then_, List.map fc else_)
| IF_NONE (then_, else_) -> IF_NONE (List.map fc then_, List.map fc else_)
| ITER l -> ITER (List.map fc l)
| LAMBDA (at, rt, body) -> LAMBDA (ft at, ft rt, List.map fc body)
| LOOP l -> LOOP (List.map fc l)
| LOOP_LEFT l -> LOOP_LEFT (List.map fc l)
| DIG n -> DIG n
| DIP (n, l) -> DIP (n, List.map fc l)
| DROP n -> DROP n
| DUG n -> DUG n
| DUP -> DUP
| DUP_N n -> DUP_N n
| PUSH (t, d) -> PUSH (ft t, fd d)
| SWAP -> SWAP
| ABS -> ABS
| ADD -> ADD
| COMPARE -> COMPARE
| EDIV -> EDIV
| EQ -> EQ
| GE -> GE
| GT -> GT
| NAT -> NAT
| INT -> INT
| BYTES -> BYTES
| ISNAT -> ISNAT
| LE -> LE
| LSL -> LSL
| LSR -> LSR
| LT -> LT
| MUL -> MUL
| NEG -> NEG
| NEQ -> NEQ
| SUB -> SUB
| SUB_MUTEZ -> SUB_MUTEZ
| AND -> AND
| NOT -> NOT
| OR -> OR
| XOR -> XOR
| BLAKE2B -> BLAKE2B
| CHECK_SIGNATURE -> CHECK_SIGNATURE
| HASH_KEY -> HASH_KEY
| KECCAK -> KECCAK
| PAIRING_CHECK -> PAIRING_CHECK
| SAPLING_EMPTY_STATE n -> SAPLING_EMPTY_STATE n
| SAPLING_VERIFY_UPDATE -> SAPLING_VERIFY_UPDATE
| SHA256 -> SHA256
| SHA512 -> SHA512
| SHA3 -> SHA3
| ADDRESS -> ADDRESS
| AMOUNT -> AMOUNT
| BALANCE -> BALANCE
| CHAIN_ID -> CHAIN_ID
| CONTRACT (t, a) -> CONTRACT (ft t, a)
| CREATE_CONTRACT c -> CREATE_CONTRACT c
| IMPLICIT_ACCOUNT -> IMPLICIT_ACCOUNT
| LEVEL -> LEVEL
| MIN_BLOCK_TIME -> MIN_BLOCK_TIME
| NOW -> NOW
| SELF a -> SELF a
| SELF_ADDRESS -> SELF_ADDRESS
| SENDER -> SENDER
| SET_DELEGATE -> SET_DELEGATE
| SOURCE -> SOURCE
| TOTAL_VOTING_POWER -> TOTAL_VOTING_POWER
| TRANSFER_TOKENS -> TRANSFER_TOKENS
| EMIT (t, a) -> EMIT (ft t, a)
| VOTING_POWER -> VOTING_POWER
| CAR -> CAR
| CDR -> CDR
| CONCAT -> CONCAT
| CONS -> CONS
| EMPTY_BIG_MAP (k, v) -> EMPTY_BIG_MAP (ft k, ft v)
| EMPTY_MAP (k, v) -> EMPTY_MAP (ft k, ft v)
| EMPTY_SET t -> EMPTY_SET (ft t)
| GET -> GET
| GET_N n -> GET_N n
| GET_AND_UPDATE -> GET_AND_UPDATE
| LEFT t -> LEFT (ft t)
| MAP l -> MAP (List.map fc l)
| MEM -> MEM
| NEVER -> NEVER
| NIL t -> NIL (ft t)
| NONE t -> NONE (ft t)
| PACK -> PACK
| PAIR -> PAIR
| PAIR_N n -> PAIR_N n
| RIGHT t -> RIGHT (ft t)
| SIZE -> SIZE
| SLICE -> SLICE
| SOME -> SOME
| UNIT -> UNIT
| UNPACK t -> UNPACK (ft t)
| UNPAIR -> UNPAIR
| UNPAIR_N n -> UNPAIR_N n
| UPDATE -> UPDATE
| UPDATE_N n -> UPDATE_N n
| JOIN_TICKETS -> JOIN_TICKETS
| READ_TICKET -> READ_TICKET
| SPLIT_TICKET -> SPLIT_TICKET
| TICKET -> TICKET
| CAST t -> CAST (ft t)
| RENAME -> RENAME
| VIEW (c, t) -> VIEW (c, ft t)
| OPEN_CHEST -> OPEN_CHEST
| CAR_N n -> CAR_N n
| CDR_N n -> CDR_N n
in
let type_ = Option.map (List.map ft) !(x.type_) in
mk_code ?type_ node
let map_code (fc : code -> code) = map_code_gen fc id id
let rec map_seq (f : code list -> code list) (code : code) =
let g x = f (List.map (map_seq f) x) in
match code.node with
| SEQ l -> {code with node = SEQ (g l)}
| IF_NONE (x, y) -> {code with node = IF_NONE (g x, g y)}
| IF_LEFT (x, y) -> {code with node = IF_LEFT (g x, g y)}
| IF_CONS (x, y) -> {code with node = IF_CONS (g x, g y)}
| MAP x -> {code with node = MAP (g x)}
| ITER x -> {code with node = ITER (g x)}
| IF (x, y) -> {code with node = IF (g x, g y)}
| LOOP x -> {code with node = LOOP (g x)}
| LOOP_LEFT x -> {code with node = LOOP_LEFT (g x)}
| LAMBDA (a, b, x) -> {code with node = LAMBDA (a, b, g x)}
| DIP (n, x) -> {code with node = DIP (n, g x)}
| _ -> map_code (map_seq f) code
module Utils : sig
val get_fun_name : (type_ -> ident) -> builtin -> ident
val flat : code -> code
val optim : code -> code
val replace_macro : code -> code
val data_to_micheline : data -> obj_micheline
val type_to_micheline : type_-> obj_micheline
val code_to_micheline : code -> obj_micheline
val to_micheline : michelson -> data -> micheline
val is_storable : type_ -> bool
end = struct
let get_fun_name ft = function
| Bmin _t -> "_min_"
| Bmax _t -> "_max_"
| Bfloor -> "_floor"
| Bceil -> "_ceil"
| BlistContains t -> "_list_contains_" ^ (ft t)
| BlistNth t -> "_list_nth_" ^ (ft t)
| BlistHead t -> "_list_head_" ^ (ft t)
| BlistTail t -> "_list_tail_" ^ (ft t)
| Bnattostring -> "_nat_to_string_"
| Bbytestonat -> "_bytes_to_nat_"
| Bnattobytes -> "_nat_to_bytes_"
| Bsimplify_rational -> "_simplify_rational_"
| Bratcmp -> "_ratcmp"
| Bratnorm -> "_ratnorm"
| Brataddsub -> "_rataddsub"
| Bratmul -> "_ratmul"
| Bratdiv -> "_ratdiv"
| Bratuminus -> "_ratuminus"
| Bratabs -> "_ratabs"
| Brattez -> "_rattez"
| Bratdur -> "_ratdur"
| Bmuteztonat -> "_muteztonat"
let flat (c : code) : code =
let f l = List.fold_left (fun accu x -> match x.node with | SEQ l -> accu @ l | _ -> accu @ [x]) [] l in
map_seq f c
let handle_failwith (c : code) : code =
let init = (false, []) in
let rec aux (c : code) =
let rec for_seq ((b, accu) : bool * code list) (l : code list) : bool * code list =
match l with
| {node = FAILWITH}::_ -> for_seq (true, (mk_code FAILWITH)::accu) []
| e::t -> begin
let bb, a = aux e in
let t = if bb then [] else t in
for_seq (bb, a::accu) t
end
| [] -> b, List.rev accu
in
let g x f =
let _, x = for_seq init x in
false, f x
in
let h x y f =
let b0, x = for_seq init x in
let b1, y = for_seq init y in
b0 && b1, f x y
in
match c.node with
| SEQ x -> g x (fun l -> mk_code (SEQ (l)))
| IF (x, y) -> h x y (fun a b -> mk_code (IF (a, b)))
| IF_NONE (x, y) -> h x y (fun a b -> mk_code (IF_NONE (a, b)))
| IF_LEFT (x, y) -> h x y (fun a b -> mk_code (IF_LEFT (a, b)))
| IF_CONS (x, y) -> h x y (fun a b -> mk_code (IF_CONS (a, b)))
| MAP x -> g x (fun l -> mk_code (MAP (l)))
| ITER x -> g x (fun l -> mk_code (ITER (l)))
| LOOP x -> g x (fun l -> mk_code (LOOP (l)))
| LOOP_LEFT x -> g x (fun l -> mk_code (LOOP_LEFT (l)))
| DIP (n, x) -> g x (fun l -> mk_code (DIP (n, l)))
| LAMBDA (a, b, c) -> g c (fun l -> mk_code (LAMBDA (a, b, l)))
| _ -> false, c
in
aux c |> snd
let factorize_instrs (c : code) : code =
let f l =
let rec aux accu (l : code list) =
let g accu l =
match accu with
| x::tl -> aux tl (x::l)
| [] -> aux accu l
in
match l with
| ({node = UNPAIR})::({node = PAIR})::t -> g accu t
| ({node = PAIR})::({node = UNPAIR})::t -> g accu t
| ({node = UNPAIR_N x})::({node = PAIR_N y})::t when x = y -> g accu t
| ({node = PAIR_N x})::({node = UNPAIR_N y})::t when x = y -> g accu t
| ({node = PAIR_N 2})::t -> g accu ((mk_code (PAIR))::t)
| ({node = UNPAIR_N 2})::t -> g accu ((mk_code (UNPAIR))::t)
| ({node = UNPAIR})::({node = DROP 1})::t -> g accu ((mk_code (CDR))::t)
| ({node = DROP x})::({node = DROP y})::t -> g accu ((mk_code (DROP (x + y)))::t)
| ({node = DUP})::({node = DROP x})::t -> g accu ((mk_code (DROP (x - 1)))::t)
| ({node = DUP})::({node = SWAP})::t -> g accu ((mk_code (DUP))::t)
| ({node = DROP 0})::t -> g accu t
| ({node = DIG 0})::t -> g accu t
| ({node = DUG 0})::t -> g accu t
| ({node = DIP (_, [])})::t -> g accu t
| ({node = DIG 1})::t -> g accu ((mk_code (SWAP))::t)
| ({node = DUG 1})::t -> g accu ((mk_code (SWAP))::t)
| ({node = SWAP})::({node = SWAP})::t -> g accu t
| e::t -> aux (e::accu) t
| [] -> List.rev accu
in
aux [] l
in
map_seq f c
let optim c =
let code = c |> handle_failwith in
if !Options.opt_g
then code
else
code
|> factorize_instrs
let replace_macro (c : code) : code =
let rec aux (c : code) : code =
match c.node with
| UNPAIR -> mk_code (SEQ [mk_code DUP; mk_code CAR; mk_code (DIP (1, [mk_code CDR]))])
| _ -> map_code aux c
in
aux c
let rec type_to_micheline (t : type_) : obj_micheline =
let f = type_to_micheline in
let prim, args =
match t.node with
| Taddress -> "address", []
| Tbig_map (k, v) -> "big_map", [f k; f v]
| Tbool -> "bool", []
| Tbytes -> "bytes", []
| Tchain_id -> "chain_id", []
| Tcontract t -> "contract", [f t]
| Tint -> "int", []
| Tkey -> "key", []
| Tkey_hash -> "key_hash", []
| Tlambda (a, r) -> "lambda", [f a; f r]
| Tlist t -> "list", [f t]
| Tmap (k, v) -> "map", [f k; f v]
| Tmutez -> "mutez", []
| Tnat -> "nat", []
| Toperation -> "operation", []
| Toption t -> "option", [f t]
| Tor (l, r) -> "or", [f l; f r]
| Tpair l -> "pair", List.map f l
| Tset t -> "set", [f t]
| Tsignature -> "signature", []
| Tstring -> "string", []
| Ttimestamp -> "timestamp", []
| Tunit -> "unit", []
| Tticket t -> "ticket", [f t]
| Tsapling_transaction n -> Format.asprintf "sapling_transaction", [Oint (string_of_int n)]
| Tsapling_state n -> Format.asprintf "sapling_state", [Oint (string_of_int n)]
| Tbls12_381_g1 -> "bls12_381_g1", []
| Tbls12_381_g2 -> "bls12_381_g2", []
| Tbls12_381_fr -> "bls12_381_fr", []
| Tnever -> "never", []
| Tchest -> "chest", []
| Tchest_key -> "chest_key", []
| Ttx_rollup_l2_address -> "tx_rollup_l2_address", []
in
let args = if List.is_empty args then None else Some args in
let annots = Option.bind (fun x -> Some [x]) t.annotation in
let prim = mk_prim ?args ?annots prim in
Oprim prim
let rec data_to_micheline (d : data) : obj_micheline =
let f = data_to_micheline in
match d with
| Dint n -> Oint (Big_int.string_of_big_int n)
| Dstring v -> Ostring v
| Dbytes v -> Obytes v
| Dunit -> Oprim (mk_prim "Unit")
| Dtrue -> Oprim (mk_prim "True")
| Dfalse -> Oprim (mk_prim "False")
| Dpair l -> Oprim (mk_prim ~args:(List.map f l) "Pair")
| Dleft v -> Oprim (mk_prim ~args:[f v] "Left")
| Dright v -> Oprim (mk_prim ~args:[f v] "Right")
| Dsome v -> Oprim (mk_prim ~args:[f v] "Some")
| Dnone -> Oprim (mk_prim "None")
| Dlist l -> Oarray (List.map f l)
| Delt (l, r) -> Oprim (mk_prim ~args:[f l; f r] "Elt")
| Dvar (x, t, b) -> begin
match t.node with
| Taddress -> Ovar (OMVstring x)
| Tbig_map (_k, _v) -> Ovar (OMVfree x)
| Tbool -> Ovar (OMVif (x, Oprim (mk_prim "True"), Oprim (mk_prim "False")))
| Tbytes -> Ovar (OMVbytes x)
| Tchain_id -> Ovar (OMVfree x)
| Tcontract _t -> Ovar (OMVfree x)
| Tint -> Ovar (OMVint (x, b))
| Tkey -> Ovar (OMVbytes x)
| Tkey_hash -> Ovar (OMVbytes x)
| Tlambda (_a, _r) -> Ovar (OMVfree x)
| Tlist _t -> Ovar (OMVfree x)
| Tmap (_k, _v) -> Ovar (OMVfree x)
| Tmutez -> Ovar (OMVint (x, b))
| Tnat -> Ovar (OMVint (x, b))
| Toperation -> Ovar (OMVfree x)
| Toption _t -> Ovar (OMVfree x)
| Tor (_l, _r) -> Ovar (OMVfree x)
| Tpair _l -> Ovar (OMVfree x)
| Tset _t -> Ovar (OMVfree x)
| Tsignature -> Ovar (OMVbytes x)
| Tstring -> Ovar (OMVstring x)
| Ttimestamp -> Ovar (OMVint (x, b))
| Tunit -> Oprim (mk_prim "Unit")
| Tticket _t -> Ovar (OMVfree x)
| Tsapling_state _n -> Ovar (OMVfree x)
| Tsapling_transaction _n -> Ovar (OMVfree x)
| Tbls12_381_g1 -> Ovar (OMVfree x)
| Tbls12_381_g2 -> Ovar (OMVfree x)
| Tbls12_381_fr -> Ovar (OMVfree x)
| Tnever -> Ovar (OMVfree x)
| Tchest -> Ovar (OMVfree x)
| Tchest_key -> Ovar (OMVfree x)
| Ttx_rollup_l2_address -> Ovar (OMVfree x)
end
| DIrCode (_id, _c) -> Oarray ([])
| Dcode c -> code_to_micheline c
| Dlambda_rec c -> Oprim (mk_prim ~args:[code_to_micheline c] "Lambda_rec")
and code_to_micheline (c : code) : obj_micheline =
let f = code_to_micheline in
let ft = type_to_micheline in
let fd = data_to_micheline in
let mk ?(args=[]) ?(annots=[]) x = Oprim (mk_prim ~args ~annots x) in
let mk_int n = Oint (string_of_int n) in
let mk_string s = Ostring s in
let mk_array l = Oarray (List.map f l) in
let fan = function | Some v -> [v] | None -> [] in
match c.node with
| SEQ l -> mk_array l
| APPLY -> mk "APPLY"
| EXEC -> mk "EXEC"
| FAILWITH -> mk "FAILWITH"
| IF (t, e) -> mk ~args:[mk_array t; mk_array e] "IF"
| IF_CONS (t, e) -> mk ~args:[mk_array t; mk_array e] "IF_CONS"
| IF_LEFT (t, e) -> mk ~args:[mk_array t; mk_array e] "IF_LEFT"
| IF_NONE (t, e) -> mk ~args:[mk_array t; mk_array e] "IF_NONE"
| ITER l -> mk ~args:[mk_array l] "ITER"
| LAMBDA (at, rt, body) -> mk ~args:[ft at; ft rt; mk_array body] "LAMBDA"
| LOOP l -> mk ~args:[mk_array l] "LOOP"
| LOOP_LEFT l -> mk ~args:[mk_array l] "LOOP_LEFT"
| DIG n -> mk ~args:[mk_int n] "DIG"
| DIP (n, l) -> mk ~args:[mk_int n; mk_array l] "DIP"
| DROP n -> mk ~args:[mk_int n] "DROP"
| DUG n -> mk ~args:[mk_int n] "DUG"
| DUP -> mk "DUP"
| DUP_N n -> mk ~args:[mk_int n] "DUP"
| PUSH (t, d) -> mk ~args:[ft t; fd d] "PUSH"
| SWAP -> mk "SWAP"
| ABS -> mk "ABS"
| ADD -> mk "ADD"
| COMPARE -> mk "COMPARE"
| EDIV -> mk "EDIV"
| EQ -> mk "EQ"
| GE -> mk "GE"
| GT -> mk "GT"
| NAT -> mk "NAT"
| INT -> mk "INT"
| BYTES -> mk "BYTES"
| ISNAT -> mk "ISNAT"
| LE -> mk "LE"
| LSL -> mk "LSL"
| LSR -> mk "LSR"
| LT -> mk "LT"
| MUL -> mk "MUL"
| NEG -> mk "NEG"
| NEQ -> mk "NEQ"
| SUB -> mk "SUB"
| SUB_MUTEZ -> mk "SUB_MUTEZ"
| AND -> mk "AND"
| NOT -> mk "NOT"
| OR -> mk "OR"
| XOR -> mk "XOR"
| BLAKE2B -> mk "BLAKE2B"
| CHECK_SIGNATURE -> mk "CHECK_SIGNATURE"
| HASH_KEY -> mk "HASH_KEY"
| KECCAK -> mk "KECCAK"
| PAIRING_CHECK -> mk "PAIRING_CHECK"
| SAPLING_EMPTY_STATE n -> mk "SAPLING_EMPTY_STATE" ~args:[Oint (string_of_int n)]
| SAPLING_VERIFY_UPDATE -> mk "SAPLING_VERIFY_UPDATE"
| SHA256 -> mk "SHA256"
| SHA512 -> mk "SHA512"
| SHA3 -> mk "SHA3"
| ADDRESS -> mk "ADDRESS"
| AMOUNT -> mk "AMOUNT"
| BALANCE -> mk "BALANCE"
| CHAIN_ID -> mk "CHAIN_ID"
| CONTRACT (t, a) -> mk ~args:[ft t] ~annots:(fan a) "CONTRACT"
| CREATE_CONTRACT c -> mk ~args:[c] "CREATE_CONTRACT"
| EMIT (t, a) -> mk ~args:[ft t] ~annots:(fan a) "EMIT"
| IMPLICIT_ACCOUNT -> mk "IMPLICIT_ACCOUNT"
| LEVEL -> mk "LEVEL"
| MIN_BLOCK_TIME -> mk "MIN_BLOCK_TIME"
| NOW -> mk "NOW"
| SELF a -> mk ~annots:(fan a) "SELF"
| SELF_ADDRESS -> mk "SELF_ADDRESS"
| SENDER -> mk "SENDER"
| SET_DELEGATE -> mk "SET_DELEGATE"
| SOURCE -> mk "SOURCE"
| TOTAL_VOTING_POWER -> mk "TOTAL_VOTING_POWER"
| TRANSFER_TOKENS -> mk "TRANSFER_TOKENS"
| VOTING_POWER -> mk "VOTING_POWER"
| CAR -> mk "CAR"
| CDR -> mk "CDR"
| CONCAT -> mk "CONCAT"
| CONS -> mk "CONS"
| EMPTY_BIG_MAP (k, v) -> mk ~args:[ft k; ft v] "EMPTY_BIG_MAP"
| EMPTY_MAP (k, v) -> mk ~args:[ft k; ft v] "EMPTY_MAP"
| EMPTY_SET t -> mk ~args:[ft t] "EMPTY_SET"
| GET -> mk "GET"
| GET_N n -> mk ~args:[mk_int n] "GET"
| GET_AND_UPDATE -> mk "GET_AND_UPDATE"
| LEFT t -> mk ~args:[ft t] "LEFT"
| MAP l -> mk ~args:[mk_array l] "MAP"
| MEM -> mk "MEM"
| NEVER -> mk "NEVER"
| NIL t -> mk ~args:[ft t] "NIL"
| NONE t -> mk ~args:[ft t] "NONE"
| PACK -> mk "PACK"
| PAIR -> mk "PAIR"
| PAIR_N n -> mk ~args:[mk_int n] "PAIR"
| RIGHT t -> mk ~args:[ft t] "RIGHT"
| SIZE -> mk "SIZE"
| SLICE -> mk "SLICE"
| SOME -> mk "SOME"
| UNIT -> mk "UNIT"
| UNPACK t -> mk ~args:[ft t] "UNPACK"
| UNPAIR -> mk "UNPAIR"
| UNPAIR_N n -> mk ~args:[mk_int n] "UNPAIR"
| UPDATE -> mk "UPDATE"
| UPDATE_N n -> mk ~args:[mk_int n] "UPDATE"
| JOIN_TICKETS -> mk "JOIN_TICKETS"
| READ_TICKET -> mk "READ_TICKET"
| SPLIT_TICKET -> mk "SPLIT_TICKET"
| TICKET -> mk "TICKET"
| CAST t -> mk ~args:[ft t] "CAST"
| RENAME -> mk "RENAME"
| VIEW (c, t) -> mk ~args:[mk_string c; ft t] "VIEW"
| OPEN_CHEST -> mk "OPEN_CHEST"
| CAR_N n -> mk ~args:[mk_int n] "CAR"
| CDR_N n -> mk ~args:[mk_int n] "CDR"
let view_to_micheline (view : view_struct) : obj_micheline =
let mk ?(args=[]) ?(annots=[]) x : obj_micheline = Oprim (mk_prim ~args ~annots x) in
let mk_string s = Ostring s in
let id = view.id in
let param = type_to_micheline view.param in
let ret = type_to_micheline view.ret in
let body = code_to_micheline view.body in
mk ~args:[
mk_string id;
param;
ret;
body
] "view"
let to_micheline (m : michelson) (s : data) : micheline =
let storage = type_to_micheline m.storage in
let parameter = type_to_micheline m.parameter in
let code = code_to_micheline m.code in
let views = List.map view_to_micheline m.views in
let f tag x = Oprim (mk_prim ~args:[x] tag) in
let parameters = m.parameters in
mk_micheline ~parameters ~views [f "storage" storage; f "parameter" parameter; f "code" code] (data_to_micheline s)
let rec is_storable (t : type_) =
match t.node with
| Taddress -> true
| Tbig_map _ -> true
| Tbool -> true
| Tbytes -> true
| Tchain_id -> true
| Tcontract _ -> false
| Tint -> true
| Tkey -> true
| Tkey_hash -> true
| Tlambda _ -> true
| Tlist t -> is_storable t
| Tmap (k, v) -> is_storable k && is_storable v
| Tmutez -> true
| Tnat -> true
| Toperation -> false
| Toption t -> is_storable t
| Tor (l, r) -> is_storable l && is_storable r
| Tpair l -> List.for_all is_storable l
| Tset t -> is_storable t
| Tsignature -> true
| Tstring -> true
| Ttimestamp -> true
| Tunit -> true
| Tticket t -> is_storable t
| Tsapling_state _n -> true
| Tsapling_transaction _n -> true
| Tbls12_381_g1 -> true
| Tbls12_381_g2 -> true
| Tbls12_381_fr -> true
| Tnever -> true
| Tchest -> true
| Tchest_key -> true
| Ttx_rollup_l2_address -> true
end
let rec to_type (o : obj_micheline) : type_ =
let fa l = match l with | a::_ -> Some a | [] -> None in
let f = to_type in
match o with
| Oprim ({prim = "address"; annots; _}) -> mk_type ?annotation:(fa annots) Taddress
| Oprim ({prim = "big_map"; annots; args = k::v::_}) -> mk_type ?annotation:(fa annots) (Tbig_map (f k, f v))
| Oprim ({prim = "bool"; annots; _}) -> mk_type ?annotation:(fa annots) Tbool
| Oprim ({prim = "bytes"; annots; _}) -> mk_type ?annotation:(fa annots) Tbytes
| Oprim ({prim = "chain_id"; annots; _}) -> mk_type ?annotation:(fa annots) Tchain_id
| Oprim ({prim = "contract"; annots; args = t::_}) -> mk_type ?annotation:(fa annots) (Tcontract (f t))
| Oprim ({prim = "int"; annots; _}) -> mk_type ?annotation:(fa annots) Tint
| Oprim ({prim = "key"; annots; _}) -> mk_type ?annotation:(fa annots) Tkey
| Oprim ({prim = "key_hash"; annots; _}) -> mk_type ?annotation:(fa annots) Tkey_hash
| Oprim ({prim = "lambda"; annots; args = a::r::_}) -> mk_type ?annotation:(fa annots) (Tlambda (f a, f r))
| Oprim ({prim = "list"; annots; args = t::_}) -> mk_type ?annotation:(fa annots) (Tlist (f t))
| Oprim ({prim = "map"; annots; args = k::v::_}) -> mk_type ?annotation:(fa annots) (Tmap (f k, f v))
| Oprim ({prim = "mutez"; annots; _}) -> mk_type ?annotation:(fa annots) Tmutez
| Oprim ({prim = "nat"; annots; _}) -> mk_type ?annotation:(fa annots) Tnat
| Oprim ({prim = "operation"; annots; _}) -> mk_type ?annotation:(fa annots) Toperation
| Oprim ({prim = "option"; annots; args = t::_}) -> mk_type ?annotation:(fa annots) (Toption (f t))
| Oprim ({prim = "or"; annots; args = a::b::_}) -> mk_type ?annotation:(fa annots) (Tor (f a, f b))
| Oprim ({prim = "pair"; annots; args = l}) -> mk_type ?annotation:(fa annots) (Tpair (List.map f l))
| Oprim ({prim = "set"; annots; args = t::_}) -> mk_type ?annotation:(fa annots) (Tset (f t))
| Oprim ({prim = "signature"; annots; _}) -> mk_type ?annotation:(fa annots) Tsignature
| Oprim ({prim = "string"; annots; _}) -> mk_type ?annotation:(fa annots) Tstring
| Oprim ({prim = "timestamp"; annots; _}) -> mk_type ?annotation:(fa annots) Ttimestamp
| Oprim ({prim = "unit"; annots; _}) -> mk_type ?annotation:(fa annots) Tunit
| Oprim ({prim = "ticket"; annots; args = t::_}) -> mk_type ?annotation:(fa annots) (Tticket (f t))
| Oprim ({prim = "sapling_state"; annots; args = (Oint n)::_; _}) -> mk_type ?annotation:(fa annots) (Tsapling_state (int_of_string n))
| Oprim ({prim = "sapling_transaction"; annots; args = (Oint n)::_; _}) -> mk_type ?annotation:(fa annots) (Tsapling_transaction (int_of_string n))
| Oprim ({prim = "bls12_381_fr"; annots; _}) -> mk_type ?annotation:(fa annots) Tbls12_381_fr
| Oprim ({prim = "bls12_381_g1"; annots; _}) -> mk_type ?annotation:(fa annots) Tbls12_381_g1
| Oprim ({prim = "bls12_381_g2"; annots; _}) -> mk_type ?annotation:(fa annots) Tbls12_381_g2
| Oprim ({prim = "never"; annots; _}) -> mk_type ?annotation:(fa annots) Tnever
| Oprim ({prim = "chest"; annots; _}) -> mk_type ?annotation:(fa annots) Tchest
| Oprim ({prim = "chest_key"; annots; _}) -> mk_type ?annotation:(fa annots) Tchest_key
| Oprim ({prim = "tx_rollup_l2_address"; annots; _}) -> mk_type ?annotation:(fa annots) Ttx_rollup_l2_address
| _ -> Format.eprintf "type unknown %a@." pp_obj_micheline o; assert false
let rec to_data (o : obj_micheline) : data =
let f = to_data in
match o with
| Oint x -> Dint (Big_int.big_int_of_string x)
| Ostring s -> Dstring s
| Obytes s -> Dbytes s
| Oprim ({prim = "Unit"; _ }) -> Dunit
| Oprim ({prim = "True"; _ }) -> Dtrue
| Oprim ({prim = "False"; _ }) -> Dfalse
| Oprim ({prim = "Pair"; args }) -> Dpair (List.map f args)
| Oprim ({prim = "Left"; args = a::_ }) -> Dleft (f a)
| Oprim ({prim = "Right"; args = a::_ }) -> Dright (f a)
| Oprim ({prim = "Some"; args = a::_ }) -> Dsome (f a)
| Oprim ({prim = "None"; _ }) -> Dnone
| Oarray l -> Dlist (List.map f l)
| Oprim ({prim = "Elt"; args = a::b::_ }) -> Delt (f a, f b)
| Oprim ({prim = "Lambda_rec"; args = _a::_ }) -> Format.eprintf "TODO Lambda_rec"; assert false
| _ -> Format.eprintf "data unknown %a@." pp_obj_micheline o; assert false
type tz_micheline = Micheline_printer.node
let to_tz_micheline (input : obj_micheline) : tz_micheline =
let emptyloc : Micheline_printer.location = {comment = None} in
let mkint v = Micheline.Int (emptyloc, v) in
let mkstring v = Micheline.String (emptyloc, v) in
let mkbytes v = Micheline.Bytes (emptyloc, v) in
let mkprim (p, args, annot) = Micheline.Prim (emptyloc, p, args, annot) in
let mkseq nodes = Micheline.Seq (emptyloc, nodes) in
let rec doit (i : obj_micheline) : tz_micheline =
match i with
| Oprim p -> mkprim (p.prim, List.map doit p.args, p.annots)
| Ostring v -> mkstring v
| Obytes v -> mkbytes (Hex.to_bytes (`Hex v))
| Oint v -> mkint (Big_int.big_int_of_string v)
| Oarray v -> mkseq (List.map doit v)
| Ovar v -> begin
let f id = mkprim (id, [], []) in
match v with
| OMVfree id -> f id
| OMVint (id, _) -> f id
| OMVstring id -> f id
| OMVbytes id -> f id
| OMVif (id, _, _) -> f id
end
in
doit input
let micheline_to_tz_micheline (input : micheline) : tz_micheline =
let obj : obj_micheline = Oarray (input.code @ input.views) in
to_tz_micheline obj