2
0

aoptutils.pas 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. function MatchOpType(const p : taicpu;type0,type1 : toptype) : Boolean;
  24. {$if max_operands>2}
  25. function MatchOpType(const p : taicpu; type0,type1,type2 : toptype) : Boolean;
  26. {$endif max_operands>2}
  27. { skips all alignment fields and returns the next label (or non-align).
  28. returns immediately with true if hp is a label }
  29. function SkipAligns(hp: tai; out hp2: tai): boolean;
  30. { skips all labels and returns the next "real" instruction }
  31. function SkipLabels(hp: tai; out hp2: tai): boolean;
  32. { sets hp2 to hp and returns True if hp is not nil }
  33. function SetAndTest(const hp: tai; out hp2: tai): Boolean;
  34. implementation
  35. uses
  36. aasmbase;
  37. function MatchOpType(const p : taicpu; type0: toptype) : Boolean; inline;
  38. begin
  39. Result:=(p.ops=1) and (p.oper[0]^.typ=type0);
  40. end;
  41. function MatchOpType(const p : taicpu; type0,type1 : toptype) : Boolean; inline;
  42. begin
  43. Result:=(p.ops=2) and (p.oper[0]^.typ=type0) and (p.oper[1]^.typ=type1);
  44. end;
  45. {$if max_operands>2}
  46. function MatchOpType(const p : taicpu; type0,type1,type2 : toptype) : Boolean; inline;
  47. begin
  48. Result:=(p.ops=3) and (p.oper[0]^.typ=type0) and (p.oper[1]^.typ=type1) and (p.oper[2]^.typ=type2);
  49. end;
  50. {$endif max_operands>2}
  51. { skips all alignment fields and returns the next label (or non-align).
  52. Returns immediately with True if hp is a label }
  53. function SkipAligns(hp: tai; out hp2: tai): boolean;
  54. begin
  55. while assigned(hp) and
  56. (hp.typ in SkipInstr + [ait_label,ait_align]) Do
  57. begin
  58. { Check that the label is actually live }
  59. if (hp.typ = ait_label) and tai_label(hp).labsym.is_used then
  60. Break;
  61. hp := tai(hp.next);
  62. end;
  63. SkipAligns := SetAndTest(hp, hp2);
  64. end;
  65. { skips all labels and returns the next "real" instruction }
  66. function SkipLabels(hp: tai; out hp2: tai): boolean;
  67. begin
  68. while assigned(hp.next) and
  69. (tai(hp.next).typ in SkipInstr + [ait_label,ait_align]) Do
  70. hp := tai(hp.next);
  71. if assigned(hp.next) then
  72. begin
  73. SkipLabels := True;
  74. hp2 := tai(hp.next)
  75. end
  76. else
  77. begin
  78. hp2 := hp;
  79. SkipLabels := False
  80. end;
  81. end;
  82. { sets hp2 to hp and returns True if hp is not nil }
  83. function SetAndTest(const hp: tai; out hp2: tai): Boolean; inline;
  84. begin
  85. hp2 := hp;
  86. Result := Assigned(hp);
  87. end;
  88. end.