daopt386.pas 98 KB

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