compiler_peg_mecpu.tcl
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 package require page::plugin ;
00035
00036 package require grammar::me::cpu::gasm
00037 package require textutil
00038 package require struct::graph
00039
00040 package require page::analysis::peg::emodes
00041 package require page::util::quote
00042 package require page::util::peg
00043
00044 namespace ::page::compiler::peg::mecpu {
00045
00046
00047
00048 namespace import ::page::util::quote::*
00049 namespace import ::page::util::peg::*
00050
00051
00052 namespace gas {
00053 namespace import ::grammar::me::cpu::gas::begin
00054 namespace import ::grammar::me::cpu::gas::done
00055 namespace import ::grammar::me::cpu::gas::lift
00056 namespace import ::grammar::me::cpu::gas::state
00057 namespace import ::grammar::me::cpu::gas::state!
00058 }
00059 namespace import ::grammar::me::cpu::gas::*
00060 rename begin {}
00061 rename done {}
00062 rename lift {}
00063 rename state {}
00064 rename state! {}
00065 }
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 ret ::page::compiler::peg::mecpu (type t) {
00081 # Resolve the mode hints. Every gen(X) having a value of 'maybe'
00082 # (or missing) is for the purposes of this code a 'yes'.
00083
00084 if {![page::analysis::peg::emodes::compute $t]} {
00085 page_error " Unable to generate a ME parser without accept/generate properties"
00086 return
00087 }
00088
00089 foreach n [$t nodes] {
00090 if {![$t keyexists $n gen] || ([$t get $n gen] eq "maybe")} {
00091 $t set $n gen 1
00092 }
00093 if {![$t keyexists $n acc]} {$t set $n acc 1}
00094 }
00095
00096 # Synthesize a program, then the assembly code.
00097
00098 mecpu::Synth $t
00099 return
00100 }
00101
00102
00103
00104
00105 ret ::page::compiler::peg::mecpu::Synth (type t) {
00106 # Phase 2: Bottom-up, synthesized attributes
00107
00108 # We use a global graph to capture instructions and their
00109 # relations. The graph is then converted into a linear list of
00110 # instructions, with proper labeling and jump instructions to
00111 # handle all non-linear control-flow.
00112
00113 set g [struct::graph g]
00114 $t set root gas::called {}
00115
00116 page_info "* Synthesize graph code"
00117
00118 $t walk root -order post -type dfs n {
00119 SynthNode $n
00120 }
00121
00122 status $g ; gdump $g synth
00123 remove_unconnected $g ; gdump $g nounconnected
00124 remove_dead $g ; gdump $g nodead
00125 denop $g ; gdump $g nonops
00126 parcmerge $g ; gdump $g parcmerge
00127 forwmerge $g ; gdump $g fmerge
00128 backmerge $g ; gdump $g bmerge
00129 status $g
00130 pathlengths $g ; gdump $g pathlen
00131 jumps $g ; gdump $g jumps
00132 status $g
00133 symbols $g $t
00134
00135 set cc [2code $t $g]
00136 #write asm/mecode [join $cc \n]
00137
00138 statistics $cc
00139
00140 $t set root asm $cc
00141 $g destroy
00142 return
00143 }
00144
00145 ret ::page::compiler::peg::mecpu::SynthNode (type n) {
00146 upvar 1 t t g g
00147 if {$n eq "root"} {
00148 set code Root
00149 } elseif {[$t keyexists $n symbol]} {
00150 set code Nonterminal
00151 } elseif {[$t keyexists $n op]} {
00152 set code [$t get $n op]
00153 } else {
00154 return -code error "PANIC. Bad node $n, cannot classify"
00155 }
00156
00157 page_log_info " [np $n] := ([linsert [$t children $n] 0 $code])"
00158
00159 SynthNode/$code $n
00160 return
00161 }
00162
00163 ret ::page::compiler::peg::mecpu::SynthNode/Root (type n) {
00164 upvar 1 t t g g
00165
00166 # Root is the grammar itself.
00167
00168 set gstart [$t get root start]
00169 set gname [$t get root name]
00170
00171 if {$gstart eq ""} {
00172 page_error " No start expression."
00173 return
00174 }
00175
00176 gas::begin $g $n halt "<Start Expression> '$gname'"
00177 $g node set [Who entry] instruction .C
00178 $g node set [Who entry] START .
00179
00180 Inline $t $gstart sexpr
00181 /At sexpr/exit/ok ; /Ok ; Jmp exit/return
00182 /At sexpr/exit/fail ; /Fail ; Jmp exit/return
00183
00184 gas::done --> $t
00185 return
00186 }
00187
00188 ret ::page::compiler::peg::mecpu::SynthNode/Nonterminal (type n) {
00189 upvar 1 t t g g
00190
00191 # This is the root of a definition.
00192 #
00193 # The text is a procedure wrapping the match code of its
00194 # expression into the required the nonterminal handling (caching
00195 # and such), plus the support code for the expression matcher.
00196
00197 set sym [$t get $n symbol]
00198 set label [$t get $n label]
00199 set gen [$t get $n gen]
00200 set mode [$t get $n mode]
00201
00202 set pe [lindex [$t children $n] 0]
00203 set egen [$t get $pe gen]
00204
00205 # -> inc_restore -found-> NOP gen: -> ok -> ias_push -> RETURN
00206 # /!found \ /
00207 # / \-fail --------->/
00208 # / !gen: -> RETURN
00209 # /
00210 # \-> icl_push (-> ias_mark) -> (*) -> SV -> inc_save (-> ias_mrewind) -X
00211 #
00212 # X -ok----> ias_push -> ier_nonterminal
00213 # \ /
00214 # \-fail ----------/
00215
00216 # Poking into the generated instructions, converting the initial
00217 # .NOP into a .C'omment.
00218
00219 set first [gas::begin $g $n !okfail "Nonterminal '$sym'"]
00220 $g node set [Who entry] instruction .C
00221 $g node set [Who entry] START .
00222
00223 Cmd inc_restore $label ; /Label restore ; /Ok
00224
00225 if {$gen} {
00226 Bra ; /Label @
00227 /Fail ; Nop ; Exit
00228 /At @
00229 /Ok ; Cmd ias_push ; Exit
00230 } else {
00231 Nop ; Exit
00232 }
00233
00234 /At restore ; /Fail
00235 Cmd icl_push ; # Balanced by inc_save (XX)
00236 Cmd icl_push ; # Balanced by pop after ier_terminal
00237
00238 if {$egen} {
00239 # [*] Needed for removal of SV's from stack after handling by
00240 # this symbol, only if expression actually generates an SV.
00241
00242 Cmd ias_mark
00243 }
00244
00245 Inline $t $pe subexpr ; /Ok ; Nop ; /Label unified
00246 /At subexpr/exit/fail ; /Fail ; Jmp unified
00247 /At unified
00248
00249 switch -exact -- $mode {
00250 value {Cmd isv_nonterminal_reduce $label}
00251 match {Cmd isv_nonterminal_range $label}
00252 leaf {Cmd isv_nonterminal_leaf $label}
00253 discard {Cmd isv_clear}
00254 default {return -code error "Bad nonterminal mode \"$mode\""}
00255 }
00256
00257 Cmd inc_save $label ; # Implied icl_pop (XX)
00258
00259 if {$egen} {
00260 # See [*], this is the removal spoken about before.
00261 Cmd ias_mrewind
00262 }
00263
00264 /Label hold
00265
00266 if {$gen} {
00267 /Ok
00268 Cmd ias_push
00269 Nop ; /Label merge
00270 /At hold ; /Fail ; Jmp merge
00271 /At merge
00272 }
00273
00274 Cmd ier_nonterminal "Expected $label"
00275 Cmd icl_pop
00276 Exit
00277
00278 gas::done --> $t
00279 return
00280 }
00281
00282 ret ::page::compiler::peg::mecpu::SynthNode/? (type n) {
00283 upvar 1 t t g g
00284
00285 # The expression e? is equivalent to e/epsilon.
00286 # And like this it is compiled.
00287
00288 set pe [lindex [$t children $n] 0]
00289
00290 gas::begin $g $n okfail ?
00291
00292 # -> icl_push -> ier_push -> (*) -ok--> ier_merge/ok --> icl_pop -ok----------------> OK
00293 # \ /
00294 # \-fail-> ier_merge/f ---> icl_rewind -> iok_ok -ok-/
00295
00296 Cmd icl_push
00297 Cmd ier_push
00298
00299 Inline $t $pe subexpr
00300
00301 /Ok
00302 Cmd ier_merge
00303 Cmd icl_pop
00304 /Ok ; Exit
00305
00306 /At subexpr/exit/fail ; /Fail
00307 Cmd ier_merge
00308 Cmd icl_rewind
00309 Cmd iok_ok
00310 /Ok ; Exit
00311
00312 gas::done --> $t
00313 return
00314 }
00315
00316 ret ::page::compiler::peg::mecpu::SynthNode
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834 ret ::page::compiler::peg::mecpu::2code (type t , type g) {
00835 page_info "* Generating ME assembler code"
00836
00837 set insn {}
00838 set start [$t get root gas::entry]
00839 set cat 0
00840 set calls [list $start]
00841
00842 while {$cat < [llength $calls]} {
00843 set now [lindex $calls $cat]
00844 incr cat
00845
00846 set at 0
00847 set pending [list $now]
00848
00849 while {$at < [llength $pending]} {
00850 set current [lindex $pending $at]
00851 incr at
00852
00853 while {$current ne ""} {
00854 if {[$g node keyexists $current WRITTEN]} break
00855
00856 insn $g $current insn
00857 $g node set $current WRITTEN .
00858
00859 if {[$g node keyexists $current SAVE]} {
00860 lappend pending [$g node get $current SAVE]
00861 }
00862 if {[$g node keyexists $current CALL]} {
00863 lappend calls [$g node get $current CALL]
00864 }
00865
00866 set current [$g node get $current NEXT]
00867 if {$current eq ""} break
00868 if {[$g node keyexists $current WRITTEN]} {
00869 lappend insn [list {} icf_jalways \
00870 [$g node get $current LABEL]]
00871 break
00872 }
00873
00874 # Process the following instruction,
00875 # if there is any.
00876 }
00877 }
00878 }
00879
00880 return $insn
00881 }
00882
00883 ret ::page::compiler::peg::mecpu::insn (type g , type current , type iv) {
00884 upvar 1 $iv insn
00885
00886 set code [$g node get $current instruction]
00887 set args [$g node get $current arguments]
00888
00889 set label {}
00890 if {[$g node keyexists $current LABEL]} {
00891 set label [$g node get $current LABEL]
00892 }
00893
00894 lappend insn [linsert $args 0 $label $code]
00895 return
00896 }
00897
00898 if 0 {
00899 if {[lindex $ins 0] eq "icf_ntcall"} {
00900 tmp = {}
00901 foreach b $branches {
00902 if {[$g node keyexists $b START]} {
00903 sym = [$g node get $b symbol]
00904 lappend ins sym_$sym
00905 } else {
00906 lappend tmp $b
00907 }
00908 }
00909 branches = $tmp
00910 }
00911 }
00912
00913
00914
00915
00916
00917
00918
00919 ret ::page::compiler::peg::mecpu::remove_unconnected (type g) {
00920 page_info "* Remove unconnected instructions"
00921
00922 foreach n [$g nodes] {
00923 if {[$g node degree $n] == 0} {
00924 page_error "$n ([printinsn $g $n])"
00925 page_error "Found unconnected node. This should not have happened."
00926 page_error "Removing the bad node."
00927
00928 $g node delete $n
00929 }
00930 }
00931 }
00932
00933 ret ::page::compiler::peg::mecpu::remove_dead (type g) {
00934 page_info "* Remove dead instructions"
00935
00936 set count 0
00937 set runs 0
00938 set hasdead 1
00939 while {$hasdead} {
00940 set hasdead 0
00941 foreach n [$g nodes] {
00942 if {[$g node keyexists $n START]} continue
00943 if {[$g node degree -in $n] > 0} continue
00944
00945 page_log_info " [np $n] removed, dead ([printinsn $g $n])"
00946
00947 $g node delete $n
00948
00949 set hasdead 1
00950 incr count
00951 }
00952 incr runs
00953 }
00954
00955 page_info " Removed [plural $count instruction] in [plural $runs run]"
00956 return
00957 }
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978 ret ::page::compiler::peg::mecpu::denop (type g) {
00979 # Remove the .NOPs and reroute control flow. We keep the pseudo
00980 # instructions for comments (.C) and the explicit branch points
00981 # (.BRA).
00982
00983 page_info "* Removing the helper .NOP instructions."
00984
00985 set count 0
00986 foreach n [$g nodes] {
00987 # Skip over nodes already deleted by a previous iteration.
00988 if {[$g node get $n instruction] ne ".NOP"} continue
00989
00990 # We keep branching .NOPs, and warn user. There shouldn't be
00991 # any. such should explicit bnrachpoints.
00992
00993 set destinations [$g arcs -out $n]
00994
00995 if {[llength $destinations] > 1} {
00996 page_error "$n ([printinsn $g $n])"
00997 page_error "Found a .NOP with more than one destination."
00998 page_error "This should have been a .BRA instruction."
00999 page_error "Not removed. Internal error. Fix the transformation."
01000 continue
01001 }
01002
01003 # Nops without a destination, dead-end's are not wanted. They
01004 # should not exist either too. We will do a general dead-end
01005 # and dead-start removal as well.
01006
01007 if {[llength $destinations] < 1} {
01008 page_error "$n ([printinsn $g $n])"
01009 page_error "Found a .NOP without any destination, i.e. a dead end."
01010 page_error "This should not have happened. Removed the node."
01011
01012 $g node delete $n
01013 continue
01014 }
01015
01016 page_log_info " [np $n] removed, updated cflow ([printinsn $g $n])"
01017
01018 # As there is exactly one destination we can now reroute all
01019 # incoming arcs around the nop to the new destination.
01020
01021 set target [$g arc target [lindex $destinations 0]]
01022 foreach a [$g arcs -in $n] {
01023 $g arc move-target $a $target
01024 }
01025
01026 $g node delete $n
01027 incr count
01028 }
01029
01030 page_info " Removed [plural $count instruction]"
01031 return
01032 }
01033
01034
01035
01036
01037
01038
01039
01040
01041 ret ::page::compiler::peg::mecpu::parcmerge (type g) {
01042 page_info "* Search for identical parallel arcs and merge them"
01043
01044 #puts [join [info loaded] \n] /seg.fault induced with tcllibc! - tree!
01045
01046 set count 0
01047 foreach n [$g nodes] {
01048 set arcs [$g arcs -out $n]
01049
01050 if {[llength $arcs] < 2} continue
01051 if {[llength $arcs] > 2} {
01052 page_error " $n ([printinsn $g $n])"
01053 page_error " Instruction has more than two destinations."
01054 page_error " That is not possible. Internal error."
01055 continue
01056 }
01057 # Two way branch. Both targets the same ?
01058
01059 foreach {a b} $arcs break
01060
01061 if {[$g arc target $a] ne [$g arc target $b]} continue
01062
01063 page_log_info " [np $n] outbound arcs merged ([printinsn $g $n])"
01064
01065 $g arc set $a condition always
01066 $g arc delete $b
01067
01068 incr count 2
01069 }
01070
01071 page_info " Merged [plural $count arc]"
01072 return
01073 }
01074
01075
01076
01077
01078
01079
01080
01081
01082
01083
01084
01085
01086
01087
01088
01089 ret ::page::compiler::peg::mecpu::forwmerge (type g) {
01090 page_info "* Forward merging of identical instructions"
01091 page_info " Delaying decisions"
01092 set count 0
01093 set runs 0
01094
01095 set merged 1
01096 while {$merged} {
01097 set merged 0
01098 foreach n [$g nodes] {
01099 # Skip nodes already killed in previous rounds.
01100 if {![$g node exists $n]} continue
01101
01102 set outbound [$g arcs -out $n]
01103 if {[llength $outbound] != 2} continue
01104
01105 foreach {aa ab} $outbound break
01106 set na [$g arc target $aa]
01107 set nb [$g arc target $ab]
01108
01109 set ia [$g node get $na instruction][$g node get $na arguments]
01110 set ib [$g node get $nb instruction][$g node get $nb arguments]
01111 if {$ia ne $ib} continue
01112
01113 # Additional condition: Inbounds in the targets not > 1
01114
01115 if {([$g node degree -in $na] > 1) ||
01116 ([$g node degree -in $nb] > 1)} continue
01117
01118 page_log_info " /Merge [np $n] : [np $na] <- [np $nb] ([printinsn $g $na])"
01119
01120 # Label all arcs out of na with the condition of the arc
01121 # into it. Ditto for the arcs out of nb. The latter also
01122 # get na as their new origin. The arcs out of n relabeled
01123 # to always. The nb is deleted. This creates the desired
01124 # control structure without having to create a new node
01125 # and filling it. We simply use na, discard nb, and
01126 # properly rewrite the arcs to have the correct
01127 # conditions.
01128
01129 foreach a [$g arcs -out $na] {
01130 $g arc set $a condition [$g arc get $aa condition]
01131 }
01132 foreach a [$g arcs -out $nb] {
01133 $g arc set $a condition [$g arc get $ab condition]
01134 $g arc move-source $a $na
01135 }
01136 $g arc set $aa condition always
01137 $g node delete $nb
01138 set merged 1
01139 incr count
01140 }
01141 incr runs
01142 }
01143
01144 # NOTE: This may require a parallel arc merge, with identification
01145 # of merge-able arcs based on the arc condition, i.e. labeling.
01146
01147 page_info " Merged [plural $count instruction] in [plural $runs run]"
01148 return
01149 }
01150
01151
01152
01153
01154
01155
01156
01157
01158
01159
01160
01161
01162 ret ::page::compiler::peg::mecpu::backmerge (type g) {
01163 page_info "* Backward merging of identical instructions"
01164 page_info " Unifying paths"
01165 set count 0
01166 set runs 0
01167
01168 set merged 1
01169 while {$merged} {
01170 set merged 0
01171 foreach n [$g nodes] {
01172 # Skip nodes already killed in previous rounds.
01173 if {![$g node exists $n]} continue
01174
01175 set inbound [$g arcs -in $n]
01176 if {[llength $inbound] < 2} continue
01177
01178 # We have more than 1 inbound arcs on this node. Check all
01179 # pairs of pre-decessors for possible unification.
01180
01181 # Additional condition: Outbounds in the targets not > 1
01182 # We check in different levels, to avoid redundant calls.
01183
01184 while {[llength $inbound] > 2} {
01185 set aa [lindex $inbound 0]
01186 set tail [lrange $inbound 1 end]
01187
01188 set na [$g arc source $aa]
01189 if {[$g node degree -out $na] > 1} {
01190 set inbound $tail
01191 continue
01192 }
01193
01194 set inbound {}
01195 foreach ab $tail {
01196 set nb [$g arc source $ab]
01197 if {[$g node degree -out $nb] > 1} continue
01198
01199 set ia [$g node get $na instruction][$g node get $na arguments]
01200 set ib [$g node get $nb instruction][$g node get $nb arguments]
01201
01202 if {$ia ne $ib} {
01203 lappend inbound $ab
01204 continue
01205 }
01206
01207 page_log_info " \\Merge [np $n] : [np $na] <- [np $nb] ([printinsn $g $na])"
01208
01209 # Discard the second node in the pair. Move all
01210 # arcs inbound into it so that they reach the
01211 # first node instead.
01212
01213 foreach a [$g arcs -in $nb] {$g arc move-target $a $na}
01214 $g node delete $nb
01215 set merged 1
01216 incr count
01217 }
01218 }
01219 }
01220 incr runs
01221 }
01222
01223 page_info " Merged [plural $count instruction] in [plural $runs run]"
01224 return
01225 }
01226
01227
01228
01229 ret ::page::compiler::peg::mecpu::pathlengths (type g) {
01230 page_info "* Find maximum length paths"
01231
01232 set pending [llength [$g nodes]]
01233
01234 set nodes {}
01235 set loops {}
01236 foreach n [$g nodes] {
01237 $g node set $n WAIT [$g node degree -out $n]
01238 set insn [$g node get $n instruction]
01239 if {($insn eq "icf_halt") || ($insn eq "icf_ntreturn")} {
01240 lappend nodes $n
01241 }
01242 if {[$g node keyexists $n LOOP]} {
01243 lappend loops $n
01244 }
01245 }
01246
01247 set level 0
01248 while {[llength $nodes]} {
01249 incr pending -[llength $nodes]
01250 set nodes [closure $g $nodes $level]
01251 incr level
01252 }
01253
01254 if {[llength $loops]} {
01255 page_info " Loop levels"
01256
01257 set nodes $loops
01258 while {[llength $nodes]} {
01259 incr pending -[llength $nodes]
01260 set nodes [closure $g $nodes $level]
01261 incr level
01262 }
01263 }
01264
01265 if {$pending} {
01266 page_info " Remainder"
01267
01268 while {$pending} {
01269 set nodes {}
01270 foreach n [$g nodes] {
01271 if {[$g node keyexists $n LEVEL]} continue
01272 if {[$g node get $n WAIT] < [$g node degree -out $n]} {
01273 lappend nodes $n
01274 }
01275 }
01276 while {[llength $nodes]} {
01277 incr pending -[llength $nodes]
01278 set nodes [closure $g $nodes $level]
01279 incr level
01280 }
01281 }
01282 }
01283 return
01284 }
01285
01286 ret ::page::compiler::peg::mecpu::closure (type g , type nodes , type level) {
01287 page_log_info " \[[format %6d $level]\] : $nodes"
01288
01289 foreach n $nodes {$g node set $n LEVEL $level}
01290
01291 set tmp {}
01292 foreach n $nodes {
01293 foreach pre [$g nodes -in $n] {
01294 # Ignore instructions already given a level.
01295 if {[$g node keyexists $pre LEVEL]} continue
01296 $g node set $pre WAIT [expr {[$g node get $pre WAIT] - 1}]
01297 if {[$g node get $pre WAIT] > 0} continue
01298 lappend tmp $pre
01299 }
01300 }
01301 return [lsort -uniq -dict $tmp]
01302 }
01303
01304 ret ::page::compiler::peg::mecpu::jumps (type g) {
01305 page_info "* Insert explicit jumps and branches"
01306
01307 foreach n [$g nodes] {
01308 # Inbound > 1, at least one is from a jump, so a label is
01309 # needed.
01310
01311 if {[llength [$g arcs -in $n]] > 1} {
01312 set go bra[string range $n 4 end]
01313 $g node set $n LABEL $go
01314 }
01315
01316 set darcs [$g arcs -out $n]
01317
01318 if {[llength $darcs] == 0} {
01319 $g node set $n NEXT ""
01320 continue
01321 }
01322
01323 if {[llength $darcs] == 1} {
01324 set da [lindex $darcs 0]
01325 set dn [$g arc target $da]
01326
01327 if {[$g node get $dn LEVEL] > [$g node get $n LEVEL]} {
01328 # Flow is backward, an uncond. jump
01329 # is needed here.
01330
01331 set go bra[string range $dn 4 end]
01332 $g node set $dn LABEL $go
01333 set j [$g node insert]
01334 $g arc move-target $da $j
01335 $g node set $j instruction icf_jalways
01336 $g node set $j arguments $go
01337
01338 $g arc insert $j $dn
01339
01340 $g node set $n NEXT $j
01341 $g node set $j NEXT ""
01342 } else {
01343 $g node set $n NEXT $dn
01344 }
01345 continue
01346 }
01347
01348 set aok {}
01349 set afl {}
01350 foreach a $darcs {
01351 if {[$g arc get $a condition] eq "ok"} {
01352 set aok $a
01353 } else {
01354 set afl $a
01355 }
01356 }
01357 set nok [$g arc target $aok]
01358 set nfl [$g arc target $afl]
01359
01360 if {[$g node get $n instruction] eq "inc_restore"} {
01361 set go bra[string range $nok 4 end]
01362 $g node set $nok LABEL $go
01363
01364 $g node set $n NEXT $nfl
01365 $g node set $n SAVE $nok
01366
01367 $g node set $n arguments [linsert [$g node get $n arguments] 0 $go]
01368 continue
01369 }
01370
01371 if {[$g node get $n instruction] ne ".BRA"} {
01372 set bra [$g node insert]
01373 $g arc move-source $aok $bra
01374 $g arc move-source $afl $bra
01375 $g arc insert $n $bra
01376 $g node set $n NEXT $bra
01377 set n $bra
01378 }
01379
01380 if {[$g node get $nok LEVEL] > [$g node get $nfl LEVEL]} {
01381 # Ok branch is direct, Fail is jump.
01382
01383 $g node set $n NEXT $nok
01384 $g node set $n SAVE $nfl
01385
01386 set go bra[string range $nfl 4 end]
01387 $g node set $nfl LABEL $go
01388 $g node set $n instruction icf_jfail
01389 $g node set $n arguments $go
01390 } else {
01391
01392 # Fail branch is direct, Ok is jump.
01393
01394 $g node set $n NEXT $nfl
01395 $g node set $n SAVE $nok
01396
01397 set go bra[string range $nok 4 end]
01398 $g node set $nok LABEL $go
01399 $g node set $n instruction icf_jok
01400 $g node set $n arguments $go
01401 }
01402 }
01403 }
01404
01405 ret ::page::compiler::peg::mecpu::symbols (type g , type t) {
01406 page_info "* Label subroutine heads"
01407
01408 # Label and mark the instructions where subroutines begin.
01409 # These markers are used by 2code to locate all actually
01410 # used subroutines.
01411
01412 foreach def [lsort -uniq [$t get root gas::called]] {
01413 set gdef [$t get $def gas::entry]
01414 foreach caller [$t get $def gas::callers] {
01415
01416 # Skip callers which are gone because of optimizations.
01417 if {![$g node exists $caller]} continue
01418
01419 $g node set $caller CALL $gdef
01420 $g node set $gdef LABEL \
01421 [lindex [$g node set $caller arguments] 0]
01422 }
01423 }
01424 return
01425 }
01426
01427
01428
01429 ret ::page::compiler::peg::mecpu::statistics (type code) {
01430 return
01431 # disabled
01432 page_info "* Statistics"
01433 statistics_si $code
01434
01435 # All higher order statistics are done only on the instructions in
01436 # a basic block, i.e. a linear sequence. We are looking for
01437 # high-probability blocks in itself, and then also for
01438 # high-probability partials.
01439
01440 set blocks [basicblocks $code]
01441
01442 # Basic basic block statistics (full blocks)
01443
01444 Init bl
01445 foreach b $blocks {Incr bl($b)}
01446 wrstat bl asm/statistics_bb.txt
01447 wrstatk bl asm/statistics_bbk.txt
01448
01449 # Statistics of all partial blocks, i.e. all possible
01450 # sub-sequences with length > 1.
01451
01452 Init ps
01453 foreach b $blocks {
01454 for {set s 0} {$s < [llength $b]} {incr s} {
01455 for {set e [expr {$s + 1}]} {$e < [llength $b]} {incr e} {
01456 Incr ps([lrange $b $s $e]) $bl($b)
01457 }
01458 }
01459 }
01460
01461 wrstat ps asm/statistics_ps.txt
01462 wrstatk ps asm/statistics_psk.txt
01463 return
01464 }
01465
01466 ret ::page::compiler::peg::mecpu::statistics_si (type code) {
01467 page_info " Single instruction probabilities."
01468
01469 # What are the most used instructions, statically speaking,
01470 # without considering context ?
01471
01472 Init si
01473 foreach i $code {
01474 foreach {label name} $i break
01475 if {$name eq ".C"} continue
01476 Incr si($name)
01477 }
01478
01479 wrstat si asm/statistics_si.txt
01480 return
01481 }
01482
01483 ret ::page::compiler::peg::mecpu::Init (type v) {
01484 upvar 1 $v var total total
01485 array set var {}
01486 set total 0
01487 return
01488 }
01489
01490 ret ::page::compiler::peg::mecpu::Incr (type v , optional n =1) {
01491 upvar 1 $v var total total
01492 if {![info exists var]} {set var $n ; incr total ; return}
01493 incr var $n
01494 incr total $n
01495 return
01496 }
01497
01498 ret ::page::compiler::peg::mecpu::wrstat (type bv , type file) {
01499 upvar 1 $bv buckets total total
01500
01501 set tmp {}
01502 foreach {name count} [array get buckets] {
01503 lappend tmp [list $name $count]
01504 }
01505
01506 set lines {}
01507 lappend lines "Total: $total"
01508
01509 set half [expr {$total / 2}]
01510 set down $total
01511
01512 foreach item [lsort -index 1 -decreasing -integer [lsort -index 0 $tmp]] {
01513 foreach {key count} $item break
01514
01515 set percent [format %6.2f [expr {$count*100.0/$total}]]%
01516 set fcount [format %8d $count]
01517
01518 lappend lines " $fcount $percent $key"
01519 incr down -$count
01520 if {$half && ($down < $half)} {
01521 lappend lines **
01522 set half 0
01523 }
01524 }
01525
01526 write $file [join $lines \n]\n
01527 return
01528 }
01529
01530 ret ::page::compiler::peg::mecpu::wrstatk (type bv , type file) {
01531 upvar 1 $bv buckets total total
01532
01533 set tmp {}
01534 foreach {name count} [array get buckets] {
01535 lappend tmp [list $name $count]
01536 }
01537
01538 set lines {}
01539 lappend lines "Total: $total"
01540
01541 set half [expr {$total / 2}]
01542 set down $total
01543
01544 foreach item [lsort -index 0 [lsort -index 1 -decreasing -integer $tmp]] {
01545 foreach {key count} $item break
01546
01547 set percent [format %6.2f [expr {$count*100.0/$total}]]%
01548 set fcount [format %8d $count]
01549
01550 lappend lines " $fcount $percent $key"
01551 incr down -$count
01552 if {$down < $half} {
01553 lappend lines **
01554 set half -1
01555 }
01556 }
01557
01558 write $file [join $lines \n]\n
01559 return
01560 }
01561
01562 ret ::page::compiler::peg::mecpu::basicblocks (type code) {
01563 set blocks {}
01564 set block {}
01565
01566 foreach i $code {
01567 foreach {label name} $i break
01568 if {
01569 ($name eq ".C") ||
01570 ($name eq "icf_jok") ||
01571 ($name eq "icf_jfail") ||
01572 ($name eq "icf_jalways") ||
01573 ($name eq "icf_ntreturn")
01574 } {
01575 # Jumps stop a block, and are not put into the block
01576 # Except if the block is of length 1. Then it is of
01577 # interest to see if certain combinations are used
01578 # often.
01579
01580 if {[llength $block]} {
01581 if {[llength $block] == 1} {lappend block $name}
01582 lappend blocks $block
01583 }
01584 set block {}
01585 continue
01586 } elseif {$label ne ""} {
01587 # A labeled instruction starts a new block and belongs to
01588 # it. Note that the previous block is saved only if it is
01589 # of length > 1. A single instruction block is not
01590 # something we can optimize.
01591
01592 if {[llength $block] > 1} {lappend blocks $block}
01593 set block [list $name]
01594 continue
01595 }
01596 # Extend current block
01597 lappend block $name
01598 }
01599
01600 if {[llength $block]} {lappend blocks $block}
01601 return $blocks
01602 }
01603
01604
01605
01606 ret ::page::compiler::peg::mecpu::printinsn (type g , type n) {
01607 return "[$g node get $n instruction] <[$g node get $n arguments]>"
01608 }
01609
01610 ret ::page::compiler::peg::mecpu::plural (type n , type prefix) {
01611 return "$n ${prefix}[expr {$n == 1 ? "" : "s"}]"
01612 }
01613
01614 ret ::page::compiler::peg::mecpu::np (type n) {
01615 format %-*s 8 $n
01616 }
01617
01618 ret ::page::compiler::peg::mecpu::status (type g) {
01619 page_info "[plural [llength [$g nodes]] instruction]"
01620 return
01621 }
01622
01623 ret ::page::compiler::peg::mecpu::gdump (type g , type file) {
01624 return
01625 # disabled
01626 variable gnext
01627 page_info " %% Saving graph to \"$file\" %%"
01628 write asm/[format %02d $gnext]_${file}.sgr [$g serialize]
01629 incr gnext
01630 return
01631 }
01632
01633
01634
01635
01636 namespace ::page::compiler::peg::mecpu {
01637 variable gnext 0
01638 }
01639
01640
01641
01642
01643 package provide page::compiler::peg::mecpu 0.1.1
01644