aoptbase.pas 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Jonas Maebe, member of the Free Pascal
  4. Development Team
  5. This unit contains the base of all optimizer related objects
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit aoptbase;
  20. Interface
  21. uses aasmbase,aasmcpu,aasmtai,
  22. cpubase;
  23. { the number of tai objects processed by an optimizer object since the last }
  24. { time a register was modified }
  25. Type TInstrSinceLastMod = Array[LoGPReg..HiGPReg] of byte;
  26. { the TAopBase object implements the basic methods that most other }
  27. { assembler optimizer objects require }
  28. Type
  29. TAoptBase = Object
  30. { processor independent methods }
  31. constructor init;
  32. destructor done;
  33. { returns true if register Reg is used by instruction p1 }
  34. Function RegInInstruction(Reg: TRegister; p1: tai): Boolean;
  35. { returns true if register Reg occurs in operand op }
  36. Function RegInOp(Reg: TRegister; const op: toper): Boolean;
  37. { returns true if register Reg is used in the reference Ref }
  38. Function RegInRef(Reg: TRegister; Const Ref: TReference): Boolean;
  39. { returns true if the references are completely equal }
  40. {Function RefsEqual(Const R1, R2: TReference): Boolean;}
  41. { gets the next tai object after current that contains info relevant }
  42. { to the optimizer in p1. If there is none, it returns false and }
  43. { sets p1 to nil }
  44. Function GetNextInstruction(Current: tai; Var Next: tai): Boolean;
  45. { gets the previous tai object after current that contains info }
  46. { relevant to the optimizer in last. If there is none, it retuns }
  47. { false and sets last to nil }
  48. Function GetLastInstruction(Current: tai; Var Last: tai): Boolean;
  49. { processor dependent methods }
  50. { returns the maximum width component of Reg. Only has to be }
  51. { overridden for the 80x86 (afaik) }
  52. Function RegMaxSize(Reg: TRegister): TRegister; Virtual;
  53. { returns true if Reg1 and Reg2 are of the samae width. Only has to }
  54. { overridden for the 80x86 (afaik) }
  55. Function RegsSameSize(Reg1, Reg2: TRegister): Boolean; Virtual;
  56. { returns whether P is a load instruction (load contents from a }
  57. { memory location or (register) variable into a register) }
  58. Function IsLoadMemReg(p: tai): Boolean; Virtual;
  59. { returns whether P is a load constant instruction (load a constant }
  60. { into a register) }
  61. Function IsLoadConstReg(p: tai): Boolean; Virtual;
  62. { returns whether P is a store instruction (store contents from a }
  63. { register to a memory location or to a (register) variable) }
  64. Function IsStoreRegMem(p: tai): Boolean; Virtual;
  65. { create a paicpu Object that loads the contents of reg1 into reg2 }
  66. Function a_load_reg_reg(reg1, reg2: TRegister): taicpu; Virtual;
  67. end;
  68. Function RefsEqual(Const R1, R2: TReference): Boolean;
  69. Implementation
  70. uses globals, aoptcpub, cpuinfo;
  71. Function RefsEqual(Const R1, R2: TReference): Boolean;
  72. Begin
  73. RefsEqual := (R1.Offset+R1.OffsetFixup = R2.Offset+R2.OffsetFixup)
  74. And (R1.Base = R2.Base)
  75. {$ifdef RefsHaveindex}
  76. And (R1.Index = R2.Index)
  77. {$endif RefsHaveindex}
  78. {$ifdef RefsHaveScale}
  79. And (R1.ScaleFactor = R2.ScaleFactor)
  80. {$endif RefsHaveScale}
  81. And (R1.Symbol = R2.Symbol)
  82. {$ifdef RefsHaveSegment}
  83. And (R1.Segment = R2.Segment)
  84. {$endif RefsHaveSegment}
  85. ;
  86. End;
  87. constructor taoptbase.init;
  88. begin
  89. end;
  90. destructor taoptbase.done;
  91. begin
  92. end;
  93. Function TAOptBase.RegInInstruction(Reg: TRegister; p1: tai): Boolean;
  94. Var Count: AWord;
  95. TmpResult: Boolean;
  96. Begin
  97. TmpResult := False;
  98. Count := 0;
  99. If (p1.typ = ait_instruction) Then
  100. Repeat
  101. TmpResult := RegInOp(Reg, PInstr(p1)^.oper[Count]);
  102. Inc(Count)
  103. Until (Count = MaxOps) or TmpResult;
  104. RegInInstruction := TmpResult
  105. End;
  106. Function TAOptBase.RegInOp(Reg: TRegister; const op: toper): Boolean;
  107. Begin
  108. Case op.typ Of
  109. Top_Reg: RegInOp := Reg = op.reg;
  110. Top_Ref: RegInOp := RegInRef(Reg, op.ref^)
  111. Else RegInOp := False
  112. End
  113. End;
  114. Function TAOptBase.RegInRef(Reg: TRegister; Const Ref: TReference): Boolean;
  115. Begin
  116. Reg := RegMaxSize(Reg);
  117. RegInRef := (Ref.Base = Reg)
  118. {$ifdef RefsHaveIndexReg}
  119. Or (Ref.Index = Reg)
  120. {$endif RefsHaveIndexReg}
  121. End;
  122. Function TAOptBase.GetNextInstruction(Current: tai; Var Next: tai): Boolean;
  123. Begin
  124. Repeat
  125. Current := tai(Current.Next);
  126. While Assigned(Current) And
  127. ((Current.typ In SkipInstr) or
  128. ((Current.typ = ait_label) And
  129. Not(Tai_Label(Current).l.is_used))) Do
  130. Current := tai(Current.Next);
  131. If Assigned(Current) And
  132. (Current.typ = ait_Marker) And
  133. (Tai_Marker(Current).Kind = NoPropInfoStart) Then
  134. Begin
  135. While Assigned(Current) And
  136. ((Current.typ <> ait_Marker) Or
  137. (Tai_Marker(Current).Kind <> NoPropInfoEnd)) Do
  138. Current := Tai(Current.Next);
  139. End;
  140. Until Not(Assigned(Current)) Or
  141. (Current.typ <> ait_Marker) Or
  142. (Tai_Marker(Current).Kind <> NoPropInfoEnd);
  143. Next := Current;
  144. If Assigned(Current) And
  145. Not((Current.typ In SkipInstr) or
  146. ((Current.typ = ait_label) And
  147. Not(Tai_Label(Current).l.is_used)))
  148. Then GetNextInstruction := True
  149. Else
  150. Begin
  151. Next := Nil;
  152. GetNextInstruction := False;
  153. End;
  154. End;
  155. Function TAOptBase.GetLastInstruction(Current: tai; Var Last: tai): Boolean;
  156. Begin
  157. Repeat
  158. Current := Tai(Current.previous);
  159. While Assigned(Current) And
  160. (((Current.typ = ait_Marker) And
  161. Not(Tai_Marker(Current).Kind in [AsmBlockEnd,NoPropInfoEnd])) or
  162. (Current.typ In SkipInstr) or
  163. ((Current.typ = ait_label) And
  164. Not(Tai_Label(Current).l.is_used))) Do
  165. Current := Tai(Current.previous);
  166. If Assigned(Current) And
  167. (Current.typ = ait_Marker) And
  168. (Tai_Marker(Current).Kind = NoPropInfoEnd) Then
  169. Begin
  170. While Assigned(Current) And
  171. ((Current.typ <> ait_Marker) Or
  172. (Tai_Marker(Current).Kind <> NoPropInfoStart)) Do
  173. Current := Tai(Current.previous);
  174. End;
  175. Until Not(Assigned(Current)) Or
  176. (Current.typ <> ait_Marker) Or
  177. (Tai_Marker(Current).Kind <> NoPropInfoStart);
  178. If Not(Assigned(Current)) or
  179. (Current.typ In SkipInstr) or
  180. ((Current.typ = ait_label) And
  181. Not(Tai_Label(Current).l.is_used)) or
  182. ((Current.typ = ait_Marker) And
  183. (Tai_Marker(Current).Kind = AsmBlockEnd))
  184. Then
  185. Begin
  186. Last := Nil;
  187. GetLastInstruction := False
  188. End
  189. Else
  190. Begin
  191. Last := Current;
  192. GetLastInstruction := True;
  193. End;
  194. End;
  195. { ******************* Processor dependent stuff *************************** }
  196. Function TAOptBase.RegMaxSize(Reg: TRegister): TRegister;
  197. Begin
  198. RegMaxSize := Reg
  199. End;
  200. Function TAOptBase.RegsSameSize(Reg1, Reg2: TRegister): Boolean;
  201. Begin
  202. RegsSameSize := True
  203. End;
  204. Function TAOptBase.IsLoadMemReg(p: tai): Boolean;
  205. Begin
  206. Abstract
  207. End;
  208. Function TAOptBase.IsLoadConstReg(p: tai): Boolean;
  209. Begin
  210. Abstract
  211. End;
  212. Function TAOptBase.IsStoreRegMem(p: tai): Boolean;
  213. Begin
  214. Abstract
  215. End;
  216. Function TAoptBase.a_load_reg_reg(reg1, reg2: TRegister): taicpu;
  217. Begin
  218. Abstract
  219. End;
  220. End.
  221. {
  222. $Log$
  223. Revision 1.6 2002-07-07 09:52:32 florian
  224. * powerpc target fixed, very simple units can be compiled
  225. * some basic stuff for better callparanode handling, far from being finished
  226. Revision 1.5 2002/05/18 13:34:05 peter
  227. * readded missing revisions
  228. Revision 1.4 2002/05/16 19:46:34 carl
  229. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  230. + try to fix temp allocation (still in ifdef)
  231. + generic constructor calls
  232. + start of tassembler / tmodulebase class cleanup
  233. }