vj-codegen 0.1.0

LLVM code generator of the vj SystemVerilog simulator.
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
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
//! Expression lowering: exhaustive dispatch over the P0 subset
//! (`IntegerLiteral` / `NamedValue` / `BinaryOp` for AND/OR/XOR/ADD/DIV/SHL),
//! falling through to a `LoweringError` (never a panic, never a silent
//! no-op) for anything outside that subset.
use crate::types::TypeWidth;
use std::collections::HashMap;
use sv_sim_sim::symid::SymbolUid;

use inkwell::AddressSpace;
use inkwell::IntPredicate;
use inkwell::builder::Builder;
use inkwell::context::Context;
use inkwell::module::{Linkage, Module};
use inkwell::values::{IntValue, PointerValue};

use crate::accessor::AccessorFns;

// Corrected vs. plan: `ExpressionKind::Binary` does not exist -- the real
// discriminant (verified against sv-lang's own doctest on
// `Expression::kind`) is `ExpressionKind::BinaryOp`. And `BinaryOp` (the
// *operator* enum: Add, BinaryAnd, ...) is not re-exported from
// `sv_lang::kinds` alongside the AST-node `*Kind` enums -- it lives at the
// `sv-lang` crate's own top level (`sv_lang::BinaryOp`), so it needs its own
// `use`.
use sv_lang::BinaryOp;
use sv_lang::KnownSystemName;
use sv_lang::MethodFlags;
use sv_lang::UnaryOp;
use sv_lang::kinds::ExpressionKind;
use sv_sim_runtime::file_io::{
    SV_RT_FEOF_SYMBOL, SV_RT_FOPEN_MCD_SYMBOL, SV_RT_FOPEN_SYMBOL, SV_RT_FSEEK_SYMBOL,
    SV_RT_SYSTEM_SYMBOL,
};
use sv_sim_runtime::plusargs::{SV_RT_PLUSARGS_TEST_SYMBOL, SV_RT_PLUSARGS_VALUE_SYMBOL};

use crate::error::LoweringError;
use crate::ops::{OpBuilder, is_definitely_false, is_definitely_true, ternary_is_definitely_true};
use crate::types::{LoweredType, int_type, lower_type};
use crate::value::{AnyValue, PackedValue, RealValue, ValueRepr, WideValueRef};

// Task 8 (CG-037): `lower_expr` now produces either an integral
// `PackedValue` or a floating `RealValue` -- a real `Conversion` node
// (int->real / real->int) can only be lowered by `lower_expr` itself (it is
// one `ExpressionKind::Conversion` match arm among the others below), so the
// whole function's return type must be able to carry either kind rather
// than only ever a `PackedValue`. Every existing arm is unaffected in
// substance, just wrapped in `LoweredValue::Packed`; every existing
// recursive call site unwraps via `into_packed` (all of this crate's other
// operators are integral-only, so an operand that turns out to be a real
// value at one of those sites is itself a loud `LoweringError`, never a
// panic).
// Review fix r2 (CG-016/CG-019/CG-129, blocking finding): `lower_expr`'s
// only two production constant-lowering call sites (the `IntegerLiteral`
// arm below and the CG-042 constant-fold `Call` arm) used to call
// `const_svint` directly, which `panic!`s past `WIDE_THRESHOLD` -- so
// `OpBuilder::lower_const_any` (Task 15's actual narrow/wide dispatch
// point) had zero callers in the real `lower_function`/`lower_expr` path,
// and a legal >128-bit SV literal crashed the compiler process instead of
// producing a diagnosable error. `LoweredValue` gains a third `Wide`
// variant so both call sites can route through `lower_const_any` and
// return `AnyValue::Wide` losslessly instead of either panicking or lying
// about the result being narrow. Full wide-value *consumption* (arithmetic
// ops, `return`-by-value ABI) is Task 16+ scope (no wide arithmetic exists
// yet for a `Wide` value to feed into) -- every existing consumer of
// `LoweredValue` (`into_packed`/`into_real`/`func.rs`'s `Return` lowering)
// treats `Wide` the same way it already treats a `Real` value at an
// integral-only site: a loud, named `LoweringError`, never a panic, never a
// silent truncation to narrow.
#[derive(Clone, Copy)]
pub enum LoweredValue<'ctx> {
    Packed(PackedValue<'ctx>),
    Real(RealValue<'ctx>),
    Wide(WideValueRef<'ctx>),
    /// An SV `string` (`crate::strings`).
    Str(crate::strings::StrValue<'ctx>),
    /// A vector wider than `WIDE_THRESHOLD` bits: a memory image
    /// (`crate::huge`), not an LLVM integer.
    Huge(crate::huge::HugeValue<'ctx>),
}

impl<'ctx> LoweredValue<'ctx> {
    pub fn into_packed(
        self,
        what: &str,
        sym: &sv_lang::Symbol,
    ) -> Result<PackedValue<'ctx>, LoweringError> {
        match self {
            LoweredValue::Packed(p) => Ok(p),
            LoweredValue::Real(_) | LoweredValue::Wide(_) | LoweredValue::Str(_) => {
                Err(LoweringError::unsupported_at(what, sym))
            }
            LoweredValue::Huge(h) => Err(LoweringError::unsupported_at(
                &format!(
                    "{what} -- the operand is a {}-bit vector, wider than the wide-value \
                     threshold (LRM 1800-2023 6.9.1), which this construct does not support",
                    h.width
                ),
                sym,
            )),
        }
    }

    pub fn into_real(
        self,
        what: &str,
        sym: &sv_lang::Symbol,
    ) -> Result<RealValue<'ctx>, LoweringError> {
        match self {
            LoweredValue::Real(r) => Ok(r),
            LoweredValue::Packed(_)
            | LoweredValue::Wide(_)
            | LoweredValue::Str(_)
            | LoweredValue::Huge(_) => Err(LoweringError::unsupported_at(what, sym)),
        }
    }
}

// --- Task 7 (CG-034, CG-035, CG-036, CG-038, CG-075): Conversion node
// dispatch classification.

/// The number of `ConversionKind` ordinals this crate's `conversion_kind()`
/// dispatch is pinned against, verified directly against the vendored
/// `sv-lang` 0.1.9 C++ source (`include/slang/ast/Expression.h`'s
/// `CK(x)` enum-generator macro): `Implicit`=0, `Propagated`=1,
/// `StreamingConcat`=2, `Explicit`=3, `BitstreamCast`=4 -- five variants,
/// not the three the plan originally enumerated. CG-075: a `sv-lang`
/// version bump that renumbers or adds a `ConversionKind` variant must show
/// up here as a visible diff (this constant, and `classify_conversion_kind`
/// below, are the single place that range is consulted), never as a silent
/// mis-lowering.
pub const CONVERSION_KIND_COUNT: u32 = 5;

/// What `lower_expr`'s `Conversion` arm does with a given `conversion_kind()`
/// ordinal. Kept as a free function over a raw `u32` (rather than inlined
/// into `lower_expr`'s match) so CG-075's "never panics on an out-of-range
/// value" guarantee is directly unit-testable without needing a live
/// `sv_lang::Expression` that produces one -- real `sv-lang` never emits a
/// value outside `0..CONVERSION_KIND_COUNT` today, so the only way to
/// exercise the catch-all arm is to call this function directly with a
/// synthetic out-of-range ordinal.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConversionAction {
    /// `Implicit`(0) / `Propagated`(1) / `Explicit`(3): an ordinary value
    /// conversion (width extend/truncate, 4-state->2-state flatten, sign
    /// reinterpretation) -- this task's own lowering.
    Value,
    /// `StreamingConcat`(2): a streaming concatenation assigned to its
    /// target type (`crate::stream::lower_stream_conversion`).
    Stream,
    /// `BitstreamCast`(4): a bit-stream cast between types of equal
    /// bit-stream width (`crate::stream::lower_bitstream_cast`).
    BitstreamCast,
}

pub fn classify_conversion_kind(kind: u32) -> Result<ConversionAction, String> {
    match kind {
        0 | 1 | 3 => Ok(ConversionAction::Value),
        2 => Ok(ConversionAction::Stream),
        4 => Ok(ConversionAction::BitstreamCast),
        other => Err(format!(
            "conversion_kind {other} is outside the pinned 0..{CONVERSION_KIND_COUNT} range \
             (Implicit=0/Propagated=1/StreamingConcat=2/Explicit=3/BitstreamCast=4) -- vendored \
             sv-lang may have added or renumbered a ConversionKind variant"
        )),
    }
}

// Task 17 (CG-022): `RangeSelect`'s `range_selection_kind()` ordinal
// dispatch, same pinned-count-plus-exhaustive-fallthrough shape as
// `CONVERSION_KIND_COUNT`/`classify_conversion_kind` above -- verified
// against the vendored sv-lang 0.1.9 C++ source (`include/slang/ast/
// SemanticFacts.h`'s `RANGE(x)` enum-generator macro:
// `x(Simple) x(IndexedUp) x(IndexedDown)`, so `Simple`=0, `IndexedUp`=1,
// `IndexedDown`=2 -- matches the Rust binding's own doc comment on
// `Expression::range_selection_kind`). A future sv-lang version that adds a
// fourth `RangeSelectionKind` variant must show up here as a visible diff,
// never as a silent mis-lowering of an out-of-range ordinal.
pub const RANGE_SELECTION_KIND_COUNT: u32 = 3;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RangeSelectionKindClass {
    /// `v[l:r]`, both bounds compile-time constant.
    Simple,
    /// `v[base +: w]`: low end is `base` itself, `w` compile-time constant.
    IndexedUp,
    /// `v[base -: w]`: low end is `base - (w - 1)`, `w` compile-time constant.
    IndexedDown,
}

pub fn classify_range_selection_kind(kind: u32) -> Result<RangeSelectionKindClass, String> {
    match kind {
        0 => Ok(RangeSelectionKindClass::Simple),
        1 => Ok(RangeSelectionKindClass::IndexedUp),
        2 => Ok(RangeSelectionKindClass::IndexedDown),
        other => Err(format!(
            "range_selection_kind {other} is outside the pinned 0..{RANGE_SELECTION_KIND_COUNT} \
             range (Simple=0/IndexedUp=1/IndexedDown=2) -- vendored sv-lang may have added or \
             renumbered a RangeSelectionKind variant"
        )),
    }
}

/// Reads a compile-time-constant integral expression's value as an `i64`
/// (`constant_value()` -> `as_integer()` -> `as_i64()`, the same chain the
/// `BinaryOp::Power` const-exponent path already uses), or a named
/// `LoweringError` if the expression is not one -- both simple part-select
/// bounds and every indexed part-select/replication WIDTH are required by
/// the LRM to be compile-time constants (slang itself already enforces
/// this during elaboration, so a failure here means either a genuinely
/// dynamic-width source slang would have rejected, or a >64-bit/X-Z-
/// containing constant `as_i64()` itself already refuses to represent).
pub(crate) fn require_const_i64<'d>(
    e: sv_lang::Expression<'d>,
    what: &str,
    sym: &sv_lang::Symbol<'d>,
) -> Result<i64, LoweringError> {
    e.constant_value()
        .as_ref()
        .and_then(sv_lang::ConstantValue::as_integer)
        .and_then(sv_lang::SVInt::as_i64)
        .ok_or_else(|| LoweringError::unsupported_at(what, sym))
}

// Deviation from plan (discovered running Task 8, not covered by the
// preflight corrections): a single `'ctx` on `LowerCtx` forces the sv-lang
// `Design`/`Symbol` lifetime to unify with the unrelated LLVM `Context`
// lifetime the moment a real caller with two independently-scoped locals
// (`Context` and `Design`) constructs one -- `fn_sym: Symbol<'ctx>` makes
// that unification a hard struct-field constraint. It went undetected in
// Task 7 because that task's own test never actually builds a `LowerCtx`.
// Give sv-lang handles their own lifetime, `'d`, independent of `'ctx`.
// Task 12 (CG-134/135/136/137), deviation from plan: `LowerCtx` grows a third
// lifetime `'m` and a `module: &'m Module<'ctx>` field -- `BinaryOp::Power`'s
// runtime-exponent path (`pow4_call`) needs to declare/reuse the shared
// `sv_ipow_{u,s}N` helper IN THE MODULE BEING LOWERED, and nothing before
// this task threaded a `Module` reference through `LowerCtx` (every earlier
// task's `Module` access was confined to `func.rs`'s `lower_function`, which
// never needed to hand it down into expression lowering). `'m` is kept
// independent of `'ctx`/`'d` for the same reason `'d` was split from `'ctx`
// in Task 8: the caller's `Module` local need not live exactly as long as
// the `Context` or `Design` locals.
pub struct LowerCtx<'ctx, 'd, 'm> {
    pub ctx: &'ctx Context,
    pub b: Builder<'ctx>,
    pub repr: ValueRepr,
    pub locals: HashMap<sv_sim_sim::symid::SymbolId, (PointerValue<'ctx>, u32, bool)>,
    /// The enclosing function/task symbol, cited via `hierarchical_path()` in
    /// every `LoweringError` fallthrough so a diagnosable location is never
    /// lost to the `"<expr>"` placeholder (ARCHITECTURE.md §6.1, "Exhaustive
    /// dispatch").
    pub fn_sym: sv_lang::Symbol<'d>,
    /// The LLVM `Module` this expression is being lowered into -- needed
    /// only by `BinaryOp::Power`'s runtime-exponent path so far (Task 12),
    /// but threaded through generally since any future op needing a shared
    /// cross-call helper (Task 21's `EntityTag::Helper` machinery) will need
    /// the same access.
    pub module: &'m Module<'ctx>,
    /// R-run Task 13 (ST-009/ST-024/ST-114): every `SignalSlotClass::Memory`
    /// (fixed-size unpacked array) symbol this expression tree might
    /// reference, by [`sv_sim_sim::symid::SymbolId`] -- consulted by the
    /// `ElementSelect`/`Assignment` arms below to recognize a dynamically
    /// (or constant) indexed memory access (`mem[i]`) and route it through
    /// [`MemAccessorCtx`]/`accessor::build_get_mem_word`/`build_set_mem_word`
    /// instead of the ordinary `locals`-keyed scalar/bit-select path (a
    /// memory has no single value `locals` could ever hold). Empty for every
    /// caller with no external memories in scope (`func.rs`'s own local
    /// subroutine-compilation pipeline, every test that builds no `Memory`
    /// signal) -- deliberately NOT keyed together with `locals` in one map,
    /// since a `Memory` symbol is never validly read/written as a whole
    /// value the way every `locals` entry is.
    pub mem_signals: HashMap<sv_sim_sim::symid::SymbolId, MemorySignalInfo>,
    /// The `(handle, accessor fns)` pair needed to actually emit a
    /// `build_get_mem_word`/`build_set_mem_word` call -- `None` whenever
    /// `mem_signals` is empty (no external memory can be referenced, so no
    /// accessor context is ever needed); `Some` at every `process_lower.rs`
    /// call site that built a nonempty `mem_signals`.
    pub mem_accessor: Option<MemAccessorCtx<'ctx, 'm>>,
    /// The current value of a compound assignment's target (`a += b` is
    /// elaborated as `a = <LValueReference> + b`), read by the
    /// `LValueReference` arm. Set only while lowering that assignment's RHS.
    pub lvalue_ref: std::cell::Cell<Option<LoweredValue<'ctx>>>,
    /// While a deferred immediate assertion's action is lowered (LRM 16.4.1):
    /// the `i64` slot holding where the action's captured output starts, or
    /// `u64::MAX` before the action's first printing call has evaluated its
    /// arguments. The arguments are evaluated when the assertion fails, so
    /// what they print (a function called in them) is not part of the
    /// deferred report; `crate::process_lower::mark_deferred_args_done` moves
    /// the start past it.
    pub deferred_start: std::cell::Cell<Option<PointerValue<'ctx>>>,
    /// The value of `$` (`q[$]`, `q[1:$]`): the last index of the queue
    /// whose selector is being lowered (`crate::collections`).
    pub unbounded: std::cell::Cell<Option<IntValue<'ctx>>>,
    /// The shadow copy of a queue / dynamic-array element while a write into
    /// PART of it (`d[i][j] = v`, `q[i].f = v`, `d[i][3] = b`) resolves: the
    /// partial target's root is this temporary, which [`store_to_releasing`]
    /// writes back to the element (LRM 7.4.5, 7.5, 7.10).
    pub coll_temp: std::cell::Cell<Option<inkwell::values::PointerValue<'ctx>>>,
    /// The iterator variables of the array methods whose `with` clauses
    /// are being lowered (`item` in `q.find(item) with (item > 3)`), each
    /// with its current element and index (`item.index`), innermost last
    /// (`crate::array_methods`).
    #[allow(clippy::type_complexity)]
    pub iter: std::cell::RefCell<
        Vec<(
            sv_sim_sim::symid::SymbolId,
            LoweredValue<'ctx>,
            LoweredValue<'ctx>,
        )>,
    >,
    /// The enclosing loops' `continue` and `break` targets, innermost last
    /// (loops lowered straight-line: every loop of a function, and a
    /// suspend-free `repeat` in a process -- a process's other loops are
    /// `process::cfg` blocks, whose `break`/`continue` are CFG jumps).
    pub loop_exits: Vec<(
        inkwell::basic_block::BasicBlock<'ctx>,
        inkwell::basic_block::BasicBlock<'ctx>,
    )>,
    /// The enclosing named blocks a `disable <block>;` can leave (LRM
    /// 1800-2023 §9.6.2), innermost last, each with its exit block. Only a
    /// subroutine body fills this: a process's `disable` is a CFG jump.
    pub block_exits: Vec<(String, inkwell::basic_block::BasicBlock<'ctx>)>,
    /// Set for a process kernel's segments: every blocking write to a
    /// module signal's shadow copy is also committed to STATE on the spot
    /// (see [`WriteThrough`]). `None` for functions and continuous assigns.
    pub write_through: Option<WriteThrough<'ctx, 'm>>,
    /// The variables a process shares with its fork branches: each one's
    /// cell pointer (an `i64` `alloca`) and its size in bytes, keyed like
    /// `locals` (whose entry is the value's working copy).
    pub cells: HashMap<sv_sim_sim::symid::SymbolId, (PointerValue<'ctx>, u32)>,
    /// A static subroutine's `output`/`inout`/`ref` formals: (its signal
    /// copy, the caller's temporary, size in bytes) -- copied back before every
    /// return.
    pub copy_out: Vec<(PointerValue<'ctx>, PointerValue<'ctx>, IntValue<'ctx>)>,
    /// The `ref` formals bound to a queue / dynamic-array element, keyed by
    /// the formal's frame local (LRM 1800-2023 13.5.2): every read of the
    /// local first refreshes it from the element and every write is put back
    /// (`crate::collections::elem_ref_refresh` / `elem_ref_flush`). Empty
    /// unless a process inlines such a call.
    pub elem_refs: HashMap<PointerValue<'ctx>, crate::collections::ElemRefSlot<'ctx>>,
    /// Classes plan Task 5 (review fix r2): while [`crate::classes::
    /// lower_new_class`] is lowering ONE OF ITS OWN property initializers
    /// (LRM 1800-2023 §8.7), this holds the handle of the object JUST
    /// ALLOCATED -- so [`crate::classes::try_read_bare_field`]/
    /// [`crate::classes::try_write_field`]'s bare-field resolution (an
    /// `int b = a + 1;` initializer referencing an earlier sibling
    /// property by bare name) reads/writes THAT object, never whatever
    /// `this` happens to be lexically ambient at the `new` expression's own
    /// call site. A `Cell`, not a `cx.locals` entry, because every
    /// expression-lowering call site threads `cx` by shared reference
    /// (`&LowerCtx`, never `&mut`) -- this is the same scoped-override-
    /// through-interior-mutability shape `lvalue_ref`/`unbounded` above
    /// already use, not a new pattern. `None` at every other call site (an
    /// ordinary method body, a continuous assign, ...), where bare-field
    /// resolution falls through unchanged to `current_this_var`/
    /// `cx.locals`. Save/restored (never unconditionally cleared) around
    /// each property's own initializer, so a `new` nested inside one
    /// property's initializer unwinds back to the OUTER object's handle,
    /// not to `None`, before that outer object's own next field is lowered.
    pub ctor_this: std::cell::Cell<Option<IntValue<'ctx>>>,
    /// The `(unit, precision)` exponents of the inlined subroutine whose body the statement being
    /// lowered was written in, when that is not the process's own scope: a delay in it is in the
    /// declaring scope's time unit (LRM 3.14.2.1), see [`crate::timescale::lower_delay_ticks`].
    pub decl_scale: std::cell::Cell<Option<(i32, i32)>>,
    /// The `this` of the class-method task inlined at the segment being
    /// lowered (`process::cfg::BasicBlock::this_var`), for a process that is
    /// not itself a method: what [`crate::classes::current_this_var`] falls
    /// back to.
    pub inline_this: Option<sv_lang::Symbol<'d>>,
    /// Inside an assertion leaf: the 0/1 flag of each end-point application
    /// (`s.triggered`) in its expression, by the instance's source offset
    /// (`sva::emit`'s `prime_end_flags`); empty everywhere else.
    pub end_flags: std::cell::RefCell<HashMap<u64, PointerValue<'ctx>>>,
    /// Task 4b (`docs/superpowers/plans/2026-10-03-sdf.md`): a specify-path
    /// VARIABLE destination's own staging-cell foreign [`crate::
    /// process_lower::SignalSlot`] (`ProcessLoweringPlan::
    /// specify_dest_redirect`, carried over unchanged) -- consulted ONLY by
    /// [`crate::select::resolve_lvalue`]'s `RootMode::Signal` branch and
    /// [`crate::process_lower::lower_nonblocking_assignment`]'s own fast-
    /// path exclusion, both keyed off a NONBLOCKING write's own lvalue
    /// symbol; never read by any other `LowerCtx` consumer. Empty at every
    /// construction site that does not build a module-level process kernel
    /// from a real `ProcessLoweringPlan` (a function/task body, a DPI
    /// thunk, a continuous assign) -- none of those can ever name a
    /// specify-path destination (LRM 30.4.1 restricts one to a module
    /// member written by a procedural block), so this field is a true
    /// no-op there, not a narrowed/unsupported case.
    pub specify_dest: HashMap<sv_sim_sim::symid::SymbolId, crate::process_lower::SignalSlot>,
}

/// Blocking writes to module signals in a process are committed to STATE
/// immediately (not only at segment end), so a same-timestep pulse such as
/// `rst = 1; rst = 0;` is a real pair of value changes other processes'
/// `@(rst)`/`$monitor` observe (LRM 1800-2023 §4.9.3/§10.4.1). The shadow
/// copy keeps serving this segment's reads.
#[derive(Clone)]
pub struct WriteThrough<'ctx, 'a> {
    pub handle: PointerValue<'ctx>,
    pub fns: &'a AccessorFns<'ctx>,
    /// A module signal's shadow `alloca` -> (region-biased slot, width,
    /// wide?).
    pub signals: HashMap<PointerValue<'ctx>, (u32, u32, bool)>,
    /// The one shared re-read block of this function's statement probes
    /// (see [`crate::vpi_dyn::SharedReload`]).
    pub reload: std::cell::RefCell<Option<crate::vpi_dyn::SharedReload<'ctx>>>,
}

/// Commits the shadow copy at `ptr` to STATE if `cx` writes through and
/// `ptr` is a module signal's shadow (see [`WriteThrough`]).
///
/// A huge shadow is committed from its dirty window only: this entry point
/// first marks the whole signal dirty (the caller wrote it anywhere); a caller
/// that knows the window it wrote marks that itself and uses
/// [`write_through_commit`].
pub(crate) fn write_through_store<'ctx>(cx: &LowerCtx<'ctx, '_, '_>, ptr: PointerValue<'ctx>) {
    crate::collections::elem_ref_flush(cx, ptr);
    if let Some(wt) = &cx.write_through
        && let Some(&(slot, width, _)) = wt.signals.get(&ptr)
        && crate::huge::is_huge_width(width)
    {
        let i64t = cx.ctx.i64_type();
        crate::huge::rt_mark(
            cx,
            wt.handle,
            slot,
            width,
            i64t.const_zero(),
            i64t.const_int(u64::from(width), false),
        );
    }
    write_through_commit(cx, ptr);
}

/// [`write_through_store`] for a huge shadow whose written window the caller
/// has already marked (`crate::huge`).
pub(crate) fn write_through_commit<'ctx>(cx: &LowerCtx<'ctx, '_, '_>, ptr: PointerValue<'ctx>) {
    let Some(wt) = &cx.write_through else {
        return;
    };
    commit_shadow(cx, wt, ptr);
}

/// Commits the shadow copy at `ptr` to STATE through `wt` (a huge one from
/// its marked windows).
pub(crate) fn commit_shadow<'ctx>(
    cx: &LowerCtx<'ctx, '_, '_>,
    wt: &WriteThrough<'ctx, '_>,
    ptr: PointerValue<'ctx>,
) {
    let Some(&(slot, width, wide)) = wt.signals.get(&ptr) else {
        return;
    };
    if crate::huge::is_huge_width(width) {
        crate::huge::rt_commit(cx, wt.handle, slot, width);
    } else {
        let cur =
            cx.b.build_load(int_type(cx.ctx, width * 2), ptr, "wt_ld")
                .unwrap()
                .into_int_value();
        if wide {
            crate::accessor::build_set_wide(cx, wt.fns, wt.handle, slot, cur, width);
        } else {
            crate::accessor::build_set_scalar(cx, wt.fns, wt.handle, slot, cur, width);
        }
    }
    // A forced signal ignores the write (`crate::force`): the copy follows
    // STATE, not the assignment.
    if crate::force::reload_after_write() {
        reload_from_state(cx, wt, ptr);
    }
}

/// Re-reads the module signal whose shadow copy is `ptr` from STATE.
pub(crate) fn reload_from_state<'ctx>(
    cx: &LowerCtx<'ctx, '_, '_>,
    wt: &WriteThrough<'ctx, '_>,
    ptr: PointerValue<'ctx>,
) {
    let Some(&(slot, width, wide)) = wt.signals.get(&ptr) else {
        return;
    };
    if crate::huge::is_huge_width(width) {
        // The shadow keeps its address: this only re-reads it from STATE.
        crate::huge::rt_prime(cx, wt.handle, slot, width, true);
        return;
    }
    let v = if wide {
        crate::accessor::build_get_wide(cx, wt.fns, wt.handle, slot, width)
    } else {
        crate::accessor::build_get_scalar(cx, wt.fns, wt.handle, slot, width)
    };
    cx.b.build_store(ptr, v).unwrap();
}

/// R-run Task 13: one `SignalSlotClass::Memory` signal's accessor-ABI
/// address -- the `expr.rs`-local mirror of `sv-sim-codegen::process_lower::
/// SignalSlot` restricted to the `Memory` case (kept as its own small type,
/// not a re-export of `SignalSlot` itself, since `process_lower.rs` is this
/// crate's own consumer of `expr.rs`, not the other way around, and
/// `SignalSlot`'s `class` field would otherwise have to be re-matched at
/// every `ElementSelect`/`Assignment` call site instead of narrowed once
/// where `LowerCtx::mem_signals` is built).
#[derive(Clone, Copy, Debug)]
pub struct MemorySignalInfo {
    pub slot: u32,
    /// The per-ROW element bit width (never the unpacked-array `Type`'s own
    /// `bit_width() == 0`).
    pub elem_width: u32,
    pub signed: bool,
    pub num_rows: u32,
    /// The declared unpacked range's lower bound (SV index of storage row 0).
    pub lower_index: i64,
}

/// R-run Task 13: the `(handle, accessor fns)` pair [`LowerCtx::
/// mem_accessor`] carries. A small `Copy` bundle (never the whole private
/// [`AccessorFns`]) so `expr.rs`'s `ElementSelect`/`Assignment` arms can
/// reach `accessor::build_get_mem_word`/`build_set_mem_word` without this
/// module owning any accessor-declaration logic itself.
#[derive(Clone, Copy)]
pub struct MemAccessorCtx<'ctx, 'a> {
    pub handle: PointerValue<'ctx>,
    pub fns: &'a AccessorFns<'ctx>,
}

// Task 13 (CG-042): the exact set of `KnownSystemName` variants this crate
// treats as a "pure/static system function ($bits, $size, type queries)" --
// LRM 1800-2023 SS20.6.1 ($bits) and SS20.7 (the array-query family: $low/
// $high/$left/$right/$size/$increment/$dimensions/$unpacked_dimensions).
// Deliberately NOT a wildcard/catch-all over "anything with a cached
// constant": see the `ExpressionKind::Call` arm's doc comment for why
// `$rtoi`/`$itor`/`$clog2`/etc. must stay excluded even though they too can
// sometimes be compile-time constant. Not one of this crate's three
// Kind::COUNT-pinned exhaustive-dispatch enums (`KnownSystemName` is a much
// larger, slower-moving registry, not an AST node-kind discriminant), so
// this is an explicit allowlist with a `false` default, not a fallthrough
// error -- callers that fail the allowlist get a named, specific
// `LoweringError` from the `Call` arm itself, never a silent no-op here.
fn is_cg042_type_query(name: KnownSystemName) -> bool {
    matches!(
        name,
        KnownSystemName::Bits
            | KnownSystemName::Size
            | KnownSystemName::Low
            | KnownSystemName::High
            | KnownSystemName::Left
            | KnownSystemName::Right
            | KnownSystemName::Increment
            | KnownSystemName::Dimensions
            | KnownSystemName::UnpackedDimensions
            // LRM 20.6.2 `$isunbounded`: an elaboration-time query too
            // (`$typename` folds to a string: `strings::lower_string_expr`).
            | KnownSystemName::IsUnbounded
    )
}

// --- Task F2 (RT-043 partial): `$time` / `$stime` / `$realtime` ----------

/// The frozen ABI symbol name for this task's own new runtime entry point --
/// `extern "C" fn() -> i64`, returning the scheduler's current tick count.
/// Declared here (the one place a `$time`/`$stime`/`$realtime` call needs
/// it) and referenced by full path from `pipeline.rs` (`RealBindings`'s
/// `time` field / `bind_or_skip`) exactly the way `crate::process_lower::
/// SV_RT_FINISH_SYMBOL` already is for `$finish` -- see
/// [`fetch_or_declare_sv_rt_time`]'s own doc comment for why this symbol
/// deliberately carries no `SimHandle` parameter, unlike the plan's literal
/// `sv_rt_time(state: SimHandle) -> u64` sketch.
pub const SV_RT_TIME_SYMBOL: &str = "sv_rt_time";

/// Declares (or fetches, if some earlier `$time`/`$stime`/`$realtime` call
/// in this same `Module` already did) `extern "C" fn sv_rt_time() -> i64`
/// (`SV_RT_TIME_SYMBOL`) -- mirrors `process_lower::declare_rt_finish`'s
/// "declared, never defined, in this crate" shape: a real caller
/// (`sv-sim-bin::run`) binds the real production body
/// (`sv_sim_sim::sched::kernel_adapter::real_sv_rt_time`) via
/// `Jit::add_global_mapping`, and a test supplies whatever stub it needs the
/// same way `tests/display_lowering.rs`/`tests/process_kernel_abi.rs`
/// already establish for `nba_enqueue`/`sv_rt_finish`.
///
/// **Deviation from the plan's literal `sv_rt_time(state: SimHandle) -> u64`
/// signature:** `lower_expr`/`LowerCtx` have no `SimHandle`-carrying
/// `PointerValue` in scope at an arbitrary expression-lowering call site
/// (unlike `$finish`'s STATEMENT-level lowering, which `lower_system_call_
/// statement` receives a `handle: PointerValue` argument for specifically to
/// pass through) -- threading one into `LowerCtx`/`lower_expr`'s signature
/// would touch every one of this file's ~20 recursive `lower_expr` call
/// sites and every test that hand-builds a `LowerCtx` for a reason entirely
/// unrelated to this task. It would also be dead weight: the real
/// production body reads the scheduler's current time through the same
/// `CURRENT_OPS` thread-local bridge `real_nba_enqueue`/`real_sv_rt_finish`
/// already use (`sv_sim_sim::sched::kernel_adapter`'s own doc comment), NOT
/// through `SimHandle` -- which, exactly as those two functions' own doc
/// comments already record, resolves only to STATE's arena, never to a
/// scheduler-ops handle. `real_sv_rt_finish` itself already ignores the
/// `SimHandle` its own ABI-pinned signature carries for the same reason.
/// The return type is a plain `i64` (never `u128`/packed): `$time`'s tick
/// count is never itself 4-state (a tick count is always fully known), so
/// the packing into `PackedValue`'s `i(2N)` convention (with an all-zero
/// unknown lane) happens once, at [`lower_time_system_call`]'s own call
/// site, not inside the runtime.
pub(crate) fn fetch_or_declare_sv_rt_time<'ctx>(
    ctx: &'ctx Context,
    module: &Module<'ctx>,
) -> inkwell::values::FunctionValue<'ctx> {
    module.get_function(SV_RT_TIME_SYMBOL).unwrap_or_else(|| {
        let fn_ty = ctx.i64_type().fn_type(&[], false);
        module.add_function(SV_RT_TIME_SYMBOL, fn_ty, Some(Linkage::External))
    })
}

// --- Named events ---------------------------------------------------------

/// A named `event`'s cell (`sv_sim_sim::state::EVENT_CELL_WIDTH` = 128 bits,
/// 2-state): the low 64 bits count triggers, the high 64 hold `tick + 1` of
/// the most recent trigger (0 = never triggered). Returns the event's shadow
/// pointer from `cx.locals` after checking its width.
pub(crate) fn event_cell_ptr<'ctx, 'd, 'm>(
    cx: &LowerCtx<'ctx, 'd, 'm>,
    target: sv_lang::Expression<'d>,
    what: &str,
) -> Result<PointerValue<'ctx>, LoweringError> {
    if !sv_sim_sim::symid::is_value_name(target.kind()) {
        return Err(LoweringError::unsupported_at(
            &format!(
                "{what}: the event operand ({:?}) must be a plain named event (event \
                 handles held in variables / hierarchical events are not supported)",
                target.kind()
            ),
            &cx.fn_sym,
        ));
    }
    let sym = target.referenced_symbol().ok_or_else(|| {
        LoweringError::unsupported_at(&format!("{what}: unresolved event name"), &cx.fn_sym)
    })?;
    if let Some(hier) = sv_sim_sim::state::event_pool::hierarchical_pool_event(&target) {
        return Err(LoweringError::unsupported_at(
            &format!(
                "{what}: the hierarchical event `{}` belongs to an instance that keeps its \
                 events in an event pool (it assigns, compares or passes an event, LRM \
                 1800-2023 15.5); another instance's access to its legacy cell would be \
                 silently lost",
                hier.name()
            ),
            &cx.fn_sym,
        ));
    }
    let (ptr, width, _) = *cx.locals.get(&sym.uid()).ok_or_else(|| {
        LoweringError::unsupported_at(
            &format!("{what}: the event is not a module-level signal of this process's scope"),
            &sym,
        )
    })?;
    if width != sv_sim_sim::state::EVENT_CELL_WIDTH {
        return Err(LoweringError::unsupported_at(
            &format!(
                "{what}: operand is a {width}-bit signal, not a named event's \
                 {}-bit cell",
                sv_sim_sim::state::EVENT_CELL_WIDTH
            ),
            &sym,
        ));
    }
    Ok(ptr)
}

/// `-> e`: increments the trigger count and stamps `tick + 1` into the high
/// half (see [`event_cell_ptr`]). Stored through the event's shadow copy;
/// the segment flushes it (`process_lower::external_refs_in_segment` records
/// an `EventTrigger` target as written).
pub(crate) fn lower_event_trigger<'ctx, 'd, 'm>(
    cx: &LowerCtx<'ctx, 'd, 'm>,
    target: sv_lang::Expression<'d>,
) -> Result<(), LoweringError> {
    if crate::class_events::lower_trigger(cx, target)? {
        return Ok(());
    }
    if sv_sim_sim::netlist::vif::is_member_access(&target) {
        return crate::vif::event_trigger(cx, target);
    }
    if let Some(r) = crate::vif::event_elem_trigger(cx, target) {
        return r;
    }
    // An element of an event array (`-> ea[i]`): one row of its memory,
    // rewritten through the memory accessor; an index that is unknown or
    // out of range triggers nothing (LRM 1800-2023 7.4.6, 15.5).
    if let Some(el) = crate::arrays::resolve_event_element(cx, target)? {
        return crate::arrays::when_valid(cx, el.valid, || {
            let cur = crate::arrays::load_event_elem(cx, &el)?;
            let triggered = event_triggered_value(cx, cur);
            crate::arrays::store_event_elem(cx, &el, triggered)
        });
    }
    let ptr = event_cell_ptr(cx, target, "-> (event trigger)")?;
    let w = sv_sim_sim::state::EVENT_CELL_WIDTH;
    let packed_ty = int_type(cx.ctx, w * 2);
    let cur =
        cx.b.build_load(packed_ty, ptr, "event_cur")
            .unwrap()
            .into_int_value();
    let packed = event_triggered_value(cx, cur);
    cx.b.build_store(ptr, packed).unwrap();
    write_through_store(cx, ptr);
    Ok(())
}

/// The cell value of an event whose cell now holds `cur` (packed, see
/// [`event_cell_ptr`]) after one more trigger: the count plus one, stamped
/// with `tick + 1`.
pub(crate) fn event_triggered_value<'ctx>(
    cx: &LowerCtx<'ctx, '_, '_>,
    cur: IntValue<'ctx>,
) -> IntValue<'ctx> {
    let w = sv_sim_sim::state::EVENT_CELL_WIDTH;
    let packed_ty = int_type(cx.ctx, w * 2);
    let val_ty = int_type(cx.ctx, w);
    let i64_ty = cx.ctx.i64_type();
    let count = cx.b.build_int_truncate(cur, i64_ty, "event_count").unwrap();
    let count =
        cx.b.build_int_add(count, i64_ty.const_int(1, false), "event_count_inc")
            .unwrap();
    let rt_time = fetch_or_declare_sv_rt_time(cx.ctx, cx.module);
    let now =
        cx.b.build_call(rt_time, &[], "event_now")
            .unwrap()
            .try_as_basic_value()
            .unwrap_basic()
            .into_int_value();
    let stamp =
        cx.b.build_int_add(now, i64_ty.const_int(1, false), "event_stamp")
            .unwrap();
    let lo = cx.b.build_int_z_extend(count, val_ty, "event_lo").unwrap();
    let hi = cx.b.build_int_z_extend(stamp, val_ty, "event_hi").unwrap();
    let hi =
        cx.b.build_left_shift(hi, val_ty.const_int(64, false), "event_hi_sh")
            .unwrap();
    let val = cx.b.build_or(lo, hi, "event_val").unwrap();
    // UNK plane (the high `w` bits of the packed value) stays 0: 2-state.
    cx.b.build_int_z_extend(val, packed_ty, "event_packed")
        .unwrap()
}

/// The packed value of watcher cell `id` (`sv_sim_sim::sva::endpoint`) read
/// through the instance handle, in a subroutine lowered on its own: the cell is
/// not one of its arguments, and the state holds the stamp the engine wrote.
fn watcher_cell_through_handle<'ctx>(
    cx: &LowerCtx<'ctx, '_, '_>,
    id: &sv_sim_sim::symid::SymbolId,
) -> Result<IntValue<'ctx>, LoweringError> {
    let accessor = cx.mem_accessor.ok_or_else(|| {
        LoweringError::unsupported_at(
            "a sequence end-point method (`.triggered`, 16.9.11) read where no instance \
             handle is available",
            &cx.fn_sym,
        )
    })?;
    let body = sv_sim_sim::netlist::enclosing_instance_body(&cx.fn_sym).ok_or_else(|| {
        LoweringError::unsupported_at(
            "a sequence end-point method (`.triggered`, 16.9.11) read outside any instance",
            &cx.fn_sym,
        )
    })?;
    let slots = crate::module::signal_slots_of(&body)?;
    let slot = slots.get(id).ok_or_else(|| {
        LoweringError::unsupported_at(
            "a sequence end-point method (`.triggered`, 16.9.11) read in a subroutine whose \
             instance is not watched (called from no procedural block of its instance)",
            &cx.fn_sym,
        )
    })?;
    Ok(crate::accessor::build_get_wide(
        cx,
        accessor.fns,
        accessor.handle,
        slot.slot,
        slot.width,
    ))
}

/// `e.triggered` (LRM 1800-2023 §15.5.3): 1 iff `e` was triggered in the
/// current time step, i.e. its stamped `tick + 1` equals `$time + 1`.
fn lower_event_triggered<'ctx, 'd, 'm>(
    cx: &LowerCtx<'ctx, 'd, 'm>,
    e: sv_lang::Expression<'d>,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    let operands = e.operands();
    let target = *operands.first().ok_or_else(|| {
        LoweringError::unsupported_at(".triggered with no event operand", &cx.fn_sym)
    })?;
    if let Some(v) = crate::class_events::lower_triggered(cx, target)? {
        return Ok(v);
    }
    if sv_sim_sim::netlist::vif::is_member_access(&target) {
        let hit = crate::vif::event_triggered(cx, target)?;
        let op = OpBuilder {
            ctx: cx.ctx,
            b: &cx.b,
        };
        return Ok(LoweredValue::Packed(op.pack(
            hit,
            cx.ctx.bool_type().const_zero(),
            1,
            cx.repr,
            false,
        )));
    }
    if let Some(hit) = crate::vif::event_elem_triggered(cx, target) {
        let op = OpBuilder {
            ctx: cx.ctx,
            b: &cx.b,
        };
        return Ok(LoweredValue::Packed(op.pack(
            hit?,
            cx.ctx.bool_type().const_zero(),
            1,
            cx.repr,
            false,
        )));
    }
    if target.kind() == ExpressionKind::AssertionInstance
        && let Some(&slot) = cx
            .end_flags
            .borrow()
            .get(&target.source_range().start.offset)
    {
        // Inside an assertion leaf: the flag the end-point edge stored.
        let bit =
            cx.b.build_load(cx.ctx.bool_type(), slot, "end_flag")
                .unwrap()
                .into_int_value();
        let op = OpBuilder {
            ctx: cx.ctx,
            b: &cx.b,
        };
        return Ok(LoweredValue::Packed(op.pack(
            bit,
            cx.ctx.bool_type().const_zero(),
            1,
            cx.repr,
            false,
        )));
    }
    let ptr = if target.kind() == ExpressionKind::AssertionInstance {
        // A procedure reading a sequence instance's end point (16.9.11): its
        // watcher's cell (`sv_sim_sim::sva::endpoint`), stamped like a named
        // event's by the engine in the Observed region.
        let id = sv_sim_sim::sva::endpoint::watcher_id(&cx.fn_sym.uid(), &target);
        match cx.locals.get(&id) {
            Some(&(p, _, _)) => p,
            // Read in a called subroutine, which owns the watcher (the cell is
            // then read through the instance handle).
            None => {
                let cur = watcher_cell_through_handle(cx, &id)?;
                let hit = event_stamped_now(cx, cur);
                let op = OpBuilder {
                    ctx: cx.ctx,
                    b: &cx.b,
                };
                return Ok(LoweredValue::Packed(op.pack(
                    hit,
                    cx.ctx.bool_type().const_zero(),
                    1,
                    cx.repr,
                    false,
                )));
            }
        }
    } else if let Some(el) = crate::arrays::resolve_event_element(cx, target)? {
        // `ea[i].triggered`: false for an index that is unknown or out of
        // range (LRM 1800-2023 7.4.6).
        let cur = crate::arrays::load_event_elem(cx, &el)?;
        let hit = event_stamped_now(cx, cur);
        let hit =
            cx.b.build_and(hit, el.valid, "event_elem_triggered")
                .unwrap();
        let op = OpBuilder {
            ctx: cx.ctx,
            b: &cx.b,
        };
        return Ok(LoweredValue::Packed(op.pack(
            hit,
            cx.ctx.bool_type().const_zero(),
            1,
            cx.repr,
            false,
        )));
    } else {
        event_cell_ptr(cx, target, ".triggered")?
    };
    let w = sv_sim_sim::state::EVENT_CELL_WIDTH;
    let packed_ty = int_type(cx.ctx, w * 2);
    let cur =
        cx.b.build_load(packed_ty, ptr, "event_cur")
            .unwrap()
            .into_int_value();
    let hit = event_stamped_now(cx, cur);
    let op = OpBuilder {
        ctx: cx.ctx,
        b: &cx.b,
    };
    Ok(LoweredValue::Packed(op.pack(
        hit,
        cx.ctx.bool_type().const_zero(),
        1,
        cx.repr,
        false,
    )))
}

/// Whether the event cell value `cur` (packed) was stamped in the current
/// time step: its high half equals `$time + 1`.
pub(crate) fn event_stamped_now<'ctx>(
    cx: &LowerCtx<'ctx, '_, '_>,
    cur: IntValue<'ctx>,
) -> IntValue<'ctx> {
    let packed_ty = cur.get_type();
    let i64_ty = cx.ctx.i64_type();
    let hi =
        cx.b.build_right_shift(cur, packed_ty.const_int(64, false), false, "event_hi")
            .unwrap();
    let hi = cx.b.build_int_truncate(hi, i64_ty, "event_stamp").unwrap();
    let rt_time = fetch_or_declare_sv_rt_time(cx.ctx, cx.module);
    let now =
        cx.b.build_call(rt_time, &[], "event_now")
            .unwrap()
            .try_as_basic_value()
            .unwrap_basic()
            .into_int_value();
    let now1 =
        cx.b.build_int_add(now, i64_ty.const_int(1, false), "event_now1")
            .unwrap();
    cx.b.build_int_compare(IntPredicate::EQ, hi, now1, "event_triggered")
        .unwrap()
}

// --- B-runtime Task 6 (RT-078..RT-082): `$random` / `$urandom` /
// `$urandom_range` -------------------------------------------------------

/// The frozen ABI symbol name for `$random`'s real runtime entry point --
/// `extern "C" fn() -> i32`. Zero-arg by construction, exactly like
/// [`SV_RT_TIME_SYMBOL`] above and for the identical reason: no per-process
/// seed pointer exists anywhere reachable from `lower_expr`'s call site today
/// (`process::ActivationRecord` has no such field yet -- `B-core`'s `CG-176`,
/// concurrently in progress, not this track's file to add it to). The real
/// production body (`sv_sim_runtime::random_sink::real_sv_rt_random`) owns a
/// thread-local seed cell internally and is the seam that becomes a plain
/// forwarder onto a real per-process field the day `CG-176` lands -- see that
/// module's own doc comment.
pub const SV_RT_RANDOM_SYMBOL: &str = "sv_rt_random";
/// Ditto, for `$urandom`.
pub const SV_RT_URANDOM_SYMBOL: &str = "sv_rt_urandom";
/// Ditto, for `$urandom_range(hi, lo)` -- `extern "C" fn(i32, i32) -> i32`.
pub const SV_RT_URANDOM_RANGE_SYMBOL: &str = "sv_rt_urandom_range";
/// Review fix r2 (blocking finding, RT-078/079): `$random(seed)`'s LRM-legal
/// optional inout `seed` argument (IEEE 1800-2023 SS20.15.1) -- a genuinely
/// DIFFERENT ABI symbol from [`SV_RT_RANDOM_SYMBOL`] (`extern "C" fn(*mut
/// i32) -> i32`, never zero-arg), since this call form reseeds purely from
/// the passed-in variable's own current value rather than any ambient
/// per-process stream -- see `sv_sim_runtime::random::sv_rt_random_reseed`'s
/// own doc comment for the full semantics and the real-`iverilog`-verified
/// behavior it matches structurally.
pub const SV_RT_RANDOM_RESEED_SYMBOL: &str = "sv_rt_random_reseed";
/// Ditto, for `$urandom(seed)` -- `sv_sim_runtime::random::
/// sv_rt_urandom_reseed`.
pub const SV_RT_URANDOM_RESEED_SYMBOL: &str = "sv_rt_urandom_reseed";

fn fetch_or_declare_sv_rt_random<'ctx>(
    ctx: &'ctx Context,
    module: &Module<'ctx>,
) -> inkwell::values::FunctionValue<'ctx> {
    module.get_function(SV_RT_RANDOM_SYMBOL).unwrap_or_else(|| {
        let fn_ty = ctx.i32_type().fn_type(&[], false);
        module.add_function(SV_RT_RANDOM_SYMBOL, fn_ty, Some(Linkage::External))
    })
}

fn fetch_or_declare_sv_rt_urandom<'ctx>(
    ctx: &'ctx Context,
    module: &Module<'ctx>,
) -> inkwell::values::FunctionValue<'ctx> {
    module
        .get_function(SV_RT_URANDOM_SYMBOL)
        .unwrap_or_else(|| {
            let fn_ty = ctx.i32_type().fn_type(&[], false);
            module.add_function(SV_RT_URANDOM_SYMBOL, fn_ty, Some(Linkage::External))
        })
}

fn fetch_or_declare_sv_rt_urandom_range<'ctx>(
    ctx: &'ctx Context,
    module: &Module<'ctx>,
) -> inkwell::values::FunctionValue<'ctx> {
    module
        .get_function(SV_RT_URANDOM_RANGE_SYMBOL)
        .unwrap_or_else(|| {
            let i32_ty = ctx.i32_type();
            let fn_ty = i32_ty.fn_type(&[i32_ty.into(), i32_ty.into()], false);
            module.add_function(SV_RT_URANDOM_RANGE_SYMBOL, fn_ty, Some(Linkage::External))
        })
}

fn fetch_or_declare_sv_rt_random_reseed<'ctx>(
    ctx: &'ctx Context,
    module: &Module<'ctx>,
) -> inkwell::values::FunctionValue<'ctx> {
    module
        .get_function(SV_RT_RANDOM_RESEED_SYMBOL)
        .unwrap_or_else(|| {
            let i32_ty = ctx.i32_type();
            let ptr_ty = ctx.ptr_type(AddressSpace::default());
            let fn_ty = i32_ty.fn_type(&[ptr_ty.into()], false);
            module.add_function(SV_RT_RANDOM_RESEED_SYMBOL, fn_ty, Some(Linkage::External))
        })
}

fn fetch_or_declare_sv_rt_urandom_reseed<'ctx>(
    ctx: &'ctx Context,
    module: &Module<'ctx>,
) -> inkwell::values::FunctionValue<'ctx> {
    module
        .get_function(SV_RT_URANDOM_RESEED_SYMBOL)
        .unwrap_or_else(|| {
            let i32_ty = ctx.i32_type();
            let ptr_ty = ctx.ptr_type(AddressSpace::default());
            let fn_ty = i32_ty.fn_type(&[ptr_ty.into()], false);
            module.add_function(SV_RT_URANDOM_RESEED_SYMBOL, fn_ty, Some(Linkage::External))
        })
}

/// Adjusts `pv` to a plain (unknown-lane-dropped), exactly-32-bit `IntValue`
/// suitable as an `i32` runtime-call argument -- `$urandom_range`'s `hi`/`lo`
/// arguments are ordinary SV integral expressions of whatever declared width
/// the source uses (`int`, a narrower `bit [3:0]`, ...), and the frozen
/// `sv_rt_urandom_range` ABI fixes both at `i32` regardless. Never branches on
/// anything but `pv.width` (CG-044's own "branch on shape, not position/
/// content" discipline, restated for this call site).
/// B-runtime Task 7 (`process_lower.rs`'s own `$fclose`/`$fflush` fd
/// marshaling reuses this verbatim, the same "one truncate/extend-to-i32
/// helper, never a second divergent one" discipline `$urandom_range`'s own
/// call sites already establish here) -- widened from `fn` to `pub(crate)
/// fn`, no other change.
pub(crate) fn packed_to_plain_i32<'ctx>(
    op: &crate::ops::OpBuilder<'_, 'ctx>,
    pv: PackedValue<'ctx>,
) -> inkwell::values::IntValue<'ctx> {
    let at_width = match pv.width.cmp(&32) {
        std::cmp::Ordering::Less => op.extend4(pv, 32, pv.signed),
        std::cmp::Ordering::Greater => op.truncate4(pv, 32),
        std::cmp::Ordering::Equal => pv,
    };
    op.lane_val(at_width)
}

/// Review fix r2 (blocking finding, RT-078/079/080): resolves `$random`/
/// `$urandom`'s optional inout `seed` argument to its local storage slot --
/// the SAME "plain `NamedValue` lvalue, loud typed error on anything else"
/// scope restriction this crate already applies to assignment/increment-
/// decrement lvalues elsewhere (the `Assignment`/pre-post-increment arms,
/// below/above) -- a bit-select/concat/non-local seed argument is real,
/// unimplemented scope, never silently accepted or misreads garbage memory.
///
/// sv-lang wraps a plain variable passed as a system-call argument in one or
/// more `ExpressionKind::Conversion` nodes (its own call-site argument-type
/// view, e.g. `integer` -> the argument's self-determined type) even when
/// the variable itself is the real, unconverted lvalue underneath -- verified
/// directly (`$random(seed)` on an `integer seed;` lowers `seed_expr.kind()`
/// as `Conversion`, not `NamedValue`, until unwrapped). This function peels
/// through every such wrapper via `conversion_operand()` before checking for
/// `NamedValue`, deliberately WITHOUT consulting the conversion node's own
/// `to_ty`/`conversion_kind()` at all: this function only ever hands back a
/// pointer to the real variable's OWN storage, at the REAL variable's own
/// declared `(width, signed)` (from `cx.locals`, not from any conversion
/// node's type view) -- [`load_seed_as_plain_i32`]/[`store_seed_from_plain_i32`]
/// already do their own correct width/sign marshaling against that real
/// declared type independently, so the outer conversion's type view is
/// simply irrelevant to this operation, never silently trusted for
/// correctness.
fn resolve_seed_lvalue<'ctx, 'd, 'm>(
    cx: &LowerCtx<'ctx, 'd, 'm>,
    seed_expr: sv_lang::Expression<'d>,
    which_name: &str,
) -> Result<(PointerValue<'ctx>, u32, bool), LoweringError> {
    let mut seed_expr = seed_expr;
    // An inout seed passed to `$dist_*` arrives wrapped in an implicit
    // `Assignment` (the argument is written back); its left side is the
    // variable.
    if seed_expr.kind() == ExpressionKind::Assignment {
        seed_expr = seed_expr.left().ok_or_else(|| {
            LoweringError::unsupported_at(
                &format!("{which_name}'s seed argument assignment has no lvalue"),
                &cx.fn_sym,
            )
        })?;
    }
    while seed_expr.kind() == ExpressionKind::Conversion {
        seed_expr = seed_expr.conversion_operand().ok_or_else(|| {
            LoweringError::unsupported_at(
                &format!("{which_name}'s seed argument conversion missing operand"),
                &cx.fn_sym,
            )
        })?;
    }
    if !sv_sim_sim::symid::is_value_name(seed_expr.kind()) {
        return Err(LoweringError::unsupported_at(
            &format!(
                "{which_name}'s optional seed argument must be a plain variable (inout) lvalue, \
                 got {:?}",
                seed_expr.kind()
            ),
            &cx.fn_sym,
        ));
    }
    let sym = seed_expr.referenced_symbol().ok_or_else(|| {
        LoweringError::unsupported_at(
            &format!("{which_name}'s seed argument unresolved name"),
            &cx.fn_sym,
        )
    })?;
    cx.locals.get(&sym.uid()).copied().ok_or_else(|| {
        LoweringError::unsupported_at(
            &format!("{which_name}'s seed argument is not a local"),
            &sym,
        )
    })
}

/// Loads `seed`'s current stored value out of `slot` (`width`/`signed`/
/// `cx.repr` exactly as `cx.locals` recorded it for this variable) and
/// marshals it down to a plain, unknown-lane-dropped `i32` via
/// [`packed_to_plain_i32`] -- the SAME marshaling operation
/// `$urandom_range`'s `hi`/`lo` arguments already use, reused here rather
/// than re-derived, per this crate's own "one evaluator" discipline.
fn load_seed_as_plain_i32<'ctx>(
    cx: &LowerCtx<'ctx, '_, '_>,
    op: &OpBuilder<'_, 'ctx>,
    slot: PointerValue<'ctx>,
    width: u32,
    signed: bool,
) -> inkwell::values::IntValue<'ctx> {
    let ty = if cx.repr == ValueRepr::FourState {
        op.i_n(width * 2)
    } else {
        op.i_n(width)
    };
    let cur_llvm =
        cx.b.build_load(ty, slot, "seed_ld")
            .unwrap()
            .into_int_value();
    let cur_pv = PackedValue {
        llvm: cur_llvm,
        width,
        repr: cx.repr,
        signed,
    };
    packed_to_plain_i32(op, cur_pv)
}

/// The inverse of [`load_seed_as_plain_i32`]: resizes the runtime call's
/// plain `i32` updated-seed result back to `width` (`result_signed`: `true`
/// for `$random`'s signed convention, `false` for `$urandom`'s unsigned one
/// -- the same per-call convention the ordinary no-seed return-value packing
/// elsewhere in this function already uses) and stores it into `slot` in
/// `cx.repr`'s own packed representation, with an all-known (never X/Z)
/// unknown lane -- the PRNG's own output is always a fully-determined value,
/// never a source of new X/Z bits.
fn store_seed_from_plain_i32<'ctx>(
    cx: &LowerCtx<'ctx, '_, '_>,
    op: &OpBuilder<'_, 'ctx>,
    slot: PointerValue<'ctx>,
    width: u32,
    updated_i32: inkwell::values::IntValue<'ctx>,
    result_signed: bool,
) {
    // `updated_i32` is a plain integer of any width (32 for the PRNG seed
    // paths, 64 for `$value$plusargs`).
    let src_w = updated_i32.get_type().get_bit_width();
    let pv32 = op.pack(
        updated_i32,
        updated_i32.get_type().const_zero(),
        src_w,
        cx.repr,
        result_signed,
    );
    let resized = match width.cmp(&src_w) {
        std::cmp::Ordering::Less => op.truncate4(pv32, width),
        std::cmp::Ordering::Greater => op.extend4(pv32, width, result_signed),
        std::cmp::Ordering::Equal => pv32,
    };
    cx.b.build_store(slot, resized.llvm).unwrap();
    // A module variable's shadow copy is committed to STATE at once.
    write_through_store(cx, slot);
}

/// Where an inout PRNG seed argument lives: a local slot (a process or
/// function variable, or a module variable's shadow copy), or any other
/// lvalue (a class property, a struct field, a collection element ...) read
/// and written through the ordinary expression/store paths.
enum SeedPlace<'ctx, 'd> {
    Slot(PointerValue<'ctx>, u32, bool),
    Lvalue(sv_lang::Expression<'d>),
}

/// [`resolve_seed_lvalue`] extended to every lvalue (LRM 1800-2023 20.15.1:
/// the seed is an inout integral variable, e.g. a class property).
fn resolve_seed_place<'ctx, 'd, 'm>(
    cx: &LowerCtx<'ctx, 'd, 'm>,
    seed_expr: sv_lang::Expression<'d>,
    which_name: &str,
) -> Result<SeedPlace<'ctx, 'd>, LoweringError> {
    let mut peeled = seed_expr;
    if peeled.kind() == ExpressionKind::Assignment {
        peeled = peeled.left().ok_or_else(|| {
            LoweringError::unsupported_at(
                &format!("{which_name}'s seed argument assignment has no lvalue"),
                &cx.fn_sym,
            )
        })?;
    }
    while peeled.kind() == ExpressionKind::Conversion {
        peeled = peeled.conversion_operand().ok_or_else(|| {
            LoweringError::unsupported_at(
                &format!("{which_name}'s seed argument conversion missing operand"),
                &cx.fn_sym,
            )
        })?;
    }
    let is_local = sv_sim_sim::symid::is_value_name(peeled.kind())
        && peeled
            .referenced_symbol()
            .is_some_and(|sym| cx.locals.contains_key(&sym.uid()));
    if is_local {
        let (slot, width, signed) = resolve_seed_lvalue(cx, seed_expr, which_name)?;
        return Ok(SeedPlace::Slot(slot, width, signed));
    }
    if !peeled.expr_type().is_some_and(|t| t.is_integral()) {
        return Err(LoweringError::unsupported_at(
            &format!("{which_name}'s seed argument must be an integral variable"),
            &cx.fn_sym,
        ));
    }
    Ok(SeedPlace::Lvalue(peeled))
}

fn load_seed_place<'ctx, 'd>(
    cx: &LowerCtx<'ctx, 'd, '_>,
    op: &OpBuilder<'_, 'ctx>,
    place: &SeedPlace<'ctx, 'd>,
) -> Result<inkwell::values::IntValue<'ctx>, LoweringError> {
    match *place {
        SeedPlace::Slot(slot, width, signed) => {
            Ok(load_seed_as_plain_i32(cx, op, slot, width, signed))
        }
        SeedPlace::Lvalue(lv) => {
            let pv = lower_expr(cx, lv)?
                .into_packed("a PRNG seed argument must be an integral value", &cx.fn_sym)?;
            Ok(packed_to_plain_i32(op, pv))
        }
    }
}

fn store_seed_place<'ctx, 'd>(
    cx: &LowerCtx<'ctx, 'd, '_>,
    op: &OpBuilder<'_, 'ctx>,
    place: &SeedPlace<'ctx, 'd>,
    updated_i32: inkwell::values::IntValue<'ctx>,
    result_signed: bool,
) -> Result<(), LoweringError> {
    match *place {
        SeedPlace::Slot(slot, width, _) => {
            store_seed_from_plain_i32(cx, op, slot, width, updated_i32, result_signed);
        }
        SeedPlace::Lvalue(lv) => {
            let width = lv.expr_type().map_or(32, |t| t.value_width());
            let pv32 = op.pack(
                updated_i32,
                updated_i32.get_type().const_zero(),
                32,
                cx.repr,
                result_signed,
            );
            let resized = match width.cmp(&32) {
                std::cmp::Ordering::Less => op.truncate4(pv32, width),
                std::cmp::Ordering::Greater => op.extend4(pv32, width, result_signed),
                std::cmp::Ordering::Equal => pv32,
            };
            store_to(cx, lv, LoweredValue::Packed(resized))?;
        }
    }
    Ok(())
}

/// `$random`/`$urandom`/`$urandom_range` (LRM 1800-2023 §20.15.1): lowers to
/// a real runtime call against [`fetch_or_declare_sv_rt_random`]/
/// [`fetch_or_declare_sv_rt_urandom`]/[`fetch_or_declare_sv_rt_urandom_range`]
/// -- NEVER a compile-time fold (unlike CG-042's type queries; each call must
/// genuinely execute the runtime PRNG, or two `$display($urandom)` calls in
/// the same process would print the same folded value forever). RT-082's own
/// negative requirement means this function's numeric OUTPUT is never
/// compared against any other simulator anywhere in this crate's own tests --
/// only same-seed same-sv-sim-run reproducibility is (see
/// `sv_sim_runtime::random`'s own module doc, and
/// `crates/sv-sim-harness/tests/random_e2e.rs`).
///
/// Review fix r2 (blocking finding): `$random`/`$urandom` also accept an
/// LRM-legal optional inout `seed` argument (IEEE 1800-2023 §20.15.1) that
/// was previously silently discarded regardless of whether the source passed
/// one -- both arms below now inspect `e.children()` and, when a seed
/// argument is present, lower a genuinely different call against
/// [`fetch_or_declare_sv_rt_random_reseed`]/
/// [`fetch_or_declare_sv_rt_urandom_reseed`] instead, via
/// [`resolve_seed_lvalue`]/[`load_seed_as_plain_i32`]/
/// [`store_seed_from_plain_i32`]. `$urandom_range` has no such argument in
/// the LRM and is unaffected.
fn lower_random_system_call<'ctx, 'd, 'm>(
    cx: &LowerCtx<'ctx, 'd, 'm>,
    which: KnownSystemName,
    e: sv_lang::Expression<'d>,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    let op = OpBuilder {
        ctx: cx.ctx,
        b: &cx.b,
    };
    match which {
        KnownSystemName::Random => {
            if let Some(seed_node) = e.children().first() {
                let seed_expr = seed_node.as_expression().ok_or_else(|| {
                    LoweringError::unsupported_at(
                        "$random's seed argument is not an expression",
                        &cx.fn_sym,
                    )
                })?;
                let place = resolve_seed_place(cx, seed_expr, "$random")?;
                let seed_i32 = load_seed_place(cx, &op, &place)?;
                let scratch =
                    crate::types::entry_alloca_in(&cx.b, cx.ctx.i32_type(), "random_seed_scratch");
                cx.b.build_store(scratch, seed_i32).unwrap();
                let rt_fn = fetch_or_declare_sv_rt_random_reseed(cx.ctx, cx.module);
                let raw =
                    cx.b.build_call(rt_fn, &[scratch.into()], "rt_random_reseed_call")
                        .unwrap()
                        .try_as_basic_value()
                        .unwrap_basic()
                        .into_int_value();
                let updated_i32 =
                    cx.b.build_load(cx.ctx.i32_type(), scratch, "random_seed_after")
                        .unwrap()
                        .into_int_value();
                store_seed_place(cx, &op, &place, updated_i32, true)?;
                return Ok(LoweredValue::Packed(op.pack(
                    raw,
                    cx.ctx.i32_type().const_zero(),
                    32,
                    cx.repr,
                    true,
                )));
            }
            let rt_fn = fetch_or_declare_sv_rt_random(cx.ctx, cx.module);
            let raw =
                cx.b.build_call(rt_fn, &[], "rt_random_call")
                    .unwrap()
                    .try_as_basic_value()
                    .unwrap_basic()
                    .into_int_value();
            Ok(LoweredValue::Packed(op.pack(
                raw,
                cx.ctx.i32_type().const_zero(),
                32,
                cx.repr,
                true,
            )))
        }
        KnownSystemName::URandom => {
            if let Some(seed_node) = e.children().first() {
                let seed_expr = seed_node.as_expression().ok_or_else(|| {
                    LoweringError::unsupported_at(
                        "$urandom's seed argument is not an expression",
                        &cx.fn_sym,
                    )
                })?;
                // LRM 18.13.1: `$urandom (int seed)` takes an ordinary integral
                // expression (`$urandom(254)`); only a plain variable is also
                // written back with the advanced seed (the existing inout view).
                let mut peeled = seed_expr;
                while peeled.kind() == ExpressionKind::Conversion {
                    match peeled.conversion_operand() {
                        Some(o) => peeled = o,
                        None => break,
                    }
                }
                let seed_var = if sv_sim_sim::symid::is_value_name(peeled.kind())
                    && peeled
                        .referenced_symbol()
                        .is_some_and(|sym| cx.locals.contains_key(&sym.uid()))
                {
                    Some(resolve_seed_lvalue(cx, seed_expr, "$urandom")?)
                } else {
                    None
                };
                let seed_i32 = match seed_var {
                    Some((slot, width, signed)) => {
                        load_seed_as_plain_i32(cx, &op, slot, width, signed)
                    }
                    None => match lower_expr(cx, seed_expr)? {
                        LoweredValue::Packed(pv) => packed_to_plain_i32(&op, pv),
                        _ => {
                            return Err(LoweringError::unsupported_at(
                                "$urandom's seed expression wider than a packed value",
                                &cx.fn_sym,
                            ));
                        }
                    },
                };
                let scratch =
                    crate::types::entry_alloca_in(&cx.b, cx.ctx.i32_type(), "urandom_seed_scratch");
                cx.b.build_store(scratch, seed_i32).unwrap();
                let rt_fn = fetch_or_declare_sv_rt_urandom_reseed(cx.ctx, cx.module);
                let raw =
                    cx.b.build_call(rt_fn, &[scratch.into()], "rt_urandom_reseed_call")
                        .unwrap()
                        .try_as_basic_value()
                        .unwrap_basic()
                        .into_int_value();
                let updated_i32 =
                    cx.b.build_load(cx.ctx.i32_type(), scratch, "urandom_seed_after")
                        .unwrap()
                        .into_int_value();
                if let Some((slot, width, _)) = seed_var {
                    store_seed_from_plain_i32(cx, &op, slot, width, updated_i32, false);
                }
                return Ok(LoweredValue::Packed(op.pack(
                    raw,
                    cx.ctx.i32_type().const_zero(),
                    32,
                    cx.repr,
                    false,
                )));
            }
            let rt_fn = fetch_or_declare_sv_rt_urandom(cx.ctx, cx.module);
            let raw =
                cx.b.build_call(rt_fn, &[], "rt_urandom_call")
                    .unwrap()
                    .try_as_basic_value()
                    .unwrap_basic()
                    .into_int_value();
            Ok(LoweredValue::Packed(op.pack(
                raw,
                cx.ctx.i32_type().const_zero(),
                32,
                cx.repr,
                false,
            )))
        }
        KnownSystemName::URandomRange => {
            let children = e.children();
            let (hi_node, rest) = children.split_first().ok_or_else(|| {
                LoweringError::unsupported_at(
                    "$urandom_range call has no arguments (needs at least `hi`)",
                    &cx.fn_sym,
                )
            })?;
            let hi_expr = hi_node.as_expression().ok_or_else(|| {
                LoweringError::unsupported_at(
                    "$urandom_range's `hi` argument is not an expression",
                    &cx.fn_sym,
                )
            })?;
            let hi_pv = lower_expr(cx, hi_expr)?.into_packed(
                "$urandom_range's `hi` argument must be an integral value",
                &cx.fn_sym,
            )?;
            let hi_i32 = packed_to_plain_i32(&op, hi_pv);
            let lo_i32 = if let Some(lo_node) = rest.first() {
                let lo_expr = lo_node.as_expression().ok_or_else(|| {
                    LoweringError::unsupported_at(
                        "$urandom_range's `lo` argument is not an expression",
                        &cx.fn_sym,
                    )
                })?;
                let lo_pv = lower_expr(cx, lo_expr)?.into_packed(
                    "$urandom_range's `lo` argument must be an integral value",
                    &cx.fn_sym,
                )?;
                packed_to_plain_i32(&op, lo_pv)
            } else {
                // LRM: `lo` defaults to 0 when omitted.
                cx.ctx.i32_type().const_zero()
            };
            let rt_fn = fetch_or_declare_sv_rt_urandom_range(cx.ctx, cx.module);
            let raw =
                cx.b.build_call(
                    rt_fn,
                    &[hi_i32.into(), lo_i32.into()],
                    "rt_urandom_range_call",
                )
                .unwrap()
                .try_as_basic_value()
                .unwrap_basic()
                .into_int_value();
            Ok(LoweredValue::Packed(op.pack(
                raw,
                cx.ctx.i32_type().const_zero(),
                32,
                cx.repr,
                false,
            )))
        }
        other => unreachable!(
            "lower_random_system_call is only ever invoked (by lower_expr's Call arm, above) for \
             KnownSystemName::{{Random,URandom,URandomRange}} -- got {other:?}"
        ),
    }
}

/// The frozen ABI symbol for every `$dist_*` function -- one entry point
/// selected by `kind` (the `sv_sim_runtime::random::DIST_*` constants):
/// `extern "C" fn(kind: i32, seed: *mut i32, a: i32, b: i32) -> i32`.
pub const SV_RT_DIST_SYMBOL: &str = "sv_rt_dist";

/// Maps a `$dist_*` system function name to `(kind, parameter count)`.
fn dist_kind(name: &str) -> Option<(i32, usize)> {
    use sv_sim_runtime::random as r;
    Some(match name.trim_start_matches('$') {
        "dist_uniform" => (r::DIST_UNIFORM, 2),
        "dist_normal" => (r::DIST_NORMAL, 2),
        "dist_exponential" => (r::DIST_EXPONENTIAL, 1),
        "dist_poisson" => (r::DIST_POISSON, 1),
        "dist_chi_square" => (r::DIST_CHI_SQUARE, 1),
        "dist_t" => (r::DIST_T, 1),
        "dist_erlang" => (r::DIST_ERLANG, 2),
        _ => return None,
    })
}

/// `$dist_uniform/normal/exponential/poisson/chi_square/t/erlang(seed, ...)`
/// (LRM 1800-2023 20.15.2): the inout `seed` goes through the same
/// load / scratch / store-back marshaling as `$random(seed)`; the result is
/// a signed 32-bit `integer`. Never compile-time folded.
fn lower_dist_system_call<'ctx, 'd, 'm>(
    cx: &LowerCtx<'ctx, 'd, 'm>,
    (kind, nparams): (i32, usize),
    name: &str,
    e: sv_lang::Expression<'d>,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    let op = OpBuilder {
        ctx: cx.ctx,
        b: &cx.b,
    };
    let children = e.children();
    if children.len() != nparams + 1 {
        return Err(LoweringError::unsupported_at(
            &format!("{name} takes a seed and {nparams} parameter(s)"),
            &cx.fn_sym,
        ));
    }
    let i32_ty = cx.ctx.i32_type();
    let mut params = Vec::new();
    for node in &children[1..] {
        let ex = node.as_expression().ok_or_else(|| {
            LoweringError::unsupported_at(
                &format!("{name}'s argument is not an expression"),
                &cx.fn_sym,
            )
        })?;
        let pv = lower_expr(cx, ex)?.into_packed(
            &format!("{name}'s arguments must be integral values"),
            &cx.fn_sym,
        )?;
        params.push(packed_to_plain_i32(&op, pv));
    }
    params.resize(2, i32_ty.const_zero());
    let seed_expr = children[0].as_expression().ok_or_else(|| {
        LoweringError::unsupported_at(
            &format!("{name}'s seed argument is not an expression"),
            &cx.fn_sym,
        )
    })?;
    let place = resolve_seed_place(cx, seed_expr, name)?;
    let seed_i32 = load_seed_place(cx, &op, &place)?;
    let scratch = crate::types::entry_alloca_in(&cx.b, i32_ty, "dist_seed_scratch");
    cx.b.build_store(scratch, seed_i32).unwrap();
    let rt_fn = fetch_or_declare_sv_rt_dist(cx.ctx, cx.module);
    let raw =
        cx.b.build_call(
            rt_fn,
            &[
                i32_ty.const_int(kind as u64, false).into(),
                scratch.into(),
                params[0].into(),
                params[1].into(),
            ],
            "rt_dist_call",
        )
        .unwrap()
        .try_as_basic_value()
        .unwrap_basic()
        .into_int_value();
    let updated =
        cx.b.build_load(i32_ty, scratch, "dist_seed_after")
            .unwrap()
            .into_int_value();
    store_seed_place(cx, &op, &place, updated, true)?;
    Ok(LoweredValue::Packed(op.pack(
        raw,
        i32_ty.const_zero(),
        32,
        cx.repr,
        true,
    )))
}

fn fetch_or_declare_sv_rt_dist<'ctx>(
    ctx: &'ctx Context,
    module: &Module<'ctx>,
) -> inkwell::values::FunctionValue<'ctx> {
    module.get_function(SV_RT_DIST_SYMBOL).unwrap_or_else(|| {
        let i32_ty = ctx.i32_type();
        let ptr_ty = ctx.ptr_type(AddressSpace::default());
        let fn_ty = i32_ty.fn_type(
            &[i32_ty.into(), ptr_ty.into(), i32_ty.into(), i32_ty.into()],
            false,
        );
        module.add_function(SV_RT_DIST_SYMBOL, fn_ty, Some(Linkage::External))
    })
}

// --- B-runtime Task 7 (RT-050/RT-051): `$fopen(path, mode)` -------------

/// Fetches (or, on first use in this `Module`, declares) the `extern "C"`
/// shim function for `$fopen` -- `int32_t sv_rt_fopen(const char *path,
/// const char *mode)`, matching `sv_sim_runtime::file_io::sv_rt_fopen`'s real
/// production symbol exactly. Mirrors [`fetch_or_declare_sv_rt_random`]'s
/// identical fetch-or-declare discipline just above.
fn fetch_or_declare_sv_rt_fopen<'ctx>(
    ctx: &'ctx Context,
    module: &Module<'ctx>,
) -> inkwell::values::FunctionValue<'ctx> {
    module.get_function(SV_RT_FOPEN_SYMBOL).unwrap_or_else(|| {
        let ptr_ty = ctx.ptr_type(AddressSpace::default());
        let i32_ty = ctx.i32_type();
        let fn_ty = i32_ty.fn_type(&[ptr_ty.into(), ptr_ty.into()], false);
        module.add_function(SV_RT_FOPEN_SYMBOL, fn_ty, Some(Linkage::External))
    })
}

/// Fetch-or-declare `int32_t sv_rt_fopen_mcd(const char *path)`, the
/// single-argument (multichannel descriptor) form of `$fopen`.
fn fetch_or_declare_sv_rt_fopen_mcd<'ctx>(
    ctx: &'ctx Context,
    module: &Module<'ctx>,
) -> inkwell::values::FunctionValue<'ctx> {
    module
        .get_function(SV_RT_FOPEN_MCD_SYMBOL)
        .unwrap_or_else(|| {
            let ptr_ty = ctx.ptr_type(AddressSpace::default());
            let i32_ty = ctx.i32_type();
            let fn_ty = i32_ty.fn_type(&[ptr_ty.into()], false);
            module.add_function(SV_RT_FOPEN_MCD_SYMBOL, fn_ty, Some(Linkage::External))
        })
}

/// `$fopen(path, mode)` (RT-050/RT-051): both arguments must be
/// string-literal constants in this crate's own scope -- forwarded through
/// `crate::systask`'s already-tested [`crate::systask::lower_format_expr`]
/// (never a second, divergent string-literal-lowering implementation for
/// the same purpose; that function already implements exactly the needed
/// "constant string-literal argument, named loud error otherwise" contract,
/// CG-046's own literal-only scope note applied here identically -- a
/// dynamic, non-literal `$fopen` path/mode is out of this task's scope, the
/// same restriction `$dumpfile`'s own path argument already has).
///
/// LRM 21.3.1 has two forms with two different return kinds: `$fopen(path,
/// mode)` returns an fd (bit 31 set) and `$fopen(path)` returns a
/// multichannel descriptor (one bit per open file, opened for writing). They
/// call two distinct runtime symbols so each keeps its own table; any other
/// arity is a named, loud `LoweringError`.
fn lower_fopen_call<'ctx, 'd, 'm>(
    cx: &LowerCtx<'ctx, 'd, 'm>,
    e: sv_lang::Expression<'d>,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    let operands = e.operands();
    let call = match operands.len() {
        1 => {
            let path_ptr = crate::systask::lower_format_expr(cx, operands[0])?;
            let rt_fn = fetch_or_declare_sv_rt_fopen_mcd(cx.ctx, cx.module);
            cx.b.build_call(rt_fn, &[path_ptr.into()], "rt_fopen_mcd_call")
                .unwrap()
        }
        2 => {
            let path_ptr = crate::systask::lower_format_expr(cx, operands[0])?;
            let mode_ptr = crate::systask::lower_format_expr(cx, operands[1])?;
            let rt_fn = fetch_or_declare_sv_rt_fopen(cx.ctx, cx.module);
            cx.b.build_call(rt_fn, &[path_ptr.into(), mode_ptr.into()], "rt_fopen_call")
                .unwrap()
        }
        n => {
            return Err(LoweringError::unsupported_at(
                &format!(
                    "$fopen takes (path) for a multichannel descriptor or (path, mode) for a file \
                     descriptor (LRM 21.3.1), got {n} arguments"
                ),
                &cx.fn_sym,
            ));
        }
    };
    let raw = call.try_as_basic_value().unwrap_basic().into_int_value();
    let op = OpBuilder {
        ctx: cx.ctx,
        b: &cx.b,
    };
    Ok(LoweredValue::Packed(op.pack(
        raw,
        cx.ctx.i32_type().const_zero(),
        32,
        cx.repr,
        true,
    )))
}

/// `$system(["cmd"])` (LRM 20.17): runs the command through the host shell and
/// yields the C `system()` result as an `int`. The command must be a string
/// literal (like `$fopen`'s path); `$system()` with no argument is
/// `system(NULL)`, i.e. "is a shell available".
fn lower_system_call<'ctx, 'd, 'm>(
    cx: &LowerCtx<'ctx, 'd, 'm>,
    e: sv_lang::Expression<'d>,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    let operands = e.operands();
    let ptr_ty = cx.ctx.ptr_type(AddressSpace::default());
    let cmd_ptr = match operands.len() {
        0 => ptr_ty.const_null(),
        1 => crate::systask::lower_format_expr(cx, operands[0])?,
        n => {
            return Err(LoweringError::unsupported_at(
                &format!("$system takes at most one argument (LRM 20.17), got {n}"),
                &cx.fn_sym,
            ));
        }
    };
    let rt_fn = cx
        .module
        .get_function(SV_RT_SYSTEM_SYMBOL)
        .unwrap_or_else(|| {
            let fn_ty = cx.ctx.i32_type().fn_type(&[ptr_ty.into()], false);
            cx.module
                .add_function(SV_RT_SYSTEM_SYMBOL, fn_ty, Some(Linkage::External))
        });
    let call =
        cx.b.build_call(rt_fn, &[cmd_ptr.into()], "rt_system_call")
            .unwrap();
    let raw = call.try_as_basic_value().unwrap_basic().into_int_value();
    let op = OpBuilder {
        ctx: cx.ctx,
        b: &cx.b,
    };
    Ok(LoweredValue::Packed(op.pack(
        raw,
        cx.ctx.i32_type().const_zero(),
        32,
        cx.repr,
        true,
    )))
}

// --- B-runtime Task 9 (RT-061/RT-063): `$fseek`/`$feof` ------------------
//
// **Scope note (documented, not silent):** these two are the ONLY read-side
// system calls this task wires all the way through codegen/the real JIT
// pipeline -- see `sv_sim_runtime::file_io::read`'s own module doc for why
// `$fgets`/`$fscanf`/`$sscanf`/`$ferror`/`$fread`/`$rewind` are NOT (each
// needs either a raw output-buffer pointer marshaled from an actual SV
// variable, a capability that does not exist anywhere in this pipeline
// today, or -- `$rewind` alone -- reconciling a genuinely void ABI with
// `sv-lang`'s own function-call grammar for no functional gain over
// `$fseek(fd, 0, 0)`, already covered by the `$fseek` path below).
// `$fseek(fd, offset, operation)` and `$feof(fd)` take/return only plain
// scalars, exactly like `$fopen(path, mode)` above, so they follow that same
// "system FUNCTION, dispatched from `expr.rs`'s `lower_expr`" pattern
// mechanically.

/// Fetches (or declares) `int32_t sv_rt_fseek(int32_t fd, int64_t offset,
/// int32_t whence)`, matching `sv_sim_runtime::file_io::read::sv_rt_fseek`'s
/// real production symbol exactly.
fn fetch_or_declare_sv_rt_fseek<'ctx>(
    ctx: &'ctx Context,
    module: &Module<'ctx>,
) -> inkwell::values::FunctionValue<'ctx> {
    module.get_function(SV_RT_FSEEK_SYMBOL).unwrap_or_else(|| {
        let i32_ty = ctx.i32_type();
        let i64_ty = ctx.i64_type();
        let fn_ty = i32_ty.fn_type(&[i32_ty.into(), i64_ty.into(), i32_ty.into()], false);
        module.add_function(SV_RT_FSEEK_SYMBOL, fn_ty, Some(Linkage::External))
    })
}

/// `$fseek(fd, offset, operation)` (RT-061): all three arguments are
/// ordinary dynamic integral expressions (never required to be compile-time
/// constants, unlike `$fopen`'s string-literal-only path/mode) -- LRM's own
/// `offset`/`operation` are plain `integer` (32-bit), so both lower through
/// the same `packed_to_plain_i32` helper `$fclose`/`$fflush`'s fd argument
/// already uses (`process_lower.rs::lower_file_io_void_call`); `offset` is
/// then sign-extended to the frozen ABI's own wider `i64` (this crate's own
/// choice, RT-061's own doc note: files larger than 2^31 bytes are a real,
/// if unusual, case the narrower LRM type would otherwise truncate).
fn lower_fseek_call<'ctx, 'd, 'm>(
    cx: &LowerCtx<'ctx, 'd, 'm>,
    e: sv_lang::Expression<'d>,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    let operands = e.operands();
    if operands.len() != 3 {
        return Err(LoweringError::unsupported_at(
            "$fseek requires exactly three arguments (fd, offset, operation)",
            &cx.fn_sym,
        ));
    }
    let op = OpBuilder {
        ctx: cx.ctx,
        b: &cx.b,
    };
    let fd_pv = lower_expr(cx, operands[0])?
        .into_packed("$fseek's fd argument must be an integral value", &cx.fn_sym)?;
    let fd_i32 = packed_to_plain_i32(&op, fd_pv);
    let offset_pv = lower_expr(cx, operands[1])?.into_packed(
        "$fseek's offset argument must be an integral value",
        &cx.fn_sym,
    )?;
    let offset_i32 = packed_to_plain_i32(&op, offset_pv);
    let offset_i64 =
        cx.b.build_int_s_extend(offset_i32, cx.ctx.i64_type(), "fseek_offset_sext")
            .unwrap();
    let whence_pv = lower_expr(cx, operands[2])?.into_packed(
        "$fseek's operation argument must be an integral value",
        &cx.fn_sym,
    )?;
    let whence_i32 = packed_to_plain_i32(&op, whence_pv);

    let rt_fn = fetch_or_declare_sv_rt_fseek(cx.ctx, cx.module);
    let raw =
        cx.b.build_call(
            rt_fn,
            &[fd_i32.into(), offset_i64.into(), whence_i32.into()],
            "rt_fseek_call",
        )
        .unwrap()
        .try_as_basic_value()
        .unwrap_basic()
        .into_int_value();
    Ok(LoweredValue::Packed(op.pack(
        raw,
        cx.ctx.i32_type().const_zero(),
        32,
        cx.repr,
        true,
    )))
}

/// Fetches (or declares) `int8_t sv_rt_feof(int32_t fd)` -- `int8_t`, not
/// `i1`, matching `bool`'s real Rust `extern "C"` ABI representation exactly
/// (`process_lower.rs::compile_iff_guard`'s own doc comment already
/// establishes this convention for `PredicateFn`'s identical `-> bool`
/// shape: "`bool`'s Rust `extern "C"` ABI representation is `i8`").
fn fetch_or_declare_sv_rt_feof<'ctx>(
    ctx: &'ctx Context,
    module: &Module<'ctx>,
) -> inkwell::values::FunctionValue<'ctx> {
    module.get_function(SV_RT_FEOF_SYMBOL).unwrap_or_else(|| {
        let i32_ty = ctx.i32_type();
        let i8_ty = ctx.i8_type();
        let fn_ty = i8_ty.fn_type(&[i32_ty.into()], false);
        module.add_function(SV_RT_FEOF_SYMBOL, fn_ty, Some(Linkage::External))
    })
}

/// `$feof(fd)` (RT-063): a single dynamic fd argument, result widened from
/// `sv_rt_feof`'s `i8` (0/1) return into a plain 32-bit unsigned `RtArg`-free
/// packed value (this crate's own established "system call returning an
/// LRM `integer`" convention, `$fopen`/`$fseek` above).
fn lower_feof_call<'ctx, 'd, 'm>(
    cx: &LowerCtx<'ctx, 'd, 'm>,
    e: sv_lang::Expression<'d>,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    let operands = e.operands();
    if operands.len() != 1 {
        return Err(LoweringError::unsupported_at(
            "$feof requires exactly one argument (fd)",
            &cx.fn_sym,
        ));
    }
    let op = OpBuilder {
        ctx: cx.ctx,
        b: &cx.b,
    };
    let fd_pv = lower_expr(cx, operands[0])?
        .into_packed("$feof's fd argument must be an integral value", &cx.fn_sym)?;
    let fd_i32 = packed_to_plain_i32(&op, fd_pv);

    let rt_fn = fetch_or_declare_sv_rt_feof(cx.ctx, cx.module);
    let raw8 =
        cx.b.build_call(rt_fn, &[fd_i32.into()], "rt_feof_call")
            .unwrap()
            .try_as_basic_value()
            .unwrap_basic()
            .into_int_value();
    let raw32 =
        cx.b.build_int_z_extend(raw8, cx.ctx.i32_type(), "feof_zext")
            .unwrap();
    Ok(LoweredValue::Packed(op.pack(
        raw32,
        cx.ctx.i32_type().const_zero(),
        32,
        cx.repr,
        false,
    )))
}

// --- B-runtime Task 11 (RT-074/RT-075/RT-076/RT-077): `$test$plusargs`/
// `$value$plusargs` -----------------------------------------------------

/// Fetches (or, on first use in this `Module`, declares) the `extern "C"`
/// shim function for `$test$plusargs` -- `bool sv_rt_plusargs_test(const
/// char *name)`, matching `sv_sim_runtime::plusargs::sv_rt_plusargs_test`'s
/// real production symbol exactly (`bool`'s Rust `extern "C"` ABI
/// representation is `i8`, the same convention `lower_feof_call`'s own doc
/// comment already establishes for this crate).
fn fetch_or_declare_sv_rt_plusargs_test<'ctx>(
    ctx: &'ctx Context,
    module: &Module<'ctx>,
) -> inkwell::values::FunctionValue<'ctx> {
    module
        .get_function(SV_RT_PLUSARGS_TEST_SYMBOL)
        .unwrap_or_else(|| {
            let ptr_ty = ctx.ptr_type(AddressSpace::default());
            let i8_ty = ctx.i8_type();
            let fn_ty = i8_ty.fn_type(&[ptr_ty.into()], false);
            module.add_function(SV_RT_PLUSARGS_TEST_SYMBOL, fn_ty, Some(Linkage::External))
        })
}

/// `$test$plusargs(name)` (RT-074/RT-075, LRM 21.6): `name` is a string
/// literal or a run-time string value (`string`, or a `reg` holding
/// characters); the call always
/// reaches the real runtime (never folded), returning a plain LRM `integer`
/// (0/1) widened from `sv_rt_plusargs_test`'s `i8` result.
fn lower_test_plusargs_call<'ctx, 'd, 'm>(
    cx: &LowerCtx<'ctx, 'd, 'm>,
    e: sv_lang::Expression<'d>,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    let operands = e.operands();
    if operands.len() != 1 {
        return Err(LoweringError::unsupported_at(
            "$test$plusargs requires exactly one argument (name)",
            &cx.fn_sym,
        ));
    }
    let name_ptr = crate::systask::lower_format_expr(cx, operands[0])?;

    let rt_fn = fetch_or_declare_sv_rt_plusargs_test(cx.ctx, cx.module);
    let raw8 =
        cx.b.build_call(rt_fn, &[name_ptr.into()], "rt_plusargs_test_call")
            .unwrap()
            .try_as_basic_value()
            .unwrap_basic()
            .into_int_value();
    let raw32 =
        cx.b.build_int_z_extend(raw8, cx.ctx.i32_type(), "plusargs_test_zext")
            .unwrap();
    let op = OpBuilder {
        ctx: cx.ctx,
        b: &cx.b,
    };
    Ok(LoweredValue::Packed(op.pack(
        raw32,
        cx.ctx.i32_type().const_zero(),
        32,
        cx.repr,
        false,
    )))
}

/// Fetches (or declares) `bool sv_rt_plusargs_value(const char *fmt,
/// RtArg *out)`, matching `sv_sim_runtime::plusargs::sv_rt_plusargs_value`'s
/// real production symbol exactly. `out` is a plain pointer to one
/// `crate::systask::rtarg_llvm_type` element -- the exact same frozen
/// `RtArg` LLVM struct shape `$display`-family marshaling already builds
/// (`crate::systask`'s own doc comment: "size/alignment must match RtArg's
/// own exactly"), reused here as a scalar out-parameter rather than an
/// array element.
fn fetch_or_declare_sv_rt_plusargs_value<'ctx>(
    ctx: &'ctx Context,
    module: &Module<'ctx>,
) -> inkwell::values::FunctionValue<'ctx> {
    module
        .get_function(SV_RT_PLUSARGS_VALUE_SYMBOL)
        .unwrap_or_else(|| {
            let ptr_ty = ctx.ptr_type(AddressSpace::default());
            let i8_ty = ctx.i8_type();
            let fn_ty = i8_ty.fn_type(&[ptr_ty.into(), ptr_ty.into()], false);
            module.add_function(SV_RT_PLUSARGS_VALUE_SYMBOL, fn_ty, Some(Linkage::External))
        })
}

/// The output variable of `$value$plusargs`'s second argument: sv-lang wraps
/// an OUTPUT-direction system-function argument in an implicit `Assignment`
/// node (`var = <formal>`), and an implicit `Conversion` may sit over the
/// variable; peel both.
fn value_plusargs_target<'d>(
    cx: &LowerCtx<'_, 'd, '_>,
    var_expr: sv_lang::Expression<'d>,
) -> Result<sv_lang::Expression<'d>, LoweringError> {
    let mut var_expr = if var_expr.kind() == ExpressionKind::Assignment {
        var_expr.left().ok_or_else(|| {
            LoweringError::unsupported_at(
                "$value$plusargs's variable argument's implicit assignment has no lvalue",
                &cx.fn_sym,
            )
        })?
    } else {
        var_expr
    };
    while var_expr.kind() == ExpressionKind::Conversion {
        var_expr = var_expr.conversion_operand().ok_or_else(|| {
            LoweringError::unsupported_at(
                "$value$plusargs's variable argument conversion missing operand",
                &cx.fn_sym,
            )
        })?;
    }
    Ok(var_expr)
}

/// `$q_initialize`, `$q_add`, `$q_remove`, `$q_full`, `$q_exam` (LRM 1800-2023
/// 20.15): one call of the runtime's multiplexed `sv_rt_stoch_q`
/// (`sv_sim_runtime::stoch_queue`). The inputs travel as (value, unknown)
/// i64 pairs and the output variables are stored from the runtime's result
/// words (an output that is not integral or is wider than 64 bits is refused
/// by name). `$q_full` returns the runtime's answer as its `integer` result;
/// the tasks yield a discarded zero.
fn lower_stoch_queue_call<'ctx, 'd, 'm>(
    cx: &LowerCtx<'ctx, 'd, 'm>,
    e: sv_lang::Expression<'d>,
    which: KnownSystemName,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    use sv_sim_runtime::stoch_queue::{ARG_WORDS, OUT_WORDS, SV_RT_STOCH_Q_SYMBOL, StochOp};
    let name = match which {
        KnownSystemName::QInitialize => "$q_initialize",
        KnownSystemName::QAdd => "$q_add",
        KnownSystemName::QRemove => "$q_remove",
        KnownSystemName::QFull => "$q_full",
        _ => "$q_exam",
    };
    // (op, arity, input operand indices, outputs as (operand, value word,
    // unknown word)).
    type Outs = &'static [(usize, usize, Option<usize>)];
    let (op, arity, inputs, outputs): (StochOp, usize, &[usize], Outs) = match which {
        KnownSystemName::QInitialize => (StochOp::Initialize, 4, &[0, 1, 2], &[(3, 0, None)]),
        KnownSystemName::QAdd => (StochOp::Add, 4, &[0, 1, 2], &[(3, 0, None)]),
        KnownSystemName::QRemove => (
            StochOp::Remove,
            4,
            &[0],
            &[(1, 1, Some(2)), (2, 3, Some(4)), (3, 0, None)],
        ),
        KnownSystemName::QFull => (StochOp::Full, 2, &[0], &[(1, 0, None)]),
        _ => (StochOp::Exam, 4, &[0, 1], &[(2, 1, Some(2)), (3, 0, None)]),
    };
    let operands = e.operands();
    if operands.len() != arity {
        return Err(LoweringError::unsupported_at(
            &format!("{name} with {} arguments (expects {arity})", operands.len()),
            &cx.fn_sym,
        ));
    }
    let i64t = cx.ctx.i64_type();
    let i32t = cx.ctx.i32_type();
    let opb = OpBuilder {
        ctx: cx.ctx,
        b: &cx.b,
    };
    let to64 = |v: IntValue<'ctx>, width: u32| -> IntValue<'ctx> {
        if width < 64 {
            cx.b.build_int_z_extend(v, i64t, "stoch_zext").unwrap()
        } else if width > 64 {
            cx.b.build_int_truncate(v, i64t, "stoch_trunc").unwrap()
        } else {
            v
        }
    };
    let args =
        crate::types::entry_alloca_in(&cx.b, i64t.array_type(ARG_WORDS as u32), "stoch_args");
    let outs = crate::types::entry_alloca_in(&cx.b, i64t.array_type(OUT_WORDS as u32), "stoch_out");
    let slot = |base: PointerValue<'ctx>, i: usize| -> PointerValue<'ctx> {
        // SAFETY: `i` is below the array length of both allocas.
        unsafe {
            cx.b.build_in_bounds_gep(i64t, base, &[i64t.const_int(i as u64, false)], "stoch_slot")
                .unwrap()
        }
    };
    let mut words = [i64t.const_zero(); ARG_WORDS];
    for (pair, &idx) in inputs.iter().enumerate() {
        let v = lower_expr(cx, operands[idx])?
            .into_packed(&format!("{name}'s argument {}", idx + 1), &cx.fn_sym)?;
        words[pair * 2] = to64(opb.lane_val(v), v.width);
        words[pair * 2 + 1] = to64(opb.lane_unk(v), v.width);
    }
    let scope = e.system_call_scope().unwrap_or(cx.fn_sym);
    let tick = crate::timescale::tick_exp(cx.ctx, cx.module, &scope);
    words[6] = i64t.const_int(crate::timescale::ticks_per_unit(&scope, tick), false);
    if op == StochOp::Exam {
        let ty = value_plusargs_target(cx, operands[2])?
            .expr_type()
            .ok_or_else(|| {
                LoweringError::unsupported_at(
                    &format!("{name}'s value variable has no type"),
                    &cx.fn_sym,
                )
            })?;
        words[7] = i64t.const_int(u64::from(ty.value_width()), false);
        words[8] = i64t.const_int(u64::from(ty.is_signed()), false);
    }
    for (i, w) in words.iter().enumerate() {
        cx.b.build_store(slot(args, i), *w).unwrap();
    }
    let ptr_ty = cx.ctx.ptr_type(AddressSpace::default());
    let rt_fn = cx
        .module
        .get_function(SV_RT_STOCH_Q_SYMBOL)
        .unwrap_or_else(|| {
            cx.module.add_function(
                SV_RT_STOCH_Q_SYMBOL,
                cx.ctx
                    .void_type()
                    .fn_type(&[i32t.into(), ptr_ty.into(), ptr_ty.into()], false),
                Some(inkwell::module::Linkage::External),
            )
        });
    cx.b.build_call(
        rt_fn,
        &[
            i32t.const_int(op as u32 as u64, false).into(),
            args.into(),
            outs.into(),
        ],
        "rt_stoch_q_call",
    )
    .unwrap();
    let load = |i: usize| -> IntValue<'ctx> {
        cx.b.build_load(i64t, slot(outs, i), "stoch_out_word")
            .unwrap()
            .into_int_value()
    };
    let from64 = |w: IntValue<'ctx>, width: u32| -> IntValue<'ctx> {
        if width < 64 {
            cx.b.build_int_truncate(w, opb.i_n(width), "stoch_fit")
                .unwrap()
        } else {
            w
        }
    };
    for &(operand, vword, uword) in outputs {
        let target = value_plusargs_target(cx, operands[operand])?;
        let ty = target.expr_type().ok_or_else(|| {
            LoweringError::unsupported_at(
                &format!("{name}'s output variable has no type"),
                &cx.fn_sym,
            )
        })?;
        if !ty.is_integral() || ty.value_width() > 64 {
            return Err(LoweringError::unsupported_at(
                &format!("{name}'s output variable is not an integral value of at most 64 bits"),
                &cx.fn_sym,
            ));
        }
        let width = ty.value_width().max(1);
        let unk = match uword {
            Some(u) => from64(load(u), width),
            None => opb.i_n(width).const_zero(),
        };
        let pv = opb.pack(
            from64(load(vword), width),
            unk,
            width,
            cx.repr,
            ty.is_signed(),
        );
        crate::expr::store_to(cx, target, LoweredValue::Packed(pv))?;
    }
    if which == KnownSystemName::QFull {
        return Ok(LoweredValue::Packed(opb.pack(
            from64(load(1), 32),
            from64(load(2), 32),
            32,
            cx.repr,
            true,
        )));
    }
    Ok(LoweredValue::Packed(opb.pack(
        i32t.const_zero(),
        i32t.const_zero(),
        32,
        cx.repr,
        false,
    )))
}

/// `$countdrivers(net [, net_is_forced, number_of_01x_drivers, number_of_0_drivers,
/// number_of_1_drivers, number_of_x_drivers])` (LRM 1800-2023 D.2, Table D.1):
/// one call of the runtime's `sv_rt_net_drivers` for the scalar net or constant
/// bit-select, then each supplied output variable stored. Returns 1 when the
/// net has more than one driver at 0, 1 or x (contention), else 0. Any other
/// first argument is refused by name.
fn lower_countdrivers_call<'ctx, 'd, 'm>(
    cx: &LowerCtx<'ctx, 'd, 'm>,
    e: sv_lang::Expression<'d>,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    let bad =
        |why: &str| LoweringError::unsupported_at(&format!("$countdrivers {why}"), &cx.fn_sym);
    let operands = e.operands();
    if operands.is_empty() || operands.len() > 6 {
        return Err(bad("with no net argument or more than six arguments"));
    }
    let mut net = operands[0];
    while net.kind() == ExpressionKind::Conversion {
        match net.conversion_operand() {
            Some(inner) => net = inner,
            None => break,
        }
    }
    let (key, off) = crate::systask::net_strength_key(net)
        .ok_or_else(|| bad("of something other than a net or a constant bit-select of one"))?;
    let mut base = net;
    while matches!(
        base.kind(),
        ExpressionKind::ElementSelect | ExpressionKind::RangeSelect
    ) {
        base = base
            .select_value()
            .ok_or_else(|| bad("of a malformed select"))?;
    }
    let sym = base
        .referenced_symbol()
        .ok_or_else(|| bad("of an unresolved name"))?;
    let wt = cx
        .write_through
        .as_ref()
        .ok_or_else(|| bad("outside a process (it reads the net's live state)"))?;
    let slot = cx
        .locals
        .get(&sym.uid())
        .and_then(|(ptr, _, _)| wt.signals.get(ptr))
        .map(|e| e.0)
        .ok_or_else(|| bad("of a net this process does not read"))?;
    let i64t = cx.ctx.i64_type();
    let i32t = cx.ctx.i32_type();
    let ptr_ty = cx.ctx.ptr_type(AddressSpace::default());
    let opb = OpBuilder {
        ctx: cx.ctx,
        b: &cx.b,
    };
    let outs = crate::types::entry_alloca_in(&cx.b, i32t.array_type(5), "countdrivers_out");
    let rt_fn = cx
        .module
        .get_function("sv_rt_net_drivers")
        .unwrap_or_else(|| {
            cx.module.add_function(
                "sv_rt_net_drivers",
                cx.ctx.void_type().fn_type(
                    &[
                        ptr_ty.into(),
                        i32t.into(),
                        i64t.into(),
                        i32t.into(),
                        ptr_ty.into(),
                    ],
                    false,
                ),
                Some(inkwell::module::Linkage::External),
            )
        });
    cx.b.build_call(
        rt_fn,
        &[
            wt.handle.into(),
            i32t.const_int(u64::from(slot), false).into(),
            i64t.const_int(key, false).into(),
            i32t.const_int(u64::from(off), false).into(),
            outs.into(),
        ],
        "rt_net_drivers_call",
    )
    .unwrap();
    let load = |i: u64| -> IntValue<'ctx> {
        // SAFETY: `i` is below the array length of `outs`.
        let p = unsafe {
            cx.b.build_in_bounds_gep(i32t, outs, &[i64t.const_int(i, false)], "countdrivers_slot")
                .unwrap()
        };
        cx.b.build_load(i32t, p, "countdrivers_word")
            .unwrap()
            .into_int_value()
    };
    // Table D.1's outputs: net_is_forced, then the 01x, 0, 1 and x counts.
    for (k, &operand) in operands.iter().enumerate().skip(1) {
        let target = value_plusargs_target(cx, operand)?;
        let ty = target
            .expr_type()
            .ok_or_else(|| bad("with an untyped output variable"))?;
        if !ty.is_integral() || ty.value_width() > 64 {
            return Err(bad(
                "with an output variable that is not integral or exceeds 64 bits",
            ));
        }
        let width = ty.value_width().max(1);
        let w = load(k as u64 - 1);
        let v = match width.cmp(&32) {
            std::cmp::Ordering::Less => {
                cx.b.build_int_truncate(w, opb.i_n(width), "cd_fit")
                    .unwrap()
            }
            std::cmp::Ordering::Equal => w,
            std::cmp::Ordering::Greater => {
                cx.b.build_int_z_extend(w, opb.i_n(width), "cd_ext")
                    .unwrap()
            }
        };
        let pv = opb.pack(
            v,
            opb.i_n(width).const_zero(),
            width,
            cx.repr,
            ty.is_signed(),
        );
        crate::expr::store_to(cx, target, LoweredValue::Packed(pv))?;
    }
    let multi =
        cx.b.build_int_compare(
            IntPredicate::UGT,
            load(1),
            i32t.const_int(1, false),
            "cd_multi",
        )
        .unwrap();
    let multi = cx.b.build_int_z_extend(multi, i32t, "cd_multi32").unwrap();
    Ok(LoweredValue::Packed(opb.pack(
        multi,
        i32t.const_zero(),
        32,
        cx.repr,
        true,
    )))
}

/// What kind of variable `$value$plusargs` writes (decides the `out` RtArg shape).
#[derive(Clone, Copy, PartialEq, Eq)]
enum PlusArgsClass {
    Int,
    Real,
    Str,
}

/// `$value$plusargs(fmt, variable)` (RT-074/RT-076, LRM 21.6): `fmt` is a
/// string literal or a run-time string value (`string`, or a `reg` holding
/// characters); the runtime parses its `name=%<spec>` and converts the
/// plusarg text to the TARGET's shape, which this call site hands over in the
/// `out` RtArg (`kind` = integral/real/string, `bit_width`): integral targets
/// of any width get the exact 4-state pattern (x/z digits, `_`, negatives,
/// all-X for text that is not a number), real targets the converted `f64`,
/// `string` targets the raw text. A LITERAL format is also checked here
/// (malformed, unknown specifier, or a non-`%s` one into a `string` target are
/// named `LoweringError`s). `variable` is any assignable lvalue (the store
/// goes through [`store_to`]).
///
/// The runtime call's own `bool` result (whether a matching plusarg was
/// found at all) becomes `$value$plusargs`'s own LRM return value (0/1);
/// `variable` is conditionally stored ONLY when the call reports `true` --
/// LRM: "If the plusarg is not found... the variable is left unchanged" --
/// via a genuine runtime conditional branch, never an unconditional store.
fn lower_value_plusargs_call<'ctx, 'd, 'm>(
    cx: &LowerCtx<'ctx, 'd, 'm>,
    e: sv_lang::Expression<'d>,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    let operands = e.operands();
    if operands.len() != 2 {
        return Err(LoweringError::unsupported_at(
            "$value$plusargs requires exactly two arguments (format, variable)",
            &cx.fn_sym,
        ));
    }
    let target = value_plusargs_target(cx, operands[1])?;
    let target_ty = target.expr_type().ok_or_else(|| {
        LoweringError::unsupported_at("$value$plusargs's variable has no type", &cx.fn_sym)
    })?;
    let class = if target_ty.is_string() {
        PlusArgsClass::Str
    } else if target_ty.is_floating() {
        PlusArgsClass::Real
    } else if target_ty.is_integral() {
        PlusArgsClass::Int
    } else {
        return Err(LoweringError::unsupported_at(
            "$value$plusargs into a variable that is not integral, real or string",
            &cx.fn_sym,
        ));
    };
    // A literal format is checked here, at compile time, against the target
    // (a malformed one is a loud error, never a run-time silent `0`); a
    // run-time format string (LRM 21.6 `string format`) is parsed by the
    // runtime on every call, which reports a bad one as "not found".
    let mut fmt_lit = operands[0];
    while fmt_lit.kind() == ExpressionKind::Conversion {
        fmt_lit = fmt_lit.conversion_operand().unwrap_or(fmt_lit);
    }
    if fmt_lit.kind() == ExpressionKind::StringLiteral {
        let fmt_text = fmt_lit.string_literal_value().unwrap_or_default();
        let (_, spec) = sv_sim_runtime::plusargs::split_value_format(&fmt_text).map_err(|err| {
            LoweringError::unsupported_at(
                &format!("$value$plusargs format string `{fmt_text}` is invalid: {err}"),
                &cx.fn_sym,
            )
        })?;
        if !matches!(spec, 'd' | 'h' | 'x' | 'o' | 'b' | 's' | 'e' | 'f' | 'g') {
            return Err(LoweringError::unsupported_at(
                &format!("$value$plusargs format specifier '%{spec}' (LRM 21.6)"),
                &cx.fn_sym,
            ));
        }
        if class == PlusArgsClass::Str && spec != 's' {
            return Err(LoweringError::unsupported_at(
                &format!(
                    "$value$plusargs with '%{spec}' into a `string` variable (only %s is defined)"
                ),
                &cx.fn_sym,
            ));
        }
    }
    let fmt_ptr = crate::systask::lower_format_expr(cx, operands[0])?;
    let target_width = target_ty.value_width();

    let rtarg_ty = crate::systask::rtarg_llvm_type(cx.ctx);
    let out_alloca = crate::types::entry_alloca_in(&cx.b, rtarg_ty, "plusargs_value_out");
    // The module's generic DataLayout gives `i128` only 8-byte alignment,
    // but `RtArg` is 16-aligned (see `systask.rs`'s `rtargs` alloca): the
    // runtime writes the whole struct through `out`.
    if let Some(instr) = out_alloca.as_instruction() {
        instr
            .set_alignment(16)
            .expect("alloca instruction accepts an explicit alignment");
    }
    // The call site's INPUTS to the runtime: the target's class (`kind`)
    // and, for an integral one, its width -- the runtime converts the
    // plusarg text to exactly that shape, whatever the (possibly run-time)
    // format's conversion character is.
    let kind_p =
        cx.b.build_struct_gep(rtarg_ty, out_alloca, 0, "plusargs_value_kind_p")
            .unwrap();
    let kind_tag = match class {
        PlusArgsClass::Int => 0,
        PlusArgsClass::Str => 2,
        PlusArgsClass::Real => 3,
    };
    cx.b.build_store(kind_p, cx.ctx.i32_type().const_int(kind_tag, false))
        .unwrap();
    let width_p =
        cx.b.build_struct_gep(rtarg_ty, out_alloca, 1, "plusargs_value_width_p")
            .unwrap();
    cx.b.build_store(
        width_p,
        cx.ctx.i32_type().const_int(u64::from(target_width), false),
    )
    .unwrap();
    let rt_fn = fetch_or_declare_sv_rt_plusargs_value(cx.ctx, cx.module);
    let found8 =
        cx.b.build_call(
            rt_fn,
            &[fmt_ptr.into(), out_alloca.into()],
            "rt_plusargs_value_call",
        )
        .unwrap()
        .try_as_basic_value()
        .unwrap_basic()
        .into_int_value();

    let entry_block = cx.b.get_insert_block().ok_or_else(|| {
        LoweringError::unsupported_at("$value$plusargs lowered outside a basic block", &cx.fn_sym)
    })?;
    let f = entry_block.get_parent().ok_or_else(|| {
        LoweringError::unsupported_at("$value$plusargs block has no parent function", &cx.fn_sym)
    })?;
    let found_block = cx.ctx.append_basic_block(f, "plusargs_value_found");
    let merge_block = cx.ctx.append_basic_block(f, "plusargs_value_merge");
    let found_bool =
        cx.b.build_int_compare(
            IntPredicate::NE,
            found8,
            cx.ctx.i8_type().const_zero(),
            "plusargs_value_found_bool",
        )
        .unwrap();
    cx.b.build_conditional_branch(found_bool, found_block, merge_block)
        .unwrap();

    cx.b.position_at_end(found_block);
    let op = OpBuilder {
        ctx: cx.ctx,
        b: &cx.b,
    };
    // `crate::systask::rtarg_llvm_type`'s own documented field order: kind,
    // bit_width, is_signed, narrow (3), real, wide_aval, wide_bval,
    // wide_words, str_ptr (8), str_len (9).
    if class == PlusArgsClass::Str {
        let ptr_ty = cx.ctx.ptr_type(AddressSpace::default());
        let ptr_field =
            cx.b.build_struct_gep(rtarg_ty, out_alloca, 8, "plusargs_value_str_ptr_p")
                .unwrap();
        let str_ptr =
            cx.b.build_load(ptr_ty, ptr_field, "plusargs_value_str_ptr")
                .unwrap()
                .into_pointer_value();
        let len_field =
            cx.b.build_struct_gep(rtarg_ty, out_alloca, 9, "plusargs_value_str_len_p")
                .unwrap();
        let len32 =
            cx.b.build_load(cx.ctx.i32_type(), len_field, "plusargs_value_str_len")
                .unwrap()
                .into_int_value();
        let len64 =
            cx.b.build_int_z_extend(len32, cx.ctx.i64_type(), "plusargs_value_str_len64")
                .unwrap();
        let s = crate::strings::intern_bytes(cx, str_ptr, len64);
        crate::fileio::store_output(cx, target, LoweredValue::Str(s))?;
    } else if class == PlusArgsClass::Real {
        let real_ptr =
            cx.b.build_struct_gep(rtarg_ty, out_alloca, 4, "plusargs_value_real_p")
                .unwrap();
        let real =
            cx.b.build_load(cx.ctx.f64_type(), real_ptr, "plusargs_value_real")
                .unwrap()
                .into_float_value();
        crate::expr::store_to(cx, target, LoweredValue::Real(RealValue { llvm: real }))?;
    } else {
        // Wide4State: `wide_aval`/`wide_bval` point at ceil(width/64)
        // little-endian words in the ABI's (aval, bval) encoding; the
        // value/unknown lanes are `aval ^ bval` / `bval`.
        let ptr_ty = cx.ctx.ptr_type(AddressSpace::default());
        let width = target_width.max(1);
        let words = u64::from(width).div_ceil(64);
        let lane_ty = op.i_n((words * 64) as u32);
        let mut lanes = Vec::new();
        for (field, name) in [(5u32, "aval"), (6u32, "bval")] {
            let slot =
                cx.b.build_struct_gep(rtarg_ty, out_alloca, field, "plusargs_value_words_p")
                    .unwrap();
            let words_ptr =
                cx.b.build_load(ptr_ty, slot, "plusargs_value_words")
                    .unwrap()
                    .into_pointer_value();
            let raw =
                cx.b.build_load(lane_ty, words_ptr, &format!("plusargs_value_{name}"))
                    .unwrap();
            inkwell::values::BasicValue::as_instruction_value(&raw)
                .expect("a load is an instruction")
                .set_alignment(8)
                .expect("load accepts an explicit alignment");
            let mut raw = raw.into_int_value();
            if width != (words * 64) as u32 {
                raw =
                    cx.b.build_int_truncate(raw, op.i_n(width), "plusargs_value_fit")
                        .unwrap();
            }
            lanes.push(raw);
        }
        let val =
            cx.b.build_xor(lanes[0], lanes[1], "plusargs_value_val")
                .unwrap();
        let pv = op.pack(val, lanes[1], width, cx.repr, false);
        crate::expr::store_to(cx, target, LoweredValue::Packed(pv))?;
    }
    cx.b.build_unconditional_branch(merge_block).unwrap();

    cx.b.position_at_end(merge_block);
    let found32 =
        cx.b.build_int_z_extend(found8, cx.ctx.i32_type(), "plusargs_value_found_zext")
            .unwrap();
    Ok(LoweredValue::Packed(op.pack(
        found32,
        cx.ctx.i32_type().const_zero(),
        32,
        cx.repr,
        false,
    )))
}

/// `$timeunit`/`$timeprecision` (LRM 1800-2023 §20.4.1) as a constant `int`.
pub(crate) fn lower_timescale_query<'ctx, 'd, 'm>(
    cx: &LowerCtx<'ctx, 'd, 'm>,
    e: sv_lang::Expression<'d>,
    name: &str,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    // LRM 20.4.1: the scale of the named module / `$unit`, else of the
    // enclosing scope. A `` `timescale `` directive lands on the module
    // declared after it, so the lookup starts at the symbol itself.
    let target = match e.operands().first() {
        Some(a) => a.arbitrary_symbol().ok_or_else(|| {
            LoweringError::unsupported_at(
                "$timeunit/$timeprecision argument must name a module, instance or $unit",
                &cx.fn_sym,
            )
        })?,
        None => e.system_call_scope().unwrap_or(cx.fn_sym),
    };
    let (unit, prec) = crate::timescale::unit_and_precision(&target);
    let v = if name == "$timeunit" { unit } else { prec };
    let op = OpBuilder {
        ctx: cx.ctx,
        b: &cx.b,
    };
    Ok(LoweredValue::Packed(op.pack(
        cx.ctx.i32_type().const_int(i64::from(v) as u64, true),
        cx.ctx.i32_type().const_zero(),
        32,
        cx.repr,
        true,
    )))
}

/// `$time`/`$stime`/`$realtime` (LRM 1800-2023 §20.3): the scheduler's
/// tick count (`sv_rt_time()`, never a compile-time fold) in the calling
/// scope's time unit -- `$time`/`$stime` rounded to an integer, `$realtime`
/// as a real (`crate::timescale`).
pub(crate) fn lower_time_system_call<'ctx, 'd, 'm>(
    cx: &LowerCtx<'ctx, 'd, 'm>,
    which: KnownSystemName,
    _name: &str,
    scope: &sv_lang::Symbol<'d>,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    let tick = crate::timescale::tick_exp(cx.ctx, cx.module, scope);
    let per_unit = crate::timescale::ticks_per_unit(scope, tick);
    let rt_fn = fetch_or_declare_sv_rt_time(cx.ctx, cx.module);
    let raw64 =
        cx.b.build_call(rt_fn, &[], "rt_time_call")
            .unwrap()
            .try_as_basic_value()
            .unwrap_basic()
            .into_int_value();

    let op = OpBuilder {
        ctx: cx.ctx,
        b: &cx.b,
    };
    let i64t = cx.ctx.i64_type();
    let ticks = raw64;
    // Ticks to whole units, rounding half up.
    let raw64 = if per_unit > 1 && which != KnownSystemName::RealTime {
        let half =
            cx.b.build_int_add(ticks, i64t.const_int(per_unit / 2, false), "time_round")
                .unwrap();
        cx.b.build_int_unsigned_div(half, i64t.const_int(per_unit, false), "time_units")
            .unwrap()
    } else {
        ticks
    };
    match which {
        KnownSystemName::Time => Ok(LoweredValue::Packed(op.pack(
            raw64,
            cx.ctx.i64_type().const_zero(),
            64,
            cx.repr,
            false,
        ))),
        KnownSystemName::STime => {
            let raw32 =
                cx.b.build_int_truncate(raw64, cx.ctx.i32_type(), "stime_trunc")
                    .unwrap();
            Ok(LoweredValue::Packed(op.pack(
                raw32,
                cx.ctx.i32_type().const_zero(),
                32,
                cx.repr,
                false,
            )))
        }
        KnownSystemName::RealTime => {
            let f =
                cx.b.build_unsigned_int_to_float(ticks, cx.ctx.f64_type(), "realtime_uitofp")
                    .unwrap();
            let f = if per_unit > 1 {
                cx.b.build_float_div(
                    f,
                    cx.ctx.f64_type().const_float(per_unit as f64),
                    "realtime_units",
                )
                .unwrap()
            } else {
                f
            };
            Ok(LoweredValue::Real(RealValue { llvm: f }))
        }
        other => unreachable!(
            "lower_time_system_call is only ever invoked (by lower_expr's Call arm, above) for \
             KnownSystemName::{{Time,STime,RealTime}} -- got {other:?}"
        ),
    }
}

/// R-run Task 13: resizes a memory row-index `PackedValue`'s VALUE plane
/// (`OpBuilder::lane_val`, unsigned -- the same "resize_unsigned" convention
/// `ops.rs`'s own `bit_select4`/`indexed_up_lo4` already use for an index
/// operand) to the accessor ABI's own `i64` `row_index` type.
///
/// **Documented scope limitation:** an index with any unknown (X/Z) bit is
/// read as whatever its VALUE-plane bits happen to be (never propagated into
/// an all-X *result* the way `bit_select4`'s closed-form arithmetic can) --
/// the accessor ABI's `row_index: u64` is a concrete address with no
/// "unknown index" representation, and turning an unknown/out-of-range index
/// into a real all-X read would need a runtime conditional around the
/// `sv_get_mem_word` call this slice does not build. Neither probe-corpus
/// memory fixture (`unpacked_array.sv`) exercises an unknown index.
pub(crate) fn mem_row_index_i64<'ctx>(
    op: &OpBuilder<'_, 'ctx>,
    cx: &LowerCtx<'ctx, '_, '_>,
    idx: PackedValue<'ctx>,
) -> IntValue<'ctx> {
    let val = op.lane_val(idx);
    let src_bits = val.get_type().get_bit_width();
    let i64_ty = cx.ctx.i64_type();
    if src_bits == 64 {
        val
    } else if src_bits < 64 && idx.signed {
        // A signed index keeps its sign (`mem[-2]` on a `[-2:1]` memory;
        // a signed 8'shC8 is -56, out of range -- iverilog-verified).
        cx.b.build_int_s_extend(val, i64_ty, "mem_row_index_sext")
            .unwrap()
    } else if src_bits < 64 {
        cx.b.build_int_z_extend(val, i64_ty, "mem_row_index_zext")
            .unwrap()
    } else {
        cx.b.build_int_truncate(val, i64_ty, "mem_row_index_trunc")
            .unwrap()
    }
}

/// Whether `e` is an unsized based literal (`'bx`, `'dz`, `'h?f`) whose
/// leftmost bit is X or Z: LRM 5.7.1 extends such a literal with that X/Z
/// to the width of the expression containing it, where a plain widening
/// would zero-fill (slang keeps the literal at its own 32-bit width).
fn unsized_literal_leads_unknown(e: sv_lang::Expression<'_>) -> bool {
    e.kind() == ExpressionKind::IntegerLiteral
        && e.is_declared_unsized() == Some(true)
        && e.integer_literal_value()
            .and_then(|cv| {
                let iv = cv.as_integer()?;
                iv.bit(iv.bit_width() as usize - 1).map(|b| b.is_unknown())
            })
            .unwrap_or(false)
}

/// A folded integer constant (`iv`, the value of `e` when given) as a
/// value; a named error, never a guess, when its bits cannot be read.
pub(crate) fn lower_constant<'ctx>(
    cx: &LowerCtx<'ctx, '_, '_>,
    iv: &sv_lang::SVInt,
    e: Option<sv_lang::Expression<'_>>,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    // A vector past the huge threshold is a memory image, never an LLVM
    // integer (`crate::huge`).
    if let Some(ex) = e
        && let Some(h) = crate::huge::lower_constant(cx, ex)?
    {
        return Ok(h);
    }
    if e.is_none() && crate::huge::is_huge_width(iv.bit_width()) {
        return Err(LoweringError::unsupported_at(
            "a constant wider than 65535 bits with no expression to lower it from",
            &cx.fn_sym,
        ));
    }
    let op = OpBuilder {
        ctx: cx.ctx,
        b: &cx.b,
    };
    match op.lower_const_any(iv, cx.repr) {
        Some(AnyValue::Narrow(p)) => Ok(LoweredValue::Packed(p)),
        Some(AnyValue::Wide(w)) => Ok(LoweredValue::Wide(w)),
        None => Err(LoweringError::unsupported_at(
            &format!(
                "a {}-bit constant whose bits sv-lang does not expose",
                iv.bit_width()
            ),
            &cx.fn_sym,
        )),
    }
}

/// Whether `type(a) <op> type(b)` holds, for an equality operator whose two
/// operands are both type references (LRM 1800-2023 6.23: types compare
/// equal exactly when they match, 6.22.1); `None` for any other expression.
pub(crate) fn type_reference_equality(e: sv_lang::Expression<'_>, bop: BinaryOp) -> Option<bool> {
    let negate = match bop {
        BinaryOp::Equality | BinaryOp::CaseEquality => false,
        BinaryOp::Inequality | BinaryOp::CaseInequality => true,
        _ => return None,
    };
    let l = e.left()?.type_reference_target()?;
    let r = e.right()?.type_reference_target()?;
    Some(l.is_matching(&r) != negate)
}

/// The current value of the target of a read-modify-write (`x++`, `x op= y`).
/// A nonexistent associative-array element is allocated with its default
/// first (LRM 1800-2023 7.8.7), so reading it for the update is not the
/// invalid read that warns (7.8.6).
pub(crate) fn read_modify_target<'ctx, 'd>(
    cx: &LowerCtx<'ctx, 'd, '_>,
    target: sv_lang::Expression<'d>,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    if target.kind() == ExpressionKind::ElementSelect
        && let Some(r) = crate::collections::read_element(cx, target)
    {
        return r;
    }
    lower_expr(cx, target)
}

/// Lowers `e` to a value every caller can consume: a vector wider than the
/// huge threshold but within `WIDE_THRESHOLD` is bridged to a [`PackedValue`]
/// (one load of the memory image), so a consumer that has no memory-image
/// path of its own keeps working. A huge-aware consumer calls
/// [`lower_expr_h`] to keep the image.
pub fn lower_expr<'ctx, 'd, 'm>(
    cx: &LowerCtx<'ctx, 'd, 'm>,
    e: sv_lang::Expression<'d>,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    let v = lower_expr_h(cx, e)?;
    Ok(crate::huge::bridge(cx, v))
}

/// The expression a modport port with an explicit expression (`.x(r[3:0])`,
/// LRM 1800-2023 25.5.4) stands for, when `e` names such a port.
pub(crate) fn modport_expr_target<'d>(
    e: &sv_lang::Expression<'d>,
) -> Option<sv_lang::Expression<'d>> {
    if !matches!(
        e.kind(),
        ExpressionKind::NamedValue | ExpressionKind::HierarchicalValue
    ) {
        return None;
    }
    let sym = e.referenced_symbol()?;
    if sym.kind() != sv_lang::kinds::SymbolKind::ModportPort {
        return None;
    }
    sym.modport_port_explicit_connection()
}

/// [`lower_expr`] that keeps a vector wider than the huge threshold as a
/// memory-image [`LoweredValue::Huge`] (`crate::huge`).
pub fn lower_expr_h<'ctx, 'd, 'm>(
    cx: &LowerCtx<'ctx, 'd, 'm>,
    e: sv_lang::Expression<'d>,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    // A modport port with an expression (`input .x(r[3:0])`, LRM 25.5.4) reads
    // as that expression of the interface instance.
    if let Some(inner) = modport_expr_target(&e) {
        return lower_expr_h(cx, inner);
    }
    // A select index evaluated once ahead of a per-candidate lowering
    // (`crate::vif::hoisted`).
    if let Some(v) = crate::vif::hoisted(&e) {
        return Ok(v);
    }
    // A vector wider than the huge threshold (or an operator reading one) is
    // a memory image: `crate::huge`.
    if let Some(v) = crate::huge::try_lower(cx, e)? {
        return Ok(v);
    }
    lower_expr_regular(cx, e)
}

/// The ordinary (non-memory-image) lowering of `e`: what [`lower_expr_h`]
/// does when `crate::huge` does not take the expression, and what
/// `crate::huge` falls back to for an operation it has no native form of.
pub(crate) fn lower_expr_regular<'ctx, 'd, 'm>(
    cx: &LowerCtx<'ctx, 'd, 'm>,
    e: sv_lang::Expression<'d>,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    // A by-value actual of a subroutine call attached to a sequence (16.11): the
    // value sampled at the end point, primed by the assertion's kernel.
    if crate::sva::proc::call_args_active()
        && let Some((slot, width, signed)) = {
            let key = crate::sva::proc::call_arg_key(&e);
            // The value of a task spawned from a match item (16.11) is read from the
            // assertion inputs the spawn hands the process.
            cx.locals.get(&key).copied().or_else(|| {
                crate::sva::proc::spawn_arg_input(&key).map(|(k, width, signed)| {
                    (
                        crate::process_lower::assert_input_slot(cx, k, width),
                        width,
                        signed,
                    )
                })
            })
        }
    {
        let op = OpBuilder {
            ctx: cx.ctx,
            b: &cx.b,
        };
        let ty = if cx.repr == ValueRepr::FourState {
            op.i_n(width * 2)
        } else {
            op.i_n(width)
        };
        let v =
            cx.b.build_load(ty, slot, "ld_call_arg")
                .unwrap()
                .into_int_value();
        return Ok(LoweredValue::Packed(PackedValue {
            llvm: v,
            width,
            repr: cx.repr,
            signed,
        }));
    }
    // A `string`-typed expression: the string-only kinds (conversions to
    // string, concatenation, ...) lower in `crate::strings`; any other kind
    // (a variable, a call, an assignment) yields the string's 64-bit id.
    if e.expr_type().is_some_and(|t| t.is_string()) {
        if let Some(s) = crate::strings::lower_string_expr(cx, e)? {
            return Ok(LoweredValue::Str(s));
        }
        let v = lower_expr_node(cx, e)?;
        return Ok(if let LoweredValue::Packed(pv) = v {
            LoweredValue::Str(crate::strings::from_storage(cx, pv))
        } else {
            v
        });
    }
    // A new queue / dynamic / associative array (`{q, x}`, `'{1, 2}`,
    // `new[n]`, `q[1:$]`): `crate::collections`.
    if let Some(r) = crate::collections::lower_constructor(cx, e) {
        return r;
    }
    let v = lower_expr_node(cx, e)?;
    // A `real`/`shortreal` read that yields its stored IEEE bits (an
    // element of a real array) is the real value.
    if let LoweredValue::Packed(pv) = v
        && e.expr_type().is_some_and(|t| t.is_floating())
    {
        return Ok(LoweredValue::Real(crate::real::bits_to_real(cx, pv)));
    }
    // An integral value's signedness is its expression type's (slang applies
    // the LRM's rules: a select is unsigned, a signed struct member or
    // element is signed, ...), whatever the node's own lowering assumed.
    if let LoweredValue::Packed(pv) = v
        && let Some(t) = e.expr_type()
        && t.is_integral()
        && t.value_width() == pv.width
    {
        return Ok(LoweredValue::Packed(PackedValue {
            signed: t.is_signed(),
            ..pv
        }));
    }
    Ok(v)
}

/// A read of the specparam `sym` whose compile-time value is `base`: the value an SDF `LABEL`
/// annotated at run time (`sv_sim_runtime::specparam`), else `base` (LRM 32.4.3, SD-C / T28).
///
/// The annotation is a real number in the module's time unit. A `real` specparam reads it
/// whole; an integral one reads it rounded to an integer (LRM 6.12.1: a real converted to an
/// integer type rounds to nearest, ties away from zero) at the specparam's own width and
/// signedness -- implementation-defined: the LRM does not say what type an annotated specparam
/// has. A delay that is just the specparam skips this and keeps the fraction
/// (`timescale::lower_delay_ticks`). A specparam wider than the packed-value limit keeps its
/// source value (the SDF host reports it as not annotated rather than annotating it silently).
fn annotated_specparam_read<'ctx>(
    cx: &LowerCtx<'ctx, '_, '_>,
    op: &OpBuilder<'_, 'ctx>,
    sym: &sv_lang::Symbol<'_>,
    base: LoweredValue<'ctx>,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    let annotated = crate::timescale::lower_specparam_read(cx, sym)?;
    let f64t = cx.ctx.f64_type();
    let none =
        cx.b.build_float_compare(
            inkwell::FloatPredicate::UNO,
            annotated,
            annotated,
            "sp_none",
        )
        .unwrap();
    match base {
        LoweredValue::Real(r) => Ok(LoweredValue::Real(RealValue {
            llvm: cx
                .b
                .build_select(none, r.llvm, annotated, "sp_real")
                .unwrap()
                .into_float_value(),
        })),
        LoweredValue::Packed(pv) => {
            // The rounding of a NaN is poison in LLVM; the select below discards that arm, but
            // feed it a harmless zero so no poison is ever formed.
            let safe =
                cx.b.build_select(none, f64t.const_zero(), annotated, "sp_safe")
                    .unwrap()
                    .into_float_value();
            let rounded = op.round_real_to_int(RealValue { llvm: safe }, pv.width, pv.signed);
            let annotated_pv = op.pack(
                rounded,
                op.i_n(pv.width).const_zero(),
                pv.width,
                pv.repr,
                pv.signed,
            );
            Ok(LoweredValue::Packed(PackedValue {
                llvm: cx
                    .b
                    .build_select(none, pv.llvm, annotated_pv.llvm, "sp_int")
                    .unwrap()
                    .into_int_value(),
                ..pv
            }))
        }
        wide @ (LoweredValue::Wide(_) | LoweredValue::Str(_) | LoweredValue::Huge(_)) => Ok(wide),
    }
}

fn lower_expr_node<'ctx, 'd, 'm>(
    cx: &LowerCtx<'ctx, 'd, 'm>,
    e: sv_lang::Expression<'d>,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    let op = OpBuilder {
        ctx: cx.ctx,
        b: &cx.b,
    };
    match e.kind() {
        // A string literal in an integral context (`w = "AB"`, `"a" < "b"`):
        // its bytes as a packed value (LRM 5.9), like an integer literal. An
        // unbased unsized literal (`'1`, `'x`) folds to its context's width.
        //
        // Review fix (r2), Task 6 finding 2: each of these three literal
        // kinds has its OWN cache-independent value accessor
        // (`integer_literal_value`/`string_literal_int_value`/
        // `unbased_unsized_literal_value`) -- the value is set once at
        // construction, never lazily folded, so each is "always available"
        // per its own doc comment. `Expression::constant_value()` instead
        // reads slang's PREFOLD-SWEEP CACHE (`FreezeReport`'s own doc: "if
        // this expression has one cached") -- populated, once, for every
        // literal reachable from the frozen `Design`'s AST at
        // `Compilation::compile()` time. A node slang synthesizes LATER,
        // on first access well after that freeze (e.g. `extends Base(7)`'s
        // own implicit `Call`, built by `Type::class_base_constructor_
        // call()` -- `classes.rs::chain_to_base`), was never visited by
        // that sweep and so is never cached, even though its own value is
        // exactly as intrinsically known as any other literal's. Confirmed
        // live: `class derived extends base(7);` failed "non-const
        // literal" on exactly this node before this fix; the identical `7`
        // written as an explicit `super.new(7)` statement already worked,
        // because THAT literal is part of the ordinary, frozen-at-compile-
        // time AST the prefold sweep did visit.
        ExpressionKind::IntegerLiteral
        | ExpressionKind::StringLiteral
        | ExpressionKind::UnbasedUnsizedIntegerLiteral => {
            let cv = match e.kind() {
                ExpressionKind::IntegerLiteral => e.integer_literal_value(),
                ExpressionKind::StringLiteral => e.string_literal_int_value(),
                ExpressionKind::UnbasedUnsizedIntegerLiteral => e.unbased_unsized_literal_value(),
                _ => unreachable!(
                    "filtered by the outer match arm above, which already restricts \
                     `e.kind()` to exactly these three -- `kind()` is a pure read with no \
                     side effects between the two calls"
                ),
            }
            .ok_or_else(|| LoweringError::unsupported_at("non-const literal", &cx.fn_sym))?;
            let iv = cv
                .as_integer()
                .ok_or_else(|| LoweringError::unsupported_at("non-integer literal", &cx.fn_sym))?;
            // Review fix r2: route through `lower_const_any` (Task 15's
            // narrow/wide dispatch point) instead of calling `const_svint`
            // directly, so a >WIDE_THRESHOLD-bit literal produces
            // `LoweredValue::Wide` instead of panicking inside
            // `const_svint`'s `assert!`.
            lower_constant(cx, iv, Some(e))
        }
        // Task 8 (CG-037): a `real` literal's own natural type is always
        // `real` (f64) per LRM 5.7.1 -- narrowing it to `shortreal` (f32) is
        // a separate `Conversion` node slang inserts around it, out of
        // CG-037's int<->real scope (see the Conversion arm's `(true,
        // true)` case below).
        ExpressionKind::RealLiteral => {
            let val = e.real_literal_value().ok_or_else(|| {
                LoweringError::unsupported_at("real literal missing value", &cx.fn_sym)
            })?;
            Ok(LoweredValue::Real(RealValue {
                llvm: cx.ctx.f64_type().const_float(val),
            }))
        }
        // A time literal (LRM 1800-2023 §5.8) is a realtime value in the
        // scope's time unit -- slang has already scaled it -- rounded to the
        // scope's precision.
        ExpressionKind::TimeLiteral => {
            let val = e.time_literal_value().ok_or_else(|| {
                LoweringError::unsupported_at("time literal missing value", &cx.fn_sym)
            })?;
            let val = match e.time_literal_scale() {
                Some(ts) => {
                    let steps = 10f64.powi(
                        crate::timescale::exp_of(ts.base) - crate::timescale::exp_of(ts.precision),
                    );
                    (val * steps).round() / steps
                }
                None => val,
            };
            Ok(LoweredValue::Real(RealValue {
                llvm: cx.ctx.f64_type().const_float(val),
            }))
        }
        // A variable or net named where slang's system-task signature takes a
        // scope or object (`$dumpports(top, fname)`'s file name) arrives as an
        // `ArbitrarySymbol`; as a value it reads like a `NamedValue`.
        ExpressionKind::NamedValue
        | ExpressionKind::HierarchicalValue
        | ExpressionKind::ArbitrarySymbol => {
            if e.kind() == ExpressionKind::ArbitrarySymbol
                && !e.referenced_symbol().is_some_and(|s| {
                    matches!(
                        s.kind(),
                        sv_lang::kinds::SymbolKind::Variable | sv_lang::kinds::SymbolKind::Net
                    )
                })
            {
                // An interface instance named as a value (`vif = bus`): its
                // virtual interface id (`crate::vif`).
                return e
                    .referenced_symbol()
                    .and_then(|s| crate::vif::instance_value(cx, &s))
                    .ok_or_else(|| {
                        LoweringError::unsupported_at(
                            "a symbol used as a value that is not an interface instance",
                            &cx.fn_sym,
                        )
                    });
            }
            // Corrected vs. plan: `Expression::as_symbol()` does not exist.
            // The real accessor for "what symbol does this expression
            // reference" is `referenced_symbol()` (verified against
            // sv-lang's own doctest on it, which uses exactly this
            // NamedValue-lookup case).
            let sym = e
                .referenced_symbol()
                .ok_or_else(|| LoweringError::unsupported_at("unresolved name", &cx.fn_sym))?;
            // An array method's iterator (`item`): the current element.
            if let Some(v) = crate::array_methods::iterator_value(cx, &sym) {
                return Ok(v);
            }
            // Classes plan Task 5 (§1.4 step 4): a bare reference to a
            // non-static class property, inside a method body (implicit
            // `this.field`) -- a `ClassProperty` is never a `cx.locals`
            // entry (its storage is the object heap, not an LLVM alloca/
            // STATE signal), so this is tried BEFORE the `cx.locals` lookup
            // just below, not after it.
            if let Some(v) = crate::classes::try_read_bare_field(cx, &sym)? {
                return Ok(v);
            }
            if let Some((slot, width, signed)) = cx.locals.get(&sym.uid()).copied() {
                crate::collections::elem_ref_refresh(cx, slot);
                let ty = if cx.repr == ValueRepr::FourState {
                    op.i_n(width * 2)
                } else {
                    op.i_n(width)
                };
                let v = cx.b.build_load(ty, slot, "ld").unwrap().into_int_value();
                let pv = PackedValue {
                    llvm: v,
                    width,
                    repr: cx.repr,
                    signed,
                };
                // A real variable's cell holds its IEEE bits.
                if sym.value_type().is_some_and(|t| t.is_floating()) {
                    return Ok(LoweredValue::Real(crate::real::bits_to_real(cx, pv)));
                }
                return Ok(LoweredValue::Packed(pv));
            }
            // R-run Task 15: found writing `enum_struct.sv`'s own fixture
            // (this task's plan text assumed "s, the enum ... should
            // already work" but never anticipated an ENUM CONSTANT
            // reference, e.g. `s = BLUE;` -- `BLUE`'s own `NamedValue` node
            // resolves to the `EnumValue` symbol, which is never primed
            // into `cx.locals` at all (it has no runtime storage of its
            // own -- it is a compile-time constant, the same category as a
            // `parameter`/`genvar`). The pre-existing code unconditionally
            // treated ANY `cx.locals` miss as "read of non-local", which is
            // correct for a genuinely-unwired signal but wrong for this
            // case. Falls back to the exact same constant-folding chain
            // the `IntegerLiteral` arm above already uses
            // (`constant_value()` -> `as_integer()` -> `lower_const_any`)
            // rather than inventing a second, divergent way to build a
            // constant `PackedValue`/`AnyValue::Wide`.
            // A specparam read inside a procedural body carries no folded value of its own
            // on the expression; the symbol's frozen constant is its source value.
            let cv = e
                .constant_value()
                .or_else(|| {
                    // A hierarchical reference into another instance's (or a
                    // generate block's, or an enum's) constant (`u1.WA`,
                    // `g.VALUE`, `e_t.A`, LRM 23.6) folds to no value on the
                    // expression itself; the target symbol's value is exact.
                    matches!(
                        sym.kind(),
                        sv_lang::kinds::SymbolKind::Specparam
                            | sv_lang::kinds::SymbolKind::Parameter
                            | sv_lang::kinds::SymbolKind::EnumValue
                    )
                    .then(|| sym.constant_value())
                    .flatten()
                })
                .ok_or_else(|| LoweringError::unsupported_at("read of non-local", &sym))?;
            let base = match &cv {
                sv_lang::ConstantValue::Real(f) => LoweredValue::Real(RealValue {
                    llvm: cx.ctx.f64_type().const_float(*f),
                }),
                // A `string` parameter/localparam: its text, as a string literal.
                sv_lang::ConstantValue::String(text) => {
                    LoweredValue::Str(crate::strings::literal(cx, text.as_bytes()))
                }
                // A class-handle parameter/localparam (LRM 6.20.2, 8.4): the only
                // constant handle is `null`, lowered like the `null` literal.
                sv_lang::ConstantValue::Null => {
                    let op = OpBuilder {
                        ctx: cx.ctx,
                        b: &cx.b,
                    };
                    LoweredValue::Packed(op.pack(
                        op.i_n(64).const_zero(),
                        op.i_n(64).const_zero(),
                        64,
                        cx.repr,
                        false,
                    ))
                }
                // An unpacked-struct parameter/localparam (LRM 6.20.2, 7.2): its fields'
                // stored bits, the same form an unpacked struct variable holds.
                sv_lang::ConstantValue::Array(_)
                    if e.expr_type().is_some_and(|t| t.is_unpacked_struct()) =>
                {
                    let ty = e.expr_type().expect("checked above");
                    let mut strs = Vec::new();
                    let bits = const_stored_bits_strs(&ty, &cv, 0, &mut strs)
                        .filter(|b| !b.is_empty() && b.len() as u32 <= crate::value::WIDE_THRESHOLD)
                        .ok_or_else(|| {
                            LoweringError::unsupported_at(
                                "an unpacked-struct named constant with a field that is not \
                                 integral, real or string (or wider than the wide-value threshold)",
                                &sym,
                            )
                        })?;
                    let mut pv = crate::value::const_bits(cx.ctx, &bits, false, cx.repr);
                    // Each string field's slot is the run-time interned id of its text.
                    for (off, text) in &strs {
                        let id = crate::strings::literal(cx, text).llvm;
                        let wide = pv.llvm.get_type();
                        let ext = cx.b.build_int_z_extend(id, wide, "sid").unwrap();
                        let sh = cx
                            .b
                            .build_left_shift(ext, wide.const_int(*off as u64, false), "sid_sh")
                            .unwrap();
                        pv.llvm = cx.b.build_or(pv.llvm, sh, "sid_or").unwrap();
                    }
                    LoweredValue::Packed(pv)
                }
                _ => {
                    let iv = cv.as_integer().ok_or_else(|| {
                        LoweringError::unsupported_at(
                            "named constant is neither an integer, a real nor a string \
                             (an unpacked-array, struct or class-handle named constant \
                             read is out of scope)",
                            &sym,
                        )
                    })?;
                    lower_constant(cx, iv, Some(e))?
                }
            };
            // SD-C / T28 (LRM 32.4.3): a specparam an SDF `LABEL` annotated reads as the
            // annotated value, not the one folded at compile time.
            if sym.kind() == sv_lang::kinds::SymbolKind::Specparam {
                return annotated_specparam_read(cx, &op, &sym, base);
            }
            Ok(base)
        }
        ExpressionKind::UnaryOp => {
            let operand_expr = e.operand().ok_or_else(|| {
                LoweringError::unsupported_at("unary op missing operand", &cx.fn_sym)
            })?;
            let uop = e.unary_op().ok_or_else(|| {
                LoweringError::unsupported_at("unary op missing operator", &cx.fn_sym)
            })?;
            // Task 26 (CG-121) support code: `++`/`--` (pre/post) need to
            // WRITE BACK into the operand's `locals` slot, unlike every
            // other unary operator here (which only reads its operand's
            // already-lowered VALUE) -- so this is a genuinely separate
            // lvalue path, intercepted before the generic `lower_expr`
            // operand evaluation below, mirroring `Assignment`'s own
            // "resolve lvalue -> locals slot" shape (same scope
            // restriction: only a plain `NamedValue` lvalue, never a
            // bit-select/concat/non-local, which is a loud `LoweringError`
            // rather than a silent no-op). Added because Task 26's `for`
            // loop step expressions (`for (...; ...; i++)`) are the most
            // common real-world site for this operator; `func.rs`'s
            // `ForLoop` arm is `lower_expr`'s only caller that needs it
            // today, but this is ordinary expression-value lowering, not
            // loop-specific, so it belongs here, not in `func.rs`.
            if matches!(
                uop,
                UnaryOp::Preincrement
                    | UnaryOp::Predecrement
                    | UnaryOp::Postincrement
                    | UnaryOp::Postdecrement
            ) {
                // Any target `select.rs` can resolve: read it as an rvalue,
                // add/subtract one at its own width, write it back.
                let old = read_modify_target(cx, operand_expr)?;
                if let LoweredValue::Real(rv) = old {
                    // `r++` on a real adds 1.0 (LRM §11.4.2).
                    let one = cx.ctx.f64_type().const_float(1.0);
                    let f = crate::real::as_f64(cx, rv.llvm);
                    let new = match uop {
                        UnaryOp::Preincrement | UnaryOp::Postincrement => {
                            cx.b.build_float_add(f, one, "real_inc").unwrap()
                        }
                        _ => cx.b.build_float_sub(f, one, "real_dec").unwrap(),
                    };
                    let new = LoweredValue::Real(RealValue { llvm: new });
                    store_to(cx, operand_expr, new)?;
                    return Ok(match uop {
                        UnaryOp::Preincrement | UnaryOp::Predecrement => new,
                        _ => old,
                    });
                }
                let old_pv =
                    old.into_packed("increment/decrement of a >128-bit target", &cx.fn_sym)?;
                let width = old_pv.width;
                let one_pv = op.pack(
                    op.i_n(width).const_int(1, false),
                    op.i_n(width).const_zero(),
                    width,
                    cx.repr,
                    old_pv.signed,
                );
                let new_pv = match uop {
                    UnaryOp::Preincrement | UnaryOp::Postincrement => op.add4(old_pv, one_pv),
                    UnaryOp::Predecrement | UnaryOp::Postdecrement => op.sub4(old_pv, one_pv),
                    _ => unreachable!("filtered by the outer matches! above"),
                };
                // Through `store_to`: the target may be an element of a
                // queue or associative array.
                store_to(cx, operand_expr, LoweredValue::Packed(new_pv))?;
                let result_pv = match uop {
                    UnaryOp::Preincrement | UnaryOp::Predecrement => new_pv,
                    UnaryOp::Postincrement | UnaryOp::Postdecrement => old_pv,
                    _ => unreachable!("filtered by the outer matches! above"),
                };
                return Ok(LoweredValue::Packed(result_pv));
            }
            if uop == UnaryOp::LogicalNot
                && let Some(non_null) = crate::events::lower_event_condition(cx, operand_expr)?
            {
                // `!e` of an event: true iff it is `null` (15.5.5.3).
                let is_null = cx.b.build_not(non_null, "ev_isnull").unwrap();
                return Ok(LoweredValue::Packed(op.pack(
                    is_null,
                    cx.ctx.bool_type().const_zero(),
                    1,
                    cx.repr,
                    false,
                )));
            }
            let operand_lv = lower_expr(cx, operand_expr)?;
            match operand_lv {
                LoweredValue::Str(_) => Err(LoweringError::unsupported_at(
                    &format!("unary op {uop:?} on a string operand"),
                    &cx.fn_sym,
                )),
                // Task 8 (CG-037) plumbing note: a negative real LITERAL
                // (e.g. `-3.9`) is not its own literal node -- slang parses
                // it as `UnaryOp::Minus` over the positive `RealLiteral`
                // `3.9` (confirmed by running the BINDING CORRECTION 2
                // regression tests against real SV source), so real->int
                // rounding's own negative-value test cases are unreachable
                // from compiled source without this arm. Only `Minus`
                // (real negation, LRM 6.12) is meaningful on a real
                // operand; every other unary operator is a loud error.
                LoweredValue::Real(rv) => match uop {
                    UnaryOp::Minus => Ok(LoweredValue::Real(RealValue {
                        llvm: cx.b.build_float_neg(rv.llvm, "fneg").unwrap(),
                    })),
                    UnaryOp::Plus => Ok(LoweredValue::Real(rv)),
                    UnaryOp::LogicalNot => {
                        let t = crate::real::real_truth(cx, rv);
                        let not = cx.b.build_not(t, "real_lnot").unwrap();
                        Ok(LoweredValue::Packed(op.pack(
                            not,
                            cx.ctx.bool_type().const_zero(),
                            1,
                            cx.repr,
                            false,
                        )))
                    }
                    other => Err(LoweringError::unsupported_at(
                        &format!("unary op {other:?} on a real operand"),
                        &cx.fn_sym,
                    )),
                },
                LoweredValue::Packed(a) => Ok(LoweredValue::Packed(match uop {
                    UnaryOp::BitwiseNot => op.not4(a),
                    UnaryOp::BitwiseAnd => op.redand4(a),
                    UnaryOp::BitwiseOr => op.redor4(a),
                    UnaryOp::BitwiseXor => op.redxor4(a),
                    // LRM 11.4.9 reduction `~&`, `~|`, `~^`/`^~`: the
                    // 1-bit reduction inverted bitwise (an X result stays X).
                    UnaryOp::BitwiseNand => op.not4(op.redand4(a)),
                    UnaryOp::BitwiseNor => op.not4(op.redor4(a)),
                    UnaryOp::BitwiseXnor => op.not4(op.redxor4(a)),
                    UnaryOp::Minus => op.neg4(a),
                    // LRM 11.4.3 unary `+` is the identity on its operand.
                    UnaryOp::Plus => a,
                    // Task F1 (CG-026): `!` (logical NOT, LRM SS11.4.7) --
                    // always a 1-bit unsigned result, reusing the
                    // reduction-OR helper (see `OpBuilder::lnot4`).
                    UnaryOp::LogicalNot => op.lnot4(a),
                    other => {
                        return Err(LoweringError::unsupported_at(
                            &format!("unary op {other:?}"),
                            &cx.fn_sym,
                        ));
                    }
                })),
                // Review fix r2: no wide-value unary arithmetic exists yet
                // (Task 16+ scope, same as every other wide-value consumer
                // in this file) -- a loud, named `LoweringError`, never a
                // panic (e.g. `a.llvm` on a `WideValueRef`, which has no
                // such field) and never a silent narrow reinterpretation.
                // Unary `+` (LRM 11.4.3) is the identity at any width.
                v @ (LoweredValue::Wide(_) | LoweredValue::Huge(_)) if uop == UnaryOp::Plus => {
                    Ok(v)
                }
                LoweredValue::Wide(_) | LoweredValue::Huge(_) => {
                    Err(LoweringError::unsupported_at(
                        &format!("unary op {uop:?} on a wide (>WIDE_THRESHOLD-bit) operand"),
                        &cx.fn_sym,
                    ))
                }
            }
        }
        ExpressionKind::BinaryOp => {
            let bop = e.binary_op().ok_or_else(|| {
                LoweringError::unsupported_at("binary op missing operator", &cx.fn_sym)
            })?;
            // `type(a) == type(b)` (LRM 1800-2023 6.23): decided at compile
            // time by type matching (6.22.1).
            if let Some(eq) = type_reference_equality(e, bop) {
                let bit = if eq {
                    sv_lang::Bit::One
                } else {
                    sv_lang::Bit::Zero
                };
                return Ok(LoweredValue::Packed(crate::value::const_bits(
                    cx.ctx,
                    &[bit],
                    false,
                    cx.repr,
                )));
            }
            // `==`/`!=` of events (LRM 1800-2023 15.5.5.2, 15.5.5.3).
            if matches!(
                bop,
                BinaryOp::Equality
                    | BinaryOp::Inequality
                    | BinaryOp::CaseEquality
                    | BinaryOp::CaseInequality
            ) && let Some(v) = crate::events::lower_event_equality(
                cx,
                e,
                matches!(bop, BinaryOp::Inequality | BinaryOp::CaseInequality),
            )? {
                return Ok(v);
            }
            // `==`/`!=` of two queues, dynamic or associative arrays.
            if matches!(
                bop,
                BinaryOp::Equality
                    | BinaryOp::Inequality
                    | BinaryOp::CaseEquality
                    | BinaryOp::CaseInequality
            ) && let Some(r) = crate::collections::lower_equality(
                cx,
                e,
                matches!(bop, BinaryOp::Inequality | BinaryOp::CaseInequality),
            ) {
                return r;
            }
            // Task 9 (CG-090, CG-007): `&&`/`||` are short-circuit control
            // flow (LRM 1800-2023 §11.4.7's own evaluation-order rule), NOT
            // a bitwise combination of two eagerly-lowered operands --
            // routed to a dedicated lowering BEFORE the generic
            // eager-lower-both-operands path below, which every other
            // binary operator in this match still uses unchanged.
            // Review fix (r1): this generic `BinaryOp` dispatch arm is the
            // "ordinary value" consumption site (an assignment RHS, a
            // sub-expression of a larger expression, ...), NOT a direct
            // if/while/wait/case condition -- that site is
            // `lower_condition` below, which alone is permitted to collapse
            // an ambiguous result to a crisp false per §12.4.
            // `lower_short_circuit_logical_value` instead preserves the
            // operand's own tri-state ambiguity per LRM SS11.4.7 Table
            // 11-4 (CG-021: every operator's value result must be
            // LRM-correct against its truth table, and this is the only
            // place `LogicalAnd`/`LogicalOr` value-lowering exists in this
            // crate).
            let is_real_operand = |x: Option<sv_lang::Expression<'d>>| {
                x.and_then(|x| x.expr_type())
                    .is_some_and(|t| t.is_floating())
            };
            if (is_real_operand(e.left()) || is_real_operand(e.right()))
                && !matches!(
                    bop,
                    BinaryOp::LogicalImplication | BinaryOp::LogicalEquivalence
                )
            {
                return lower_real_binary_expr(cx, e, bop);
            }
            let is_string_operand = |x: Option<sv_lang::Expression<'d>>| {
                x.and_then(|x| x.expr_type()).is_some_and(|t| t.is_string())
            };
            if (is_string_operand(e.left()) || is_string_operand(e.right()))
                && let Some(v) = crate::strings::lower_string_binary(cx, e, bop)?
            {
                return Ok(v);
            }
            if e.left()
                .and_then(|x| x.expr_type())
                .is_some_and(|t| t.is_unpacked_array())
            {
                return crate::arrays::compare_arrays(cx, e, bop).map(LoweredValue::Packed);
            }
            if matches!(bop, BinaryOp::LogicalAnd | BinaryOp::LogicalOr) {
                if let Some(v) = fold_constant_logical(cx, e)? {
                    return Ok(v);
                }
                return lower_short_circuit_logical_value(
                    cx,
                    e,
                    bop == BinaryOp::LogicalAnd,
                    false,
                );
            }
            // `a -> b` is `!a || b` (LRM 11.4.7): the right operand is only
            // evaluated when the left is not definitely false.
            if bop == BinaryOp::LogicalImplication {
                return lower_short_circuit_logical_value(cx, e, false, true);
            }
            // `a <-> b` evaluates both operands: `(a -> b) && (b -> a)`,
            // i.e. the 1-bit XNOR of the two tri-state truths (LRM 11.4.7).
            if bop == BinaryOp::LogicalEquivalence {
                return lower_logical_equivalence(cx, e);
            }
            // Task 12 (CG-134, CG-135, CG-136, CG-137): power (`**`) is
            // routed BEFORE the generic eager-lower-both-operands path below
            // for the same reason `&&`/`||` are -- its two paths need
            // genuinely different treatment of the right operand, not a
            // uniform `PackedValue`. Per docs/design/codegen.md §2 / the
            // plan: try `e.right().constant_value()` first (a compile-time-
            // constant exponent unrolls via `pow4_const`, CG-134, with ZERO
            // IR built for the exponent operand itself -- not even a lowered
            // constant, so `const_exponent_unrolls_no_call_instruction`'s
            // "only muls" assertion can never be muddied by an unrelated
            // unused-constant instruction); else fall to the runtime
            // `pow4_call` helper (CG-135/CG-136).
            //
            // `iv.as_i64()` (not `iv.has_unknown()`/a raw bit read) is the
            // right query here: it already returns `None` whenever the
            // constant contains any X/Z bit or doesn't fit in 64 bits
            // (verified against the vendored sv-lang 0.1.9 source,
            // `rust/sv-lang/src/constant.rs`'s `SVInt::as_i64` doc comment:
            // "or `None` if it has unknown bits or does not fit") -- exactly
            // the two cases where compile-time-unrolling is impossible, so
            // both correctly fall through to the runtime path instead of
            // this task inventing its own separate has-unknown check.
            if bop == BinaryOp::Power {
                let base = lower_expr(
                    cx,
                    e.left().ok_or_else(|| {
                        LoweringError::unsupported_at("power missing base operand", &cx.fn_sym)
                    })?,
                )?
                .into_packed("power on a non-integral (real) base operand", &cx.fn_sym)?;
                let exp_expr = e.right().ok_or_else(|| {
                    LoweringError::unsupported_at("power missing exponent operand", &cx.fn_sym)
                })?;
                let const_exp: Option<i64> = exp_expr
                    .constant_value()
                    .as_ref()
                    .and_then(sv_lang::ConstantValue::as_integer)
                    .and_then(sv_lang::SVInt::as_i64);
                return Ok(LoweredValue::Packed(match const_exp {
                    Some(exp) => op.pow4_const(base, exp),
                    None => {
                        let exp_pv = lower_expr(cx, exp_expr)?.into_packed(
                            "power on a non-integral (real) exponent operand",
                            &cx.fn_sym,
                        )?;
                        // A runtime exponent wider than 64 bits (LRM 11.4.3:
                        // self-determined) goes to the `_e{width}` helper
                        // variant, which scans only the exponent bits that
                        // can matter. The unrolled scan is `min(exp, base)`
                        // steps of base-width multiplies, so an absurdly
                        // wide pair is refused by name, never lowered into
                        // a multi-million-multiply function.
                        if exp_pv.width > 64
                            && u64::from(exp_pv.width.min(base.width)) * u64::from(base.width)
                                > 4_000_000
                        {
                            return Err(LoweringError::unsupported_at(
                                "'**' with a runtime exponent wider than 64 bits \
                                 on a base this wide (the unrolled square-and-\
                                 multiply scan would exceed the size limit)",
                                &cx.fn_sym,
                            ));
                        }
                        // BINDING/design note: per the "only ever consult
                        // the left/base operand's own `signed`" discipline
                        // this crate already applies to every other
                        // signed-sensitive op (`div4`/`mod4`/the relational
                        // pair), `pow4_call`'s `signed` selects the
                        // `sv_ipow_{u,s}N` variant by the BASE's own
                        // signedness (whether the LRM `-1` special case is
                        // even checkable) -- see `pow4_call`'s doc comment
                        // for how the exponent's OWN, independent
                        // signedness (it is self-determined per LRM
                        // 1800-2023, so it need not match the base's) is
                        // still handled correctly despite this crate's
                        // single `signed: bool` parameter.
                        op.pow4_call(cx.module, base, exp_pv, base.signed)
                    }
                }));
            }
            let l = lower_expr(
                cx,
                e.left().ok_or_else(|| {
                    LoweringError::unsupported_at("binary op missing left operand", &cx.fn_sym)
                })?,
            )?
            .into_packed(
                "binary op on a non-integral (real) left operand",
                &cx.fn_sym,
            )?;
            let r = lower_expr(
                cx,
                e.right().ok_or_else(|| {
                    LoweringError::unsupported_at("binary op missing right operand", &cx.fn_sym)
                })?,
            )?
            .into_packed(
                "binary op on a non-integral (real) right operand",
                &cx.fn_sym,
            )?;
            Ok(LoweredValue::Packed(match bop {
                BinaryOp::BinaryAnd => op.and4(l, r),
                BinaryOp::BinaryOr => op.or4(l, r),
                BinaryOp::BinaryXor => op.xor4(l, r),
                // LRM 11.4.8 binary `~^`/`^~`: the bitwise XOR inverted
                // (an X bit stays X).
                BinaryOp::BinaryXnor => op.not4(op.xor4(l, r)),
                BinaryOp::Add => op.add4(l, r),
                BinaryOp::Subtract => op.sub4(l, r),
                BinaryOp::Multiply => op.mul4(l, r),
                BinaryOp::Divide => op.div4(l, r),
                BinaryOp::Mod => op.mod4(l, r),
                BinaryOp::LogicalShiftLeft | BinaryOp::ArithmeticShiftLeft => op.shl4(l, r),
                BinaryOp::LogicalShiftRight => op.lshr4(l, r),
                // ashr4 itself dispatches on `l.signed` (LRM SS11.4.10:
                // `>>>` on an unsigned operand is defined as `>>`, and
                // slang's AST does not already distinguish the two cases at
                // this BinaryOp variant -- see ashr4's doc comment).
                BinaryOp::ArithmeticShiftRight => op.ashr4(l, r),
                // CG-130: `===`/`!==` are pure bitwise identity over both
                // planes, always known -- `!==` is a plain boolean NOT of
                // `===`'s result (`not4` on a result whose own unk lane is
                // always 0 reduces to exactly that, no separate formula
                // needed).
                BinaryOp::CaseEquality => op.caseeq4(l, r),
                BinaryOp::CaseInequality => op.not4(op.caseeq4(l, r)),
                // CG-131: `==?`/`!=?` are the shared wildcard-match
                // primitive with `treat_x_as_wildcard: true` -- ASYMMETRIC
                // per IEEE 1800-2023 SS11.4.6: only `r`'s (the right-hand
                // operand's) X/Z bits are wildcards, so unlike `caseeq4`
                // this result can itself be X (see `wildcard_match`'s doc
                // comment). `!=?` mirrors `!==`'s NOT-of-the-positive-form
                // lowering; `not4` correctly propagates the X here, unlike
                // `caseeq4`'s always-known result.
                BinaryOp::WildcardEquality => op.wildeq4(l, r),
                BinaryOp::WildcardInequality => op.not4(op.wildeq4(l, r)),
                // Task 11 (CG-021): loose `==`/`!=` -- any-operand-unknown
                // anywhere -> X, distinct from `===`/`!==`'s always-known
                // bitwise-identity formula above (see `OpBuilder::eq4`'s
                // doc comment).
                BinaryOp::Equality => op.eq4(l, r),
                BinaryOp::Inequality => op.neq4(l, r),
                // Task 11 (CG-021): relational operators. Per ARCHITECTURE.md's
                // Codegen note (the same "only ever consult `l`" discipline
                // `div4`/`mod4` already follow for `signed`), dispatch to the
                // signed or unsigned word-compare pair based solely on the
                // left operand's own `signed` -- slang's inserted `Conversion`
                // nodes have already equalized `l`/`r`'s signedness before a
                // binary op lowers.
                BinaryOp::LessThan => {
                    if l.signed {
                        op.slt4(l, r)
                    } else {
                        op.ult4(l, r)
                    }
                }
                BinaryOp::LessThanEqual => {
                    if l.signed {
                        op.sle4(l, r)
                    } else {
                        op.ule4(l, r)
                    }
                }
                BinaryOp::GreaterThan => {
                    if l.signed {
                        op.sgt4(l, r)
                    } else {
                        op.ugt4(l, r)
                    }
                }
                BinaryOp::GreaterThanEqual => {
                    if l.signed {
                        op.sge4(l, r)
                    } else {
                        op.uge4(l, r)
                    }
                }
                other => {
                    return Err(LoweringError::unsupported_at(
                        &format!("binary op {other:?}"),
                        &cx.fn_sym,
                    ));
                }
            }))
        }
        // Task 10 (CG-039, CG-040, CG-041, CG-008): ternary `?:` -- see
        // `lower_ternary` below for the full formula.
        ExpressionKind::ConditionalOp => lower_ternary(cx, e),
        // CG-132/CG-133: `x inside {c0, c1, ...}` -- each candidate is
        // either a plain value (wildcard-matched against `x`, CG-132's
        // "plain element" case) or a `ValueRange` (`[lo:hi]`, an
        // X-propagating `>=`/`<=` pair, CG-132's "range" case). Every
        // candidate's contribution is already a genuinely 3-valued (0/1/X)
        // single bit (see `OpBuilder::inside_plain_candidate4`/
        // `inside_range_candidate4`'s doc comments), so OR-reducing them
        // via the existing 4-state `or4` implements CG-133's "any matched
        // wins, else X if any was ambiguous, else 0" formula directly.
        //
        // `x dist {...}` (16.14.2): "when a property with biasing is used
        // within an assert or cover assertion statement, the dist operator is
        // equivalent to the inside operator, and the weight specification is
        // ignored". Simulation of an assertion never draws values, so a
        // `dist` lowers exactly as the `inside` over the same items.
        ExpressionKind::Inside | ExpressionKind::Dist => {
            let is_dist = e.kind() == ExpressionKind::Dist;
            let left_expr = if is_dist {
                e.dist_left()
            } else {
                e.inside_left()
            };
            let left_expr = left_expr.ok_or_else(|| {
                LoweringError::unsupported_at("inside missing left operand", &cx.fn_sym)
            })?;
            let candidates = if is_dist {
                e.dist_items().iter().map(|item| item.value()).collect()
            } else {
                e.inside_range_list()
            };
            // LRM 11.4.13: a string left operand is compared by `==` (a
            // range by the string relational operators, 6.16).
            if left_expr.expr_type().is_some_and(|t| t.is_string()) {
                return crate::strings::lower_string_inside(cx, left_expr, candidates);
            }
            let left = match lower_expr(cx, left_expr)? {
                LoweredValue::Real(r) => {
                    return crate::real::lower_real_inside(cx, r, candidates);
                }
                other => other.into_packed("inside on a non-integral left operand", &cx.fn_sym)?,
            };
            if candidates.is_empty() {
                return Err(LoweringError::unsupported_at(
                    "inside with an empty candidate list",
                    &cx.fn_sym,
                ));
            }
            let mut acc: Option<PackedValue<'ctx>> = None;
            for cand in candidates {
                let contrib = inside_candidate(cx, left, cand)?;
                acc = Some(match acc {
                    None => contrib,
                    Some(prev) => op.or4(prev, contrib),
                });
            }
            Ok(LoweredValue::Packed(
                acc.expect("candidates non-empty, checked above"),
            ))
        }
        // Task 17 (CG-022): bit-select (`v[i]`) -- closed-form shift+
        // truncate over both lanes (`OpBuilder::bit_select4`), never a
        // per-bit loop. `select_value()`/`selector()` give the base vector
        // and the (possibly dynamic, possibly X/Z-containing, possibly
        // out-of-range) index expression respectively; every LRM SS11.5.1
        // failure mode (unknown index, out-of-range index) is folded into
        // `bit_select4`'s own closed-form formula, not special-cased here.
        //
        // Scope note (documented limitation, consistent with this crate's
        // existing width model -- `types.rs::lower_type` already treats
        // every integral `Type` as a flat `bit_width()`-wide value with no
        // declared-bound-direction concept anywhere else in this crate
        // either): the index is read here as a direct PHYSICAL bit offset
        // from the value's own LSB (bit 0), which is only correct for the
        // common canonical `[N-1:0]`-declared vector. A non-canonical
        // ascending declared range (`logic [0:7] x; x[3]`) would need the
        // declared lower bound threaded through from `sv_lang::Type` to
        // remap the logical index to a physical offset -- genuinely out of
        // this task's scope (no other operator in this crate consults
        // declared-bound direction either).
        ExpressionKind::ElementSelect => {
            // An element of a queue, dynamic or associative array.
            if let Some(r) = crate::collections::read_element_warning(cx, e) {
                return r;
            }
            if let Some(r) = crate::collections::read_fixed_in_coll(cx, e) {
                return r;
            }
            let base_expr = e.select_value().ok_or_else(|| {
                LoweringError::unsupported_at("bit-select missing base value", &cx.fn_sym)
            })?;
            // `s[i]` on a string: its i-th character.
            if base_expr.expr_type().is_some_and(|t| t.is_string()) {
                let idx = e.selector().ok_or_else(|| {
                    LoweringError::unsupported_at("string index missing", &cx.fn_sym)
                })?;
                return crate::strings::lower_getc(cx, base_expr, idx);
            }
            // `vif.a[i]` (`crate::vif`).
            if let Some(v) = crate::vif::read_element(cx, e) {
                return v;
            }
            // An element of a named constant `string` or `real` array.
            if let Some(v) = crate::arrays::read_constant_unpacked_element(cx, e)? {
                return Ok(v);
            }
            // An unpacked-array element (`mem[i]`, `a[i][j]`, a local
            // array's element): `crate::arrays`, never a bit-select.
            if let Some(r) = crate::arrays::resolve_element(cx, e)? {
                return Ok(LoweredValue::Packed(crate::arrays::read_element(cx, &r)?));
            }
            let base = lower_expr(cx, base_expr)?
                .into_packed("bit-select on a non-integral (real) base value", &cx.fn_sym)?;
            // Declared-range-aware (`select.rs`): `v[i]` on `logic [8:1] v`,
            // `logic [0:7] v`, or a packed 2-D `logic [3:0][7:0] v` (an
            // 8-bit element) all address the right physical bits.
            let geom = crate::select::geom_of(base_expr.expr_type(), base.width);
            let window = crate::select::select_window(cx, e, base.width, geom)?;
            let mut out = crate::select::read_window(&op, base, window);
            out.signed = e.expr_type().is_some_and(|t| t.is_signed());
            Ok(LoweredValue::Packed(crate::select::two_state_result(
                &op, e, out,
            )))
        }
        // Task 17 (CG-022): part-select -- `v[l:r]` (Simple, both bounds
        // compile-time constant), `v[base +: w]` (IndexedUp), `v[base -: w]`
        // (IndexedDown, `w` compile-time constant in both indexed forms).
        // All three modes route through the single shared closed-form
        // `OpBuilder::part_select4` window-extraction primitive; only the
        // (constant vs. dynamic) `lo`/`lo_unknown` computation differs per
        // mode, delegated to `OpBuilder::{indexed_up_lo4,indexed_down_lo4}`
        // for the two dynamic-base forms. Same physical-offset scope note
        // as `ElementSelect` above.
        ExpressionKind::RangeSelect => {
            let base_expr = e.select_value().ok_or_else(|| {
                LoweringError::unsupported_at("part-select missing base value", &cx.fn_sym)
            })?;
            let base = lower_expr(cx, base_expr)?.into_packed(
                "part-select on a non-integral (real) base value",
                &cx.fn_sym,
            )?;
            // Declared-range-aware window (`select.rs`); a constant
            // out-of-range simple select reads X.
            let geom = crate::select::geom_of(base_expr.expr_type(), base.width);
            let window = crate::select::select_window(cx, e, base.width, geom)?;
            let out = crate::select::read_window(&op, base, window);
            Ok(LoweredValue::Packed(crate::select::two_state_result(
                &op, e, out,
            )))
        }
        // Task 17 (CG-022): concatenation `{a, b, ...}` -- closed-form
        // shift+or assembly over each operand's own already-lowered val/unk
        // lanes (`OpBuilder::concat4`), MSB-operand-first per SV ordering
        // (`Expression::operands()` already yields the operand list in
        // slang's own source order, which for `{a, b}` is `[a, b]` with `a`
        // as the MSB-most operand -- verified against the vendored sv-lang
        // 0.1.9 C++ source, `ConcatenationExpression::operands()`). A loop
        // over the (small, compile-time-fixed) OPERAND COUNT here is not
        // the "runtime loop over bit width" CG-022 forbids -- each operand
        // becomes exactly one shift+or pair in the emitted IR regardless of
        // how wide that operand's own type is (see
        // `closed_form_no_bitwidth_loop_in_emitted_ir`, tests/
        // select_concat.rs).
        ExpressionKind::Concatenation => {
            let operand_exprs = e.operands();
            if operand_exprs.is_empty() {
                return Err(LoweringError::unsupported_at(
                    "concatenation with no operands (e.g. an empty queue `{}`) is out of scope",
                    &cx.fn_sym,
                ));
            }
            // A zero-count replication operand contributes nothing (LRM
            // 1800-2023 §11.4.12.1); slang requires a positive-size sibling.
            // The dropped operand's own side effects still run (`{0{f()}}`
            // calls `f` once, matching iverilog and ivtest concat3/concat4),
            // in source order; its value is discarded.
            let mut kept = Vec::with_capacity(operand_exprs.len());
            for o in operand_exprs {
                if is_zero_replication(&o) {
                    if let Some(inner) = o.replication_concat() {
                        let _ = lower_expr(cx, inner)?;
                    }
                } else {
                    kept.push(o);
                }
            }
            let operand_exprs = kept;
            let mut operands = Vec::with_capacity(operand_exprs.len());
            for oe in operand_exprs {
                operands.push(lower_expr(cx, oe)?.into_packed(
                    "concatenation operand must itself lower to an integral value",
                    &cx.fn_sym,
                )?);
            }
            Ok(LoweredValue::Packed(op.concat4(&operands)))
        }
        // Task 17 (CG-022): replication `{n{x}}` -- LRM 1800-2023 SS11.4.12
        // requires `n` to be a compile-time constant (slang's own
        // elaboration already enforces this -- `ReplicationExpression`'s
        // `count()` is only ever reachable here already const-folded, same
        // discipline as `BinaryOp::Power`'s const-exponent fast path
        // above). `replication_concat()` is itself always an
        // `ExpressionKind::Concatenation` node (verified against the
        // vendored sv-lang 0.1.9 C++ source,
        // `ReplicationExpression::fromSyntax`: the syntax grammar's
        // multiplier-brace body is always a nested `{...}` concatenation,
        // even for a single element like `{3{2'b01}}`), so lowering it
        // through the ordinary `Concatenation` arm above via recursive
        // `lower_expr` already produces the correctly-assembled "unit" to
        // repeat -- `OpBuilder::replicate4` is then just `concat4` of that
        // unit repeated `n` times, closed-form over the (compile-time-
        // fixed) `n`, never a per-bit loop.
        //
        // A count of 0 is only legal per LRM when NESTED inside another
        // concatenation (`{a, {0{b}}}`, contributing zero width), and the
        // `Concatenation` arm drops such operands ([`is_zero_replication`]).
        // A standalone `{0{x}}` is already a hard compile-time error
        // upstream (slang's own `ReplicationZeroOutsideConcat` diagnostic),
        // so this arm's own `count <= 0` check below is a defense-in-depth
        // backstop -- a loud `LoweringError`, never a silent zero-width or
        // negative-width result.
        ExpressionKind::Replication => {
            let count_expr = e.replication_count().ok_or_else(|| {
                LoweringError::unsupported_at("replication missing count", &cx.fn_sym)
            })?;
            let count_i64 = require_const_i64(
                count_expr,
                "replication count must be a compile-time constant",
                &cx.fn_sym,
            )?;
            if count_i64 <= 0 {
                return Err(LoweringError::unsupported_at(
                    "replication with a non-positive count (a nested zero-count \
                     replication contributing zero width to an enclosing \
                     concatenation is out of scope)",
                    &cx.fn_sym,
                ));
            }
            let unit_expr = e.replication_concat().ok_or_else(|| {
                LoweringError::unsupported_at("replication missing concat operand", &cx.fn_sym)
            })?;
            let unit = lower_expr(cx, unit_expr)?.into_packed(
                "replication operand must itself lower to an integral value",
                &cx.fn_sym,
            )?;
            Ok(LoweredValue::Packed(op.replicate4(unit, count_i64 as u32)))
        }
        // Task 7 (CG-034, CG-035, CG-036, CG-038, CG-075): every operand
        // slang has resized/reinterpreted (assignment/return coercion,
        // operand equalization, explicit `signed'()`/`8'()`-style casts)
        // arrives wrapped in one of these -- CG-038: this arm trusts the
        // node's own `expr_type()`/`conversion_operand()` verbatim and
        // never re-derives a width/sign rule of its own from the raw
        // operand type.
        ExpressionKind::Conversion => {
            // A `const'(expr)` of a procedural concurrent assertion (16.14.6):
            // the value the process captured when it reached the assertion,
            // primed under the cast's synthetic key by the assertion's host.
            if e.conversion_is_const_cast() == Some(true)
                && let Some((slot, width, signed)) = cx
                    .locals
                    .get(&crate::sva::proc::const_cast_key(&e))
                    .copied()
            {
                let ty = if cx.repr == ValueRepr::FourState {
                    op.i_n(width * 2)
                } else {
                    op.i_n(width)
                };
                let v =
                    cx.b.build_load(ty, slot, "ld_const")
                        .unwrap()
                        .into_int_value();
                return Ok(LoweredValue::Packed(PackedValue {
                    llvm: v,
                    width,
                    repr: cx.repr,
                    signed,
                }));
            }
            let operand_expr = e.conversion_operand().ok_or_else(|| {
                LoweringError::unsupported_at("conversion missing operand", &cx.fn_sym)
            })?;
            if let Some(v) = crate::events::lower_event_truth(cx, e)? {
                return Ok(v);
            }
            // A string converted to an integral type: its bytes (LRM 6.16).
            if operand_expr.expr_type().is_some_and(|t| t.is_string())
                && let Some(to) = e.expr_type()
                && to.is_integral()
            {
                let s = crate::strings::to_string(cx, lower_expr_h(cx, operand_expr)?)?;
                return Ok(LoweredValue::Packed(crate::strings::to_bits(
                    cx,
                    s,
                    to.value_width(),
                    to.is_signed(),
                )?));
            }
            let kind = e.conversion_kind().ok_or_else(|| {
                LoweringError::unsupported_at("conversion missing conversion_kind", &cx.fn_sym)
            })?;
            match classify_conversion_kind(kind) {
                Ok(ConversionAction::Value) => {}
                Ok(ConversionAction::Stream) => {
                    return crate::stream::lower_stream_conversion(cx, e);
                }
                Ok(ConversionAction::BitstreamCast) => {
                    return crate::stream::lower_bitstream_cast(cx, e);
                }
                Err(msg) => return Err(LoweringError::unsupported_at(&msg, &cx.fn_sym)),
            }

            let to_ty = e.expr_type().ok_or_else(|| {
                LoweringError::unsupported_at("conversion missing result type", &cx.fn_sym)
            })?;
            let from_ty = operand_expr.expr_type().ok_or_else(|| {
                LoweringError::unsupported_at("conversion operand missing type", &cx.fn_sym)
            })?;

            // Task 8 (CG-037): dispatch on the (from, to) floating-ness
            // pair BEFORE the int<->int path below, exactly like CG-038's
            // discipline of trusting only this node's own `expr_type()`/
            // `conversion_operand()` -- never re-deriving a rule from the
            // raw operand.
            match (from_ty.is_floating(), to_ty.is_floating()) {
                (false, true) => {
                    // IntToFloat (CG-037): `sitofp`/`uitofp` per the
                    // OPERAND's own signedness (mirrors CG-035's "the value
                    // being converted decides", not the target) -- a real
                    // LLVM float-conversion instruction, never a bitcast
                    // (int_to_real_cast_uses_float_conversion_instruction,
                    // tests/real.rs).
                    if !from_ty.is_integral() {
                        return Err(LoweringError::unsupported_at(
                            &format!(
                                "conversion from non-integral, non-floating type {} to real",
                                from_ty.to_sv_string()
                            ),
                            &cx.fn_sym,
                        ));
                    }
                    let from_pv = lower_expr(cx, operand_expr)?.into_packed(
                        "int-to-real conversion operand must itself lower to an integral value",
                        &cx.fn_sym,
                    )?;
                    let float_ty = match lower_type(cx.ctx, &to_ty, cx.repr)? {
                        LoweredType::Float(t) => t,
                        LoweredType::Int(_) => {
                            unreachable!("to_ty.is_floating() was just checked true")
                        }
                    };
                    // CG-034-style flatten: real has no unknown plane, so
                    // any X/Z bit in the operand must resolve to a
                    // definite 0 (mask-then-truncate) before the float
                    // conversion, never survive as a spurious bit pattern.
                    let flat = op.to_2state4(from_pv);
                    let raw = op.lane_val(flat);
                    let float_v = if from_pv.signed {
                        cx.b.build_signed_int_to_float(raw, float_ty, "sitofp")
                            .unwrap()
                    } else {
                        cx.b.build_unsigned_int_to_float(raw, float_ty, "uitofp")
                            .unwrap()
                    };
                    // Every computed real is an f64 (`crate::real`).
                    Ok(LoweredValue::Real(RealValue {
                        llvm: crate::real::as_f64(cx, float_v),
                    }))
                }
                (true, false) => {
                    // FloatToInt (CG-037, BINDING CORRECTION 2): the
                    // IMPLICIT real->int Conversion node ROUNDS to nearest,
                    // ties away from zero -- NEVER truncates (`$rtoi`'s
                    // rule; see `rtoi_is_a_distinct_unimplemented_path_
                    // not_the_conversion_node`, tests/real.rs, which
                    // guards this exact node reusing `$rtoi`'s -- currently
                    // nonexistent -- lowering).
                    if !to_ty.is_integral() {
                        return Err(LoweringError::unsupported_at(
                            &format!(
                                "conversion from real to non-integral, non-floating type {}",
                                to_ty.to_sv_string()
                            ),
                            &cx.fn_sym,
                        ));
                    }
                    let from_rv = lower_expr(cx, operand_expr)?.into_real(
                        "real-to-int conversion operand must itself lower to a real value",
                        &cx.fn_sym,
                    )?;
                    let to_width = to_ty.value_width();
                    // A NaN or infinite real has no integer value: a 4-state
                    // destination takes all-x (the established simulator
                    // behaviour, ivtest cast_int; LRM 6.12.2 leaves it
                    // unspecified), a 2-state one 0. `v - v` is NaN exactly
                    // for NaN/inf; the operand is zeroed first so no LLVM
                    // poison is ever formed.
                    let fty = from_rv.llvm.get_type();
                    let diff =
                        cx.b.build_float_sub(from_rv.llvm, from_rv.llvm, "rti_vmv")
                            .unwrap();
                    let bad = cx
                        .b
                        .build_float_compare(inkwell::FloatPredicate::UNO, diff, diff, "rti_bad")
                        .unwrap();
                    let safe =
                        cx.b.build_select(bad, fty.const_zero(), from_rv.llvm, "rti_safe")
                            .unwrap()
                            .into_float_value();
                    let rounded =
                        op.round_real_to_int(RealValue { llvm: safe }, to_width, to_ty.is_signed());
                    let wty = op.i_n(to_width);
                    let unk = if to_ty.is_four_state() {
                        cx.b.build_select(bad, wty.const_all_ones(), wty.const_zero(), "rti_unk")
                            .unwrap()
                            .into_int_value()
                    } else {
                        wty.const_zero()
                    };
                    Ok(LoweredValue::Packed(op.pack(
                        rounded,
                        unk,
                        to_width,
                        cx.repr,
                        to_ty.is_signed(),
                    )))
                }
                (true, true) => {
                    // real <-> shortreal: rounded to the target's precision
                    // (kept as an f64, `crate::real`).
                    let rv = lower_expr(cx, operand_expr)?
                        .into_real("real conversion of a non-real value", &cx.fn_sym)?;
                    let f = crate::real::as_f64(cx, rv.llvm);
                    Ok(LoweredValue::Real(RealValue {
                        llvm: if to_ty.bit_width() == 32 {
                            crate::real::round_to_short(cx, f)
                        } else {
                            f
                        },
                    }))
                }
                (false, false) => {
                    let from = lower_expr(cx, operand_expr)?.into_packed(
                        "int<->int conversion operand must itself lower to an integral value",
                        &cx.fn_sym,
                    )?;

                    // CG-034: flatten 4-state -> 2-state (mask-then-truncate,
                    // `known_val = val & ~unk`) BEFORE any width adjustment,
                    // exactly when the target is 2-state and the source is
                    // 4-state (a same-width `logic`->`bit` cast is the minimal
                    // case; slang emits this as its own single-step Conversion
                    // node, verified against the vendored sv-lang 0.1.9 -- a
                    // combined width-change-and-flatten source expression instead
                    // decomposes into a nested pair of Conversion nodes, which
                    // `lower_expr`'s ordinary recursion into `conversion_operand()`
                    // already composes correctly with no special-casing needed
                    // here).
                    let flattened = if from_ty.is_four_state() && !to_ty.is_four_state() {
                        op.to_2state4(from)
                    } else {
                        from
                    };

                    // CG-035 (BINDING CORRECTION 1): width-adjust using the
                    // OPERAND's OWN signedness for the extension fill (LRM
                    // 11.6.1 -- sign- vs zero-extension depends on the value
                    // being extended, not the target). CG-038: `to_width` comes
                    // only from the Conversion node's own `expr_type()`.
                    let to_width = to_ty.value_width();
                    let from_width = from_ty.value_width();
                    let width_adjusted = match to_width.cmp(&from_width) {
                        std::cmp::Ordering::Greater => {
                            // A `Propagated` conversion (kind 1) is the
                            // operand of a context-determined operator
                            // being widened to the operator's type: LRM
                            // 11.8.2 sign-extends it only if THAT type is
                            // signed, so a signed operand of a mixed-sign
                            // (hence unsigned) expression is zero-extended.
                            let fill = if kind == 1 {
                                to_ty.is_signed()
                            } else {
                                flattened.signed
                            } || unsized_literal_leads_unknown(operand_expr);
                            op.extend4(flattened, to_width, fill)
                        }
                        std::cmp::Ordering::Less => op.truncate4(flattened, to_width),
                        std::cmp::Ordering::Equal => flattened,
                    };

                    // CG-036: sign conversion changes only the interpretation used
                    // by later arithmetic/relational lowering -- every bit above
                    // is already final; relabel `signed` to the Conversion node's
                    // own result type without touching the `IntValue`.
                    Ok(LoweredValue::Packed(PackedValue {
                        signed: to_ty.is_signed(),
                        ..width_adjusted
                    }))
                }
            }
        }
        // Task 13 (CG-042): constant-fold pure/static system functions
        // ($bits, $size, type queries) entirely at compile time. sv-lang's
        // own prefold sweep (`Options::with_prefold`, which defaults to
        // `true` -- `Compilation::new`/`frontend::compile_source` never
        // override it) already evaluates every statically-foldable
        // expression during elaboration and caches the result, so this arm
        // does not re-implement $bits'/$size's own type-query semantics: it
        // only has to recognize a `Call` node that IS a system call, whose
        // `known_name_id()` names one of CG-042's own type-query subset
        // (`is_cg042_type_query`, below -- the "same `known_name_id()`
        // system-call dispatch point Task 30 later extends for
        // `$display`"), AND already carries a cached `constant_value()`,
        // and emit that constant directly via the same `constant_value()`
        // -> `as_integer()` -> `const_svint()` path the `IntegerLiteral` arm
        // above already uses -- zero runtime call instruction, the exact
        // CG-042 acceptance.
        //
        // Deliberately NOT dispatched here, restricted by the explicit
        // `is_cg042_type_query` allowlist rather than "any system call that
        // happens to have a cached constant": `$rtoi`/`$itor`/`$clog2`/the
        // real<->bits reinterpretation family/the math-library functions
        // are all *sometimes* compile-time-constant too (e.g. `$rtoi(3.9)`
        // on a literal argument) but are NOT "type queries" -- CG-042's
        // ledger requirement names only `$bits`, `$size`, and "type
        // queries" (LRM 1800-2023 SS20.6.1/SS20.7's array-query family:
        // `$low`/`$high`/`$left`/`$right`/`$increment`/`$dimensions`/
        // `$unpacked_dimensions`). Folding those other, unrelated
        // system-function families here would be scope creep AND would
        // silently break `rtoi_is_a_distinct_unimplemented_path_not_the_
        // conversion_node` (tests/real.rs, pinned by BINDING CORRECTION 2:
        // `$rtoi` must stay a distinct, not-yet-implemented path even
        // though it can be constant). Every other case -- a user-defined
        // subroutine call, a system call outside the allowlist, or an
        // allowlisted call that genuinely isn't constant (a dynamic `$size`
        // on a dynamic array) -- is a loud, named `LoweringError`, never a
        // silent fallback or a panic; the "not constant" and "not in
        // CG-042's allowlist" cases are deliberately distinct messages so a
        // failure is diagnosable rather than lumped into one generic
        // "unsupported call" bucket.
        // R-run Task 10 (`docs/superpowers/plans/2026-09-27-run-wiring.md`;
        // CG-061 reused, CG-042 deliberately untouched): a `Call` expression
        // that is NOT a system call used to be an unconditional rejection
        // here ("user subroutine calls are out of Task 13's/CG-042's
        // scope") -- generalized to resolve an ordinary (non-DPI) SV
        // `function`/`task` call via `lower_user_subroutine_call`, below.
        // See that function's own doc comment for the DPI-import-vs-
        // genuinely-unsupported split and the automatic/non-suspending
        // scope restriction (CG-154).
        ExpressionKind::Call => {
            if e.is_system_call() != Some(true) {
                return lower_user_subroutine_call(cx, e);
            }
            let sub = e.system_subroutine().ok_or_else(|| {
                LoweringError::unsupported_at(
                    "system call missing its SystemMethod handle",
                    &cx.fn_sym,
                )
            })?;
            let name = sub.name();
            if sub.known_name_id() == KnownSystemName::Cast {
                return crate::sysfunc::lower_cast(cx, e);
            }
            // A system task or function a VPI library registered (36.3).
            if let Some(v) = crate::vpi_systf::lower_call(cx, e, &name)? {
                return Ok(v);
            }
            // Coverage (19.9, 20.13): `$get_coverage` and friends.
            if matches!(
                sub.known_name_id(),
                KnownSystemName::CoverageControl
                    | KnownSystemName::CoverageGetMax
                    | KnownSystemName::CoverageGet
                    | KnownSystemName::CoverageMerge
                    | KnownSystemName::CoverageSave
                    | KnownSystemName::GetCoverage
                    | KnownSystemName::SetCoverageDbName
                    | KnownSystemName::LoadCoverageDb
            ) {
                let args: Vec<sv_lang::Expression<'d>> = e
                    .children()
                    .into_iter()
                    .filter_map(|n| n.as_expression())
                    .collect();
                if let Some(v) = crate::covergroup::lower_system_call(cx, &name, &args)? {
                    return Ok(v);
                }
            }
            // `randomize` takes a collection as its first argument without
            // being a collection method: dispatch it before the collection
            // methods below claim it (`std::randomize(q)` is then refused by
            // name in `scope_randomize`).
            if sub.known_name_id() == KnownSystemName::Randomize {
                return crate::classes_randomize::lower_randomize_call(cx, e);
            }
            // An array manipulation method (`sum`, `find`, `sort`, ...) or
            // an iterator's `index`.
            if let Some(r) = crate::array_methods::lower(cx, sub.known_name_id(), e) {
                return r;
            }
            // A queue / dynamic / associative array method or `$size`.
            if let Some(r) = crate::collections::lower_method(cx, sub.known_name_id(), e) {
                return r;
            }
            if sub.known_name_id() == KnownSystemName::CountDrivers {
                return lower_countdrivers_call(cx, e);
            }
            // LRM 20.15 stochastic analysis tasks and `$q_full`.
            if matches!(
                sub.known_name_id(),
                KnownSystemName::QInitialize
                    | KnownSystemName::QAdd
                    | KnownSystemName::QRemove
                    | KnownSystemName::QFull
                    | KnownSystemName::QExam
            ) {
                return lower_stoch_queue_call(cx, e, sub.known_name_id());
            }
            // Task F2 (RT-043 partial): `$time`/`$stime`/`$realtime` are
            // system FUNCTIONS (so they belong at this `expr.rs` call site,
            // like CG-042's type queries below), but -- unlike every one of
            // those -- their value is never compile-time constant (it is the
            // live scheduler tick count), so they must be dispatched BEFORE
            // the `is_cg042_type_query` allowlist / `constant_value()` check
            // rather than routed through either: they always lower to a real
            // runtime CALL, never a fold.
            if matches!(
                sub.known_name_id(),
                KnownSystemName::Time | KnownSystemName::STime | KnownSystemName::RealTime
            ) {
                let scope = e.system_call_scope().unwrap_or(cx.fn_sym);
                return lower_time_system_call(cx, sub.known_name_id(), &name, &scope);
            }
            // LRM 20.4.1 `$timeunit`/`$timeprecision`: the scale is static, so
            // the call is a signed 32-bit constant (the exponent, `-9` for 1ns).
            if name == "$timeunit" || name == "$timeprecision" {
                return lower_timescale_query(cx, e, &name);
            }
            // LRM 20.15.2 `$dist_*`: a runtime call, never a fold.
            if let Some(kind) = dist_kind(&name) {
                return lower_dist_system_call(cx, kind, &name, e);
            }
            // B-runtime Task 6 (RT-078..RT-082): `$random`/`$urandom`/
            // `$urandom_range` are, like `$time` above, system FUNCTIONS
            // whose value is never compile-time constant (every call must
            // genuinely draw from the runtime PRNG) -- dispatched here,
            // before the CG-042 type-query allowlist / `constant_value()`
            // check, for the identical reason `$time` is.
            if matches!(
                sub.known_name_id(),
                KnownSystemName::Random | KnownSystemName::URandom | KnownSystemName::URandomRange
            ) {
                return lower_random_system_call(cx, sub.known_name_id(), e);
            }
            // B-runtime Task 7 (RT-050/RT-051): `$fopen(path, mode)` is,
            // like `$time`/`$random` above, a system FUNCTION whose value
            // can never be compile-time-folded (every call must reach the
            // real, mutable MCD table) -- dispatched here, before the
            // CG-042 type-query allowlist, for the identical reason those
            // two are.
            if sub.known_name_id() == KnownSystemName::FOpen {
                return lower_fopen_call(cx, e);
            }
            if sub.known_name_id() == KnownSystemName::System {
                return lower_system_call(cx, e);
            }
            // B-runtime Task 9 (RT-061/RT-063): `$fseek`/`$feof`, the same
            // "system FUNCTION, never compile-time-foldable" dispatch as
            // `$fopen` immediately above (see `lower_fseek_call`'s own doc
            // comment for the scope note on why these two, and only these
            // two, of this task's read-side calls are wired here).
            if sub.known_name_id() == KnownSystemName::FSeek {
                return lower_fseek_call(cx, e);
            }
            if sub.known_name_id() == KnownSystemName::FEof {
                return lower_feof_call(cx, e);
            }
            // `$inferred_disable` in a checker's assertion expression (17.4).
            if let Some(r) = crate::sva::emit::lower_inferred_disable(cx, sub.known_name_id()) {
                return r;
            }
            // Sampled value functions ($sampled/$rose/$past/..., 16.9.3).
            if let Some(r) = crate::sampled::lower_sampled_call(cx, sub.known_name_id(), e) {
                return r;
            }
            if sub.known_name_id() == KnownSystemName::Triggered {
                return lower_event_triggered(cx, e);
            }
            if let Some(r) = crate::fileio::lower_file_read_sysfunc(cx, sub.known_name_id(), e) {
                return r;
            }
            if let Some(r) = crate::real::lower_real_sysfunc(cx, sub.known_name_id(), e) {
                return r;
            }
            if let Some(r) = crate::strings::lower_string_sysfunc(cx, sub.known_name_id(), e) {
                return r;
            }
            if let Some(r) = crate::sysfunc::lower_bit_sysfunc(cx, sub.known_name_id(), e) {
                return r;
            }
            if let Some(r) = crate::sysfunc::lower_enum_method(cx, sub.known_name_id(), e) {
                return r;
            }
            // B-runtime Task 11 (RT-074/RT-075/RT-076/RT-077):
            // `$test$plusargs`/`$value$plusargs` are, like `$fopen`/
            // `$fseek`/`$feof` above, system FUNCTIONS whose value can
            // never be compile-time-folded (every call must reach the real
            // process argv) -- dispatched here, before the CG-042
            // type-query allowlist, for the identical reason.
            if sub.known_name_id() == KnownSystemName::TestPlusArgs {
                return lower_test_plusargs_call(cx, e);
            }
            if sub.known_name_id() == KnownSystemName::ValuePlusArgs {
                return lower_value_plusargs_call(cx, e);
            }
            // Constraints plan C3.5: `obj.randomize()` is a system call that
            // is never constant-foldable; it dispatches through the object's
            // dynamic class to the constraint solver.
            if sub.known_name_id() == KnownSystemName::Randomize {
                return crate::classes_randomize::lower_randomize_call(cx, e);
            }
            // The built-in randomization-control methods (LRM 18.8, 18.9),
            // constraints plan C20: `obj.v.rand_mode(..)`, `obj.c.constraint_mode(..)`.
            if sub.known_name_id() == KnownSystemName::RandMode {
                return crate::rand_mode::lower_rand_mode_call(cx, e);
            }
            if sub.known_name_id() == KnownSystemName::ConstraintMode {
                return crate::rand_mode::lower_constraint_mode_call(cx, e);
            }
            if !is_cg042_type_query(sub.known_name_id()) {
                return Err(LoweringError::unsupported_at(
                    &format!(
                        "system call {name} is not one of CG-042's constant- \
                         foldable type-query functions ($bits/$size/$low/ \
                         $high/$left/$right/$increment/$dimensions/ \
                         $unpacked_dimensions) -- a runtime-behavior system \
                         call (e.g. $display/$random) is CG-043's scope, and \
                         a different constant-sometimes-foldable family (e.g. \
                         $rtoi/$itor/$clog2) is a separate, not-yet- \
                         implemented path, not Task 13's"
                    ),
                    &cx.fn_sym,
                ));
            }
            // `$bits(u1.A)`: slang folds no value through a hierarchical
            // reference, but the argument's type is elaborated, so a
            // fixed-size data type has an exact bit count (LRM 20.6.2).
            if e.constant_value().is_none()
                && sub.known_name_id() == KnownSystemName::Bits
                && let Some(arg) = e.children().into_iter().find_map(|n| n.as_expression())
                && let Some(t) = arg.expr_type()
                && t.is_fixed_size()
                && t.bitstream_width() > 0
                && t.bitstream_width() <= u32::MAX as u64
            {
                let w = t.bitstream_width() as u32;
                let bits: Vec<sv_lang::Bit> = (0..32)
                    .map(|i| {
                        if (w >> i) & 1 == 1 {
                            sv_lang::Bit::One
                        } else {
                            sv_lang::Bit::Zero
                        }
                    })
                    .collect();
                return Ok(LoweredValue::Packed(crate::value::const_bits(
                    cx.ctx, &bits, false, cx.repr,
                )));
            }
            let cv = e.constant_value().ok_or_else(|| {
                LoweringError::unsupported_at(
                    &format!(
                        "type-query system call {name} is not a compile-time \
                         constant here (e.g. $size on a genuinely dynamic \
                         array) -- CG-042 only constant-folds statically- \
                         foldable calls"
                    ),
                    &cx.fn_sym,
                )
            })?;
            let iv = cv.as_integer().ok_or_else(|| {
                LoweringError::unsupported_at(
                    &format!("system call {name}'s folded constant is not an integer"),
                    &cx.fn_sym,
                )
            })?;
            // Review fix r2: same `lower_const_any` dispatch as the
            // `IntegerLiteral` arm above -- this is CG-042's own constant-
            // folding path, and its folded constant is just as capable of
            // being a >WIDE_THRESHOLD-bit `SVInt` (e.g. `$bits` on a wide
            // packed type) as a literal is.
            lower_constant(cx, iv, Some(e))
        }
        // Task 9 test-support: minimal scalar `=` assignment-to-local
        // lowering. Not itself a CG-090/CG-091/CG-007 requirement, but
        // necessary infrastructure this task's own tests need to observe a
        // runtime side effect (per the plan's own fallback: "substitute an
        // assignment-to-local as the side effect" for the &&/|| short-
        // circuit regressions, since task/system calls (Task 12/26) don't
        // exist yet -- see this crate's Task 9 deviations). Deliberately
        // narrow and loud about anything outside that scope: a compound
        // assignment (`+=`, ...), a nonblocking assignment (`<=`), or a
        // non-plain-variable lvalue (bit-select, concat, ...) is a typed
        // `LoweringError`, never silently accepted or mishandled.
        ExpressionKind::Assignment => {
            // `a op= b`: slang elaborates the RHS as `<LValueReference> op
            // b`; the reference reads the target's current value.
            if e.is_compound_assignment() == Some(true) {
                let lhs_expr = e.left().ok_or_else(|| {
                    LoweringError::unsupported_at("assignment missing lvalue", &cx.fn_sym)
                })?;
                let rhs_expr = e.right().ok_or_else(|| {
                    LoweringError::unsupported_at("assignment missing rvalue", &cx.fn_sym)
                })?;
                // An integral unpacked-array element target resolves ONCE
                // (LRM 11.4.1: `a op= b` is `a = a op b` with `a` evaluated
                // once, so `x[i++] += 2` bumps `i` a single time).
                if lhs_expr.kind() == ExpressionKind::ElementSelect
                    && lhs_expr.expr_type().is_some_and(|t| t.is_integral())
                    && let Some(r) = crate::arrays::resolve_element(cx, lhs_expr)?
                {
                    let current = LoweredValue::Packed(crate::arrays::read_element(cx, &r)?);
                    let saved = cx.lvalue_ref.replace(Some(current));
                    let rhs = lower_expr(cx, rhs_expr);
                    cx.lvalue_ref.set(saved);
                    let rhs_pv = rhs?.into_packed(
                        "compound assignment of a non-integral value to an array element",
                        &cx.fn_sym,
                    )?;
                    let rhs_pv = crate::select::fit_width(&op, rhs_pv, r.width);
                    crate::arrays::write_element(cx, &r, rhs_pv)?;
                    return Ok(LoweredValue::Packed(rhs_pv));
                }
                // A packed select / field target with a side-effecting index
                // (`x[i++] += 1`): one resolution, as above.
                if crate::classes::has_side_effects(lhs_expr)
                    && lhs_expr.expr_type().is_some_and(|t| t.is_integral())
                    && let Some(v) = crate::select::lower_modify_target(cx, lhs_expr, &mut |old| {
                        let saved = cx.lvalue_ref.replace(Some(LoweredValue::Packed(old)));
                        let rhs = lower_expr(cx, rhs_expr);
                        cx.lvalue_ref.set(saved);
                        rhs?.into_packed("compound assignment of a non-integral value", &cx.fn_sym)
                    })?
                {
                    return Ok(LoweredValue::Packed(v));
                }
                let current = read_modify_target(cx, lhs_expr)?;
                let saved = cx.lvalue_ref.replace(Some(current));
                let rhs = lower_expr(cx, rhs_expr);
                cx.lvalue_ref.set(saved);
                return store_to(cx, lhs_expr, rhs?);
            }
            if e.is_nonblocking() == Some(true) {
                return crate::func::lower_subroutine_nba(cx, e);
            }
            let lhs_expr = e.left().ok_or_else(|| {
                LoweringError::unsupported_at("assignment missing lvalue", &cx.fn_sym)
            })?;
            // A modport port with an expression writes that expression
            // (LRM 25.5.4).
            let lhs_expr = modport_expr_target(&lhs_expr).unwrap_or(lhs_expr);
            // `e1 = e2`, `e = null` of events (LRM 1800-2023 15.5.5.1).
            if lhs_expr.expr_type().is_some_and(|t| t.is_event()) {
                let rhs_expr = e.right().ok_or_else(|| {
                    LoweringError::unsupported_at("assignment missing rvalue", &cx.fn_sym)
                })?;
                return crate::events::lower_event_assignment(cx, lhs_expr, rhs_expr);
            }
            // `{<<n{a, b}} = q`: a queue, dynamic array or string source
            // has a runtime length (LRM 1800-2023 11.4.14.3).
            if lhs_expr.kind() == ExpressionKind::Streaming
                && let Some(rhs_expr) = e.right()
                && (crate::stream::source_needs_runtime(rhs_expr)
                    || crate::stream::target_needs_runtime(lhs_expr))
            {
                crate::stream::unpack_runtime_source(
                    cx,
                    lhs_expr,
                    rhs_expr,
                    &mut |operand, with, bits| {
                        if let Some(w) = with {
                            return crate::stream_with::unpack(cx, operand, w, bits);
                        }
                        store_to(cx, operand, LoweredValue::Packed(bits)).map(|_| ())
                    },
                    None,
                    None,
                )?;
                return Ok(LoweredValue::Packed(op.pack(
                    cx.ctx.bool_type().const_zero(),
                    cx.ctx.bool_type().const_zero(),
                    1,
                    cx.repr,
                    false,
                )));
            }
            // Classes plan Task 5 (§1.4 step 4): `obj.field = v` or a bare,
            // implicit-`this` `field = v` -- this arm's own blocking-
            // assignment path has its OWN plain-`NamedValue`-to-`cx.locals`
            // shortcut further down (never reached via `store_to`, unlike
            // every call site below this one), so the SAME class-field
            // intercept `store_to` itself carries is repeated here too.
            if crate::classes::is_class_field_lvalue(lhs_expr) {
                let rhs_expr = e.right().ok_or_else(|| {
                    LoweringError::unsupported_at("assignment missing rvalue", &cx.fn_sym)
                })?;
                let rhs = crate::collections::owned(cx, rhs_expr)?;
                let v = crate::classes::try_assign_field(cx, lhs_expr, rhs)?.ok_or_else(|| {
                    LoweringError::unsupported_at(
                        "internal: is_class_field_lvalue/try_write_field disagreed",
                        &cx.fn_sym,
                    )
                })?;
                return Ok(v);
            }
            // A slice of a queue or dynamic array (`q[1:2] = '{..}`, LRM 7.6).
            if let Some(rhs_expr) = e.right()
                && let Some(done) = crate::arrays::assign_coll_slice(cx, lhs_expr, rhs_expr)
            {
                done?;
                return Ok(LoweredValue::Packed(op.pack(
                    cx.ctx.bool_type().const_zero(),
                    cx.ctx.bool_type().const_zero(),
                    1,
                    cx.repr,
                    false,
                )));
            }
            // A whole queue, dynamic or associative array: a copy.
            if crate::collections::coll_of_expr(&lhs_expr).is_some() {
                let rhs_expr = e.right().ok_or_else(|| {
                    LoweringError::unsupported_at("assignment missing rvalue", &cx.fn_sym)
                })?;
                return crate::collections::assign_whole(cx, lhs_expr, rhs_expr);
            }
            // `vif.a = rhs`, `vif.b[1] = row` (`crate::vif`).
            if let Some(rhs_expr) = e.right()
                && let Some(done) = crate::vif::assign_array(cx, lhs_expr, rhs_expr)
            {
                done?;
                return Ok(LoweredValue::Packed(op.pack(
                    cx.ctx.bool_type().const_zero(),
                    cx.ctx.bool_type().const_zero(),
                    1,
                    cx.repr,
                    false,
                )));
            }
            // An unpacked array, whole or part (`a = '{1, 2}`, `a = b`,
            // `m[1] = row`, `a[0:1] = b[2:3]`).
            if let Some((dst, shape)) = crate::arrays::array_target(cx, lhs_expr)? {
                let rhs_expr = e.right().ok_or_else(|| {
                    LoweringError::unsupported_at("assignment missing rvalue", &cx.fn_sym)
                })?;
                crate::arrays::assign_array(cx, dst, &shape, rhs_expr)?;
                // The value of an array assignment expression is never used
                // as a packed value (slang rejects that); a statement
                // discards this.
                return Ok(LoweredValue::Packed(op.pack(
                    cx.ctx.bool_type().const_zero(),
                    cx.ctx.bool_type().const_zero(),
                    1,
                    cx.repr,
                    false,
                )));
            }
            // An unpacked-array element write (`mem[i] = rhs`, `a[i][j] =
            // rhs`, a local array's element): `crate::arrays`. A real/string
            // element is stored as its bits.
            if let Some(r) = crate::arrays::resolve_element(cx, lhs_expr)? {
                let rhs_expr = e.right().ok_or_else(|| {
                    LoweringError::unsupported_at("assignment missing rvalue", &cx.fn_sym)
                })?;
                let rhs = crate::collections::owned(cx, rhs_expr)?;
                // `r` is the element's one resolution (LRM 11.4.2: `a[i++] =
                // h` increments `i` once); the old handle is read from it
                // rather than by lowering `lhs_expr` a second time.
                if let Some(t) = lhs_expr.expr_type() {
                    crate::collections::release_element(cx, &t, &r)?;
                }
                let rhs_pv = crate::real::to_storage(
                    cx,
                    rhs,
                    r.width,
                    "assignment of a >128-bit value to an array element",
                )?;
                let rhs_pv = crate::select::fit_width(&op, rhs_pv, r.width);
                crate::arrays::write_element(cx, &r, rhs_pv)?;
                return Ok(LoweredValue::Packed(rhs_pv));
            }
            // Bit/part/indexed selects, struct fields, `mem[i][j]` and
            // concatenation targets: a read-modify-write of the storage
            // root (`select.rs`).
            if !sv_sim_sim::symid::is_value_name(lhs_expr.kind()) {
                let rhs_expr = e.right().ok_or_else(|| {
                    LoweringError::unsupported_at("assignment missing rvalue", &cx.fn_sym)
                })?;
                let rhs = crate::collections::owned(cx, rhs_expr)?;
                return crate::collections::store_releasing(cx, lhs_expr, rhs);
            }
            let sym = lhs_expr.referenced_symbol().ok_or_else(|| {
                LoweringError::unsupported_at("assignment lvalue unresolved name", &cx.fn_sym)
            })?;
            let (slot, width, signed) = *cx
                .locals
                .get(&sym.uid())
                .ok_or_else(|| LoweringError::unsupported_at("assignment to a non-local", &sym))?;
            let rhs_expr = e.right().ok_or_else(|| {
                LoweringError::unsupported_at("assignment missing rvalue", &cx.fn_sym)
            })?;
            let rhs = crate::collections::owned(cx, rhs_expr)?;
            crate::collections::release_old(cx, lhs_expr)?;
            let rhs_pv = crate::real::to_storage(
                cx,
                rhs,
                width,
                "assignment of a >128-bit value to a local",
            )?;
            cx.b.build_store(slot, rhs_pv.llvm).unwrap();
            write_through_store(cx, slot);
            // SV's own assignment-as-expression semantics: the expression's
            // value is the assigned value (here always discarded by the
            // caller -- `func.rs`'s `ExpressionStatement` arm -- but
            // returned properly regardless, matching every other producing
            // arm in this match).
            Ok(LoweredValue::Packed(PackedValue {
                llvm: rhs_pv.llvm,
                width,
                repr: cx.repr,
                signed,
            }))
        }
        // R-run Task 15 (`docs/superpowers/plans/2026-09-27-run-wiring.md`
        // Task 15; the "no ledger ID covers enum/struct member access"
        // gap that task's own text flags): `p.field` on a packed
        // struct/union -- a compile-time-constant bit-offset/width slice
        // of the base's own already-lowered packed value. Reuses the same
        // `part_select4` window-extraction primitive `ElementSelect`/
        // `RangeSelect` above already use for exactly this "shift+
        // truncate a known window out of a wider packed value" shape.
        // Unlike `ElementSelect`'s dynamic `idx`, a struct field's offset
        // and width are both struct-SHAPE facts fixed at elaboration
        // (`Symbol::field_bit_offset()`/`Type::bit_width()` on the member
        // symbol), never runtime-computed, so `lo` is always a compile-
        // time LLVM constant and `lo_unknown` is always `false` -- no
        // dynamic out-of-range case exists the way an `ElementSelect`
        // index can be.
        //
        // An `enum` value itself (`s`, `s_t s;`) needs NO handling here at
        // all: it is a plain `NamedValue` read of a variable whose
        // underlying representation is its own declared integral base
        // type (`logic [N:0]`) -- it already round-trips through the
        // ordinary `NamedValue` arm above, exactly as this task's own
        // plan text says.
        ExpressionKind::MemberAccess => {
            // `vif.m` (`crate::vif`).
            if let Some(v) = crate::vif::read(cx, e) {
                return v;
            }
            // A covergroup's `option.x` / `type_option.x` (19.7).
            if let Some(v) = crate::covergroup::lower_option_read(cx, e)? {
                return Ok(v);
            }
            let base_expr = e.operand().ok_or_else(|| {
                LoweringError::unsupported_at("member access missing base value", &cx.fn_sym)
            })?;
            // Classes plan Task 5 (§1.4 step 4): `obj.field` -- a non-
            // static class property read through the object heap, never a
            // bit-packed slice of `base`'s own value the way every other
            // arm below this one is. Tried before `base_expr` is even
            // lowered as an ordinary packed value (a class handle's
            // PACKED form IS just its 64-bit reference, so `into_packed`
            // below would not itself reject it -- this check is about
            // ROUTING the READ, not about `into_packed` failing).
            //
            // Gated on the base being a class AT ALL (not also on `member`
            // being a non-static instance field): a class base with
            // anything else through this syntax (a static property via
            // instance syntax; a method symbol, unreachable here since a
            // method reference alone is never a plain `MemberAccess`
            // VALUE) must get a named, loud rejection here too, never fall
            // through to the packed-struct-field fallback below, which
            // would otherwise silently reinterpret the HANDLE's own raw
            // bits as if they were field storage (`select::field_window`'s
            // own defensive `is_class()` guard closes the identical gap for
            // the write side, `store_to`/`resolve_lvalue`).
            //
            // A `static` property (8.9), spelled `obj.count` here or
            // `Class::count` (8.23's class-scope `::`, a `NamedValue`, see
            // that arm), lives in its specialization's heap singleton
            // (`classes::read_static`, classes plan Task 9 -- Task 4 gave
            // STATE a per-template segment but never wired codegen access
            // to it). The receiver is NOT evaluated: there is exactly one
            // `count` for the whole specialization, whatever `obj` is
            // (including a null handle, which LRM 8.9 allows for a static).
            if base_expr.expr_type().is_some_and(|t| t.is_class()) {
                let member = e.member_symbol().ok_or_else(|| {
                    LoweringError::unsupported_at(
                        "member access on a class value missing its member symbol",
                        &cx.fn_sym,
                    )
                })?;
                if crate::classes::is_static_field(&member) {
                    return crate::classes::read_static(cx, &member);
                }
                // `obj.P` for a class parameter / localparam (LRM 8.25): a
                // constant of the handle's specialization, so the receiver
                // is not evaluated (as for a static property above).
                if matches!(member.kind(), sv_lang::kinds::SymbolKind::Parameter)
                    && let Some(cv) = member.constant_value()
                    && let Some(iv) = cv.as_integer()
                {
                    return lower_constant(cx, iv, None);
                }
                if !crate::classes::is_instance_field(&member) {
                    return Err(LoweringError::unsupported_at(
                        "a class member access that is neither a static nor a non-static \
                         property read",
                        &cx.fn_sym,
                    ));
                }
                let h = crate::classes::receiver_handle(cx, base_expr)?;
                return crate::classes::read_field_checked(cx, h, &member, e.source_range().start);
            }
            // `tagged_union.M` (11.9): the member, after the run-time tag
            // check.
            if let Some(v) = crate::tagged::read_member(cx, e, base_expr)? {
                return Ok(v);
            }
            let base = lower_expr(cx, base_expr)?.into_packed(
                "member access on a non-integral (real) base value is out of scope",
                &cx.fn_sym,
            )?;
            let member = e.member_symbol().ok_or_else(|| {
                LoweringError::unsupported_at("member access missing member symbol", &cx.fn_sym)
            })?;
            let field_ty = member.value_type().ok_or_else(|| {
                LoweringError::unsupported_at("member access field missing a type", &cx.fn_sym)
            })?;
            // A field of an unpacked struct/union: a slice of the base's
            // flat stored bits, read back as the field's own kind of value.
            if let Some(base_ty) = base_expr.expr_type()
                && sv_sim_sim::state::storage::is_unpacked_aggregate(&base_ty)
            {
                let (off, fw) = crate::select::field_window(Some(base_ty), &member)
                    .filter(|(off, fw)| *fw > 0 && off + fw <= u64::from(base.width))
                    .ok_or_else(|| {
                        LoweringError::unsupported_at(
                            "unpacked struct/union field outside its base's stored bits",
                            &cx.fn_sym,
                        )
                    })?;
                let lo = op.i_n(base.width).const_int(off, false);
                let bits = PackedValue {
                    signed: field_ty.is_signed(),
                    ..op.part_select4(base, lo, cx.ctx.bool_type().const_zero(), fw as u32)
                };
                return Ok(if field_ty.is_floating() {
                    LoweredValue::Real(crate::real::bits_to_real(cx, bits))
                } else if field_ty.is_string() {
                    LoweredValue::Str(crate::strings::from_storage(cx, bits))
                } else {
                    LoweredValue::Packed(bits)
                });
            }
            // Mirrors `layout.rs`'s own `!ty.is_integral()` gate for
            // exactly the same reason: a `real`/`string`/class-handle/...
            // field has no packed-bit representation this crate can slice
            // out of `base`'s val/unk lanes. A loud, named `LoweringError`,
            // never a panic reading a nonexistent bit window and never a
            // silent reinterpretation of the field's raw bits as an
            // integer.
            if !field_ty.is_integral() {
                return Err(LoweringError::unsupported_at(
                    "member access on a non-integral struct/union field \
                     (real/string/class-handle/...) is out of scope",
                    &cx.fn_sym,
                ));
            }
            let field_width_u64 = field_ty.bit_width();
            let offset_u64 = member.field_bit_offset();
            // Bounds-checked BEFORE any cast to `u32`: proves both
            // `field_width_u64` and `offset_u64` individually fit in
            // `base.width` (itself already a real `u32`), so the casts
            // below are total, never a silent truncation of an
            // out-of-range value -- the same "check first, cast after"
            // discipline `layout.rs::checked_bit_width_u16` documents.
            if field_width_u64 == 0
                || field_width_u64 > base.width as u64
                || offset_u64 > (base.width as u64) - field_width_u64
            {
                return Err(LoweringError::unsupported_at(
                    "member access field offset/width does not fit within its \
                     base struct/union's own already-lowered width",
                    &cx.fn_sym,
                ));
            }
            let field_width = field_width_u64 as u32;
            let offset = offset_u64 as u32;
            let lo = op.i_n(base.width).const_int(offset as u64, false);
            let lo_unknown = cx.ctx.bool_type().const_zero();
            // The field keeps its own signedness (`logic signed [7:0] x`
            // reads as signed, LRM 1800-2023 §7.2.1).
            Ok(LoweredValue::Packed(PackedValue {
                signed: field_ty.is_signed(),
                ..op.part_select4(base, lo, lo_unknown, field_width)
            }))
        }
        // `null` (a chandle's): the 64-bit zero pointer value.
        // `$` in a queue index or slice (`q[$]`, `q[1:$]`).
        // Classes plan Task 5 (§1.4 step 2): `new(args)`/`new` -- object
        // allocation, property initialization, and the explicit
        // constructor call (`crate::classes`).
        ExpressionKind::NewClass => crate::classes::lower_new_class(cx, e),
        ExpressionKind::NewCovergroup => crate::covergroup::lower_new(cx, e),
        // Classes plan Task 10 (LRM 8.12): `new src` -- shallow copy.
        ExpressionKind::CopyClass => crate::classes::lower_copy_class(cx, e),
        ExpressionKind::UnboundedLiteral => crate::collections::lower_unbounded(cx)
            .ok_or_else(|| LoweringError::unsupported_at("`$` outside a queue index", &cx.fn_sym)),
        ExpressionKind::NullLiteral => {
            let op = OpBuilder {
                ctx: cx.ctx,
                b: &cx.b,
            };
            Ok(LoweredValue::Packed(op.pack(
                op.i_n(64).const_zero(),
                op.i_n(64).const_zero(),
                64,
                cx.repr,
                false,
            )))
        }
        // An output argument's `actual = Conversion(<formal>)` (LRM 13.5):
        // the formal's value, set by `func::copy_back_outputs`.
        ExpressionKind::LValueReference | ExpressionKind::EmptyArgument => {
            cx.lvalue_ref.get().ok_or_else(|| {
                LoweringError::unsupported_at(
                    "lvalue reference outside a compound assignment",
                    &cx.fn_sym,
                )
            })
        }
        // A packed assignment pattern: a packed struct (`'{a: 1, b: 2}`,
        // `'{default: 0}`) or packed array (`'{8'h1, 8'h2}`, `'{4{2'b01}}`)
        // value. An unpacked-array pattern is an array value, handled by
        // `crate::arrays` where arrays are assigned or compared.
        ExpressionKind::SimpleAssignmentPattern
        | ExpressionKind::StructuredAssignmentPattern
        | ExpressionKind::ReplicatedAssignmentPattern => lower_packed_pattern(cx, e),
        ExpressionKind::TaggedUnion => crate::tagged::lower_tagged_expr(cx, e),
        // `min:typ:max` (LRM 1800-2023 11.11): the tool picks one operand per
        // run; this simulator always takes the typical value (the default
        // delay-selection of 30.4.1) and lowers only it.
        ExpressionKind::MinTypMax => {
            let typ = e
                .children()
                .into_iter()
                .filter_map(|n| n.as_expression())
                .nth(1)
                .ok_or_else(|| {
                    LoweringError::unsupported_at(
                        "a min:typ:max expression with no typical operand",
                        &cx.fn_sym,
                    )
                })?;
            lower_expr(cx, typ)
        }
        // A bare fixed-width stream as the source of a stream unpack
        // (`{>>{x}} = {<<byte{x}}`, LRM 1800-2023 11.4.14.3): its bits.
        // A runtime-length stream is handled by the assignment arm.
        // (Written as a guarded catch-all so the supported-constructs scan
        // does not list the conditional arm as a lowered kind.)
        kind if kind == ExpressionKind::Streaming && !crate::stream::source_needs_runtime(e) => {
            Ok(LoweredValue::Packed(crate::stream::lower_streaming(cx, e)?))
        }
        other => Err(LoweringError::unsupported_at(
            &format!("expression kind {other:?}"),
            &cx.fn_sym,
        )),
    }
}

/// The stored bits (LSB first) of the constant `cv` of type `ty`, in the form
/// a variable of that type holds: an integral value as is, a real as its IEEE
/// bits, an unpacked struct as its fields (first field most significant), a
/// fixed-size unpacked array with its lowest index least significant
/// (`sv_sim_sim::state::storage`). `None` for anything else (a string, a
/// collection, a class handle, a union).
pub(crate) fn const_stored_bits(
    ty: &sv_lang::Type<'_>,
    cv: &sv_lang::ConstantValue,
) -> Option<Vec<sv_lang::Bit>> {
    let mut strs = Vec::new();
    let bits = const_stored_bits_strs(ty, cv, 0, &mut strs)?;
    // A string field's stored form is a run-time interned id: no constant bits.
    strs.is_empty().then_some(bits)
}

/// [`const_stored_bits`] that also reports each `string` field it met as
/// `(bit offset in the result, text)`, leaving that field's 64-bit slot zero
/// for the caller to fill with the run-time interned id (LRM 6.20.2: an
/// unpacked-struct named constant may hold a string).
pub(crate) fn const_stored_bits_strs(
    ty: &sv_lang::Type<'_>,
    cv: &sv_lang::ConstantValue,
    base: usize,
    strs: &mut Vec<(usize, Vec<u8>)>,
) -> Option<Vec<sv_lang::Bit>> {
    use sv_lang::{Bit, ConstantValue};
    use sv_sim_sim::state::storage;
    if ty.is_string() {
        let ConstantValue::String(text) = cv else {
            return None;
        };
        strs.push((base, text.as_bytes().to_vec()));
        return Some(vec![Bit::Zero; storage::storage_width(ty)? as usize]);
    }
    // A class-handle constant is only ever `null` (LRM 6.20.2, 8.4): the zero handle.
    if matches!(cv, ConstantValue::Null) {
        return Some(vec![Bit::Zero; storage::storage_width(ty)? as usize]);
    }
    if ty.is_floating() {
        let ConstantValue::Real(f) = cv else {
            return None;
        };
        let w = ty.bit_width() as usize;
        let raw = if w == 32 {
            u64::from((*f as f32).to_bits())
        } else {
            f.to_bits()
        };
        return Some(
            (0..w)
                .map(|i| {
                    if raw >> i & 1 == 1 {
                        Bit::One
                    } else {
                        Bit::Zero
                    }
                })
                .collect(),
        );
    }
    if ty.is_integral() {
        let v = cv.as_integer()?;
        let mut bits = sv_sim_sim::state::svint_bits(v)?.into_owned();
        let pad = if v.is_signed() {
            bits.last().copied().unwrap_or(Bit::Zero)
        } else {
            Bit::Zero
        };
        bits.resize(ty.bit_width() as usize, pad);
        return Some(bits);
    }
    let ConstantValue::Array(elems) = cv else {
        return None;
    };
    let total = storage::storage_width(ty)? as usize;
    let mut out = vec![Bit::Zero; total];
    if ty.is_unpacked_struct() {
        for (i, f) in ty.fields().enumerate() {
            let (lo, w) = storage::field_slice(ty, i as u32)?;
            let sub =
                const_stored_bits_strs(&f.value_type()?, elems.get(i)?, base + lo as usize, strs)?;
            if sub.len() as u64 != w {
                return None;
            }
            out[lo as usize..(lo + w) as usize].copy_from_slice(&sub);
        }
        return Some(out);
    }
    if ty.is_unpacked_array() && !ty.is_dynamically_sized_array() {
        let elem = ty.element_type()?;
        let ew = storage::storage_width(&elem)? as usize;
        let n = elems.len();
        let ascending =
            ty.fixed_unpacked_array_range().left < ty.fixed_unpacked_array_range().right;
        for (p, el) in elems.iter().enumerate() {
            let slot = if ascending { p } else { n - 1 - p };
            let sub = const_stored_bits_strs(&elem, el, base + slot * ew, strs)?;
            if sub.len() != ew || (slot + 1) * ew > total {
                return None;
            }
            out[slot * ew..(slot + 1) * ew].copy_from_slice(&sub);
        }
        return Some(out);
    }
    None
}

/// Refuses a tolerance range (`[a +/- b]`, `[a +%- b]`, LRM 1800-2023
/// 11.4.13), which slang parses only as 1800-2023 and whose `left`/`right`
/// are the centre and the tolerance, not the bounds: lowered as `[lo:hi]` it
/// would be a silent wrong answer. The semantic node does not keep the
/// operator, so it is read from the syntax (a no-op outside a
/// [`crate::classes::DesignSyntaxScope`], where no tree is available).
pub(crate) fn refuse_tolerance_range(
    cand: sv_lang::Expression<'_>,
    sym: &sv_lang::Symbol,
) -> Result<(), LoweringError> {
    use sv_lang::AstNode;
    use sv_lang::nodes::ValueRangeExpressionSyntax;

    let op = crate::classes::with_design_trees(|trees| {
        trees.iter().find_map(|tree| {
            let node = cand.syntax(tree)?;
            let op = ValueRangeExpressionSyntax::cast(node)?.op()?;
            Some(op.text().to_string())
        })
    });
    match op.as_deref() {
        None | Some(":") => Ok(()),
        Some(op) => Err(LoweringError::unsupported_at(
            &format!("an `inside` tolerance range (`[a {op} b]`, LRM 11.4.13)"),
            sym,
        )),
    }
}

/// `Some(relative)` when `cand` is a tolerance range (`+%-` is relative),
/// read from the syntax like [`refuse_tolerance_range`].
pub(crate) fn tolerance_op(cand: sv_lang::Expression<'_>) -> Option<bool> {
    use sv_lang::AstNode;
    use sv_lang::nodes::ValueRangeExpressionSyntax;
    crate::classes::with_design_trees(|trees| {
        trees.iter().find_map(|tree| {
            let node = cand.syntax(tree)?;
            let op = ValueRangeExpressionSyntax::cast(node)?.op()?;
            match op.text() {
                "+/-" => Some(false),
                "+%-" => Some(true),
                _ => None,
            }
        })
    })
}

/// Whether a range bound is `$` (LRM 1800-2023 11.4.13), looking through
/// implicit conversions; LRM 6.20.7: a parameter assigned `$` is usable
/// wherever `$` is, so a named constant folding to the unbounded value
/// opens the range the same way.
pub(crate) fn is_open_bound(x: Option<sv_lang::Expression<'_>>) -> bool {
    let mut x = x;
    while let Some(c) = x.filter(|c| c.kind() == ExpressionKind::Conversion) {
        x = c.conversion_operand();
    }
    x.is_some_and(|x| {
        x.kind() == ExpressionKind::UnboundedLiteral
            || matches!(x.constant_value(), Some(sv_lang::ConstantValue::Unbounded))
    })
}

/// Whether `left` matches one `inside` candidate (LRM 1800-2023 §11.4.13):
/// a range `[lo:hi]` (`lo <= left <= hi`) or a value (`left ==? value`).
/// Shared by the `inside` operator and `case ... inside` items.
pub(crate) fn inside_candidate<'ctx, 'd>(
    cx: &LowerCtx<'ctx, 'd, '_>,
    left: PackedValue<'ctx>,
    cand: sv_lang::Expression<'d>,
) -> Result<PackedValue<'ctx>, LoweringError> {
    let op = OpBuilder {
        ctx: cx.ctx,
        b: &cx.b,
    };
    let bound = |x: Option<sv_lang::Expression<'d>>, which: &str| {
        let x = x.ok_or_else(|| {
            LoweringError::unsupported_at(
                &format!("inside range missing {which} bound"),
                &cx.fn_sym,
            )
        })?;
        lower_expr(cx, x)?.into_packed(
            &format!("inside range with a non-integral (real) {which} bound"),
            &cx.fn_sym,
        )
    };
    if cand.kind() == ExpressionKind::ValueRange {
        if let Some(relative) = tolerance_op(cand) {
            // LRM 11.4.13: `[a +/- b]` / `[a +%- b]`; `left`/`right` are the
            // centre and the tolerance.
            let a = bound(cand.left(), "centre")?;
            let b = bound(cand.right(), "tolerance")?;
            return Ok(op.inside_tolerance_candidate4(left, a, b, relative));
        }
        // `$` as a bound is the minimum / maximum of the left operand's type
        // (LRM 1800-2023 11.4.13): that side always holds, so the operand
        // itself stands in for it (keeping an X operand X).
        let open = is_open_bound;
        let lo = if open(cand.left()) {
            left
        } else {
            bound(cand.left(), "low")?
        };
        let hi = if open(cand.right()) {
            left
        } else {
            bound(cand.right(), "high")?
        };
        return Ok(op.inside_range_candidate4(left, lo, hi));
    }
    // A queue or dynamic array of integral elements: its size is only known
    // at run time, so scan it with a loop (LRM 1800-2023 11.4.13).
    if let Some(c) = crate::collections::coll_of_expr(&cand) {
        if c.assoc || !c.elem.is_integral() {
            return Err(LoweringError::unsupported_at(
                "inside over an associative array or a collection of non-integral elements",
                &cx.fn_sym,
            ));
        }
        return inside_collection_scan(cx, left, cand, &c);
    }
    // An unpacked array in the set stands for every one of its elements
    // (LRM 1800-2023 11.4.13: "if an expression in the set is an array, each
    // of its elements is a member"); a fixed-size array is unrolled over its
    // rows, an element that is X/Z matches by the plain-item rule.
    if cand
        .expr_type()
        .is_some_and(|t| t.is_unpacked_array() && !sv_sim_sim::state::storage::is_collection(&t))
    {
        // A class property or unpacked-struct field array has no row storage
        // of its own: read a snapshot of it (`spill_member_array`); an
        // array-valued call reads its returned temporary.
        let target = match crate::arrays::array_target(cx, cand)? {
            Some(t) => Some(t),
            None => match crate::arrays::spill_member_array(cx, cand)? {
                Some(t) => Some(t),
                None => crate::arrays::array_call_value(cx, cand)?,
            },
        };
        let Some((r, shape)) = target else {
            return Err(LoweringError::unsupported_at(
                "inside over an unpacked array that is not a variable, memory, member or call",
                &cx.fn_sym,
            ));
        };
        let i64t = cx.ctx.i64_type();
        let mut acc: Option<PackedValue<'ctx>> = None;
        for k in 0..shape.rows() {
            let el = crate::arrays::ElemRef {
                backing: r.backing,
                row: cx
                    .b
                    .build_int_add(r.base, i64t.const_int(k, false), "inside_row")
                    .unwrap(),
                valid: r.valid,
                width: shape.elem.value_width(),
                signed: shape.elem.is_signed(),
                default: crate::arrays::default_value(cx, &shape.elem),
            };
            let item = crate::arrays::read_element(cx, &el)?;
            let c = op.inside_plain_candidate4(left, item);
            acc = Some(match acc {
                None => c,
                Some(prev) => op.or4(prev, c),
            });
        }
        return acc.ok_or_else(|| {
            LoweringError::unsupported_at("inside over a zero-element array", &cx.fn_sym)
        });
    }
    let item = lower_expr(cx, cand)?
        .into_packed("inside with a non-integral (real) candidate", &cx.fn_sym)?;
    Ok(op.inside_plain_candidate4(left, item))
}

/// `left inside {coll}` for a queue or dynamic array `cand`: OR of the
/// plain-item match of `left` against each element, accumulated in a loop
/// over the run-time size (LRM 1800-2023 11.4.13; an empty collection
/// matches nothing).
fn inside_collection_scan<'ctx, 'd>(
    cx: &LowerCtx<'ctx, 'd, '_>,
    left: PackedValue<'ctx>,
    cand: sv_lang::Expression<'d>,
    c: &crate::collections::Coll<'d>,
) -> Result<PackedValue<'ctx>, LoweringError> {
    let op = OpBuilder {
        ctx: cx.ctx,
        b: &cx.b,
    };
    let i64t = cx.ctx.i64_type();
    let h = crate::collections::handle_of(cx, cand)?;
    let n = crate::collections::size_of_handle(cx, c, h);
    let f =
        cx.b.get_insert_block()
            .and_then(|bb| bb.get_parent())
            .expect("lowering happens inside a function");
    // The accumulator is a 1-bit four-state flag, zero (known 0) to start.
    let shape = PackedValue {
        llvm: op.packed_type(1, cx.repr).const_zero(),
        width: 1,
        repr: cx.repr,
        signed: false,
    };
    let acc_ty = shape.llvm.get_type();
    let acc_slot = entry_alloca(cx, acc_ty, "inside_scan_acc");
    cx.b.build_store(acc_slot, shape.llvm).unwrap();
    let i_slot = entry_alloca(cx, i64t, "inside_scan_i");
    cx.b.build_store(i_slot, i64t.const_zero()).unwrap();
    let head = cx.ctx.append_basic_block(f, "inside_scan_head");
    let body = cx.ctx.append_basic_block(f, "inside_scan_body");
    let done = cx.ctx.append_basic_block(f, "inside_scan_done");
    cx.b.build_unconditional_branch(head).unwrap();
    cx.b.position_at_end(head);
    let i = cx.b.build_load(i64t, i_slot, "i").unwrap().into_int_value();
    let more =
        cx.b.build_int_compare(inkwell::IntPredicate::SLT, i, n, "more")
            .unwrap();
    cx.b.build_conditional_branch(more, body, done).unwrap();
    cx.b.position_at_end(body);
    let LoweredValue::Packed(item) = crate::collections::read_at_coll(cx, c, h, i) else {
        return Err(LoweringError::unsupported_at(
            "an `inside` collection element that is not a packed value",
            &cx.fn_sym,
        ));
    };
    let m = op.inside_plain_candidate4(left, item);
    let load = |name: &str| PackedValue {
        llvm: cx
            .b
            .build_load(acc_ty, acc_slot, name)
            .unwrap()
            .into_int_value(),
        ..shape
    };
    let next = op.or4(load("acc"), m);
    cx.b.build_store(acc_slot, next.llvm).unwrap();
    let inc =
        cx.b.build_int_add(i, i64t.const_int(1, false), "i_next")
            .unwrap();
    cx.b.build_store(i_slot, inc).unwrap();
    cx.b.build_unconditional_branch(head).unwrap();
    cx.b.position_at_end(done);
    Ok(load("inside_scan_res"))
}

/// A packed assignment pattern's value: its elements (slang has already
/// resolved member/index/default setters into one per member or element,
/// in declaration order), concatenated most significant first (LRM
/// 1800-2023 §10.9.2).
fn lower_packed_pattern<'ctx, 'd>(
    cx: &LowerCtx<'ctx, 'd, '_>,
    e: sv_lang::Expression<'d>,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    let op = OpBuilder {
        ctx: cx.ctx,
        b: &cx.b,
    };
    let ty = e.expr_type().ok_or_else(|| {
        LoweringError::unsupported_at("assignment pattern has no type", &cx.fn_sym)
    })?;
    // An unpacked struct's stored form is its fields' stored bits, first
    // field most significant -- the same concatenation. A fixed-size
    // unpacked array reaching here is held as bits too (a struct field,
    // `sv_sim_sim::state::storage`): lowest index least significant, so an
    // ascending one's elements concatenate in reverse.
    let bits_array = ty.is_unpacked_array() && !ty.is_dynamically_sized_array();
    if !ty.is_integral() && !ty.is_unpacked_struct() && !bits_array {
        return Err(LoweringError::unsupported_at(
            "an unpacked assignment pattern (an array value) outside an array assignment, \
             initializer or comparison",
            &cx.fn_sym,
        ));
    }
    let mut elems = e.pattern_elements();
    if e.kind() == ExpressionKind::ReplicatedAssignmentPattern {
        let n = e
            .replicated_pattern_count()
            .and_then(|c| c.constant_value())
            .and_then(|c| c.as_i64())
            .ok_or_else(|| {
                LoweringError::unsupported_at(
                    "a replicated assignment pattern with a non-constant count",
                    &cx.fn_sym,
                )
            })?;
        elems = (0..n).flat_map(|_| elems.iter().copied()).collect();
    }
    if bits_array {
        let range = ty.fixed_unpacked_array_range();
        if range.left < range.right {
            elems.reverse();
        }
    }
    let mut parts = Vec::with_capacity(elems.len());
    for el in elems {
        // A collection element or field: a copy (`crate::collections`).
        let v = crate::real::to_storage(
            cx,
            crate::collections::owned(cx, el)?,
            el.expr_type().map(|t| t.value_width()).unwrap_or(0),
            "an assignment pattern element wider than 128 bits",
        )?;
        let w = el
            .expr_type()
            .map(|t| t.value_width())
            .filter(|w| *w > 0)
            .unwrap_or(v.width);
        parts.push(crate::select::fit_width(&op, v, w));
    }
    if parts.is_empty() {
        return Err(LoweringError::unsupported_at(
            "an empty assignment pattern",
            &cx.fn_sym,
        ));
    }
    let v = op.concat4(&parts);
    let width = ty.value_width();
    if v.width != width {
        return Err(LoweringError::unsupported_at(
            &format!(
                "a packed assignment pattern's elements total {} bits for a {width}-bit type",
                v.width
            ),
            &cx.fn_sym,
        ));
    }
    Ok(LoweredValue::Packed(PackedValue {
        signed: ty.is_signed(),
        ..v
    }))
}

/// R-run Task 10 (CG-061 reused, CG-042 untouched): a `Call` expression this
/// crate's own frontend classifies as NOT a system call
/// (`Expression::is_system_call() != Some(true)`) -- an ordinary SV
/// `function`/`task` call used in EXPRESSION position (e.g. `x = sq(7)`, or,
/// nested, `show(sq(7))`'s inner `sq(7)`). Distinguishes two genuinely
/// different "outside CG-042's allowlist" cases the pre-Task-10 code lumped
/// into one unconditional rejection:
///
/// - a DPI import call: a direct C call ([`crate::dpi::lower_dpi_import_call`]).
/// - an ordinary (non-DPI) subroutine call resolving to a real
///   `SubroutineSymbol`: lowered via [`crate::func::resolve_callee_fn`] --
///   the SAME same-module/mangled/cross-module resolution
///   `func.rs::lower_task_call` (a bare, statement-position call) already
///   uses, so this call site never has to know or care whether the callee
///   sits in the same LLVM `Module` this pipeline already lowered it into
///   or genuinely lives in a different one (CG-061).
///
/// Scope restriction (named, not silent): only an AUTOMATIC, NON-SUSPENDING
/// function call reaches here successfully. A task whose OWN body contains a
/// suspend point (a `#delay`/`@event`/`wait(expr)` statement) fails inside
/// `func::lower_function`'s own `lower_stmt` dispatch (the `Timed`/`Wait`
/// arms, CG-154) the first time this pipeline tries to lower that task's
/// body at all -- long before any call site like this one is ever reached; a
/// task call used as an EXPRESSION value is in any case not legal SV (only a
/// `function` call can appear in expression position at all), so this
/// function's own real-world callees are always `function`s, never `task`s.
/// A real-typed return value is likewise out of scope, mirroring
/// `lower_function`'s own real-typed-ARGUMENT restriction (CG-037 covers the
/// `Conversion` node, not real-typed function-call plumbing).
fn lower_user_subroutine_call<'ctx, 'd, 'm>(
    cx: &LowerCtx<'ctx, 'd, 'm>,
    call: sv_lang::Expression<'d>,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    // Classes plan Task 5 (§1.4 step 2): `obj.method(args)` used as a
    // value -- `call_this_class()` gives the receiver expression directly
    // whenever this is a method call at all, tried before every other
    // call-site resolution below (none of which know how to find a class
    // method's own mangled name/receiver). Deviation (found running this
    // task's own fixtures): `call_this_class()` is ALSO `Some` for a
    // virtual-interface modport function call -- `vif::call_targets` is
    // checked first and excludes that case, matching `func::
    // lower_task_call`'s own identical guard.
    // A covergroup's predefined methods (19.8).
    if let Some(v) = crate::covergroup::lower_call(cx, call)? {
        return Ok(v);
    }
    // The built-in `process` class (Task 13) has no SV bodies: its calls
    // (`p.status()`, `process::self()`) go straight to the runtime.
    match crate::classes::lower_std_process_call(cx, call)? {
        crate::classes::ProcessCall::Lowered(Some(v)) => return Ok(v),
        crate::classes::ProcessCall::Lowered(None) => {
            return Err(LoweringError::unsupported_at(
                "a void method call cannot be used as an expression value",
                &cx.fn_sym,
            ));
        }
        crate::classes::ProcessCall::NotProcess => {}
    }
    let not_vif_call = sv_sim_sim::netlist::vif::call_targets(&call).is_none();
    // A function or method whose body spawns processes (LRM 1800-2023
    // 13.4.4) is inlined into the calling process ahead of the statement
    // (`CfgBuilder::hoist_suspending_function_calls`): the call's value is
    // its result variable, a local of the process.
    // (Two calls of one function in a statement, and a virtual method, park
    // the value in a temporary of the call's own: `ArgBinding::CallResult`.)
    if let Some(callee) = call.call_subroutine()
        && callee.subroutine_kind() == Some(sv_lang::SubroutineKind::Function)
        && let Some(rv) = callee.subroutine_return_val_var()
        && let Some((slot, width, signed)) = cx
            .locals
            .get(&sv_sim_sim::process::cfg::call_result_key(&call))
            .or_else(|| cx.locals.get(&rv.uid()))
            .copied()
        && sv_sim_sim::process::cfg::subroutine_can_suspend(&callee)
    {
        let op = OpBuilder {
            ctx: cx.ctx,
            b: &cx.b,
        };
        let ty = if cx.repr == ValueRepr::FourState {
            op.i_n(width * 2)
        } else {
            op.i_n(width)
        };
        let v =
            cx.b.build_load(ty, slot, "ld_ret")
                .unwrap()
                .into_int_value();
        let pv = PackedValue {
            llvm: v,
            width,
            repr: cx.repr,
            signed,
        };
        if rv.value_type().is_some_and(|t| t.is_floating()) {
            return Ok(LoweredValue::Real(crate::real::bits_to_real(cx, pv)));
        }
        return Ok(LoweredValue::Packed(pv));
    }
    if not_vif_call && let Some(recv) = call.call_this_class() {
        return crate::classes::lower_method_call(cx, Some(recv), call);
    }
    // Review fix (r2), Task 6 finding 1: `super.method(...)` returning a
    // VALUE (e.g. `return super.val();`) and a bare, implicit-`this`
    // sibling-method call used as a value -- see `func::lower_task_call`'s
    // identical fallback (and `classes::resolve_implicit_this_instance_
    // method`'s own doc comment) for why `call_this_class()` is `None`
    // here despite there being a live, current-object receiver.
    if not_vif_call
        && let Some(callee) = call.call_subroutine()
        && crate::classes::resolve_implicit_this_instance_method(callee)?.is_some()
    {
        return crate::classes::lower_method_call(cx, None, call);
    }
    let callee = call.call_subroutine().ok_or_else(|| {
        LoweringError::unsupported_at(
            "non-system call missing its callee subroutine symbol",
            &cx.fn_sym,
        )
    })?;
    // Through a virtual interface: the selected instance's own function
    // (`crate::vif::call_arms`); `null` gives the return type's default.
    if crate::vif::call_arms_apply(&call) {
        let ret = callee
            .subroutine_return_type()
            .filter(|t| !t.is_void())
            .ok_or_else(|| {
                LoweringError::unsupported_at(
                    "a void function/task call cannot be used as an expression value",
                    &cx.fn_sym,
                )
            })?;
        let (slot, is_real) = if ret.is_floating() {
            let slot = entry_alloca(cx, cx.ctx.f64_type(), "vif_ret");
            cx.b.build_store(slot, cx.ctx.f64_type().const_zero())
                .unwrap();
            (slot, true)
        } else {
            let op = OpBuilder {
                ctx: cx.ctx,
                b: &cx.b,
            };
            let ty = op.packed_type(ret.value_width(), cx.repr);
            let slot = entry_alloca(cx, ty, "vif_ret");
            cx.b.build_store(slot, crate::arrays::default_value(cx, &ret))
                .unwrap();
            (slot, false)
        };
        let (arms, merge) = crate::vif::call_arms(cx, call).expect("checked")?;
        for (callee, bb) in arms {
            cx.b.position_at_end(bb);
            match lower_user_subroutine_call_of(cx, callee, call)? {
                LoweredValue::Real(r) => {
                    cx.b.build_store(slot, r.llvm).unwrap();
                }
                LoweredValue::Packed(p) => {
                    cx.b.build_store(slot, p.llvm).unwrap();
                }
                _ => {
                    return Err(LoweringError::unsupported_at(
                        "a string or wide function result through a virtual interface",
                        &cx.fn_sym,
                    ));
                }
            }
            cx.b.build_unconditional_branch(merge).unwrap();
        }
        cx.b.position_at_end(merge);
        return Ok(if is_real {
            let f =
                cx.b.build_load(cx.ctx.f64_type(), slot, "vif_ret_ld")
                    .unwrap()
                    .into_float_value();
            LoweredValue::Real(RealValue { llvm: f })
        } else {
            let op = OpBuilder {
                ctx: cx.ctx,
                b: &cx.b,
            };
            let ty = op.packed_type(ret.value_width(), cx.repr);
            let v =
                cx.b.build_load(ty, slot, "vif_ret_ld")
                    .unwrap()
                    .into_int_value();
            LoweredValue::Packed(PackedValue {
                llvm: v,
                width: ret.value_width(),
                repr: cx.repr,
                signed: ret.is_signed(),
            })
        });
    }
    // An interface `extern` function (LRM 25.7): the module's definition.
    let callee = sv_sim_sim::netlist::itf::call_target(&call).unwrap_or(callee);
    lower_user_subroutine_call_of(cx, callee, call)
}

/// [`lower_user_subroutine_call`] of `call` to `callee`.
fn lower_user_subroutine_call_of<'ctx, 'd, 'm>(
    cx: &LowerCtx<'ctx, 'd, 'm>,
    callee: sv_lang::Symbol<'d>,
    call: sv_lang::Expression<'d>,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    if callee.subroutine_flags().contains(MethodFlags::DPI_IMPORT) {
        let v = crate::dpi::lower_dpi_import_call(cx, &callee, call)?.ok_or_else(|| {
            LoweringError::unsupported_at(
                "a void DPI import cannot be used as an expression value",
                &cx.fn_sym,
            )
        })?;
        crate::stmt_shared::leave_if_aborted(cx)?;
        return Ok(v);
    }
    // A void `task`'s `subroutine_return_type()` is `Some(<void Type>)`,
    // never `None` (`Type::is_void()` is the real signal -- see
    // `func::lower_function`'s own `is_void_return` for the identical
    // correction) -- checked explicitly here rather than via `.ok_or_else`
    // on `None`, which a void callee could never actually trigger.
    let ret_sv = callee.subroutine_return_type();
    if ret_sv.as_ref().is_none_or(sv_lang::Type::is_void) {
        return Err(LoweringError::unsupported_at(
            "a void function/task call cannot be used as an expression value",
            &cx.fn_sym,
        ));
    }
    let ret_sv = ret_sv.unwrap();
    let width = ret_sv.value_width();
    let signed = ret_sv.is_signed();
    let returns_real = ret_sv.is_floating();

    let callee_fn = crate::func::resolve_callee_fn(cx, callee)?;
    let (mut llvm_args, copy_backs) = crate::func::lower_call_args(cx, &callee, call)?;
    // A result wider than an LLVM integer comes back as an image the caller
    // owns (the last argument).
    let image = if crate::huge::is_image_width(width) && ret_sv.is_integral() {
        let img = crate::huge::result_image(cx, width)?;
        llvm_args.push(img.into());
        Some(img)
    } else {
        None
    };
    if llvm_args.len() != callee_fn.count_params() as usize {
        return Err(LoweringError::unsupported_at(
            "user function call argument count does not match the callee's \
             declared parameter count",
            &cx.fn_sym,
        ));
    }
    let call_site =
        cx.b.build_call(callee_fn, &llvm_args, "user_fn_call")
            .unwrap();
    if sv_sim_sim::process::cfg::subroutine_can_abort(&callee) {
        crate::stmt_shared::leave_if_aborted(cx)?;
    }
    crate::func::commit_callee_writes(cx, &callee);
    if crate::dpi::reaches_context_import(&callee) {
        crate::dpi::reload_shadows(cx);
    }
    crate::func::copy_back_outputs(cx, copy_backs)?;
    if let Some(ptr) = image {
        return Ok(LoweredValue::Huge(crate::huge::HugeValue {
            ptr,
            width,
            signed,
        }));
    }
    let ret_v = call_site.try_as_basic_value().basic().ok_or_else(|| {
        LoweringError::unsupported_at(
            "user function call produced no return value at the LLVM \
                 level despite a non-void SV return type",
            &cx.fn_sym,
        )
    })?;
    if returns_real {
        let f = crate::real::as_f64(cx, ret_v.into_float_value());
        return Ok(LoweredValue::Real(RealValue { llvm: f }));
    }
    Ok(LoweredValue::Packed(PackedValue {
        llvm: ret_v.into_int_value(),
        width,
        repr: cx.repr,
        signed,
    }))
}

// Task 9 (CG-090, CG-007), review fix (r1): `lower_condition` is the ONE
// entry point every direct if/while/wait/case branch-condition site must
// call (currently only `func.rs`'s `Conditional`/`if` arm exists). When
// `expr` is itself a direct `&&`/`||` BinaryOp, this is CG-090's actual
// short-circuit-condition site -- routed to
// `lower_short_circuit_logical_condition`, the ARCHITECTURE.md-cited §12.4
// "ambiguous -> false" collapse (also skipping the right operand's
// evaluation whenever the left ALONE is not provably true/false, since a
// branch decision alone never needs to distinguish "definitely false" from
// "ambiguous" -- both take the else path). Every other expression kind
// (including a `&&`/`||` nested inside a larger condition, e.g. `(a && b)
// && c`'s outer `&&`) instead goes through the ordinary value-preserving
// `lower_expr`, followed by `is_definitely_true` on the fully-reduced
// result -- behaviorally equivalent for the branch DECISION itself (any
// ambiguity anywhere still collapses to false), never a per-bit merge
// (CG-091: that is the ternary `?:`'s own, distinct job).
/// LRM 20.6.2 / 11.4.7: a `&&`/`||` the front end already folded to an integer
/// (`!$isunbounded(P) && P == 0`, whose right operand is never evaluated when `P` is `$`)
/// lowers as that constant: lowering the unevaluated operand would read the `$` parameter,
/// which has no integer value.
fn fold_constant_logical<'ctx, 'd, 'm>(
    cx: &LowerCtx<'ctx, 'd, 'm>,
    e: sv_lang::Expression<'d>,
) -> Result<Option<LoweredValue<'ctx>>, LoweringError> {
    match e.constant_value().and_then(|cv| cv.as_integer().cloned()) {
        Some(iv) => lower_constant(cx, &iv, Some(e)).map(Some),
        None => Ok(None),
    }
}

pub fn lower_condition<'ctx, 'd, 'm>(
    cx: &LowerCtx<'ctx, 'd, 'm>,
    expr: sv_lang::Expression<'d>,
) -> Result<inkwell::values::IntValue<'ctx>, LoweringError> {
    // An event as a condition: true iff it is not `null` (15.5.5.3).
    if let Some(v) = crate::events::lower_event_condition(cx, expr)? {
        return Ok(v);
    }
    if expr.kind() == ExpressionKind::BinaryOp
        && let Some(bop) = expr.binary_op()
        && matches!(bop, BinaryOp::LogicalAnd | BinaryOp::LogicalOr)
    {
        let lv = match fold_constant_logical(cx, expr)? {
            Some(v) => v,
            None => lower_short_circuit_logical_condition(cx, expr, bop == BinaryOp::LogicalAnd)?,
        };
        // `lower_short_circuit_logical_condition` already produces a
        // crisp (unk=0) boolean, so this `is_definitely_true` is a
        // pass-through -- kept for a single, uniform "the condition
        // site always ends in `is_definitely_true`" shape rather
        // than a special-cased early return of a raw bit.
        let pv = lv.into_packed("condition must be an integral (non-real) value", &cx.fn_sym)?;
        return Ok(is_definitely_true(cx.ctx, &cx.b, pv));
    }
    let pv = match lower_expr_h(cx, expr)? {
        LoweredValue::Real(rv) => return Ok(crate::real::real_truth(cx, rv)),
        other => crate::huge::truth_packed(
            cx,
            other,
            crate::huge::TruthMode::Strict,
            "condition of a >128-bit value",
        )?,
    };
    Ok(is_definitely_true(cx.ctx, &cx.b, pv))
}

/// [`lower_condition`] for the condition `!expr`: 1 exactly where the logical
/// negation of `expr` is KNOWN true (LRM 11.4.7), that is where `expr` is a
/// fully known zero. Not `!lower_condition(expr)`: an `x`/`z` expression is not
/// true but its negation `!x` is `x`, which a condition collapses to false too,
/// so both `expr` and `!expr` fail on it. `expr` is lowered by `lower_expr`
/// even when it is a `&&`/`||`, because the crisp collapse of
/// `lower_short_circuit_logical_condition` would turn an ambiguous `a && b`
/// into a known 0 and its negation into a spurious true.
pub fn lower_negated_condition<'ctx, 'd, 'm>(
    cx: &LowerCtx<'ctx, 'd, 'm>,
    expr: sv_lang::Expression<'d>,
) -> Result<inkwell::values::IntValue<'ctx>, LoweringError> {
    let pv = match lower_expr_h(cx, expr)? {
        LoweredValue::Real(rv) => {
            // A real is never ambiguous: `!r` is true exactly for 0.0.
            let truth = crate::real::real_truth(cx, rv);
            return Ok(cx.b.build_not(truth, "real_not").unwrap());
        }
        // A huge value is read as the one-bit truth proxy (`WideOp::Truth`).
        other => crate::huge::truth_packed(
            cx,
            other,
            crate::huge::TruthMode::Strict,
            "condition of a >128-bit value",
        )?,
    };
    Ok(is_definitely_false(cx.ctx, &cx.b, pv))
}

/// Stores `value` into the target expression `lhs` (a real value becomes
/// the real target's stored bits) and returns it as the assignment's value.
pub(crate) fn store_to<'ctx, 'd>(
    cx: &LowerCtx<'ctx, 'd, '_>,
    lhs: sv_lang::Expression<'d>,
    value: LoweredValue<'ctx>,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    store_to_releasing(cx, lhs, value, None)
}

/// [`store_to`] for a target that holds reference-counted handles, with the
/// target resolved once: `release` (when given) receives the stored value the
/// target held, just before `value` replaces it (`select::
/// lower_assign_releasing`). Class properties and collection elements
/// release their own old value in their store and never call `release`.
pub(crate) fn store_to_releasing<'ctx, 'd>(
    cx: &LowerCtx<'ctx, 'd, '_>,
    lhs: sv_lang::Expression<'d>,
    value: LoweredValue<'ctx>,
    release: Option<&mut dyn FnMut(PackedValue<'ctx>)>,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    // Classes plan Task 5 (§1.4 step 4): `obj.field = v` or, inside a
    // method body, a bare implicit-`this` `field = v` -- written through
    // the object heap, before every other lvalue shape below (none of
    // which have any storage root a class property could ever be).
    // A whole class property wider than `WIDE_THRESHOLD` is an image copy.
    if matches!(value, LoweredValue::Huge(_))
        && crate::huge::class_prop_root(lhs)
        && crate::huge::try_store(cx, lhs, value)?.is_some()
    {
        return Ok(value);
    }
    if let Some(v) = crate::classes::try_write_field(cx, lhs, value)? {
        return Ok(v);
    }
    // A covergroup's `option.x = v` / `type_option.x = v` (19.7).
    if let Some(v) = crate::covergroup::lower_option_write(cx, lhs, value)? {
        return Ok(v);
    }
    // A vector wider than `WIDE_THRESHOLD` (a whole variable or a select of
    // one): `crate::huge`.
    if crate::huge::try_store(cx, lhs, value)?.is_some() {
        return Ok(value);
    }
    // One collection of a fixed array of them inside such an element.
    if let Some(r) = crate::collections::write_fixed_in_coll(cx, lhs, value) {
        r?;
        return Ok(value);
    }
    // An element of a queue, dynamic or associative array.
    if let Some(r) = crate::collections::write_element(cx, lhs, value) {
        r?;
        return Ok(value);
    }
    // A write into part of a queue / dynamic-array element
    // (`d[i][j] = v`, `q[i].f = v`): a read-modify-write of the element.
    if let Some(elem) = crate::collections::partial_element_target(lhs) {
        return store_partial_element(cx, lhs, elem, value, release);
    }
    // A stream target (`{<<8{a, b}} = v`) unpacks the source at its own
    // width (`crate::stream::unpack_stream`).
    let stream_width = match value {
        LoweredValue::Packed(p) if lhs.kind() == ExpressionKind::Streaming => Some(p.width),
        _ => None,
    };
    let width = stream_width
        .or_else(|| lhs.expr_type().map(|t| t.value_width()).filter(|w| *w > 0))
        .ok_or_else(|| {
            LoweringError::unsupported_at("assignment target has no width", &cx.fn_sym)
        })?;
    let bits = crate::real::to_storage(cx, value, width, "assignment of a >128-bit value")?;
    match release {
        Some(release) => crate::select::lower_assign_releasing(cx, lhs, bits, release)?,
        None => crate::select::lower_assign_to(cx, lhs, bits)?,
    }
    Ok(value)
}

/// A binary operator with at least one real operand: arithmetic and
/// comparisons on two reals (slang converts both sides), or `&&`/`||`
/// (self-determined operands, so one side may be integral).
fn lower_real_binary_expr<'ctx, 'd>(
    cx: &LowerCtx<'ctx, 'd, '_>,
    e: sv_lang::Expression<'d>,
    bop: BinaryOp,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    let left = e.left().ok_or_else(|| {
        LoweringError::unsupported_at("binary op missing left operand", &cx.fn_sym)
    })?;
    let right = e.right().ok_or_else(|| {
        LoweringError::unsupported_at("binary op missing right operand", &cx.fn_sym)
    })?;
    let l = lower_expr_h(cx, left)?;
    let r = lower_expr_h(cx, right)?;
    if matches!(bop, BinaryOp::LogicalAnd | BinaryOp::LogicalOr) {
        let truth = |v: LoweredValue<'ctx>| -> Result<IntValue<'ctx>, LoweringError> {
            Ok(match v {
                LoweredValue::Real(rv) => crate::real::real_truth(cx, rv),
                other => is_definitely_true(
                    cx.ctx,
                    &cx.b,
                    crate::huge::truth_packed(
                        cx,
                        other,
                        crate::huge::TruthMode::Strict,
                        "logical operand of a >128-bit value",
                    )?,
                ),
            })
        };
        let (a, b) = (truth(l)?, truth(r)?);
        let bit = if bop == BinaryOp::LogicalAnd {
            cx.b.build_and(a, b, "real_land").unwrap()
        } else {
            cx.b.build_or(a, b, "real_lor").unwrap()
        };
        let op = OpBuilder {
            ctx: cx.ctx,
            b: &cx.b,
        };
        return Ok(LoweredValue::Packed(op.pack(
            bit,
            cx.ctx.bool_type().const_zero(),
            1,
            cx.repr,
            false,
        )));
    }
    let (LoweredValue::Real(lr), LoweredValue::Real(rr)) = (l, r) else {
        return Err(LoweringError::unsupported_at(
            &format!("binary op {bop:?} between a real and a non-real operand"),
            &cx.fn_sym,
        ));
    };
    crate::real::real_binary(cx, bop, lr, rr).ok_or_else(|| {
        LoweringError::unsupported_at(
            &format!("operator {bop:?} is not defined on real operands"),
            &cx.fn_sym,
        )
    })
}

// Task 9 (CG-090, CG-007): `&&`/`||` genuine short-circuit lowering, for the
// direct if/while/wait/case branch-CONDITION site only (reached solely via
// `lower_condition` above, never from the generic `BinaryOp` dispatch) --
// `is_definitely_true` (CG-007's "X-as-false", not the ternary's per-bit
// X-merge -- CG-091) reduces each operand to a boolean BEFORE combining, and
// the right operand is only ever lowered inside a conditionally-taken basic
// block, so a right operand with a side effect (an assignment, a future
// task/void-function call) genuinely does not execute at runtime when the
// left operand alone already determines the result: `false && rhs`
// short-circuits to `false` without evaluating `rhs`; `true || rhs`
// short-circuits to `true` without evaluating `rhs`. The result is always a
// plain, fully-known boolean (`unk` lane forced to 0) -- per CG-007's
// decision record, `&&`/`||` never produce an X result the way the
// ternary's bitwise merge can. This crisp collapse is spec-correct ONLY
// for this direct-condition context (LRM §12.4); `&&`/`||` used as an
// ordinary value (e.g. an assignment RHS) must instead preserve Table
// 11-4's genuine ambiguity -- see `lower_short_circuit_logical_value`
// below, which is what `lower_expr`'s generic `BinaryOp` arm actually
// calls.
//
// Review fix (r2): "the left operand alone already determines the result"
// above is, per IEEE 1800-2023 §11.4.7, DEFINITELY-FALSE for `&&` (not
// merely "not definitely true") and DEFINITELY-TRUE for `||` -- an
// ambiguous (X/Z-containing) left is not, by itself, sufficient to
// determine either operator's result, so the right operand's side effects
// still execute in that case for both operators. See
// `lower_short_circuit_logical_condition`'s own body comments for the
// exact skip predicates and the resulting merge-value fix this implies for
// `&&`.
fn lower_short_circuit_logical_condition<'ctx, 'd, 'm>(
    cx: &LowerCtx<'ctx, 'd, 'm>,
    e: sv_lang::Expression<'d>,
    is_and: bool,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    let left_expr = e.left().ok_or_else(|| {
        LoweringError::unsupported_at("logical &&/|| missing left operand", &cx.fn_sym)
    })?;
    let right_expr = e.right().ok_or_else(|| {
        LoweringError::unsupported_at("logical &&/|| missing right operand", &cx.fn_sym)
    })?;

    let left_pv = crate::huge::truth_packed(
        cx,
        lower_expr_h(cx, left_expr)?,
        crate::huge::TruthMode::Strict,
        "logical &&/|| on a non-integral (real) left operand",
    )?;
    let left_true = is_definitely_true(cx.ctx, &cx.b, left_pv);

    let entry_block = cx.b.get_insert_block().ok_or_else(|| {
        LoweringError::unsupported_at("logical &&/|| lowered outside a basic block", &cx.fn_sym)
    })?;
    let f = entry_block.get_parent().ok_or_else(|| {
        LoweringError::unsupported_at("logical &&/|| block has no parent function", &cx.fn_sym)
    })?;

    let (eval_name, merge_name) = if is_and {
        ("land_rhs", "land_merge")
    } else {
        ("lor_rhs", "lor_merge")
    };
    let rhs_block = cx.ctx.append_basic_block(f, eval_name);
    let merge_block = cx.ctx.append_basic_block(f, merge_name);

    // Review fix (r2), CG-090/CG-007/CG-091 blocking finding: IEEE
    // 1800-2023 SS11.4.7 only permits skipping the right operand's
    // evaluation when the LEFT operand ALONE is sufficient to determine the
    // *value* result -- for `&&` that is exactly "left is DEFINITELY 0",
    // never merely "left is not definitely true". `is_definitely_true`
    // being false fires on BOTH a definitely-false left AND an ambiguous
    // (X/Z-containing) left (e.g. `x && bump()` with `x` all-X: the result
    // is genuinely X, not determined by the left alone, so `bump()`'s side
    // effect must still run -- verified against iverilog, the project's
    // golden reference, which prints the side effect). So the `&&` arm must
    // branch on a dedicated "left is definitely false" predicate, not on
    // `left_true`/`is_definitely_true(left)`. Mirrors
    // `lower_short_circuit_logical_value`'s `land_known_false` computation
    // (`!left_val && !left_unk`), but computed over `redor4`'s Kleene-OR
    // reduction of `left_pv` first so a dominant known-1 bit amid unknown
    // bits still correctly reads as "not definitely false" (matching the
    // value path's own reduce-before-test structure exactly).
    //
    // The `||` arm's existing `left_true` skip condition is UNCHANGED and
    // already correct: `||`'s right operand is only skippable when the left
    // ALONE is sufficient, i.e. when it is DEFINITELY 1 -- exactly what
    // `is_definitely_true` tests.
    let op = OpBuilder {
        ctx: cx.ctx,
        b: &cx.b,
    };
    let left_reduced = op.redor4(left_pv);
    let left_reduced_val = op.lane_val(left_reduced);
    let left_reduced_unk = op.lane_unk(left_reduced);
    let land_not_val =
        cx.b.build_not(left_reduced_val, "land_cond_not_val")
            .unwrap();
    let land_not_unk =
        cx.b.build_not(left_reduced_unk, "land_cond_not_unk")
            .unwrap();
    let left_definitely_false =
        cx.b.build_and(land_not_val, land_not_unk, "land_cond_known_false")
            .unwrap();

    // `&&`: skip straight to `merge_block` (short-circuit to false) only
    // when the left operand is DEFINITELY FALSE; a definitely-true OR an
    // ambiguous left both require evaluating the right operand, since only
    // a definitely-false left is sufficient to determine the AND result.
    // `||`: only reach `rhs_block` when the left operand is NOT definitely
    // true (a definitely-true left already determines a true result).
    if is_and {
        cx.b.build_conditional_branch(left_definitely_false, merge_block, rhs_block)
            .unwrap();
    } else {
        cx.b.build_conditional_branch(left_true, merge_block, rhs_block)
            .unwrap();
    }
    let short_circuit_pred = entry_block;

    cx.b.position_at_end(rhs_block);
    let right_pv = crate::huge::truth_packed(
        cx,
        lower_expr_h(cx, right_expr)?,
        crate::huge::TruthMode::Strict,
        "logical &&/|| on a non-integral (real) right operand",
    )?;
    let right_true = is_definitely_true(cx.ctx, &cx.b, right_pv);
    // `&&`'s rhs_block is now reachable with an AMBIGUOUS left (not only a
    // definitely-true one, per the gating fix above), so the merge value
    // reached via this block can no longer be `right_true` alone: unlike
    // `||` (where a definitely-true right always dominates a not-definitely-
    // true left under Kleene OR, so `right_true` alone happens to already be
    // exactly correct), `&&`'s Kleene AND has no such dominance from a
    // known-1 -- `X && 1 == X`, not `1`. So this arm's merge value must be
    // `is_definitely_true` of the genuine Kleene-AND combination of the
    // (redor4-reduced) left and right operands, matching
    // `lower_short_circuit_logical_value`'s own `and4` use. This is what
    // keeps `logical_and_with_x_operand_uses_is_definitely_true_not_bitwise_and`
    // (`4'bxxxx && 1'b1` -> else/222, since the AND is ambiguous, not
    // definitely true) correct now that this block is reached on that exact
    // ambiguous-left case.
    let and_merge_true = if is_and {
        let right_reduced = op.redor4(right_pv);
        let and_combined = op.and4(left_reduced, right_reduced);
        is_definitely_true(cx.ctx, &cx.b, and_combined)
    } else {
        right_true
    };
    // Re-fetch the insertion block (not `rhs_block` itself): lowering the
    // right operand may have opened/closed further blocks of its own (e.g.
    // a nested if/&&/||), so the phi's real predecessor is wherever the
    // builder ended up, exactly like the analogous re-fetch after lowering
    // each arm of an eventual ternary.
    let rhs_pred = cx.b.get_insert_block().ok_or_else(|| {
        LoweringError::unsupported_at(
            "logical &&/|| right operand left no open basic block",
            &cx.fn_sym,
        )
    })?;
    cx.b.build_unconditional_branch(merge_block).unwrap();

    cx.b.position_at_end(merge_block);
    let bool_ty = cx.ctx.bool_type();
    let phi = cx.b.build_phi(bool_ty, "logical_result").unwrap();
    let short_circuit_val = if is_and {
        bool_ty.const_zero()
    } else {
        bool_ty.const_int(1, false)
    };
    phi.add_incoming(&[
        (&short_circuit_val, short_circuit_pred),
        (&and_merge_true, rhs_pred),
    ]);
    let result_i1 = phi.as_basic_value().into_int_value();

    Ok(LoweredValue::Packed(op.pack(
        result_i1,
        bool_ty.const_zero(),
        1,
        cx.repr,
        false,
    )))
}

// Review fix (r1), CG-021: `&&`/`||` used as an ORDINARY value (e.g. an
// assignment RHS -- exactly what `logical_and_short_circuits_second_operand`
// /`logical_or_short_circuits_second_operand`, tests/branch_reduction.rs,
// already exercise) is not a branch-condition site, so LRM §12.4's
// "ambiguous -> false" collapse does NOT apply -- LRM SS11.4.7's own Table
// 11-4 does instead: a definite operand combined with an ambiguous one can
// legitimately produce an ambiguous (X) RESULT (`1 && x = x`, `0 && x = 0`),
// not always a definite boolean.
//
// Table 11-4 is exactly Kleene 3-valued AND/OR applied to each operand's own
// truthiness reduction: `redor4` (the same "any known-1 bit dominates, else
// X if any bit is unknown, else 0" reduction `if`'s condition already uses)
// reduces each side to a single tri-state bit, and `and4`/`or4` (this
// crate's existing bitwise 4-state AND/OR, CG-analogous to `if`'s own
// operators) already implement that exact Kleene table on a single bit:
// `0 AND4 x = 0`, `1 AND4 x = x`, `x AND4 x = x` (and the OR mirror) --
// verified directly against the LRM's own Table 11-4 entries.
//
// Genuine short-circuiting is still preserved, but its TRIGGER differs from
// the crisp condition-only lowering above: the right operand's side effects
// must still not execute whenever the LEFT operand ALONE already pins down
// the combined result with zero remaining ambiguity -- for `&&` that means a
// left operand that is DEFINITELY 0 (an ambiguous left is NOT enough: `x &&
// 0` is 0 but `x && 1` is `x`, so the exact result genuinely depends on the
// right operand and it must be evaluated); for `||`, a left operand that is
// DEFINITELY 1.
fn lower_short_circuit_logical_value<'ctx, 'd, 'm>(
    cx: &LowerCtx<'ctx, 'd, 'm>,
    e: sv_lang::Expression<'d>,
    is_and: bool,
    negate_left: bool,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    let left_expr = e.left().ok_or_else(|| {
        LoweringError::unsupported_at("logical &&/|| missing left operand", &cx.fn_sym)
    })?;
    let right_expr = e.right().ok_or_else(|| {
        LoweringError::unsupported_at("logical &&/|| missing right operand", &cx.fn_sym)
    })?;

    let op = OpBuilder {
        ctx: cx.ctx,
        b: &cx.b,
    };

    let left_pv = crate::huge::truth_packed(
        cx,
        lower_expr_h(cx, left_expr)?,
        crate::huge::TruthMode::Ternary,
        "logical &&/|| on a non-integral (real) left operand",
    )?;
    // Reduce (not collapse): a width-1 tri-state PackedValue -- 1 if any bit
    // of the left operand is known-1, X if ambiguous, else 0. Never forced
    // to a crisp i1 the way the condition-site lowering does.
    let mut left_reduced = op.redor4(left_pv);
    // `->` is `!left || right` (LRM 11.4.7): negate the tri-state truth.
    if negate_left {
        left_reduced = op.not4(left_reduced);
    }
    let left_val = op.lane_val(left_reduced);
    let left_unk = op.lane_unk(left_reduced);

    let entry_block = cx.b.get_insert_block().ok_or_else(|| {
        LoweringError::unsupported_at("logical &&/|| lowered outside a basic block", &cx.fn_sym)
    })?;
    let f = entry_block.get_parent().ok_or_else(|| {
        LoweringError::unsupported_at("logical &&/|| block has no parent function", &cx.fn_sym)
    })?;

    // `&&`: skip the right operand only when the left is DEFINITELY 0 --
    // `!left_val && !left_unk` (an ambiguous left, `left_unk=1`, must NOT
    // skip: the exact result still depends on the right operand).
    // `||`: skip the right operand only when the left is DEFINITELY 1 --
    // `left_val` alone suffices, since `redor4`'s own `pack()` guarantees
    // `unk=0` whenever `val=1` (a dominant known-1 bit always wins over any
    // other unknown bit in the reduction).
    let skip_rhs = if is_and {
        let not_val = cx.b.build_not(left_val, "land_not_val").unwrap();
        let not_unk = cx.b.build_not(left_unk, "land_not_unk").unwrap();
        cx.b.build_and(not_val, not_unk, "land_known_false")
            .unwrap()
    } else {
        left_val
    };

    let (eval_name, merge_name) = if is_and {
        ("land_rhs_v", "land_merge_v")
    } else {
        ("lor_rhs_v", "lor_merge_v")
    };
    let rhs_block = cx.ctx.append_basic_block(f, eval_name);
    let merge_block = cx.ctx.append_basic_block(f, merge_name);
    // Skip straight to the merge block (short-circuit, result = the left
    // operand's own reduced value/unk unchanged) when `skip_rhs` holds;
    // otherwise evaluate the right operand in `rhs_block`.
    cx.b.build_conditional_branch(skip_rhs, merge_block, rhs_block)
        .unwrap();
    let short_circuit_pred = entry_block;

    cx.b.position_at_end(rhs_block);
    let right_pv = crate::huge::truth_packed(
        cx,
        lower_expr_h(cx, right_expr)?,
        crate::huge::TruthMode::Ternary,
        "logical &&/|| on a non-integral (real) right operand",
    )?;
    let right_reduced = op.redor4(right_pv);
    let combined = if is_and {
        op.and4(left_reduced, right_reduced)
    } else {
        op.or4(left_reduced, right_reduced)
    };
    let combined_val = op.lane_val(combined);
    let combined_unk = op.lane_unk(combined);
    // Re-fetch the insertion block, not `rhs_block` itself, for the same
    // reason as the condition-site lowering above: lowering the right
    // operand may have opened/closed further blocks of its own.
    let rhs_pred = cx.b.get_insert_block().ok_or_else(|| {
        LoweringError::unsupported_at(
            "logical &&/|| right operand left no open basic block",
            &cx.fn_sym,
        )
    })?;
    cx.b.build_unconditional_branch(merge_block).unwrap();

    cx.b.position_at_end(merge_block);
    let bool_ty = cx.ctx.bool_type();
    let val_phi = cx.b.build_phi(bool_ty, "logical_val").unwrap();
    val_phi.add_incoming(&[(&left_val, short_circuit_pred), (&combined_val, rhs_pred)]);
    let unk_phi = cx.b.build_phi(bool_ty, "logical_unk").unwrap();
    unk_phi.add_incoming(&[(&left_unk, short_circuit_pred), (&combined_unk, rhs_pred)]);

    Ok(LoweredValue::Packed(op.pack(
        val_phi.as_basic_value().into_int_value(),
        unk_phi.as_basic_value().into_int_value(),
        1,
        cx.repr,
        false,
    )))
}

/// `a <-> b` (LRM 11.4.7): both operands are evaluated, each reduced to its
/// tri-state truth; the result is their 1-bit XNOR (x if either is ambiguous).
fn lower_logical_equivalence<'ctx, 'd, 'm>(
    cx: &LowerCtx<'ctx, 'd, 'm>,
    e: sv_lang::Expression<'d>,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    let (Some(le), Some(re)) = (e.left(), e.right()) else {
        return Err(LoweringError::unsupported_at(
            "logical <-> missing operand",
            &cx.fn_sym,
        ));
    };
    let op = OpBuilder {
        ctx: cx.ctx,
        b: &cx.b,
    };
    let side = |x: sv_lang::Expression<'d>| -> Result<PackedValue<'ctx>, LoweringError> {
        let pv = crate::huge::truth_packed(
            cx,
            lower_expr_h(cx, x)?,
            crate::huge::TruthMode::Ternary,
            "logical <-> on a non-integral (real) operand",
        )?;
        Ok(op.redor4(pv))
    };
    let l = side(le)?;
    let r = side(re)?;
    Ok(LoweredValue::Packed(op.not4(op.xor4(l, r))))
}

// Task 10 (CG-040): the side-effect-purity check that gates the ternary's
// `select`-only fast path (CG-041). A PURELY SYNTACTIC recursive walk over
// an expression's own semantic children (`Expression::children()`), with no
// dependency on process-lowering or state (per CG-040's own "lives inside
// sv-sim-codegen, no cross-subsystem dependency" requirement) -- it never
// evaluates or lowers anything, only inspects the AST shape.
//
// Deviation from the plan text (which describes this as "private"): CG-040's
// own acceptance calls for direct unit tests on this function
// ("purity_check_rejects_call_expression" et al., independent of any
// process/state dependency), and this crate's existing precedent for a
// function whose own tests live in an external `tests/*.rs` integration
// file (which can only see a crate's PUBLIC API) is to make that function
// `pub` with a doc comment explaining why -- see `classify_conversion_kind`
// in this same file (Task 7/CG-075) for the identical shape of this exact
// tension. `pub` here, not `pub(crate)`: `tests/ternary.rs` is an external
// crate like any other integration test.
///
/// True if `e` -- and every expression nested inside it -- is free of
/// side-effecting `ExpressionKind`s. This is a FAST-PATH GATE (CG-041):
/// getting a genuinely-pure kind wrong in the conservative direction (i.e.
/// classifying it impure) only costs the eager `select` optimization and
/// falls back to the always-correct `br`+`phi` general path in
/// `lower_ternary`; getting an impure kind wrong in the other direction
/// (classifying it pure) is a real correctness bug -- it would run that
/// side effect unconditionally via the eager `select` path even when the
/// ternary's actual chosen arm doesn't execute it. So every arm below is
/// listed explicitly and the default (both the trailing wildcard, forced by
/// `ExpressionKind` being `#[non_exhaustive]` -- see `sv-lang-kinds`'s
/// `ast_kind.rs`, so no literal match on this enum can itself be closed --
/// and every kind this crate does not otherwise reason about, e.g.
/// `NewArray`/`NewClass`/`NewCovergroup`/`CopyClass`, which genuinely run
/// constructor/initializer code) is IMPURE, never pure.
///
/// Explicitly pure (leaf literals/reads, or composite/structural nodes
/// whose own evaluation has no effect beyond combining already-lowered
/// children -- purity of the whole recurses into those children below):
/// `IntegerLiteral`, `RealLiteral`, `TimeLiteral`,
/// `UnbasedUnsizedIntegerLiteral`, `NullLiteral`, `UnboundedLiteral`,
/// `StringLiteral`, `NamedValue`, `HierarchicalValue`, `BinaryOp`,
/// `ConditionalOp`, `Inside`, `Concatenation`, `Replication`,
/// `ElementSelect`, `RangeSelect`, `MemberAccess`, `Conversion`,
/// `DataType`, `TypeReference`, `LValueReference`,
/// `SimpleAssignmentPattern`, `StructuredAssignmentPattern`,
/// `ReplicatedAssignmentPattern`, `EmptyArgument`, `ValueRange`,
/// `MinTypMax`, `ClockingEvent`, `TaggedUnion`.
///
/// `UnaryOp` is pure except for pre/post increment-or-decrement.
///
/// Explicitly impure: `Call` (system or user -- either could itself contain
/// further side effects and this crate has no purity annotation to
/// distinguish them), `Assignment` (blocking/nonblocking), `NewArray`,
/// `NewClass`, `NewCovergroup`, `CopyClass` (object/covergroup construction
/// and copy-construction run constructor code -- the exact kinds this
/// review flagged a prior `_ => true` catch-all silently mis-classified as
/// pure).
///
/// Conservatively impure (this crate's P0 subset has no lowering for these
/// today and/or their purity is not obviously safe -- `Invalid`,
/// `Streaming`, `ArbitrarySymbol`, `Dist`, `AssertionInstance`): each is
/// listed explicitly below and returns `Ok(false)`.
///
/// Review-fix (r3): ARCHITECTURE.md SS7 item 9 is binding -- "every match on
/// .../`ExpressionKind`/... (in codegen and analysis) must end its wildcard
/// arm in a Barrier/hard-`LoweringError` default, never a silent no-op" --
/// so the trailing wildcard (forced by `ExpressionKind` being
/// `#[non_exhaustive]`, catching only a FUTURE vendored `sv-lang` kind bump)
/// is a hard `LoweringError`, matching this file's own `classify_conversion_
/// kind` precedent, NOT a silently-returned `false`. `false` was safe in the
/// sense that it only cost the CG-041 fast-path optimization, but SS7 item 9
/// bars a silent default on the letter, not just the correctness spirit --
/// a future kind-count bump must fail loudly here, not slip through
/// undiagnosed.
///
/// Task 28 (CG-157): the "Conservatively impure" arm below is this crate's
/// one real conservative/best-effort branch -- classifying `Invalid`/
/// `Streaming`/`ArbitrarySymbol`/`Dist`/`AssertionInstance` as impure is a
/// safe default (costs only the CG-041 fast path, never a correctness bug),
/// not a proven-correct classification, since this crate has no lowering
/// (and in most cases no purity reasoning at all) for any of them. `sink`
/// gets a `record_fallback` call there, naming the exact kind matched, so
/// an attached harness can observe every time this function took the
/// conservative branch instead of a fully-reasoned one.
pub fn is_side_effect_free(
    e: &sv_lang::Expression,
    fn_sym: &sv_lang::Symbol,
    sink: &dyn crate::coverage::CoverageSink,
) -> Result<bool, LoweringError> {
    let self_pure = match e.kind() {
        // --- Pure leaves and pure structural/composite nodes. ---
        ExpressionKind::IntegerLiteral
        | ExpressionKind::RealLiteral
        | ExpressionKind::TimeLiteral
        | ExpressionKind::UnbasedUnsizedIntegerLiteral
        | ExpressionKind::NullLiteral
        | ExpressionKind::UnboundedLiteral
        | ExpressionKind::StringLiteral
        | ExpressionKind::NamedValue
        | ExpressionKind::HierarchicalValue
        | ExpressionKind::BinaryOp
        | ExpressionKind::Inside
        | ExpressionKind::Concatenation
        | ExpressionKind::Replication
        | ExpressionKind::RangeSelect
        | ExpressionKind::Conversion
        | ExpressionKind::DataType
        | ExpressionKind::TypeReference
        | ExpressionKind::LValueReference
        | ExpressionKind::SimpleAssignmentPattern
        | ExpressionKind::StructuredAssignmentPattern
        | ExpressionKind::ReplicatedAssignmentPattern
        | ExpressionKind::EmptyArgument
        | ExpressionKind::ValueRange
        | ExpressionKind::MinTypMax
        | ExpressionKind::ClockingEvent
        | ExpressionKind::TaggedUnion => true,

        // --- Conditionally pure. ---
        // `v.M` of a tagged union checks `v`'s tag at run time and reports
        // an error (11.9); an `e matches p ? a : b` binds its pattern's
        // variables (12.6.3).
        // `h.m` on a class handle reports a null-handle error when `h` is
        // null (8.4), which an unselected ?: arm must never do (11.4.11).
        ExpressionKind::MemberAccess => !e
            .operand()
            .and_then(|b| b.expr_type())
            .is_some_and(|t| t.is_tagged_union() || t.is_class()),
        // `a[k]` of an associative array warns when `k` is nonexistent or
        // invalid (7.8.6), which an unselected ?: arm must never do.
        ExpressionKind::ElementSelect => !e
            .select_value()
            .and_then(|b| b.expr_type())
            .is_some_and(|t| t.is_associative_array()),
        ExpressionKind::ConditionalOp => !e.conditions().iter().any(|c| c.pattern().is_some()),
        ExpressionKind::UnaryOp => !matches!(
            e.unary_op(),
            Some(
                UnaryOp::Preincrement
                    | UnaryOp::Predecrement
                    | UnaryOp::Postincrement
                    | UnaryOp::Postdecrement
            )
        ),

        // --- Explicitly, unconditionally impure. ---
        ExpressionKind::Call
        | ExpressionKind::Assignment
        | ExpressionKind::NewArray
        | ExpressionKind::NewClass
        | ExpressionKind::NewCovergroup
        | ExpressionKind::CopyClass => false,

        // --- Conservatively impure: not obviously safe, and/or outside
        // this crate's currently-supported subset. ---
        ExpressionKind::Invalid
        | ExpressionKind::Streaming
        | ExpressionKind::ArbitrarySymbol
        | ExpressionKind::Dist
        | ExpressionKind::AssertionInstance => {
            // CG-157: this is the conservative/best-effort branch itself --
            // report it before returning the safe-default `false`.
            sink.record_fallback(
                crate::coverage::construct_id_for_expression_kind(e.kind()),
                crate::coverage::loc_for_expr(e),
            );
            false
        }

        // `ExpressionKind` is `#[non_exhaustive]`: a future vendored
        // `sv-lang` bump that adds a variant this match has never seen must
        // NOT silently fall through as either pure or (the pre-r3 shape)
        // a plain `false` -- ARCHITECTURE.md SS7 item 9 requires a hard,
        // diagnosable error so the gap is caught at run time, not silently
        // mis-optimized.
        _ => {
            return Err(LoweringError::unsupported_at(
                &format!(
                    "is_side_effect_free: unrecognized ExpressionKind {:?} \
                     (sv-lang's ExpressionKind::COUNT likely grew past this \
                     match -- ARCHITECTURE.md SS7 item 9: no silent \
                     no-op default)",
                    e.kind()
                ),
                fn_sym,
            ));
        }
    };
    if !self_pure {
        return Ok(false);
    }
    for c in e.children().iter() {
        if let Some(ce) = c.as_expression()
            && !is_side_effect_free(&ce, fn_sym, sink)?
        {
            return Ok(false);
        }
    }
    Ok(true)
}

// Task 10 (CG-039, CG-040, CG-041, CG-008): ternary `?:` (`ExpressionKind::
// ConditionalOp`) -- formula per docs/design/codegen.md §2 / the plan:
//
// - The condition is always evaluated exactly once, regardless of the arms'
//   purity.
// - CG-041 fast path: when BOTH arms are `is_side_effect_free` (CG-040),
//   eagerly lower both (safe -- neither has an effect to duplicate) and
//   combine entirely via `OpBuilder::select` (the whole packed val+unk
//   value in one LLVM `select` instruction, see its own doc comment) --
//   NO new basic blocks, so a pure-armed ternary costs nothing beyond one
//   extra `select` over the general shape below. The ambiguous-condition
//   merge (CG-008, `ternary_merge4`) is still computed unconditionally here
//   (cheap, branch-free) and selected in only when neither `cond_true` nor
//   `cond_false` holds.
// - General path (CG-039): a determinate condition evaluates ONLY the
//   chosen arm -- genuine `br`+`phi` control flow across three blocks
//   (then-only / else-only / both-arms-evaluated-and-merged), so the
//   unchosen arm's side effects never execute for a determinate condition,
//   fixing the "evaluates both arms" bug. An ambiguous condition takes the
//   third block, which evaluates BOTH arms (their side effects both occur,
//   intentionally -- `ternary_ambiguous_condition_evaluates_both_arms`,
//   tests/ternary.rs, proves this is spec-correct per CG-008's own merge
//   semantics, not a regression) and merges via `ternary_merge4`.
/// [`lower_ternary`] with a real or string result.
fn lower_nonintegral_ternary<'ctx, 'd>(
    cx: &LowerCtx<'ctx, 'd, '_>,
    cond_true: IntValue<'ctx>,
    cond_false: IntValue<'ctx>,
    then_expr: sv_lang::Expression<'d>,
    else_expr: sv_lang::Expression<'d>,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    // An arm as a real (`f64`) or a string id (`i64`).
    let arm = |e: sv_lang::Expression<'d>| -> Result<inkwell::values::BasicValueEnum<'ctx>, LoweringError> {
        match lower_expr(cx, e)? {
            LoweredValue::Real(r) => Ok(crate::real::as_f64(cx, r.llvm).into()),
            LoweredValue::Str(st) => Ok(st.llvm.into()),
            _ => Err(LoweringError::unsupported_at(
                "a conditional operator arm of a real or string result is neither",
                &cx.fn_sym,
            )),
        }
    };
    let f = cx
        .b
        .get_insert_block()
        .and_then(|bb| bb.get_parent())
        .ok_or_else(|| LoweringError::unsupported_at("ternary outside a function", &cx.fn_sym))?;
    // Each arm is lowered exactly once (a chain of nested conditionals in an arm would
    // otherwise double per level): `then_block` runs unless the condition is definitely
    // false, `else_block` unless it is definitely true, and an ambiguous condition falls
    // from the first into the second so both arms run.
    let then_block = cx.ctx.append_basic_block(f, "tern_then");
    let else_block = cx.ctx.append_basic_block(f, "tern_else");
    let merge_block = cx.ctx.append_basic_block(f, "tern_merge");
    let entry_pred = cx.b.get_insert_block().expect("positioned");
    cx.b.build_conditional_branch(cond_false, else_block, then_block)
        .unwrap();

    cx.b.position_at_end(then_block);
    let then_v = arm(then_expr)?;
    let then_pred = cx.b.get_insert_block().expect("positioned");
    cx.b.build_conditional_branch(cond_true, merge_block, else_block)
        .unwrap();

    cx.b.position_at_end(else_block);
    let then_seen = cx.b.build_phi(then_v.get_type(), "tern_then_seen").unwrap();
    let zero: inkwell::values::BasicValueEnum<'ctx> = if then_v.is_float_value() {
        cx.ctx.f64_type().const_zero().into()
    } else {
        then_v.get_type().into_int_type().const_zero().into()
    };
    then_seen.add_incoming(&[(&zero, entry_pred), (&then_v, then_pred)]);
    let a = then_seen.as_basic_value();
    let b = arm(else_expr)?;
    let is_real = a.is_float_value();
    if is_real != b.is_float_value() {
        return Err(LoweringError::unsupported_at(
            "conditional operator arms of different types",
            &cx.fn_sym,
        ));
    }
    let merged: inkwell::values::BasicValueEnum<'ctx> = if is_real {
        let (a, b) = (a.into_float_value(), b.into_float_value());
        let same =
            cx.b.build_float_compare(inkwell::FloatPredicate::OEQ, a, b, "tern_same")
                .unwrap();
        cx.b.build_select(same, a, cx.ctx.f64_type().const_zero(), "tern_real")
            .unwrap()
    } else {
        let (a, b) = (a.into_int_value(), b.into_int_value());
        let same =
            cx.b.build_int_compare(inkwell::IntPredicate::EQ, a, b, "tern_same")
                .unwrap();
        let empty = crate::strings::literal(cx, b"").llvm;
        cx.b.build_select(same, a, empty, "tern_str").unwrap()
    };
    let else_val =
        cx.b.build_select(cond_false, b, merged, "tern_else_val")
            .unwrap();
    let else_pred = cx.b.get_insert_block().expect("positioned");
    cx.b.build_unconditional_branch(merge_block).unwrap();

    cx.b.position_at_end(merge_block);
    let phi = cx.b.build_phi(then_v.get_type(), "tern_result").unwrap();
    phi.add_incoming(&[(&then_v, then_pred), (&else_val, else_pred)]);
    let v = phi.as_basic_value();
    Ok(if is_real {
        LoweredValue::Real(RealValue {
            llvm: v.into_float_value(),
        })
    } else {
        LoweredValue::Str(crate::strings::StrValue {
            llvm: v.into_int_value(),
        })
    })
}

fn lower_ternary<'ctx, 'd, 'm>(
    cx: &LowerCtx<'ctx, 'd, 'm>,
    e: sv_lang::Expression<'d>,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    let conds = e.conditions();
    if conds.len() != 1 || conds[0].pattern().is_some() {
        return crate::tagged::lower_pattern_ternary(cx, e);
    }
    let cond_expr = conds[0].expr();
    let then_expr = e.true_value().ok_or_else(|| {
        LoweringError::unsupported_at("ternary missing true-value arm", &cx.fn_sym)
    })?;
    let else_expr = e.false_value().ok_or_else(|| {
        LoweringError::unsupported_at("ternary missing false-value arm", &cx.fn_sym)
    })?;

    let op = OpBuilder {
        ctx: cx.ctx,
        b: &cx.b,
    };
    let cond_pv = crate::huge::truth_packed(
        cx,
        lower_expr_h(cx, cond_expr)?,
        crate::huge::TruthMode::Ternary,
        "ternary condition must be an integral (non-real) value",
    )?;
    // Task 10 review-fix (r3): the ternary's own condition-truth primitive,
    // NOT `is_definitely_true` (that one is CG-007's stricter if/while/&&/||
    // rule -- requires the WHOLE condition known -- which spuriously routed
    // a condition like `4'b1xxx` (a known-1 bit plus unrelated X bits) onto
    // the ambiguous `ternary_merge4` path instead of cleanly selecting the
    // then-arm; see `ternary_is_definitely_true`'s doc comment).
    let cond_true = ternary_is_definitely_true(cx.ctx, &cx.b, cond_pv);
    let cond_false = is_definitely_false(cx.ctx, &cx.b, cond_pv);

    // A real or string result (LRM 1800-2023 §11.4.11): the chosen arm; for
    // an ambiguous condition both arms, their value if they are equal, else
    // the type's default (Table 7-1: 0.0, "").
    if e.expr_type()
        .is_some_and(|t| t.is_floating() || t.is_string())
    {
        return lower_nonintegral_ternary(cx, cond_true, cond_false, then_expr, else_expr);
    }

    // CG-159 scope note: no `sv-sim-harness` crate exists anywhere in this
    // workspace yet to attach a real `CoverageSink` to (see
    // `coverage.rs`'s module doc comment), and `LowerCtx` carries no sink
    // field of its own -- threading one through every `LowerCtx`
    // construction site across this crate is a wider change than this
    // task's scope, so the production lowering pipeline uses
    // `NoopCoverageSink` here. The real call-site wiring this task
    // delivers (CG-157) is exercised directly against
    // `is_side_effect_free`, not through this production path -- see
    // `tests/coverage_sink.rs::record_fallback_called_on_every_
    // conservative_path`.
    if is_side_effect_free(&then_expr, &cx.fn_sym, &crate::coverage::NoopCoverageSink)?
        && is_side_effect_free(&else_expr, &cx.fn_sym, &crate::coverage::NoopCoverageSink)?
    {
        let then_pv = lower_expr(cx, then_expr)?.into_packed(
            "ternary then-arm must be an integral (non-real) value",
            &cx.fn_sym,
        )?;
        let else_pv = lower_expr(cx, else_expr)?.into_packed(
            "ternary else-arm must be an integral (non-real) value",
            &cx.fn_sym,
        )?;
        if then_pv.width != else_pv.width {
            return Err(LoweringError::unsupported_at(
                "ternary then/else arms have mismatched widths (expected \
                 slang's own inserted Conversion nodes to have already \
                 equalized them)",
                &cx.fn_sym,
            ));
        }
        let merged = op.ternary_merge4(then_pv, else_pv);
        let inner = op.select(cond_false, else_pv, merged);
        let result = op.select(cond_true, then_pv, inner);
        return Ok(LoweredValue::Packed(result));
    }

    let entry_block = cx.b.get_insert_block().ok_or_else(|| {
        LoweringError::unsupported_at("ternary lowered outside a basic block", &cx.fn_sym)
    })?;
    let f = entry_block.get_parent().ok_or_else(|| {
        LoweringError::unsupported_at("ternary block has no parent function", &cx.fn_sym)
    })?;

    // Each arm is lowered exactly once (nested conditionals in an arm would otherwise
    // double the emitted code per level, CG-039): `then_block` runs unless the condition
    // is definitely false, `else_block` unless it is definitely true; an ambiguous
    // condition falls from the first into the second so both arms run and merge (CG-008).
    let then_block = cx.ctx.append_basic_block(f, "tern_then");
    let else_block = cx.ctx.append_basic_block(f, "tern_else");
    let merge_block = cx.ctx.append_basic_block(f, "tern_merge");
    cx.b.build_conditional_branch(cond_false, else_block, then_block)
        .unwrap();

    cx.b.position_at_end(then_block);
    let then_pv = lower_expr(cx, then_expr)?.into_packed(
        "ternary then-arm must be an integral (non-real) value",
        &cx.fn_sym,
    )?;
    let then_pred = cx.b.get_insert_block().ok_or_else(|| {
        LoweringError::unsupported_at("ternary then-arm left no open basic block", &cx.fn_sym)
    })?;
    cx.b.build_conditional_branch(cond_true, merge_block, else_block)
        .unwrap();

    cx.b.position_at_end(else_block);
    let seen =
        cx.b.build_phi(then_pv.llvm.get_type(), "tern_then_seen")
            .unwrap();
    let zero = then_pv.llvm.get_type().const_zero();
    seen.add_incoming(&[(&zero, entry_block), (&then_pv.llvm, then_pred)]);
    let seen_pv = PackedValue {
        llvm: seen.as_basic_value().into_int_value(),
        ..then_pv
    };
    let else_pv = lower_expr(cx, else_expr)?.into_packed(
        "ternary else-arm must be an integral (non-real) value",
        &cx.fn_sym,
    )?;
    if then_pv.width != else_pv.width {
        return Err(LoweringError::unsupported_at(
            "ternary then/else arms have mismatched widths (expected \
             slang's own inserted Conversion nodes to have already \
             equalized them)",
            &cx.fn_sym,
        ));
    }
    let merged_pv = op.ternary_merge4(seen_pv, else_pv);
    let else_val = op.select(cond_false, else_pv, merged_pv);
    let else_pred = cx.b.get_insert_block().ok_or_else(|| {
        LoweringError::unsupported_at("ternary else-arm left no open basic block", &cx.fn_sym)
    })?;
    cx.b.build_unconditional_branch(merge_block).unwrap();

    cx.b.position_at_end(merge_block);
    let phi =
        cx.b.build_phi(then_pv.llvm.get_type(), "tern_result")
            .unwrap();
    phi.add_incoming(&[(&then_pv.llvm, then_pred), (&else_val.llvm, else_pred)]);
    Ok(LoweredValue::Packed(PackedValue {
        llvm: phi.as_basic_value().into_int_value(),
        width: then_pv.width,
        repr: cx.repr,
        signed: then_pv.signed,
    }))
}

/// An `alloca` in the current function's entry block: a call inside an SV
/// loop is a back-edge within one activation, so a call-site `alloca` would
/// grow the stack on every iteration.
pub(crate) fn entry_alloca<'ctx, T: inkwell::types::BasicType<'ctx>>(
    cx: &crate::expr::LowerCtx<'ctx, '_, '_>,
    ty: T,
    name: &str,
) -> PointerValue<'ctx> {
    crate::types::entry_alloca_in(&cx.b, ty, name)
}

/// Whether `e` is a replication with a constant count of zero (`{0{x}}`),
/// which as a concatenation operand contributes no bits (LRM 1800-2023
/// §11.4.12.1).
pub(crate) fn is_zero_replication(e: &sv_lang::Expression<'_>) -> bool {
    e.kind() == ExpressionKind::Replication
        && e.replication_count()
            .and_then(|c| c.constant_value())
            .is_some_and(|v| v.as_integer().and_then(|i| i.as_u64()) == Some(0))
}

/// `lhs = value` where `lhs` selects into PART of a queue or dynamic-array
/// element (`d[i][j]`, `q[i].f`, `d[i][3]`; `elem` is the `c[i]` it bottoms
/// out at): the element is read into a shadow temporary, `lhs` is written
/// into that through the ordinary lvalue path ([`LowerCtx::coll_temp`]), and
/// the temporary is stored back with `c[i] = temp`, so an out-of-range `i`
/// stays a no-op (LRM 7.5.6, 7.10.2). A collection element holds no handle,
/// so there is nothing for a releasing store to release.
fn store_partial_element<'ctx, 'd>(
    cx: &LowerCtx<'ctx, 'd, '_>,
    lhs: sv_lang::Expression<'d>,
    elem: sv_lang::Expression<'d>,
    value: LoweredValue<'ctx>,
    _release: Option<&mut dyn FnMut(PackedValue<'ctx>)>,
) -> Result<LoweredValue<'ctx>, LoweringError> {
    let op = OpBuilder {
        ctx: cx.ctx,
        b: &cx.b,
    };
    let cur = crate::collections::read_element(cx, elem)
        .ok_or_else(|| LoweringError::unsupported_at("a non-collection element", &cx.fn_sym))??
        .into_packed("a collection element written in part", &cx.fn_sym)?;
    let ptr = entry_alloca(cx, op.packed_type(cur.width, cx.repr), "coll_part");
    cx.b.build_store(ptr, cur.llvm).unwrap();
    let width = lhs
        .expr_type()
        .map(|t| t.value_width())
        .filter(|w| *w > 0)
        .ok_or_else(|| {
            LoweringError::unsupported_at("assignment target has no width", &cx.fn_sym)
        })?;
    let bits = crate::real::to_storage(cx, value, width, "assignment of a >128-bit value")?;
    let saved = cx.coll_temp.replace(Some(ptr));
    let r = crate::select::lower_assign_to(cx, lhs, bits);
    cx.coll_temp.set(saved);
    r?;
    let new = PackedValue {
        llvm: cx
            .b
            .build_load(op.packed_type(cur.width, cx.repr), ptr, "coll_part_new")
            .unwrap()
            .into_int_value(),
        ..cur
    };
    let store = || {
        crate::collections::write_element(cx, elem, LoweredValue::Packed(new)).unwrap_or_else(
            || {
                Err(LoweringError::unsupported_at(
                    "a non-collection element",
                    &cx.fn_sym,
                ))
            },
        )
    };
    // An associative array's store would create the key even when the select
    // chain was out of range (and so wrote nothing into the temporary).
    let assoc = elem
        .select_value()
        .and_then(|b| crate::collections::coll_of_expr(&b))
        .is_some_and(|c| c.assoc);
    if assoc {
        let ok = crate::collections::chain_in_range(cx, lhs, elem)?;
        crate::collections::guarded_by(cx, ok, store)?;
    } else {
        store()?;
    }
    Ok(value)
}

#[cfg(test)]
mod tests {
    use super::*;

    /// `mem_row_index_i64` widens an SV index to `i64` following the index
    /// expression's OWN signedness: an unsigned 8-bit `0xC8` is 200, a
    /// signed one is -56 (out of range for a `[0:255]` memory -- iverilog
    /// reads X for exactly that). `mem_row_index` then subtracts the
    /// declared lower bound, so negative ranges (`[-2:1]`) work too. Uses an
    /// index with its top bit set, where the two extensions differ, so a
    /// mutant swapping either one is caught. (Earlier this test asserted
    /// zero-extension regardless of signedness -- correct only while the
    /// raw index WAS the storage row.)
    #[test]
    fn mem_row_index_i64_extends_a_narrow_index_by_its_own_signedness() {
        let design = crate::frontend::compile_source("module m; endmodule\n").unwrap();
        let body = design
            .top_instances()
            .next()
            .unwrap()
            .instance_body()
            .unwrap();

        let llvm_ctx = Context::create();
        let module = llvm_ctx.create_module("test_mem_row_index_i64");
        let i8_ty = llvm_ctx.i8_type();
        let i64_ty = llvm_ctx.i64_type();
        let fn_ty = i64_ty.fn_type(&[i8_ty.into()], false);
        let f = module.add_function("test_fn", fn_ty, None);
        let entry = llvm_ctx.append_basic_block(f, "entry");
        let b = llvm_ctx.create_builder();
        b.position_at_end(entry);
        let param = f.get_nth_param(0).unwrap().into_int_value();

        let cx = LowerCtx {
            ctx: &llvm_ctx,
            b,
            repr: ValueRepr::FourState,
            locals: HashMap::new(),
            fn_sym: body,
            module: &module,
            mem_signals: HashMap::new(),
            mem_accessor: None,
            lvalue_ref: Default::default(),
            unbounded: Default::default(),
            coll_temp: Default::default(),
            iter: Default::default(),
            loop_exits: Default::default(),
            write_through: None,
            cells: Default::default(),
            copy_out: Default::default(),
            ctor_this: Default::default(),
            decl_scale: Default::default(),
            inline_this: Default::default(),
            end_flags: Default::default(),
            specify_dest: Default::default(),
            elem_refs: Default::default(),
            block_exits: Default::default(),
            deferred_start: Default::default(),
        };
        let op = OpBuilder {
            ctx: cx.ctx,
            b: &cx.b,
        };

        // `param` is a genuine (non-constant) SSA value, so the builder
        // cannot constant-fold the extend away -- a real `zext`/`sext`
        // instruction is guaranteed to appear in the emitted IR either way.
        let idx = PackedValue {
            llvm: param,
            width: 8,
            repr: ValueRepr::TwoState,
            signed: false,
        };
        let row_index = mem_row_index_i64(&op, &cx, idx);
        cx.b.build_return(Some(&row_index)).unwrap();

        // The same index, typed signed.
        let fs = module.add_function("test_fn_signed", fn_ty, None);
        let entry_s = llvm_ctx.append_basic_block(fs, "entry");
        cx.b.position_at_end(entry_s);
        let idx_s = PackedValue {
            llvm: fs.get_nth_param(0).unwrap().into_int_value(),
            width: 8,
            repr: ValueRepr::TwoState,
            signed: true,
        };
        let row_index_s = mem_row_index_i64(&op, &cx, idx_s);
        cx.b.build_return(Some(&row_index_s)).unwrap();

        assert!(
            f.verify(true) && fs.verify(true),
            "the emitted IR must pass LLVM's own verifier"
        );

        let jit = crate::jit::Jit::from_module(module).expect("JIT engine construction failed");
        let raw = jit
            .get_raw("test_fn")
            .expect("test_fn must be a real symbol in the JIT'd module");
        // SAFETY: `test_fn` was declared and defined above as a genuine
        // `extern "C" fn(i8) -> i64` inside this same JIT'd module, and
        // `f.verify(true)` already confirmed the emitted IR is well-formed
        // before this call.
        let callable: extern "C" fn(i8) -> i64 =
            unsafe { std::mem::transmute::<*const (), extern "C" fn(i8) -> i64>(raw) };

        // 0xC8 == 200u8 == -56i8.
        let result = callable(-56i8);
        assert_eq!(
            result, 200,
            "an UNSIGNED 8-bit index 0xC8 must zero-extend to 200"
        );

        let raw_s = jit
            .get_raw("test_fn_signed")
            .expect("test_fn_signed must be a real symbol in the JIT'd module");
        // SAFETY: same as `callable` above -- `test_fn_signed` has the
        // identical `extern "C" fn(i8) -> i64` signature and verified.
        let callable_s: extern "C" fn(i8) -> i64 =
            unsafe { std::mem::transmute::<*const (), extern "C" fn(i8) -> i64>(raw_s) };
        assert_eq!(
            callable_s(-56i8),
            -56,
            "a SIGNED 8-bit index 0xC8 is -56 and must sign-extend"
        );
    }
}