aoptutils.pas 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. {
  2. Copyright (c) 1998-2016 by Florian Klaempfl and Jonas Maebe
  3. This unit contains helper procedures for the assembler peephole optimizer
  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 aoptutils;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cpubase,aasmtai,aasmcpu;
  22. function MatchOpType(const p : taicpu;type0: toptype) : Boolean;
  23. {$if max_operands>1}
  24. function MatchOpType(const p : taicpu;type0,type1 : toptype) : Boolean;
  25. {$endif max_operands>1}
  26. {$if max_operands>2}
  27. function MatchOpType(const p : taicpu; type0,type1,type2 : toptype) : Boolean;
  28. {$endif max_operands>2}
  29. { skips all labels and returns the next "real" instruction }
  30. function SkipLabels(hp: tai; out hp2: tai): boolean;
  31. { sets hp2 to hp and returns True if hp is not nil }
  32. function SetAndTest(const hp: tai; out hp2: tai): Boolean; inline;
  33. { Set Store and Result to Condition (useful as an inline assignment in a conditional block) }
  34. function SetAndTest(const Condition: Boolean; out Store: Boolean): Boolean; inline;
  35. implementation
  36. uses
  37. aasmbase;
  38. function MatchOpType(const p : taicpu; type0: toptype) : Boolean; inline;
  39. begin
  40. Result:=(p.ops=1) and (p.oper[0]^.typ=type0);
  41. end;
  42. {$if max_operands>1}
  43. function MatchOpType(const p : taicpu; type0,type1 : toptype) : Boolean; inline;
  44. begin
  45. Result:=(p.ops=2) and (p.oper[0]^.typ=type0) and (p.oper[1]^.typ=type1);
  46. end;
  47. {$endif max_operands>1}
  48. {$if max_operands>2}
  49. function MatchOpType(const p : taicpu; type0,type1,type2 : toptype) : Boolean; inline;
  50. begin
  51. Result:=(p.ops=3) and (p.oper[0]^.typ=type0) and (p.oper[1]^.typ=type1) and (p.oper[2]^.typ=type2);
  52. end;
  53. {$endif max_operands>2}
  54. { skips all labels and returns the next "real" instruction }
  55. function SkipLabels(hp: tai; out hp2: tai): boolean;
  56. begin
  57. while assigned(hp.next) and
  58. (tai(hp.next).typ in SkipInstr + [ait_label]) Do
  59. hp := tai(hp.next);
  60. if assigned(hp.next) then
  61. begin
  62. SkipLabels := True;
  63. hp2 := tai(hp.next)
  64. end
  65. else
  66. begin
  67. hp2 := hp;
  68. SkipLabels := False
  69. end;
  70. end;
  71. { sets hp2 to hp and returns True if hp is not nil }
  72. function SetAndTest(const hp: tai; out hp2: tai): Boolean;
  73. begin
  74. hp2 := hp;
  75. Result := Assigned(hp);
  76. end;
  77. { Set Store and Result to Condition (useful as an inline assignment in a conditional block) }
  78. function SetAndTest(const Condition: Boolean; out Store: Boolean): Boolean;
  79. begin
  80. Store := Condition;
  81. Result := Store;
  82. end;
  83. end.