daopt386.pas 78 KB

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