optbase.pas 5.3 KB

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