daopt386.pas 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  1. {
  2. $Id$
  3. Copyright (c) 1997-98 by Jonas Maebe
  4. This unit contains the data flow analyzer and several helper procedures
  5. and functions.
  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. {$ifDef TP}
  20. {$UnDef JumpAnal}
  21. {$Endif TP}
  22. Unit DAOpt386;
  23. Interface
  24. Uses AAsm, CObjects
  25. {$ifdef i386}
  26. ,i386
  27. {$endif}
  28. ;
  29. {*********************** Procedures and Functions ************************}
  30. Procedure InsertLLItem(AsmL: PAasmOutput; prev, foll, new_one: PLinkedList_Item);
  31. Function Reg32(Reg: TRegister): TRegister;
  32. Function RefsEqual(Const R1, R2: TReference): Boolean;
  33. Function IsGP32Reg(Reg: TRegister): Boolean;
  34. Function RegInRef(Reg: TRegister; Const Ref: TReference): Boolean;
  35. Function RegInInstruction(Reg: TRegister; p1: Pai): Boolean;
  36. Function PowerOf2(L: Longint): Longint;
  37. Function GetNextInstruction(Current: Pai; Var Next: Pai): Boolean;
  38. Function GetLastInstruction(Current: Pai; Var Last: Pai): Boolean;
  39. Function RegsSameContent(p1, p2: Pai; Reg: TRegister): Boolean;
  40. Function InstructionsEqual(p1, p2: Pai): Boolean;
  41. Procedure DFAPass1(AsmL: PAasmOutput);
  42. Function DFAPass2(AsmL: PAasmOutput): Pai;
  43. Procedure ShutDownDFA;
  44. Function FindLabel(L: PLabel; Var hp: Pai): Boolean;
  45. {Procedure FindLoHiLabels(AsmL: PAasmOutput; Var LoLab, HiLab, LabDif: Longint);}
  46. {******************************* Constants *******************************}
  47. Const
  48. {ait_* types which don't result in executable code or which don't influence
  49. the way the program runs/behaves}
  50. SkipInstr = [ait_comment
  51. {$ifdef GDB}
  52. ,ait_stabs, ait_stabn, ait_stab_function_name
  53. {$endif GDB}
  54. {$ifdef regalloc}
  55. ,ait_regalloc, ait_regdealloc
  56. {$endif regalloc}
  57. ];
  58. {the maximum number of things (registers, memory, ...) a single instruction
  59. changes}
  60. MaxCh = 3;
  61. {Possible register content types}
  62. con_Unknown = 0;
  63. con_ref = 1;
  64. con_const = 2;
  65. {********************************* Types *********************************}
  66. Type
  67. {What an instruction can change}
  68. TChange = (C_None,
  69. C_EAX, C_ECX, C_EDX, C_EBX, C_ESP, C_EBP, C_ESI, C_EDI,
  70. C_CDirFlag {clear direction flag}, C_SDirFlag {set dir flag},
  71. C_Flags, C_FPU, C_Op1, C_Op2, C_Op3, C_MemEDI);
  72. {the possible states of a flag}
  73. TFlagContents = (F_Unknown, F_NotSet, F_Set);
  74. {the properties of a cpu instruction}
  75. TAsmInstrucProp = Record
  76. {how many things it changes}
  77. NCh: Byte;
  78. {and what it changes}
  79. Ch: Array[1..MaxCh] of TChange;
  80. End;
  81. TContent = Record
  82. {start and end of block instructions that defines the
  83. content of this register. If Typ = con_const, then
  84. Longint(StartMod) = value of the constant)}
  85. StartMod: Pointer;
  86. {starts at 1, gets increased everytime the register is modified}
  87. State: Word;
  88. {how many instructions starting with StarMod does the block consist of}
  89. NrOfMods: Byte;
  90. {if one register gets a block assigned from an other register,
  91. this variable holds the name of that register (so it can be
  92. substituted when checking the block afterwards)}
  93. { ModReg: TRegister; }
  94. {the tpye of the content of the register: constant, ...}
  95. Typ: Byte;
  96. End;
  97. {Contents of the integer registers}
  98. TRegContent = Array[R_NO..R_EDI] Of TContent;
  99. {contents of the FPU registers}
  100. TRegFPUContent = Array[R_ST..R_ST7] Of TContent;
  101. {information record with the contents of every register. Every Pai object
  102. gets one of these assigned: a pointer to it is stored in the Line field and
  103. the original line number is stored in LineSave}
  104. TPaiProp = Record
  105. Regs: TRegContent;
  106. { FPURegs: TRegFPUContent;} {currently not yet used}
  107. LineSave: Longint;
  108. {status of the direction flag}
  109. DirFlag: TFlagContents;
  110. {can this instruction be removed?}
  111. CanBeRemoved: Boolean;
  112. End;
  113. PPaiProp = ^TPaiProp;
  114. {$IfDef TP}
  115. TPaiPropBlock = Array[1..(65520 div (((SizeOf(TPaiProp)+1)div 2)*2))] Of TPaiProp;
  116. {$else}
  117. TPaiPropBlock = Array[1..250000] Of TPaiProp;
  118. {$EndIf TP}
  119. PPaiPropBlock = ^TPaiPropBlock;
  120. TInstrSinceLastMod = Array[R_EAX..R_EDI] Of Byte;
  121. TLabelTableItem = Record
  122. PaiObj: Pai;
  123. {$IfNDef TP}
  124. InstrNr: Longint;
  125. RefsFound: Word;
  126. JmpsProcessed: Word
  127. {$EndIf TP}
  128. End;
  129. {$IfDef tp}
  130. TLabelTable = Array[0..10000] Of TLabelTableItem;
  131. {$Else tp}
  132. TLabelTable = Array[0..2500000] Of TLabelTableItem;
  133. {$Endif tp}
  134. PLabelTable = ^TLabelTable;
  135. TwoWords = Record
  136. Word1, Word2: Word;
  137. End;
  138. {******************************* Variables *******************************}
  139. Var
  140. {the amount of PaiObjects in the current assembler list}
  141. NrOfPaiObjs,
  142. {for TP only: the amount of PPaiProps that can be stored in the PaiPropBlock}
  143. NrOfPaiFast: Longint;
  144. {Array which holds all (FPC) or as much as possible (TP) PPaiProps}
  145. PaiPropBlock: PPaiPropBlock;
  146. LoLab, HiLab, LabDif: Longint;
  147. LTable: PLabelTable;
  148. {*********************** End of Interface section ************************}
  149. Implementation
  150. Uses globals, systems, strings, verbose, hcodegen,
  151. {$ifdef i386}
  152. cgi386;
  153. {$endif i386}
  154. Const AsmInstr: Array[tasmop] Of TAsmInstrucProp = (
  155. {MOV} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  156. {MOVZX} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  157. {MOVSX} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  158. {LABEL} (NCh: 255; Ch: (C_None, C_None, C_None)), {don't know value of any register}
  159. {ADD} (NCh: 2; Ch: (C_Op2, C_Flags, C_None)),
  160. {CALL} (NCh: 255; Ch: (C_None, C_None, C_None)), {don't know value of any register}
  161. {IDIV} (NCh: 3; Ch: (C_EAX, C_EDX, C_Flags)),
  162. {IMUL} (NCh: 3; Ch: (C_EAX, C_EDX, C_Flags)), {handled separately, because several forms exist}
  163. {JMP} (NCh: 255; Ch: (C_None, C_None, C_None)), {don't know value of any register}
  164. {LEA} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  165. {MUL} (NCh: 3; Ch: (C_EAX, C_EDX, C_Flags)),
  166. {NEG} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  167. {NOT} (NCh: 2; Ch: (C_Op1, C_Flags, C_None)),
  168. {POP} (NCh: 2; Ch: (C_Op1, C_ESP, C_None)),
  169. {POPAD} (NCh: 255; Ch: (C_None, C_None, C_None)), {don't know value of any register}
  170. {PUSH} (NCh: 1; Ch: (C_ESP, C_None, C_None)),
  171. {PUSHAD} (NCh: 1; Ch: (C_ESP, C_None, C_None)),
  172. {RET} (NCh: 255; Ch: (C_None, C_None, C_None)), {don't know value of any register}
  173. {SUB} (NCh: 2; Ch: (C_Op2, C_Flags, C_None)),
  174. {XCHG} (NCh: 2; Ch: (C_Op1, C_Op2, C_None)), {(will be) handled seperately}
  175. {XOR} (NCh: 2; Ch: (C_Op2, C_Flags, C_None)),
  176. {FILD} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  177. {CMP} (NCh: 1; Ch: (C_Flags, C_None, C_None)),
  178. {JZ} (NCh: 0; Ch: (C_None, C_None, C_None)),
  179. {INC} (NCh: 2; Ch: (C_Op1, C_Flags, C_None)),
  180. {DEC} (NCh: 2; Ch: (C_Op1, C_Flags, C_None)),
  181. {SETE} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  182. {SETNE} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  183. {SETL} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  184. {SETG} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  185. {SETLE} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  186. {SETGE} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  187. {JE} (NCh: 0; Ch: (C_None, C_None, C_None)),
  188. {JNE} (NCh: 0; Ch: (C_None, C_None, C_None)),
  189. {JL} (NCh: 0; Ch: (C_None, C_None, C_None)),
  190. {JG} (NCh: 0; Ch: (C_None, C_None, C_None)),
  191. {JLE} (NCh: 0; Ch: (C_None, C_None, C_None)),
  192. {JGE} (NCh: 0; Ch: (C_None, C_None, C_None)),
  193. {OR} (NCh: 2; Ch: (C_Op2, C_Flags, C_None)),
  194. {FLD} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  195. {FADD} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  196. {FMUL} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  197. {FSUB} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  198. {FDIV} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  199. {FCHS} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  200. {FLD1} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  201. {FIDIV} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  202. {CLTD} (NCh: 1; Ch: (C_EDX, C_None, C_None)),
  203. {JNZ} (NCh: 0; Ch: (C_None, C_None, C_None)),
  204. {FSTP} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  205. {AND} (NCh: 2; Ch: (C_Op2, C_Flags, C_None)),
  206. {JNO} (NCh: 0; Ch: (C_None, C_None, C_None)),
  207. {NOTH} (NCh: 0; Ch: (C_None, C_None, C_None)), {***???***}
  208. {NONE} (NCh: 0; Ch: (C_None, C_None, C_None)),
  209. {ENTER} (NCh: 1; Ch: (C_ESP, C_None, C_None)),
  210. {LEAVE} (NCh: 1; Ch: (C_ESP, C_None, C_None)),
  211. {CLD} (NCh: 1; Ch: (C_CDirFlag, C_None, C_None)),
  212. {MOVS} (NCh: 3; Ch: (C_ESI, C_EDI, C_MemEDI)),
  213. {REP} (NCh: 1; Ch: (C_ECX, C_None, C_None)),
  214. {SHL} (NCh: 2; Ch: (C_Op2, C_Flags, C_None)),
  215. {SHR} (NCh: 2; Ch: (C_Op2, C_Flags, C_None)),
  216. {BOUND} (NCh: 0; Ch: (C_None, C_None, C_None)),
  217. {JNS} (NCh: 0; Ch: (C_None, C_None, C_None)),
  218. {JS} (NCh: 0; Ch: (C_None, C_None, C_None)),
  219. {JO} (NCh: 0; Ch: (C_None, C_None, C_None)),
  220. {SAR} (NCh: 2; Ch: (C_Op2, C_Flags, C_None)),
  221. {TEST} (NCh: 1; Ch: (C_Flags, C_None, C_None)),
  222. {FCOM} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  223. {FCOMP} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  224. {FCOMPP} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  225. {FXCH} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  226. {FADDP} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  227. {FMULP} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  228. {FSUBP} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  229. {FDIVP} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  230. {FNSTS} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  231. {SAHF} (NCh: 1; Ch: (C_Flags, C_None, C_None)),
  232. {FDIVRP} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  233. {FSUBRP} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  234. {SETC} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  235. {SETNC} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  236. {JC} (NCh: 0; Ch: (C_None, C_None, C_None)),
  237. {JNC} (NCh: 0; Ch: (C_None, C_None, C_None)),
  238. {JA} (NCh: 0; Ch: (C_None, C_None, C_None)),
  239. {JAE} (NCh: 0; Ch: (C_None, C_None, C_None)),
  240. {JB} (NCh: 0; Ch: (C_None, C_None, C_None)),
  241. {JBE} (NCh: 0; Ch: (C_None, C_None, C_None)),
  242. {SETA} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  243. {SETAE} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  244. {SETB} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  245. {SETBE} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  246. {AAA} (NCh: 2; Ch: (C_EAX, C_Flags, C_None)),
  247. {AAD} (NCh: 2; Ch: (C_EAX, C_Flags, C_None)),
  248. {AAM} (NCh: 2; Ch: (C_EAX, C_Flags, C_None)),
  249. {AAS} (NCh: 2; Ch: (C_EAX, C_Flags, C_None)),
  250. {CBW} (NCh: 1; Ch: (C_EAX, C_None, C_None)),
  251. {CDQ} (NCh: 2; Ch: (C_EAX, C_EDX, C_None)),
  252. {CLC} (NCh: 1; Ch: (C_Flags, C_None, C_None)),
  253. {CLI} (NCh: 1; Ch: (C_Flags, C_None, C_None)),
  254. {CLTS} (NCh: 0; Ch: (C_None, C_None, C_None)),
  255. {CMC} (NCh: 1; Ch: (C_Flags, C_None, C_None)),
  256. {CWD} (NCh: 2; Ch: (C_EAX, C_EDX, C_None)),
  257. {CWDE} (NCh: 1; Ch: (C_EAX, C_None, C_None)),
  258. {DAA} (NCh: 1; Ch: (C_EAX, C_None, C_None)),
  259. {DAS} (NCh: 1; Ch: (C_EAX, C_None, C_None)),
  260. {HLT} (NCh: 0; Ch: (C_None, C_None, C_None)),
  261. {IRET} (NCh: 255; Ch: (C_None, C_None, C_None)), {don't know value of any register}
  262. {LAHF} (NCh: 1; Ch: (C_EAX, C_None, C_None)),
  263. {LODS} (NCh: 2; Ch: (C_EAX, C_ESI, C_None)),
  264. {LOCK} (NCh: 0; Ch: (C_None, C_None, C_None)),
  265. {NOP} (NCh: 0; Ch: (C_None, C_None, C_None)),
  266. {PUSHA} (NCh: 1; Ch: (C_ESP, C_None, C_None)),
  267. {PUSHF} (NCh: 1; Ch: (C_ESP, C_None, C_None)),
  268. {PUSHFD} (NCh: 1; Ch: (C_ESP, C_None, C_None)),
  269. {STC} (NCh: 1; Ch: (C_Flags, C_None, C_None)),
  270. {STD} (NCh: 1; Ch: (C_SDirFlag, C_None, C_None)),
  271. {STI} (NCh: 1; Ch: (C_Flags, C_None, C_None)),
  272. {STOS} (NCh: 2; Ch: (C_MemEDI, C_EDI, C_None)),
  273. {WAIT} (NCh: 0; Ch: (C_None, C_None, C_None)),
  274. {XLAT} (NCh: 1; Ch: (C_EAX, C_None, C_None)),
  275. {XLATB} (NCh: 1; Ch: (C_EAX, C_None, C_None)),
  276. {MOVSB} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  277. {MOVSBL} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  278. {MOVSBW} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  279. {MOVSWL} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  280. {MOVZB} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  281. {MOVZWL} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  282. {POPA} (NCh: 255; Ch: (C_None, C_None, C_None)), {don't know value of any register}
  283. {IN} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  284. {OUT} (NCh: 0; Ch: (C_None, C_None, C_None)),
  285. {LDS} (NCh: 2; Ch: (C_Op2, C_None, C_None)),
  286. {LCS} (NCh: 2; Ch: (C_Op2, C_None, C_None)),
  287. {LES} (NCh: 2; Ch: (C_Op2, C_None, C_None)),
  288. {LFS} (NCh: 2; Ch: (C_Op2, C_None, C_None)),
  289. {LGS} (NCh: 2; Ch: (C_Op2, C_None, C_None)),
  290. {LSS} (NCh: 2; Ch: (C_Op2, C_None, C_None)),
  291. {POPF} (NCh: 2; Ch: (C_Flags, C_ESP, C_None)),
  292. {SBB} (NCh: 2; Ch: (C_Op2, C_Flags, C_None)),
  293. {ADC} (NCh: 2; Ch: (C_Op2, C_Flags, C_None)),
  294. {DIV} (NCh: 3; Ch: (C_EAX, C_EDX, C_Flags)),
  295. {ROR} (NCh: 2; Ch: (C_Op2, C_Flags, C_None)),
  296. {ROL} (NCh: 2; Ch: (C_Op2, C_Flags, C_None)),
  297. {RCL} (NCh: 2; Ch: (C_Op2, C_Flags, C_None)),
  298. {RCR} (NCh: 2; Ch: (C_Op2, C_Flags, C_None)),
  299. {SAL} (NCh: 2; Ch: (C_Op2, C_Flags, C_None)),
  300. {SHLD} (NCh: 2; Ch: (C_Op3, C_Flags, C_None)),
  301. {SHRD} (NCh: 2; Ch: (C_Op3, C_Flags, C_None)),
  302. {LCALL} (NCh: 255; Ch: (C_None, C_None, C_None)), {don't know value of any register}
  303. {LJMP} (NCh: 255; Ch: (C_None, C_None, C_None)), {don't know value of any register}
  304. {LRET} (NCh: 255; Ch: (C_None, C_None, C_None)), {don't know value of any register}
  305. {JNAE} (NCh: 0; Ch: (C_None, C_None, C_None)),
  306. {JNB} (NCh: 0; Ch: (C_None, C_None, C_None)),
  307. {JNA} (NCh: 0; Ch: (C_None, C_None, C_None)),
  308. {JNBE} (NCh: 0; Ch: (C_None, C_None, C_None)),
  309. {JP} (NCh: 0; Ch: (C_None, C_None, C_None)),
  310. {JNP} (NCh: 0; Ch: (C_None, C_None, C_None)),
  311. {JPE} (NCh: 0; Ch: (C_None, C_None, C_None)),
  312. {JPO} (NCh: 0; Ch: (C_None, C_None, C_None)),
  313. {JNGE} (NCh: 0; Ch: (C_None, C_None, C_None)),
  314. {JNG} (NCh: 0; Ch: (C_None, C_None, C_None)),
  315. {JNL} (NCh: 0; Ch: (C_None, C_None, C_None)),
  316. {JNLE} (NCh: 0; Ch: (C_None, C_None, C_None)),
  317. {JCXZ} (NCh: 0; Ch: (C_None, C_None, C_None)),
  318. {JECXZ} (NCh: 0; Ch: (C_None, C_None, C_None)),
  319. {LOOP} (NCh: 1; Ch: (C_ECX, C_None, C_None)),
  320. {CMPS} (NCh: 3; Ch: (C_ESI, C_EDI, C_Flags)),
  321. {INS} (NCh: 1; Ch: (C_EDI, C_None, C_None)),
  322. {OUTS} (NCh: 1; Ch: (C_ESI, C_None, C_None)),
  323. {SCAS} (NCh: 2; Ch: (C_EDI, C_Flags, C_None)),
  324. {BSF} (NCh: 2; Ch: (C_Op2, C_Flags, C_None)),
  325. {BSR} (NCh: 2; Ch: (C_Op2, C_Flags, C_None)),
  326. {BT} (NCh: 1; Ch: (C_Flags, C_None, C_None)),
  327. {BTC} (NCh: 2; Ch: (C_Op2, C_Flags, C_None)),
  328. {BTR} (NCh: 2; Ch: (C_Op2, C_Flags, C_None)),
  329. {BTS} (NCh: 2; Ch: (C_Op2, C_Flags, C_None)),
  330. {INT} (NCh: 255; Ch: (C_None, C_None, C_None)), {don't know value of any register}
  331. {INT3} (NCh: 0; Ch: (C_None, C_None, C_None)),
  332. {INTO} (NCh: 255; Ch: (C_None, C_None, C_None)), {don't know value of any register}
  333. {BOUNDL} (NCh: 0; Ch: (C_None, C_None, C_None)),
  334. {BOUNDW} (NCh: 0; Ch: (C_None, C_None, C_None)),
  335. {LOOPZ} (NCh: 1; Ch: (C_ECX, C_None, C_None)),
  336. {LOOPE} (NCh: 1; Ch: (C_ECX, C_None, C_None)),
  337. {LOOPNZ} (NCh: 1; Ch: (C_ECX, C_None, C_None)),
  338. {LOOPNE} (NCh: 1; Ch: (C_ECX, C_None, C_None)),
  339. {SETO} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  340. {SETNO} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  341. {SETNAE} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  342. {SETNB} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  343. {SETZ} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  344. {SETNZ} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  345. {SETNA} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  346. {SETNBE} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  347. {SETS} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  348. {SETNS} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  349. {SETP} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  350. {SETPE} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  351. {SETNP} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  352. {SETPO} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  353. {SETNGE} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  354. {SETNL} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  355. {SETNG} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  356. {SETNLE} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  357. {ARPL} (NCh: 1; Ch: (C_Flags, C_None, C_None)),
  358. {LAR} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  359. {LGDT} (NCh: 0; Ch: (C_None, C_None, C_None)),
  360. {LIDT} (NCh: 0; Ch: (C_None, C_None, C_None)),
  361. {LLDT} (NCh: 0; Ch: (C_None, C_None, C_None)),
  362. {LMSW} (NCh: 0; Ch: (C_None, C_None, C_None)),
  363. {LSL} (NCh: 2; Ch: (C_Op2, C_Flags, C_None)),
  364. {LTR} (NCh: 0; Ch: (C_None, C_None, C_None)),
  365. {SGDT} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  366. {SIDT} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  367. {SLDT} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  368. {SMSW} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  369. {STR} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  370. {VERR} (NCh: 1; Ch: (C_Flags, C_None, C_None)),
  371. {VERW} (NCh: 1; Ch: (C_Flags, C_None, C_None)),
  372. {FABS} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  373. {FBLD} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  374. {FBSTP} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  375. {FCLEX} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  376. {FNCLEX} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  377. {FCOS} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  378. {FDECSTP}(NCh: 1; Ch: (C_FPU, C_None, C_None)),
  379. {FDISI} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  380. {FNDISI} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  381. {FDIVR} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  382. {FENI} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  383. {FNENI} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  384. {FFREE} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  385. {FIADD} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  386. {FICOM} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  387. {FICOMP} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  388. {FIDIVR} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  389. {FIMUL} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  390. {FINCSTP}(NCh: 1; Ch: (C_FPU, C_None, C_None)),
  391. {FINIT} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  392. {FNINIT} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  393. {FIST} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  394. {FISTP} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  395. {FISUB} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  396. {FSUBR} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  397. {FLDCW} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  398. {FLDENV} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  399. {FLDLG2} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  400. {FLDLN2} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  401. {FLDL2E} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  402. {FLDL2T} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  403. {FLDPI} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  404. {FLDS} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  405. {FLDZ} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  406. {FNOP} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  407. {FPATAN} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  408. {FPREM} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  409. {FPREM1} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  410. {FPTAN} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  411. {FRNDINT}(NCh: 1; Ch: (C_FPU, C_None, C_None)),
  412. {FRSTOR} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  413. {FSAVE} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  414. {FNSAVE} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  415. {FSCALE} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  416. {FSETPM} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  417. {FSIN} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  418. {FSINCOS}(NCh: 1; Ch: (C_FPU, C_None, C_None)),
  419. {FSQRT} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  420. {FST} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  421. {FSTCW} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  422. {FNSTCW} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  423. {FSTENV} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  424. {FNSTENV}(NCh: 1; Ch: (C_Op1, C_None, C_None)),
  425. {FSTSW} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  426. {FNSTSW} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  427. {FTST} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  428. {FUCOM} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  429. {FUCOMP} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  430. {FUCOMPP}(NCh: 1; Ch: (C_FPU, C_None, C_None)),
  431. {FWAIT} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  432. {FXAM} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  433. {FXTRACT}(NCh: 1; Ch: (C_FPU, C_None, C_None)),
  434. {FYL2X} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  435. {FYL2XP1}(NCh: 1; Ch: (C_FPU, C_None, C_None)),
  436. {F2XM1} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  437. {FILDQ} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  438. {FILDS} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  439. {FILDL} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  440. {FLDL} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  441. {FLDT} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  442. {FISTQ} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  443. {FISTS} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  444. {FISTL} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  445. {FSTL} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  446. {FSTS} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  447. {FSTPS} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  448. {FISTPL} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  449. {FSTPL} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  450. {FISTPS} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  451. {FISTPQ} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  452. {FSTPT} (NCh: 1; Ch: (C_Op1, C_None, C_None)),
  453. {FCOMPS} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  454. {FICOMPL}(NCh: 1; Ch: (C_FPU, C_None, C_None)),
  455. {FCOMPL} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  456. {FICOMPS}(NCh: 1; Ch: (C_FPU, C_None, C_None)),
  457. {FCOMS} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  458. {FICOML} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  459. {FCOML} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  460. {FICOMS} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  461. {FIADDL} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  462. {FADDL} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  463. {FIADDS} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  464. {FISUBL} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  465. {FSUBL} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  466. {FISUBS} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  467. {FSUBS} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  468. {FSUBR} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  469. {FSUBRS} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  470. {FISUBRL}(NCh: 1; Ch: (C_FPU, C_None, C_None)),
  471. {FSUBRL} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  472. {FISUBRS}(NCh: 1; Ch: (C_FPU, C_None, C_None)),
  473. {FMULS} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  474. {FIMUL} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  475. {FMULL} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  476. {FIMULS} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  477. {FIDIVS} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  478. {FIDIVL} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  479. {FDIVL} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  480. {FIDIVS} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  481. {FDIVRS} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  482. {FIDIVRL}(NCh: 1; Ch: (C_FPU, C_None, C_None)),
  483. {FDIVRL} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  484. {FIDIVRS}(NCh: 1; Ch: (C_FPU, C_None, C_None)),
  485. {REPE} (NCh: 0; Ch: (C_ECX, C_None, C_None)),
  486. {REPNE} (NCh: 0; Ch: (C_ECX, C_None, C_None)),
  487. {FADDS} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  488. {POPFD} (NCh: 2; Ch: (C_ESP, C_Flags, C_None)),
  489. {below are the MMX instructions}
  490. {A_EMMS} (NCh: 1; Ch: (C_FPU, C_None, C_None)),
  491. {A_MOVD} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  492. {A_MOVQ} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  493. {A_PACKSSDW} (NCh: 255; Ch: (C_FPU, C_None, C_None)),
  494. {A_PACKSSWB} (NCh: 255; Ch: (C_FPU, C_None, C_None)),
  495. {A_PACKUSWB} (NCh: 255; Ch: (C_FPU, C_None, C_None)),
  496. {A_PADDB} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  497. {A_PADDD} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  498. {A_PADDSB} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  499. {A_PADDSW} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  500. {A_PADDUSB} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  501. {A_PADDUSW} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  502. {A_PADDW} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  503. {A_PAND} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  504. {A_PANDN} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  505. {A_PCMPEQB} (NCh: 255; Ch: (C_FPU, C_None, C_None)),
  506. {A_PCMPEQD} (NCh: 255; Ch: (C_FPU, C_None, C_None)),
  507. {A_PCMPEQW} (NCh: 255; Ch: (C_FPU, C_None, C_None)),
  508. {A_PCMPGTB} (NCh: 255; Ch: (C_FPU, C_None, C_None)),
  509. {A_PCMPGTD} (NCh: 255; Ch: (C_FPU, C_None, C_None)),
  510. {A_PCMPGTW} (NCh: 255; Ch: (C_FPU, C_None, C_None)),
  511. {A_PMADDWD} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  512. {A_PMULHW} (NCh: 255; Ch: (C_FPU, C_None, C_None)),
  513. {A_PMULLW} (NCh: 255; Ch: (C_FPU, C_None, C_None)),
  514. {A_POR} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  515. {A_PSLLD} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  516. {A_PSLLQ} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  517. {A_PSLLW} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  518. {A_PSRAD} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  519. {A_PSRAW} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  520. {A_PSRLD} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  521. {A_PSRLQ} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  522. {A_PSRLW} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  523. {A_PSUBB} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  524. {A_PSUBD} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  525. {A_PSUBSB} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  526. {A_PSUBSW} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  527. {A_PSUBUSB} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  528. {A_PSUBUSW} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  529. {A_PSUBW} (NCh: 1; Ch: (C_Op2, C_None, C_None)),
  530. {A_PUNPCKHBW} (NCh: 255; Ch: (C_FPU, C_None, C_None)),
  531. {A_PUNPCKHDQ} (NCh: 255; Ch: (C_FPU, C_None, C_None)),
  532. {A_PUNPCKHWD} (NCh: 255; Ch: (C_FPU, C_None, C_None)),
  533. {A_PUNPCKLBW} (NCh: 255; Ch: (C_FPU, C_None, C_None)),
  534. {A_PUNPCKLDQ} (NCh: 255; Ch: (C_FPU, C_None, C_None)),
  535. {A_PUNPCKLWD} (NCh: 255; Ch: (C_FPU, C_None, C_None)),
  536. {A_PXOR} (NCh: 1; Ch: (C_Op2, C_None, C_None)));
  537. Var
  538. {How many instructions are betwen the current instruction and the last one
  539. that modified the register}
  540. NrOfInstrSinceLastMod: TInstrSinceLastMod;
  541. {************************ Create the Label table ************************}
  542. Procedure FindLoHiLabels(AsmL: PAasmOutput; Var LowLabel, HighLabel, LabelDif: Longint);
  543. {Walks through the paasmlist to find the lowest and highest label number;
  544. Since 0.9.3: also removes unused labels}
  545. Var LabelFound: Boolean;
  546. P, hp1: Pai;
  547. Begin
  548. LabelFound := False;
  549. LowLabel := MaxLongint;
  550. HighLabel := 0;
  551. P := Pai(AsmL^.first);
  552. While Assigned(p) Do
  553. Begin
  554. If (Pai(p)^.typ = ait_label) Then
  555. If (Pai_Label(p)^.l^.is_used)
  556. Then
  557. Begin
  558. LabelFound := True;
  559. If (Pai_Label(p)^.l^.nb < LowLabel) Then
  560. LowLabel := Pai_Label(p)^.l^.nb;
  561. If (Pai_Label(p)^.l^.nb > HighLabel) Then
  562. HighLabel := Pai_Label(p)^.l^.nb;
  563. End
  564. { Else
  565. Begin
  566. hp1 := pai(p^.next);
  567. AsmL^.Remove(p);
  568. Dispose(p, Done);
  569. p := hp1;
  570. continue;
  571. End};
  572. p := pai(p^.next);
  573. End;
  574. If LabelFound
  575. Then LabelDif := HighLabel+1-LowLabel
  576. Else LabelDif := 0;
  577. End;
  578. Procedure BuildLabelTable(AsmL: PAasmOutput; Var LabelTable: PLabelTable; LowLabel: Longint; Var LabelDif: Longint);
  579. {Builds a table with the locations of the labels in the paasmoutput}
  580. Var p: Pai;
  581. Begin
  582. If (LabelDif <> 0) Then
  583. Begin
  584. {$IfDef TP}
  585. If (MaxAvail >= LabelDif*SizeOf(Pai))
  586. Then
  587. Begin
  588. {$EndIf TP}
  589. GetMem(LabelTable, LabelDif*SizeOf(TLabelTableItem));
  590. FillChar(LabelTable^, LabelDif*SizeOf(TLabelTableItem), 0);
  591. p := pai(AsmL^.first);
  592. While Assigned(p) Do
  593. Begin
  594. If (Pai(p)^.typ = ait_label) And
  595. (Pai_Label(p)^.l^.is_used) Then
  596. LabelTable^[Pai_Label(p)^.l^.nb-LowLabel].PaiObj := p;
  597. p := pai(p^.next);
  598. End;
  599. {$IfDef TP}
  600. End
  601. Else LabelDif := 0;
  602. {$EndIf TP}
  603. End;
  604. End;
  605. {************************ Search the Label table ************************}
  606. Function FindLabel(L: PLabel; Var hp: Pai): Boolean;
  607. {searches for the specified label starting from hp as long as the
  608. encountered instructions are labels, to be able to optimize constructs like
  609. jne l2 jmp l2
  610. jmp l3 and l1:
  611. l1: l2:
  612. l2:}
  613. Var TempP: Pai;
  614. Begin
  615. TempP := hp;
  616. While Assigned(TempP) and
  617. (TempP^.typ In SkipInstr + [ait_label]) Do
  618. If (TempP^.typ <> ait_Label) Or
  619. (pai_label(TempP)^.l <> L)
  620. Then TempP := Pai(TempP^.next)
  621. Else
  622. Begin
  623. hp := TempP;
  624. FindLabel := True;
  625. exit
  626. End;
  627. FindLabel := False;
  628. End;
  629. {************************ Some general functions ************************}
  630. Function Reg32(Reg: TRegister): TRegister;
  631. {Returns the 32 bit component of Reg if it exists, otherwise Reg is returned}
  632. Begin
  633. Reg32 := Reg;
  634. If (Reg >= R_AX)
  635. Then
  636. If (Reg <= R_DI)
  637. Then Reg32 := Reg16ToReg32(Reg)
  638. Else
  639. If (Reg <= R_BL)
  640. Then Reg32 := Reg8toReg32(Reg);
  641. End;
  642. Function PowerOf2(L: Longint): Longint;
  643. Var Counter, TempVal: Longint;
  644. Begin
  645. TempVal := 1;
  646. For Counter := 1 to L Do
  647. TempVal := TempVal * 2;
  648. PowerOf2 := TempVal;
  649. End;
  650. { inserts new_one between prev and foll }
  651. Procedure InsertLLItem(AsmL: PAasmOutput; prev, foll, new_one: PLinkedList_Item);
  652. Begin
  653. If Assigned(prev) Then
  654. If Assigned(foll) Then
  655. Begin
  656. If Assigned(new_one) Then
  657. Begin
  658. new_one^.previous := prev;
  659. new_one^.next := foll;
  660. prev^.next := new_one;
  661. foll^.previous := new_one;
  662. End;
  663. End
  664. Else AsmL^.Concat(new_one)
  665. Else If Assigned(Foll) Then AsmL^.Insert(new_one)
  666. End;
  667. {********************* Compare parts of Pai objects *********************}
  668. Function RefsEqual(Const R1, R2: TReference): Boolean;
  669. Begin
  670. If R1.IsIntValue
  671. Then RefsEqual := R2.IsIntValue and (R1.Offset = R2.Offset)
  672. Else If (R1.Offset = R2.Offset) And (R1.Base = R2.Base) And
  673. (R1.Index = R2.Index) And (R1.Segment = R2.Segment) And
  674. (R1.ScaleFactor = R2.ScaleFactor)
  675. Then
  676. Begin
  677. If Assigned(R1.Symbol)
  678. Then RefsEqual := Assigned(R2.Symbol) And (R1.Symbol^=R2.Symbol^)
  679. Else RefsEqual := Not(Assigned(R2.Symbol));
  680. End
  681. Else RefsEqual := False;
  682. End;
  683. Function IsGP32Reg(Reg: TRegister): Boolean;
  684. {Checks if the register is a 32 bit general purpose register}
  685. Begin
  686. If (Reg >= R_EAX) and (Reg <= R_EBX)
  687. Then IsGP32Reg := True
  688. Else IsGP32reg := False
  689. End;
  690. Function RegInRef(Reg: TRegister; Const Ref: TReference): Boolean;
  691. Begin {checks whether Ref contains a reference to Reg}
  692. Reg := Reg32(Reg);
  693. RegInRef := (Ref.Base = Reg) Or (Ref.Index = Reg)
  694. End;
  695. Function RegInInstruction(Reg: TRegister; p1: Pai): Boolean;
  696. {checks if Reg is used by the instruction p1}
  697. Var TmpResult: Boolean;
  698. Begin
  699. TmpResult := False;
  700. If (Pai(p1)^.typ = ait_instruction) Then
  701. Begin
  702. Case Pai386(p1)^.op1t Of
  703. Top_Reg: TmpResult := Reg = TRegister(Pai386(p1)^.op1);
  704. Top_Ref: TmpResult := RegInRef(Reg, TReference(Pai386(p1)^.op1^))
  705. End;
  706. If Not(TmpResult) Then
  707. Case Pai386(p1)^.op2t Of
  708. Top_Reg:
  709. if Pai386(p1)^.op3t<>Top_reg
  710. then TmpResult := Reg = TRegister(Pai386(p1)^.op2)
  711. else TmpResult := longint(Reg) = twowords(Pai386(p1)^.op2).word1;
  712. Top_Ref: TmpResult := RegInRef(Reg, TReference(Pai386(p1)^.op2^))
  713. End;
  714. If Not(TmpResult) Then
  715. Case Pai386(p1)^.op3t Of
  716. Top_Reg: TmpResult := longint(Reg) =twowords(Pai386(p1)^.op2).word2;
  717. Top_none:;
  718. else
  719. internalerror($Da);
  720. End
  721. End;
  722. RegInInstruction := TmpResult
  723. End;
  724. {********************* GetNext and GetLastInstruction *********************}
  725. Function GetNextInstruction(Current: Pai; Var Next: Pai): Boolean;
  726. {skips ait_regalloc, ait_regdealloc and ait_stab* objects and puts the
  727. next pai object in Next. Returns false if there isn't any}
  728. Begin
  729. GetNextInstruction := False;
  730. Current := Pai(Current^.Next);
  731. While Assigned(Current) And
  732. ((Pai(Current)^.typ In SkipInstr) or
  733. ((Pai(Current)^.typ = ait_label) And
  734. Not(Pai_Label(Current)^.l^.is_used))) Do
  735. Current := Pai(Current^.Next);
  736. If Assigned(Current)
  737. Then
  738. Begin
  739. Next := Current;
  740. GetNextInstruction := True;
  741. End;
  742. End;
  743. Function GetLastInstruction(Current: Pai; Var Last: Pai): Boolean;
  744. {skips the ait-types in SkipInstr puts the previous pai object in
  745. Last. Returns false if there isn't any}
  746. Begin
  747. GetLastInstruction := False;
  748. Current := Pai(Current^.previous);
  749. While Assigned(Current) And
  750. ((Pai(Current)^.typ In SkipInstr) or
  751. ((Pai(Current)^.typ = ait_label) And
  752. Not(Pai_Label(Current)^.l^.is_used))) Do
  753. Current := Pai(Current^.previous);
  754. If Assigned(Current)
  755. Then
  756. Begin
  757. Last := Current;
  758. GetLastInstruction := True;
  759. End;
  760. End;
  761. {******************* The Data Flow Analyzer functions ********************}
  762. (*Function FindZeroreg(p: Pai; Var Result: TRegister): Boolean;
  763. {Finds a register which contains the constant zero}
  764. Var Counter: TRegister;
  765. Begin
  766. Counter := R_EAX;
  767. FindZeroReg := True;
  768. While (Counter <= R_EDI) And
  769. ((PPaiProp(p^.fileinfo.line)^.Regs[Counter].Typ <> Con_Const) or
  770. (PPaiProp(p^.fileinfo.line)^.Regs[Counter].StartMod <> Pointer(0))) Do
  771. Inc(Byte(Counter));
  772. If (PPaiProp(p^.fileinfo.line)^.Regs[Counter].Typ = Con_Const) And
  773. (PPaiProp(p^.fileinfo.line)^.Regs[Counter].StartMod = Pointer(0))
  774. Then Result := Counter
  775. Else FindZeroReg := False;
  776. End;*)
  777. Function TCh2Reg(Ch: TChange): TRegister;
  778. {converts a TChange variable to a TRegister}
  779. Begin
  780. If (CH <= C_EDI)
  781. Then TCh2Reg := TRegister(Byte(Ch))
  782. Else InternalError($db)
  783. End;
  784. Procedure IncState(Var S: Word);
  785. {Increases the state by 1, wraps around at $ffff to 0 (so we won't get
  786. overflow errors}
  787. Begin
  788. If (s <> $ffff)
  789. Then Inc(s)
  790. Else s := 0
  791. End;
  792. Procedure DestroyReg(p1: PPaiProp; Reg: TRegister);
  793. {Destroys the contents of the register Reg in the PPaiProp of P}
  794. Var TmpState: Longint;
  795. Begin
  796. Reg := Reg32(Reg);
  797. NrOfInstrSinceLastMod[Reg] := 0;
  798. If (Reg >= R_EAX) And (Reg <= R_EDI)
  799. Then
  800. With p1^.Regs[Reg] Do
  801. Begin
  802. IncState(State);
  803. TmpState := State;
  804. FillChar(p1^.Regs[Reg], SizeOf(TContent), 0);
  805. State := TmpState;
  806. End;
  807. End;
  808. Function OpsEqual(typ: Longint; op1, op2: Pointer): Boolean;
  809. Begin {checks whether the two ops are equal}
  810. Case typ Of
  811. Top_Reg, Top_Const: OpsEqual := op1 = op2;
  812. Top_Ref: OpsEqual := RefsEqual(TReference(op1^), TReference(op2^));
  813. Top_None: OpsEqual := True
  814. Else OpsEqual := False
  815. End;
  816. End;
  817. Function RegsSameContent(p1, p2: Pai; Reg: TRegister): Boolean;
  818. {checks whether Reg has the same content in the PPaiProp of p1 and p2}
  819. Begin
  820. Reg := Reg32(Reg);
  821. RegsSameContent :=
  822. PPaiProp(p1^.fileinfo.line)^.Regs[Reg].State =
  823. PPaiProp(p2^.fileinfo.line)^.Regs[Reg].State;
  824. End;
  825. Function InstructionsEqual(p1, p2: Pai): Boolean;
  826. Begin {checks whether two Pai386 instructions are equal}
  827. InstructionsEqual :=
  828. Assigned(p1) And Assigned(p2) And
  829. {$ifdef regalloc}
  830. ((((Pai(p1)^.typ = ait_regalloc) And
  831. (Pai(p2)^.typ = ait_regalloc)) Or
  832. ((Pai(p1)^.typ = ait_regdealloc) And
  833. (Pai(p2)^.typ = ait_regdealloc))) And
  834. (PaiRegAlloc(p1)^.reg = PaiRegAlloc(p2)^.reg)) Or
  835. {$endif regalloc}
  836. ((Pai(p1)^.typ = ait_instruction) And
  837. (Pai(p1)^.typ = ait_instruction) And
  838. (Pai386(p1)^._operator = Pai386(p2)^._operator) And
  839. (Pai386(p1)^.op1t = Pai386(p2)^.op1t) And
  840. (Pai386(p1)^.op2t = Pai386(p2)^.op2t) And
  841. OpsEqual(Pai386(p1)^.op1t, Pai386(p1)^.op1, Pai386(p2)^.op1) And
  842. OpsEqual(Pai386(p1)^.op2t, Pai386(p1)^.op2, Pai386(p2)^.op2))
  843. End;
  844. Procedure DestroyRefs(p: pai; Const Ref: TReference; WhichReg: TRegister);
  845. {destroys all registers which possibly contain a reference to Ref, WhichReg
  846. is the register whose contents are being written to memory (if this proc
  847. is called because of a "mov?? %reg, (mem)" instruction)}
  848. Var Counter: TRegister;
  849. Begin
  850. WhichReg := Reg32(WhichReg);
  851. If ((Ref.base = ProcInfo.FramePointer) And
  852. (Ref.Index = R_NO)) Or
  853. Assigned(Ref.Symbol)
  854. Then
  855. {write something to a parameter, a local or global variable, so
  856. * with uncertzain optimizations on:
  857. - destroy the contents of registers <> WhichReg whose StartMod is of
  858. the form "mov?? (Ref), %reg". WhichReg is destroyed if it's StartMod
  859. is of that form and NrOfMods > 1 (so if it is a pointer based on Ref)
  860. * with uncertzain optimizations off:
  861. - also destroy registers that contain any pointer}
  862. For Counter := R_EAX to R_EDI Do
  863. With PPaiProp(p^.fileinfo.line)^.Regs[Counter] Do
  864. Begin
  865. If (typ = Con_Ref) And
  866. {StarMod is always of the type ait_instruction}
  867. (Pai386(StartMod)^.op1t = top_ref) And
  868. ((RefsEqual(TReference(Pai386(StartMod)^.op1^), Ref) And
  869. ((Counter <> WhichReg) Or (NrOfMods <> 1))) Or
  870. (Not(cs_UncertainOpts in aktglobalswitches) And
  871. (NrOfMods <> 1)))
  872. Then DestroyReg(PPaiProp(p^.fileinfo.line), Counter)
  873. End
  874. Else
  875. {write something to a pointer location, so
  876. * with uncertain optimzations on:
  877. - do not destroy registers which contain a local/global variable or a
  878. parameter, except if DestroyRefs is called because of a "movsl"
  879. * with uncertain optimzations off:
  880. - destroy every register which contains a memory location
  881. }
  882. For Counter := R_EAX to R_EDI Do
  883. With PPaiProp(p^.fileinfo.line)^.Regs[Counter] Do
  884. If (typ = Con_Ref) And
  885. (Not(cs_UncertainOpts in aktglobalswitches) Or
  886. {for movsl}
  887. (Ref.Base = R_EDI) Or
  888. {don't destroy if reg contains a parameter, local or global variable}
  889. Not((NrOfMods = 1) And
  890. (Pai386(StartMod)^.op1t = top_ref) And
  891. ((PReference(Pai386(StartMod)^.op1)^.base = ProcInfo.FramePointer) Or
  892. Assigned(PReference(Pai386(StartMod)^.op1)^.Symbol)
  893. )
  894. )
  895. )
  896. Then DestroyReg(PPaiProp(p^.FileInfo.Line), Counter)
  897. End;
  898. Procedure DestroyAllRegs(p: PPaiProp);
  899. Var Counter: TRegister;
  900. Begin {initializes/desrtoys all registers}
  901. For Counter := R_EAX To R_EDI Do
  902. DestroyReg(p, Counter);
  903. p^.DirFlag := F_Unknown;
  904. End;
  905. Procedure Destroy(PaiObj: Pai; opt: Longint; Op: Pointer);
  906. Begin
  907. Case opt Of
  908. top_reg: DestroyReg(PPaiProp(PaiObj^.fileinfo.line), TRegister(Op));
  909. top_ref: DestroyRefs(PaiObj, TReference(Op^), R_NO);
  910. top_symbol:;
  911. End;
  912. End;
  913. Procedure DFAPass1(AsmL: PAasmOutput);
  914. {gathers the RegAlloc data... still need to think about where to store it}
  915. Begin
  916. FindLoHiLabels(AsmL, LoLab, HiLab, LabDif);
  917. BuildLabelTable(AsmL, LTable, LoLab, LabDif);
  918. End;
  919. Function DoDFAPass2(First: Pai): Pai;
  920. {Analyzes the Data Flow of an assembler list. Starts creating the reg
  921. contents for the instructions starting with p. Returns the last pai which has
  922. been processed}
  923. Var
  924. CurProp: PPaiProp;
  925. {$ifdef AnalyzeLoops}
  926. TmpState,
  927. {$endif AnalyzeLoops}
  928. Cnt, InstrCnt : Longint;
  929. InstrProp: TAsmInstrucProp;
  930. p : Pai;
  931. {$Ifdef JumpAnal}
  932. hp : pai;
  933. {$endif}
  934. TmpRef: TReference;
  935. TmpReg: TRegister;
  936. Begin
  937. p := First;
  938. InstrCnt := 1;
  939. FillChar(NrOfInstrSinceLastMod, SizeOf(NrOfInstrSinceLastMod), 0);
  940. While Assigned(p) Do
  941. Begin
  942. DoDFAPass2 := p;
  943. {$IfDef TP}
  944. If (InstrCnt <= NrOfPaiFast) Then
  945. {$EndIf TP}
  946. CurProp := @PaiPropBlock^[InstrCnt]
  947. {$IfDef TP}
  948. Else New(CurProp)
  949. {$EndIf TP}
  950. ;
  951. If (p <> First)
  952. Then
  953. {$ifdef JumpAnal}
  954. Begin
  955. If (p^.Typ <> ait_label) Then
  956. {$endif JumpAnal}
  957. Begin
  958. CurProp^.Regs := PPaiProp(Pai(p^.previous)^.fileinfo.line)^.Regs;
  959. CurProp^.DirFlag := PPaiProp(Pai(p^.previous)^.fileinfo.line)^.DirFlag
  960. End
  961. {$ifdef JumpAnal}
  962. End
  963. {$endif JumpAnal}
  964. Else
  965. Begin
  966. FillChar(CurProp^, SizeOf(CurProp^), 0);
  967. { For TmpReg := R_EAX to R_EDI Do
  968. CurProp^.Regs[TmpReg].State := 1;}
  969. End;
  970. CurProp^.CanBeRemoved := False;
  971. {$ifdef TP}
  972. CurProp^.linesave := p^.fileinfo.line;
  973. PPaiProp(p^.fileinfo.line) := CurProp;
  974. {$Endif TP}
  975. For TmpReg := R_EAX To R_EDI Do
  976. Inc(NrOfInstrSinceLastMod[TmpReg]);
  977. Case p^.typ Of
  978. ait_label:
  979. {$Ifndef JumpAnal}
  980. If (Pai_label(p)^.l^.is_used) Then
  981. DestroyAllRegs(CurProp);
  982. {$Else JumpAnal}
  983. Begin
  984. If (Pai_Label(p)^.is_used) Then
  985. With LTable^[Pai_Label(p)^.l^.nb-LoLab] Do
  986. {$IfDef AnalyzeLoops}
  987. If (RefsFound = Pai_Label(p)^.l^.RefCount)
  988. {$Else AnalyzeLoops}
  989. If (JmpsProcessed = Pai_Label(p)^.l^.RefCount)
  990. {$EndIf AnalyzeLoops}
  991. Then
  992. {all jumps to this label have been found}
  993. {$IfDef AnalyzeLoops}
  994. If (JmpsProcessed > 0)
  995. Then
  996. {$EndIf AnalyzeLoops}
  997. {we've processed at least one jump to this label}
  998. Begin
  999. If Not(GetLastInstruction(p, hp) And
  1000. (hp^.typ = ait_labeled_instruction) And
  1001. (Pai_Labeled(hp)^._operator = A_JMP))
  1002. Then
  1003. {previous instruction not a JMP -> the contents of the registers after the
  1004. previous intruction has been executed have to be taken into account as well}
  1005. For TmpReg := R_EAX to R_EDI Do
  1006. Begin
  1007. If (CurProp^.Regs[TmpReg].State <>
  1008. PPaiProp(Pai(p^.Previous)^.FileInfo.Line)^.Regs[TmpReg].State)
  1009. Then DestroyReg(CurProp, TmpReg)
  1010. End
  1011. End
  1012. {$IfDef AnalyzeLoops}
  1013. Else
  1014. {a label from a backward jump (e.g. a loop), no jump to this label has
  1015. already been processed}
  1016. If Not(GetLastInstruction(p, hp) And
  1017. (hp^.typ = ait_labeled_instruction) And
  1018. (Pai_Labeled(hp)^._operator = A_JMP))
  1019. Then
  1020. {previous instruction not a jmp, so keep all the registers' contents from the
  1021. previous instruction}
  1022. Begin
  1023. CurProp^.Regs := PPaiProp(Pai(p^.Previous)^.FileInfo.Line)^.Regs;
  1024. CurProp^.DirFlag := PPaiProp(Pai(p^.Previous)^.FileInfo.Line)^.DirFlag;
  1025. End
  1026. Else
  1027. {previous instruction a jmp and no jump to this label processed yet}
  1028. Begin
  1029. hp := p;
  1030. Cnt := InstrCnt;
  1031. {continue until we find a jump to the label or a label which has already
  1032. been processed}
  1033. While GetNextInstruction(hp, hp) And
  1034. Not((hp^.typ = ait_labeled_instruction) And
  1035. (Pai_Labeled(hp)^.lab^.nb = Pai_Label(p)^.l^.nb)) And
  1036. Not((hp^.typ = ait_label) And
  1037. (LTable^[Pai_Label(hp)^.l^.nb-LoLab].RefsFound
  1038. = Pai_Label(hp)^.l^.RefCount) And
  1039. (LTable^[Pai_Label(hp)^.l^.nb-LoLab].JmpsProcessed > 0)) Do
  1040. Inc(Cnt);
  1041. If (hp^.typ = ait_label)
  1042. Then
  1043. {there's a processed label after the current one}
  1044. Begin
  1045. CurProp^.Regs := PaiPropBlock^[Cnt].Regs;
  1046. CurProp^.DirFlag := PaiPropBlock^[Cnt].DirFlag;
  1047. End
  1048. Else
  1049. {there's no label anymore after the current one, or they haven't been
  1050. processed yet}
  1051. Begin
  1052. CurProp^.Regs := PPaiProp(Pai(p^.Previous)^.FileInfo.Line)^.Regs;
  1053. CurProp^.DirFlag := PPaiProp(Pai(p^.Previous)^.FileInfo.Line)^.DirFlag;
  1054. DestroyAllRegs(PPaiProp(Pai(p^.Previous)^.FileInfo.Line))
  1055. End
  1056. End
  1057. {$EndIf AnalyzeLoops}
  1058. Else
  1059. {not all references to this label have been found, so destroy all registers}
  1060. Begin
  1061. CurProp^.Regs := PPaiProp(Pai(p^.Previous)^.FileInfo.Line)^.Regs;
  1062. CurProp^.DirFlag := PPaiProp(Pai(p^.Previous)^.FileInfo.Line)^.DirFlag;
  1063. DestroyAllRegs(CurProp)
  1064. End;
  1065. End;
  1066. {$EndIf JumpAnal}
  1067. ait_labeled_instruction:
  1068. {$IfNDef JumpAnal}
  1069. ;
  1070. {$Else JumpAnal}
  1071. With LTable^[Pai_Labeled(p)^.lab^.nb-LoLab] Do
  1072. If (RefsFound = Pai_Labeled(p)^.lab^.RefCount) Then
  1073. Begin
  1074. If (InstrCnt < InstrNr)
  1075. Then
  1076. {forward jump}
  1077. If (JmpsProcessed = 0) Then
  1078. {no jump to this label has been processed yet}
  1079. Begin
  1080. PaiPropBlock^[InstrNr].Regs := CurProp^.Regs;
  1081. PaiPropBlock^[InstrNr].DirFlag := CurProp^.DirFlag;
  1082. Inc(JmpsProcessed);
  1083. End
  1084. Else
  1085. Begin
  1086. For TmpReg := R_EAX to R_EDI Do
  1087. If (PaiPropBlock^[InstrNr].Regs[TmpReg].State <>
  1088. CurProp^.Regs[TmpReg].State) Then
  1089. DestroyReg(@PaiPropBlock^[InstrNr], TmpReg);
  1090. Inc(JmpsProcessed);
  1091. End
  1092. {$ifdef AnalyzeLoops}
  1093. Else
  1094. { backward jump, a loop for example}
  1095. { If (JmpsProcessed > 0) Or
  1096. Not(GetLastInstruction(PaiObj, hp) And
  1097. (hp^.typ = ait_labeled_instruction) And
  1098. (Pai_Labeled(hp)^._operator = A_JMP))
  1099. Then}
  1100. {instruction prior to label is not a jmp, or at least one jump to the label
  1101. has yet been processed}
  1102. Begin
  1103. Inc(JmpsProcessed);
  1104. For TmpReg := R_EAX to R_EDI Do
  1105. If (PaiPropBlock^[InstrNr].Regs[TmpReg].State <>
  1106. CurProp^.Regs[TmpReg].State)
  1107. Then
  1108. Begin
  1109. TmpState := PaiPropBlock^[InstrNr].Regs[TmpReg].State;
  1110. Cnt := InstrNr;
  1111. While (TmpState = PaiPropBlock^[Cnt].Regs[TmpReg].State) Do
  1112. Begin
  1113. DestroyReg(@PaiPropBlock^[Cnt], TmpReg);
  1114. Inc(Cnt);
  1115. End;
  1116. While (Cnt <= InstrCnt) Do
  1117. Begin
  1118. Inc(PaiPropBlock^[Cnt].Regs[TmpReg].State);
  1119. Inc(Cnt)
  1120. End
  1121. End;
  1122. End
  1123. { Else }
  1124. {instruction prior to label is a jmp and no jumps to the label have yet been
  1125. processed}
  1126. { Begin
  1127. Inc(JmpsProcessed);
  1128. For TmpReg := R_EAX to R_EDI Do
  1129. Begin
  1130. TmpState := PaiPropBlock^[InstrNr].Regs[TmpReg].State;
  1131. Cnt := InstrNr;
  1132. While (TmpState = PaiPropBlock^[Cnt].Regs[TmpReg].State) Do
  1133. Begin
  1134. PaiPropBlock^[Cnt].Regs[TmpReg] := CurProp^.Regs[TmpReg];
  1135. Inc(Cnt);
  1136. End;
  1137. TmpState := PaiPropBlock^[InstrNr].Regs[TmpReg].State;
  1138. While (TmpState = PaiPropBlock^[Cnt].Regs[TmpReg].State) Do
  1139. Begin
  1140. DestroyReg(@PaiPropBlock^[Cnt], TmpReg);
  1141. Inc(Cnt);
  1142. End;
  1143. While (Cnt <= InstrCnt) Do
  1144. Begin
  1145. Inc(PaiPropBlock^[Cnt].Regs[TmpReg].State);
  1146. Inc(Cnt)
  1147. End
  1148. End
  1149. End}
  1150. {$endif AnalyzeLoops}
  1151. End;
  1152. {$EndIf JumpAnal}
  1153. {$ifdef GDB}
  1154. ait_stabs, ait_stabn, ait_stab_function_name:;
  1155. {$endif GDB}
  1156. {$ifdef regalloc}
  1157. ait_regalloc, ait_regdealloc:;
  1158. {$endif regalloc}
  1159. ait_instruction:
  1160. Begin
  1161. InstrProp := AsmInstr[Pai386(p)^._operator];
  1162. Case Pai386(p)^._operator Of
  1163. A_MOV, A_MOVZX, A_MOVSX:
  1164. Begin
  1165. Case Pai386(p)^.op1t Of
  1166. Top_Reg:
  1167. Case Pai386(p)^.op2t Of
  1168. Top_Reg:
  1169. Begin
  1170. DestroyReg(CurProp, TRegister(Pai386(p)^.op2));
  1171. { CurProp^.Regs[TRegister(Pai386(p)^.op2)] :=
  1172. CurProp^.Regs[TRegister(Pai386(p)^.op1)];
  1173. If (CurProp^.Regs[TRegister(Pai386(p)^.op2)].ModReg = R_NO) Then
  1174. CurProp^.Regs[TRegister(Pai386(p)^.op2)].ModReg :=
  1175. Tregister(Pai386(p)^.op1);}
  1176. End;
  1177. Top_Ref: DestroyRefs(p, TReference(Pai386(p)^.op2^), TRegister(Pai386(p)^.op1));
  1178. End;
  1179. Top_Ref:
  1180. Begin {destination is always a register in this case}
  1181. TmpReg := Reg32(TRegister(Pai386(p)^.op2));
  1182. If (RegInRef(TmpReg, TReference(Pai386(p)^.op1^)))
  1183. Then
  1184. Begin
  1185. With CurProp^.Regs[TmpReg] Do
  1186. Begin
  1187. IncState(State);
  1188. If (typ <> Con_Ref) Then
  1189. Begin
  1190. typ := Con_Ref;
  1191. StartMod := p;
  1192. End;
  1193. {also store how many instructions are part of the sequence in the first
  1194. instructions PPaiProp, so it can be easily accessed from within
  1195. CheckSequence}
  1196. Inc(NrOfMods, NrOfInstrSinceLastMod[TmpReg]);
  1197. PPaiProp(Pai(StartMod)^.fileinfo.line)^.Regs[TmpReg].NrOfMods := NrOfMods;
  1198. NrOfInstrSinceLastMod[TmpReg] := 0;
  1199. End;
  1200. End
  1201. Else
  1202. Begin
  1203. DestroyReg(CurProp, TmpReg);
  1204. With CurProp^.Regs[TmpReg] Do
  1205. Begin
  1206. Typ := Con_Ref;
  1207. StartMod := p;
  1208. NrOfMods := 1;
  1209. End;
  1210. End;
  1211. End;
  1212. Top_Const:
  1213. Begin
  1214. Case Pai386(p)^.op2t Of
  1215. Top_Reg:
  1216. Begin
  1217. TmpReg := Reg32(TRegister(Pai386(p)^.op2));
  1218. With CurProp^.Regs[TmpReg] Do
  1219. Begin
  1220. {it doesn't matter that the state is changed,
  1221. it isn't looked at when removing constant reloads}
  1222. DestroyReg(CurProp, TmpReg);
  1223. typ := Con_Const;
  1224. StartMod := Pai386(p)^.op1;
  1225. End
  1226. End;
  1227. Top_Ref: DestroyRefs(P, TReference(Pai386(p)^.op2^), R_NO);
  1228. End;
  1229. End;
  1230. End;
  1231. End;
  1232. A_IMUL:
  1233. Begin
  1234. If (Pai386(p)^.Op3t = top_none)
  1235. Then
  1236. If (Pai386(p)^.Op2t = top_none)
  1237. Then
  1238. Begin
  1239. DestroyReg(CurProp, R_EAX);
  1240. DestroyReg(CurProp, R_EDX)
  1241. End
  1242. Else
  1243. Begin
  1244. If (Pai386(p)^.Op2t = top_reg) Then
  1245. DestroyReg(CurProp, TRegister(Pai386(p)^.Op2));
  1246. End
  1247. Else If (Pai386(p)^.Op3t = top_reg) Then
  1248. DestroyReg(CurProp, TRegister(longint(twowords(Pai386(p)^.Op2).word2)));
  1249. End;
  1250. A_XOR:
  1251. Begin
  1252. If (Pai386(p)^.op1t = top_reg) And
  1253. (Pai386(p)^.op2t = top_reg) And
  1254. (Pai386(p)^.op1 = Pai386(p)^.op2)
  1255. Then
  1256. Begin
  1257. DestroyReg(CurProp, Tregister(Pai386(p)^.op1));
  1258. CurProp^.Regs[Reg32(Tregister(Pai386(p)^.op1))].typ := Con_Const;
  1259. CurProp^.Regs[Reg32(Tregister(Pai386(p)^.op1))].StartMod := Pointer(0)
  1260. End
  1261. Else Destroy(p, Pai386(p)^.op2t, Pai386(p)^.op2);
  1262. End
  1263. Else
  1264. Begin
  1265. If InstrProp.NCh <> 255
  1266. Then
  1267. For Cnt := 1 To InstrProp.NCh Do
  1268. Case InstrProp.Ch[Cnt] Of
  1269. C_None:;
  1270. C_EAX..C_EDI: DestroyReg(CurProp, TCh2Reg(InstrProp.Ch[Cnt]));
  1271. C_CDirFlag: CurProp^.DirFlag := F_NotSet;
  1272. C_SDirFlag: CurProp^.DirFlag := F_Set;
  1273. C_Op1: Destroy(p, Pai386(p)^.op1t, Pai386(p)^.op1);
  1274. C_Op2: Destroy(p, Pai386(p)^.op2t, Pai386(p)^.op2);
  1275. C_Op3: Destroy(p, Pai386(p)^.op2t, Pointer(Longint(TwoWords(Pai386(p)^.op2).word2)));
  1276. C_MemEDI:
  1277. Begin
  1278. FillChar(TmpRef, SizeOf(TmpRef), 0);
  1279. TmpRef.Base := R_EDI;
  1280. DestroyRefs(p, TmpRef, R_NO)
  1281. End;
  1282. C_Flags, C_FPU:;
  1283. End
  1284. Else
  1285. Begin
  1286. DestroyAllRegs(CurProp);
  1287. End;
  1288. End;
  1289. End;
  1290. End
  1291. Else
  1292. Begin
  1293. DestroyAllRegs(CurProp);
  1294. End;
  1295. End;
  1296. Inc(InstrCnt);
  1297. p := Pai(p^.next);
  1298. End;
  1299. End;
  1300. Function InitDFAPass2(AsmL: PAasmOutput): Boolean;
  1301. {reserves memory for the PPaiProps in one big memory block when not using
  1302. TP, returns False if not enough memory is available for the optimizer in all
  1303. cases}
  1304. Var p: Pai;
  1305. Count: Longint;
  1306. { TmpStr: String; }
  1307. Begin
  1308. P := Pai(AsmL^.First);
  1309. NrOfPaiObjs := 1;
  1310. While (P <> Pai(AsmL^.last)) Do
  1311. Begin
  1312. {$IfNDef TP}
  1313. Case P^.Typ Of
  1314. ait_labeled_instruction:
  1315. begin
  1316. If (Pai_Labeled(P)^.lab^.nb >= LoLab) And
  1317. (Pai_Labeled(P)^.lab^.nb <= HiLab) Then
  1318. Inc(LTable^[Pai_Labeled(P)^.lab^.nb-LoLab].RefsFound);
  1319. end;
  1320. ait_label:
  1321. Begin
  1322. If (Pai_Label(p)^.l^.is_used) Then
  1323. LTable^[Pai_Label(P)^.l^.nb-LoLab].InstrNr := NrOfPaiObjs
  1324. End;
  1325. { ait_instruction:
  1326. Begin
  1327. If (Pai386(p)^._operator = A_PUSH) And
  1328. (Pai386(p)^.op1t = top_symbol) And
  1329. (PCSymbol(Pai386(p)^.op1)^.offset = 0) Then
  1330. Begin
  1331. TmpStr := StrPas(PCSymbol(Pai386(p)^.op1)^.symbol);
  1332. If}
  1333. End;
  1334. {$EndIf TP}
  1335. Inc(NrOfPaiObjs);
  1336. P := Pai(P^.next)
  1337. End;
  1338. {$IfDef TP}
  1339. If (MemAvail < (SizeOf(TPaiProp)*NrOfPaiObjs))
  1340. {this doesn't have to be one contiguous block}
  1341. Then InitDFAPass2 := False
  1342. Else
  1343. Begin
  1344. InitDFAPass2 := True;
  1345. If (MaxAvail < 65520)
  1346. Then NrOfPaiFast := MaxAvail Div (((SizeOf(TPaiProp)+1) div 2)*2)
  1347. Else NrOfPaiFast := 65520 Div (((SizeOf(TPaiProp)+1) div 2)*2);
  1348. If (NrOfPaiFast > 0) Then
  1349. GetMem(PaiPropBlock, NrOfPaiFast*(((SizeOf(TPaiProp)+1) div 2)*2));
  1350. End;
  1351. {$Else}
  1352. {Uncomment the next line to see how much memory the reloading optimizer needs}
  1353. { Writeln((NrOfPaiObjs*(((SizeOf(TPaiProp)+3)div 4)*4)));}
  1354. {no need to check mem/maxavail, we've got as much virtual memory as we want}
  1355. InitDFAPass2 := True;
  1356. GetMem(PaiPropBlock, NrOfPaiObjs*(((SizeOf(TPaiProp)+3)div 4)*4));
  1357. NrOfPaiFast := NrOfPaiObjs;
  1358. p := Pai(AsmL^.First);
  1359. For Count := 1 To NrOfPaiObjs Do
  1360. Begin
  1361. PaiPropBlock^[Count].LineSave := p^.fileinfo.line;
  1362. PPaiProp(p^.fileinfo.line) := @PaiPropBlock^[Count];
  1363. p := Pai(p^.next);
  1364. End;
  1365. {$EndIf TP}
  1366. End;
  1367. Function DFAPass2(AsmL: PAasmOutPut): Pai;
  1368. Begin
  1369. If InitDFAPass2(AsmL)
  1370. Then DFAPass2 := DoDFAPass2(Pai(AsmL^.First))
  1371. Else DFAPass2 := Nil;
  1372. End;
  1373. Procedure ShutDownDFA;
  1374. Begin
  1375. If LabDif <> 0 Then
  1376. FreeMem(LTable, LabDif*SizeOf(TLabelTableItem));
  1377. End;
  1378. End.
  1379. {
  1380. $Log$
  1381. Revision 1.11 1998-09-15 14:05:27 jonas
  1382. * fixed optimizer incompatibilities with freelabel code in psub
  1383. Revision 1.10 1998/09/09 15:33:58 peter
  1384. * removed warnings
  1385. Revision 1.9 1998/09/03 16:24:51 florian
  1386. * bug of type conversation from dword to real fixed
  1387. * bug fix of Jonas applied
  1388. Revision 1.8 1998/08/28 10:56:59 peter
  1389. * removed warnings
  1390. Revision 1.7 1998/08/19 16:07:44 jonas
  1391. * changed optimizer switches + cleanup of DestroyRefs in daopt386.pas
  1392. Revision 1.6 1998/08/10 14:49:57 peter
  1393. + localswitches, moduleswitches, globalswitches splitting
  1394. Revision 1.5 1998/08/09 13:56:24 jonas
  1395. * small bugfix for uncertain optimizations in DestroyRefs
  1396. Revision 1.4 1998/08/06 19:40:25 jonas
  1397. * removed $ before and after Log in comment
  1398. Revision 1.3 1998/08/05 16:00:14 florian
  1399. * some fixes for ansi strings
  1400. * log to Log changed
  1401. }