daopt386.pas 55 KB

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