daopt386.pas 82 KB

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