--- gforth/prims2x.fs 2002/09/14 08:20:19 1.113 +++ gforth/prims2x.fs 2002/09/22 09:54:19 1.115 @@ -250,6 +250,12 @@ variable in-part \ true if processing a create combined-prims max-combined cells allot variable num-combined +: map-combined { xt -- } + \ perform xt for all components of the current combined instruction + num-combined @ 0 +do + combined-prims i th @ xt execute + loop ; + table constant combinations \ the keys are the sequences of pointers to primitives @@ -733,14 +739,15 @@ stack inst-stream IP Cell endif ." }" cr ; +: output-profile-part ( p ) + ." add_inst(b, " quote + prim-name 2@ type + quote ." );" cr ; + : output-profile-combined ( -- ) \ generate code for postprocessing the VM block profile stuff ." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr - num-combined @ 0 +do - ." add_inst(b, " quote - combined-prims i th @ prim-name 2@ type - quote ." );" cr - loop + ['] output-profile-part map-combined ." ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr combined-prims num-combined @ 1- th @ prim-c-code 2@ s" SET_IP" search nip nip combined-prims num-combined @ 1- th @ prim-c-code 2@ s" SUPER_END" search nip nip or if @@ -826,6 +833,9 @@ stack inst-stream IP Cell : output-alias ( -- ) ( primitive-number @ . ." alias " ) ." Primitive " prim prim-name 2@ type cr ; +: output-prim-num ( -- ) + prim prim-num @ 8 + 4 .r space prim prim-name 2@ type cr ; + : output-forth ( -- ) prim prim-forth-code @ 0= IF \ output-alias @@ -1051,9 +1061,7 @@ stack inst-stream IP Cell : output-parts ( -- ) prim >r in-part on current-depth max-stacks cells erase - num-combined @ 0 +do - combined-prims i th @ output-part - loop + ['] output-part map-combined in-part off r> to prim ; @@ -1079,13 +1087,16 @@ stack inst-stream IP Cell \ peephole optimization rules +\ data for a simple peephole optimizer that always tries to combine +\ the currently compiled instruction with the last one. + \ in order for this to work as intended, shorter combinations for each \ length must be present, and the longer combinations must follow \ shorter ones (this restriction may go away in the future). : output-peephole ( -- ) combined-prims num-combined @ 1- cells combinations search-wordlist - s" the prefix for this combination must be defined earlier" ?print-error + s" the prefix for this superinstruction must be defined earlier" ?print-error ." {" execute prim-num @ 5 .r ." ," combined-prims num-combined @ 1- th @ prim-num @ 5 .r ." ," @@ -1093,15 +1104,53 @@ stack inst-stream IP Cell combined prim-c-name 2@ type ." */" cr ; -: output-forth-peephole ( -- ) - combined-prims num-combined @ 1- cells combinations search-wordlist - s" the prefix for this combination must be defined earlier" ?print-error - execute prim-num @ 5 .r - combined-prims num-combined @ 1- th @ prim-num @ 5 .r - combined prim-num @ 5 .r ." prim, \ " - combined prim-c-name 2@ type - cr ; +\ cost and superinstruction data for a sophisticated combiner (e.g., +\ shortest path) + +\ This is intended as initializer for a structure like this + +\ struct super { +\ int loads; /* number of stack loads */ +\ int stores; /* number of stack stores */ +\ int updates; /* number of stack pointer updates */ +\ int length; /* number of components */ +\ int *components; /* array of vm_prim indexes of components */ +\ }; + +\ How do you know which primitive or combined instruction this +\ structure refers to? By the order of cost structures, as in most +\ other cases. + +: compute-costs { p -- nloads nstores nupdates } + \ compute the number of loads, stores, and stack pointer updates + \ of a primitive or combined instruction; does not take TOS + \ caching into account, nor that IP updates are combined with + \ other stuff + 0 max-stacks 0 +do + p prim-stacks-in i th @ + + loop + 0 max-stacks 0 +do + p prim-stacks-out i th @ + + loop + 0 max-stacks 0 +do + p prim-stacks-in i th @ p prim-stacks-out i th @ <> - + loop ; + +: output-num-part ( p -- ) + prim-num @ 4 .r ." ," ; + +: output-costs ( -- ) + ." {" prim compute-costs + rot 2 .r ." ," swap 2 .r ." ," 2 .r ." ," + combined if + num-combined @ 2 .r + ." , ((int []){" ['] output-num-part map-combined ." })}, /* " + else + ." 1, ((int []){" prim prim-num @ 4 .r ." })}, /* " + endif + prim prim-name 2@ type ." */" + cr ; \ the parser