cpclib-asm 0.6.0

cpclib libraries related to z80 assembling
Documentation
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
pub mod delayed_command;
pub mod file;
pub mod function;
pub mod list;
pub mod listing_output;
pub mod r#macro;
pub mod matrix;
pub mod page_info;
pub mod report;
pub mod save_command;
pub mod stable_ticker;
pub mod symbols_output;

pub mod processed_token;

use std::borrow::Borrow;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::fmt;
use std::fmt::{Debug, Display};
use std::io::Write;
use std::path::PathBuf;
use std::sync::{Arc, RwLock};
use std::time::Instant;

use cpclib_basic::*;
use cpclib_common::bitvec::prelude::BitVec;
use cpclib_common::itertools::Itertools;
use cpclib_common::lazy_static::__Deref;
#[cfg(not(target_arch = "wasm32"))]
use cpclib_common::rayon::prelude::*;
use cpclib_common::smallvec::SmallVec;
use cpclib_sna::*;
use cpclib_tokens::ToSimpleToken;

use self::function::{Function, FunctionBuilder, HardCodedFunction};
use self::listing_output::*;
use self::processed_token::ProcessedToken;
use self::report::SavedFile;
use self::symbols_output::SymbolOutputGenerator;
use crate::assembler::processed_token::visit_processed_tokens;
use crate::delayed_command::*;
use crate::page_info::PageInformation;
use crate::preamble::*;
use crate::report::Report;
use crate::save_command::*;
use crate::stable_ticker::*;
use crate::{AssemblingOptions, PhysicalAddress};

/// Use smallvec to put stuff on the stack not the heap and (hope so) speed up assembling
const MAX_SIZE: usize = 4;
const REPEAT_START_VALUE: i32 = 1;
const MMR_PAGES_SELECTION: [u8; 9] = [
    0xC0,
    0b11_000_0_01,
    0b11_001_0_01,
    0b11_010_0_01,
    0b11_011_0_01,
    0b11_100_0_01,
    0b11_101_0_01,
    0b11_110_0_01,
    0b11_111_0_01
];

#[allow(missing_docs)]
pub type Bytes = SmallVec<[u8; MAX_SIZE]>;

/// Add the encoding of an indexed structure
fn add_index(m: &mut Bytes, idx: i32) -> Result<(), AssemblerError> {
    if idx < -127 || idx > 128 {
        // Err(format!("Index error {}", idx).into())
        eprintln!("Index error {}", idx);
    }
    // else {
    let val = (idx & 0xFF) as u8;
    add_byte(m, val);
    Ok(())
    // }
}

fn add_byte(m: &mut Bytes, b: u8) {
    m.push(b);
}

fn add_word(m: &mut Bytes, w: u16) {
    m.push((w % 256) as u8);
    m.push((w / 256) as u8);
}

fn add_index_register_code(m: &mut Bytes, r: IndexRegister16) {
    add_byte(m, indexed_register16_to_code(r));
}

const DD: u8 = 0xDD;
const FD: u8 = 0xFD;

trait MyDefault {
    fn default() -> Self;
}

/// ! Lots of things will probably be inspired from RASM
type Bank = [u8; 0x1_0000];
impl MyDefault for Bank {
    fn default() -> Bank {
        [0; 0x1_0000]
    }
}

/// Number of banks allowed in a snapshot
const NB_BANKS: usize = 9;

/// The Banks of interest
/// only one is added at first. Others are created on demand
type Banks = Vec<Bank>;
impl MyDefault for Banks {
    fn default() -> Self {
        vec![Bank::default()]
    }
}

/// Several passes are needed to properly assemble a source file.
/// This structure allows to code which pass is going to be analysed.
/// First pass consists in collecting the various labels to manipulate and so on. Some labels stay unknown at this moment.
/// Second pass serves to get the final values
#[derive(Clone, Copy, Debug)]
pub enum AssemblingPass {
    Uninitialized,
    FirstPass,
    SecondPass, // and subsequent
    Finished,
    ListingPass // pass dedicated to the listing production
}
impl fmt::Display for AssemblingPass {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let content = match self {
            AssemblingPass::Uninitialized => "Uninitialized",
            AssemblingPass::FirstPass => "1",
            AssemblingPass::SecondPass => "2",
            AssemblingPass::Finished => "Finished",
            AssemblingPass::ListingPass => "Listing"
        };
        write!(f, "{}", content)
    }
}

#[allow(missing_docs)]
#[allow(unused)]
impl AssemblingPass {
    fn is_uninitialized(self) -> bool {
        match self {
            AssemblingPass::Uninitialized => true,
            _ => false
        }
    }

    pub fn is_finished(self) -> bool {
        match self {
            AssemblingPass::Finished => true,
            _ => false
        }
    }

    pub fn is_first_pass(self) -> bool {
        match self {
            AssemblingPass::FirstPass => true,
            _ => false
        }
    }

    pub fn is_second_pass(self) -> bool {
        match self {
            AssemblingPass::SecondPass => true,
            _ => false
        }
    }

    pub fn is_listing_pass(self) -> bool {
        match self {
            AssemblingPass::ListingPass => true,
            _ => false
        }
    }

    fn next_pass(self) -> Self {
        match self {
            AssemblingPass::Uninitialized => AssemblingPass::FirstPass,
            AssemblingPass::FirstPass => AssemblingPass::SecondPass,
            AssemblingPass::SecondPass => AssemblingPass::Finished,
            AssemblingPass::Finished | AssemblingPass::ListingPass => panic!()
        }
    }
}

/// Trait to implement for each type of token.
/// it allows to drive the appropriate data vonversion
pub trait Visited {
    /// Make all the necessary for the given token
    fn visited(&self, env: &mut Env) -> Result<(), AssemblerError>;
}

impl Visited for Token {
    fn visited(&self, env: &mut Env) -> Result<(), AssemblerError> {
        visit_token(self, env)
    }
}

impl Visited for LocatedToken {
    fn visited(&self, env: &mut Env) -> Result<(), AssemblerError> {
        // dbg!(env.output_address, self.as_token());
        visit_located_token(self, env).map_err(|e| e.locate(self.span().clone()))
    }
}

type AssemblerWarning = AssemblerError;

/// Store all the necessary information when handling a crunched section
#[derive(Clone)]
struct CrunchedSectionState {
    /// Start of the crunched section for code assembled from the sources.
    /// None for code assembled from tokens
    // mainly usefull for error messages; nothing more
    crunched_section_start: Option<Z80Span>
}

impl CrunchedSectionState {
    pub fn new(span: Option<Z80Span>) -> Self {
        CrunchedSectionState {
            crunched_section_start: span
        }
    }
}

#[derive(Clone)]
pub struct CharsetEncoding {
    lut: std::collections::HashMap<char, i32>
}

impl CharsetEncoding {
    pub fn new() -> Self {
        let mut enc = Self {
            lut: Default::default()
        };
        enc.reset();
        enc
    }

    pub fn reset(&mut self) {
        self.lut.clear()
    }

    pub fn update(&mut self, spec: &CharsetFormat, env: &Env) -> Result<(), AssemblerError> {
        match spec {
            CharsetFormat::Reset => self.reset(),
            CharsetFormat::CharsList(l, s) => {
                let mut s = env.resolve_expr_must_never_fail(s)?.int()?;
                for c in l.iter() {
                    self.lut.insert(*c, s);
                    s += 1;
                }
            }
            CharsetFormat::Char(c, i) => {
                let c = env.resolve_expr_must_never_fail(c)?.char()?;
                let i = env.resolve_expr_must_never_fail(i)?.int()?;
                self.lut.insert(c, i);
            }
            CharsetFormat::Interval(a, b, s) => {
                let a = env.resolve_expr_must_never_fail(a)?.char()?;
                let b = env.resolve_expr_must_never_fail(b)?.char()?;
                let mut s = env.resolve_expr_must_never_fail(s)?.int()?;
                for c in a..=b {
                    self.lut.insert(c, s);
                    s += 1;
                }
            }
        }

        Ok(())
    }

    pub fn transform_char(&self, c: char) -> u8 {
        self.lut.get(&c).cloned().unwrap_or(c as i32) as _
    }

    pub fn transform_string(&self, s: &str) -> Vec<u8> {
        s.chars().map(|c| self.transform_char(c)).collect_vec()
    }
}

#[derive(Clone, Debug)]
struct Section {
    /// Name of the section
    name: String,
    /// Start address of the section
    start: u16,
    /// Last (included) address of the section
    stop: u16,
    /// Expected mmr configuration
    mmr: u8,

    output_adr: u16,
    code_adr: u16
}

impl Section {
    fn new(name: &str, start: u16, stop: u16, mmr: u8) -> Self {
        Section {
            mmr,
            name: name.to_owned(),
            start,
            stop,

            output_adr: start,
            code_adr: start
        }
    }

    fn contains(&self, addr: u16) -> bool {
        addr >= self.start && addr <= self.stop
    }

    fn new_pass(&mut self) {
        self.output_adr = self.start;
        self.code_adr = self.start;
    }
}

/// Environment of the assembly
#[allow(missing_docs)]
pub struct Env {
    /// Current pass
    pass: AssemblingPass,
    pub(crate) ctx: ParserContext,
    real_nb_passes: usize,
    /// If true at the end of the pass, can prematurely stop the assembling
    /// Hidden in a rwlock to allow a modification even in non mutable state
    can_skip_next_passes: RwLock<bool>,
    /// An issue in a crunched section requires an additional pass
    request_additional_pass: RwLock<bool>,
    /// true when it is an additional pass
    requested_additional_pass: bool,

    /// Check if we are assembling a crunched section as there are some limitations
    crunched_section_state: Option<CrunchedSectionState>,

    /// Stable counter of nops
    stable_counters: StableTickerCounters,

    /// gate array configuration
    ga_mmr: u8,
    /// duplicate of the output address to be sure to select the appropriate page info
    output_address: u16,

    /// Ensemble of pages (2 for a stock CPC) for the snapshot
    pages_info_sna: Vec<PageInformation>,

    /// Memory configuration is controlled by the underlying snapshot.
    /// It will ease the generation of snapshots but may complexify the generation of files
    sna: cpclib_sna::Snapshot,
    sna_version: cpclib_sna::SnapshotVersion,

    /// List of banks (temporary memory)
    banks: Vec<(Bank, PageInformation, BitVec)>,
    /// Some functionalities may be disabled once assembler is in a bank (TODO determine which ones)
    selected_bank: Option<usize>,

    /// Counter for the unique labels within macros
    macro_seed: usize,

    charset_encoding: CharsetEncoding,

    /// Track where bytes has been written to forbid overriding them when generating data
    /// BUG: should be stored individually in each bank ?
    written_bytes: BitVec,
    byte_written: bool,

    symbols: SymbolsTableCaseDependent,

    /// Return value of the currently executed function. Is almost always None
    return_value: Option<ExprResult>,
    functions: BTreeMap<String, Arc<Function>>,

    /// Set only if the run instruction has been used
    run_options: Option<(u16, Option<u16>)>,

    /// optional object that manages the listing output
    output_trigger: Option<ListingOutputTrigger>,
    /// Listing of symbols generator
    symbols_output: SymbolOutputGenerator,

    string_warning_done: bool,
    warnings: Vec<AssemblerWarning>,

    /// Counter to disable some instruction in rorg stuff
    nested_rorg: usize,

    /// List of all sections
    sections: HashMap<String, Arc<RwLock<Section>>>,
    /// Current section if any
    current_section: Option<Arc<RwLock<Section>>>,

    saved_files: Option<Vec<SavedFile>>,

    // Store the error that has been temporarily discarded at previous pass, by expecting they will be not be raised at current pass
    previous_pass_discarded_errors: HashSet<String>,
    // Store the error that has been temporarily discarded, by expecting they will be fixed at next pass
    current_pass_discarded_errors: HashSet<String>,

    if_token_adr_to_used_decision: HashMap<usize, bool>,
    if_token_adr_to_unused_decision: HashMap<usize, bool>,

    included_paths: HashSet<PathBuf>,

    // temporary stuff
    extra_print_from_function: RwLock<Vec<PrintOrPauseCommand>>,
    extra_failed_assert_from_function: RwLock<Vec<FailedAssertCommand>>
}

impl Clone for Env {
    fn clone(&self) -> Self {
        Self {
            ctx: self.ctx.clone(),
            can_skip_next_passes: (*self.can_skip_next_passes.read().unwrap().deref()).into(),
            request_additional_pass: (*self.request_additional_pass.read().unwrap().deref()).into(),
            pass: self.pass.clone(),
            real_nb_passes: self.real_nb_passes.clone(),
            crunched_section_state: self.crunched_section_state.clone(),
            stable_counters: self.stable_counters.clone(),
            ga_mmr: self.ga_mmr.clone(),
            output_address: self.output_address.clone(),
            pages_info_sna: self.pages_info_sna.clone(),
            sna: self.sna.clone(),
            sna_version: self.sna_version.clone(),
            banks: self.banks.clone(),
            selected_bank: self.selected_bank.clone(),
            macro_seed: self.macro_seed.clone(),
            charset_encoding: self.charset_encoding.clone(),
            written_bytes: self.written_bytes.clone(),
            byte_written: self.byte_written.clone(),
            symbols: self.symbols.clone(),
            run_options: self.run_options.clone(),
            output_trigger: self.output_trigger.clone(),
            symbols_output: self.symbols_output.clone(),
            string_warning_done: self.string_warning_done.clone(),
            warnings: self.warnings.clone(),
            nested_rorg: self.nested_rorg.clone(),
            sections: self.sections.clone(),
            current_section: self.current_section.clone(),
            saved_files: self.saved_files.clone(),

            if_token_adr_to_used_decision: self.if_token_adr_to_used_decision.clone(),
            if_token_adr_to_unused_decision: self.if_token_adr_to_unused_decision.clone(),
            requested_additional_pass: self.requested_additional_pass,

            functions: self.functions.clone(),
            return_value: self.return_value.clone(),

            current_pass_discarded_errors: self.current_pass_discarded_errors.clone(),
            previous_pass_discarded_errors: self.previous_pass_discarded_errors.clone(),

            included_paths: self.included_paths.clone(),
            extra_print_from_function: self
                .extra_print_from_function
                .read()
                .unwrap()
                .clone()
                .into(),
            extra_failed_assert_from_function: self
                .extra_failed_assert_from_function
                .read()
                .unwrap()
                .clone()
                .into()
        }
    }
}
impl fmt::Debug for Env {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Env{{ pass: {:?}, symbols {:?} }}",
            self.pass,
            self.symbols()
        )
    }
}

impl Default for Env {
    fn default() -> Self {
        Self {
            pass: AssemblingPass::Uninitialized,
            stable_counters: StableTickerCounters::default(),
            ctx: Default::default(),
            pages_info_sna: vec![Default::default(); 2],
            ga_mmr: 0xC0, // standard memory configuration

            macro_seed: 0,
            charset_encoding: CharsetEncoding::new(),
            sna: {
                let mut sna = Snapshot::new_6128().unwrap();
                sna.unwrap_memory_chunks();
                sna
            },
            sna_version: cpclib_sna::SnapshotVersion::V3,

            symbols: SymbolsTableCaseDependent::default(),
            run_options: None,
            written_bytes: BitVec::repeat(false, 0x4000 * 2 * 4),
            byte_written: false,
            output_trigger: None,
            symbols_output: Default::default(),

            crunched_section_state: None,

            string_warning_done: false,
            warnings: Vec::new(),
            nested_rorg: 0,

            sections: HashMap::<String, Arc<RwLock<Section>>>::default(),
            current_section: None,
            output_address: 0,
            banks: Vec::new(),
            selected_bank: None,

            real_nb_passes: 0,
            saved_files: None,
            can_skip_next_passes: true.into(),
            request_additional_pass: false.into(),

            if_token_adr_to_used_decision: HashMap::default(),
            if_token_adr_to_unused_decision: HashMap::default(),
            requested_additional_pass: false,

            functions: Default::default(),
            return_value: None,

            current_pass_discarded_errors: HashSet::default(),
            previous_pass_discarded_errors: HashSet::default(),

            included_paths: HashSet::default(),

            extra_print_from_function: Vec::new().into(),
            extra_failed_assert_from_function: Vec::new().into()
        }
    }
}

/// Symbols handling
impl Env {
    pub fn symbols(&self) -> &SymbolsTableCaseDependent {
        &self.symbols
    }

    pub fn symbols_mut(&mut self) -> &mut SymbolsTableCaseDependent {
        &mut self.symbols
    }

    /// Compute the expression thanks to the symbol table of the environment.
    /// If the expression is not solvable in first pass, 0 is returned.
    /// If the expression is not solvable in second pass, an error is returned
    ///
    /// However, when assembling in a crunched section, the expression MUST NOT fail. edit: why ? I do not get it now and I have removed this limitation
    pub fn resolve_expr_may_fail_in_first_pass<E: ExprEvaluationExt>(
        &self,
        exp: &E
    ) -> Result<ExprResult, AssemblerError> {
        match exp.resolve(self) {
            Ok(value) => Ok(value),
            Err(e) => {
                if self.pass.is_first_pass() {
                    *self.can_skip_next_passes.write().unwrap() = false;
                    Ok(0.into())
                }
                else {
                    Err(e)
                }
            }
        }
    }

    /// Compute the expression thanks to the symbol table of the environment.
    /// An error is systematically raised if the expression is not solvable (i.e., labels are unknown)
    fn resolve_expr_must_never_fail<E: ExprEvaluationExt>(
        &self,
        exp: &E
    ) -> Result<ExprResult, AssemblerError> {
        match exp.resolve(self) {
            Ok(value) => Ok(value),
            Err(e) => {
                if self.pass.is_first_pass() {
                    *self.can_skip_next_passes.write().unwrap() = false;
                    Err(e)
                }
                else {
                    Err(e)
                }
            }
        }
    }

    pub(crate) fn add_function_parameter_to_symbols_table<S: Into<Symbol>, V: Into<Value>>(
        &mut self,
        symbol: S,
        value: V
    ) -> Result<(), AssemblerError> {
        let symbol = symbol.into();
        // // we do not test that, otherwise it is impossible to do recursive functions
        // if self.symbols().contains_symbol(symbol.clone())? {
        // return Err(AssemblerError::IncoherentCode{msg: format!("Function parameter {} already present", symbol)})
        // }
        self.symbols.set_symbol_to_value(symbol, value)?;
        Ok(())
    }

    /// Add a symbol to the symbol table.
    /// In pass 1: the label MUST be absent
    /// In pass 2: the label MUST be present and of the same value
    fn add_symbol_to_symbol_table<E: Into<Value>>(
        &mut self,
        label: &str,
        value: E
    ) -> Result<(), AssemblerError> {
        let already_present = self.symbols().contains_symbol(label)?;
        let value = value.into();

        match (already_present, self.pass) {
            (true, AssemblingPass::FirstPass) => Err(AssemblerError::SymbolAlreadyExists {
                symbol: label.to_string(),
            }),
            (false, AssemblingPass::SecondPass ) => {
                // here we weaken the test to allow multipass stuff
                if ! self.requested_additional_pass &&
                   ! *self.request_additional_pass.read().unwrap()
                {
                        Err(AssemblerError::IncoherentCode{msg: format!(
                        "Label {} is not present in the symbol table in pass {}. There is an issue with some  conditional code.",
                        label, self.pass
                    )})
                } else {
                    self.symbols_mut()
                        .set_symbol_to_value(label, value)?;
                    Ok(())
                }
             },
            (false,  AssemblingPass::ListingPass) => Err(AssemblerError::IncoherentCode{msg: format!(
                "Label {} is not present in the symbol table in pass {}. There is an issue with some  conditional code.",
                label, self.pass
            )}),
            (false, AssemblingPass::FirstPass) | (false, AssemblingPass::Uninitialized) => {
                self.symbols_mut()
                    .set_symbol_to_value(label, value)?;
                Ok(())
            }
            (true, AssemblingPass::SecondPass | AssemblingPass::ListingPass) => {
                self.symbols_mut()
                    .update_symbol_to_value(&label.to_owned(), value)?;
                Ok(())
            }
            (_, _) => panic!(
                "add_symbol_to_symbol_table / unmanaged case {}, {}, {} {:#?}",
                self.pass, label, already_present, value
            ),
        }
    }

    /// Track the symbols for an expression that has been properly executed
    fn track_used_symbols(&mut self, e: &Expr) {
        e.symbols_used()
            .into_iter()
            .for_each(|symbol| self.symbols.use_symbol(symbol))
    }
}
/// Report handling
impl Env {
    pub fn report(&self, start: &Instant) -> Report {
        Report::from((self, start))
    }
}

/// Include once handling {
impl Env {
    fn has_included(&self, path: &PathBuf) -> bool {
        self.included_paths.contains(path)
    }

    fn mark_included(&mut self, path: PathBuf) {
        self.included_paths.insert(path);
    }
}

/// Error handling
impl Env {
    /// If the error has not been raised at the previous pass, store it and do not propagate it. Otherwise, propagate it
    pub fn add_error_discardable_one_pass(
        &mut self,
        e: AssemblerError
    ) -> Result<(), AssemblerError> {
        let repr = SimplerAssemblerError(&e).to_string();
        if self.previous_pass_discarded_errors.contains(&repr) {
            return Err(e);
        }
        else {
            self.current_pass_discarded_errors.insert(repr);
            return Ok(());
        }
    }
}
/// Namespace handling
impl Env {
    fn enter_namespace(&mut self, namespace: &str) -> Result<(), AssemblerError> {
        if namespace.contains(".") {
            return Err(AssemblerError::AssemblingError {
                msg: format!("Invalid namespace \"{}\"", namespace)
            });
        }
        self.symbols_mut().enter_namespace(namespace);
        Ok(())
    }

    fn leave_namespace(&mut self) -> Result<Symbol, AssemblerError> {
        self.symbols_mut().leave_namespace().map_err(|e| e.into())
    }
}

#[allow(missing_docs)]
impl Env {
    /// Create an environment that embeds a copy of the given table and is configured to be in the latest pass.
    /// Mainly used for tests.
    pub fn with_table(symbols: &SymbolsTable) -> Self {
        let mut env = Self::default();
        env.symbols.set_table(symbols.clone());
        env.pass = AssemblingPass::SecondPass;
        env
    }

    pub fn with_table_case_dependent(symbols: &SymbolsTableCaseDependent) -> Self {
        let mut env = Self::default();
        env.symbols = symbols.clone();
        env.pass = AssemblingPass::SecondPass;
        env
    }

    pub fn warnings(&self) -> &[AssemblerWarning] {
        &self.warnings
    }

    /// Manage the play with data for the output listing
    fn handle_output_trigger(&mut self, new: &LocatedToken) {
        if self.pass.is_listing_pass() && self.output_trigger.is_some() {
            let addr = self.logical_output_address();
            let trigg = self.output_trigger.as_mut().unwrap();
            trigg.new_token(
                new,
                addr as _,
                if self.crunched_section_state.is_some() {
                    AddressKind::CrunchedArea
                }
                else {
                    AddressKind::Address
                }
            );
        }
    }

    /// Start a new pass by cleaning up datastructures.
    /// The only thing to keep is the symbol table
    pub(crate) fn start_new_pass(&mut self) {
        self.requested_additional_pass |= !self.current_pass_discarded_errors.is_empty();

        let mut can_change_request = true;
        if !self.pass.is_listing_pass() {
            self.pass = if self.real_nb_passes == 0
                || !*self.can_skip_next_passes.read().unwrap().deref()
            {
                if *self.request_additional_pass.read().unwrap() {
                    if self.pass.is_first_pass() {
                        can_change_request = false;
                    }
                    AssemblingPass::SecondPass
                }
                else {
                    self.pass.next_pass()
                }
            }
            else {
                if !*self.request_additional_pass.read().unwrap() {
                    AssemblingPass::Finished
                }
                else {
                    AssemblingPass::SecondPass
                }
            };
        }

        if !self.pass.is_finished() || self.pass.is_listing_pass() {
            if !self.pass.is_listing_pass() {
                self.real_nb_passes += 1;
            }

            std::mem::swap(
                &mut self.current_pass_discarded_errors,
                &mut self.previous_pass_discarded_errors
            );
            self.current_pass_discarded_errors.clear();

            self.stable_counters = StableTickerCounters::default();
            self.run_options = None;
            self.written_bytes().fill(false);
            self.warnings.retain(|elem| !elem.is_override_memory());
            self.pages_info_sna.iter_mut().for_each(|p| p.new_pass());

            self.sections
                .iter_mut()
                .for_each(|s| s.1.write().unwrap().new_pass());
            self.current_section = None;

            self.banks.iter_mut().for_each(|bank| {
                bank.1.new_pass();
                bank.2.fill(false);
            });
            self.selected_bank = None;

            // environnement is not reset when assembling is finished
            self.output_address = 0;
            let page_info = self.active_page_info_mut();
            page_info.logical_outputadr = 0;
            page_info.logical_codeadr = 0;
            self.update_dollar();

            self.ga_mmr = 0xC0;
            self.macro_seed = 0;
            self.charset_encoding.reset();
            // self.sna = Default::default(); // We finally keep the snapshot for the memory function
            self.sna_version = cpclib_sna::SnapshotVersion::V3;

            self.can_skip_next_passes = true.into();
            if can_change_request {
                self.request_additional_pass = false.into();
            }
            self.symbols.new_pass();
        }
    }

    /// Handle the actions to do after assembling.
    /// ATM it is only the save of data for each page
    fn handle_post_actions(&mut self) -> Result<Vec<SavedFile>, AssemblerError> {
        self.handle_assert()?;
        self.handle_print()?;
        self.handle_breakpoints()?;
        self.handle_file_save()
    }

    /// We handle breakpoint ONLY for the pages stored in the snapshot
    /// as they are stored inside a chunck of the snapshot:
    /// If one day another export is coded, we could export the others too.
    fn handle_breakpoints(&mut self) -> Result<(), AssemblerError> {
        let mut winape_raw = Vec::new();

        let pages_mmr = MMR_PAGES_SELECTION;
        for (activepage, _page) in pages_mmr[0..self.pages_info_sna.len()].iter().enumerate() {
            for brk in self.pages_info_sna[activepage].collect_breakpoints() {
                let info = AssemblerError::RelocatedInfo {
                    info: Box::new(AssemblerError::AssemblingError {
                        msg: format!("Add a breakpoint in 0x{:04x}", brk.address)
                    }),
                    span: brk.span.clone()
                };
                eprint!("{}", info);

                winape_raw.extend(brk.winape_raw());
            }
        }

        assert_eq!(winape_raw.len() % 5, 0);

        let winape_chunk = WinapeBreakPointChunk::from([b'B', b'R', b'K', b'S'], winape_raw);

        if winape_chunk.nb_breakpoints() > 0 {
            self.sna.add_chunk(winape_chunk);
        }

        Ok(())
    }

    fn handle_assert(&mut self) -> Result<(), AssemblerError> {
        let backup = self.ga_mmr;

        // ga values to properly switch the pages
        let pages_mmr = MMR_PAGES_SELECTION;

        let mut assert_failures: Option<AssemblerError> = None;

        // Print from the snapshot
        for (activepage, page) in pages_mmr[0..self.pages_info_sna.len()].iter().enumerate() {
            self.ga_mmr = *page;
            let mut l_errors = self.pages_info_sna[activepage].collect_assert_failure();
            match (&mut assert_failures, &mut l_errors) {
                (_, Ok(_)) => {
                    // nothing to do
                }
                (
                    Some(AssemblerError::MultipleErrors { errors: e1 }),
                    Err(AssemblerError::MultipleErrors { errors: e2 })
                ) => {
                    e1.append(e2);
                }
                (None, Err(l_errors)) => {
                    assert_failures = Some(l_errors.clone());
                }
                _ => unreachable!()
            }
        }

        for bank in self.banks.iter() {
            let mut l_errors = bank.1.collect_assert_failure();
            match (&mut assert_failures, &mut l_errors) {
                (_, Ok(_)) => {
                    // nothing to do
                }
                (
                    Some(AssemblerError::MultipleErrors { errors: e1 }),
                    Err(AssemblerError::MultipleErrors { errors: e2 })
                ) => {
                    e1.append(e2);
                }
                (None, Err(l_errors)) => {
                    assert_failures = Some(l_errors.clone());
                }
                _ => unreachable!()
            }
        }

        self.ga_mmr = backup;

        // All possible messages have been printed.
        // Errors are generated for the others
        if let Some(errors) = assert_failures {
            Err(errors)
        }
        else {
            Ok(())
        }
    }

    fn handle_print(&mut self) -> Result<(), AssemblerError> {
        let backup = self.ga_mmr;

        // ga values to properly switch the pages
        let pages_mmr = MMR_PAGES_SELECTION;

        let mut print_errors: Option<AssemblerError> = None;
        let mut writer = std::io::stdout();

        // Print from the snapshot
        for (activepage, page) in pages_mmr[0..self.pages_info_sna.len()].iter().enumerate() {
            self.ga_mmr = *page;
            let mut l_errors = self.pages_info_sna[activepage].execute_print_or_pause(&mut writer);
            match (&mut print_errors, &mut l_errors) {
                (_, Ok(_)) => {
                    // nothing to do
                }
                (
                    Some(AssemblerError::MultipleErrors { errors: e1 }),
                    Err(AssemblerError::MultipleErrors { errors: e2 })
                ) => {
                    e1.append(e2);
                }
                (None, Err(l_errors)) => {
                    print_errors = Some(l_errors.clone());
                }
                _ => unreachable!()
            }
        }

        for bank in self.banks.iter() {
            let mut l_errors = bank.1.execute_print_or_pause(&mut writer);
            match (&mut print_errors, &mut l_errors) {
                (_, Ok(_)) => {
                    // nothing to do
                }
                (
                    Some(AssemblerError::MultipleErrors { errors: e1 }),
                    Err(AssemblerError::MultipleErrors { errors: e2 })
                ) => {
                    e1.append(e2);
                }
                (None, Err(l_errors)) => {
                    print_errors = Some(l_errors.clone());
                }
                _ => unreachable!()
            }
        }

        self.ga_mmr = backup;

        // All possible messages have been printed.
        // Errors are generated for the others
        if let Some(errors) = print_errors {
            Err(errors)
        }
        else {
            Ok(())
        }
    }

    fn handle_file_save(&mut self) -> Result<Vec<SavedFile>, AssemblerError> {
        let backup = self.ga_mmr;

        // ga values to properly switch the pages
        let pages_mmr = MMR_PAGES_SELECTION;

        let mut saved_files = Vec::new();

        // save from snapshot
        for (activepage, page) in pages_mmr[0..self.pages_info_sna.len()].iter().enumerate() {
            self.ga_mmr = *page;
            let mut saved = self.pages_info_sna[activepage].execute_save(self)?;
            saved_files.append(&mut saved);
        }

        // save from extra memory / can be done in parallal
        self.ga_mmr = 0xC0;

        #[cfg(not(target_arch = "wasm32"))]
        let iter = self.banks.par_iter();
        #[cfg(target_arch = "wasm32")]
        let iter = self.banks.iter();
        let mut saved = iter
            .map(|bank| bank.1.execute_save(self))
            .collect::<Result<Vec<_>, AssemblerError>>()?;
        for s in &mut saved {
            saved_files.append(s);
        }

        // restor memory conf
        self.ga_mmr = backup;
        Ok(saved_files)
    }
}

/// Output handling
impl Env {
    /// TODO
    fn active_page_info(&self) -> &PageInformation {
        match &self.selected_bank {
            Some(idx) => &self.banks[*idx].1,
            None => {
                let active_page =
                    self.logical_to_physical_address(self.output_address).page() as usize;
                &self.pages_info_sna[active_page]
            }
        }
    }

    /// TODO remove this method and its calls
    fn active_page_info_mut(&mut self) -> &mut PageInformation {
        match &self.selected_bank {
            Some(idx) => &mut self.banks[*idx].1,
            None => {
                let active_page =
                    self.logical_to_physical_address(self.output_address).page() as usize;
                &mut self.pages_info_sna[active_page]
            }
        }
    }

    fn written_bytes(&mut self) -> &mut BitVec {
        match &self.selected_bank {
            Some(idx) => &mut self.banks[*idx].2,
            None => &mut self.written_bytes
        }
    }

    /// Return the address where the next byte will be written
    pub fn logical_output_address(&self) -> u16 {
        self.active_page_info().logical_outputadr
    }

    /// Return the address of dollar
    pub fn logical_code_address(&self) -> u16 {
        self.active_page_info().logical_codeadr
    }

    pub fn limit_address(&self) -> u16 {
        self.active_page_info().limit
    }

    pub fn start_address(&self) -> Option<u16> {
        self.active_page_info().startadr
    }

    pub fn maximum_address(&self) -> u16 {
        self.active_page_info().maxadr
    }

    /// . Update the value of $ in the symbol table in order to take the current  output address
    pub fn update_dollar(&mut self) {
        let addr = self.logical_to_physical_address(self.logical_code_address());
        self.symbols.set_current_address(addr);

        let addr = self.logical_to_physical_address(self.logical_output_address());
        self.symbols.set_current_output_address(addr);
    }

    /// Produce the memory for the required limits
    /// TODO check that the implementation is still correct with snapshot inclusion
    /// BUG  does not take into account extra bank configuration
    pub fn memory(&self, start: u16, size: u16) -> Vec<u8> {
        let mut mem = Vec::new();
        for pos in start..(start + size) {
            let address = self.logical_to_physical_address(pos as _);
            mem.push(self.peek(&address));
        }
        mem
    }

    /// Returns the stream of bytes produced for a 64k compilation
    pub fn produced_bytes(&self) -> Vec<u8> {
        let (start, length) = match self.start_address() {
            Some(start) => {
                if start > self.maximum_address() {
                    (0, 0)
                }
                else {
                    (start, self.maximum_address() as usize - start as usize + 1)
                }
            }
            None => (0, 0)
        };

        self.memory(start, length as _)
    }

    /// Returns the address of the 1st written byte
    pub fn loading_address(&self) -> Option<u16> {
        self.start_address()
    }

    /// Returns the address from when to start the program
    /// TODO really configure this address
    pub fn execution_address(&self) -> Option<u16> {
        self.start_address()
    }

    /// Output one byte either in the appropriate bank of the snapshot or in the termporary bank
    /// return true if it raised an override warning
    pub fn output(&mut self, v: u8) -> Result<bool, AssemblerError> {
        //   dbg!(self.logical_output_address(), self.output_address);
        if self.logical_output_address() != self.output_address {
            return Err(AssemblerError::BugInAssembler {
                msg: format!(
                    "Sync issue with output address (0x{:x} != 0x{:x})",
                    self.logical_output_address(),
                    self.output_address
                )
            });
        }

        // dbg!(self.output_address(), &v);
        let physical_address =
            self.logical_to_physical_address(self.logical_output_address() as u16);

        // Check if it is legal to output the value
        if self.logical_code_address() > self.limit_address()
            || (self.active_page_info().fail_next_write_if_zero && self.logical_code_address() == 0)
        {
            // dbg!(self.logical_output_address() > self.limit_address(), self.active_page_info().fail_next_write_if_zero && self.logical_output_address()==0);

            return Err(AssemblerError::OutputExceedsLimits(
                physical_address,
                self.limit_address() as _
            ));
        }
        for protected_area in &self.active_page_info().protected_areas {
            if protected_area.contains(&(self.logical_code_address() as u16)) {
                return Err(AssemblerError::OutputProtected {
                    area: protected_area.clone(),
                    address: self.logical_code_address() as _
                });
            }
        }

        self.byte_written = true;

        // update the maximm 64k position
        self.active_page_info_mut().maxadr =
            self.maximum_address().max(self.logical_output_address());
        if self.active_page_info().startadr.is_none() {
            self.active_page_info_mut().startadr = Some(self.logical_output_address());
        }

        let abstract_address = physical_address.offset_in_cpc();
        let already_used = *self.written_bytes().get(abstract_address as usize).unwrap();

        let r#override = if already_used {
            let r#override = AssemblerError::OverrideMemory(physical_address.clone(), 1);
            if self.allow_memory_override() {
                self.warnings.push(r#override);
                true
            }
            else {
                return Err(r#override);
            }
        }
        else {
            false
        };

        if self.selected_bank.is_none() {
            if let Some(section) = &self.current_section {
                let section = section.read().unwrap();
                if !section.contains(physical_address.address()) {
                    return Err(AssemblerError::AssemblingError {
                        msg: format!(
                            "SECTION error: write address 0x{:x} out of range [Ox{:}-Ox{:}]",
                            physical_address.address(),
                            section.start,
                            section.stop
                        )
                    });
                }
            }
        }

        match &self.selected_bank {
            Some(idx) => {
                self.banks[*idx].0[self.output_address as usize] = v;
            }
            None => {
                self.sna.set_byte(abstract_address, v);
            }
        }
        self.written_bytes().set(abstract_address as _, true);

        // Add the byte to the listing space
        if self.pass.is_listing_pass() && self.output_trigger.is_some() {
            self.output_trigger.as_mut().unwrap().write_byte(v);
        }

        self.active_page_info_mut().logical_outputadr =
            self.logical_output_address().wrapping_add(1);
        self.output_address = self.logical_output_address();
        self.active_page_info_mut().logical_codeadr = self.logical_code_address().wrapping_add(1);

        // we have written all memory and are trying to restart
        if self.logical_output_address() == 0 {
            self.active_page_info_mut().fail_next_write_if_zero = true;
        }

        {
            let (output, code) = (
                self.active_page_info().logical_outputadr,
                self.active_page_info().logical_codeadr
            );

            if let Some(section) = &mut self.current_section {
                section.write().unwrap().output_adr = output;
                section.write().unwrap().code_adr = code;
            }
        }

        self.update_dollar();

        Ok(r#override)
    }

    pub fn allow_memory_override(&self) -> bool {
        true // TODO parametrize it in the options (and set false by default)
    }

    /// Write consecutives bytes
    pub fn output_bytes(&mut self, bytes: &[u8]) -> Result<(), AssemblerError> {
        //        dbg!(self.logical_output_address(), bytes);

        let mut previously_overrided = false;
        for b in bytes.iter() {
            let currently_overrided = self.output(*b)?;

            match (previously_overrided, currently_overrided) {
                (true, true) => {
                    // remove the latestwarning as it is a duplicate
                    let extra_override_idx = self
                        .warnings
                        .iter_mut()
                        .rev()
                        .position(|w| {
                            if let AssemblerError::OverrideMemory(..) = w {
                                true
                            }
                            else {
                                false
                            }
                        })
                        .unwrap(); // cannot fail by construction
                    self.warnings
                        .remove(self.warnings.len() - 1 - extra_override_idx); // rev impose to change index order

                    // get the last override warning and update it
                    let r#override = self
                        .warnings
                        .iter_mut()
                        .rev()
                        .find(|w| {
                            if let AssemblerError::OverrideMemory(..) = w {
                                true
                            }
                            else {
                                false
                            }
                        })
                        .unwrap(); // cannot fail by construction

                    // increase its size
                    match r#override {
                        AssemblerError::OverrideMemory(_, ref mut size) => {
                            *size += 1;
                        }
                        _ => unreachable!()
                    };
                }
                _ => {
                    // nothing to do
                }
            }

            previously_overrided = currently_overrided;
        }

        Ok(())
    }

    pub fn peek(&self, address: &PhysicalAddress) -> u8 {
        let address = address.offset_in_cpc();
        match &self.selected_bank {
            Some(idx) => self.banks[*idx].0[address as usize],
            None => self.sna.get_byte(address)
        }
    }

    pub fn poke(&mut self, byte: u8, address: &PhysicalAddress) {
        let address = address.offset_in_cpc();
        match &self.selected_bank {
            Some(idx) => {
                self.banks[*idx].0[address as usize] = byte;
            }
            None => {
                self.sna.set_byte(address, byte);
            }
        }
    }

    /// Get the size of the generated binary.
    /// ATTENTION it can only work when geneating 0x10000 files
    pub fn size(&self) -> u16 {
        if self.start_address().is_none() {
            panic!("Unable to compute size now");
        }
        else {
            (self.logical_output_address() - self.start_address().unwrap()) as u16
        }
    }

    /// Evaluate the expression according to the current state of the environment
    pub fn eval(&self, expr: &Expr) -> Result<ExprResult, AssemblerError> {
        expr.resolve(self)
    }

    pub fn sna(&self) -> &cpclib_sna::Snapshot {
        &self.sna
    }

    pub fn sna_version(&self) -> cpclib_sna::SnapshotVersion {
        self.sna_version
    }

    pub fn save_sna<P: AsRef<std::path::Path>>(&self, fname: P) -> Result<(), std::io::Error> {
        self.sna().save(fname, self.sna_version())
    }

    /// Compute the relative address. Is authorized to fail at first pass
    fn absolute_to_relative_may_fail_in_first_pass(
        &self,
        address: i32,
        opcode_delta: i32
    ) -> Result<u8, AssemblerError> {
        match absolute_to_relative(address, opcode_delta, self.symbols()) {
            Ok(value) => Ok(value),
            Err(error) => {
                if self.pass.is_first_pass() {
                    Ok(0)
                }
                else {
                    Err(AssemblerError::RelativeAddressUncomputable {
                        address,
                        pass: self.pass,
                        error: Box::new(error)
                    })
                }
            }
        }
    }
}

impl Env {
    fn visit_breakpoint(
        &mut self,
        exp: Option<&Expr>,
        span: Option<Z80Span>
    ) -> Result<(), AssemblerError> {
        if exp.is_some() {
            return Err(AssemblerError::BugInAssembler {
                msg: format!("Breakpoint with an expression is not yet implemented")
            });
        }

        let current_address = self.logical_code_address();
        let page = match self.logical_to_physical_address(current_address).page() {
            0 => 0,
            1 => 1,
            _ => {
                return Err(AssemblerError::BugInAssembler {
                    msg: format!(
                        "Page selection not handled 0x{:x}",
                        self.logical_to_physical_address(current_address).page()
                    )
                })
            }
        };

        let brk = BreakpointCommand::new(current_address, page, span.unwrap());
        self.active_page_info_mut().add_breakpoint_command(brk);

        Ok(())
    }
}

#[allow(missing_docs)]
impl Env {
    /// Write in w the list of symbols
    pub fn generate_symbols_output<W: Write>(&self, w: &mut W) -> std::io::Result<()> {
        self.symbols_output.generate(w, self.symbols())
    }

    /// Visit all the tokens of the slice of tokens.
    /// Return true if an additional pass is requested
    pub fn visit_listing<T: ListingElement + Visited + MayHaveSpan>(
        &mut self,
        listing: &[T]
    ) -> Result<(), AssemblerError> {
        for token in listing.iter() {
            let _res = token.visited(self)?;
        }

        Ok(())
    }

    /// TODO set the limit for the current page
    fn visit_limit(&mut self, exp: &Expr) -> Result<(), AssemblerError> {
        let value = self.resolve_expr_must_never_fail(exp)?.int()?;
        self.active_page_info_mut().limit = value as _;

        if self.limit_address() <= self.maximum_address() {
            return Err(AssemblerError::OutputAlreadyExceedsLimits(
                self.limit_address() as _
            ));
        }
        if self.limit_address() == 0 {
            eprintln!("[WARNING] Do you really want to set a limit of 0 ?");
        }
        Ok(())
    }

    fn visit_label(&mut self, label: &str) -> Result<(), AssemblerError> {
        let label = self.symbols().normalize_symbol(label);
        let label = label.value();

        // A label cannot be defined multiple times
        let res = if self.symbols().contains_symbol(label)?
            && (self.pass.is_first_pass()
                || !(self.symbols().kind(label)? == "address"
                    || self.symbols().kind(label)? == "any"))
        {
            Err(AssemblerError::AlreadyDefinedSymbol {
                symbol: label.into(),
                kind: self.symbols().kind(label)?.into()
            })
        }
        else {
            if !label.starts_with('.') {
                self.symbols_mut().set_current_label(label)?;
            }

            // If the current address is not set up, we force it to be 0
            let value = match self.symbols().current_address() {
                Ok(address) => address,
                Err(_) => 0
            };
            let addr = self.logical_to_physical_address(value);

            self.add_symbol_to_symbol_table(label, addr)
        };

        // Try to fallback on a macro call - parser is not that much great
        if let Err(AssemblerError::AlreadyDefinedSymbol { symbol: _, kind }) = &res {
            if kind == "macro" || kind == "struct" {
                return Err(AssemblerError::AssemblingError {
                    msg:
                        "Use (void) for macros with no parameters to disambiguate them with labels"
                            .to_owned()
                });
            // self.visit_call_macro_or_build_struct(&Token::MacroCall(
            // label.into(),
            // Default::default()
            // ))
            }
            else {
                res
            }
        }
        else {
            res
        }
    }

    fn visit_noexport<S: Borrow<str> + Display>(
        &mut self,
        labels: &[S]
    ) -> Result<(), AssemblerError> {
        if labels.is_empty() {
            self.symbols_output.forbid_all_symbols();
        }
        else {
            labels
                .iter()
                .for_each(|l| self.symbols_output.forbid_symbol(l.borrow()));
        }

        Ok(())
    }

    fn visit_export<S: Borrow<str> + Display>(
        &mut self,
        labels: &[S]
    ) -> Result<(), AssemblerError> {
        if labels.is_empty() {
            self.symbols_output.allow_all_symbols();
        }
        else {
            labels
                .iter()
                .for_each(|l| self.symbols_output.allow_symbol(l.borrow()));
        }

        Ok(())
    }

    fn visit_multi_pushes(&mut self, regs: &[DataAccess]) -> Result<(), AssemblerError> {
        let result = regs
            .iter()
            .map(|reg| assemble_push(reg))
            .collect::<Result<Vec<_>, AssemblerError>>()?;
        let result = result.into_iter().flatten().collect_vec();
        self.output_bytes(&result)
    }

    fn visit_multi_pops(&mut self, regs: &[DataAccess]) -> Result<(), AssemblerError> {
        let result = regs
            .iter()
            .map(|reg| assemble_pop(reg))
            .collect::<Result<Vec<_>, AssemblerError>>()?;
        let result = result.into_iter().flatten().collect_vec();
        self.output_bytes(&result)
    }

    pub fn visit_macro_definition(
        &mut self,
        name: &str,
        arguments: &[&str],
        code: &str,
        source: Option<&Z80Span>
    ) -> Result<(), AssemblerError> {
        if self.pass.is_first_pass() && self.symbols().contains_symbol(name)? {
            return Err(AssemblerError::SymbolAlreadyExists {
                symbol: name.to_owned()
            });
        }

        let source = source.map(|s| s.into());

        self.symbols_mut().set_symbol_to_value(
            name,
            Macro::new(name.into(), arguments, code.to_owned(), source)
        )?;
        Ok(())
    }

    pub fn visit_waitnops(&mut self, count: &Expr) -> Result<(), AssemblerError> {
        // TODO really use a clever way
        let bytes = self.assemble_nop(Mnemonic::Nop, Some(count))?;
        self.output_bytes(&bytes)?;

        self.stable_counters
            .update_counters(self.resolve_expr_may_fail_in_first_pass(count)?.int()? as _);
        Ok(())
    }

    pub fn visit_struct_definition<T: ListingElement + ToSimpleToken, S: Borrow<str>>(
        &mut self,
        name: &str,
        content: &[(S, T)],
        source: Option<&Z80Span>
    ) -> Result<(), AssemblerError> {
        if self.pass.is_first_pass() && self.symbols().contains_symbol(name)? {
            return Err(AssemblerError::SymbolAlreadyExists {
                symbol: name.to_owned()
            });
        }

        let r#struct = Struct::new(name, content, source.map(|s| s.into()));
        // add inner index BEFORE the structure. It should reduce infinite loops
        let mut index = 0;
        for (f, s) in r#struct.fields_size(self.symbols()) {
            self.symbols_mut()
                .set_symbol_to_value(format!("{}.{}", name, f), index)?;
            index += s;
        }
        self.symbols_mut().set_symbol_to_value(name, r#struct)?;

        Ok(())
    }

    pub fn visit_buildsna(
        &mut self,
        version: Option<&SnapshotVersion>
    ) -> Result<(), AssemblerError> {
        self.sna_version = version.cloned().unwrap_or(SnapshotVersion::V3);
        Ok(())
    }

    pub fn visit_align(
        &mut self,
        boundary: &Expr,
        fill: Option<&Expr>
    ) -> Result<(), AssemblerError> {
        let boundary = self.resolve_expr_must_never_fail(boundary)?.int()? as u16;
        let fill = match fill {
            Some(fill) => self.resolve_expr_may_fail_in_first_pass(fill)?.int()? as u8,
            None => 0
        };

        while self.logical_output_address() as u16 % boundary != 0 {
            self.output(fill)?;
        }

        Ok(())
    }

    fn visit_section(&mut self, name: &str) -> Result<(), AssemblerError> {
        let section = match self.sections.get(name) {
            Some(section) => section,
            None => {
                return Err(AssemblerError::AssemblingError {
                    msg: format!("Section '{}' does not exists", name)
                });
            }
        };

        let (output_adr, code_adr, mmr) = {
            let section = section.read().unwrap();

            if section.mmr != self.ga_mmr {
                self.warnings.push(AssemblerError::AssemblingError{
                    msg: format!("Gate Array configuration is not coherent with the section. We  manually set it (0x{:x} expected instead of 0x{:x})", section.mmr, self.ga_mmr)
                });
            }

            (section.output_adr, section.code_adr, section.mmr)
        };

        self.current_section = Some(Arc::clone(section));

        self.ga_mmr = mmr;
        self.output_address = output_adr;

        self.active_page_info_mut().logical_outputadr = output_adr;
        self.active_page_info_mut().logical_codeadr = code_adr;

        self.update_dollar();
        self.output_trigger
            .as_mut()
            .map(|o| o.replace_address(code_adr.into()));
        Ok(())
    }

    fn visit_range(&mut self, name: &str, start: &Expr, stop: &Expr) -> Result<(), AssemblerError> {
        let start = self.resolve_expr_must_never_fail(start)?.int()? as u16;
        let stop = self.resolve_expr_must_never_fail(stop)?.int()? as u16;
        let mmr = self.ga_mmr;

        if let Some(section) = self.sections.get(name) {
            let section = section.read().unwrap();
            if start != section.start
                || stop != section.stop
                || name != section.name
                || mmr != section.mmr
            {
                return Err(AssemblerError::AssemblingError {
                    msg: format!(
                        "Section '{}' is already defined from 0x{:x} to 0x{:x} in 0x{:x}",
                        section.name, section.start, section.stop, section.mmr
                    )
                });
            }
        }
        else {
            let section = Arc::new(RwLock::new(Section::new(name, start, stop, mmr)));

            self.sections.insert(name.to_owned(), section);
        }

        Ok(())
    }

    fn visit_next_and_co(
        &mut self,
        destination: &str,
        source: &str,
        delta: Option<&Expr>,
        can_override: bool
    ) -> Result<(), AssemblerError> {
        if !can_override && self.symbols.contains_symbol(destination)? && self.pass.is_first_pass()
        {
            let kind = self.symbols().kind(Symbol::from(destination))?;
            return Err(AssemblerError::AlreadyDefinedSymbol {
                symbol: destination.into(),
                kind: kind.into()
            });
        }

        // setup the value
        let value = self.resolve_expr_must_never_fail(&Expr::Label(source.into()))?;
        if can_override {
            self.symbols_mut()
                .assign_symbol_to_value(destination, value.clone())?;
        }
        else {
            self.add_symbol_to_symbol_table(destination, value.clone())?;
        }
        self.output_trigger
            .as_mut()
            .map(|o| o.replace_address(value.clone()));

        // increase next one
        let delta = match delta {
            Some(delta) => self.resolve_expr_must_never_fail(delta)?,
            None => 1.into()
        };
        let value = (value + delta)?;

        self.symbols_mut().assign_symbol_to_value(source, value)?;

        Ok(())
    }

    /// return the page and bank configuration for the given address at the current mmr configuration
    /// https://grimware.org/doku.php/documentations/devices/gatearray#mmr
    pub fn logical_to_physical_address(&self, address: u16) -> PhysicalAddress {
        PhysicalAddress::new(address, self.ga_mmr)
    }

    fn visit_bank(&mut self, exp: Option<&Expr>) -> Result<(), AssemblerError> {
        if self.nested_rorg > 0 {
            return Err(AssemblerError::NotAllowed);
        }

        match exp {
            Some(exp) => {
                // prefix provided, we explicitely want one configuration
                let mmr = self.resolve_expr_must_never_fail(exp)?.int()?;
                if mmr < 0xC0 || mmr > 0xC7 {
                    return Err(AssemblerError::MMRError { value: mmr });
                }

                let mmr = mmr as u8;
                self.ga_mmr = mmr;
            }
            None => {
                // nothing provided, we write in a temporary area
                if self.pass.is_first_pass() {
                    self.selected_bank = Some(self.banks.len());
                    self.banks.push((
                        Bank::default(),
                        PageInformation::default(),
                        BitVec::repeat(false, 0x4000 * 4)
                    ));
                }
                else {
                    self.selected_bank = self.selected_bank.map(|v| v + 1).or(Some(0));
                    if *self.selected_bank.as_ref().unwrap() >= self.banks.len() {
                        return Err(AssemblerError::AssemblingError {
                            msg: "There were less banks in previous pass".to_owned()
                        });
                    }
                }

                self.ga_mmr = 0xC0;
                self.output_address = 0;
                let page_info = self.active_page_info_mut();
                page_info.logical_outputadr = 0;
                page_info.logical_codeadr = 0;
            }
        }

        Ok(())
    }

    // total switch of page
    fn visit_bankset(&mut self, exp: &Expr) -> Result<(), AssemblerError> {
        if self.nested_rorg > 0 {
            return Err(AssemblerError::NotAllowed);
        }

        let page = self.resolve_expr_must_never_fail(exp)?.int()? as u8; // This value MUST be interpretable once executed

        eprintln!("Warning need to code sna memory extension if needed");
        self.select_page(page)?;
        Ok(())
    }

    fn select_page(&mut self, page: u8) -> Result<(), AssemblerError> {
        if self.nested_rorg > 0 {
            return Err(AssemblerError::NotAllowed);
        }

        if
        // page < 0 ||
        page >= 8 {
            return Err(AssemblerError::InvalidArgument {
                msg: format!(
                    "{} is invalid. BANKSET only accept values from 0 to 7",
                    page
                )
                .into()
            });
        }

        if page == 0 {
            self.ga_mmr = 0b11_000_0_00;
        }
        else {
            self.ga_mmr = 0b11_000_0_10 + ((page - 1) << 3);
        }

        let page = page as usize;
        let nb_pages = self.pages_info_sna.len();
        let expected_nb = nb_pages.max(page + 1);
        if expected_nb > nb_pages {
            self.pages_info_sna.resize(expected_nb, Default::default());
            self.written_bytes().resize(expected_nb * 0x1_0000, false);
        }

        self.output_address = self.logical_output_address();
        self.update_dollar();
        Ok(())
    }

    /// Remove the given variable from the table of symbols
    pub fn visit_undef(&mut self, label: &str) -> Result<(), AssemblerError> {
        match self.symbols_mut().remove_symbol(label)? {
            Some(_) => Ok(()),
            None => {
                Err(AssemblerError::UnknownSymbol {
                    symbol: label.into(),
                    closest: self.symbols().closest_symbol(label, SymbolFor::Number)?
                })
            }
        }
    }

    pub fn visit_protect(&mut self, start: &Expr, stop: &Expr) -> Result<(), AssemblerError> {
        if self.pass.is_first_pass() {
            let start = self.resolve_expr_must_never_fail(start)?.int()? as u16;
            let stop = self.resolve_expr_must_never_fail(stop)?.int()? as u16;

            self.active_page_info_mut()
                .protected_areas
                .push(start..=stop);
        }

        Ok(())
    }

    fn build_string_from_formatted_expression(
        &self,
        info: &[FormattedExpr]
    ) -> Result<String, AssemblerError> {
        let mut repr = String::default();
        for (_idx, current) in info.iter().enumerate() {
            // if idx != 0 {
            // repr += " ";
            // }
            // we do not want the space anymore
            match current {
                FormattedExpr::Raw(Expr::String(string)) => {
                    repr += string;
                }
                FormattedExpr::Raw(Expr::Char(char)) => {
                    repr += &char.to_string();
                }
                FormattedExpr::Raw(expr) => {
                    let value = self.resolve_expr_may_fail_in_first_pass(expr)?;
                    repr += &value.to_string();
                }
                FormattedExpr::Formatted(format, expr) => {
                    let value = self.resolve_expr_may_fail_in_first_pass(expr)?.int()? as i32;
                    repr += &format.string_representation(value);
                }
            }
        }

        Ok(repr)
    }

    /// Print the evaluation of the expression in the 2nd pass
    pub fn visit_print(&mut self, info: &[FormattedExpr], span: Option<Z80Span>) {
        let print_or_error = match self.build_string_from_formatted_expression(info) {
            Ok(msg) => either::Either::Left(msg),
            Err(error) => either::Either::Right(error)
        };

        self.active_page_info_mut().add_print_command(PrintCommand {
            span,
            print_or_error
        })
    }

    pub fn visit_pause(&mut self, span: Option<Z80Span>) {
        self.active_page_info_mut().add_pause_command(span.into());
    }

    pub fn visit_fail(&self, info: Option<&[FormattedExpr]>) -> Result<(), AssemblerError> {
        let repr = info
            .map(|info| self.build_string_from_formatted_expression(info))
            .unwrap_or_else(|| Ok("".to_owned()))?;
        dbg!(Err(AssemblerError::Fail { msg: repr }))
    }

    // BUG the file is saved in any case EVEN if there is a crash in the assembler later
    // TODO delay the save but retreive the data now
    pub fn visit_save(
        &mut self,
        filename: &str,
        address: Option<&Expr>,
        size: Option<&Expr>,
        save_type: Option<&SaveType>,
        dsk_filename: Option<&String>,
        _side: Option<&Expr>
    ) -> Result<(), AssemblerError> {
        if cfg!(target_arch = "wasm32") {
            return Err(AssemblerError::AssemblingError {
                msg: "SAVE directive is not allowed in a web-based assembling.".to_owned()
            });
        }

        let from = match address {
            Some(address) => Some(self.resolve_expr_must_never_fail(address)?.int()?),
            None => None
        };
        let size = match size {
            Some(size) => Some(self.resolve_expr_must_never_fail(size)?.int()?),
            None => None
        };

        let page_info = self.active_page_info_mut();
        page_info.add_save_command(SaveCommand::new(
            from,
            size,
            filename.to_owned(),
            save_type.cloned(),
            dsk_filename.cloned()
        ));

        Ok(())
    }

    pub fn visit_charset(&mut self, format: &CharsetFormat) -> Result<(), AssemblerError> {
        let mut new_charset = CharsetEncoding::new();
        std::mem::swap(&mut new_charset, &mut self.charset_encoding);
        new_charset.update(format, self)?;
        std::mem::swap(&mut new_charset, &mut self.charset_encoding); // XXX lost in case of error
        Ok(())
    }

    pub fn visit_snainit(&mut self, fname: &str) -> Result<(), AssemblerError> {
        if self.byte_written {
            return Err(AssemblerError::AssemblingError {
                msg: format!(
                    "Some bytes has alrady been produced; you cannot import the snapshot {}.",
                    fname
                )
            });
        }
        self.sna = Snapshot::load(fname).map_err(|e| {
            AssemblerError::AssemblingError {
                msg: format!("Error while loading snapshot. {}", e)
            }
        })?;
        dbg!(self.sna.memory_size_header());
        dbg!(self
            .sna
            .chunks()
            .iter()
            .map(|c| String::from_utf8_lossy(c.code()))
            .collect_vec());
        self.sna.unwrap_memory_chunks();
        dbg!(self.sna.memory_size_header());
        dbg!(self
            .sna
            .chunks()
            .iter()
            .map(|c| String::from_utf8_lossy(c.code()))
            .collect_vec());
        Ok(())
    }

    pub fn visit_snaset(
        &mut self,
        flag: &cpclib_sna::SnapshotFlag,
        value: &cpclib_sna::FlagValue
    ) -> Result<(), AssemblerError> {
        self.sna
            .set_value(*flag, value.as_u16().unwrap())
            .map_err(|e| e.into())
    }

    pub fn visit_incbin(&mut self, data: &[u8]) -> Result<(), AssemblerError> {
        self.output_bytes(data)
    }

    /// Handle a crunched section.
    /// bytes generated during previous pass or previous loop are provided TO NOT crunched them an additional time if they are similar
    pub fn visit_crunched_section<'tokens, T: Visited + ListingElement + MayHaveSpan + Sync>(
        &mut self,
        kind: &CrunchType,
        lst: &mut [ProcessedToken<'tokens, T>],
        previous_bytes: &mut Option<Vec<u8>>,
        previous_crunched_bytes: &mut Option<Vec<u8>>,
        span: Option<&Z80Span>
    ) -> Result<(), AssemblerError>
    where
        <T as cpclib_tokens::ListingElement>::Expr: ExprEvaluationExt,
        ProcessedToken<'tokens, T>: FunctionBuilder,
        <<T as cpclib_tokens::ListingElement>::TestKind as cpclib_tokens::TestKindElement>::Expr:
            ExprEvaluationExt
    {
        // deactivated because there is no reason to do such thing
        // crunched section is disabled inside crunched section
        // if let Some(state) = & self.crunched_section_state {
        // let base = AssemblerError::AlreadyInCrunchedSection(state.crunched_section_start);
        // if let Some(span) = span {
        // return Err(AssemblerError::RelocatedError{error:base, span});
        // } else {
        // return Err(base);
        // }
        // }

        let could_display_warning_message = self.active_page_info().limit != 0xFFFF
            || !self.active_page_info().protected_areas.is_empty();

        // from here, the modifications to the memory will be forgotten afterwise.
        // for this reason everything is done in a cloned environnement
        // TODO to have a more stable memory function, see if we can keep some steps between the passes
        // TODO OR play all the passes directly now
        let mut crunched_env = self.clone();
        crunched_env.crunched_section_state = CrunchedSectionState::new(span.cloned()).into();
        // codeadr stays the same
        crunched_env.active_page_info_mut().logical_outputadr = 0; // relocated output at 0 to be sure to have 64kb available
                                                                   // XXX probably a wrong behavior/to see with users

        crunched_env.active_page_info_mut().startadr = None; // reset the counter to obtain the bytes
        crunched_env.active_page_info_mut().maxadr = 0;
        crunched_env.active_page_info_mut().limit = 0xFFFF; // disable limit (to be redone in the area)
        crunched_env.active_page_info_mut().protected_areas.clear(); // remove protected areas
        crunched_env.output_address = 0;

        self.output_trigger
            .as_mut()
            .map(|t| t.enter_crunched_section());

        visit_processed_tokens(lst, &mut crunched_env).map_err(|e| {
            dbg!(&self.pass, &crunched_env.pass);
            let e = AssemblerError::CrunchedSectionError { error: e.into() };
            match span {
                Some(span) => {
                    AssemblerError::RelocatedError {
                        error: e.into(),
                        span: span.clone()
                    }
                }
                None => e
            }
        })?;
        self.output_trigger
            .as_mut()
            .map(|t| t.leave_crunched_section());

        // get the new data and crunch it if needed
        let bytes = crunched_env.produced_bytes();

        let must_crunch = previous_bytes
            .as_ref()
            .map(|b| b.as_slice() != bytes.as_slice())
            .unwrap_or(true);
        if must_crunch {
            let crunched: Vec<u8> = if bytes.is_empty() {
                Vec::new()
            }
            else {
                kind.crunch(&bytes).map_err(|e| {
                    match span {
                        Some(span) => {
                            AssemblerError::RelocatedError {
                                error: e.into(),
                                span: span.clone()
                            }
                        }
                        None => e
                    }
                })?
            };
            previous_crunched_bytes.replace(crunched);
            previous_bytes.replace(bytes);
        }
        else {
        }

        let _bytes = previous_bytes.as_ref().unwrap();
        let crunched = previous_crunched_bytes.as_ref().unwrap();

        // inject the crunched data
        self.visit_incbin(&crunched).map_err(|e| {
            match span {
                Some(span) => {
                    AssemblerError::RelocatedError {
                        error: e.into(),
                        span: span.clone()
                    }
                }
                None => e
            }
        })?;

        // update the symbol table with the new symbols obtained in the crunched section
        std::mem::swap(self.symbols_mut(), crunched_env.symbols_mut());
        let can_skip_next_passes = (*self.can_skip_next_passes.read().unwrap().deref()
            & *crunched_env.can_skip_next_passes.read().unwrap())
        .into(); // report missing symbols from the crunched area to the current area
        let request_additional_pass = (*self.request_additional_pass.read().unwrap().deref()
            | *crunched_env.request_additional_pass.read().unwrap())
        .into();
        *self.can_skip_next_passes.write().unwrap() = can_skip_next_passes;
        *self.request_additional_pass.write().unwrap() = request_additional_pass;

        self.macro_seed = crunched_env.macro_seed;

        // TODO display ONLY if:
        // - no LIMIT/PROTECT has been used in the crunched area
        // - a possible forbidden write has been done (maybe too complex to implement)
        if could_display_warning_message {
            self.warnings.push(
                AssemblerWarning::AssemblingError{
                    msg: "Memory protection systems are disabled in crunched section. If you want to keep them, explicitely use LIMIT or PROTECT directives in the crunched section.".to_owned()
                }
            );
        }

        Ok(())
    }
}

impl Env {
    fn assemble_nop(&self, kind: Mnemonic, count: Option<&Expr>) -> Result<Bytes, AssemblerError> {
        let count = match count {
            Some(count) => self.resolve_expr_must_never_fail(count)?.int()?,
            None => 1
        };
        let mut bytes = Bytes::new();
        for _i in 0..count {
            match kind {
                Mnemonic::Nop => {
                    bytes.push(0);
                }
                Mnemonic::Nop2 => {
                    bytes.push(0xED);
                    bytes.push(0xFF);
                }
                _ => unreachable!()
            }
        }
        Ok(bytes)
    }
}
/// Visit the tokens during several passes without providing a specific symbol table.
pub fn visit_tokens_all_passes<
    'token,
    T: 'token + Visited + ToSimpleToken + Debug + Sync + ListingElement + MayHaveSpan
>(
    tokens: &'token [T],
    ctx: &ParserContext
) -> Result<Env, AssemblerError>
where
    <T as cpclib_tokens::ListingElement>::Expr: ExprEvaluationExt,
    <<T as cpclib_tokens::ListingElement>::TestKind as cpclib_tokens::TestKindElement>::Expr:
        ExprEvaluationExt,
    ProcessedToken<'token, T>: FunctionBuilder
{
    let options = AssemblingOptions::default();
    visit_tokens_all_passes_with_options(tokens, &options, ctx)
}

impl Env {
    pub fn new(options: &AssemblingOptions, ctx: &ParserContext) -> Self {
        let mut env = Env::default();
        env.ctx = ctx.clone();
        env.symbols =
            SymbolsTableCaseDependent::new(options.symbols().clone(), options.case_sensitive());

        if let Some(builder) = &options.output_builder {
            env.output_trigger = ListingOutputTrigger {
                token: None,
                bytes: Vec::new(),
                builder: builder.clone(),
                start: 0
            }
            .into();
        }
        env
    }

    pub fn pass(&self) -> &AssemblingPass {
        &self.pass
    }
}

// Functions related
impl Env {
    pub fn visit_return(&mut self, e: &Expr) -> Result<(), AssemblerError> {
        debug_assert!(self.return_value.is_none());
        self.return_value = Some(self.resolve_expr_must_never_fail(e)?);
        Ok(())
    }

    pub fn user_defined_function(&self, name: &str) -> Result<&Function, AssemblerError> {
        match self.functions.get(name) {
            Some(f) => Ok(f),
            None => Err(AssemblerError::FunctionUnknown(name.to_owned()))
        }
    }

    pub fn any_function<'res>(
        &'res self,
        name: &'res str
    ) -> Result<&'res Function, AssemblerError> {
        match HardCodedFunction::by_name(name) {
            Some(f) => Ok(f),
            None => self.user_defined_function(name)
        }
    }
}

/// Visit the tokens during several passes by providing a specific symbol table.
/// Warning Listing output is only possible for LocatedToken
pub fn visit_tokens_all_passes_with_options<'token, T>(
    tokens: &'token [T],
    options: &AssemblingOptions,
    ctx: &ParserContext
) -> Result<Env, AssemblerError>
where
    T: Visited + ToSimpleToken + Debug + Sync + ListingElement + MayHaveSpan,
    <T as cpclib_tokens::ListingElement>::Expr: ExprEvaluationExt,
    <<T as cpclib_tokens::ListingElement>::TestKind as TestKindElement>::Expr: ExprEvaluationExt,
    ProcessedToken<'token, T>: FunctionBuilder
{
    let mut env = Env::new(options, ctx);
    let mut tokens = processed_token::build_processed_tokens_list(tokens, &mut env);
    loop {
        env.start_new_pass();
        // println!("[pass] {:?}", env.pass);

        if env.pass.is_finished() {
            break;
        }

        processed_token::visit_processed_tokens(&mut tokens, &mut env)?;
    }

    // Add an additional pass to build the listing (this way it is built only one time)
    if options.output_builder.is_some() {
        env.pass = AssemblingPass::ListingPass;
        env.start_new_pass();
        processed_token::visit_processed_tokens(&mut tokens, &mut env)
            .map_err(|e| eprintln!("{}", e))
            .expect("No error can arise in listing output mode; there is a bug somewhere");
    }

    if let Some(trigger) = env.output_trigger.as_mut() {
        trigger.finish()
    }

    env.saved_files = Some(env.handle_post_actions()?);

    Ok(env)
}

/// Visit the tokens during a single pass. Is deprecated in favor to the mulitpass version
#[deprecated(note = "use visit_tokens_one_pass")]
pub fn visit_tokens<T: Visited>(tokens: &[T]) -> Result<Env, AssemblerError> {
    visit_tokens_one_pass(tokens)
}

/// Assemble the tokens doing one pass only (so symbols are not properly treated)
pub fn visit_tokens_one_pass<T: Visited>(tokens: &[T]) -> Result<Env, AssemblerError> {
    let mut env = Env::default();

    for token in tokens.iter() {
        token.visited(&mut env)?;
    }

    Ok(env)
}

/// Apply the effect of the localised token. Most of the action is delegated to visit_token.
/// The difference with the standard token is the ability to embed listing
pub fn visit_located_token(
    outer_token: &LocatedToken,
    env: &mut Env
) -> Result<(), AssemblerError> {
    let nb_warnings = env.warnings.len();

    // cheat on the lifetime of tokens
    let outer_token = unsafe { (outer_token as *const LocatedToken).as_ref().unwrap() };

    env.handle_output_trigger(outer_token);

    let span = outer_token.span();
    match outer_token {
        LocatedToken::Standard { token, span } => {
            match token {
                Token::Assert(exp, txt) => {
                    visit_assert(exp, txt.as_ref(), env, Some(span.clone()))?;
                    Ok(())
                }

                Token::Breakpoint(expr) => env.visit_breakpoint(expr.as_ref(), Some(span.clone())),

                Token::Macro(_name, _arguments, _code) => {
                    panic!("Should delegate it to ProcessedToken")
                }
                Token::MacroCall(..) => {
                    panic!("Should not be called")
                }

                Token::Pause => {
                    env.visit_pause(Some(span.clone()));
                    Ok(())
                }

                Token::Print(ref exp) => {
                    env.visit_print(exp.as_ref(), Some(span.clone()));
                    Ok(())
                }

                Token::Struct(name, content) => {
                    env.visit_struct_definition(name, content.as_slice(), Some(span))
                        .map_err(|e| e.locate(span.clone()))
                }
                Token::Incbin { .. } | Token::Include(..) => {
                    panic!("Should never be called");
                    // if content.read().unwrap().is_none() {
                    // outer_token
                    // .read_referenced_file(&outer_token.context().1)
                    // .and_then(|_| visit_located_token(outer_token, env))
                    // .map_err(|e| e.locate(span.clone()))
                    // }
                    // else {
                    // env.visit_incbin(content.read().unwrap().as_ref().unwrap())
                    // }
                    // .map_err(|err| {
                    // AssemblerError::IncludedFileError {
                    // span: span.clone(),
                    // error: Box::new(err)
                    // }
                    // })
                }
                _ => {
                    token.visited(env).map_err(|err| {
                        AssemblerError::RelocatedError {
                            error: Box::new(err),
                            span: span.clone()
                        }
                    })
                }
            }
        }

        LocatedToken::CrunchedSection(..) => {
            panic!("Should be handled by ProcessedToken")
        }

        LocatedToken::Function(..) => {
            panic!("Should be handled by ProcessedToken")
        }

        LocatedToken::If(..) => {
            panic!("Should be handled by ProcessedToken")
        }

        LocatedToken::Module(name, code, span) => {
            env.enter_namespace(name)
                .map_err(|e| e.locate(span.clone()))?;
            env.visit_listing(code)?;
            env.leave_namespace().map_err(|e| e.locate(span.clone()))?;
            Ok(())
        }

        LocatedToken::Repeat(..) | LocatedToken::RepeatUntil(..) | LocatedToken::Rorg(..) => {
            panic!("Should be handled by ProcessedToken")
        }

        LocatedToken::Switch(value, cases, default, span) => {
            env.visit_switch(
                value,
                cases.iter().map(|c| (&c.0, &c.1[..], c.2)),
                default.as_ref().map(|l| &l[..]),
                Some(span)
            )
        }
        LocatedToken::While(cond, inner, span) => env.visit_while(cond, inner, Some(span.clone())),
        LocatedToken::Iterate(..) => {
            panic!("Should never be called")
        }
        LocatedToken::For { .. } => {
            panic!("Should never be called")
        }
        LocatedToken::Label(label) => {
            env.visit_label(label.as_str())
                .map_err(|e| e.locate(label.clone()))
        }
        LocatedToken::MacroCall(_name, _args, _span) => {
            panic!("Should never be called")
        }
        LocatedToken::Struct(name, args, span) => {
            env.visit_struct_definition(name.as_str(), args, Some(span))
        }
        LocatedToken::Defb(l, span) => {
            visit_db_or_dw_or_str(DbLikeKind::Defb, l.as_ref(), env)
                .map_err(|e| e.locate(span.clone()))
        }
        LocatedToken::Defw(l, span) => {
            visit_db_or_dw_or_str(DbLikeKind::Defw, l.as_ref(), env)
                .map_err(|e| e.locate(span.clone()))
        }
        LocatedToken::Str(l, span) => {
            visit_db_or_dw_or_str(DbLikeKind::Str, l.as_ref(), env)
                .map_err(|e| e.locate(span.clone()))
        }
        LocatedToken::Include(..) => panic!("Should never been called"),
        LocatedToken::Incbin { .. } => panic!("Should never been called"),
        LocatedToken::Macro {
            name: _,
            params: _,
            content: _,
            span: _
        } => panic!("Should never been called")
    }?;

    // Patch the warnings to inject them a location
    let nb_additional_warnings = env.warnings.len() - nb_warnings;
    for i in 0..nb_additional_warnings {
        let warning = &mut env.warnings[i + nb_warnings];
        if !warning.is_located() {
            *warning = AssemblerError::RelocatedWarning {
                warning: Box::new(warning.clone()),
                span: span.clone()
            };
        }
    }

    env.move_delayed_commands_of_functions();

    Ok(())
}

/// Apply the effect of the token
fn visit_token(token: &Token, env: &mut Env) -> Result<(), AssemblerError> {
    env.update_dollar();
    // dbg!(token, env.active_page_info());
    match token {
        Token::Align(ref boundary, ref fill) => env.visit_align(boundary, fill.as_ref()),
        Token::Assert(ref exp, ref txt) => {
            visit_assert(exp, txt.as_ref(), env, None)?;
            Ok(())
        }
        Token::Basic(ref variables, ref hidden_lines, ref code) => {
            env.visit_basic(variables.as_ref(), hidden_lines.as_ref(), code)
        }
        Token::BuildSna(ref v) => env.visit_buildsna(v.as_ref()),
        Token::Bank(ref exp) => env.visit_bank(exp.as_ref()),
        Token::Bankset(ref v) => env.visit_bankset(v),
        Token::Breakpoint(ref exp) => env.visit_breakpoint(exp.as_ref(), None),
        Token::Org(ref address, ref address2) => visit_org(address, address2.as_ref(), env),
        Token::Defb(l) => visit_db_or_dw_or_str(DbLikeKind::Defb, l.as_ref(), env),
        Token::Defw(l) => visit_db_or_dw_or_str(DbLikeKind::Defw, l.as_ref(), env),
        Token::Str(l) => visit_db_or_dw_or_str(DbLikeKind::Str, l.as_ref(), env),
        Token::Defs(_) => visit_defs(token, env),
        Token::End => visit_end(env),
        Token::OpCode(ref mnemonic, ref arg1, ref arg2, ref arg3) => {
            visit_opcode(*mnemonic, &arg1, &arg2, &arg3, env)?;
            // Compute duration only if it is necessary
            if !env.stable_counters.is_empty() {
                let duration = token.estimated_duration()?;
                env.stable_counters.update_counters(duration);
            }
            Ok(())
        }
        Token::Comment(_) => Ok(()), // Nothing to do for a comment
        Token::List => {
            env.output_trigger.as_mut().map(|l| {
                l.on();
            });
            Ok(())
        }
        Token::NoList => {
            env.output_trigger.as_mut().map(|l| {
                l.off();
            });
            Ok(())
        }
        Token::Include(_, _namespace, _once) => {
            panic!("ERROR - Should never be executed");/*
            if *once {
                unimplemented!("ONCE on hardcoded tokens");
            }

            if let Some(namespace) = namespace.as_ref() {
                env.enter_namespace(namespace)?;
            }
            env.visit_listing(cell.read().unwrap().as_ref().unwrap())?;
            if namespace.is_some() {
                env.leave_namespace()?;
            }
            */
            Ok(())
        }
        Token::Include(_fname, _namespace, _once) => {
            panic!("ERROR - Should never be executed");
        }
        Token::Incbin {
            fname: _,
            offset: _,
            length: _,
            extended_offset: _,
            off: _,
            transformation: _
        } => panic!("Error - should never be called"),

        Token::If(ref _cases, ref _other) => {
            panic!("Should be handled by ProcessedToken")
        }
        Token::Label(ref label) => env.visit_label(label),
        Token::Limit(ref exp) => env.visit_limit(exp),
        Token::MultiPush(ref regs) => env.visit_multi_pushes(regs),
        Token::MultiPop(ref regs) => env.visit_multi_pops(regs),
        Token::NoExport(ref labels) => env.visit_noexport(labels.as_slice()),
        Token::Export(ref labels) => env.visit_export(labels.as_slice()),
        Token::Equ(ref label, ref exp) => visit_equ(label, exp, env),
        Token::Assign(ref label, ref exp, op) => visit_assign(label, exp, op.as_ref(), env),
        Token::Pause => {
            env.visit_pause(None);
            Ok(())
        }
        Token::Protect(ref start, ref end) => env.visit_protect(start, end),
        Token::Print(ref exp) => {
            env.visit_print(exp.as_ref(), None);
            Ok(())
        }
        Token::Fail(ref exp) => env.visit_fail(exp.as_ref().map(|v| v.as_slice())),
        Token::Repeat(..) => {
            panic!("Should never be called")
        }
        Token::Run(address, gate_array) => env.visit_run(address, gate_array.as_ref()),
        Token::Rorg(ref _exp, ref _code) => panic!("Is delegated to ProcessedToken"),
        Token::Save {
            filename,
            address,
            size,
            save_type,
            dsk_filename,
            side
        } => {
            env.visit_save(
                filename,
                address.as_ref(),
                size.as_ref(),
                save_type.as_ref(),
                dsk_filename.as_ref(),
                side.as_ref()
            )
        }
        Token::Charset(format) => env.visit_charset(format),
        Token::SnaSet(flag, value) => env.visit_snaset(flag, value),
        Token::StableTicker(ref ticker) => visit_stableticker(ticker, env),
        Token::Undef(ref label) => env.visit_undef(label),
        Token::Macro(_name, _arguments, _code) => {
            panic!("Is delegated to ProcessedToken")
        }
        Token::MacroCall(_name, _parameters) => panic!("Should never been called"),
        Token::Struct(name, content) => env.visit_struct_definition(name, content.as_slice(), None),
        Token::WaitNops(count) => env.visit_waitnops(count),
        Token::Next(label, source, delta) => {
            env.visit_next_and_co(label, source, delta.as_ref(), false)
        }
        Token::SnaInit(fname) => env.visit_snainit(fname),
        Token::SetN(label, source, delta) => {
            env.visit_next_and_co(label, source, delta.as_ref(), true)
        }
        Token::Range(name, start, stop) => env.visit_range(name, start, stop),
        Token::Section(name) => env.visit_section(name),

        Token::Return(exp) => env.visit_return(exp),
        _ => unimplemented!("{:?}", token)
    }?;

    env.move_delayed_commands_of_functions();
    Ok(())
}

/// No error is generated here; everything is delayed at the end of assembling.
/// Returns false in case of assert failure
fn visit_assert(
    exp: &Expr,
    txt: Option<&Vec<FormattedExpr>>,
    env: &mut Env,
    span: Option<Z80Span>
) -> Result<bool, AssemblerError> {
    let res = match env.resolve_expr_must_never_fail(exp) {
        Err(e) => Err(e),

        Ok(value) => {
            if !value.bool()? {
                let _symbols = env.symbols();
                let oper = |left: &Expr, right: &Expr, oper: &str| -> String {
                    let res_left = left.resolve(env).unwrap();
                    let res_right = right.resolve(env).unwrap();

                    match (&res_left, &res_right) {
                        (ExprResult::Char(c1), ExprResult::Char(c2)) => {
                            format!("['{}' {} '{}']", *c1 as char, oper, *c2 as char)
                        }
                        _ => {
                            format!("[{} {} {}] ", res_left, oper, res_right)
                                + &format!("[0x{:x} {} 0x{:x}] ", res_left, oper, res_right)
                        }
                    }
                };

                let prefix = match exp {
                    Expr::BinaryOperation(BinaryOperation::Equal, box left, box right) => {
                        oper(left, right, "==")
                    }

                    Expr::BinaryOperation(BinaryOperation::LowerOrEqual, box left, box right) => {
                        oper(left, right, "<=")
                    }

                    Expr::BinaryOperation(BinaryOperation::GreaterOrEqual, box left, box right) => {
                        oper(left, right, ">=")
                    }

                    Expr::BinaryOperation(BinaryOperation::StrictlyLower, box left, box right) => {
                        oper(left, right, "<")
                    }

                    Expr::BinaryOperation(
                        BinaryOperation::StrictlyGreater,
                        box left,
                        box right
                    ) => oper(left, right, ">"),

                    _ => "".to_string()
                };

                Err(AssemblerError::AssertionFailed {
                    msg: prefix
                        + &if txt.is_some() {
                            env.build_string_from_formatted_expression(txt.unwrap())?
                        }
                        else {
                            "".to_owned()
                        },
                    test: exp.to_string(),
                    guidance: env.to_assert_string(exp)
                })
            }
            else {
                Ok(())
            }
        }
    };

    if let Err(assert_error) = res {
        let assert_error = if let Some(span) = span {
            assert_error.locate(span)
        }
        else {
            assert_error
        };
        env.active_page_info_mut()
            .add_failed_assert_command(assert_error.into());
        Ok(false)
    }
    else {
        Ok(true)
    }
}

impl Env {
    pub fn visit_while<
        E: ExprEvaluationExt,
        T: ListingElement<Expr = E> + Visited + MayHaveSpan
    >(
        &mut self,
        cond: &E,
        code: &[T],
        span: Option<Z80Span>
    ) -> Result<(), AssemblerError> {
        while self.resolve_expr_must_never_fail(cond)?.bool()? {
            // generate the bytes
            self.visit_listing(code).map_err(|e| {
                AssemblerError::WhileIssue {
                    error: Box::new(e),
                    span: span.clone()
                }
            })?;
        }

        Ok(())
    }

    /// Handle the switch directive
    pub fn visit_switch<'a, T, E>(
        &mut self,
        value: &E,
        cases: impl Iterator<Item = (&'a E, &'a [T], bool)>,
        default: Option<&'a [T]>,
        _span: Option<&Z80Span>
    ) -> Result<(), AssemblerError>
    where
        E: ExprEvaluationExt,
        E: 'a,
        T: 'a + ListingElement<Expr = E> + Visited + MayHaveSpan
    {
        let value = self.resolve_expr_must_never_fail(value)?;
        let mut met = false;
        let mut broken = false;
        for (case, listing, r#break) in cases {
            // check if case must be executed
            let case = self.resolve_expr_must_never_fail(case)?;
            met |= case == value;

            // inject code if needed and leave if break is present
            if met {
                self.visit_listing(listing)?;
                if r#break {
                    broken = true;
                    break;
                }
            }
        }

        // execute default if any
        if !met || !broken {
            if let Some(default) = default {
                self.visit_listing(default)?;
            }
        }

        Ok(())
    }

    /// Handle the iterate repetition directive
    /// Values is either a list of values or a Expression that represents a list
    pub fn visit_iterate<
        'token,
        E: ExprEvaluationExt,
        T: ListingElement<Expr = E> + Visited + MayHaveSpan + Sync
    >(
        &mut self,
        counter_name: &str,
        values: either::Either<&Vec<E>, &E>,
        code: &mut [ProcessedToken<'token, T>],
        span: Option<&Z80Span>
    ) -> Result<(), AssemblerError>
    where
        <T as cpclib_tokens::ListingElement>::Expr: ExprEvaluationExt,
        <<T as cpclib_tokens::ListingElement>::TestKind as TestKindElement>::Expr:
            ExprEvaluationExt,
        ProcessedToken<'token, T>: FunctionBuilder
    {
        let counter_name = format!("{{{}}}", counter_name);
        let counter_name = counter_name.as_str();
        if self.symbols().contains_symbol(counter_name)? {
            return Err(AssemblerError::RepeatIssue {
                error: AssemblerError::ExpressionError(ExpressionError::OwnError(
                    box AssemblerError::AssemblingError {
                        msg: format!("Counter {} already exists", counter_name)
                    }
                ))
                .into(),
                span: span.cloned(),
                repetition: 0
            });
        }

        // Get the values (all args or list explosion)
        // BUG: iteration over values make the expressions progressively evaluated, while iteration over a list make its expressions evaluated at first loop
        match values {
            either::Either::Left(values) => {
                for (i, value) in values.iter().enumerate() {
                    let counter_value = self.resolve_expr_must_never_fail(value).map_err(|e| {
                        AssemblerError::RepeatIssue {
                            error: Box::new(e),
                            span: span.cloned(),
                            repetition: i as _
                        }
                    })?;
                    self.inner_visit_repeat(
                        Some(counter_name),
                        Some(counter_value),
                        i as _,
                        code,
                        span
                    )?;
                }
            }
            either::Either::Right(values) => {
                match self.resolve_expr_must_never_fail(values)? {
                    ExprResult::List(values) => {
                        for (i, counter_value) in values.into_iter().enumerate() {
                            self.inner_visit_repeat(
                                Some(counter_name),
                                Some(counter_value),
                                i as _,
                                code,
                                span.clone()
                            )?;
                        }
                    }
                    _ => {
                        return Err(AssemblerError::AssemblingError {
                            msg: format!("REPEAT issue: {} is not a list", values)
                        })
                    }
                }
            }
        }

        // Apply the iteration

        // TODO restore a previous value if any
        self.symbols_mut().remove_symbol(counter_name)?;

        Ok(())
    }

    pub fn visit_rorg<'token, T, E>(
        &mut self,
        address: &E,
        code: &mut [ProcessedToken<'token, T>],
        span: Option<&Z80Span>
    ) -> Result<(), AssemblerError>
    where
        E: ExprEvaluationExt,
        T: ListingElement<Expr = E> + Visited + MayHaveSpan + Sync,
        <T as cpclib_tokens::ListingElement>::Expr: ExprEvaluationExt,
        <<T as cpclib_tokens::ListingElement>::TestKind as TestKindElement>::Expr:
            ExprEvaluationExt,
        ProcessedToken<'token, T>: FunctionBuilder
    {
        // Get the next code address
        let address = self
            .resolve_expr_must_never_fail(address)
            .map_err(|error| {
                match span {
                    Some(span) => {
                        AssemblerError::RelocatedError {
                            error: Box::new(error),
                            span: span.clone()
                        }
                    }
                    None => error
                }
            })?
            .int()?;

        // do not change the output address
        {
            let page_info = self.active_page_info_mut();
            page_info.logical_codeadr = address as _;
        }

        self.update_dollar();
        let value = self.active_page_info().logical_codeadr;

        self.output_trigger
            .as_mut()
            .map(|o| o.replace_address(value.into()));

        // execute the listing
        self.nested_rorg += 1; // used to disable page functionalities
        visit_processed_tokens(code, self)?;
        self.nested_rorg -= 1;

        // restore the appropriate  address
        let page_info = self.active_page_info_mut();
        page_info.logical_codeadr = page_info.logical_outputadr;

        Ok(())
    }

    /// Handle the for directive
    pub fn visit_for<'token, E: ExprEvaluationExt, T>(
        &mut self,
        label: &str,
        start: &E,
        stop: &E,
        step: Option<&E>,
        code: &mut [ProcessedToken<'token, T>],
        span: Option<&Z80Span>
    ) -> Result<(), AssemblerError>
    where
        T: ListingElement<Expr = E> + Visited + MayHaveSpan + Sync,
        <T as cpclib_tokens::ListingElement>::Expr: ExprEvaluationExt,
        <<T as cpclib_tokens::ListingElement>::TestKind as TestKindElement>::Expr:
            ExprEvaluationExt,
        ProcessedToken<'token, T>: FunctionBuilder
    {
        let counter_name = format!("{{{}}}", label);
        if self.symbols().contains_symbol(&counter_name)? {
            return Err(AssemblerError::ForIssue {
                error: AssemblerError::ExpressionError(ExpressionError::OwnError(
                    box AssemblerError::AssemblingError {
                        msg: format!("Counter {} already exists", &counter_name)
                    }
                ))
                .into(),
                span: span.cloned()
            });
        }

        let mut counter_value = self.resolve_expr_must_never_fail(start)?;
        let stop = self.resolve_expr_must_never_fail(stop)?;
        let step = match step {
            Some(step) => self.resolve_expr_must_never_fail(step)?,
            None => 1i32.into()
        };

        let zero = ExprResult::from(0i32);

        if step == zero {
            return Err(AssemblerError::ForIssue {
                error: AssemblerError::ExpressionError(ExpressionError::OwnError(
                    box AssemblerError::AssemblingError {
                        msg: format!("Infinite loop")
                    }
                ))
                .into(),
                span: span.cloned()
            });
        }

        if step < zero {
            return Err(AssemblerError::ForIssue {
                error: AssemblerError::ExpressionError(ExpressionError::OwnError(
                    box AssemblerError::AssemblingError {
                        msg: format!("Negative step is not yet handled")
                    }
                ))
                .into(),
                span: span.cloned()
            });
        }

        let mut i = 1;
        while counter_value <= stop {
            self.inner_visit_repeat(
                Some(counter_name.as_str()),
                Some(counter_value.clone()),
                i as _,
                code,
                span
            )?;
            counter_value = (counter_value + &step)?;
            i += 1;
        }

        self.symbols_mut().remove_symbol(counter_name)?;

        Ok(())
    }

    /// Handle the standard repetition directive
    pub fn visit_repeat_until<'token, E, T>(
        &mut self,
        cond: &E,
        code: &mut [ProcessedToken<'token, T>],
        span: Option<&Z80Span>
    ) -> Result<(), AssemblerError>
    where
        E: ExprEvaluationExt,
        T: ListingElement<Expr = E> + Visited + MayHaveSpan + Sync,
        <T as cpclib_tokens::ListingElement>::Expr: ExprEvaluationExt,
        <<T as cpclib_tokens::ListingElement>::TestKind as TestKindElement>::Expr:
            ExprEvaluationExt,
        ProcessedToken<'token, T>: FunctionBuilder
    {
        let mut i = 0;
        loop {
            i = i + 1;
            self.inner_visit_repeat(None, None, i as _, code, span.clone())?;
            let res = self.resolve_expr_must_never_fail(cond)?;
            if res.bool()? {
                break;
            }
        }

        Ok(())
    }

    /// Handle the statndard repetition directive
    pub fn visit_repeat<'token, T, E>(
        &mut self,
        count: &E,
        code: &mut [ProcessedToken<'token, T>],
        counter: Option<&str>,
        counter_start: Option<&E>,
        span: Option<&Z80Span>
    ) -> Result<(), AssemblerError>
    where
        E: ExprEvaluationExt,
        T: ListingElement<Expr = E> + Visited + MayHaveSpan + Sync,
        <T as cpclib_tokens::ListingElement>::Expr: ExprEvaluationExt,
        <<T as cpclib_tokens::ListingElement>::TestKind as TestKindElement>::Expr:
            ExprEvaluationExt,
        ProcessedToken<'token, T>: FunctionBuilder
    {
        // get the number of loops
        let count = self.resolve_expr_must_never_fail(count)?.int()?;

        // get the counter name of any
        let counter_name = counter.as_ref().map(|counter| format!("{{{}}}", counter));
        let counter_name = counter_name.as_ref().map(|s| s.as_str());
        if let Some(counter_name) = counter_name {
            if self.symbols().contains_symbol(counter_name)? {
                return Err(AssemblerError::RepeatIssue {
                    error: AssemblerError::ExpressionError(ExpressionError::OwnError(
                        box AssemblerError::AssemblingError {
                            msg: format!("Counter {} already exists", counter_name)
                        }
                    ))
                    .into(),
                    span: span.cloned(),
                    repetition: 0
                });
            }
        }

        // get the first value
        let mut counter_value = counter_start
            .map(|start| self.resolve_expr_must_never_fail(start))
            .unwrap_or(Ok(REPEAT_START_VALUE.into()))?;

        for i in 0..count {
            self.inner_visit_repeat(
                counter_name,
                Some(counter_value.clone()),
                i as _,
                code,
                span.clone()
            )?;
            // handle the counter update
            counter_value += 1i32.into();
        }

        if let Some(counter_name) = counter_name {
            self.symbols_mut().remove_symbol(counter_name)?;
        }
        Ok(())
    }

    /// Handle the code generation for all the repetition variants
    fn inner_visit_repeat<'token, T: ListingElement + Visited + MayHaveSpan + Sync>(
        &mut self,
        counter_name: Option<&str>,
        counter_value: Option<ExprResult>,
        iteration: i32,
        code: &mut [ProcessedToken<'token, T>],
        span: Option<&Z80Span>
    ) -> Result<(), AssemblerError>
    where
        <T as cpclib_tokens::ListingElement>::Expr: ExprEvaluationExt,
        <<T as cpclib_tokens::ListingElement>::TestKind as TestKindElement>::Expr:
            ExprEvaluationExt,
        ProcessedToken<'token, T>: FunctionBuilder
    {
        // handle symbols unicity
        {
            self.macro_seed += 1;
            let seed = self.macro_seed;
            self.symbols_mut().push_seed(seed);
        }

        // handle counter value update
        if let Some(counter_name) = counter_name {
            self.symbols_mut()
                .set_symbol_to_value(counter_name, counter_value.clone().unwrap())?;
        }

        // generate the bytes
        visit_processed_tokens(code, self).map_err(|e| {
            let e = if let AssemblerError::RelocatedError {
                error: box AssemblerError::UnknownSymbol { closest: _, symbol },
                span
            } = &e
            {
                dbg!(symbol, counter_name);
                if let Some(counter_name) = counter_name {
                    if counter_name == &format!("{{{}}}", symbol) {
                        AssemblerError::RelocatedError {
                            error: box AssemblerError::UnknownSymbol {
                                closest: Some(counter_name.into()),
                                symbol: symbol.clone()
                            },
                            span: span.clone()
                        }
                    }
                    else {
                        e
                    }
                }
                else {
                    e
                }
            }
            else {
                e
            };

            AssemblerError::RepeatIssue {
                error: Box::new(e),
                span: span.cloned(),
                repetition: iteration as _
            }
        })?;

        // handle the end of visibility of unique labels
        self.symbols_mut().pop_seed();

        Ok(())
    }

    /// Generate a string that is helpfull for assertion understanding (i.e. show the operation and evaluate the rest)
    /// Crash if expression cannot be computed
    fn to_assert_string(&self, exp: &Expr) -> String {
        let format = |oper, left, right| {
            format!(
                "0x{:x} {} 0x{:x}",
                self.resolve_expr_must_never_fail(left).unwrap(),
                oper,
                self.resolve_expr_must_never_fail(right).unwrap(),
            )
        };

        if exp.is_binary_operation() {
            let code = match exp.binary_operation() {
                BinaryOperation::Equal => Some("=="),
                BinaryOperation::GreaterOrEqual => Some(">="),
                BinaryOperation::StrictlyGreater => Some(">"),
                BinaryOperation::StrictlyLower => Some("<"),
                BinaryOperation::LowerOrEqual => Some("<="),
                _ => None
            };

            match code {
                Some(code) => {
                    let left = exp.arg1();
                    let right = exp.arg2();
                    format(code, left, right)
                }

                None => format!("0x{:x}", self.resolve_expr_must_never_fail(exp).unwrap())
            }
        }
        else {
            format!("0x{:x}", self.resolve_expr_must_never_fail(exp).unwrap())
        }
    }

    fn visit_run(&mut self, address: &Expr, ga: Option<&Expr>) -> Result<(), AssemblerError> {
        let address = self.resolve_expr_may_fail_in_first_pass(address)?.int()?;

        self.output_trigger
            .as_mut()
            .map(|o| o.replace_address(address.into()));

        if self.run_options.is_some() {
            return Err(AssemblerError::RunAlreadySpecified);
        }
        self.sna
            .set_value(cpclib_sna::SnapshotFlag::Z80_PC, address as _)?;

        match ga {
            None => {
                self.run_options = Some((address as _, None));
            }
            Some(ga_expr) => {
                let ga_expr = self.resolve_expr_may_fail_in_first_pass(ga_expr)?.int()?;
                self.sna.set_value(SnapshotFlag::GA_RAMCFG, address as _)?;
                self.run_options = Some((address as _, Some(ga_expr as _)));
            }
        }
        Ok(())
    }
}

/// Macro related code
impl Env {
    pub fn inc_macro_seed(&mut self) {
        self.macro_seed += 1;
    }

    pub fn macro_seed(&self) -> usize {
        self.macro_seed
    }
}

fn visit_equ(label: &str, exp: &Expr, env: &mut Env) -> Result<(), AssemblerError> {
    if env.symbols().contains_symbol(label)? && env.pass.is_first_pass() {
        Err(AssemblerError::AlreadyDefinedSymbol {
            symbol: label.into(),
            kind: env.symbols().kind(label)?.into()
        })
    }
    else {
        let value = env.resolve_expr_may_fail_in_first_pass(exp)?;
        env.output_trigger
            .as_mut()
            .map(|o| o.replace_address(value.clone()));
        env.add_symbol_to_symbol_table(label, value)
    }
}

fn visit_assign(
    label: &str,
    exp: &Expr,
    op: Option<&BinaryOperation>,
    env: &mut Env
) -> Result<(), AssemblerError> {
    let value = if let Some(op) = op {
        let new_exp = Expr::BinaryOperation(*op, box Expr::Label(label.into()), box exp.clone());
        env.resolve_expr_must_never_fail(&new_exp)?
    }
    else {
        env.resolve_expr_may_fail_in_first_pass(exp)?
    };

    env.output_trigger
        .as_mut()
        .map(|o| o.replace_address(value.clone()));

    env.symbols_mut().assign_symbol_to_value(label, value)?;

    Ok(())
}

fn visit_defs(token: &Token, env: &mut Env) -> Result<(), AssemblerError> {
    match token {
        Token::Defs(l) => {
            for (e, f) in l.iter() {
                let bytes = assemble_defs_item(e, f.as_ref(), env)?;
                env.output_bytes(&bytes)?;
            }
            Ok(())
        }
        _ => unreachable!()
    }
}

fn visit_end(_env: &mut Env) -> Result<(), AssemblerError> {
    eprintln!("END directive is not implemented");
    Ok(())
}

pub enum DbLikeKind {
    Defb,
    Defw,
    Str
}

impl From<&Token> for DbLikeKind {
    fn from(token: &Token) -> Self {
        match token {
            Token::Defb(..) => Self::Defb,
            Token::Defw(..) => Self::Defw,
            Token::Str(..) => Self::Str,
            _ => unreachable!()
        }
    }
}

impl DbLikeKind {
    fn mask(&self) -> u16 {
        match self {
            DbLikeKind::Defb => 0xFF,
            DbLikeKind::Defw => 0xFFFF,
            DbLikeKind::Str => 0xFF
        }
    }
}

// TODO refactor code with assemble_opcode or other functions manipulating bytes
pub fn visit_db_or_dw_or_str<E: ExprEvaluationExt + ExprElement>(
    kind: DbLikeKind,
    exprs: &[E],
    env: &mut Env
) -> Result<(), AssemblerError> {
    let mask = kind.mask();

    let output = |env: &mut Env, val: i32, mask: u16| -> Result<(), AssemblerError> {
        if mask == 0xFF {
            env.output(val as u8)?;
        }
        else {
            let high = ((val & 0xFF00) >> 8) as u8;
            let low = (val & 0xFF) as u8;
            env.output(low)?;
            env.output(high)?;
        }
        Ok(())
    };

    let output_expr_result = |env: &mut Env, expr: ExprResult, mask: u16| {
        match &expr {
            ExprResult::Float(_)
            | ExprResult::Value(_)
            | ExprResult::Bool(_)
            | ExprResult::Char(_) => output(env, expr.int()?, mask),
            ExprResult::String(s) => {
                for c in s.chars() {
                    output(env, c as _, mask)?;
                }
                Ok(())
            }
            ExprResult::List(l) => {
                for c in l {
                    output(env, c.int()?, mask)?;
                }
                Ok(())
            }
            ExprResult::Matrix { .. } => {
                for row in expr.matrix_rows() {
                    for c in row.list_content() {
                        output(env, c.int()?, mask)?;
                    }
                }
                Ok(())
            }
        }
    };

    let backup_address = env.logical_output_address();
    for exp in exprs.iter() {
        if exp.is_string() {
            let s = exp.string();
            let bytes = env.charset_encoding.transform_string(s);
            for b in &bytes {
                output(env, *b as _, mask)?
            }
            env.update_dollar();
        }
        else if exp.is_char() {
            let c = exp.char();
            let b = env.charset_encoding.transform_char(c);
            output(env, b as _, mask)?;
            env.update_dollar();
        }
        else {
            let val = env.resolve_expr_may_fail_in_first_pass(exp)?;
            output_expr_result(env, val, mask)?;
            env.update_dollar();
        }
    }

    // Patch the last char of a str
    if matches!(kind, DbLikeKind::Str) && backup_address < env.logical_output_address() {
        let last_address = env.logical_output_address() - 1;
        let last_address = env.logical_to_physical_address(last_address as _);
        let last_value = env.peek(&last_address);
        env.poke(last_value | 0x80, &last_address);
    }

    Ok(())
}

impl Env {
    // TODO find a more efficient way; there a tons of copies there...
    fn move_delayed_commands_of_functions(&mut self) {
        {
            let prints = self.extra_print_from_function.read().unwrap().clone();
            for print in prints.into_iter() {
                self.active_page_info_mut()
                    .add_print_or_pause_command(print);
            }
            self.extra_print_from_function.write().unwrap().clear();
        }

        {
            let asserts = self
                .extra_failed_assert_from_function
                .read()
                .unwrap()
                .clone();
            for assert in asserts.into_iter() {
                self.active_page_info_mut()
                    .add_failed_assert_command(assert);
            }
            self.extra_failed_assert_from_function
                .write()
                .unwrap()
                .clear();
        }
    }
}

#[allow(missing_docs)]
impl Env {
    pub fn visit_basic<S: Borrow<str> + Display>(
        &mut self,
        variables: Option<&Vec<S>>,
        hidden_lines: Option<&Vec<u16>>,
        code: &str
    ) -> Result<(), AssemblerError> {
        let bytes = self.assemble_basic(variables, hidden_lines, code)?;

        // If the basic directive is the VERY first thing to output,
        // we assume startadr is 0x170 as for any basic program
        if self.start_address().is_none() {
            self.active_page_info_mut().logical_outputadr = 0x170;
            self.active_page_info_mut().logical_codeadr = self.logical_output_address();
            self.active_page_info_mut().startadr = Some(self.logical_output_address());
            self.output_address = 0x170;
        }

        self.output_bytes(&bytes)
    }

    pub fn assemble_basic<S: Borrow<str> + Display>(
        &mut self,
        variables: Option<&Vec<S>>,
        hidden_lines: Option<&Vec<u16>>,
        code: &str
    ) -> Result<Vec<u8>, AssemblerError> {
        // Build the final basic code by replacing variables by value
        // Hexadecimal is used to ensure a consistent 2 bytes representation
        let basic_src = {
            let mut basic = code.to_owned();
            match variables {
                None => {}
                Some(arguments) => {
                    for argument in arguments {
                        let key = format!("{{{}}}", argument);
                        let value = format!(
                            "&{:X}",
                            self.resolve_expr_may_fail_in_first_pass(&Expr::from(
                                argument.borrow()
                            ))?
                        );
                        basic = basic.replace(&key, &value);
                    }
                }
            }
            basic
        };

        // build the basic tokens
        let mut basic = BasicProgram::parse(basic_src)?;
        if hidden_lines.is_some() {
            basic.hide_lines(hidden_lines.unwrap())?;
        }
        Ok(basic.as_bytes())
    }
}

/// When visiting a repetition, we unroll the loop and stream the tokens
/// TODO reimplement it in a similar way that the LocatedToken version that is better
pub fn visit_repeat(rept: &Token, env: &mut Env) -> Result<(), AssemblerError> {
    let tokens = rept.unroll(env).unwrap()?;

    for token in &tokens {
        visit_token(token, env)?;
    }

    Ok(())
}

/// Manage the stable ticker stuff.
/// - Start: register a counter
/// - Stop: store counter count
#[allow(clippy::cast_possible_wrap)]
pub fn visit_stableticker(
    ticker: &StableTickerAction,
    env: &mut Env
) -> Result<(), AssemblerError> {
    match ticker {
        StableTickerAction::Start(ref name) => {
            env.stable_counters.add_counter(name)?;
            Ok(())
        }
        StableTickerAction::Stop => {
            match env.stable_counters.release_last_counter() {
                None => Err(AssemblerError::NoActiveCounter),
                Some((label, count)) => env.add_symbol_to_symbol_table(&label, count)
            }
        }
    }
}

/// Assemble DEFS directive
pub fn assemble_defs_item(
    expr: &Expr,
    fill: Option<&Expr>,
    env: &mut Env
) -> Result<Bytes, AssemblerError> {
    let count = match env.resolve_expr_must_never_fail(expr) {
        Ok(amount) => amount.int()?,
        Err(e) => {
            env.add_error_discardable_one_pass(e)?;
            *env.request_additional_pass.write().unwrap() = true; // we expect to obtain this value later
            0.into()
        }
    };
    let value = if fill.is_none() {
        0
    }
    else {
        let value = env
            .resolve_expr_may_fail_in_first_pass(fill.unwrap())?
            .int()?;
        (value & 0xFF) as u8
    };

    let mut bytes = Bytes::with_capacity(count as usize);
    bytes.resize_with(count as _, || value);

    Ok(bytes)
}

/// Assemble align directive. It can only work if current address is known...
pub fn assemble_align(
    expr: &Expr,
    fill: Option<&Expr>,
    env: &Env
) -> Result<Bytes, AssemblerError> {
    let expression = env.resolve_expr_must_never_fail(expr)?.int()? as u16;
    let current = env.symbols().current_address()?;
    let value = if fill.is_none() {
        0
    }
    else {
        let value = env
            .resolve_expr_may_fail_in_first_pass(fill.unwrap())?
            .int()?;
        (value & 0xFF) as u8
    };

    // compute the number of 0 to put
    let mut until = current;
    while until % expression != 0 {
        until += 1;
    }

    // Create the vector
    let hole = (until - current) as usize;
    let mut bytes = Bytes::with_capacity(hole);
    for _i in 0..hole {
        bytes.push(value);
    }

    // and return it
    Ok(bytes)
}

/// Assemble the opcode and inject in the environement
pub(crate) fn visit_opcode(
    mnemonic: Mnemonic,
    arg1: &Option<DataAccess>,
    arg2: &Option<DataAccess>,
    arg3: &Option<Register8>,
    env: &mut Env
) -> Result<(), AssemblerError> {
    // TODO update $ in the symbol table
    let bytes = assemble_opcode(mnemonic, arg1, arg2, arg3, env)?;
    for b in bytes.iter() {
        env.output(*b)?;
    }

    Ok(())
}

/// Assemble an opcode and returns the generated bytes or the error message if it is impossible to
/// assemblea.
/// We assum the opcode is properlt coded. Panic occurs if it is not the case
pub fn assemble_opcode(
    mnemonic: Mnemonic,
    arg1: &Option<DataAccess>,
    arg2: &Option<DataAccess>,
    arg3: &Option<Register8>,
    env: &mut Env
) -> Result<Bytes, AssemblerError> {
    match mnemonic {
        Mnemonic::And | Mnemonic::Or | Mnemonic::Xor => {
            assemble_logical_operator(mnemonic, arg1.as_ref().unwrap(), env)
        }
        Mnemonic::Add | Mnemonic::Adc => {
            assemble_add_or_adc(
                mnemonic,
                arg1.as_ref().unwrap(),
                arg2.as_ref().unwrap(),
                env
            )
        }
        Mnemonic::Cp => env.assemble_cp(arg1.as_ref().unwrap()),
        Mnemonic::ExMemSp => assemble_ex_memsp(arg1.as_ref().unwrap()),
        Mnemonic::Dec | Mnemonic::Inc => assemble_inc_dec(mnemonic, arg1.as_ref().unwrap(), env),
        Mnemonic::Djnz => assemble_djnz(arg1.as_ref().unwrap(), env),
        Mnemonic::In => assemble_in(arg1.as_ref().unwrap(), &arg2.as_ref().unwrap(), env),
        Mnemonic::Ld => assemble_ld(arg1.as_ref().unwrap(), &arg2.as_ref().unwrap(), env),
        Mnemonic::Ldi
        | Mnemonic::Ldd
        | Mnemonic::Ldir
        | Mnemonic::Lddr
        | Mnemonic::Outi
        | Mnemonic::Outd
        | Mnemonic::Ei
        | Mnemonic::Di
        | Mnemonic::ExAf
        | Mnemonic::ExHlDe
        | Mnemonic::Exx
        | Mnemonic::Halt
        | Mnemonic::Ind
        | Mnemonic::Indr
        | Mnemonic::Ini
        | Mnemonic::Inir
        | Mnemonic::Rla
        | Mnemonic::Rlca
        | Mnemonic::Rrca
        | Mnemonic::Rra
        | Mnemonic::Reti
        | Mnemonic::Retn
        | Mnemonic::Scf
        | Mnemonic::Ccf
        | Mnemonic::Cpd
        | Mnemonic::Cpdr
        | Mnemonic::Cpi
        | Mnemonic::Cpir
        | Mnemonic::Cpl
        | Mnemonic::Daa
        | Mnemonic::Neg
        | Mnemonic::Otdr
        | Mnemonic::Otir
        | Mnemonic::Rld
        | Mnemonic::Rrd => assemble_no_arg(mnemonic),
        Mnemonic::Out => assemble_out(arg1.as_ref().unwrap(), &arg2.as_ref().unwrap(), env),
        Mnemonic::Jr | Mnemonic::Jp | Mnemonic::Call => {
            assemble_call_jr_or_jp(mnemonic, arg1.as_ref(), arg2.as_ref().unwrap(), env)
        }
        Mnemonic::Pop => assemble_pop(arg1.as_ref().unwrap()),
        Mnemonic::Push => assemble_push(arg1.as_ref().unwrap()),
        Mnemonic::Bit | Mnemonic::Res | Mnemonic::Set => {
            assemble_bit_res_or_set(
                mnemonic,
                arg1.as_ref().unwrap(),
                arg2.as_ref().unwrap(),
                arg3.as_ref(),
                env
            )
        }
        Mnemonic::Ret => assemble_ret(arg1),
        Mnemonic::Rst => assemble_rst(arg1.as_ref().unwrap(), env),
        Mnemonic::Im => assemble_im(arg1.as_ref().unwrap(), env),
        Mnemonic::Nop => env.assemble_nop(Mnemonic::Nop, arg1.as_ref().map(|v| v.expr().unwrap())),
        Mnemonic::Nop2 => env.assemble_nop(Mnemonic::Nop2, None),

        Mnemonic::Sub => env.assemble_sub(arg1.as_ref().unwrap()),
        Mnemonic::Sbc => env.assemble_sbc(arg1.as_ref().unwrap(), arg2.as_ref().unwrap()),
        Mnemonic::Sla
        | Mnemonic::Sra
        | Mnemonic::Srl
        | Mnemonic::Sl1
        | Mnemonic::Rl
        | Mnemonic::Rr
        | Mnemonic::Rlc
        | Mnemonic::Rrc => env.assemble_shift(mnemonic, arg1.as_ref().unwrap(), arg2.as_ref())
    }
}

fn visit_org(address: &Expr, address2: Option<&Expr>, env: &mut Env) -> Result<(), AssemblerError> {
    // org $ set org to the output address (cf. rasm)
    let code_adr = if address2.is_none() && address == &"$".into() {
        if env.start_address().is_none() {
            return Err(AssemblerError::InvalidArgument {
                msg: "ORG: $ cannot be used now".into()
            });
        }
        env.logical_output_address() as i32
    }
    else {
        env.resolve_expr_must_never_fail(address)?.int()?
    };

    let output_adr = if address2.is_some() {
        env.resolve_expr_must_never_fail(address2.unwrap())?.int()?
    }
    else {
        code_adr.clone()
    };

    // TODO Check overlapping region
    let page_info = env.active_page_info_mut();
    page_info.logical_outputadr = output_adr as _;
    page_info.logical_codeadr = code_adr as _;
    page_info.fail_next_write_if_zero = false;

    // Specify start address at first use
    env.active_page_info_mut().startadr = match env.start_address() {
        Some(val) => val.min(env.logical_output_address()),
        None => env.logical_output_address()
    }
    .into();

    env.output_address = output_adr as _;

    assert_eq!(env.logical_output_address(), env.output_address);
    env.update_dollar();
    Ok(())
}

fn assemble_no_arg(mnemonic: Mnemonic) -> Result<Bytes, AssemblerError> {
    let bytes: &[u8] = match mnemonic {
        Mnemonic::Ldi => &[0xED, 0xA0],
        Mnemonic::Ldd => &[0xED, 0xA8],
        Mnemonic::Lddr => &[0xED, 0xB8],
        Mnemonic::Ldir => &[0xED, 0xB0],
        Mnemonic::Di => &[0xF3],
        Mnemonic::ExAf => &[0x08],
        Mnemonic::ExHlDe => &[0xEB],
        Mnemonic::Exx => &[0xD9],
        Mnemonic::Ei => &[0xFB],
        Mnemonic::Halt => &[0x76],
        Mnemonic::Ind => &[0xED, 0xAA],
        Mnemonic::Indr => &[0xED, 0xBA],
        Mnemonic::Ini => &[0xED, 0xA2],
        Mnemonic::Inir => &[0xED, 0xB2],
        Mnemonic::Outd => &[0xED, 0xAB],
        Mnemonic::Outi => &[0xED, 0xA3],
        Mnemonic::Rla => &[0x17],
        Mnemonic::Rlca => &[0x07],
        Mnemonic::Rrca => &[0x0F],
        Mnemonic::Rra => &[0x1F],
        Mnemonic::Reti => &[0xED, 0x4D],
        Mnemonic::Retn => &[0xED, 0x45],
        Mnemonic::Scf => &[0x37],
        Mnemonic::Ccf => &[0x3F],
        // added
        Mnemonic::Cpd => &[0xED, 0xA9],
        Mnemonic::Cpdr => &[0xED, 0xB9],
        Mnemonic::Cpi => &[0xED, 0xA1],
        Mnemonic::Cpir => &[0xED, 0xB1],
        Mnemonic::Cpl => &[0x2F],
        Mnemonic::Daa => &[0x27],
        Mnemonic::Neg => &[0xED, 0x44],
        Mnemonic::Otdr => &[0xED, 0xBB],
        Mnemonic::Otir => &[0xED, 0xB3],
        Mnemonic::Rld => &[0xED, 0x6F],
        Mnemonic::Rrd => &[0xED, 0x67],
        _ => {
            return Err(AssemblerError::BugInAssembler {
                msg: format!("{} not treated", mnemonic)
            });
        }
    };

    Ok(Bytes::from_slice(bytes))
}

fn assemble_inc_dec(mne: Mnemonic, arg1: &DataAccess, env: &Env) -> Result<Bytes, AssemblerError> {
    let mut bytes = Bytes::new();

    let is_inc = match mne {
        Mnemonic::Inc => true,
        Mnemonic::Dec => false,
        _ => panic!("Impossible case")
    };

    match arg1 {
        DataAccess::Register16(ref reg) => {
            let base = if is_inc { 0b0000_0011 } else { 0b0000_1011 };
            let byte = base | (register16_to_code_with_sp(*reg) << 4);
            bytes.push(byte);
        }

        DataAccess::IndexRegister16(ref reg) => {
            bytes.push(indexed_register16_to_code(*reg));
            bytes.push(if is_inc { 0x23 } else { 0x2B });
        }

        DataAccess::Register8(ref reg) => {
            bytes.push(
                if is_inc { 0b0000_0100 } else { 0b0000_0101 } | (register8_to_code(*reg) << 3)
            );
        }

        DataAccess::IndexRegister8(ref reg) => {
            bytes.push(indexed_register16_to_code(reg.complete()));
            bytes.push(
                if is_inc { 0b0000_0100 } else { 0b0000_0101 }
                    | (indexregister8_to_code(*reg) << 3)
            );
        }

        DataAccess::MemoryRegister16(Register16::Hl) => {
            bytes.push(if is_inc { 0x34 } else { 0x35 });
        }

        DataAccess::IndexRegister16WithIndex(ref reg, ref exp) => {
            let val = (env.resolve_expr_may_fail_in_first_pass(exp)?.int()? & 0xFF) as u8;

            bytes.push(indexed_register16_to_code(*reg));
            bytes.push(if is_inc { 0x34 } else { 0x35 });
            bytes.push(val);
        }
        _ => {
            return Err(AssemblerError::BugInAssembler {
                msg: format!(
                    "{}: not implemented for {:?}",
                    mne.to_string().to_owned(),
                    arg1
                )
            });
        }
    }

    Ok(bytes)
}

/// Converts an absolute address to a relative one (relative to $)
pub fn absolute_to_relative<T: AsRef<SymbolsTable>>(
    address: i32,
    opcode_delta: i32,
    sym: T
) -> Result<u8, AssemblerError> {
    match sym.as_ref().current_address() {
        Err(_msg) => Err(AssemblerError::UnknownAssemblingAddress),
        Ok(root) => {
            let delta = (address - i32::from(root)) - opcode_delta;
            if delta > 128 || delta < -127 {
                Err(AssemblerError::InvalidArgument {
                    msg: format!(
                        "Address 0x{:x} relative to 0x{:x} is too far {}",
                        address, root, delta
                    )
                })
            }
            else {
                let res = (delta & 0xFF) as u8;
                Ok(res)
            }
        }
    }
}

fn assemble_ret(arg1: &Option<DataAccess>) -> Result<Bytes, AssemblerError> {
    let mut bytes = Bytes::new();

    if arg1.is_some() {
        if let Some(&DataAccess::FlagTest(ref test)) = arg1.as_ref() {
            let flag = flag_test_to_code(*test);
            bytes.push(0b1100_0000 | (flag << 3));
        }
        else {
            return Err(AssemblerError::InvalidArgument {
                msg: format!("RET: wrong argument for ret")
            });
        }
    }
    else {
        bytes.push(0xC9);
    };

    Ok(bytes)
}

fn assemble_rst(arg1: &DataAccess, env: &Env) -> Result<Bytes, AssemblerError> {
    let mut bytes = Bytes::new();
    let val = env
        .resolve_expr_may_fail_in_first_pass(arg1.get_expression().unwrap())?
        .int()?;

    let p = match val {
        0x00 => 0b000,
        0x08 => 0b001,
        0x10 => 0b010,
        0x18 => 0b011,
        0x20 => 0b100,
        0x28 => 0b101,
        0x30 => 0b110,
        0x38 => 0b111,

        // just for convenience
        10 => 0b010,
        18 => 0b011,
        20 => 0b100,
        28 => 0b101,
        30 => 0b110,
        38 => 0b111,
        _ => {
            return Err(AssemblerError::InvalidArgument {
                msg: format!("RST cannot take {} as argument.", val)
            })
        }
    };

    bytes.push(0b11000111 | p << 3);
    Ok(bytes)
}

fn assemble_im(arg1: &DataAccess, env: &Env) -> Result<Bytes, AssemblerError> {
    let mut bytes = Bytes::new();
    let val = env
        .resolve_expr_may_fail_in_first_pass(arg1.get_expression().unwrap())?
        .int()?;

    let code = match val {
        0x00 => 0x46,
        0x01 => 0x56,
        0x02 => 0x5E,
        _ => {
            return Err(AssemblerError::InvalidArgument {
                msg: format!("IM cannot take {} as argument.", val)
            })
        }
    };

    bytes.push(0xED);
    bytes.push(code);
    Ok(bytes)
}

/// arg1 contains the tests
/// arg2 contains the information
pub fn assemble_call_jr_or_jp(
    mne: Mnemonic,
    arg1: Option<&DataAccess>,
    arg2: &DataAccess,
    env: &mut Env
) -> Result<Bytes, AssemblerError> {
    let mut bytes = Bytes::new();

    let is_jr = match mne {
        Mnemonic::Jr => true,
        Mnemonic::Jp | Mnemonic::Call => false,
        _ => unreachable!()
    };

    let is_call = match mne {
        Mnemonic::Call => true,
        Mnemonic::Jp | Mnemonic::Jr => false,
        _ => unreachable!()
    };

    let is_jp = !(is_call || is_jr);

    // compute the flag code if any
    // TODO raise an error if the flag test for jr is wrong
    let flag_code = if arg1.is_some() {
        match arg1.as_ref() {
            Some(DataAccess::FlagTest(ref test)) => Some(flag_test_to_code(*test)),
            _ => {
                return Err(AssemblerError::InvalidArgument {
                    msg: format!(
                        "{}: wrong flag argument",
                        mne.to_string().to_ascii_uppercase()
                    )
                })
            }
        }
    }
    else {
        None
    };

    // Treat address
    if let DataAccess::Expression(ref e) = arg2 {
        let address = env.resolve_expr_may_fail_in_first_pass(e)?.int()?;
        if is_jr {
            let relative = if e.is_relative() {
                address as u8
            }
            else {
                env.absolute_to_relative_may_fail_in_first_pass(address, 2)? as u8
            };
            if flag_code.is_some() {
                // jr - flag
                add_byte(&mut bytes, 0b0010_0000 | (flag_code.unwrap() << 3));
            }
            else {
                // jr - no flag
                add_byte(&mut bytes, 0b0001_1000);
            }
            add_byte(&mut bytes, relative);
        }
        else if is_call {
            match flag_code {
                Some(flag) => add_byte(&mut bytes, 0b1100_0100 | (flag << 3)),
                None => add_byte(&mut bytes, 0xCD)
            }
            add_word(&mut bytes, address as u16);
        }
        else {
            if flag_code.is_some() {
                // jp - flag
                add_byte(&mut bytes, 0b1100_0010 | (flag_code.unwrap() << 3))
            }
            else {
                // jp - no flag
                add_byte(&mut bytes, 0xC3);
            }
            add_word(&mut bytes, address as u16);
        }

        env.track_used_symbols(e);
    }
    else if let DataAccess::MemoryRegister16(Register16::Hl) = arg2 {
        assert!(is_jp);
        add_byte(&mut bytes, 0xE9);
    }
    else if let DataAccess::MemoryIndexRegister16(ref reg) = arg2 {
        assert!(is_jp);
        add_byte(&mut bytes, indexed_register16_to_code(*reg));
        add_byte(&mut bytes, 0xE9);
    }
    else {
        return Err(AssemblerError::BugInAssembler {
            msg: format!("{}: parameter {:?} not treated", mne, arg2)
        });
    }

    Ok(bytes)
}

fn assemble_djnz(arg1: &DataAccess, env: &Env) -> Result<Bytes, AssemblerError> {
    if let DataAccess::Expression(ref expr) = arg1 {
        let mut bytes = Bytes::new();
        let address = env.resolve_expr_may_fail_in_first_pass(expr)?.int()?;
        let relative = if expr.is_relative() {
            address as u8
        }
        else {
            env.absolute_to_relative_may_fail_in_first_pass(address, 1 + 1)? as u8
        };
        bytes.push(0x10);
        bytes.push(relative);

        Ok(bytes)
    }
    else {
        unreachable!()
    }
}

#[allow(missing_docs)]
impl Env {
    pub fn assemble_cp(&mut self, arg: &DataAccess) -> Result<Bytes, AssemblerError> {
        let mut bytes = Bytes::new();

        match arg {
            DataAccess::Register8(ref reg) => {
                add_byte(&mut bytes, 0b1011_1000 + register8_to_code(*reg));
            }

            DataAccess::IndexRegister8(ref reg) => {
                add_byte(&mut bytes, indexed_register16_to_code(reg.complete()));
                add_byte(&mut bytes, 0b1011_1000 + indexregister8_to_code(*reg));
            }
            DataAccess::Expression(ref exp) => {
                add_byte(&mut bytes, 0xFE);
                add_byte(
                    &mut bytes,
                    self.resolve_expr_may_fail_in_first_pass(exp)?.int()? as _
                );
            }

            DataAccess::MemoryRegister16(Register16::Hl) => {
                add_byte(&mut bytes, 0xBE);
            }

            DataAccess::IndexRegister16WithIndex(ref reg, ref idx) => {
                add_byte(&mut bytes, indexed_register16_to_code(*reg));
                add_byte(&mut bytes, 0xBE);
                add_byte(
                    &mut bytes,
                    self.resolve_expr_may_fail_in_first_pass(idx)?.int()? as _
                );
            }

            _ => unreachable!()
        }

        Ok(bytes)
    }

    pub fn assemble_sub(&mut self, arg: &DataAccess) -> Result<Bytes, AssemblerError> {
        let mut bytes = Bytes::new();

        match arg {
            DataAccess::Expression(ref exp) => {
                let val = (self.resolve_expr_may_fail_in_first_pass(exp)?.int()? & 0xFF) as u8;
                bytes.push(0xD6);
                bytes.push(val);
            }

            DataAccess::Register8(ref reg) => {
                bytes.push(0b10010000 + (register8_to_code(*reg)));
            }

            DataAccess::IndexRegister8(ref reg) => {
                bytes.push(indexed_register16_to_code(reg.complete()));
                bytes.push(0b10010000 + (indexregister8_to_code(*reg)));
            }

            DataAccess::MemoryRegister16(Register16::Hl) => {
                bytes.push(0x96);
            }

            DataAccess::IndexRegister16WithIndex(ref reg, ref exp) => {
                let val = (self.resolve_expr_may_fail_in_first_pass(exp)?.int()? & 0xFF) as u8;

                bytes.push(indexed_register16_to_code(*reg));
                bytes.push(0x96);
                bytes.push(val);
            }
            _ => {
                unreachable!();
            }
        }

        Ok(bytes)
    }

    pub fn assemble_sbc(
        &mut self,
        arg1: &DataAccess,
        arg2: &DataAccess
    ) -> Result<Bytes, AssemblerError> {
        let mut bytes = Bytes::new();

        if arg1.is_register_a() {
            match arg2 {
                DataAccess::Register8(ref reg) => {
                    bytes.push(0b10011000 + register8_to_code(*reg));
                }

                DataAccess::IndexRegister8(ref reg) => {
                    bytes.push(indexed_register16_to_code(reg.complete()));
                    bytes.push(0b10011000 + indexregister8_to_code(*reg));
                }

                DataAccess::Expression(ref exp) => {
                    let val = self.resolve_expr_may_fail_in_first_pass(exp)?.int()? as u8;
                    bytes.push(0xDE);
                    bytes.push(val);
                }

                DataAccess::MemoryRegister16(Register16::Hl) => {
                    bytes.push(0x9E);
                }

                DataAccess::IndexRegister16WithIndex(ref reg, ref exp) => {
                    bytes.push(indexed_register16_to_code(*reg));
                    bytes.push(0x9E);
                    let val = self.resolve_expr_may_fail_in_first_pass(exp)?.int()? as u8;
                    bytes.push(val);
                }

                _ => unreachable!()
            }
        }
        else {
            assert!(arg1.is_register_hl());

            match arg2 {
                DataAccess::Register16(ref reg) => {
                    bytes.push(0xED);
                    bytes.push(0b0100_0010 | register16_to_code_with_sp(*reg) << 4);
                }
                _ => unreachable!()
            }
        }

        Ok(bytes)
    }

    pub fn assemble_shift(
        &mut self,
        mne: Mnemonic,
        target: &DataAccess,
        hidden: Option<&DataAccess>
    ) -> Result<Bytes, AssemblerError> {
        let mut bytes = Bytes::new();

        if let DataAccess::Register8(ref reg) = target {
            add_byte(&mut bytes, 0xCB);
            let byte = if mne.is_sla() {
                0b0010_0000
            }
            else if mne.is_sra() {
                0b0010_1000
            }
            else if mne.is_srl() {
                0b0011_1000
            }
            else if mne.is_rlc() {
                0b0000_0000
            }
            else if mne.is_rrc() {
                0b0000_1000
            }
            else if mne.is_rl() {
                0b0001_0000
            }
            else if mne.is_rr() {
                0b0001_1000
            }
            else if mne.is_sl1() {
                0b0011_0000
            }
            else {
                unreachable!()
            } + register8_to_code(*reg);
            add_byte(&mut bytes, byte);
        }
        else {
            assert!(match target {
                DataAccess::MemoryRegister16(Register16::Hl) => true,
                DataAccess::IndexRegister16WithIndex(..) => true,
                _ => false
            });

            // add prefix for ix/iy
            match target {
                DataAccess::IndexRegister16WithIndex(ref reg, ref exp) => {
                    let val = self.resolve_expr_may_fail_in_first_pass(exp)?.int()? as u8;
                    bytes.push(indexed_register16_to_code(*reg));
                    add_byte(&mut bytes, 0xCB);
                    bytes.push(val);
                }

                DataAccess::MemoryRegister16(Register16::Hl) => {
                    add_byte(&mut bytes, 0xCB);
                }

                _ => {
                    return Err(AssemblerError::InvalidArgument {
                        msg: format!("{} cannot take {} as argument", mne, target)
                    })
                }
            };

            // some hidden opcode modify this byte
            let mut byte: u8 = if mne.is_sla() {
                0x26
            }
            else if mne.is_sra() {
                0x2E
            }
            else if mne.is_srl() {
                0x3E
            }
            else if mne.is_rlc() {
                0x06
            }
            else if mne.is_rrc() {
                0x0E
            }
            else if mne.is_rl() {
                0x16
            }
            else if mne.is_rr() {
                0x1E
            }
            else if mne.is_sl1() {
                0x36
            }
            else {
                unreachable!()
            };

            if hidden.is_some() {
                let delta: i8 = match hidden.unwrap().get_register8().unwrap() {
                    Register8::A => 1,
                    Register8::L => -1,
                    Register8::H => -2,
                    Register8::E => -3,
                    Register8::D => -4,
                    Register8::C => -5,
                    Register8::B => -6
                };
                if delta < 0 {
                    byte -= delta.abs() as u8;
                }
                else {
                    byte += delta as u8;
                }
            }
            bytes.push(byte);
        }

        Ok(bytes)
    }
}

fn assemble_ld(arg1: &DataAccess, arg2: &DataAccess, env: &Env) -> Result<Bytes, AssemblerError> {
    let mut bytes = Bytes::new();

    // Destination is 8bits register
    if let DataAccess::Register8(ref dst) = arg1 {
        let dst = register8_to_code(*dst);
        match arg2 {
            DataAccess::Register8(ref src) => {
                // R. Zaks p 297
                let src = register8_to_code(*src);

                let code = 0b0100_0000 + (dst << 3) + src;
                bytes.push(code);
            }

            DataAccess::IndexRegister8(ref src) => {
                bytes.push(indexed_register16_to_code(src.complete()));
                let src = indexregister8_to_code(*src);
                let code = 0b0100_0000 + (dst << 3) + src;
                bytes.push(code);
            }

            DataAccess::Expression(ref exp) => {
                let val = (env.resolve_expr_may_fail_in_first_pass(exp)?.int()? & 0xFF) as u8;

                bytes.push(0b0000_0110 | (dst << 3));
                bytes.push(val);
            }

            DataAccess::IndexRegister16WithIndex(ref reg, ref exp) => {
                let val = env.resolve_expr_may_fail_in_first_pass(exp)?.int()?;

                add_index_register_code(&mut bytes, *reg);
                add_byte(&mut bytes, 0b0100_0110 | (dst << 3));
                add_index(&mut bytes, val)?;
            }

            DataAccess::MemoryRegister16(Register16::Hl) => {
                add_byte(&mut bytes, 0b0100_0110 | (dst << 3));
            }
            DataAccess::MemoryIndexRegister16(reg) => {
                add_index_register_code(&mut bytes, *reg);
                add_byte(&mut bytes, 0b0100_0110 | (dst << 3));
            }

            DataAccess::MemoryRegister16(ref memreg) if arg1.is_register_a() => {
                let byte = match memreg {
                    Register16::Bc => 0x0A,
                    Register16::De => 0x1A,
                    _ => unreachable!()
                };
                add_byte(&mut bytes, byte);
            }

            DataAccess::Memory(ref expr) => {
                // dst is A
                let val = env.resolve_expr_may_fail_in_first_pass(expr)?.int()?;
                add_byte(&mut bytes, 0x3A);
                add_word(&mut bytes, val as _);
            }

            DataAccess::SpecialRegisterI => {
                assert!(arg1.is_register_a());
                bytes.push(0xED);
                bytes.push(0x57);
            }

            DataAccess::SpecialRegisterR => {
                assert!(arg1.is_register_a());
                bytes.push(0xED);
                bytes.push(0x5F);
            }

            _ => {
                return Err(AssemblerError::BugInAssembler {
                    msg: format!("LD: not properly implemented for '{:?}, {:?}'", arg1, arg2)
                });
            }
        }
    }
    // Destination is 16 bits register
    else if let DataAccess::Register16(ref dst) = arg1 {
        let dst_code = register16_to_code_with_sp(*dst);

        match arg2 {
            DataAccess::Expression(ref exp) => {
                let val = (env.resolve_expr_may_fail_in_first_pass(exp)?.int()? & 0xFFFF) as u16;

                add_byte(&mut bytes, 0b0000_0001 | (dst_code << 4));
                add_word(&mut bytes, val);
            }

            DataAccess::Register16(Register16::Hl) if dst.is_sp() => {
                add_byte(&mut bytes, 0xF9);
            }

            DataAccess::IndexRegister16(ref reg) if dst.is_sp() => {
                add_byte(&mut bytes, indexed_register16_to_code(*reg));
                add_byte(&mut bytes, 0xF9);
            }

            // Fake instruction splitted in 2 bits operations
            DataAccess::Register16(ref src) => {
                println!("{:?}, {:?}", dst.split(), src.split());
                let bytes_high = assemble_ld(
                    &DataAccess::Register8(dst.high().unwrap()),
                    &DataAccess::Register8(src.high().unwrap()),
                    env
                )
                .unwrap();
                let bytes_low = assemble_ld(
                    &DataAccess::Register8(dst.low().unwrap()),
                    &DataAccess::Register8(src.low().unwrap()),
                    env
                )
                .unwrap();

                bytes.extend_from_slice(&bytes_low);
                bytes.extend_from_slice(&bytes_high);
            }

            DataAccess::Memory(ref expr) => {
                let val = (env.resolve_expr_may_fail_in_first_pass(expr)?.int()? & 0xFFFF) as u16;

                if let Register16::Hl = dst {
                    add_byte(&mut bytes, 0x2A);
                    add_word(&mut bytes, val);
                }
                else {
                    add_byte(&mut bytes, 0xED);
                    add_byte(
                        &mut bytes,
                        (register16_to_code_with_sp(*dst) << 4) + 0b0100_1011
                    );
                    add_word(&mut bytes, val);
                }
            }

            _ => {}
        }
    }
    else if let DataAccess::IndexRegister8(ref dst) = arg1 {
        add_byte(&mut bytes, indexed_register16_to_code(dst.complete()));
        match arg2 {
            DataAccess::Expression(ref exp) => {
                let val = (env.resolve_expr_may_fail_in_first_pass(exp)?.int()? & 0xFF) as u8;
                bytes.push(0b0000_0110 | (indexregister8_to_code(*dst) << 3));
                bytes.push(val);
            }

            DataAccess::Register8(ref src) => {
                let code = register8_to_code(*src);

                let code = if dst.is_high() {
                    0b0110_0000 + code
                }
                else {
                    0x68 + code
                };
                bytes.push(code);
            }

            DataAccess::IndexRegister8(ref src) => {
                assert_eq!(dst.complete(), src.complete());

                let byte = match (dst.is_low(), src.is_low()) {
                    (false, false) => 0x64,
                    (false, true) => 0x65,
                    (true, false) => 0x6C,
                    (true, true) => 0x6D
                };
                bytes.push(byte)
            }

            _ => unreachable!()
        }
    }
    // Distinatin is 16 bits indexed register
    else if let DataAccess::IndexRegister16(ref dst) = arg1 {
        let code = indexed_register16_to_code(*dst);

        match arg2 {
            DataAccess::Expression(ref exp) => {
                let val = (env.resolve_expr_may_fail_in_first_pass(exp)?.int()? & 0xFFFF) as u16;

                add_byte(&mut bytes, code);
                add_byte(&mut bytes, 0x21);
                add_word(&mut bytes, val);
            }

            DataAccess::Memory(ref exp) => {
                let val = (env.resolve_expr_may_fail_in_first_pass(exp)?.int()? & 0xFFFF) as u16;

                add_byte(&mut bytes, code);
                add_byte(&mut bytes, 0x2A);
                add_word(&mut bytes, val);
            }
            _ => {}
        }
    }
    // Destination is memory indexed by register
    else if let DataAccess::MemoryRegister16(ref dst) = arg1 {
        // Want to store in memory pointed by register
        match dst {
            Register16::Hl => {
                if let DataAccess::Register8(ref src) = arg2 {
                    let src = register8_to_code(*src);
                    let code = 0b0111_0000 | src;
                    bytes.push(code);
                }
                else if let DataAccess::Expression(ref exp) = arg2 {
                    let val = (env.resolve_expr_may_fail_in_first_pass(exp)?.int()? & 0xFF) as u8;
                    bytes.push(0x36);
                    bytes.push(val);
                }
            }

            Register16::De if DataAccess::Register8(Register8::A) == *arg2 => {
                bytes.push(0b0001_0010);
            }

            Register16::Bc if DataAccess::Register8(Register8::A) == *arg2 => {
                bytes.push(0b0000_0010);
            }

            _ => {}
        }
    }
    else if let DataAccess::MemoryIndexRegister16(ref dst) = arg1 {
        add_index_register_code(&mut bytes, *dst);
        add_byte(&mut bytes, indexed_register16_to_code(*dst));

        if let DataAccess::Register8(ref src) = arg2 {
            let src = register8_to_code(*src);
            let code = 0b0111_0000 | src;
            bytes.push(code);
        }
        else if let DataAccess::Expression(ref exp) = arg2 {
            let val = (env.resolve_expr_may_fail_in_first_pass(exp)?.int()? & 0xFF) as u8;
            bytes.push(0x36);
            bytes.push(val);
        }
    }
    // Destination is memory form ix/iy + n
    else if let DataAccess::IndexRegister16WithIndex(ref reg, ref exp) = arg1 {
        add_byte(&mut bytes, indexed_register16_to_code(*reg));
        let delta = (env.resolve_expr_may_fail_in_first_pass(exp)?.int()? & 0xFF) as u8;

        match arg2 {
            DataAccess::Expression(ref exp) => {
                let value = (env.resolve_expr_may_fail_in_first_pass(exp)?.int()? & 0xFF) as u8;
                add_byte(&mut bytes, 0x36);
                add_byte(&mut bytes, delta);
                add_byte(&mut bytes, value);
            }

            DataAccess::Register8(ref src) => {
                add_byte(&mut bytes, 0x70 + register8_to_code(*src));
                add_byte(&mut bytes, delta);
            }
            _ => {
                // possible fake instruction
                bytes.clear();
            }
        }
    }
    // Destination is memory
    else if let DataAccess::Memory(ref exp) = arg1 {
        let address = env.resolve_expr_may_fail_in_first_pass(exp)?.int()?;

        match arg2 {
            DataAccess::IndexRegister16(IndexRegister16::Ix) => {
                bytes.push(0xDD);
                bytes.push(0b0010_0010);
                add_word(&mut bytes, address as _);
            }
            DataAccess::IndexRegister16(IndexRegister16::Iy) => {
                bytes.push(0xFD);
                bytes.push(0b0010_0010);
                add_word(&mut bytes, address as _);
            }
            DataAccess::Register16(Register16::Hl) => {
                bytes.push(0b0010_0010);
                add_word(&mut bytes, address as _);
            }
            DataAccess::Register16(ref reg) => {
                bytes.push(0xED);
                bytes.push(0b0100_0011 | (register16_to_code_with_sp(*reg) << 4));
                add_word(&mut bytes, address as _);
            }
            DataAccess::Register8(Register8::A) => {
                bytes.push(0x32);
                add_word(&mut bytes, address as _);
            }

            _ => {}
        }
    }
    else if let DataAccess::SpecialRegisterI = arg1 {
        if let DataAccess::Register8(Register8::A) = arg2 {
            bytes.push(0xED);
            bytes.push(0x47)
        }
        else {
            unreachable!();
        }
    }
    else if let DataAccess::SpecialRegisterR = arg1 {
        if let DataAccess::Register8(Register8::A) = arg2 {
            bytes.push(0xED);
            bytes.push(0x4F)
        }
        else {
            unreachable!();
        }
    }

    // handle fake instructions
    if bytes.is_empty() {
        match (arg1, arg2) {
            (DataAccess::Register16(dst), DataAccess::Register16(src)) => {
                bytes.extend(
                    assemble_ld(
                        &DataAccess::Register8(dst.low().unwrap()),
                        &DataAccess::Register8(src.low().unwrap()),
                        env
                    )?
                    .iter()
                    .cloned()
                );
                bytes.extend(
                    assemble_ld(
                        &DataAccess::Register8(dst.high().unwrap()),
                        &DataAccess::Register8(src.high().unwrap()),
                        env
                    )?
                    .iter()
                    .cloned()
                );
            }

            (DataAccess::Register16(Register16::Hl), DataAccess::IndexRegister16(_))
            | (DataAccess::IndexRegister16(_), DataAccess::Register16(Register16::Hl))
            | (DataAccess::IndexRegister16(_), DataAccess::IndexRegister16(_)) => {
                bytes.extend(assemble_push(arg2)?);
                bytes.extend(assemble_pop(arg1)?);
            }

            // general registers from indexed
            (DataAccess::Register16(dst), DataAccess::IndexRegister16(src)) => {
                bytes.extend(
                    assemble_ld(
                        &DataAccess::Register8(dst.low().unwrap()),
                        &DataAccess::IndexRegister8(src.low()),
                        env
                    )?
                    .iter()
                    .cloned()
                );
                bytes.extend(
                    assemble_ld(
                        &DataAccess::Register8(dst.high().unwrap()),
                        &DataAccess::IndexRegister8(src.high()),
                        env
                    )?
                    .iter()
                    .cloned()
                );
            }
            // general > indexed
            (DataAccess::IndexRegister16(dst), DataAccess::Register16(src)) => {
                bytes.extend(
                    assemble_ld(
                        &DataAccess::IndexRegister8(dst.low()),
                        &DataAccess::Register8(src.low().unwrap()),
                        env
                    )?
                    .iter()
                    .cloned()
                );
                bytes.extend(
                    assemble_ld(
                        &DataAccess::IndexRegister8(dst.high()),
                        &DataAccess::Register8(src.high().unwrap()),
                        env
                    )?
                    .iter()
                    .cloned()
                );
            }

            (DataAccess::Register16(dst), DataAccess::IndexRegister16WithIndex(src, index)) => {
                bytes.extend(
                    assemble_ld(
                        &DataAccess::Register8(dst.low().unwrap()),
                        &DataAccess::IndexRegister16WithIndex(src.clone(), index.clone()),
                        env
                    )?
                    .iter()
                    .cloned()
                );
                bytes.extend(
                    assemble_ld(
                        &DataAccess::Register8(dst.high().unwrap()),
                        &DataAccess::IndexRegister16WithIndex(src.clone(), index.add(1)),
                        env
                    )?
                    .iter()
                    .cloned()
                );
            }
            (DataAccess::IndexRegister16WithIndex(dst, index), DataAccess::Register16(src)) => {
                bytes.extend(
                    assemble_ld(
                        &DataAccess::IndexRegister16WithIndex(dst.clone(), index.clone()),
                        &DataAccess::Register8(src.low().unwrap()),
                        env
                    )?
                    .iter()
                    .cloned()
                );
                bytes.extend(
                    assemble_ld(
                        &DataAccess::IndexRegister16WithIndex(dst.clone(), index.add(1)),
                        &DataAccess::Register8(src.high().unwrap()),
                        env
                    )?
                    .iter()
                    .cloned()
                );
            }

            (DataAccess::Register16(dst), DataAccess::MemoryRegister16(Register16::Hl)) => {
                bytes.extend(
                    assemble_ld(
                        &DataAccess::Register8(dst.low().unwrap()),
                        &DataAccess::MemoryRegister16(Register16::Hl),
                        env
                    )?
                    .iter()
                    .cloned()
                );
                bytes.extend(assemble_inc_dec(
                    Mnemonic::Inc,
                    &DataAccess::Register16(Register16::Hl),
                    env
                )?);
                bytes.extend(
                    assemble_ld(
                        &DataAccess::Register8(dst.high().unwrap()),
                        &DataAccess::MemoryRegister16(Register16::Hl),
                        env
                    )?
                    .iter()
                    .cloned()
                );
                bytes.extend(assemble_inc_dec(
                    Mnemonic::Dec,
                    &DataAccess::Register16(Register16::Hl),
                    env
                )?);
            }
            (DataAccess::MemoryRegister16(Register16::Hl), DataAccess::Register16(src)) => {
                bytes.extend(
                    assemble_ld(
                        &DataAccess::MemoryRegister16(Register16::Hl),
                        &DataAccess::Register8(src.low().unwrap()),
                        env
                    )?
                    .iter()
                    .cloned()
                );
                bytes.extend(assemble_inc_dec(
                    Mnemonic::Inc,
                    &DataAccess::Register16(Register16::Hl),
                    env
                )?);
                bytes.extend(
                    assemble_ld(
                        &DataAccess::MemoryRegister16(Register16::Hl),
                        &DataAccess::Register8(src.high().unwrap()),
                        env
                    )?
                    .iter()
                    .cloned()
                );
                bytes.extend(assemble_inc_dec(
                    Mnemonic::Dec,
                    &DataAccess::Register16(Register16::Hl),
                    env
                )?);
            }

            _ => {}
        }
    }

    if bytes.is_empty() {
        Err(AssemblerError::BugInAssembler {
            msg: format!("LD: not properly implemented for '{:?}, {:?}'", arg1, arg2)
        })
    }
    else {
        Ok(bytes)
    }
}

fn assemble_in(arg1: &DataAccess, arg2: &DataAccess, env: &Env) -> Result<Bytes, AssemblerError> {
    let mut bytes = Bytes::new();

    if *arg1 == DataAccess::Expression(0.into()) {
        assert_eq!(arg2, &DataAccess::PortC);
        bytes.push(0xED);
        bytes.push(0x70);
    }
    else {
        match arg2 {
            DataAccess::PortC => {
                match arg1 {
                    DataAccess::Register8(ref reg) => {
                        bytes.push(0xED);
                        bytes.push(0b0100_0000 | (register8_to_code(*reg) << 3))
                    }
                    _ => panic!()
                }
            }

            DataAccess::PortN(ref exp) => {
                if let DataAccess::Register8(Register8::A) = arg1 {
                    let val = (env.resolve_expr_may_fail_in_first_pass(exp)?.int()? & 0xFF) as u8;
                    bytes.push(0xDB);
                    bytes.push(val);
                }
            }

            _ => panic!("{:?}", arg2)
        };
    }

    if bytes.is_empty() {
        Err(AssemblerError::BugInAssembler {
            msg: format!("IN: not properly implemented for '{:?}, {:?}'", arg1, arg2)
        })
    }
    else {
        Ok(bytes)
    }
}

fn assemble_out(arg1: &DataAccess, arg2: &DataAccess, env: &Env) -> Result<Bytes, AssemblerError> {
    let mut bytes = Bytes::new();

    if *arg2 == DataAccess::Expression(0.into()) {
        assert_eq!(arg1, &DataAccess::PortC);
        bytes.push(0xED);
        bytes.push(0x71);
    }
    else {
        match arg1 {
            DataAccess::PortC => {
                if let DataAccess::Register8(ref reg) = arg2 {
                    bytes.push(0xED);
                    bytes.push(0b0100_0001 | (register8_to_code(*reg) << 3))
                }

                if let DataAccess::Expression(Expr::Value(0)) = arg2 {
                    bytes.push(0xED);
                    bytes.push(0x71);
                }
            }

            DataAccess::PortN(ref exp) => {
                if let DataAccess::Register8(Register8::A) = arg2 {
                    let val = (env.resolve_expr_may_fail_in_first_pass(exp)?.int()? & 0xFF) as u8;
                    bytes.push(0xD3);
                    bytes.push(val);
                }
            }
            _ => {}
        };
    }

    if bytes.is_empty() {
        Err(AssemblerError::BugInAssembler {
            msg: format!("OUT: not properly implemented for '{:?}, {:?}'", arg1, arg2)
        })
    }
    else {
        Ok(bytes)
    }
}

fn assemble_pop(arg1: &DataAccess) -> Result<Bytes, AssemblerError> {
    let mut bytes = Bytes::new();

    match arg1 {
        DataAccess::Register16(ref reg) => {
            let byte = 0b1100_0001 | (register16_to_code_with_af(*reg) << 4);
            bytes.push(byte);
        }
        DataAccess::IndexRegister16(ref reg) => {
            bytes.push(indexed_register16_to_code(*reg));
            bytes.push(0xE1);
        }
        _ => {
            return Err(AssemblerError::InvalidArgument {
                msg: format!("POP: not implemented for {:?}", arg1)
            });
        }
    }

    Ok(bytes)
}

fn assemble_push(arg1: &DataAccess) -> Result<Bytes, AssemblerError> {
    let mut bytes = Bytes::new();

    match arg1 {
        DataAccess::Register16(ref reg) => {
            let byte = 0b1100_0101 | (register16_to_code_with_af(*reg) << 4);
            bytes.push(byte);
        }
        DataAccess::IndexRegister16(ref reg) => {
            bytes.push(indexed_register16_to_code(*reg));
            bytes.push(0xE5);
        }
        _ => {
            return Err(AssemblerError::InvalidArgument {
                msg: format!("PUSH: not implemented for {:?}", arg1)
            });
        }
    }

    Ok(bytes)
}

fn assemble_logical_operator(
    mnemonic: Mnemonic,
    arg1: &DataAccess,
    env: &Env
) -> Result<Bytes, AssemblerError> {
    let mut bytes = Bytes::new();

    let memory_code = || {
        match mnemonic {
            Mnemonic::And => 0xA6,
            Mnemonic::Or => 0xB6,
            Mnemonic::Xor => 0xAE,
            _ => unreachable!()
        }
    };

    match arg1 {
        DataAccess::Register8(ref reg) => {
            let base = match mnemonic {
                Mnemonic::And => 0b1010_0000,
                Mnemonic::Or => 0b1011_0000,
                Mnemonic::Xor => 0b1010_1000,
                _ => unreachable!()
            };
            bytes.push(base + register8_to_code(*reg));
        }

        DataAccess::IndexRegister8(ref reg) => {
            bytes.push(indexed_register16_to_code(reg.complete()));
            let base = match mnemonic {
                Mnemonic::And => 0b1010_0000,
                Mnemonic::Or => 0b1011_0000,
                Mnemonic::Xor => 0b1010_1000,
                _ => unreachable!()
            };
            bytes.push(base + indexregister8_to_code(*reg));
        }

        DataAccess::Expression(ref exp) => {
            let base = match mnemonic {
                Mnemonic::And => 0xE6,
                Mnemonic::Or => 0xF6,
                Mnemonic::Xor => 0xEE,
                _ => unreachable!()
            };
            let value = env.resolve_expr_may_fail_in_first_pass(exp)?.int()? & 0xFF;
            bytes.push(base);
            bytes.push(value as u8);
        }

        DataAccess::MemoryRegister16(Register16::Hl) => {
            bytes.push(memory_code());
        }

        DataAccess::IndexRegister16WithIndex(ref reg, ref exp) => {
            let value = env.resolve_expr_may_fail_in_first_pass(exp)?.int()? & 0xFF;
            bytes.push(indexed_register16_to_code(*reg));
            bytes.push(memory_code());
            bytes.push(value as u8);
        }
        _ => unreachable!()
    }

    Ok(bytes)
}

fn assemble_ex_memsp(arg1: &DataAccess) -> Result<Bytes, AssemblerError> {
    let mut bytes = Bytes::new();

    if let DataAccess::IndexRegister16(ref reg) = arg1 {
        bytes.push(indexed_register16_to_code(*reg));
    }

    bytes.push(0xE3);
    Ok(bytes)
}

fn assemble_add_or_adc(
    mnemonic: Mnemonic,
    arg1: &DataAccess,
    arg2: &DataAccess,
    env: &Env
) -> Result<Bytes, AssemblerError> {
    let mut bytes = Bytes::new();
    let is_add = match mnemonic {
        Mnemonic::Add => true,
        Mnemonic::Adc => false,
        _ => panic!("Impossible case")
    };

    match arg1 {
        DataAccess::Register8(Register8::A) => {
            match arg2 {
                DataAccess::MemoryRegister16(Register16::Hl) => {
                    if is_add {
                        bytes.push(0b1000_0110);
                    }
                    else {
                        bytes.push(0b1000_1110);
                    }
                }

                DataAccess::IndexRegister16WithIndex(ref reg, ref exp) => {
                    let val = env.resolve_expr_may_fail_in_first_pass(exp)?.int()?;

                    // TODO check if the code is ok
                    bytes.push(indexed_register16_to_code(*reg));
                    if is_add {
                        bytes.push(0b1000_0110);
                    }
                    else {
                        bytes.push(0x8E);
                    }
                    add_index(&mut bytes, val)?;
                }

                DataAccess::Expression(ref exp) => {
                    let val = env.resolve_expr_may_fail_in_first_pass(exp)?.int()? as u8;
                    if is_add {
                        bytes.push(0b1100_0110);
                    }
                    else {
                        bytes.push(0xCE);
                    }
                    bytes.push(val);
                }

                DataAccess::Register8(ref reg) => {
                    let base = if is_add { 0b1000_0000 } else { 0b1000_1000 };
                    bytes.push(base | register8_to_code(*reg));
                }

                DataAccess::IndexRegister8(ref reg) => {
                    bytes.push(indexed_register16_to_code(reg.complete()));
                    let base = if is_add { 0b1000_0000 } else { 0b1000_1000 };
                    bytes.push(base | indexregister8_to_code(*reg));
                }
                _ => {}
            }
        }

        DataAccess::Register16(Register16::Hl) => {
            if let DataAccess::Register16(ref reg) = arg2 {
                let base = if is_add {
                    0b0000_1001
                }
                else {
                    bytes.push(0xED);
                    0b0100_1010
                };

                bytes.push(base | (register16_to_code_with_sp(*reg) << 4));
            }
        }

        DataAccess::IndexRegister16(ref reg1) => {
            match arg2 {
                DataAccess::Register16(ref reg2) => {
                    // TODO Error if reg2 = HL
                    bytes.push(indexed_register16_to_code(*reg1));
                    let base = if is_add {
                        0b0000_1001
                    }
                    else {
                        panic!();
                    };
                    bytes.push(
                        base | (register16_to_code_with_indexed(&DataAccess::Register16(*reg2))
                            << 4)
                    )
                }

                DataAccess::IndexRegister16(ref reg2) => {
                    if reg1 != reg2 {
                        return Err(AssemblerError::InvalidArgument {
                            msg: "Unable to add different indexed registers".to_owned()
                        });
                    }

                    bytes.push(indexed_register16_to_code(*reg1));
                    let base = if is_add {
                        0b0000_1001
                    }
                    else {
                        panic!();
                    };
                    bytes.push(
                        base | (register16_to_code_with_indexed(&DataAccess::IndexRegister16(
                            *reg2
                        )) << 4)
                    )
                }

                _ => {}
            }
        }
        _ => {}
    }

    if bytes.is_empty() {
        Err(AssemblerError::BugInAssembler {
            msg: format!("{:?} not implemented for {:?} {:?}", mnemonic, arg1, arg2)
        })
    }
    else {
        Ok(bytes)
    }
}

fn assemble_bit_res_or_set(
    mnemonic: Mnemonic,
    arg1: &DataAccess,
    arg2: &DataAccess,
    hidden: Option<&Register8>,
    env: &Env
) -> Result<Bytes, AssemblerError> {
    let mut bytes = Bytes::new();

    // Get the bit of interest
    let bit = match arg1 {
        DataAccess::Expression(ref e) => {
            let bit = (env.resolve_expr_may_fail_in_first_pass(e)?.int()? & 0xFF) as u8;
            if bit > 7 {
                return Err(AssemblerError::InvalidArgument {
                    msg: format!("{}: {} is an invalid value", mnemonic.to_string(), bit)
                });
            }
            bit
        }
        _ => unreachable!()
    };

    // Get the code to differentiate the instructions
    // the value can be modified by some hidden instructions
    let code = match mnemonic {
        Mnemonic::Res => 0b1000_0000,
        Mnemonic::Set => 0b1100_0000,
        Mnemonic::Bit => 0b0100_0000,
        _ => unreachable!()
    };

    // Apply it to the right thing
    if let DataAccess::Register8(ref reg) = arg2 {
        //    let mut code = code + 0b0110;

        bytes.push(0xCB);
        bytes.push(code | (bit << 3) | register8_to_code(*reg))
    }
    else {
        assert!(match arg2 {
            DataAccess::MemoryRegister16(Register16::Hl) => true,
            DataAccess::IndexRegister16WithIndex(..) => true,
            _ => false
        });

        let mut code = code + 0b0110;

        if let DataAccess::IndexRegister16WithIndex(ref reg, delta) = arg2 {
            bytes.push(indexed_register16_to_code(*reg));
            add_byte(&mut bytes, 0xCB);
            let delta = (env.resolve_expr_may_fail_in_first_pass(delta)?.int()? & 0xFF) as u8;
            add_byte(&mut bytes, delta);

            // patch the code for hidden opcode
            if hidden.is_some() {
                let fix: i8 = match hidden.unwrap() {
                    Register8::A => 1,
                    Register8::L => -1,
                    Register8::H => -2,
                    Register8::E => -3,
                    Register8::D => -4,
                    Register8::C => -5,
                    Register8::B => -6
                };
                if fix < 0 {
                    code -= fix.abs() as u8;
                }
                else {
                    code += fix as u8;
                }
            }
        }
        else {
            bytes.push(0xCB);
        }

        bytes.push(code | (bit << 3));
    }

    Ok(bytes)
}

fn indexed_register16_to_code(reg: IndexRegister16) -> u8 {
    match reg {
        IndexRegister16::Ix => DD,
        IndexRegister16::Iy => FD
    }
}

/// Return the code that represents a 8bits register.
/// A: 0b111
/// B: 0b000
/// C: 0b001
/// D: 0b010
/// E: 0b011
/// H: 0b100
/// L: 0b101
#[inline]
fn register8_to_code(reg: Register8) -> u8 {
    match reg {
        Register8::A => 0b111,
        Register8::B => 0b000,
        Register8::C => 0b001,
        Register8::D => 0b010,
        Register8::E => 0b011,
        Register8::H => 0b100,
        Register8::L => 0b101
    }
}

#[inline]
fn indexregister8_to_code(reg: IndexRegister8) -> u8 {
    match reg {
        IndexRegister8::Ixh | IndexRegister8::Iyh => register8_to_code(Register8::H),
        IndexRegister8::Ixl | IndexRegister8::Iyl => register8_to_code(Register8::L)
    }
}

/// Return the code that represents a 16 bits register
fn register16_to_code_with_af(reg: Register16) -> u8 {
    match reg {
        Register16::Bc => 0b00,
        Register16::De => 0b01,
        Register16::Hl => 0b10,
        Register16::Af => 0b11,
        _ => panic!("no mapping for {:?}", reg)
    }
}

fn register16_to_code_with_sp(reg: Register16) -> u8 {
    match reg {
        Register16::Bc => 0b00,
        Register16::De => 0b01,
        Register16::Hl => 0b10,
        Register16::Sp => 0b11,
        _ => panic!("no mapping for {:?}", reg)
    }
}

fn register16_to_code_with_indexed(reg: &DataAccess) -> u8 {
    match reg {
        DataAccess::Register16(Register16::Bc) => 0b00,
        DataAccess::Register16(Register16::De) => 0b01,
        DataAccess::IndexRegister16(_) => 0b10,
        DataAccess::Register16(Register16::Sp) => 0b11,
        _ => panic!("no mapping for {:?}", reg)
    }
}

fn flag_test_to_code(flag: FlagTest) -> u8 {
    match flag {
        FlagTest::NZ => 0b000,
        FlagTest::Z => 0b001,
        FlagTest::NC => 0b010,
        FlagTest::C => 0b011,

        // the following flags are not used for jr
        FlagTest::PO => 0b100,
        FlagTest::PE => 0b101,
        FlagTest::P => 0b110,
        FlagTest::M => 0b111
    }
}

#[cfg(test)]
#[allow(deprecated)]
mod test {

    use super::processed_token::build_processed_token;
    use super::*;

    fn visit_token(token: &Token, env: &mut Env) -> Result<(), AssemblerError> {
        let mut processed = build_processed_token(token, env);
        processed.visited(env)
    }

    fn visit_tokens(tokens: &[Token]) -> Result<Env, AssemblerError> {
        let mut env = Env::default();
        for t in tokens {
            visit_token(t, &mut env)?;
        }
        Ok(env)
    }

    #[test]
    pub fn test_inc_b() {
        let mut env = Env::default();
        let res = assemble_inc_dec(
            Mnemonic::Inc,
            &DataAccess::Register8(Register8::B),
            &mut env
        )
        .unwrap();
        assert_eq!(res.len(), 1);
        assert_eq!(res[0], 0x04);
    }

    #[test]
    pub fn test_pop() {
        let res = assemble_pop(&DataAccess::Register16(Register16::Af)).unwrap();
        assert_eq!(res.len(), 1);
        assert_eq!(res[0], 0b1111_0001);
    }

    #[test]
    fn test_jump() {
        let res = assemble_call_jr_or_jp(
            Mnemonic::Jp,
            Some(&DataAccess::FlagTest(FlagTest::Z)),
            &DataAccess::Expression(Expr::Value(0x1234)),
            &mut Env::default()
        )
        .unwrap();
        assert_eq!(res.len(), 3);
        assert_eq!(res[0], 0b1100_1010);
        assert_eq!(res[1], 0x34);
        assert_eq!(res[2], 0x12);
    }

    #[test]
    pub fn test_assert() {
        let mut env = Env::default();
        env.start_new_pass();

        assert!(visit_assert(
            &Expr::BinaryOperation(
                BinaryOperation::Equal,
                Box::new(0i32.into()),
                Box::new(0i32.into())
            ),
            None,
            &mut env,
            None
        )
        .unwrap());
        assert!(!visit_assert(
            &Expr::BinaryOperation(
                BinaryOperation::Equal,
                Box::new(1i32.into()),
                Box::new(0i32.into())
            ),
            None,
            &mut env,
            None
        )
        .unwrap());
    }

    #[test]
    pub fn test_undef() {
        let mut env = Env::default();
        env.start_new_pass();

        env.visit_label("toto").unwrap();
        assert!(env.symbols().contains_symbol("toto").unwrap());
        env.visit_undef("toto").unwrap();
        assert!(!env.symbols().contains_symbol("toto").unwrap());
        assert!(env.visit_undef("toto").is_err());
    }

    #[test]
    pub fn test_inc_dec() {
        let env = Env::default();
        let res =
            assemble_inc_dec(Mnemonic::Inc, &DataAccess::Register16(Register16::De), &env).unwrap();
        assert_eq!(res.len(), 1);
        assert_eq!(res[0], 0x13);

        let res =
            assemble_inc_dec(Mnemonic::Dec, &DataAccess::Register8(Register8::B), &env).unwrap();
        assert_eq!(res.len(), 1);
        assert_eq!(res[0], 0x05);
    }

    #[test]
    pub fn test_res() {
        let env = Env::default();
        let res = assemble_bit_res_or_set(
            Mnemonic::Res,
            &DataAccess::Expression(0.into()),
            &DataAccess::Register8(Register8::B),
            None,
            &env
        )
        .unwrap();

        assert_eq!(res.as_ref(), &[0xCB, 0b10000000]);

        let env = Env::default();
        let res = assemble_bit_res_or_set(
            Mnemonic::Res,
            &DataAccess::Expression(2.into()),
            &DataAccess::Register8(Register8::C),
            None,
            &env
        )
        .unwrap();

        assert_eq!(res.as_ref(), &[0xCB, 0b10010001]);

        let env = Env::default();
        let res = assemble_bit_res_or_set(
            Mnemonic::Res,
            &DataAccess::Expression(2.into()),
            &DataAccess::MemoryRegister16(Register16::Hl),
            None,
            &env
        )
        .unwrap();

        assert_eq!(res.as_ref(), &[0xCB, 0b10010110]);

        let env = Env::default();
        let res = assemble_bit_res_or_set(
            Mnemonic::Res,
            &DataAccess::Expression(2.into()),
            &DataAccess::IndexRegister16WithIndex(IndexRegister16::Ix, 3.into()),
            None,
            &env
        )
        .unwrap();

        assert_eq!(res.as_ref(), &[0xDD, 0xCB, 3, 0b10010110]);

        let env = Env::default();
        let res = assemble_bit_res_or_set(
            Mnemonic::Res,
            &DataAccess::Expression(2.into()),
            &DataAccess::IndexRegister16WithIndex(IndexRegister16::Ix, 3.into()),
            Some(&Register8::B),
            &env
        )
        .unwrap();

        assert_eq!(res.as_ref(), &[0xDD, 0xCB, 3, 0b10010000]);
    }

    #[test]
    pub fn test_ld() {
        let res = assemble_ld(
            &DataAccess::Register16(Register16::De),
            &DataAccess::Expression(Expr::Value(0x1234)),
            &Env::default()
        )
        .unwrap();
        assert_eq!(res.len(), 3);
        assert_eq!(res[0], 0x11);
        assert_eq!(res[1], 0x34);
        assert_eq!(res[2], 0x12);
    }

    #[test]
    #[should_panic]
    pub fn test_ld_fail() {
        let _res = assemble_ld(
            &DataAccess::Register16(Register16::Af),
            &DataAccess::Expression(Expr::Value(0x1234)),
            &Env::default()
        )
        .unwrap();
    }

    #[test]
    pub fn test_ld_r16_r16() {
        let res = assemble_ld(
            &DataAccess::Register16(Register16::De),
            &DataAccess::Register16(Register16::Hl),
            &Env::default()
        )
        .unwrap();
        assert_eq!(res.len(), 2);
    }

    #[test]
    pub fn test_repeat() {
        let tokens = vec![
            Token::Org(0.into(), None),
            Token::Repeat(
                10.into(),
                vec![Token::OpCode(Mnemonic::Nop, None, None, None)].into(),
                None,
                None
            ),
        ];

        let count = visit_tokens(&tokens).unwrap().size();
        assert_eq!(count, 10);
    }

    #[test]
    pub fn test_double_repeat() {
        let tokens = vec![
            Token::Org(0.into(), None),
            Token::Repeat(
                10.into(),
                vec![Token::Repeat(
                    10.into(),
                    vec![Token::OpCode(Mnemonic::Nop, None, None, None)].into(),
                    None,
                    None
                )]
                .into(),
                None,
                None
            ),
        ];

        let count = visit_tokens(&tokens).unwrap().size();
        assert_eq!(count, 100);
    }

    #[test]
    pub fn test_assemble_logical_operator() {
        let operators = [Mnemonic::And, Mnemonic::Or, Mnemonic::Xor];
        let operands = [
            DataAccess::Register8(Register8::A),
            DataAccess::Expression(0.into()),
            DataAccess::MemoryRegister16(Register16::Hl),
            DataAccess::IndexRegister16WithIndex(IndexRegister16::Ix, 2.into())
        ];

        for operator in &operators {
            for operand in &operands {
                let token = Token::OpCode(*operator, Some(operand.clone()), None, None);
                visit_tokens(&[token]).unwrap();
            }
        }
    }

    #[test]
    pub fn test_count() {
        let tokens = vec![
            Token::Org(0.into(), None),
            Token::OpCode(Mnemonic::Nop, None, None, None),
            Token::OpCode(Mnemonic::Nop, None, None, None),
            Token::OpCode(Mnemonic::Nop, None, None, None),
            Token::OpCode(Mnemonic::Nop, None, None, None),
            Token::OpCode(Mnemonic::Nop, None, None, None),
            Token::OpCode(Mnemonic::Nop, None, None, None),
            Token::OpCode(Mnemonic::Nop, None, None, None),
            Token::OpCode(Mnemonic::Nop, None, None, None),
            Token::OpCode(Mnemonic::Nop, None, None, None),
            Token::OpCode(Mnemonic::Nop, None, None, None),
        ];

        let count = visit_tokens(&tokens).unwrap().size();
        assert_eq!(count, 10);
    }

    #[test]
    pub fn test_stableticker() {
        let tokens = vec![
            Token::StableTicker(StableTickerAction::Start("myticker".into())),
            Token::OpCode(
                Mnemonic::Inc,
                Some(DataAccess::Register16(Register16::Hl)),
                None,
                None
            ),
            Token::StableTicker(StableTickerAction::Stop),
        ];

        let env = visit_tokens(&tokens);
        assert!(env.is_ok());
        let env = env.unwrap();

        let val = env.symbols().int_value("myticker");
        assert_eq!(val.unwrap().unwrap(), 2);
    }

    #[test]
    pub fn basic_no_variable() {
        let tokens = vec![Token::Basic(None, None, "10 PRINT &DEAD".to_owned())];

        let env = visit_tokens(&tokens);
        println!("{:?}", env);
        assert!(env.is_ok());
    }

    #[test]
    pub fn basic_variable_unset() {
        let tokens = vec![Token::Basic(
            Some(vec!["STUFF".into()]),
            None,
            "10 PRINT {STUFF}".to_owned()
        )];

        let env = visit_tokens(&tokens);
        println!("{:?}", env);
        assert!(env.is_err());
    }

    #[test]
    pub fn basic_variable_set() {
        let tokens = vec![
            Token::Label("STUFF".into()),
            Token::Basic(Some(vec!["STUFF".into()]), None, "10 PRINT {STUFF}".into()),
        ];

        let env = visit_tokens(&tokens);
        println!("{:?}", env);
        assert!(env.is_ok());
    }

    #[test]
    pub fn test_duration() {
        let tokens = vec![Token::OpCode(
            Mnemonic::Ld,
            Some(DataAccess::Register8(Register8::A)),
            Some(DataAccess::Expression(Expr::UnaryTokenOperation(
                UnaryTokenOperation::Duration,
                Box::new(Token::OpCode(
                    Mnemonic::Inc,
                    Some(DataAccess::Register16(Register16::Hl)),
                    None,
                    None
                ))
            ))),
            None
        )];

        let env = visit_tokens(&tokens);
        assert!(env.is_ok());
        let env = env.unwrap();
        let bytes = env.memory(0, 2);
        assert_eq!(bytes[1], 2);
    }

    #[test]
    pub fn test_opcode() {
        let tokens = vec![Token::OpCode(
            Mnemonic::Ld,
            Some(DataAccess::Register8(Register8::A)),
            Some(DataAccess::Expression(Expr::UnaryTokenOperation(
                UnaryTokenOperation::Opcode,
                Box::new(Token::OpCode(
                    Mnemonic::Inc,
                    Some(DataAccess::Register16(Register16::Hl)),
                    None,
                    None
                ))
            ))),
            None
        )];

        let env = visit_tokens(&tokens);
        assert!(env.is_ok());
        let env = env.unwrap();
        let bytes = env.memory(0, 2);
        assert_eq!(
            bytes[1],
            assemble_inc_dec(Mnemonic::Inc, &DataAccess::Register16(Register16::Hl), &env).unwrap()
                [0]
        );
    }

    #[test]
    pub fn test_bytes() {
        let mut m = Bytes::new();

        add_byte(&mut m, 2);
        assert_eq!(m.len(), 1);
        assert_eq!(m[0], 2);

        add_word(&mut m, 0x1234);
        assert_eq!(m.len(), 3);
        assert_eq!(m[1], 0x34);
        assert_eq!(m[2], 0x12);
    }

    #[test]
    pub fn test_labels() {
        let mut env = Env::default();
        let res = visit_token(&Token::Org(0x4000.into(), None), &mut env);
        assert!(res.is_ok());
        assert!(!env.symbols().contains_symbol("hello").unwrap());
        let res = visit_token(&Token::Label("hello".into()), &mut env);
        assert!(res.is_ok());
        assert!(env.symbols().contains_symbol("hello").unwrap());
        assert_eq!(env.symbols().int_value("hello").unwrap(), 0x4000.into());
    }

    #[test]
    pub fn test_jr() {
        let res = dbg!(visit_tokens_all_passes(
            &[
                Token::Org(0x4000.into(), None),
                Token::OpCode(
                    Mnemonic::Jr,
                    None,
                    Some(DataAccess::Expression(Expr::Label("$".into()))),
                    None,
                ),
            ],
            ctx()
        ));

        assert!(res.is_ok());
        let env = res.unwrap();

        assert_eq!(
            env.memory(0x4000, 2),
            &[0x18, 0u8.wrapping_sub(1).wrapping_sub(1)]
        );
    }

    /// Check if  label already exists
    #[test]
    pub fn label_exists() {
        let res = visit_tokens_all_passes(
            &[
                Token::Org(0x4000.into(), None),
                Token::Label("hello".into()),
                Token::Label("hello".into())
            ],
            ctx()
        );
        assert!(res.is_err());
    }

    #[test]
    pub fn test_rorg() {
        let res = visit_tokens_all_passes(
            &[
                Token::Org(0x4000i32.into(), None),
                Token::Rorg(
                    0x8000i32.into(),
                    vec![Token::Defb(vec![Expr::Label("$".into())])].into()
                )
            ],
            ctx()
        );
        assert!(res.is_ok());
    }

    #[test]
    pub fn test_two_passes() {
        let tokens = vec![
            Token::Org(0x123i32.into(), None),
            Token::OpCode(
                Mnemonic::Ld,
                Some(DataAccess::Register16(Register16::Hl)),
                Some(DataAccess::Expression(Expr::Label("test".into()))),
                None
            ),
            Token::Label("test".into()),
        ];
        let env = visit_tokens(&tokens);
        assert!(env.is_err());

        let env = visit_tokens_all_passes(&tokens, ctx());
        assert!(env.is_ok());
        let env = env.ok().unwrap();

        let count = env.size();
        assert_eq!(count, 3);

        assert_eq!(
            env.symbols()
                .int_value(&"test".to_owned())
                .unwrap()
                .unwrap(),
            0x123 + 3
        );
        let buffer = env.memory(0x123, 3);
        assert_eq!(buffer[1], 0x23 + 3);
        assert_eq!(buffer[2], 0x1);
    }

    #[test]
    fn test_read_bytes() {
        let tokens = vec![
            Token::Org(0x100.into(), None),
            Token::Defb(vec![1.into(), 2.into()]),
            Token::Defb(vec![3.into(), 4.into()]),
        ];

        let env = visit_tokens(&tokens).unwrap();
        let bytes = env.memory(0x100, 4);
        assert_eq!(bytes, vec![1, 2, 3, 4]);
    }

    #[test]
    pub fn test_undocumented_rlc() {
        let res = visit_tokens_all_passes(
            &[
                Token::Org(0x100.into(), None),
                Token::OpCode(
                    Mnemonic::Rlc,
                    Some(DataAccess::IndexRegister16WithIndex(
                        IndexRegister16::Iy,
                        2.into()
                    )),
                    Some(DataAccess::Register8(Register8::C)),
                    None
                )
            ],
            ctx()
        );
        assert!(res.is_ok());
        let env = res.unwrap();
        let bytes = env.memory(0x100, 4);
        assert_eq!(bytes, vec![0xFD, 0xCB, 0x2, 0x1]);
    }

    #[test]
    pub fn test_undocumented_res() {
        // normal case
        let res = visit_tokens_all_passes(
            &[
                Token::Org(0x100.into(), None),
                Token::OpCode(
                    Mnemonic::Res,
                    Some(DataAccess::Expression(4.into())),
                    Some(DataAccess::MemoryRegister16(Register16::Hl)),
                    None
                )
            ],
            ctx()
        );
        assert!(res.is_ok());
        let env = res.unwrap();
        let bytes = env.memory(0x100, 2);
        assert_eq!(bytes, vec![0xCB, 0xA6]);

        let res = visit_tokens_all_passes(
            &[
                Token::Org(0x100.into(), None),
                Token::OpCode(
                    Mnemonic::Res,
                    Some(DataAccess::Expression(4.into())),
                    Some(DataAccess::IndexRegister16WithIndex(
                        IndexRegister16::Iy,
                        2.into()
                    )),
                    Some(Register8::A)
                )
            ],
            ctx()
        );
        assert!(res.is_ok());
        let env = res.unwrap();
        let bytes = env.memory(0x100, 4);
        assert_eq!(bytes, vec![0xFD, 0xCB, 0x2, 0xA7]);
    }

    lazy_static::lazy_static! {
        static ref  CTX: ParserContext = Default::default();
    }
    fn ctx() -> &'static ParserContext {
        &CTX
    }
}