daopt386.pas 83 KB

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