daopt386.pas 84 KB

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