File: compiler_peg_mecpu.tcl

package info (click to toggle)
tcllib 1.20%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 68,064 kB
  • sloc: tcl: 216,842; ansic: 14,250; sh: 2,846; xml: 1,766; yacc: 1,145; pascal: 881; makefile: 107; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (1642 lines) | stat: -rw-r--r-- 40,759 bytes parent folder | download | duplicates (8)
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
# -*- tcl -*-
#
# Copyright (c) 2005 by Andreas Kupries <andreas_kupries@users.sourceforge.net>
# Parser Generator / Transformation - Compile grammar to ME cpu instructions.

# This package assumes to be used from within a PAGE plugin. It uses
# the API commands listed below. These are identical across the major
# types of PAGE plugins, allowing this package to be used in reader,
# transform, and writer plugins. It cannot be used in a configuration
# plugin, and this makes no sense either.
#
# To ensure that our assumption is ok we require the relevant pseudo
# package setup by the PAGE plugin management code.
#
# -----------------+--
# page_info        | Reporting to the user.
# page_warning     |
# page_error       |
# -----------------+--
# page_log_error   | Reporting of internals.
# page_log_warning |
# page_log_info    |
# -----------------+--

# ### ### ### ######### ######### #########
## Dumping the input grammar. But not as Tcl or other code. In PEG
## format again, pretty printing.

# ### ### ### ######### ######### #########
## Requisites

# @mdgen NODEP: page::plugin

package require page::plugin ; # S.a. pseudo-package.

package require grammar::me::cpu::gasm
package require textutil
package require struct::graph

package require page::analysis::peg::emodes
package require page::util::quote
package require page::util::peg

namespace eval ::page::compiler::peg::mecpu {
    # Get the peg char de/encoder commands.
    # (unquote, quote'tcl)

    namespace import ::page::util::quote::*
    namespace import ::page::util::peg::*


    namespace eval gas {
	namespace import ::grammar::me::cpu::gas::begin
	namespace import ::grammar::me::cpu::gas::done
	namespace import ::grammar::me::cpu::gas::lift
	namespace import ::grammar::me::cpu::gas::state
	namespace import ::grammar::me::cpu::gas::state!
    }
    namespace import ::grammar::me::cpu::gas::*
    rename begin  {}
    rename done   {}
    rename lift   {}
    rename state  {}
    rename state! {}
}

# ### ### ### ######### ######### #########
## Data structures for the generated code.

## All data is held in node attributes of the tree. Per node:
##
## asm - List of instructions implementing the node.



# ### ### ### ######### ######### #########
## API

proc ::page::compiler::peg::mecpu {t} {
    # Resolve the mode hints. Every gen(X) having a value of 'maybe'
    # (or missing) is for the purposes of this code a 'yes'.

    if {![page::analysis::peg::emodes::compute $t]} {
	page_error "  Unable to generate a ME parser without accept/generate properties"
	return
    }

    foreach n [$t nodes] {
	if {![$t keyexists $n gen] || ([$t get $n gen] eq "maybe")} {
	    $t set $n gen 1
	}
	if {![$t keyexists $n acc]} {$t set $n acc 1}
    }

    # Synthesize a program, then the assembly code.

    mecpu::Synth $t
    return
}

# ### ### ### ######### ######### #########
## Internal. Helpers

proc ::page::compiler::peg::mecpu::Synth {t} {
    # Phase 2: Bottom-up, synthesized attributes

    # We use a global graph to capture instructions and their
    # relations. The graph is then converted into a linear list of
    # instructions, with proper labeling and jump instructions to
    # handle all non-linear control-flow.

    set g [struct::graph g]
    $t set root gas::called {}

    page_info "* Synthesize graph code"

    $t walk root -order post -type dfs n {
	SynthNode $n
    }

    status             $g  ;  gdump $g synth
    remove_unconnected $g  ;  gdump $g nounconnected
    remove_dead        $g  ;  gdump $g nodead
    denop              $g  ;  gdump $g nonops
    parcmerge          $g  ;  gdump $g parcmerge
    forwmerge          $g  ;  gdump $g fmerge
    backmerge          $g  ;  gdump $g bmerge
    status             $g  
    pathlengths        $g  ;  gdump $g pathlen
    jumps              $g  ;  gdump $g jumps
    status             $g
    symbols            $g $t

    set cc [2code $t $g]
    #write asm/mecode [join $cc \n]

    statistics $cc

    $t set root asm $cc
    $g destroy
    return
}

proc ::page::compiler::peg::mecpu::SynthNode {n} {
    upvar 1 t t g g
    if {$n eq "root"} {
	set code Root
    } elseif {[$t keyexists $n symbol]} {
	set code Nonterminal
    } elseif {[$t keyexists $n op]} {
	set code [$t get $n op]
    } else {
	return -code error "PANIC. Bad node $n, cannot classify"
    }

    page_log_info "  [np $n] := ([linsert [$t children $n] 0 $code])"

    SynthNode/$code $n
    return
}

proc ::page::compiler::peg::mecpu::SynthNode/Root {n} {
    upvar 1 t t g g

    # Root is the grammar itself.

    set gstart [$t get root start]
    set gname  [$t get root name]

    if {$gstart eq ""} {
	page_error "  No start expression."
	return
    }

    gas::begin $g $n halt "<Start Expression> '$gname'"
    $g node set [Who entry] instruction .C
    $g node set [Who entry] START .

    Inline $t $gstart sexpr
    /At sexpr/exit/ok   ; /Ok   ; Jmp exit/return
    /At sexpr/exit/fail ; /Fail ; Jmp exit/return

    gas::done --> $t
    return
}

proc ::page::compiler::peg::mecpu::SynthNode/Nonterminal {n} {
    upvar 1 t t g g

    # This is the root of a definition.
    #
    # The text is a procedure wrapping the match code of its
    # expression into the required the nonterminal handling (caching
    # and such), plus the support code for the expression matcher.

    set sym      [$t get $n symbol]
    set label    [$t get $n label]
    set gen      [$t get $n gen]
    set mode     [$t get $n mode]

    set pe       [lindex [$t children $n] 0]
    set egen     [$t get $pe gen]

    # -> inc_restore -found-> NOP  gen:  -> ok -> ias_push -> RETURN
    #               /!found             \                  /
    #              /                     \-fail --------->/
    #             /               !gen: -> RETURN
    #            /
    #            \-> icl_push (-> ias_mark) -> (*) -> SV -> inc_save (-> ias_mrewind) -X
    #
    # X -ok----> ias_push -> ier_nonterminal
    #  \                  /
    #   \-fail ----------/

    # Poking into the generated instructions, converting the initial
    # .NOP into a .C'omment.

    set first [gas::begin $g $n !okfail "Nonterminal '$sym'"]
    $g node set [Who entry] instruction .C
    $g node set [Who entry] START .

    Cmd inc_restore $label ; /Label restore ; /Ok

    if {$gen} {
	Bra ; /Label @
	/Fail ; Nop          ; Exit
	/At @
	/Ok   ; Cmd ias_push ; Exit
    } else {
	Nop ; Exit
    }

    /At restore ; /Fail
    Cmd icl_push ; # Balanced by inc_save (XX)
    Cmd icl_push ; # Balanced by pop after ier_terminal

    if {$egen} {
	# [*] Needed for removal of SV's from stack after handling by
	# this symbol, only if expression actually generates an SV.

	Cmd ias_mark
    }

    Inline $t $pe subexpr ; /Ok   ; Nop ; /Label unified
    /At subexpr/exit/fail ; /Fail ; Jmp unified
    /At unified

    switch -exact -- $mode {
	value   {Cmd isv_nonterminal_reduce $label}
	match   {Cmd isv_nonterminal_range  $label}
	leaf    {Cmd isv_nonterminal_leaf   $label}
	discard {Cmd isv_clear}
	default {return -code error "Bad nonterminal mode \"$mode\""}
    }

    Cmd inc_save $label ; # Implied icl_pop (XX)

    if {$egen} {
	# See [*], this is the removal spoken about before.
	Cmd ias_mrewind
    }

    /Label hold

    if {$gen} {
	/Ok
	Cmd ias_push
	Nop           ; /Label merge
	/At hold ; /Fail ; Jmp merge
	/At merge
    }

    Cmd ier_nonterminal "Expected $label"
    Cmd icl_pop
    Exit

    gas::done --> $t
    return
}

proc ::page::compiler::peg::mecpu::SynthNode/? {n} {
    upvar 1 t t g g

    # The expression e? is equivalent to e/epsilon.
    # And like this it is compiled.

    set pe       [lindex [$t children $n] 0]

    gas::begin $g $n okfail ?

    # -> icl_push -> ier_push -> (*) -ok--> ier_merge/ok --> icl_pop -ok----------------> OK
    #                             \                                                    /
    #                              \-fail-> ier_merge/f ---> icl_rewind -> iok_ok -ok-/

    Cmd icl_push
    Cmd ier_push

    Inline $t $pe subexpr

    /Ok
    Cmd ier_merge
    Cmd icl_pop
    /Ok ; Exit

    /At subexpr/exit/fail ; /Fail
    Cmd ier_merge
    Cmd icl_rewind
    Cmd iok_ok
    /Ok ; Exit

    gas::done --> $t
    return
}

proc ::page::compiler::peg::mecpu::SynthNode/* {n} {
    upvar 1 t t g g

    # Kleene star is like a repeated ?

    # Note: Compilation as while loop, as done now
    # means that the parser has no information about
    # the intermediate structure of the input in his
    # cache.

    # Future: Create a helper symbol X and compile
    # the expression e = e'* as:
    #     e = X; X <- (e' X)?
    # with match data for X put into the cache. This
    # is not exactly equivalent, the structure of the
    # AST is different (right-nested tree instead of
    # a list). This however can be handled with a
    # special nonterminal mode to expand the current
    # SV on the stack.

    # Note 2: This is a transformation which can be
    # done on the grammar itself, before the actual
    # backend is let loose. This "strength reduction"
    # allows us to keep this code here.

    set pe       [lindex [$t children $n] 0]
    set egen     [$t get $pe gen]

    # Build instruction graph.

    #  /<---------------------------------------------------------------\
    #  \_                                                                \_
    # ---> icl_push -> ier_push -> (*) -ok--> ier_merge/ok --> icl_pop ->/
    #                               \
    #                                \-fail-> ier_merge/f ---> icl_rewind -> iok_ok -> OK

    gas::begin $g $n okfail *

    Cmd icl_push ; /Label header
    Cmd ier_push

    Inline $t $pe loop

    /Ok
    Cmd ier_merge
    Cmd icl_pop
    Jmp header ; /CloseLoop

    /At loop/exit/fail ; /Fail
    Cmd ier_merge
    Cmd icl_rewind
    Cmd iok_ok
    /Ok ; Exit

    gas::done --> $t
    return
}

proc ::page::compiler::peg::mecpu::SynthNode/+ {n} {
    upvar 1 t t g g

    # Positive Kleene star x+ is equivalent to x x*
    # This is how it is compiled. See also the notes
    # at the * above, they apply in essence here as
    # well, except that the transformat scheme is
    # slighty different:
    #
    # e = e'*  ==> e = X; X <- e' X?

    set pe [lindex [$t children $n] 0]

    # Build instruction graph.

    # icl_push -> ier_push -> (*) -fail-> ier_merge/fl -> icl_rewind -> FAIL
    #                          \
    #                           \--ok---> ier_merge/ok -> icl_pop ->\_
    #                                                               /
    #    /<--------------------------------------------------------/
    #   /
    #  /<---------------------------------------------------------------\
    #  \_                                                                \_
    #   -> icl_push -> ier_push -> (*) -ok--> ier_merge/ok --> icl_pop ->/
    #                               \
    #                                \-fail-> ier_merge/f ---> icl_rewind -> iok_ok -> OK

    gas::begin $g $n okfail +

    Cmd icl_push
    Cmd ier_push

    Inline $t $pe first
    /At first/exit/fail ; /Fail
    Cmd ier_merge
    Cmd icl_rewind
    /Fail ; Exit

    /At first/exit/ok ; /Ok
    Cmd ier_merge
    Cmd icl_pop

    # Loop copied from Kleene *, it is *

    Cmd icl_push ; /Label header
    Cmd ier_push

    # For the loop we create the sub-expression instruction graph a
    # second time. This is done by walking the subtree a second time
    # and constructing a completely new node set. The result is
    # imported under a new name.

    set save [gas::state]
    $t walk $pe -order post -type dfs n {SynthNode $n}
    gas::state! $save
    Inline $t $pe loop

    /Ok
    Cmd ier_merge
    Cmd icl_pop
    Jmp header ; /CloseLoop

    /At loop/exit/fail ; /Fail
    Cmd ier_merge
    Cmd icl_rewind
    Cmd iok_ok
    /Ok ; Exit

    gas::done --> $t
    return
}

proc ::page::compiler::peg::mecpu::SynthNode// {n} {
    upvar 1 t t g g

    set args [$t children $n]

    if {![llength $args]} {
	error "PANIC. Empty choice."

    } elseif {[llength $args] == 1} {
	# A choice over one branch is no real choice. The code
	# generated for the child applies here as well.

	gas::lift $t $n <-- [lindex $args 0]
	return
    }

    # Choice over at least two branches.
    # Build instruction graph.

    # -> BRA
    #
    # BRA -> icl_push (-> ias_mark) -> ier_push -> (*) -ok -> ier_merge -> BRA'OK
    #                                              \-fail -> ier_merge (-> ias_mrewind) -> icl_rewind -> BRA'FAIL
    #
    # BRA'FAIL -> BRA
    # BRA'FAIL -> FAIL (last branch)
    #
    # BRA'OK -> icl_pop -> OK

    gas::begin $g $n okfail /

    /Clear
    Cmd icl_pop ; /Label BRA'OK ; /Ok ; Exit
    /At entry

    foreach pe $args {
	set egen [$t get $pe gen]

	# Note: We do not check for static match results. Doing so is
	# an optimization we can do earlier, directly on the tree.

	Cmd icl_push
	if {$egen} {Cmd ias_mark}

	Cmd ier_push
	Inline $t $pe subexpr

	/Ok
	Cmd ier_merge
	Jmp BRA'OK

	/At subexpr/exit/fail ; /Fail
	Cmd ier_merge
	if {$egen} {Cmd ias_mrewind}
	Cmd icl_rewind

	# Branch failed. Go to the next branch. Fail completely at
	# last branch.
    }

    /Fail ; Exit

    gas::done --> $t
    return
}

proc ::page::compiler::peg::mecpu::SynthNode/x {n} {
    upvar 1 t t g g

    set args [$t children $n]

    if {![llength $args]} {
	error "PANIC. Empty sequence."

    } elseif {[llength $args] == 1} {
	# A sequence of one element is no real sequence. The code
	# generated for the child applies here as well.

	gas::lift $t $n <-- [lindex $args 0]
	return
    }

    # Sequence of at least two elements.
    # Build instruction graph.

    # -> icl_push -> SEG
    #
    # SEG (-> ias_mark) -> ier_push -> (*) -ok -> ier_merge -> SEG'OK
    #                                  \-fail -> ier_merge -> SEG'FAIL
    #
    # SEG'OK -> SEG
    # SEG'OK -> icl_pop -> OK (last segment)
    #
    # SEG'FAIL (-> ias_mrewind) -> icl_rewind -> FAIL

    gas::begin $g $n okfail x

    /Clear
    Cmd icl_rewind ; /Label SEG'FAIL ; /Fail ; Exit

    /At entry
    Cmd icl_push

    set gen 0
    foreach pe $args {
	set egen [$t get $pe gen]
	if {$egen && !$gen} {
	    set gen 1

	    # From here on out is the sequence able to generate
	    # semantic values which have to be canceled when
	    # backtracking.

	    Cmd ias_mark ; /Label @mark

	    /Clear
	    Cmd ias_mrewind ; Jmp SEG'FAIL ; /Label SEG'FAIL

	    /At @mark
	}

	Cmd ier_push
	Inline $t $pe subexpr

	/At subexpr/exit/fail ; /Fail
	Cmd ier_merge
	Jmp SEG'FAIL

	/At subexpr/exit/ok ; /Ok
	Cmd ier_merge 
    }

    Cmd icl_pop
    /Ok ; Exit

    gas::done --> $t
    return
}

proc ::page::compiler::peg::mecpu::SynthNode/& {n} {
    upvar 1 t t g g
    SynthLookahead $n no
    return
}

proc ::page::compiler::peg::mecpu::SynthNode/! {n} {
    upvar 1 t t g g
    SynthLookahead $n yes
    return
}

proc ::page::compiler::peg::mecpu::SynthNode/dot {n} {
    upvar 1 t t g g
    SynthTerminal $n {} "any character"
    return
}

proc ::page::compiler::peg::mecpu::SynthNode/epsilon {n} {
    upvar 1 t t g g

    gas::begin $g $n okfail epsilon

    Cmd iok_ok ; /Ok ; Exit

    gas::done --> $t
    return
}

proc ::page::compiler::peg::mecpu::SynthNode/alnum {n} {
    upvar 1 t t g g
    SynthClass $n alnum
    return
}

proc ::page::compiler::peg::mecpu::SynthNode/alpha {n} {
    upvar 1 t t g g
    SynthClass $n alpha
    return
}

proc ::page::compiler::peg::mecpu::SynthNode/digit {n} {
    upvar 1 t t g g
    SynthClass $n digit
    return
}

proc ::page::compiler::peg::mecpu::SynthNode/xdigit {n} {
    upvar 1 t t g g
    SynthClass $n xdigit
    return
}

proc ::page::compiler::peg::mecpu::SynthNode/punct {n} {
    upvar 1 t t g g
    SynthClass $n punct
    return
}

proc ::page::compiler::peg::mecpu::SynthNode/space {n} {
    upvar 1 t t g g
    SynthClass $n space
    return
}

proc ::page::compiler::peg::mecpu::SynthNode/.. {n} {
    upvar 1 t t g g
    # Range is [x-y]

    set b [$t get $n begin]
    set e [$t get $n end]

    set tb [quote'tcl $b]
    set te [quote'tcl $e]

    set pb [quote'tclstr $b]
    set pe [quote'tclstr $e]

    SynthTerminal $n [list ict_match_tokrange $tb $te] "\\\[${pb}..${pe}\\\]"
    return
}

proc ::page::compiler::peg::mecpu::SynthNode/t {n} {
    upvar 1 t t g g

    # Terminal node. Primitive matching.
    # Code is parameterized by gen(X) of this node X.

    set ch  [$t get $n char]
    set tch [quote'tcl    $ch]
    set pch [quote'tclstr $ch]

    SynthTerminal $n [list ict_match_token $tch] $pch
    return
}

proc ::page::compiler::peg::mecpu::SynthNode/n {n} {
    upvar 1 t t g g

    # Nonterminal node. Primitive matching.
    # The code is parameterized by acc(X) of this node X, and gen(D)
    # of the invoked nonterminal D.

    set sym   [$t get $n sym]
    set def   [$t get $n def]

    gas::begin $g $n okfail call'$sym'

    if {$def eq ""} {
	# Invokation of an undefined nonterminal. This will always fail.

	Note "Match for undefined symbol '$sym'"
	Cmdd iok_fail ; /Fail ; Exit
	gas::done --> $t

    } else {
	# Combinations
	# Acc Gen Action
	# --- --- ------
	#   0   0 Plain match
	#   0   1 Match with canceling of the semantic value.
	#   1   0 Plain match
	#   1   1 Plain match
	# --- --- ------

	if {[$t get $n acc] || ![$t get $def gen]} {
	    Cmd icf_ntcall sym_$sym ; /Label CALL
	    /Ok   ; Exit
	    /Fail ; Exit

	} else {
	    Cmd ias_mark
	    Cmd icf_ntcall sym_$sym ; /Label CALL
	    Cmd ias_mrewind
	    /Ok   ; Exit
	    /Fail ; Exit
	}

	set caller [Who CALL]
	gas::done --> $t

	$t lappend $def gas::callers $caller
	$t lappend root gas::called  $def
    }

    return
}

proc ::page::compiler::peg::mecpu::SynthLookahead {n negated} {
    upvar 1 g g t t

    # Note: Per the rules about expression modes (! is a lookahead
    # ____| operator) this node has a mode of 'discard', and its child
    # ____| has so as well.

    # assert t get n  mode == discard
    # assert t get pe mode == discard

    set op       [$t get $n op]
    set pe       [lindex [$t children $n] 0]
    set eop      [$t get $pe op]

    # -> icl_push -> (*) -ok--> icl_rewind -> OK
    #                 \--fail-> icl_rewind -> FAIL

    # -> icl_push -> (*) -ok--> icl_rewind -> iok_negate -> FAIL
    #                 \--fail-> icl_rewind -> iok_negate -> OK

    gas::begin $g $n okfail [expr {$negated ? "!" : "&"}]

    Cmd icl_push
    Inline $t $pe subexpr

    /Ok
    Cmd icl_rewind
    if {$negated} { Cmd iok_negate ; /Fail } else /Ok ; Exit

    /At subexpr/exit/fail ; /Fail
    Cmd icl_rewind
    if {$negated} { Cmd iok_negate ; /Ok } else /Fail ; Exit

    gas::done --> $t
    return
}

proc ::page::compiler::peg::mecpu::SynthClass {n op} {
    upvar 1 t t g g
    SynthTerminal $n [list ict_match_tokclass $op] <$op>
    return
}

proc ::page::compiler::peg::mecpu::SynthTerminal {n cmd msg} {
    upvar 1 t t g g

    # 4 cases (+/- cmd, +/- sv).
    #
    # (A) +cmd+sv
    #     entry -> advance -ok-> match -ok-> sv -> OK
    #              \             \
    #               \             \-fail----------> FAIL
    #                \-fail----------------------/
    #
    # (B) -cmd+sv
    #     entry -> advance -ok-> sv -> OK
    #              \
    #               \-fail-----------> FAIL
    #
    # (C) +cmd-sv
    #     entry -> advance -ok-> match -ok-> OK
    #              \             \
    #               \             \-fail---> FAIL
    #                \-fail---------------/
    #
    # (D) -cmd-sv
    #     entry -> advance -ok-> OK
    #              \
    #               \-fail-----> FAIL

    gas::begin $g $n okfail M'[lindex $cmd 0]

    Cmd ict_advance "Expected $msg (got EOF)"
    /Fail ; Exit
    /Ok

    if {[llength $cmd]} {
	lappend cmd "Expected $msg"
	eval [linsert $cmd 0 Cmd]
	/Fail ; Exit
	/Ok
    }

    if {[$t get $n gen]} {
	Cmd isv_terminal
	/Ok
    }

    Exit

    gas::done --> $t
    return
}

# ### ### ### ######### ######### #########
## Internal. Extending the graph of instructions (expression
## framework, new instructions, (un)conditional sequencing).

# ### ### ### ######### ######### #########
## Internal. Working on the graph of instructions.

proc ::page::compiler::peg::mecpu::2code {t g} {
    page_info "* Generating ME assembler code"

    set insn  {}
    set start [$t get root gas::entry]
    set cat 0
    set calls [list $start]

    while {$cat < [llength $calls]} {
	set  now [lindex $calls $cat]
	incr cat

	set at 0
	set pending [list $now]

	while {$at < [llength $pending]} {
	    set  current [lindex $pending $at]
	    incr at

	    while {$current ne ""} {
		if {[$g node keyexists $current WRITTEN]} break

		insn $g $current insn
		$g node set $current WRITTEN .

		if {[$g node keyexists $current SAVE]} {
		    lappend pending [$g node get $current SAVE]
		}
		if {[$g node keyexists $current CALL]} {
		    lappend calls [$g node get $current CALL]
		}

		set  current [$g node get $current NEXT]
		if {$current eq ""} break
		if {[$g node keyexists $current WRITTEN]} {
		    lappend insn [list {} icf_jalways \
			    [$g node get $current LABEL]]
		    break
		}

		# Process the following instruction,
		# if there is any.
	    }
	}
    }

    return $insn
}

proc ::page::compiler::peg::mecpu::insn {g current iv} {
    upvar 1 $iv insn

    set code [$g node get $current instruction]
    set args [$g node get $current arguments]

    set label {}
    if {[$g node keyexists $current LABEL]} {
	set label [$g node get $current LABEL]
    }

    lappend insn [linsert $args 0 $label $code]
    return
}

if 0 {
    if {[lindex $ins 0] eq "icf_ntcall"} {
	set tmp {}
	foreach b $branches {
	    if {[$g node keyexists $b START]} {
		set sym [$g node get $b symbol]
		lappend ins     sym_$sym
	    } else {
		lappend tmp $b
	    }
	}
	set branches $tmp
    }
}

# ### ### ### ######### ######### #########
## Optimizations.
#
## I. Remove all nodes which are not connected to anything.
##    There should be none.

proc ::page::compiler::peg::mecpu::remove_unconnected {g} {
    page_info "* Remove unconnected instructions"

    foreach n [$g nodes] {
	if {[$g node degree $n] == 0} {
	    page_error "$n ([printinsn $g $n])"
	    page_error "Found unconnected node. This should not have happened."
	    page_error "Removing the bad node."

	    $g node delete $n
	}
    }
}

proc ::page::compiler::peg::mecpu::remove_dead {g} {
    page_info "* Remove dead instructions"

    set count 0
    set runs 0
    set hasdead 1
    while {$hasdead} {
	set hasdead 0
	foreach n [$g nodes] {
	    if {[$g node keyexists $n START]} continue
	    if {[$g node degree -in $n] > 0}  continue

	    page_log_info "    [np $n] removed, dead ([printinsn $g $n])"

	    $g node delete $n

	    set hasdead 1
	    incr count
	}
	incr runs
    }

    page_info "  Removed [plural $count instruction] in [plural $runs run]"
    return
}

# ### ### ### ######### ######### #########
## Optimizations.
#
## II. We have lots of .NOP instructions in the control flow, as part
##     of the framework. They made the handling of expressions easier,
##     providing clear and fixed anchor nodes to connect to from
##     inside and outside, but are rather like the epsilon-transitions
##     in a (D,N)FA. Now is the time to get rid of them.
#
##     We keep the .C'omments, and explicit .BRA'nches.
##     We should not have any .NOP which is a dead-end (without
##     successor), nor should we find .NOPs with more than one
##     successor. The latter should have been .BRA'nches. Both
##     situations are reported on. Dead-ends we
##     remove. Multi-destination NOPs we keep.
#
##     Without the nops in place to confus the flow we can perform a
##     series peep-hole optimizations to merge/split branches.

proc ::page::compiler::peg::mecpu::denop {g} {
    # Remove the .NOPs and reroute control flow. We keep the pseudo
    # instructions for comments (.C) and the explicit branch points
    # (.BRA).

    page_info "* Removing the helper .NOP instructions."

    set count 0
    foreach n [$g nodes] {
	# Skip over nodes already deleted by a previous iteration.
	if {[$g node get $n instruction] ne ".NOP"} continue

	# We keep branching .NOPs, and warn user. There shouldn't be
	# any. such should explicit bnrachpoints.

	set destinations [$g arcs -out $n]

	if {[llength $destinations] > 1} {
	    page_error "$n ([printinsn $g $n])"
	    page_error "Found a .NOP with more than one destination."
	    page_error "This should have been a .BRA instruction."
	    page_error "Not removed. Internal error. Fix the transformation."
	    continue
	}

	# Nops without a destination, dead-end's are not wanted. They
	# should not exist either too. We will do a general dead-end
	# and dead-start removal as well.

	if {[llength $destinations] < 1} {
	    page_error "$n ([printinsn $g $n])"
	    page_error "Found a .NOP without any destination, i.e. a dead end."
	    page_error "This should not have happened. Removed the node."

	    $g node delete $n
	    continue
	}

	page_log_info "    [np $n] removed, updated cflow ([printinsn $g $n])"

	# As there is exactly one destination we can now reroute all
	# incoming arcs around the nop to the new destination.

	set target [$g arc target [lindex $destinations 0]]
	foreach a [$g arcs -in $n] {
	    $g arc move-target $a $target
	}

	$g node delete $n
	incr count
    }

    page_info "  Removed [plural $count instruction]"
    return
}


# ### ### ### ######### ######### #########
## Optimizations.
#

# Merge parallel arcs (remove one, make the other unconditional).

proc ::page::compiler::peg::mecpu::parcmerge {g} {
    page_info "* Search for identical parallel arcs and merge them"

    #puts [join  [info loaded] \n] /seg.fault induced with tcllibc! - tree!

    set count 0
    foreach n [$g nodes] {
	set arcs [$g arcs -out $n]

	if {[llength $arcs] < 2} continue
	if {[llength $arcs] > 2} {
	    page_error "  $n ([printinsn $g $n])"
	    page_error "  Instruction has more than two destinations."
	    page_error "  That is not possible. Internal error."
	    continue
	}
	# Two way branch. Both targets the same ?

	foreach {a b} $arcs break

	if {[$g arc target $a] ne [$g arc target $b]} continue

	page_log_info "    [np $n] outbound arcs merged ([printinsn $g $n])"

	$g arc set $a condition always
	$g arc delete $b

	incr count 2
    }

    page_info "  Merged [plural $count arc]"
    return
}

# Use knowledge of the match status before and after an instruction to
# label the arcs a bit better (This may guide the forward and backward
# merging.).

# Forward merging of instructions.
# An ok/fail decision is done as late as possible.
#
#  /- ok ---> Y -> U               /- ok ---> U
# X                    ==>   X -> Y
#  \- fail -> Y -> V               \- fail -> V

# The Y must not have additional inputs. This more complex case we
# will look at later.

proc ::page::compiler::peg::mecpu::forwmerge {g} {
    page_info "* Forward merging of identical instructions"
    page_info "  Delaying decisions"
    set count 0
    set runs 0

    set merged 1
    while {$merged} {
	set merged 0
	foreach n [$g nodes] {
	    # Skip nodes already killed in previous rounds.
	    if {![$g node exists $n]} continue

	    set outbound [$g arcs -out $n]
	    if {[llength $outbound] != 2} continue

	    foreach {aa ab} $outbound break
	    set na [$g arc target $aa]
	    set nb [$g arc target $ab]

	    set ia [$g node get $na instruction][$g node get $na arguments]
	    set ib [$g node get $nb instruction][$g node get $nb arguments]
	    if {$ia ne $ib} continue

	    # Additional condition: Inbounds in the targets not > 1

	    if {([$g node degree -in $na] > 1) ||
		([$g node degree -in $nb] > 1)} continue

	    page_log_info "    /Merge [np $n] : [np $na] <- [np $nb] ([printinsn $g $na])"

	    # Label all arcs out of na with the condition of the arc
	    # into it.  Ditto for the arcs out of nb. The latter also
	    # get na as their new origin. The arcs out of n relabeled
	    # to always. The nb is deleted. This creates the desired
	    # control structure without having to create a new node
	    # and filling it. We simply use na, discard nb, and
	    # properly rewrite the arcs to have the correct
	    # conditions.

	    foreach a [$g arcs -out $na] {
		$g arc set $a condition [$g arc get $aa condition]
	    }
	    foreach a [$g arcs -out $nb] {
		$g arc set $a condition [$g arc get $ab condition]
		$g arc move-source $a $na
	    }
	    $g arc set     $aa condition always
	    $g node delete $nb
	    set merged 1
	    incr count
	}
	incr runs
    }

    # NOTE: This may require a parallel arc merge, with identification
    #       of merge-able arcs based on the arc condition, i.e. labeling.

    page_info "  Merged [plural $count instruction] in [plural $runs run]"
    return
}

# Backward merging of instructions.
# Common backends are put together.
#
# U -> Y ->\             U ->\
#           -> X   ==>        -> Y -> X
# V -> Y ->/             V ->/

# Note. It is possible for an instruction to be amenable to both for-
# and backward merging. No heuristics are known to decide which is
# better.

proc ::page::compiler::peg::mecpu::backmerge {g} {
    page_info "* Backward merging of identical instructions"
    page_info "  Unifying paths"
    set count 0
    set runs 0

    set merged 1
    while {$merged} {
	set merged 0
	foreach n [$g nodes] {
	    # Skip nodes already killed in previous rounds.
	    if {![$g node exists $n]} continue

	    set inbound [$g arcs -in $n]
	    if {[llength $inbound] < 2} continue

	    # We have more than 1 inbound arcs on this node. Check all
	    # pairs of pre-decessors for possible unification.

	    # Additional condition: Outbounds in the targets not > 1
	    # We check in different levels, to avoid redundant calls.

	    while {[llength $inbound] > 2} {
		set aa   [lindex $inbound 0]
		set tail [lrange $inbound 1 end]

		set na [$g arc source $aa]
		if {[$g node degree -out $na] > 1} {
		    set inbound $tail
		    continue
		}

		set inbound {}
		foreach ab $tail {
		    set nb [$g arc source $ab]
		    if {[$g node degree -out $nb] > 1} continue

		    set ia [$g node get $na instruction][$g node get $na arguments]
		    set ib [$g node get $nb instruction][$g node get $nb arguments]

		    if {$ia ne $ib} {
			lappend inbound $ab
			continue
		    }

		    page_log_info "    \\Merge [np $n] : [np $na] <- [np $nb] ([printinsn $g $na])"

		    # Discard the second node in the pair. Move all
		    # arcs inbound into it so that they reach the
		    # first node instead.

		    foreach a [$g arcs -in $nb] {$g arc move-target $a $na}
		    $g node delete $nb
		    set merged 1
		    incr count
		}
	    }
	}
	incr runs
    }

    page_info "  Merged [plural $count instruction] in [plural $runs run]"
    return
}

# ### ### ### ######### ######### #########

proc ::page::compiler::peg::mecpu::pathlengths {g} {
    page_info "* Find maximum length paths"

    set pending [llength [$g nodes]]

    set nodes {}
    set loops {}
    foreach n [$g nodes] {
	$g node set $n WAIT [$g node degree -out $n]
	set insn [$g node get $n instruction]
	if {($insn eq "icf_halt") || ($insn eq "icf_ntreturn")} {
	    lappend nodes $n
	}
	if {[$g node keyexists $n LOOP]} {
	    lappend loops $n
	}
    }

    set level 0
    while {[llength $nodes]} {
	incr pending -[llength $nodes]
	set nodes [closure $g $nodes $level]
	incr level
    }

    if {[llength $loops]} {
	page_info "  Loop levels"

	set nodes $loops
	while {[llength $nodes]} {
	    incr pending -[llength $nodes]
	    set nodes [closure $g $nodes $level]
	    incr level
	}
    }

    if {$pending} {
	page_info  "  Remainder"

	while {$pending} {
	    set nodes {}
	    foreach n [$g nodes] {
		if {[$g node keyexists $n LEVEL]} continue
		if {[$g node get $n WAIT] < [$g node degree -out $n]} {
		    lappend nodes $n
		}
	    }
	    while {[llength $nodes]} {
		incr pending -[llength $nodes]
		set nodes [closure $g $nodes $level]
		incr level
	    }
	}
    }
    return
}

proc ::page::compiler::peg::mecpu::closure {g nodes level} {
    page_log_info "  \[[format %6d $level]\] : $nodes"

    foreach n $nodes {$g node set $n LEVEL $level}

    set tmp {}
    foreach n $nodes {
	foreach pre [$g nodes -in $n] {
	    # Ignore instructions already given a level.
	    if {[$g node keyexists $pre LEVEL]} continue
	    $g node set $pre WAIT [expr {[$g node get $pre WAIT] - 1}]
	    if {[$g node get $pre WAIT] > 0} continue
	    lappend tmp $pre
	}
    }
    return [lsort -uniq -dict $tmp]
}

proc ::page::compiler::peg::mecpu::jumps {g} {
    page_info "* Insert explicit jumps and branches"

    foreach n [$g nodes] {
	# Inbound > 1, at least one is from a jump, so a label is
	# needed.

	if {[llength [$g arcs -in $n]] > 1} {
	    set go bra[string range $n 4 end]
	    $g node set $n LABEL $go
	}

	set darcs [$g arcs -out $n]

	if {[llength $darcs] == 0} {
	    $g node set $n NEXT ""
	    continue
	}

	if {[llength $darcs] == 1} {
	    set da [lindex $darcs 0]
	    set dn [$g arc target $da]

	    if {[$g node get $dn LEVEL] > [$g node get $n LEVEL]} {
		# Flow is backward, an uncond. jump
		# is needed here.

		set go bra[string range $dn 4 end]
		$g node set $dn LABEL $go
		set j [$g node insert]
		$g arc move-target $da $j
		$g node set $j instruction icf_jalways
		$g node set $j arguments   $go

		$g arc insert $j $dn

		$g node set $n NEXT $j
		$g node set $j NEXT ""
	    } else {
		$g node set $n NEXT $dn
	    }
	    continue
	}

	set aok {}
	set afl {}
	foreach a $darcs {
	    if {[$g arc get $a condition] eq "ok"} {
		set aok $a
	    } else {
		set afl $a
	    }
	}
	set nok [$g arc target $aok]
	set nfl [$g arc target $afl]

	if {[$g node get $n instruction] eq "inc_restore"} {
	    set go bra[string range $nok 4 end]
	    $g node set $nok LABEL $go

	    $g node set $n NEXT $nfl
	    $g node set $n SAVE $nok

	    $g node set $n arguments [linsert [$g node get $n arguments] 0 $go]
	    continue
	}

	if {[$g node get $n instruction] ne ".BRA"} {
	    set bra [$g node insert]
	    $g arc move-source $aok $bra
	    $g arc move-source $afl $bra
	    $g arc insert $n $bra
	    $g node set $n NEXT $bra
	    set n $bra
	}

	if {[$g node get $nok LEVEL] > [$g node get $nfl LEVEL]} {
	    # Ok branch is direct, Fail is jump.

	    $g node set $n NEXT $nok
	    $g node set $n SAVE $nfl

	    set go bra[string range $nfl 4 end]
	    $g node set $nfl LABEL $go
	    $g node set $n instruction icf_jfail
	    $g node set $n arguments   $go
	} else {

	    # Fail branch is direct, Ok is jump.

	    $g node set $n NEXT $nfl
	    $g node set $n SAVE $nok

	    set go bra[string range $nok 4 end]
	    $g node set $nok LABEL $go
	    $g node set $n instruction icf_jok
	    $g node set $n arguments   $go
	}
    }
}

proc ::page::compiler::peg::mecpu::symbols {g t} {
    page_info "* Label subroutine heads"

    # Label and mark the instructions where subroutines begin.
    # These markers are used by 2code to locate all actually
    # used subroutines.

    foreach def [lsort -uniq [$t get root gas::called]] {
	set gdef [$t get $def gas::entry]
	foreach caller [$t get $def gas::callers] {

	    # Skip callers which are gone because of optimizations.
	    if {![$g node exists $caller]} continue

	    $g node set $caller CALL $gdef
	    $g node set $gdef LABEL \
		    [lindex [$g node set $caller arguments] 0]
	}
    }
    return
}

# ### ### ### ######### ######### #########

proc ::page::compiler::peg::mecpu::statistics {code} {
    return
    # disabled
    page_info "* Statistics"
    statistics_si $code

    # All higher order statistics are done only on the instructions in
    # a basic block, i.e. a linear sequence. We are looking for
    # high-probability blocks in itself, and then also for
    # high-probability partials.

    set blocks [basicblocks $code]

    # Basic basic block statistics (full blocks)

    Init bl
    foreach b $blocks {Incr bl($b)}
    wrstat  bl asm/statistics_bb.txt
    wrstatk bl asm/statistics_bbk.txt

    # Statistics of all partial blocks, i.e. all possible
    # sub-sequences with length > 1.

    Init ps
    foreach b $blocks {
	for {set s 0} {$s < [llength $b]} {incr s} {
	    for {set e [expr {$s + 1}]} {$e < [llength $b]} {incr e} {
		Incr ps([lrange $b $s $e]) $bl($b)
	    }
	}
    }

    wrstat  ps asm/statistics_ps.txt
    wrstatk ps asm/statistics_psk.txt
    return
}

proc ::page::compiler::peg::mecpu::statistics_si {code} {
    page_info "  Single instruction probabilities."

    # What are the most used instructions, statically speaking,
    # without considering context ?

    Init si
    foreach i $code {
	foreach {label name} $i break
	if {$name eq ".C"} continue
	Incr si($name)
    }

    wrstat si asm/statistics_si.txt
    return
}

proc ::page::compiler::peg::mecpu::Init {v} {
    upvar 1 $v var total total
    array set var {}
    set total 0
    return
}

proc ::page::compiler::peg::mecpu::Incr {v {n 1}} {
    upvar 1 $v var total total
    if {![info exists var]} {set var $n ; incr total ; return}
    incr var $n
    incr total $n
    return
}

proc ::page::compiler::peg::mecpu::wrstat {bv file} {
    upvar 1 $bv buckets total total

    set tmp  {}
    foreach {name count} [array get buckets] {
	lappend tmp [list $name $count]
    }

    set     lines {}
    lappend lines "Total: $total"

    set half [expr {$total / 2}]
    set down $total

    foreach item [lsort -index 1 -decreasing -integer [lsort -index 0 $tmp]] {
	foreach {key count} $item break

	set percent [format %6.2f [expr {$count*100.0/$total}]]%
	set fcount  [format %8d $count]

	lappend lines "  $fcount $percent $key"
	incr down -$count
	if {$half && ($down < $half)} {
	    lappend lines **
	    set half 0
	}
    }

    write $file [join $lines \n]\n
    return
}

proc ::page::compiler::peg::mecpu::wrstatk {bv file} {
    upvar 1 $bv buckets total total

    set tmp  {}
    foreach {name count} [array get buckets] {
	lappend tmp [list $name $count]
    }

    set     lines {}
    lappend lines "Total: $total"

    set half [expr {$total / 2}]
    set down $total

    foreach item  [lsort -index 0 [lsort -index 1 -decreasing -integer $tmp]] {
	foreach {key count} $item break

	set percent [format %6.2f [expr {$count*100.0/$total}]]%
	set fcount  [format %8d $count]

	lappend lines "  $fcount $percent $key"
	incr down -$count
	if {$down < $half} {
	    lappend lines **
	    set half -1
	}
    }

    write $file [join $lines \n]\n
    return
}

proc ::page::compiler::peg::mecpu::basicblocks {code} {
    set blocks {}
    set block {}

    foreach i $code {
	foreach {label name} $i break
	if {
	    ($name eq ".C")          ||
	    ($name eq "icf_jok")     ||
	    ($name eq "icf_jfail")   ||
	    ($name eq "icf_jalways") ||
	    ($name eq "icf_ntreturn")
	} {
	    # Jumps stop a block, and are not put into the block
	    # Except if the block is of length 1. Then it is of
	    # interest to see if certain combinations are used
	    # often.

	    if {[llength $block]} {
		if {[llength $block] == 1} {lappend block $name}
		lappend blocks $block
	    }
	    set block {}
	    continue
	} elseif {$label ne ""} {
	    # A labeled instruction starts a new block and belongs to
	    # it. Note that the previous block is saved only if it is
	    # of length > 1. A single instruction block is not
	    # something we can optimize.

	    if {[llength $block] > 1} {lappend blocks $block}
	    set block [list $name]
	    continue
	}
	# Extend current block
	lappend block $name
    }

    if {[llength $block]} {lappend blocks $block}
    return $blocks
}

# ### ### ### ######### ######### #########

proc ::page::compiler::peg::mecpu::printinsn {g n} {
    return "[$g node get $n instruction] <[$g node get $n arguments]>"
}

proc ::page::compiler::peg::mecpu::plural {n prefix} {
    return "$n ${prefix}[expr {$n == 1 ? "" : "s"}]"
}

proc ::page::compiler::peg::mecpu::np {n} {
    format %-*s 8 $n
}

proc ::page::compiler::peg::mecpu::status {g} {
    page_info "[plural [llength [$g nodes]] instruction]"
    return
}

proc ::page::compiler::peg::mecpu::gdump {g file} {
    return
    # disabled
    variable gnext
    page_info "  %% Saving graph to \"$file\" %%"
    write asm/[format %02d $gnext]_${file}.sgr [$g serialize]
    incr gnext
    return
}

# ### ### ### ######### ######### #########
## Internal. Strings.

namespace eval ::page::compiler::peg::mecpu {
    variable gnext 0
}

# ### ### ### ######### ######### #########
## Ready

package provide page::compiler::peg::mecpu 0.1.1