daopt386.pas 78 KB

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