daopt386.pas 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269
  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,
  27. i386base,i386asm
  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. New2OldReg: TRegArray;
  36. End;
  37. {possible actions on an operand: read, write or modify (= read & write)}
  38. TOpAction = (OpAct_Read, OpAct_Write, OpAct_Modify, OpAct_Unknown);
  39. {*********************** Procedures and Functions ************************}
  40. Procedure InsertLLItem(AsmL: PAasmOutput; prev, foll, new_one: PLinkedList_Item);
  41. Function Reg32(Reg: TRegister): TRegister;
  42. Function RefsEquivalent(Const R1, R2: TReference; Var RegInfo: TRegInfo; OpAct: TOpAction): Boolean;
  43. Function RefsEqual(Const R1, R2: TReference): Boolean;
  44. Function IsGP32Reg(Reg: TRegister): Boolean;
  45. Function RegInRef(Reg: TRegister; Const Ref: TReference): Boolean;
  46. Function RegInInstruction(Reg: TRegister; p1: Pai): Boolean;
  47. Function RegModifiedByInstruction(Reg: TRegister; p1: Pai): Boolean;
  48. Function GetNextInstruction(Current: Pai; Var Next: Pai): Boolean;
  49. Function GetLastInstruction(Current: Pai; Var Last: Pai): Boolean;
  50. Procedure SkipHead(var P: Pai);
  51. Procedure UpdateUsedRegs(Var UsedRegs: TRegSet; p: Pai);
  52. Function RegsEquivalent(OldReg, NewReg: TRegister; Var RegInfo: TRegInfo; OpAct: TopAction): Boolean;
  53. Function InstructionsEquivalent(p1, p2: Pai; Var RegInfo: TRegInfo): Boolean;
  54. Function OpsEqual(const o1,o2:toper): Boolean;
  55. Function DFAPass1(AsmL: PAasmOutput; BlockStart: Pai): Pai;
  56. Function DFAPass2(
  57. {$ifdef statedebug}
  58. AsmL: PAasmOutPut;
  59. {$endif statedebug}
  60. BlockStart, BlockEnd: Pai): Boolean;
  61. Procedure ShutDownDFA;
  62. Function FindLabel(L: PasmLabel; Var hp: Pai): Boolean;
  63. {******************************* Constants *******************************}
  64. Const
  65. {ait_* types which don't result in executable code or which don't influence
  66. the way the program runs/behaves}
  67. SkipInstr = [ait_comment, ait_align, ait_symbol
  68. {$ifdef GDB}
  69. ,ait_stabs, ait_stabn, ait_stab_function_name
  70. {$endif GDB}
  71. ,ait_regalloc, ait_tempalloc
  72. ];
  73. {the maximum number of things (registers, memory, ...) a single instruction
  74. changes}
  75. MaxCh = 3;
  76. {Possible register content types}
  77. con_Unknown = 0;
  78. con_ref = 1;
  79. con_const = 2;
  80. {********************************* Types *********************************}
  81. Type
  82. {What an instruction can change}
  83. TChange = (C_None,
  84. {Read from a register}
  85. C_REAX, C_RECX, C_REDX, C_REBX, C_RESP, C_REBP, C_RESI, C_REDI,
  86. {write from a register}
  87. C_WEAX, C_WECX, C_WEDX, C_WEBX, C_WESP, C_WEBP, C_WESI, C_WEDI,
  88. {read and write from/to a register}
  89. C_RWEAX, C_RWECX, C_RWEDX, C_RWEBX, C_RWESP, C_RWEBP, C_RWESI, C_RWEDI,
  90. C_CDirFlag {clear direction flag}, C_SDirFlag {set dir flag},
  91. C_RFlags, C_WFlags, C_RWFlags, C_FPU,
  92. C_Rop1, C_Wop1, C_RWop1,
  93. C_Rop2, C_Wop2, C_RWop2,
  94. C_Rop3, C_WOp3, C_RWOp3,
  95. C_WMemEDI,
  96. C_All);
  97. {the possible states of a flag}
  98. TFlagContents = (F_Unknown, F_NotSet, F_Set);
  99. {the properties of a cpu instruction}
  100. TAsmInstrucProp = Record
  101. {how many things it changes}
  102. { NCh: Byte;}
  103. {and what it changes}
  104. Ch: Array[1..MaxCh] of TChange;
  105. End;
  106. TContent = Packed Record
  107. {start and end of block instructions that defines the
  108. content of this register. If Typ = con_const, then
  109. Longint(StartMod) = value of the constant)}
  110. StartMod: pai;
  111. {starts at 0, gets increased everytime the register is written to}
  112. WState: Byte;
  113. {starts at 0, gets increased everytime the register is read from}
  114. RState: Byte;
  115. {how many instructions starting with StarMod does the block consist of}
  116. NrOfMods: Byte;
  117. {the type of the content of the register: unknown, memory, constant}
  118. Typ: Byte;
  119. End;
  120. {Contents of the integer registers}
  121. TRegContent = Array[R_EAX..R_EDI] Of TContent;
  122. {contents of the FPU registers}
  123. TRegFPUContent = Array[R_ST..R_ST7] Of TContent;
  124. {information record with the contents of every register. Every Pai object
  125. gets one of these assigned: a pointer to it is stored in the OptInfo field}
  126. TPaiProp = Record
  127. Regs: TRegContent;
  128. { FPURegs: TRegFPUContent;} {currently not yet used}
  129. {allocated Registers}
  130. UsedRegs: TRegSet;
  131. {status of the direction flag}
  132. DirFlag: TFlagContents;
  133. {can this instruction be removed?}
  134. CanBeRemoved: Boolean;
  135. End;
  136. PPaiProp = ^TPaiProp;
  137. {$IfNDef TP}
  138. TPaiPropBlock = Array[1..250000] Of TPaiProp;
  139. PPaiPropBlock = ^TPaiPropBlock;
  140. {$EndIf TP}
  141. TInstrSinceLastMod = Array[R_EAX..R_EDI] Of Byte;
  142. TLabelTableItem = Record
  143. PaiObj: Pai;
  144. {$IfDef JumpAnal}
  145. InstrNr: Longint;
  146. RefsFound: Word;
  147. JmpsProcessed: Word
  148. {$EndIf JumpAnal}
  149. End;
  150. {$IfDef tp}
  151. TLabelTable = Array[0..10000] Of TLabelTableItem;
  152. {$Else tp}
  153. TLabelTable = Array[0..2500000] Of TLabelTableItem;
  154. {$Endif tp}
  155. PLabelTable = ^TLabelTable;
  156. {******************************* Variables *******************************}
  157. Var
  158. {the amount of PaiObjects in the current assembler list}
  159. NrOfPaiObjs: Longint;
  160. {$IfNDef TP}
  161. {Array which holds all TPaiProps}
  162. PaiPropBlock: PPaiPropBlock;
  163. {$EndIf TP}
  164. LoLab, HiLab, LabDif: Longint;
  165. LTable: PLabelTable;
  166. {*********************** End of Interface section ************************}
  167. Implementation
  168. Uses
  169. globals, systems, strings, verbose, hcodegen;
  170. Const AsmInstr: Array[tasmop] Of TAsmInstrucProp = (
  171. {A_<NONE>} (Ch: (C_All, C_None, C_None)), { new }
  172. {A_LOCK} (Ch: (C_None, C_None, C_None)),
  173. {A_REP} (Ch: (C_RWECX, C_RFlags, C_None)),
  174. {A_REPE} (Ch: (C_RWECX, C_RFlags, C_None)),
  175. {A_REPNE} (Ch: (C_RWECX, C_RFlags, C_None)),
  176. {A_REPNZ} (Ch: (C_WECX, C_RWFLAGS, C_None)), { new }
  177. {A_REPZ} (Ch: (C_WECX, C_RWFLAGS, C_None)), { new }
  178. {A_SEGCS} (Ch: (C_None, C_None, C_None)), { new }
  179. {A_SEGES} (Ch: (C_None, C_None, C_None)), { new }
  180. {A_SEGDS} (Ch: (C_None, C_None, C_None)), { new }
  181. {A_SEGFS} (Ch: (C_None, C_None, C_None)), { new }
  182. {A_SEGGS} (Ch: (C_None, C_None, C_None)), { new }
  183. {A_SEGSS} (Ch: (C_None, C_None, C_None)), { new }
  184. {A_AAA} (Ch: (C_RWEAX, C_WFlags, C_None)),
  185. {A_AAD} (Ch: (C_RWEAX, C_WFlags, C_None)),
  186. {A_AAM} (Ch: (C_RWEAX, C_WFlags, C_None)),
  187. {A_AAS} (Ch: (C_RWEAX, C_WFlags, C_None)),
  188. {A_ADC} (Ch: (C_RWop2, C_Rop1, C_RWFlags)),
  189. {A_ADD} (Ch: (C_RWop2, C_Rop1, C_WFlags)),
  190. {A_AND} (Ch: (C_RWop2, C_Rop1, C_WFlags)),
  191. {A_ARPL} (Ch: (C_WFlags, C_None, C_None)),
  192. {A_BOUND} (Ch: (C_Rop1, C_None, C_None)),
  193. {A_BSF} (Ch: (C_Wop2, C_WFlags, C_Rop1)),
  194. {A_BSR} (Ch: (C_Wop2, C_WFlags, C_Rop1)),
  195. {A_BSWAP} (Ch: (C_RWOp1, C_None, C_None)), { new }
  196. {A_BT} (Ch: (C_WFlags, C_Rop1, C_None)),
  197. {A_BTC} (Ch: (C_RWop2, C_Rop1, C_WFlags)),
  198. {A_BTR} (Ch: (C_RWop2, C_Rop1, C_WFlags)),
  199. {A_BTS} (Ch: (C_RWop2, C_Rop1, C_WFlags)),
  200. {A_CALL} (Ch: (C_All, C_None, C_None)), {don't know value of any register}
  201. {A_CBW} (Ch: (C_RWEAX, C_None, C_None)),
  202. {A_CDQ} (Ch: (C_RWEAX, C_WEDX, C_None)),
  203. {A_CLC} (Ch: (C_WFlags, C_None, C_None)),
  204. {A_CLD} (Ch: (C_CDirFlag, C_None, C_None)),
  205. {A_CLI} (Ch: (C_WFlags, C_None, C_None)),
  206. {A_CLTS} (Ch: (C_None, C_None, C_None)),
  207. {A_CMC} (Ch: (C_WFlags, C_None, C_None)),
  208. {A_CMP} (Ch: (C_WFlags, C_None, C_None)),
  209. {A_CMPSB} (Ch: (C_All, C_None, C_None)), { new }
  210. {A_CMPSD} (Ch: (C_All, C_None, C_None)), { new }
  211. {A_CMPSW} (Ch: (C_All, C_None, C_None)), { new }
  212. {A_CMPXCHG} (Ch: (C_All, C_None, C_None)), { new }
  213. {A_CMPXCHG486} (Ch: (C_All, C_None, C_None)), { new }
  214. {A_CMPXCHG8B} (Ch: (C_All, C_None, C_None)), { new }
  215. {A_CPUID} (Ch: (C_All, C_None, C_none)),
  216. {A_CWD} (Ch: (C_RWEAX, C_WEDX, C_None)),
  217. {A_CWDE} (Ch: (C_RWEAX, C_None, C_None)),
  218. {A_DAA} (Ch: (C_RWEAX, C_None, C_None)),
  219. {A_DAS} (Ch: (C_RWEAX, C_None, C_None)),
  220. {A_DEC} (Ch: (C_RWop1, C_WFlags, C_None)),
  221. {A_DIV} (Ch: (C_RWEAX, C_WEDX, C_WFlags)),
  222. {A_EMMS} (Ch: (C_FPU, C_None, C_None)), { new }
  223. {A_ENTER} (Ch: (C_RWESP, C_None, C_None)),
  224. {A_EQU} (Ch: (C_None, C_None, C_None)), { new }
  225. {A_F2XM1} (Ch: (C_FPU, C_None, C_None)),
  226. {A_FABS} (Ch: (C_FPU, C_None, C_None)),
  227. {A_FADD} (Ch: (C_FPU, C_None, C_None)),
  228. {A_FADDP} (Ch: (C_FPU, C_None, C_None)),
  229. {A_FBLD} (Ch: (C_Rop1, C_FPU, C_None)),
  230. {A_FBSTP} (Ch: (C_Wop1, C_FPU, C_None)),
  231. {A_FCHS} (Ch: (C_FPU, C_None, C_None)),
  232. {A_FCLEX} (Ch: (C_FPU, C_None, C_None)),
  233. {A_FCMOVB} (Ch: (C_FPU, C_RFLAGS, C_None)), { new }
  234. {A_FCMOVBE} (Ch: (C_FPU, C_RFLAGS, C_None)), { new }
  235. {A_FCMOVE} (Ch: (C_FPU, C_RFLAGS, C_None)), { new }
  236. {A_FCMOVNB} (Ch: (C_FPU, C_RFLAGS, C_None)), { new }
  237. {A_FCMOVNBE} (Ch: (C_FPU, C_RFLAGS, C_None)), { new }
  238. {A_FCMOVNE} (Ch: (C_FPU, C_RFLAGS, C_None)), { new }
  239. {A_FCMOVNU} (Ch: (C_FPU, C_RFLAGS, C_None)), { new }
  240. {A_FCMOVU} (Ch: (C_FPU, C_RFLAGS, C_None)), { new }
  241. {A_FCOM} (Ch: (C_FPU, C_None, C_None)),
  242. {A_FCOMI} (Ch: (C_WFLAGS, C_None, C_None)), { new }
  243. {A_FCOMIP} (Ch: (C_FPU, C_WFLAGS, C_None)), { new }
  244. {A_FCOMP} (Ch: (C_FPU, C_None, C_None)),
  245. {A_FCOMPP} (Ch: (C_FPU, C_None, C_None)),
  246. {A_FCOS} (Ch: (C_FPU, C_None, C_None)),
  247. {A_FDECSTP} (Ch: (C_FPU, C_None, C_None)),
  248. {A_FDISI} (Ch: (C_FPU, C_None, C_None)),
  249. {A_FDIV} (Ch: (C_FPU, C_None, C_None)),
  250. {A_FDIVP} (Ch: (C_FPU, C_None, C_None)),
  251. {A_FDIVR} (Ch: (C_FPU, C_None, C_None)),
  252. {A_FDIVRP} (Ch: (C_FPU, C_None, C_None)),
  253. {A_FEMMS} (Ch: (C_All, C_None, C_None)), { new }
  254. {A_FENI} (Ch: (C_FPU, C_None, C_None)),
  255. {A_FFREE} (Ch: (C_FPU, C_None, C_None)),
  256. {A_FIADD} (Ch: (C_FPU, C_None, C_None)),
  257. {A_FICOM} (Ch: (C_FPU, C_None, C_None)),
  258. {A_FICOMP} (Ch: (C_FPU, C_None, C_None)),
  259. {A_FIDIV} (Ch: (C_FPU, C_None, C_None)),
  260. {A_FIDIVR} (Ch: (C_FPU, C_None, C_None)),
  261. {A_FILD} (Ch: (C_FPU, C_None, C_None)),
  262. {A_FIMUL} (Ch: (C_FPU, C_None, C_None)),
  263. {A_FINCSTP} (Ch: (C_FPU, C_None, C_None)),
  264. {A_FINIT} (Ch: (C_FPU, C_None, C_None)),
  265. {A_FIST} (Ch: (C_Wop1, C_None, C_None)),
  266. {A_FISTP} (Ch: (C_Wop1, C_None, C_None)),
  267. {A_FISUB} (Ch: (C_FPU, C_None, C_None)),
  268. {A_FISUBR} (Ch: (C_FPU, C_None, C_None)), { new }
  269. {A_FLD} (Ch: (C_Rop1, C_FPU, C_None)),
  270. {A_FLD1} (Ch: (C_FPU, C_None, C_None)),
  271. {A_FLDCW} (Ch: (C_FPU, C_None, C_None)),
  272. {A_FLDENV} (Ch: (C_FPU, C_None, C_None)),
  273. {A_FLDL2E} (Ch: (C_FPU, C_None, C_None)),
  274. {A_FLDL2T} (Ch: (C_FPU, C_None, C_None)),
  275. {A_FLDLG2} (Ch: (C_FPU, C_None, C_None)),
  276. {A_FLDLN2} (Ch: (C_FPU, C_None, C_None)),
  277. {A_FLDPI} (Ch: (C_FPU, C_None, C_None)),
  278. {A_FLDZ} (Ch: (C_FPU, C_None, C_None)),
  279. {A_FMUL} (Ch: (C_FPU, C_None, C_None)),
  280. {A_FMULP} (Ch: (C_FPU, C_None, C_None)),
  281. {A_FNCLEX} (Ch: (C_FPU, C_None, C_None)),
  282. {A_FNDISI} (Ch: (C_FPU, C_None, C_None)),
  283. {A_FNENI} (Ch: (C_FPU, C_None, C_None)),
  284. {A_FNINIT} (Ch: (C_FPU, C_None, C_None)),
  285. {A_FNOP} (Ch: (C_FPU, C_None, C_None)),
  286. {A_FNSAVE} (Ch: (C_FPU, C_None, C_None)),
  287. {A_FNSTCW} (Ch: (C_Wop1, C_None, C_None)),
  288. {A_FNSTENV} (Ch: (C_Wop1, C_None, C_None)),
  289. {A_FNSTSW} (Ch: (C_Wop1, C_None, C_None)),
  290. {A_FPATAN} (Ch: (C_FPU, C_None, C_None)),
  291. {A_FPREM} (Ch: (C_FPU, C_None, C_None)),
  292. {A_FPREM1} (Ch: (C_FPU, C_None, C_None)),
  293. {A_FPTAN} (Ch: (C_FPU, C_None, C_None)),
  294. {A_FRNDINT} (Ch: (C_FPU, C_None, C_None)),
  295. {A_FRSTOR} (Ch: (C_FPU, C_None, C_None)),
  296. {A_FSAVE} (Ch: (C_Wop1, C_None, C_None)),
  297. {A_FSCALE} (Ch: (C_FPU, C_None, C_None)),
  298. {A_FSETPM} (Ch: (C_FPU, C_None, C_None)),
  299. {A_FSIN} (Ch: (C_FPU, C_None, C_None)),
  300. {A_FSINCOS} (Ch: (C_FPU, C_None, C_None)),
  301. {A_FSQRT} (Ch: (C_FPU, C_None, C_None)),
  302. {A_FST} (Ch: (C_Wop1, C_None, C_None)),
  303. {A_FSTCW} (Ch: (C_Wop1, C_None, C_None)),
  304. {A_FSTENV} (Ch: (C_Wop1, C_None, C_None)),
  305. {A_FSTP} (Ch: (C_Wop1, C_FPU, C_None)),
  306. {A_FSTSW} (Ch: (C_Wop1, C_None, C_None)),
  307. {A_FSUB} (Ch: (C_FPU, C_None, C_None)),
  308. {A_FSUBP} (Ch: (C_FPU, C_None, C_None)),
  309. {A_FSUBR} (Ch: (C_FPU, C_None, C_None)),
  310. {A_FSUBRP} (Ch: (C_FPU, C_None, C_None)),
  311. {A_FTST} (Ch: (C_FPU, C_None, C_None)),
  312. {A_FUCOM} (Ch: (C_None, C_None, C_None)), {changes fpu status word}
  313. {A_FUCOMI} (Ch: (C_WFLAGS, C_None, C_None)), { new }
  314. {A_FUCOMIP} (Ch: (C_FPU, C_WFLAGS, C_None)), { new }
  315. {A_FUCOMP} (Ch: (C_FPU, C_None, C_None)),
  316. {A_FUCOMPP} (Ch: (C_FPU, C_None, C_None)),
  317. {A_FWAIT} (Ch: (C_FPU, C_None, C_None)),
  318. {A_FXAM} (Ch: (C_FPU, C_None, C_None)),
  319. {A_FXCH} (Ch: (C_FPU, C_None, C_None)),
  320. {A_FXTRACT} (Ch: (C_FPU, C_None, C_None)),
  321. {A_FYL2X} (Ch: (C_FPU, C_None, C_None)),
  322. {A_FYL2XP1} (Ch: (C_FPU, C_None, C_None)),
  323. {A_HLT} (Ch: (C_None, C_None, C_None)),
  324. {A_IBTS} (Ch: (C_All, C_None, C_None)), { new }
  325. {A_ICEBP} (Ch: (C_All, C_None, C_None)), { new }
  326. {A_IDIV} (Ch: (C_RWEAX, C_WEDX, C_WFlags)),
  327. {A_IMUL} (Ch: (C_RWEAX, C_WEDX, C_WFlags)), {handled separately, because several forms exist}
  328. {A_IN} (Ch: (C_Wop2, C_Rop1, C_None)),
  329. {A_INC} (Ch: (C_RWop1, C_WFlags, C_None)),
  330. {A_INSB} (Ch: (C_WMemEDI, C_RWEDI, C_None)), { new }
  331. {A_INSD} (Ch: (C_WMemEDI, C_RWEDI, C_None)), { new }
  332. {A_INSW} (Ch: (C_WMemEDI, C_RWEDI, C_None)), { new }
  333. {A_INT} (Ch: (C_All, C_None, C_None)), {don't know value of any register}
  334. {A_INT01} (Ch: (C_All, C_None, C_None)), { new }
  335. {A_INT1} (Ch: (C_All, C_None, C_None)), { new }
  336. {A_INT3} (Ch: (C_None, C_None, C_None)),
  337. {A_INTO} (Ch: (C_All, C_None, C_None)), {don't know value of any register}
  338. {A_INVD} (Ch: (C_All, C_None, C_None)), { new }
  339. {A_INVLPG} (Ch: (C_All, C_None, C_None)), { new }
  340. {A_IRET} (Ch: (C_All, C_None, C_None)), {don't know value of any register}
  341. {A_IRETD} (Ch: (C_All, C_None, C_None)), { new }
  342. {A_IRETW} (Ch: (C_All, C_None, C_None)), { new }
  343. {A_JCXZ} (Ch: (C_RECX, C_None, C_None)),
  344. {A_JECXZ} (Ch: (C_RECX, C_None, C_None)),
  345. {A_JMP} (Ch: (C_None, C_None, C_None)),
  346. {A_LAHF} (Ch: (C_WEAX, C_RFlags, C_None)),
  347. {A_LAR} (Ch: (C_Wop2, C_None, C_None)),
  348. {A_LDS} (Ch: (C_Wop2, C_None, C_None)),
  349. {A_LEA} (Ch: (C_Wop2, C_Rop1, C_None)),
  350. {A_LEAVE} (Ch: (C_RWESP, C_None, C_None)),
  351. {A_LES} (Ch: (C_Wop2, C_None, C_None)),
  352. {A_LFS} (Ch: (C_Wop2, C_None, C_None)),
  353. {A_LGDT} (Ch: (C_None, C_None, C_None)),
  354. {A_LGS} (Ch: (C_Wop2, C_None, C_None)),
  355. {A_LIDT} (Ch: (C_None, C_None, C_None)),
  356. {A_LLDT} (Ch: (C_None, C_None, C_None)),
  357. {A_LMSW} (Ch: (C_None, C_None, C_None)),
  358. {A_LOADALL} (Ch: (C_All, C_None, C_None)), { new }
  359. {A_LOADALL286} (Ch: (C_All, C_None, C_None)), { new }
  360. {A_LODSB} (Ch: (C_WEAX, C_RWESI, C_None)), { new }
  361. {A_LODSD} (Ch: (C_WEAX, C_RWESI, C_None)), { new }
  362. {A_LODSW} (Ch: (C_WEAX, C_RWESI, C_None)), { new }
  363. {A_LOOP} (Ch: (C_RWECX, C_None, C_None)),
  364. {A_LOOPE} (Ch: (C_RWECX, C_RFlags, C_None)),
  365. {A_LOOPNE} (Ch: (C_RWECX, C_RFlags, C_None)),
  366. {A_LOOPNZ} (Ch: (C_RWECX, C_RFlags, C_None)),
  367. {A_LOOPZ} (Ch: (C_RWECX, C_RFlags, C_None)),
  368. {A_LSL} (Ch: (C_Wop2, C_WFlags, C_None)),
  369. {A_LSS} (Ch: (C_Wop2, C_None, C_None)),
  370. {A_LTR} (Ch: (C_None, C_None, C_None)),
  371. {A_MOV} (Ch: (C_Wop2, C_Rop1, C_None)),
  372. {A_MOVD} (Ch: (C_All, C_None, C_None)), { new }
  373. {A_MOVQ} (Ch: (C_All, C_None, C_None)), { new }
  374. {A_MOVSB} (Ch: (C_All, C_Rop1, C_None)),
  375. {A_MOVSD} (Ch: (C_All, C_None, C_None)), { new }
  376. {A_MOVSW} (Ch: (C_All, C_None, C_None)), { new }
  377. {A_MOVSX} (Ch: (C_Wop2, C_Rop1, C_None)),
  378. {A_MOVZX} (Ch: (C_Wop2, C_Rop1, C_None)),
  379. {A_MUL} (Ch: (C_RWEAX, C_WEDX, C_WFlags)),
  380. {A_NEG} (Ch: (C_RWop1, C_None, C_None)),
  381. {A_NOP} (Ch: (C_None, C_None, C_None)),
  382. {A_NOT} (Ch: (C_RWop1, C_WFlags, C_None)),
  383. {A_OR} (Ch: (C_RWop2, C_WFlags, C_None)),
  384. {A_OUT} (Ch: (C_Rop1, C_Rop2, C_None)),
  385. {A_OUTSB} (Ch: (C_All, C_None, C_None)), { new }
  386. {A_OUTSD} (Ch: (C_All, C_None, C_None)), { new }
  387. {A_OUTSW} (Ch: (C_All, C_None, C_None)), { new }
  388. {A_PACKSSDW} (Ch: (C_All, C_None, C_None)), { new }
  389. {A_PACKSSWB} (Ch: (C_All, C_None, C_None)), { new }
  390. {A_PACKUSWB} (Ch: (C_All, C_None, C_None)), { new }
  391. {A_PADDB} (Ch: (C_All, C_None, C_None)), { new }
  392. {A_PADDD} (Ch: (C_All, C_None, C_None)), { new }
  393. {A_PADDSB} (Ch: (C_All, C_None, C_None)), { new }
  394. {A_PADDSIW} (Ch: (C_All, C_None, C_None)), { new }
  395. {A_PADDSW} (Ch: (C_All, C_None, C_None)), { new }
  396. {A_PADDUSB} (Ch: (C_All, C_None, C_None)), { new }
  397. {A_PADDUSW} (Ch: (C_All, C_None, C_None)), { new }
  398. {A_PADDW} (Ch: (C_All, C_None, C_None)), { new }
  399. {A_PAND} (Ch: (C_All, C_None, C_None)), { new }
  400. {A_PANDN} (Ch: (C_All, C_None, C_None)), { new }
  401. {A_PAVEB} (Ch: (C_All, C_None, C_None)), { new }
  402. {A_PAVGUSB} (Ch: (C_All, C_None, C_None)), { new }
  403. {A_PCMPEQB} (Ch: (C_All, C_None, C_None)), { new }
  404. {A_PCMPEQD} (Ch: (C_All, C_None, C_None)), { new }
  405. {A_PCMPEQW} (Ch: (C_All, C_None, C_None)), { new }
  406. {A_PCMPGTB} (Ch: (C_All, C_None, C_None)), { new }
  407. {A_PCMPGTD} (Ch: (C_All, C_None, C_None)), { new }
  408. {A_PCMPGTW} (Ch: (C_All, C_None, C_None)), { new }
  409. {A_PDISTIB} (Ch: (C_All, C_None, C_None)), { new }
  410. {A_PF2ID} (Ch: (C_All, C_None, C_None)), { new }
  411. {A_PFACC} (Ch: (C_All, C_None, C_None)), { new }
  412. {A_PFADD} (Ch: (C_All, C_None, C_None)), { new }
  413. {A_PFCMPEQ} (Ch: (C_All, C_None, C_None)), { new }
  414. {A_PFCMPGE} (Ch: (C_All, C_None, C_None)), { new }
  415. {A_PFCMPGT} (Ch: (C_All, C_None, C_None)), { new }
  416. {A_PFMAX} (Ch: (C_All, C_None, C_None)), { new }
  417. {A_PFMIN} (Ch: (C_All, C_None, C_None)), { new }
  418. {A_PFMUL} (Ch: (C_All, C_None, C_None)), { new }
  419. {A_PFRCP} (Ch: (C_All, C_None, C_None)), { new }
  420. {A_PFRCPIT1} (Ch: (C_All, C_None, C_None)), { new }
  421. {A_PFRCPIT2} (Ch: (C_All, C_None, C_None)), { new }
  422. {A_PFRSQIT1} (Ch: (C_All, C_None, C_None)), { new }
  423. {A_PFRSQRT} (Ch: (C_All, C_None, C_None)), { new }
  424. {A_PFSUB} (Ch: (C_All, C_None, C_None)), { new }
  425. {A_PFSUBR} (Ch: (C_All, C_None, C_None)), { new }
  426. {A_PI2FD} (Ch: (C_All, C_None, C_None)), { new }
  427. {A_PMACHRIW} (Ch: (C_All, C_None, C_None)), { new }
  428. {A_PMADDWD} (Ch: (C_All, C_None, C_None)), { new }
  429. {A_PMAGW} (Ch: (C_All, C_None, C_None)), { new }
  430. {A_PMULHRIW} (Ch: (C_All, C_None, C_None)), { new }
  431. {A_PMULHRWA} (Ch: (C_All, C_None, C_None)), { new }
  432. {A_PMULHRWC} (Ch: (C_All, C_None, C_None)), { new }
  433. {A_PMULHW} (Ch: (C_All, C_None, C_None)), { new }
  434. {A_PMULLW} (Ch: (C_All, C_None, C_None)), { new }
  435. {A_PMVGEZB} (Ch: (C_All, C_None, C_None)), { new }
  436. {A_PMVLZB} (Ch: (C_All, C_None, C_None)), { new }
  437. {A_PMVNZB} (Ch: (C_All, C_None, C_None)), { new }
  438. {A_PMVZB} (Ch: (C_All, C_None, C_None)), { new }
  439. {A_POP} (Ch: (C_Wop1, C_RWESP, C_None)),
  440. {A_POPA} (Ch: (C_All, C_None, C_None)), {don't know value of any register}
  441. {A_POPAD} (Ch: (C_All, C_None, C_None)), {don't know value of any register}
  442. {A_POPAW} (Ch: (C_All, C_None, C_None)), { new }
  443. {A_POPF} (Ch: (C_RWESP, C_WFlags, C_None)),
  444. {A_POPFD} (Ch: (C_RWESP, C_WFlags, C_None)),
  445. {A_POPFW} (Ch: (C_RWESP, C_WFLAGS, C_None)), { new }
  446. {A_POR} (Ch: (C_All, C_None, C_None)), { new }
  447. {A_PREFETCH} (Ch: (C_All, C_None, C_None)), { new }
  448. {A_PREFETCHW} (Ch: (C_All, C_None, C_None)), { new }
  449. {A_PSLLD} (Ch: (C_All, C_None, C_None)), { new }
  450. {A_PSLLQ} (Ch: (C_All, C_None, C_None)), { new }
  451. {A_PSLLW} (Ch: (C_All, C_None, C_None)), { new }
  452. {A_PSRAD} (Ch: (C_All, C_None, C_None)), { new }
  453. {A_PSRAW} (Ch: (C_All, C_None, C_None)), { new }
  454. {A_PSRLD} (Ch: (C_All, C_None, C_None)), { new }
  455. {A_PSRLQ} (Ch: (C_All, C_None, C_None)), { new }
  456. {A_PSRLW} (Ch: (C_All, C_None, C_None)), { new }
  457. {A_PSUBB} (Ch: (C_All, C_None, C_None)), { new }
  458. {A_PSUBD} (Ch: (C_All, C_None, C_None)), { new }
  459. {A_PSUBSB} (Ch: (C_All, C_None, C_None)), { new }
  460. {A_PSUBSIW} (Ch: (C_All, C_None, C_None)), { new }
  461. {A_PSUBSW} (Ch: (C_All, C_None, C_None)), { new }
  462. {A_PSUBUSB} (Ch: (C_All, C_None, C_None)), { new }
  463. {A_PSUBUSW} (Ch: (C_All, C_None, C_None)), { new }
  464. {A_PSUBW} (Ch: (C_All, C_None, C_None)), { new }
  465. {A_PUNPCKHBW} (Ch: (C_All, C_None, C_None)), { new }
  466. {A_PUNPCKHDQ} (Ch: (C_All, C_None, C_None)), { new }
  467. {A_PUNPCKHWD} (Ch: (C_All, C_None, C_None)), { new }
  468. {A_PUNPCKLBW} (Ch: (C_All, C_None, C_None)), { new }
  469. {A_PUNPCKLDQ} (Ch: (C_All, C_None, C_None)), { new }
  470. {A_PUNPCKLWD} (Ch: (C_All, C_None, C_None)), { new }
  471. {A_PUSH} (Ch: (C_RWESP, C_None, C_None)),
  472. {A_PUSHA} (Ch: (C_All, C_None, C_None)),
  473. {A_PUSHAD} (Ch: (C_All, C_None, C_None)),
  474. {A_PUSHAW} (Ch: (C_All, C_None, C_None)), { new }
  475. {A_PUSHF} (Ch: (C_RWESP, C_RFlags, C_None)),
  476. {A_PUSHFD} (Ch: (C_RWESP, C_RFlags, C_None)),
  477. {A_PUSHFW} (Ch: (C_RWESP, C_RFLAGS, C_None)), { new }
  478. {A_PXOR} (Ch: (C_All, C_None, C_None)), { new }
  479. {A_RCL} (Ch: (C_RWop2, C_Rop1, C_RWFlags)),
  480. {A_RCR} (Ch: (C_RWop2, C_Rop1, C_RWFlags)),
  481. {A_RDMSR} (Ch: (C_WEAX, C_WEDX, C_None)), { new }
  482. {A_RDPMC} (Ch: (C_WEAX, C_WEDX, C_None)), { new }
  483. {A_RDTSC} (Ch: (C_WEAX, C_WEDX, C_None)), { new }
  484. {A_RESB} (Ch: (C_All, C_None, C_None)), { new }
  485. {A_RET} (Ch: (C_All, C_None, C_None)),
  486. {A_RETF} (Ch: (C_All, C_None, C_None)), { new }
  487. {A_RETN} (Ch: (C_All, C_None, C_None)), { new }
  488. {A_ROL} (Ch: (C_RWop2, C_Rop1, C_RWFlags)),
  489. {A_ROR} (Ch: (C_RWop2, C_Rop1, C_RWFlags)),
  490. {A_RSM} (Ch: (C_All, C_None, C_None)), { new }
  491. {A_SAHF} (Ch: (C_WFlags, C_REAX, C_None)),
  492. {A_SAL} (Ch: (C_RWop2, C_Rop1, C_RWFlags)),
  493. {A_SALC} (Ch: (C_WEAX, C_RFLAGS, C_None)), { new }
  494. {A_SAR} (Ch: (C_RWop2, C_Rop1, C_WFlags)),
  495. {A_SBB} (Ch: (C_RWop2, C_Rop1, C_RWFlags)),
  496. {A_SCASB} (Ch: (C_All, C_None, C_None)), { new }
  497. {A_SCASD} (Ch: (C_All, C_None, C_None)), { new }
  498. {A_SCASW} (Ch: (C_All, C_None, C_None)), { new }
  499. {A_SGDT} (Ch: (C_Wop1, C_None, C_None)),
  500. {A_SHL} (Ch: (C_RWop2, C_Rop1, C_WFlags)),
  501. {A_SHLD} (Ch: (C_RWOp3, C_RWFlags, C_Rop2)),
  502. {A_SHR} (Ch: (C_RWop2, C_Rop1, C_WFlags)),
  503. {A_SHRD} (Ch: (C_RWOp3, C_RWFlags, C_Rop2)),
  504. {A_SIDT} (Ch: (C_Wop1, C_None, C_None)),
  505. {A_SLDT} (Ch: (C_Wop1, C_None, C_None)),
  506. {A_SMI} (Ch: (C_All, C_None, C_None)), { new }
  507. {A_SMSW} (Ch: (C_Wop1, C_None, C_None)),
  508. {A_STC} (Ch: (C_WFlags, C_None, C_None)),
  509. {A_STD} (Ch: (C_SDirFlag, C_None, C_None)),
  510. {A_STI} (Ch: (C_WFlags, C_None, C_None)),
  511. {A_STOSB} (Ch: (C_REAX, C_WMemEDI, C_RWEDI)), { new }
  512. {A_STOSD} (Ch: (C_REAX, C_WMemEDI, C_RWEDI)), { new }
  513. {A_STOSW} (Ch: (C_REAX, C_WMemEDI, C_RWEDI)), { new }
  514. {A_STR} (Ch: (C_Wop1, C_None, C_None)),
  515. {A_SUB} (Ch: (C_RWop2, C_Rop1, C_WFlags)),
  516. {A_TEST} (Ch: (C_WFlags, C_Rop1, C_Rop2)),
  517. {A_UMOV} (Ch: (C_All, C_None, C_None)), { new }
  518. {A_VERR} (Ch: (C_WFlags, C_None, C_None)),
  519. {A_VERW} (Ch: (C_WFlags, C_None, C_None)),
  520. {A_WAIT} (Ch: (C_None, C_None, C_None)),
  521. {A_WBINVD} (Ch: (C_None, C_None, C_None)), { new }
  522. {A_WRMSR} (Ch: (C_All, C_None, C_None)), { new }
  523. {A_XADD} (Ch: (C_All, C_None, C_None)), { new }
  524. {A_XBTS} (Ch: (C_All, C_None, C_None)), { new }
  525. {A_XCHG} (Ch: (C_RWop1, C_RWop2, C_None)), {(might be) handled seperately}
  526. {A_XLAT} (Ch: (C_WEAX, C_REBX, C_None)),
  527. {A_XLATB} (Ch: (C_WEAX, C_REBX, C_None)),
  528. {A_XOR} (Ch: (C_RWop2, C_Rop1, C_WFlags)),
  529. {A_CMOV} (Ch: (C_ROp1, C_WOp2, C_RFLAGS)), { new }
  530. {A_J} (Ch: (C_None, C_None, C_None)), { new }
  531. {A_SET} (Ch: (C_WEAX, C_RFLAGS, C_None)) { new }
  532. );
  533. Var
  534. {How many instructions are between the current instruction and the last one
  535. that modified the register}
  536. NrOfInstrSinceLastMod: TInstrSinceLastMod;
  537. {************************ Create the Label table ************************}
  538. Function FindLoHiLabels(Var LowLabel, HighLabel, LabelDif: Longint; BlockStart: Pai): Pai;
  539. {Walks through the paasmlist to find the lowest and highest label number}
  540. Var LabelFound: Boolean;
  541. P: Pai;
  542. Begin
  543. LabelFound := False;
  544. LowLabel := MaxLongint;
  545. HighLabel := 0;
  546. P := BlockStart;
  547. While Assigned(P) And
  548. ((P^.typ <> Ait_Marker) Or
  549. (Pai_Marker(P)^.Kind <> AsmBlockStart)) Do
  550. Begin
  551. If (Pai(p)^.typ = ait_label) Then
  552. If (Pai_Label(p)^.l^.is_used)
  553. Then
  554. Begin
  555. LabelFound := True;
  556. If (Pai_Label(p)^.l^.labelnr < LowLabel) Then
  557. LowLabel := Pai_Label(p)^.l^.labelnr;
  558. If (Pai_Label(p)^.l^.labelnr > HighLabel) Then
  559. HighLabel := Pai_Label(p)^.l^.labelnr;
  560. End;
  561. GetNextInstruction(p, p);
  562. End;
  563. FindLoHiLabels := p;
  564. If LabelFound
  565. Then LabelDif := HighLabel+1-LowLabel
  566. Else LabelDif := 0;
  567. End;
  568. Function FindRegAlloc(Reg: TRegister; StartPai: Pai): Boolean;
  569. {Returns true if a ait_alloc object for Reg is found in the block of Pai's
  570. starting with StartPai and ending with the next "real" instruction}
  571. Begin
  572. FindRegAlloc:=False;
  573. Repeat
  574. While Assigned(StartPai) And
  575. ((StartPai^.typ in (SkipInstr - [ait_regAlloc])) Or
  576. ((StartPai^.typ = ait_label) and
  577. Not(Pai_Label(StartPai)^.l^.Is_Used))) Do
  578. StartPai := Pai(StartPai^.Next);
  579. If Assigned(StartPai) And
  580. (StartPai^.typ = ait_regAlloc) and (PairegAlloc(StartPai)^.allocation) Then
  581. Begin
  582. if PairegAlloc(StartPai)^.Reg = Reg then
  583. begin
  584. FindRegAlloc:=true;
  585. exit;
  586. end;
  587. StartPai := Pai(StartPai^.Next);
  588. End
  589. else
  590. exit;
  591. Until false;
  592. End;
  593. Procedure BuildLabelTableAndFixRegAlloc(AsmL: PAasmOutput; Var LabelTable: PLabelTable; LowLabel: Longint;
  594. Var LabelDif: Longint; BlockStart, BlockEnd: Pai);
  595. {Builds a table with the locations of the labels in the paasmoutput.
  596. Also fixes some RegDeallocs like "# %eax released; push (%eax)"}
  597. Var p, hp1, hp2: Pai;
  598. UsedRegs: TRegSet;
  599. Begin
  600. UsedRegs := [];
  601. If (LabelDif <> 0) Then
  602. Begin
  603. {$IfDef TP}
  604. If (MaxAvail >= LabelDif*SizeOf(Pai))
  605. Then
  606. Begin
  607. {$EndIf TP}
  608. GetMem(LabelTable, LabelDif*SizeOf(TLabelTableItem));
  609. FillChar(LabelTable^, LabelDif*SizeOf(TLabelTableItem), 0);
  610. p := BlockStart;
  611. While (P <> BlockEnd) Do
  612. Begin
  613. Case p^.typ Of
  614. ait_Label:
  615. If Pai_Label(p)^.l^.is_used Then
  616. LabelTable^[Pai_Label(p)^.l^.labelnr-LowLabel].PaiObj := p;
  617. ait_regAlloc:
  618. begin
  619. if PairegAlloc(p)^.Allocation then
  620. Begin
  621. If Not(PaiRegAlloc(p)^.Reg in UsedRegs) Then
  622. UsedRegs := UsedRegs + [PaiRegAlloc(p)^.Reg]
  623. Else
  624. Begin
  625. hp1 := p;
  626. hp2 := nil;
  627. While GetLastInstruction(hp1, hp1) And
  628. Not(RegInInstruction(PaiRegAlloc(p)^.Reg, hp1)) Do
  629. hp2 := hp1;
  630. If hp2 <> nil Then
  631. Begin
  632. hp1 := New(PaiRegAlloc, DeAlloc(PaiRegAlloc(p)^.Reg));
  633. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp1);
  634. End;
  635. End;
  636. End
  637. else
  638. Begin
  639. UsedRegs := UsedRegs - [PaiRegAlloc(p)^.Reg];
  640. hp1 := p;
  641. hp2 := nil;
  642. While Not(FindRegAlloc(PaiRegAlloc(p)^.Reg, Pai(hp1^.Next))) And
  643. GetNextInstruction(hp1, hp1) And
  644. RegInInstruction(PaiRegAlloc(p)^.Reg, hp1) Do
  645. hp2 := hp1;
  646. If hp2 <> nil Then
  647. Begin
  648. hp1 := Pai(p^.previous);
  649. AsmL^.Remove(p);
  650. InsertLLItem(AsmL, hp2, Pai(hp2^.Next), p);
  651. p := hp1;
  652. End;
  653. End;
  654. end;
  655. End;
  656. P := Pai(p^.Next);
  657. While Assigned(p) And
  658. (p^.typ in (SkipInstr - [ait_regalloc])) Do
  659. P := Pai(P^.Next);
  660. End;
  661. {$IfDef TP}
  662. End
  663. Else LabelDif := 0;
  664. {$EndIf TP}
  665. End;
  666. End;
  667. {************************ Search the Label table ************************}
  668. Function FindLabel(L: PasmLabel; Var hp: Pai): Boolean;
  669. {searches for the specified label starting from hp as long as the
  670. encountered instructions are labels, to be able to optimize constructs like
  671. jne l2 jmp l2
  672. jmp l3 and l1:
  673. l1: l2:
  674. l2:}
  675. Var TempP: Pai;
  676. Begin
  677. TempP := hp;
  678. While Assigned(TempP) and
  679. (TempP^.typ In SkipInstr + [ait_label]) Do
  680. If (TempP^.typ <> ait_Label) Or
  681. (pai_label(TempP)^.l <> L)
  682. Then GetNextInstruction(TempP, TempP)
  683. Else
  684. Begin
  685. hp := TempP;
  686. FindLabel := True;
  687. exit
  688. End;
  689. FindLabel := False;
  690. End;
  691. {************************ Some general functions ************************}
  692. Function Reg32(Reg: TRegister): TRegister;
  693. {Returns the 32 bit component of Reg if it exists, otherwise Reg is returned}
  694. Begin
  695. Reg32 := Reg;
  696. If (Reg >= R_AX)
  697. Then
  698. If (Reg <= R_DI)
  699. Then Reg32 := Reg16ToReg32(Reg)
  700. Else
  701. If (Reg <= R_BL)
  702. Then Reg32 := Reg8toReg32(Reg);
  703. End;
  704. { inserts new_one between prev and foll }
  705. Procedure InsertLLItem(AsmL: PAasmOutput; prev, foll, new_one: PLinkedList_Item);
  706. Begin
  707. If Assigned(prev) Then
  708. If Assigned(foll) Then
  709. Begin
  710. If Assigned(new_one) Then
  711. Begin
  712. new_one^.previous := prev;
  713. new_one^.next := foll;
  714. prev^.next := new_one;
  715. foll^.previous := new_one;
  716. End;
  717. End
  718. Else AsmL^.Concat(new_one)
  719. Else If Assigned(Foll) Then AsmL^.Insert(new_one)
  720. End;
  721. {********************* Compare parts of Pai objects *********************}
  722. Function RegsSameSize(Reg1, Reg2: TRegister): Boolean;
  723. {returns true if Reg1 and Reg2 are of the same size (so if they're both
  724. 8bit, 16bit or 32bit)}
  725. Begin
  726. If (Reg1 <= R_EDI)
  727. Then RegsSameSize := (Reg2 <= R_EDI)
  728. Else
  729. If (Reg1 <= R_DI)
  730. Then RegsSameSize := (Reg2 in [R_AX..R_DI])
  731. Else
  732. If (Reg1 <= R_BL)
  733. Then RegsSameSize := (Reg2 in [R_AL..R_BL])
  734. Else RegsSameSize := False
  735. End;
  736. Procedure AddReg2RegInfo(OldReg, NewReg: TRegister; Var RegInfo: TRegInfo);
  737. {updates the ???RegsEncountered and ???2???Reg fields of RegInfo. Assumes that
  738. OldReg and NewReg have the same size (has to be chcked in advance with
  739. RegsSameSize) and that neither equals R_NO}
  740. Begin
  741. With RegInfo Do
  742. Begin
  743. NewRegsEncountered := NewRegsEncountered + [NewReg];
  744. OldRegsEncountered := OldRegsEncountered + [OldReg];
  745. New2OldReg[NewReg] := OldReg;
  746. Case OldReg Of
  747. R_EAX..R_EDI:
  748. Begin
  749. NewRegsEncountered := NewRegsEncountered + [Reg32toReg16(NewReg)];
  750. OldRegsEncountered := OldRegsEncountered + [Reg32toReg16(OldReg)];
  751. New2OldReg[Reg32toReg16(NewReg)] := Reg32toReg16(OldReg);
  752. If (NewReg in [R_EAX..R_EBX]) And
  753. (OldReg in [R_EAX..R_EBX]) Then
  754. Begin
  755. NewRegsEncountered := NewRegsEncountered + [Reg32toReg8(NewReg)];
  756. OldRegsEncountered := OldRegsEncountered + [Reg32toReg8(OldReg)];
  757. New2OldReg[Reg32toReg8(NewReg)] := Reg32toReg8(OldReg);
  758. End;
  759. End;
  760. R_AX..R_DI:
  761. Begin
  762. NewRegsEncountered := NewRegsEncountered + [Reg16toReg32(NewReg)];
  763. OldRegsEncountered := OldRegsEncountered + [Reg16toReg32(OldReg)];
  764. New2OldReg[Reg16toReg32(NewReg)] := Reg16toReg32(OldReg);
  765. If (NewReg in [R_AX..R_BX]) And
  766. (OldReg in [R_AX..R_BX]) Then
  767. Begin
  768. NewRegsEncountered := NewRegsEncountered + [Reg16toReg8(NewReg)];
  769. OldRegsEncountered := OldRegsEncountered + [Reg16toReg8(OldReg)];
  770. New2OldReg[Reg16toReg8(NewReg)] := Reg16toReg8(OldReg);
  771. End;
  772. End;
  773. R_AL..R_BL:
  774. Begin
  775. NewRegsEncountered := NewRegsEncountered + [Reg8toReg32(NewReg)]
  776. + [Reg8toReg16(NewReg)];
  777. OldRegsEncountered := OldRegsEncountered + [Reg8toReg32(OldReg)]
  778. + [Reg8toReg16(OldReg)];
  779. New2OldReg[Reg8toReg32(NewReg)] := Reg8toReg32(OldReg);
  780. End;
  781. End;
  782. End;
  783. End;
  784. Procedure AddOpRegInfo(const o:Toper; Var RegInfo: TRegInfo);
  785. Begin
  786. Case o.typ Of
  787. Top_Reg:
  788. If (o.reg <> R_NO) Then
  789. AddReg2RegInfo(o.reg, o.reg, RegInfo);
  790. Top_Ref:
  791. Begin
  792. If o.ref^.base <> R_NO Then
  793. AddReg2RegInfo(o.ref^.base, o.ref^.base, RegInfo);
  794. If o.ref^.index <> R_NO Then
  795. AddReg2RegInfo(o.ref^.index, o.ref^.index, RegInfo);
  796. End;
  797. End;
  798. End;
  799. Function RegsEquivalent(OldReg, NewReg: TRegister; Var RegInfo: TRegInfo; OPAct: TOpAction): Boolean;
  800. Begin
  801. If Not((OldReg = R_NO) Or (NewReg = R_NO)) Then
  802. If RegsSameSize(OldReg, NewReg) Then
  803. With RegInfo Do
  804. {here we always check for the 32 bit component, because it is possible that
  805. the 8 bit component has not been set, event though NewReg already has been
  806. processed. This happens if it has been compared with a register that doesn't
  807. have an 8 bit component (such as EDI). In that case the 8 bit component is
  808. still set to R_NO and the comparison in the Else-part will fail}
  809. If (Reg32(OldReg) in OldRegsEncountered) Then
  810. If (Reg32(NewReg) in NewRegsEncountered) Then
  811. RegsEquivalent := (OldReg = New2OldReg[NewReg])
  812. { If we haven't encountered the new register yet, but we have encountered the
  813. old one already, the new one can only be correct if it's being written to
  814. (and consequently the old one is also being written to), otherwise
  815. movl -8(%ebp), %eax and movl -8(%ebp), %eax
  816. movl (%eax), %eax movl (%edx), %edx
  817. are considered equivalent}
  818. Else
  819. If (OpAct = OpAct_Write) Then
  820. Begin
  821. AddReg2RegInfo(OldReg, NewReg, RegInfo);
  822. RegsEquivalent := True
  823. End
  824. Else Regsequivalent := False
  825. Else
  826. If Not(Reg32(NewReg) in NewRegsEncountered) Then
  827. Begin
  828. AddReg2RegInfo(OldReg, NewReg, RegInfo);
  829. RegsEquivalent := True
  830. End
  831. Else RegsEquivalent := False
  832. Else RegsEquivalent := False
  833. Else RegsEquivalent := OldReg = NewReg
  834. End;
  835. Function RefsEquivalent(Const R1, R2: TReference; var RegInfo: TRegInfo; OpAct: TOpAction): Boolean;
  836. Begin
  837. If R1.is_immediate Then
  838. RefsEquivalent := R2.is_immediate and (R1.Offset = R2.Offset)
  839. Else
  840. RefsEquivalent := (R1.Offset+R1.OffsetFixup = R2.Offset+R2.OffsetFixup) And
  841. RegsEquivalent(R1.Base, R2.Base, RegInfo, OpAct) And
  842. RegsEquivalent(R1.Index, R2.Index, RegInfo, OpAct) And
  843. (R1.Segment = R2.Segment) And (R1.ScaleFactor = R2.ScaleFactor) And
  844. (R1.Symbol = R2.Symbol);
  845. End;
  846. Function RefsEqual(Const R1, R2: TReference): Boolean;
  847. Begin
  848. If R1.is_immediate Then
  849. RefsEqual := R2.is_immediate and (R1.Offset = R2.Offset)
  850. Else
  851. RefsEqual := (R1.Offset+R1.OffsetFixup = R2.Offset+R2.OffsetFixup) And
  852. (R1.Segment = R2.Segment) And (R1.Base = R2.Base) And
  853. (R1.Index = R2.Index) And (R1.ScaleFactor = R2.ScaleFactor) And
  854. (R1.Symbol=R2.Symbol);
  855. End;
  856. Function IsGP32Reg(Reg: TRegister): Boolean;
  857. {Checks if the register is a 32 bit general purpose register}
  858. Begin
  859. If (Reg >= R_EAX) and (Reg <= R_EBX)
  860. Then IsGP32Reg := True
  861. Else IsGP32reg := False
  862. End;
  863. Function RegInRef(Reg: TRegister; Const Ref: TReference): Boolean;
  864. Begin {checks whether Ref contains a reference to Reg}
  865. Reg := Reg32(Reg);
  866. RegInRef := (Ref.Base = Reg) Or (Ref.Index = Reg)
  867. End;
  868. Function RegInInstruction(Reg: TRegister; p1: Pai): Boolean;
  869. {checks if Reg is used by the instruction p1}
  870. Var TmpResult: Boolean;
  871. Begin
  872. TmpResult := False;
  873. If (Pai(p1)^.typ = ait_instruction) Then
  874. Begin
  875. Case Pai386(p1)^.oper[0].typ Of
  876. Top_Reg: TmpResult := Reg = Pai386(p1)^.oper[0].reg;
  877. Top_Ref: TmpResult := RegInRef(Reg, Pai386(p1)^.oper[0].ref^);
  878. End;
  879. If Not(TmpResult) Then
  880. Case Pai386(p1)^.oper[1].typ Of
  881. Top_Reg: TmpResult := (Reg = Pai386(p1)^.oper[1].reg);
  882. Top_Ref: TmpResult := RegInRef(Reg, Pai386(p1)^.oper[1].ref^)
  883. End;
  884. If Not(TmpResult) Then
  885. Case Pai386(p1)^.oper[2].typ Of
  886. Top_Reg: TmpResult := (Reg = Pai386(p1)^.oper[2].reg);
  887. Top_Ref: TmpResult := RegInRef(Reg, Pai386(p1)^.oper[2].ref^)
  888. End
  889. End;
  890. RegInInstruction := TmpResult
  891. End;
  892. {Function RegInOp(Reg: TRegister; const o:toper): Boolean;
  893. Begin
  894. RegInOp := False;
  895. Case opt Of
  896. top_reg: RegInOp := Reg = o.reg;
  897. top_ref: RegInOp := (Reg = o.ref^.Base) Or
  898. (Reg = o.ref^.Index);
  899. End;
  900. End;}
  901. Function RegModifiedByInstruction(Reg: TRegister; p1: Pai): Boolean;
  902. {returns true if Reg is modified by the instruction p1. P1 is assumed to be
  903. of the type ait_instruction}
  904. Var hp: Pai;
  905. Begin
  906. If GetLastInstruction(p1, hp)
  907. Then
  908. RegModifiedByInstruction :=
  909. PPAiProp(p1^.OptInfo)^.Regs[Reg].WState <>
  910. PPAiProp(hp^.OptInfo)^.Regs[Reg].WState
  911. Else RegModifiedByInstruction := True;
  912. End;
  913. {********************* GetNext and GetLastInstruction *********************}
  914. Function GetNextInstruction(Current: Pai; Var Next: Pai): Boolean;
  915. {skips ait_regalloc, ait_regdealloc and ait_stab* objects and puts the
  916. next pai object in Next. Returns false if there isn't any}
  917. Begin
  918. Repeat
  919. Current := Pai(Current^.Next);
  920. While Assigned(Current) And
  921. ((Current^.typ In SkipInstr) or
  922. ((Current^.typ = ait_label) And
  923. Not(Pai_Label(Current)^.l^.is_used))) Do
  924. Current := Pai(Current^.Next);
  925. If Assigned(Current) And
  926. (Current^.typ = ait_Marker) And
  927. (Pai_Marker(Current)^.Kind = NoPropInfoStart) Then
  928. Begin
  929. While Assigned(Current) And
  930. ((Current^.typ <> ait_Marker) Or
  931. (Pai_Marker(Current)^.Kind <> NoPropInfoEnd)) Do
  932. Current := Pai(Current^.Next);
  933. End;
  934. Until Not(Assigned(Current)) Or
  935. (Current^.typ <> ait_Marker) Or
  936. (Pai_Marker(Current)^.Kind <> NoPropInfoEnd);
  937. Next := Current;
  938. If Assigned(Current) And
  939. Not((Current^.typ In SkipInstr) or
  940. ((Current^.typ = ait_label) And
  941. Not(Pai_Label(Current)^.l^.is_used)))
  942. Then GetNextInstruction := True
  943. Else
  944. Begin
  945. Next := Nil;
  946. GetNextInstruction := False;
  947. End;
  948. End;
  949. Function GetLastInstruction(Current: Pai; Var Last: Pai): Boolean;
  950. {skips the ait-types in SkipInstr puts the previous pai object in
  951. Last. Returns false if there isn't any}
  952. Begin
  953. Repeat
  954. Current := Pai(Current^.previous);
  955. While Assigned(Current) And
  956. (((Current^.typ = ait_Marker) And
  957. Not(Pai_Marker(Current)^.Kind in [AsmBlockEnd,NoPropInfoEnd])) or
  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^.previous);
  962. If Assigned(Current) And
  963. (Current^.typ = ait_Marker) And
  964. (Pai_Marker(Current)^.Kind = NoPropInfoEnd) Then
  965. Begin
  966. While Assigned(Current) And
  967. ((Current^.typ <> ait_Marker) Or
  968. (Pai_Marker(Current)^.Kind <> NoPropInfoStart)) Do
  969. Current := Pai(Current^.previous);
  970. End;
  971. Until Not(Assigned(Current)) Or
  972. (Current^.typ <> ait_Marker) Or
  973. (Pai_Marker(Current)^.Kind <> NoPropInfoStart);
  974. If Not(Assigned(Current)) or
  975. (Current^.typ In SkipInstr) or
  976. ((Current^.typ = ait_label) And
  977. Not(Pai_Label(Current)^.l^.is_used)) or
  978. ((Current^.typ = ait_Marker) And
  979. (Pai_Marker(Current)^.Kind = AsmBlockEnd))
  980. Then
  981. Begin
  982. Last := Nil;
  983. GetLastInstruction := False
  984. End
  985. Else
  986. Begin
  987. Last := Current;
  988. GetLastInstruction := True;
  989. End;
  990. End;
  991. Procedure SkipHead(var P: Pai);
  992. Var OldP: Pai;
  993. Begin
  994. Repeat
  995. OldP := P;
  996. If (P^.typ in SkipInstr) Or
  997. ((P^.typ = ait_marker) And
  998. (Pai_Marker(P)^.Kind = AsmBlockEnd)) Then
  999. GetNextInstruction(P, P)
  1000. Else If ((P^.Typ = Ait_Marker) And
  1001. (Pai_Marker(P)^.Kind = NoPropInfoStart)) Then
  1002. {a marker of the NoPropInfoStart can't be the first instruction of a
  1003. paasmoutput list}
  1004. GetNextInstruction(Pai(P^.Previous),P);
  1005. If (P^.Typ = Ait_Marker) And
  1006. (Pai_Marker(P)^.Kind = AsmBlockStart) Then
  1007. Begin
  1008. P := Pai(P^.Next);
  1009. While (P^.typ <> Ait_Marker) Or
  1010. (Pai_Marker(P)^.Kind <> AsmBlockEnd) Do
  1011. P := Pai(P^.Next)
  1012. End;
  1013. Until P = OldP
  1014. End;
  1015. {******************* The Data Flow Analyzer functions ********************}
  1016. Procedure UpdateUsedRegs(Var UsedRegs: TRegSet; p: Pai);
  1017. {updates UsedRegs with the RegAlloc Information coming after P}
  1018. Begin
  1019. Repeat
  1020. While Assigned(p) And
  1021. ((p^.typ in (SkipInstr - [ait_RegAlloc])) or
  1022. ((p^.typ = ait_label) And
  1023. Not(Pai_Label(p)^.l^.is_used))) Do
  1024. p := Pai(p^.next);
  1025. While Assigned(p) And
  1026. (p^.typ=ait_RegAlloc) Do
  1027. Begin
  1028. if pairegalloc(p)^.allocation then
  1029. UsedRegs := UsedRegs + [PaiRegAlloc(p)^.Reg]
  1030. else
  1031. UsedRegs := UsedRegs - [PaiRegAlloc(p)^.Reg];
  1032. p := pai(p^.next);
  1033. End;
  1034. Until Not(Assigned(p)) Or
  1035. (Not(p^.typ in SkipInstr) And
  1036. Not((p^.typ = ait_label) And
  1037. Not(Pai_Label(p)^.l^.is_used)));
  1038. End;
  1039. (*Function FindZeroreg(p: Pai; Var Result: TRegister): Boolean;
  1040. {Finds a register which contains the constant zero}
  1041. Var Counter: TRegister;
  1042. Begin
  1043. Counter := R_EAX;
  1044. FindZeroReg := True;
  1045. While (Counter <= R_EDI) And
  1046. ((PPaiProp(p^.OptInfo)^.Regs[Counter].Typ <> Con_Const) or
  1047. (PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod <> Pointer(0))) Do
  1048. Inc(Byte(Counter));
  1049. If (PPaiProp(p^.OptInfo)^.Regs[Counter].Typ = Con_Const) And
  1050. (PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod = Pointer(0))
  1051. Then Result := Counter
  1052. Else FindZeroReg := False;
  1053. End;*)
  1054. Function TCh2Reg(Ch: TChange): TRegister;
  1055. {converts a TChange variable to a TRegister}
  1056. Begin
  1057. If (Ch <= C_REDI) Then
  1058. TCh2Reg := TRegister(Byte(Ch))
  1059. Else
  1060. If (Ch <= C_WEDI) Then
  1061. TCh2Reg := TRegister(Byte(Ch) - Byte(C_REDI))
  1062. Else
  1063. If (Ch <= C_RWEDI) Then
  1064. TCh2Reg := TRegister(Byte(Ch) - Byte(C_WEDI))
  1065. Else InternalError($db)
  1066. End;
  1067. Procedure IncState(Var S: Byte);
  1068. {Increases S by 1, wraps around at $ffff to 0 (so we won't get overflow
  1069. errors}
  1070. Begin
  1071. If (s <> $ff)
  1072. Then Inc(s)
  1073. Else s := 0
  1074. End;
  1075. Function RegInSequence(Reg: TRegister; Const Content: TContent): Boolean;
  1076. {checks the whole sequence of Content (so StartMod and and the next NrOfMods
  1077. Pai objects) to see whether Reg is used somewhere, without it being loaded
  1078. with something else first}
  1079. Var p: Pai;
  1080. Counter: Byte;
  1081. TmpResult: Boolean;
  1082. RegsChecked: TRegSet;
  1083. Begin
  1084. RegsChecked := [];
  1085. p := Content.StartMod;
  1086. TmpResult := False;
  1087. Counter := 1;
  1088. While Not(TmpResult) And
  1089. (Counter <= Content.NrOfMods) Do
  1090. Begin
  1091. If (p^.typ = ait_instruction) and
  1092. ((Pai386(p)^.opcode = A_MOV) or
  1093. (Pai386(p)^.opcode = A_MOVZX) or
  1094. (Pai386(p)^.opcode = A_MOVSX))
  1095. Then
  1096. If (Pai386(p)^.oper[0].typ = top_ref)
  1097. Then
  1098. With Pai386(p)^.oper[0].ref^ Do
  1099. If (Base = ProcInfo.FramePointer) And
  1100. (Index = R_NO)
  1101. Then RegsChecked := RegsChecked + [Reg32(Pai386(p)^.oper[1].reg)]
  1102. Else
  1103. Begin
  1104. If (Base = Reg) And
  1105. Not(Base In RegsChecked)
  1106. Then TmpResult := True;
  1107. If Not(TmpResult) And
  1108. (Index = Reg) And
  1109. Not(Index In RegsChecked)
  1110. Then TmpResult := True;
  1111. End;
  1112. Inc(Counter);
  1113. GetNextInstruction(p,p)
  1114. End;
  1115. RegInSequence := TmpResult
  1116. End;
  1117. Procedure DestroyReg(p1: PPaiProp; Reg: TRegister);
  1118. {Destroys the contents of the register Reg in the PPaiProp p1, as well as the
  1119. contents of registers are loaded with a memory location based on Reg}
  1120. Var TmpWState, TmpRState: Byte;
  1121. Counter: TRegister;
  1122. Begin
  1123. Reg := Reg32(Reg);
  1124. NrOfInstrSinceLastMod[Reg] := 0;
  1125. If (Reg >= R_EAX) And (Reg <= R_EDI)
  1126. Then
  1127. Begin
  1128. With p1^.Regs[Reg] Do
  1129. Begin
  1130. IncState(WState);
  1131. TmpWState := WState;
  1132. TmpRState := RState;
  1133. FillChar(p1^.Regs[Reg], SizeOf(TContent), 0);
  1134. WState := TmpWState;
  1135. RState := TmpRState;
  1136. End;
  1137. For Counter := R_EAX to R_EDI Do
  1138. With p1^.Regs[Counter] Do
  1139. If (Typ = Con_Ref) And
  1140. RegInSequence(Reg, p1^.Regs[Counter])
  1141. Then
  1142. Begin
  1143. IncState(WState);
  1144. TmpWState := WState;
  1145. TmpRState := RState;
  1146. FillChar(p1^.Regs[Counter], SizeOf(TContent), 0);
  1147. WState := TmpWState;
  1148. RState := TmpRState;
  1149. End;
  1150. End;
  1151. End;
  1152. {Procedure AddRegsToSet(p: Pai; Var RegSet: TRegSet);
  1153. Begin
  1154. If (p^.typ = ait_instruction) Then
  1155. Begin
  1156. Case Pai386(p)^.oper[0].typ Of
  1157. top_reg:
  1158. If Not(Pai386(p)^.oper[0].reg in [R_NO,R_ESP,ProcInfo.FramePointer]) Then
  1159. RegSet := RegSet + [Pai386(p)^.oper[0].reg];
  1160. top_ref:
  1161. With TReference(Pai386(p)^.oper[0]^) Do
  1162. Begin
  1163. If Not(Base in [ProcInfo.FramePointer,R_NO,R_ESP])
  1164. Then RegSet := RegSet + [Base];
  1165. If Not(Index in [ProcInfo.FramePointer,R_NO,R_ESP])
  1166. Then RegSet := RegSet + [Index];
  1167. End;
  1168. End;
  1169. Case Pai386(p)^.oper[1].typ Of
  1170. top_reg:
  1171. If Not(Pai386(p)^.oper[1].reg in [R_NO,R_ESP,ProcInfo.FramePointer]) Then
  1172. If RegSet := RegSet + [TRegister(TwoWords(Pai386(p)^.oper[1]).Word1];
  1173. top_ref:
  1174. With TReference(Pai386(p)^.oper[1]^) Do
  1175. Begin
  1176. If Not(Base in [ProcInfo.FramePointer,R_NO,R_ESP])
  1177. Then RegSet := RegSet + [Base];
  1178. If Not(Index in [ProcInfo.FramePointer,R_NO,R_ESP])
  1179. Then RegSet := RegSet + [Index];
  1180. End;
  1181. End;
  1182. End;
  1183. End;}
  1184. Function OpsEquivalent(const o1, o2: toper; Var RegInfo: TRegInfo; OpAct: TopAction): Boolean;
  1185. Begin {checks whether the two ops are equivalent}
  1186. OpsEquivalent := False;
  1187. if o1.typ=o2.typ then
  1188. Case o1.typ Of
  1189. Top_Reg:
  1190. OpsEquivalent :=RegsEquivalent(o1.reg,o2.reg, RegInfo, OpAct);
  1191. Top_Ref:
  1192. OpsEquivalent := RefsEquivalent(o1.ref^, o2.ref^, RegInfo, OpAct);
  1193. Top_None:
  1194. OpsEquivalent := True
  1195. End;
  1196. End;
  1197. Function OpsEqual(const o1,o2:toper): Boolean;
  1198. Begin {checks whether the two ops are equal}
  1199. OpsEqual := False;
  1200. if o1.typ=o2.typ then
  1201. Case o1.typ Of
  1202. Top_Reg :
  1203. OpsEqual:=o1.reg=o2.reg;
  1204. Top_Ref :
  1205. OpsEqual := RefsEqual(o1.ref^, o2.ref^);
  1206. Top_Const :
  1207. OpsEqual:=o1.val=o2.val;
  1208. Top_Symbol :
  1209. OpsEqual:=(o1.sym=o2.sym) and (o1.symofs=o2.symofs);
  1210. Top_None :
  1211. OpsEqual := True
  1212. End;
  1213. End;
  1214. Function InstructionsEquivalent(p1, p2: Pai; Var RegInfo: TRegInfo): Boolean;
  1215. Begin {checks whether two Pai386 instructions are equal}
  1216. If Assigned(p1) And Assigned(p2) And
  1217. (Pai(p1)^.typ = ait_instruction) And
  1218. (Pai(p1)^.typ = ait_instruction) And
  1219. (Pai386(p1)^.opcode = Pai386(p2)^.opcode) And
  1220. (Pai386(p1)^.oper[0].typ = Pai386(p2)^.oper[0].typ) And
  1221. (Pai386(p1)^.oper[1].typ = Pai386(p2)^.oper[1].typ) And
  1222. (Pai386(p1)^.oper[2].typ = Pai386(p2)^.oper[2].typ)
  1223. Then
  1224. {both instructions have the same structure:
  1225. "<operator> <operand of type1>, <operand of type 2>"}
  1226. If ((Pai386(p1)^.opcode = A_MOV) or
  1227. (Pai386(p1)^.opcode = A_MOVZX) or
  1228. (Pai386(p1)^.opcode = A_MOVSX)) And
  1229. (Pai386(p1)^.oper[0].typ = top_ref) {then .oper[1]t = top_reg} Then
  1230. If Not(RegInRef(Pai386(p1)^.oper[1].reg, Pai386(p1)^.oper[0].ref^)) Then
  1231. {the "old" instruction is a load of a register with a new value, not with
  1232. a value based on the contents of this register (so no "mov (reg), reg")}
  1233. If Not(RegInRef(Pai386(p2)^.oper[1].reg, Pai386(p2)^.oper[0].ref^)) And
  1234. RefsEqual(Pai386(p1)^.oper[0].ref^, Pai386(p2)^.oper[0].ref^)
  1235. Then
  1236. {the "new" instruction is also a load of a register with a new value, and
  1237. this value is fetched from the same memory location}
  1238. Begin
  1239. With Pai386(p2)^.oper[0].ref^ Do
  1240. Begin
  1241. If Not(Base in [ProcInfo.FramePointer, R_NO, R_ESP])
  1242. {it won't do any harm if the register is already in RegsLoadedForRef}
  1243. Then RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef + [Base];
  1244. If Not(Index in [ProcInfo.FramePointer, R_NO, R_ESP])
  1245. Then RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef + [Index];
  1246. End;
  1247. {add the registers from the reference (.oper[0]) to the RegInfo, all registers
  1248. from the reference are the same in the old and in the new instruction
  1249. sequence}
  1250. AddOpRegInfo(Pai386(p1)^.oper[0], RegInfo);
  1251. {the registers from .oper[1] have to be equivalent, but not necessarily equal}
  1252. InstructionsEquivalent :=
  1253. RegsEquivalent(Pai386(p1)^.oper[1].reg, Pai386(p2)^.oper[1].reg, RegInfo, OpAct_Write);
  1254. End
  1255. {the registers are loaded with values from different memory locations. If
  1256. this was allowed, the instructions "mov -4(esi),eax" and "mov -4(ebp),eax"
  1257. would be considered equivalent}
  1258. Else InstructionsEquivalent := False
  1259. Else
  1260. {load register with a value based on the current value of this register}
  1261. Begin
  1262. With Pai386(p2)^.oper[0].ref^ Do
  1263. Begin
  1264. If Not(Base in [ProcInfo.FramePointer,
  1265. Reg32(Pai386(p2)^.oper[1].reg),R_NO,R_ESP])
  1266. {it won't do any harm if the register is already in RegsLoadedForRef}
  1267. Then
  1268. Begin
  1269. RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef + [Base];
  1270. {$ifdef csdebug}
  1271. Writeln(att_reg2str[base], ' added');
  1272. {$endif csdebug}
  1273. end;
  1274. If Not(Index in [ProcInfo.FramePointer,
  1275. Reg32(Pai386(p2)^.oper[1].reg),R_NO,R_ESP])
  1276. Then
  1277. Begin
  1278. RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef + [Index];
  1279. {$ifdef csdebug}
  1280. Writeln(att_reg2str[index], ' added');
  1281. {$endif csdebug}
  1282. end;
  1283. End;
  1284. If Not(Reg32(Pai386(p2)^.oper[1].reg) In [ProcInfo.FramePointer,R_NO,R_ESP])
  1285. Then
  1286. Begin
  1287. RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef -
  1288. [Reg32(Pai386(p2)^.oper[1].reg)];
  1289. {$ifdef csdebug}
  1290. Writeln(att_reg2str[Reg32(Pai386(p2)^.oper[1].reg)], ' removed');
  1291. {$endif csdebug}
  1292. end;
  1293. InstructionsEquivalent :=
  1294. OpsEquivalent(Pai386(p1)^.oper[0], Pai386(p2)^.oper[0], RegInfo, OpAct_Read) And
  1295. OpsEquivalent(Pai386(p1)^.oper[1], Pai386(p2)^.oper[1], RegInfo, OpAct_Write)
  1296. End
  1297. Else
  1298. {an instruction <> mov, movzx, movsx}
  1299. InstructionsEquivalent :=
  1300. OpsEquivalent(Pai386(p1)^.oper[0], Pai386(p2)^.oper[0], RegInfo, OpAct_Unknown) And
  1301. OpsEquivalent(Pai386(p1)^.oper[1], Pai386(p2)^.oper[1], RegInfo, OpAct_Unknown)
  1302. {the instructions haven't even got the same structure, so they're certainly
  1303. not equivalent}
  1304. Else
  1305. InstructionsEquivalent := False;
  1306. End;
  1307. (*
  1308. Function InstructionsEqual(p1, p2: Pai): Boolean;
  1309. Begin {checks whether two Pai386 instructions are equal}
  1310. InstructionsEqual :=
  1311. Assigned(p1) And Assigned(p2) And
  1312. ((Pai(p1)^.typ = ait_instruction) And
  1313. (Pai(p1)^.typ = ait_instruction) And
  1314. (Pai386(p1)^.opcode = Pai386(p2)^.opcode) And
  1315. (Pai386(p1)^.oper[0].typ = Pai386(p2)^.oper[0].typ) And
  1316. (Pai386(p1)^.oper[1].typ = Pai386(p2)^.oper[1].typ) And
  1317. OpsEqual(Pai386(p1)^.oper[0].typ, Pai386(p1)^.oper[0], Pai386(p2)^.oper[0]) And
  1318. OpsEqual(Pai386(p1)^.oper[1].typ, Pai386(p1)^.oper[1], Pai386(p2)^.oper[1]))
  1319. End;
  1320. *)
  1321. Function RefInInstruction(Const Ref: TReference; p: Pai): Boolean;
  1322. {checks whehter Ref is used in P}
  1323. Var TmpResult: Boolean;
  1324. Begin
  1325. TmpResult := False;
  1326. If (p^.typ = ait_instruction) Then
  1327. Begin
  1328. If (Pai386(p)^.oper[0].typ = Top_Ref) Then
  1329. TmpResult := RefsEqual(Ref, Pai386(p)^.oper[0].ref^);
  1330. If Not(TmpResult) And (Pai386(p)^.oper[1].typ = Top_Ref) Then
  1331. TmpResult := RefsEqual(Ref, Pai386(p)^.oper[1].ref^);
  1332. End;
  1333. RefInInstruction := TmpResult;
  1334. End;
  1335. Function RefInSequence(Const Ref: TReference; Content: TContent): Boolean;
  1336. {checks the whole sequence of Content (so StartMod and and the next NrOfMods
  1337. Pai objects) to see whether Ref is used somewhere}
  1338. Var p: Pai;
  1339. Counter: Byte;
  1340. TmpResult: Boolean;
  1341. Begin
  1342. p := Content.StartMod;
  1343. TmpResult := False;
  1344. Counter := 1;
  1345. While Not(TmpResult) And
  1346. (Counter <= Content.NrOfMods) Do
  1347. Begin
  1348. If (p^.typ = ait_instruction) And
  1349. RefInInstruction(Ref, p)
  1350. Then TmpResult := True;
  1351. Inc(Counter);
  1352. GetNextInstruction(p,p)
  1353. End;
  1354. RefInSequence := TmpResult
  1355. End;
  1356. Procedure DestroyRefs(p: pai; Const Ref: TReference; WhichReg: TRegister);
  1357. {destroys all registers which possibly contain a reference to Ref, WhichReg
  1358. is the register whose contents are being written to memory (if this proc
  1359. is called because of a "mov?? %reg, (mem)" instruction)}
  1360. Var Counter: TRegister;
  1361. Begin
  1362. WhichReg := Reg32(WhichReg);
  1363. If ((Ref.base = ProcInfo.FramePointer) And
  1364. (Ref.Index = R_NO)) Or
  1365. Assigned(Ref.Symbol)
  1366. Then
  1367. {write something to a parameter, a local or global variable, so
  1368. * with uncertzain optimizations on:
  1369. - destroy the contents of registers whose contents have somewhere a
  1370. "mov?? (Ref), %reg". WhichReg (this is the register whose contents
  1371. are being written to memory) is not destroyed if it's StartMod is
  1372. of that form and NrOfMods = 1 (so if it holds ref, but is not a
  1373. pointer based on Ref)
  1374. * with uncertain optimizations off:
  1375. - also destroy registers that contain any pointer}
  1376. For Counter := R_EAX to R_EDI Do
  1377. With PPaiProp(p^.OptInfo)^.Regs[Counter] Do
  1378. Begin
  1379. If (typ = Con_Ref) And
  1380. (Not(cs_UncertainOpts in aktglobalswitches) And
  1381. (NrOfMods <> 1)
  1382. ) Or
  1383. (RefInSequence(Ref,PPaiProp(p^.OptInfo)^.Regs[Counter]) And
  1384. ((Counter <> WhichReg) Or
  1385. ((NrOfMods = 1) And
  1386. {StarMod is always of the type ait_instruction}
  1387. (Pai386(StartMod)^.oper[0].typ = top_ref) And
  1388. RefsEqual(Pai386(StartMod)^.oper[0].ref^, Ref)
  1389. )
  1390. )
  1391. )
  1392. Then DestroyReg(PPaiProp(p^.OptInfo), Counter)
  1393. End
  1394. Else
  1395. {write something to a pointer location, so
  1396. * with uncertain optimzations on:
  1397. - do not destroy registers which contain a local/global variable or a
  1398. parameter, except if DestroyRefs is called because of a "movsl"
  1399. * with uncertain optimzations off:
  1400. - destroy every register which contains a memory location
  1401. }
  1402. For Counter := R_EAX to R_EDI Do
  1403. With PPaiProp(p^.OptInfo)^.Regs[Counter] Do
  1404. If (typ = Con_Ref) And
  1405. (Not(cs_UncertainOpts in aktglobalswitches) Or
  1406. {for movsl}
  1407. (Ref.Base = R_EDI) Or
  1408. {don't destroy if reg contains a parameter, local or global variable}
  1409. Not((NrOfMods = 1) And
  1410. (Pai386(StartMod)^.oper[0].typ = top_ref) And
  1411. ((Pai386(StartMod)^.oper[0].ref^.base = ProcInfo.FramePointer) Or
  1412. Assigned(Pai386(StartMod)^.oper[0].ref^.Symbol)
  1413. )
  1414. )
  1415. )
  1416. Then DestroyReg(PPaiProp(p^.OptInfo), Counter)
  1417. End;
  1418. Procedure DestroyAllRegs(p: PPaiProp);
  1419. Var Counter: TRegister;
  1420. Begin {initializes/desrtoys all registers}
  1421. For Counter := R_EAX To R_EDI Do
  1422. DestroyReg(p, Counter);
  1423. p^.DirFlag := F_Unknown;
  1424. End;
  1425. Procedure DestroyOp(PaiObj: Pai; const o:Toper);
  1426. Begin
  1427. Case o.typ Of
  1428. top_reg: DestroyReg(PPaiProp(PaiObj^.OptInfo), o.reg);
  1429. top_ref: DestroyRefs(PaiObj, o.ref^, R_NO);
  1430. top_symbol:;
  1431. End;
  1432. End;
  1433. Procedure ReadReg(p: PPaiProp; Reg: TRegister);
  1434. Begin
  1435. Reg := Reg32(Reg);
  1436. If Reg in [R_EAX..R_EDI] Then
  1437. IncState(p^.Regs[Reg32(Reg)].RState)
  1438. End;
  1439. Procedure ReadRef(p: PPaiProp; Ref: PReference);
  1440. Begin
  1441. If Ref^.Base <> R_NO Then
  1442. ReadReg(p, Ref^.Base);
  1443. If Ref^.Index <> R_NO Then
  1444. ReadReg(p, Ref^.Index);
  1445. End;
  1446. Procedure ReadOp(P: PPaiProp;const o:toper);
  1447. Begin
  1448. Case o.typ Of
  1449. top_reg: ReadReg(P, o.reg);
  1450. top_ref: ReadRef(P, o.ref);
  1451. top_symbol : ;
  1452. End;
  1453. End;
  1454. Function DFAPass1(AsmL: PAasmOutput; BlockStart: Pai): Pai;
  1455. {gathers the RegAlloc data... still need to think about where to store it to
  1456. avoid global vars}
  1457. Var BlockEnd: Pai;
  1458. Begin
  1459. BlockEnd := FindLoHiLabels(LoLab, HiLab, LabDif, BlockStart);
  1460. BuildLabelTableAndFixRegAlloc(AsmL, LTable, LoLab, LabDif, BlockStart, BlockEnd);
  1461. DFAPass1 := BlockEnd;
  1462. End;
  1463. Procedure DoDFAPass2(
  1464. {$Ifdef StateDebug}
  1465. AsmL: PAasmOutput;
  1466. {$endif statedebug}
  1467. BlockStart, BlockEnd: Pai);
  1468. {Analyzes the Data Flow of an assembler list. Starts creating the reg
  1469. contents for the instructions starting with p. Returns the last pai which has
  1470. been processed}
  1471. Var
  1472. CurProp: PPaiProp;
  1473. {$ifdef AnalyzeLoops}
  1474. TmpState: Byte;
  1475. {$endif AnalyzeLoops}
  1476. Cnt, InstrCnt : Longint;
  1477. InstrProp: TAsmInstrucProp;
  1478. UsedRegs: TRegSet;
  1479. p, hp : Pai;
  1480. TmpRef: TReference;
  1481. TmpReg: TRegister;
  1482. Begin
  1483. p := BlockStart;
  1484. UsedRegs := [];
  1485. UpdateUsedregs(UsedRegs, p);
  1486. SkipHead(P);
  1487. BlockStart := p;
  1488. InstrCnt := 1;
  1489. FillChar(NrOfInstrSinceLastMod, SizeOf(NrOfInstrSinceLastMod), 0);
  1490. While (P <> BlockEnd) Do
  1491. Begin
  1492. {$IfDef TP}
  1493. New(CurProp);
  1494. {$Else TP}
  1495. CurProp := @PaiPropBlock^[InstrCnt];
  1496. {$EndIf TP}
  1497. If (p <> BlockStart)
  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^.OptInfo)^.Regs;
  1506. CurProp^.DirFlag := PPaiProp(hp^.OptInfo)^.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. PPaiProp(p^.OptInfo) := CurProp;
  1520. {$Endif TP}
  1521. For TmpReg := R_EAX To R_EDI Do
  1522. Inc(NrOfInstrSinceLastMod[TmpReg]);
  1523. Case p^.typ Of
  1524. ait_label:
  1525. {$Ifndef JumpAnal}
  1526. If (Pai_label(p)^.l^.is_used) Then
  1527. DestroyAllRegs(CurProp);
  1528. {$Else JumpAnal}
  1529. Begin
  1530. If (Pai_Label(p)^.is_used) Then
  1531. With LTable^[Pai_Label(p)^.l^.labelnr-LoLab] Do
  1532. {$IfDef AnalyzeLoops}
  1533. If (RefsFound = Pai_Label(p)^.l^.RefCount)
  1534. {$Else AnalyzeLoops}
  1535. If (JmpsProcessed = Pai_Label(p)^.l^.RefCount)
  1536. {$EndIf AnalyzeLoops}
  1537. Then
  1538. {all jumps to this label have been found}
  1539. {$IfDef AnalyzeLoops}
  1540. If (JmpsProcessed > 0)
  1541. Then
  1542. {$EndIf AnalyzeLoops}
  1543. {we've processed at least one jump to this label}
  1544. Begin
  1545. If (GetLastInstruction(p, hp) And
  1546. Not(((hp^.typ = ait_instruction)) And
  1547. (pai386_labeled(hp)^.is_jmp))
  1548. Then
  1549. {previous instruction not a JMP -> the contents of the registers after the
  1550. previous intruction has been executed have to be taken into account as well}
  1551. For TmpReg := R_EAX to R_EDI Do
  1552. Begin
  1553. If (CurProp^.Regs[TmpReg].WState <>
  1554. PPaiProp(hp^.OptInfo)^.Regs[TmpReg].WState)
  1555. Then DestroyReg(CurProp, TmpReg)
  1556. End
  1557. End
  1558. {$IfDef AnalyzeLoops}
  1559. Else
  1560. {a label from a backward jump (e.g. a loop), no jump to this label has
  1561. already been processed}
  1562. If GetLastInstruction(p, hp) And
  1563. Not(hp^.typ = ait_instruction) And
  1564. (pai386_labeled(hp)^.opcode = A_JMP))
  1565. Then
  1566. {previous instruction not a jmp, so keep all the registers' contents from the
  1567. previous instruction}
  1568. Begin
  1569. CurProp^.Regs := PPaiProp(hp^.OptInfo)^.Regs;
  1570. CurProp^.DirFlag := PPaiProp(hp^.OptInfo)^.DirFlag;
  1571. End
  1572. Else
  1573. {previous instruction a jmp and no jump to this label processed yet}
  1574. Begin
  1575. hp := p;
  1576. Cnt := InstrCnt;
  1577. {continue until we find a jump to the label or a label which has already
  1578. been processed}
  1579. While GetNextInstruction(hp, hp) And
  1580. Not((hp^.typ = ait_instruction) And
  1581. (pai386(hp)^.is_jmp) and
  1582. (pasmlabel(pai386(hp)^.oper[0].sym)^.labelnr = Pai_Label(p)^.l^.labelnr)) And
  1583. Not((hp^.typ = ait_label) And
  1584. (LTable^[Pai_Label(hp)^.l^.labelnr-LoLab].RefsFound
  1585. = Pai_Label(hp)^.l^.RefCount) And
  1586. (LTable^[Pai_Label(hp)^.l^.labelnr-LoLab].JmpsProcessed > 0)) Do
  1587. Inc(Cnt);
  1588. If (hp^.typ = ait_label)
  1589. Then
  1590. {there's a processed label after the current one}
  1591. Begin
  1592. CurProp^.Regs := PaiPropBlock^[Cnt].Regs;
  1593. CurProp^.DirFlag := PaiPropBlock^[Cnt].DirFlag;
  1594. End
  1595. Else
  1596. {there's no label anymore after the current one, or they haven't been
  1597. processed yet}
  1598. Begin
  1599. GetLastInstruction(p, hp);
  1600. CurProp^.Regs := PPaiProp(hp^.OptInfo)^.Regs;
  1601. CurProp^.DirFlag := PPaiProp(hp^.OptInfo)^.DirFlag;
  1602. DestroyAllRegs(PPaiProp(hp^.OptInfo))
  1603. End
  1604. End
  1605. {$EndIf AnalyzeLoops}
  1606. Else
  1607. {not all references to this label have been found, so destroy all registers}
  1608. Begin
  1609. GetLastInstruction(p, hp);
  1610. CurProp^.Regs := PPaiProp(hp^.OptInfo)^.Regs;
  1611. CurProp^.DirFlag := PPaiProp(hp^.OptInfo)^.DirFlag;
  1612. DestroyAllRegs(CurProp)
  1613. End;
  1614. End;
  1615. {$EndIf JumpAnal}
  1616. {$ifdef GDB}
  1617. ait_stabs, ait_stabn, ait_stab_function_name:;
  1618. {$endif GDB}
  1619. ait_instruction:
  1620. Begin
  1621. if pai386(p)^.is_jmp then
  1622. begin
  1623. {$IfNDef JumpAnal}
  1624. ;
  1625. {$Else JumpAnal}
  1626. With LTable^[pasmlabel(pai386(p)^.oper[0].sym)^.labelnr-LoLab] Do
  1627. If (RefsFound = pasmlabel(pai386(p)^.oper[0].sym)^.RefCount) Then
  1628. Begin
  1629. If (InstrCnt < InstrNr)
  1630. Then
  1631. {forward jump}
  1632. If (JmpsProcessed = 0) Then
  1633. {no jump to this label has been processed yet}
  1634. Begin
  1635. PaiPropBlock^[InstrNr].Regs := CurProp^.Regs;
  1636. PaiPropBlock^[InstrNr].DirFlag := CurProp^.DirFlag;
  1637. Inc(JmpsProcessed);
  1638. End
  1639. Else
  1640. Begin
  1641. For TmpReg := R_EAX to R_EDI Do
  1642. If (PaiPropBlock^[InstrNr].Regs[TmpReg].WState <>
  1643. CurProp^.Regs[TmpReg].WState) Then
  1644. DestroyReg(@PaiPropBlock^[InstrNr], TmpReg);
  1645. Inc(JmpsProcessed);
  1646. End
  1647. {$ifdef AnalyzeLoops}
  1648. Else
  1649. { backward jump, a loop for example}
  1650. { If (JmpsProcessed > 0) Or
  1651. Not(GetLastInstruction(PaiObj, hp) And
  1652. (hp^.typ = ait_labeled_instruction) And
  1653. (pai386_labeled(hp)^.opcode = A_JMP))
  1654. Then}
  1655. {instruction prior to label is not a jmp, or at least one jump to the label
  1656. has yet been processed}
  1657. Begin
  1658. Inc(JmpsProcessed);
  1659. For TmpReg := R_EAX to R_EDI Do
  1660. If (PaiPropBlock^[InstrNr].Regs[TmpReg].WState <>
  1661. CurProp^.Regs[TmpReg].WState)
  1662. Then
  1663. Begin
  1664. TmpState := PaiPropBlock^[InstrNr].Regs[TmpReg].WState;
  1665. Cnt := InstrNr;
  1666. While (TmpState = PaiPropBlock^[Cnt].Regs[TmpReg].WState) Do
  1667. Begin
  1668. DestroyReg(@PaiPropBlock^[Cnt], TmpReg);
  1669. Inc(Cnt);
  1670. End;
  1671. While (Cnt <= InstrCnt) Do
  1672. Begin
  1673. Inc(PaiPropBlock^[Cnt].Regs[TmpReg].WState);
  1674. Inc(Cnt)
  1675. End
  1676. End;
  1677. End
  1678. { Else }
  1679. {instruction prior to label is a jmp and no jumps to the label have yet been
  1680. processed}
  1681. { Begin
  1682. Inc(JmpsProcessed);
  1683. For TmpReg := R_EAX to R_EDI Do
  1684. Begin
  1685. TmpState := PaiPropBlock^[InstrNr].Regs[TmpReg].WState;
  1686. Cnt := InstrNr;
  1687. While (TmpState = PaiPropBlock^[Cnt].Regs[TmpReg].WState) Do
  1688. Begin
  1689. PaiPropBlock^[Cnt].Regs[TmpReg] := CurProp^.Regs[TmpReg];
  1690. Inc(Cnt);
  1691. End;
  1692. TmpState := PaiPropBlock^[InstrNr].Regs[TmpReg].WState;
  1693. While (TmpState = PaiPropBlock^[Cnt].Regs[TmpReg].WState) Do
  1694. Begin
  1695. DestroyReg(@PaiPropBlock^[Cnt], TmpReg);
  1696. Inc(Cnt);
  1697. End;
  1698. While (Cnt <= InstrCnt) Do
  1699. Begin
  1700. Inc(PaiPropBlock^[Cnt].Regs[TmpReg].WState);
  1701. Inc(Cnt)
  1702. End
  1703. End
  1704. End}
  1705. {$endif AnalyzeLoops}
  1706. End;
  1707. {$EndIf JumpAnal}
  1708. end
  1709. else
  1710. begin
  1711. InstrProp := AsmInstr[Pai386(p)^.opcode];
  1712. Case Pai386(p)^.opcode Of
  1713. A_MOV, A_MOVZX, A_MOVSX:
  1714. Begin
  1715. Case Pai386(p)^.oper[0].typ Of
  1716. Top_Reg:
  1717. Case Pai386(p)^.oper[1].typ Of
  1718. Top_Reg:
  1719. Begin
  1720. DestroyReg(CurProp, Pai386(p)^.oper[1].reg);
  1721. ReadReg(CurProp, Pai386(p)^.oper[0].reg);
  1722. { CurProp^.Regs[Pai386(p)^.oper[1].reg] :=
  1723. CurProp^.Regs[Pai386(p)^.oper[0].reg];
  1724. If (CurProp^.Regs[Pai386(p)^.oper[1].reg].ModReg = R_NO) Then
  1725. CurProp^.Regs[Pai386(p)^.oper[1].reg].ModReg :=
  1726. Pai386(p)^.oper[0].reg;}
  1727. End;
  1728. Top_Ref:
  1729. Begin
  1730. ReadReg(CurProp, Pai386(p)^.oper[0].reg);
  1731. ReadRef(CurProp, Pai386(p)^.oper[1].ref);
  1732. DestroyRefs(p, Pai386(p)^.oper[1].ref^, Pai386(p)^.oper[0].reg);
  1733. End;
  1734. End;
  1735. Top_Ref:
  1736. Begin {destination is always a register in this case}
  1737. ReadRef(CurProp, Pai386(p)^.oper[0].ref);
  1738. ReadReg(CurProp, Pai386(p)^.oper[1].reg);
  1739. TmpReg := Reg32(Pai386(p)^.oper[1].reg);
  1740. If RegInRef(TmpReg, Pai386(p)^.oper[0].ref^) And
  1741. (CurProp^.Regs[TmpReg].Typ = Con_Ref)
  1742. Then
  1743. Begin
  1744. With CurProp^.Regs[TmpReg] Do
  1745. Begin
  1746. IncState(WState);
  1747. {also store how many instructions are part of the sequence in the first
  1748. instructions PPaiProp, so it can be easily accessed from within
  1749. CheckSequence}
  1750. Inc(NrOfMods, NrOfInstrSinceLastMod[TmpReg]);
  1751. PPaiProp(Pai(StartMod)^.OptInfo)^.Regs[TmpReg].NrOfMods := NrOfMods;
  1752. NrOfInstrSinceLastMod[TmpReg] := 0;
  1753. End;
  1754. End
  1755. Else
  1756. Begin
  1757. DestroyReg(CurProp, TmpReg);
  1758. If Not(RegInRef(TmpReg, Pai386(p)^.oper[0].ref^)) Then
  1759. With CurProp^.Regs[TmpReg] Do
  1760. Begin
  1761. Typ := Con_Ref;
  1762. StartMod := p;
  1763. NrOfMods := 1;
  1764. End
  1765. End;
  1766. {$ifdef StateDebug}
  1767. hp := new(pai_asm_comment,init(strpnew(att_reg2str[TmpReg]+': '+tostr(CurProp^.Regs[TmpReg].WState))));
  1768. InsertLLItem(AsmL, p, p^.next, hp);
  1769. {$endif StateDebug}
  1770. End;
  1771. Top_Const:
  1772. Begin
  1773. Case Pai386(p)^.oper[1].typ Of
  1774. Top_Reg:
  1775. Begin
  1776. TmpReg := Reg32(Pai386(p)^.oper[1].reg);
  1777. With CurProp^.Regs[TmpReg] Do
  1778. Begin
  1779. DestroyReg(CurProp, TmpReg);
  1780. typ := Con_Const;
  1781. StartMod := p;
  1782. End
  1783. End;
  1784. Top_Ref:
  1785. Begin
  1786. ReadRef(CurProp, Pai386(p)^.oper[1].ref);
  1787. DestroyRefs(P, Pai386(p)^.oper[1].ref^, R_NO);
  1788. End;
  1789. End;
  1790. End;
  1791. End;
  1792. End;
  1793. A_IMUL:
  1794. Begin
  1795. ReadOp(CurProp, Pai386(p)^.oper[0]);
  1796. ReadOp(CurProp, Pai386(p)^.oper[1]);
  1797. If (Pai386(p)^.oper[2].typ = top_none) Then
  1798. If (Pai386(p)^.oper[1].typ = top_none) Then
  1799. Begin
  1800. DestroyReg(CurProp, R_EAX);
  1801. DestroyReg(CurProp, R_EDX)
  1802. End
  1803. Else
  1804. DestroyOp(p, Pai386(p)^.oper[1])
  1805. Else
  1806. DestroyReg(CurProp, Pai386(p)^.oper[2].reg);
  1807. End;
  1808. A_XOR:
  1809. Begin
  1810. ReadOp(CurProp, Pai386(p)^.oper[0]);
  1811. ReadOp(CurProp, Pai386(p)^.oper[1]);
  1812. If (Pai386(p)^.oper[0].typ = top_reg) And
  1813. (Pai386(p)^.oper[1].typ = top_reg) And
  1814. (Pai386(p)^.oper[0].reg = Pai386(p)^.oper[1].reg)
  1815. Then
  1816. Begin
  1817. DestroyReg(CurProp, Pai386(p)^.oper[0].reg);
  1818. CurProp^.Regs[Reg32(Pai386(p)^.oper[0].reg)].typ := Con_Const;
  1819. CurProp^.Regs[Reg32(Pai386(p)^.oper[0].reg)].StartMod := Pointer(0)
  1820. End
  1821. Else
  1822. DestroyOp(p, Pai386(p)^.oper[1]);
  1823. End
  1824. Else
  1825. Begin
  1826. Cnt := 1;
  1827. While (Cnt <= MaxCh) And
  1828. (InstrProp.Ch[Cnt] <> C_None) Do
  1829. Begin
  1830. Case InstrProp.Ch[Cnt] Of
  1831. C_REAX..C_REDI: ReadReg(CurProp,TCh2Reg(InstrProp.Ch[Cnt]));
  1832. C_WEAX..C_RWEDI:
  1833. Begin
  1834. If (InstrProp.Ch[Cnt] >= C_RWEAX) Then
  1835. ReadReg(CurProp, TCh2Reg(InstrProp.Ch[Cnt]));
  1836. DestroyReg(CurProp, TCh2Reg(InstrProp.Ch[Cnt]));
  1837. End;
  1838. C_CDirFlag: CurProp^.DirFlag := F_NotSet;
  1839. C_SDirFlag: CurProp^.DirFlag := F_Set;
  1840. C_Rop1: ReadOp(CurProp, Pai386(p)^.oper[0]);
  1841. C_Rop2: ReadOp(CurProp, Pai386(p)^.oper[1]);
  1842. C_ROp3: ReadOp(CurProp, Pai386(p)^.oper[2]);
  1843. C_Wop1..C_RWop1:
  1844. Begin
  1845. If (InstrProp.Ch[Cnt] = C_RWop1) Then
  1846. ReadOp(CurProp, Pai386(p)^.oper[0]);
  1847. DestroyOp(p, Pai386(p)^.oper[0]);
  1848. End;
  1849. C_Wop2..C_RWop2:
  1850. Begin
  1851. If (InstrProp.Ch[Cnt] = C_RWop2) Then
  1852. ReadOp(CurProp, Pai386(p)^.oper[1]);
  1853. DestroyOp(p, Pai386(p)^.oper[1]);
  1854. End;
  1855. C_WOp3..C_RWOp3:
  1856. Begin
  1857. If (InstrProp.Ch[Cnt] = C_RWOp3) Then
  1858. ReadOp(CurProp, Pai386(p)^.oper[2]);
  1859. DestroyOp(p, Pai386(p)^.oper[2]);
  1860. End;
  1861. C_WMemEDI:
  1862. Begin
  1863. ReadReg(CurProp, R_EDI);
  1864. FillChar(TmpRef, SizeOf(TmpRef), 0);
  1865. TmpRef.Base := R_EDI;
  1866. DestroyRefs(p, TmpRef, R_NO)
  1867. End;
  1868. C_RFlags, C_WFlags, C_RWFlags, C_FPU:
  1869. Else
  1870. Begin
  1871. DestroyAllRegs(CurProp);
  1872. End;
  1873. End;
  1874. Inc(Cnt);
  1875. End
  1876. End;
  1877. end;
  1878. End;
  1879. End
  1880. Else
  1881. Begin
  1882. DestroyAllRegs(CurProp);
  1883. End;
  1884. End;
  1885. Inc(InstrCnt);
  1886. GetNextInstruction(p, p);
  1887. End;
  1888. End;
  1889. Function InitDFAPass2(BlockStart, BlockEnd: Pai): Boolean;
  1890. {reserves memory for the PPaiProps in one big memory block when not using
  1891. TP, returns False if not enough memory is available for the optimizer in all
  1892. cases}
  1893. Var p: Pai;
  1894. Count: Longint;
  1895. { TmpStr: String; }
  1896. Begin
  1897. P := BlockStart;
  1898. SkipHead(P);
  1899. NrOfPaiObjs := 0;
  1900. While (P <> BlockEnd) Do
  1901. Begin
  1902. {$IfDef JumpAnal}
  1903. Case P^.Typ Of
  1904. ait_label:
  1905. Begin
  1906. If (Pai_Label(p)^.l^.is_used) Then
  1907. LTable^[Pai_Label(P)^.l^.labelnr-LoLab].InstrNr := NrOfPaiObjs
  1908. End;
  1909. ait_instruction:
  1910. begin
  1911. if pai386(p)^.is_jmp then
  1912. begin
  1913. If (pasmlabel(pai386(P)^.oper[0].sym)^.labelnr >= LoLab) And
  1914. (pasmlabel(pai386(P)^.oper[0].sym)^.labelnr <= HiLab) Then
  1915. Inc(LTable^[pasmlabel(pai386(P)^.oper[0].sym)^.labelnr-LoLab].RefsFound);
  1916. end;
  1917. end;
  1918. { ait_instruction:
  1919. Begin
  1920. If (Pai386(p)^.opcode = A_PUSH) And
  1921. (Pai386(p)^.oper[0].typ = top_symbol) And
  1922. (PCSymbol(Pai386(p)^.oper[0])^.offset = 0) Then
  1923. Begin
  1924. TmpStr := StrPas(PCSymbol(Pai386(p)^.oper[0])^.symbol);
  1925. If}
  1926. End;
  1927. {$EndIf JumpAnal}
  1928. Inc(NrOfPaiObjs);
  1929. GetNextInstruction(p, p);
  1930. End;
  1931. {$IfDef TP}
  1932. If (MemAvail < (SizeOf(TPaiProp)*NrOfPaiObjs))
  1933. Or (NrOfPaiObjs = 0)
  1934. {this doesn't have to be one contiguous block}
  1935. Then InitDFAPass2 := False
  1936. Else InitDFAPass2 := True;
  1937. {$Else}
  1938. {Uncomment the next line to see how much memory the reloading optimizer needs}
  1939. { Writeln((NrOfPaiObjs*(((SizeOf(TPaiProp)+3)div 4)*4)));}
  1940. {no need to check mem/maxavail, we've got as much virtual memory as we want}
  1941. If NrOfPaiObjs <> 0 Then
  1942. Begin
  1943. InitDFAPass2 := True;
  1944. GetMem(PaiPropBlock, NrOfPaiObjs*(((SizeOf(TPaiProp)+3)div 4)*4));
  1945. p := BlockStart;
  1946. SkipHead(p);
  1947. For Count := 1 To NrOfPaiObjs Do
  1948. Begin
  1949. PPaiProp(p^.OptInfo) := @PaiPropBlock^[Count];
  1950. GetNextInstruction(p, p);
  1951. End;
  1952. End
  1953. Else InitDFAPass2 := False;
  1954. {$EndIf TP}
  1955. End;
  1956. Function DFAPass2(
  1957. {$ifdef statedebug}
  1958. AsmL: PAasmOutPut;
  1959. {$endif statedebug}
  1960. BlockStart, BlockEnd: Pai): Boolean;
  1961. Begin
  1962. If InitDFAPass2(BlockStart, BlockEnd) Then
  1963. Begin
  1964. DoDFAPass2(
  1965. {$ifdef statedebug}
  1966. asml,
  1967. {$endif statedebug}
  1968. BlockStart, BlockEnd);
  1969. DFAPass2 := True
  1970. End
  1971. Else DFAPass2 := False;
  1972. End;
  1973. Procedure ShutDownDFA;
  1974. Begin
  1975. If LabDif <> 0 Then
  1976. FreeMem(LTable, LabDif*SizeOf(TLabelTableItem));
  1977. End;
  1978. End.
  1979. {
  1980. $Log$
  1981. Revision 1.48 1999-07-01 18:21:21 jonas
  1982. * removed unused AsmL parameter from FindLoHiLabels
  1983. Revision 1.47 1999/05/27 19:44:24 peter
  1984. * removed oldasm
  1985. * plabel -> pasmlabel
  1986. * -a switches to source writing automaticly
  1987. * assembler readers OOPed
  1988. * asmsymbol automaticly external
  1989. * jumptables and other label fixes for asm readers
  1990. Revision 1.46 1999/05/08 20:40:02 jonas
  1991. * seperate OPTimizer INFO pointer field in tai object
  1992. * fix to GetLastInstruction that sometimes caused a crash
  1993. Revision 1.45 1999/05/01 13:48:37 peter
  1994. * merged nasm compiler
  1995. Revision 1.6 1999/04/18 17:57:21 jonas
  1996. * fix for crash when the first instruction of a sequence that gets
  1997. optimized is removed (this situation can't occur aymore now)
  1998. Revision 1.5 1999/04/16 11:49:50 peter
  1999. + tempalloc
  2000. + -at to show temp alloc info in .s file
  2001. Revision 1.4 1999/04/14 09:07:42 peter
  2002. * asm reader improvements
  2003. Revision 1.3 1999/03/31 13:55:29 peter
  2004. * assembler inlining working for ag386bin
  2005. Revision 1.2 1999/03/29 16:05:46 peter
  2006. * optimizer working for ag386bin
  2007. Revision 1.1 1999/03/26 00:01:10 peter
  2008. * first things for optimizer (compiles but cycle crashes)
  2009. Revision 1.39 1999/02/26 00:48:18 peter
  2010. * assembler writers fixed for ag386bin
  2011. Revision 1.38 1999/02/25 21:02:34 peter
  2012. * ag386bin updates
  2013. + coff writer
  2014. Revision 1.37 1999/02/22 02:15:20 peter
  2015. * updates for ag386bin
  2016. Revision 1.36 1999/01/20 17:41:26 jonas
  2017. * small bugfix (memory corruption could occur when certain fpu instructions
  2018. were encountered)
  2019. Revision 1.35 1999/01/08 12:39:22 florian
  2020. Changes of Alexander Stohr integrated:
  2021. + added KNI opcodes
  2022. + added KNI registers
  2023. + added 3DNow! opcodes
  2024. + added 64 bit and 128 bit register flags
  2025. * translated a few comments into english
  2026. Revision 1.34 1998/12/29 18:48:19 jonas
  2027. + optimize pascal code surrounding assembler blocks
  2028. Revision 1.33 1998/12/17 16:37:38 jonas
  2029. + extra checks in RegsEquivalent so some more optimizations can be done (which
  2030. where disabled by the second fix from revision 1.22)
  2031. Revision 1.32 1998/12/15 19:33:58 jonas
  2032. * uncommented OpsEqual & added to interface because popt386 uses it now
  2033. Revision 1.31 1998/12/11 00:03:13 peter
  2034. + globtype,tokens,version unit splitted from globals
  2035. Revision 1.30 1998/12/02 16:23:39 jonas
  2036. * changed "if longintvar in set" to case or "if () or () .." statements
  2037. * tree.pas: changed inlinenumber (and associated constructor/vars) to a byte
  2038. Revision 1.29 1998/11/26 21:45:31 jonas
  2039. - removed A_CLTD opcode (use A_CDQ instead)
  2040. * changed cbw, cwde and cwd to cbtw, cwtl and cwtd in att_.oper[1]str array
  2041. * in daopt386: adapted AsmInstr array to reflect changes + fixed line too long
  2042. Revision 1.27 1998/11/24 19:47:22 jonas
  2043. * fixed problems posible with 3 operand instructions
  2044. Revision 1.26 1998/11/24 12:50:09 peter
  2045. * fixed crash
  2046. Revision 1.25 1998/11/18 17:58:22 jonas
  2047. + gathering of register reading data, nowhere used yet (necessary for instruction scheduling)
  2048. Revision 1.24 1998/11/13 10:13:44 peter
  2049. + cpuid,emms support for asm readers
  2050. Revision 1.23 1998/11/09 19:40:46 jonas
  2051. * fixed comments from last commit (apparently there's still a 255 char limit :( )
  2052. Revision 1.22 1998/11/09 19:33:40 jonas
  2053. * changed specific bugfix (which was actually wrong implemented, but
  2054. did the right thing in most cases nevertheless) to general bugfix
  2055. * fixed bug that caused
  2056. mov (ebp), edx mov (ebp), edx
  2057. mov (edx), edx mov (edx), edx
  2058. ... being changed to ...
  2059. mov (ebp), edx mov edx, eax
  2060. mov (eax), eax
  2061. but this disabled another small correct optimization...
  2062. Revision 1.21 1998/11/02 23:17:49 jonas
  2063. * fixed bug shown in sortbug program from fpc-devel list
  2064. Revision 1.20 1998/10/22 13:24:51 jonas
  2065. * changed TRegSet to a small set
  2066. Revision 1.19 1998/10/20 09:29:24 peter
  2067. * bugfix so that code like
  2068. movl 48(%esi),%esi movl 48(%esi),%esi
  2069. pushl %esi doesn't get changed to pushl %esi
  2070. movl 48(%esi),%edi movl %esi,%edi
  2071. Revision 1.18 1998/10/07 16:27:02 jonas
  2072. * changed state to WState (WriteState), added RState for future use in
  2073. instruction scheduling
  2074. * RegAlloc data from the CG is now completely being patched and corrected (I
  2075. think)
  2076. Revision 1.17 1998/10/02 17:30:20 jonas
  2077. * small patches to regdealloc data
  2078. Revision 1.16 1998/10/01 20:21:47 jonas
  2079. * inter-register CSE, still requires some tweaks (peepholeoptpass2, better RegAlloc)
  2080. Revision 1.15 1998/09/20 18:00:20 florian
  2081. * small compiling problems fixed
  2082. Revision 1.14 1998/09/20 17:12:36 jonas
  2083. * small fix for uncertain optimizations & more cleaning up
  2084. Revision 1.12 1998/09/16 18:00:01 jonas
  2085. * optimizer now completely dependant on GetNext/GetLast instruction, works again with -dRegAlloc
  2086. Revision 1.11 1998/09/15 14:05:27 jonas
  2087. * fixed optimizer incompatibilities with freelabel code in psub
  2088. Revision 1.10 1998/09/09 15:33:58 peter
  2089. * removed warnings
  2090. Revision 1.9 1998/09/03 16:24:51 florian
  2091. * bug of type conversation from dword to real fixed
  2092. * bug fix of Jonas applied
  2093. Revision 1.8 1998/08/28 10:56:59 peter
  2094. * removed warnings
  2095. Revision 1.7 1998/08/19 16:07:44 jonas
  2096. * changed optimizer switches + cleanup of DestroyRefs in daopt386.pas
  2097. Revision 1.6 1998/08/10 14:49:57 peter
  2098. + localswitches, moduleswitches, globalswitches splitting
  2099. Revision 1.5 1998/08/09 13:56:24 jonas
  2100. * small bugfix for uncertain optimizations in DestroyRefs
  2101. Revision 1.4 1998/08/06 19:40:25 jonas
  2102. * removed $ before and after Log in comment
  2103. Revision 1.3 1998/08/05 16:00:14 florian
  2104. * some fixes for ansi strings
  2105. * log to Log changed
  2106. }