home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lifeos2.zip / LIFE-1.02 / TESTS / LF / PEEP.LF < prev    next >
Text File  |  1996-06-04  |  1KB  |  59 lines

  1. % A generic peephole optimizer.
  2.  
  3. % This rewrites patterns in an input list to patterns in an output list.
  4. % Closure is done.
  5.  
  6. % Example input:
  7. % > peep([a,b,c,a,a,t],Out)?
  8. % One peephole pass done with 3 changes.
  9. % One peephole pass done with 0 changes.
  10. % *** Yes
  11. % Out = [c,a,t].
  12. % --1>
  13.  
  14. import("accumulators")?
  15.  
  16. % Accumulator declarations:
  17. acc_info(in,     X,Out,In,acc_pred=>(Out=[X|In]))?
  18. acc_info(out,    X,Out,In,acc_pred=>(Out=[X|In]))?
  19. acc_info(changes,X,In,Out,acc_pred=>(Out=In+X))?
  20.  
  21. pred_info([peep_loop,peep_one], [in,out,changes])?
  22. % pred_info(peep_one,  [in,out,changes])?
  23.  
  24. % Closure calculation of peepholer:
  25. peep(In, Out) :--
  26.     peep_loop
  27.         with (in(In,[]), out(Mid,[]), changes(0,C)),
  28.     write("One peephole pass done with ",C," changes."), nl,
  29.     cond(C=:=0, Out=Mid, peep(Mid,Out))?
  30.  
  31. peep_loop :--
  32.     % Deep magic (a brownie point if you figure out what this does !):
  33.     % This continues peepholing with the result of peep_one:
  34.     peep_one with (glob(in) = in => inv(out)),
  35.     !,
  36.     1+changes,
  37.     peep_loop?
  38. peep_loop :-- [] is in, !, [] is out?
  39. peep_loop :-- I+in, I+out, peep_loop?
  40.  
  41. % Sample set of peephole rules:
  42. peep_one :--     % Rewrite a,b => c.
  43.     a+in,
  44.     b+in,
  45.     !,
  46.     c+out?
  47. peep_one :--    % Rewrite X,X => X. (This unifies adjacent elements, if possible).
  48.     X+in,
  49.     X+in,
  50.     !,
  51.     X+out?
  52. peep_one :--    % Rewrite X,Y => X. (This does not unify adjacent elements).
  53.     X+in,
  54.     Y+in,
  55.     {X===Y},
  56.     !,
  57.     X+out?
  58.