daopt386.pas 95 KB

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