optbase.pas 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. e8 : Integer;
  63. begin
  64. e8:=e div 8;
  65. if e8>high(s) then
  66. SetLength(s,e8+1);
  67. s[e8]:=s[e8] or (1 shl (e mod 8));
  68. end;
  69. procedure DFASetIncludeSet(var d : tdfaset;const s : tdfaset);
  70. var
  71. i : integer;
  72. begin
  73. if length(s)>length(d) then
  74. SetLength(d,length(s));
  75. for i:=0 to high(s) do
  76. d[i]:=d[i] or s[i];
  77. end;
  78. procedure DFASetExclude(var s : tdfaset;e : integer);
  79. var
  80. e8 : Integer;
  81. begin
  82. e8:=e div 8;
  83. if e8<=high(s) then
  84. s[e8]:=s[e8] and not(1 shl (e mod 8));
  85. end;
  86. function DFASetIn(const s : tdfaset;e : integer) : boolean;
  87. var
  88. e8 : Integer;
  89. begin
  90. e8:=e div 8;
  91. if e8<=high(s) then
  92. result:=(s[e8] and (1 shl (e mod 8)))<>0
  93. else
  94. result:=false;
  95. end;
  96. procedure DFASetUnion(var d : tdfaset;const s1,s2 : tdfaset);
  97. var
  98. i : integer;
  99. begin
  100. SetLength(d,max(Length(s1),Length(s2)));
  101. for i:=0 to min(high(s1),high(s2)) do
  102. d[i]:=s1[i] or s2[i];
  103. if high(s1)<high(s2) then
  104. for i:=high(s1)+1 to high(s2) do
  105. d[i]:=s2[i]
  106. else
  107. for i:=high(s2)+1 to high(s1) do
  108. d[i]:=s1[i];
  109. end;
  110. procedure DFASetIntersect(var d : tdfaset;const s1,s2 : tdfaset);
  111. var
  112. i : integer;
  113. begin
  114. SetLength(d,min(Length(s1),Length(s2)));
  115. for i:=0 to high(d) do
  116. d[i]:=s1[i] and s2[i];
  117. end;
  118. procedure DFASetDiff(var d : tdfaset;const s1,s2 : tdfaset);
  119. var
  120. i : integer;
  121. begin
  122. SetLength(d,length(s1));
  123. for i:=0 to high(d) do
  124. if i>high(s2) then
  125. d[i]:=s1[i]
  126. else
  127. d[i]:=s1[i] and not(s2[i]);
  128. end;
  129. function DFASetNotEqual(const s1,s2 : tdfaset) : boolean;
  130. var
  131. i : integer;
  132. begin
  133. result:=true;
  134. { one set could be larger than the other }
  135. if length(s1)>length(s2) then
  136. begin
  137. for i:=0 to high(s2) do
  138. if s1[i]<>s2[i] then
  139. exit;
  140. { check remaining part being zero }
  141. for i:=length(s2) to high(s1) do
  142. if s1[i]<>0 then
  143. exit;
  144. end
  145. else
  146. begin
  147. for i:=0 to high(s1) do
  148. if s1[i]<>s2[i] then
  149. exit;
  150. { check remaining part being zero }
  151. for i:=length(s1) to high(s2) do
  152. if s2[i]<>0 then
  153. exit;
  154. end;
  155. result:=false;
  156. end;
  157. procedure PrintDFASet(var f : text;s : TDFASet);
  158. var
  159. i : integer;
  160. first : boolean;
  161. begin
  162. first:=true;
  163. for i:=0 to Length(s)*8 do
  164. begin
  165. if DFASetIn(s,i) then
  166. begin
  167. if not(first) then
  168. write(f,',');
  169. write(f,i);
  170. first:=false;
  171. end;
  172. end;
  173. end;
  174. end.