summaryrefslogtreecommitdiff
path: root/stage2/cc_x86.s
blob: 8638e9b2649aba9e0000859b1e5c82cb23c988cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
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
; Copyright (C) 2016 Jeremiah Orians
; This file is part of stage0.
;
; stage0 is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
;
; stage0 is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with stage0.  If not, see <http://www.gnu.org/licenses/>.

;; A Minimal C Compiler
;; type Cells are in the following form:
;; NEXT (0), SIZE (4), OFFSET (8), INDIRECT (12), MEMBERS (16), TYPE (20), NAME (24)
;; token_list Cells are in the following form:
;; NEXT (0), LOCALS/PREV (4), S (8), TYPE/FILENAME (12), ARGUMENTS/DEPTH/LINENUMBER (16)
;; Each being the length of a register [32bits]
;;

;; STACK space: End of program -> 512KB (0x80000) [Could be reduced]
;; HEAP space: 512KB -> End of Memory

;; R15 is the STACK pointer
;; R14 is the HEAP pointer

:start
	;; Prep TAPE_02
	LOADUI R0 0x1101
	FOPEN_WRITE

	;; Prep TAPE_01
	LOADUI R0 0x1100
	FOPEN_READ

:main
	LOADUI R0 0x1100            ; Pass Tape_01 for reading
	LOADR32 R14 @HEAP           ; Setup Initial HEAP
	LOADUI R15 $STACK           ; Setup Initial STACK
	CALLI R15 @read_all_tokens  ; Read all Tokens in Tape_01
	CALLI R15 @reverse_list     ; Fix Token Order
;	CALLI R15 @debug_list       ; Lets try to debug token errors
	MOVE R13 R0                 ; Set global_token for future reading
	FALSE R12                   ; Set struct token_list* out to NULL
	FALSE R11                   ; Set struct token_list* list_strings to NULL
	FALSE R10                   ; Set struct token_list* globals_list to NULL
	CALLI R15 @program          ; Build our output
	LOADUI R0 $header_string1   ; Using our first header string
	LOADUI R1 0x1101            ; Using Tape_02
	CALLI R15 @file_print       ; Write string
	MOVE R0 R12                 ; using Contents of output_list
	CALLI R15 @recursive_output ; Recursively write
	LOADUI R0 $header_string2   ; Using our second header string
	CALLI R15 @file_print       ; Write string
	MOVE R0 R10                 ; using Contents of globals_list
	CALLI R15 @recursive_output ; Recursively write
	LOADUI R0 $header_string3   ; Using our third header string
	CALLI R15 @file_print       ; Write string
	MOVE R0 R11                 ; using Contents of strings_list
	CALLI R15 @recursive_output ; Recursively write
	LOADUI R0 $header_string4   ; Using our final header string
	CALLI R15 @file_print       ; Write string
	HALT                        ; We have completed compiling our input

;; Symbol lists
:global_constant_list
	NOP
:global_symbol_list
	NOP
:global_function_list
	NOP

;; Pointer to initial HEAP ADDRESS
:HEAP
	'00080000'

;; Output strings
:header_string1
	"
# Core program
"
:header_string2
	"
# Program global variables
"

:header_string3
	"
# Program strings
"

:header_string4
	"
:ELF_end
"

;; clearWhiteSpace function
;; Receives a character in R0 and FILE* in R1 and line_num in R11
;; Returns first non-whitespace character in R0
:clearWhiteSpace
	CMPSKIPI.NE R0 32           ; Check for a Space
	JUMP @clearWhiteSpace_reset ; Looks like we need to remove a space

	CMPSKIPI.NE R0 9            ; Check for a tab
	JUMP @clearWhiteSpace_reset ; Looks like we need to remove a tab

	CMPSKIPI.E R0 10            ; Check for a newline
	RET R15                     ; Looks we found a non-whitespace

	ADDUI R11 R11 1             ; Increment line number
	;; Fall through to iterate to next char

:clearWhiteSpace_reset
	FGETC                       ; Get next char
	JUMP @clearWhiteSpace       ; Iterate


;; consume_byte function
;; Receives a char in R0, FILE* in R1 and index in R13
;; Returns next char in R0
:consume_byte
	STOREX8 R0 R14 R13          ; Put char onto HEAP
	ADDUI R13 R13 1             ; Increment index
	FGETC                       ; Get next char
	RET R15


;; consume_word function
;; Receives a char in R0, FILE* in R1, FREQUENT in R2 and index in R13
;; Returns next char in R0
:consume_word
	PUSHR R3 R15                ; Protect R3
	FALSE R3                    ; ESCAPE is FALSE
:consume_word_reset
	JUMP.NZ R3 @consume_word_iter1
	CMPSKIPI.NE R0 92           ; If \
	TRUE R3                     ; Looks like we are in an escape
	JUMP @consume_word_iter2
:consume_word_iter1
	FALSE R3                    ; Looks like we are no longer in an escape
:consume_word_iter2
	CALLI R15 @consume_byte     ; Store the char
	JUMP.NZ R3 @consume_word_reset        ; If escape loop
	CMPJUMPI.NE R0 R2 @consume_word_reset ; if not matching frequent loop
	FGETC                       ; Get a new char to return
	POPR R3 R15                 ; Restore R3
	RET R15


;; fixup_label function
;; Receives nothing (But uses R14 as HEAP pointer)
;; Returns 32 in R0 and no other registers altered
:fixup_label
	PUSHR R1 R15                ; Protect R1 from change
	PUSHR R2 R15                ; Protect R2 from change
	LOADUI R0 58                ; Set HOLD to :
	FALSE R2                    ; Set I to 0
:fixup_label_reset
	MOVE R1 R0                  ; Set PREV = HOLD
	LOADXU8 R0 R14 R2           ; Read hold_string[I] into HOLD
	STOREX8 R1 R14 R2           ; Set hold_string[I] = PREV
	ADDUI R2 R2 1               ; increment I
	JUMP.NZ R0 @fixup_label_reset ; Loop until we hit a NULL

	;; clean up
	ADDUI R2 R2 1               ; increment I
	LOADUI R0 32                ; Put 32 in R0
	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	RET R15


;; in_set2 function
;; Receives a Char in R0, FILE* in R1, char* in R2 and index in R13
;; Return result in R2
:in_set2
	PUSHR R3 R15                ; Protect R3 from changes
:in_set2_reset
	LOADU8 R3 R2 0              ; Get char from list
	JUMP.Z R3 @in_set2_fail     ; Stop when 0 == s[0]
	CMPJUMPI.E R0 R3 @in_set2_done ; We found a match
	ADDUI R2 R2 1               ; Increment to next char
	JUMP.NZ R3 @in_set2_reset   ; Iterate if not NULL

:in_set2_fail
	;; Looks like not found
	FALSE R2                    ; Return FALSE

:in_set2_done
	CMPSKIPI.E R2 0             ; Provided not FALSE
	TRUE R2                     ; The result is true
	POPR R3 R15                 ; Restore R3
	RET R15


;; in_set function
;; Receives a Char in R0, char* in R1
;; Return result in R0
:in_set
	PUSHR R2 R15                ; Protect R3 from changes
:in_set_reset
	LOADU8 R2 R1 0              ; Get char from list
	JUMP.Z R2 @in_set_fail      ; Stop when 0 == s[0]
	CMPJUMPI.E R0 R2 @in_set_done ; We found a match
	ADDUI R1 R1 1               ; Increment to next char
	JUMP.NZ R2 @in_set_reset    ; Iterate if not NULL

:in_set_fail
	;; Looks like not found
	FALSE R1                    ; Return FALSE

:in_set_done
	CMPSKIPI.E R1 0             ; Provided not FALSE
	TRUE R2                     ; The result is true
	MOVE R0 R2                  ; Put result in correct place
	POPR R2 R15                 ; Restore R3
	RET R15

;; Common in_set strings of interest
;; As Raw strings (") is forbidden and ' has some restrictions
:nice_chars
	"	
 !#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
:keyword_chars
	"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"
:variable_chars
	"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"
:symbol_chars
	"<=>|&!-"
:hex_chars
	"0123456789ABCDEF"
:digit_chars
	"0123456789"
:whitespace_chars
	" 	
"

;; preserve_keyword function
;; Receives a Char in R0, FILE* in R1 and index in R13
;; Overwrites R2
;; Returns next CHAR
:preserve_keyword
	LOADUI R2 $keyword_chars    ; Using keyword list of chars
	CALLI R15 @in_set2          ; Check if in list
	JUMP.Z R2 @preserve_keyword_label ; if not in set, stop iterating

:preserve_keyword_reset
	CALLI R15 @consume_byte     ; Consume another byte
	JUMP @preserve_keyword      ; Iterate

:preserve_keyword_label
	CMPSKIPI.NE R0 58           ; Check for label (:)
	CALLI R15 @fixup_label      ; Looks like we found one
	RET R15


;; preserve_symbol function
;; Receives a Char in R0, FILE* in R1 and index in R13
;; Overwrites R2
;; Returns next CHAR
:preserve_symbol
	LOADUI R2 $symbol_chars     ; Using symbol list of chars
	CALLI R15 @in_set2          ; Check if in list
	JUMP.NZ R2 @preserve_symbol_reset

	;; Looks we didn't find anything we wanted to preserve
	RET R15

:preserve_symbol_reset
	CALLI R15 @consume_byte     ; Consume another byte
	JUMP @preserve_symbol       ; Iterate


;; purge_macro function
;; Receives a Char in R0, FILE* in R1 and index in R13
;; Returns next CHAR via jumping to get_token_reset
:purge_macro
	CMPSKIPI.NE R0 10           ; Check for Line Feed
	JUMP @get_token_reset       ; Looks like we found it, call it done

	FGETC                       ; Looks like we need another CHAR
	JUMP @purge_macro           ; Keep looping


;; get_token function
;; Receives a Char in R0, FILE* in R1, line_num in R11 and TOKEN in R10
;; sets index in R13 and current in R12
;; Overwrites R2
;; Returns next CHAR
:get_token
	PUSHR R12 R15               ; Preserve R12
	PUSHR R13 R15               ; Preserve R13
	COPY R12 R14                ; Save CURRENT's Address
	ADDUI R14 R14 20            ; Update Malloc to free space for string
:get_token_reset
	FALSE R13                   ; Reset string_index to 0
	CALLI R15 @clearWhiteSpace  ; Clear any leading whitespace
	CMPSKIPI.NE R0 35           ; Deal with # line macros
	JUMP @purge_macro           ; Returns at get_token_reset

	;; Check for keywords
	LOADUI R2 $keyword_chars    ; Using keyword list
	CALLI R15 @in_set2          ; Check if keyword
	JUMP.Z R2 @get_token_symbol ; if not a keyword
	CALLI R15 @preserve_keyword ; Yep its a keyword
	JUMP @get_token_done        ; Be done with token

	;; Check for symbols
:get_token_symbol
	LOADUI R2 $symbol_chars     ; Using symbol list
	CALLI R15 @in_set2          ; Check if symbol
	JUMP.Z R2 @get_token_char   ; If not a symbol
	CALLI R15 @preserve_symbol  ; Yep its a symbol
	JUMP @get_token_done        ; Be done with token

	;; Check for char
:get_token_char
	CMPSKIPI.E R0 39            ; Check if '
	JUMP @get_token_string      ; Not a '
	COPY R2 R0                  ; Prepare for consume_word
	CALLI R15 @consume_word     ; Call it
	JUMP @get_token_done        ; Be done with token

	;; Check for string
:get_token_string
	CMPSKIPI.E R0 34            ; Check if "
	JUMP @get_token_EOF         ; Not a "
	COPY R2 R0                  ; Prepare for consume_word
	CALLI R15 @consume_word     ; Call it
	JUMP @get_token_done        ; Be done with token

	;; Check for EOF
:get_token_EOF
	CMPSKIPI.L R0 0             ; If c < 0
	JUMP @get_token_comment     ; If not EOF
	POPR R13 R15                ; Restore R13
	POPR R12 R15                ; Restore R12
	RET R15                     ; Otherwise just return the EOF

	;; Check for C comments
:get_token_comment
	CMPSKIPI.E R0 47            ; Deal with non-comments
	JUMP @get_token_else        ; immediately

	CALLI R15 @consume_byte     ; Deal with another byte
	CMPSKIPI.NE R0 42           ; if * make it a block comment
	JUMP @get_token_comment_block ; and purge it all

	CMPSKIPI.E R0 47            ; Check if not //
	JUMP @get_token_done        ; Finish off the token

	;; Looks like it was //
	FGETC                       ; Get next char
	JUMP @get_token_reset       ; Try again

	;; Deal with the mess that is C block comments
:get_token_comment_block
	FGETC                       ; Get next char
:get_token_comment_block_outer
	CMPSKIPI.NE R0 47           ; Check for closing /
	JUMP @get_token_comment_block_outer_done ; Yep has closing /
:get_token_comment_block_inner
	CMPSKIPI.NE R0 42           ; Check for preclosing *
	JUMP @get_token_comment_block_inner_done ; Yep has *

	;; Otherwise we are just consuming
	FGETC                       ; Remove another CHAR
	CMPSKIPI.NE R0 10           ; Check for Line Feed
	ADDUI R11 R11 1             ; Found one, updating line number
	JUMP @get_token_comment_block_inner

:get_token_comment_block_inner_done
	FGETC                       ; Remove another CHAR
	CMPSKIPI.NE R0 10           ; Check for Line Feed
	ADDUI R11 R11 1             ; Found one, updating line number
	JUMP @get_token_comment_block_outer

:get_token_comment_block_outer_done
	FGETC                       ; Remove another CHAR
	JUMP @get_token_reset       ; And Try again

	;; Deal with default case
:get_token_else
	CALLI R15 @consume_byte     ; Consume the byte and be done

:get_token_done
	ADDUI R13 R13 2             ; Pad with NULL the string
	STORE32 R14 R12 8           ; Set CURRENT->S to String
	ADD R14 R14 R13             ; Add string length to HEAP

	STORE32 R10 R12 0           ; CURRENT->NEXT = TOKEN
	STORE32 R10 R12 4           ; CURRENT->PREV = TOKE
	STORE32 R11 R12 16          ; CURRENT->LINENUM = LINE_NUM
	MOVE R10 R12                ; SET TOKEN to CURRENT
	POPR R13 R15                ; Restore R13
	POPR R12 R15                ; Restore R12
	RET R15


;; reverse_list function
;; Receives a Token_list in R0
;; Returns List in Reverse order in R0
:reverse_list
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	FALSE R1                    ; Set ROOT to NULL
	CMPJUMPI.E R0 R1 @reverse_list_done ; ABORT if given a NULL
:reverse_list_reset
	LOAD32 R2 R0 0              ; SET next to HEAD->NEXT
	STORE32 R1 R0 0             ; SET HEAD->NEXT to ROOT
	MOVE R1 R0                  ; SET ROOT to HEAD
	MOVE R0 R2                  ; SET HEAD to NEXT
	JUMP.NZ R0 @reverse_list_reset ; Iterate if HEAD not NULL

:reverse_list_done
	MOVE R0 R1                  ; SET Result to ROOT
	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	RET R15


;; read_all_tokens function
;; Receives a FILE* in R0
;; sets line_num in R11 and TOKEN in R10
;; Overwrites R2
;; Returns struct token_list* in R0
:read_all_tokens
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	PUSHR R10 R15               ; Protect R10
	PUSHR R11 R15               ; Protect R11
	MOVE R1 R0                  ; Set R1 as FILE*
	FGETC                       ; Read our first CHAR
	LOADUI R11 1                ; Start line_num at 1
	FALSE R10                   ; First token is NULL
:read_all_tokens_reset
	JUMP.NP R0 @read_all_tokens_done
	CALLI R15 @get_token
	JUMP @read_all_tokens_reset
:read_all_tokens_done
	MOVE R0 R10                 ; Return the Token
	POPR R11 R15                ; Restore R11
	POPR R10 R15                ; Restore R10
	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	RET R15


;; parse_string function
;; Receives char* string in R0
;; R14 is HEAP Pointer
;; Returns char* in R0
:parse_string
	PUSHR R1 R15                ; Protect R1
	COPY R1 R0                  ; Make a copy of STRING
	CALLI R15 @weird            ; Check if string is weird
	SWAP R0 R1
	JUMP.Z R1 @parse_string_regular ; Deal with regular strings

	;; Looks like we have a weirdo
	CALLI R15 @collect_weird_string ; Create our weird string
	JUMP @parse_string_done     ; Simply return what was created
:parse_string_regular
	CALLI R15 @collect_regular_string
:parse_string_done
	POPR R1 R15                 ; Restore R1
	RET R15


;; weird function
;; Analyze string to determine if it's output would be weird for mescc-tools
;; Receives char* in R0
;; Returns BOOL in R0
:weird
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	PUSHR R3 R15                ; Protect R3
	PUSHR R4 R15                ; Protect R4
	FALSE R2                    ; Assume FALSE
	ADDUI R3 R0 1               ; STRING = STRING + 1
:weird_iter
	JUMP.NZ R2 @weird_done      ; Stop if TRUE
	LOADU8 R4 R3 0              ; C = STRING[0]
	JUMP.Z R4 @weird_done       ; Be done at NULL Termination
	CMPSKIPI.E R4 92            ; If not '\\'
	JUMP @weird_post_escape     ; Looks like no escape analysis

	;; Deal with the mess
	COPY R0 R3                  ; Using STRING
	CALLI R15 @escape_lookup    ; Get our CHAR
	MOVE R4 R0                  ; C = ESCAPE_LOOKUP(STRING)
	LOADU8 R0 R3 1              ; STRING[1]
	CMPSKIPI.NE R0 120          ; if 'x' == STRING[1]
	ADDUI R3 R3 2               ; STRING = STRING + 2
	ADDUI R3 R3 1               ; STRING = STRING + 1

:weird_post_escape
	LOADUI R1 $nice_chars       ; using list of nice CHARS
	COPY R0 R4                  ; using copy of C
	CALLI R15 @in_set           ; Use in_set
	CMPSKIPI.NE R0 0            ; IF TRUE
	TRUE R2                     ; Return TRUE

	ADDUI R3 R3 1               ; STRING = STRING + 1
	LOADUI R1 $whitespace_chars ; Check Whitespace Chars
	COPY R0 R4                  ; Using copy of C
	CALLI R15 @in_set           ; Use in_set
	JUMP.Z R0 @weird_iter       ; If False simply loop
	LOADU8 R0 R3 0              ; STRING[1]
	CMPSKIPI.NE R0 58           ; If ':' == STRING[1]
	TRUE R2                     ; Flip flag
	JUMP @weird_iter            ; Keep trying to find an answer

:weird_done
	MOVE R0 R2                  ; Whatever is in R2 is the answer
	POPR R4 R15                 ; Restore R4
	POPR R3 R15                 ; Restore R3
	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	RET R15


;; collect_weird_string function
;; Converts weird string into a form mescc-tools can handle cleanly
;; Receives char* in R0
;; R14 is HEAP Pointer and $hex_chars as the table
;; Returns char* in R0
:collect_weird_string
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	PUSHR R3 R15                ; Protect R3
	PUSHR R4 R15                ; Protect R4
	LOADUI R4 $hex_chars        ; Pointer to TABLE
	COPY R3 R14                 ; Get HOLD
	MOVE R2 R0                  ; Put STRING in Place
	LOADUI R0 39                ; Prefix with '
	PUSH8 R0 R3                 ; HOLD[0] = '\'' && HOLD = HOLD + 1
:collect_weird_string_iter
	ADDUI R2 R2 1               ; STRING = STRING + 1
	LOADUI R0 32                ; Insert ' '
	PUSH8 R0 R3                 ; HOLD[0] = ' ' && HOLD = HOLD + 1
	COPY R0 R2                  ; copy STRING
	CALLI R15 @escape_lookup    ; Get char value
	ANDI R1 R0 0x0F             ; Save Bottom out of the way
	SR0I R0 4                   ; Isolate Top
	LOADXU8 R0 R4 R0            ; Using Table
	LOADXU8 R1 R4 R1            ; Using Table
	PUSH8 R0 R3                 ; HOLD[0] = TABLE[(TEMP >> 4)] && HOLD = HOLD + 1
	PUSH8 R1 R3                 ; HOLD[0] = TABLE[(TEMP & 15)] && HOLD = HOLD + 1
	LOADU8 R0 R2 0              ; STRING[0]
	JUMP.Z R0 @collect_weird_string_done ; Stop if NULL
	CMPSKIPI.E R0 92            ; IF STRING[0] != '\\'
	JUMP @collect_weird_string_check ; Deal with iteration
	LOADU8 R0 R2 1              ; STRING[1]
	CMPSKIPI.NE R0 120          ; If STRING[1] == 'x'
	ADDUI R2 R2 2               ; STRING = STRING + 2
	ADDUI R2 R2 1               ; STRING = STRING + 1
:collect_weird_string_check
	LOADU8 R0 R2 1              ; STRING[1]
	JUMP.NZ R0 @collect_weird_string_iter

:collect_weird_string_done
	LOADUI R0 32                ; Insert ' '
	PUSH8 R0 R3                 ; HOLD[0] = ' ' && HOLD = HOLD + 1
	LOADUI R0 48                ; Insert '0'
	PUSH8 R0 R3                 ; HOLD[0] = '0' && HOLD = HOLD + 1
	PUSH8 R0 R3                 ; HOLD[0] = '0' && HOLD = HOLD + 1
	LOADUI R0 39                ; Insert '\''
	PUSH8 R0 R3                 ; HOLD[0] = '\'' && HOLD = HOLD + 1
	LOADUI R0 10                ; Insert '\n'
	PUSH8 R0 R3                 ; HOLD[0] = '\n' && HOLD = HOLD + 1
	ADDUI R3 R3 1               ; NULL Terminate
	SWAP R3 R14                 ; CALLOC HOLD
	MOVE R0 R3                  ; Return HOLD
	POPR R4 R15                 ; Restore R4
	POPR R3 R15                 ; Restore R3
	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	RET R15


;; hex function
;; Receives Char in R0
;; Return Int in R0
:hex
	SUBUI R0 R0 48              ; First shift
	CMPSKIPI.GE R0 10           ; If 0-9
	RET R15                     ; Be done

	;; Deal with A-F
	ANDI R0 R0 0xDF             ; Unset high bit
	SUBUI R0 R0 7               ; Shift them down
	CMPSKIPI.GE R0 10           ; if between 9 and A
	JUMP @hex_error             ; Throw an error
	CMPSKIPI.L R0 16            ; if > F
	JUMP @hex_error             ; Throw an error
	RET R15

:hex_error
	LOADUI R0 $hex_error_message ; Our message
	FALSE R1                    ; For human
	CALLI R15 @file_print       ; write it
	CALLI R15 @line_error       ; More info
	HALT

:hex_error_message
	"Tried to print non-hex number
"


;; escape_lookup function
;; Receives char* in R0
;; Returns char in R0
:escape_lookup
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	MOVE R1 R0                  ; Put C in the right spot
	FALSE R2                    ; Our flag for done
	LOADU8 R0 R1 0              ; c[0]
	CMPSKIPI.E R0 92            ; If C[0] != '\\'
	JUMP @escape_lookup_none    ; Deal with none case

	LOADU8 R0 R1 1              ; c[1]
	CMPSKIPI.NE R0 120          ; if \x??
	JUMP @escape_lookup_hex

	;; Deal with \? escapes
	CMPSKIPI.NE R0 110          ; If \n
	LOADUI R2 10                ; return \n

	CMPSKIPI.NE R0 116          ; If \t
	LOADUI R2 9                 ; return \t

	CMPSKIPI.NE R0 92           ; If \\
	LOADUI R2 92                ; return \\

	CMPSKIPI.NE R0 39           ; If \'
	LOADUI R2 39                ; return \'

	CMPSKIPI.NE R0 34           ; If \"
	LOADUI R2 34                ; return \"

	CMPSKIPI.NE R0 114          ; If \r
	LOADUI R2 13                ; return \r

	JUMP.Z R2 @escape_lookup_error ; Looks like we got something weird
	JUMP @escape_lookup_done    ; Otherwise just use our R2

:escape_lookup_none
	MOVE R2 R0                  ; We just return the char at C[0]
	JUMP @escape_lookup_done    ; Be done

:escape_lookup_hex
	LOADU8 R0 R1 2              ; c[2]
	CALLI R15 @hex              ; Get first char
	SL0I R0 4                   ; Shift our first nybble
	MOVE R2 R0                  ; Protect our top nybble
	LOADU8 R0 R1 3              ; c[3]
	CALLI R15 @hex              ; Get second char
	ADD R2 R2 R0                ; \x?? => ? << 4 + ?

:escape_lookup_done
	MOVE R0 R2                  ; R2 has our answer
	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	RET R15

:escape_lookup_error
	MOVE R2 R0                  ; Protect Char that failed
	LOADUI R0 $escape_lookup_string0 ; Load message
	FALSE R1                    ; We want the User to see
	CALLI R15 @file_print       ; Write it
	MOVE R0 R2                  ; Our CHAR
	FPUTC                       ; Write it
	LOADUI R0 10                ; '\n'
	FPUTC                       ; Write it
	CALLI R15 @line_error       ; Provide some debug information
	HALT

:escape_lookup_string0
	"Received invalid escape \\"


;; collect_regular_string function
;; Converts C string into a RAW string for mescc-tools
;; Receives char* in R0
;; R14 is HEAP Pointer
;; Returns char* in R0
:collect_regular_string
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	COPY R2 R14                 ; MESSAGE
	MOVE R1 R0                  ; Put STRING in the right place
:collect_regular_string_iter
	LOADU8 R0 R1 0              ; STRING[0]
	JUMP.Z R0 @collect_regular_string_done ; End at NULL
	CMPSKIPI.NE R0 92           ; if STRING[0] == '\\'
	JUMP @collect_regular_string_escape ; deal with escapes

	;; Deal with vannilla chars
	STORE8 R0 R2 0              ; MESSAGE[0] = STRING[0]
	ADDUI R2 R2 1               ; MESSAGE = MESSAGE + 1
	ADDUI R1 R1 1               ; STRING = STRING + 1
	JUMP @collect_regular_string_iter ; Loop

:collect_regular_string_escape
	COPY R0 R1                  ; Prepare for call
	CALLI R15 @escape_lookup    ; Get what weird char we need
	STORE8 R0 R2 0              ; MESSAGE[0] = escape_lookup(string)
	ADDUI R2 R2 1               ; MESSAGE = MESSAGE + 1
	LOADU8 R0 R1 1              ; STRING[1]
	CMPSKIPI.NE R0 120          ; if \x??
	ADDUI R1 R1 2               ; STRING = STRING + 2
	ADDUI R1 R1 2               ; STRING = STRING + 2
	JUMP @collect_regular_string_iter ; Loop

:collect_regular_string_done
	LOADUI R0 34                ; Using "
	STORE8 R0 R2 0              ; MESSAGE[0] = '"'
	LOADUI R0 10                ; Using '\n'
	STORE8 R0 R2 1              ; MESSAGE[1] = "\n"
	ADDUI R2 R2 3               ; Add extra NULL padding
	SWAP R2 R14                 ; Update HEAP
	MOVE R0 R2                  ; Put MESSAGE in the right Spot
	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	RET R15


;; unary_expr_sizeof function
;; Receives nothing
;; Returns nothing
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
:unary_expr_sizeof
	PUSHR R0 R15                ; Protect R0
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	LOADUI R0 $unary_expr_sizeof_string0 ; Our first error message
	LOADUI R1 $open_paren       ; Using "("
	CALLI R15 @require_match    ; Ensure a match
	CALLI R15 @type_name        ; Get type_name
	MOVE R2 R0                  ; Protect A
	LOADUI R0 $unary_expr_sizeof_string1 ; Our final error message
	LOADUI R1 $close_paren      ; Using ")"
	CALLI R15 @require_match    ; Ensure a match

	LOADUI R0 $unary_expr_sizeof_string2 ; Our header
	CALLI R15 @emit_out         ; emit it
	LOAD32 R0 R2 4              ; A->SIZE
	CALLI R15 @numerate_number  ; Convert to string
	CALLI R15 @emit_out         ; emit it
	LOADUI R0 $newline          ; Using "\n"
	CALLI R15 @emit_out         ; emit it

	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	POPR R0 R15                 ; Restore R0
	RET R15

:unary_expr_sizeof_string0
	"ERROR in unary_expr
Missing (
"
:unary_expr_sizeof_string1
	"ERROR in unary_expr
Missing )
"
:unary_expr_sizeof_string2
	"LOAD_IMMEDIATE_eax %"


;; constant_load function
;; Receives struct token_list* a in R0
;; Returns nothing
:constant_load
	PUSHR R0 R15                ; Protect R0
	LOADUI R0 $constant_load_string0 ; Our header
	CALLI R15 @emit_out         ; emit it
	POPR R0 R15                 ; Restore R0
	LOAD32 R0 R0 16             ; A->ARGUMENTS
	LOAD32 R0 R0 8              ; A->ARGUMENTS->S
	CALLI R15 @emit_out         ; emit it
	LOADUI R0 $newline          ; Using "\n"
	CALLI R15 @emit_out         ; emit it
	RET R15

:constant_load_string0
	"LOAD_IMMEDIATE_eax %"


;; variable_load function
;; Receives struct token_list* a in R0
;;	and struct token_list* current_target in R8
;; Returns Nothing
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
:variable_load
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	MOVE R2 R0                  ; Protect A

	;; Check if function call
	LOADUI R0 $type_function_name ; Using "FUNCTION"
	LOAD32 R1 R2 12             ; A->TYPE
	LOAD32 R1 R1 24             ; A->TYPE->NAME
	CALLI R15 @match            ; IF "FUNCTION" ==  A->TYPE->NAME
	JUMP.Z R0 @variable_load_regular ; Nope

	LOADUI R0 $open_paren       ; Using "("
	LOAD32 R1 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @match            ; IF "(" == GLOBAL_TOKEN->S
	JUMP.Z R0 @variable_load_regular ; Nope

	;; Deal with function call
	LOAD32 R0 R2 16             ; A->DEPTH
	CALLI R15 @numerate_number  ; Convert to string
	TRUE R1                     ; Passing TRUE
	CALLI R15 @function_call    ; DO IT
	JUMP @variable_load_done    ; Be done

:variable_load_regular
	LOAD32 R8 R2 12             ; CURRENT_TARGET = A->TYPE
	LOADUI R0 $variable_load_string0 ; Our prefix
	CALLI R15 @emit_out         ; emit it

	LOAD32 R0 R2 16             ; A->DEPTH
	CALLI R15 @numerate_number  ; Convert to string
	CALLI R15 @emit_out         ; emit it

	LOADUI R0 $newline          ; Using "\n"
	CALLI R15 @emit_out         ; emit it

	;; check for special case 1
	LOADUI R0 $equal            ; Using "="
	LOAD32 R1 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @match            ; IF GLOBAL_TOKEN->S == "="
	JUMP.NZ R0 @variable_load_done ; Be done

	;; deal with the general case
	LOADUI R0 $variable_load_string1 ; Our postfix
	CALLI R15 @emit_out         ; emit it

:variable_load_done
	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	RET R15

:variable_load_string0
	"LOAD_BASE_ADDRESS_eax %"
:variable_load_string1
	"LOAD_INTEGER
"


;; function_load function
;; Receives struct token_list* a in R0
;; Returns nothing
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
:function_load
	PUSHR R1 R15                ; Protect R1
	LOAD32 R0 R0 8              ; A->S
	PUSHR R0 R15                ; Protect A->S
	LOADUI R0 $open_paren       ; Using "("
	LOAD32 R1 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @match            ; If GLOBAL_TOKEN->S == "("
	JUMP.Z R0 @function_load_regular ; If not do the simple thing

	;; Deal iwth function call
	POPR R0 R15                 ; Restore A->S
	FALSE R1                    ; FALSE
	CALLI R15 @function_call    ; Do the function call
	JUMP @function_load_done    ; Clean up

:function_load_regular
	LOADUI R0 $function_load_string0 ; Using our header string
	CALLI R15 @emit_out         ; emit it
	POPR R0 R15                 ; Restore A->S
	CALLI R15 @emit_out         ; emit it
	LOADUI R0 $newline          ; Using "\n"
	CALLI R15 @emit_out         ; emit it

:function_load_done
	POPR R1 R15                 ; Restore R1
	RET R15

:function_load_string0
	"LOAD_IMMEDIATE_eax &FUNCTION_"


;; global_load function
;; Receives struct token_list* a in R0
;;	and struct token_list* current_target in R8
;; Returns nothing
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
:global_load
	PUSHR R0 R15                ; Protect A
	LOAD32 R8 R0 12             ; CURRENT_TARGET = A->TYPE
	LOADUI R0 $global_load_string0 ; Our header string
	CALLI R15 @emit_out         ; emit it
	POPR R0 R15                 ; Restore A
	LOAD32 R0 R0 8              ; A->S
	CALLI R15 @emit_out         ; emit it
	LOADUI R0 $newline          ; Using "\n"
	CALLI R15 @emit_out         ; emit it

	PUSHR R1 R15                ; Protect R1
	LOADUI R0 $equal            ; Using "="
	LOAD32 R1 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @match            ; IF GLOBAL_TOKEN->S == "="
	JUMP.NZ R0 @global_load_done ; Skip the following

	;; Deal with non-assignment
	LOADUI R0 $global_load_string1 ; Our footer string
	CALLI R15 @emit_out         ; emit it

:global_load_done
	POPR R1 R15                 ; Restore R1
	RET R15

:global_load_string0
	"LOAD_IMMEDIATE_eax &GLOBAL_"
:global_load_string1
	"LOAD_INTEGER
"


;; primary_expr_failure function
;; Fails hard and fast
;; Receives nothing
;; HALTs and will trash registers
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
:primary_expr_failure
	LOADUI R0 $primary_expr_failure_string0 ; Our first string
	FALSE R1                    ; Display to User
	CALLI R15 @file_print       ; Print it

	LOAD32 R0 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @file_print       ; Print it

	LOADUI R0 $primary_expr_failure_string1 ; Our last string
	CALLI R15 @file_print       ; Print it

	CALLI R15 @line_error       ; Make it a line error message too
	HALT

:primary_expr_failure_string0
	"Received "
:primary_expr_failure_string1
	" in primary_expr
"


;; primary_expr_string function
;; Receives struct token_list* global_token in R13,
;;	struct token_list* out in R12,
;;	struct token_list* string_list in R11
;;	struct token_list* global_list in R10
;;	and struct token_list* FUNC in R9
;;	and struct token_list* current_target in R8
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns the token_lists modified
:primary_expr_string
	PUSHR R0 R15                ; Protect R0
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	LOADR32 R0 @current_count   ; Using CURRENT_COUNT
	ADDUI R1 R0 1               ; CURRENT_COUNT = CURRENT_COUNT + 1
	STORER32 R1 @current_count  ; Update CURRENT_COUNT
	CALLI R15 @numerate_number  ; Convert to string
	MOVE R2 R0                  ; Put string in safe place
	LOADUI R0 $primary_expr_string_string0 ; Our string prefix
	CALLI R15 @emit_out         ; emit it
	LOAD32 R0 R9 8              ; FUNCTION->S
	COPY R1 R2                  ; NUMBER_STRING
	CALLI R15 @uniqueID_out     ; Make it unique

	;; The target
	LOADUI R0 $primary_expr_string_string1
	COPY R1 R11                 ; Using STRINGS_LIST
	CALLI R15 @emit             ; emit it
	MOVE R1 R0                  ; Put STRINGS_LIST in correct place
	LOAD32 R0 R9 8              ; Using FUNCTION->S
	CALLI R15 @uniqueID         ; Make it unique
	MOVE R11 R0                 ; Update STRINGS_LIST

	;; Parse the string
	LOAD32 R0 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @parse_string     ; Parse it
	COPY R1 R11                 ; Using STRINGS_LIST
	CALLI R15 @emit             ; emit it
	MOVE R11 R0                 ; Update STRINGS_LIST

	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	POPR R0 R15                 ; Restore R0
	RET R15

:primary_expr_string_string0
	"LOAD_IMMEDIATE_eax &STRING_"
:primary_expr_string_string1
	":STRING_"


;; primary_expr_char function
;; Receives struct token_list* global_token in R13,
;;	struct token_list* out in R12,
;;	struct token_list* string_list in R11
;;	struct token_list* global_list in R10
;;	and struct token_list* FUNC in R9
;;	and struct token_list* current_target in R8
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns the token_lists modified
:primary_expr_char
	PUSHR R0 R15                ; Protect R0
	LOADUI R0 $primary_expr_char_string0 ; Using our header string
	CALLI R15 @emit_out         ; emit it

	LOAD32 R0 R13 8             ; GLOBAL_TOKEN->S
	ADDUI R0 R0 1               ; GLOBAL_TOKEN->S + 1
	CALLI R15 @escape_lookup    ; escape_lookup value
	CALLI R15 @numerate_number  ; Make it a string
	CALLI R15 @emit_out         ; emit it

	LOADUI R0 $newline          ; Using "\n"
	CALLI R15 @emit_out         ; emit it

	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	POPR R0 R15                 ; Restore R0
	RET R15

:primary_expr_char_string0
	"LOAD_IMMEDIATE_eax %"


;; primary_expr_number function
;;	struct token_list* out in R12,
;;	struct token_list* string_list in R11
;;	struct token_list* global_list in R10
;;	and struct token_list* FUNC in R9
;;	and struct token_list* current_target in R8
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns the token_lists modified
:primary_expr_number
	LOADUI R0 $primary_expr_number_string0 ; Our header
	CALLI R15 @emit_out         ; emit it
	LOAD32 R0 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @emit_out         ; emit it
	LOADUI R0 $newline          ; Using "\n"
	CALLI R15 @emit_out         ; emit it
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	RET R15

:primary_expr_number_string0
	"LOAD_IMMEDIATE_eax %"


;; primary_expr_variable function
;;	struct token_list* out in R12,
;;	struct token_list* string_list in R11
;;	struct token_list* global_list in R10
;;	and struct token_list* FUNC in R9
;;	and struct token_list* current_target in R8
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns the token_lists modified
:primary_expr_variable
	PUSHR R0 R15                ; Protect R0
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	LOAD32 R2 R13 8             ; S = GLOBAL_TOKEN->S
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT

	COPY R0 R2                  ; Using S
	LOADR32 R1 @global_constant_list
	CALLI R15 @sym_lookup       ; Lookup S in CONSTANTS
	JUMP.Z R0 @primary_expr_variable_locals ; try Locals

	;; Deal with Constants
	CALLI R15 @constant_load    ; A is in R0 already
	JUMP @primary_expr_variable_done ; Moving on

:primary_expr_variable_locals
	COPY R0 R2                  ; Using S
	LOAD32 R1 R9 4              ; Using FUNCTION->LOCALS
	CALLI R15 @sym_lookup       ; Lookup S in Locals
	JUMP.Z R0 @primary_expr_variable_arguments ; try arguments

	;; Deal with Locals
	CALLI R15 @variable_load    ; A is in R0 already
	JUMP @primary_expr_variable_done ; Moving on

:primary_expr_variable_arguments
	COPY R0 R2                  ; Using S
	LOAD32 R1 R9 16             ; Using FUNCTION->ARGUMENTS
	CALLI R15 @sym_lookup       ; Lookup S in arguments
	JUMP.Z R0 @primary_expr_variable_function ; try Functions

	;; Deal with argument
	CALLI R15 @variable_load    ; A is in R0 already
	JUMP @primary_expr_variable_done ; Moving on

:primary_expr_variable_function
	COPY R0 R2                  ; Using S
	LOADR32 R1 @global_function_list ; Get current GLOBAL_FUNCTION_LIST
	CALLI R15 @sym_lookup       ; Lookup S in GLOBAL_FUNCTION_LIST
	JUMP.Z R0 @primary_expr_variable_global ; try Globals

	;; Deal with function
	CALLI R15 @function_load    ; Dothe work
	JUMP @primary_expr_variable_done ; Moving on

:primary_expr_variable_global
	COPY R0 R2                  ; Using S
	LOADR32 R1 @global_symbol_list ; Get current GLOBAL_SYMBOL_LIST
	CALLI R15 @sym_lookup       ; Lookup S in GLOBAL_SYMBOL_LIST
	JUMP.Z R0 @primary_expr_variable_failure ; Looks like it isn't anything we know

	;; Deal with a global
	CALLI R15 @global_load

:primary_expr_variable_done
	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	POPR R0 R15                 ; Restore R0
	RET R15

:primary_expr_variable_failure
	MOVE R0 R2                  ; Using S
	FALSE R1                    ; We want the user to see
	CALLI R15 @file_print       ; Print it

	LOADUI R0 $primary_expr_variable_string0 ; Body
	CALLI R15 @file_print       ; Print it
	CALLI R15 @line_error       ; Provide useful error info
	HALT

:primary_expr_variable_string0
	" is not a defined symbol
"


;; promote_type function
;; Receives struct type* in R0 and struct type* in R1
;; Returns first match struct type* in R0
:promote_type
	JUMP.Z R1 @promote_type_abort0 ; If B is NULL just abort
	PUSHR R1 R15                ; Protect R1
	SWAP R0 R1                  ; Give A a try
	JUMP.Z R1 @promote_type_abort1 ; A is NULL just short abort

	;; Looks like we have a bunch of work to do
	PUSHR R2 R15                ; Protect R2
	PUSHR R3 R15                ; Protect R3
	PUSHR R4 R15                ; Protect R4
	PUSHR R5 R15                ; Protect R5
	MOVE R5 R1                  ; Put A in the right spot
	MOVE R4 R0                  ; Put B in the right spot
	LOADR32 R3 @global_types    ; I = GLOBAL_TYPES

:promote_type_iter
	LOAD32 R1 R3 24             ; I->NAME
	LOAD32 R0 R5 24             ; A->NAME
	CMPJUMPI.E R0 R1 @promote_type_done ; break
	LOAD32 R0 R4 24             ; B->NAME
	CMPJUMPI.E R0 R1 @promote_type_done ; break

	LOAD32 R1 R3 12             ; I->INDIRECT
	LOAD32 R1 R1 24             ; I->INDIRECT->NAME
	LOAD32 R0 R5 24             ; A->NAME
	CMPJUMPI.E R0 R1 @promote_type_done ; break
	LOAD32 R0 R4 24             ; B->NAME
	CMPJUMPI.E R0 R1 @promote_type_done ; break

	LOAD32 R3 R3 0              ; I = I->NEXT
	JUMP.NZ R3 @promote_type_iter ; Loop if not NULL

:promote_type_done
	MOVE R0 R3                  ; Return I
	POPR R5 R15                 ; Restore R5
	POPR R4 R15                 ; Restore R4
	POPR R3 R15                 ; Restore R3
	POPR R2 R15                 ; Restore R2
:promote_type_abort1
	POPR R1 R15                 ; Restore R1
:promote_type_abort0
	RET R15


;; common_recursion function
;; Receives FUNCTION* in R0
;;	struct token_list* out in R12,
;;	struct token_list* string_list in R11
;;	struct token_list* global_list in R10
;;	and struct token_list* FUNC in R9
;;	and struct token_list* current_target in R8
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns the token_lists modified
:common_recursion
	PUSHR R0 R15                ; Protect R0
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	MOVE R2 R0                  ; Protect F
	COPY R1 R8                  ; LAST_TYPE = CURRENT_TARGET
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT

	LOADUI R0 $common_recursion_string0 ; Header string
	CALLI R15 @emit_out         ; Our header

	CALL R2 R15                 ; CALL F()

	COPY R0 R8                  ; Using CURRENT_TARGET
	CALLI R15 @promote_type     ; Promote type
	MOVE R8 R0                  ; update CURRENT_TARGET

	LOADUI R0 $common_recursion_string1 ; Footer string
	CALLI R15 @emit_out         ; Our footer
	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	POPR R0 R15                 ; Restore R0
	RET R15

:common_recursion_string0
	"PUSH_eax	#_common_recursion
"
:common_recursion_string1
	"POP_ebx	# _common_recursion
"


;; general_recursion function
;; Receives FUNCTION F in R0, char* s in R1, char* name in R2
;; and FUNCTION ITERATE in R3
;;	struct token_list* out in R12,
;;	struct token_list* string_list in R11
;;	struct token_list* global_list in R10
;;	and struct token_list* FUNC in R9
;;	and struct token_list* current_target in R8
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns nothing
:general_recursion
	PUSHR R0 R15                ; Protect R0
	PUSHR R1 R15                ; Protect S
	PUSHR R0 R15                ; Protect F
	COPY R0 R2                  ; Using NAME
	LOAD32 R1 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @match            ; IF GLOBAL_TOKEN->S == NAME
	JUMP.Z R0 @general_recursion_done

	;; deal with case of match
	POPR R0 R15                 ; Restore F
	CALLI R15 @common_recursion ; Recurse

	POPR R1 R15                 ; Restore S
	COPY R0 R1                  ; Put S in correct place
	CALLI R15 @emit_out         ; emit it

	CALL R3 R15                 ; CALL ITERATE()
	POPR R0 R15                 ; Restore R0
	RET R15                     ; Don't double pop

:general_recursion_done
	POPR R0 R15                 ; Restore F
	POPR R1 R15                 ; Restore S
	POPR R0 R15                 ; Restore R0
	RET R15


;; ceil_log2 function
;; Receives INT A in R0
;; Returns LOG2(A) in R0
:ceil_log2
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	FALSE R2                    ; RESULT = 0

	SUBI R1 R0 1                ; A - 1
	AND R1 R1 R0                ; A & (A - 1)
	CMPSKIPI.NE R1 0            ; IF (A & (A - 1)) == 0
	LOADI R2 -1                 ; RESULT = -1

:ceil_log2_iter
	JUMP.Z R0 @ceil_log2_done   ; IF A > 0
	ADDI R2 R2 1                ; RESULT = RESULT + 1
	SARI R0 1                   ; A = A >> 1
	JUMP @ceil_log2_iter        ; Loop

:ceil_log2_done
	MOVE R0 R2                  ; Use RESULT
	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	RET R15


;; postfix_expr_arrow function
;; Receives nothing
;;	struct token_list* out in R12,
;;	struct token_list* string_list in R11
;;	struct token_list* global_list in R10
;;	and struct token_list* FUNC in R9
;;	and struct token_list* current_target in R8
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns the token_lists modified
:postfix_expr_arrow
	PUSHR R0 R15                ; Protect R0
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	LOADUI R0 $postfix_expr_arrow_string0 ; Our header string
	CALLI R15 @emit_out         ; Emit it
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT

	COPY R0 R8                  ; Passing CURRENT_TARGET
	LOAD32 R1 R13 8             ; Using GLOBAL_TOKEN->S
	CALLI R15 @lookup_member    ; Look it up
	LOAD32 R2 R0 4              ; Protect I->SIZE
	LOAD32 R8 R0 20             ; CURRENT_TARGET = I->TYPE
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT

	LOAD32 R1 R0 8              ; I->OFFSET
	JUMP.Z R1 @postfix_expr_arrow_offset ; If no offset needed skip the work

	;; Deal with non-zero offsets
	LOADUI R0 $postfix_expr_arrow_string1 ; Our first prefix
	CALLI R15 @emit_out         ; Emit it
	LOADUI R0 $postfix_expr_arrow_string2 ; Our second prefix
	CALLI R15 @emit_out         ; Emit it

	MOVE R0 R1                  ; Put I->OFFSET in the right place
	CALLI R15 @numerate_number  ; Convert to string
	CALLI R15 @emit_out         ; Emit it

	LOADUI R0 $postfix_expr_arrow_string3 ; Our postfix
	CALLI R15 @emit_out         ; Emit it

:postfix_expr_arrow_offset
	LOADUI R0 $equal            ; Using "="
	LOAD32 R1 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @match            ; IF GLOBAL_TOKEN->S == "="
	JUMP.NZ R0 @postfix_expr_arrow_done

	LOADUI R0 4                 ; Compare against 4
	CMPJUMPI.L R0 R2 @postfix_expr_arrow_done

	;; Deal with special case
	LOADUI R0 $postfix_expr_arrow_string4 ; Our final string
	CALLI R15 @emit_out         ; Emit it

:postfix_expr_arrow_done
	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	POPR R0 R15                 ; Restore R0
	RET R15

:postfix_expr_arrow_string0
	"# looking up offset
"
:postfix_expr_arrow_string1
	"# -> offset calculation
"
:postfix_expr_arrow_string2
	"LOAD_IMMEDIATE_ebx %"
:postfix_expr_arrow_string3
	"
ADD_ebx_to_eax
"
:postfix_expr_arrow_string4
	"LOAD_INTEGER
"


;; postfix_expr_array function
;; Receives nothing
;;	struct token_list* out in R12,
;;	struct token_list* string_list in R11
;;	struct token_list* global_list in R10
;;	and struct token_list* FUNC in R9
;;	and struct token_list* current_target in R8
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns the token_lists modified
:postfix_expr_array
	PUSHR R0 R15                ; Protect R0
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	COPY R2 R8                  ; ARRAY = CURRENT_TARGET
	LOADUI R0 $expression       ; Using EXPRESSION
	CALLI R15 @common_recursion ; Recurse
	MOVE R8 R2                  ; CURRENT_TARGET = ARRAY
	LOADUI R2 $postfix_expr_array_string0 ; ASSIGN = load integer

	LOADUI R0 $type_char_indirect_name ; Using "char*"
	LOAD32 R1 R8 24             ; CURRENT_TARGET->NAME
	CALLI R15 @match            ; IF CURRENT_TARGET->NAME == "char*"
	CMPSKIPI.E R0 0             ; deal with Byte
	LOADUI R2 $postfix_expr_array_string1 ; ASSIGN = load byte
	JUMP.NZ R0 @postfix_expr_array_byte ; Skip if Byte

	;; Deal with larger than byte
	LOADUI R0 $postfix_expr_array_string2 ; Our shift
	CALLI R15 @emit_out         ; emit it
	LOAD32 R0 R8 12             ; CURRENT_TARGET->INDIRECT
	LOAD32 R0 R0 4              ; CURRENT_TARGET->INDIRECT->SIZE
	CALLI R15 @ceil_log2        ; LOG2(CURRENT_TARGET->INDIRECT->SIZE)
	CALLI R15 @numerate_number  ; Convert to string
	CALLI R15 @emit_out         ; emit it
	LOADUI R0 $newline          ; Using "\n"
	CALLI R15 @emit_out         ; emit it

:postfix_expr_array_byte
	LOADUI R0 $postfix_expr_array_string3 ; Add the offset
	CALLI R15 @emit_out         ; emit it
	LOADUI R0 $postfix_expr_array_string4 ; Our final error message
	LOADUI R1 $close_bracket    ; Using "]"
	CALLI R15 @require_match    ; Ensure match

	LOADUI R0 $equal            ; Using "="
	LOAD32 R1 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @match            ; IF GLOBAL_TOKEN->S == "="
	CMPSKIPI.E R0 0             ; If match
	LOADUI R2 $postfix_expr_array_string5 ; empty string

	MOVE R0 R2                  ; What ever string survived
	CALLI R15 @emit_out         ; emit it

	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	POPR R0 R15                 ; Restore R0

	RET R15

:postfix_expr_array_string0
	"LOAD_INTEGER
"
:postfix_expr_array_string1
	"LOAD_BYTE
"
:postfix_expr_array_string2
	"SAL_eax_Immediate8 !"
:postfix_expr_array_string3
	"ADD_ebx_to_eax
"
:postfix_expr_array_string4
	"ERROR in postfix_expr
Missing ]
"
:postfix_expr_array_string5
	""


;; postfix_expr_stub function
;; Receives nothing
;;	struct token_list* out in R12,
;;	struct token_list* string_list in R11
;;	struct token_list* global_list in R10
;;	and struct token_list* FUNC in R9
;;	and struct token_list* current_target in R8
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns the token_lists modified
:postfix_expr_stub
	PUSHR R0 R15                ; Protect R0
	PUSHR R1 R15                ; Protect R1

	LOADUI R0 $open_bracket     ; Using "["
	LOAD32 R1 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @match            ; IF GLOBAL_TOKEN->S == "["
	JUMP.Z R0 @postfix_expr_stub_next

	;; Deal with "[" case
	CALLI R15 @postfix_expr_array ; process
	CALLI R15 @postfix_expr_stub ; recurse

:postfix_expr_stub_next
	LOADUI R0 $arrow_string     ; Using "->"
	LOAD32 R1 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @match            ; IF GLOBAL_TOKEN->S == "->"
	JUMP.Z R0 @postfix_expr_stub_done ; clean up

	;; Deal with "->" case
	CALLI R15 @postfix_expr_arrow ; Process
	CALLI R15 @postfix_expr_stub ; recurse

:postfix_expr_stub_done
	POPR R1 R15                 ; Restore R1
	POPR R0 R15                 ; Restore R0
	RET R15


;; postfix_expr function
;;	struct token_list* out in R12,
;;	struct token_list* string_list in R11
;;	struct token_list* global_list in R10
;;	and struct token_list* FUNC in R9
;;	and struct token_list* current_target in R8
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns the token_lists modified
:postfix_expr
	CALLI R15 @primary_expr     ; Walk up the tree
	CALLI R15 @postfix_expr_stub ; Deal with nodes on this level
	RET R15


;; additive_expr_stub function
;; receives nothing
;; returns nothing
;; Updates struct token_list*
:additive_expr_stub
	PUSHR R0 R15                ; Protect R0
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	PUSHR R3 R15                ; Protect R3
	;; Fixed pieces
	LOADUI R0 $postfix_expr     ; Set First argument
	LOADUI R3 $additive_expr_stub

	;; The + bit
	LOADUI R1 $additive_expr_stub_string0 ; Our first operation
	LOADUI R2 $plus_string      ; Using "+"
	CALLI R15 @general_recursion

	;; The - bit
	LOADUI R1 $additive_expr_stub_string1 ; Our second operation
	LOADUI R2 $minus_string     ; Using "-"
	CALLI R15 @general_recursion

	;; The * bit
	LOADUI R1 $additive_expr_stub_string2 ; Our third operation
	LOADUI R2 $multiply_string  ; Using "*"
	CALLI R15 @general_recursion

	;; The / bit
	LOADUI R1 $additive_expr_stub_string3 ; Our fourth operation
	LOADUI R2 $divide_string    ; Using "/"
	CALLI R15 @general_recursion

	;; The % bit
	LOADUI R1 $additive_expr_stub_string4 ; Our fifth operation
	LOADUI R2 $modulus_string   ; Using "%"
	CALLI R15 @general_recursion

	;; The << bit
	LOADUI R1 $additive_expr_stub_string5 ; Our sixth operation
	LOADUI R2 $left_shift_string ; Using "<<"
	CALLI R15 @general_recursion

	;; The >> bit
	LOADUI R1 $additive_expr_stub_string6 ; Our final operation
	LOADUI R2 $right_shift_string ; Using ">>"
	CALLI R15 @general_recursion

	POPR R3 R15                 ; Restore R3
	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	POPR R0 R15                 ; Restore R0
	RET R15

:additive_expr_stub_string0
	"ADD_ebx_to_eax
"
:additive_expr_stub_string1
	"SUBTRACT_eax_from_ebx_into_ebx
MOVE_ebx_to_eax
"
:additive_expr_stub_string2
	"MULTIPLY_eax_by_ebx_into_eax
"
:additive_expr_stub_string3
	"XCHG_eax_ebx
LOAD_IMMEDIATE_edx %0
DIVIDE_eax_by_ebx_into_eax
"
:additive_expr_stub_string4
	"XCHG_eax_ebx
LOAD_IMMEDIATE_edx %0
MODULUS_eax_from_ebx_into_ebx
MOVE_edx_to_eax
"
:additive_expr_stub_string5
	"COPY_eax_to_ecx
COPY_ebx_to_eax
SAL_eax_cl
"
:additive_expr_stub_string6
	"COPY_eax_to_ecx
COPY_ebx_to_eax
SAR_eax_cl
"


;; additive_expr function
;; Receives struct token_list* global_token in R13,
;;	struct token_list* out in R12,
;;	struct token_list* string_list in R11
;;	struct token_list* global_list in R10
;;	and struct token_list* FUNC in R9
;;	and struct token_list* current_target in R8
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns the token_lists modified
:additive_expr
	CALLI R15 @postfix_expr     ; Walk up the tree
	CALLI R15 @additive_expr_stub ; Deal with nodes at this level
	RET R15


;; relational_expr_stub function
;; receives nothing
;; returns nothing
;; Updates struct token_list*
:relational_expr_stub
	PUSHR R0 R15                ; Protect R0
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	PUSHR R3 R15                ; Protect R3
	;; Fixed pieces
	LOADUI R0 $additive_expr     ; Set First argument
	LOADUI R3 $relational_expr_stub

	;; The < bit
	LOADUI R1 $relational_expr_stub_string0 ; Our first operation
	LOADUI R2 $less_than_string ; Using "<"
	CALLI R15 @general_recursion

	;; The <= bit
	LOADUI R1 $relational_expr_stub_string1 ; Our second operation
	LOADUI R2 $less_than_equal_string ; Using "<="
	CALLI R15 @general_recursion

	;; The >= bit
	LOADUI R1 $relational_expr_stub_string2 ; Our third operation
	LOADUI R2 $greater_than_equal_string ; Using ">="
	CALLI R15 @general_recursion

	;; The > bit
	LOADUI R1 $relational_expr_stub_string3 ; Our fourth operation
	LOADUI R2 $greater_than_string ; Using ">"
	CALLI R15 @general_recursion

	;; The == bit
	LOADUI R1 $relational_expr_stub_string4 ; Our fifth operation
	LOADUI R2 $equal_to_string  ; Using "=="
	CALLI R15 @general_recursion

	;; The != bit
	LOADUI R1 $relational_expr_stub_string5 ; Our final operation
	LOADUI R2 $not_equal_string ; Using "!="
	CALLI R15 @general_recursion

	POPR R3 R15                 ; Restore R3
	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	POPR R0 R15                 ; Restore R0
	RET R15

:relational_expr_stub_string0
	"CMP
SETL
MOVEZBL
"
:relational_expr_stub_string1
	"CMP
SETLE
MOVEZBL
"
:relational_expr_stub_string2
	"CMP
SETGE
MOVEZBL
"
:relational_expr_stub_string3
	"CMP
SETG
MOVEZBL
"
:relational_expr_stub_string4
	"CMP
SETE
MOVEZBL
"
:relational_expr_stub_string5
	"CMP
SETNE
MOVEZBL
"


;; relational_expr function
;; Receives struct token_list* global_token in R13,
;;	struct token_list* out in R12,
;;	struct token_list* string_list in R11
;;	struct token_list* global_list in R10
;;	and struct token_list* FUNC in R9
;;	and struct token_list* current_target in R8
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns the token_lists modified
:relational_expr
	CALLI R15 @additive_expr    ; Walk up the tree
	CALLI R15 @relational_expr_stub ; Deal with nodes at this level
	RET R15


;; relational_expr_stub function
;; receives nothing
;; returns nothing
;; Updates struct token_list*
:bitwise_expr_stub
	PUSHR R0 R15                ; Protect R0
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	PUSHR R3 R15                ; Protect R3
	;; Fixed pieces
	LOADUI R0 $relational_expr  ; Set First argument
	LOADUI R3 $bitwise_expr_stub

	;; The & bit
	LOADUI R1 $bitwise_expr_stub_string0 ; Our first operation
	LOADUI R2 $bitwise_and      ; Using "&"
	CALLI R15 @general_recursion

	;; The && bit
	LOADUI R1 $bitwise_expr_stub_string0 ; Our first operation
	LOADUI R2 $logical_and      ; Using "&&"
	CALLI R15 @general_recursion

	;; The | bit
	LOADUI R1 $bitwise_expr_stub_string1 ; Our second operation
	LOADUI R2 $bitwise_or       ; Using "|"
	CALLI R15 @general_recursion

	;; The || bit
	LOADUI R1 $bitwise_expr_stub_string1 ; Our second operation
	LOADUI R2 $logical_or       ; Using "||"
	CALLI R15 @general_recursion

	;; The ^ bit
	LOADUI R1 $bitwise_expr_stub_string2 ; Our second operation
	LOADUI R2 $bitwise_xor       ; Using "^"
	CALLI R15 @general_recursion

	POPR R3 R15                 ; Restore R3
	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	POPR R0 R15                 ; Restore R0
	RET R15

:bitwise_expr_stub_string0
	"AND_eax_ebx
"
:bitwise_expr_stub_string1
	"OR_eax_ebx
"
:bitwise_expr_stub_string2
	"XOR_ebx_eax_into_eax
"


;; bitwise_expr function
;; Receives struct token_list* global_token in R13,
;;	struct token_list* out in R12,
;;	struct token_list* string_list in R11
;;	struct token_list* global_list in R10
;;	and struct token_list* FUNC in R9
;;	and struct token_list* current_target in R8
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns the token_lists modified
:bitwise_expr
	CALLI R15 @relational_expr  ; Walk up the tree
	CALLI R15 @bitwise_expr_stub ; Deal with nodes at this level
	RET R15


;; primary_expr function
;; Receives struct token_list* global_token in R13,
;;	struct token_list* out in R12,
;;	struct token_list* string_list in R11
;;	struct token_list* global_list in R10
;;	and struct token_list* FUNC in R9
;;	and struct token_list* current_target in R8
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns the token_lists modified
:primary_expr
	PUSHR R0 R15                ; Protect R0
	PUSHR R1 R15                ; Protect R1
	LOADUI R0 $sizeof_string    ; Load "sizeof"
	LOAD32 R1 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @match            ; IF GLOBAL_TOKEN->S == "sizeof"
	JUMP.Z R0 @primary_expr_negate ; Guess not

	;; Deal with sizeof expression
	CALLI R15 @unary_expr_sizeof ; Do real work
	JUMP @primary_expr_done     ; Wrap up

:primary_expr_negate
	LOADU8 R0 R1 0              ; GLOBAL_TOKEN->S[0]
	CMPSKIPI.E R0 45            ; IF GLOBAL_TOKEN->S[0] == '-'
	JUMP @primary_expr_bang     ; If not try '!'

	;; Deal with -a and -4 expressions
	LOADUI R0 $primary_expr_str0 ; Load HEADER
	CALLI R15 @emit_out         ; emit it
	LOADUI R0 $primary_expr     ; Using PRIMARY_EXPR
	CALLI R15 @common_recursion ; Recurse
	LOADUI R0 $primary_expr_str1 ; add footer
	CALLI R15 @emit_out         ; emit it

	JUMP @primary_expr_done     ; Wrap up

:primary_expr_bang
	CMPSKIPI.E R0 33            ; IF GLOBAL_TOKEN->S[0] == "!"
	JUMP @primary_expr_nested   ; If not try '('

	;; deal with !a expressions
	LOADUI R0 $primary_expr_str2 ; Load HEADER
	CALLI R15 @emit_out         ; emit it
	LOADUI R0 $postfix_expr     ; Using POSTFIX_EXPR
	CALLI R15 @common_recursion ; Recurse
	LOADUI R0 $primary_expr_str3 ; add footer
	CALLI R15 @emit_out         ; emit it

	JUMP @primary_expr_done     ; Wrap up

:primary_expr_nested
	CMPSKIPI.E R0 40            ; IF GLOBAL_TOKEN->S[0] == '('
	JUMP @primary_expr_ch       ; If not try 'char'

	;; Deal with ( expr )
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	CALLI R15 @expression       ; Recurse

	LOADUI R0 $primary_expr_str4 ; Using error message
	LOADUI R1 $close_paren      ; Using ")"
	CALLI R15 @require_match    ; Make sure we have closing match

	JUMP @primary_expr_done     ; Wrap up

:primary_expr_ch
	CMPSKIPI.E R0 39            ; IF GLOBAL_TOKEN->S[0] == '\''
	JUMP @primary_expr_st       ; If not try "string"

	;; Deal with 'char'
	CALLI R15 @primary_expr_char ; Collect char
	JUMP @primary_expr_done     ; Wrap up

:primary_expr_st
	CMPSKIPI.E R0 34            ; IF GLOBAL_TOKEN->S[0] == '"'
	JUMP @primary_expr_var      ; If not try variables

	;; deal with "string"
	CALLI R15 @primary_expr_string ; Collect string
	JUMP @primary_expr_done     ; Wrap up

:primary_expr_var
	LOADUI R1 $variable_chars   ; Using a-z+A-Z+_
	CALLI R15 @in_set           ; IF GLOBAL_TOKEN->S[0] in a-z+A-Z+_
	JUMP.Z R0 @primary_expr_num

	;; Deal with foo TODO
	CALLI R15 @primary_expr_variable ; deal with names
	JUMP @primary_expr_done     ; Wrap up

:primary_expr_num
	LOAD32 R0 R13 8             ; GLOBAL_TOKEN->S
	LOADU8 R0 R0 0              ; GLOBAL_TOKEN->S[0]
	LOADUI R1 $digit_chars      ; Using 0-9
	CALLI R15 @in_set           ; IF GLOBAL_TOKEN->S[0] in 0-9
	JUMP.Z R0 @primary_expr_failure ; Fail HARD

	;; Deal with 5
	CALLI R15 @primary_expr_number ; deal with number

:primary_expr_done
	POPR R1 R15                 ; Restore R1
	POPR R0 R15                 ; Restore R0
	RET R15

:primary_expr_str0
	"LOAD_IMMEDIATE_eax %0
"
:primary_expr_str1
	"SUBTRACT_eax_from_ebx_into_ebx
MOVE_ebx_to_eax
"
:primary_expr_str2
	"LOAD_IMMEDIATE_eax %1
"
:primary_expr_str3
	"XOR_ebx_eax_into_eax
"
:primary_expr_str4
	"Error in Primary expression
Didn't get )
"


;; expression function
;; Receives struct token_list* global_token in R13,
;;	struct token_list* out in R12,
;;	struct token_list* string_list in R11
;;	struct token_list* global_list in R10
;;	and struct token_list* FUNC in R9
;;	and struct token_list* current_target in R8
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns the token_lists modified
:expression
	PUSHR R0 R15                ; Protect R0
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	PUSHR R3 R15                ; Protect R3
	CALLI R15 @bitwise_expr     ; Check for more primitives first
	LOADUI R0 $equal            ; Using "="
	LOAD32 R1 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @match            ; IF GLOBAL_TOKEN->S == "="
	JUMP.Z R0 @expression_done  ; Be done

	;; Determine store type
	LOADUI R3 $expression_string1 ; Assuming the default of STORE CHAR

	;; First possible reason for INT
	LOADUI R0 $close_bracket    ; Using "]"
	LOAD32 R1 R13 4             ; GLOBAL_TOKEN->PREV
	LOAD32 R1 R1 8              ; GLOBAL_TOKEN->PREV->S
	CALLI R15 @match            ; IF GLOBAL_TOKEN->PREV-> == "]"
	CMPSKIPI.NE R0 0            ; IF FALSE
	LOADUI R3 $expression_string0 ; STORE INTEGER

	;; Second possible reason for INTeger
	LOADUI R0 $type_char_indirect_name ; Using "char*"
	LOAD32 R1 R8 24             ; CURRENT_TARGET->NAME
	CALLI R15 @match            ; IF CURRENT_TARGET->NAME == "char*"
	CMPSKIPI.NE R0 0            ; IF FALSE
	LOADUI R3 $expression_string0 ; STORE INTEGER

	;; Recurse to evaluate expression being stored
	LOADUI R0 $expression       ; Using expression
	CALLI R15 @common_recursion ; Perform common recursion

	;; Put our string and clean up
	MOVE R0 R3                  ; Using our STORED string
	CALLI R15 @emit_out         ; emit it

	FALSE R8                    ; CURRENT_TARGET = NULL

:expression_done
	POPR R3 R15                 ; Restore R3
	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	POPR R0 R15                 ; Restore R0
	RET R15

:expression_string0
	"STORE_INTEGER
"
:expression_string1
	"STORE_CHAR
"

;; process_if function
;;	struct token_list* out in R12,
;;	struct token_list* string_list in R11
;;	struct token_list* global_list in R10
;;	and struct token_list* FUNC in R9
;;	and struct token_list* current_target in R8
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns the token_lists modified
:process_if
	PUSHR R0 R15                ; Protect R0
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	LOADR32 R0 @current_count   ; Using CURRENT_COUNT
	ADDUI R1 R0 1               ; CURRENT_COUNT = CURRENT_COUNT + 1
	STORER32 R1 @current_count  ; Update CURRENT_COUNT
	CALLI R15 @numerate_number  ; Convert CURRENT_COUNT to string
	MOVE R2 R0                  ; Protect our string

	LOADUI R0 $process_if_string0 ; using first string
	CALLI R15 @emit_out         ; emit it
	LOAD32 R0 R9 8              ; FUNCTION->S
	COPY R1 R2                  ; Using our current count string
	CALLI R15 @uniqueID_out     ; Add unique identifier

	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	LOADUI R0 $process_if_string1 ; Our first error message
	LOADUI R1 $open_paren       ; Using "("
	CALLI R15 @require_match    ; Make sure we have what we need
	CALLI R15 @expression       ; Recurse to get our boolean expression

	LOADUI R0 $process_if_string2 ; Our test and jump
	CALLI R15 @emit_out         ; emit it
	LOAD32 R0 R9 8              ; FUNCTION->S
	COPY R1 R2                  ; Using our current count string
	CALLI R15 @uniqueID_out     ; Add unique identifier

	LOADUI R0 $process_if_string3 ; Our second error message
	LOADUI R1 $close_paren      ; Using ")"
	CALLI R15 @require_match    ; Make sure we have what we need
	CALLI R15 @statement        ; Collect our if statement

	LOADUI R0 $process_if_string4 ; Our jump over else
	CALLI R15 @emit_out         ; emit it
	LOAD32 R0 R9 8              ; FUNCTION->S
	COPY R1 R2                  ; Using our current count string
	CALLI R15 @uniqueID_out     ; Add unique identifier
	LOADUI R0 $process_if_string5 ; Our else label
	CALLI R15 @emit_out         ; emit it
	LOAD32 R0 R9 8              ; FUNCTION->S
	CALLI R15 @uniqueID_out     ; Add unique identifier

	LOADUI R0 $else_string      ; Using "else"
	LOAD32 R1 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @match            ; IF GLOBAL_TOKEN->S == "else"
	JUMP.Z R0 @process_if_else  ; Looks like no else

	;; Deal with else
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN =  GLOBAL_TOKEN->NEXT
	CALLI R15 @statement        ; Grab else statement

:process_if_else
	LOADUI R0 $process_if_string6 ; Our jump over else
	CALLI R15 @emit_out         ; emit it
	LOAD32 R0 R9 8              ; FUNCTION->S
	COPY R1 R2                  ; Using our current count string
	CALLI R15 @uniqueID_out     ; Add unique identifier

	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	POPR R0 R15                 ; Restore R0
	RET R15

:process_if_string0
	"# IF_"
:process_if_string1
	"ERROR in process_if
MISSING (
"
:process_if_string2
	"TEST
JUMP_EQ %ELSE_"
:process_if_string3
	"ERROR in process_if
MISSING )
"
:process_if_string4
	"JUMP %_END_IF_"
:process_if_string5
	":ELSE_"
:process_if_string6
	":_END_IF_"


;; save_break_frame microfunction
;; Overwrites R0 and R1
;; Saves break frame on stack
;; Returns to caller
:save_break_frame
	POPR R1 R15                 ; Save return address
	LOADR32 R0 @break_frame     ; Obtain BREAK_FRAME
	PUSHR R0 R15                ; Protect BREAK_FRAME
	LOADR32 R0 @break_target_head ; obtain HEAD
	PUSHR R0 R15                ; Protect HEAD
	LOADR32 R0 @break_target_func ; obtain FUNC
	PUSHR R0 R15                ; Protect FUNC
	LOADR32 R0 @break_target_num ; obtain NUM
	PUSHR R0 R15                ; Protect NUM
	PUSHR R1 R15                ; Set where we are returning to
	RET R15


;; restore_break_frame microfunction
;; Overwrites R0 and R1
;; Restores break frame from stack
;; Returns to caller
:restore_break_frame
	POPR R1 R15                 ; Save return address
	POPR R0 R15                 ; obtain NUM
	STORER32 R0 @break_target_num ; Restore NUM
	POPR R0 R15                 ; obtain FUNC
	STORER32 R0 @break_target_func ; Restore FUNC
	POPR R0 R15                 ; obtain HEAD
	STORER32 R0 @break_target_head ; Restore HEAD
	POPR R0 R15                 ; obtain BREAK_FRAME
	STORER32 R0 @break_frame    ; Restore BREAK_FRAME
	PUSHR R1 R15                ; Set where we are returning to
	RET R15


;; set_break_frame microfunction
;; Receives char* num in R0, char* head in R1
;; Overwrites R0
;; Sets break frame using
;; R9 holding FUNC
;; Returns to calling function
:set_break_frame
	STORER32 R1 @break_target_head ; update BREAK_TARGET_HEAD
	STORER32 R0 @break_target_num ; Update BREAK_TARGET_NUM
	LOAD32 R0 R9 4              ; Using FUNCTION->LOCALS
	STORER32 R0 @break_frame    ; update BREAK_FRAME
	LOAD32 R0 R9 8              ; Using FUNCTION->S
	STORER32 R0 @break_target_func ; update BREAK_TARGET_FUNC
	RET R15


;; process_for function
;;	struct token_list* out in R12,
;;	struct token_list* string_list in R11
;;	struct token_list* global_list in R10
;;	and struct token_list* FUNC in R9
;;	and struct token_list* current_target in R8
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns the token_lists modified
:process_for
	PUSHR R2 R15                ; Protect R2
	PUSHR R1 R15                ; Protect R1
	PUSHR R0 R15                ; Protect R0
	CALLI R15 @save_break_frame ; Save break frame

	LOADR32 R0 @current_count   ; Using CURRENT_COUNT
	ADDUI R1 R0 1               ; CURRENT_COUNT = CURRENT_COUNT + 1
	STORER32 R1 @current_count  ; Update CURRENT_COUNT
	CALLI R15 @numerate_number  ; Convert to string
	COPY R2 R0                  ; Protect NUMBER_STRING

	LOADUI R1 $process_for_string0 ; Get new HEAD
	CALLI R15 @set_break_frame  ; Set the break frame values

	LOADUI R0 $process_for_string1 ; Our comment header
	CALLI R15 @emit_out         ; emit it
	COPY R1 R2                  ; Using NUMBER_STRING
	LOAD32 R0 R9 8              ; FUNCTION->S
	CALLI R15 @uniqueID_out     ; emit it

	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT

	LOADUI R0 $process_for_string2 ; Our first error message
	LOADUI R1 $open_paren       ; Using "("
	CALLI R15 @require_match    ; Verify match
	LOADUI R0 $semicolon        ; Using ";"
	LOAD32 R1 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @match            ; IF GLOBAL_TOKEN->S -- ";"
	CMPSKIPI.NE R0 0            ; If GLOBAL_TOKEN->S != ";"
	CALLI R15 @expression       ; Skip that step

	LOADUI R0 $process_for_string3 ; Our comment header
	CALLI R15 @emit_out         ; emit it
	COPY R1 R2                  ; Using NUMBER_STRING
	LOAD32 R0 R9 8              ; FUNCTION->S
	CALLI R15 @uniqueID_out     ; emit it

	LOADUI R0 $process_for_string4 ; Our second error message
	LOADUI R1 $semicolon        ; Using ";"
	CALLI R15 @require_match    ; Verify match
	CALLI R15 @expression       ; TEST logic required

	LOADUI R0 $process_for_string5 ; Our comment header
	CALLI R15 @emit_out         ; emit it
	COPY R1 R2                  ; Using NUMBER_STRING
	LOAD32 R0 R9 8              ; FUNCTION->S
	CALLI R15 @uniqueID_out     ; emit it
	LOADUI R0 $process_for_string6 ; Our comment header
	CALLI R15 @emit_out         ; emit it
	LOAD32 R0 R9 8              ; FUNCTION->S
	CALLI R15 @uniqueID_out     ; emit it
	LOADUI R0 $process_for_string7 ; Our comment header
	CALLI R15 @emit_out         ; emit it
	LOAD32 R0 R9 8              ; FUNCTION->S
	CALLI R15 @uniqueID_out     ; emit it

	LOADUI R0 $process_for_string8 ; Our third error message
	LOADUI R1 $semicolon        ; Using ";"
	CALLI R15 @require_match    ; Verify match
	CALLI R15 @expression       ; Iterator logic

	LOADUI R0 $process_for_string9 ; Our comment header
	CALLI R15 @emit_out         ; emit it
	COPY R1 R2                  ; Using NUMBER_STRING
	LOAD32 R0 R9 8              ; FUNCTION->S
	CALLI R15 @uniqueID_out     ; emit it
	LOADUI R0 $process_for_string10 ; Our comment header
	CALLI R15 @emit_out         ; emit it
	LOAD32 R0 R9 8              ; FUNCTION->S
	CALLI R15 @uniqueID_out     ; emit it

	LOADUI R0 $process_for_string11 ; Our final error message
	LOADUI R1 $close_paren      ; Using ")"
	CALLI R15 @require_match    ; Verify match
	CALLI R15 @statement        ; Main body

	LOADUI R0 $process_for_string12 ; Our comment header
	CALLI R15 @emit_out         ; emit it
	COPY R1 R2                  ; Using NUMBER_STRING
	LOAD32 R0 R9 8              ; FUNCTION->S
	CALLI R15 @uniqueID_out     ; emit it
	LOADUI R0 $process_for_string13 ; Our comment header
	CALLI R15 @emit_out         ; emit it
	LOAD32 R0 R9 8              ; FUNCTION->S
	CALLI R15 @uniqueID_out     ; emit it

	CALLI R15 @restore_break_frame ; Restore break frame
	POPR R0 R15                 ; Restore R0
	POPR R1 R15                 ; Restore R1
	POPR R2 R15                 ; Restore R2
	RET R15

:process_for_string0
	"FOR_END_"
:process_for_string1
	"# FOR_initialization_"
:process_for_string2
	"ERROR in process_for
MISSING (
"
:process_for_string3
	":FOR_"
:process_for_string4
	"ERROR in process_for
MISSING ;1
"
:process_for_string5
	"TEST
JUMP_EQ %FOR_END_"
:process_for_string6
	"JUMP %FOR_THEN_"
:process_for_string7
	":FOR_ITER_"
:process_for_string8
	"ERROR in process_for
MISSING ;2
"
:process_for_string9
	"JUMP %FOR_"
:process_for_string10
	":FOR_THEN_"
:process_for_string11
	"ERROR in process_for
MISSING )
"
:process_for_string12
	"JUMP %FOR_ITER_"
:process_for_string13
	":FOR_END_"


;; process_do function
;;	struct token_list* out in R12,
;;	struct token_list* string_list in R11
;;	struct token_list* global_list in R10
;;	and struct token_list* FUNC in R9
;;	and struct token_list* current_target in R8
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns the token_lists modified
:process_do
	PUSHR R2 R15                ; Protect R2
	PUSHR R1 R15                ; Protect R1
	PUSHR R0 R15                ; Protect R0
	CALLI R15 @save_break_frame ; Save break frame

	LOADR32 R0 @current_count   ; Using CURRENT_COUNT
	ADDUI R1 R0 1               ; CURRENT_COUNT = CURRENT_COUNT + 1
	STORER32 R1 @current_count  ; Update CURRENT_COUNT
	CALLI R15 @numerate_number  ; Convert to string
	COPY R2 R0                  ; Protect NUMBER_STRING

	LOADUI R1 $process_do_string0 ; Using our desired head
	CALLI R15 @set_break_frame  ; Set the break frame values

	LOADUI R0 $process_do_string1 ; Our label
	CALLI R15 @emit_out         ; emit it
	COPY R1 R2                  ; Using NUMBER_STRING
	LOAD32 R0 R9 8              ; FUNCTION->S
	CALLI R15 @uniqueID_out     ; emit it

	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	CALLI R15 @statement        ; Collect our Do statement

	LOADUI R0 $process_do_string2 ; our first error message
	LOADUI R1 $while_string     ; Using "while"
	CALLI R15 @require_match    ; Check for match
	LOADUI R0 $process_do_string3 ; our second error message
	LOADUI R1 $open_paren       ; Using "("
	CALLI R15 @require_match    ; Check for match
	CALLI R15 @expression       ; Our logical expression
	LOADUI R0 $process_do_string4 ; our third error message
	LOADUI R1 $close_paren      ; Using ")"
	CALLI R15 @require_match    ; Check for match
	LOADUI R0 $process_do_string5 ; our final error message
	LOADUI R1 $semicolon        ; Using ";"
	CALLI R15 @require_match    ; Check for match

	LOADUI R0 $process_do_string6 ; Our test string
	CALLI R15 @emit_out         ; emit it
	COPY R1 R2                  ; Put NUMBER_STRING in right place
	LOAD32 R0 R9 8              ; FUNCTION->S
	CALLI R15 @uniqueID_out     ; emit it
	LOADUI R0 $process_do_string7 ; Our end label string
	CALLI R15 @emit_out         ; emit it
	LOAD32 R0 R9 8              ; FUNCTION->S
	CALLI R15 @uniqueID_out     ; emit it

	CALLI R15 @restore_break_frame ; Restore break frame
	POPR R0 R15                 ; Restore R0
	POPR R1 R15                 ; Restore R1
	POPR R2 R15                 ; Restore R2
	RET R15

:process_do_string0
	"DO_END_"
:process_do_string1
	":DO_"
:process_do_string2
	"ERROR in process_do
MISSING while
"
:process_do_string3
	"ERROR in process_do
MISSING (
"
:process_do_string4
	"ERROR in process_do
MISSING )
"
:process_do_string5
	"ERROR in process_do
MISSING ;
"
:process_do_string6
	"TEST
JUMP_NE %DO_"
:process_do_string7
	":DO_END_"


;; process_while function
;;	struct token_list* out in R12,
;;	struct token_list* string_list in R11
;;	struct token_list* global_list in R10
;;	and struct token_list* FUNC in R9
;;	and struct token_list* current_target in R8
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns the token_lists modified
:process_while
	PUSHR R2 R15                ; Protect R2
	PUSHR R1 R15                ; Protect R1
	PUSHR R0 R15                ; Protect R0
	CALLI R15 @save_break_frame ; Save break frame

	LOADR32 R0 @current_count   ; Using CURRENT_COUNT
	ADDUI R1 R0 1               ; CURRENT_COUNT = CURRENT_COUNT + 1
	STORER32 R1 @current_count  ; Update CURRENT_COUNT
	CALLI R15 @numerate_number  ; Convert to string
	COPY R2 R0                  ; Protect NUMBER_STRING

	LOADUI R1 $process_while_string0 ; Set HEAD
	CALLI R15 @set_break_frame ; Set the break frame values

	LOADUI R0 $process_while_string1 ; Our head label
	CALLI R15 @emit_out         ; emit it
	COPY R1 R2                  ; Using NUMBER_STRING
	LOAD32 R0 R9 8              ; FUNCTION->S
	CALLI R15 @uniqueID_out     ; emit it

	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	LOADUI R0 $process_while_string2 ; Our first error message
	LOADUI R1 $open_paren       ; Using "("
	CALLI R15 @require_match    ; Check for match
	CALLI R15 @expression       ; Collect test expression

	LOADUI R0 $process_while_string3 ; Our test and jump
	CALLI R15 @emit_out         ; emit it
	COPY R1 R2                  ; Using NUMBER_STRING
	LOAD32 R0 R9 8              ; FUNCTION->S
	CALLI R15 @uniqueID_out     ; emit it
	LOADUI R0 $process_while_string4 ; Our trailing comment
	CALLI R15 @emit_out         ; emit it
	LOAD32 R0 R9 8              ; FUNCTION->S
	CALLI R15 @uniqueID_out     ; emit it

	LOADUI R0 $process_while_string5 ; Our first error message
	LOADUI R1 $close_paren      ; Using ")"
	CALLI R15 @require_match    ; Check for match
	CALLI R15 @statement        ; Collect our loop statement

	LOADUI R0 $process_while_string6 ; Our test and jump
	CALLI R15 @emit_out         ; emit it
	COPY R1 R2                  ; Using NUMBER_STRING
	LOAD32 R0 R9 8              ; FUNCTION->S
	CALLI R15 @uniqueID_out     ; emit it
	LOADUI R0 $process_while_string7 ; Our trailing comment
	CALLI R15 @emit_out         ; emit it
	LOAD32 R0 R9 8              ; FUNCTION->S
	CALLI R15 @uniqueID_out     ; emit it

	CALLI R15 @restore_break_frame ; Restore break frame
	POPR R0 R15                 ; Restore R0
	POPR R1 R15                 ; Restore R1
	POPR R2 R15                 ; Restore R2
	RET R15

:process_while_string0
	"END_WHILE_"
:process_while_string1
	":WHILE_"
:process_while_string2
	"ERROR in process_while
MISSING (
"
:process_while_string3
	"TEST
JUMP_EQ %END_WHILE_"
:process_while_string4
	"# THEN_while_"
:process_while_string5
	"ERROR in process_while
MISSING )
"
:process_while_string6
	"JUMP %WHILE_"
:process_while_string7
	":END_WHILE_"


;; return_result function
;; Receives nothing
;; Returns nothing
;;	and struct token_list* FUNC in R9
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
:return_result
	PUSHR R0 R15                ; Protect R0
	PUSHR R1 R15                ; Protect R1
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	LOAD32 R0 R13 8             ; GLOBAL_TOKEN->S
	LOADU8 R0 R0 0              ; GLOBAL_TOKEN->S[0]
	CMPSKIPI.E R0 59            ; IF GLOBAL_TOKEN->S[0] == ';'
	CALLI R15 @expression       ; Evaluate expression

	LOADUI R0 $return_result_string0 ; Using or error message
	LOADUI R1 $semicolon        ; Using ";"
	CALLI R15 @require_match    ; Require a match to ";"

	LOADUI R0 $return_result_string1 ; Our pop command
	LOAD32 R1 R9 4              ; FUNCTION->LOCALS

:return_result_iter
	JUMP.Z R1 @return_result_done ; Be done when we hit NULL
	CALLI R15 @emit_out         ; Put the string every iteration
	LOAD32 R1 R1 0              ; I = I->NEXT
	JUMP @return_result_iter    ; Keep looping

:return_result_done
	LOADUI R0 $return_result_string2 ; Our footer
	CALLI R15 @emit_out         ; emit it
	POPR R1 R15                 ; Restore R1
	POPR R0 R15                 ; Restore R0
	RET R15

:return_result_string0
	"ERROR in return_result
MISSING ;
"
:return_result_string1
	"POP_ebx	# _return_result_locals
"
:return_result_string2
	"RETURN
"


;; process_break function
;; Receives nothing
;;	and struct token_list* FUNC in R9
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns the token_lists modified
:process_break
	PUSHR R0 R15                ; Protect R0
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	LOADR32 R0 @break_target_head ; BREAK_TARGET_HEAD
	JUMP.NZ R0 @process_break_NON_NULL

	;; Deal with NULL == BREAK_TARGET_HEAD
	LOADUI R0 $process_break_string0 ; Our first error message
	FALSE R1                    ; Write for User
	CALLI R15 @file_print       ; write it
	CALLI R15 @line_error       ; Give useful info
	LOADUI R0 $newline          ; Using "\n"
	CALLI R15 @file_print       ; Print it
	HALT

:process_break_NON_NULL
	LOADR32 R2 @break_frame     ; BREAK_FRAME
	LOAD32 R1 R9 4              ; I = FUNCTION->LOCALS
	LOADUI R0 $process_break_string1 ; Our pop string

:process_break_iter
	CMPJUMPI.E R1 R2 @process_break_done ; IF I == BREAK_FRAME
	JUMP.Z R1 @process_break_done ; IF NULL == I break
	CALLI R15 @emit_out         ; emit pop
	LOAD32 R1 R1 0              ; I = I->NEXT
	JUMP @process_break_iter    ; Loop

:process_break_done
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	LOADUI R0 $process_break_string2 ; Our jump string
	CALLI R15 @emit_out         ; emit it
	LOADR32 R0 @break_target_head ; Our HEAD string
	CALLI R15 @emit_out         ; emit it
	LOADR32 R0 @break_target_func ; Our FUNC string
	CALLI R15 @emit_out         ; emit it
	LOADUI R0 $underline        ; Using "_"
	CALLI R15 @emit_out         ; emit it
	LOADR32 R0 @break_target_num ; Our NUM string
	CALLI R15 @emit_out         ; emit it
	LOADUI R0 $newline          ; Using "\n"
	CALLI R15 @emit_out         ; emit it

	LOADUI R0 $process_break_string3 ; Our final error string
	LOADUI R1 $semicolon        ; Using ";"
	CALLI R15 @require_match    ; Make sure we get that match

	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	POPR R0 R15                 ; Restore R0
	RET R15

:process_break_string0
	"Not inside of a loop or case statement
"
:process_break_string1
	"POP_ebx	# break_cleanup_locals
"
:process_break_string2
	"JUMP %"
:process_break_string3
	"ERROR in break statement
Missing ;
"

:break_frame
	NOP
:break_target_head
	NOP
:break_target_func
	NOP
:break_target_num
	NOP


;; process_asm function
;; Receives struct token_list* global_token in R13,
;;	struct token_list* out in R12,
;;	struct token_list* string_list in R11
;;	struct token_list* global_list in R10
;;	and struct token_list* FUNC in R9
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns the token_lists modified
:process_asm
	PUSHR R0 R15                ; Protect R0
	PUSHR R1 R15                ; Protect R1
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT

	;; First required match
	LOADUI R0 $process_asm_string0 ; Using our First error message
	LOADUI R1 $open_paren       ; Using "("
	CALLI R15 @require_match    ; Make sure of our required match

:process_asm_iter
	LOAD32 R0 R13 8             ; GLOBAL_TOKEN->S
	LOADU8 R0 R0 0              ; GLOBAL_TOKEN->S[0]
	CMPSKIPI.E R0 34            ; IF GLOBAL_TOKEN->S[0] == '"'
	JUMP @process_asm_done      ; Otherwise be done

	;; Add block of assembly
	LOAD32 R0 R13 8             ; GLOBAL_TOKEN->S
	ADDUI R0 R0 1               ; GLOBAL_TOKEN->S + 1
	CALLI R15 @emit_out         ; emit it

	LOADUI R0 $newline          ; Using "\n"
	CALLI R15 @emit_out         ; emit it

	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	JUMP @process_asm_iter

:process_asm_done
	LOADUI R0 $process_asm_string1 ; Using our First error message
	LOADUI R1 $close_paren      ; Using ")"
	CALLI R15 @require_match    ; Make sure of our required match

	LOADUI R0 $process_asm_string2 ; Using our First error message
	LOADUI R1 $semicolon        ; Using ";"
	CALLI R15 @require_match    ; Make sure of our required match

	POPR R1 R15                 ; Restore R1
	POPR R0 R15                 ; Restore R0
	RET R15

:process_asm_string0
	"ERROR in process_asm
MISSING (
"
:process_asm_string1
	"ERROR in process_asm
MISSING )
"
:process_asm_string2
	"ERROR in process_asm
MISSING ;
"


;; recursive_statement function
;; Receives struct token_list* global_token in R13,
;;	struct token_list* out in R12,
;;	struct token_list* string_list in R11
;;	struct token_list* global_list in R10
;;	and struct token_list* FUNC in R9
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns the token_lists modified
:recursive_statement
	PUSHR R0 R15                ; Protect R0
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	PUSHR R3 R15                ; Protect R3
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	LOAD32 R3 R9 4              ; FRAME = FUNCTION->LOCALS
:recursive_statement_iter
	LOAD32 R1 R13 8             ; GLOBAL_TOKEN->S
	LOADUI R0 $close_curly_brace ; '}'
	CALLI R15 @match            ; IF GLOBAL_TOKEN->S == "}"
	JUMP.NZ R0 @recursive_statement_cleanup

	;; Lets collect those statements
	CALLI R15 @statement        ; Collect next statement
	JUMP @recursive_statement_iter ; Iterate

:recursive_statement_cleanup
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	LOAD32 R1 R12 8             ; OUT->S
	LOADUI R0 $recursive_statement_string0 ; "RETURN\n"
	CALLI R15 @match            ; IF OUT->S == "RETURN\n"
	JUMP.NZ R0 @recursive_statement_done ; Save some work

	;; Lets pop them all off
	LOAD32 R2 R9 4              ; FUNC->LOCALS
	LOADUI R0 $recursive_statement_string1 ; Our POP string
:recursive_statement_pop
	CMPJUMPI.E R2 R3 @recursive_statement_done
	CALLI R15 @emit_out         ; emit it
	LOAD32 R2 R2 0              ; I = I->NEXT
	JUMP.NZ R2 @recursive_statement_pop ; Keep looping

:recursive_statement_done
	STORE32 R3 R9 4             ; FUNC->LOCALS = FRAME
	POPR R3 R15                 ; Restore R3
	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	POPR R0 R15                 ; Restore R0
	RET R15

:recursive_statement_string0
	"RETURN
"
:recursive_statement_string1
	"POP_ebx	# _recursive_statement_locals
"


;; statement function
;; Receives struct token_list* global_token in R13,
;;	struct token_list* out in R12,
;;	struct token_list* string_list in R11
;;	struct token_list* global_list in R10
;;	and struct token_list* FUNC in R9
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns the token_lists modified
:statement
	PUSHR R0 R15                ; Protect R0
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	LOAD32 R2 R13 8             ; GLOBAL_TOKEN->S
	LOADU8 R0 R2 0              ; GLOBAL_TOKEN->S[0]
	CMPSKIPI.E R0 123           ; If GLOBAL_TOKEN->S[0] != '{'
	JUMP @statement_label       ; Try next match

	;; Deal with { statements }
	CALLI R15 @recursive_statement
	JUMP @statement_done        ; All done

:statement_label
	CMPSKIPI.E R0 58            ; If GLOBAL_TOKEN->S[0] != ':'
	JUMP @statement_collect_local ; Try next match

	;; Deal with :label
	LOAD32 R0 R13 8             ; Using GLOBAL_TOKEN->S
	CALLI R15 @emit_out         ; emit it

	LOADUI R0 $statement_string0 ; Using label string
	CALLI R15 @emit_out         ; emit it
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	JUMP @statement_done        ; Move on to next thing

:statement_collect_local
	LOADUI R0 $struct           ; Using "struct"
	LOAD32 R1 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @match            ; IF GLOBAL_TOKEN->S == "struct"
	JUMP.NZ R0 @statement_collect_local_0

	;; Otherwise check if it is a primitive
	LOADUI R0 $prim_types       ; Using the Primitive types list
	SWAP R0 R1                  ; Put in correct order
	CALLI R15 @lookup_type      ; Check if a primitive type
	JUMP.Z R0 @statement_process_if ; If not try the next one

:statement_collect_local_0
	CALLI R15 @collect_local    ; Collect the local
	JUMP @statement_done        ; And move on

:statement_process_if
	LOADUI R0 $if_string        ; Using "if"
	LOAD32 R1 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @match            ; IF GLOBAL_TOKEN->S == "if"
	JUMP.Z R0 @statement_process_do
	CALLI R15 @process_if       ; Collect that if statement
	JUMP @statement_done        ; Move on to next thing

:statement_process_do
	LOADUI R0 $do_string        ; Using "do"
	LOAD32 R1 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @match            ; IF GLOBAL_TOKEN->S == "do"
	JUMP.Z R0 @statement_process_while
	CALLI R15 @process_do       ; Collect that do statement
	JUMP @statement_done        ; Move on to next thing

:statement_process_while
	LOADUI R0 $while_string     ; Using "while"
	LOAD32 R1 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @match            ; IF GLOBAL_TOKEN->S == "while"
	JUMP.Z R0 @statement_process_for
	CALLI R15 @process_while    ; Collect that while statement
	JUMP @statement_done        ; Move on to next thing

:statement_process_for
	LOADUI R0 $for_string       ; Using "for"
	LOAD32 R1 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @match            ; IF GLOBAL_TOKEN->S == "for"
	JUMP.Z R0 @statement_process_asm
	CALLI R15 @process_for      ; Collect that FOR statement
	JUMP @statement_done        ; Move on to next thing

:statement_process_asm
	LOADUI R0 $asm_string       ; Using "asm"
	LOAD32 R1 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @match            ; IF GLOBAL_TOKEN->S == "asm"
	JUMP.Z R0 @statement_goto
	CALLI R15 @process_asm      ; Collect that ASM statement
	JUMP @statement_done        ; Move on to next thing

:statement_goto
	LOADUI R0 $goto_string      ; Using "goto"
	LOAD32 R1 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @match            ; IF GLOBAL_TOKEN->S == "goto"
	JUMP.Z R0 @statement_return_result

	;; Deal with goto label:
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	LOADUI R0 $statement_string1 ; Using our JUMP string
	CALLI R15 @emit_out         ; emit it

	LOAD32 R0 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @emit_out         ; emit it

	LOADUI R0 $newline          ; "\n"
	CALLI R15 @emit_out         ; emit it

	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT

	LOADUI R0 $statement_string2 ; Using our error message
	LOADUI R1 $semicolon        ; Using ";"
	CALLI R15 @require_match    ; Make sure of our required match
	JUMP @statement_done        ; Move on

:statement_return_result
	LOADUI R0 $return_string    ; Using "return"
	LOAD32 R1 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @match            ; IF GLOBAL_TOKEN->S == "return"
	JUMP.Z R0 @statement_break

	;; Deal with return statements in functions
	CALLI R15 @return_result    ; Do all of the work
	JUMP @statement_done        ; Move on to next

:statement_break
	LOADUI R0 $break_string     ; Using "break"
	LOAD32 R1 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @match            ; IF GLOBAL_TOKEN->S == "break"
	JUMP.Z R0 @statement_continue

	;; Let break function deal with updating out
	CALLI R15 @process_break    ; Do all the work
	JUMP @statement_done        ; Move on to next

:statement_continue
	LOADUI R0 $continue_string  ; Using "continue"
	LOAD32 R1 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @match            ; IF GLOBAL_TOKEN->S == "continue"
	JUMP.Z R0 @statement_expression

	;; Simple Continue compatibility
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT

	LOADUI R0 $statement_string3 ; Using our continue comment string
	CALLI R15 @emit_out         ; emit it

	LOADUI R0 $statement_string2 ; Using our error message
	LOADUI R1 $semicolon        ; Using ";"
	CALLI R15 @require_match    ; Make sure of our required match
	JUMP @statement_done        ; Move on

:statement_expression
	CALLI R15 @expression       ; Do expression evaluation
	LOADUI R0 $statement_string2 ; Load our error message
	LOADUI R1 $semicolon        ; use ";"
	CALLI R15 @require_match    ; Make sure GLOBAL_TOKEN-> == ";"

:statement_done
	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	POPR R0 R15                 ; Restore R0
	RET R15

:statement_string0
	"	#C goto label
"
:statement_string1
	"JUMP %"
:statement_string2
	"ERROR in statement
MISSING ;
"
:statement_string3
	"
#continue statement
"


;; collect_local function
;; Receives struct token_list* global_token in R13,
;;	struct token_list* out in R12,
;;	struct token_list* string_list in R11
;;	struct token_list* global_list in R10
;;	and struct token_list* FUNC in R9
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns the token_lists modified
:collect_local
	PUSHR R0 R15                ; Protect R0
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	CALLI R15 @type_name        ; Get it's type
	MOVE R1 R0                  ; Prepare for call
	LOAD32 R0 R13 8             ; GLOBAL_TOKEN->S
	LOAD32 R2 R9 4              ; FUNC->LOCALS
	CALLI R15 @sym_declare      ; SET A
	MOVE R2 R0                  ; Protect A

	;; Figure out depth
	LOADUI R0 $main_string      ; Using "main"
	LOAD32 R1 R9 8              ; FUNC->S
	CALLI R15 @match            ; IF FUNC->S == "main"
	JUMP.Z R0 @collect_local_0  ; Try next
	LOAD32 R0 R9 4              ; FUNC->LOCALS
	JUMP.NZ R0 @collect_local_0 ; Try next

	LOADI R0 -20                ; The default depth for main
	STORE32 R0 R2 16            ; A->DEPTH = -20
	JUMP @collect_local_output  ; Deal with header

:collect_local_0
	LOAD32 R0 R9 16             ; FUNC->ARGS
	JUMP.NZ R0 @collect_local_1 ; Try Next
	LOAD32 R0 R9 4              ; FUNC->LOCALS
	JUMP.NZ R0 @collect_local_1 ; Try Next

	LOADI R0 -8                 ; The default depth for foo()
	STORE32 R0 R2 16            ; A->DEPTH = -8
	JUMP @collect_local_output  ; Deal with header

:collect_local_1
	LOAD32 R0 R9 4              ; FUNC->LOCALS
	JUMP.NZ R0 @collect_local_2 ; Try Next

	LOAD32 R0 R9 16             ; FUNC->ARGS
	LOAD32 R0 R0 16             ; FUNC->ARGS->DEPTH
	SUBI R0 R0 8                ; DEPTH = FUNC->ARGS->DEPTH - 8
	STORE32 R0 R2 16            ; A->DEPTH = DEPTH
	JUMP @collect_local_output  ; Deal with header

:collect_local_2
	LOAD32 R0 R9 4              ; FUNC->LOCALS
	LOAD32 R0 R0 16             ; FUNC->LOCALS->DEPTH
	SUBI R0 R0 4                ; DEPTH = FUNC->LOCALS->DEPTH - 4
	STORE32 R0 R2 16            ; A->DEPTH = DEPTH

:collect_local_output
	STORE32 R2 R9 4             ; FUNC->LOCALS = A

	;; Output header
	LOADUI R0 $collect_local_string0 ; Starting with the comment
	CALLI R15 @emit_out         ; emit it

	LOAD32 R0 R2 8              ; A->S
	CALLI R15 @emit_out         ; emit it

	LOADUI R0 $newline          ; Using "\n"
	CALLI R15 @emit_out         ; emit it

	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT

	;; Deal with possible assignment
	LOADUI R0 $equal            ; Using "="
	LOAD32 R1 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @match            ; IF GLOBAL_TOKEN->S == "="
	JUMP.Z R0 @collect_local_nonassign

	;; Deal with assignment of the local
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	CALLI R15 @expression       ; Update OUT with the evaluation of the Expression

:collect_local_nonassign
	LOADUI R0 $collect_local_string1 ; Our error message
	LOADUI R1 $semicolon        ; Using ";"
	CALLI R15 @require_match    ; Make sure GLOBAL_TOKEN->S == ";"

	;; Final Footer
	LOADUI R0 $collect_local_string2 ; Add our PUSH statement
	CALLI R15 @emit_out         ; emit it

	LOAD32 R0 R2 8              ; A->S
	CALLI R15 @emit_out         ; emit it

	LOADUI R0 $newline          ; Using "\n"
	CALLI R15 @emit_out         ; emit it

	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	POPR R0 R15                 ; Restore R0
	RET R15

:collect_local_string0
	"# Defining local "
:collect_local_string1
	"ERROR in collect_local
Missing ;
"
:collect_local_string2
	"PUSH_eax	#"


;; collect_arguments function
;; Receives struct token_list* global_token in R13,
;;	struct token_list* out in R12,
;;	struct token_list* string_list in R11
;;	struct token_list* global_list in R10
;;	and struct token_list* FUNC in R9
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns the token_lists modified
:collect_arguments
	PUSHR R0 R15                ; Protect R0
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
:collect_arguments_iter
	LOADUI R0 $close_paren      ; Using ")"
	LOAD32 R1 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @match            ; IF GLOBAL_TOKEN->S == ")"
	JUMP.NZ R0 @collect_arguments_done ; Be done

	;; Collect the arguments
	CALLI R15 @type_name        ; Get what type we are working with
	MOVE R1 R0                  ; Put TYPE where it will be used
	LOAD32 R0 R13 8             ; GLOBAL_TOKEN->S
	LOADU8 R0 R0 0              ; GLOBAL_TOKEN->S[0]
	CMPSKIPI.NE R0 41           ; IF GLOBAL_TOKEN->S[0] == ')'
	JUMP @collect_arguments_iter3 ; foo(int,char,void) doesn't need anything done

	;; Check for foo(int a,...)
	CMPSKIPI.NE R0 41           ; IF GLOBAL_TOKEN->S[0] == ','
	JUMP @collect_arguments_iter3 ; Looks like final case

	;; Deal with foo(int a, ...)
	LOAD32 R0 R13 8             ; GLOBAL_TOKEN->S
	LOAD32 R2 R9 16             ; FUNC->ARGUMENTS
	CALLI R15 @sym_declare      ; Get A
	MOVE R2 R0                  ; Get A out of the way

:collect_arguments_func
	LOAD32 R0 R9 16             ; FUNC->ARGS
	CMPSKIPI.E R0 0             ; IF NULL == FUNC->ARGS
	LOAD32 R0 R0 16             ; FUNC->ARGS->DEPTH
	SUBI R0 R0 4                ; FUNC->ARGS->DEPTH - 4 or NULL - 4 (-4)
	STORE32 R0 R2 16            ; A->DEPTH = VALUE

:collect_arguments_iter2
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	STORE32 R2 R9 16            ; FUNC->ARGUMENTS = A

:collect_arguments_iter3
	LOAD32 R0 R13 8             ; GLOBAL_TOKEN->S
	LOADU8 R0 R0 0              ; GLOBAL_TOKEN->S[0]
	CMPSKIPI.NE R0 44           ; IF GLOBAL_TOKEN->S[0] == ','
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	JUMP @collect_arguments_iter ; Keep looping

:collect_arguments_done
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	POPR R0 R15                 ; Restore R0
	RET R15


;; declare_function function
;; Receives struct token_list* global_token in R13,
;;	struct token_list* out in R12,
;;	struct token_list* string_list in R11
;;	and struct token_list* global_list in R10
;; SETS R9 to struct token_list* FUNC
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns the token_lists modified
:declare_function
	PUSHR R0 R15                ; Protect R0
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	FALSE R0                    ; Using Zero
	STORER32 R0 @current_count  ; CURRENT_COUNT = 0
	LOAD32 R0 R13 4             ; GLOBAL_TOKEN->PREV
	LOAD32 R0 R0 8              ; GLOBAL_TOKEN->PREV->S
	FALSE R1                    ; Passing NULL
	LOADR32 R2 @global_function_list ; where the global function list is located
	CALLI R15 @sym_declare      ; declare FUNC
	STORER32 R0 @global_function_list ; GLOBAL_FUNCTION_LIST = FUNC
	MOVE R9 R0                  ; SETS FUNC
	CALLI R15 @collect_arguments ; Collect function arguments
	LOAD32 R2 R13 8             ; GLOBAL_TOKEN->S
	LOADU8 R0 R2 0              ; GLOBAL_TOKEN->S[0]
	CMPSKIPI.NE R0 59           ; IF GLOBAL_TOKEN->S[0] == ';'
	JUMP @declare_function_prototype ; Don't waste time

	;; Looks like it is an actual function definition
	LOADUI R0 $declare_function_string0 ; Using first string
	CALLI R15 @emit_out         ; emit it

	LOAD32 R0 R9 8              ; Using FUNC->S
	CALLI R15 @emit_out         ; emit it

	LOADUI R0 $newline          ; Using "\n"
	CALLI R15 @emit_out         ; emit it

	LOADUI R0 $declare_function_string1 ; Using second string
	CALLI R15 @emit_out         ; emit it

	LOAD32 R0 R9 8              ; Using FUNC->S
	CALLI R15 @emit_out         ; emit it

	LOADUI R0 $newline          ; Using "\n"
	CALLI R15 @emit_out         ; emit it

:declare_function_nonmain
	FALSE R1                    ; Cleaning up before call
	CALLI R15 @statement        ; Collect the statement

	;; Prevent Duplicate Returns
	LOAD32 R1 R12 8             ; OUT->S
	LOADUI R0 $declare_function_string2 ; Our final string
	CALLI R15 @match            ; Check for Match
	JUMP.NZ R0 @declare_function_done ; Clean up

	;; Deal with adding the return
	LOADUI R0 $declare_function_string2 ; Our final string
	CALLI R15 @emit_out         ; emit it

:declare_function_done
	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	POPR R0 R15                 ; Restore R0
	RET R15

:declare_function_prototype
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	JUMP @declare_function_done ; Clean up

:declare_function_string0
	"# Defining function "
:declare_function_string1
	":FUNCTION_"
:declare_function_string2
	"RETURN
"

:current_count
	NOP


;; program function
;; Receives struct token_list* global_token in R13,
;;	struct token_list* out in R12,
;;	struct token_list* string_list in R11
;;	and struct token_list* global_list in R10
;;	and struct token_list* FUNC in R9
;;	and struct token_list* current_target in R8
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns the token_lists modified
:program
	PUSHR R0 R15                ; Protect R0
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	PUSHR R3 R15                ; Protect R3
:program_iter
	JUMP.Z R13 @program_done    ; Looks like we read all the tokens
	LOADUI R0 $constant         ; Using the constant string
	LOAD32 R1 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @match            ; Check if they match
	JUMP.Z R0 @program_type     ; Looks like not

	;; Deal with CONSTANT case
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	LOAD32 R0 R13 8             ; GLOBAL_TOKEN->S
	FALSE R1                    ; Set NULL
	LOADR32 R2 @global_constant_list ; GLOBAL_CONSTANTS_LIST
	CALLI R15 @sym_declare      ; Declare the global constant
	STORER32 R0 @global_constant_list ; Update global constant

	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	STORE32 R13 R0 16           ; GLOBAL_CONSTANT_LIST->ARGUMENTS = GLOBAL_TOKEN
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	JUMP @program_iter          ; Loop again

:program_type
	CALLI R15 @type_name        ; Get the type
	JUMP.Z R0 @program_iter     ; If newly defined type iterate

	;; Looks like we got a defined type
	MOVE R1 R0                  ; Put the type where it can be used
	LOAD32 R0 R13 8             ; GLOBAL_TOKEN->S
	LOADUI R3 $global_symbol_list ; Get address of global symbol list
	LOAD32 R2 R3 0              ; GLOBAL_SYMBOLS_LIST
	CALLI R15 @sym_declare      ; Declare that global symbol
	STORE32 R0 R3 0             ; Update global symbol list

	LOAD32 R3 R13 8             ; GLOBAL_TOKEN->S
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	LOADUI R0 $semicolon        ; Get semicolon string
	LOAD32 R1 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @match            ; Check if they match
	JUMP.Z R0 @program_function ; If not a match

	;; Deal with case of TYPE NAME;
	COPY R1 R10                 ; Using GLOBALS_LIST
	LOADUI R0 $program_string0  ; Using the GLOBAL_ prefix
	CALLI R15 @emit             ; emit it
	MOVE R1 R0                  ; Move new GLOBALS_LIST into Place
	MOVE R0 R3                  ; Use GLOBAL_TOKEN->PREV->S
	CALLI R15 @emit             ; emit it
	MOVE R1 R0                  ; Move new GLOBALS_LIST into Place
	LOADUI R0 $program_string1  ; Using the NOP postfix
	CALLI R15 @emit             ; emit it
	MOVE R10 R0                 ; Move new GLOBALS_LIST into Place
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	JUMP @program_iter

:program_function
	LOADUI R0 $open_paren       ; Get open paren string
	CALLI R15 @match            ; Check if they match
	JUMP.Z R0 @program_assign   ; If not a match

	;; Deal with case of TYPE NAME(...)
	CALLI R15 @declare_function
	JUMP @program_iter

:program_assign
	LOADUI R0 $equal            ; Get equal string
	CALLI R15 @match            ; Check if they match
	JUMP.Z R0 @program_error    ; If not a match
	COPY R1 R10                 ; Using GLOBALS_LIST
	LOADUI R0 $program_string0  ; Using the GLOBAL_ prefix
	CALLI R15 @emit             ; emit it
	MOVE R1 R0                  ; Move new GLOBALS_LIST into Place
	MOVE R0 R3                  ; Use GLOBAL_TOKEN->PREV->S
	CALLI R15 @emit             ; emit it
	MOVE R1 R0                  ; Move new GLOBALS_LIST into Place
	LOADUI R0 $newline          ; Using the Newline postfix
	CALLI R15 @emit             ; emit it
	MOVE R10 R0                 ; Update GLOBALS_LIST
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	LOAD32 R0 R13 8             ; GLOBAL_TOKEN->S
	LOADU8 R0 R0 0              ; GLOBAL_TOKEN->S[0]
	LOADUI R1 $digit_chars      ; 0-9
	CALLI R15 @in_set           ; Figure out if in set
	JUMP.Z R0 @program_assign_string ; If not in sets

	;; Looks like we have an int
	COPY R1 R10                 ; Using GLOBALS_LIST
	LOADUI R0 $percent          ; Using percent prefix
	CALLI R15 @emit             ; emit it
	MOVE R1 R0                  ; Put GLOBALS_LIST into Place
	LOAD32 R0 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @emit             ; emit it
	MOVE R1 R0                  ; Put GLOBALS_LIST into Place
	LOADUI R0 $newline          ; Using newline postfix
	CALLI R15 @emit             ; emit it
	MOVE R10 R0                 ; Update GLOBALS_LIST
	JUMP @program_assign_done   ; Move on

:program_assign_string
	LOAD32 R0 R13 8             ; GLOBAL_TOKEN->S
	LOADU8 R0 R0 0              ; GLOBAL_TOKEN->S[0]
	CMPSKIPI.E R0 34            ; If GLOBAL_TOKEN->S[0] == '"'
	JUMP @program_error         ; If not we hit an error

	;; Looks like we have a string
	LOAD32 R0 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @parse_string     ; Parse it into useful form
	COPY R1 R10                 ; GLOBALS_LIST
	CALLI R15 @emit             ; emit it
	MOVE R10 R0                 ; Update GLOBALS_LIST

:program_assign_done
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	LOADUI R0 $program_string4  ; Potential error message
	LOADUI R1 $semicolon        ; Checking for ;
	CALLI R15 @require_match    ; Catch those errors
	JUMP @program_iter

:program_error
	LOADUI R0 $program_string2  ; message part 1
	FALSE R1                    ; Show to user
	CALLI R15 @file_print       ; write
	LOAD32 R0 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @file_print       ; write
	LOADUI R0 $program_string3  ; message part 2
	CALLI R15 @file_print       ; write
	CALLI R15 @line_error       ; Provide a meaningful error message
	HALT

:program_done
	POPR R3 R15                 ; Restore R3
	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	POPR R0 R15                 ; Restore R0
	RET R15

:program_string0
	":GLOBAL_"
:program_string1
	"
NULL
"
:program_string2
	"Received "
:program_string3
	" in program
"
:program_string4
"ERROR in Program
Missing ;
"


;; sym_declare function
;; Receives char* in R0, struct type* in R1, struct token_list* in R2
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns struct token_list* in R0
:sym_declare
	STORE32 R2 R14 0            ; A->NEXT = LIST
	STORE32 R0 R14 8            ; A->S = S
	STORE32 R1 R14 12           ; A->TYPE = T
	ADDUI R0 R14 20             ; CALLOC struct token_list
	SWAP R0 R14                 ; Prepare for Return
	RET R15


;; sym_lookup function
;; Receives char* in R0 and struct token_list in R1
;; Returns struct token_list* or NULL in R0
:sym_lookup
	PUSHR R2 R15                ; Protect R2
	MOVE R2 R1                  ; Protect I
	MOVE R1 R0                  ; Put S in proper place
:sym_lookup_iter
	JUMP.Z R2 @sym_lookup_done  ; Stop if NULL
	LOAD32 R0 R2 8              ; I->S
	CALLI R15 @match            ; if I->S == S
	JUMP.NZ R0 @sym_lookup_done ; Stop if match
	LOAD32 R2 R2 0              ; I = I->NEXT
	JUMP @sym_lookup_iter       ; Keep looping

:sym_lookup_done
	MOVE R0 R2                  ; Using R2 as our result
	POPR R2 R15                 ; Restore R2
	RET R15


;; function_call function
;; Receives CHAR* S in R0 and INT BOOL in R1
;;	struct token_list* out in R12,
;;	struct token_list* string_list in R11
;;	and struct token_list* global_list in R10
;;	and struct token_list* FUNC in R9
;;	and struct token_list* current_target in R8
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns the token_lists modified
:function_call
	PUSHR R0 R15                ; Protect R0
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	PUSHR R3 R15                ; Protect R3
	PUSHR R4 R15                ; Protect R4
	MOVE R2 R0                  ; Protect S
	MOVE R3 R1                  ; Protect BOOL

	LOADUI R0 $function_call_string0 ; Our first error message
	LOADUI R1 $open_paren       ; Using "("
	CALLI R15 @require_match    ; Make sure of a match

	FALSE R4                    ; PASSED = 0
	LOADUI R0 $function_call_string1 ; Our first header
	CALLI R15 @emit_out         ; emit it
	LOADUI R0 $function_call_string2 ; Our second header
	CALLI R15 @emit_out         ; emit it
	LOADUI R0 $function_call_string3 ; Our third header
	CALLI R15 @emit_out         ; emit it

	LOAD32 R0 R13 8             ; GLOBAL_TOKEN->S
	LOADU8 R0 R0 0              ; GLOBAL_TOKEN->S[0]
	CMPSKIPI.NE R0 41           ; IF GLOBAL_TOKEN->S[0] != ')'
	JUMP @function_call_collect_done ; looks like function()

	;; Collect arguments
	CALLI R15 @expression       ; Deal with first argument
	LOADUI R0 $function_call_string4 ; Push it onto stack
	CALLI R15 @emit_out         ; emit it
	ADDUI R4 R4 1               ; PASSED = 1

:function_call_collect_iter
	LOAD32 R0 R13 8             ; GLOBAL_TOKEN->S
	LOADU8 R0 R0 0              ; GLOBAL_TOKEN->S[0]
	CMPSKIPI.E R0 44            ; IF GLOBAL_TOKEN->S[0] != ','
	JUMP @function_call_collect_done ; looks like we are done collecting arguments

	;; Collect another argument
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	CALLI R15 @expression       ; Deal with Nth argument
	LOADUI R0 $function_call_string5 ; Push it onto stack
	CALLI R15 @emit_out         ; emit it
	ADDUI R4 R4 1               ; PASSED = PASSED + 1
	JUMP @function_call_collect_iter ; Keep looping

:function_call_collect_done
	LOADUI R0 $function_call_string6 ; Our second error message
	LOADUI R1 $close_paren      ; Using ")"
	CALLI R15 @require_match    ; Make sure of a match

	JUMP.Z R3 @function_call_call_false ; if BOOL != TRUE

	;; Deal with TRUE == BOOL
	LOADUI R0 $function_call_string7 ; Our first prefix
	CALLI R15 @emit_out         ; emit it
	MOVE R0 R2                  ; Using S
	CALLI R15 @emit_out         ; emit it
	LOADUI R0 $function_call_string8 ; Our first postfix
	CALLI R15 @emit_out         ; emit it
	LOADUI R0 $function_call_string9 ; Our second postfix
	CALLI R15 @emit_out         ; emit it
	LOADUI R0 $function_call_string10 ; Our last postfix
	CALLI R15 @emit_out         ; emit it
	JUMP @function_call_call_done ; Move on

:function_call_call_false
	;; Deal with FALSE == BOOL
	LOADUI R0 $function_call_string11 ; Our first prefix
	CALLI R15 @emit_out         ; emit it
	LOADUI R0 $function_call_string12 ; Our last prefix
	CALLI R15 @emit_out         ; emit it
	MOVE R0 R2                  ; Using S
	CALLI R15 @emit_out         ; emit it
	LOADUI R0 $newline          ; Using "\n"
	CALLI R15 @emit_out         ; emit it

:function_call_call_done
	LOADUI R0 $function_call_string13 ; Our POP

:function_call_pop_iter
	JUMP.Z R4 @function_call_pop_done ; Skip POP if out of args on Stack
	CALLI R15 @emit_out         ; emit our POP
	SUBI R4 R4 1                ; PASSED = PASSED - 1
	JUMP @function_call_pop_iter ; Loop

:function_call_pop_done
	LOADUI R0 $function_call_string14 ; Our first postfix
	CALLI R15 @emit_out         ; emit it
	LOADUI R0 $function_call_string15 ; Our final postfix
	CALLI R15 @emit_out         ; emit it

	POPR R4 R15                 ; Restore R4
	POPR R3 R15                 ; Restore R3
	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	POPR R0 R15                 ; Restore R0
	RET R15

:function_call_string0
	"ERROR in process_expression_list
No ( was found
"
:function_call_string1
	"PUSH_edi	# Prevent overwriting in recursion
"
:function_call_string2
	"PUSH_ebp	# Protect the old base pointer
"
:function_call_string3
	"COPY_esp_to_edi	# Copy new base pointer
"
:function_call_string4
	"PUSH_eax	#_process_expression1
"
:function_call_string5
	"PUSH_eax	#_process_expression2
"
:function_call_string6
	"ERROR in process_expression_list
No ) was found
"
:function_call_string7
	"LOAD_BASE_ADDRESS_eax %"
:function_call_string8
	"
LOAD_INTEGER
"
:function_call_string9
	"COPY_edi_to_ebp
"
:function_call_string10
	"CALL_eax
"
:function_call_string11
	"COPY_edi_to_ebp
"
:function_call_string12
	"CALL_IMMEDIATE %FUNCTION_"
:function_call_string13
	"POP_ebx	# _process_expression_locals
"
:function_call_string14
	"POP_ebp	# Restore old base pointer
"
:function_call_string15
	"POP_edi	# Prevent overwrite
"


;; emit function
;; Receives char* in R0, struct token_list* in R1
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns struct token_list* in R0
:emit
	PUSHR R2 R15                ; Protect R2
	COPY R2 R14                 ; Pointer to T
	ADDUI R14 R14 20            ; CALLOC struct token_list
	STORE32 R1 R2 0             ; T->NEXT = HEAD
	STORE32 R0 R2 8             ; T->S = S
	MOVE R0 R2                  ; Put T in proper spot for return
	POPR R2 R15                 ; Restore R2
	RET R15


;; emit_out function
;; Receives char* in R0
;;	struct token_list* out in R12,
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns struct token_list* in R0
:emit_out
	STORE32 R12 R14 0           ; T->NEXT = OUT
	ADDUI R12 R14 20            ; Get T
	SWAP R12 R14                ; CALLOC struct token_list
	STORE32 R0 R12 8            ; T->S = S
	RET R15


;; uniqueID function
;; Receives char* in R0, struct token_list* in R1 and char* in R2
;; Calls emit repeatedly
;; Returns struct token_list* in R0
:uniqueID
	CALLI R15 @emit             ; emit S

	MOVE R1 R0                  ; Put L in the correct place
	LOADUI R0 $underline        ; Using "_"
	CALLI R15 @emit             ; emit it

	MOVE R1 R0                  ; Put L in the correct place
	COPY R0 R2                  ; Put NUM in the correct place
	CALLI R15 @emit             ; emit NUM

	MOVE R1 R0                  ; Put L in the correct place
	LOADUI R0 $newline          ; Using "\n"
	CALLI R15 @emit             ; emit it
	RET R15


;; uniqueID_out function
;; Receives char* in R0, char* in R1
;; Calls emit_out repeatedly
;; Returns nothing
:uniqueID_out
	CALLI R15 @emit_out         ; emit S

	LOADUI R0 $underline        ; Using "_"
	CALLI R15 @emit_out         ; emit it

	COPY R0 R1                  ; Put NUM in the correct place
	CALLI R15 @emit_out         ; emit NUM

	LOADUI R0 $newline          ; Using "\n"
	CALLI R15 @emit_out         ; emit it
	RET R15


;; file_print function
;; Receives pointer to string in R0 and FILE* in R1
;; Returns nothing
:file_print
	PUSHR R2 R15                ; Protect R2 from Overwrite
	MOVE R2 R0                  ; Put string pointer into place
:file_print_read
	LOAD8 R0 R2 0               ; Get a char
	JUMP.Z R0 @file_print_done  ; If NULL be done
	FPUTC                       ; Write the Char
	ADDUI R2 R2 1               ; Point at next CHAR
	JUMP @file_print_read       ; Loop again
:file_print_done
	POPR R2 R15                 ; Restore R2
	RET R15


;; recursive_output function
;; Receives token_list in R0 and FILE* in R1
;; Returns nothing and alters nothing
:recursive_output
	JUMP.Z R0 @recursive_output_abort ; Abort if NULL
	PUSHR R2 R15                ; Preserve R2 from recursion
	MOVE R2 R0                  ; Preserve R0 from recursion
	LOAD32 R0 R2 0              ; Using I->NEXT
	CALLI R15 @recursive_output ; Recurse
	LOAD32 R0 R2 8              ; Using I->S
	CALLI R15 @file_print       ; Write the string
	MOVE R0 R2                  ; Put R0 back
	POPR R2 R15                 ; Restore R0
:recursive_output_abort
	RET R15


;; match function
;; Receives a CHAR* in R0, CHAR* in R1
;; Returns Bool in R0 indicating if strings match
:match
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	PUSHR R3 R15                ; Protect R3
	PUSHR R4 R15                ; Protect R4
	MOVE R2 R0                  ; Put First string in place
	MOVE R3 R1                  ; Put Second string in place
	LOADUI R4 0                 ; Set initial index of 0
:match_cmpbyte
	LOADXU8 R0 R2 R4            ; Get a byte of our first string
	LOADXU8 R1 R3 R4            ; Get a byte of our second string
	ADDUI R4 R4 1               ; Prep for next loop
	CMPSKIP.NE R1 R0            ; Compare the bytes
	JUMP.NZ R1 @match_cmpbyte   ; Loop if bytes are equal
;; Done
	FALSE R2                    ; Default answer
	CMPSKIP.NE R0 R1            ; If ended loop with everything matching
	TRUE R2                     ; Set as TRUE
	MOVE R0 R2                  ; Prepare for return
	POPR R4 R15                 ; Restore R4
	POPR R3 R15                 ; Restore R3
	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	RET R15


;; lookup_type function
;; Receives a CHAR* in R0 and struct type* in R1
;; Returns struct type* in R0 or NULL if no match
:lookup_type
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	MOVE R2 R1                  ; Put START in correct place
	MOVE R1 R0                  ; Put S in correct place
:lookup_type_iter
	LOAD32 R0 R2 24             ; Get I->NAME
	CALLI R15 @match            ; Check if I->NAME == S
	JUMP.NZ R0 @lookup_type_done ; If match found be done
	LOAD32 R2 R2 0              ; I = I->NEXT
	JUMP.NZ R2 @lookup_type_iter ; Otherwise iterate until I == NULL
:lookup_type_done
	MOVE R0 R2                  ; Our answer (I or NULL)
	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	RET R15


;; lookup_member function
;; Receives struct type* parent in R0 and char* name in R1
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
	;; Returns struct type* of member in R0 or aborts with error
:lookup_member
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	PUSHR R3 R15                ; Protect R3
	LOAD32 R3 R0 24             ; PARENT->NAME for error
	MOVE R2 R0                  ; I = PARENT
:lookup_member_iter
	LOAD32 R2 R2 16             ; I = I->MEMBERS
	JUMP.Z R2 @lookup_member_error ; We failed hard
	LOAD32 R0 R2 24             ; I->NAME
	CALLI R15 @match            ; IF I->NAME == NAME
	JUMP.Z R0 @lookup_member_iter ; Loop again

:lookup_member_done
	MOVE R0 R2                  ; Put I in the correct place
	POPR R3 R15                 ; Restore R3
	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	RET R15

:lookup_member_error
	FALSE R1                    ; Write to TTY
	LOADUI R0 $lookup_member_string0 ; Our header string
	CALLI R15 @file_print       ; Print it
	MOVE R0 R3                  ; Using PARENT->NAME
	CALLI R15 @file_print       ; Print it
	LOADUI R0 $arrow_string     ; Using "->"
	CALLI R15 @file_print       ; Print it
	LOAD32 R0 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @file_print       ; Print it
	LOADUI R0 $lookup_member_string1 ; Our footer string
	CALLI R15 @file_print       ; Print it
	CALLI R15 @line_error       ; Give line info
	LOADUI R0 $newline          ; Our final addition
	CALLI R15 @file_print       ; Print it
	HALT

:lookup_member_string0
	"ERROR in lookup_member "
:lookup_member_string1
	" does not exist
"


;; build_member function
;; Receives a struct type* in R0, int in R1 and int in R2
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Modifies R2 to current member_size
;; Returns struct type* in R0
:build_member
	PUSHR R3 R15                ; Protect R3
	PUSHR R4 R15                ; Protect R4
	PUSHR R5 R15                ; Protect R5
	PUSHR R6 R15                ; Protect R6
	MOVE R6 R1                  ; Protect OFFSET
	MOVE R4 R0                  ; Protect LAST
	CALLI R15 @type_name        ; Get MEMBER_TYPE
	MOVE R5 R0                  ; Protect MEMBER_TYPE
	ADDUI R3 R14 28             ; CALLOC struct type
	SWAP R3 R14                 ; SET I

	LOAD32 R0 R13 8             ; GLOBAL_TOKEN->S
	STORE32 R0 R3 24            ; I->NAME = GLOBAL_TOKEN->S
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	STORE32 R4 R3 16            ; I->MEMBERS = LAST

	LOADUI R0 $open_bracket     ; Using "["
	LOAD32 R1 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @match            ; IF GLOBAL_TOKEN->S == "["
	JUMP.Z R0 @build_member_single

	;; Deal with type name [ number ] ;
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	LOAD32 R0 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @numerate_string  ; Convert string to int NUMBER
	LOAD32 R1 R5 20             ; MEMBER_TYPE->TYPE
	LOAD32 R1 R1 4              ; MEMBER_TYPE->TYPE->SIZE
	MULU R0 R0 R1               ; MEMBER_TYPE->TYPE->SIZE * NUMBER
	STORE32 R0 R3 4             ; I->SIZE = MEMBER_TYPE->TYPE->SIZE * NUMBER
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	LOADUI R0 $build_member_string0 ; Our error message
	LOADUI R1 $close_bracket    ; Using "]"
	CALLI R15 @require_match    ; Make sure it is right
	JUMP @build_member_done     ; Skip over single steps

:build_member_single
	LOAD32 R0 R5 4              ; MEMBER_TYPE->SIZE
	STORE32 R0 R3 4             ; I->SIZE = MEMBER_TYPE->SIZE

:build_member_done
	LOAD32 R2 R3 4              ; MEMBER_SIZE = I->SIZE
	STORE32 R5 R3 20            ; I->TYPE = MEMBER_TYPE
	STORE32 R6 R3 8             ; I->OFFSET = OFFSET
	MOVE R1 R6                  ; Restore OFFSET
	MOVE R0 R3                  ; RETURN I in R0
	POPR R6 R15                 ; Restore R6
	POPR R5 R15                 ; Restore R5
	POPR R4 R15                 ; Restore R4
	POPR R3 R15                 ; Restore R3
	RET R15

:build_member_string0
	"Struct only supports [num] form
"


;; build_union function
;; Receives a struct type* in R0, int in R1 and int in R2
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Modifies R2 to current member_size
;; Returns struct type* in R0
:build_union
	PUSHR R3 R15                ; Protect R3
	PUSHR R4 R15                ; Protect R4
	PUSHR R5 R15                ; Protect R5
	MOVE R4 R0                  ; Protect LAST
	MOVE R3 R1                  ; Protect OFFSET
	FALSE R5                    ; SIZE = 0
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	LOADUI R0 $build_union_string0 ; ERROR MESSAGE
	LOADUI R1 $open_curly_brace ; OPEN CURLY BRACE
	CALLI R15 @require_match    ; Ensure we have that curly brace
:build_union_iter
	LOAD32 R0 R13 8             ; GLOBAL_TOKEN->S
	LOADU8 R0 R0 0              ; GLOBAL_TOKEN->S[0]
	LOADUI R1 125               ; numerical value of }
	CMPJUMPI.E R0 R1 @build_union_done ; No more looping required
	MOVE R0 R4                  ; We are passing last to be overwritten
	MOVE R1 R3                  ; We are also passing OFFSET
	CALLI R15 @build_member     ; To build_member to get new LAST and new member_size
	CMPSKIP.LE R2 R5            ; If MEMBER_SIZE > SIZE
	COPY R5 R2                  ; SIZE = MEMMER_SIZE
	MOVE R4 R0                  ; Protect LAST
	MOVE R3 R1                  ; Protect OFFSET
	LOADUI R0 $build_union_string1 ; ERROR MESSAGE
	LOADUI R1 $semicolon        ; SEMICOLON
	CALLI R15 @require_match    ; Ensure we have that curly brace
	JUMP @build_union_iter      ; Loop until we get that closing curly brace
:build_union_done
	MOVE R2 R5                  ; Setting MEMBER_SIZE = SIZE
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	MOVE R1 R3                  ; Restore OFFSET
	MOVE R0 R4                  ; Restore LAST as we are turning that
	POPR R5 R15                 ; Restore R5
	POPR R4 R15                 ; Restore R4
	POPR R3 R15                 ; Restore R3
	RET R15

:build_union_string0
"ERROR in build_union
Missing {
"
:build_union_string1
"ERROR in build_union
Missing ;
"


;; create_struct function
;; Receives Nothing
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns Nothing
:create_struct
	PUSHR R0 R15                ; Protect R0
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	PUSHR R3 R15                ; Protect R3
	PUSHR R4 R15                ; Protect R4
	PUSHR R5 R15                ; Protect R5
	PUSHR R6 R15                ; Protect R6
	FALSE R5                    ; OFFSET = 0
	FALSE R2                    ; MEMBER_SIZE = 0
	COPY R3 R14                 ; SET HEAD
	ADDUI R14 R14 28            ; CALLOC struct type
	COPY R4 R14                 ; SET I
	ADDUI R14 R14 28            ; CALLOC struct type
	LOAD32 R0 R13 8             ; GLOBAL_TOKEN->S
	STORE32 R0 R3 24            ; HEAD->NAME = GLOBAL_TOKEN->S
	STORE32 R0 R4 24            ; I->NAME = GLOBAL_TOKEN->S
	STORE32 R4 R3 12            ; HEAD->INDIRECT = I
	STORE32 R3 R4 12            ; I->INDIRECT - HEAD
	LOADR32 R0 @global_types    ; Get Address of GLOBAL_TYPES
	STORE R0 R3 0               ; HEAD->NEXT = GLOBAL_TYPES
	STORER32 R3 @global_types   ; GLOBAL_TYPES = HEAD
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	LOADUI R0 4                 ; Standard Pointer SIZE
	STORE32 R0 R4 4             ; I->SIZE = 4
	LOADUI R0 $create_struct_string0 ; ERROR MESSAGE
	LOADUI R1 $open_curly_brace ; OPEN CURLY BRACE
	CALLI R15 @require_match    ; Ensure we have that curly brace
	FALSE R6                    ; LAST = NULL
:create_struct_iter
	LOAD32 R0 R13 8             ; GLOBAL_TOKEN->S
	LOADU8 R0 R0 0              ; GLOBAL_TOKEN->S[0]
	LOADUI R1 125               ; Numerical value of }
	CMPJUMPI.E R0 R1 @create_struct_done ; Stop looping if match
	LOADUI R1 $union            ; Pointer to string UNION
	LOAD32 R0 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @match            ; Check if they Match
	SWAP R0 R6                  ; Put LAST in place
	MOVE R1 R5                  ; Put OFFSET in place
	JUMP.NZ R6 @create_struct_union ; Deal with union case

	;; Deal with standard member case
	CALLI R15 @build_member     ; Sets new LAST and MEMBER_SIZE
	JUMP @create_struct_iter2   ; reset for loop

:create_struct_union
	CALLI R15 @build_union

:create_struct_iter2
	ADD R5 R1 R2                ; OFFSET = OFFSET + MEMBER_SIZE
	SWAP R0 R6                  ; Put LAST in place
	LOADUI R0 $create_struct_string1 ; ERROR MESSAGE
	LOADUI R1 $semicolon        ; SEMICOLON
	CALLI R15 @require_match    ; Ensure we have that semicolon
	JUMP @create_struct_iter    ; Keep Looping

:create_struct_done
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	LOADUI R0 $create_struct_string1 ; ERROR MESSAGE
	LOADUI R1 $semicolon        ; SEMICOLON
	CALLI R15 @require_match    ; Ensure we have that semicolon
	STORE32 R5 R3 4             ; HEAD->SIZE = OFFSET
	STORE32 R6 R3 16            ; HEAD->MEMBERS = LAST
	STORE32 R6 R4 16            ; I->MEMBERS = LAST
	POPR R6 R15                 ; Restore R6
	POPR R5 R15                 ; Restore R5
	POPR R4 R15                 ; Restore R4
	POPR R3 R15                 ; Restore R3
	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	POPR R0 R15                 ; Restore R0
	RET R15

:create_struct_string0
"ERROR in create_struct
Missing {
"
:create_struct_string1
"ERROR in create_struct
Missing ;
"


;; type_name function
;; Receives Nothing
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns struct type* in R0
:type_name
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	LOADUI R0 $struct           ; String for struct for comparison
	LOAD32 R1 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @match            ; Check if they match
	CMPSKIPI.E R0 0             ; If STRUCTURE
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	LOAD32 R2 R13 8             ; GLOBAL_TOKEN->S
	LOADR32 R1 @global_types    ; Check using the GLOBAL TYPES LIST
	SWAP R0 R2                  ; Put GLOBAL_TOKEN->S in the right place
	CALLI R15 @lookup_type      ; RET = lookup_type(GLOBAL_TOKEN->S)
	MOVE R1 R2                  ; Put STRUCTURE in the right place
	CMPSKIP.E R0 R1             ; If RET == NULL and !STRUCTURE
	JUMP @type_name_struct      ; Guess not

	;; Exit with useful error message
	FALSE R1                    ; We will want to be writing the error message for the Human
	LOADUI R0 $type_name_string0 ; The first string
	CALLI R15 @file_print       ; Display it
	LOAD32 R0 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @file_print       ; Display it
	LOADUI R0 $newline          ; Terminating linefeed
	CALLI R15 @file_print       ; Display it
	CALLI R15 @line_error       ; Give useful debug info
	HALT                        ; Just exit

:type_name_struct
	JUMP.NZ R0 @type_name_iter  ; If was found
	CALLI R15 @create_struct    ; Otherwise create it
	JUMP @type_name_done        ; and be done

:type_name_iter
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	LOAD32 R1 R13 8             ; GLOBAL_TOKEN->S
	LOADU8 R1 R1 0              ; GLOBAL_TOKEN->S[0]
	CMPSKIPI.E R1 42            ; if GLOBAL_TOKEN->S[0] == '*'
	JUMP @type_name_done        ; Looks like Nope
	LOAD32 R0 R0 12             ; RET = RET->INDIRECT
	JUMP @type_name_iter        ; Keep looping

:type_name_done
	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	RET R15

:type_name_string0
"Unknown type "


;; line_error function
;; Receives Nothing
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns nothing
:line_error
	PUSHR R0 R15                ; Protect R0
	PUSHR R1 R15                ; Protect R1
	LOADUI R0 $line_error_string0 ; Our leading string
	FALSE R1                    ; We want the user to see
	CALLI R15 @file_print       ; Print it
	LOAD32 R0 R13 16            ; GLOBAL_TOKEN->LINENUMBER
	CALLI R15 @numerate_number  ; Get a string pointer for number
	CALLI R15 @file_print       ; And print it
	POPR R1 R15                 ; Restore R1
	POPR R0 R15                 ; Restore R0
	RET R15

:line_error_string0
	"In file: TTY1 On line: "


;; require_match function
;; Receives char* in R0 and char* in R1
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns Nothing
:require_match
	PUSHR R0 R15                ; Protect R0
	PUSHR R2 R15                ; Protect R2
	MOVE R2 R0                  ; Get MESSAGE out of the way
	LOAD32 R0 R13 8             ; GLOBAL_TOKEN->S
	CALLI R15 @match            ; Check if GLOBAL_TOKEN->S == REQUIRED
	JUMP.NZ R0 @require_match_done ; Looks like it was a match

	;; Terminate with an error
	MOVE R0 R2                  ; Put MESSAGE in required spot
	FALSE R1                    ; We want to write for user
	CALLI R15 @file_print       ; Write it
	CALLI R15 @line_error       ; And provide some debug info
	HALT                        ; Then Stop immediately

:require_match_done
	LOAD32 R13 R13 0            ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
	POPR R2 R15                 ; Restore R2
	POPR R0 R15                 ; Restore R0
	RET R15


;; numerate_number function
;; Receives int in R0
;; R13 Holds pointer to global_token, R14 is HEAP Pointer
;; Returns pointer to string generated
:numerate_number
	PUSHR R1 R15                ; Preserve R1
	PUSHR R2 R15                ; Preserve R2
	PUSHR R3 R15                ; Preserve R3
	PUSHR R4 R15                ; Preserve R4
	PUSHR R5 R15                ; Preserve R5
	PUSHR R6 R15                ; Preserve R6
	MOVE R3 R0                  ; Move Integer out of the way
	COPY R1 R14                 ; Get pointer result
	ADDUI R14 R14 16            ; CALLOC the 16 chars of space
	FALSE R6                    ; Set index to 0

	JUMP.Z R3 @numerate_number_ZERO ; Deal with Special case of ZERO
	JUMP.P R3 @numerate_number_Positive
	LOADUI R0 45                ; Using -
	STOREX8 R0 R1 R6            ; write leading -
	ADDUI R6 R6 1               ; Increment by 1
	NOT R3 R3                   ; Flip into positive
	ADDUI R3 R3 1               ; Adjust twos

:numerate_number_Positive
	LOADR R2 @Max_Decimal       ; Starting from the Top
	LOADUI R5 10                ; We move down by 10
	FALSE R4                    ; Flag leading Zeros

:numerate_number_0
	DIVIDE R0 R3 R3 R2          ; Break off top 10
	CMPSKIPI.E R0 0             ; If Not Zero
	TRUE R4                     ; Flip the Flag

	JUMP.Z R4 @numerate_number_1 ; Skip leading Zeros
	ADDUI R0 R0 48              ; Shift into ASCII
	STOREX8 R0 R1 R6            ; write digit
	ADDUI R6 R6 1               ; Increment by 1

:numerate_number_1
	DIV R2 R2 R5                ; Look at next 10
	CMPSKIPI.E R2 0             ; If we reached the bottom STOP
	JUMP @numerate_number_0     ; Otherwise keep looping

:numerate_number_done
	FALSE R0                    ; NULL Terminate
	STOREX8 R0 R1 R6            ; write
	MOVE R0 R1                  ; Return pointer to our string
	;; Cleanup
	POPR R6 R15                 ; Restore R6
	POPR R5 R15                 ; Restore R5
	POPR R4 R15                 ; Restore R4
	POPR R3 R15                 ; Restore R3
	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	RET R15

:numerate_number_ZERO
	LOADUI R0 48                ; Using Zero
	STOREX8 R0 R1 R6            ; write
	ADDUI R6 R6 1               ; Increment by 1
	JUMP @numerate_number_done  ; Be done

:Max_Decimal
	'3B9ACA00'


;; numerate_string function
;; Receives pointer To string in R0
;; Returns number in R0 equal to value of string
;; Or Zero in the event of invalid string
:numerate_string
	PUSHR R1 R15                ; Protect R1
	PUSHR R2 R15                ; Protect R2
	PUSHR R3 R15                ; Protect R3
	PUSHR R4 R15                ; Protect R4

	;; Initialize
	MOVE R1 R0                  ; Get Text pointer out of the way
	FALSE R2                    ; Set Negative flag to false
	FALSE R3                    ; Set current count to Zero
	LOAD8 R0 R1 1               ; Get second byte
	CMPSKIPI.NE R0 120          ; If the second byte is x
	JUMP @numerate_string_hex   ; treat string like hex

	;; Deal with Decimal input
	LOADUI R4 10                ; Multiply by 10
	LOAD8 R0 R1 0               ; Get a byte
	CMPSKIPI.NE R0 45           ; If - toggle flag
	TRUE R2                     ; So that we know to negate
	CMPSKIPI.E R2 0             ; If toggled
	ADDUI R1 R1 1               ; Move to next
:numerate_string_dec
	LOAD8 R0 R1 0               ; Get a byte

	CMPSKIPI.NE R0 0            ; If NULL
	JUMP @numerate_string_done  ; Be done

	MUL R3 R3 R4                ; Shift counter by 10
	SUBI R0 R0 48               ; Convert ascii to number
	CMPSKIPI.GE R0 0            ; If less than a number
	JUMP @numerate_string_done  ; Terminate NOW
	CMPSKIPI.L R0 10            ; If more than a number
	JUMP @numerate_string_done  ; Terminate NOW
	ADDU R3 R3 R0               ; Don't add to the count

	ADDUI R1 R1 1               ; Move onto next byte
	JUMP @numerate_string_dec

	;; Deal with Hex input
:numerate_string_hex
	LOAD8 R0 R1 0               ; Get a byte
	CMPSKIPI.E R0 48            ; All hex strings start with 0x
	JUMP @numerate_string_done  ; Be done if not a match
	ADDUI R1 R1 2               ; Move to after leading 0x
:numerate_string_hex_0
	LOAD8 R0 R1 0               ; Get a byte
	CMPSKIPI.NE R0 0            ; If NULL
	JUMP @numerate_string_done  ; Be done

	SL0I R3 4                   ; Shift counter by 16
	SUBI R0 R0 48               ; Convert ascii number to number
	CMPSKIPI.L R0 10            ; If A-F
	SUBI R0 R0 7                ; Shove into Range
	CMPSKIPI.L R0 16            ; If a-f
	SUBI R0 R0 32               ; Shove into Range
	ADDU R3 R3 R0               ; Add to the count

	ADDUI R1 R1 1               ; Get next Hex
	JUMP @numerate_string_hex_0

;; Clean up
:numerate_string_done
	CMPSKIPI.E R2 0             ; If Negate flag has been set
	NEG R3 R3                   ; Make the number negative
	MOVE R0 R3                  ; Put number in R0

	POPR R4 R15                 ; Restore R4
	POPR R3 R15                 ; Restore R3
	POPR R2 R15                 ; Restore R2
	POPR R1 R15                 ; Restore R1
	RET R15


;; Keywords
:union
	"union"
:struct
	"struct"
:constant
	"CONSTANT"
:main_string
	"main"
:argc_string
	"argc"
:argv_string
	"argv"
:if_string
	"if"
:else_string
	"else"
:do_string
	"do"
:while_string
	"while"
:for_string
	"for"
:asm_string
	"asm"
:goto_string
	"goto"
:return_string
	"return"
:break_string
	"break"
:continue_string
	"continue"
:sizeof_string
	"sizeof"
:plus_string
	"+"
:minus_string
	"-"
:multiply_string
	"*"
:divide_string
	"/"
:modulus_string
	"%"
:left_shift_string
	"<<"
:right_shift_string
	">>"
:less_than_string
	"<"
:less_than_equal_string
	"<="
:greater_than_equal_string
	">="
:greater_than_string
	">"
:equal_to_string
	"=="
:not_equal_string
	"!="
:bitwise_and
	"&"
:logical_and
	"&&"
:bitwise_or
	"|"
:logical_or
	"||"
:bitwise_xor
	"^"
:arrow_string
	"->"


;; Frequently Used strings
;; Generally used by require_match
:open_curly_brace
	"{"
:close_curly_brace
	"}"
:open_paren
	"("
:close_paren
	")"
:open_bracket
	"["
:close_bracket
	"]"
:semicolon
	";"
:equal
	"="
:percent
	"%"
:newline
	"
"
:underline
	"_"

;; Global types
;; NEXT (0), SIZE (4), OFFSET (8), INDIRECT (12), MEMBERS (16), TYPE (20), NAME (24)
:global_types
	&type_void

:prim_types
:type_void
	&type_int                   ; NEXT
	'00 00 00 04'               ; SIZE
	NOP                         ; OFFSET
	&type_void                  ; INDIRECT
	NOP                         ; MEMBERS
	&type_void                  ; TYPE
	&type_void_name             ; NAME
:type_void_name
	"void"

:type_int
	&type_char                  ; NEXT
	'00 00 00 04'               ; SIZE
	NOP                         ; OFFSET
	&type_int                   ; INDIRECT
	NOP                         ; MEMBERS
	&type_int                   ; TYPE
	&type_int_name              ; NAME
:type_int_name
	"int"

:type_char
	&type_file                  ; NEXT
	'00 00 00 01'               ; SIZE
	NOP                         ; OFFSET
	&type_char_indirect         ; INDIRECT
	NOP                         ; MEMBERS
	&type_char                  ; TYPE
	&type_char_name             ; NAME
:type_char_name
	"char"

:type_char_indirect
	&type_file                  ; NEXT
	'00 00 00 04'               ; SIZE
	NOP                         ; OFFSET
	&type_char_double_indirect  ; INDIRECT
	NOP                         ; MEMBERS
	&type_char_indirect         ; TYPE
	&type_char_indirect_name    ; NAME
:type_char_indirect_name
	"char*"

:type_char_double_indirect
	&type_file                  ; NEXT
	'00 00 00 04'               ; SIZE
	NOP                         ; OFFSET
	&type_char_double_indirect  ; INDIRECT
	NOP                         ; MEMBERS
	&type_char_indirect         ; TYPE
	&type_char_double_indirect_name ; NAME
:type_char_double_indirect_name
	"char**"

:type_file
	&type_function              ; NEXT
	'00 00 00 04'               ; SIZE
	NOP                         ; OFFSET
	&type_file                  ; INDIRECT
	NOP                         ; MEMBERS
	&type_file                  ; TYPE
	&type_file_name             ; NAME
:type_file_name
	"FILE"

:type_function
	&type_unsigned              ; NEXT
	'00 00 00 04'               ; SIZE
	NOP                         ; OFFSET
	&type_function              ; INDIRECT
	NOP                         ; MEMBERS
	&type_function              ; TYPE
	&type_function_name         ; NAME
:type_function_name
	"FUNCTION"

:type_unsigned
	NOP                         ; NEXT (NULL)
	'00 00 00 04'               ; SIZE
	NOP                         ; OFFSET
	&type_unsigned              ; INDIRECT
	NOP                         ; MEMBERS
	&type_unsigned              ; TYPE
	&type_unsigned_name         ; NAME
:type_unsigned_name
	"unsigned"


;; debug_list function
;; Receives struct token_list* in R0
;; Prints contents of list and HALTS
;; Does not return
:debug_list
	MOVE R9 R0                  ; Protect the list Pointer
	FALSE R1                    ; Write to TTY

:debug_list_iter
	;; Header
	LOADUI R0 $debug_list_string0 ; Using our first string
	CALLI R15 @file_print       ; Print it
	COPY R0 R9                  ; Use address of pointer
	CALLI R15 @numerate_number  ; Convert it into a string
	CALLI R15 @file_print       ; Print it

	;; NEXT
	LOADUI R0 $debug_list_string1 ; Using our second string
	CALLI R15 @file_print       ; Print it
	LOAD32 R0 R9 0              ; Use address of pointer
	CALLI R15 @numerate_number  ; Convert it into a string
	CALLI R15 @file_print       ; Print it

	;; PREV
	LOADUI R0 $debug_list_string2 ; Using our third string
	CALLI R15 @file_print       ; Print it
	LOAD32 R0 R9 4              ; Use address of pointer
	CALLI R15 @numerate_number  ; Convert it into a string
	CALLI R15 @file_print       ; Print it

	;; S
	LOADUI R0 $debug_list_string3 ; Using our fourth string
	CALLI R15 @file_print       ; Print it
	LOAD32 R0 R9 8              ; Use address of pointer
	CALLI R15 @numerate_number  ; Convert it into a string
	CALLI R15 @file_print       ; Print it

	;; S Contents
	LOADUI R0 $debug_list_string4 ; Using our Prefix string
	CALLI R15 @file_print       ; Print it
	LOAD32 R0 R9 8              ; Use address of pointer
	CMPSKIPI.NE R0 0            ; If NULL Pointer
	LOADUI R0 $debug_list_string_null ; Give meaningful message instead
	CALLI R15 @file_print       ; Print it

	;; TYPE
	LOADUI R0 $debug_list_string5 ; Using our fifth string
	CALLI R15 @file_print       ; Print it
	LOAD32 R0 R9 12             ; Use address of pointer
	CALLI R15 @numerate_number  ; Convert it into a string
	CALLI R15 @file_print       ; Print it

	;; PREV
	LOADUI R0 $debug_list_string6 ; Using our sixth string
	CALLI R15 @file_print       ; Print it
	LOAD32 R0 R9 16             ; Use address of pointer
	CALLI R15 @numerate_number  ; Convert it into a string
	CALLI R15 @file_print       ; Print it

	;; Add some space
	LOADUI R0 10                ; Using NEWLINE
	FPUTC
	FPUTC

	;; Iterate if next not NULL
	LOAD32 R9 R9 0              ; TOKEN = TOKEN->NEXT
	JUMP.NZ R9 @debug_list_iter

	;; Looks lke we are done, wrap it up
	HALT


:debug_list_string0
"Token_list node at address: "
:debug_list_string1
	"
NEXT address: "
:debug_list_string2
	"
PREV address: "

:debug_list_string3
	"
S address: "

:debug_list_string4
	"
The contents of S are: "

:debug_list_string5
	"
TYPE address: "

:debug_list_string6
	"
ARGUMENTS address: "

:debug_list_string_null
	">::<NULL>::<"

:STACK