File: theme-d-tree-il-target-compilation.scm

package info (click to toggle)
theme-d 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 12,784 kB
  • sloc: lisp: 47,684; sh: 4,200; makefile: 455; ansic: 319
file content (3701 lines) | stat: -rw-r--r-- 129,505 bytes parent folder | download
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
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
;; Copyright (C) 2017 Tommi Höynälänmaa
;; Distributed under GNU General Public License version 3,
;; see file doc/GPL-3.


;; *** Tree-IL target compilation ***


(import (th-scheme-utilities stdutils))


(define tc-tree-il-object-fwd '())
(define theme-target-tree-il-compile-fwd '())


(define (tree-il-get-call-keyword linker)
  (case (hfield-ref linker 's-intermediate-language)
    ((tree-il-3.0) 'call)
    ((tree-il-2.2) 'call)
    ((tree-il-2.0) 'apply)
    (else (raise '(tree-il-get-call-keyword:internal-error)))))


(define (tree-il-get-seq-keyword linker)
  (case (hfield-ref linker 's-intermediate-language)
    ((tree-il-3.0) 'seq)
    ((tree-il-2.2) 'seq)
    ((tree-il-2.0) 'begin)
    (else (raise '(tree-il-get-seq-keyword:internal-error)))))


(define (tc-tree-il-2.0-compile-sequence l-target-exprs)
  (cons 'begin l-target-exprs))


(define (tc-tree-il-2.2-compile-sequence l-target-exprs)
  (cond
   ((null? l-target-exprs) '(void))
   ((= (length l-target-exprs) 1) (car l-target-exprs))
   (else
    (list 'seq
	  (car l-target-exprs)
	  (tc-tree-il-2.2-compile-sequence (cdr l-target-exprs))))))


(define tc-tree-il-3.0-compile-sequence
  tc-tree-il-2.2-compile-sequence)


(define (tc-tree-il-compile-sequence linker l-target-exprs)
  (case (hfield-ref linker 's-intermediate-language)
    ((tree-il-3.0) (tc-tree-il-3.0-compile-sequence l-target-exprs))
    ((tree-il-2.2) (tc-tree-il-2.2-compile-sequence l-target-exprs))
    ((tree-il-2.0) (tc-tree-il-2.0-compile-sequence l-target-exprs))
    (else (raise '(tree-il-compile-sequence:internal-error)))))


(define (get-lambda-attr s-name)
  (cond
   ((null? s-name) '())
   ((symbol? s-name) `((name . ,s-name)))
   (else (raise 'internal-error))))

  
(define (make-instance-test linker t-obj expr-type)
  (assert (is-entity? expr-type))
  (cond
   ((eq? expr-type tc-object)
    '(const #t))
   ((eq? expr-type tt-none)
    '(const #f))
   ((not (is-known-object? expr-type))
    (let ((s-call (tree-il-get-call-keyword linker))
	  (t-expr-type (theme-target-tree-il-compile-fwd linker expr-type)))
      `(,s-call (toplevel is-instance?)
		,t-obj ,t-expr-type)))
   ((and (is-tc-pair? expr-type)
	 (eq? (tt-car expr-type) tc-object)
	 (eq? (tt-cdr expr-type) tc-object))
    (let ((s-call (tree-il-get-call-keyword linker)))
      `(,s-call (toplevel pair?) ,t-obj)))
   (else
    (let ((p-prim (assq expr-type gl-l-prim-pred))
	  (s-call (tree-il-get-call-keyword linker)))
      (if p-prim
	  `(,s-call (toplevel ,(cdr p-prim))
		    ,t-obj)
	  (let ((t-expr-type
		 (theme-target-tree-il-compile-fwd linker expr-type)))
	    (cond
	     ((and (is-t-param-class-instance? expr-type)
		   (not (is-tc-pair? expr-type)))
	      `(,s-call (toplevel is-instance-of-class?)
			,t-obj ,t-expr-type))
	     ((is-t-simple-class? expr-type)
	      ;; We know that the expression type is not an instance
	      ;; of a parametrized class here.
	      `(,s-call (toplevel is-instance-of-simple-class?)
			,t-obj ,t-expr-type))
	     (else 
	      `(,s-call (toplevel is-instance?)
			,t-obj ,t-expr-type)))))))))


(define (make-subtype-test linker ent-type1 ent-type2)
  (assert (is-entity? ent-type1))
  (assert (is-entity? ent-type2))
  (let ((sx-type1
	 (theme-target-tree-il-compile-fwd linker ent-type1))
	(sx-type2
	 (theme-target-tree-il-compile-fwd linker ent-type2))
	(s-call (tree-il-get-call-keyword linker)))
    (cond
     ((or (not (is-known-object? ent-type1))
	  (not (is-t-class? ent-type1))
	  (not (is-known-object? ent-type2))
	  (not (is-t-class? ent-type2)))
      `(,s-call (toplevel is-subtype?) ,sx-type1 ,sx-type2))
     ((is-t-simple-class? ent-type2)
      `(,s-call (toplevel is-simple-class-subtype?) ,sx-type1 ,sx-type2))
     ((and (is-t-param-class-instance? ent-type2)
	   (not (is-tc-pair? ent-type2)))
      `(,s-call (toplevel is-param-inst-subclass?) ,sx-type1 ,sx-type2))
     (else
      `(,s-call (toplevel is-subtype?) ,sx-type1 ,sx-type2)))))


(define (make-normal-equiv-expression linker s-kind x1 x2)
  (let ((p-pred-name (assq s-kind gl-al-target-eq-pred)))
    (assert (not (eq? p-pred-name #f)))
    (let ((s-pred-name (cdr p-pred-name))
	  (s-call (tree-il-get-call-keyword linker)))
      `(,s-call (toplevel ,s-pred-name) ,x1 ,x2))))


(define (optimize-eq-by-value1 linker s-kind i
			       l-fields1 l-fields2
			       x-obj1 x-obj2)
  (assert (= (length l-fields1) (length l-fields2)))
  (if (null? l-fields1)
      '(const #t)
      (let* ((type1 (tno-field-ref (car l-fields1) 'type))
	     (type2 (tno-field-ref (car l-fields2) 'type))
	     (s-call (tree-il-get-call-keyword linker))
	     (x-elem1
	      `(,s-call (toplevel vector-ref)
			,x-obj1
			(const ,i)))
	     (x-elem2
	      `(,s-call (toplevel vector-ref)
			,x-obj2
			(const ,i)))
	     ;; No deep recursion into contents.
	     (x-test (make-equiv-expression1 linker s-kind #f
					     type1 x-elem1
					     type2 x-elem2))
	     (x-next (optimize-eq-by-value1 linker s-kind (+ i 1)
					    (cdr l-fields1)
					    (cdr l-fields2)
					    x-obj1
					    x-obj2)))
	`(if ,x-test ,x-next (const #f)))))


(define (is-var-ref? x)
  (and (list? x)
       (not-null? x)
       (or (eq? (car x) 'toplevel)
	   (eq? (car x) 'lexical))))


;; This procedure works only for noninheritable classes.
(define (optimize-eq-by-value linker s-kind cl1 x1 cl2 x2)
  (let* ((l-fields1 (tno-field-ref cl1 'l-all-fields))
	 (l-fields2 (tno-field-ref cl2 'l-all-fields))
	 (i-nr-fields (length l-fields1)))
    (assert (= i-nr-fields (length l-fields2)))
    (if (<= i-nr-fields gl-i-max-ebv-opt)
	(let* ((wrap1? (not (is-var-ref? x1)))
	       (wrap2? (not (is-var-ref? x2)))
	       (x-obj1
		(if wrap1?
		    (tc-tree-il-var-ref0 linker
					 (linker-alloc-loc linker 'tmp1 #f))
		    x1))
	       (x-obj2
		(if wrap2?
		    (tc-tree-il-var-ref0 linker
					 (linker-alloc-loc linker 'tmp2 #f))
		    x2))
	       (x-body (optimize-eq-by-value1 linker s-kind 1
					      l-fields1
					      l-fields2
					      x-obj1
					      x-obj2))
	       (x-body2
		(if wrap2?
		    `(let (tmp2) (,(caddr x-obj2)) (,x2) ,x-body)
		    x-body))
	       (x-body1
		(if wrap1?
		    `(let (tmp1) (,(caddr x-obj1)) (,x1) ,x-body2)
		    x-body2)))
	  x-body1)
	#f)))

	    
(define (optimize-pair-eq-by-value linker s-kind cl1 x1 cl2 x2)
  (assert (is-tc-pair? cl1))
  (assert (is-tc-pair? cl2))
  (let* ((wrap1? (not (is-var-ref? x1)))
	 (wrap2? (not (is-var-ref? x2)))
	 (x-obj1
	  (if wrap1?
	      (tc-tree-il-var-ref0 linker
				   (linker-alloc-loc linker 'tmp1 #f))
	      x1))
	 (x-obj2
	  (if wrap2?
	      (tc-tree-il-var-ref0 linker
				   (linker-alloc-loc linker 'tmp2 #f))
	      x2))
	 (type11 (get-pair-first-type cl1))
	 (type12 (get-pair-second-type cl1))
	 (type21 (get-pair-first-type cl2))
	 (type22 (get-pair-second-type cl2))
	 (s-call (tree-il-get-call-keyword linker))
	 (x-elem11 `(,s-call (toplevel car) ,x-obj1))
	 (x-elem12 `(,s-call (toplevel cdr) ,x-obj1))
	 (x-elem21 `(,s-call (toplevel car) ,x-obj2))
	 (x-elem22 `(,s-call (toplevel cdr) ,x-obj2))
	 ;; No deeper recursion.
	 (x-test1 (make-equiv-expression1 linker s-kind #f
					  type11 x-elem11
					  type21 x-elem21))
	 (x-test2 (make-equiv-expression1 linker s-kind #f
					  type12 x-elem12
					  type22 x-elem22))
	 (x-body
	  `(if ,x-test1 ,x-test2 (const #f)))
	 (x-body2
	  (if wrap2?
	      `(let (tmp2) (,(caddr x-obj2)) (,x2) ,x-body)
	      x-body))
	 (x-body1
	  (if wrap1?
	      `(let (tmp1) (,(caddr x-obj1)) (,x1) ,x-body2)
	      x-body2)))
    x-body1))


(define (make-equiv-expression2 linker s-kind rec? type1 x-arg1 type2 x-arg2)
  (let* ((s-call (tree-il-get-call-keyword linker))
	 (x-opt
	  ;; Types type1 and type2 are classes here. They are either
	  ;; user defined nonatomic classes, vector classes,
	  ;; custom primitive classes or goops classes.
	  (case s-kind
	    ((equal-objects?)
	     (let ((s-pred1
		    (hashq-ref (hfield-ref linker
					   'ht-equal-objects)
			       type1))
		   (s-pred2
		    (hashq-ref (hfield-ref linker
					   'ht-equal-objects)
			       type2)))
	       ;; The equal-objects? predicate is eqv? for all
	       ;; GOOPS classes.
	       (cond
		((and (not s-pred1) (not s-pred2))
		 ;; Use eqv? instead of eq? so that the predicate
		 ;; is correct in case the runtime type of
		 ;; either argument object is a GOOPS class.
		 `(,s-call (toplevel eqv?) ,x-arg1 ,x-arg2))
		((eq? s-pred1 s-pred2)
		 `(,s-call (toplevel ,s-pred1) ,x-arg1 ,x-arg2))
		(else #f))))
	    ((equal-values?)
	     (let ((s-pred1
		    (hashq-ref (hfield-ref linker
					   'ht-equal)
			       type1))
		   (s-pred2
		    (hashq-ref (hfield-ref linker
					   'ht-equal)
			       type2)))
	       (cond
		((or (tno-field-ref type1 'goops?)
		     (tno-field-ref type2 'goops?))
		 #f)
		((and s-pred1 (eq? s-pred1 s-pred2))
		 ;; If type1 and type2 are custom primitive classes
		 ;; they should be equal here.
		 `(,s-call (toplevel ,s-pred1) ,x-arg1 ,x-arg2))
		((and
		  (not (tno-field-ref type1 'inheritable?))
		  (not (tno-field-ref type2 'inheritable?))
		  ;; Classes type1 and type2 are the same class
		  ;; if the conditions above are true.
		  (not (tno-field-ref type1 'eq-by-value?)))
		 `(,s-call (toplevel eq?) ,x-arg1 ,x-arg2))
		((and
		  rec?
		  (not (tno-field-ref type1 'inheritable?))
		  (not (tno-field-ref type2 'inheritable?))
		  ;; Classes type1 and type2 are the same class
		  ;; if the conditions above are true.
		  (tno-field-ref type1 'eq-by-value?))
		 (optimize-eq-by-value linker s-kind
				       type1 x-arg1
				       type2 x-arg2))
		(else #f))))
	    ((equal-contents?)
	     (let ((s-pred1
		    (hashq-ref (hfield-ref linker
					   'ht-equal-contents)
			       type1))
		   (s-pred2
		    (hashq-ref (hfield-ref linker
					   'ht-equal-contents)
			       type2)))
	       (cond
		((or (tno-field-ref type1 'goops?)
		     (tno-field-ref type2 'goops?))
		 #f)
		((and s-pred1 (eq? s-pred1 s-pred2))
		 `(,s-call (toplevel ,s-pred1) ,x-arg1 ,x-arg2))
		((and
		  rec?
		  (not (tno-field-ref type1 'inheritable?))
		  (not (tno-field-ref type2 'inheritable?)))
		 ;; Classes type1 and type2 are the same class
		 ;; if the conditions above are true.
		 (optimize-eq-by-value linker s-kind
				       type1 x-arg1
				       type2 x-arg2))
		(else #f))))
	    (else (raise 'internal-error)))))
    (if x-opt
	x-opt
	(make-normal-equiv-expression linker s-kind
				      x-arg1 x-arg2))))


(define (make-equiv-expression1 linker s-kind rec? type1 x-arg1 type2 x-arg2)
  (assert (memq s-kind (list 'equal-values? 'equal-objects? 'equal-contents?)))
  (let* ((binder (hfield-ref linker 'binder-instantiation))
	 (cl1? (is-t-instance? binder type1 tc-class))
	 (cl2? (is-t-instance? binder type2 tc-class))
	 (s-call (tree-il-get-call-keyword linker)))
    (if (and cl1? cl2?
	     (not (is-t-subtype? binder type1 type2))
	     (not (is-t-subtype? binder type2 type1)))
	'(const #f)	
	(let* ((s-pred1 (get-equiv-pred s-kind type1))
	       (s-pred2 (get-equiv-pred s-kind type2)))
	  (cond
	   ((or s-pred1 s-pred2) =>
	    (lambda (s-pred)
	      `(,s-call (toplevel ,s-pred) ,x-arg1 ,x-arg2)))
	   ((or (eq? type1 tc-eof) (eq? type2 tc-eof))
	      `(,s-call (toplevel eq?) ,x-arg1 ,x-arg2))	    
	   ((or (not cl1?) (not cl2?))
	    (make-normal-equiv-expression linker s-kind x-arg1 x-arg2))
	   ((and (is-tc-pair? type1) (is-tc-pair? type2))
	    (cond
	     ((eq? s-kind 'equal-objects?)
	      `(,s-call (toplevel eq?) ,x-arg1 ,x-arg2))
	     (rec?
	      (optimize-pair-eq-by-value linker s-kind
					 type1 x-arg1 type2 x-arg2))
	     (else
	      (make-normal-equiv-expression linker s-kind x-arg1 x-arg2))))
	   ((or (is-tc-pair? type1) (is-tc-pair? type2))
	    ;; If we enter here either of types type1 and type2 is <object>.
	    (if (eq? s-kind 'equal-objects?)
		`(,s-call (toplevel eq?) ,x-arg1 ,x-arg2)
		(make-normal-equiv-expression linker s-kind x-arg1 x-arg2)))
	   (else
	    (make-equiv-expression2 linker s-kind rec?
				    type1 x-arg1 type2 x-arg2)))))))


(define (make-equiv-expression linker s-kind ent1 ent2)
  (let* ((x1 (theme-target-tree-il-compile linker ent1))
	 (x2 (theme-target-tree-il-compile linker ent2))
	 (type1 (get-entity-type ent1))
	 (type2 (get-entity-type ent2)))
    (make-equiv-expression1 linker s-kind #t type1 x1 type2 x2)))


(define (tc-tree-il-var-ref0 linker address)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? address <address>))
  (let ((sym-name (get-target-var-name linker address)))
    (if (hfield-ref address 'toplevel?)
	`(toplevel ,sym-name)
	(let ((sym-source-name (hfield-ref address 'source-name)))
	  `(lexical ,sym-source-name ,sym-name)))))


(define (tc-tree-il-var-ref linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <variable-reference>))
  (let* ((variable (hfield-ref repr 'variable))
	 (address (hfield-ref variable 'address)))
    ;; Forward declared variables are always toplevel.
    (if (hfield-ref address 'toplevel?)
	(let ((s-name (get-target-var-name linker address))
	      (s-source-name (hfield-ref address 'source-name))
	      (s-call (tree-il-get-call-keyword linker)))
	  `(,s-call (toplevel check-var-unspecified)
		    (toplevel ,s-name)
		    (const ,s-source-name)))
	(tc-tree-il-var-ref0 linker address))))


(define (tc-tree-il-primitive-object linker repr)
  (assert (hfield-ref repr 'primitive?))
  (cond
   ((is-t-atomic-object? repr)
    (list 'const (get-contents repr)))
   ((not-null? (hfield-ref repr 'address))
    (tc-tree-il-object-with-address linker repr))
   (else
    (list 'const (get-contents repr)))))


(define (tc-tree-il-compile-type-var linker tvar)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (is-t-type-variable? tvar))
  (tc-tree-il-var-ref0 linker (hfield-ref tvar 'address)))


(define (tc-tree-il-generate-type-check linker obj-expr type-expr)
  (assert (is-linker? linker))
  (let ((s-call (tree-il-get-call-keyword linker)))
    (cond
     ((hfield-ref linker 'verbose-typechecks?)
      `(,s-call (toplevel check-type-verbose)
		,obj-expr ,type-expr
		(const ,obj-expr) (const ,type-expr)))
     (else
      `(,s-call (toplevel check-type) ,obj-expr ,type-expr)))))


(define (tc-tree-il-letrec-unspecified-check linker expr s-var-name)
  (let ((s-call (tree-il-get-call-keyword linker)))
    `(,s-call (toplevel check-letrec-unspecified)
	      ,expr
	      (const ,s-var-name))))


(define (tc-tree-il-call-stack-debug linker repr t-body)
  (if (hfield-ref linker 'runtime-pretty-backtrace?)
      (let* ((s-kind (hfield-ref repr 's-kind))
	     (s-name (hfield-ref repr 's-name))
	     (l-module (hfield-ref repr 'l-module))
	     (addr-result (linker-alloc-loc linker 'result #f))
	     (s-gensym (get-target-var-name linker addr-result))
	     (s-call (tree-il-get-call-keyword linker))
	     (s-seq (tree-il-get-seq-keyword linker)))
	`(,s-seq
	   (,s-call (toplevel call-stack-push)
		    (const ,s-kind)
		    (const ,s-name)
		    (const ,l-module))
	   (let (result) (,s-gensym) (,t-body)
		(,s-seq
		 (,s-call (toplevel call-stack-pop))
		 (lexical result ,s-gensym)))))
      t-body))


(define (generate-let*-expression t-names t-gensyms t-values t-body)
  (if (null? t-names)
      t-body
      `(let (,(car t-names)) (,(car t-gensyms)) (,(car t-values))
	    ,(generate-let*-expression (cdr t-names)
				       (cdr t-gensyms)
				       (cdr t-values)
				       t-body))))


(define (tc-tree-il-construct-procedure0 linker lst-source-names
					 lst-gensyms
					 t-body t-arg-descs
					 t-args)
  (let* ((s-call (tree-il-get-call-keyword linker))
	 (addr-parsed (linker-alloc-loc linker 'parsed-args #f))
	 (sym-parsed-gensym (get-target-var-name linker addr-parsed))
	 (t-var-parsed (list 'lexical 'parsed-args sym-parsed-gensym))
	 (lst-indices (get-integer-sequence 0 (length lst-source-names)))
	 (lst-values (map (lambda (i) `(,s-call (toplevel list-ref)
						,t-var-parsed
						(const ,i)))
			  lst-indices))
	 (lst-expr
	  (generate-let*-expression lst-source-names lst-gensyms lst-values
				    t-body)))
    `(let (parsed-args) (,sym-parsed-gensym)
	  ((,s-call (toplevel translate-call-arguments)
		    (,s-call (toplevel list) ,@t-arg-descs) ,t-args))
	  ,lst-expr)))


(define (tc-tree-il-construct-procedure linker repr
					arg-vars t-body t-arg-descs
					t-result-type-desc
					simple-args?
					simple-args-with-tail?
					s-name)
  (let* ((lst-addr (map (lambda (var) (hfield-ref var 'address)) arg-vars))
	 (lst-source-names (map (lambda (address)
				  (hfield-ref address 'source-name))
				lst-addr))
	 (lst-gensyms (map (lambda (address)
			     (get-target-var-name linker address))
			   lst-addr)))
    (cond
     (simple-args?
      (let ((t-body1 (tc-tree-il-call-stack-debug linker repr t-body)))
	`(lambda ,(get-lambda-attr s-name)
	   (lambda-case ((,lst-source-names #f #f #f ()
					    ,lst-gensyms)
			 ,t-body1)))))
     (simple-args-with-tail?
      (let ((t-body1 (tc-tree-il-call-stack-debug linker repr t-body)))
	`(lambda ,(get-lambda-attr s-name)
	   (lambda-case ((,(drop-right lst-source-names 1)
			  #f
			  ,(last lst-source-names)
			  #f ()
			  ,lst-gensyms)
			 ,t-body1)))))
     (else
      (let* ((addr-args (linker-alloc-loc linker 'args #f))
	     (sym-args-gensym (get-target-var-name linker addr-args))
	     (t-args (list 'lexical 'args sym-args-gensym))
	     (t-actual-body
	      (if (not-null? t-args)
		  (tc-tree-il-construct-procedure0 linker lst-source-names
						   lst-gensyms t-body
						   t-arg-descs t-args)
		  t-body))
	     (t-actual-body1 (tc-tree-il-call-stack-debug linker repr
							  t-actual-body)))
	`(lambda ,(get-lambda-attr s-name)
	   (lambda-case ((() #f args #f ()
			  (,sym-args-gensym))
			 ,t-actual-body1))))))))


(define (get-source-names lst-args)
  (map (lambda (arg) (hfield-ref (hfield-ref arg 'address) 'source-name))
       lst-args))


(define (tc-tree-il-param-proc-body-simple-args
	 linker repr l-params t-body l-args s-name)
  (let* ((l-all-args (append l-params l-args))
	 (l-names (get-source-names l-all-args))
	 (l-gensyms (get-gensyms linker l-all-args))
	 (t-body1 (tc-tree-il-call-stack-debug linker repr t-body)))
    `(lambda ,(get-lambda-attr s-name)
       (lambda-case (((,@l-names) #f #f #f () (,@l-gensyms))
		     ,t-body1)))))


(define (tc-tree-il-param-proc-body-simple-args-with-tail
	 linker repr l-params t-body l-args s-name)
  (let* ((l-all-args (append l-params l-args))
	 (l-names (get-source-names l-all-args))
	 (l-normal-names (drop-right l-names 1))
	 (l-rest-name (last l-names))
	 (l-gensyms (get-gensyms linker l-all-args))
	 (t-body1 (tc-tree-il-call-stack-debug linker repr t-body)))
    `(lambda ,(get-lambda-attr s-name)
       (lambda-case (((,@l-normal-names) #f ,l-rest-name #f () (,@l-gensyms))
		     ,t-body1)))))


(define (tc-tree-il-param-proc-body-nonsimple-args
	 linker repr l-params t-body l-args t-arg-descs s-name)
  (let* ((l-param-names (get-source-names l-params))
	 (l-param-gensyms (get-gensyms linker l-params))
	 (l-arg-names (get-source-names l-args))
	 (l-arg-gensyms (get-gensyms linker l-args))
	 (addr-args (linker-alloc-loc linker 'args #f))
	 (s-args-gensym (get-target-var-name linker addr-args))
	 (t-args-ref (list 'lexical 'args s-args-gensym))
	 (t-actual-body
	  (tc-tree-il-construct-procedure0 linker
					   l-arg-names
					   l-arg-gensyms
					   t-body t-arg-descs
					   t-args-ref))
	 (t-actual-body1 (tc-tree-il-call-stack-debug linker repr
						      t-actual-body)))
    `(lambda ,(get-lambda-attr s-name)
       (lambda-case ((,l-param-names #f args #f ()
		      (,@l-param-gensyms ,s-args-gensym))
		     ,t-actual-body1)))))


(define (tc-tree-il-param-proc-body-no-args
	 linker repr l-params t-body s-name)
  (let* ((l-names (get-source-names l-params))
	 (l-gensyms (get-gensyms linker l-params))
	 (t-body1 (tc-tree-il-call-stack-debug linker repr t-body)))
    `(lambda ,(get-lambda-attr s-name)
       (lambda-case ((,l-names #f #f #f ()
			       ,l-gensyms)
		     ,t-body1)))))


(define (tc-tree-il-construct-param-proc-body linker repr l-params t-body
					      l-args
					      t-arg-descs
					      t-result-type-desc
					      s-name
					      no-result?
					      simple-args?
					      simple-args-with-tail?
					      type-dispatched?)
  (assert (is-linker? linker))
  (let ((t-body1
	 (if no-result?
	     t-body
	     (tc-tree-il-generate-type-check linker t-body
					     t-result-type-desc))))
    (cond
     ((null? l-args)
      (tc-tree-il-param-proc-body-no-args
       linker repr l-params t-body1 s-name))
     (simple-args?
      (tc-tree-il-param-proc-body-simple-args
       linker repr l-params t-body1 l-args s-name))
     (simple-args-with-tail?
      (tc-tree-il-param-proc-body-simple-args-with-tail
       linker repr l-params t-body1 l-args s-name))
     (else
      (tc-tree-il-param-proc-body-nonsimple-args
       linker repr l-params t-body1 l-args t-arg-descs s-name)))))


(define (tc-tree-il-compile-proc-expr linker repr l-tvars)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <procedure-expression>))
  (assert (list? l-tvars))
  (let* ((args (hfield-ref repr 'arg-variables))
	 (arg-descs (hfield-ref repr 'arg-descs))
	 (simple? (is-simple-arg-list2? arg-descs))
	 (simple-with-tail? (is-simple-arg-list-with-tail? arg-descs))
	 (c-arg-descs (map (lambda (desc)
			     (theme-target-tree-il-compile linker desc))
			   arg-descs))
	 (body (hfield-ref repr 'body))
	 (c-body (theme-target-tree-il-compile linker body))
	 (type (get-entity-type repr))
	 (c-type (theme-target-tree-il-compile linker type))
	 (result-type (hfield-ref repr 'result-type))
	 (c-result-type (theme-target-tree-il-compile linker result-type))
	 (s-name (hfield-ref repr 's-name))
	 (s-call (tree-il-get-call-keyword linker)))
    `(,s-call (toplevel _i_make-procedure)
	      ,c-type
	      ,(tc-tree-il-construct-procedure linker repr args c-body
					       c-arg-descs
					       c-result-type
					       simple? simple-with-tail?
					       s-name))))


(define (tc-tree-il-compile-param-proc-expr linker repr0 repr l-tvars s-name)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <procedure-expression>))
  (assert (list? l-tvars))
  (let* ((args (hfield-ref repr 'arg-variables))
	 (arg-descs (hfield-ref repr 'arg-descs))
	 (simple? (is-simple-arg-list2? arg-descs))
	 (simple-with-tail? (is-simple-arg-list-with-tail? arg-descs))
	 (c-arg-descs (map (lambda (desc)
			     (theme-target-tree-il-compile linker desc))
			   arg-descs))
	 (body (hfield-ref repr 'body))
	 (c-body (theme-target-tree-il-compile linker body))
	 (type (get-entity-type repr))
	 (c-type (theme-target-tree-il-compile linker type))
	 (result-type (hfield-ref repr 'result-type))
	 (c-result-type (theme-target-tree-il-compile linker result-type)))
    (let ((no-result? (linker-entity-is-none? linker result-type))
	  (type-dispatched? (entity-type-dispatched? body)))
      (tc-tree-il-construct-param-proc-body linker
					    repr0
					    l-tvars
					    c-body args c-arg-descs
					    c-result-type
					    s-name
					    no-result?
					    simple?
					    simple-with-tail?
					    type-dispatched?))))



(define (tc-tree-il-prim-proc-check linker result-type t-result-type t-cl)
  (let ((s-call (tree-il-get-call-keyword linker)))
    (cond
     ((or (not (is-known-object? result-type))
	  (not (is-t-class? result-type)))
      `(,s-call (toplevel is-subtype?) ,t-cl ,t-result-type))
     ((is-t-simple-class? result-type)
      `(,s-call (toplevel is-simple-class-subtype?) ,t-cl ,t-result-type))
     ((and (is-t-param-class-instance? result-type)
	   (not (is-tc-pair? result-type)))
      `(,s-call (toplevel is-param-inst-subclass?) ,t-cl ,t-result-type))
     (else
      `(,s-call (toplevel is-subtype?) ,t-cl ,t-result-type)))))


(define (tc-tree-il-construct-prim-proc-wrapper linker expr)
  (let* ((s-call (tree-il-get-call-keyword linker))
	 (binder (hfield-ref linker 'binder-instantiation))
	 (address (hfield-ref expr 'address))
	 (type-proc (get-entity-type expr))
	 (type-result (tno-field-ref type-proc 'type-result))
	 (type-arglist (tno-field-ref type-proc 'type-arglist))
	 (simple? (is-tuple-type? binder type-arglist))
	 (n-args (if simple? (tuple-type-length binder type-arglist) -1))
	 (l-arg-numbers (if simple? (get-integer-sequence 1 n-args) '()))
	 (l-arg-names (if simple?
			  (map
			   (lambda (i)
			     (string->symbol
			      (string-append "arg" (number->string i))))
			   l-arg-numbers)
			  '()))
	 (l-arg-gensyms
	  (if simple?
	      (map (lambda (s-name)
		     (let ((addr (linker-alloc-loc linker s-name #f)))
		       (get-target-var-name linker addr)))
		   l-arg-names)
	      '()))
	 (t-proc (tc-tree-il-var-ref0 linker address))
	 (t-result-type (theme-target-tree-il-compile linker type-result))
	 (source-name (hfield-ref address 'source-name))
	 (str-source-name (symbol->string source-name))
	 (addr-arguments
	  (if (not simple?)
	      (linker-alloc-loc linker 'arguments #f)
	      '()))
	 (sym-arguments-gensym
	  (if (not simple?)
	      (get-target-var-name linker addr-arguments)
	      '()))
	 (t-arguments
	  (if (not simple?)
	      (list 'lexical 'arguments sym-arguments-gensym)
	      '()))
	 (addr-result (linker-alloc-loc linker 'result #f))
	 (sym-result-gensym (get-target-var-name linker addr-result))
	 (t-result (list 'lexical 'result sym-result-gensym))
	 (texpr-header
	  (if simple?
	      `(,l-arg-names #f #f #f () ,l-arg-gensyms)    
	      `(() #f arguments #f () (,sym-arguments-gensym))))
	 (l-texpr-arglist
	  (if simple?
	      (map (lambda (s-name s-gensym) (list 'lexical s-name s-gensym))
		   l-arg-names l-arg-gensyms)
	      '()))
	 (texpr-call
	  (if simple?
	      `(,s-call ,t-proc ,@l-texpr-arglist)
	      `(,s-call (toplevel apply) ,t-proc ,t-arguments))))
    (if (eq? type-result tc-object)
	`(lambda ()
	   (lambda-case (,texpr-header
			 (let (result)
			   (,sym-result-gensym)
			   (,texpr-call)
			   (if
			    (,s-call (toplevel is-valid-theme-d-object?)
				     ,t-result)
			    ,t-result
			    (,s-call (toplevel _i_invalid-theme-d-object-error)
				     ,t-result
				     (const ,str-source-name)))))))
	(let ((p-prim (assq type-result gl-l-prim-pred))
	      (t-result-type
	       (theme-target-tree-il-compile linker type-result)))
	  (if p-prim
	      `(lambda ()
		 (lambda-case (,texpr-header
			       (let (result)
				 (,sym-result-gensym)
				 (,texpr-call)
				 (if
				  (,s-call (toplevel ,(cdr p-prim))
					   ,t-result)
				  ,t-result
				  (,s-call (toplevel _i_result-type-error)
					   ,t-result
					   ,t-result-type
					   (const ,str-source-name)))))))
	      (let* ((addr-result-type-var
		      (linker-alloc-loc linker 'result-type-var #f))
		     (sym-result-type-var-gensym
		      (get-target-var-name linker addr-result-type-var))
		     (t-result-type-var
		      (list 'lexical 'result-type1 sym-result-type-var-gensym))
		     (addr-cl
		      (linker-alloc-loc linker 'cl-var #f))
		     (sym-cl-gensym
		      (get-target-var-name linker addr-cl))
		     (t-cl
		      (list 'lexical 'cl sym-cl-gensym))
		     (t-check
		      (tc-tree-il-prim-proc-check linker
						  type-result
						  t-result-type-var
						  t-cl)))
		`(lambda ()
		   (lambda-case (,texpr-header
				 (let (result result-type1)
				   (,sym-result-gensym
				    ,sym-result-type-var-gensym)
				   (,texpr-call ,t-result-type)
				   (let (cl)
				     (,sym-cl-gensym)
				     ((,s-call (toplevel theme-class-of0)
					       ,t-result))
				     (if
				      ,t-cl
				      (if
				       ,t-check
				       ,t-result
				       (,s-call (toplevel _i_result-type-error)
						,t-result
						,t-result-type-var
						(const ,str-source-name)))
				      (,s-call
				       (toplevel
					_i_invalid-theme-d-object-error)
				       ,t-result
				       (const ,str-source-name))))))))))))))


(define (tc-tree-il-compile-prim-proc-ref linker repr param-proc? l-tvars)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <prim-proc-ref>))
  (let* ((s-call (tree-il-get-call-keyword linker))
	 (result
	  (if (not param-proc?)
	      (let ((type (get-entity-type repr))
		    (t-var-ref
		     (tc-tree-il-var-ref0 linker
					  (hfield-ref repr 'address))))	
		(cond
		 ((is-tc-simple-proc? type)
		  (list s-call '(toplevel _i_make-procedure)
			(tc-tree-il-object-fwd linker type '() #f)
			t-var-ref))
		 ((is-tc-param-proc? type)
		  `(,s-call (toplevel _i_make-param-proc)
			    ,(tc-tree-il-object-fwd linker type '() #f)
			    ,t-var-ref
			    (const ,(hfield-ref (hfield-ref repr 'address)
						'source-name))))
		 (else
		  (dvar1-set! repr)
		  (raise 'internal-error-with-prim-proc))))
	      (let* ((s-name (get-target-var-name linker
						  (hfield-ref repr 'address)))
		     (addr-args (linker-alloc-loc linker 'args #f))
		     (s-gensym-args (get-target-var-name linker addr-args))
		     (l-source-names (get-source-names l-tvars))
		     (l-gensyms (get-gensyms linker l-tvars)))
		(assert (hfield-ref (hfield-ref repr 'address) 'toplevel?))
		`(lambda () (lambda-case
			     ((,l-source-names #f args () ()
					       (,@l-gensyms ,s-gensym-args))
			      (,s-call (toplevel apply)
				       (toplevel ,s-name)
				       (lexical args ,s-gensym-args)))))))))
    result))


(define (tc-tree-il-param-prim-proc-wrapper linker address result-type
					    l-tvars)
  (assert (hfield-ref address 'toplevel?))
  (let* ((name (get-target-var-name linker address))
	 (t-result-type (theme-target-tree-il-compile linker result-type))
	 (source-name (hfield-ref address 'source-name))
	 (l-source-names (get-source-names l-tvars))
	 (l-gensyms (get-gensyms linker l-tvars))
	 (addr-args (linker-alloc-loc linker 'args #f))
	 (s-gensym-args (get-target-var-name linker addr-args))
	 (s-call (tree-il-get-call-keyword linker))
	 (s-seq (tree-il-get-seq-keyword linker)))
    ;; Maybe we could set the procedure name in the procedure properties.
    (if (linker-entity-is-none? linker result-type)
	`(lambda () (lambda-case
		     ((,l-source-names #f args () ()
				       (,@l-gensyms ,s-gensym-args))
		      (,s-call (toplevel apply)
			       (toplevel ,name)
			       (lexical args ,s-gensym-args)))))
	(let* ((addr-result (linker-alloc-loc linker 'result #f))
	       (s-gensym-result (get-target-var-name linker addr-result))
	       (addr-result-type (linker-alloc-loc linker 'result-type #f))
	       (s-gensym-result-type
		(get-target-var-name linker addr-result-type)))
	  `(lambda () (lambda-case
		       ((,l-source-names #f args () ()
					 (,@l-gensyms ,s-gensym-args))
			(let (result result-type)
			  (,s-gensym-result ,s-gensym-result-type)
			  ((,s-call (toplevel apply)
				    (toplevel ,name)
				    (lexical args ,s-gensym-args))
			   ,t-result-type)
			  (,s-seq
			    (,s-call (toplevel _i_check-result-type)
				     (lexical result ,s-gensym-result)
				     (lexical result-type ,s-gensym-result-type)
				     (const ,(symbol->string source-name)))
			    (lexical result ,s-gensym-result))))))))))


(define (tc-tree-il-param-prim-proc-wrapper2 linker address type result-type
					     l-tvars)
  ;; <none> result type must be taken care by the caller.
  (assert (not (eqv? result-type tt-none)))
  (let* ((s-name (get-target-var-name linker address))
	 (t-type (theme-target-tree-il-compile linker type))
	 (t-result-type (theme-target-tree-il-compile linker result-type))
	 (source-name (hfield-ref address 'source-name))
	 (l-source-names (get-source-names l-tvars))
	 (l-gensyms (get-gensyms linker l-tvars))
	 (l-tvar-refs (map (lambda (s-source-name s-gensym)
			     (list 'lexical s-source-name s-gensym))
			   l-source-names l-gensyms))
	 (addr-args (linker-alloc-loc linker 'args #f))
	 (s-gensym-args (get-target-var-name linker addr-args))
	 (addr-result (linker-alloc-loc linker 'result #f))
	 (s-gensym-result (get-target-var-name linker addr-result))
	 (addr-result-type (linker-alloc-loc linker 'result-type #f))
	 (s-gensym-result-type
	  (get-target-var-name linker addr-result-type))
	 (s-call (tree-il-get-call-keyword linker))
	 (s-seq (tree-il-get-seq-keyword linker)))
    ;; Maybe we could set the procedure name in the procedure properties.
    `(,s-call (toplevel _i_make-param-proc)
	      ,t-type
	      (lambda ()
		(lambda-case ((,l-source-names #f args () ()
					       (,@l-gensyms ,s-gensym-args))
			      (let (result result-type)
				(,s-gensym-result ,s-gensym-result-type)
				((,s-call (toplevel apply)
					  (toplevel ,s-name)
					  (,s-call
					   (toplevel make-list-with-tail)
					   (,s-call (toplevel list)
						    ,@l-tvar-refs)
					   (lexical args ,s-gensym-args)))
				 ,t-result-type)
				(,s-seq
				 (,s-call (toplevel _i_check-result-type)
					  (lexical result ,s-gensym-result)
					  (lexical result-type
						   ,s-gensym-result-type)
					  (const ,(symbol->string source-name)))
				 (lexical result ,s-gensym-result))))))
	      (const ()))))


(define (tc-tree-il-compile-checked-prim-proc linker repr param-proc? l-tvars)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <checked-prim-proc>))
  (assert (boolean? param-proc?))
  (assert (list? l-tvars))
  (let ((type (get-entity-type repr)))
    (strong-assert
     (or (is-tc-simple-proc? type)
	 (is-tc-param-proc? type)))
    (let ((result
	   (cond
	    (param-proc?
	     (let ((result-type (tno-field-ref type 'type-result)))
	       (tc-tree-il-param-prim-proc-wrapper
	     	linker
	     	(hfield-ref repr 'address)
	     	result-type
	     	l-tvars)))
	    ((is-tc-simple-proc? type)
	     ;; We must not check the result value
	     ;; if the result type is <none>.
	     (let ((result-type (tno-field-ref type 'type-result))
		   (t-var-ref
		    (tc-tree-il-var-ref0 linker
					 (hfield-ref repr 'address)))
		   (s-call (tree-il-get-call-keyword linker)))
	       (if (linker-entity-is-none? linker result-type)
		   (list s-call '(toplevel _i_make-procedure)
			 (tc-tree-il-object-fwd linker (get-entity-type repr)
						'() #f)
			 t-var-ref)
		   (list s-call '(toplevel _i_make-procedure)
			 (tc-tree-il-object-fwd linker type '() #f)
			 (tc-tree-il-construct-prim-proc-wrapper
			  linker repr)))))
	    ((is-tc-param-proc? type)
	     (let* ((result-type
		     (tno-field-ref (tno-field-ref type 'type-contents)
				    'type-result))
	     	    (l-tvars (tno-field-ref type 'l-tvars)))
	       (if (linker-entity-is-none? linker result-type)
	     	   (let* ((s-name (get-target-var-name
				   linker
				   (hfield-ref repr 'address)))
			  (t-type (tc-tree-il-object-fwd
				   linker
				   (get-entity-type repr)))
			  (s-call (tree-il-get-call-keyword linker)))
		     `(,s-call (toplevel _i_make-param-proc)
			       ,t-type
			       ,s-name
			       ,s-name))
	     	   (tc-tree-il-param-prim-proc-wrapper2
	     	    linker
	     	    (hfield-ref repr 'address)
	     	    type
	     	    result-type
	     	    l-tvars))))
	    (else (raise 'internal-error-1)))))
      result)))


(define (tc-tree-il-class-field-texpr linker field)
  (let* ((name (tno-field-ref field 's-name))
	 (type (theme-target-tree-il-compile linker
					     (tno-field-ref field 'type)))
	 (read-access (tno-field-ref field 's-read-access))
	 (write-access (tno-field-ref field 's-write-access))
	 (has-init-value? (tno-field-ref field 'has-init-value?))
	 (r-init-value
	  (if has-init-value?
	      (tno-field-ref field 'x-init-value)
	      '()))
	 (t-init-value
	  (if has-init-value?
	      (theme-target-tree-il-compile linker r-init-value)
	      '(const ()))))
    (list `(const ,name) type `(const ,read-access) `(const ,write-access)
	  `(const ,has-init-value?) t-init-value)))


(define (tc-tree-il-class-field-texprs linker fields)
  (let* ((s-call (tree-il-get-call-keyword linker))
	 (l-texprs
	  (map (lambda (fld)
		 `(,s-call (toplevel make-field)   
			   ,@(tc-tree-il-class-field-texpr linker fld)))
	       fields)))
    `(,s-call (toplevel list) ,@l-texprs)))


(define (tc-tree-il-get-initializer-body linker object-arg fields
					 l-field-arg-names
					 l-field-arg-gensyms)
  (let ((body '())
	(i-arg 0)
	(s-call (tree-il-get-call-keyword linker)))
    (do ((i 1 (+ i 1)) (cur-lst fields (cdr cur-lst)))
	((null? cur-lst))
      (let* ((cur-field (car cur-lst))
	     (has-init-value? (tno-field-ref cur-field 'has-init-value?))
	     (cur-init-value
	      (if has-init-value?
		  (tno-field-ref cur-field 'x-init-value)
		  '())))
	(if (null? cur-init-value)
	    (let* ((s-cur-arg-name (list-ref l-field-arg-names i-arg))
		   (s-gensym-cur-arg (list-ref l-field-arg-gensyms i-arg))
		   (t-cur-arg-ref (list 'lexical
					s-cur-arg-name
					s-gensym-cur-arg)))		  
	      (set! body
		    (append body
			    (list
			     `(,s-call (toplevel vector-set!)
				       ,object-arg
				       (const ,i)
				       ,t-cur-arg-ref))))
	      (set! i-arg (+ i-arg 1)))
	    (let ((t-contents (theme-target-tree-il-compile
			       linker cur-init-value)))
	      (set! body
		    (append body
			    (list
			     `(,s-call (toplevel vector-set!)
				       ,object-arg
				       (const ,i)
				       ,t-contents))))))))
    body))


(define (tc-tree-il-get-constructor-body
	 linker t-class fields l-field-arg-names l-field-arg-gensyms)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (is-target-object? t-class))
  (assert (list? fields))
  (assert (list? l-field-arg-names))
  (assert (list? l-field-arg-gensyms))
  (let* ((s-call (tree-il-get-call-keyword linker))
	 (s-seq (tree-il-get-seq-keyword linker))
	 (class-texpr (tc-tree-il-var-ref0
		       linker
		       (hfield-ref t-class 'address)))
	 (addr-result (linker-alloc-loc linker 'result #f))
	 (s-gensym-result (get-target-var-name linker addr-result))
	 (class-init-texpr
	  `(,s-call (toplevel vector-set!)
		    (lexical result ,s-gensym-result)
		    (toplevel i-object-class)
		    ,class-texpr))
	 (initializer-texprs
	  (tc-tree-il-get-initializer-body
	   linker
	   (list 'lexical 'result s-gensym-result)
	   fields
	   l-field-arg-names
	   l-field-arg-gensyms))
	 (field-count (length fields))
	 (texpr-sub
	  (tc-tree-il-compile-sequence
	   linker
	   `(,class-init-texpr
	     ,@initializer-texprs
	     (lexical result ,s-gensym-result))))
	 (result
	  ;; Vektorin ensimmäinen alkio on olion tyyppi.
	  `(let (result) (,s-gensym-result)
		((,s-call (toplevel make-vector)
			  (const ,(+ field-count 1))
			  (const ())))
		,texpr-sub)))
    result))


(define (tc-tree-il-get-constructor-def linker to-class t-type)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (is-target-object? to-class))
  (let* ((fields (tno-field-ref to-class 'l-all-fields))
	 (l-field-arg-names (get-constructor-field-args fields))
	 (l-field-arg-addresses
	  (map* (lambda (s-name) (linker-alloc-loc linker s-name #f))
		l-field-arg-names))
	 (l-field-arg-gensyms
	  (map (lambda (address) (get-target-var-name linker address))
	       l-field-arg-addresses))
	 (binder (get-binder-for-inst linker))
	 (body (tc-tree-il-get-constructor-body linker to-class
						fields
						l-field-arg-names
						l-field-arg-gensyms))
	 (proc
	  `(lambda () (lambda-case
		       ((,l-field-arg-names #f #f () () ,l-field-arg-gensyms)
			,body))))
	 (s-call (tree-il-get-call-keyword linker)))
    `(,s-call (toplevel _i_make-procedure) ,t-type ,proc)))


(define (tc-tree-il-declared-var-def linker addr t-expr r-type read-only?)
  (assert (is-target-object? r-type))
  (let ((t-var (tc-tree-il-var-ref0 linker addr))
	(addr-raw (address-hash-ref (get-ht-raw-procs linker) addr))
	(s-call (tree-il-get-call-keyword linker)))
    (cond
     (addr-raw
      (let ((t-raw (tc-tree-il-var-ref0 linker addr-raw)))
	`(_splice
	  (,s-call (toplevel vector-copy-contents)
		   ,t-expr
		   ,t-var)
	  (set! ,t-raw
		(,s-call (toplevel vector-ref)
			 ,t-var
			 (const 1))))))
     ((is-primitive-class? r-type)
      `(set! ,t-var ,t-expr))
     ((is-pair-class? r-type)
      `(,s-call (toplevel set-cons!) ,t-var ,t-expr))
     ((not read-only?) `(set! ,t-var ,t-expr)) 
     (else
      `(,s-call (toplevel vector-copy-contents)
		,t-expr
		,t-var)))))


(define (tc-tree-il-nondeclared-var-def linker repr)
  (let* ((variable (hfield-ref repr 'variable))
	 (type (get-entity-type variable))
	 (address (hfield-ref variable 'address))
	 (s-var-name (get-target-var-name linker address))
	 (t-expr1
	  `(define
	     ,s-var-name
	     ,(theme-target-tree-il-compile
	       linker
	       (hfield-ref repr 'value-expr)))))
    (if (and (hfield-ref variable 'read-only?)
	     (is-t-general-proc-type? type))
	(let* ((addr-raw (linker-alloc-loc linker 'raw #t)) 
	       (s-raw-name (get-target-var-name linker addr-raw))
	       (s-call (tree-il-get-call-keyword linker)))
	  (address-hash-set! (get-ht-raw-procs linker)
			     (hfield-ref variable 'address)
			     addr-raw)
	  `(_splice ,t-expr1
		    (define ,s-raw-name
		      (,s-call (toplevel vector-ref)
			       (toplevel ,s-var-name)
			       (const 1)))))
	t-expr1)))


(define (tc-tree-il-var-def0 linker value-expr)
  (if (is-target-object? value-expr)
      (tc-tree-il-object-fwd linker value-expr '() #t)
      (theme-target-tree-il-compile linker value-expr)))


(define (get-param-proc-instance0
	 linker
	 s-gensym-arguments
	 t-inst-type
	 t-param-proc
	 t-params)
  (let ((s-call (tree-il-get-call-keyword linker)))
    `(,s-call (toplevel _i_make-procedure)
	      ,t-inst-type
	      (lambda ()
		(lambda-case
		 ((() #f arguments () ()
		   (,s-gensym-arguments))
		  (,s-call (toplevel apply)
			   (,s-call (toplevel vector-ref)
				    ,t-param-proc
				    (toplevel i-param-proc-raw-proc))
			   (,s-call (toplevel append)
				    (,s-call (toplevel list) ,@t-params)
				    (lexical arguments
					     ,s-gensym-arguments)))))))))


;; The following procedure works also for <expr-param-proc-dispatch>.
;; The following procedure may be useless since <expr-param-proc-instance>'s
;; and <expr-param-proc-dispatch>'s are replaced by variable references
;; when type variables are bound for the target compilation (?).
(define (tc-tree-il-param-proc-instance1 linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (or (hrecord-is-instance? repr <expr-param-proc-instance>)
	      (hrecord-is-instance? repr <expr-param-proc-dispatch>)))
  (let* ((r-param-proc (hfield-ref repr 'param-proc))
	 (r-params (hfield-ref repr 'params))
	 (param-cache (hfield-ref linker 'param-cache-instantiation))
	 (binder (get-binder-for-tc linker))
	 (r-inst (param-cache-fetch param-cache
				    r-param-proc r-params)))
    (if (not (eqv? r-inst #f))
	(let* ((r-inst-type (get-entity-type (cdr r-inst)))
	       (t-inst-type (theme-target-tree-il-compile linker r-inst-type))
	       (t-var-ref
		  (tc-tree-il-var-ref0
		   linker
		   (hfield-ref (cdr r-inst) 'address)))
	       (s-call (tree-il-get-call-keyword linker)))
	  `(,s-call (toplevel _i_make-procedure)
		    ,t-inst-type
		    ,t-var-ref))
	;; We should enter here only when compiling parametrized procedures.
	(let* ((r-inst-type (get-entity-type repr))
	       (t-inst-type (theme-target-tree-il-compile-fwd
			     linker r-inst-type))
	       (t-param-proc (theme-target-tree-il-compile-fwd
			      linker r-param-proc))
	       (t-params (map* (lambda (r-param)
				 (theme-target-tree-il-compile-fwd
				  linker r-param))
			       r-params))
	       (addr-arguments (linker-alloc-loc linker 'arguments #f))
	       (s-gensym-arguments
		(get-target-var-name linker addr-arguments)))
	  (get-param-proc-instance0 linker s-gensym-arguments t-inst-type
				    t-param-proc t-params)))))


(define (tc-tree-il-param-proc-instance-expr linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (or (hrecord-is-instance? repr <expr-param-proc-instance>)
	      (hrecord-is-instance? repr <expr-param-proc-dispatch>)))
  (let* ((r-inst-type (get-entity-type repr))
	 (t-inst-type (theme-target-tree-il-compile-fwd linker r-inst-type))
	 (r-param-proc (hfield-ref repr 'param-proc))
	 (r-params (hfield-ref repr 'params))
	 (t-param-proc (theme-target-tree-il-compile-fwd linker r-param-proc))
	 (t-params (map* (lambda (r-param)
			   (theme-target-tree-il-compile-fwd linker r-param))
			 r-params))
	 (addr-arguments (linker-alloc-loc linker 'arguments #f))
	 (s-gensym-arguments
	  (get-target-var-name linker addr-arguments))
	 (s-call (tree-il-get-call-keyword linker)))
    `(,s-call (toplevel _i_make-procedure)
	      ,t-inst-type
	      (lambda ()
		(lambda-case
		 ((() #f arguments () () (,s-gensym-arguments))
		  (,s-call (toplevel apply)
			   (,s-call (toplevel vector-ref)
				    ,t-param-proc
				    (toplevel i-param-proc-raw-proc))
			   (,s-call (toplevel append)
				    (,s-call (toplevel list) ,@t-params)
				    (lexical arguments
					     ,s-gensym-arguments)))))))))


(define (tc-tree-il-var-def linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <variable-definition>))
  (if (var-def-is-used? linker repr)
      (let* ((variable (hfield-ref repr 'variable))
	     (address (hfield-ref variable 'address))
	     (read-only? (hfield-ref variable 'read-only?))
	     (result
	      (if (hfield-ref repr 'declared?)
		  (let ((t-expr
			 (theme-target-tree-il-compile
			  linker
			  (hfield-ref repr 'value-expr)))
			(r-type
			 (get-entity-type (hfield-ref repr 'variable))))
		    (tc-tree-il-declared-var-def linker address t-expr r-type
						 read-only?))
		  (tc-tree-il-nondeclared-var-def linker repr))))
	(assert (hfield-ref address 'toplevel?))
	result)
      (begin
	'(void))))


(define (tc-tree-il-set-expr linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <set-expression>))
  (let* ((address (hfield-ref (hfield-ref repr 'variable) 'address))
	 (t-var (tc-tree-il-var-ref0 linker address)))
    (if (not (hfield-ref linker 'tcomp-inside-param-proc?))
	`(set! ,t-var
	       ,(theme-target-tree-il-compile
		 linker
		 (hfield-ref repr 'value-expr)))
	(let* ((r-var-type (get-entity-type (hfield-ref repr 'variable)))
	       (t-var-type (theme-target-tree-il-compile linker r-var-type))
	       (r-value-expr (hfield-ref repr 'value-expr))
	       (t-value-expr
		(theme-target-tree-il-compile linker r-value-expr))
	       (t-final-value
		(if (entity-type-dispatched? r-value-expr)
		    t-value-expr
		    (tc-tree-il-generate-type-check
		     linker t-value-expr t-var-type))))
	  `(set! ,t-var ,t-final-value)))))


(define (make-default-proc linker s-name)
  (if (hfield-ref linker 'verbose-unlinked-procedures?)
      (let* ((addr-args (linker-alloc-loc linker 'args #f))
	     (s-args-gensym (get-target-var-name linker addr-args))
	     (s-call (tree-il-get-call-keyword linker)))
	`(lambda ()
	   (lambda-case ((() #f args #f () (,s-args-gensym))
			 (,s-call (toplevel proc-not-linked)
				  (const ,s-name))))))
      '(const ())))


(define (tc-tree-il-fw-decl linker repr)
  (if (or (hfield-ref repr 'redecl?)
	  (and (hfield-ref linker 'strip?)
	       (not (decl-is-used? linker repr)))) 
      '(void)
      (let* ((var (hfield-ref repr 'variable))
	     (var-name (get-target-var-name-for-loc linker var))
	     (type (get-entity-type var))
	     (s-call (tree-il-get-call-keyword linker)))
	(cond
	 ((and (hfield-ref var 'read-only?)
	       (is-t-general-proc-type? type))
	  (let* ((addr-raw (linker-alloc-loc linker 'raw #t))
		 (s-raw-name (get-target-var-name linker addr-raw)) 
		 (nr-of-fields (length (tno-field-ref type 'l-all-fields)))
		 (x-default-impl
		  (make-default-proc
		   linker
		   (hfield-ref (hfield-ref var 'address) 'source-name))))
	    (assert (= nr-of-fields 1))
	    (address-hash-set! (get-ht-raw-procs linker)
			       (hfield-ref var 'address)
			       addr-raw)
	    `(_splice
	      (define ,var-name
		(,s-call (toplevel vector)
			 (toplevel _b_unspecified)
			 ,x-default-impl))
	      (define ,s-raw-name ,x-default-impl))))
	 ((is-primitive-class? type)
	  `(define ,var-name (toplevel _b_unspecified)))
	 ((is-pair-class? type)
	  `(define ,var-name (,s-call (toplevel cons)
				      (toplevel _b_unspecified)
				      (toplevel _b_unspecified))))
	 ((not (hfield-ref var 'read-only?))
	  `(define ,var-name (toplevel _b_unspecified)))
	 (else
	  (let ((binder (get-binder-for-tc linker)))
	    (assert (is-t-instance? binder type tc-class))
	    (let ((nr-of-fields (length (tno-field-ref type 'l-all-fields))))
	      `(define ,var-name
		 (,s-call (toplevel make-vector) (const ,(+ nr-of-fields 1))
			  (toplevel _b_unspecified))))))))))


(define (tc-tree-il-prim-proc-ref linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <prim-proc-ref>))
  (tc-tree-il-compile-prim-proc-ref linker repr #f '()))


(define (tc-tree-il-checked-prim-proc linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <checked-prim-proc>))
  (tc-tree-il-compile-checked-prim-proc linker repr #f '()))


(define (tc-tree-il-simple-proc-appl linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <proc-appl>))
  (assert (is-t-instance? 
	   (get-binder-for-tc linker)
	   (get-entity-type (hfield-ref repr 'proc))
	   tpc-simple-proc))
  (let* ((args (hfield-ref repr 'arglist))
	 (comp
	  (lambda (arg)
	    (theme-target-tree-il-compile linker arg)))
	 (proc (hfield-ref repr 'proc)))
    (cond
     ;; The following test is an optimization.
     ((and
       (hrecord-is-instance? proc <expr-constructor>)
       (let ((to-class (hfield-ref proc 'clas))
	     (binder (get-binder-for-tc linker)))
	 (is-t-instance? binder to-class tpc-pair)))
      (let ((s-call (tree-il-get-call-keyword linker))
	    (t-args (map* comp args)))
	(strong-assert (= (length t-args) 2))
	`(,s-call (toplevel cons) ,@t-args)))
     ((eq? proc tp-is-instance)
      (strong-assert (= (length args) 2))
      ;; The object is compiled but the type is not.
      (let ((t-obj (comp (car args)))
	    (expr-type (cadr args)))
	(make-instance-test linker t-obj expr-type)))
     ((eq? proc tp-is-subtype)
      (strong-assert (= (length args) 2))
      (let ((ent-type1 (car args))
	    (ent-type2 (cadr args)))
	(make-subtype-test linker ent-type1 ent-type2)))
     ((eq? proc tp-equal-values)
      (strong-assert (= (length args) 2))
      ;; Arguments are not compiled here.
      (make-equiv-expression linker 'equal-values? (car args) (cadr args)))
     ((eq? proc tp-equal-objects)
      (strong-assert (= (length args) 2))
      ;; Arguments are not compiled here.
      (make-equiv-expression linker 'equal-objects? (car args) (cadr args)))
     ((eq? proc tp-equal-contents)
      (strong-assert (= (length args) 2))
      ;; Arguments are not compiled here.
      (make-equiv-expression linker 'equal-contents? (car args) (cadr args)))
     (else
      (let* ((t-proc (theme-target-tree-il-compile linker proc))
	     (t-args (map* comp args))
	     (s-call (tree-il-get-call-keyword linker)))
	(if (and (not (hfield-ref repr 'runtime-arglist-typecheck?))
		 (and-map? entity-type-dispatched? args))
	    (let* ((addr1 (if (is-known-object? proc)
			      (tno-field-ref proc 'addr-raw-proc)
			      '()))
		   (addr-raw
		    (if (not-null? addr1)
			addr1
			(let ((address (hfield-ref proc 'address)))
			  (and (not-null? address)
			       (address-hash-ref
				(get-ht-raw-procs linker)
				address))))))
	      (if addr-raw
		  ;; A raw procedure address is always toplevel.
		  (let ((s-var-name (get-target-var-name linker addr-raw)))
		    `(,s-call (toplevel ,s-var-name) ,@t-args))
		  `(,s-call (,s-call (toplevel vector-ref) ,t-proc (const 1))
			    ,@t-args)))
	    (let* ((addr-proc (linker-alloc-loc linker 'proc #f))
		   (sym-proc-gensym (get-target-var-name linker addr-proc))
		   (t-proc-var (list 'lexical 'proc sym-proc-gensym))
		   (s-proc-name
		    (let ((address (hfield-ref proc 'address)))
		      (if (not-null? address)
			  (hfield-ref address 'source-name)
			  '()))))
	      `(let (proc) (,sym-proc-gensym) (,t-proc)
		    (,s-call (toplevel apply)
			     (,s-call (toplevel vector-ref)
				      ,t-proc-var
				      (const 1))
			     (,s-call (toplevel check-arglist-type)
				      ,t-proc-var
				      (,s-call (toplevel list)
					       ,@t-args)
				      (const ,s-proc-name)))))))))))


(define (tc-tree-il-apply-expr linker repr)
  (let* ((arglist (hfield-ref repr 'arglist))
	 (proc (car arglist))
	 (arglist2 (cadr arglist))
	 (comp (lambda (rexpr) (theme-target-tree-il-compile linker rexpr)))
	 (t-proc (comp proc))
	 (t-arglist2 (comp arglist2))
	 (s-call (tree-il-get-call-keyword linker)))
    (if (is-tc-simple-proc? (get-entity-type proc))
	`(,s-call (toplevel apply)
		  (,s-call (toplevel vector-ref)
			   ,t-proc
			   (toplevel i-simple-proc-raw-proc))
		  ,t-arglist2)
	(let* ((static-arg-types (hfield-ref repr 'static-arg-types))
	       (t-static
		(if (list? static-arg-types)
		    (let ((t (map* comp static-arg-types)))
		      `(,s-call (toplevel list) ,@t))
		    (comp static-arg-types))))
	  `(,s-call (toplevel _i_call-proc)
		    ,t-proc
		    ,t-arglist2
		    (,s-call (toplevel cadr) ,t-static))))))


(define (tc-tree-il-param-proc-appl linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <proc-appl>))
  (assert (target-type=?
	   (get-entity-type (get-entity-type (hfield-ref repr 'proc)))
	   tpc-param-proc))
  (let* ((args (hfield-ref repr 'arglist))
	 (comp (lambda (rexpr) (theme-target-tree-il-compile linker rexpr)))
	 (t-args (map* comp args))
	 (param-proc (hfield-ref repr 'proc))
	 (t-param-proc (comp param-proc))
	 (static-arg-types (hfield-ref repr 'static-arg-types))
	 (s-call (tree-il-get-call-keyword linker))
	 ;; static-arg-types may be a single variable
	 ;; because of optimization.
	 (t-static
	  (cond
	   ((null? static-arg-types)
	    '(const ()))
	   ((list? static-arg-types)
	    (let ((t (map* comp static-arg-types)))
	      `(,s-call (toplevel list) ,@t)))
	   (else
	    (comp static-arg-types))))
	 (result
	  `(,s-call (toplevel _i_call-param-proc)
		    ,t-param-proc
		    (,s-call (toplevel list) ,@t-args)
		    ,t-static)))
    result))


(define (tc-tree-il-checked-abst-proc-appl linker t-proc t-args t-static)
  (let* ((s-call (tree-il-get-call-keyword linker))
	 (addr-proc
	  (linker-alloc-loc linker 'proc #f))
	 (s-gensym-proc
	  (get-target-var-name linker addr-proc))
	 (addr-args
	  (linker-alloc-loc linker 'args #f))
	 (s-gensym-args
	  (get-target-var-name linker addr-args))
	 (addr-static
	  (linker-alloc-loc linker 'static-types #f))
	 (s-gensym-static
	  (get-target-var-name linker addr-static)))
    `(let (prog args static-types)
       (,s-gensym-proc ,s-gensym-args ,s-gensym-static)
       (,t-proc (,s-call (toplevel list) ,@t-args) ,t-static)
       (if (,s-call (toplevel is-instance-of-simple-class?)
		    (,s-call (toplevel vector-ref)
			     (lexical proc ,s-gensym-proc)
			     (toplevel i-object-class))
		    (toplevel _b_:simple-proc))
	   (,s-call (toplevel _i_call-proc)
		    (lexical proc ,s-gensym-proc)
		    (,s-call (toplevel check-arglist-type)
			     (lexical proc ,s-gensym-proc)
			     (lexical args ,s-gensym-args)
			     (const ()))
		    (lexical static-types ,s-gensym-static))
	   (,s-call (toplevel _i_call-proc)
		    (lexical proc ,s-gensym-proc)
		    (lexical args ,s-gensym-args)
		    (lexical static-types ,s-gensym-static))))))

	   
(define (tc-tree-il-abstract-proc-appl linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <proc-appl>))
  (let ((to-type (get-entity-type (hfield-ref repr 'proc)))
	(binder (get-binder-for-tc linker)))
    (strong-assert (is-t-instance? binder to-type tmt-procedure))
    (let* ((args (hfield-ref repr 'arglist))
	   (t-args (map (lambda (arg)
			  (theme-target-tree-il-compile linker arg))
			args))
	   (proc (hfield-ref repr 'proc))
	   (s-call (tree-il-get-call-keyword linker))
	   (result
	    ;; The following test is an optimization.
	    ;; It has probably no effect with abstract procedure applications.
	    (if (and
		 (hrecord-is-instance? proc <expr-constructor>)
		 (let ((to-class (hfield-ref proc 'clas)))
		   (and (not-null? to-class)
			(is-t-instance? binder to-class tpc-pair))))
		`(,s-call (toplevel cons) ,@t-args)
		(let* ((t-proc (theme-target-tree-il-compile linker proc))
		       ;; (static-arg-types
		       ;; (map (lambda (arg) (hfield-ref arg 'type)) args))
		       (comp (lambda (rexpr)
			       (theme-target-tree-il-compile linker rexpr)))
		       ;; (t-static-arg-types (map* comp static-arg-types)))
		       (static-arg-types (hfield-ref repr 'static-arg-types))
		       ;; static-arg-types may be a single variable
		       ;; because of optimization.
		       (t-static
			(if (list? static-arg-types)
			    (let ((t (map* comp static-arg-types)))
			      `(,s-call (toplevel list) ,@t))
			    (comp static-arg-types))))
		  (if (and (not (hfield-ref repr 'runtime-arglist-typecheck?))
			   (and-map? entity-type-dispatched? args))
		      `(,s-call (toplevel _i_call-proc)
				,t-proc
				(,s-call (toplevel list) ,@t-args)
				,t-static)
		      (tc-tree-il-checked-abst-proc-appl
		       linker t-proc t-args t-static))))))
      result)))


(define (tc-tree-il-generic-proc-appl linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <proc-appl>))
  (assert (is-tc-gen-proc?
	   (get-entity-type (hfield-ref repr 'proc))))
  (let* ((t-proc (theme-target-tree-il-compile linker (hfield-ref repr 'proc)))
	 (t-arglist
	  (map* (lambda (repr-arg)
		  (theme-target-tree-il-compile linker repr-arg))
		(hfield-ref repr 'arglist)))
	 (s-call (tree-il-get-call-keyword linker))
	 (t-expr
	 `(,s-call (toplevel _i_call-generic-proc)
		   ,t-proc
		   (,s-call (toplevel list)
			    ,@t-arglist))))
    ;; (if (and (entity-type-dispatched? repr)
    ;; 	     (not (linker-entity-is-none? linker (get-entity-type repr))))
    ;; 	(let ((t-type-expr (theme-target-tree-il-compile
    ;; 			    linker (get-entity-type repr))))
    ;; 	  (tc-tree-il-generate-type-check linker t-expr t-type-expr))
    ;; 	t-expr)))
    t-expr))


(define (tc-tree-il-proc-appl linker repr)
  (let* ((binder (get-binder-for-tc linker))
	 (type (get-entity-type (hfield-ref repr 'proc)))
	 (result
	  (cond
	   ((and
	     (is-pure-entity? (hfield-ref repr 'proc))
	     (is-apply-proc? (hfield-ref repr 'proc)))
	    (tc-tree-il-apply-expr linker repr))
	   ((null? type)
	    (tc-tree-il-abstract-proc-appl linker repr))
	   ((is-t-instance? binder type tpc-simple-proc)
	    (tc-tree-il-simple-proc-appl linker repr))
	   ((is-t-instance? binder type tpc-param-proc)
	    (tc-tree-il-param-proc-appl linker repr))
	   ((is-t-instance? binder type tmc-gen-proc)
	    (tc-tree-il-generic-proc-appl linker repr))
	   ((is-t-instance? binder type tmt-procedure)
	    (tc-tree-il-abstract-proc-appl linker repr))
	   (else
	    (dvar1-set! repr)
	    (raise 'internal-error-in-procedure-application)))))
    result))


(define (tc-tree-il-proc-expr linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <procedure-expression>))
  (tc-tree-il-compile-proc-expr linker repr '()))


(define (tc-tree-il-param-proc-body linker repr body l-tvars s-name)
  (assert (hrecord-is-instance? linker <linker>))
  (assert
   (or (hrecord-is-instance? body <procedure-expression>)
       (hrecord-is-instance? body <prim-proc-ref>)
       (hrecord-is-instance? body <checked-prim-proc>)))
  (assert (list? l-tvars))
  (cond
   ((hrecord-is-instance? body <procedure-expression>)
    (tc-tree-il-compile-param-proc-expr linker repr body l-tvars s-name))
   ;; Should we remove the following?
   ((hrecord-is-instance? body <prim-proc-ref>)
    (tc-tree-il-compile-prim-proc-ref linker body #t l-tvars))
   ((hrecord-is-instance? body <checked-prim-proc>)
    (tc-tree-il-compile-checked-prim-proc linker body #t l-tvars))
   (else
    ;; We should never arrive here.
    (raise 'internal-error-2))))


(define (tc-tree-il-param-proc-expr linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <param-proc-expr>))
  (let ((inside-param-proc-old? (hfield-ref linker 'tcomp-inside-param-proc?)))
    (hfield-set! linker 'tcomp-inside-param-proc? #t)
    (let* ((to-type (get-entity-type repr))
	   (vcomp (lambda (argvar)
		    (get-target-var-name linker
					 (hfield-ref argvar 'address))))
	   (l-params (hfield-ref repr 'type-variables))
	   (t-type (theme-target-tree-il-compile linker to-type))
	   (to (hfield-ref repr 'to-value))
	   (s-name (if (not-null? to) (tno-field-ref to 's-name) '()))
	   (t-proc-body (tc-tree-il-param-proc-body
			 linker repr (hfield-ref repr 'body) l-params s-name))
	   (binder (get-binder-for-tc linker))
	   (s-call (tree-il-get-call-keyword linker)))
      (assert (is-t-instance? binder to-type tpc-param-proc))
      (hfield-set! linker 'tcomp-inside-param-proc? inside-param-proc-old?)
      `(,s-call (toplevel _i_make-param-proc)
		,t-type
		,t-proc-body
		(const ,s-name)))))


(define (tc-tree-il-if linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <if-form>))
  (let* ((condition (hfield-ref repr 'condition))
	 (then-expr (hfield-ref repr 'then-expr))
	 (else-expr (hfield-ref repr 'else-expr))
	 (comp (lambda (repr1) (theme-target-tree-il-compile linker repr1)))
	 (t-condition
	  (if (or (entity-type-dispatched? condition)
		  (not (hfield-ref repr 'boolean-cond?)))
	      (comp condition)
	      (tc-tree-il-generate-type-check linker
					      (comp condition)
					      (comp tc-boolean)))))
    (cond
     ((equal? t-condition '(const #t)) (comp then-expr))
     ((equal? t-condition '(const #f)) (comp else-expr))
     (else
      `(if ,t-condition ,(comp then-expr) ,(comp else-expr))))))


;; HUOM: Jos paluuarvo on tyhjä, niin
;; semantiikka voi poiketa Schemen do-lauseesta
;; vastaavassa tapauksessa.
(define (tc-tree-il-until linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <until-form>))
  (let ((condition (hfield-ref repr 'condition))
	(result (hfield-ref repr 'result))
	(body (hfield-ref repr 'body))
	(comp (lambda (repr1) (theme-target-tree-il-compile linker repr1))))
    ;; <empty-expression> puuttuville arvoille
    ;; ehto ei saa puuttua
    (assert (not-null? condition))
    (assert (not (is-empty-expr? condition)))
    (assert (not-null? result))
    (assert (not-null? body))
    (let* ((t-condition
	    (if (entity-type-dispatched? condition)
		(comp condition)
		(tc-tree-il-generate-type-check linker (comp condition)
						(comp tc-boolean))))
	   (t-result (comp result))
	   (t-body (if (not (is-empty-expr? body)) (comp body) '()))
	   (addr-loop (linker-alloc-loc linker 'loop #f))
	   (s-gensym-loop (get-target-var-name linker addr-loop))
	   (s-call (tree-il-get-call-keyword linker))
	   (s-seq (tree-il-get-seq-keyword linker)))
     `(letrec (loop)
	(,s-gensym-loop)
	((lambda ((name . loop))
	   (lambda-case
	    ((() #f #f #f () ())
	     (if ,t-condition
		 ,t-result
		 (,s-seq
		  ,t-body
		  (,s-call (lexical loop ,s-gensym-loop))))))))
	(,s-call (lexical loop ,s-gensym-loop))))))


(define (tc-tree-il-compound linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <compound-expression>))
  (let* ((subexprs (hfield-ref repr 'subexprs))
	 (comp (lambda (repr1) (theme-target-tree-il-compile linker repr1)))
	 (t-subexprs (map comp subexprs)))
    (tc-tree-il-compile-sequence linker t-subexprs)))


(define (tc-tree-il-parse-let-variable linker var)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (is-letvar? var))
  (let* ((variable (cadr var))
	 (type (list-ref var 3))
	 (init-expr (list-ref var 4)))
    (assert (hrecord-is-instance? variable <normal-variable>))
    (assert (hrecord-is-instance? init-expr <entity>))
    (let* ((address (hfield-ref variable 'address))
	   (t-var-name (get-target-var-name linker address))
	   (s-source-name (hfield-ref address 'source-name)))
      (if (hfield-ref linker 'tcomp-inside-param-proc?)
	  (let ((type-check
		 (if (or (null? type)
			 (entity-type-dispatched? init-expr))
		     (theme-target-tree-il-compile linker init-expr)
		     (tc-tree-il-generate-type-check
		      linker
		      (theme-target-tree-il-compile linker init-expr)
		      (theme-target-tree-il-compile linker
						    type)))))
	    (list s-source-name t-var-name type-check))
	  (list
	   s-source-name
	   t-var-name
	   (theme-target-tree-il-compile linker init-expr))))))


(define (tc-tree-il-parse-letrec-variable linker var)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (is-letvar? var))
  (let ((variable (cadr var))
	(type (list-ref var 3))
	(init-expr (list-ref var 4)))
    (assert (hrecord-is-instance? variable <normal-variable>))
    (assert (hrecord-is-instance? init-expr <entity>))
    (let* ((address (hfield-ref variable 'address))
	   (t-var-name (get-target-var-name linker address))
	   (s-source-name (hfield-ref address 'source-name))
	   (t-init-expr0 (theme-target-tree-il-compile linker init-expr))
	   (t-init-expr (tc-tree-il-letrec-unspecified-check
			 linker t-init-expr0 s-source-name)))
      (if (hfield-ref linker 'tcomp-inside-param-proc?)
	  (let ((type-check
		 (if (entity-type-dispatched? init-expr)
		     t-init-expr
		     (tc-tree-il-generate-type-check
		      linker
		      t-init-expr
		      (theme-target-tree-il-compile linker
						    type)))))
	    (list s-source-name t-var-name type-check))
	  (list
	   s-source-name
	   t-var-name
	   t-init-expr)))))


(define (tc-tree-il-parse-let-variables linker variables rec?)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (list? variables))
  (if rec?
      (map (lambda (var) (tc-tree-il-parse-letrec-variable linker var))
	   variables)
      (map (lambda (var) (tc-tree-il-parse-let-variable linker var))
	   variables)))


(define (tc-tree-il-let linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <let-expression>))
  (if gl-test3 (raise 'test-error-3))
  (let ((recursive? (hfield-ref repr 'recursive?))
	(order? (hfield-ref repr 'order?))
	(variables (hfield-ref repr 'variables))
	(body (hfield-ref repr 'body)))
    (let* ((keyword (get-let-keyword recursive? order?))
	   (t-variables (tc-tree-il-parse-let-variables linker variables
							recursive?))
	   (t-body (theme-target-tree-il-compile linker body))
	   (t-names (map car t-variables))
	   (t-gensyms (map cadr t-variables))
	   (t-vals (map caddr t-variables)))
      (list keyword
	    t-names
	    t-gensyms
	    t-vals
	    t-body))))


(define (tc-tree-il-cast linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <cast-expression>))
  (let ((type (get-entity-type repr))
	(value-expr (hfield-ref repr 'value-expr))
	(default-expr (hfield-ref repr 'default-expr)))
    (assert (is-target-object? type))
    (assert (is-entity? value-expr))
    (assert (is-entity? default-expr))
    (let ((result
	   (cond
	    ;; The following check is an optimization.
	    ((is-t-subtype? (get-binder-for-tc linker)
			    (get-entity-type value-expr)
			    type)
	     (theme-target-tree-il-compile linker value-expr))
	    ((hrecord-is-instance? value-expr <variable-reference>)
	     (let* ((t-obj
		     (tc-tree-il-var-ref0
		      linker
		      (hfield-ref (hfield-ref value-expr 'variable)
				  'address)))
		    (t-test (make-instance-test linker t-obj type))
		    (t-default-expr
		     (if (is-empty-expr? default-expr)
			 (let ((s-call (tree-il-get-call-keyword linker)))
			   `(,s-call
			     (toplevel _i_cast-error)
			     ,(theme-target-tree-il-compile linker
							    type)
			     ,t-obj))
			 (theme-target-tree-il-compile
			  linker default-expr))))
	       `(if ,t-test ,t-obj ,t-default-expr)))
	    (else
	     (let* ((addr-obj (linker-alloc-loc linker 'obj #f))
		    (s-gensym-obj (get-target-var-name linker addr-obj))
		    (t-test
		     (make-instance-test linker `(lexical obj ,s-gensym-obj)
					 type))
		    (t-value-expr (theme-target-tree-il-compile
				   linker value-expr))
		    (t-default-expr
		     (if (is-empty-expr? default-expr)
			 (let ((s-call (tree-il-get-call-keyword linker)))
			   `(,s-call
			     (toplevel _i_cast-error)
			     ,(theme-target-tree-il-compile linker
							    type)
			     (lexical obj ,s-gensym-obj)))
			 (theme-target-tree-il-compile
			  linker default-expr))))
	       `(let (obj) (,s-gensym-obj) (,t-value-expr)
		     (if
		      ,t-test
		      (lexical obj ,s-gensym-obj)
		      ,t-default-expr)))))))
      result)))


(define (tc-tree-il-static-cast linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <static-cast-expression>))
  (theme-target-tree-il-compile linker
				(hfield-ref repr 'ent-value)))


(define (tc-tree-il-compile-class-def linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <class-definition>))
  (let* ((var (hfield-ref repr 'variable))
	 (val (hfield-ref var 'value))
	 (binder (get-binder-for-tc linker)))
    (if (and (is-target-object? val)
	     (is-t-instance? binder val tc-class))
	(let ((tt-constructor (tno-field-ref val 'type-constructor)))
	  (if (null? tt-constructor)
	      (raise 'undefined-constructor)
	      (let* ((s-call (tree-il-get-call-keyword linker))
		     (var-name (get-target-var-name
				linker
				(hfield-ref val 'address)))
		     (t-var-ref
		      (tc-tree-il-var-ref0 linker
					   (hfield-ref val 'address)))
		     (t-super
		      (tno-field-ref val 'cl-superclass))
		     (body
		      (list
		       `(const ,(tno-field-ref val 'str-name))
		       (tc-tree-il-object-fwd linker t-super '() #f)
		       (tc-tree-il-class-field-texprs
			linker
			(tno-field-ref val 'l-fields))
		       `(const ,(tno-field-ref val 'inheritable?))
		       `(const ,(tno-field-ref val 'immutable?))
		       `(const ,(tno-field-ref val 'eq-by-value?))
		       `(const ,(tno-field-ref val 's-ctr-access))))
		     (t-ctr-type
		      (theme-target-tree-il-compile linker tt-constructor))
		     (ctr
		      (tc-tree-il-get-constructor-def linker val t-ctr-type))
		     (x-def
		      (if (hfield-ref repr 'declared?)
			  `(,s-call (toplevel vector-copy-contents-rev)
				    ,t-var-ref
				    (,s-call (toplevel _i_make-class) ,@body))
			  `(define ,var-name
			     (,s-call (toplevel _i_make-class) ,@body))))
		     (x-ctr-type-set
		      `(,s-call (toplevel vector-set!)
				,t-var-ref
				(toplevel i-class-type-constructor)
				,t-ctr-type))
		     (x-ctr-set
		      `(,s-call (toplevel vector-set!)
				,t-var-ref
				(toplevel i-class-proc-constructor)
				,ctr)))
		`(_splice ,x-def ,x-ctr-type-set ,x-ctr-set))))
	(raise 'internal-error-in-class))))


(define (tc-tree-il-class-def linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <class-definition>))
  (if (var-def-is-used? linker repr)
      (tc-tree-il-compile-class-def linker repr)
      '(void)))


(define (tc-tree-il-field-ref linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <field-ref-expr>))
  (let ((r-object (hfield-ref repr 'object))
	(r-field-name (hfield-ref repr 'field-name)))
    (let ((t-object (theme-target-tree-il-compile linker r-object))
	  (to-type (get-entity-type r-object))
	  (s-call (tree-il-get-call-keyword linker)))
      (cond
       ((and (hfield-ref repr 'const-field-name?)
	     (not-null? to-type))
	(assert (symbol? r-field-name))
	;; Procedure get-field-index raises an exception if the field is
	;; undefined.
	(let ((field-index (get-field-index
			    r-field-name
			    to-type)))
	  (if (and (is-target-object? r-object)
		   (or
		    (hfield-ref r-object 'incomplete?)
		    (eq? (hfield-ref r-object 'al-field-values) #f)))
	      `(,s-call (toplevel check-field-unspecified)
			(,s-call (toplevel vector-ref) ,t-object
				 (const ,field-index))
			(const ,r-field-name))
	      `(,s-call (toplevel vector-ref) ,t-object
			(const ,field-index)))))
       ((symbol? r-field-name)
	`(,s-call (toplevel _i_field-ref)
		  ,t-object
		  (const ,r-field-name)))
       (else
	(let ((t-field-name (theme-target-tree-il-compile linker
							  r-field-name)))
	  `(,s-call (toplevel _i_field-ref)
		    ,t-object
		    ,t-field-name)))))))
       

(define (tc-tree-il-field-set linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <field-set-expr>))
  (let ((r-object (hfield-ref repr 'object))
	(r-field-name (hfield-ref repr 'field-name))
	(r-field-value (hfield-ref repr 'field-value)))
    (let* ((t-object (theme-target-tree-il-compile linker r-object))
	   (t-field-value (theme-target-tree-il-compile linker r-field-value))
	   (to-type (get-entity-type r-object))
	   (s-call (tree-il-get-call-keyword linker))
	   (result
	    (cond
	     ((and (hfield-ref repr 'const-field-name?)
		   (not-null? to-type))
	      ;; Procedures get-field and get-field-index raise an exception
	      ;; if the field is undefined.
	      (if (hfield-ref linker 'tcomp-inside-param-proc?)
		  (let* ((field-index (get-field-index
				       r-field-name
				       to-type))
			 (field (get-field r-field-name to-type))
			 (r-field-type (tno-field-ref field 'type))
			 (t-field-type
			  (theme-target-tree-il-compile linker r-field-type))
			 (t-wrapped-value
			  (if (entity-type-dispatched? r-field-value)
			      t-field-value
			      (tc-tree-il-generate-type-check
			       linker t-field-value t-field-type))))
		    `(,s-call (toplevel vector-set!)
			      ,t-object
			      (const ,field-index)
			      ,t-wrapped-value))
		  (let ((field-index (get-field-index
				      r-field-name
				      to-type)))
		    `(,s-call (toplevel vector-set!)
			      ,t-object
			      (const ,field-index)
			      ,t-field-value))))
	     ((symbol? r-field-name)
	      `(,s-call (toplevel _i_field-set!)
			,t-object
			(const ,r-field-name)
			,t-field-value))
	     (else
	      (let ((t-field-name (theme-target-tree-il-compile
				   linker r-field-name)))
	      `(,s-call (toplevel _i_field-set!)
			,t-object
			,t-field-name
			,t-field-value))))))
      result)))


(define (tc-tree-il-do-compile-param-ltype-def linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <param-logical-type-def>))
  (let* ((s-call (tree-il-get-call-keyword linker))
	 (var (hfield-ref repr 'variable))
	 (var-name (get-target-var-name
		    linker
		    (hfield-ref var 'address)))
	 (t-var-ref (tc-tree-il-var-ref0 linker (hfield-ref var 'address)))
	 (str-name (symbol->string (hfield-ref (hfield-ref var 'address)
					       'source-name)))
	 (r-val (hfield-ref repr 'value-expr))
	 (r-tvars (hfield-ref repr 'type-variables))
	 (nr-of-tvars (length r-tvars))
	 (t-val (theme-target-tree-il-compile linker r-val))
	 (l-tvar-names (get-source-names r-tvars))
	 (l-tvar-gensyms (get-gensyms linker r-tvars))
	 (first-number (alloc-tvar-number-range linker nr-of-tvars))
	 (tvar-numbers (get-integer-sequence
			first-number
			nr-of-tvars))
	 (t-tvar-objects
	  (map (lambda (tvar-number)
		 `(,s-call (toplevel make-tvar-object)
			   (const ,tvar-number)))
	       tvar-numbers))
	 (addr-tvar-list (linker-alloc-loc linker 'tvar-list #f))
	 (s-gensym-tvar-list (get-target-var-name linker addr-tvar-list))
	 (t-body
	  `(let (tvar-list) (,s-gensym-tvar-list)
		((,s-call (toplevel list) ,@t-tvar-objects))
		(,s-call (toplevel _i_make-param-ltype)
			 (const ,str-name)
			 (lexical tvar-list ,s-gensym-tvar-list)
			 (,s-call (toplevel apply)
				  (lambda ()
				    (lambda-case ((,l-tvar-names
						   #f #f () ()
						   ,l-tvar-gensyms)
						  ,t-val)))
				  (lexical tvar-list ,s-gensym-tvar-list))
			 (toplevel _b_<object>)
			 (const ,nr-of-tvars)))))
    (if (hfield-ref repr 'declared?)
	`(,s-call (toplevel vector-copy-contents-rev)
		  ,t-var-ref
		  ,t-body)
	`(define ,var-name ,t-body))))


(define (tc-tree-il-param-ltype-def linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <param-logical-type-def>))
  (if (var-def-is-used? linker repr)
      (tc-tree-il-do-compile-param-ltype-def linker repr)
      '(void)))


(define (tc-tree-il-param-class-def0 linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <param-class-definition>))
  (let* ((var (hfield-ref repr 'variable))
	 (val (hfield-ref var 'value))
	 (binder (get-binder-for-tc linker)))
    (let ((result
	   (cond
	    ((and (is-target-object? val)
		  (is-t-instance? binder val tc-class))
	     (let* ((s-call (tree-il-get-call-keyword linker))
		    (var-name (get-target-var-name-for-loc linker var))
		    (r-super
		     (tno-field-ref val 'cl-instance-superclass))
		    (t-super (theme-target-tree-il-compile linker r-super))
		    (t-fields
		     (tc-tree-il-class-field-texprs
		      linker
		      (tno-field-ref val 'l-instance-fields)))
		    (l-tvars (tno-field-ref val 'l-tvars))
		    (l-tvar-names (get-source-names l-tvars))
		    (l-tvar-gensyms (get-gensyms linker l-tvars))
		    (nr-of-tvars (length l-tvars))
		    (first-number (alloc-tvar-number-range linker
							   nr-of-tvars))
		    (tvar-numbers (get-integer-sequence
				   first-number
				   nr-of-tvars))
		    (t-tvar-objects
		     (map (lambda (tvar-number)
			    `(,s-call (toplevel make-tvar-object)
				      (const ,tvar-number)))
			  tvar-numbers)))
	       (let* ((x-super-proc
		       `(lambda ()
			  (lambda-case
			   ((,l-tvar-names #f #f () ()
					   ,l-tvar-gensyms)
			    ,t-super))))
		      (x-fields-proc
		       `(lambda ()
			  (lambda-case
			   ((,l-tvar-names #f #f () ()
					   ,l-tvar-gensyms)
			    ,t-fields))))
		      ;; Variable t-tvar-objects could be factorized
		      ;; in the following.
		      (body
		       (list
			`(const ,(tno-field-ref val 'str-name))
			`(const ,(tno-field-ref val 'i-params))
			`(,s-call (toplevel list) ,@t-tvar-objects)
			`(,s-call ,x-super-proc ,@t-tvar-objects)
			`(,s-call ,x-fields-proc ,@t-tvar-objects)
			`(const ,(tno-field-ref val 'instances-inheritable?))
			`(const ,(tno-field-ref val 'instances-immutable?))
			`(const ,(tno-field-ref val 'instances-eq-by-value?))
			`(const ,(tno-field-ref val 'instance-has-constructor?))
			`(const ,(tno-field-ref val 's-instance-ctr-access)))))
		 (if (hfield-ref repr 'declared?)
		     `(,s-call (toplevel vector-copy-contents-rev)
		       (toplevel ,var-name)
		       (,s-call (toplevel _i_make-param-class) ,@body))
		     `(define ,var-name
		       (,s-call (toplevel _i_make-param-class) ,@body))))))
	    (else (raise 'internal-error-in-class)))))
      result)))


(define (tc-tree-il-param-class-def linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <param-class-definition>))
  (if (var-def-is-used? linker repr)
      (tc-tree-il-param-class-def0 linker repr)
      '(void)))


(define (tc-tree-il-constructor linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <expr-constructor>))
  (let* ((clas (hfield-ref repr 'clas))
	 (binder (get-binder-for-tc linker))
	 (s-call (tree-il-get-call-keyword linker)))
    (cond
     ((null? clas)
      (raise 'internal-undefined-class))
     ((is-t-instance? binder clas tpc-pair)
      (let* ((r-type (get-entity-type repr))
	     (c-type (theme-target-tree-il-compile linker r-type)))	
	`(,s-call (toplevel _i_make-procedure)
		  ,c-type
		  cons)))
     (else
      (let ((t-clas (theme-target-tree-il-compile linker clas)))
	`(,s-call (toplevel get-constructor) ,t-clas))))))


(define (tc-tree-il-match-type-clause linker lst-clause t-obj-match-ref)
  (let* ((var (car lst-clause))
	 (has-var? (not-null? var))
	 (s-var-name
	  (if has-var?
	      (hfield-ref (hfield-ref var 'address) 'source-name)
	      '()))
	 (s-var-gensym
	  (if has-var?
	      (get-target-var-name linker (hfield-ref var 'address))
	      '()))
	 (expr-type (cadr lst-clause))
	 (expr-to-eval (caddr lst-clause))
	 (texpr-to-eval (theme-target-tree-il-compile linker expr-to-eval))
	 (opt? (list-ref lst-clause 3)))
    (assert (boolean? opt?))
    (cond
     ((and has-var? (not opt?))
      (list (make-instance-test linker t-obj-match-ref expr-type)
	    `(let (,s-var-name) (,s-var-gensym)
		  (,t-obj-match-ref)
		  ,texpr-to-eval)))
     ((and has-var? opt?)
      `((const #t)
	(let (,s-var-name) (,s-var-gensym)
	     (,t-obj-match-ref)
	     ,texpr-to-eval)))
     ((and (not has-var?) (not opt?))
      (list (make-instance-test linker t-obj-match-ref expr-type)
	    texpr-to-eval))
     ((and (not has-var?) opt?)
      `((const #t) ,texpr-to-eval))
     (else
      ;; We should not arrive here.
      (raise 'internal-error)))))


(define (tc-tree-il-match-type-body linker t-clauses t-else)
  (assert (list? t-clauses))
  (if (null? t-clauses)
      t-else
      (let ((t-first (car t-clauses))
	    (t-rest (cdr t-clauses)))
	(assert (and (list? t-first) (= (length t-first) 2)))
	;; The following test is an optimization.
	(if (equal? (car t-first) '(const #t))
	    (cadr t-first)
	    `(if ,(car t-first)
		 ,(cadr t-first)
		 ,(tc-tree-il-match-type-body linker t-rest t-else))))))


(define (tc-tree-il-match-type linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <match-type-expression>))
  (let* ((expr-to-match (hfield-ref repr 'expr-to-match))
	 (lst-repr-clauses (hfield-ref repr 'lst-proper-clauses))
	 (expr-else (hfield-ref repr 'expr-else))
	 (texpr-to-match (theme-target-tree-il-compile linker expr-to-match))
	 (addr-obj-match (linker-alloc-loc linker 'obj-match #f))
	 (s-gensym-obj-match (get-target-var-name linker addr-obj-match))
	 (t-obj-match-ref (list 'lexical 'obj-match s-gensym-obj-match))
	 (texpr-clauses
	  (map (lambda (lst-repr-clause)
		 (tc-tree-il-match-type-clause linker lst-repr-clause
					       t-obj-match-ref))
	       lst-repr-clauses))
	 (texpr-else
	  (if (and (hfield-ref repr 'strong?)
		   (is-empty-expr? expr-else))
	      (let ((s-call (tree-il-get-call-keyword linker)))
		`(,s-call (toplevel _i_match-type-strong-no-match)))
	      (theme-target-tree-il-compile linker expr-else)))
	 (t-body (tc-tree-il-match-type-body linker texpr-clauses texpr-else)))
    `(let (obj-match) (,s-gensym-obj-match) (,texpr-to-match)
	  ,t-body)))


(define (tc-tree-il-param-proc-instance linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <expr-param-proc-instance>))
  (if (is-known-object? (hfield-ref repr 'param-proc))
      (tc-tree-il-param-proc-instance1 linker repr)
      (tc-tree-il-param-proc-instance-expr linker repr)))


(define (tc-tree-il-param-proc-dispatch linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <expr-param-proc-dispatch>))
  ;; Procedure tc-tree-il-param-proc-instance1 works also for
  ;; <expr-param-proc-dispatch>.
  (tc-tree-il-param-proc-instance1 linker repr))


(define (tc-tree-il-gen-proc-def linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <generic-procedure-definition>))
  (if (var-def-is-used? linker repr)
      (let ((gen-proc-name (symbol->string
			    (hfield-ref
			     (hfield-ref
			      (hfield-ref repr 'variable)
			      'address)
			     'source-name)))
	    (var-name (get-target-var-name
		       linker
		       (hfield-ref (hfield-ref repr 'variable) 'address)))
	    (s-call (tree-il-get-call-keyword linker)))
	(begin
	  `(define ,var-name (,s-call (toplevel make-empty-gen-proc)
				      (const ,gen-proc-name)))))
      '(void)))


(define (tc-tree-il-method-def linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <method-definition>))
  (let* ((gen-proc (hfield-ref repr 'gen-proc))
	 (procexpr (hfield-ref repr 'procexpr))
	 (addr-gen-proc (hfield-ref gen-proc 'address))
	 (ht-used (hfield-ref linker 'ht-used))
	 (ht-rebound (hfield-ref linker 'ht-rebound)))
    (if (or (not (hfield-ref linker 'strip?))
	    (hfield-ref repr 'include?)
	    (hashq-ref ht-rebound repr)
	    (address-hash-ref ht-used addr-gen-proc))
	(begin
	  (let ((t-gen-proc (theme-target-tree-il-compile linker gen-proc)))
	    (let ((t-procexpr (theme-target-tree-il-compile linker procexpr))
		  (s-call (tree-il-get-call-keyword linker))
		  (static? (hfield-ref repr 'static-method?)))
	      (assert (boolean? static?))
	      (if (hfield-ref repr 'declared?)
		  (let ((old-address (hfield-ref repr 'old-address)))
		    (assert (not-null? old-address))
		    (let ((old-var-name
			   (get-target-var-name linker old-address)))
		      `(_splice (set! (toplevel ,old-var-name) ,t-procexpr)
				(,s-call (toplevel _i_add-method!)
					 ,t-gen-proc ,t-procexpr
					 (const ,static?)))))
		  `(,s-call (toplevel _i_add-method!)
			    ,t-gen-proc ,t-procexpr (const ,static?))))))
	'(void))))


(define (tc-tree-il-method-decl linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <method-declaration>))
  (let* ((method (hfield-ref repr 'method))
	 (address (hfield-ref method 'address))
	 (gen-proc (hfield-ref repr 'gen-proc))
	 (addr-gen-proc (hfield-ref gen-proc 'address))
	 (ht-used (hfield-ref linker 'ht-used))
	 (ht-used-decls (hfield-ref linker 'ht-used-decls))
	 (ht-rebound (hfield-ref linker 'ht-rebound)))
    (if (or (not (hfield-ref linker 'strip?))
	    (hfield-ref repr 'include?)
	    (hashq-ref ht-rebound repr)
	    (address-hash-ref ht-used addr-gen-proc)
	    (address-hash-ref ht-used-decls address))
	(let ((var-name (get-target-var-name linker address)))
	  `(define ,var-name (const ())))
	'(void))))


(define (tc-tree-il-generic-proc-dispatch linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <generic-proc-dispatch>))
  (let* ((proc-type (get-entity-type repr))
	 (result-type (tno-field-ref proc-type 'type-result))
	 (appl-pure? (hfield-ref repr 'appl-pure?))
	 (appl-always-returns? (hfield-ref repr 'appl-always-returns?))
	 (appl-never-returns? (hfield-ref repr 'appl-never-returns?))
	 (t-gen-proc (theme-target-tree-il-compile
		      linker (hfield-ref repr 'generic-proc)))
	 (l-arg-types
	  (map* (lambda (repr-arg)
		  (theme-target-tree-il-compile linker repr-arg))
		(hfield-ref repr 'arg-types)))
	 (t-result-type
	  (if (and (entity-type-dispatched? repr)
		   (not (linker-entity-is-none? linker result-type)))
	      (theme-target-tree-il-compile linker result-type)
	      '(const ())))
	 (s-call (tree-il-get-call-keyword linker)))
    `(,s-call (toplevel _i_dispatch-generic-proc)
	      ,t-gen-proc
	      ,t-result-type
	      (,s-call (toplevel list)
		       ,@l-arg-types)
	      (const ,appl-pure?)
	      (const ,appl-always-returns?)
	      (const ,appl-never-returns?))))
    

(define (tc-tree-il-prim-class-def0 linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <prim-class-def>))
  (let* ((name (hfield-ref repr 'name))
	 (target-name (hfield-ref repr 'target-name))
	 (goops? (hfield-ref repr 'goops?))
	 (r-superclass (hfield-ref repr 'superclass))
	 (t-superclass (tc-tree-il-object-fwd linker
					      r-superclass '() #f))
	 (inh? (hfield-ref repr 'inh?))
	 (imm? (hfield-ref repr 'imm?))
	 (ebv? (hfield-ref repr 'ebv?))
	 (t-member-var-name (hfield-ref repr 'member-target-name))
	 (t-equal-var-name (hfield-ref repr 'equal-target-name))
	 (t-equal-objects-var-name (hfield-ref repr 'equal-objects-target-name))
	 (t-equal-contents-var-name
	  (hfield-ref repr 'equal-contents-target-name))
	 (t-var-name
	  (get-target-var-name
	   linker (hfield-ref (hfield-ref repr 'variable) 'address)))
	 (s-call (tree-il-get-call-keyword linker))
	 ;; Custom primitive classes are always declared forward.
	 (t-creation
	  (if goops?
	      `(,s-call (toplevel vector-copy-contents-rev)
			(toplevel ,t-var-name)
			(,s-call (toplevel create-goops-class)
				 (const ,name)
				 ,t-superclass
				 (const ,inh?)
				 (const ,imm?)
				 (const ,ebv?)))
	      `(,s-call (toplevel vector-copy-contents-rev)
			(toplevel ,t-var-name)
			(,s-call (toplevel make-custom-prim-class)
				 (const ,name)
				 (const ,imm?)
				 (const ,ebv?)))))
	 (t-notify
	  (if goops?
	      `(,s-call (toplevel notify-goops-class)
			(toplevel ,t-var-name)
			(toplevel ,target-name)
			(toplevel ,t-equal-var-name)
			(toplevel ,t-equal-contents-var-name))
	      `(,s-call (toplevel notify-custom-prim-class)
			(toplevel ,t-var-name)
			(toplevel ,t-member-var-name)
			(toplevel ,t-equal-var-name)
			(toplevel ,t-equal-objects-var-name)
			(toplevel ,t-equal-contents-var-name))))
	 (t-final
	  (list '_splice
		t-creation 
		t-notify)))
    t-final))


(define (tc-tree-il-prim-class-def linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <prim-class-def>))
  (if (var-def-is-used? linker repr)
      (tc-tree-il-prim-class-def0 linker repr)
      '(void)))


(define (tc-tree-il-zero linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <zero-expr>))
  (let* ((r-clas (hfield-ref repr 'clas))
	 (t-clas (theme-target-tree-il-compile linker r-clas))
	 (s-call (tree-il-get-call-keyword linker)))
    (if (is-t-instance? (get-binder-for-tc linker)
			r-clas
			tc-class)
	(let ((zero-value (tno-field-ref r-clas 'x-zero-value)))
	  (cond
	   ((tno-field-ref r-clas 'zero-prim?)
	    (assert (is-primitive-value? zero-value))
	    `(const ,zero-value))
	   ((is-address? zero-value)
	    (tc-tree-il-var-ref0 linker zero-value))
	   (else
	    `(,s-call (toplevel vector-ref)
		      ,t-clas
		      (toplevel i-class-zero-value)))))
	`(,s-call (toplevel get-zero) ,t-clas))))


(define (tc-tree-il-zero-setting linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <zero-setting-expr>))
  (let* ((var-cl (hfield-ref repr 'var-class))
	 (r-zero-proc (hfield-ref repr 'zero-proc))
	 (param? (hfield-ref repr 'param?))
	 (t-cl (tc-tree-il-var-ref0 linker (hfield-ref var-cl 'address)))
	 (t-zero-proc (theme-target-tree-il-compile linker r-zero-proc))
	 (s-call (tree-il-get-call-keyword linker)))
    (if param?
	`(_splice
	  (,s-call (toplevel vector-set!)
		   ,t-cl
		   (toplevel i-param-class-instance-has-zero)
		   (const #t))
	  (,s-call (toplevel vector-set!)
		   ,t-cl
		   (toplevel i-param-class-instance-zero-proc)
		   ,t-zero-proc))
	`(_splice
	  (,s-call (toplevel vector-set!)
		   ,t-cl
		   (toplevel i-class-has-zero)
		   (const #t))
	  (,s-call (toplevel vector-set!)
		   ,t-cl
		   (toplevel i-class-zero-value)
		   (,s-call (toplevel _i_call-proc)
			    ,t-zero-proc
			    (const ())
			    (const ())))))))


(define (tc-tree-il-signature-member linker r-member)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (is-signature-member? (get-binder-for-tc linker) r-member))
  (let ((obj-target (car r-member))
	(r-type (cdr r-member)))
    (let ((p-target (theme-target-tree-il-compile linker obj-target))
	  (p-type (theme-target-tree-il-compile linker r-type))
	  (s-call (tree-il-get-call-keyword linker)))
      `(,s-call (toplevel cons) ,p-target ,p-type))))


(define (tc-tree-il-signature-def linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <signature-definition>))
  (let* ((var (hfield-ref repr 'variable))
	 (to (hfield-ref var 'value)))
    (assert (and (not-null? to)
		 (target-type=? (get-entity-type to)
				tc-signature)))
    (let* ((r-members (tno-field-ref to 'l-members))
	   (p-members (map* (lambda (r-member)
			      (tc-tree-il-signature-member linker r-member))
			    r-members))
	   (s-gensym-var
	    (get-target-var-name linker (hfield-ref var 'address)))
	   (declared? (hfield-ref repr 'declared?))
	   (def-kw (if declared? 'set! 'define))
	   (s-call (tree-il-get-call-keyword linker)))
      (if (hfield-ref repr 'declared?)
	  `(set! (toplevel ,s-gensym-var)
		 (,s-call (toplevel make-signature)
			  (,s-call (toplevel list)
				   ,@p-members)))
	  `(define ,s-gensym-var
	     (,s-call (toplevel make-signature)
		      (,s-call (toplevel list)
			       ,@p-members)))))))


(define (tc-tree-il-param-signature0 linker to)
  (let* ((l-tvars (tno-field-ref to 'l-tvars))
	 (nr-of-tvars (length l-tvars))
	 (l-tvar-names (get-source-names l-tvars))
	 (l-tvar-gensyms (get-gensyms linker l-tvars))
	 (first-number (alloc-tvar-number-range linker
						nr-of-tvars))
	 (tvar-numbers (get-integer-sequence
			first-number
			nr-of-tvars))
	 (s-call (tree-il-get-call-keyword linker))
	 (t-tvar-objects
	  (map (lambda (tvar-number)
		 `(,s-call (toplevel make-tvar-object)
			   (const ,tvar-number)))
	       tvar-numbers))
	 (r-members (tno-field-ref to 'l-members))
	 (t-members (map* (lambda (r-member)
			    (tc-tree-il-signature-member linker r-member))
			  r-members))
	 (x-sig-lambda
	  `(lambda ()
	     (lambda-case ((,l-tvar-names #f #f () () ,l-tvar-gensyms)
			   (,s-call (toplevel list)
				    ,@t-members))))))
    `(,s-call (toplevel make-param-signature)
	      (,s-call (toplevel list) ,@t-tvar-objects)
	      (,s-call ,x-sig-lambda
		       ,@t-tvar-objects))))


(define (tc-tree-il-param-signature-def linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <param-signature-definition>))
  (let* ((var (hfield-ref repr 'variable))
	 (var-name (get-target-var-name linker (hfield-ref var 'address)))
	 (to (hfield-ref var 'value))
	 (t-expr (tc-tree-il-param-signature0 linker to)))
    (if (hfield-ref repr 'declared?)
	`(set! (toplevel ,var-name) ,t-expr)
	`(define ,var-name ,t-expr))))


(define (tc-tree-il-force-pure-expr linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <force-pure-expr>))
  (theme-target-tree-il-compile-fwd linker
				    (hfield-ref repr 'repr-component)))


(define (tc-tree-il-assertion linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <assertion-expr>))
  (if (or (hfield-ref linker 'all-assertions?)
	  (hfield-ref repr 'strong?))
      (let ((t-condition
	     (theme-target-tree-il-compile linker
					   (hfield-ref repr 'condition)))
	    (condition-source-expr (hfield-ref repr 'condition-source-expr))
	    (s-call (tree-il-get-call-keyword linker)))
	`(if (,s-call (toplevel not) ,t-condition)
	     (,s-call (toplevel _i_raise-assertion-failed)
		      (const ,condition-source-expr))
	     (void)))
      '(void)))


(define (tc-tree-il-debug-output linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <debug-output-expr>))
  (let ((x-message (hfield-ref repr 'x-message))
	(s-call (tree-il-get-call-keyword linker)))
    `(,s-call (toplevel theme-debug-print) (const ,x-message))))


(define (tc-tree-il-empty linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? repr <empty-expression>))
  '(void))


(define (tc-tree-il-do-nothing linker repr) #f)


(define (tc-tree-il-error linker repr)
  ;; TBD: Move the error output to theme-d-linker-errors
  (write-error-info "Unknown expression type")
  (write-error-info (hrecord-type-name-of repr))
  (raise 'unknown-expr-type))


;; Note: Type loops are not handled here.
(define tc-tree-il-proc-table
  (list
   (cons <generic-procedure-definition> tc-tree-il-gen-proc-def)
   (cons <variable-definition> tc-tree-il-var-def)
   (cons <set-expression> tc-tree-il-set-expr)
   (cons <variable-reference> tc-tree-il-var-ref)
   (cons <prim-proc-ref> tc-tree-il-prim-proc-ref)
   (cons <checked-prim-proc> tc-tree-il-checked-prim-proc)
   (cons <prim-class-def> tc-tree-il-prim-class-def)
   (cons <class-definition> tc-tree-il-class-def)
   (cons <param-class-definition> tc-tree-il-param-class-def)
   (cons <expr-constructor> tc-tree-il-constructor)
   (cons <zero-expr> tc-tree-il-zero)
   (cons <field-ref-expr> tc-tree-il-field-ref)
   (cons <field-set-expr> tc-tree-il-field-set)
   (cons <proc-appl> tc-tree-il-proc-appl)
   (cons <procedure-expression> tc-tree-il-proc-expr)
   (cons <method-definition> tc-tree-il-method-def)
   (cons <method-declaration> tc-tree-il-method-decl)
   (cons <let-expression> tc-tree-il-let)
   (cons <cast-expression> tc-tree-il-cast)
   (cons <static-cast-expression> tc-tree-il-static-cast)
   (cons <match-type-expression> tc-tree-il-match-type)
   (cons <if-form> tc-tree-il-if)
   (cons <compound-expression> tc-tree-il-compound)
   (cons <until-form> tc-tree-il-until)
   (cons <forward-declaration> tc-tree-il-fw-decl)
   (cons <param-logical-type-def> tc-tree-il-param-ltype-def)
   (cons <param-proc-expr> tc-tree-il-param-proc-expr)
   (cons <expr-param-proc-instance> tc-tree-il-param-proc-instance)
   (cons <expr-param-proc-dispatch> tc-tree-il-param-proc-dispatch)
   (cons <generic-proc-dispatch> tc-tree-il-generic-proc-dispatch)
   (cons <signature-definition> tc-tree-il-signature-def)
   (cons <param-signature-definition> tc-tree-il-param-signature-def)
   (cons <zero-setting-expr> tc-tree-il-zero-setting)
   (cons <force-pure-expr> tc-tree-il-force-pure-expr)
   (cons <assertion-expr> tc-tree-il-assertion)
   (cons <debug-output-expr> tc-tree-il-debug-output)
   (cons <empty-expression> tc-tree-il-empty)
   (cons <normal-variable> tc-tree-il-error)
   (cons <target-object> tc-tree-il-error)))


(define (tc-tree-il-object-with-address linker to)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (is-target-object? to))
  (let ((address (hfield-ref to 'address)))
    (assert (not-null? address))
    (tc-tree-il-var-ref0 linker address)))


(define (tc-tree-il-pair-class linker to lst-visited)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (and (is-target-object? to) (is-tc-pair? to)))
  (let* ((tvv (tno-field-ref to 'l-tvar-values))
	 (tt-first (tc-tree-il-object-fwd linker (car tvv)
					  lst-visited #f))
	 (tt-second (tc-tree-il-object-fwd linker (cadr tvv)
					   lst-visited #f))
	 (s-call (tree-il-get-call-keyword linker)))
    (if (contains-type-modifiers? to)
	`(,s-call (toplevel _i_get-pair-class-general1)
		  (,s-call (toplevel list) ,tt-first ,tt-second))
	`(,s-call (toplevel _i_get-pair-class-general)
		  (,s-call (toplevel list) ,tt-first ,tt-second)))))


(define (tc-tree-il-pair linker to lst-visited)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (and (is-target-object? to) (is-tc-pair? (get-entity-type to))))
  (let* ((first (tc-tree-il-object-fwd linker (tno-field-ref to 'first)
				       lst-visited #f))
	 (second (tc-tree-il-object-fwd linker (tno-field-ref to 'second)
					lst-visited #f))
	 (s-call (tree-il-get-call-keyword linker)))
    `(,s-call (toplevel cons) ,first ,second)))


(define (tc-tree-il-union-type linker to lst-visited)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (and (is-target-object? to) (is-tt-union? to)))
  (let* ((r-member-types (tno-field-ref to 'l-member-types))
	 (p-member-types
	  (map* (lambda (to-member)
		  (tc-tree-il-object-fwd linker to-member lst-visited #f))
		r-member-types))
	 (s-call (tree-il-get-call-keyword linker)))
    `(,s-call (toplevel _i_make-concrete-union)
	      (,s-call (toplevel list) ,@p-member-types))))


(define (tc-tree-il-rest linker repr lst-visited)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (is-t-rest? repr))
  (let* ((component (tno-field-ref repr 'type-component))
	 (compiled-component (tc-tree-il-object-fwd linker component
						    lst-visited #f))
	 (s-call (tree-il-get-call-keyword linker)))
    `(,s-call (toplevel _i_make-rest-expression) ,compiled-component)))


(define (tc-tree-il-type-list linker repr lst-visited)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (is-t-type-list? repr))
  (let* ((subtypes (tno-field-ref repr 'l-subtypes))
	 (compiled-subexprs
	  (map
	   (lambda (subrepr)
	     (tc-tree-il-object-fwd linker subrepr lst-visited #f))
	   subtypes))
	 (s-call (tree-il-get-call-keyword linker)))
    `(,s-call (toplevel create-type-list)
	      (,s-call (toplevel list) ,@compiled-subexprs))))


(define (tc-tree-il-splice linker repr lst-visited)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (is-t-splice? repr))
  (let* ((component (tno-field-ref repr 'type-component))
	 (compiled-component (tc-tree-il-object-fwd
			      linker component lst-visited #f))
	 (s-call (tree-il-get-call-keyword linker)))
    `(,s-call (toplevel _i_make-splice-expression) ,compiled-component)))


(define (tc-tree-il-type-loop linker repr lst-visited)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (is-t-type-loop? repr))
  (let ((s-iter-var-name (hfield-ref (hfield-ref (tno-field-ref repr 'tvar)
						 'address)
				     'source-name))
	(s-gensym-iter-var (get-target-var-name
			    linker
			    (hfield-ref (tno-field-ref repr 'tvar)
					'address)))
	(t-subtypes (tc-tree-il-object-fwd
		     linker
		     (tno-field-ref repr 'x-subtypes)
		     lst-visited
		     #f))
	(t-iter-expr (tc-tree-il-object-fwd
		      linker
		      (tno-field-ref repr 'x-iter-expr)
		      lst-visited
		      #f))
	(s-call (tree-il-get-call-keyword linker)))
    (let* ((tvar-number (alloc-tvar-number-range linker 1))
	   (t-iter-var-object `(,s-call (toplevel make-tvar-object)
					(const ,tvar-number))))
      ;; We replace the iteration type variable with corresponding
      ;; type variable object.
      `(,s-call (toplevel construct-type-loop-repr1)
		(,s-call (toplevel vector-ref)
			 (toplevel gl-rte)
			 (toplevel i-rte-arg-xlat))
		,t-iter-var-object
		,t-subtypes
		(,s-call
		 (lambda () (lambda-case (((,s-iter-var-name) #f #f () ()
					   (,s-gensym-iter-var))
					  ,t-iter-expr)))
		 ,t-iter-var-object)))))


(define (tc-tree-il-type-join linker repr lst-visited)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (is-t-type-join? repr))
  (let* ((subtypes (tno-field-ref repr 'l-subtypes))
	 (compiled-subtypes
	  (map
	   (lambda (subrepr)
	     (tc-tree-il-object-fwd linker subrepr lst-visited #f))
	   subtypes))
	 (s-call (tree-il-get-call-keyword linker)))
    `(,s-call (toplevel _i_make-type-join) ,@compiled-subtypes)))


(define (tc-tree-il-general-proc-type linker repr lst-visited simple?)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (or (is-tt-procedure? repr)
	      (is-tc-simple-proc? repr)))
  (assert (boolean? simple?))
  (let* ((arg-list-type (tno-field-ref repr 'type-arglist))
	 (result-type (tno-field-ref repr 'type-result))
	 (pure-proc? (tno-field-ref repr 'pure-proc?))
	 (appl-always-returns? (tno-field-ref repr 'appl-always-returns?))
	 (appl-never-returns? (tno-field-ref repr 'appl-never-returns?))
	 (static-method? (tno-field-ref repr 'static-method?))
	 (comp (lambda (repr1)
		 (tc-tree-il-object-fwd
		  linker
		  repr1
		  lst-visited
		  #f)))
	 (internal-proc-name '_i_make-procedure-type)
	 (s-call (tree-il-get-call-keyword linker)))
    ;; We use <none> for a missing value.
    (assert (not-null? result-type))
    (list s-call `(toplevel ,internal-proc-name)
	  (comp arg-list-type)
	  (comp result-type)
	  `(const ,pure-proc?)
	  `(const ,appl-always-returns?)
	  `(const ,appl-never-returns?)
	  `(const ,static-method?)
	  `(const ,simple?))))


(define (tc-tree-il-cycle linker to lst-visited)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (is-target-object? to))
  ;; Cycles use lexical variables.
  (let* ((address (linker-alloc-loc linker 'cycle-3 #f))
	 (t-var-name (get-target-var-name linker address))
	 (t-var-ref (list 'lexical 'cycle-3 t-var-name))
	 (r-type (get-entity-type to))
	 (t-type (tc-tree-il-object-fwd linker (get-entity-type to)
					lst-visited #f))
	 (lst-old-encl (hfield-ref linker 'lst-enclosing-cycles)))
    (hfield-set! linker 'lst-enclosing-cycles
		 (cons (cons to address)
		       (hfield-ref linker 'lst-enclosing-cycles)))
    (let ((p-contents (tc-tree-il-object-fwd linker to lst-visited
					     #t))
	  (binder (get-binder-for-tc linker))
	  (s-call (tree-il-get-call-keyword linker)))
      (hfield-set! linker 'lst-enclosing-cycles lst-old-encl)
      ;; We should also check that r-type is the exact class of the cycle
      ;; contents.
      (if (is-t-instance? binder r-type tc-class)
	  (let ((i-elements (+ (length (tno-field-ref r-type 'l-all-fields))
			       1))
		(texpr-body
		 (tc-tree-il-compile-sequence
		  linker
		  `((,s-call (toplevel vector-set!)
			     ,t-var-ref
			     (toplevel i-object-class)
			     ,t-type)
		    (,s-call (toplevel vector-copy-contents-rev)
			     ,t-var-ref
			     ,p-contents)
		    ,t-var-ref))))
	    `(let (cycle-3) (,t-var-name)
		  ((,s-call (toplevel make-vector)
			    (const ,i-elements)
			    (const ())))
		  ,texpr-body))
	  `(let (cycle-3) (,t-var-name)
		((,s-call (toplevel make-singleton) (const ())))
		(begin
		  (,s-call (toplevel vector-set!)
			   ,t-var-ref
			   (toplevel i-singleton-element)
			   ,p-contents)
		  ,t-var-ref))))))


(define (tc-tree-il-param-proc-class linker repr lst-visited)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (is-tc-param-proc? repr))
  (let* ((type-vars (tno-field-ref repr 'l-tvars))
	 (inst-type (tno-field-ref repr 'type-contents))
	 (l-source-names (get-source-names type-vars))
	 (l-gensyms (get-gensyms linker type-vars))
	 (nr-of-tvars (length type-vars))
	 (first-number (alloc-tvar-number-range linker nr-of-tvars))
	 (tvar-numbers (get-integer-sequence
			first-number
			nr-of-tvars))
	 (s-call (tree-il-get-call-keyword linker))
	 (t-tvar-objects
	  (map (lambda (tvar-number)
		 `(,s-call (toplevel make-tvar-object)
			   (const ,tvar-number)))
	       tvar-numbers))
	 (t-inst-type (tc-tree-il-object-fwd linker inst-type lst-visited #f)))
    ;; Variable t-tvar-objects could be probably optimized so that it is
    ;; computed only once in the following.
    `(,s-call (toplevel _i_make-param-proc-class)
	      (const ,first-number)
	      (const ,nr-of-tvars)
	      (,s-call (toplevel list) ,@t-tvar-objects)
	      (,s-call
	       (lambda () (lambda-case ((,l-source-names #f #f () ()
							 ,l-gensyms)
					,t-inst-type)))
	       ,@t-tvar-objects))))


(define (tc-tree-il-abstract-param-type-inst linker to lst-visited)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (is-target-object? to))
  (let* ((r-param-type (tno-field-ref to 'type-meta))
	 (p-param-type (tc-tree-il-object-fwd linker r-param-type
					      lst-visited #f))
	 (r-args (tno-field-ref to 'l-type-args))
	 (p-args (map (lambda (repr) (tc-tree-il-object-fwd
				      linker repr lst-visited #f))
		      r-args))
	 (decl-proc
	  (cond
	   ((is-t-param-class? r-param-type)
	    '_i_get-param-class-inst1)
	   ((is-t-param-logical-type? r-param-type)
	    '_i_get-param-ltype-inst1)
	   (else (raise 'internal-error-3))))
	 (s-call (tree-il-get-call-keyword linker)))
    `(,s-call (toplevel ,decl-proc)
	      ,p-param-type
	      (,s-call (toplevel list) ,@p-args))))


(define (tc-tree-il-vector-class linker repr lst-visited)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (is-tc-vector? repr))
  (let* ((member-type-repr (car (tno-field-ref repr 'l-tvar-values)))
	 (member-type-texpr (tc-tree-il-object-fwd linker member-type-repr
						   lst-visited #f))
	 (s-call (tree-il-get-call-keyword linker)))
    `(,s-call (toplevel _i_construct-vector) ,member-type-texpr)))


(define (tc-tree-il-mutable-vector-class linker repr lst-visited)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (is-tc-mutable-vector? repr))
  (let* ((member-type-repr (car (tno-field-ref repr 'l-tvar-values)))
	 (member-type-texpr (tc-tree-il-object-fwd linker member-type-repr
						   lst-visited #f))
	 (s-call (tree-il-get-call-keyword linker)))
    `(,s-call (toplevel _i_construct-mutable-vector) ,member-type-texpr)))


(define (tc-tree-il-value-vector-class linker repr lst-visited)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (is-tc-value-vector? repr))
  (let* ((member-type-repr (car (tno-field-ref repr 'l-tvar-values)))
	 (member-type-texpr (tc-tree-il-object-fwd linker member-type-repr
						   lst-visited #f))
	 (s-call (tree-il-get-call-keyword linker)))
    `(,s-call (toplevel _i_construct-value-vector) ,member-type-texpr)))


(define (tc-tree-il-mutable-value-vector-class linker repr lst-visited)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (is-tc-mutable-value-vector? repr))
  (let* ((member-type-repr (car (tno-field-ref repr 'l-tvar-values)))
	 (member-type-texpr (tc-tree-il-object-fwd linker member-type-repr
						   lst-visited #f))
	 (s-call (tree-il-get-call-keyword linker)))
    `(,s-call (toplevel _i_construct-mutable-value-vector)
	      ,member-type-texpr)))


(define (tc-tree-il-param-class-instance linker repr lst-visited)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (is-t-param-class-instance? repr))
  (let ((result
	 (let* ((r-param-class (get-entity-type repr))
		(t-param-class (tc-tree-il-object-fwd linker r-param-class
						      lst-visited #f))
		(r-params (tno-field-ref repr 'l-tvar-values))
		(t-params
		 (map* (lambda (param)
			 (tc-tree-il-object-fwd linker param lst-visited #f))
		       r-params))
		(s-call (tree-il-get-call-keyword linker)))
	   ;; Formerly we compiled the value expression of the
	   ;; parametrized logical type if it was available.
	   `(,s-call (toplevel _i_get-concrete-param-class-inst)
		     ,t-param-class
		     (,s-call (toplevel list) ,@t-params)))))
    result))


(define (tc-tree-il-signature linker to l-visited)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (is-t-signature? to))
  (let* ((r-members (tno-field-ref to 'l-members))
	 (p-members (map* (lambda (r-member)
			    (tc-tree-il-signature-member linker r-member))
			  r-members))
	 (s-call (tree-il-get-call-keyword linker)))
    `(,s-call (toplevel make-signature)
	      (,s-call (toplevel list) ,@p-members))))


(define (tc-tree-il-param-signature linker to l-visited)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (is-t-param-signature? to))
  (tc-tree-il-param-signature0 linker to))


(define (tc-tree-il-gen-proc-class linker repr lst-visited)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (is-tc-gen-proc? repr))
  (let* ((comp (lambda (rexpr)
		 (tc-tree-il-object-fwd linker rexpr lst-visited #f)))
	 (r-method-classes (tno-field-ref repr 'l-method-classes))
	 (t-method-classes (map* comp r-method-classes))
	 (s-call (tree-il-get-call-keyword linker)))
    `(,s-call (toplevel make-gen-proc-class)
	      (,s-call (toplevel list) ,@t-method-classes))))


(define (tc-tree-il-incomplete-object linker to lst-visited)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (is-target-object? to))
  (raise 'trying-to-compile-incomplete-object))


(define (tc-tree-il-do-comp-object linker to lst-visited compile-always?)
  (let* ((binder (get-binder-for-tc linker))
	 (result
	  (cond
	   ((null? to) '())
	   ((is-t-type-variable? to)
	    (tc-tree-il-compile-type-var linker to))
	   ((and (not compile-always?)
		 (not-null? (hfield-ref to 'address)))
	    (tc-tree-il-object-with-address linker to))
	   ((hfield-ref to 'primitive?)
	    (tc-tree-il-primitive-object linker to))
	   ;; The following may be an error situation.
	   ((hfield-ref to 'incomplete?)
	    (tc-tree-il-incomplete-object linker to lst-visited))
	   ((is-tc-pair? to)
	    (tc-tree-il-pair-class linker to lst-visited)) 
	   ((is-tt-union? to)
	    (tc-tree-il-union-type linker to lst-visited))
	   ((is-tc-param-proc? to)
	    (tc-tree-il-param-proc-class linker to lst-visited))
	   ((is-t-param-class-instance? to)
	    (tc-tree-il-param-class-instance linker to lst-visited))
	   ((is-tc-vector? to)
	    (tc-tree-il-vector-class linker to lst-visited))
	   ((is-tc-mutable-vector? to)
	    (tc-tree-il-mutable-vector-class linker to lst-visited))
	   ((is-tc-value-vector? to)
	    (tc-tree-il-value-vector-class linker to lst-visited))
	   ((is-tc-mutable-value-vector? to)
	    (tc-tree-il-mutable-value-vector-class linker to lst-visited))
	   ((is-tt-procedure? to)
	    (tc-tree-il-general-proc-type linker to lst-visited #f))
	   ((is-tc-simple-proc? to)
	    (tc-tree-il-general-proc-type linker to lst-visited #t))
	   ((is-tc-gen-proc? to)
	    (tc-tree-il-gen-proc-class linker to lst-visited))
	   ((is-t-param-signature? to)
	    (tc-tree-il-param-signature linker to lst-visited))
	   ((is-t-signature? to)
	    (tc-tree-il-signature linker to lst-visited))
	   ((is-t-rest? to)
	    (tc-tree-il-rest linker to lst-visited))
	   ((is-t-splice? to)
	    (tc-tree-il-splice linker to lst-visited))
	   ((is-t-type-list? to)
	    (tc-tree-il-type-list linker to lst-visited))
	   ((is-t-type-loop? to)
	    (tc-tree-il-type-loop linker to lst-visited))
	   ((is-t-type-join? to)
	    (tc-tree-il-type-join linker to lst-visited))
	   ((is-tc-pair? (get-entity-type to))
	    (tc-tree-il-pair linker to lst-visited))
	   ((is-t-apti? to)
	    (tc-tree-il-abstract-param-type-inst linker to lst-visited))
	   ((is-t-cycle? to)
	    (raise 'internal-error-with-cycles))
	   (else
	    (dvar1-set! to)
	    (raise 'unknown-object-type)))))
    result))


(define (tc-tree-il-object linker to lst-visited compile-always?)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (is-target-object? to))
  (assert (list? lst-visited))
  (assert (boolean? compile-always?))
  (let ((old-indent gl-indent))
    (set! gl-indent (+ gl-indent 1))
    (let ((lst-new-visited (cons to lst-visited))
	  (binder (get-binder-for-tc linker)))
      (let ((result
	     (if compile-always?
		 (tc-tree-il-do-comp-object linker to lst-new-visited #t)
		 (let* ((a (assv to
				 (hfield-ref linker 'lst-enclosing-cycles))))
		   (if a
		       (tc-tree-il-var-ref0 linker (cdr a))
		       (if (hashq-ref (hfield-ref linker 'ht-cycles) to)
			   (tc-tree-il-cycle linker to lst-new-visited)
			   (tc-tree-il-do-comp-object linker to
						      lst-visited #f)))))))
	(set! gl-indent old-indent)
	result))))


(set! tc-tree-il-object-fwd tc-tree-il-object)


(define (theme-target-tree-il-compile linker repr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (is-entity? repr))
  (hfield-set! linker 'state 'target-compilation)
  (let ((prev-repr (hfield-ref linker 'current-repr))
	(old-indent gl-indent))
    (set! gl-indent (+ gl-indent 1))
    (hfield-set! linker 'current-repr repr)
    (let ((result
	   (cond
	    ((is-t-primitive-object? repr)
	     (tc-tree-il-primitive-object linker repr))
	    ((is-expression? repr)
	     (let ((proc (hrecord-type-inquire tc-tree-il-proc-table 
					       (hrecord-type-of repr))))
	       (if proc
		   (proc linker repr)
		   (begin
		     ;; TBD: Move the error output to theme-d-linker-errors.
		     (write-line (hrecord-type-get-name (hrecord-type-of repr)))
		     (raise 'compilation-not-implemented)))))
	    ((is-target-object? repr)
	     (tc-tree-il-object linker repr '() #f))
	    (else
	     (raise 'invalid-entity)))))
      (hfield-set! linker 'current-repr prev-repr)
      (set! gl-indent old-indent)
      result)))


(set! theme-target-tree-il-compile-fwd theme-target-tree-il-compile)


(define (make-inst-raw-proc linker addr-proc addr-raw)
  (let ((t-var-proc (tc-tree-il-var-ref0 linker addr-proc))
	(s-var-raw (get-target-var-name linker addr-raw))
	(s-call (tree-il-get-call-keyword linker)))
    `(define ,s-var-raw
       (,s-call (toplevel vector-ref)
		,t-var-proc
		(const 1)))))


(define (tc-tree-il-instance linker instance)
  (assert (list? instance))
  (hfield-set! linker 'current-instance instance)
  (let* ((inst-type (car instance))
	 (to (cadr instance))
	 (address (hfield-ref to 'address)))
    (assert (memq inst-type '(class ltype proc declared-proc raw-proc)))
    (let ((result
	   (case inst-type
	     ((class)
	      (assert (= (length instance) 4))
	      (if (param-class-inst-contains-tvars? instance)
		  '()
		  (let ((param-class (list-ref instance 2))
			(params (list-ref instance 3))
			(binder (get-binder-for-tc linker))
			(ht-cycles (hfield-ref linker 'ht-cycles)))
		    (hash-clear! ht-cycles)
		    ;;		      (detect-cycles binder to ht-cycles '())
		    (detect-cycles binder param-class ht-cycles '())
		    (for-each (lambda (ent)
				(detect-cycles binder ent ht-cycles '()))
			      params)
		    (let ((result
			   (tc-tree-il-param-class-instantiation
			    linker address to param-class params)))
		      (hash-clear! ht-cycles)
		      result))))
	     ((proc)
	      (assert (= (length instance) 3))
	      (let ((expr (list-ref instance 2))
		    (binder (get-binder-for-tc linker))
		    (ht-cycles (hfield-ref linker 'ht-cycles)))
		(hash-clear! ht-cycles)
		(detect-cycles binder expr ht-cycles '())
		(let ((result
		       (tc-tree-il-param-proc-instantiation
			linker address to expr)))
		  (hash-clear! ht-cycles)
		  result)))
	     ((raw-proc)
	      (assert (= (length instance) 3))
	      (let ((addr-raw (caddr instance)))
		(make-inst-raw-proc linker address addr-raw)))
	     (else (raise 'internal-error-in-param-def-instance)))))
      (hfield-set! linker 'current-instance '())
      result)))


(set! tc-tree-il-instance-fwd tc-tree-il-instance)


(define (tc-tree-il-instances linker lst-instances)
  (map (lambda (inst) (tc-tree-il-instance linker inst))
       lst-instances))


(define (tc-tree-il-param-class-instance-predef linker clas address)
  (let ((var-name (get-target-var-name linker address))
	(s-call (tree-il-get-call-keyword linker)))
    `(define
       ,var-name
       (,s-call (toplevel make-pci-preobject2) ,clas))))


(define (tc-tree-il-instance-predef linker instance)
  (case (car instance)
    ((class)
     (if (param-class-inst-contains-tvars? instance)
	 '()
	 (let* ((to (list-ref instance 1))
		(address (hfield-ref to 'address))
		(clas (get-entity-type to))
		(t-clas (theme-target-tree-il-compile linker clas)))
	   (tc-tree-il-param-class-instance-predef linker t-clas address))))
    (else '())))


(set! tc-tree-il-instance-predef-fwd
      tc-tree-il-instance-predef)


(define (tc-tree-il-instance-predefs linker lst-instances)
  (map (lambda (inst) (tc-tree-il-instance-predef linker inst))
       lst-instances))


(define (tc-tree-il-factorized-expr linker fact-expr)
  (assert (is-linker? linker))
  (assert (hrecord-is-instance? fact-expr <factorized-expr>))
  (assert (is-target-object? (hfield-ref fact-expr 'to)))
    (let* ((var-name (get-target-var-name
		      linker
		      (hfield-ref fact-expr 'address)))
	   (r-value-expr (hfield-ref fact-expr 'to))
	   (binder (get-binder-for-tc linker))
	   (ht-cycles (hfield-ref linker 'ht-cycles)))
      (hash-clear! ht-cycles)
      (detect-cycles binder r-value-expr ht-cycles '())
      (let ((result
	     (if (not (is-t-type-list? r-value-expr))
		 (let ((t-value-expr
			(tc-tree-il-object linker r-value-expr '() #f)))
		   `(define ,var-name ,t-value-expr))
		 (let* ((comp (lambda (r-subexpr)
				(tc-tree-il-object linker r-subexpr '() #f)))
			(subexprs (hfield-ref r-value-expr 'subexprs))
			(t-value-exprs (map* comp subexprs))
			(s-call (tree-il-get-call-keyword linker)))
		   `(define ,var-name
		      (,s-call (toplevel list) ,@t-value-exprs))))))
	(hash-clear! ht-cycles)
	result)))
  

(set! tc-tree-il-factorized-expr-fwd tc-tree-il-factorized-expr)


(define (tc-tree-il-param-class-instantiation linker address to
					      param-class params)
  (let* ((t-param-class (theme-target-tree-il-compile linker param-class))
	 (t-params (map* (lambda (param)
			   (theme-target-tree-il-compile linker param))
			 params))
	 (t-var-ref (tc-tree-il-var-ref0 linker address))
	 (s-call (tree-il-get-call-keyword linker))
	 (result
	  `(,s-call (toplevel vector-copy-contents-rev)
		    ,t-var-ref
		    (,s-call (toplevel _i_make-param-class-inst)
			     ,t-param-class
			     (,s-call (toplevel list) ,@t-params)))))
    result))


(define (tc-tree-il-param-ltype-instantiation linker var param-ltype params)
  (let* ((t-param-ltype (theme-target-tree-il-compile linker param-ltype))
	 (t-params (map* (lambda (param)
			   (theme-target-tree-il-compile linker param))
			 params))
	 (address (hfield-ref var 'address))
	 (var-name (get-target-var-name linker address))
	 (s-call (tree-il-get-call-keyword linker)))
    `(define ,var-name
       (,s-call (toplevel _i_get-concrete-param-ltype-inst)
		,t-param-ltype
		(,s-call (toplevel list) ,@t-params)))))


(define (tc-tree-il-param-proc-instantiation linker address to expr)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (is-address? address))
  (assert (or (null? expr) (is-entity? expr)))
  (if (null? expr)
      '(const ())
      (let ((var-name (get-target-var-name linker address))
	    (t-expr (theme-target-tree-il-compile linker expr)))
	(list 'define var-name t-expr))))


(define (tc-tree-il-decl-proc-instance linker var param-proc type-var-values)
  (assert (hrecord-is-instance? linker <linker>))
  (assert (hrecord-is-instance? var <normal-variable>))
  (assert (is-target-object? param-proc))
  (if (hfield-ref param-proc 'incomplete?)
      (raise 'undefined-declared-param-proc)
      (let* ((address (hfield-ref var 'address))
	     (var-name (get-target-var-name linker address))
	     (to-ppc (get-entity-type param-proc))
	     (tvars (tno-field-ref to-ppc 'l-tvars))
	     (type-var-bindings (map cons tvars type-var-values))
	     (value-expr (tno-field-ref param-proc 'x-value-expr))
	     (bind-result (inst-bind-type-vars linker type-var-bindings
					       value-expr))
	     (bound-value-expr (car bind-result))
	     (instantiations (cdr bind-result))
	     (t-preinst (tc-tree-il-instance-predefs
			 linker instantiations))
	     (t-inst (tc-tree-il-instances linker instantiations))
	     (t-proc-expr
	      (theme-target-tree-il-compile linker bound-value-expr))
	     (t-def-expr (list 'define var-name t-proc-expr))
	     (result
	      (if (and (pair? t-def-expr) (eqv? (car t-def-expr) '_splice))
		  (append t-preinst t-inst (cdr t-def-expr))
		  (append t-preinst t-inst (list t-def-expr)))))
	result)))


(define (tc-tree-il-entity linker entity)
  (hfield-set! linker 'current-toplevel-repr entity)
  (let ((result
	 (cond
	  ;; Objects have no effect as toplevel expressions.
	  ((is-target-object? entity)
;;	   (hfield-set! linker 'current-toplevel-repr entity)
	   '(const ()))
	  ((is-expression? entity)
;;	   (hfield-set! linker 'current-toplevel-repr entity)
	   (let ((binder (get-binder-for-tc linker))
		 (ht-cycles (hfield-ref linker 'ht-cycles)))
	     (hash-clear! ht-cycles)
	     (detect-cycles binder entity ht-cycles '())
	     (let ((result
		    (theme-target-tree-il-compile linker entity)))
	       (hash-clear! ht-cycles)
	       result)))
	  ((hrecord-is-instance? entity <linker-instance-predef>)
;;	   (hfield-set! linker 'current-toplevel-repr entity)
	   (tc-tree-il-instance-predef
	    linker
	    (hfield-ref entity 'lst-instance)))
	  ((hrecord-is-instance? entity <linker-instance>)
;;	   (hfield-set! linker 'current-toplevel-repr entity)
	   (tc-tree-il-instance
	    linker
	    (hfield-ref entity 'lst-instance)))
	  ((hrecord-is-instance? entity <factorized-expr>)
;;	   (hfield-set! linker 'current-toplevel-repr entity)
	   (tc-tree-il-factorized-expr linker entity))
	  (else 
	   (raise 'invalid-entity)))))
    result))


(define (tc-tree-il-import-target-module linker l-s-module-name)
  (strong-assert (and (list? l-s-module-name)
		      (not-null? l-s-module-name)
		      (for-all symbol? l-s-module-name)))
  (let* ((addr-iface (linker-alloc-loc linker 'iface #f))
	 (s-iface-gensym (get-target-var-name linker addr-iface))
	 (s-call (tree-il-get-call-keyword linker))
	 (s-seq (tree-il-get-seq-keyword linker)))
    `(,s-seq (let (iface)
	       (,s-iface-gensym)
	       ((,s-call (@@ (guile) resolve-r6rs-interface)
		      (const ,l-s-module-name)))
	       (,s-call (@@ (guile) call-with-deferred-observers)
		     (lambda ()
		       (lambda-case
			((() #f #f #f () ())
			 (,s-call (@@ (guile) module-use-interfaces!)
			       (,s-call (@@ (guile) current-module))
			       (,s-call (@@ (guile) list)
					(lexical
					 iface
					 ,s-iface-gensym))))))))
	     (if (const #f) (const #f) (void)))))


(define (tc-tree-il-define-os-main linker)
  (let* ((addr-args (linker-alloc-loc linker 'args #f))
	 (s-args-gensym (get-target-var-name linker addr-args))
	 (addr-continuation (linker-alloc-loc linker 'continuation #f))
	 (s-continuation-gensym
	  (get-target-var-name linker addr-continuation))
	 (addr-exc (linker-alloc-loc linker 'exc #f))
	 (s-exc-gensym (get-target-var-name linker addr-exc))
	 (addr-t (linker-alloc-loc linker 't #f))
	 (s-t-gensym (get-target-var-name linker addr-t))
	 (s-call (tree-il-get-call-keyword linker))
	 (s-seq (tree-il-get-seq-keyword linker)))
    `(define main
       (lambda ((name . main))
	 (lambda-case
	  (((args) #f #f #f () (,s-args-gensym))
	   (,s-call (@@ (rnrs exceptions) call/cc)
		 (lambda ()
		   (lambda-case
		    (((continuation)
		      #f
		      #f
		      #f
		      ()
		      (,s-continuation-gensym))
		     (,s-call (@@ (rnrs exceptions) with-exception-handler)
			   (lambda ()
			     (lambda-case
			      (((exc) #f #f #f () (,s-exc-gensym))
			       (,s-call (lexical
				      continuation
				      ,s-continuation-gensym)
				     (,s-call (toplevel my-error-exit)
					   (lexical
					    exc
					    ,s-exc-gensym))))))
			   (lambda ()
			     (lambda-case
			      ((() #f #f #f () ())
			       (,s-seq (,s-call
					(toplevel set-rte-exception-info!)
					(const #t))
				    (if (if (let (t)
					      (,s-t-gensym)
					      ((,s-call (toplevel null?)
						     (lexical
						      args
						      ,s-args-gensym)))
					      (if (lexical
						   t
						   ,s-t-gensym)
						  (lexical
						   t
						   ,s-t-gensym)
						  (,s-call (toplevel =)
							(,s-call (toplevel
							       length)
							      (lexical
							       args
							       ,s-args-gensym))
							(const 1))))
					    (,s-call (toplevel eq?)
						  (,s-call (toplevel vector-ref)
							(,s-call (toplevel
							       vector-ref)
							      (toplevel _main)
							      (toplevel
							       i-object-class))
							(toplevel
							 i-procedure-class-arg-list-type))
						  (toplevel _b_<null>))
					    (const #f))
					(,s-call (toplevel main0))
					(,s-call (toplevel main0)
					      (lexical
					       args
					       ,s-args-gensym))))))))))))))))))


(define (tc-tree-il-define-main linker)
  (assert (hrecord-is-instance? linker <linker>))
  (let* ((my-main (get-main linker))
	 (result-type
	  (tno-field-ref (get-entity-type my-main) 'type-result))
	 (addr-args (linker-alloc-loc linker 'args #f))
	 (s-args-gensym (get-target-var-name linker addr-args))
	 (t-args-var (list 'lexical 'args s-args-gensym))
	 (s-call (tree-il-get-call-keyword linker))
	 (s-seq (tree-il-get-seq-keyword linker)))
    (let* ((sx-def1
	    (if (linker-entity-is-none? linker result-type)
		`(define main0
		    (lambda ((name . main0))
		      (lambda-case ((() #f args #f () (,s-args-gensym))
				    (if
				     (,s-call
				      (toplevel _i_check-procedure-arg-list-type?)
				      ,t-args-var
				      (,s-call (toplevel vector-ref)
					       (toplevel _main)
					       (const 0)))
				     (,s-seq
				      (,s-call (toplevel apply)
					       (,s-call (toplevel vector-ref)
							(toplevel _main)
							(const 1))
					       ,t-args-var)
				      (const 0))
				     (,s-call (toplevel raise)
					      (const invalid-arguments-for-main)))))))
		(let* ((addr-res (linker-alloc-loc linker 'res #f))
		       (s-res-gensym (get-target-var-name linker addr-res))
		       (t-res-var (list 'lexical 'res s-res-gensym)))
		`(define main0
		    (lambda ((name . main0))
		      (lambda-case ((() #f args #f () (,s-args-gensym))
				    (if
				     (,s-call
				      (toplevel _i_check-procedure-arg-list-type?)
				      ,t-args-var
				      (,s-call (toplevel vector-ref)
					       (toplevel _main)
					       (const 0)))
				     (let (res)
				       (,s-res-gensym)
				       ((,s-call (toplevel apply)
						 (,s-call (toplevel vector-ref)
							  (toplevel _main)
							  (const 1))
						 ,t-args-var))
				       (,s-call (toplevel theme-exit)
						,t-res-var))
				     (,s-call
				      (toplevel raise)
				      (const invalid-arguments-for-main))))))))))
	   (sx-def2 (tc-tree-il-define-os-main linker)))
      (list sx-def1 sx-def2))))