daopt386.pas 56 KB

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