daopt386.pas 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655
  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 AAsm, CObjects
  25. {$ifdef i386}
  26. ,i386
  27. {$endif}
  28. ;
  29. {*********************** Procedures and Functions ************************}
  30. Procedure InsertLLItem(AsmL: PAasmOutput; prev, foll, new_one: PLinkedList_Item);
  31. Function Reg32(Reg: TRegister): TRegister;
  32. Function RefsEqual(Const R1, R2: TReference): Boolean;
  33. Function IsGP32Reg(Reg: TRegister): Boolean;
  34. Function RegInRef(Reg: TRegister; Const Ref: TReference): Boolean;
  35. Function RegInInstruction(Reg: TRegister; p1: Pai): Boolean;
  36. Function GetNextInstruction(Current: Pai; Var Next: Pai): Boolean;
  37. Function GetLastInstruction(Current: Pai; Var Last: Pai): Boolean;
  38. Function RegsSameContent(p1, p2: Pai; Reg: TRegister): Boolean;
  39. Function InstructionsEqual(p1, p2: Pai): Boolean;
  40. Procedure DFAPass1(AsmL: PAasmOutput);
  41. Function DFAPass2(AsmL: PAasmOutput): Pai;
  42. Procedure ShutDownDFA;
  43. Function FindLabel(L: PLabel; Var hp: Pai): Boolean;
  44. {Procedure FindLoHiLabels(AsmL: PAasmOutput; Var LoLab, HiLab, LabDif: Longint);}
  45. {******************************* Constants *******************************}
  46. Const
  47. {ait_* types which don't result in executable code or which don't influence
  48. the way the program runs/behaves}
  49. SkipInstr = [ait_comment
  50. {$ifdef GDB}
  51. ,ait_stabs, ait_stabn, ait_stab_function_name
  52. {$endif GDB}
  53. ,ait_regalloc, ait_regdealloc
  54. ];
  55. {the maximum number of things (registers, memory, ...) a single instruction
  56. changes}
  57. MaxCh = 3;
  58. {Possible register content types}
  59. con_Unknown = 0;
  60. con_ref = 1;
  61. con_const = 2;
  62. {********************************* Types *********************************}
  63. Type
  64. TRegArray = Array[R_EAX..R_EDI] of TRegister;
  65. TRegSet = Set of TRegister;
  66. TRegInfo = Record
  67. RegsEncountered: TRegSet;
  68. SubstRegs: TRegArray;
  69. End;
  70. {What an instruction can change}
  71. TChange = (C_None,
  72. C_REAX, C_RECX, C_REDX, C_REBX, C_RESP, C_REBP, C_RESI, C_REDI,
  73. C_WEAX, C_WECX, C_WEDX, C_WEBX, C_WESP, C_WEBP, C_WESI, C_WEDI,
  74. C_RWEAX, C_RWECX, C_RWEDX, C_RWEBX, C_RWESP, C_RWEBP, C_RWESI, C_RWEDI,
  75. C_CDirFlag {clear direction flag}, C_SDirFlag {set dir flag},
  76. C_Flags, C_FPU, C_Op1, C_Op2, C_Op3, C_MemEDI, C_All);
  77. {the possible states of a flag}
  78. TFlagContents = (F_Unknown, F_NotSet, F_Set);
  79. {the properties of a cpu instruction}
  80. TAsmInstrucProp = Record
  81. {how many things it changes}
  82. { NCh: Byte;}
  83. {and what it changes}
  84. Ch: Array[1..MaxCh] of TChange;
  85. End;
  86. TContent = Record
  87. {start and end of block instructions that defines the
  88. content of this register. If Typ = con_const, then
  89. Longint(StartMod) = value of the constant)}
  90. StartMod: Pointer;
  91. {starts at 1, gets increased everytime the register is modified}
  92. State: Word;
  93. {how many instructions starting with StarMod does the block consist of}
  94. NrOfMods: Byte;
  95. {if one register gets a block assigned from an other register,
  96. this variable holds the name of that register (so it can be
  97. substituted when checking the block afterwards)}
  98. { ModReg: TRegister; }
  99. {the type of the content of the register: constant, ...}
  100. Typ: Byte;
  101. End;
  102. {Contents of the integer registers}
  103. TRegContent = Array[R_EAX..R_EDI] Of TContent;
  104. {contents of the FPU registers}
  105. TRegFPUContent = Array[R_ST..R_ST7] Of TContent;
  106. {information record with the contents of every register. Every Pai object
  107. gets one of these assigned: a pointer to it is stored in the Line field and
  108. the original line number is stored in LineSave}
  109. TPaiProp = Record
  110. Regs: TRegContent;
  111. { FPURegs: TRegFPUContent;} {currently not yet used}
  112. LineSave: Longint;
  113. {status of the direction flag}
  114. DirFlag: TFlagContents;
  115. {can this instruction be removed?}
  116. CanBeRemoved: Boolean;
  117. End;
  118. PPaiProp = ^TPaiProp;
  119. {$IfDef TP}
  120. TPaiPropBlock = Array[1..(65520 div (((SizeOf(TPaiProp)+1)div 2)*2))] Of TPaiProp;
  121. {$else}
  122. TPaiPropBlock = Array[1..250000] Of TPaiProp;
  123. {$EndIf TP}
  124. PPaiPropBlock = ^TPaiPropBlock;
  125. TInstrSinceLastMod = Array[R_EAX..R_EDI] Of Byte;
  126. TLabelTableItem = Record
  127. PaiObj: Pai;
  128. {$IfNDef TP}
  129. InstrNr: Longint;
  130. RefsFound: Word;
  131. JmpsProcessed: Word
  132. {$EndIf TP}
  133. End;
  134. {$IfDef tp}
  135. TLabelTable = Array[0..10000] Of TLabelTableItem;
  136. {$Else tp}
  137. TLabelTable = Array[0..2500000] Of TLabelTableItem;
  138. {$Endif tp}
  139. PLabelTable = ^TLabelTable;
  140. TwoWords = Record
  141. Word1, Word2: Word;
  142. End;
  143. {******************************* Variables *******************************}
  144. Var
  145. {the amount of PaiObjects in the current assembler list}
  146. NrOfPaiObjs: Longint;
  147. {Array which holds all (FPC) or as much as possible (TP) PPaiProps}
  148. PaiPropBlock: PPaiPropBlock;
  149. LoLab, HiLab, LabDif: Longint;
  150. LTable: PLabelTable;
  151. {*********************** End of Interface section ************************}
  152. Implementation
  153. Uses globals, systems, strings, verbose, hcodegen,
  154. {$ifdef i386}
  155. pass_2;
  156. {$endif i386}
  157. Const AsmInstr: Array[tasmop] Of TAsmInstrucProp = (
  158. {MOV} (Ch: (C_Op2, C_None, C_None)),
  159. {MOVZX} (Ch: (C_Op2, C_None, C_None)),
  160. {MOVSX} (Ch: (C_Op2, C_None, C_None)),
  161. {LABEL} (Ch: (C_All, C_None, C_None)), {don't know value of any register}
  162. {ADD} (Ch: (C_Op2, C_Flags, C_None)),
  163. {CALL} (Ch: (C_All, C_None, C_None)), {don't know value of any register}
  164. {IDIV} (Ch: (C_WEAX, C_WEDX, C_Flags)),
  165. {IMUL} (Ch: (C_WEAX, C_WEDX, C_Flags)), {handled separately, because several forms exist}
  166. {JMP} (Ch: (C_None, C_None, C_None)),
  167. {LEA} (Ch: (C_Op2, C_None, C_None)),
  168. {MUL} (Ch: (C_RWEAX, C_WEDX, C_Flags)),
  169. {NEG} (Ch: (C_Op1, C_None, C_None)),
  170. {NOT} (Ch: (C_Op1, C_Flags, C_None)),
  171. {POP} (Ch: (C_Op1, C_RWESP, C_None)),
  172. {POPAD} (Ch: (C_None, C_None, C_None)), {don't know value of any register}
  173. {PUSH} (Ch: (C_RWESP, C_None, C_None)),
  174. {PUSHAD} (Ch: (C_RWESP, C_None, C_None)),
  175. {RET} (Ch: (C_None, C_None, C_None)), {don't know value of any register}
  176. {SUB} (Ch: (C_Op2, C_Flags, C_None)),
  177. {XCHG} (Ch: (C_Op1, C_Op2, C_None)), {(will be) handled seperately}
  178. {XOR} (Ch: (C_Op2, C_Flags, C_None)),
  179. {FILD} (Ch: (C_FPU, C_None, C_None)),
  180. {CMP} (Ch: (C_Flags, C_None, C_None)),
  181. {JZ} (Ch: (C_None, C_None, C_None)),
  182. {INC} (Ch: (C_Op1, C_Flags, C_None)),
  183. {DEC} (Ch: (C_Op1, C_Flags, C_None)),
  184. {SETE} (Ch: (C_Op1, C_None, C_None)),
  185. {SETNE} (Ch: (C_Op1, C_None, C_None)),
  186. {SETL} (Ch: (C_Op1, C_None, C_None)),
  187. {SETG} (Ch: (C_Op1, C_None, C_None)),
  188. {SETLE} (Ch: (C_Op1, C_None, C_None)),
  189. {SETGE} (Ch: (C_Op1, C_None, C_None)),
  190. {JE} (Ch: (C_None, C_None, C_None)),
  191. {JNE} (Ch: (C_None, C_None, C_None)),
  192. {JL} (Ch: (C_None, C_None, C_None)),
  193. {JG} (Ch: (C_None, C_None, C_None)),
  194. {JLE} (Ch: (C_None, C_None, C_None)),
  195. {JGE} (Ch: (C_None, C_None, C_None)),
  196. {OR} (Ch: (C_Op2, C_Flags, C_None)),
  197. {FLD} (Ch: (C_FPU, C_None, C_None)),
  198. {FADD} (Ch: (C_FPU, C_None, C_None)),
  199. {FMUL} (Ch: (C_FPU, C_None, C_None)),
  200. {FSUB} (Ch: (C_FPU, C_None, C_None)),
  201. {FDIV} (Ch: (C_FPU, C_None, C_None)),
  202. {FCHS} (Ch: (C_FPU, C_None, C_None)),
  203. {FLD1} (Ch: (C_FPU, C_None, C_None)),
  204. {FIDIV} (Ch: (C_FPU, C_None, C_None)),
  205. {CLTD} (Ch: (C_WEDX, C_None, C_None)),
  206. {JNZ} (Ch: (C_None, C_None, C_None)),
  207. {FSTP} (Ch: (C_Op1, C_None, C_None)),
  208. {AND} (Ch: (C_Op2, C_Flags, C_None)),
  209. {JNO} (Ch: (C_None, C_None, C_None)),
  210. {NOTH} (Ch: (C_None, C_None, C_None)), {***???***}
  211. {NONE} (Ch: (C_None, C_None, C_None)),
  212. {ENTER} (Ch: (C_RWESP, C_None, C_None)),
  213. {LEAVE} (Ch: (C_RWESP, C_None, C_None)),
  214. {CLD} (Ch: (C_CDirFlag, C_None, C_None)),
  215. {MOVS} (Ch: (C_RWESI, C_RWEDI, C_MemEDI)),
  216. {REP} (Ch: (C_RWECX, C_None, C_None)),
  217. {SHL} (Ch: (C_Op2, C_Flags, C_None)),
  218. {SHR} (Ch: (C_Op2, C_Flags, C_None)),
  219. {BOUND} (Ch: (C_None, C_None, C_None)),
  220. {JNS} (Ch: (C_None, C_None, C_None)),
  221. {JS} (Ch: (C_None, C_None, C_None)),
  222. {JO} (Ch: (C_None, C_None, C_None)),
  223. {SAR} (Ch: (C_Op2, C_Flags, C_None)),
  224. {TEST} (Ch: (C_Flags, C_None, C_None)),
  225. {FCOM} (Ch: (C_FPU, C_None, C_None)),
  226. {FCOMP} (Ch: (C_FPU, C_None, C_None)),
  227. {FCOMPP} (Ch: (C_FPU, C_None, C_None)),
  228. {FXCH} (Ch: (C_FPU, C_None, C_None)),
  229. {FADDP} (Ch: (C_FPU, C_None, C_None)),
  230. {FMULP} (Ch: (C_FPU, C_None, C_None)),
  231. {FSUBP} (Ch: (C_FPU, C_None, C_None)),
  232. {FDIVP} (Ch: (C_FPU, C_None, C_None)),
  233. {FNSTS} (Ch: (C_Op1, C_None, C_None)),
  234. {SAHF} (Ch: (C_Flags, C_None, C_None)),
  235. {FDIVRP} (Ch: (C_FPU, C_None, C_None)),
  236. {FSUBRP} (Ch: (C_FPU, C_None, C_None)),
  237. {SETC} (Ch: (C_Op1, C_None, C_None)),
  238. {SETNC} (Ch: (C_Op1, C_None, C_None)),
  239. {JC} (Ch: (C_None, C_None, C_None)),
  240. {JNC} (Ch: (C_None, C_None, C_None)),
  241. {JA} (Ch: (C_None, C_None, C_None)),
  242. {JAE} (Ch: (C_None, C_None, C_None)),
  243. {JB} (Ch: (C_None, C_None, C_None)),
  244. {JBE} (Ch: (C_None, C_None, C_None)),
  245. {SETA} (Ch: (C_Op1, C_None, C_None)),
  246. {SETAE} (Ch: (C_Op1, C_None, C_None)),
  247. {SETB} (Ch: (C_Op1, C_None, C_None)),
  248. {SETBE} (Ch: (C_Op1, C_None, C_None)),
  249. {AAA} (Ch: (C_RWEAX, C_Flags, C_None)),
  250. {AAD} (Ch: (C_RWEAX, C_Flags, C_None)),
  251. {AAM} (Ch: (C_RWEAX, C_Flags, C_None)),
  252. {AAS} (Ch: (C_RWEAX, C_Flags, C_None)),
  253. {CBW} (Ch: (C_RWEAX, C_None, C_None)),
  254. {CDQ} (Ch: (C_RWEAX, C_WEDX, C_None)),
  255. {CLC} (Ch: (C_Flags, C_None, C_None)),
  256. {CLI} (Ch: (C_Flags, C_None, C_None)),
  257. {CLTS} (Ch: (C_None, C_None, C_None)),
  258. {CMC} (Ch: (C_Flags, C_None, C_None)),
  259. {CWD} (Ch: (C_RWEAX, C_WEDX, C_None)),
  260. {CWDE} (Ch: (C_RWEAX, C_None, C_None)),
  261. {DAA} (Ch: (C_RWEAX, C_None, C_None)),
  262. {DAS} (Ch: (C_RWEAX, C_None, C_None)),
  263. {HLT} (Ch: (C_None, C_None, C_None)),
  264. {IRET} (Ch: (C_All, C_None, C_None)), {don't know value of any register}
  265. {LAHF} (Ch: (C_WEAX, C_None, C_None)),
  266. {LODS} (Ch: (C_WEAX, C_RWESI, C_None)),
  267. {LOCK} (Ch: (C_None, C_None, C_None)),
  268. {NOP} (Ch: (C_None, C_None, C_None)),
  269. {PUSHA} (Ch: (C_RWESP, C_None, C_None)),
  270. {PUSHF} (Ch: (C_RWESP, C_None, C_None)),
  271. {PUSHFD} (Ch: (C_RWESP, C_None, C_None)),
  272. {STC} (Ch: (C_Flags, C_None, C_None)),
  273. {STD} (Ch: (C_SDirFlag, C_None, C_None)),
  274. {STI} (Ch: (C_Flags, C_None, C_None)),
  275. {STOS} (Ch: (C_MemEDI, C_RWEDI, C_None)),
  276. {WAIT} (Ch: (C_None, C_None, C_None)),
  277. {XLAT} (Ch: (C_WEAX, C_None, C_None)),
  278. {XLATB} (Ch: (C_WEAX, C_None, C_None)),
  279. {MOVSB} (Ch: (C_Op2, C_None, C_None)),
  280. {MOVSBL} (Ch: (C_Op2, C_None, C_None)),
  281. {MOVSBW} (Ch: (C_Op2, C_None, C_None)),
  282. {MOVSWL} (Ch: (C_Op2, C_None, C_None)),
  283. {MOVZB} (Ch: (C_Op2, C_None, C_None)),
  284. {MOVZWL} (Ch: (C_Op2, C_None, C_None)),
  285. {POPA} (Ch: (C_None, C_None, C_None)), {don't know value of any register}
  286. {IN} (Ch: (C_Op2, C_None, C_None)),
  287. {OUT} (Ch: (C_None, C_None, C_None)),
  288. {LDS} (Ch: (C_Op2, C_None, C_None)),
  289. {LCS} (Ch: (C_Op2, C_None, C_None)),
  290. {LES} (Ch: (C_Op2, C_None, C_None)),
  291. {LFS} (Ch: (C_Op2, C_None, C_None)),
  292. {LGS} (Ch: (C_Op2, C_None, C_None)),
  293. {LSS} (Ch: (C_Op2, C_None, C_None)),
  294. {POPF} (Ch: (C_RWESP, C_Flags, C_None)),
  295. {SBB} (Ch: (C_Op2, C_Flags, C_None)),
  296. {ADC} (Ch: (C_Op2, C_Flags, C_None)),
  297. {DIV} (Ch: (C_RWEAX, C_WEDX, C_Flags)),
  298. {ROR} (Ch: (C_Op2, C_Flags, C_None)),
  299. {ROL} (Ch: (C_Op2, C_Flags, C_None)),
  300. {RCL} (Ch: (C_Op2, C_Flags, C_None)),
  301. {RCR} (Ch: (C_Op2, C_Flags, C_None)),
  302. {SAL} (Ch: (C_Op2, C_Flags, C_None)),
  303. {SHLD} (Ch: (C_Op3, C_Flags, C_None)),
  304. {SHRD} (Ch: (C_Op3, C_Flags, C_None)),
  305. {LCALL} (Ch: (C_All, C_None, C_None)), {don't know value of any register}
  306. {LJMP} (Ch: (C_All, C_None, C_None)), {don't know value of any register}
  307. {LRET} (Ch: (C_All, C_None, C_None)), {don't know value of any register}
  308. {JNAE} (Ch: (C_None, C_None, C_None)),
  309. {JNB} (Ch: (C_None, C_None, C_None)),
  310. {JNA} (Ch: (C_None, C_None, C_None)),
  311. {JNBE} (Ch: (C_None, C_None, C_None)),
  312. {JP} (Ch: (C_None, C_None, C_None)),
  313. {JNP} (Ch: (C_None, C_None, C_None)),
  314. {JPE} (Ch: (C_None, C_None, C_None)),
  315. {JPO} (Ch: (C_None, C_None, C_None)),
  316. {JNGE} (Ch: (C_None, C_None, C_None)),
  317. {JNG} (Ch: (C_None, C_None, C_None)),
  318. {JNL} (Ch: (C_None, C_None, C_None)),
  319. {JNLE} (Ch: (C_None, C_None, C_None)),
  320. {JCXZ} (Ch: (C_None, C_None, C_None)),
  321. {JECXZ} (Ch: (C_None, C_None, C_None)),
  322. {LOOP} (Ch: (C_RWECX, C_None, C_None)),
  323. {CMPS} (Ch: (C_RWESI, C_RWEDI, C_Flags)),
  324. {INS} (Ch: (C_RWEDI, C_MemEDI, C_None)),
  325. {OUTS} (Ch: (C_RWESI, C_None, C_None)),
  326. {SCAS} (Ch: (C_RWEDI, C_Flags, C_None)),
  327. {BSF} (Ch: (C_Op2, C_Flags, C_None)),
  328. {BSR} (Ch: (C_Op2, C_Flags, C_None)),
  329. {BT} (Ch: (C_Flags, C_None, C_None)),
  330. {BTC} (Ch: (C_Op2, C_Flags, C_None)),
  331. {BTR} (Ch: (C_Op2, C_Flags, C_None)),
  332. {BTS} (Ch: (C_Op2, C_Flags, C_None)),
  333. {INT} (Ch: (C_All, C_None, C_None)), {don't know value of any register}
  334. {INT3} (Ch: (C_None, C_None, C_None)),
  335. {INTO} (Ch: (C_All, C_None, C_None)), {don't know value of any register}
  336. {BOUNDL} (Ch: (C_None, C_None, C_None)),
  337. {BOUNDW} (Ch: (C_None, C_None, C_None)),
  338. {LOOPZ} (Ch: (C_RWECX, C_None, C_None)),
  339. {LOOPE} (Ch: (C_RWECX, C_None, C_None)),
  340. {LOOPNZ} (Ch: (C_RWECX, C_None, C_None)),
  341. {LOOPNE} (Ch: (C_RWECX, C_None, C_None)),
  342. {SETO} (Ch: (C_Op1, C_None, C_None)),
  343. {SETNO} (Ch: (C_Op1, C_None, C_None)),
  344. {SETNAE} (Ch: (C_Op1, C_None, C_None)),
  345. {SETNB} (Ch: (C_Op1, C_None, C_None)),
  346. {SETZ} (Ch: (C_Op1, C_None, C_None)),
  347. {SETNZ} (Ch: (C_Op1, C_None, C_None)),
  348. {SETNA} (Ch: (C_Op1, C_None, C_None)),
  349. {SETNBE} (Ch: (C_Op1, C_None, C_None)),
  350. {SETS} (Ch: (C_Op1, C_None, C_None)),
  351. {SETNS} (Ch: (C_Op1, C_None, C_None)),
  352. {SETP} (Ch: (C_Op1, C_None, C_None)),
  353. {SETPE} (Ch: (C_Op1, C_None, C_None)),
  354. {SETNP} (Ch: (C_Op1, C_None, C_None)),
  355. {SETPO} (Ch: (C_Op1, C_None, C_None)),
  356. {SETNGE} (Ch: (C_Op1, C_None, C_None)),
  357. {SETNL} (Ch: (C_Op1, C_None, C_None)),
  358. {SETNG} (Ch: (C_Op1, C_None, C_None)),
  359. {SETNLE} (Ch: (C_Op1, C_None, C_None)),
  360. {ARPL} (Ch: (C_Flags, C_None, C_None)),
  361. {LAR} (Ch: (C_Op2, C_None, C_None)),
  362. {LGDT} (Ch: (C_None, C_None, C_None)),
  363. {LIDT} (Ch: (C_None, C_None, C_None)),
  364. {LLDT} (Ch: (C_None, C_None, C_None)),
  365. {LMSW} (Ch: (C_None, C_None, C_None)),
  366. {LSL} (Ch: (C_Op2, C_Flags, C_None)),
  367. {LTR} (Ch: (C_None, C_None, C_None)),
  368. {SGDT} (Ch: (C_Op1, C_None, C_None)),
  369. {SIDT} (Ch: (C_Op1, C_None, C_None)),
  370. {SLDT} (Ch: (C_Op1, C_None, C_None)),
  371. {SMSW} (Ch: (C_Op1, C_None, C_None)),
  372. {STR} (Ch: (C_Op1, C_None, C_None)),
  373. {VERR} (Ch: (C_Flags, C_None, C_None)),
  374. {VERW} (Ch: (C_Flags, C_None, C_None)),
  375. {FABS} (Ch: (C_FPU, C_None, C_None)),
  376. {FBLD} (Ch: (C_FPU, C_None, C_None)),
  377. {FBSTP} (Ch: (C_Op1, C_None, C_None)),
  378. {FCLEX} (Ch: (C_FPU, C_None, C_None)),
  379. {FNCLEX} (Ch: (C_FPU, C_None, C_None)),
  380. {FCOS} (Ch: (C_FPU, C_None, C_None)),
  381. {FDECSTP}(Ch: (C_FPU, C_None, C_None)),
  382. {FDISI} (Ch: (C_FPU, C_None, C_None)),
  383. {FNDISI} (Ch: (C_FPU, C_None, C_None)),
  384. {FDIVR} (Ch: (C_FPU, C_None, C_None)),
  385. {FENI} (Ch: (C_FPU, C_None, C_None)),
  386. {FNENI} (Ch: (C_FPU, C_None, C_None)),
  387. {FFREE} (Ch: (C_FPU, C_None, C_None)),
  388. {FIADD} (Ch: (C_FPU, C_None, C_None)),
  389. {FICOM} (Ch: (C_FPU, C_None, C_None)),
  390. {FICOMP} (Ch: (C_FPU, C_None, C_None)),
  391. {FIDIVR} (Ch: (C_FPU, C_None, C_None)),
  392. {FIMUL} (Ch: (C_FPU, C_None, C_None)),
  393. {FINCSTP}(Ch: (C_FPU, C_None, C_None)),
  394. {FINIT} (Ch: (C_FPU, C_None, C_None)),
  395. {FNINIT} (Ch: (C_FPU, C_None, C_None)),
  396. {FIST} (Ch: (C_Op1, C_None, C_None)),
  397. {FISTP} (Ch: (C_Op1, C_None, C_None)),
  398. {FISUB} (Ch: (C_FPU, C_None, C_None)),
  399. {FSUBR} (Ch: (C_FPU, C_None, C_None)),
  400. {FLDCW} (Ch: (C_FPU, C_None, C_None)),
  401. {FLDENV} (Ch: (C_FPU, C_None, C_None)),
  402. {FLDLG2} (Ch: (C_FPU, C_None, C_None)),
  403. {FLDLN2} (Ch: (C_FPU, C_None, C_None)),
  404. {FLDL2E} (Ch: (C_FPU, C_None, C_None)),
  405. {FLDL2T} (Ch: (C_FPU, C_None, C_None)),
  406. {FLDPI} (Ch: (C_FPU, C_None, C_None)),
  407. {FLDS} (Ch: (C_FPU, C_None, C_None)),
  408. {FLDZ} (Ch: (C_FPU, C_None, C_None)),
  409. {FNOP} (Ch: (C_FPU, C_None, C_None)),
  410. {FPATAN} (Ch: (C_FPU, C_None, C_None)),
  411. {FPREM} (Ch: (C_FPU, C_None, C_None)),
  412. {FPREM1} (Ch: (C_FPU, C_None, C_None)),
  413. {FPTAN} (Ch: (C_FPU, C_None, C_None)),
  414. {FRNDINT}(Ch: (C_FPU, C_None, C_None)),
  415. {FRSTOR} (Ch: (C_FPU, C_None, C_None)),
  416. {FSAVE} (Ch: (C_Op1, C_None, C_None)),
  417. {FNSAVE} (Ch: (C_FPU, C_None, C_None)),
  418. {FSCALE} (Ch: (C_FPU, C_None, C_None)),
  419. {FSETPM} (Ch: (C_FPU, C_None, C_None)),
  420. {FSIN} (Ch: (C_FPU, C_None, C_None)),
  421. {FSINCOS}(Ch: (C_FPU, C_None, C_None)),
  422. {FSQRT} (Ch: (C_FPU, C_None, C_None)),
  423. {FST} (Ch: (C_Op1, C_None, C_None)),
  424. {FSTCW} (Ch: (C_Op1, C_None, C_None)),
  425. {FNSTCW} (Ch: (C_Op1, C_None, C_None)),
  426. {FSTENV} (Ch: (C_Op1, C_None, C_None)),
  427. {FNSTENV}(Ch: (C_Op1, C_None, C_None)),
  428. {FSTSW} (Ch: (C_Op1, C_None, C_None)),
  429. {FNSTSW} (Ch: (C_Op1, C_None, C_None)),
  430. {FTST} (Ch: (C_FPU, C_None, C_None)),
  431. {FUCOM} (Ch: (C_FPU, C_None, C_None)),
  432. {FUCOMP} (Ch: (C_FPU, C_None, C_None)),
  433. {FUCOMPP}(Ch: (C_FPU, C_None, C_None)),
  434. {FWAIT} (Ch: (C_FPU, C_None, C_None)),
  435. {FXAM} (Ch: (C_FPU, C_None, C_None)),
  436. {FXTRACT}(Ch: (C_FPU, C_None, C_None)),
  437. {FYL2X} (Ch: (C_FPU, C_None, C_None)),
  438. {FYL2XP1}(Ch: (C_FPU, C_None, C_None)),
  439. {F2XM1} (Ch: (C_FPU, C_None, C_None)),
  440. {FILDQ} (Ch: (C_FPU, C_None, C_None)),
  441. {FILDS} (Ch: (C_FPU, C_None, C_None)),
  442. {FILDL} (Ch: (C_FPU, C_None, C_None)),
  443. {FLDL} (Ch: (C_FPU, C_None, C_None)),
  444. {FLDT} (Ch: (C_FPU, C_None, C_None)),
  445. {FISTQ} (Ch: (C_Op1, C_None, C_None)),
  446. {FISTS} (Ch: (C_Op1, C_None, C_None)),
  447. {FISTL} (Ch: (C_Op1, C_None, C_None)),
  448. {FSTL} (Ch: (C_Op1, C_None, C_None)),
  449. {FSTS} (Ch: (C_Op1, C_None, C_None)),
  450. {FSTPS} (Ch: (C_Op1, C_None, C_None)),
  451. {FISTPL} (Ch: (C_Op1, C_None, C_None)),
  452. {FSTPL} (Ch: (C_Op1, C_None, C_None)),
  453. {FISTPS} (Ch: (C_Op1, C_None, C_None)),
  454. {FISTPQ} (Ch: (C_Op1, C_None, C_None)),
  455. {FSTPT} (Ch: (C_Op1, C_None, C_None)),
  456. {FCOMPS} (Ch: (C_FPU, C_None, C_None)),
  457. {FICOMPL}(Ch: (C_FPU, C_None, C_None)),
  458. {FCOMPL} (Ch: (C_FPU, C_None, C_None)),
  459. {FICOMPS}(Ch: (C_FPU, C_None, C_None)),
  460. {FCOMS} (Ch: (C_FPU, C_None, C_None)),
  461. {FICOML} (Ch: (C_FPU, C_None, C_None)),
  462. {FCOML} (Ch: (C_FPU, C_None, C_None)),
  463. {FICOMS} (Ch: (C_FPU, C_None, C_None)),
  464. {FIADDL} (Ch: (C_FPU, C_None, C_None)),
  465. {FADDL} (Ch: (C_FPU, C_None, C_None)),
  466. {FIADDS} (Ch: (C_FPU, C_None, C_None)),
  467. {FISUBL} (Ch: (C_FPU, C_None, C_None)),
  468. {FSUBL} (Ch: (C_FPU, C_None, C_None)),
  469. {FISUBS} (Ch: (C_FPU, C_None, C_None)),
  470. {FSUBS} (Ch: (C_FPU, C_None, C_None)),
  471. {FSUBR} (Ch: (C_FPU, C_None, C_None)),
  472. {FSUBRS} (Ch: (C_FPU, C_None, C_None)),
  473. {FISUBRL}(Ch: (C_FPU, C_None, C_None)),
  474. {FSUBRL} (Ch: (C_FPU, C_None, C_None)),
  475. {FISUBRS}(Ch: (C_FPU, C_None, C_None)),
  476. {FMULS} (Ch: (C_FPU, C_None, C_None)),
  477. {FIMUL} (Ch: (C_FPU, C_None, C_None)),
  478. {FMULL} (Ch: (C_FPU, C_None, C_None)),
  479. {FIMULS} (Ch: (C_FPU, C_None, C_None)),
  480. {FIDIVS} (Ch: (C_FPU, C_None, C_None)),
  481. {FIDIVL} (Ch: (C_FPU, C_None, C_None)),
  482. {FDIVL} (Ch: (C_FPU, C_None, C_None)),
  483. {FIDIVS} (Ch: (C_FPU, C_None, C_None)),
  484. {FDIVRS} (Ch: (C_FPU, C_None, C_None)),
  485. {FIDIVRL}(Ch: (C_FPU, C_None, C_None)),
  486. {FDIVRL} (Ch: (C_FPU, C_None, C_None)),
  487. {FIDIVRS}(Ch: (C_FPU, C_None, C_None)),
  488. {REPE} (Ch: (C_RWECX, C_None, C_None)),
  489. {REPNE} (Ch: (C_RWECX, C_None, C_None)),
  490. {FADDS} (Ch: (C_FPU, C_None, C_None)),
  491. {POPFD} (Ch: (C_RWESP, C_Flags, C_None)),
  492. {below are the MMX instructions}
  493. {A_EMMS} (Ch: (C_FPU, C_None, C_None)),
  494. {A_MOVD} (Ch: (C_Op2, C_None, C_None)),
  495. {A_MOVQ} (Ch: (C_Op2, C_None, C_None)),
  496. {A_PACKSSDW} (Ch: (C_All, C_None, C_None)),
  497. {A_PACKSSWB} (Ch: (C_All, C_None, C_None)),
  498. {A_PACKUSWB} (Ch: (C_All, C_None, C_None)),
  499. {A_PADDB} (Ch: (C_Op2, C_None, C_None)),
  500. {A_PADDD} (Ch: (C_Op2, C_None, C_None)),
  501. {A_PADDSB} (Ch: (C_Op2, C_None, C_None)),
  502. {A_PADDSW} (Ch: (C_Op2, C_None, C_None)),
  503. {A_PADDUSB} (Ch: (C_Op2, C_None, C_None)),
  504. {A_PADDUSW} (Ch: (C_Op2, C_None, C_None)),
  505. {A_PADDW} (Ch: (C_Op2, C_None, C_None)),
  506. {A_PAND} (Ch: (C_Op2, C_None, C_None)),
  507. {A_PANDN} (Ch: (C_Op2, C_None, C_None)),
  508. {A_PCMPEQB} (Ch: (C_All, C_None, C_None)),
  509. {A_PCMPEQD} (Ch: (C_All, C_None, C_None)),
  510. {A_PCMPEQW} (Ch: (C_All, C_None, C_None)),
  511. {A_PCMPGTB} (Ch: (C_All, C_None, C_None)),
  512. {A_PCMPGTD} (Ch: (C_All, C_None, C_None)),
  513. {A_PCMPGTW} (Ch: (C_All, C_None, C_None)),
  514. {A_PMADDWD} (Ch: (C_Op2, C_None, C_None)),
  515. {A_PMULHW} (Ch: (C_All, C_None, C_None)),
  516. {A_PMULLW} (Ch: (C_All, C_None, C_None)),
  517. {A_POR} (Ch: (C_Op2, C_None, C_None)),
  518. {A_PSLLD} (Ch: (C_Op2, C_None, C_None)),
  519. {A_PSLLQ} (Ch: (C_Op2, C_None, C_None)),
  520. {A_PSLLW} (Ch: (C_Op2, C_None, C_None)),
  521. {A_PSRAD} (Ch: (C_Op2, C_None, C_None)),
  522. {A_PSRAW} (Ch: (C_Op2, C_None, C_None)),
  523. {A_PSRLD} (Ch: (C_Op2, C_None, C_None)),
  524. {A_PSRLQ} (Ch: (C_Op2, C_None, C_None)),
  525. {A_PSRLW} (Ch: (C_Op2, C_None, C_None)),
  526. {A_PSUBB} (Ch: (C_Op2, C_None, C_None)),
  527. {A_PSUBD} (Ch: (C_Op2, C_None, C_None)),
  528. {A_PSUBSB} (Ch: (C_Op2, C_None, C_None)),
  529. {A_PSUBSW} (Ch: (C_Op2, C_None, C_None)),
  530. {A_PSUBUSB} (Ch: (C_Op2, C_None, C_None)),
  531. {A_PSUBUSW} (Ch: (C_Op2, C_None, C_None)),
  532. {A_PSUBW} (Ch: (C_Op2, C_None, C_None)),
  533. {A_PUNPCKHBW} (Ch: (C_All, C_None, C_None)),
  534. {A_PUNPCKHDQ} (Ch: (C_All, C_None, C_None)),
  535. {A_PUNPCKHWD} (Ch: (C_All, C_None, C_None)),
  536. {A_PUNPCKLBW} (Ch: (C_All, C_None, C_None)),
  537. {A_PUNPCKLDQ} (Ch: (C_All, C_None, C_None)),
  538. {A_PUNPCKLWD} (Ch: (C_All, C_None, C_None)),
  539. {A_PXOR} (Ch: (C_Op2, C_None, C_None)));
  540. Var
  541. {How many instructions are between the current instruction and the last one
  542. that modified the register}
  543. NrOfInstrSinceLastMod: TInstrSinceLastMod;
  544. {************************ Create the Label table ************************}
  545. Procedure FindLoHiLabels(AsmL: PAasmOutput; Var LowLabel, HighLabel, LabelDif: Longint);
  546. {Walks through the paasmlist to find the lowest and highest label number;
  547. Since 0.9.3: also removes unused labels}
  548. Var LabelFound: Boolean;
  549. P, hp1: Pai;
  550. Begin
  551. LabelFound := False;
  552. LowLabel := MaxLongint;
  553. HighLabel := 0;
  554. P := Pai(AsmL^.first);
  555. While Assigned(p) Do
  556. Begin
  557. If (Pai(p)^.typ = ait_label) Then
  558. If (Pai_Label(p)^.l^.is_used)
  559. Then
  560. Begin
  561. LabelFound := True;
  562. If (Pai_Label(p)^.l^.nb < LowLabel) Then
  563. LowLabel := Pai_Label(p)^.l^.nb;
  564. If (Pai_Label(p)^.l^.nb > HighLabel) Then
  565. HighLabel := Pai_Label(p)^.l^.nb;
  566. End
  567. { Else
  568. Begin
  569. hp1 := pai(p^.next);
  570. AsmL^.Remove(p);
  571. Dispose(p, Done);
  572. p := hp1;
  573. continue;
  574. End};
  575. GetNextInstruction(p, p);
  576. End;
  577. If LabelFound
  578. Then LabelDif := HighLabel+1-LowLabel
  579. Else LabelDif := 0;
  580. End;
  581. Procedure BuildLabelTable(AsmL: PAasmOutput; Var LabelTable: PLabelTable; LowLabel: Longint; Var LabelDif: Longint);
  582. {Builds a table with the locations of the labels in the paasmoutput}
  583. Var p: Pai;
  584. Begin
  585. If (LabelDif <> 0) Then
  586. Begin
  587. {$IfDef TP}
  588. If (MaxAvail >= LabelDif*SizeOf(Pai))
  589. Then
  590. Begin
  591. {$EndIf TP}
  592. GetMem(LabelTable, LabelDif*SizeOf(TLabelTableItem));
  593. FillChar(LabelTable^, LabelDif*SizeOf(TLabelTableItem), 0);
  594. p := pai(AsmL^.first);
  595. While Assigned(p) Do
  596. Begin
  597. If (Pai(p)^.typ = ait_label) And
  598. (Pai_Label(p)^.l^.is_used) Then
  599. LabelTable^[Pai_Label(p)^.l^.nb-LowLabel].PaiObj := p;
  600. GetNextInstruction(p, p);
  601. End;
  602. {$IfDef TP}
  603. End
  604. Else LabelDif := 0;
  605. {$EndIf TP}
  606. End;
  607. End;
  608. {************************ Search the Label table ************************}
  609. Function FindLabel(L: PLabel; Var hp: Pai): Boolean;
  610. {searches for the specified label starting from hp as long as the
  611. encountered instructions are labels, to be able to optimize constructs like
  612. jne l2 jmp l2
  613. jmp l3 and l1:
  614. l1: l2:
  615. l2:}
  616. Var TempP: Pai;
  617. Begin
  618. TempP := hp;
  619. While Assigned(TempP) and
  620. (TempP^.typ In SkipInstr + [ait_label]) Do
  621. If (TempP^.typ <> ait_Label) Or
  622. (pai_label(TempP)^.l <> L)
  623. Then GetNextInstruction(TempP, TempP)
  624. Else
  625. Begin
  626. hp := TempP;
  627. FindLabel := True;
  628. exit
  629. End;
  630. FindLabel := False;
  631. End;
  632. {************************ Some general functions ************************}
  633. Function Reg32(Reg: TRegister): TRegister;
  634. {Returns the 32 bit component of Reg if it exists, otherwise Reg is returned}
  635. Begin
  636. Reg32 := Reg;
  637. If (Reg >= R_AX)
  638. Then
  639. If (Reg <= R_DI)
  640. Then Reg32 := Reg16ToReg32(Reg)
  641. Else
  642. If (Reg <= R_BL)
  643. Then Reg32 := Reg8toReg32(Reg);
  644. End;
  645. { inserts new_one between prev and foll }
  646. Procedure InsertLLItem(AsmL: PAasmOutput; prev, foll, new_one: PLinkedList_Item);
  647. Begin
  648. If Assigned(prev) Then
  649. If Assigned(foll) Then
  650. Begin
  651. If Assigned(new_one) Then
  652. Begin
  653. new_one^.previous := prev;
  654. new_one^.next := foll;
  655. prev^.next := new_one;
  656. foll^.previous := new_one;
  657. End;
  658. End
  659. Else AsmL^.Concat(new_one)
  660. Else If Assigned(Foll) Then AsmL^.Insert(new_one)
  661. End;
  662. {********************* Compare parts of Pai objects *********************}
  663. Function RegsEquivalent(Reg1, Reg2: TRegister; Var RegInfo: TRegInfo): Boolean;
  664. Begin
  665. With RegInfo Do
  666. If Not(Reg1 in RegsEncountered) Then
  667. Begin
  668. RegsEncountered := RegsEncountered + [Reg1];
  669. SubstRegs[Reg1] := Reg2;
  670. RegsEquivalent := True
  671. End
  672. Else RegsEquivalent := Reg1 = SubstRegs[Reg2];
  673. End;
  674. Function RefsEquivalent(Const R1, R2: TReference; var RegInfo: TRegInfo): Boolean;
  675. Begin
  676. If R1.IsIntValue
  677. Then RefsEquivalent := R2.IsIntValue and (R1.Offset = R2.Offset)
  678. Else If (R1.Offset = R2.Offset) And
  679. RegsEquivalent(R1.Base, R2.Base, RegInfo) And
  680. RegsEquivalent(R1.Index, R2.Index, RegInfo) And
  681. (R1.Segment = R2.Segment) And (R1.ScaleFactor = R2.ScaleFactor)
  682. Then
  683. Begin
  684. If Assigned(R1.Symbol)
  685. Then RefsEquivalent := Assigned(R2.Symbol) And (R1.Symbol^=R2.Symbol^)
  686. Else RefsEquivalent := Not(Assigned(R2.Symbol));
  687. End
  688. Else RefsEquivalent := False;
  689. End;
  690. Function RefsEqual(Const R1, R2: TReference): Boolean;
  691. Begin
  692. If R1.IsIntValue
  693. Then RefsEqual := R2.IsIntValue and (R1.Offset = R2.Offset)
  694. Else If (R1.Offset = R2.Offset) And (R1.Base = R2.Base) And
  695. (R1.Index = R2.Index) And (R1.Segment = R2.Segment) And
  696. (R1.ScaleFactor = R2.ScaleFactor)
  697. Then
  698. Begin
  699. If Assigned(R1.Symbol)
  700. Then RefsEqual := Assigned(R2.Symbol) And (R1.Symbol^=R2.Symbol^)
  701. Else RefsEqual := Not(Assigned(R2.Symbol));
  702. End
  703. Else RefsEqual := False;
  704. End;
  705. Function IsGP32Reg(Reg: TRegister): Boolean;
  706. {Checks if the register is a 32 bit general purpose register}
  707. Begin
  708. If (Reg >= R_EAX) and (Reg <= R_EBX)
  709. Then IsGP32Reg := True
  710. Else IsGP32reg := False
  711. End;
  712. Function SubstRegInRef(Reg: TRegister; Const Ref: TReference; var RegInfo: TRegInfo): Boolean;
  713. Begin
  714. Reg := Reg32(Reg);
  715. With RegInfo Do
  716. SubstRegInRef := RegsEquivalent(Reg, SubstRegs[Ref.Base], RegInfo) And
  717. RegsEquivalent(Reg, SubstRegs[Ref.Base], RegInfo);
  718. End;
  719. Function RegInRef(Reg: TRegister; Const Ref: TReference): Boolean;
  720. Begin {checks whether Ref contains a reference to Reg}
  721. Reg := Reg32(Reg);
  722. RegInRef := (Ref.Base = Reg) Or (Ref.Index = Reg)
  723. End;
  724. Function RegInInstruction(Reg: TRegister; p1: Pai): Boolean;
  725. {checks if Reg is used by the instruction p1}
  726. Var TmpResult: Boolean;
  727. Begin
  728. TmpResult := False;
  729. If (Pai(p1)^.typ = ait_instruction) Then
  730. Begin
  731. Case Pai386(p1)^.op1t Of
  732. Top_Reg: TmpResult := Reg = TRegister(Pai386(p1)^.op1);
  733. Top_Ref: TmpResult := RegInRef(Reg, TReference(Pai386(p1)^.op1^))
  734. End;
  735. If Not(TmpResult) Then
  736. Case Pai386(p1)^.op2t Of
  737. Top_Reg:
  738. if Pai386(p1)^.op3t<>Top_reg
  739. then TmpResult := Reg = TRegister(Pai386(p1)^.op2)
  740. else TmpResult := longint(Reg) = twowords(Pai386(p1)^.op2).word1;
  741. Top_Ref: TmpResult := RegInRef(Reg, TReference(Pai386(p1)^.op2^))
  742. End;
  743. If Not(TmpResult) Then
  744. Case Pai386(p1)^.op3t Of
  745. Top_Reg: TmpResult := longint(Reg) =twowords(Pai386(p1)^.op2).word2;
  746. Top_none:;
  747. else
  748. internalerror($Da);
  749. End
  750. End;
  751. RegInInstruction := TmpResult
  752. End;
  753. {********************* GetNext and GetLastInstruction *********************}
  754. Function GetNextInstruction(Current: Pai; Var Next: Pai): Boolean;
  755. {skips ait_regalloc, ait_regdealloc and ait_stab* objects and puts the
  756. next pai object in Next. Returns false if there isn't any}
  757. Begin
  758. Current := Pai(Current^.Next);
  759. While Assigned(Current) And
  760. ((Current^.typ In SkipInstr) or
  761. ((Current^.typ = ait_label) And
  762. Not(Pai_Label(Current)^.l^.is_used))) Do
  763. Current := Pai(Current^.Next);
  764. Next := Current;
  765. If Assigned(Current) And
  766. Not((Current^.typ In SkipInstr) or
  767. ((Current^.typ = ait_label) And
  768. Not(Pai_Label(Current)^.l^.is_used)))
  769. Then GetNextInstruction := True
  770. Else
  771. Begin
  772. Next := Nil;
  773. GetNextInstruction := False;
  774. End;
  775. End;
  776. Function GetLastInstruction(Current: Pai; Var Last: Pai): Boolean;
  777. {skips the ait-types in SkipInstr puts the previous pai object in
  778. Last. Returns false if there isn't any}
  779. Begin
  780. Current := Pai(Current^.previous);
  781. While Assigned(Current) And
  782. ((Pai(Current)^.typ In SkipInstr) or
  783. ((Pai(Current)^.typ = ait_label) And
  784. Not(Pai_Label(Current)^.l^.is_used))) Do
  785. Current := Pai(Current^.previous);
  786. Last := Current;
  787. If Assigned(Current) And
  788. Not((Current^.typ In SkipInstr) or
  789. ((Current^.typ = ait_label) And
  790. Not(Pai_Label(Current)^.l^.is_used)))
  791. Then GetLastInstruction := True
  792. Else
  793. Begin
  794. Last := Nil;
  795. GetLastInstruction := False
  796. End;
  797. End;
  798. {******************* The Data Flow Analyzer functions ********************}
  799. (*Function FindZeroreg(p: Pai; Var Result: TRegister): Boolean;
  800. {Finds a register which contains the constant zero}
  801. Var Counter: TRegister;
  802. Begin
  803. Counter := R_EAX;
  804. FindZeroReg := True;
  805. While (Counter <= R_EDI) And
  806. ((PPaiProp(p^.fileinfo.line)^.Regs[Counter].Typ <> Con_Const) or
  807. (PPaiProp(p^.fileinfo.line)^.Regs[Counter].StartMod <> Pointer(0))) Do
  808. Inc(Byte(Counter));
  809. If (PPaiProp(p^.fileinfo.line)^.Regs[Counter].Typ = Con_Const) And
  810. (PPaiProp(p^.fileinfo.line)^.Regs[Counter].StartMod = Pointer(0))
  811. Then Result := Counter
  812. Else FindZeroReg := False;
  813. End;*)
  814. Function TCh2Reg(Ch: TChange): TRegister;
  815. {converts a TChange variable to a TRegister}
  816. Begin
  817. If (Ch <= C_REDI) Then
  818. TCh2Reg := TRegister(Byte(Ch))
  819. Else
  820. If (Ch <= C_WEDI) Then
  821. TCh2Reg := TRegister(Byte(Ch) - Byte(C_REDI))
  822. Else
  823. If (Ch <= C_RWEDI) Then
  824. TCh2Reg := TRegister(Byte(Ch) - Byte(C_WEDI))
  825. Else InternalError($db)
  826. End;
  827. Procedure IncState(Var S: Word);
  828. {Increases the state by 1, wraps around at $ffff to 0 (so we won't get
  829. overflow errors}
  830. Begin
  831. If (s <> $ffff)
  832. Then Inc(s)
  833. Else s := 0
  834. End;
  835. Function RegInSequence(Reg: TRegister; Const Content: TContent): Boolean;
  836. {checks the whole sequence of Content (so StartMod and and the next NrOfMods
  837. Pai objects) to see whether Reg is used somewhere, without it being loaded
  838. with something else first}
  839. Var p: Pai;
  840. Counter: Byte;
  841. TmpResult: Boolean;
  842. RegsChecked: TRegSet;
  843. Begin
  844. RegsChecked := [];
  845. p := Content.StartMod;
  846. TmpResult := False;
  847. Counter := 1;
  848. While Not(TmpResult) And
  849. (Counter <= Content.NrOfMods) Do
  850. Begin
  851. If (p^.typ = ait_instruction) and
  852. (Pai386(p)^._operator in [A_MOV, A_MOVZX, A_MOVSX])
  853. Then
  854. If (Pai386(p)^.op1t = top_ref)
  855. Then
  856. With TReference(Pai386(p)^.op1^) Do
  857. If (Base = ProcInfo.FramePointer) And
  858. (Index = R_NO)
  859. Then RegsChecked := RegsChecked + [Reg32(TRegister(Pai386(p)^.op2))]
  860. Else
  861. Begin
  862. If (Base = Reg) And
  863. Not(Base In RegsChecked)
  864. Then TmpResult := True;
  865. If Not(TmpResult) And
  866. (Index = Reg) And
  867. Not(Index In RegsChecked)
  868. Then TmpResult := True;
  869. End;
  870. Inc(Counter);
  871. GetNextInstruction(p,p)
  872. End;
  873. RegInSequence := TmpResult
  874. End;
  875. Procedure DestroyReg(p1: PPaiProp; Reg: TRegister);
  876. {Destroys the contents of the register Reg in the PPaiProp p1, as well as the
  877. contents of registers are loaded with a memory location based on Reg}
  878. Var TmpState: Longint;
  879. Counter: TRegister;
  880. Begin
  881. Reg := Reg32(Reg);
  882. NrOfInstrSinceLastMod[Reg] := 0;
  883. If (Reg >= R_EAX) And (Reg <= R_EDI)
  884. Then
  885. Begin
  886. With p1^.Regs[Reg] Do
  887. Begin
  888. IncState(State);
  889. TmpState := State;
  890. FillChar(p1^.Regs[Reg], SizeOf(TContent), 0);
  891. State := TmpState;
  892. End;
  893. For Counter := R_EAX to R_EDI Do
  894. With p1^.Regs[Counter] Do
  895. If (Typ = Con_Ref) And
  896. RegInSequence(Reg, p1^.Regs[Counter])
  897. Then
  898. Begin
  899. IncState(State);
  900. TmpState := State;
  901. FillChar(p1^.Regs[Counter], SizeOf(TContent), 0);
  902. State := TmpState;
  903. End;
  904. End;
  905. End;
  906. Function OpsEqual(typ: Longint; op1, op2: Pointer): Boolean;
  907. Begin {checks whether the two ops are equal}
  908. Case typ Of
  909. Top_Reg, Top_Const: OpsEqual := op1 = op2;
  910. Top_Ref: OpsEqual := RefsEqual(TReference(op1^), TReference(op2^));
  911. Top_None: OpsEqual := True
  912. Else OpsEqual := False
  913. End;
  914. End;
  915. Function RegsSameContent(p1, p2: Pai; Reg: TRegister): Boolean;
  916. {checks whether Reg has the same content in the PPaiProp of p1 and p2}
  917. Begin
  918. Reg := Reg32(Reg);
  919. RegsSameContent :=
  920. PPaiProp(p1^.fileinfo.line)^.Regs[Reg].State =
  921. PPaiProp(p2^.fileinfo.line)^.Regs[Reg].State;
  922. End;
  923. Function InstructionsEqual(p1, p2: Pai): Boolean;
  924. Begin {checks whether two Pai386 instructions are equal}
  925. InstructionsEqual :=
  926. Assigned(p1) And Assigned(p2) And
  927. ((Pai(p1)^.typ = ait_instruction) And
  928. (Pai(p1)^.typ = ait_instruction) And
  929. (Pai386(p1)^._operator = Pai386(p2)^._operator) And
  930. (Pai386(p1)^.op1t = Pai386(p2)^.op1t) And
  931. (Pai386(p1)^.op2t = Pai386(p2)^.op2t) And
  932. OpsEqual(Pai386(p1)^.op1t, Pai386(p1)^.op1, Pai386(p2)^.op1) And
  933. OpsEqual(Pai386(p1)^.op2t, Pai386(p1)^.op2, Pai386(p2)^.op2))
  934. End;
  935. Function RefInInstruction(Const Ref: TReference; p: Pai): Boolean;
  936. {checks whehter Ref is used in P}
  937. Var TmpResult: Boolean;
  938. Begin
  939. TmpResult := False;
  940. If (p^.typ = ait_instruction) Then
  941. Begin
  942. If (Pai386(p)^.op1t = Top_Ref)
  943. Then TmpResult := RefsEqual(Ref, TReference(Pai386(p)^.op1^));
  944. If Not(TmpResult) And
  945. (Pai386(p)^.op2t = Top_Ref)
  946. Then TmpResult := RefsEqual(Ref, TReference(Pai386(p)^.op2^));
  947. End;
  948. RefInInstruction := TmpResult;
  949. End;
  950. Function RefInSequence(Const Ref: TReference; Content: TContent): Boolean;
  951. {checks the whole sequence of Content (so StartMod and and the next NrOfMods
  952. Pai objects) to see whether Ref is used somewhere}
  953. Var p: Pai;
  954. Counter: Byte;
  955. TmpResult: Boolean;
  956. Begin
  957. p := Content.StartMod;
  958. TmpResult := False;
  959. Counter := 1;
  960. While Not(TmpResult) And
  961. (Counter <= Content.NrOfMods) Do
  962. Begin
  963. If (p^.typ = ait_instruction) And
  964. RefInInstruction(Ref, p)
  965. Then TmpResult := True;
  966. Inc(Counter);
  967. GetNextInstruction(p,p)
  968. End;
  969. RefInSequence := TmpResult
  970. End;
  971. Procedure DestroyRefs(p: pai; Const Ref: TReference; WhichReg: TRegister);
  972. {destroys all registers which possibly contain a reference to Ref, WhichReg
  973. is the register whose contents are being written to memory (if this proc
  974. is called because of a "mov?? %reg, (mem)" instruction)}
  975. Var Counter: TRegister;
  976. Begin
  977. WhichReg := Reg32(WhichReg);
  978. If ((Ref.base = ProcInfo.FramePointer) And
  979. (Ref.Index = R_NO)) Or
  980. Assigned(Ref.Symbol)
  981. Then
  982. {write something to a parameter, a local or global variable, so
  983. * with uncertzain optimizations on:
  984. - destroy the contents of registers whose contents have somewhere a
  985. "mov?? (Ref), %reg". WhichReg (this is the register whose contents
  986. are being written to memory) is not destroyed if it's StartMod is
  987. of that form and NrOfMods = 1 (so if it holds ref, but is not a
  988. pointer based on Ref)
  989. * with uncertain optimizations off:
  990. - also destroy registers that contain any pointer}
  991. For Counter := R_EAX to R_EDI Do
  992. With PPaiProp(p^.fileinfo.line)^.Regs[Counter] Do
  993. Begin
  994. If (typ = Con_Ref) And
  995. (Not(cs_UncertainOpts in aktglobalswitches) And
  996. (NrOfMods <> 1)
  997. ) Or
  998. (RefInSequence(Ref,PPaiProp(p^.fileinfo.line)^.Regs[Counter]) And
  999. ((Counter <> WhichReg) Or
  1000. ((NrOfMods = 1) And
  1001. {StarMod is always of the type ait_instruction}
  1002. (Pai386(StartMod)^.op1t = top_ref) And
  1003. RefsEqual(TReference(Pai386(StartMod)^.op1^), Ref)
  1004. )
  1005. )
  1006. )
  1007. Then DestroyReg(PPaiProp(p^.fileinfo.line), Counter)
  1008. End
  1009. Else
  1010. {write something to a pointer location, so
  1011. * with uncertain optimzations on:
  1012. - do not destroy registers which contain a local/global variable or a
  1013. parameter, except if DestroyRefs is called because of a "movsl"
  1014. * with uncertain optimzations off:
  1015. - destroy every register which contains a memory location
  1016. }
  1017. For Counter := R_EAX to R_EDI Do
  1018. With PPaiProp(p^.fileinfo.line)^.Regs[Counter] Do
  1019. If (typ = Con_Ref) And
  1020. (Not(cs_UncertainOpts in aktglobalswitches) Or
  1021. {for movsl}
  1022. (Ref.Base = R_EDI) Or
  1023. {don't destroy if reg contains a parameter, local or global variable}
  1024. Not((NrOfMods = 1) And
  1025. (Pai386(StartMod)^.op1t = top_ref) And
  1026. ((PReference(Pai386(StartMod)^.op1)^.base = ProcInfo.FramePointer) Or
  1027. Assigned(PReference(Pai386(StartMod)^.op1)^.Symbol)
  1028. )
  1029. )
  1030. )
  1031. Then DestroyReg(PPaiProp(p^.FileInfo.Line), Counter)
  1032. End;
  1033. Procedure DestroyAllRegs(p: PPaiProp);
  1034. Var Counter: TRegister;
  1035. Begin {initializes/desrtoys all registers}
  1036. For Counter := R_EAX To R_EDI Do
  1037. DestroyReg(p, Counter);
  1038. p^.DirFlag := F_Unknown;
  1039. End;
  1040. Procedure Destroy(PaiObj: Pai; opt: Longint; Op: Pointer);
  1041. Begin
  1042. Case opt Of
  1043. top_reg: DestroyReg(PPaiProp(PaiObj^.fileinfo.line), TRegister(Op));
  1044. top_ref: DestroyRefs(PaiObj, TReference(Op^), R_NO);
  1045. top_symbol:;
  1046. End;
  1047. End;
  1048. Procedure DFAPass1(AsmL: PAasmOutput);
  1049. {gathers the RegAlloc data... still need to think about where to store it}
  1050. Begin
  1051. FindLoHiLabels(AsmL, LoLab, HiLab, LabDif);
  1052. BuildLabelTable(AsmL, LTable, LoLab, LabDif);
  1053. End;
  1054. Function DoDFAPass2(
  1055. {$Ifdef StateDebug}
  1056. AsmL: PAasmOutput;
  1057. {$endif statedebug}
  1058. First: Pai): Pai;
  1059. {Analyzes the Data Flow of an assembler list. Starts creating the reg
  1060. contents for the instructions starting with p. Returns the last pai which has
  1061. been processed}
  1062. Var
  1063. CurProp: PPaiProp;
  1064. {$ifdef AnalyzeLoops}
  1065. TmpState,
  1066. {$endif AnalyzeLoops}
  1067. Cnt, InstrCnt : Longint;
  1068. InstrProp: TAsmInstrucProp;
  1069. p, hp : Pai;
  1070. TmpRef: TReference;
  1071. TmpReg: TRegister;
  1072. Begin
  1073. p := First;
  1074. If (First^.typ in SkipInstr) Then
  1075. GetNextInstruction(First, p);
  1076. First := p;
  1077. InstrCnt := 1;
  1078. FillChar(NrOfInstrSinceLastMod, SizeOf(NrOfInstrSinceLastMod), 0);
  1079. While Assigned(p) Do
  1080. Begin
  1081. DoDFAPass2 := p;
  1082. {$IfDef TP}
  1083. New(CurProp);
  1084. {$Else TP}
  1085. CurProp := @PaiPropBlock^[InstrCnt];
  1086. {$EndIf TP}
  1087. If (p <> First)
  1088. Then
  1089. {$ifdef JumpAnal}
  1090. Begin
  1091. If (p^.Typ <> ait_label) Then
  1092. {$endif JumpAnal}
  1093. Begin
  1094. GetLastInstruction(p, hp);
  1095. CurProp^.Regs := PPaiProp(hp^.fileinfo.line)^.Regs;
  1096. CurProp^.DirFlag := PPaiProp(hp^.fileinfo.line)^.DirFlag;
  1097. End
  1098. {$ifdef JumpAnal}
  1099. End
  1100. {$endif JumpAnal}
  1101. Else
  1102. Begin
  1103. FillChar(CurProp^, SizeOf(CurProp^), 0);
  1104. { For TmpReg := R_EAX to R_EDI Do
  1105. CurProp^.Regs[TmpReg].State := 1;}
  1106. End;
  1107. CurProp^.CanBeRemoved := False;
  1108. {$ifdef TP}
  1109. CurProp^.linesave := p^.fileinfo.line;
  1110. PPaiProp(p^.fileinfo.line) := CurProp;
  1111. {$Endif TP}
  1112. { If Not(p^.typ in SkipInstr) Then}
  1113. For TmpReg := R_EAX To R_EDI Do
  1114. Inc(NrOfInstrSinceLastMod[TmpReg]);
  1115. Case p^.typ Of
  1116. ait_label:
  1117. {$Ifndef JumpAnal}
  1118. If (Pai_label(p)^.l^.is_used) Then
  1119. DestroyAllRegs(CurProp);
  1120. {$Else JumpAnal}
  1121. Begin
  1122. If (Pai_Label(p)^.is_used) Then
  1123. With LTable^[Pai_Label(p)^.l^.nb-LoLab] Do
  1124. {$IfDef AnalyzeLoops}
  1125. If (RefsFound = Pai_Label(p)^.l^.RefCount)
  1126. {$Else AnalyzeLoops}
  1127. If (JmpsProcessed = Pai_Label(p)^.l^.RefCount)
  1128. {$EndIf AnalyzeLoops}
  1129. Then
  1130. {all jumps to this label have been found}
  1131. {$IfDef AnalyzeLoops}
  1132. If (JmpsProcessed > 0)
  1133. Then
  1134. {$EndIf AnalyzeLoops}
  1135. {we've processed at least one jump to this label}
  1136. Begin
  1137. If (GetLastInstruction(p, hp) And
  1138. Not((hp^.typ = ait_labeled_instruction) And
  1139. (Pai_Labeled(hp)^._operator = A_JMP))
  1140. Then
  1141. {previous instruction not a JMP -> the contents of the registers after the
  1142. previous intruction has been executed have to be taken into account as well}
  1143. For TmpReg := R_EAX to R_EDI Do
  1144. Begin
  1145. If (CurProp^.Regs[TmpReg].State <>
  1146. PPaiProp(hp^.FileInfo.Line)^.Regs[TmpReg].State)
  1147. Then DestroyReg(CurProp, TmpReg)
  1148. End
  1149. End
  1150. {$IfDef AnalyzeLoops}
  1151. Else
  1152. {a label from a backward jump (e.g. a loop), no jump to this label has
  1153. already been processed}
  1154. If GetLastInstruction(p, hp) And
  1155. Not(hp^.typ = ait_labeled_instruction) And
  1156. (Pai_Labeled(hp)^._operator = A_JMP))
  1157. Then
  1158. {previous instruction not a jmp, so keep all the registers' contents from the
  1159. previous instruction}
  1160. Begin
  1161. CurProp^.Regs := PPaiProp(hp^.FileInfo.Line)^.Regs;
  1162. CurProp^.DirFlag := PPaiProp(hp^.FileInfo.Line)^.DirFlag;
  1163. End
  1164. Else
  1165. {previous instruction a jmp and no jump to this label processed yet}
  1166. Begin
  1167. hp := p;
  1168. Cnt := InstrCnt;
  1169. {continue until we find a jump to the label or a label which has already
  1170. been processed}
  1171. While GetNextInstruction(hp, hp) And
  1172. Not((hp^.typ = ait_labeled_instruction) And
  1173. (Pai_Labeled(hp)^.lab^.nb = Pai_Label(p)^.l^.nb)) And
  1174. Not((hp^.typ = ait_label) And
  1175. (LTable^[Pai_Label(hp)^.l^.nb-LoLab].RefsFound
  1176. = Pai_Label(hp)^.l^.RefCount) And
  1177. (LTable^[Pai_Label(hp)^.l^.nb-LoLab].JmpsProcessed > 0)) Do
  1178. Inc(Cnt);
  1179. If (hp^.typ = ait_label)
  1180. Then
  1181. {there's a processed label after the current one}
  1182. Begin
  1183. CurProp^.Regs := PaiPropBlock^[Cnt].Regs;
  1184. CurProp^.DirFlag := PaiPropBlock^[Cnt].DirFlag;
  1185. End
  1186. Else
  1187. {there's no label anymore after the current one, or they haven't been
  1188. processed yet}
  1189. Begin
  1190. GetLastInstruction(p, hp);
  1191. CurProp^.Regs := PPaiProp(hp^.FileInfo.Line)^.Regs;
  1192. CurProp^.DirFlag := PPaiProp(hp^.FileInfo.Line)^.DirFlag;
  1193. DestroyAllRegs(PPaiProp(hp^.FileInfo.Line))
  1194. End
  1195. End
  1196. {$EndIf AnalyzeLoops}
  1197. Else
  1198. {not all references to this label have been found, so destroy all registers}
  1199. Begin
  1200. GetLastInstruction(p, hp);
  1201. CurProp^.Regs := PPaiProp(hp^.FileInfo.Line)^.Regs;
  1202. CurProp^.DirFlag := PPaiProp(hp^.FileInfo.Line)^.DirFlag;
  1203. DestroyAllRegs(CurProp)
  1204. End;
  1205. End;
  1206. {$EndIf JumpAnal}
  1207. ait_labeled_instruction:
  1208. {$IfNDef JumpAnal}
  1209. ;
  1210. {$Else JumpAnal}
  1211. With LTable^[Pai_Labeled(p)^.lab^.nb-LoLab] Do
  1212. If (RefsFound = Pai_Labeled(p)^.lab^.RefCount) Then
  1213. Begin
  1214. If (InstrCnt < InstrNr)
  1215. Then
  1216. {forward jump}
  1217. If (JmpsProcessed = 0) Then
  1218. {no jump to this label has been processed yet}
  1219. Begin
  1220. PaiPropBlock^[InstrNr].Regs := CurProp^.Regs;
  1221. PaiPropBlock^[InstrNr].DirFlag := CurProp^.DirFlag;
  1222. Inc(JmpsProcessed);
  1223. End
  1224. Else
  1225. Begin
  1226. For TmpReg := R_EAX to R_EDI Do
  1227. If (PaiPropBlock^[InstrNr].Regs[TmpReg].State <>
  1228. CurProp^.Regs[TmpReg].State) Then
  1229. DestroyReg(@PaiPropBlock^[InstrNr], TmpReg);
  1230. Inc(JmpsProcessed);
  1231. End
  1232. {$ifdef AnalyzeLoops}
  1233. Else
  1234. { backward jump, a loop for example}
  1235. { If (JmpsProcessed > 0) Or
  1236. Not(GetLastInstruction(PaiObj, hp) And
  1237. (hp^.typ = ait_labeled_instruction) And
  1238. (Pai_Labeled(hp)^._operator = A_JMP))
  1239. Then}
  1240. {instruction prior to label is not a jmp, or at least one jump to the label
  1241. has yet been processed}
  1242. Begin
  1243. Inc(JmpsProcessed);
  1244. For TmpReg := R_EAX to R_EDI Do
  1245. If (PaiPropBlock^[InstrNr].Regs[TmpReg].State <>
  1246. CurProp^.Regs[TmpReg].State)
  1247. Then
  1248. Begin
  1249. TmpState := PaiPropBlock^[InstrNr].Regs[TmpReg].State;
  1250. Cnt := InstrNr;
  1251. While (TmpState = PaiPropBlock^[Cnt].Regs[TmpReg].State) Do
  1252. Begin
  1253. DestroyReg(@PaiPropBlock^[Cnt], TmpReg);
  1254. Inc(Cnt);
  1255. End;
  1256. While (Cnt <= InstrCnt) Do
  1257. Begin
  1258. Inc(PaiPropBlock^[Cnt].Regs[TmpReg].State);
  1259. Inc(Cnt)
  1260. End
  1261. End;
  1262. End
  1263. { Else }
  1264. {instruction prior to label is a jmp and no jumps to the label have yet been
  1265. processed}
  1266. { Begin
  1267. Inc(JmpsProcessed);
  1268. For TmpReg := R_EAX to R_EDI Do
  1269. Begin
  1270. TmpState := PaiPropBlock^[InstrNr].Regs[TmpReg].State;
  1271. Cnt := InstrNr;
  1272. While (TmpState = PaiPropBlock^[Cnt].Regs[TmpReg].State) Do
  1273. Begin
  1274. PaiPropBlock^[Cnt].Regs[TmpReg] := CurProp^.Regs[TmpReg];
  1275. Inc(Cnt);
  1276. End;
  1277. TmpState := PaiPropBlock^[InstrNr].Regs[TmpReg].State;
  1278. While (TmpState = PaiPropBlock^[Cnt].Regs[TmpReg].State) Do
  1279. Begin
  1280. DestroyReg(@PaiPropBlock^[Cnt], TmpReg);
  1281. Inc(Cnt);
  1282. End;
  1283. While (Cnt <= InstrCnt) Do
  1284. Begin
  1285. Inc(PaiPropBlock^[Cnt].Regs[TmpReg].State);
  1286. Inc(Cnt)
  1287. End
  1288. End
  1289. End}
  1290. {$endif AnalyzeLoops}
  1291. End;
  1292. {$EndIf JumpAnal}
  1293. {$ifdef GDB}
  1294. ait_stabs, ait_stabn, ait_stab_function_name:;
  1295. {$endif GDB}
  1296. ait_regalloc, ait_regdealloc:;
  1297. ait_instruction:
  1298. Begin
  1299. InstrProp := AsmInstr[Pai386(p)^._operator];
  1300. Case Pai386(p)^._operator Of
  1301. A_MOV, A_MOVZX, A_MOVSX:
  1302. Begin
  1303. Case Pai386(p)^.op1t Of
  1304. Top_Reg:
  1305. Case Pai386(p)^.op2t Of
  1306. Top_Reg:
  1307. Begin
  1308. DestroyReg(CurProp, TRegister(Pai386(p)^.op2));
  1309. { CurProp^.Regs[TRegister(Pai386(p)^.op2)] :=
  1310. CurProp^.Regs[TRegister(Pai386(p)^.op1)];
  1311. If (CurProp^.Regs[TRegister(Pai386(p)^.op2)].ModReg = R_NO) Then
  1312. CurProp^.Regs[TRegister(Pai386(p)^.op2)].ModReg :=
  1313. Tregister(Pai386(p)^.op1);}
  1314. End;
  1315. Top_Ref: DestroyRefs(p, TReference(Pai386(p)^.op2^), TRegister(Pai386(p)^.op1));
  1316. End;
  1317. Top_Ref:
  1318. Begin {destination is always a register in this case}
  1319. TmpReg := Reg32(TRegister(Pai386(p)^.op2));
  1320. {$ifdef StateDebug}
  1321. hp := new(pai_asm_comment,init(strpnew(att_reg2str[TmpReg]+': '+tostr(CurProp^.Regs[TmpReg].State))));
  1322. InsertLLItem(AsmL, p, p^.next, hp);
  1323. {$endif SteDebug}
  1324. If RegInRef(TmpReg, TReference(Pai386(p)^.op1^)) And
  1325. (CurProp^.Regs[TmpReg].Typ = Con_Ref)
  1326. Then
  1327. Begin
  1328. With CurProp^.Regs[TmpReg] Do
  1329. Begin
  1330. IncState(State);
  1331. {also store how many instructions are part of the sequence in the first
  1332. instructions PPaiProp, so it can be easily accessed from within
  1333. CheckSequence}
  1334. Inc(NrOfMods, NrOfInstrSinceLastMod[TmpReg]);
  1335. PPaiProp(Pai(StartMod)^.fileinfo.line)^.Regs[TmpReg].NrOfMods := NrOfMods;
  1336. NrOfInstrSinceLastMod[TmpReg] := 0;
  1337. End;
  1338. End
  1339. Else
  1340. Begin
  1341. DestroyReg(CurProp, TmpReg);
  1342. With CurProp^.Regs[TmpReg] Do
  1343. Begin
  1344. Typ := Con_Ref;
  1345. StartMod := p;
  1346. NrOfMods := 1;
  1347. End;
  1348. End;
  1349. End;
  1350. Top_Const:
  1351. Begin
  1352. Case Pai386(p)^.op2t Of
  1353. Top_Reg:
  1354. Begin
  1355. TmpReg := Reg32(TRegister(Pai386(p)^.op2));
  1356. With CurProp^.Regs[TmpReg] Do
  1357. Begin
  1358. {it doesn't matter that the state is changed,
  1359. it isn't looked at when removing constant reloads}
  1360. DestroyReg(CurProp, TmpReg);
  1361. typ := Con_Const;
  1362. StartMod := Pai386(p)^.op1;
  1363. End
  1364. End;
  1365. Top_Ref: DestroyRefs(P, TReference(Pai386(p)^.op2^), R_NO);
  1366. End;
  1367. End;
  1368. End;
  1369. End;
  1370. A_IMUL:
  1371. Begin
  1372. If (Pai386(p)^.Op3t = top_none)
  1373. Then
  1374. If (Pai386(p)^.Op2t = top_none)
  1375. Then
  1376. Begin
  1377. DestroyReg(CurProp, R_EAX);
  1378. DestroyReg(CurProp, R_EDX)
  1379. End
  1380. Else
  1381. Begin
  1382. If (Pai386(p)^.Op2t = top_reg) Then
  1383. DestroyReg(CurProp, TRegister(Pai386(p)^.Op2));
  1384. End
  1385. Else If (Pai386(p)^.Op3t = top_reg) Then
  1386. DestroyReg(CurProp, TRegister(longint(twowords(Pai386(p)^.Op2).word2)));
  1387. End;
  1388. A_XOR:
  1389. Begin
  1390. If (Pai386(p)^.op1t = top_reg) And
  1391. (Pai386(p)^.op2t = top_reg) And
  1392. (Pai386(p)^.op1 = Pai386(p)^.op2)
  1393. Then
  1394. Begin
  1395. DestroyReg(CurProp, Tregister(Pai386(p)^.op1));
  1396. CurProp^.Regs[Reg32(Tregister(Pai386(p)^.op1))].typ := Con_Const;
  1397. CurProp^.Regs[Reg32(Tregister(Pai386(p)^.op1))].StartMod := Pointer(0)
  1398. End
  1399. Else Destroy(p, Pai386(p)^.op2t, Pai386(p)^.op2);
  1400. End
  1401. Else
  1402. Begin
  1403. Cnt := 1;
  1404. While (Cnt <= MaxCh) And
  1405. (InstrProp.Ch[Cnt] <> C_None) Do
  1406. Begin
  1407. Case InstrProp.Ch[Cnt] Of
  1408. C_WEAX..C_RWEDI: DestroyReg(CurProp, TCh2Reg(InstrProp.Ch[Cnt]));
  1409. C_CDirFlag: CurProp^.DirFlag := F_NotSet;
  1410. C_SDirFlag: CurProp^.DirFlag := F_Set;
  1411. C_Op1: Destroy(p, Pai386(p)^.op1t, Pai386(p)^.op1);
  1412. C_Op2: Destroy(p, Pai386(p)^.op2t, Pai386(p)^.op2);
  1413. C_Op3: Destroy(p, Pai386(p)^.op2t, Pointer(Longint(TwoWords(Pai386(p)^.op2).word2)));
  1414. C_MemEDI:
  1415. Begin
  1416. FillChar(TmpRef, SizeOf(TmpRef), 0);
  1417. TmpRef.Base := R_EDI;
  1418. DestroyRefs(p, TmpRef, R_NO)
  1419. End;
  1420. C_Flags, C_FPU:
  1421. Else
  1422. Begin
  1423. DestroyAllRegs(CurProp);
  1424. End;
  1425. End;
  1426. Inc(Cnt);
  1427. End
  1428. End;
  1429. End;
  1430. End
  1431. Else
  1432. Begin
  1433. DestroyAllRegs(CurProp);
  1434. End;
  1435. End;
  1436. Inc(InstrCnt);
  1437. GetNextInstruction(p, p);
  1438. End;
  1439. End;
  1440. Function InitDFAPass2(AsmL: PAasmOutput): Boolean;
  1441. {reserves memory for the PPaiProps in one big memory block when not using
  1442. TP, returns False if not enough memory is available for the optimizer in all
  1443. cases}
  1444. Var p: Pai;
  1445. Count: Longint;
  1446. { TmpStr: String; }
  1447. Begin
  1448. P := Pai(AsmL^.First);
  1449. If (p^.typ in SkipInstr) Then
  1450. GetNextInstruction(p, p);
  1451. NrOfPaiObjs := 0;
  1452. While Assigned(P) Do
  1453. Begin
  1454. {$IfNDef TP}
  1455. Case P^.Typ Of
  1456. ait_labeled_instruction:
  1457. begin
  1458. If (Pai_Labeled(P)^.lab^.nb >= LoLab) And
  1459. (Pai_Labeled(P)^.lab^.nb <= HiLab) Then
  1460. Inc(LTable^[Pai_Labeled(P)^.lab^.nb-LoLab].RefsFound);
  1461. end;
  1462. ait_label:
  1463. Begin
  1464. If (Pai_Label(p)^.l^.is_used) Then
  1465. LTable^[Pai_Label(P)^.l^.nb-LoLab].InstrNr := NrOfPaiObjs
  1466. End;
  1467. { ait_instruction:
  1468. Begin
  1469. If (Pai386(p)^._operator = A_PUSH) And
  1470. (Pai386(p)^.op1t = top_symbol) And
  1471. (PCSymbol(Pai386(p)^.op1)^.offset = 0) Then
  1472. Begin
  1473. TmpStr := StrPas(PCSymbol(Pai386(p)^.op1)^.symbol);
  1474. If}
  1475. End;
  1476. {$EndIf TP}
  1477. Inc(NrOfPaiObjs);
  1478. GetNextInstruction(p, p);
  1479. End;
  1480. {$IfDef TP}
  1481. If (MemAvail < (SizeOf(TPaiProp)*NrOfPaiObjs))
  1482. Or (NrOfPaiObjs = 0)
  1483. {this doesn't have to be one contiguous block}
  1484. Then InitDFAPass2 := False
  1485. Else InitDFAPass2 := True;
  1486. {$Else}
  1487. {Uncomment the next line to see how much memory the reloading optimizer needs}
  1488. { Writeln((NrOfPaiObjs*(((SizeOf(TPaiProp)+3)div 4)*4)));}
  1489. {no need to check mem/maxavail, we've got as much virtual memory as we want}
  1490. If NrOfPaiObjs <> 0 Then
  1491. Begin
  1492. InitDFAPass2 := True;
  1493. GetMem(PaiPropBlock, NrOfPaiObjs*(((SizeOf(TPaiProp)+3)div 4)*4));
  1494. p := Pai(AsmL^.First);
  1495. If (p^.typ in SkipInstr) Then
  1496. GetNextInstruction(p, p);
  1497. For Count := 1 To NrOfPaiObjs Do
  1498. Begin
  1499. PaiPropBlock^[Count].LineSave := p^.fileinfo.line;
  1500. PPaiProp(p^.fileinfo.line) := @PaiPropBlock^[Count];
  1501. GetNextInstruction(p, p);
  1502. End;
  1503. End
  1504. Else InitDFAPass2 := False;
  1505. {$EndIf TP}
  1506. End;
  1507. Function DFAPass2(AsmL: PAasmOutPut): Pai;
  1508. Begin
  1509. If InitDFAPass2(AsmL)
  1510. Then DFAPass2 := DoDFAPass2(
  1511. {$ifdef statedebug}
  1512. asml,
  1513. {$endif statedbug}
  1514. Pai(AsmL^.First))
  1515. Else DFAPass2 := Nil;
  1516. End;
  1517. Procedure ShutDownDFA;
  1518. Begin
  1519. If LabDif <> 0 Then
  1520. FreeMem(LTable, LabDif*SizeOf(TLabelTableItem));
  1521. End;
  1522. End.
  1523. {
  1524. $Log$
  1525. Revision 1.15 1998-09-20 18:00:20 florian
  1526. * small compiling problems fixed
  1527. Revision 1.14 1998/09/20 17:12:36 jonas
  1528. * small fix for uncertain optimizations & more cleaning up
  1529. Revision 1.12 1998/09/16 18:00:01 jonas
  1530. * optimizer now completely dependant on GetNext/GetLast instruction, works again with -dRegAlloc
  1531. Revision 1.11 1998/09/15 14:05:27 jonas
  1532. * fixed optimizer incompatibilities with freelabel code in psub
  1533. Revision 1.10 1998/09/09 15:33:58 peter
  1534. * removed warnings
  1535. Revision 1.9 1998/09/03 16:24:51 florian
  1536. * bug of type conversation from dword to real fixed
  1537. * bug fix of Jonas applied
  1538. Revision 1.8 1998/08/28 10:56:59 peter
  1539. * removed warnings
  1540. Revision 1.7 1998/08/19 16:07:44 jonas
  1541. * changed optimizer switches + cleanup of DestroyRefs in daopt386.pas
  1542. Revision 1.6 1998/08/10 14:49:57 peter
  1543. + localswitches, moduleswitches, globalswitches splitting
  1544. Revision 1.5 1998/08/09 13:56:24 jonas
  1545. * small bugfix for uncertain optimizations in DestroyRefs
  1546. Revision 1.4 1998/08/06 19:40:25 jonas
  1547. * removed $ before and after Log in comment
  1548. Revision 1.3 1998/08/05 16:00:14 florian
  1549. * some fixes for ansi strings
  1550. * log to Log changed
  1551. }