daopt386.pas 82 KB

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