daopt386.pas 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185
  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
  25. GlobType,
  26. CObjects,Aasm,i386;
  27. Type
  28. TRegArray = Array[R_EAX..R_BL] of TRegister;
  29. TRegSet = Set of R_EAX..R_BL;
  30. TRegInfo = Record
  31. NewRegsEncountered, OldRegsEncountered: TRegSet;
  32. RegsLoadedForRef: TRegSet;
  33. Old2NewReg, New2OldReg: TRegArray;
  34. End;
  35. {*********************** Procedures and Functions ************************}
  36. Procedure InsertLLItem(AsmL: PAasmOutput; prev, foll, new_one: PLinkedList_Item);
  37. Function Reg32(Reg: TRegister): TRegister;
  38. Function RefsEquivalent(Const R1, R2: TReference; Var RegInfo: TRegInfo): Boolean;
  39. Function RefsEqual(Const R1, R2: TReference): Boolean;
  40. Function IsGP32Reg(Reg: TRegister): Boolean;
  41. Function RegInRef(Reg: TRegister; Const Ref: TReference): Boolean;
  42. Function RegInInstruction(Reg: TRegister; p1: Pai): Boolean;
  43. Function RegModifiedByInstruction(Reg: TRegister; p1: Pai): Boolean;
  44. Function GetNextInstruction(Current: Pai; Var Next: Pai): Boolean;
  45. Function GetLastInstruction(Current: Pai; Var Last: Pai): Boolean;
  46. Procedure UpdateUsedRegs(Var UsedRegs: TRegSet; p: Pai);
  47. Function RegsEquivalent(OldReg, NewReg: TRegister; Var RegInfo: TRegInfo): Boolean;
  48. Function InstructionsEquivalent(p1, p2: Pai; Var RegInfo: TRegInfo): Boolean;
  49. Procedure DFAPass1(AsmL: PAasmOutput);
  50. Function DFAPass2(AsmL: PAasmOutput): Pai;
  51. Procedure ShutDownDFA;
  52. Function FindLabel(L: PLabel; Var hp: Pai): Boolean;
  53. {Procedure FindLoHiLabels(AsmL: PAasmOutput; Var LoLab, HiLab, LabDif: Longint);}
  54. {******************************* Constants *******************************}
  55. Const
  56. {ait_* types which don't result in executable code or which don't influence
  57. the way the program runs/behaves}
  58. SkipInstr = [ait_comment, ait_align, ait_symbol
  59. {$ifdef GDB}
  60. ,ait_stabs, ait_stabn, ait_stab_function_name
  61. {$endif GDB}
  62. ,ait_regalloc, ait_regdealloc
  63. ];
  64. {the maximum number of things (registers, memory, ...) a single instruction
  65. changes}
  66. MaxCh = 3;
  67. {Possible register content types}
  68. con_Unknown = 0;
  69. con_ref = 1;
  70. con_const = 2;
  71. {********************************* Types *********************************}
  72. Type
  73. {What an instruction can change}
  74. TChange = (C_None,
  75. {Read from a register}
  76. C_REAX, C_RECX, C_REDX, C_REBX, C_RESP, C_REBP, C_RESI, C_REDI,
  77. {write from a register}
  78. C_WEAX, C_WECX, C_WEDX, C_WEBX, C_WESP, C_WEBP, C_WESI, C_WEDI,
  79. {read and write from/to a register}
  80. C_RWEAX, C_RWECX, C_RWEDX, C_RWEBX, C_RWESP, C_RWEBP, C_RWESI, C_RWEDI,
  81. C_CDirFlag {clear direction flag}, C_SDirFlag {set dir flag},
  82. C_RFlags, C_WFlags, C_RWFlags, C_FPU,
  83. C_ROp1, C_WOp1, C_RWOp1,
  84. C_ROp2, C_WOp2, C_RWOp2,
  85. C_ROp3, C_WOp3, C_RWOp3,
  86. C_WMemEDI,
  87. C_All);
  88. {the possible states of a flag}
  89. TFlagContents = (F_Unknown, F_NotSet, F_Set);
  90. {the properties of a cpu instruction}
  91. TAsmInstrucProp = Record
  92. {how many things it changes}
  93. { NCh: Byte;}
  94. {and what it changes}
  95. Ch: Array[1..MaxCh] of TChange;
  96. End;
  97. TContent = Record
  98. {start and end of block instructions that defines the
  99. content of this register. If Typ = con_const, then
  100. Longint(StartMod) = value of the constant)}
  101. StartMod: Pointer;
  102. {starts at 0, gets increased everytime the register is written to}
  103. WState: Byte;
  104. {starts at 0, gets increased everytime the register is read from}
  105. RState: Byte;
  106. {how many instructions starting with StarMod does the block consist of}
  107. NrOfMods: Byte;
  108. {the type of the content of the register: unknown, memory, constant}
  109. Typ: Byte;
  110. End;
  111. {Contents of the integer registers}
  112. TRegContent = Array[R_EAX..R_EDI] Of TContent;
  113. {contents of the FPU registers}
  114. TRegFPUContent = Array[R_ST..R_ST7] Of TContent;
  115. {information record with the contents of every register. Every Pai object
  116. gets one of these assigned: a pointer to it is stored in the Line field and
  117. the original line number is stored in LineSave}
  118. TPaiProp = Record
  119. Regs: TRegContent;
  120. { FPURegs: TRegFPUContent;} {currently not yet used}
  121. LineSave: Longint;
  122. {allocated Registers}
  123. UsedRegs: TRegSet;
  124. {status of the direction flag}
  125. DirFlag: TFlagContents;
  126. {can this instruction be removed?}
  127. CanBeRemoved: Boolean;
  128. End;
  129. PPaiProp = ^TPaiProp;
  130. {$IfNDef TP}
  131. TPaiPropBlock = Array[1..250000] Of TPaiProp;
  132. PPaiPropBlock = ^TPaiPropBlock;
  133. {$EndIf TP}
  134. TInstrSinceLastMod = Array[R_EAX..R_EDI] Of Byte;
  135. TLabelTableItem = Record
  136. PaiObj: Pai;
  137. {$IfDef JumpAnal}
  138. InstrNr: Longint;
  139. RefsFound: Word;
  140. JmpsProcessed: Word
  141. {$EndIf JumpAnal}
  142. End;
  143. {$IfDef tp}
  144. TLabelTable = Array[0..10000] Of TLabelTableItem;
  145. {$Else tp}
  146. TLabelTable = Array[0..2500000] Of TLabelTableItem;
  147. {$Endif tp}
  148. PLabelTable = ^TLabelTable;
  149. TwoWords = Record
  150. Word1, Word2: Word;
  151. End;
  152. {******************************* Variables *******************************}
  153. Var
  154. {the amount of PaiObjects in the current assembler list}
  155. NrOfPaiObjs: Longint;
  156. {$IfNDef TP}
  157. {Array which holds all TPaiProps}
  158. PaiPropBlock: PPaiPropBlock;
  159. {$EndIf TP}
  160. LoLab, HiLab, LabDif: Longint;
  161. LTable: PLabelTable;
  162. {*********************** End of Interface section ************************}
  163. Implementation
  164. Uses globals, systems, strings, verbose, hcodegen,
  165. {$ifdef i386}
  166. pass_2;
  167. {$endif i386}
  168. Const AsmInstr: Array[tasmop] Of TAsmInstrucProp = (
  169. {MOV} (Ch: (C_WOp2, C_ROp1, C_None)),
  170. {MOVZX} (Ch: (C_WOp2, C_ROp1, C_None)),
  171. {MOVSX} (Ch: (C_WOp2, C_ROp1, C_None)),
  172. {LABEL} (Ch: (C_All, C_None, C_None)), {don't know value of any register}
  173. {ADD} (Ch: (C_RWOp2, C_ROp1, C_WFlags)),
  174. {CALL} (Ch: (C_All, C_None, C_None)), {don't know value of any register}
  175. {IDIV} (Ch: (C_RWEAX, C_WEDX, C_WFlags)),
  176. {IMUL} (Ch: (C_RWEAX, C_WEDX, C_WFlags)), {handled separately, because several forms exist}
  177. {JMP} (Ch: (C_None, C_None, C_None)),
  178. {LEA} (Ch: (C_WOp2, C_ROp1, C_None)),
  179. {MUL} (Ch: (C_RWEAX, C_WEDX, C_WFlags)),
  180. {NEG} (Ch: (C_RWOp1, C_None, C_None)),
  181. {NOT} (Ch: (C_RWOp1, C_WFlags, C_None)),
  182. {POP} (Ch: (C_WOp1, C_RWESP, C_None)),
  183. {POPAD} (Ch: (C_All, C_None, C_None)), {don't know value of any register}
  184. {PUSH} (Ch: (C_RWESP, C_None, C_None)),
  185. {PUSHAD} (Ch: (C_RWESP, C_None, C_None)),
  186. {RET} (Ch: (C_ALL, C_None, C_None)),
  187. {SUB} (Ch: (C_RWOp2, C_ROp1, C_WFlags)),
  188. {XCHG} (Ch: (C_RWOp1, C_RWOp2, C_None)), {(might be) handled seperately}
  189. {XOR} (Ch: (C_RWOp2, C_ROp1, C_WFlags)),
  190. {FILD} (Ch: (C_FPU, C_None, C_None)),
  191. {CMP} (Ch: (C_WFlags, C_None, C_None)),
  192. {JZ} (Ch: (C_RFlags, C_None, C_None)),
  193. {INC} (Ch: (C_RWOp1, C_WFlags, C_None)),
  194. {DEC} (Ch: (C_RWOp1, C_WFlags, C_None)),
  195. {SETE} (Ch: (C_WOp1, C_RFlags, C_None)),
  196. {SETNE} (Ch: (C_WOp1, C_RFlags, C_None)),
  197. {SETL} (Ch: (C_WOp1, C_RFlags, C_None)),
  198. {SETG} (Ch: (C_WOp1, C_RFlags, C_None)),
  199. {SETLE} (Ch: (C_WOp1, C_RFlags, C_None)),
  200. {SETGE} (Ch: (C_WOp1, C_RFlags, C_None)),
  201. {JE} (Ch: (C_RFlags, C_None, C_None)),
  202. {JNE} (Ch: (C_RFlags, C_None, C_None)),
  203. {JL} (Ch: (C_RFlags, C_None, C_None)),
  204. {JG} (Ch: (C_RFlags, C_None, C_None)),
  205. {JLE} (Ch: (C_RFlags, C_None, C_None)),
  206. {JGE} (Ch: (C_RFlags, C_None, C_None)),
  207. {OR} (Ch: (C_RWOp2, C_WFlags, C_None)),
  208. {FLD} (Ch: (C_ROp1, C_FPU, C_None)),
  209. {FADD} (Ch: (C_FPU, C_None, C_None)),
  210. {FMUL} (Ch: (C_FPU, C_None, C_None)),
  211. {FSUB} (Ch: (C_FPU, C_None, C_None)),
  212. {FDIV} (Ch: (C_FPU, C_None, C_None)),
  213. {FCHS} (Ch: (C_FPU, C_None, C_None)),
  214. {FLD1} (Ch: (C_FPU, C_None, C_None)),
  215. {FIDIV} (Ch: (C_FPU, C_None, C_None)),
  216. {JNZ} (Ch: (C_RFlags, C_None, C_None)),
  217. {FSTP} (Ch: (C_WOp1, C_FPU, C_None)),
  218. {AND} (Ch: (C_RWOp2, C_ROp1, C_WFlags)),
  219. {JNO} (Ch: (C_RFlags, C_None, C_None)),
  220. {NOTH} (Ch: (C_None, C_None, C_None)), {***???***}
  221. {NONE} (Ch: (C_None, C_None, C_None)),
  222. {ENTER} (Ch: (C_RWESP, C_None, C_None)),
  223. {LEAVE} (Ch: (C_RWESP, C_None, C_None)),
  224. {CLD} (Ch: (C_CDirFlag, C_None, C_None)),
  225. {MOVS} (Ch: (C_RWESI, C_RWEDI, C_WMemEDI)),
  226. {REP} (Ch: (C_RWECX, C_RFlags, C_None)),
  227. {SHL} (Ch: (C_RWOp2, C_ROp1, C_WFlags)),
  228. {SHR} (Ch: (C_RWOp2, C_ROp1, C_WFlags)),
  229. {BOUND} (Ch: (C_ROp1, C_None, C_None)),
  230. {JNS} (Ch: (C_RFlags, C_None, C_None)),
  231. {JS} (Ch: (C_RFlags, C_None, C_None)),
  232. {JO} (Ch: (C_RFlags, C_None, C_None)),
  233. {SAR} (Ch: (C_RWOp2, C_ROp1, C_WFlags)),
  234. {TEST} (Ch: (C_WFlags, C_ROp1, C_ROp2)),
  235. {FCOM} (Ch: (C_FPU, C_None, C_None)),
  236. {FCOMP} (Ch: (C_FPU, C_None, C_None)),
  237. {FCOMPP} (Ch: (C_FPU, C_None, C_None)),
  238. {FXCH} (Ch: (C_FPU, C_None, C_None)),
  239. {FADDP} (Ch: (C_FPU, C_None, C_None)),
  240. {FMULP} (Ch: (C_FPU, C_None, C_None)),
  241. {FSUBP} (Ch: (C_FPU, C_None, C_None)),
  242. {FDIVP} (Ch: (C_FPU, C_None, C_None)),
  243. {FNSTS} (Ch: (C_WOp1, C_None, C_None)),
  244. {SAHF} (Ch: (C_WFlags, C_REAX, C_None)),
  245. {FDIVRP} (Ch: (C_FPU, C_None, C_None)),
  246. {FSUBRP} (Ch: (C_FPU, C_None, C_None)),
  247. {SETC} (Ch: (C_WOp1, C_RFlags, C_None)),
  248. {SETNC} (Ch: (C_WOp1, C_RFlags, C_None)),
  249. {JC} (Ch: (C_None, C_RFlags, C_None)),
  250. {JNC} (Ch: (C_RFlags, C_None, C_None)),
  251. {JA} (Ch: (C_RFlags, C_None, C_None)),
  252. {JAE} (Ch: (C_RFlags, C_None, C_None)),
  253. {JB} (Ch: (C_RFlags, C_None, C_None)),
  254. {JBE} (Ch: (C_RFlags, C_None, C_None)),
  255. {SETA} (Ch: (C_WOp1, C_RFlags, C_None)),
  256. {SETAE} (Ch: (C_WOp1, C_RFlags, C_None)),
  257. {SETB} (Ch: (C_WOp1, C_RFlags, C_None)),
  258. {SETBE} (Ch: (C_WOp1, C_RFlags, C_None)),
  259. {AAA} (Ch: (C_RWEAX, C_WFlags, C_None)),
  260. {AAD} (Ch: (C_RWEAX, C_WFlags, C_None)),
  261. {AAM} (Ch: (C_RWEAX, C_WFlags, C_None)),
  262. {AAS} (Ch: (C_RWEAX, C_WFlags, C_None)),
  263. {CBW} (Ch: (C_RWEAX, C_None, C_None)),
  264. {CDQ} (Ch: (C_RWEAX, C_WEDX, C_None)),
  265. {CLC} (Ch: (C_WFlags, C_None, C_None)),
  266. {CLI} (Ch: (C_WFlags, C_None, C_None)),
  267. {CLTS} (Ch: (C_None, C_None, C_None)),
  268. {CMC} (Ch: (C_WFlags, C_None, C_None)),
  269. {CWD} (Ch: (C_RWEAX, C_WEDX, C_None)),
  270. {CWDE} (Ch: (C_RWEAX, C_None, C_None)),
  271. {DAA} (Ch: (C_RWEAX, C_None, C_None)),
  272. {DAS} (Ch: (C_RWEAX, C_None, C_None)),
  273. {HLT} (Ch: (C_None, C_None, C_None)),
  274. {IRET} (Ch: (C_All, C_None, C_None)), {don't know value of any register}
  275. {LAHF} (Ch: (C_WEAX, C_RFlags, C_None)),
  276. {LODS} (Ch: (C_WEAX, C_RWESI, C_None)),
  277. {LOCK} (Ch: (C_None, C_None, C_None)),
  278. {NOP} (Ch: (C_None, C_None, C_None)),
  279. {PUSHA} (Ch: (C_ALL, C_None, C_None)), {not true, but a pushall is usually followed by an instruction that does, so
  280. it won hurt either}
  281. {PUSHF} (Ch: (C_RWESP, C_RFlags, C_None)),
  282. {PUSHFD} (Ch: (C_RWESP, C_RFlags, C_None)),
  283. {STC} (Ch: (C_WFlags, C_None, C_None)),
  284. {STD} (Ch: (C_SDirFlag, C_None, C_None)),
  285. {STI} (Ch: (C_WFlags, C_None, C_None)),
  286. {STOS} (Ch: (C_WMemEDI, C_RWEDI, C_REAX)),
  287. {WAIT} (Ch: (C_None, C_None, C_None)),
  288. {XLAT} (Ch: (C_WEAX, C_REBX, C_None)),
  289. {XLATB} (Ch: (C_WEAX, C_REBX, C_None)),
  290. {MOVSB} (Ch: (C_WOp2, C_ROp1, C_None)),
  291. {MOVSBL} (Ch: (C_WOp2, C_ROp1, C_None)),
  292. {MOVSBW} (Ch: (C_WOp2, C_ROp1, C_None)),
  293. {MOVSWL} (Ch: (C_WOp2, C_ROp1, C_None)),
  294. {MOVZB} (Ch: (C_WOp2, C_ROp1, C_None)),
  295. {MOVZWL} (Ch: (C_WOp2, C_ROp1, C_None)),
  296. {POPA} (Ch: (C_All, C_None, C_None)), {don't know value of any register}
  297. {IN} (Ch: (C_WOp2, C_ROp1, C_None)),
  298. {OUT} (Ch: (C_ROp1, C_ROp2, C_None)),
  299. {LDS} (Ch: (C_WOp2, C_None, C_None)),
  300. {LCS} (Ch: (C_WOp2, C_None, C_None)),
  301. {LES} (Ch: (C_WOp2, C_None, C_None)),
  302. {LFS} (Ch: (C_WOp2, C_None, C_None)),
  303. {LGS} (Ch: (C_WOp2, C_None, C_None)),
  304. {LSS} (Ch: (C_WOp2, C_None, C_None)),
  305. {POPF} (Ch: (C_RWESP, C_WFlags, C_None)),
  306. {SBB} (Ch: (C_RWOp2, C_ROp1, C_RWFlags)),
  307. {ADC} (Ch: (C_RWOp2, C_ROp1, C_RWFlags)),
  308. {DIV} (Ch: (C_RWEAX, C_WEDX, C_WFlags)),
  309. {ROR} (Ch: (C_RWOp2, C_ROp1, C_RWFlags)),
  310. {ROL} (Ch: (C_RWOp2, C_ROp1, C_RWFlags)),
  311. {RCL} (Ch: (C_RWOp2, C_ROp1, C_RWFlags)),
  312. {RCR} (Ch: (C_RWOp2, C_ROp1, C_RWFlags)),
  313. {SAL} (Ch: (C_RWOp2, C_ROp1, C_RWFlags)),
  314. {SHLD} (Ch: (C_RWOp3, C_RWFlags, C_ROp2)),
  315. {SHRD} (Ch: (C_RWOp3, C_RWFlags, C_ROp2)),
  316. {LCALL} (Ch: (C_All, C_None, C_None)), {don't know value of any register}
  317. {LJMP} (Ch: (C_All, C_None, C_None)), {don't know value of any register}
  318. {LRET} (Ch: (C_All, C_None, C_None)), {don't know value of any register}
  319. {JNAE} (Ch: (C_RFlags, C_None, C_None)),
  320. {JNB} (Ch: (C_RFlags, C_None, C_None)),
  321. {JNA} (Ch: (C_RFlags, C_None, C_None)),
  322. {JNBE} (Ch: (C_RFlags, C_None, C_None)),
  323. {JP} (Ch: (C_RFlags, C_None, C_None)),
  324. {JNP} (Ch: (C_RFlags, C_None, C_None)),
  325. {JPE} (Ch: (C_RFlags, C_None, C_None)),
  326. {JPO} (Ch: (C_RFlags, C_None, C_None)),
  327. {JNGE} (Ch: (C_RFlags, C_None, C_None)),
  328. {JNG} (Ch: (C_RFlags, C_None, C_None)),
  329. {JNL} (Ch: (C_RFlags, C_None, C_None)),
  330. {JNLE} (Ch: (C_RFlags, C_None, C_None)),
  331. {JCXZ} (Ch: (C_RECX, C_None, C_None)),
  332. {JECXZ} (Ch: (C_RECX, C_None, C_None)),
  333. {LOOP} (Ch: (C_RWECX, C_None, C_None)),
  334. {CMPS} (Ch: (C_RWESI, C_RWEDI, C_WFlags)),
  335. {INS} (Ch: (C_RWEDI, C_WMemEDI, C_None)),
  336. {OUTS} (Ch: (C_RWESI, C_None, C_None)),
  337. {SCAS} (Ch: (C_RWEDI, C_WFlags, C_None)),
  338. {BSF} (Ch: (C_WOp2, C_WFlags, C_ROp1)),
  339. {BSR} (Ch: (C_WOp2, C_WFlags, C_ROp1)),
  340. {BT} (Ch: (C_WFlags, C_ROp1, C_None)),
  341. {BTC} (Ch: (C_RWOp2, C_ROp1, C_WFlags)),
  342. {BTR} (Ch: (C_RWOp2, C_ROp1, C_WFlags)),
  343. {BTS} (Ch: (C_RWOp2, C_ROp1, C_WFlags)),
  344. {INT} (Ch: (C_All, C_None, C_None)), {don't know value of any register}
  345. {INT3} (Ch: (C_None, C_None, C_None)),
  346. {INTO} (Ch: (C_All, C_None, C_None)), {don't know value of any register}
  347. {BOUNDL} (Ch: (C_ROp1, C_None, C_None)),
  348. {BOUNDW} (Ch: (C_ROp1, C_None, C_None)),
  349. {LOOPZ} (Ch: (C_RWECX, C_RFlags, C_None)),
  350. {LOOPE} (Ch: (C_RWECX, C_RFlags, C_None)),
  351. {LOOPNZ} (Ch: (C_RWECX, C_RFlags, C_None)),
  352. {LOOPNE} (Ch: (C_RWECX, C_RFlags, C_None)),
  353. {SETO} (Ch: (C_WOp1, C_RFlags, C_None)),
  354. {SETNO} (Ch: (C_WOp1, C_RFlags, C_None)),
  355. {SETNAE} (Ch: (C_WOp1, C_RFlags, C_None)),
  356. {SETNB} (Ch: (C_WOp1, C_RFlags, C_None)),
  357. {SETZ} (Ch: (C_WOp1, C_RFlags, C_None)),
  358. {SETNZ} (Ch: (C_WOp1, C_RFlags, C_None)),
  359. {SETNA} (Ch: (C_WOp1, C_RFlags, C_None)),
  360. {SETNBE} (Ch: (C_WOp1, C_RFlags, C_None)),
  361. {SETS} (Ch: (C_WOp1, C_RFlags, C_None)),
  362. {SETNS} (Ch: (C_WOp1, C_RFlags, C_None)),
  363. {SETP} (Ch: (C_WOp1, C_RFlags, C_None)),
  364. {SETPE} (Ch: (C_WOp1, C_RFlags, C_None)),
  365. {SETNP} (Ch: (C_WOp1, C_RFlags, C_None)),
  366. {SETPO} (Ch: (C_WOp1, C_RFlags, C_None)),
  367. {SETNGE} (Ch: (C_WOp1, C_RFlags, C_None)),
  368. {SETNL} (Ch: (C_WOp1, C_RFlags, C_None)),
  369. {SETNG} (Ch: (C_WOp1, C_RFlags, C_None)),
  370. {SETNLE} (Ch: (C_WOp1, C_RFlags, C_None)),
  371. {ARPL} (Ch: (C_WFlags, C_None, C_None)),
  372. {LAR} (Ch: (C_WOp2, C_None, C_None)),
  373. {LGDT} (Ch: (C_None, C_None, C_None)),
  374. {LIDT} (Ch: (C_None, C_None, C_None)),
  375. {LLDT} (Ch: (C_None, C_None, C_None)),
  376. {LMSW} (Ch: (C_None, C_None, C_None)),
  377. {LSL} (Ch: (C_WOp2, C_WFlags, C_None)),
  378. {LTR} (Ch: (C_None, C_None, C_None)),
  379. {SGDT} (Ch: (C_WOp1, C_None, C_None)),
  380. {SIDT} (Ch: (C_WOp1, C_None, C_None)),
  381. {SLDT} (Ch: (C_WOp1, C_None, C_None)),
  382. {SMSW} (Ch: (C_WOp1, C_None, C_None)),
  383. {STR} (Ch: (C_WOp1, C_None, C_None)),
  384. {VERR} (Ch: (C_WFlags, C_None, C_None)),
  385. {VERW} (Ch: (C_WFlags, C_None, C_None)),
  386. {FABS} (Ch: (C_FPU, C_None, C_None)),
  387. {FBLD} (Ch: (C_ROp1, C_FPU, C_None)),
  388. {FBSTP} (Ch: (C_WOp1, C_FPU, C_None)),
  389. {FCLEX} (Ch: (C_FPU, C_None, C_None)),
  390. {FNCLEX} (Ch: (C_FPU, C_None, C_None)),
  391. {FCOS} (Ch: (C_FPU, C_None, C_None)),
  392. {FDECSTP}(Ch: (C_FPU, C_None, C_None)),
  393. {FDISI} (Ch: (C_FPU, C_None, C_None)),
  394. {FNDISI} (Ch: (C_FPU, C_None, C_None)),
  395. {FDIVR} (Ch: (C_FPU, C_None, C_None)),
  396. {FENI} (Ch: (C_FPU, C_None, C_None)),
  397. {FNENI} (Ch: (C_FPU, C_None, C_None)),
  398. {FFREE} (Ch: (C_FPU, C_None, C_None)),
  399. {FIADD} (Ch: (C_FPU, C_None, C_None)),
  400. {FICOM} (Ch: (C_FPU, C_None, C_None)),
  401. {FICOMP} (Ch: (C_FPU, C_None, C_None)),
  402. {FIDIVR} (Ch: (C_FPU, C_None, C_None)),
  403. {FIMUL} (Ch: (C_FPU, C_None, C_None)),
  404. {FINCSTP}(Ch: (C_FPU, C_None, C_None)),
  405. {FINIT} (Ch: (C_FPU, C_None, C_None)),
  406. {FNINIT} (Ch: (C_FPU, C_None, C_None)),
  407. {FIST} (Ch: (C_WOp1, C_None, C_None)),
  408. {FISTP} (Ch: (C_WOp1, C_None, C_None)),
  409. {FISUB} (Ch: (C_FPU, C_None, C_None)),
  410. {FSUBR} (Ch: (C_FPU, C_None, C_None)),
  411. {FLDCW} (Ch: (C_FPU, C_None, C_None)),
  412. {FLDENV} (Ch: (C_FPU, C_None, C_None)),
  413. {FLDLG2} (Ch: (C_FPU, C_None, C_None)),
  414. {FLDLN2} (Ch: (C_FPU, C_None, C_None)),
  415. {FLDL2E} (Ch: (C_FPU, C_None, C_None)),
  416. {FLDL2T} (Ch: (C_FPU, C_None, C_None)),
  417. {FLDPI} (Ch: (C_FPU, C_None, C_None)),
  418. {FLDS} (Ch: (C_FPU, C_None, C_None)),
  419. {FLDZ} (Ch: (C_FPU, C_None, C_None)),
  420. {FNOP} (Ch: (C_FPU, C_None, C_None)),
  421. {FPATAN} (Ch: (C_FPU, C_None, C_None)),
  422. {FPREM} (Ch: (C_FPU, C_None, C_None)),
  423. {FPREM1} (Ch: (C_FPU, C_None, C_None)),
  424. {FPTAN} (Ch: (C_FPU, C_None, C_None)),
  425. {FRNDINT}(Ch: (C_FPU, C_None, C_None)),
  426. {FRSTOR} (Ch: (C_FPU, C_None, C_None)),
  427. {FSAVE} (Ch: (C_WOp1, C_None, C_None)),
  428. {FNSAVE} (Ch: (C_FPU, C_None, C_None)),
  429. {FSCALE} (Ch: (C_FPU, C_None, C_None)),
  430. {FSETPM} (Ch: (C_FPU, C_None, C_None)),
  431. {FSIN} (Ch: (C_FPU, C_None, C_None)),
  432. {FSINCOS}(Ch: (C_FPU, C_None, C_None)),
  433. {FSQRT} (Ch: (C_FPU, C_None, C_None)),
  434. {FST} (Ch: (C_WOp1, C_None, C_None)),
  435. {FSTCW} (Ch: (C_WOp1, C_None, C_None)),
  436. {FNSTCW} (Ch: (C_WOp1, C_None, C_None)),
  437. {FSTENV} (Ch: (C_WOp1, C_None, C_None)),
  438. {FNSTENV}(Ch: (C_WOp1, C_None, C_None)),
  439. {FSTSW} (Ch: (C_WOp1, C_None, C_None)),
  440. {FNSTSW} (Ch: (C_WOp1, C_None, C_None)),
  441. {FTST} (Ch: (C_FPU, C_None, C_None)),
  442. {FUCOM} (Ch: (C_FPU, C_None, C_None)),
  443. {FUCOMP} (Ch: (C_FPU, C_None, C_None)),
  444. {FUCOMPP}(Ch: (C_FPU, C_None, C_None)),
  445. {FWAIT} (Ch: (C_FPU, C_None, C_None)),
  446. {FXAM} (Ch: (C_FPU, C_None, C_None)),
  447. {FXTRACT}(Ch: (C_FPU, C_None, C_None)),
  448. {FYL2X} (Ch: (C_FPU, C_None, C_None)),
  449. {FYL2XP1}(Ch: (C_FPU, C_None, C_None)),
  450. {F2XM1} (Ch: (C_FPU, C_None, C_None)),
  451. {FILDQ} (Ch: (C_FPU, C_None, C_None)),
  452. {FILDS} (Ch: (C_FPU, C_None, C_None)),
  453. {FILDL} (Ch: (C_FPU, C_None, C_None)),
  454. {FLDL} (Ch: (C_FPU, C_None, C_None)),
  455. {FLDT} (Ch: (C_FPU, C_None, C_None)),
  456. {FISTQ} (Ch: (C_WOp1, C_None, C_None)),
  457. {FISTS} (Ch: (C_WOp1, C_None, C_None)),
  458. {FISTL} (Ch: (C_WOp1, C_None, C_None)),
  459. {FSTL} (Ch: (C_WOp1, C_None, C_None)),
  460. {FSTS} (Ch: (C_WOp1, C_None, C_None)),
  461. {FSTPS} (Ch: (C_WOp1, C_FPU, C_None)),
  462. {FISTPL} (Ch: (C_WOp1, C_None, C_None)),
  463. {FSTPL} (Ch: (C_WOp1, C_FPU, C_None)),
  464. {FISTPS} (Ch: (C_WOp1, C_FPU, C_None)),
  465. {FISTPQ} (Ch: (C_WOp1, C_FPU, C_None)),
  466. {FSTPT} (Ch: (C_WOp1, C_FPU, C_None)),
  467. {FCOMPS} (Ch: (C_FPU, C_None, C_None)),
  468. {FICOMPL}(Ch: (C_FPU, C_None, C_None)),
  469. {FCOMPL} (Ch: (C_FPU, C_None, C_None)),
  470. {FICOMPS}(Ch: (C_FPU, C_None, C_None)),
  471. {FCOMS} (Ch: (C_FPU, C_None, C_None)),
  472. {FICOML} (Ch: (C_FPU, C_None, C_None)),
  473. {FCOML} (Ch: (C_FPU, C_None, C_None)),
  474. {FICOMS} (Ch: (C_FPU, C_None, C_None)),
  475. {FIADDL} (Ch: (C_FPU, C_None, C_None)),
  476. {FADDL} (Ch: (C_FPU, C_None, C_None)),
  477. {FIADDS} (Ch: (C_FPU, C_None, C_None)),
  478. {FISUBL} (Ch: (C_FPU, C_None, C_None)),
  479. {FSUBL} (Ch: (C_FPU, C_None, C_None)),
  480. {FISUBS} (Ch: (C_FPU, C_None, C_None)),
  481. {FSUBS} (Ch: (C_FPU, C_None, C_None)),
  482. {FSUBR} (Ch: (C_FPU, C_None, C_None)),
  483. {FSUBRS} (Ch: (C_FPU, C_None, C_None)),
  484. {FISUBRL}(Ch: (C_FPU, C_None, C_None)),
  485. {FSUBRL} (Ch: (C_FPU, C_None, C_None)),
  486. {FISUBRS}(Ch: (C_FPU, C_None, C_None)),
  487. {FMULS} (Ch: (C_FPU, C_None, C_None)),
  488. {FIMUL} (Ch: (C_FPU, C_None, C_None)),
  489. {FMULL} (Ch: (C_FPU, C_None, C_None)),
  490. {FIMULS} (Ch: (C_FPU, C_None, C_None)),
  491. {FIDIVS} (Ch: (C_FPU, C_None, C_None)),
  492. {FIDIVL} (Ch: (C_FPU, C_None, C_None)),
  493. {FDIVL} (Ch: (C_FPU, C_None, C_None)),
  494. {FIDIVS} (Ch: (C_FPU, C_None, C_None)),
  495. {FDIVRS} (Ch: (C_FPU, C_None, C_None)),
  496. {FIDIVRL}(Ch: (C_FPU, C_None, C_None)),
  497. {FDIVRL} (Ch: (C_FPU, C_None, C_None)),
  498. {FIDIVRS}(Ch: (C_FPU, C_None, C_None)),
  499. {REPE} (Ch: (C_RWECX, C_RFlags, C_None)),
  500. {REPNE} (Ch: (C_RWECX, C_RFlags, C_None)),
  501. {CPUID} (Ch: (C_All, C_None, C_none)),
  502. {FADDS} (Ch: (C_FPU, C_None, C_None)),
  503. {POPFD} (Ch: (C_RWESP, C_WFlags, C_None)),
  504. {below are the MMX instructions}
  505. {A_EMMS} (Ch: (C_FPU, C_None, C_None)),
  506. {A_MOVD} (Ch: (C_WOp2, C_None, C_None)),
  507. {A_MOVQ} (Ch: (C_WOp2, C_None, C_None)),
  508. {A_PACKSSDW} (Ch: (C_All, C_None, C_None)),
  509. {A_PACKSSWB} (Ch: (C_All, C_None, C_None)),
  510. {A_PACKUSWB} (Ch: (C_All, C_None, C_None)),
  511. {A_PADDB} (Ch: (C_RWOp2, C_None, C_None)),
  512. {A_PADDD} (Ch: (C_RWOp2, C_None, C_None)),
  513. {A_PADDSB} (Ch: (C_RWOp2, C_None, C_None)),
  514. {A_PADDSW} (Ch: (C_RWOp2, C_None, C_None)),
  515. {A_PADDUSB} (Ch: (C_RWOp2, C_None, C_None)),
  516. {A_PADDUSW} (Ch: (C_RWOp2, C_None, C_None)),
  517. {A_PADDW} (Ch: (C_RWOp2, C_None, C_None)),
  518. {A_PAND} (Ch: (C_RWOp2, C_None, C_None)),
  519. {A_PANDN} (Ch: (C_RWOp2, C_None, C_None)),
  520. {A_PCMPEQB} (Ch: (C_All, C_None, C_None)),
  521. {A_PCMPEQD} (Ch: (C_All, C_None, C_None)),
  522. {A_PCMPEQW} (Ch: (C_All, C_None, C_None)),
  523. {A_PCMPGTB} (Ch: (C_All, C_None, C_None)),
  524. {A_PCMPGTD} (Ch: (C_All, C_None, C_None)),
  525. {A_PCMPGTW} (Ch: (C_All, C_None, C_None)),
  526. {A_PMADDWD} (Ch: (C_RWOp2, C_None, C_None)),
  527. {A_PMULHW} (Ch: (C_All, C_None, C_None)),
  528. {A_PMULLW} (Ch: (C_All, C_None, C_None)),
  529. {A_POR} (Ch: (C_RWOp2, C_None, C_None)),
  530. {A_PSLLD} (Ch: (C_RWOp2, C_None, C_None)),
  531. {A_PSLLQ} (Ch: (C_RWOp2, C_None, C_None)),
  532. {A_PSLLW} (Ch: (C_RWOp2, C_None, C_None)),
  533. {A_PSRAD} (Ch: (C_RWOp2, C_None, C_None)),
  534. {A_PSRAW} (Ch: (C_RWOp2, C_None, C_None)),
  535. {A_PSRLD} (Ch: (C_RWOp2, C_None, C_None)),
  536. {A_PSRLQ} (Ch: (C_RWOp2, C_None, C_None)),
  537. {A_PSRLW} (Ch: (C_RWOp2, C_None, C_None)),
  538. {A_PSUBB} (Ch: (C_RWOp2, C_None, C_None)),
  539. {A_PSUBD} (Ch: (C_RWOp2, C_None, C_None)),
  540. {A_PSUBSB} (Ch: (C_RWOp2, C_None, C_None)),
  541. {A_PSUBSW} (Ch: (C_RWOp2, C_None, C_None)),
  542. {A_PSUBUSB} (Ch: (C_RWOp2, C_None, C_None)),
  543. {A_PSUBUSW} (Ch: (C_RWOp2, C_None, C_None)),
  544. {A_PSUBW} (Ch: (C_RWOp2, C_None, C_None)),
  545. {A_PUNPCKHBW} (Ch: (C_All, C_None, C_None)),
  546. {A_PUNPCKHDQ} (Ch: (C_All, C_None, C_None)),
  547. {A_PUNPCKHWD} (Ch: (C_All, C_None, C_None)),
  548. {A_PUNPCKLBW} (Ch: (C_All, C_None, C_None)),
  549. {A_PUNPCKLDQ} (Ch: (C_All, C_None, C_None)),
  550. {A_PUNPCKLWD} (Ch: (C_All, C_None, C_None)),
  551. {A_PXOR} (Ch: (C_RWOp2, C_None, C_None)));
  552. Var
  553. {How many instructions are between the current instruction and the last one
  554. that modified the register}
  555. NrOfInstrSinceLastMod: TInstrSinceLastMod;
  556. {************************ Create the Label table ************************}
  557. Procedure FindLoHiLabels(AsmL: PAasmOutput; Var LowLabel, HighLabel, LabelDif: Longint);
  558. {Walks through the paasmlist to find the lowest and highest label number;
  559. Since 0.9.3: also removes unused labels}
  560. Var LabelFound: Boolean;
  561. P{, hp1}: Pai;
  562. Begin
  563. LabelFound := False;
  564. LowLabel := MaxLongint;
  565. HighLabel := 0;
  566. P := Pai(AsmL^.first);
  567. While Assigned(p) Do
  568. Begin
  569. If (Pai(p)^.typ = ait_label) Then
  570. If (Pai_Label(p)^.l^.is_used)
  571. Then
  572. Begin
  573. LabelFound := True;
  574. If (Pai_Label(p)^.l^.nb < LowLabel) Then
  575. LowLabel := Pai_Label(p)^.l^.nb;
  576. If (Pai_Label(p)^.l^.nb > HighLabel) Then
  577. HighLabel := Pai_Label(p)^.l^.nb;
  578. End
  579. { Else
  580. Begin
  581. hp1 := pai(p^.next);
  582. AsmL^.Remove(p);
  583. Dispose(p, Done);
  584. p := hp1;
  585. continue;
  586. End};
  587. GetNextInstruction(p, p);
  588. End;
  589. If LabelFound
  590. Then LabelDif := HighLabel+1-LowLabel
  591. Else LabelDif := 0;
  592. End;
  593. Function FindRegAlloc(Reg: TRegister; StartPai: Pai): Boolean;
  594. {Returns true if a ait_regalloc object for Reg is found in the block of Pai's
  595. starting with StartPai and ending with the next "real" instruction}
  596. Var TmpResult: Boolean;
  597. Begin
  598. TmpResult := False;
  599. Repeat
  600. While Assigned(StartPai) And
  601. ((StartPai^.typ in (SkipInstr - [ait_RegAlloc])) Or
  602. ((StartPai^.typ = ait_label) and
  603. Not(Pai_Label(StartPai)^.l^.Is_Used))) Do
  604. StartPai := Pai(StartPai^.Next);
  605. If Assigned(StartPai) And
  606. (StartPai^.typ = ait_RegAlloc) Then
  607. Begin
  608. TmpResult := (PaiRegAlloc(StartPai)^.Reg = Reg);
  609. StartPai := Pai(StartPai^.Next);
  610. End;
  611. Until Not(Assigned(StartPai)) Or
  612. Not(StartPai^.typ in SkipInstr) or TmpResult;
  613. FindRegAlloc := TmpResult;
  614. End;
  615. Procedure BuildLabelTableAndFixRegAlloc(AsmL: PAasmOutput; Var LabelTable: PLabelTable; LowLabel: Longint;
  616. Var LabelDif: Longint);
  617. {Builds a table with the locations of the labels in the paasmoutput.
  618. Also fixes some RegDeallocs like "# %eax released; push (%eax)"}
  619. Var p, hp1, hp2: Pai;
  620. UsedRegs: TRegSet;
  621. Begin
  622. UsedRegs := [];
  623. If (LabelDif <> 0) Then
  624. Begin
  625. {$IfDef TP}
  626. If (MaxAvail >= LabelDif*SizeOf(Pai))
  627. Then
  628. Begin
  629. {$EndIf TP}
  630. GetMem(LabelTable, LabelDif*SizeOf(TLabelTableItem));
  631. FillChar(LabelTable^, LabelDif*SizeOf(TLabelTableItem), 0);
  632. p := pai(AsmL^.first);
  633. While Assigned(p) Do
  634. Begin
  635. Case p^.typ Of
  636. ait_Label:
  637. If Pai_Label(p)^.l^.is_used Then
  638. LabelTable^[Pai_Label(p)^.l^.nb-LowLabel].PaiObj := p;
  639. ait_RegAlloc:
  640. Begin
  641. If Not(PaiRegAlloc(p)^.Reg in UsedRegs) Then
  642. UsedRegs := UsedRegs + [PaiRegAlloc(p)^.Reg]
  643. Else
  644. Begin
  645. hp1 := p;
  646. hp2 := nil;
  647. While GetLastInstruction(hp1, hp1) And
  648. Not(RegInInstruction(PaiRegAlloc(p)^.Reg, hp1)) Do
  649. hp2 := hp1;
  650. If hp2 <> nil Then
  651. Begin
  652. hp1 := New(PaiRegDeAlloc, Init(PaiRegAlloc(p)^.Reg));
  653. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp1);
  654. End;
  655. End;
  656. End;
  657. ait_RegDeAlloc:
  658. Begin
  659. UsedRegs := UsedRegs - [PaiRegDeAlloc(p)^.Reg];
  660. hp1 := p;
  661. hp2 := nil;
  662. While Not(FindRegAlloc(PaiRegDeAlloc(p)^.Reg, Pai(hp1^.Next))) And
  663. GetNextInstruction(hp1, hp1) And
  664. RegInInstruction(PaiRegDeAlloc(p)^.Reg, hp1) Do
  665. hp2 := hp1;
  666. If hp2 <> nil Then
  667. Begin
  668. hp1 := Pai(p^.previous);
  669. AsmL^.Remove(p);
  670. InsertLLItem(AsmL, hp2, Pai(hp2^.Next), p);
  671. p := hp1;
  672. End;
  673. End;
  674. End;
  675. P := Pai(p^.Next);
  676. While Assigned(p) And
  677. (p^.typ in (SkipInstr - [ait_regdealloc,ait_regalloc])) Do
  678. P := Pai(P^.Next);
  679. End;
  680. {$IfDef TP}
  681. End
  682. Else LabelDif := 0;
  683. {$EndIf TP}
  684. End;
  685. End;
  686. {************************ Search the Label table ************************}
  687. Function FindLabel(L: PLabel; Var hp: Pai): Boolean;
  688. {searches for the specified label starting from hp as long as the
  689. encountered instructions are labels, to be able to optimize constructs like
  690. jne l2 jmp l2
  691. jmp l3 and l1:
  692. l1: l2:
  693. l2:}
  694. Var TempP: Pai;
  695. Begin
  696. TempP := hp;
  697. While Assigned(TempP) and
  698. (TempP^.typ In SkipInstr + [ait_label]) Do
  699. If (TempP^.typ <> ait_Label) Or
  700. (pai_label(TempP)^.l <> L)
  701. Then GetNextInstruction(TempP, TempP)
  702. Else
  703. Begin
  704. hp := TempP;
  705. FindLabel := True;
  706. exit
  707. End;
  708. FindLabel := False;
  709. End;
  710. {************************ Some general functions ************************}
  711. Function Reg32(Reg: TRegister): TRegister;
  712. {Returns the 32 bit component of Reg if it exists, otherwise Reg is returned}
  713. Begin
  714. Reg32 := Reg;
  715. If (Reg >= R_AX)
  716. Then
  717. If (Reg <= R_DI)
  718. Then Reg32 := Reg16ToReg32(Reg)
  719. Else
  720. If (Reg <= R_BL)
  721. Then Reg32 := Reg8toReg32(Reg);
  722. End;
  723. { inserts new_one between prev and foll }
  724. Procedure InsertLLItem(AsmL: PAasmOutput; prev, foll, new_one: PLinkedList_Item);
  725. Begin
  726. If Assigned(prev) Then
  727. If Assigned(foll) Then
  728. Begin
  729. If Assigned(new_one) Then
  730. Begin
  731. new_one^.previous := prev;
  732. new_one^.next := foll;
  733. prev^.next := new_one;
  734. foll^.previous := new_one;
  735. End;
  736. End
  737. Else AsmL^.Concat(new_one)
  738. Else If Assigned(Foll) Then AsmL^.Insert(new_one)
  739. End;
  740. {********************* Compare parts of Pai objects *********************}
  741. Function RegsSameSize(Reg1, Reg2: TRegister): Boolean;
  742. {returns true if Reg1 and Reg2 are of the same size (so if they're both
  743. 8bit, 16bit or 32bit)}
  744. Begin
  745. If (Reg1 <= R_EDI)
  746. Then RegsSameSize := (Reg2 <= R_EDI)
  747. Else
  748. If (Reg1 <= R_DI)
  749. Then RegsSameSize := (Reg2 in [R_AX..R_DI])
  750. Else
  751. If (Reg1 <= R_BL)
  752. Then RegsSameSize := (Reg2 in [R_AL..R_BL])
  753. Else RegsSameSize := False
  754. End;
  755. Procedure AddReg2RegInfo(OldReg, NewReg: TRegister; Var RegInfo: TRegInfo);
  756. {updates the ???RegsEncountered and ???2???Reg fields of RegInfo. Assumes that
  757. OldReg and NewReg have the same size (has to be chcked in advance with
  758. RegsSameSize) and that neither equals R_NO}
  759. Begin
  760. With RegInfo Do
  761. Begin
  762. NewRegsEncountered := NewRegsEncountered + [NewReg];
  763. OldRegsEncountered := OldRegsEncountered + [OldReg];
  764. New2OldReg[NewReg] := OldReg;
  765. Old2NewReg[OldReg] := NewReg;
  766. Case OldReg Of
  767. R_EAX..R_EDI:
  768. Begin
  769. NewRegsEncountered := NewRegsEncountered + [Reg32toReg16(NewReg)];
  770. OldRegsEncountered := OldRegsEncountered + [Reg32toReg16(OldReg)];
  771. New2OldReg[Reg32toReg16(NewReg)] := Reg32toReg16(OldReg);
  772. Old2NewReg[Reg32toReg16(OldReg)] := Reg32toReg16(NewReg);
  773. If (NewReg in [R_EAX..R_EBX]) And
  774. (OldReg in [R_EAX..R_EBX]) Then
  775. Begin
  776. NewRegsEncountered := NewRegsEncountered + [Reg32toReg8(NewReg)];
  777. OldRegsEncountered := OldRegsEncountered + [Reg32toReg8(OldReg)];
  778. New2OldReg[Reg32toReg8(NewReg)] := Reg32toReg8(OldReg);
  779. Old2NewReg[Reg32toReg8(OldReg)] := Reg32toReg8(NewReg);
  780. End;
  781. End;
  782. R_AX..R_DI:
  783. Begin
  784. NewRegsEncountered := NewRegsEncountered + [Reg16toReg32(NewReg)];
  785. OldRegsEncountered := OldRegsEncountered + [Reg16toReg32(OldReg)];
  786. New2OldReg[Reg16toReg32(NewReg)] := Reg16toReg32(OldReg);
  787. Old2NewReg[Reg16toReg32(OldReg)] := Reg16toReg32(NewReg);
  788. If (NewReg in [R_AX..R_BX]) And
  789. (OldReg in [R_AX..R_BX]) Then
  790. Begin
  791. NewRegsEncountered := NewRegsEncountered + [Reg16toReg8(NewReg)];
  792. OldRegsEncountered := OldRegsEncountered + [Reg16toReg8(OldReg)];
  793. New2OldReg[Reg16toReg8(NewReg)] := Reg16toReg8(OldReg);
  794. Old2NewReg[Reg16toReg8(OldReg)] := Reg16toReg8(NewReg);
  795. End;
  796. End;
  797. R_AL..R_BL:
  798. Begin
  799. NewRegsEncountered := NewRegsEncountered + [Reg8toReg32(NewReg)]
  800. + [Reg8toReg16(NewReg)];
  801. OldRegsEncountered := OldRegsEncountered + [Reg8toReg32(OldReg)]
  802. + [Reg8toReg16(OldReg)];
  803. New2OldReg[Reg8toReg32(NewReg)] := Reg8toReg32(OldReg);
  804. Old2NewReg[Reg8toReg16(OldReg)] := Reg8toReg16(NewReg);
  805. End;
  806. End;
  807. End;
  808. End;
  809. Procedure AddOp2RegInfo(typ: Longint; Op: Pointer; Var RegInfo: TRegInfo);
  810. Begin
  811. Case typ Of
  812. Top_Reg:
  813. If (TRegister(op) <> R_NO) Then
  814. AddReg2RegInfo(TRegister(op), TRegister(op), RegInfo);
  815. Top_Ref:
  816. Begin
  817. If TReference(op^).base <> R_NO Then
  818. AddReg2RegInfo(TReference(op^).base, TReference(op^).base, RegInfo);
  819. If TReference(op^).index <> R_NO Then
  820. AddReg2RegInfo(TReference(op^).index, TReference(op^).index, RegInfo);
  821. End;
  822. End;
  823. End;
  824. Function RegsEquivalent(OldReg, NewReg: TRegister; Var RegInfo: TRegInfo): Boolean;
  825. Begin
  826. If Not((OldReg = R_NO) Or (NewReg = R_NO)) Then
  827. If RegsSameSize(OldReg, NewReg) Then
  828. With RegInfo Do
  829. {here we always check for the 32 bit component, because it is possible that
  830. the 8 bit component has not been set, event though NewReg already has been
  831. processed. This happens if it has been compared with a register that doesn't
  832. have an 8 bit component (such as EDI). In that case the 8 bit component is
  833. still set to R_NO and the comparison in the Else-part will fail}
  834. If Not((Reg32(NewReg) in NewRegsEncountered) Or
  835. (Reg32(OldReg) in OldRegsEncountered)) Then
  836. Begin
  837. AddReg2RegInfo(OldReg, NewReg, RegInfo);
  838. RegsEquivalent := True
  839. End
  840. Else RegsEquivalent :=
  841. (Reg32(NewReg) in NewRegsEncountered) And
  842. (Reg32(OldReg) in OldRegsEncountered) And
  843. (OldReg = New2OldReg[NewReg])
  844. Else RegsEquivalent := False
  845. Else RegsEquivalent := OldReg = NewReg
  846. End;
  847. Function RefsEquivalent(Const R1, R2: TReference; var RegInfo: TRegInfo): Boolean;
  848. Begin
  849. If R1.IsIntValue
  850. Then RefsEquivalent := R2.IsIntValue and (R1.Offset = R2.Offset)
  851. Else If (R1.Offset = R2.Offset) And
  852. RegsEquivalent(R1.Base, R2.Base, RegInfo) And
  853. RegsEquivalent(R1.Index, R2.Index, RegInfo) And
  854. (R1.Segment = R2.Segment) And (R1.ScaleFactor = R2.ScaleFactor)
  855. Then
  856. Begin
  857. If Assigned(R1.Symbol)
  858. Then RefsEquivalent := Assigned(R2.Symbol) And (R1.Symbol^=R2.Symbol^)
  859. Else RefsEquivalent := Not(Assigned(R2.Symbol));
  860. End
  861. Else RefsEquivalent := False;
  862. End;
  863. Function RefsEqual(Const R1, R2: TReference): Boolean;
  864. Begin
  865. If R1.IsIntValue
  866. Then RefsEqual := R2.IsIntValue and (R1.Offset = R2.Offset)
  867. Else If (R1.Offset = R2.Offset) And (R1.Base = R2.Base) And
  868. (R1.Index = R2.Index) And (R1.Segment = R2.Segment) And
  869. (R1.ScaleFactor = R2.ScaleFactor)
  870. Then
  871. Begin
  872. If Assigned(R1.Symbol)
  873. Then RefsEqual := Assigned(R2.Symbol) And (R1.Symbol^=R2.Symbol^)
  874. Else RefsEqual := Not(Assigned(R2.Symbol));
  875. End
  876. Else RefsEqual := False;
  877. End;
  878. Function IsGP32Reg(Reg: TRegister): Boolean;
  879. {Checks if the register is a 32 bit general purpose register}
  880. Begin
  881. If (Reg >= R_EAX) and (Reg <= R_EBX)
  882. Then IsGP32Reg := True
  883. Else IsGP32reg := False
  884. End;
  885. Function RegInRef(Reg: TRegister; Const Ref: TReference): Boolean;
  886. Begin {checks whether Ref contains a reference to Reg}
  887. Reg := Reg32(Reg);
  888. RegInRef := (Ref.Base = Reg) Or (Ref.Index = Reg)
  889. End;
  890. Function RegInInstruction(Reg: TRegister; p1: Pai): Boolean;
  891. {checks if Reg is used by the instruction p1}
  892. Var TmpResult: Boolean;
  893. Begin
  894. TmpResult := False;
  895. If (Pai(p1)^.typ = ait_instruction) Then
  896. Begin
  897. Case Pai386(p1)^.op1t Of
  898. Top_Reg: TmpResult := Reg = TRegister(Pai386(p1)^.op1);
  899. Top_Ref: TmpResult := RegInRef(Reg, TReference(Pai386(p1)^.op1^))
  900. End;
  901. If Not(TmpResult) Then
  902. Case Pai386(p1)^.op2t Of
  903. Top_Reg:
  904. if Pai386(p1)^.op3t<>Top_reg
  905. then TmpResult := Reg = TRegister(Pai386(p1)^.op2)
  906. else TmpResult := longint(Reg) = twowords(Pai386(p1)^.op2).word1;
  907. Top_Ref: TmpResult := RegInRef(Reg, TReference(Pai386(p1)^.op2^))
  908. End;
  909. If Not(TmpResult) Then
  910. Case Pai386(p1)^.op3t Of
  911. Top_Reg: TmpResult := longint(Reg) =twowords(Pai386(p1)^.op2).word2;
  912. Top_none:;
  913. else
  914. internalerror($Da);
  915. End
  916. End;
  917. RegInInstruction := TmpResult
  918. End;
  919. {Function RegInOp(Reg: TRegister; opt: Longint; op: Pointer): Boolean;
  920. Begin
  921. RegInOp := False;
  922. Case opt Of
  923. top_reg: RegInOp := Reg = TRegister(Pointer);
  924. top_ref: RegInOp := (Reg = TReference(op^).Base) Or
  925. (Reg = TReference(op^).Index);
  926. End;
  927. End;}
  928. Function RegModifiedByInstruction(Reg: TRegister; p1: Pai): Boolean;
  929. {returns true if Reg is modified by the instruction p1. P1 is assumed to be
  930. of the type ait_instruction}
  931. Var hp: Pai;
  932. Begin
  933. If GetLastInstruction(p1, hp)
  934. Then
  935. RegModifiedByInstruction :=
  936. PPAiProp(p1^.fileinfo.line)^.Regs[Reg].WState <>
  937. PPAiProp(hp^.fileinfo.line)^.Regs[Reg].WState
  938. Else RegModifiedByInstruction := True;
  939. End;
  940. {********************* GetNext and GetLastInstruction *********************}
  941. Function GetNextInstruction(Current: Pai; Var Next: Pai): Boolean;
  942. {skips ait_regalloc, ait_regdealloc and ait_stab* objects and puts the
  943. next pai object in Next. Returns false if there isn't any}
  944. Begin
  945. Repeat
  946. Current := Pai(Current^.Next);
  947. While Assigned(Current) And
  948. ((Current^.typ In SkipInstr) or
  949. ((Current^.typ = ait_label) And
  950. Not(Pai_Label(Current)^.l^.is_used))) Do
  951. Current := Pai(Current^.Next);
  952. If Assigned(Current) And
  953. (Current^.typ = ait_Marker) And
  954. (Pai_Marker(Current)^.Kind = NoPropInfoStart) Then
  955. Begin
  956. While Assigned(Current) And
  957. Not((Current^.typ = ait_Marker) And
  958. (Pai_Marker(Current)^.Kind = NoPropInfoEnd)) Do
  959. Current := Pai(Current^.Next)
  960. End;
  961. Until Not(Assigned(Current)) Or
  962. (Current^.typ <> ait_Marker);
  963. Next := Current;
  964. If Assigned(Current) And
  965. Not((Current^.typ In SkipInstr) or
  966. ((Current^.typ = ait_label) And
  967. Not(Pai_Label(Current)^.l^.is_used)))
  968. Then GetNextInstruction := True
  969. Else
  970. Begin
  971. Next := Nil;
  972. GetNextInstruction := False;
  973. End;
  974. End;
  975. Function GetLastInstruction(Current: Pai; Var Last: Pai): Boolean;
  976. {skips the ait-types in SkipInstr puts the previous pai object in
  977. Last. Returns false if there isn't any}
  978. Begin
  979. Repeat
  980. Current := Pai(Current^.previous);
  981. While Assigned(Current) And
  982. ((Pai(Current)^.typ In SkipInstr) or
  983. ((Pai(Current)^.typ = ait_label) And
  984. Not(Pai_Label(Current)^.l^.is_used))) Do
  985. Current := Pai(Current^.previous);
  986. If Assigned(Current) And
  987. (Current^.typ = ait_Marker) And
  988. (Pai_Marker(Current)^.Kind = NoPropInfoEnd) Then
  989. Begin
  990. While Assigned(Current) And
  991. Not((Current^.typ = ait_Marker) And
  992. (Pai_Marker(Current)^.Kind = NoPropInfoStart)) Do
  993. Current := Pai(Current^.previous);
  994. End;
  995. Until Not(Assigned(Current)) Or
  996. (Current^.typ <> ait_Marker);
  997. Last := Current;
  998. If Assigned(Current) And
  999. Not((Current^.typ In SkipInstr) or
  1000. ((Current^.typ = ait_label) And
  1001. Not(Pai_Label(Current)^.l^.is_used)))
  1002. Then GetLastInstruction := True
  1003. Else
  1004. Begin
  1005. Last := Nil;
  1006. GetLastInstruction := False
  1007. End;
  1008. End;
  1009. {******************* The Data Flow Analyzer functions ********************}
  1010. Procedure UpdateUsedRegs(Var UsedRegs: TRegSet; p: Pai);
  1011. {updates UsedRegs with the RegAlloc Information coming after P}
  1012. Begin
  1013. Repeat
  1014. While Assigned(p) And
  1015. ((p^.typ in (SkipInstr - [ait_RegAlloc, ait_RegDealloc])) or
  1016. ((p^.typ = ait_label) And
  1017. Not(Pai_Label(p)^.l^.is_used))) Do
  1018. p := Pai(p^.next);
  1019. While Assigned(p) And
  1020. (p^.typ in [ait_RegAlloc, ait_RegDealloc]) Do
  1021. Begin
  1022. Case p^.typ Of
  1023. ait_RegAlloc: UsedRegs := UsedRegs + [PaiRegAlloc(p)^.Reg];
  1024. ait_regdealloc: UsedRegs := UsedRegs - [PaiRegDeAlloc(p)^.Reg];
  1025. End;
  1026. p := pai(p^.next);
  1027. End;
  1028. Until Not(Assigned(p)) Or
  1029. (Not(p^.typ in SkipInstr) And
  1030. Not((p^.typ = ait_label) And
  1031. Not(Pai_Label(p)^.l^.is_used)));
  1032. End;
  1033. (*Function FindZeroreg(p: Pai; Var Result: TRegister): Boolean;
  1034. {Finds a register which contains the constant zero}
  1035. Var Counter: TRegister;
  1036. Begin
  1037. Counter := R_EAX;
  1038. FindZeroReg := True;
  1039. While (Counter <= R_EDI) And
  1040. ((PPaiProp(p^.fileinfo.line)^.Regs[Counter].Typ <> Con_Const) or
  1041. (PPaiProp(p^.fileinfo.line)^.Regs[Counter].StartMod <> Pointer(0))) Do
  1042. Inc(Byte(Counter));
  1043. If (PPaiProp(p^.fileinfo.line)^.Regs[Counter].Typ = Con_Const) And
  1044. (PPaiProp(p^.fileinfo.line)^.Regs[Counter].StartMod = Pointer(0))
  1045. Then Result := Counter
  1046. Else FindZeroReg := False;
  1047. End;*)
  1048. Function TCh2Reg(Ch: TChange): TRegister;
  1049. {converts a TChange variable to a TRegister}
  1050. Begin
  1051. If (Ch <= C_REDI) Then
  1052. TCh2Reg := TRegister(Byte(Ch))
  1053. Else
  1054. If (Ch <= C_WEDI) Then
  1055. TCh2Reg := TRegister(Byte(Ch) - Byte(C_REDI))
  1056. Else
  1057. If (Ch <= C_RWEDI) Then
  1058. TCh2Reg := TRegister(Byte(Ch) - Byte(C_WEDI))
  1059. Else InternalError($db)
  1060. End;
  1061. Procedure IncState(Var S: Byte);
  1062. {Increases S by 1, wraps around at $ffff to 0 (so we won't get overflow
  1063. errors}
  1064. Begin
  1065. If (s <> $ff)
  1066. Then Inc(s)
  1067. Else s := 0
  1068. End;
  1069. Function RegInSequence(Reg: TRegister; Const Content: TContent): Boolean;
  1070. {checks the whole sequence of Content (so StartMod and and the next NrOfMods
  1071. Pai objects) to see whether Reg is used somewhere, without it being loaded
  1072. with something else first}
  1073. Var p: Pai;
  1074. Counter: Byte;
  1075. TmpResult: Boolean;
  1076. RegsChecked: TRegSet;
  1077. Begin
  1078. RegsChecked := [];
  1079. p := Content.StartMod;
  1080. TmpResult := False;
  1081. Counter := 1;
  1082. While Not(TmpResult) And
  1083. (Counter <= Content.NrOfMods) Do
  1084. Begin
  1085. If (p^.typ = ait_instruction) and
  1086. ((Pai386(p)^._operator = A_MOV) or
  1087. (Pai386(p)^._operator = A_MOVZX) or
  1088. (Pai386(p)^._operator = A_MOVSX))
  1089. Then
  1090. If (Pai386(p)^.op1t = top_ref)
  1091. Then
  1092. With TReference(Pai386(p)^.op1^) Do
  1093. If (Base = ProcInfo.FramePointer) And
  1094. (Index = R_NO)
  1095. Then RegsChecked := RegsChecked + [Reg32(TRegister(Pai386(p)^.op2))]
  1096. Else
  1097. Begin
  1098. If (Base = Reg) And
  1099. Not(Base In RegsChecked)
  1100. Then TmpResult := True;
  1101. If Not(TmpResult) And
  1102. (Index = Reg) And
  1103. Not(Index In RegsChecked)
  1104. Then TmpResult := True;
  1105. End;
  1106. Inc(Counter);
  1107. GetNextInstruction(p,p)
  1108. End;
  1109. RegInSequence := TmpResult
  1110. End;
  1111. Procedure DestroyReg(p1: PPaiProp; Reg: TRegister);
  1112. {Destroys the contents of the register Reg in the PPaiProp p1, as well as the
  1113. contents of registers are loaded with a memory location based on Reg}
  1114. Var TmpWState, TmpRState: Byte;
  1115. Counter: TRegister;
  1116. Begin
  1117. Reg := Reg32(Reg);
  1118. NrOfInstrSinceLastMod[Reg] := 0;
  1119. If (Reg >= R_EAX) And (Reg <= R_EDI)
  1120. Then
  1121. Begin
  1122. With p1^.Regs[Reg] Do
  1123. Begin
  1124. IncState(WState);
  1125. TmpWState := WState;
  1126. TmpRState := RState;
  1127. FillChar(p1^.Regs[Reg], SizeOf(TContent), 0);
  1128. WState := TmpWState;
  1129. RState := TmpRState;
  1130. End;
  1131. For Counter := R_EAX to R_EDI Do
  1132. With p1^.Regs[Counter] Do
  1133. If (Typ = Con_Ref) And
  1134. RegInSequence(Reg, p1^.Regs[Counter])
  1135. Then
  1136. Begin
  1137. IncState(WState);
  1138. TmpWState := WState;
  1139. TmpRState := RState;
  1140. FillChar(p1^.Regs[Counter], SizeOf(TContent), 0);
  1141. WState := TmpWState;
  1142. RState := TmpRState;
  1143. End;
  1144. End;
  1145. End;
  1146. {Procedure AddRegsToSet(p: Pai; Var RegSet: TRegSet);
  1147. Begin
  1148. If (p^.typ = ait_instruction) Then
  1149. Begin
  1150. Case Pai386(p)^.op1t Of
  1151. top_reg:
  1152. If Not(TRegister(Pai386(p)^.op1) in [R_NO,R_ESP,ProcInfo.FramePointer]) Then
  1153. RegSet := RegSet + [TRegister(Pai386(p)^.op1)];
  1154. top_ref:
  1155. With TReference(Pai386(p)^.op1^) Do
  1156. Begin
  1157. If Not(Base in [ProcInfo.FramePointer,R_NO,R_ESP])
  1158. Then RegSet := RegSet + [Base];
  1159. If Not(Index in [ProcInfo.FramePointer,R_NO,R_ESP])
  1160. Then RegSet := RegSet + [Index];
  1161. End;
  1162. End;
  1163. Case Pai386(p)^.op2t Of
  1164. top_reg:
  1165. If Not(TRegister(Pai386(p)^.op2) in [R_NO,R_ESP,ProcInfo.FramePointer]) Then
  1166. If RegSet := RegSet + [TRegister(TwoWords(Pai386(p)^.op2).Word1];
  1167. top_ref:
  1168. With TReference(Pai386(p)^.op2^) Do
  1169. Begin
  1170. If Not(Base in [ProcInfo.FramePointer,R_NO,R_ESP])
  1171. Then RegSet := RegSet + [Base];
  1172. If Not(Index in [ProcInfo.FramePointer,R_NO,R_ESP])
  1173. Then RegSet := RegSet + [Index];
  1174. End;
  1175. End;
  1176. End;
  1177. End;}
  1178. Function OpsEquivalent(typ: Longint; OldOp, NewOp: Pointer; Var RegInfo: TRegInfo): Boolean;
  1179. Begin {checks whether the two ops are equivalent}
  1180. Case typ Of
  1181. Top_Reg: OpsEquivalent := RegsEquivalent(TRegister(OldOp), TRegister(NewOp), RegInfo);
  1182. Top_Const: OpsEquivalent := OldOp = NewOp;
  1183. Top_Ref: OpsEquivalent := RefsEquivalent(TReference(OldOp^), TReference(NewOp^), RegInfo);
  1184. Top_None: OpsEquivalent := True
  1185. Else OpsEquivalent := False
  1186. End;
  1187. End;
  1188. (*Function OpsEqual(typ: Longint; op1, op2: Pointer): Boolean;
  1189. Begin {checks whether the two ops are equal}
  1190. Case typ Of
  1191. Top_Reg, Top_Const: OpsEqual := op1 = op2;
  1192. Top_Ref: OpsEqual := RefsEqual(TReference(op1^), TReference(op2^));
  1193. Top_None: OpsEqual := True
  1194. Else OpsEqual := False
  1195. End;
  1196. End; *)
  1197. Function InstructionsEquivalent(p1, p2: Pai; Var RegInfo: TRegInfo): Boolean;
  1198. Begin {checks whether two Pai386 instructions are equal}
  1199. If Assigned(p1) And Assigned(p2) And
  1200. (Pai(p1)^.typ = ait_instruction) And
  1201. (Pai(p1)^.typ = ait_instruction) And
  1202. (Pai386(p1)^._operator = Pai386(p2)^._operator) And
  1203. (Pai386(p1)^.op1t = Pai386(p2)^.op1t) And
  1204. (Pai386(p1)^.op2t = Pai386(p2)^.op2t) And
  1205. (Pai386(p1)^.op3t = Pai386(p2)^.op3t)
  1206. Then
  1207. {both instructions have the same structure:
  1208. "<operator> <operand of type1>, <operand of type 2>"}
  1209. If ((Pai386(p1)^._operator = A_MOV) or
  1210. (Pai386(p1)^._operator = A_MOVZX) or
  1211. (Pai386(p1)^._operator = A_MOVSX)) And
  1212. (Pai386(p1)^.op1t = top_ref) {then op2t = top_reg} Then
  1213. If Not(RegInRef(TRegister(Pai386(p1)^.op2), TReference(Pai386(p1)^.op1^))) Then
  1214. {the "old" instruction is a load of a register with a new value, not with
  1215. a value based on the contents of this register (so no "mov (reg), reg")}
  1216. If Not(RegInRef(TRegister(Pai386(p2)^.op2), TReference(Pai386(p2)^.op1^))) And
  1217. RefsEqual(TReference(Pai386(p1)^.op1^), TReference(Pai386(p2)^.op1^))
  1218. Then
  1219. {the "new" instruction is also a load of a register with a new value, and
  1220. this value is fetched from the same memory location}
  1221. Begin
  1222. With TReference(Pai386(p2)^.op1^) Do
  1223. Begin
  1224. If Not(Base in [ProcInfo.FramePointer, R_NO, R_ESP])
  1225. {it won't do any harm if the register is already in RegsLoadedForRef}
  1226. Then RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef + [Base];
  1227. If Not(Index in [ProcInfo.FramePointer, R_NO, R_ESP])
  1228. Then RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef + [Index];
  1229. End;
  1230. {add the registers from the reference (op1) to the RegInfo, all registers
  1231. from the reference are the same in the old and in the new instruction
  1232. sequence}
  1233. AddOp2RegInfo(Pai386(p1)^.op1t, Pai386(p1)^.op1, RegInfo);
  1234. {the registers from op2 have to be equivalent, but not necessarily equal}
  1235. InstructionsEquivalent :=
  1236. RegsEquivalent(TRegister(Pai386(p1)^.op2), TRegister(Pai386(p2)^.op2),
  1237. RegInfo);
  1238. End
  1239. {the registers are loaded with values from different memory locations. If
  1240. this was allowed, the instructions "mov -4(esi),eax" and "mov -4(ebp),eax"
  1241. would be considered equivalent}
  1242. Else InstructionsEquivalent := False
  1243. Else
  1244. {load register with a value based on the current value of this register}
  1245. Begin
  1246. With TReference(Pai386(p2)^.op1^) Do
  1247. Begin
  1248. If Not(Base in [ProcInfo.FramePointer,
  1249. Reg32(TRegister(Pai386(p2)^.op2)),R_NO,R_ESP])
  1250. {it won't do any harm if the register is already in RegsLoadedForRef}
  1251. Then
  1252. {$ifdef csdebug}
  1253. Begin
  1254. {$endif csdebug}
  1255. RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef + [Base];
  1256. {$ifdef csdebug}
  1257. Writeln(att_reg2str[base], ' added');
  1258. end;
  1259. {$endif csdebug}
  1260. If Not(Index in [ProcInfo.FramePointer,
  1261. Reg32(TRegister(Pai386(p2)^.op2)),R_NO,R_ESP])
  1262. Then
  1263. {$ifdef csdebug}
  1264. Begin
  1265. {$endif csdebug}
  1266. RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef + [Index];
  1267. {$ifdef csdebug}
  1268. Writeln(att_reg2str[index], ' added');
  1269. end;
  1270. {$endif csdebug}
  1271. End;
  1272. If Not(Reg32(TRegister(Pai386(p2)^.op2)) In [ProcInfo.FramePointer,
  1273. R_NO,R_ESP])
  1274. Then
  1275. {$ifdef csdebug}
  1276. Begin
  1277. {$endif csdebug}
  1278. RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef -
  1279. [Reg32(TRegister(Pai386(p2)^.op2))];
  1280. {$ifdef csdebug}
  1281. Writeln(att_reg2str[Reg32(TRegister(Pai386(p2)^.op2))], ' removed');
  1282. end;
  1283. {$endif csdebug}
  1284. InstructionsEquivalent :=
  1285. OpsEquivalent(Pai386(p1)^.op1t, Pai386(p1)^.op1, Pai386(p2)^.op1, RegInfo) And
  1286. OpsEquivalent(Pai386(p1)^.op2t, Pai386(p1)^.op2, Pai386(p2)^.op2, RegInfo)
  1287. End
  1288. Else
  1289. {an instruction <> mov, movzx, movsx}
  1290. If (Pai386(p1)^.op3t = top_none) Then
  1291. InstructionsEquivalent :=
  1292. OpsEquivalent(Pai386(p1)^.op1t, Pai386(p1)^.op1, Pai386(p2)^.op1, RegInfo) And
  1293. OpsEquivalent(Pai386(p1)^.op2t, Pai386(p1)^.op2, Pai386(p2)^.op2, RegInfo)
  1294. Else
  1295. InstructionsEquivalent :=
  1296. OpsEquivalent(Pai386(p1)^.op1t, Pai386(p1)^.op1, Pai386(p2)^.op1, RegInfo) And
  1297. OpsEquivalent(Pai386(p1)^.op2t, Pointer(Longint(TwoWords(Pai386(p1)^.op2).Word1)),
  1298. Pointer(Longint(TwoWords(Pai386(p2)^.op2).Word1)), RegInfo) And
  1299. OpsEquivalent(Pai386(p1)^.op3t, Pointer(Longint(TwoWords(Pai386(p1)^.op2).Word2)),
  1300. Pointer(Longint(TwoWords(Pai386(p2)^.op2).Word2)), RegInfo)
  1301. {the instructions haven't even got the same structure, so they're certainly
  1302. not equivalent}
  1303. Else InstructionsEquivalent := False;
  1304. End;
  1305. (*
  1306. Function InstructionsEqual(p1, p2: Pai): Boolean;
  1307. Begin {checks whether two Pai386 instructions are equal}
  1308. InstructionsEqual :=
  1309. Assigned(p1) And Assigned(p2) And
  1310. ((Pai(p1)^.typ = ait_instruction) And
  1311. (Pai(p1)^.typ = ait_instruction) And
  1312. (Pai386(p1)^._operator = Pai386(p2)^._operator) And
  1313. (Pai386(p1)^.op1t = Pai386(p2)^.op1t) And
  1314. (Pai386(p1)^.op2t = Pai386(p2)^.op2t) And
  1315. OpsEqual(Pai386(p1)^.op1t, Pai386(p1)^.op1, Pai386(p2)^.op1) And
  1316. OpsEqual(Pai386(p1)^.op2t, Pai386(p1)^.op2, Pai386(p2)^.op2))
  1317. End;
  1318. *)
  1319. Function RefInInstruction(Const Ref: TReference; p: Pai): Boolean;
  1320. {checks whehter Ref is used in P}
  1321. Var TmpResult: Boolean;
  1322. Begin
  1323. TmpResult := False;
  1324. If (p^.typ = ait_instruction) Then
  1325. Begin
  1326. If (Pai386(p)^.op1t = Top_Ref)
  1327. Then TmpResult := RefsEqual(Ref, TReference(Pai386(p)^.op1^));
  1328. If Not(TmpResult) And
  1329. (Pai386(p)^.op2t = Top_Ref)
  1330. Then TmpResult := RefsEqual(Ref, TReference(Pai386(p)^.op2^));
  1331. End;
  1332. RefInInstruction := TmpResult;
  1333. End;
  1334. Function RefInSequence(Const Ref: TReference; Content: TContent): Boolean;
  1335. {checks the whole sequence of Content (so StartMod and and the next NrOfMods
  1336. Pai objects) to see whether Ref is used somewhere}
  1337. Var p: Pai;
  1338. Counter: Byte;
  1339. TmpResult: Boolean;
  1340. Begin
  1341. p := Content.StartMod;
  1342. TmpResult := False;
  1343. Counter := 1;
  1344. While Not(TmpResult) And
  1345. (Counter <= Content.NrOfMods) Do
  1346. Begin
  1347. If (p^.typ = ait_instruction) And
  1348. RefInInstruction(Ref, p)
  1349. Then TmpResult := True;
  1350. Inc(Counter);
  1351. GetNextInstruction(p,p)
  1352. End;
  1353. RefInSequence := TmpResult
  1354. End;
  1355. Procedure DestroyRefs(p: pai; Const Ref: TReference; WhichReg: TRegister);
  1356. {destroys all registers which possibly contain a reference to Ref, WhichReg
  1357. is the register whose contents are being written to memory (if this proc
  1358. is called because of a "mov?? %reg, (mem)" instruction)}
  1359. Var Counter: TRegister;
  1360. Begin
  1361. WhichReg := Reg32(WhichReg);
  1362. If ((Ref.base = ProcInfo.FramePointer) And
  1363. (Ref.Index = R_NO)) Or
  1364. Assigned(Ref.Symbol)
  1365. Then
  1366. {write something to a parameter, a local or global variable, so
  1367. * with uncertzain optimizations on:
  1368. - destroy the contents of registers whose contents have somewhere a
  1369. "mov?? (Ref), %reg". WhichReg (this is the register whose contents
  1370. are being written to memory) is not destroyed if it's StartMod is
  1371. of that form and NrOfMods = 1 (so if it holds ref, but is not a
  1372. pointer based on Ref)
  1373. * with uncertain optimizations off:
  1374. - also destroy registers that contain any pointer}
  1375. For Counter := R_EAX to R_EDI Do
  1376. With PPaiProp(p^.fileinfo.line)^.Regs[Counter] Do
  1377. Begin
  1378. If (typ = Con_Ref) And
  1379. (Not(cs_UncertainOpts in aktglobalswitches) And
  1380. (NrOfMods <> 1)
  1381. ) Or
  1382. (RefInSequence(Ref,PPaiProp(p^.fileinfo.line)^.Regs[Counter]) And
  1383. ((Counter <> WhichReg) Or
  1384. ((NrOfMods = 1) And
  1385. {StarMod is always of the type ait_instruction}
  1386. (Pai386(StartMod)^.op1t = top_ref) And
  1387. RefsEqual(TReference(Pai386(StartMod)^.op1^), Ref)
  1388. )
  1389. )
  1390. )
  1391. Then DestroyReg(PPaiProp(p^.fileinfo.line), Counter)
  1392. End
  1393. Else
  1394. {write something to a pointer location, so
  1395. * with uncertain optimzations on:
  1396. - do not destroy registers which contain a local/global variable or a
  1397. parameter, except if DestroyRefs is called because of a "movsl"
  1398. * with uncertain optimzations off:
  1399. - destroy every register which contains a memory location
  1400. }
  1401. For Counter := R_EAX to R_EDI Do
  1402. With PPaiProp(p^.fileinfo.line)^.Regs[Counter] Do
  1403. If (typ = Con_Ref) And
  1404. (Not(cs_UncertainOpts in aktglobalswitches) Or
  1405. {for movsl}
  1406. (Ref.Base = R_EDI) Or
  1407. {don't destroy if reg contains a parameter, local or global variable}
  1408. Not((NrOfMods = 1) And
  1409. (Pai386(StartMod)^.op1t = top_ref) And
  1410. ((PReference(Pai386(StartMod)^.op1)^.base = ProcInfo.FramePointer) Or
  1411. Assigned(PReference(Pai386(StartMod)^.op1)^.Symbol)
  1412. )
  1413. )
  1414. )
  1415. Then DestroyReg(PPaiProp(p^.FileInfo.Line), Counter)
  1416. End;
  1417. Procedure DestroyAllRegs(p: PPaiProp);
  1418. Var Counter: TRegister;
  1419. Begin {initializes/desrtoys all registers}
  1420. For Counter := R_EAX To R_EDI Do
  1421. DestroyReg(p, Counter);
  1422. p^.DirFlag := F_Unknown;
  1423. End;
  1424. Procedure Destroy(PaiObj: Pai; Opt: Longint; Op: Pointer);
  1425. Begin
  1426. Case Opt Of
  1427. top_reg: DestroyReg(PPaiProp(PaiObj^.fileinfo.line), TRegister(Op));
  1428. top_ref: DestroyRefs(PaiObj, TReference(Op^), R_NO);
  1429. top_symbol:;
  1430. End;
  1431. End;
  1432. Procedure ReadReg(p: PPaiProp; Reg: TRegister);
  1433. Begin
  1434. IncState(p^.Regs[Reg32(Reg)].RState)
  1435. End;
  1436. Procedure ReadRef(p: PPaiProp; Ref: PReference);
  1437. Begin
  1438. If Ref^.Base <> R_NO Then
  1439. ReadReg(p, Ref^.Base);
  1440. If Ref^.Index <> R_NO Then
  1441. ReadReg(p, Ref^.Index);
  1442. End;
  1443. Procedure ReadOp(P: PPaiProp; opt: Longint; Op: Pointer);
  1444. Begin
  1445. Case Opt Of
  1446. top_reg: ReadReg(P, TRegister(Op));
  1447. top_ref: ReadRef(P, PReference(Op));
  1448. top_symbol:
  1449. End;
  1450. End;
  1451. Procedure DFAPass1(AsmL: PAasmOutput);
  1452. {gathers the RegAlloc data... still need to think about where to store it}
  1453. Begin
  1454. FindLoHiLabels(AsmL, LoLab, HiLab, LabDif);
  1455. BuildLabelTableAndFixRegAlloc(AsmL, LTable, LoLab, LabDif);
  1456. End;
  1457. Function DoDFAPass2(
  1458. {$Ifdef StateDebug}
  1459. AsmL: PAasmOutput;
  1460. {$endif statedebug}
  1461. First: Pai): Pai;
  1462. {Analyzes the Data Flow of an assembler list. Starts creating the reg
  1463. contents for the instructions starting with p. Returns the last pai which has
  1464. been processed}
  1465. Var
  1466. CurProp: PPaiProp;
  1467. {$ifdef AnalyzeLoops}
  1468. TmpState: Byte;
  1469. {$endif AnalyzeLoops}
  1470. Cnt, InstrCnt : Longint;
  1471. InstrProp: TAsmInstrucProp;
  1472. UsedRegs: TRegSet;
  1473. p, hp : Pai;
  1474. TmpRef: TReference;
  1475. TmpReg: TRegister;
  1476. Begin
  1477. p := First;
  1478. UsedRegs := [];
  1479. UpdateUsedregs(UsedRegs, p);
  1480. If (First^.typ in SkipInstr) Then
  1481. GetNextInstruction(p, p);
  1482. First := p;
  1483. InstrCnt := 1;
  1484. FillChar(NrOfInstrSinceLastMod, SizeOf(NrOfInstrSinceLastMod), 0);
  1485. While Assigned(p) Do
  1486. Begin
  1487. DoDFAPass2 := p;
  1488. {$IfDef TP}
  1489. New(CurProp);
  1490. {$Else TP}
  1491. CurProp := @PaiPropBlock^[InstrCnt];
  1492. {$EndIf TP}
  1493. If (p <> First)
  1494. Then
  1495. Begin
  1496. {$ifdef JumpAnal}
  1497. If (p^.Typ <> ait_label) Then
  1498. {$endif JumpAnal}
  1499. Begin
  1500. GetLastInstruction(p, hp);
  1501. CurProp^.Regs := PPaiProp(hp^.fileinfo.line)^.Regs;
  1502. CurProp^.DirFlag := PPaiProp(hp^.fileinfo.line)^.DirFlag;
  1503. End
  1504. End
  1505. Else
  1506. Begin
  1507. FillChar(CurProp^, SizeOf(CurProp^), 0);
  1508. { For TmpReg := R_EAX to R_EDI Do
  1509. CurProp^.Regs[TmpReg].WState := 1;}
  1510. End;
  1511. CurProp^.UsedRegs := UsedRegs;
  1512. CurProp^.CanBeRemoved := False;
  1513. UpdateUsedRegs(UsedRegs, Pai(p^.Next));
  1514. {$ifdef TP}
  1515. CurProp^.linesave := p^.fileinfo.line;
  1516. PPaiProp(p^.fileinfo.line) := CurProp;
  1517. {$Endif TP}
  1518. For TmpReg := R_EAX To R_EDI Do
  1519. Inc(NrOfInstrSinceLastMod[TmpReg]);
  1520. Case p^.typ Of
  1521. ait_label:
  1522. {$Ifndef JumpAnal}
  1523. If (Pai_label(p)^.l^.is_used) Then
  1524. DestroyAllRegs(CurProp);
  1525. {$Else JumpAnal}
  1526. Begin
  1527. If (Pai_Label(p)^.is_used) Then
  1528. With LTable^[Pai_Label(p)^.l^.nb-LoLab] Do
  1529. {$IfDef AnalyzeLoops}
  1530. If (RefsFound = Pai_Label(p)^.l^.RefCount)
  1531. {$Else AnalyzeLoops}
  1532. If (JmpsProcessed = Pai_Label(p)^.l^.RefCount)
  1533. {$EndIf AnalyzeLoops}
  1534. Then
  1535. {all jumps to this label have been found}
  1536. {$IfDef AnalyzeLoops}
  1537. If (JmpsProcessed > 0)
  1538. Then
  1539. {$EndIf AnalyzeLoops}
  1540. {we've processed at least one jump to this label}
  1541. Begin
  1542. If (GetLastInstruction(p, hp) And
  1543. Not(((hp^.typ = ait_labeled_instruction) or
  1544. (hp^.typ = ait_instruction)) And
  1545. (Pai_Labeled(hp)^._operator = A_JMP))
  1546. Then
  1547. {previous instruction not a JMP -> the contents of the registers after the
  1548. previous intruction has been executed have to be taken into account as well}
  1549. For TmpReg := R_EAX to R_EDI Do
  1550. Begin
  1551. If (CurProp^.Regs[TmpReg].WState <>
  1552. PPaiProp(hp^.FileInfo.Line)^.Regs[TmpReg].WState)
  1553. Then DestroyReg(CurProp, TmpReg)
  1554. End
  1555. End
  1556. {$IfDef AnalyzeLoops}
  1557. Else
  1558. {a label from a backward jump (e.g. a loop), no jump to this label has
  1559. already been processed}
  1560. If GetLastInstruction(p, hp) And
  1561. Not(hp^.typ = ait_labeled_instruction) And
  1562. (Pai_Labeled(hp)^._operator = A_JMP))
  1563. Then
  1564. {previous instruction not a jmp, so keep all the registers' contents from the
  1565. previous instruction}
  1566. Begin
  1567. CurProp^.Regs := PPaiProp(hp^.FileInfo.Line)^.Regs;
  1568. CurProp^.DirFlag := PPaiProp(hp^.FileInfo.Line)^.DirFlag;
  1569. End
  1570. Else
  1571. {previous instruction a jmp and no jump to this label processed yet}
  1572. Begin
  1573. hp := p;
  1574. Cnt := InstrCnt;
  1575. {continue until we find a jump to the label or a label which has already
  1576. been processed}
  1577. While GetNextInstruction(hp, hp) And
  1578. Not((hp^.typ = ait_labeled_instruction) And
  1579. (Pai_Labeled(hp)^.lab^.nb = Pai_Label(p)^.l^.nb)) And
  1580. Not((hp^.typ = ait_label) And
  1581. (LTable^[Pai_Label(hp)^.l^.nb-LoLab].RefsFound
  1582. = Pai_Label(hp)^.l^.RefCount) And
  1583. (LTable^[Pai_Label(hp)^.l^.nb-LoLab].JmpsProcessed > 0)) Do
  1584. Inc(Cnt);
  1585. If (hp^.typ = ait_label)
  1586. Then
  1587. {there's a processed label after the current one}
  1588. Begin
  1589. CurProp^.Regs := PaiPropBlock^[Cnt].Regs;
  1590. CurProp^.DirFlag := PaiPropBlock^[Cnt].DirFlag;
  1591. End
  1592. Else
  1593. {there's no label anymore after the current one, or they haven't been
  1594. processed yet}
  1595. Begin
  1596. GetLastInstruction(p, hp);
  1597. CurProp^.Regs := PPaiProp(hp^.FileInfo.Line)^.Regs;
  1598. CurProp^.DirFlag := PPaiProp(hp^.FileInfo.Line)^.DirFlag;
  1599. DestroyAllRegs(PPaiProp(hp^.FileInfo.Line))
  1600. End
  1601. End
  1602. {$EndIf AnalyzeLoops}
  1603. Else
  1604. {not all references to this label have been found, so destroy all registers}
  1605. Begin
  1606. GetLastInstruction(p, hp);
  1607. CurProp^.Regs := PPaiProp(hp^.FileInfo.Line)^.Regs;
  1608. CurProp^.DirFlag := PPaiProp(hp^.FileInfo.Line)^.DirFlag;
  1609. DestroyAllRegs(CurProp)
  1610. End;
  1611. End;
  1612. {$EndIf JumpAnal}
  1613. ait_labeled_instruction:
  1614. {$IfNDef JumpAnal}
  1615. ;
  1616. {$Else JumpAnal}
  1617. With LTable^[Pai_Labeled(p)^.lab^.nb-LoLab] Do
  1618. If (RefsFound = Pai_Labeled(p)^.lab^.RefCount) Then
  1619. Begin
  1620. If (InstrCnt < InstrNr)
  1621. Then
  1622. {forward jump}
  1623. If (JmpsProcessed = 0) Then
  1624. {no jump to this label has been processed yet}
  1625. Begin
  1626. PaiPropBlock^[InstrNr].Regs := CurProp^.Regs;
  1627. PaiPropBlock^[InstrNr].DirFlag := CurProp^.DirFlag;
  1628. Inc(JmpsProcessed);
  1629. End
  1630. Else
  1631. Begin
  1632. For TmpReg := R_EAX to R_EDI Do
  1633. If (PaiPropBlock^[InstrNr].Regs[TmpReg].WState <>
  1634. CurProp^.Regs[TmpReg].WState) Then
  1635. DestroyReg(@PaiPropBlock^[InstrNr], TmpReg);
  1636. Inc(JmpsProcessed);
  1637. End
  1638. {$ifdef AnalyzeLoops}
  1639. Else
  1640. { backward jump, a loop for example}
  1641. { If (JmpsProcessed > 0) Or
  1642. Not(GetLastInstruction(PaiObj, hp) And
  1643. (hp^.typ = ait_labeled_instruction) And
  1644. (Pai_Labeled(hp)^._operator = A_JMP))
  1645. Then}
  1646. {instruction prior to label is not a jmp, or at least one jump to the label
  1647. has yet been processed}
  1648. Begin
  1649. Inc(JmpsProcessed);
  1650. For TmpReg := R_EAX to R_EDI Do
  1651. If (PaiPropBlock^[InstrNr].Regs[TmpReg].WState <>
  1652. CurProp^.Regs[TmpReg].WState)
  1653. Then
  1654. Begin
  1655. TmpState := PaiPropBlock^[InstrNr].Regs[TmpReg].WState;
  1656. Cnt := InstrNr;
  1657. While (TmpState = PaiPropBlock^[Cnt].Regs[TmpReg].WState) Do
  1658. Begin
  1659. DestroyReg(@PaiPropBlock^[Cnt], TmpReg);
  1660. Inc(Cnt);
  1661. End;
  1662. While (Cnt <= InstrCnt) Do
  1663. Begin
  1664. Inc(PaiPropBlock^[Cnt].Regs[TmpReg].WState);
  1665. Inc(Cnt)
  1666. End
  1667. End;
  1668. End
  1669. { Else }
  1670. {instruction prior to label is a jmp and no jumps to the label have yet been
  1671. processed}
  1672. { Begin
  1673. Inc(JmpsProcessed);
  1674. For TmpReg := R_EAX to R_EDI Do
  1675. Begin
  1676. TmpState := PaiPropBlock^[InstrNr].Regs[TmpReg].WState;
  1677. Cnt := InstrNr;
  1678. While (TmpState = PaiPropBlock^[Cnt].Regs[TmpReg].WState) Do
  1679. Begin
  1680. PaiPropBlock^[Cnt].Regs[TmpReg] := CurProp^.Regs[TmpReg];
  1681. Inc(Cnt);
  1682. End;
  1683. TmpState := PaiPropBlock^[InstrNr].Regs[TmpReg].WState;
  1684. While (TmpState = PaiPropBlock^[Cnt].Regs[TmpReg].WState) Do
  1685. Begin
  1686. DestroyReg(@PaiPropBlock^[Cnt], TmpReg);
  1687. Inc(Cnt);
  1688. End;
  1689. While (Cnt <= InstrCnt) Do
  1690. Begin
  1691. Inc(PaiPropBlock^[Cnt].Regs[TmpReg].WState);
  1692. Inc(Cnt)
  1693. End
  1694. End
  1695. End}
  1696. {$endif AnalyzeLoops}
  1697. End;
  1698. {$EndIf JumpAnal}
  1699. {$ifdef GDB}
  1700. ait_stabs, ait_stabn, ait_stab_function_name:;
  1701. {$endif GDB}
  1702. ait_instruction:
  1703. Begin
  1704. InstrProp := AsmInstr[Pai386(p)^._operator];
  1705. Case Pai386(p)^._operator Of
  1706. A_MOV, A_MOVZX, A_MOVSX:
  1707. Begin
  1708. Case Pai386(p)^.op1t Of
  1709. Top_Reg:
  1710. Case Pai386(p)^.op2t Of
  1711. Top_Reg:
  1712. Begin
  1713. DestroyReg(CurProp, TRegister(Pai386(p)^.op2));
  1714. ReadReg(CurProp, TRegister(Pai386(p)^.op1));
  1715. { CurProp^.Regs[TRegister(Pai386(p)^.op2)] :=
  1716. CurProp^.Regs[TRegister(Pai386(p)^.op1)];
  1717. If (CurProp^.Regs[TRegister(Pai386(p)^.op2)].ModReg = R_NO) Then
  1718. CurProp^.Regs[TRegister(Pai386(p)^.op2)].ModReg :=
  1719. Tregister(Pai386(p)^.op1);}
  1720. End;
  1721. Top_Ref:
  1722. Begin
  1723. ReadReg(CurProp, TRegister(Pai386(p)^.op1));
  1724. ReadRef(CurProp, PReference(Pai386(p)^.op2));
  1725. DestroyRefs(p, TReference(Pai386(p)^.op2^), TRegister(Pai386(p)^.op1));
  1726. End;
  1727. End;
  1728. Top_Ref:
  1729. Begin {destination is always a register in this case}
  1730. ReadRef(CurProp, PReference(Pai386(p)^.op1));
  1731. ReadReg(CurProp, TRegister(Pai386(p)^.Op2));
  1732. TmpReg := Reg32(TRegister(Pai386(p)^.op2));
  1733. If RegInRef(TmpReg, TReference(Pai386(p)^.op1^)) And
  1734. (CurProp^.Regs[TmpReg].Typ = Con_Ref)
  1735. Then
  1736. Begin
  1737. With CurProp^.Regs[TmpReg] Do
  1738. Begin
  1739. IncState(WState);
  1740. {also store how many instructions are part of the sequence in the first
  1741. instructions PPaiProp, so it can be easily accessed from within
  1742. CheckSequence}
  1743. Inc(NrOfMods, NrOfInstrSinceLastMod[TmpReg]);
  1744. PPaiProp(Pai(StartMod)^.fileinfo.line)^.Regs[TmpReg].NrOfMods := NrOfMods;
  1745. NrOfInstrSinceLastMod[TmpReg] := 0;
  1746. End;
  1747. End
  1748. Else
  1749. Begin
  1750. DestroyReg(CurProp, TmpReg);
  1751. If Not(RegInRef(TmpReg, TReference(Pai386(p)^.op1^))) Then
  1752. With CurProp^.Regs[TmpReg] Do
  1753. Begin
  1754. Typ := Con_Ref;
  1755. StartMod := p;
  1756. NrOfMods := 1;
  1757. End
  1758. End;
  1759. {$ifdef StateDebug}
  1760. hp := new(pai_asm_comment,init(strpnew(att_reg2str[TmpReg]+': '+tostr(CurProp^.Regs[TmpReg].WState))));
  1761. InsertLLItem(AsmL, p, p^.next, hp);
  1762. {$endif StateDebug}
  1763. End;
  1764. Top_Const:
  1765. Begin
  1766. Case Pai386(p)^.op2t Of
  1767. Top_Reg:
  1768. Begin
  1769. TmpReg := Reg32(TRegister(Pai386(p)^.op2));
  1770. With CurProp^.Regs[TmpReg] Do
  1771. Begin
  1772. DestroyReg(CurProp, TmpReg);
  1773. typ := Con_Const;
  1774. StartMod := Pai386(p)^.op1;
  1775. End
  1776. End;
  1777. Top_Ref:
  1778. Begin
  1779. ReadRef(CurProp, PReference(Pai386(p)^.op2));
  1780. DestroyRefs(P, TReference(Pai386(p)^.op2^), R_NO);
  1781. End;
  1782. End;
  1783. End;
  1784. End;
  1785. End;
  1786. A_IMUL:
  1787. Begin
  1788. ReadOp(CurProp, Pai386(p)^.Op1t, Pai386(p)^.Op1);
  1789. If (Pai386(p)^.Op2t = Top_Ref) Then
  1790. ReadOp(CurProp, Pai386(p)^.Op2t, Pai386(p)^.Op2)
  1791. Else ReadOp(CurProp, Pai386(p)^.Op2t, Pointer(Longint(TwoWords(Pai386(p)^.Op2).Word1)));
  1792. ReadOp(CurProp, Pai386(p)^.Op3t, Pointer(LongInt(TwoWords(Pai386(p)^.Op2).Word2)));
  1793. If (Pai386(p)^.Op3t = top_none)
  1794. Then
  1795. If (Pai386(p)^.Op2t = top_none)
  1796. Then
  1797. Begin
  1798. DestroyReg(CurProp, R_EAX);
  1799. DestroyReg(CurProp, R_EDX)
  1800. End
  1801. Else Destroy(p, Pai386(p)^.Op2t, Pai386(p)^.Op2)
  1802. Else DestroyReg(CurProp, TRegister(longint(twowords(Pai386(p)^.Op2).word2)));
  1803. End;
  1804. A_XOR:
  1805. Begin
  1806. ReadOp(CurProp, Pai386(p)^.Op1t, Pai386(p)^.Op1);
  1807. ReadOp(CurProp, Pai386(p)^.Op2t, Pai386(p)^.Op2);
  1808. If (Pai386(p)^.op1t = top_reg) And
  1809. (Pai386(p)^.op2t = top_reg) And
  1810. (Pai386(p)^.op1 = Pai386(p)^.op2)
  1811. Then
  1812. Begin
  1813. DestroyReg(CurProp, Tregister(Pai386(p)^.op1));
  1814. CurProp^.Regs[Reg32(Tregister(Pai386(p)^.op1))].typ := Con_Const;
  1815. CurProp^.Regs[Reg32(Tregister(Pai386(p)^.op1))].StartMod := Pointer(0)
  1816. End
  1817. Else Destroy(p, Pai386(p)^.op2t, Pai386(p)^.op2);
  1818. End
  1819. Else
  1820. Begin
  1821. Cnt := 1;
  1822. While (Cnt <= MaxCh) And
  1823. (InstrProp.Ch[Cnt] <> C_None) Do
  1824. Begin
  1825. Case InstrProp.Ch[Cnt] Of
  1826. C_REAX..C_REDI: ReadReg(CurProp,TCh2Reg(InstrProp.Ch[Cnt]));
  1827. C_WEAX..C_RWEDI:
  1828. Begin
  1829. If (InstrProp.Ch[Cnt] >= C_RWEAX) Then
  1830. ReadReg(CurProp, TCh2Reg(InstrProp.Ch[Cnt]));
  1831. DestroyReg(CurProp, TCh2Reg(InstrProp.Ch[Cnt]));
  1832. End;
  1833. C_CDirFlag: CurProp^.DirFlag := F_NotSet;
  1834. C_SDirFlag: CurProp^.DirFlag := F_Set;
  1835. C_ROp1: ReadOp(CurProp, Pai386(p)^.op1t, Pai386(p)^.op1);
  1836. C_ROp2: If (Pai386(p)^.Op3t = top_none) Then
  1837. ReadOp(CurProp, Pai386(p)^.op2t, Pai386(p)^.op2)
  1838. Else ReadOp(CurProp, Pai386(p)^.op2t, Pointer(Longint(TwoWords(Pai386(p)^.op2).word1)));
  1839. C_ROp3: ReadOp(CurProp, Pai386(p)^.op3t, Pointer(Longint(TwoWords(Pai386(p)^.op2).word2)));
  1840. C_WOp1..C_RWOp1:
  1841. Begin
  1842. If (InstrProp.Ch[Cnt] = C_RWOp1) Then
  1843. ReadOp(CurProp, Pai386(p)^.op1t, Pai386(p)^.op1);
  1844. Destroy(p, Pai386(p)^.op1t, Pai386(p)^.op1);
  1845. End;
  1846. C_WOp2..C_RWOp2:
  1847. Begin
  1848. If (InstrProp.Ch[Cnt] = C_RWOp2) Then
  1849. If (Pai386(p)^.Op3t = top_none) Then
  1850. ReadOp(CurProp, Pai386(p)^.op2t, Pai386(p)^.op2)
  1851. Else ReadOp(CurProp, Pai386(p)^.op2t, Pointer(Longint(TwoWords(Pai386(p)^.op2).word1)));
  1852. If (Pai386(p)^.Op3t = top_none) Then
  1853. Destroy(p, Pai386(p)^.op2t, Pai386(p)^.op2)
  1854. Else Destroy(p, Pai386(p)^.op2t, Pointer(Longint(TwoWords(Pai386(p)^.op2).word1)));
  1855. End;
  1856. C_WOp3..C_RWOp3:
  1857. Begin
  1858. If (InstrProp.Ch[Cnt] = C_RWOp3) Then
  1859. ReadOp(CurProp, Pai386(p)^.op3t, Pointer(Longint(TwoWords(Pai386(p)^.op2).word2)));
  1860. Destroy(p, Pai386(p)^.op3t, Pointer(Longint(TwoWords(Pai386(p)^.op2).word2)));
  1861. End;
  1862. C_WMemEDI:
  1863. Begin
  1864. ReadReg(CurProp, R_EDI);
  1865. FillChar(TmpRef, SizeOf(TmpRef), 0);
  1866. TmpRef.Base := R_EDI;
  1867. DestroyRefs(p, TmpRef, R_NO)
  1868. End;
  1869. C_RFlags, C_WFlags, C_RWFlags, C_FPU:
  1870. Else
  1871. Begin
  1872. DestroyAllRegs(CurProp);
  1873. End;
  1874. End;
  1875. Inc(Cnt);
  1876. End
  1877. End;
  1878. End;
  1879. End
  1880. Else
  1881. Begin
  1882. DestroyAllRegs(CurProp);
  1883. End;
  1884. End;
  1885. Inc(InstrCnt);
  1886. GetNextInstruction(p, p);
  1887. End;
  1888. End;
  1889. Function InitDFAPass2(AsmL: PAasmOutput): Boolean;
  1890. {reserves memory for the PPaiProps in one big memory block when not using
  1891. TP, returns False if not enough memory is available for the optimizer in all
  1892. cases}
  1893. Var p: Pai;
  1894. Count: Longint;
  1895. { TmpStr: String; }
  1896. Begin
  1897. P := Pai(AsmL^.First);
  1898. If (p^.typ in SkipInstr) Then
  1899. GetNextInstruction(p, p);
  1900. NrOfPaiObjs := 0;
  1901. While Assigned(P) Do
  1902. Begin
  1903. {$IfDef JumpAnal}
  1904. Case P^.Typ Of
  1905. ait_labeled_instruction:
  1906. begin
  1907. If (Pai_Labeled(P)^.lab^.nb >= LoLab) And
  1908. (Pai_Labeled(P)^.lab^.nb <= HiLab) Then
  1909. Inc(LTable^[Pai_Labeled(P)^.lab^.nb-LoLab].RefsFound);
  1910. end;
  1911. ait_label:
  1912. Begin
  1913. If (Pai_Label(p)^.l^.is_used) Then
  1914. LTable^[Pai_Label(P)^.l^.nb-LoLab].InstrNr := NrOfPaiObjs
  1915. End;
  1916. { ait_instruction:
  1917. Begin
  1918. If (Pai386(p)^._operator = A_PUSH) And
  1919. (Pai386(p)^.op1t = top_symbol) And
  1920. (PCSymbol(Pai386(p)^.op1)^.offset = 0) Then
  1921. Begin
  1922. TmpStr := StrPas(PCSymbol(Pai386(p)^.op1)^.symbol);
  1923. If}
  1924. End;
  1925. {$EndIf JumpAnal}
  1926. Inc(NrOfPaiObjs);
  1927. GetNextInstruction(p, p);
  1928. End;
  1929. {$IfDef TP}
  1930. If (MemAvail < (SizeOf(TPaiProp)*NrOfPaiObjs))
  1931. Or (NrOfPaiObjs = 0)
  1932. {this doesn't have to be one contiguous block}
  1933. Then InitDFAPass2 := False
  1934. Else InitDFAPass2 := True;
  1935. {$Else}
  1936. {Uncomment the next line to see how much memory the reloading optimizer needs}
  1937. { Writeln((NrOfPaiObjs*(((SizeOf(TPaiProp)+3)div 4)*4)));}
  1938. {no need to check mem/maxavail, we've got as much virtual memory as we want}
  1939. If NrOfPaiObjs <> 0 Then
  1940. Begin
  1941. InitDFAPass2 := True;
  1942. GetMem(PaiPropBlock, NrOfPaiObjs*(((SizeOf(TPaiProp)+3)div 4)*4));
  1943. p := Pai(AsmL^.First);
  1944. If (p^.typ in SkipInstr) Then
  1945. GetNextInstruction(p, p);
  1946. For Count := 1 To NrOfPaiObjs Do
  1947. Begin
  1948. PaiPropBlock^[Count].LineSave := p^.fileinfo.line;
  1949. PPaiProp(p^.fileinfo.line) := @PaiPropBlock^[Count];
  1950. GetNextInstruction(p, p);
  1951. End;
  1952. End
  1953. Else InitDFAPass2 := False;
  1954. {$EndIf TP}
  1955. End;
  1956. Function DFAPass2(AsmL: PAasmOutPut): Pai;
  1957. Begin
  1958. If InitDFAPass2(AsmL)
  1959. Then DFAPass2 := DoDFAPass2(
  1960. {$ifdef statedebug}
  1961. asml,
  1962. {$endif statedebug}
  1963. Pai(AsmL^.First))
  1964. Else DFAPass2 := Nil;
  1965. End;
  1966. Procedure ShutDownDFA;
  1967. Begin
  1968. If LabDif <> 0 Then
  1969. FreeMem(LTable, LabDif*SizeOf(TLabelTableItem));
  1970. End;
  1971. End.
  1972. {
  1973. $Log$
  1974. Revision 1.31 1998-12-11 00:03:13 peter
  1975. + globtype,tokens,version unit splitted from globals
  1976. Revision 1.30 1998/12/02 16:23:39 jonas
  1977. * changed "if longintvar in set" to case or "if () or () .." statements
  1978. * tree.pas: changed inlinenumber (and associated constructor/vars) to a byte
  1979. Revision 1.29 1998/11/26 21:45:31 jonas
  1980. - removed A_CLTD opcode (use A_CDQ instead)
  1981. * changed cbw, cwde and cwd to cbtw, cwtl and cwtd in att_op2str array
  1982. * in daopt386: adapted AsmInstr array to reflect changes + fixed line too long
  1983. Revision 1.27 1998/11/24 19:47:22 jonas
  1984. * fixed problems posible with 3 operand instructions
  1985. Revision 1.26 1998/11/24 12:50:09 peter
  1986. * fixed crash
  1987. Revision 1.25 1998/11/18 17:58:22 jonas
  1988. + gathering of register reading data, nowhere used yet (necessary for instruction scheduling)
  1989. Revision 1.24 1998/11/13 10:13:44 peter
  1990. + cpuid,emms support for asm readers
  1991. Revision 1.23 1998/11/09 19:40:46 jonas
  1992. * fixed comments from last commit (apparently there's still a 255 char limit :( )
  1993. Revision 1.22 1998/11/09 19:33:40 jonas
  1994. * changed specific bugfix (which was actually wrong implemented, but
  1995. did the right thing in most cases nevertheless) to general bugfix
  1996. * fixed bug that caused
  1997. mov (ebp), edx mov (ebp), edx
  1998. mov (edx), edx mov (edx), edx
  1999. ... being changed to ...
  2000. mov (ebp), edx mov edx, eax
  2001. mov (eax), eax
  2002. but this disabled another small correct optimization...
  2003. Revision 1.21 1998/11/02 23:17:49 jonas
  2004. * fixed bug shown in sortbug program from fpc-devel list
  2005. Revision 1.20 1998/10/22 13:24:51 jonas
  2006. * changed TRegSet to a small set
  2007. Revision 1.19 1998/10/20 09:29:24 peter
  2008. * bugfix so that code like
  2009. movl 48(%esi),%esi movl 48(%esi),%esi
  2010. pushl %esi doesn't get changed to pushl %esi
  2011. movl 48(%esi),%edi movl %esi,%edi
  2012. Revision 1.18 1998/10/07 16:27:02 jonas
  2013. * changed state to WState (WriteState), added RState for future use in
  2014. instruction scheduling
  2015. * RegAlloc data from the CG is now completely being patched and corrected (I
  2016. think)
  2017. Revision 1.17 1998/10/02 17:30:20 jonas
  2018. * small patches to regdealloc data
  2019. Revision 1.16 1998/10/01 20:21:47 jonas
  2020. * inter-register CSE, still requires some tweaks (peepholeoptpass2, better RegAlloc)
  2021. Revision 1.15 1998/09/20 18:00:20 florian
  2022. * small compiling problems fixed
  2023. Revision 1.14 1998/09/20 17:12:36 jonas
  2024. * small fix for uncertain optimizations & more cleaning up
  2025. Revision 1.12 1998/09/16 18:00:01 jonas
  2026. * optimizer now completely dependant on GetNext/GetLast instruction, works again with -dRegAlloc
  2027. Revision 1.11 1998/09/15 14:05:27 jonas
  2028. * fixed optimizer incompatibilities with freelabel code in psub
  2029. Revision 1.10 1998/09/09 15:33:58 peter
  2030. * removed warnings
  2031. Revision 1.9 1998/09/03 16:24:51 florian
  2032. * bug of type conversation from dword to real fixed
  2033. * bug fix of Jonas applied
  2034. Revision 1.8 1998/08/28 10:56:59 peter
  2035. * removed warnings
  2036. Revision 1.7 1998/08/19 16:07:44 jonas
  2037. * changed optimizer switches + cleanup of DestroyRefs in daopt386.pas
  2038. Revision 1.6 1998/08/10 14:49:57 peter
  2039. + localswitches, moduleswitches, globalswitches splitting
  2040. Revision 1.5 1998/08/09 13:56:24 jonas
  2041. * small bugfix for uncertain optimizations in DestroyRefs
  2042. Revision 1.4 1998/08/06 19:40:25 jonas
  2043. * removed $ before and after Log in comment
  2044. Revision 1.3 1998/08/05 16:00:14 florian
  2045. * some fixes for ansi strings
  2046. * log to Log changed
  2047. }