aoptcpub.pas 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Jonas Maebe, member of the Free Pascal
  4. Development Team
  5. This unit contains several types and constants necessary for the
  6. optimizer to work on the 80x86 architecture
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ****************************************************************************
  19. }
  20. Unit aoptcpub; { Assembler OPTimizer CPU specific Base }
  21. { enable the following define if memory references can have both a base and }
  22. { index register in 1 operand }
  23. {$define RefsHaveIndexReg}
  24. { enable the following define if memory references can have a scaled index }
  25. {$define RefsHaveScale}
  26. { enable the following define if memory references can have a segment }
  27. { override }
  28. {$define RefsHaveSegment}
  29. Interface
  30. uses aasm, cpubase, cpuasm, aoptbase;
  31. Type
  32. { possible actions on an operand: read, write or modify (= read & write) }
  33. TOpAction = (OpAct_Read, OpAct_Write, OpAct_Modify, OpAct_Unknown);
  34. { type of a normal instruction }
  35. TInstr = Taicpu;
  36. PInstr = ^TInstr;
  37. TFlag = (DirFlag);
  38. TFlagContents = (F_Unknown, F_Clear, F_Set);
  39. { ************************************************************************* }
  40. { **************************** TCondRegs ********************************** }
  41. { ************************************************************************* }
  42. { Info about the conditional registers }
  43. TCondRegs = Object
  44. Flags: Array[TFlag] of TFlagContents;
  45. Constructor Init;
  46. Procedure InitFlag(f: TFlag);
  47. Procedure SetFlag(f: TFlag);
  48. Procedure ClearFlag(f: TFlag);
  49. Function GetFlag(f: TFlag): TFlagContents;
  50. Destructor Done;
  51. End;
  52. { ************************************************************************* }
  53. { **************************** TAoptBaseCpu ******************************* }
  54. { ************************************************************************* }
  55. TAoptBaseCpu = Object(TAoptBase)
  56. Function RegMaxSize(Reg: TRegister): TRegister; Virtual;
  57. Function RegsSameSize(Reg1, Reg2: TRegister): Boolean; Virtual;
  58. Function IsLoadMemReg(p: pai): Boolean; Virtual;
  59. Function IsLoadConstReg(p: pai): Boolean; Virtual;
  60. Function IsStoreRegMem(p: pai): Boolean; Virtual;
  61. Function a_load_reg_reg(reg1, reg2: TRegister): paicpu; virtual;
  62. End;
  63. { ************************************************************************* }
  64. { ******************************* Constants ******************************* }
  65. { ************************************************************************* }
  66. Const
  67. { the maximum number of operands an instruction has }
  68. MaxOps = 3;
  69. {Oper index of operand that contains the source (reference) with a load }
  70. {instruction }
  71. LoadSrc = 0;
  72. {Oper index of operand that contains the destination (register) with a load }
  73. {instruction }
  74. LoadDst = 1;
  75. {Oper index of operand that contains the source (register) with a store }
  76. {instruction }
  77. StoreSrc = 0;
  78. {Oper index of operand that contains the destination (reference) with a load }
  79. {instruction }
  80. StoreDst = 1;
  81. Implementation
  82. uses cpuinfo;
  83. { ************************************************************************* }
  84. { **************************** TCondRegs ********************************** }
  85. { ************************************************************************* }
  86. Constructor TCondRegs.init;
  87. Begin
  88. FillChar(Flags, SizeOf(Flags), Byte(F_Unknown))
  89. End;
  90. Procedure TCondRegs.InitFlag(f: TFlag);
  91. Begin
  92. Flags[f] := F_Unknown
  93. End;
  94. Procedure TCondRegs.SetFlag(f: TFlag);
  95. Begin
  96. Flags[f] := F_Set
  97. End;
  98. Procedure TCondRegs.ClearFlag(f: TFlag);
  99. Begin
  100. Flags[f] := F_Clear
  101. End;
  102. Function TCondRegs.GetFlag(f: TFlag): TFlagContents;
  103. Begin
  104. GetFlag := Flags[f]
  105. End;
  106. Destructor TCondRegs.Done; {$ifdef inl} inline; {$endif inl}
  107. Begin
  108. End;
  109. { ************************************************************************* }
  110. { **************************** TAoptBaseCpu ******************************* }
  111. { ************************************************************************* }
  112. Function TAoptBaseCpu.RegMaxSize(Reg: TRegister): TRegister;
  113. Begin
  114. RegMaxSize := Reg;
  115. If (Reg >= R_AX)
  116. Then
  117. If (Reg <= R_DI)
  118. Then RegMaxSize := Reg16ToReg32(Reg)
  119. Else
  120. If (Reg <= R_BL)
  121. Then RegMaxSize := Reg8toReg32(Reg)
  122. End;
  123. Function TAOptBaseCpu.RegsSameSize(Reg1, Reg2: TRegister): Boolean;
  124. Begin
  125. If (Reg1 <= R_EDI)
  126. Then RegsSameSize := (Reg2 <= R_EDI)
  127. Else
  128. If (Reg1 <= R_DI)
  129. Then RegsSameSize := (Reg2 in [R_AX..R_DI])
  130. Else
  131. If (Reg1 <= R_BL)
  132. Then RegsSameSize := (Reg2 in [R_AL..R_BL])
  133. Else RegsSameSize := False
  134. End;
  135. Function TAOptBaseCpu.IsLoadMemReg(p: pai): Boolean;
  136. Begin
  137. IsLoadMemReg :=
  138. (p^.typ = ait_instruction) and
  139. ((PInstr(p)^.OpCode = A_MOV) or
  140. (PInstr(p)^.OpCode = A_MOVZX) or
  141. (PInstr(p)^.OpCode = A_MOVSX)) And
  142. (PInstr(p)^.oper[LoadSrc].typ = top_ref);
  143. End;
  144. Function TAOptBaseCpu.IsLoadConstReg(p: pai): Boolean;
  145. Begin
  146. IsLoadConstReg :=
  147. (p^.typ = ait_instruction) and
  148. (PInstr(p)^.OpCode = A_MOV) And
  149. (PInstr(p)^.oper[LoadSrc].typ = top_const);
  150. End;
  151. Function TAOptBaseCpu.IsStoreRegMem(p: pai): Boolean;
  152. Begin
  153. IsStoreRegMem :=
  154. (p^.typ = ait_instruction) and
  155. ((PInstr(p)^.OpCode = A_MOV) or
  156. (PInstr(p)^.OpCode = A_MOVZX) or
  157. (PInstr(p)^.OpCode = A_MOVSX)) And
  158. (PInstr(p)^.oper[StoreDst].typ = top_ref);
  159. End;
  160. Function TAOptBaseCpu.a_load_reg_reg(reg1, reg2: TRegister): paicpu;
  161. Begin
  162. a_load_reg_Reg := New(paicpu,Op_Reg_Reg(A_MOV, S_L, reg1, reg2))
  163. End;
  164. End.
  165. {
  166. $Log$
  167. Revision 1.2 2002-09-07 15:25:14 peter
  168. * old logs removed and tabs fixed
  169. }