optutils.pas 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. {
  2. Helper routines for the optimizer
  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 optutils;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node;
  22. procedure SetNodeSucessors(p : tnode);
  23. implementation
  24. uses
  25. nbas,nflw;
  26. procedure SetNodeSucessors(p : tnode);
  27. var
  28. Continuestack : TFPList;
  29. Breakstack : TFPList;
  30. { sets the successor nodes of a node tree block
  31. returns the first node of the tree if it's a controll flow node }
  32. function DoSet(p : tnode;succ : tnode) : tnode;
  33. var
  34. hp1,hp2 : tnode;
  35. begin
  36. result:=nil;
  37. case p.nodetype of
  38. statementn:
  39. begin
  40. hp1:=p;
  41. result:=p;
  42. while assigned(hp1) do
  43. begin
  44. if assigned(tstatementnode(hp1).right) then
  45. begin
  46. hp2:=DoSet(tstatementnode(hp1).statement,tstatementnode(hp1).next);
  47. if assigned(hp2) then
  48. tstatementnode(hp1).successor:=hp2
  49. else
  50. tstatementnode(hp1).successor:=tstatementnode(hp1).right;
  51. end
  52. else
  53. begin
  54. hp2:=DoSet(tstatementnode(hp1).statement,successor);
  55. if assigned(hp2) then
  56. tstatementnode(hp1).successor:=hp2
  57. else
  58. tstatementnode(hp1).successor:=successor;
  59. end;
  60. end;
  61. end;
  62. blockn:
  63. begin
  64. result:=DoSet(tblocknode(p).statements,successor);
  65. end;
  66. forn:
  67. begin
  68. Breakstack.Add(successor);
  69. Continuestack.Add(p);
  70. result:=p;
  71. DoSet(tfornode(p).statements,successor);
  72. Breakstack.Delete(Count-1);
  73. Continuestack.Delete(Count-1);
  74. end;
  75. breakn:
  76. begin
  77. result:=p;
  78. p.successor:=tnode(Breakstack.Last);
  79. end;
  80. continuen:
  81. begin
  82. result:=p;
  83. p.successor:=tnode(Continuestack.Last);
  84. end;
  85. { exit is actually a jump to some final. code
  86. exitn:
  87. begin
  88. result:=p;
  89. p.successor:=nil;
  90. end;
  91. }
  92. ifn,
  93. whilerepeatn,
  94. exitn,
  95. withn,
  96. casen,
  97. labeln,
  98. goton,
  99. tryexceptn,
  100. raisen,
  101. tryfinallyn,
  102. onn,
  103. nothingn:
  104. internalerror(2007050501);
  105. end;
  106. end;
  107. begin
  108. Breakstack:=TFPList.Create;
  109. Continuestack:=TFPList.Create;
  110. DoSet(p,nil);
  111. Continuestack.Free;
  112. Breakstack.Free;
  113. end;
  114. end.