optbase.pas 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. {
  2. Basic node optimizer stuff
  3. Copyright (c) 2007 by Florian Klaempfl
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit optbase;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype;
  22. type
  23. { this should maybe replaced by a spare set,
  24. using a dyn. array makes assignments cheap }
  25. tdfaset = array of byte;
  26. PDFASet = ^TDFASet;
  27. toptinfo = record
  28. { index of the current node inside the dfa sets, aword(-1) if no entry }
  29. index : aword;
  30. { dfa }
  31. def : tdfaset;
  32. use : tdfaset;
  33. life : tdfaset;
  34. defsum : tdfaset;
  35. end;
  36. poptinfo = ^toptinfo;
  37. { basic set operations for dfa sets }
  38. { add e to s }
  39. procedure DFASetInclude(var s : tdfaset;e : integer);
  40. { add s to d }
  41. procedure DFASetIncludeSet(var d : tdfaset;const s : tdfaset);
  42. { remove e from s }
  43. procedure DFASetExclude(var s : tdfaset;e : integer);
  44. { test if s contains e }
  45. function DFASetIn(const s : tdfaset;e : integer) : boolean;
  46. { d:=s1+s2; }
  47. procedure DFASetUnion(var d : tdfaset;const s1,s2 : tdfaset);
  48. { d:=s1*s2; }
  49. procedure DFASetIntersect(var d : tdfaset;const s1,s2 : tdfaset);
  50. { d:=s1-s2; }
  51. procedure DFASetDiff(var d : tdfaset;const s1,s2 : tdfaset);
  52. { s1<>s2; }
  53. function DFASetNotEqual(const s1,s2 : tdfaset) : boolean;
  54. { output DFA set }
  55. procedure PrintDFASet(var f : text;s : TDFASet);
  56. implementation
  57. uses
  58. cutils;
  59. procedure DFASetInclude(var s : tdfaset;e : integer);
  60. var
  61. i,
  62. oldhigh,
  63. e8 : Integer;
  64. begin
  65. e8:=e div 8;
  66. if e8>high(s) then
  67. begin
  68. oldhigh:=high(s);
  69. SetLength(s,e8+1);
  70. for i:=oldhigh+1 to high(s) do
  71. s[i]:=0;
  72. end;
  73. s[e8]:=s[e8] or (1 shl (e mod 8));
  74. end;
  75. procedure DFASetIncludeSet(var d : tdfaset;const s : tdfaset);
  76. var
  77. i : integer;
  78. begin
  79. if length(s)>length(d) then
  80. SetLength(d,length(s));
  81. for i:=0 to high(s) do
  82. d[i]:=d[i] or s[i];
  83. end;
  84. procedure DFASetExclude(var s : tdfaset;e : integer);
  85. var
  86. e8 : Integer;
  87. begin
  88. e8:=e div 8;
  89. if e8>high(s) then
  90. SetLength(s,e8+1);
  91. s[e8]:=s[e8] and not(1 shl (e mod 8));
  92. end;
  93. function DFASetIn(const s : tdfaset;e : integer) : boolean;
  94. var
  95. e8 : Integer;
  96. begin
  97. result:=false;
  98. e8:=e div 8;
  99. if e8>high(s) then
  100. exit;
  101. result:=(s[e8] and (1 shl (e mod 8)))<>0;
  102. end;
  103. procedure DFASetUnion(var d : tdfaset;const s1,s2 : tdfaset);
  104. var
  105. i : integer;
  106. begin
  107. SetLength(d,max(Length(s1),Length(s2)));
  108. for i:=0 to high(s1) do
  109. d[i]:=s1[i];
  110. for i:=0 to high(s2) do
  111. d[i]:=d[i] or s2[i];
  112. end;
  113. procedure DFASetIntersect(var d : tdfaset;const s1,s2 : tdfaset);
  114. var
  115. i : integer;
  116. begin
  117. SetLength(d,min(Length(s1),Length(s2)));
  118. for i:=0 to min(high(s1),high(s2)) do
  119. d[i]:=s1[i] and s2[i];
  120. end;
  121. procedure DFASetDiff(var d : tdfaset;const s1,s2 : tdfaset);
  122. var
  123. i : integer;
  124. begin
  125. SetLength(d,length(s1));
  126. for i:=0 to high(d) do
  127. if i>high(s2) then
  128. d[i]:=s1[i]
  129. else
  130. d[i]:=s1[i] and not(s2[i]);
  131. end;
  132. function DFASetNotEqual(const s1,s2 : tdfaset) : boolean;
  133. var
  134. i : integer;
  135. begin
  136. result:=true;
  137. { one set could be larger than the other }
  138. if length(s1)>length(s2) then
  139. begin
  140. for i:=0 to high(s2) do
  141. if s1[i]<>s2[i] then
  142. exit;
  143. { check remaining part being zero }
  144. for i:=length(s2) to high(s1) do
  145. if s1[i]<>0 then
  146. exit;
  147. end
  148. else
  149. begin
  150. for i:=0 to high(s1) do
  151. if s1[i]<>s2[i] then
  152. exit;
  153. { check remaining part being zero }
  154. for i:=length(s1) to high(s2) do
  155. if s2[i]<>0 then
  156. exit;
  157. end;
  158. result:=false;
  159. end;
  160. procedure PrintDFASet(var f : text;s : TDFASet);
  161. var
  162. i : integer;
  163. first : boolean;
  164. begin
  165. first:=true;
  166. for i:=0 to Length(s)*8 do
  167. begin
  168. if DFASetIn(s,i) then
  169. begin
  170. if not(first) then
  171. write(f,',');
  172. write(f,i);
  173. first:=false;
  174. end;
  175. end;
  176. end;
  177. end.