daopt386.pas 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479
  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) Then
  595. LabelTable^[Pai_Label(p)^.l^.nb-LowLabel].PaiObj := p;
  596. p := pai(p^.next);
  597. End;
  598. {$IfDef TP}
  599. End
  600. Else LabelDif := 0;
  601. {$EndIf TP}
  602. End;
  603. End;
  604. {************************ Search the Label table ************************}
  605. Function FindLabel(L: PLabel; Var hp: Pai): Boolean;
  606. {searches for the specified label starting from hp as long as the
  607. encountered instructions are labels, to be able to optimize constructs like
  608. jne l2 jmp l2
  609. jmp l3 and l1:
  610. l1: l2:
  611. l2:}
  612. Var TempP: Pai;
  613. Begin
  614. TempP := hp;
  615. While Assigned(TempP) and
  616. (pai(TempP)^.typ In SkipInstr + [ait_label]) Do
  617. If (pai_label(TempP)^.l <> L)
  618. Then TempP := Pai(TempP^.next)
  619. Else
  620. Begin
  621. hp := TempP;
  622. FindLabel := True;
  623. exit
  624. End;
  625. FindLabel := False
  626. End;
  627. {************************ Some general functions ************************}
  628. Function Reg32(Reg: TRegister): TRegister;
  629. {Returns the 32 bit component of Reg if it exists, otherwise Reg is returned}
  630. Begin
  631. Reg32 := Reg;
  632. If (Reg >= R_AX)
  633. Then
  634. If (Reg <= R_DI)
  635. Then Reg32 := Reg16ToReg32(Reg)
  636. Else
  637. If (Reg <= R_BL)
  638. Then Reg32 := Reg8toReg32(Reg);
  639. End;
  640. Function PowerOf2(L: Longint): Longint;
  641. Var Counter, TempVal: Longint;
  642. Begin
  643. TempVal := 1;
  644. For Counter := 1 to L Do
  645. TempVal := TempVal * 2;
  646. PowerOf2 := TempVal;
  647. End;
  648. { inserts new_one between prev and foll }
  649. Procedure InsertLLItem(AsmL: PAasmOutput; prev, foll, new_one: PLinkedList_Item);
  650. Begin
  651. If Assigned(prev) Then
  652. If Assigned(foll) Then
  653. Begin
  654. If Assigned(new_one) Then
  655. Begin
  656. new_one^.previous := prev;
  657. new_one^.next := foll;
  658. prev^.next := new_one;
  659. foll^.previous := new_one;
  660. End;
  661. End
  662. Else AsmL^.Concat(new_one)
  663. Else If Assigned(Foll) Then AsmL^.Insert(new_one)
  664. End;
  665. {********************* Compare parts of Pai objects *********************}
  666. Function RefsEqual(Const R1, R2: TReference): Boolean;
  667. Begin
  668. If R1.IsIntValue
  669. Then RefsEqual := R2.IsIntValue and (R1.Offset = R2.Offset)
  670. Else If (R1.Offset = R2.Offset) And (R1.Base = R2.Base) And
  671. (R1.Index = R2.Index) And (R1.Segment = R2.Segment) And
  672. (R1.ScaleFactor = R2.ScaleFactor)
  673. Then
  674. Begin
  675. If Assigned(R1.Symbol)
  676. Then RefsEqual := Assigned(R2.Symbol) And (R1.Symbol^=R2.Symbol^)
  677. Else RefsEqual := Not(Assigned(R2.Symbol));
  678. End
  679. Else RefsEqual := False;
  680. End;
  681. Function IsGP32Reg(Reg: TRegister): Boolean;
  682. {Checks if the register is a 32 bit general purpose register}
  683. Begin
  684. If (Reg >= R_EAX) and (Reg <= R_EBX)
  685. Then IsGP32Reg := True
  686. Else IsGP32reg := False
  687. End;
  688. Function RegInRef(Reg: TRegister; Const Ref: TReference): Boolean;
  689. Begin {checks whether Ref contains a reference to Reg}
  690. Reg := Reg32(Reg);
  691. RegInRef := (Ref.Base = Reg) Or (Ref.Index = Reg)
  692. End;
  693. Function RegInInstruction(Reg: TRegister; p1: Pai): Boolean;
  694. {checks if Reg is used by the instruction p1}
  695. Var TmpResult: Boolean;
  696. Begin
  697. TmpResult := False;
  698. If (Pai(p1)^.typ = ait_instruction) Then
  699. Begin
  700. Case Pai386(p1)^.op1t Of
  701. Top_Reg: TmpResult := Reg = TRegister(Pai386(p1)^.op1);
  702. Top_Ref: TmpResult := RegInRef(Reg, TReference(Pai386(p1)^.op1^))
  703. End;
  704. If Not(TmpResult) Then
  705. Case Pai386(p1)^.op2t Of
  706. Top_Reg:
  707. if Pai386(p1)^.op3t<>Top_reg
  708. then TmpResult := Reg = TRegister(Pai386(p1)^.op2)
  709. else TmpResult := longint(Reg) = twowords(Pai386(p1)^.op2).word1;
  710. Top_Ref: TmpResult := RegInRef(Reg, TReference(Pai386(p1)^.op2^))
  711. End;
  712. If Not(TmpResult) Then
  713. Case Pai386(p1)^.op3t Of
  714. Top_Reg: TmpResult := longint(Reg) =twowords(Pai386(p1)^.op2).word2;
  715. Top_none:;
  716. else
  717. internalerror($Da);
  718. End
  719. End;
  720. RegInInstruction := TmpResult
  721. End;
  722. {********************* GetNext and GetLastInstruction *********************}
  723. Function GetNextInstruction(Current: Pai; Var Next: Pai): Boolean;
  724. {skips ait_regalloc, ait_regdealloc and ait_stab* objects and puts the
  725. next pai object in Next. Returns false if there isn't any}
  726. Begin
  727. GetNextInstruction := False;
  728. Current := Pai(Current^.Next);
  729. While Assigned(Current) And
  730. (Pai(Current)^.typ In SkipInstr) Do
  731. Current := Pai(Current^.Next);
  732. If Assigned(Current)
  733. Then
  734. Begin
  735. Next := Current;
  736. GetNextInstruction := True;
  737. End;
  738. End;
  739. Function GetLastInstruction(Current: Pai; Var Last: Pai): Boolean;
  740. {skips the ait-types in SkipInstr puts the previous pai object in
  741. Last. Returns false if there isn't any}
  742. Begin
  743. GetLastInstruction := False;
  744. Current := Pai(Current^.previous);
  745. While Assigned(Current) And
  746. (Pai(Current)^.typ In SkipInstr) Do
  747. Current := Pai(Current^.previous);
  748. If Assigned(Current)
  749. Then
  750. Begin
  751. Last := Current;
  752. GetLastInstruction := True;
  753. End;
  754. End;
  755. {******************* The Data Flow Analyzer functions ********************}
  756. (*Function FindZeroreg(p: Pai; Var Result: TRegister): Boolean;
  757. {Finds a register which contains the constant zero}
  758. Var Counter: TRegister;
  759. Begin
  760. Counter := R_EAX;
  761. FindZeroReg := True;
  762. While (Counter <= R_EDI) And
  763. ((PPaiProp(p^.fileinfo.line)^.Regs[Counter].Typ <> Con_Const) or
  764. (PPaiProp(p^.fileinfo.line)^.Regs[Counter].StartMod <> Pointer(0))) Do
  765. Inc(Byte(Counter));
  766. If (PPaiProp(p^.fileinfo.line)^.Regs[Counter].Typ = Con_Const) And
  767. (PPaiProp(p^.fileinfo.line)^.Regs[Counter].StartMod = Pointer(0))
  768. Then Result := Counter
  769. Else FindZeroReg := False;
  770. End;*)
  771. Function TCh2Reg(Ch: TChange): TRegister;
  772. {converts a TChange variable to a TRegister}
  773. Begin
  774. If (CH <= C_EDI)
  775. Then TCh2Reg := TRegister(Byte(Ch))
  776. Else InternalError($db)
  777. End;
  778. Procedure IncState(Var S: Word);
  779. {Increases the state by 1, wraps around at $ffff to 0 (so we won't get
  780. overflow errors}
  781. Begin
  782. If (s <> $ffff)
  783. Then Inc(s)
  784. Else s := 0
  785. End;
  786. Procedure DestroyReg(p1: PPaiProp; Reg: TRegister);
  787. {Destroys the contents of the register Reg in the PPaiProp of P}
  788. Var TmpState: Longint;
  789. Begin
  790. Reg := Reg32(Reg);
  791. NrOfInstrSinceLastMod[Reg] := 0;
  792. If (Reg >= R_EAX) And (Reg <= R_EDI)
  793. Then
  794. With p1^.Regs[Reg] Do
  795. Begin
  796. IncState(State);
  797. TmpState := State;
  798. FillChar(p1^.Regs[Reg], SizeOf(TContent), 0);
  799. State := TmpState;
  800. End;
  801. End;
  802. Function OpsEqual(typ: Longint; op1, op2: Pointer): Boolean;
  803. Begin {checks whether the two ops are equal}
  804. Case typ Of
  805. Top_Reg, Top_Const: OpsEqual := op1 = op2;
  806. Top_Ref: OpsEqual := RefsEqual(TReference(op1^), TReference(op2^));
  807. Top_None: OpsEqual := True
  808. Else OpsEqual := False
  809. End;
  810. End;
  811. Function RegsSameContent(p1, p2: Pai; Reg: TRegister): Boolean;
  812. {checks whether Reg has the same content in the PPaiProp of p1 and p2}
  813. Begin
  814. Reg := Reg32(Reg);
  815. RegsSameContent :=
  816. PPaiProp(p1^.fileinfo.line)^.Regs[Reg].State =
  817. PPaiProp(p2^.fileinfo.line)^.Regs[Reg].State;
  818. End;
  819. Function InstructionsEqual(p1, p2: Pai): Boolean;
  820. Begin {checks whether two Pai386 instructions are equal}
  821. InstructionsEqual :=
  822. Assigned(p1) And Assigned(p2) And
  823. {$ifdef regalloc}
  824. ((((Pai(p1)^.typ = ait_regalloc) And
  825. (Pai(p2)^.typ = ait_regalloc)) Or
  826. ((Pai(p1)^.typ = ait_regdealloc) And
  827. (Pai(p2)^.typ = ait_regdealloc))) And
  828. (PaiRegAlloc(p1)^.reg = PaiRegAlloc(p2)^.reg)) Or
  829. {$endif regalloc}
  830. ((Pai(p1)^.typ = ait_instruction) And
  831. (Pai(p1)^.typ = ait_instruction) And
  832. (Pai386(p1)^._operator = Pai386(p2)^._operator) And
  833. (Pai386(p1)^.op1t = Pai386(p2)^.op1t) And
  834. (Pai386(p1)^.op2t = Pai386(p2)^.op2t) And
  835. OpsEqual(Pai386(p1)^.op1t, Pai386(p1)^.op1, Pai386(p2)^.op1) And
  836. OpsEqual(Pai386(p1)^.op2t, Pai386(p1)^.op2, Pai386(p2)^.op2))
  837. End;
  838. Procedure DestroyRefs(p: pai; Const Ref: TReference; WhichReg: TRegister);
  839. {destroys all registers which possibly contain a reference to Ref, WhichReg
  840. is the register whose contents are being written to memory (if this proc
  841. is called because of a "mov?? %reg, (mem)" instruction)}
  842. Var Counter: TRegister;
  843. Begin
  844. WhichReg := Reg32(WhichReg);
  845. If ((Ref.base = ProcInfo.FramePointer) And
  846. (Ref.Index = R_NO)) Or
  847. Assigned(Ref.Symbol)
  848. Then
  849. {write something to a parameter, a local or global variable, so
  850. * with uncertzain optimizations on:
  851. - destroy the contents of registers <> WhichReg whose StartMod is of
  852. the form "mov?? (Ref), %reg". WhichReg is destroyed if it's StartMod
  853. is of that form and NrOfMods > 1 (so if it is a pointer based on Ref)
  854. * with uncertzain optimizations off:
  855. - also destroy registers that contain any pointer}
  856. For Counter := R_EAX to R_EDI Do
  857. With PPaiProp(p^.fileinfo.line)^.Regs[Counter] Do
  858. Begin
  859. If (typ = Con_Ref) And
  860. {StarMod is always of the type ait_instruction}
  861. (Pai386(StartMod)^.op1t = top_ref) And
  862. ((RefsEqual(TReference(Pai386(StartMod)^.op1^), Ref) And
  863. ((Counter <> WhichReg) Or (NrOfMods <> 1))) Or
  864. (Not(cs_UncertainOpts in aktglobalswitches) And
  865. (NrOfMods <> 1)))
  866. Then DestroyReg(PPaiProp(p^.fileinfo.line), Counter)
  867. End
  868. Else
  869. {write something to a pointer location, so
  870. * with uncertain optimzations on:
  871. - do not destroy registers which contain a local/global variable or a
  872. parameter, except if DestroyRefs is called because of a "movsl"
  873. * with uncertain optimzations off:
  874. - destroy every register which contains a memory location
  875. }
  876. For Counter := R_EAX to R_EDI Do
  877. With PPaiProp(p^.fileinfo.line)^.Regs[Counter] Do
  878. If (typ = Con_Ref) And
  879. (Not(cs_UncertainOpts in aktglobalswitches) Or
  880. {for movsl}
  881. (Ref.Base = R_EDI) Or
  882. {don't destroy if reg contains a parameter, local or global variable}
  883. Not((NrOfMods = 1) And
  884. (Pai386(StartMod)^.op1t = top_ref) And
  885. ((PReference(Pai386(StartMod)^.op1)^.base = ProcInfo.FramePointer) Or
  886. Assigned(PReference(Pai386(StartMod)^.op1)^.Symbol)
  887. )
  888. )
  889. )
  890. Then DestroyReg(PPaiProp(p^.FileInfo.Line), Counter)
  891. End;
  892. Procedure DestroyAllRegs(p: PPaiProp);
  893. Var Counter: TRegister;
  894. Begin {initializes/desrtoys all registers}
  895. For Counter := R_EAX To R_EDI Do
  896. DestroyReg(p, Counter);
  897. p^.DirFlag := F_Unknown;
  898. End;
  899. Procedure Destroy(PaiObj: Pai; opt: Longint; Op: Pointer);
  900. Begin
  901. Case opt Of
  902. top_reg: DestroyReg(PPaiProp(PaiObj^.fileinfo.line), TRegister(Op));
  903. top_ref: DestroyRefs(PaiObj, TReference(Op^), R_NO);
  904. top_symbol:;
  905. End;
  906. End;
  907. Procedure DFAPass1(AsmL: PAasmOutput);
  908. {gathers the RegAlloc data... still need to think about where to store it}
  909. Begin
  910. FindLoHiLabels(AsmL, LoLab, HiLab, LabDif);
  911. BuildLabelTable(AsmL, LTable, LoLab, LabDif);
  912. End;
  913. Function DoDFAPass2(First: Pai): Pai;
  914. {Analyzes the Data Flow of an assembler list. Starts creating the reg
  915. contents for the instructions starting with p. Returns the last pai which has
  916. been processed}
  917. Var
  918. CurProp: PPaiProp;
  919. {$ifdef AnalyzeLoops}
  920. TmpState,
  921. {$endif AnalyzeLoops}
  922. Cnt, InstrCnt : Longint;
  923. InstrProp: TAsmInstrucProp;
  924. p, hp: Pai;
  925. TmpRef: TReference;
  926. TmpReg: TRegister;
  927. Begin
  928. p := First;
  929. InstrCnt := 1;
  930. FillChar(NrOfInstrSinceLastMod, SizeOf(NrOfInstrSinceLastMod), 0);
  931. While Assigned(p) Do
  932. Begin
  933. DoDFAPass2 := p;
  934. {$IfDef TP}
  935. If (InstrCnt <= NrOfPaiFast) Then
  936. {$EndIf TP}
  937. CurProp := @PaiPropBlock^[InstrCnt]
  938. {$IfDef TP}
  939. Else New(CurProp)
  940. {$EndIf TP}
  941. ;
  942. If (p <> First)
  943. Then
  944. {$ifdef JumpAnal}
  945. Begin
  946. If (p^.Typ <> ait_label) Then
  947. {$endif JumpAnal}
  948. Begin
  949. CurProp^.Regs := PPaiProp(Pai(p^.previous)^.fileinfo.line)^.Regs;
  950. CurProp^.DirFlag := PPaiProp(Pai(p^.previous)^.fileinfo.line)^.DirFlag
  951. End
  952. {$ifdef JumpAnal}
  953. End
  954. {$endif JumpAnal}
  955. Else
  956. Begin
  957. FillChar(CurProp^, SizeOf(CurProp^), 0);
  958. { For TmpReg := R_EAX to R_EDI Do
  959. CurProp^.Regs[TmpReg].State := 1;}
  960. End;
  961. CurProp^.CanBeRemoved := False;
  962. {$ifdef TP}
  963. CurProp^.linesave := p^.fileinfo.line;
  964. PPaiProp(p^.fileinfo.line) := CurProp;
  965. {$Endif TP}
  966. For TmpReg := R_EAX To R_EDI Do
  967. Inc(NrOfInstrSinceLastMod[TmpReg]);
  968. Case p^.typ Of
  969. ait_label:
  970. {$Ifndef JumpAnal}
  971. DestroyAllRegs(CurProp);
  972. {$Else JumpAnal}
  973. Begin
  974. With LTable^[Pai_Label(p)^.l^.nb-LoLab] Do
  975. {$IfDef AnalyzeLoops}
  976. If (RefsFound = Pai_Label(p)^.l^.RefCount)
  977. {$Else AnalyzeLoops}
  978. If (JmpsProcessed = Pai_Label(p)^.l^.RefCount)
  979. {$EndIf AnalyzeLoops}
  980. Then
  981. {all jumps to this label have been found}
  982. {$IfDef AnalyzeLoops}
  983. If (JmpsProcessed > 0)
  984. Then
  985. {$EndIf AnalyzeLoops}
  986. {we've processed at least one jump to this label}
  987. Begin
  988. If Not(GetLastInstruction(p, hp) And
  989. (hp^.typ = ait_labeled_instruction) And
  990. (Pai_Labeled(hp)^._operator = A_JMP))
  991. Then
  992. {previous instruction not a JMP -> the contents of the registers after the
  993. previous intruction has been executed have to be taken into account as well}
  994. For TmpReg := R_EAX to R_EDI Do
  995. Begin
  996. If (CurProp^.Regs[TmpReg].State <>
  997. PPaiProp(Pai(p^.Previous)^.FileInfo.Line)^.Regs[TmpReg].State)
  998. Then DestroyReg(CurProp, TmpReg)
  999. End
  1000. End
  1001. {$IfDef AnalyzeLoops}
  1002. Else
  1003. {a label from a backward jump (e.g. a loop), no jump to this label has
  1004. already been processed}
  1005. If Not(GetLastInstruction(p, hp) And
  1006. (hp^.typ = ait_labeled_instruction) And
  1007. (Pai_Labeled(hp)^._operator = A_JMP))
  1008. Then
  1009. {previous instruction not a jmp, so keep all the registers' contents from the
  1010. previous instruction}
  1011. Begin
  1012. CurProp^.Regs := PPaiProp(Pai(p^.Previous)^.FileInfo.Line)^.Regs;
  1013. CurProp^.DirFlag := PPaiProp(Pai(p^.Previous)^.FileInfo.Line)^.DirFlag;
  1014. End
  1015. Else
  1016. {previous instruction a jmp and no jump to this label processed yet}
  1017. Begin
  1018. hp := p;
  1019. Cnt := InstrCnt;
  1020. {continue until we find a jump to the label or a label which has already
  1021. been processed}
  1022. While GetNextInstruction(hp, hp) And
  1023. Not((hp^.typ = ait_labeled_instruction) And
  1024. (Pai_Labeled(hp)^.lab^.nb = Pai_Label(p)^.l^.nb)) And
  1025. Not((hp^.typ = ait_label) And
  1026. (LTable^[Pai_Label(hp)^.l^.nb-LoLab].RefsFound
  1027. = Pai_Label(hp)^.l^.RefCount) And
  1028. (LTable^[Pai_Label(hp)^.l^.nb-LoLab].JmpsProcessed > 0)) Do
  1029. Inc(Cnt);
  1030. If (hp^.typ = ait_label)
  1031. Then
  1032. {there's a processed label after the current one}
  1033. Begin
  1034. CurProp^.Regs := PaiPropBlock^[Cnt].Regs;
  1035. CurProp^.DirFlag := PaiPropBlock^[Cnt].DirFlag;
  1036. End
  1037. Else
  1038. {there's no label anymore after the current one, or they haven't been
  1039. processed yet}
  1040. Begin
  1041. CurProp^.Regs := PPaiProp(Pai(p^.Previous)^.FileInfo.Line)^.Regs;
  1042. CurProp^.DirFlag := PPaiProp(Pai(p^.Previous)^.FileInfo.Line)^.DirFlag;
  1043. DestroyAllRegs(PPaiProp(Pai(p^.Previous)^.FileInfo.Line))
  1044. End
  1045. End
  1046. {$EndIf AnalyzeLoops}
  1047. Else
  1048. {not all references to this label have been found, so destroy all registers}
  1049. Begin
  1050. CurProp^.Regs := PPaiProp(Pai(p^.Previous)^.FileInfo.Line)^.Regs;
  1051. CurProp^.DirFlag := PPaiProp(Pai(p^.Previous)^.FileInfo.Line)^.DirFlag;
  1052. DestroyAllRegs(CurProp)
  1053. End;
  1054. End;
  1055. {$EndIf JumpAnal}
  1056. ait_labeled_instruction:
  1057. {$IfNDef JumpAnal}
  1058. ;
  1059. {$Else JumpAnal}
  1060. With LTable^[Pai_Labeled(p)^.lab^.nb-LoLab] Do
  1061. If (RefsFound = Pai_Labeled(p)^.lab^.RefCount) Then
  1062. Begin
  1063. If (InstrCnt < InstrNr)
  1064. Then
  1065. {forward jump}
  1066. If (JmpsProcessed = 0) Then
  1067. {no jump to this label has been processed yet}
  1068. Begin
  1069. PaiPropBlock^[InstrNr].Regs := CurProp^.Regs;
  1070. PaiPropBlock^[InstrNr].DirFlag := CurProp^.DirFlag;
  1071. Inc(JmpsProcessed);
  1072. End
  1073. Else
  1074. Begin
  1075. For TmpReg := R_EAX to R_EDI Do
  1076. If (PaiPropBlock^[InstrNr].Regs[TmpReg].State <>
  1077. CurProp^.Regs[TmpReg].State) Then
  1078. DestroyReg(@PaiPropBlock^[InstrNr], TmpReg);
  1079. Inc(JmpsProcessed);
  1080. End
  1081. {$ifdef AnalyzeLoops}
  1082. Else
  1083. { backward jump, a loop for example}
  1084. { If (JmpsProcessed > 0) Or
  1085. Not(GetLastInstruction(PaiObj, hp) And
  1086. (hp^.typ = ait_labeled_instruction) And
  1087. (Pai_Labeled(hp)^._operator = A_JMP))
  1088. Then}
  1089. {instruction prior to label is not a jmp, or at least one jump to the label
  1090. has yet been processed}
  1091. Begin
  1092. Inc(JmpsProcessed);
  1093. For TmpReg := R_EAX to R_EDI Do
  1094. If (PaiPropBlock^[InstrNr].Regs[TmpReg].State <>
  1095. CurProp^.Regs[TmpReg].State)
  1096. Then
  1097. Begin
  1098. TmpState := PaiPropBlock^[InstrNr].Regs[TmpReg].State;
  1099. Cnt := InstrNr;
  1100. While (TmpState = PaiPropBlock^[Cnt].Regs[TmpReg].State) Do
  1101. Begin
  1102. DestroyReg(@PaiPropBlock^[Cnt], TmpReg);
  1103. Inc(Cnt);
  1104. End;
  1105. While (Cnt <= InstrCnt) Do
  1106. Begin
  1107. Inc(PaiPropBlock^[Cnt].Regs[TmpReg].State);
  1108. Inc(Cnt)
  1109. End
  1110. End;
  1111. End
  1112. { Else }
  1113. {instruction prior to label is a jmp and no jumps to the label have yet been
  1114. processed}
  1115. { Begin
  1116. Inc(JmpsProcessed);
  1117. For TmpReg := R_EAX to R_EDI Do
  1118. Begin
  1119. TmpState := PaiPropBlock^[InstrNr].Regs[TmpReg].State;
  1120. Cnt := InstrNr;
  1121. While (TmpState = PaiPropBlock^[Cnt].Regs[TmpReg].State) Do
  1122. Begin
  1123. PaiPropBlock^[Cnt].Regs[TmpReg] := CurProp^.Regs[TmpReg];
  1124. Inc(Cnt);
  1125. End;
  1126. TmpState := PaiPropBlock^[InstrNr].Regs[TmpReg].State;
  1127. While (TmpState = PaiPropBlock^[Cnt].Regs[TmpReg].State) Do
  1128. Begin
  1129. DestroyReg(@PaiPropBlock^[Cnt], TmpReg);
  1130. Inc(Cnt);
  1131. End;
  1132. While (Cnt <= InstrCnt) Do
  1133. Begin
  1134. Inc(PaiPropBlock^[Cnt].Regs[TmpReg].State);
  1135. Inc(Cnt)
  1136. End
  1137. End
  1138. End}
  1139. {$endif AnalyzeLoops}
  1140. End;
  1141. {$EndIf JumpAnal}
  1142. {$ifdef GDB}
  1143. ait_stabs, ait_stabn, ait_stab_function_name:;
  1144. {$endif GDB}
  1145. {$ifdef regalloc}
  1146. ait_regalloc, ait_regdealloc:;
  1147. {$endif regalloc}
  1148. ait_instruction:
  1149. Begin
  1150. InstrProp := AsmInstr[Pai386(p)^._operator];
  1151. Case Pai386(p)^._operator Of
  1152. A_MOV, A_MOVZX, A_MOVSX:
  1153. Begin
  1154. Case Pai386(p)^.op1t Of
  1155. Top_Reg:
  1156. Case Pai386(p)^.op2t Of
  1157. Top_Reg:
  1158. Begin
  1159. DestroyReg(CurProp, TRegister(Pai386(p)^.op2));
  1160. { CurProp^.Regs[TRegister(Pai386(p)^.op2)] :=
  1161. CurProp^.Regs[TRegister(Pai386(p)^.op1)];
  1162. If (CurProp^.Regs[TRegister(Pai386(p)^.op2)].ModReg = R_NO) Then
  1163. CurProp^.Regs[TRegister(Pai386(p)^.op2)].ModReg :=
  1164. Tregister(Pai386(p)^.op1);}
  1165. End;
  1166. Top_Ref: DestroyRefs(p, TReference(Pai386(p)^.op2^), TRegister(Pai386(p)^.op1));
  1167. End;
  1168. Top_Ref:
  1169. Begin {destination is always a register in this case}
  1170. TmpReg := Reg32(TRegister(Pai386(p)^.op2));
  1171. If (RegInRef(TmpReg, TReference(Pai386(p)^.op1^)))
  1172. Then
  1173. Begin
  1174. With CurProp^.Regs[TmpReg] Do
  1175. Begin
  1176. IncState(State);
  1177. If (typ <> Con_Ref) Then
  1178. Begin
  1179. typ := Con_Ref;
  1180. StartMod := p;
  1181. End;
  1182. {also store how many instructions are part of the sequence in the first
  1183. instructions PPaiProp, so it can be easily accessed from within
  1184. CheckSequence}
  1185. Inc(NrOfMods, NrOfInstrSinceLastMod[TmpReg]);
  1186. PPaiProp(Pai(StartMod)^.fileinfo.line)^.Regs[TmpReg].NrOfMods := NrOfMods;
  1187. NrOfInstrSinceLastMod[TmpReg] := 0;
  1188. End;
  1189. End
  1190. Else
  1191. Begin
  1192. DestroyReg(CurProp, TmpReg);
  1193. With CurProp^.Regs[TmpReg] Do
  1194. Begin
  1195. Typ := Con_Ref;
  1196. StartMod := p;
  1197. NrOfMods := 1;
  1198. End;
  1199. End;
  1200. End;
  1201. Top_Const:
  1202. Begin
  1203. Case Pai386(p)^.op2t Of
  1204. Top_Reg:
  1205. Begin
  1206. TmpReg := Reg32(TRegister(Pai386(p)^.op2));
  1207. With CurProp^.Regs[TmpReg] Do
  1208. Begin
  1209. {it doesn't matter that the state is changed,
  1210. it isn't looked at when removing constant reloads}
  1211. DestroyReg(CurProp, TmpReg);
  1212. typ := Con_Const;
  1213. StartMod := Pai386(p)^.op1;
  1214. End
  1215. End;
  1216. Top_Ref: DestroyRefs(P, TReference(Pai386(p)^.op2^), R_NO);
  1217. End;
  1218. End;
  1219. End;
  1220. End;
  1221. A_IMUL:
  1222. Begin
  1223. If (Pai386(p)^.Op3t = top_none)
  1224. Then
  1225. If (Pai386(p)^.Op2t = top_none)
  1226. Then
  1227. Begin
  1228. DestroyReg(CurProp, R_EAX);
  1229. DestroyReg(CurProp, R_EDX)
  1230. End
  1231. Else
  1232. Begin
  1233. If (Pai386(p)^.Op2t = top_reg) Then
  1234. DestroyReg(CurProp, TRegister(Pai386(p)^.Op2));
  1235. End
  1236. Else If (Pai386(p)^.Op3t = top_reg) Then
  1237. DestroyReg(CurProp, TRegister(longint(twowords(Pai386(p)^.Op2).word2)));
  1238. End;
  1239. A_XOR:
  1240. Begin
  1241. If (Pai386(p)^.op1t = top_reg) And
  1242. (Pai386(p)^.op2t = top_reg) And
  1243. (Pai386(p)^.op1 = Pai386(p)^.op2)
  1244. Then
  1245. Begin
  1246. DestroyReg(CurProp, Tregister(Pai386(p)^.op1));
  1247. CurProp^.Regs[Reg32(Tregister(Pai386(p)^.op1))].typ := Con_Const;
  1248. CurProp^.Regs[Reg32(Tregister(Pai386(p)^.op1))].StartMod := Pointer(0)
  1249. End
  1250. Else Destroy(p, Pai386(p)^.op2t, Pai386(p)^.op2);
  1251. End
  1252. Else
  1253. Begin
  1254. If InstrProp.NCh <> 255
  1255. Then
  1256. For Cnt := 1 To InstrProp.NCh Do
  1257. Case InstrProp.Ch[Cnt] Of
  1258. C_None:;
  1259. C_EAX..C_EDI: DestroyReg(CurProp, TCh2Reg(InstrProp.Ch[Cnt]));
  1260. C_CDirFlag: CurProp^.DirFlag := F_NotSet;
  1261. C_SDirFlag: CurProp^.DirFlag := F_Set;
  1262. C_Op1: Destroy(p, Pai386(p)^.op1t, Pai386(p)^.op1);
  1263. C_Op2: Destroy(p, Pai386(p)^.op2t, Pai386(p)^.op2);
  1264. C_Op3: Destroy(p, Pai386(p)^.op2t, Pointer(Longint(TwoWords(Pai386(p)^.op2).word2)));
  1265. C_MemEDI:
  1266. Begin
  1267. FillChar(TmpRef, SizeOf(TmpRef), 0);
  1268. TmpRef.Base := R_EDI;
  1269. DestroyRefs(p, TmpRef, R_NO)
  1270. End;
  1271. C_Flags, C_FPU:;
  1272. End
  1273. Else
  1274. Begin
  1275. DestroyAllRegs(CurProp);
  1276. End;
  1277. End;
  1278. End;
  1279. End
  1280. Else
  1281. Begin
  1282. DestroyAllRegs(CurProp);
  1283. End;
  1284. End;
  1285. Inc(InstrCnt);
  1286. p := Pai(p^.next);
  1287. End;
  1288. End;
  1289. Function InitDFAPass2(AsmL: PAasmOutput): Boolean;
  1290. {reserves memory for the PPaiProps in one big memory block when not using
  1291. TP, returns False if not enough memory is available for the optimizer in all
  1292. cases}
  1293. Var p: Pai;
  1294. Count: Longint;
  1295. { TmpStr: String; }
  1296. Begin
  1297. P := Pai(AsmL^.First);
  1298. NrOfPaiObjs := 1;
  1299. While (P <> Pai(AsmL^.last)) Do
  1300. Begin
  1301. {$IfNDef TP}
  1302. Case P^.Typ Of
  1303. ait_labeled_instruction:
  1304. begin
  1305. If (Pai_Labeled(P)^.lab^.nb >= LoLab) And
  1306. (Pai_Labeled(P)^.lab^.nb <= HiLab) Then
  1307. Inc(LTable^[Pai_Labeled(P)^.lab^.nb-LoLab].RefsFound);
  1308. end;
  1309. ait_label:
  1310. Begin
  1311. LTable^[Pai_Label(P)^.l^.nb-LoLab].InstrNr := NrOfPaiObjs
  1312. End;
  1313. { ait_instruction:
  1314. Begin
  1315. If (Pai386(p)^._operator = A_PUSH) And
  1316. (Pai386(p)^.op1t = top_symbol) And
  1317. (PCSymbol(Pai386(p)^.op1)^.offset = 0) Then
  1318. Begin
  1319. TmpStr := StrPas(PCSymbol(Pai386(p)^.op1)^.symbol);
  1320. If}
  1321. End;
  1322. {$EndIf TP}
  1323. Inc(NrOfPaiObjs);
  1324. P := Pai(P^.next)
  1325. End;
  1326. {$IfDef TP}
  1327. If (MemAvail < (SizeOf(TPaiProp)*NrOfPaiObjs))
  1328. {this doesn't have to be one contiguous block}
  1329. Then InitDFAPass2 := False
  1330. Else
  1331. Begin
  1332. InitDFAPass2 := True;
  1333. If (MaxAvail < 65520)
  1334. Then NrOfPaiFast := MaxAvail Div (((SizeOf(TPaiProp)+1) div 2)*2)
  1335. Else NrOfPaiFast := 65520 Div (((SizeOf(TPaiProp)+1) div 2)*2);
  1336. If (NrOfPaiFast > 0) Then
  1337. GetMem(PaiPropBlock, NrOfPaiFast*(((SizeOf(TPaiProp)+1) div 2)*2));
  1338. End;
  1339. {$Else}
  1340. {Uncomment the next line to see how much memory the reloading optimizer needs}
  1341. { Writeln((NrOfPaiObjs*(((SizeOf(TPaiProp)+3)div 4)*4)));}
  1342. {no need to check mem/maxavail, we've got as much virtual memory as we want}
  1343. InitDFAPass2 := True;
  1344. GetMem(PaiPropBlock, NrOfPaiObjs*(((SizeOf(TPaiProp)+3)div 4)*4));
  1345. NrOfPaiFast := NrOfPaiObjs;
  1346. p := Pai(AsmL^.First);
  1347. For Count := 1 To NrOfPaiObjs Do
  1348. Begin
  1349. PaiPropBlock^[Count].LineSave := p^.fileinfo.line;
  1350. PPaiProp(p^.fileinfo.line) := @PaiPropBlock^[Count];
  1351. p := Pai(p^.next);
  1352. End;
  1353. {$EndIf TP}
  1354. End;
  1355. Function DFAPass2(AsmL: PAasmOutPut): Pai;
  1356. Begin
  1357. If InitDFAPass2(AsmL)
  1358. Then DFAPass2 := DoDFAPass2(Pai(AsmL^.First))
  1359. Else DFAPass2 := Nil;
  1360. End;
  1361. Procedure ShutDownDFA;
  1362. Begin
  1363. If LabDif <> 0 Then
  1364. FreeMem(LTable, LabDif*SizeOf(TLabelTableItem));
  1365. End;
  1366. End.
  1367. {
  1368. $Log$
  1369. Revision 1.9 1998-09-03 16:24:51 florian
  1370. * bug of type conversation from dword to real fixed
  1371. * bug fix of Jonas applied
  1372. Revision 1.8 1998/08/28 10:56:59 peter
  1373. * removed warnings
  1374. Revision 1.7 1998/08/19 16:07:44 jonas
  1375. * changed optimizer switches + cleanup of DestroyRefs in daopt386.pas
  1376. Revision 1.6 1998/08/10 14:49:57 peter
  1377. + localswitches, moduleswitches, globalswitches splitting
  1378. Revision 1.5 1998/08/09 13:56:24 jonas
  1379. * small bugfix for uncertain optimizations in DestroyRefs
  1380. Revision 1.4 1998/08/06 19:40:25 jonas
  1381. * removed $ before and after Log in comment
  1382. Revision 1.3 1998/08/05 16:00:14 florian
  1383. * some fixes for ansi strings
  1384. * log to Log changed
  1385. }