daopt386.pas 82 KB

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