daopt386.pas 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256
  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
  25. GlobType,
  26. CObjects,Aasm,
  27. i386base,i386asm
  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. New2OldReg: TRegArray;
  36. End;
  37. {possible actions on an operand: read, write or modify (= read & write)}
  38. TOpAction = (OpAct_Read, OpAct_Write, OpAct_Modify, OpAct_Unknown);
  39. {*********************** Procedures and Functions ************************}
  40. Procedure InsertLLItem(AsmL: PAasmOutput; prev, foll, new_one: PLinkedList_Item);
  41. Function Reg32(Reg: TRegister): TRegister;
  42. Function RefsEquivalent(Const R1, R2: TReference; Var RegInfo: TRegInfo; OpAct: TOpAction): Boolean;
  43. Function RefsEqual(Const R1, R2: TReference): Boolean;
  44. Function IsGP32Reg(Reg: TRegister): Boolean;
  45. Function RegInRef(Reg: TRegister; Const Ref: TReference): Boolean;
  46. Function RegInInstruction(Reg: TRegister; p1: Pai): Boolean;
  47. Function RegModifiedByInstruction(Reg: TRegister; p1: Pai): Boolean;
  48. Function GetNextInstruction(Current: Pai; Var Next: Pai): Boolean;
  49. Function GetLastInstruction(Current: Pai; Var Last: Pai): Boolean;
  50. Procedure SkipHead(var P: Pai);
  51. Procedure UpdateUsedRegs(Var UsedRegs: TRegSet; p: Pai);
  52. Function RegsEquivalent(OldReg, NewReg: TRegister; Var RegInfo: TRegInfo; OpAct: TopAction): Boolean;
  53. Function InstructionsEquivalent(p1, p2: Pai; Var RegInfo: TRegInfo): Boolean;
  54. Function OpsEqual(const o1,o2:toper): Boolean;
  55. Function DFAPass1(AsmL: PAasmOutput; BlockStart: Pai): Pai;
  56. Function DFAPass2(
  57. {$ifdef statedebug}
  58. AsmL: PAasmOutPut;
  59. {$endif statedebug}
  60. BlockStart, BlockEnd: Pai): Boolean;
  61. Procedure ShutDownDFA;
  62. Function FindLabel(L: PLabel; Var hp: Pai): Boolean;
  63. {******************************* Constants *******************************}
  64. Const
  65. {ait_* types which don't result in executable code or which don't influence
  66. the way the program runs/behaves}
  67. SkipInstr = [ait_comment, ait_align, ait_symbol
  68. {$ifdef GDB}
  69. ,ait_stabs, ait_stabn, ait_stab_function_name
  70. {$endif GDB}
  71. ,ait_regalloc, ait_tempalloc
  72. ];
  73. {the maximum number of things (registers, memory, ...) a single instruction
  74. changes}
  75. MaxCh = 3;
  76. {Possible register content types}
  77. con_Unknown = 0;
  78. con_ref = 1;
  79. con_const = 2;
  80. {********************************* Types *********************************}
  81. Type
  82. {What an instruction can change}
  83. TChange = (C_None,
  84. {Read from a register}
  85. C_REAX, C_RECX, C_REDX, C_REBX, C_RESP, C_REBP, C_RESI, C_REDI,
  86. {write from a register}
  87. C_WEAX, C_WECX, C_WEDX, C_WEBX, C_WESP, C_WEBP, C_WESI, C_WEDI,
  88. {read and write from/to a register}
  89. C_RWEAX, C_RWECX, C_RWEDX, C_RWEBX, C_RWESP, C_RWEBP, C_RWESI, C_RWEDI,
  90. C_CDirFlag {clear direction flag}, C_SDirFlag {set dir flag},
  91. C_RFlags, C_WFlags, C_RWFlags, C_FPU,
  92. C_Rop1, C_Wop1, C_RWop1,
  93. C_Rop2, C_Wop2, C_RWop2,
  94. C_Rop3, C_WOp3, C_RWOp3,
  95. C_WMemEDI,
  96. C_All);
  97. {the possible states of a flag}
  98. TFlagContents = (F_Unknown, F_NotSet, F_Set);
  99. {the properties of a cpu instruction}
  100. TAsmInstrucProp = Record
  101. {how many things it changes}
  102. { NCh: Byte;}
  103. {and what it changes}
  104. Ch: Array[1..MaxCh] of TChange;
  105. End;
  106. TContent = Packed Record
  107. {start and end of block instructions that defines the
  108. content of this register. If Typ = con_const, then
  109. Longint(StartMod) = value of the constant)}
  110. StartMod: pai;
  111. {starts at 0, gets increased everytime the register is written to}
  112. WState: Byte;
  113. {starts at 0, gets increased everytime the register is read from}
  114. RState: Byte;
  115. {how many instructions starting with StarMod does the block consist of}
  116. NrOfMods: Byte;
  117. {the type of the content of the register: unknown, memory, constant}
  118. Typ: Byte;
  119. End;
  120. {Contents of the integer registers}
  121. TRegContent = Array[R_EAX..R_EDI] Of TContent;
  122. {contents of the FPU registers}
  123. TRegFPUContent = Array[R_ST..R_ST7] Of TContent;
  124. {information record with the contents of every register. Every Pai object
  125. gets one of these assigned: a pointer to it is stored in the OptInfo field}
  126. TPaiProp = Record
  127. Regs: TRegContent;
  128. { FPURegs: TRegFPUContent;} {currently not yet used}
  129. {allocated Registers}
  130. UsedRegs: TRegSet;
  131. {status of the direction flag}
  132. DirFlag: TFlagContents;
  133. {can this instruction be removed?}
  134. CanBeRemoved: Boolean;
  135. End;
  136. PPaiProp = ^TPaiProp;
  137. {$IfNDef TP}
  138. TPaiPropBlock = Array[1..250000] Of TPaiProp;
  139. PPaiPropBlock = ^TPaiPropBlock;
  140. {$EndIf TP}
  141. TInstrSinceLastMod = Array[R_EAX..R_EDI] Of Byte;
  142. TLabelTableItem = Record
  143. PaiObj: Pai;
  144. {$IfDef JumpAnal}
  145. InstrNr: Longint;
  146. RefsFound: Word;
  147. JmpsProcessed: Word
  148. {$EndIf JumpAnal}
  149. End;
  150. {$IfDef tp}
  151. TLabelTable = Array[0..10000] Of TLabelTableItem;
  152. {$Else tp}
  153. TLabelTable = Array[0..2500000] Of TLabelTableItem;
  154. {$Endif tp}
  155. PLabelTable = ^TLabelTable;
  156. {******************************* Variables *******************************}
  157. Var
  158. {the amount of PaiObjects in the current assembler list}
  159. NrOfPaiObjs: Longint;
  160. {$IfNDef TP}
  161. {Array which holds all TPaiProps}
  162. PaiPropBlock: PPaiPropBlock;
  163. {$EndIf TP}
  164. LoLab, HiLab, LabDif: Longint;
  165. LTable: PLabelTable;
  166. {*********************** End of Interface section ************************}
  167. Implementation
  168. Uses
  169. globals, systems, strings, verbose, hcodegen;
  170. Const AsmInstr: Array[tasmop] Of TAsmInstrucProp = (
  171. {A_<NONE>} (Ch: (C_All, C_None, C_None)), { new }
  172. {A_LOCK} (Ch: (C_None, C_None, C_None)),
  173. {A_REP} (Ch: (C_RWECX, C_RFlags, C_None)),
  174. {A_REPE} (Ch: (C_RWECX, C_RFlags, C_None)),
  175. {A_REPNE} (Ch: (C_RWECX, C_RFlags, C_None)),
  176. {A_REPNZ} (Ch: (C_WECX, C_RWFLAGS, C_None)), { new }
  177. {A_REPZ} (Ch: (C_WECX, C_RWFLAGS, C_None)), { new }
  178. {A_SEGCS} (Ch: (C_None, C_None, C_None)), { new }
  179. {A_SEGES} (Ch: (C_None, C_None, C_None)), { new }
  180. {A_SEGDS} (Ch: (C_None, C_None, C_None)), { new }
  181. {A_SEGFS} (Ch: (C_None, C_None, C_None)), { new }
  182. {A_SEGGS} (Ch: (C_None, C_None, C_None)), { new }
  183. {A_SEGSS} (Ch: (C_None, C_None, C_None)), { new }
  184. {A_AAA} (Ch: (C_RWEAX, C_WFlags, C_None)),
  185. {A_AAD} (Ch: (C_RWEAX, C_WFlags, C_None)),
  186. {A_AAM} (Ch: (C_RWEAX, C_WFlags, C_None)),
  187. {A_AAS} (Ch: (C_RWEAX, C_WFlags, C_None)),
  188. {A_ADC} (Ch: (C_RWop2, C_Rop1, C_RWFlags)),
  189. {A_ADD} (Ch: (C_RWop2, C_Rop1, C_WFlags)),
  190. {A_AND} (Ch: (C_RWop2, C_Rop1, C_WFlags)),
  191. {A_ARPL} (Ch: (C_WFlags, C_None, C_None)),
  192. {A_BOUND} (Ch: (C_Rop1, C_None, C_None)),
  193. {A_BSF} (Ch: (C_Wop2, C_WFlags, C_Rop1)),
  194. {A_BSR} (Ch: (C_Wop2, C_WFlags, C_Rop1)),
  195. {A_BSWAP} (Ch: (C_RWOp1, C_None, C_None)), { new }
  196. {A_BT} (Ch: (C_WFlags, C_Rop1, C_None)),
  197. {A_BTC} (Ch: (C_RWop2, C_Rop1, C_WFlags)),
  198. {A_BTR} (Ch: (C_RWop2, C_Rop1, C_WFlags)),
  199. {A_BTS} (Ch: (C_RWop2, C_Rop1, C_WFlags)),
  200. {A_CALL} (Ch: (C_All, C_None, C_None)), {don't know value of any register}
  201. {A_CBW} (Ch: (C_RWEAX, C_None, C_None)),
  202. {A_CDQ} (Ch: (C_RWEAX, C_WEDX, C_None)),
  203. {A_CLC} (Ch: (C_WFlags, C_None, C_None)),
  204. {A_CLD} (Ch: (C_CDirFlag, C_None, C_None)),
  205. {A_CLI} (Ch: (C_WFlags, C_None, C_None)),
  206. {A_CLTS} (Ch: (C_None, C_None, C_None)),
  207. {A_CMC} (Ch: (C_WFlags, C_None, C_None)),
  208. {A_CMP} (Ch: (C_WFlags, C_None, C_None)),
  209. {A_CMPSB} (Ch: (C_All, C_None, C_None)), { new }
  210. {A_CMPSD} (Ch: (C_All, C_None, C_None)), { new }
  211. {A_CMPSW} (Ch: (C_All, C_None, C_None)), { new }
  212. {A_CMPXCHG} (Ch: (C_All, C_None, C_None)), { new }
  213. {A_CMPXCHG486} (Ch: (C_All, C_None, C_None)), { new }
  214. {A_CMPXCHG8B} (Ch: (C_All, C_None, C_None)), { new }
  215. {A_CPUID} (Ch: (C_All, C_None, C_none)),
  216. {A_CWD} (Ch: (C_RWEAX, C_WEDX, C_None)),
  217. {A_CWDE} (Ch: (C_RWEAX, C_None, C_None)),
  218. {A_DAA} (Ch: (C_RWEAX, C_None, C_None)),
  219. {A_DAS} (Ch: (C_RWEAX, C_None, C_None)),
  220. {A_DEC} (Ch: (C_RWop1, C_WFlags, C_None)),
  221. {A_DIV} (Ch: (C_RWEAX, C_WEDX, C_WFlags)),
  222. {A_EMMS} (Ch: (C_FPU, C_None, C_None)), { new }
  223. {A_ENTER} (Ch: (C_RWESP, C_None, C_None)),
  224. {A_EQU} (Ch: (C_None, C_None, C_None)), { new }
  225. {A_F2XM1} (Ch: (C_FPU, C_None, C_None)),
  226. {A_FABS} (Ch: (C_FPU, C_None, C_None)),
  227. {A_FADD} (Ch: (C_FPU, C_None, C_None)),
  228. {A_FADDP} (Ch: (C_FPU, C_None, C_None)),
  229. {A_FBLD} (Ch: (C_Rop1, C_FPU, C_None)),
  230. {A_FBSTP} (Ch: (C_Wop1, C_FPU, C_None)),
  231. {A_FCHS} (Ch: (C_FPU, C_None, C_None)),
  232. {A_FCLEX} (Ch: (C_FPU, C_None, C_None)),
  233. {A_FCMOVB} (Ch: (C_FPU, C_RFLAGS, C_None)), { new }
  234. {A_FCMOVBE} (Ch: (C_FPU, C_RFLAGS, C_None)), { new }
  235. {A_FCMOVE} (Ch: (C_FPU, C_RFLAGS, C_None)), { new }
  236. {A_FCMOVNB} (Ch: (C_FPU, C_RFLAGS, C_None)), { new }
  237. {A_FCMOVNBE} (Ch: (C_FPU, C_RFLAGS, C_None)), { new }
  238. {A_FCMOVNE} (Ch: (C_FPU, C_RFLAGS, C_None)), { new }
  239. {A_FCMOVNU} (Ch: (C_FPU, C_RFLAGS, C_None)), { new }
  240. {A_FCMOVU} (Ch: (C_FPU, C_RFLAGS, C_None)), { new }
  241. {A_FCOM} (Ch: (C_FPU, C_None, C_None)),
  242. {A_FCOMI} (Ch: (C_WFLAGS, C_None, C_None)), { new }
  243. {A_FCOMIP} (Ch: (C_FPU, C_WFLAGS, C_None)), { new }
  244. {A_FCOMP} (Ch: (C_FPU, C_None, C_None)),
  245. {A_FCOMPP} (Ch: (C_FPU, C_None, C_None)),
  246. {A_FCOS} (Ch: (C_FPU, C_None, C_None)),
  247. {A_FDECSTP} (Ch: (C_FPU, C_None, C_None)),
  248. {A_FDISI} (Ch: (C_FPU, C_None, C_None)),
  249. {A_FDIV} (Ch: (C_FPU, C_None, C_None)),
  250. {A_FDIVP} (Ch: (C_FPU, C_None, C_None)),
  251. {A_FDIVR} (Ch: (C_FPU, C_None, C_None)),
  252. {A_FDIVRP} (Ch: (C_FPU, C_None, C_None)),
  253. {A_FEMMS} (Ch: (C_All, C_None, C_None)), { new }
  254. {A_FENI} (Ch: (C_FPU, C_None, C_None)),
  255. {A_FFREE} (Ch: (C_FPU, C_None, C_None)),
  256. {A_FIADD} (Ch: (C_FPU, C_None, C_None)),
  257. {A_FICOM} (Ch: (C_FPU, C_None, C_None)),
  258. {A_FICOMP} (Ch: (C_FPU, C_None, C_None)),
  259. {A_FIDIV} (Ch: (C_FPU, C_None, C_None)),
  260. {A_FIDIVR} (Ch: (C_FPU, C_None, C_None)),
  261. {A_FILD} (Ch: (C_FPU, C_None, C_None)),
  262. {A_FIMUL} (Ch: (C_FPU, C_None, C_None)),
  263. {A_FINCSTP} (Ch: (C_FPU, C_None, C_None)),
  264. {A_FINIT} (Ch: (C_FPU, C_None, C_None)),
  265. {A_FIST} (Ch: (C_Wop1, C_None, C_None)),
  266. {A_FISTP} (Ch: (C_Wop1, C_None, C_None)),
  267. {A_FISUB} (Ch: (C_FPU, C_None, C_None)),
  268. {A_FISUBR} (Ch: (C_FPU, C_None, C_None)), { new }
  269. {A_FLD} (Ch: (C_Rop1, C_FPU, C_None)),
  270. {A_FLD1} (Ch: (C_FPU, C_None, C_None)),
  271. {A_FLDCW} (Ch: (C_FPU, C_None, C_None)),
  272. {A_FLDENV} (Ch: (C_FPU, C_None, C_None)),
  273. {A_FLDL2E} (Ch: (C_FPU, C_None, C_None)),
  274. {A_FLDL2T} (Ch: (C_FPU, C_None, C_None)),
  275. {A_FLDLG2} (Ch: (C_FPU, C_None, C_None)),
  276. {A_FLDLN2} (Ch: (C_FPU, C_None, C_None)),
  277. {A_FLDPI} (Ch: (C_FPU, C_None, C_None)),
  278. {A_FLDZ} (Ch: (C_FPU, C_None, C_None)),
  279. {A_FMUL} (Ch: (C_FPU, C_None, C_None)),
  280. {A_FMULP} (Ch: (C_FPU, C_None, C_None)),
  281. {A_FNCLEX} (Ch: (C_FPU, C_None, C_None)),
  282. {A_FNDISI} (Ch: (C_FPU, C_None, C_None)),
  283. {A_FNENI} (Ch: (C_FPU, C_None, C_None)),
  284. {A_FNINIT} (Ch: (C_FPU, C_None, C_None)),
  285. {A_FNOP} (Ch: (C_FPU, C_None, C_None)),
  286. {A_FNSAVE} (Ch: (C_FPU, C_None, C_None)),
  287. {A_FNSTCW} (Ch: (C_Wop1, C_None, C_None)),
  288. {A_FNSTENV} (Ch: (C_Wop1, C_None, C_None)),
  289. {A_FNSTSW} (Ch: (C_Wop1, C_None, C_None)),
  290. {A_FPATAN} (Ch: (C_FPU, C_None, C_None)),
  291. {A_FPREM} (Ch: (C_FPU, C_None, C_None)),
  292. {A_FPREM1} (Ch: (C_FPU, C_None, C_None)),
  293. {A_FPTAN} (Ch: (C_FPU, C_None, C_None)),
  294. {A_FRNDINT} (Ch: (C_FPU, C_None, C_None)),
  295. {A_FRSTOR} (Ch: (C_FPU, C_None, C_None)),
  296. {A_FSAVE} (Ch: (C_Wop1, C_None, C_None)),
  297. {A_FSCALE} (Ch: (C_FPU, C_None, C_None)),
  298. {A_FSETPM} (Ch: (C_FPU, C_None, C_None)),
  299. {A_FSIN} (Ch: (C_FPU, C_None, C_None)),
  300. {A_FSINCOS} (Ch: (C_FPU, C_None, C_None)),
  301. {A_FSQRT} (Ch: (C_FPU, C_None, C_None)),
  302. {A_FST} (Ch: (C_Wop1, C_None, C_None)),
  303. {A_FSTCW} (Ch: (C_Wop1, C_None, C_None)),
  304. {A_FSTENV} (Ch: (C_Wop1, C_None, C_None)),
  305. {A_FSTP} (Ch: (C_Wop1, C_FPU, C_None)),
  306. {A_FSTSW} (Ch: (C_Wop1, C_None, C_None)),
  307. {A_FSUB} (Ch: (C_FPU, C_None, C_None)),
  308. {A_FSUBP} (Ch: (C_FPU, C_None, C_None)),
  309. {A_FSUBR} (Ch: (C_FPU, C_None, C_None)),
  310. {A_FSUBRP} (Ch: (C_FPU, C_None, C_None)),
  311. {A_FTST} (Ch: (C_FPU, C_None, C_None)),
  312. {A_FUCOM} (Ch: (C_None, C_None, C_None)), {changes fpu status word}
  313. {A_FUCOMI} (Ch: (C_WFLAGS, C_None, C_None)), { new }
  314. {A_FUCOMIP} (Ch: (C_FPU, C_WFLAGS, C_None)), { new }
  315. {A_FUCOMP} (Ch: (C_FPU, C_None, C_None)),
  316. {A_FUCOMPP} (Ch: (C_FPU, C_None, C_None)),
  317. {A_FWAIT} (Ch: (C_FPU, C_None, C_None)),
  318. {A_FXAM} (Ch: (C_FPU, C_None, C_None)),
  319. {A_FXCH} (Ch: (C_FPU, C_None, C_None)),
  320. {A_FXTRACT} (Ch: (C_FPU, C_None, C_None)),
  321. {A_FYL2X} (Ch: (C_FPU, C_None, C_None)),
  322. {A_FYL2XP1} (Ch: (C_FPU, C_None, C_None)),
  323. {A_HLT} (Ch: (C_None, C_None, C_None)),
  324. {A_IBTS} (Ch: (C_All, C_None, C_None)), { new }
  325. {A_ICEBP} (Ch: (C_All, C_None, C_None)), { new }
  326. {A_IDIV} (Ch: (C_RWEAX, C_WEDX, C_WFlags)),
  327. {A_IMUL} (Ch: (C_RWEAX, C_WEDX, C_WFlags)), {handled separately, because several forms exist}
  328. {A_IN} (Ch: (C_Wop2, C_Rop1, C_None)),
  329. {A_INC} (Ch: (C_RWop1, C_WFlags, C_None)),
  330. {A_INSB} (Ch: (C_WMemEDI, C_RWEDI, C_None)), { new }
  331. {A_INSD} (Ch: (C_WMemEDI, C_RWEDI, C_None)), { new }
  332. {A_INSW} (Ch: (C_WMemEDI, C_RWEDI, C_None)), { new }
  333. {A_INT} (Ch: (C_All, C_None, C_None)), {don't know value of any register}
  334. {A_INT01} (Ch: (C_All, C_None, C_None)), { new }
  335. {A_INT1} (Ch: (C_All, C_None, C_None)), { new }
  336. {A_INT3} (Ch: (C_None, C_None, C_None)),
  337. {A_INTO} (Ch: (C_All, C_None, C_None)), {don't know value of any register}
  338. {A_INVD} (Ch: (C_All, C_None, C_None)), { new }
  339. {A_INVLPG} (Ch: (C_All, C_None, C_None)), { new }
  340. {A_IRET} (Ch: (C_All, C_None, C_None)), {don't know value of any register}
  341. {A_IRETD} (Ch: (C_All, C_None, C_None)), { new }
  342. {A_IRETW} (Ch: (C_All, C_None, C_None)), { new }
  343. {A_JCXZ} (Ch: (C_RECX, C_None, C_None)),
  344. {A_JECXZ} (Ch: (C_RECX, C_None, C_None)),
  345. {A_JMP} (Ch: (C_None, C_None, C_None)),
  346. {A_LAHF} (Ch: (C_WEAX, C_RFlags, C_None)),
  347. {A_LAR} (Ch: (C_Wop2, C_None, C_None)),
  348. {A_LDS} (Ch: (C_Wop2, C_None, C_None)),
  349. {A_LEA} (Ch: (C_Wop2, C_Rop1, C_None)),
  350. {A_LEAVE} (Ch: (C_RWESP, C_None, C_None)),
  351. {A_LES} (Ch: (C_Wop2, C_None, C_None)),
  352. {A_LFS} (Ch: (C_Wop2, C_None, C_None)),
  353. {A_LGDT} (Ch: (C_None, C_None, C_None)),
  354. {A_LGS} (Ch: (C_Wop2, C_None, C_None)),
  355. {A_LIDT} (Ch: (C_None, C_None, C_None)),
  356. {A_LLDT} (Ch: (C_None, C_None, C_None)),
  357. {A_LMSW} (Ch: (C_None, C_None, C_None)),
  358. {A_LOADALL} (Ch: (C_All, C_None, C_None)), { new }
  359. {A_LOADALL286} (Ch: (C_All, C_None, C_None)), { new }
  360. {A_LODSB} (Ch: (C_WEAX, C_RWESI, C_None)), { new }
  361. {A_LODSD} (Ch: (C_WEAX, C_RWESI, C_None)), { new }
  362. {A_LODSW} (Ch: (C_WEAX, C_RWESI, C_None)), { new }
  363. {A_LOOP} (Ch: (C_RWECX, C_None, C_None)),
  364. {A_LOOPE} (Ch: (C_RWECX, C_RFlags, C_None)),
  365. {A_LOOPNE} (Ch: (C_RWECX, C_RFlags, C_None)),
  366. {A_LOOPNZ} (Ch: (C_RWECX, C_RFlags, C_None)),
  367. {A_LOOPZ} (Ch: (C_RWECX, C_RFlags, C_None)),
  368. {A_LSL} (Ch: (C_Wop2, C_WFlags, C_None)),
  369. {A_LSS} (Ch: (C_Wop2, C_None, C_None)),
  370. {A_LTR} (Ch: (C_None, C_None, C_None)),
  371. {A_MOV} (Ch: (C_Wop2, C_Rop1, C_None)),
  372. {A_MOVD} (Ch: (C_All, C_None, C_None)), { new }
  373. {A_MOVQ} (Ch: (C_All, C_None, C_None)), { new }
  374. {A_MOVSB} (Ch: (C_All, C_Rop1, C_None)),
  375. {A_MOVSD} (Ch: (C_All, C_None, C_None)), { new }
  376. {A_MOVSW} (Ch: (C_All, C_None, C_None)), { new }
  377. {A_MOVSX} (Ch: (C_Wop2, C_Rop1, C_None)),
  378. {A_MOVZX} (Ch: (C_Wop2, C_Rop1, C_None)),
  379. {A_MUL} (Ch: (C_RWEAX, C_WEDX, C_WFlags)),
  380. {A_NEG} (Ch: (C_RWop1, C_None, C_None)),
  381. {A_NOP} (Ch: (C_None, C_None, C_None)),
  382. {A_NOT} (Ch: (C_RWop1, C_WFlags, C_None)),
  383. {A_OR} (Ch: (C_RWop2, C_WFlags, C_None)),
  384. {A_OUT} (Ch: (C_Rop1, C_Rop2, C_None)),
  385. {A_OUTSB} (Ch: (C_All, C_None, C_None)), { new }
  386. {A_OUTSD} (Ch: (C_All, C_None, C_None)), { new }
  387. {A_OUTSW} (Ch: (C_All, C_None, C_None)), { new }
  388. {A_PACKSSDW} (Ch: (C_All, C_None, C_None)), { new }
  389. {A_PACKSSWB} (Ch: (C_All, C_None, C_None)), { new }
  390. {A_PACKUSWB} (Ch: (C_All, C_None, C_None)), { new }
  391. {A_PADDB} (Ch: (C_All, C_None, C_None)), { new }
  392. {A_PADDD} (Ch: (C_All, C_None, C_None)), { new }
  393. {A_PADDSB} (Ch: (C_All, C_None, C_None)), { new }
  394. {A_PADDSIW} (Ch: (C_All, C_None, C_None)), { new }
  395. {A_PADDSW} (Ch: (C_All, C_None, C_None)), { new }
  396. {A_PADDUSB} (Ch: (C_All, C_None, C_None)), { new }
  397. {A_PADDUSW} (Ch: (C_All, C_None, C_None)), { new }
  398. {A_PADDW} (Ch: (C_All, C_None, C_None)), { new }
  399. {A_PAND} (Ch: (C_All, C_None, C_None)), { new }
  400. {A_PANDN} (Ch: (C_All, C_None, C_None)), { new }
  401. {A_PAVEB} (Ch: (C_All, C_None, C_None)), { new }
  402. {A_PAVGUSB} (Ch: (C_All, C_None, C_None)), { new }
  403. {A_PCMPEQB} (Ch: (C_All, C_None, C_None)), { new }
  404. {A_PCMPEQD} (Ch: (C_All, C_None, C_None)), { new }
  405. {A_PCMPEQW} (Ch: (C_All, C_None, C_None)), { new }
  406. {A_PCMPGTB} (Ch: (C_All, C_None, C_None)), { new }
  407. {A_PCMPGTD} (Ch: (C_All, C_None, C_None)), { new }
  408. {A_PCMPGTW} (Ch: (C_All, C_None, C_None)), { new }
  409. {A_PDISTIB} (Ch: (C_All, C_None, C_None)), { new }
  410. {A_PF2ID} (Ch: (C_All, C_None, C_None)), { new }
  411. {A_PFACC} (Ch: (C_All, C_None, C_None)), { new }
  412. {A_PFADD} (Ch: (C_All, C_None, C_None)), { new }
  413. {A_PFCMPEQ} (Ch: (C_All, C_None, C_None)), { new }
  414. {A_PFCMPGE} (Ch: (C_All, C_None, C_None)), { new }
  415. {A_PFCMPGT} (Ch: (C_All, C_None, C_None)), { new }
  416. {A_PFMAX} (Ch: (C_All, C_None, C_None)), { new }
  417. {A_PFMIN} (Ch: (C_All, C_None, C_None)), { new }
  418. {A_PFMUL} (Ch: (C_All, C_None, C_None)), { new }
  419. {A_PFRCP} (Ch: (C_All, C_None, C_None)), { new }
  420. {A_PFRCPIT1} (Ch: (C_All, C_None, C_None)), { new }
  421. {A_PFRCPIT2} (Ch: (C_All, C_None, C_None)), { new }
  422. {A_PFRSQIT1} (Ch: (C_All, C_None, C_None)), { new }
  423. {A_PFRSQRT} (Ch: (C_All, C_None, C_None)), { new }
  424. {A_PFSUB} (Ch: (C_All, C_None, C_None)), { new }
  425. {A_PFSUBR} (Ch: (C_All, C_None, C_None)), { new }
  426. {A_PI2FD} (Ch: (C_All, C_None, C_None)), { new }
  427. {A_PMACHRIW} (Ch: (C_All, C_None, C_None)), { new }
  428. {A_PMADDWD} (Ch: (C_All, C_None, C_None)), { new }
  429. {A_PMAGW} (Ch: (C_All, C_None, C_None)), { new }
  430. {A_PMULHRIW} (Ch: (C_All, C_None, C_None)), { new }
  431. {A_PMULHRWA} (Ch: (C_All, C_None, C_None)), { new }
  432. {A_PMULHRWC} (Ch: (C_All, C_None, C_None)), { new }
  433. {A_PMULHW} (Ch: (C_All, C_None, C_None)), { new }
  434. {A_PMULLW} (Ch: (C_All, C_None, C_None)), { new }
  435. {A_PMVGEZB} (Ch: (C_All, C_None, C_None)), { new }
  436. {A_PMVLZB} (Ch: (C_All, C_None, C_None)), { new }
  437. {A_PMVNZB} (Ch: (C_All, C_None, C_None)), { new }
  438. {A_PMVZB} (Ch: (C_All, C_None, C_None)), { new }
  439. {A_POP} (Ch: (C_Wop1, C_RWESP, C_None)),
  440. {A_POPA} (Ch: (C_All, C_None, C_None)), {don't know value of any register}
  441. {A_POPAD} (Ch: (C_All, C_None, C_None)), {don't know value of any register}
  442. {A_POPAW} (Ch: (C_All, C_None, C_None)), { new }
  443. {A_POPF} (Ch: (C_RWESP, C_WFlags, C_None)),
  444. {A_POPFD} (Ch: (C_RWESP, C_WFlags, C_None)),
  445. {A_POPFW} (Ch: (C_RWESP, C_WFLAGS, C_None)), { new }
  446. {A_POR} (Ch: (C_All, C_None, C_None)), { new }
  447. {A_PREFETCH} (Ch: (C_All, C_None, C_None)), { new }
  448. {A_PREFETCHW} (Ch: (C_All, C_None, C_None)), { new }
  449. {A_PSLLD} (Ch: (C_All, C_None, C_None)), { new }
  450. {A_PSLLQ} (Ch: (C_All, C_None, C_None)), { new }
  451. {A_PSLLW} (Ch: (C_All, C_None, C_None)), { new }
  452. {A_PSRAD} (Ch: (C_All, C_None, C_None)), { new }
  453. {A_PSRAW} (Ch: (C_All, C_None, C_None)), { new }
  454. {A_PSRLD} (Ch: (C_All, C_None, C_None)), { new }
  455. {A_PSRLQ} (Ch: (C_All, C_None, C_None)), { new }
  456. {A_PSRLW} (Ch: (C_All, C_None, C_None)), { new }
  457. {A_PSUBB} (Ch: (C_All, C_None, C_None)), { new }
  458. {A_PSUBD} (Ch: (C_All, C_None, C_None)), { new }
  459. {A_PSUBSB} (Ch: (C_All, C_None, C_None)), { new }
  460. {A_PSUBSIW} (Ch: (C_All, C_None, C_None)), { new }
  461. {A_PSUBSW} (Ch: (C_All, C_None, C_None)), { new }
  462. {A_PSUBUSB} (Ch: (C_All, C_None, C_None)), { new }
  463. {A_PSUBUSW} (Ch: (C_All, C_None, C_None)), { new }
  464. {A_PSUBW} (Ch: (C_All, C_None, C_None)), { new }
  465. {A_PUNPCKHBW} (Ch: (C_All, C_None, C_None)), { new }
  466. {A_PUNPCKHDQ} (Ch: (C_All, C_None, C_None)), { new }
  467. {A_PUNPCKHWD} (Ch: (C_All, C_None, C_None)), { new }
  468. {A_PUNPCKLBW} (Ch: (C_All, C_None, C_None)), { new }
  469. {A_PUNPCKLDQ} (Ch: (C_All, C_None, C_None)), { new }
  470. {A_PUNPCKLWD} (Ch: (C_All, C_None, C_None)), { new }
  471. {A_PUSH} (Ch: (C_RWESP, C_None, C_None)),
  472. {A_PUSHA} (Ch: (C_All, C_None, C_None)),
  473. {A_PUSHAD} (Ch: (C_All, C_None, C_None)),
  474. {A_PUSHAW} (Ch: (C_All, C_None, C_None)), { new }
  475. {A_PUSHF} (Ch: (C_RWESP, C_RFlags, C_None)),
  476. {A_PUSHFD} (Ch: (C_RWESP, C_RFlags, C_None)),
  477. {A_PUSHFW} (Ch: (C_RWESP, C_RFLAGS, C_None)), { new }
  478. {A_PXOR} (Ch: (C_All, C_None, C_None)), { new }
  479. {A_RCL} (Ch: (C_RWop2, C_Rop1, C_RWFlags)),
  480. {A_RCR} (Ch: (C_RWop2, C_Rop1, C_RWFlags)),
  481. {A_RDMSR} (Ch: (C_WEAX, C_WEDX, C_None)), { new }
  482. {A_RDPMC} (Ch: (C_WEAX, C_WEDX, C_None)), { new }
  483. {A_RDTSC} (Ch: (C_WEAX, C_WEDX, C_None)), { new }
  484. {A_RESB} (Ch: (C_All, C_None, C_None)), { new }
  485. {A_RET} (Ch: (C_All, C_None, C_None)),
  486. {A_RETF} (Ch: (C_All, C_None, C_None)), { new }
  487. {A_RETN} (Ch: (C_All, C_None, C_None)), { new }
  488. {A_ROL} (Ch: (C_RWop2, C_Rop1, C_RWFlags)),
  489. {A_ROR} (Ch: (C_RWop2, C_Rop1, C_RWFlags)),
  490. {A_RSM} (Ch: (C_All, C_None, C_None)), { new }
  491. {A_SAHF} (Ch: (C_WFlags, C_REAX, C_None)),
  492. {A_SAL} (Ch: (C_RWop2, C_Rop1, C_RWFlags)),
  493. {A_SALC} (Ch: (C_WEAX, C_RFLAGS, C_None)), { new }
  494. {A_SAR} (Ch: (C_RWop2, C_Rop1, C_WFlags)),
  495. {A_SBB} (Ch: (C_RWop2, C_Rop1, C_RWFlags)),
  496. {A_SCASB} (Ch: (C_All, C_None, C_None)), { new }
  497. {A_SCASD} (Ch: (C_All, C_None, C_None)), { new }
  498. {A_SCASW} (Ch: (C_All, C_None, C_None)), { new }
  499. {A_SGDT} (Ch: (C_Wop1, C_None, C_None)),
  500. {A_SHL} (Ch: (C_RWop2, C_Rop1, C_WFlags)),
  501. {A_SHLD} (Ch: (C_RWOp3, C_RWFlags, C_Rop2)),
  502. {A_SHR} (Ch: (C_RWop2, C_Rop1, C_WFlags)),
  503. {A_SHRD} (Ch: (C_RWOp3, C_RWFlags, C_Rop2)),
  504. {A_SIDT} (Ch: (C_Wop1, C_None, C_None)),
  505. {A_SLDT} (Ch: (C_Wop1, C_None, C_None)),
  506. {A_SMI} (Ch: (C_All, C_None, C_None)), { new }
  507. {A_SMSW} (Ch: (C_Wop1, C_None, C_None)),
  508. {A_STC} (Ch: (C_WFlags, C_None, C_None)),
  509. {A_STD} (Ch: (C_SDirFlag, C_None, C_None)),
  510. {A_STI} (Ch: (C_WFlags, C_None, C_None)),
  511. {A_STOSB} (Ch: (C_REAX, C_WMemEDI, C_RWEDI)), { new }
  512. {A_STOSD} (Ch: (C_REAX, C_WMemEDI, C_RWEDI)), { new }
  513. {A_STOSW} (Ch: (C_REAX, C_WMemEDI, C_RWEDI)), { new }
  514. {A_STR} (Ch: (C_Wop1, C_None, C_None)),
  515. {A_SUB} (Ch: (C_RWop2, C_Rop1, C_WFlags)),
  516. {A_TEST} (Ch: (C_WFlags, C_Rop1, C_Rop2)),
  517. {A_UMOV} (Ch: (C_All, C_None, C_None)), { new }
  518. {A_VERR} (Ch: (C_WFlags, C_None, C_None)),
  519. {A_VERW} (Ch: (C_WFlags, C_None, C_None)),
  520. {A_WAIT} (Ch: (C_None, C_None, C_None)),
  521. {A_WBINVD} (Ch: (C_None, C_None, C_None)), { new }
  522. {A_WRMSR} (Ch: (C_All, C_None, C_None)), { new }
  523. {A_XADD} (Ch: (C_All, C_None, C_None)), { new }
  524. {A_XBTS} (Ch: (C_All, C_None, C_None)), { new }
  525. {A_XCHG} (Ch: (C_RWop1, C_RWop2, C_None)), {(might be) handled seperately}
  526. {A_XLAT} (Ch: (C_WEAX, C_REBX, C_None)),
  527. {A_XLATB} (Ch: (C_WEAX, C_REBX, C_None)),
  528. {A_XOR} (Ch: (C_RWop2, C_Rop1, C_WFlags)),
  529. {A_CMOV} (Ch: (C_ROp1, C_WOp2, C_RFLAGS)), { new }
  530. {A_J} (Ch: (C_None, C_None, C_None)), { new }
  531. {A_SET} (Ch: (C_WEAX, C_RFLAGS, C_None)) { new }
  532. );
  533. Var
  534. {How many instructions are between the current instruction and the last one
  535. that modified the register}
  536. NrOfInstrSinceLastMod: TInstrSinceLastMod;
  537. {************************ Create the Label table ************************}
  538. Function FindLoHiLabels(AsmL: PAasmOutput; Var LowLabel, HighLabel, LabelDif: Longint; BlockStart: Pai): Pai;
  539. {Walks through the paasmlist to find the lowest and highest label number}
  540. Var LabelFound: Boolean;
  541. P: Pai;
  542. Begin
  543. LabelFound := False;
  544. LowLabel := MaxLongint;
  545. HighLabel := 0;
  546. P := BlockStart;
  547. While Assigned(P) And
  548. ((P^.typ <> Ait_Marker) Or
  549. (Pai_Marker(P)^.Kind <> AsmBlockStart)) Do
  550. Begin
  551. If (Pai(p)^.typ = ait_label) Then
  552. If (Pai_Label(p)^.l^.is_used)
  553. Then
  554. Begin
  555. LabelFound := True;
  556. If (Pai_Label(p)^.l^.nb < LowLabel) Then
  557. LowLabel := Pai_Label(p)^.l^.nb;
  558. If (Pai_Label(p)^.l^.nb > HighLabel) Then
  559. HighLabel := Pai_Label(p)^.l^.nb;
  560. End
  561. { Else
  562. Begin
  563. hp1 := pai(p^.next);
  564. AsmL^.Remove(p);
  565. Dispose(p, Done);
  566. p := hp1;
  567. continue;
  568. End};
  569. GetNextInstruction(p, p);
  570. End;
  571. FindLoHiLabels := p;
  572. If LabelFound
  573. Then LabelDif := HighLabel+1-LowLabel
  574. Else LabelDif := 0;
  575. End;
  576. Function FindRegAlloc(Reg: TRegister; StartPai: Pai): Boolean;
  577. {Returns true if a ait_alloc object for Reg is found in the block of Pai's
  578. starting with StartPai and ending with the next "real" instruction}
  579. Begin
  580. FindRegAlloc:=False;
  581. Repeat
  582. While Assigned(StartPai) And
  583. ((StartPai^.typ in (SkipInstr - [ait_regAlloc])) Or
  584. ((StartPai^.typ = ait_label) and
  585. Not(Pai_Label(StartPai)^.l^.Is_Used))) Do
  586. StartPai := Pai(StartPai^.Next);
  587. If Assigned(StartPai) And
  588. (StartPai^.typ = ait_regAlloc) and (PairegAlloc(StartPai)^.allocation) Then
  589. Begin
  590. if PairegAlloc(StartPai)^.Reg = Reg then
  591. begin
  592. FindRegAlloc:=true;
  593. exit;
  594. end;
  595. StartPai := Pai(StartPai^.Next);
  596. End
  597. else
  598. exit;
  599. Until false;
  600. End;
  601. Procedure BuildLabelTableAndFixRegAlloc(AsmL: PAasmOutput; Var LabelTable: PLabelTable; LowLabel: Longint;
  602. Var LabelDif: Longint; BlockStart, BlockEnd: Pai);
  603. {Builds a table with the locations of the labels in the paasmoutput.
  604. Also fixes some RegDeallocs like "# %eax released; push (%eax)"}
  605. Var p, hp1, hp2: Pai;
  606. UsedRegs: TRegSet;
  607. Begin
  608. UsedRegs := [];
  609. If (LabelDif <> 0) Then
  610. Begin
  611. {$IfDef TP}
  612. If (MaxAvail >= LabelDif*SizeOf(Pai))
  613. Then
  614. Begin
  615. {$EndIf TP}
  616. GetMem(LabelTable, LabelDif*SizeOf(TLabelTableItem));
  617. FillChar(LabelTable^, LabelDif*SizeOf(TLabelTableItem), 0);
  618. p := BlockStart;
  619. While (P <> BlockEnd) Do
  620. Begin
  621. Case p^.typ Of
  622. ait_Label:
  623. If Pai_Label(p)^.l^.is_used Then
  624. LabelTable^[Pai_Label(p)^.l^.nb-LowLabel].PaiObj := p;
  625. ait_regAlloc:
  626. begin
  627. if PairegAlloc(p)^.Allocation then
  628. Begin
  629. If Not(PaiRegAlloc(p)^.Reg in UsedRegs) Then
  630. UsedRegs := UsedRegs + [PaiRegAlloc(p)^.Reg]
  631. Else
  632. Begin
  633. hp1 := p;
  634. hp2 := nil;
  635. While GetLastInstruction(hp1, hp1) And
  636. Not(RegInInstruction(PaiRegAlloc(p)^.Reg, hp1)) Do
  637. hp2 := hp1;
  638. If hp2 <> nil Then
  639. Begin
  640. hp1 := New(PaiRegAlloc, DeAlloc(PaiRegAlloc(p)^.Reg));
  641. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp1);
  642. End;
  643. End;
  644. End
  645. else
  646. Begin
  647. UsedRegs := UsedRegs - [PaiRegAlloc(p)^.Reg];
  648. hp1 := p;
  649. hp2 := nil;
  650. While Not(FindRegAlloc(PaiRegAlloc(p)^.Reg, Pai(hp1^.Next))) And
  651. GetNextInstruction(hp1, hp1) And
  652. RegInInstruction(PaiRegAlloc(p)^.Reg, hp1) Do
  653. hp2 := hp1;
  654. If hp2 <> nil Then
  655. Begin
  656. hp1 := Pai(p^.previous);
  657. AsmL^.Remove(p);
  658. InsertLLItem(AsmL, hp2, Pai(hp2^.Next), p);
  659. p := hp1;
  660. End;
  661. End;
  662. end;
  663. End;
  664. P := Pai(p^.Next);
  665. While Assigned(p) And
  666. (p^.typ in (SkipInstr - [ait_regalloc])) Do
  667. P := Pai(P^.Next);
  668. End;
  669. {$IfDef TP}
  670. End
  671. Else LabelDif := 0;
  672. {$EndIf TP}
  673. End;
  674. End;
  675. {************************ Search the Label table ************************}
  676. Function FindLabel(L: PLabel; Var hp: Pai): Boolean;
  677. {searches for the specified label starting from hp as long as the
  678. encountered instructions are labels, to be able to optimize constructs like
  679. jne l2 jmp l2
  680. jmp l3 and l1:
  681. l1: l2:
  682. l2:}
  683. Var TempP: Pai;
  684. Begin
  685. TempP := hp;
  686. While Assigned(TempP) and
  687. (TempP^.typ In SkipInstr + [ait_label]) Do
  688. If (TempP^.typ <> ait_Label) Or
  689. (pai_label(TempP)^.l <> L)
  690. Then GetNextInstruction(TempP, TempP)
  691. Else
  692. Begin
  693. hp := TempP;
  694. FindLabel := True;
  695. exit
  696. End;
  697. FindLabel := False;
  698. End;
  699. {************************ Some general functions ************************}
  700. Function Reg32(Reg: TRegister): TRegister;
  701. {Returns the 32 bit component of Reg if it exists, otherwise Reg is returned}
  702. Begin
  703. Reg32 := Reg;
  704. If (Reg >= R_AX)
  705. Then
  706. If (Reg <= R_DI)
  707. Then Reg32 := Reg16ToReg32(Reg)
  708. Else
  709. If (Reg <= R_BL)
  710. Then Reg32 := Reg8toReg32(Reg);
  711. End;
  712. { inserts new_one between prev and foll }
  713. Procedure InsertLLItem(AsmL: PAasmOutput; prev, foll, new_one: PLinkedList_Item);
  714. Begin
  715. If Assigned(prev) Then
  716. If Assigned(foll) Then
  717. Begin
  718. If Assigned(new_one) Then
  719. Begin
  720. new_one^.previous := prev;
  721. new_one^.next := foll;
  722. prev^.next := new_one;
  723. foll^.previous := new_one;
  724. End;
  725. End
  726. Else AsmL^.Concat(new_one)
  727. Else If Assigned(Foll) Then AsmL^.Insert(new_one)
  728. End;
  729. {********************* Compare parts of Pai objects *********************}
  730. Function RegsSameSize(Reg1, Reg2: TRegister): Boolean;
  731. {returns true if Reg1 and Reg2 are of the same size (so if they're both
  732. 8bit, 16bit or 32bit)}
  733. Begin
  734. If (Reg1 <= R_EDI)
  735. Then RegsSameSize := (Reg2 <= R_EDI)
  736. Else
  737. If (Reg1 <= R_DI)
  738. Then RegsSameSize := (Reg2 in [R_AX..R_DI])
  739. Else
  740. If (Reg1 <= R_BL)
  741. Then RegsSameSize := (Reg2 in [R_AL..R_BL])
  742. Else RegsSameSize := False
  743. End;
  744. Procedure AddReg2RegInfo(OldReg, NewReg: TRegister; Var RegInfo: TRegInfo);
  745. {updates the ???RegsEncountered and ???2???Reg fields of RegInfo. Assumes that
  746. OldReg and NewReg have the same size (has to be chcked in advance with
  747. RegsSameSize) and that neither equals R_NO}
  748. Begin
  749. With RegInfo Do
  750. Begin
  751. NewRegsEncountered := NewRegsEncountered + [NewReg];
  752. OldRegsEncountered := OldRegsEncountered + [OldReg];
  753. New2OldReg[NewReg] := OldReg;
  754. Case OldReg Of
  755. R_EAX..R_EDI:
  756. Begin
  757. NewRegsEncountered := NewRegsEncountered + [Reg32toReg16(NewReg)];
  758. OldRegsEncountered := OldRegsEncountered + [Reg32toReg16(OldReg)];
  759. New2OldReg[Reg32toReg16(NewReg)] := Reg32toReg16(OldReg);
  760. If (NewReg in [R_EAX..R_EBX]) And
  761. (OldReg in [R_EAX..R_EBX]) Then
  762. Begin
  763. NewRegsEncountered := NewRegsEncountered + [Reg32toReg8(NewReg)];
  764. OldRegsEncountered := OldRegsEncountered + [Reg32toReg8(OldReg)];
  765. New2OldReg[Reg32toReg8(NewReg)] := Reg32toReg8(OldReg);
  766. End;
  767. End;
  768. R_AX..R_DI:
  769. Begin
  770. NewRegsEncountered := NewRegsEncountered + [Reg16toReg32(NewReg)];
  771. OldRegsEncountered := OldRegsEncountered + [Reg16toReg32(OldReg)];
  772. New2OldReg[Reg16toReg32(NewReg)] := Reg16toReg32(OldReg);
  773. If (NewReg in [R_AX..R_BX]) And
  774. (OldReg in [R_AX..R_BX]) Then
  775. Begin
  776. NewRegsEncountered := NewRegsEncountered + [Reg16toReg8(NewReg)];
  777. OldRegsEncountered := OldRegsEncountered + [Reg16toReg8(OldReg)];
  778. New2OldReg[Reg16toReg8(NewReg)] := Reg16toReg8(OldReg);
  779. End;
  780. End;
  781. R_AL..R_BL:
  782. Begin
  783. NewRegsEncountered := NewRegsEncountered + [Reg8toReg32(NewReg)]
  784. + [Reg8toReg16(NewReg)];
  785. OldRegsEncountered := OldRegsEncountered + [Reg8toReg32(OldReg)]
  786. + [Reg8toReg16(OldReg)];
  787. New2OldReg[Reg8toReg32(NewReg)] := Reg8toReg32(OldReg);
  788. End;
  789. End;
  790. End;
  791. End;
  792. Procedure AddOpRegInfo(const o:Toper; Var RegInfo: TRegInfo);
  793. Begin
  794. Case o.typ Of
  795. Top_Reg:
  796. If (o.reg <> R_NO) Then
  797. AddReg2RegInfo(o.reg, o.reg, RegInfo);
  798. Top_Ref:
  799. Begin
  800. If o.ref^.base <> R_NO Then
  801. AddReg2RegInfo(o.ref^.base, o.ref^.base, RegInfo);
  802. If o.ref^.index <> R_NO Then
  803. AddReg2RegInfo(o.ref^.index, o.ref^.index, RegInfo);
  804. End;
  805. End;
  806. End;
  807. Function RegsEquivalent(OldReg, NewReg: TRegister; Var RegInfo: TRegInfo; OPAct: TOpAction): Boolean;
  808. Begin
  809. If Not((OldReg = R_NO) Or (NewReg = R_NO)) Then
  810. If RegsSameSize(OldReg, NewReg) Then
  811. With RegInfo Do
  812. {here we always check for the 32 bit component, because it is possible that
  813. the 8 bit component has not been set, event though NewReg already has been
  814. processed. This happens if it has been compared with a register that doesn't
  815. have an 8 bit component (such as EDI). In that case the 8 bit component is
  816. still set to R_NO and the comparison in the Else-part will fail}
  817. If (Reg32(OldReg) in OldRegsEncountered) Then
  818. If (Reg32(NewReg) in NewRegsEncountered) Then
  819. RegsEquivalent := (OldReg = New2OldReg[NewReg])
  820. { If we haven't encountered the new register yet, but we have encountered the
  821. old one already, the new one can only be correct if it's being written to
  822. (and consequently the old one is also being written to), otherwise
  823. movl -8(%ebp), %eax and movl -8(%ebp), %eax
  824. movl (%eax), %eax movl (%edx), %edx
  825. are considered equivalent}
  826. Else
  827. If (OpAct = OpAct_Write) Then
  828. Begin
  829. AddReg2RegInfo(OldReg, NewReg, RegInfo);
  830. RegsEquivalent := True
  831. End
  832. Else Regsequivalent := False
  833. Else
  834. If Not(Reg32(NewReg) in NewRegsEncountered) Then
  835. Begin
  836. AddReg2RegInfo(OldReg, NewReg, RegInfo);
  837. RegsEquivalent := True
  838. End
  839. Else RegsEquivalent := False
  840. Else RegsEquivalent := False
  841. Else RegsEquivalent := OldReg = NewReg
  842. End;
  843. Function RefsEquivalent(Const R1, R2: TReference; var RegInfo: TRegInfo; OpAct: TOpAction): Boolean;
  844. Begin
  845. If R1.is_immediate Then
  846. RefsEquivalent := R2.is_immediate and (R1.Offset = R2.Offset)
  847. Else
  848. RefsEquivalent := (R1.Offset+R1.OffsetFixup = R2.Offset+R2.OffsetFixup) And
  849. RegsEquivalent(R1.Base, R2.Base, RegInfo, OpAct) And
  850. RegsEquivalent(R1.Index, R2.Index, RegInfo, OpAct) And
  851. (R1.Segment = R2.Segment) And (R1.ScaleFactor = R2.ScaleFactor) And
  852. (R1.Symbol = R2.Symbol);
  853. End;
  854. Function RefsEqual(Const R1, R2: TReference): Boolean;
  855. Begin
  856. If R1.is_immediate Then
  857. RefsEqual := R2.is_immediate and (R1.Offset = R2.Offset)
  858. Else
  859. RefsEqual := (R1.Offset+R1.OffsetFixup = R2.Offset+R2.OffsetFixup) And
  860. (R1.Segment = R2.Segment) And (R1.Base = R2.Base) And
  861. (R1.Index = R2.Index) And (R1.ScaleFactor = R2.ScaleFactor) And
  862. (R1.Symbol=R2.Symbol);
  863. End;
  864. Function IsGP32Reg(Reg: TRegister): Boolean;
  865. {Checks if the register is a 32 bit general purpose register}
  866. Begin
  867. If (Reg >= R_EAX) and (Reg <= R_EBX)
  868. Then IsGP32Reg := True
  869. Else IsGP32reg := False
  870. End;
  871. Function RegInRef(Reg: TRegister; Const Ref: TReference): Boolean;
  872. Begin {checks whether Ref contains a reference to Reg}
  873. Reg := Reg32(Reg);
  874. RegInRef := (Ref.Base = Reg) Or (Ref.Index = Reg)
  875. End;
  876. Function RegInInstruction(Reg: TRegister; p1: Pai): Boolean;
  877. {checks if Reg is used by the instruction p1}
  878. Var TmpResult: Boolean;
  879. Begin
  880. TmpResult := False;
  881. If (Pai(p1)^.typ = ait_instruction) Then
  882. Begin
  883. Case Pai386(p1)^.oper[0].typ Of
  884. Top_Reg: TmpResult := Reg = Pai386(p1)^.oper[0].reg;
  885. Top_Ref: TmpResult := RegInRef(Reg, Pai386(p1)^.oper[0].ref^);
  886. End;
  887. If Not(TmpResult) Then
  888. Case Pai386(p1)^.oper[1].typ Of
  889. Top_Reg: TmpResult := (Reg = Pai386(p1)^.oper[1].reg);
  890. Top_Ref: TmpResult := RegInRef(Reg, Pai386(p1)^.oper[1].ref^)
  891. End;
  892. If Not(TmpResult) Then
  893. Case Pai386(p1)^.oper[2].typ Of
  894. Top_Reg: TmpResult := (Reg = Pai386(p1)^.oper[2].reg);
  895. Top_Ref: TmpResult := RegInRef(Reg, Pai386(p1)^.oper[2].ref^)
  896. End
  897. End;
  898. RegInInstruction := TmpResult
  899. End;
  900. {Function RegInOp(Reg: TRegister; const o:toper): Boolean;
  901. Begin
  902. RegInOp := False;
  903. Case opt Of
  904. top_reg: RegInOp := Reg = o.reg;
  905. top_ref: RegInOp := (Reg = o.ref^.Base) Or
  906. (Reg = o.ref^.Index);
  907. End;
  908. End;}
  909. Function RegModifiedByInstruction(Reg: TRegister; p1: Pai): Boolean;
  910. {returns true if Reg is modified by the instruction p1. P1 is assumed to be
  911. of the type ait_instruction}
  912. Var hp: Pai;
  913. Begin
  914. If GetLastInstruction(p1, hp)
  915. Then
  916. RegModifiedByInstruction :=
  917. PPAiProp(p1^.OptInfo)^.Regs[Reg].WState <>
  918. PPAiProp(hp^.OptInfo)^.Regs[Reg].WState
  919. Else RegModifiedByInstruction := True;
  920. End;
  921. {********************* GetNext and GetLastInstruction *********************}
  922. Function GetNextInstruction(Current: Pai; Var Next: Pai): Boolean;
  923. {skips ait_regalloc, ait_regdealloc and ait_stab* objects and puts the
  924. next pai object in Next. Returns false if there isn't any}
  925. Begin
  926. Repeat
  927. Current := Pai(Current^.Next);
  928. While Assigned(Current) And
  929. ((Current^.typ In SkipInstr) or
  930. ((Current^.typ = ait_label) And
  931. Not(Pai_Label(Current)^.l^.is_used))) Do
  932. Current := Pai(Current^.Next);
  933. If Assigned(Current) And
  934. (Current^.typ = ait_Marker) And
  935. (Pai_Marker(Current)^.Kind = NoPropInfoStart) Then
  936. Begin
  937. While Assigned(Current) And
  938. ((Current^.typ <> ait_Marker) Or
  939. (Pai_Marker(Current)^.Kind <> NoPropInfoEnd)) Do
  940. Current := Pai(Current^.Next);
  941. End;
  942. Until Not(Assigned(Current)) Or
  943. (Current^.typ <> ait_Marker) Or
  944. (Pai_Marker(Current)^.Kind <> NoPropInfoEnd);
  945. Next := Current;
  946. If Assigned(Current) And
  947. Not((Current^.typ In SkipInstr) or
  948. ((Current^.typ = ait_label) And
  949. Not(Pai_Label(Current)^.l^.is_used)))
  950. Then GetNextInstruction := True
  951. Else
  952. Begin
  953. Next := Nil;
  954. GetNextInstruction := False;
  955. End;
  956. End;
  957. Function GetLastInstruction(Current: Pai; Var Last: Pai): Boolean;
  958. {skips the ait-types in SkipInstr puts the previous pai object in
  959. Last. Returns false if there isn't any}
  960. Begin
  961. Repeat
  962. Current := Pai(Current^.previous);
  963. While Assigned(Current) And
  964. (((Current^.typ = ait_Marker) And
  965. Not(Pai_Marker(Current)^.Kind in [AsmBlockEnd,NoPropInfoEnd])) or
  966. (Current^.typ In SkipInstr) or
  967. ((Current^.typ = ait_label) And
  968. Not(Pai_Label(Current)^.l^.is_used))) Do
  969. Current := Pai(Current^.previous);
  970. If Assigned(Current) And
  971. (Current^.typ = ait_Marker) And
  972. (Pai_Marker(Current)^.Kind = NoPropInfoEnd) Then
  973. Begin
  974. While Assigned(Current) And
  975. ((Current^.typ <> ait_Marker) Or
  976. (Pai_Marker(Current)^.Kind <> NoPropInfoStart)) Do
  977. Current := Pai(Current^.previous);
  978. End;
  979. Until Not(Assigned(Current)) Or
  980. (Current^.typ <> ait_Marker) Or
  981. (Pai_Marker(Current)^.Kind <> NoPropInfoStart);
  982. If Not(Assigned(Current)) or
  983. (Current^.typ In SkipInstr) or
  984. ((Current^.typ = ait_label) And
  985. Not(Pai_Label(Current)^.l^.is_used)) or
  986. ((Current^.typ = ait_Marker) And
  987. (Pai_Marker(Current)^.Kind = AsmBlockEnd))
  988. Then
  989. Begin
  990. Last := Nil;
  991. GetLastInstruction := False
  992. End
  993. Else
  994. Begin
  995. Last := Current;
  996. GetLastInstruction := True;
  997. End;
  998. End;
  999. Procedure SkipHead(var P: Pai);
  1000. Var OldP: Pai;
  1001. Begin
  1002. Repeat
  1003. OldP := P;
  1004. If (P^.typ in SkipInstr) Or
  1005. ((P^.typ = ait_marker) And
  1006. (Pai_Marker(P)^.Kind = AsmBlockEnd)) Then
  1007. GetNextInstruction(P, P)
  1008. Else If ((P^.Typ = Ait_Marker) And
  1009. (Pai_Marker(P)^.Kind = NoPropInfoStart)) Then
  1010. {a marker of the NoPropInfoStart can't be the first instruction of a
  1011. paasmoutput list}
  1012. GetNextInstruction(Pai(P^.Previous),P);
  1013. If (P^.Typ = Ait_Marker) And
  1014. (Pai_Marker(P)^.Kind = AsmBlockStart) Then
  1015. Begin
  1016. P := Pai(P^.Next);
  1017. While (P^.typ <> Ait_Marker) Or
  1018. (Pai_Marker(P)^.Kind <> AsmBlockEnd) Do
  1019. P := Pai(P^.Next)
  1020. End;
  1021. Until P = OldP
  1022. End;
  1023. {******************* The Data Flow Analyzer functions ********************}
  1024. Procedure UpdateUsedRegs(Var UsedRegs: TRegSet; p: Pai);
  1025. {updates UsedRegs with the RegAlloc Information coming after P}
  1026. Begin
  1027. Repeat
  1028. While Assigned(p) And
  1029. ((p^.typ in (SkipInstr - [ait_RegAlloc])) or
  1030. ((p^.typ = ait_label) And
  1031. Not(Pai_Label(p)^.l^.is_used))) Do
  1032. p := Pai(p^.next);
  1033. While Assigned(p) And
  1034. (p^.typ=ait_RegAlloc) Do
  1035. Begin
  1036. if pairegalloc(p)^.allocation then
  1037. UsedRegs := UsedRegs + [PaiRegAlloc(p)^.Reg]
  1038. else
  1039. UsedRegs := UsedRegs - [PaiRegAlloc(p)^.Reg];
  1040. p := pai(p^.next);
  1041. End;
  1042. Until Not(Assigned(p)) Or
  1043. (Not(p^.typ in SkipInstr) And
  1044. Not((p^.typ = ait_label) And
  1045. Not(Pai_Label(p)^.l^.is_used)));
  1046. End;
  1047. (*Function FindZeroreg(p: Pai; Var Result: TRegister): Boolean;
  1048. {Finds a register which contains the constant zero}
  1049. Var Counter: TRegister;
  1050. Begin
  1051. Counter := R_EAX;
  1052. FindZeroReg := True;
  1053. While (Counter <= R_EDI) And
  1054. ((PPaiProp(p^.OptInfo)^.Regs[Counter].Typ <> Con_Const) or
  1055. (PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod <> Pointer(0))) Do
  1056. Inc(Byte(Counter));
  1057. If (PPaiProp(p^.OptInfo)^.Regs[Counter].Typ = Con_Const) And
  1058. (PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod = Pointer(0))
  1059. Then Result := Counter
  1060. Else FindZeroReg := False;
  1061. End;*)
  1062. Function TCh2Reg(Ch: TChange): TRegister;
  1063. {converts a TChange variable to a TRegister}
  1064. Begin
  1065. If (Ch <= C_REDI) Then
  1066. TCh2Reg := TRegister(Byte(Ch))
  1067. Else
  1068. If (Ch <= C_WEDI) Then
  1069. TCh2Reg := TRegister(Byte(Ch) - Byte(C_REDI))
  1070. Else
  1071. If (Ch <= C_RWEDI) Then
  1072. TCh2Reg := TRegister(Byte(Ch) - Byte(C_WEDI))
  1073. Else InternalError($db)
  1074. End;
  1075. Procedure IncState(Var S: Byte);
  1076. {Increases S by 1, wraps around at $ffff to 0 (so we won't get overflow
  1077. errors}
  1078. Begin
  1079. If (s <> $ff)
  1080. Then Inc(s)
  1081. Else s := 0
  1082. End;
  1083. Function RegInSequence(Reg: TRegister; Const Content: TContent): Boolean;
  1084. {checks the whole sequence of Content (so StartMod and and the next NrOfMods
  1085. Pai objects) to see whether Reg is used somewhere, without it being loaded
  1086. with something else first}
  1087. Var p: Pai;
  1088. Counter: Byte;
  1089. TmpResult: Boolean;
  1090. RegsChecked: TRegSet;
  1091. Begin
  1092. RegsChecked := [];
  1093. p := Content.StartMod;
  1094. TmpResult := False;
  1095. Counter := 1;
  1096. While Not(TmpResult) And
  1097. (Counter <= Content.NrOfMods) Do
  1098. Begin
  1099. If (p^.typ = ait_instruction) and
  1100. ((Pai386(p)^.opcode = A_MOV) or
  1101. (Pai386(p)^.opcode = A_MOVZX) or
  1102. (Pai386(p)^.opcode = A_MOVSX))
  1103. Then
  1104. If (Pai386(p)^.oper[0].typ = top_ref)
  1105. Then
  1106. With Pai386(p)^.oper[0].ref^ Do
  1107. If (Base = ProcInfo.FramePointer) And
  1108. (Index = R_NO)
  1109. Then RegsChecked := RegsChecked + [Reg32(Pai386(p)^.oper[1].reg)]
  1110. Else
  1111. Begin
  1112. If (Base = Reg) And
  1113. Not(Base In RegsChecked)
  1114. Then TmpResult := True;
  1115. If Not(TmpResult) And
  1116. (Index = Reg) And
  1117. Not(Index In RegsChecked)
  1118. Then TmpResult := True;
  1119. End;
  1120. Inc(Counter);
  1121. GetNextInstruction(p,p)
  1122. End;
  1123. RegInSequence := TmpResult
  1124. End;
  1125. Procedure DestroyReg(p1: PPaiProp; Reg: TRegister);
  1126. {Destroys the contents of the register Reg in the PPaiProp p1, as well as the
  1127. contents of registers are loaded with a memory location based on Reg}
  1128. Var TmpWState, TmpRState: Byte;
  1129. Counter: TRegister;
  1130. Begin
  1131. Reg := Reg32(Reg);
  1132. NrOfInstrSinceLastMod[Reg] := 0;
  1133. If (Reg >= R_EAX) And (Reg <= R_EDI)
  1134. Then
  1135. Begin
  1136. With p1^.Regs[Reg] Do
  1137. Begin
  1138. IncState(WState);
  1139. TmpWState := WState;
  1140. TmpRState := RState;
  1141. FillChar(p1^.Regs[Reg], SizeOf(TContent), 0);
  1142. WState := TmpWState;
  1143. RState := TmpRState;
  1144. End;
  1145. For Counter := R_EAX to R_EDI Do
  1146. With p1^.Regs[Counter] Do
  1147. If (Typ = Con_Ref) And
  1148. RegInSequence(Reg, p1^.Regs[Counter])
  1149. Then
  1150. Begin
  1151. IncState(WState);
  1152. TmpWState := WState;
  1153. TmpRState := RState;
  1154. FillChar(p1^.Regs[Counter], SizeOf(TContent), 0);
  1155. WState := TmpWState;
  1156. RState := TmpRState;
  1157. End;
  1158. End;
  1159. End;
  1160. {Procedure AddRegsToSet(p: Pai; Var RegSet: TRegSet);
  1161. Begin
  1162. If (p^.typ = ait_instruction) Then
  1163. Begin
  1164. Case Pai386(p)^.oper[0].typ Of
  1165. top_reg:
  1166. If Not(Pai386(p)^.oper[0].reg in [R_NO,R_ESP,ProcInfo.FramePointer]) Then
  1167. RegSet := RegSet + [Pai386(p)^.oper[0].reg];
  1168. top_ref:
  1169. With TReference(Pai386(p)^.oper[0]^) Do
  1170. Begin
  1171. If Not(Base in [ProcInfo.FramePointer,R_NO,R_ESP])
  1172. Then RegSet := RegSet + [Base];
  1173. If Not(Index in [ProcInfo.FramePointer,R_NO,R_ESP])
  1174. Then RegSet := RegSet + [Index];
  1175. End;
  1176. End;
  1177. Case Pai386(p)^.oper[1].typ Of
  1178. top_reg:
  1179. If Not(Pai386(p)^.oper[1].reg in [R_NO,R_ESP,ProcInfo.FramePointer]) Then
  1180. If RegSet := RegSet + [TRegister(TwoWords(Pai386(p)^.oper[1]).Word1];
  1181. top_ref:
  1182. With TReference(Pai386(p)^.oper[1]^) Do
  1183. Begin
  1184. If Not(Base in [ProcInfo.FramePointer,R_NO,R_ESP])
  1185. Then RegSet := RegSet + [Base];
  1186. If Not(Index in [ProcInfo.FramePointer,R_NO,R_ESP])
  1187. Then RegSet := RegSet + [Index];
  1188. End;
  1189. End;
  1190. End;
  1191. End;}
  1192. Function OpsEquivalent(const o1, o2: toper; Var RegInfo: TRegInfo; OpAct: TopAction): Boolean;
  1193. Begin {checks whether the two ops are equivalent}
  1194. OpsEquivalent := False;
  1195. if o1.typ=o2.typ then
  1196. Case o1.typ Of
  1197. Top_Reg:
  1198. OpsEquivalent :=RegsEquivalent(o1.reg,o2.reg, RegInfo, OpAct);
  1199. Top_Ref:
  1200. OpsEquivalent := RefsEquivalent(o1.ref^, o2.ref^, RegInfo, OpAct);
  1201. Top_None:
  1202. OpsEquivalent := True
  1203. End;
  1204. End;
  1205. Function OpsEqual(const o1,o2:toper): Boolean;
  1206. Begin {checks whether the two ops are equal}
  1207. OpsEqual := False;
  1208. if o1.typ=o2.typ then
  1209. Case o1.typ Of
  1210. Top_Reg :
  1211. OpsEqual:=o1.reg=o2.reg;
  1212. Top_Ref :
  1213. OpsEqual := RefsEqual(o1.ref^, o2.ref^);
  1214. Top_Const :
  1215. OpsEqual:=o1.val=o2.val;
  1216. Top_Symbol :
  1217. OpsEqual:=(o1.sym=o2.sym) and (o1.symofs=o2.symofs);
  1218. Top_None :
  1219. OpsEqual := True
  1220. End;
  1221. End;
  1222. Function InstructionsEquivalent(p1, p2: Pai; Var RegInfo: TRegInfo): Boolean;
  1223. Begin {checks whether two Pai386 instructions are equal}
  1224. If Assigned(p1) And Assigned(p2) And
  1225. (Pai(p1)^.typ = ait_instruction) And
  1226. (Pai(p1)^.typ = ait_instruction) And
  1227. (Pai386(p1)^.opcode = Pai386(p2)^.opcode) And
  1228. (Pai386(p1)^.oper[0].typ = Pai386(p2)^.oper[0].typ) And
  1229. (Pai386(p1)^.oper[1].typ = Pai386(p2)^.oper[1].typ) And
  1230. (Pai386(p1)^.oper[2].typ = Pai386(p2)^.oper[2].typ)
  1231. Then
  1232. {both instructions have the same structure:
  1233. "<operator> <operand of type1>, <operand of type 2>"}
  1234. If ((Pai386(p1)^.opcode = A_MOV) or
  1235. (Pai386(p1)^.opcode = A_MOVZX) or
  1236. (Pai386(p1)^.opcode = A_MOVSX)) And
  1237. (Pai386(p1)^.oper[0].typ = top_ref) {then .oper[1]t = top_reg} Then
  1238. If Not(RegInRef(Pai386(p1)^.oper[1].reg, Pai386(p1)^.oper[0].ref^)) Then
  1239. {the "old" instruction is a load of a register with a new value, not with
  1240. a value based on the contents of this register (so no "mov (reg), reg")}
  1241. If Not(RegInRef(Pai386(p2)^.oper[1].reg, Pai386(p2)^.oper[0].ref^)) And
  1242. RefsEqual(Pai386(p1)^.oper[0].ref^, Pai386(p2)^.oper[0].ref^)
  1243. Then
  1244. {the "new" instruction is also a load of a register with a new value, and
  1245. this value is fetched from the same memory location}
  1246. Begin
  1247. With Pai386(p2)^.oper[0].ref^ Do
  1248. Begin
  1249. If Not(Base in [ProcInfo.FramePointer, R_NO, R_ESP])
  1250. {it won't do any harm if the register is already in RegsLoadedForRef}
  1251. Then RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef + [Base];
  1252. If Not(Index in [ProcInfo.FramePointer, R_NO, R_ESP])
  1253. Then RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef + [Index];
  1254. End;
  1255. {add the registers from the reference (.oper[0]) to the RegInfo, all registers
  1256. from the reference are the same in the old and in the new instruction
  1257. sequence}
  1258. AddOpRegInfo(Pai386(p1)^.oper[0], RegInfo);
  1259. {the registers from .oper[1] have to be equivalent, but not necessarily equal}
  1260. InstructionsEquivalent :=
  1261. RegsEquivalent(Pai386(p1)^.oper[1].reg, Pai386(p2)^.oper[1].reg, RegInfo, OpAct_Write);
  1262. End
  1263. {the registers are loaded with values from different memory locations. If
  1264. this was allowed, the instructions "mov -4(esi),eax" and "mov -4(ebp),eax"
  1265. would be considered equivalent}
  1266. Else InstructionsEquivalent := False
  1267. Else
  1268. {load register with a value based on the current value of this register}
  1269. Begin
  1270. With Pai386(p2)^.oper[0].ref^ Do
  1271. Begin
  1272. If Not(Base in [ProcInfo.FramePointer,
  1273. Reg32(Pai386(p2)^.oper[1].reg),R_NO,R_ESP])
  1274. {it won't do any harm if the register is already in RegsLoadedForRef}
  1275. Then
  1276. Begin
  1277. RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef + [Base];
  1278. {$ifdef csdebug}
  1279. Writeln(att_reg2str[base], ' added');
  1280. {$endif csdebug}
  1281. end;
  1282. If Not(Index in [ProcInfo.FramePointer,
  1283. Reg32(Pai386(p2)^.oper[1].reg),R_NO,R_ESP])
  1284. Then
  1285. Begin
  1286. RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef + [Index];
  1287. {$ifdef csdebug}
  1288. Writeln(att_reg2str[index], ' added');
  1289. {$endif csdebug}
  1290. end;
  1291. End;
  1292. If Not(Reg32(Pai386(p2)^.oper[1].reg) In [ProcInfo.FramePointer,R_NO,R_ESP])
  1293. Then
  1294. Begin
  1295. RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef -
  1296. [Reg32(Pai386(p2)^.oper[1].reg)];
  1297. {$ifdef csdebug}
  1298. Writeln(att_reg2str[Reg32(Pai386(p2)^.oper[1].reg)], ' removed');
  1299. {$endif csdebug}
  1300. end;
  1301. InstructionsEquivalent :=
  1302. OpsEquivalent(Pai386(p1)^.oper[0], Pai386(p2)^.oper[0], RegInfo, OpAct_Read) And
  1303. OpsEquivalent(Pai386(p1)^.oper[1], Pai386(p2)^.oper[1], RegInfo, OpAct_Write)
  1304. End
  1305. Else
  1306. {an instruction <> mov, movzx, movsx}
  1307. InstructionsEquivalent :=
  1308. OpsEquivalent(Pai386(p1)^.oper[0], Pai386(p2)^.oper[0], RegInfo, OpAct_Unknown) And
  1309. OpsEquivalent(Pai386(p1)^.oper[1], Pai386(p2)^.oper[1], RegInfo, OpAct_Unknown)
  1310. {the instructions haven't even got the same structure, so they're certainly
  1311. not equivalent}
  1312. Else
  1313. InstructionsEquivalent := False;
  1314. End;
  1315. (*
  1316. Function InstructionsEqual(p1, p2: Pai): Boolean;
  1317. Begin {checks whether two Pai386 instructions are equal}
  1318. InstructionsEqual :=
  1319. Assigned(p1) And Assigned(p2) And
  1320. ((Pai(p1)^.typ = ait_instruction) And
  1321. (Pai(p1)^.typ = ait_instruction) And
  1322. (Pai386(p1)^.opcode = Pai386(p2)^.opcode) And
  1323. (Pai386(p1)^.oper[0].typ = Pai386(p2)^.oper[0].typ) And
  1324. (Pai386(p1)^.oper[1].typ = Pai386(p2)^.oper[1].typ) And
  1325. OpsEqual(Pai386(p1)^.oper[0].typ, Pai386(p1)^.oper[0], Pai386(p2)^.oper[0]) And
  1326. OpsEqual(Pai386(p1)^.oper[1].typ, Pai386(p1)^.oper[1], Pai386(p2)^.oper[1]))
  1327. End;
  1328. *)
  1329. Function RefInInstruction(Const Ref: TReference; p: Pai): Boolean;
  1330. {checks whehter Ref is used in P}
  1331. Var TmpResult: Boolean;
  1332. Begin
  1333. TmpResult := False;
  1334. If (p^.typ = ait_instruction) Then
  1335. Begin
  1336. If (Pai386(p)^.oper[0].typ = Top_Ref) Then
  1337. TmpResult := RefsEqual(Ref, Pai386(p)^.oper[0].ref^);
  1338. If Not(TmpResult) And (Pai386(p)^.oper[1].typ = Top_Ref) Then
  1339. TmpResult := RefsEqual(Ref, Pai386(p)^.oper[1].ref^);
  1340. End;
  1341. RefInInstruction := TmpResult;
  1342. End;
  1343. Function RefInSequence(Const Ref: TReference; Content: TContent): Boolean;
  1344. {checks the whole sequence of Content (so StartMod and and the next NrOfMods
  1345. Pai objects) to see whether Ref is used somewhere}
  1346. Var p: Pai;
  1347. Counter: Byte;
  1348. TmpResult: Boolean;
  1349. Begin
  1350. p := Content.StartMod;
  1351. TmpResult := False;
  1352. Counter := 1;
  1353. While Not(TmpResult) And
  1354. (Counter <= Content.NrOfMods) Do
  1355. Begin
  1356. If (p^.typ = ait_instruction) And
  1357. RefInInstruction(Ref, p)
  1358. Then TmpResult := True;
  1359. Inc(Counter);
  1360. GetNextInstruction(p,p)
  1361. End;
  1362. RefInSequence := TmpResult
  1363. End;
  1364. Procedure DestroyRefs(p: pai; Const Ref: TReference; WhichReg: TRegister);
  1365. {destroys all registers which possibly contain a reference to Ref, WhichReg
  1366. is the register whose contents are being written to memory (if this proc
  1367. is called because of a "mov?? %reg, (mem)" instruction)}
  1368. Var Counter: TRegister;
  1369. Begin
  1370. WhichReg := Reg32(WhichReg);
  1371. If ((Ref.base = ProcInfo.FramePointer) And
  1372. (Ref.Index = R_NO)) Or
  1373. Assigned(Ref.Symbol)
  1374. Then
  1375. {write something to a parameter, a local or global variable, so
  1376. * with uncertzain optimizations on:
  1377. - destroy the contents of registers whose contents have somewhere a
  1378. "mov?? (Ref), %reg". WhichReg (this is the register whose contents
  1379. are being written to memory) is not destroyed if it's StartMod is
  1380. of that form and NrOfMods = 1 (so if it holds ref, but is not a
  1381. pointer based on Ref)
  1382. * with uncertain optimizations off:
  1383. - also destroy registers that contain any pointer}
  1384. For Counter := R_EAX to R_EDI Do
  1385. With PPaiProp(p^.OptInfo)^.Regs[Counter] Do
  1386. Begin
  1387. If (typ = Con_Ref) And
  1388. (Not(cs_UncertainOpts in aktglobalswitches) And
  1389. (NrOfMods <> 1)
  1390. ) Or
  1391. (RefInSequence(Ref,PPaiProp(p^.OptInfo)^.Regs[Counter]) And
  1392. ((Counter <> WhichReg) Or
  1393. ((NrOfMods = 1) And
  1394. {StarMod is always of the type ait_instruction}
  1395. (Pai386(StartMod)^.oper[0].typ = top_ref) And
  1396. RefsEqual(Pai386(StartMod)^.oper[0].ref^, Ref)
  1397. )
  1398. )
  1399. )
  1400. Then DestroyReg(PPaiProp(p^.OptInfo), Counter)
  1401. End
  1402. Else
  1403. {write something to a pointer location, so
  1404. * with uncertain optimzations on:
  1405. - do not destroy registers which contain a local/global variable or a
  1406. parameter, except if DestroyRefs is called because of a "movsl"
  1407. * with uncertain optimzations off:
  1408. - destroy every register which contains a memory location
  1409. }
  1410. For Counter := R_EAX to R_EDI Do
  1411. With PPaiProp(p^.OptInfo)^.Regs[Counter] Do
  1412. If (typ = Con_Ref) And
  1413. (Not(cs_UncertainOpts in aktglobalswitches) Or
  1414. {for movsl}
  1415. (Ref.Base = R_EDI) Or
  1416. {don't destroy if reg contains a parameter, local or global variable}
  1417. Not((NrOfMods = 1) And
  1418. (Pai386(StartMod)^.oper[0].typ = top_ref) And
  1419. ((Pai386(StartMod)^.oper[0].ref^.base = ProcInfo.FramePointer) Or
  1420. Assigned(Pai386(StartMod)^.oper[0].ref^.Symbol)
  1421. )
  1422. )
  1423. )
  1424. Then DestroyReg(PPaiProp(p^.OptInfo), Counter)
  1425. End;
  1426. Procedure DestroyAllRegs(p: PPaiProp);
  1427. Var Counter: TRegister;
  1428. Begin {initializes/desrtoys all registers}
  1429. For Counter := R_EAX To R_EDI Do
  1430. DestroyReg(p, Counter);
  1431. p^.DirFlag := F_Unknown;
  1432. End;
  1433. Procedure DestroyOp(PaiObj: Pai; const o:Toper);
  1434. Begin
  1435. Case o.typ Of
  1436. top_reg: DestroyReg(PPaiProp(PaiObj^.OptInfo), o.reg);
  1437. top_ref: DestroyRefs(PaiObj, o.ref^, R_NO);
  1438. top_symbol:;
  1439. End;
  1440. End;
  1441. Procedure ReadReg(p: PPaiProp; Reg: TRegister);
  1442. Begin
  1443. Reg := Reg32(Reg);
  1444. If Reg in [R_EAX..R_EDI] Then
  1445. IncState(p^.Regs[Reg32(Reg)].RState)
  1446. End;
  1447. Procedure ReadRef(p: PPaiProp; Ref: PReference);
  1448. Begin
  1449. If Ref^.Base <> R_NO Then
  1450. ReadReg(p, Ref^.Base);
  1451. If Ref^.Index <> R_NO Then
  1452. ReadReg(p, Ref^.Index);
  1453. End;
  1454. Procedure ReadOp(P: PPaiProp;const o:toper);
  1455. Begin
  1456. Case o.typ Of
  1457. top_reg: ReadReg(P, o.reg);
  1458. top_ref: ReadRef(P, o.ref);
  1459. top_symbol : ;
  1460. End;
  1461. End;
  1462. Function DFAPass1(AsmL: PAasmOutput; BlockStart: Pai): Pai;
  1463. {gathers the RegAlloc data... still need to think about where to store it to
  1464. avoid global vars}
  1465. Var BlockEnd: Pai;
  1466. Begin
  1467. BlockEnd := FindLoHiLabels(AsmL, LoLab, HiLab, LabDif, BlockStart);
  1468. BuildLabelTableAndFixRegAlloc(AsmL, LTable, LoLab, LabDif, BlockStart, BlockEnd);
  1469. DFAPass1 := BlockEnd;
  1470. End;
  1471. Procedure DoDFAPass2(
  1472. {$Ifdef StateDebug}
  1473. AsmL: PAasmOutput;
  1474. {$endif statedebug}
  1475. BlockStart, BlockEnd: Pai);
  1476. {Analyzes the Data Flow of an assembler list. Starts creating the reg
  1477. contents for the instructions starting with p. Returns the last pai which has
  1478. been processed}
  1479. Var
  1480. CurProp: PPaiProp;
  1481. {$ifdef AnalyzeLoops}
  1482. TmpState: Byte;
  1483. {$endif AnalyzeLoops}
  1484. Cnt, InstrCnt : Longint;
  1485. InstrProp: TAsmInstrucProp;
  1486. UsedRegs: TRegSet;
  1487. p, hp : Pai;
  1488. TmpRef: TReference;
  1489. TmpReg: TRegister;
  1490. Begin
  1491. p := BlockStart;
  1492. UsedRegs := [];
  1493. UpdateUsedregs(UsedRegs, p);
  1494. SkipHead(P);
  1495. BlockStart := p;
  1496. InstrCnt := 1;
  1497. FillChar(NrOfInstrSinceLastMod, SizeOf(NrOfInstrSinceLastMod), 0);
  1498. While (P <> BlockEnd) Do
  1499. Begin
  1500. {$IfDef TP}
  1501. New(CurProp);
  1502. {$Else TP}
  1503. CurProp := @PaiPropBlock^[InstrCnt];
  1504. {$EndIf TP}
  1505. If (p <> BlockStart)
  1506. Then
  1507. Begin
  1508. {$ifdef JumpAnal}
  1509. If (p^.Typ <> ait_label) Then
  1510. {$endif JumpAnal}
  1511. Begin
  1512. GetLastInstruction(p, hp);
  1513. CurProp^.Regs := PPaiProp(hp^.OptInfo)^.Regs;
  1514. CurProp^.DirFlag := PPaiProp(hp^.OptInfo)^.DirFlag;
  1515. End
  1516. End
  1517. Else
  1518. Begin
  1519. FillChar(CurProp^, SizeOf(CurProp^), 0);
  1520. { For TmpReg := R_EAX to R_EDI Do
  1521. CurProp^.Regs[TmpReg].WState := 1;}
  1522. End;
  1523. CurProp^.UsedRegs := UsedRegs;
  1524. CurProp^.CanBeRemoved := False;
  1525. UpdateUsedRegs(UsedRegs, Pai(p^.Next));
  1526. {$ifdef TP}
  1527. PPaiProp(p^.OptInfo) := CurProp;
  1528. {$Endif TP}
  1529. For TmpReg := R_EAX To R_EDI Do
  1530. Inc(NrOfInstrSinceLastMod[TmpReg]);
  1531. Case p^.typ Of
  1532. ait_label:
  1533. {$Ifndef JumpAnal}
  1534. If (Pai_label(p)^.l^.is_used) Then
  1535. DestroyAllRegs(CurProp);
  1536. {$Else JumpAnal}
  1537. Begin
  1538. If (Pai_Label(p)^.is_used) Then
  1539. With LTable^[Pai_Label(p)^.l^.nb-LoLab] Do
  1540. {$IfDef AnalyzeLoops}
  1541. If (RefsFound = Pai_Label(p)^.l^.RefCount)
  1542. {$Else AnalyzeLoops}
  1543. If (JmpsProcessed = Pai_Label(p)^.l^.RefCount)
  1544. {$EndIf AnalyzeLoops}
  1545. Then
  1546. {all jumps to this label have been found}
  1547. {$IfDef AnalyzeLoops}
  1548. If (JmpsProcessed > 0)
  1549. Then
  1550. {$EndIf AnalyzeLoops}
  1551. {we've processed at least one jump to this label}
  1552. Begin
  1553. If (GetLastInstruction(p, hp) And
  1554. Not(((hp^.typ = ait_labeled_instruction) or
  1555. (hp^.typ = ait_instruction)) And
  1556. (pai386_labeled(hp)^.opcode = A_JMP))
  1557. Then
  1558. {previous instruction not a JMP -> the contents of the registers after the
  1559. previous intruction has been executed have to be taken into account as well}
  1560. For TmpReg := R_EAX to R_EDI Do
  1561. Begin
  1562. If (CurProp^.Regs[TmpReg].WState <>
  1563. PPaiProp(hp^.OptInfo)^.Regs[TmpReg].WState)
  1564. Then DestroyReg(CurProp, TmpReg)
  1565. End
  1566. End
  1567. {$IfDef AnalyzeLoops}
  1568. Else
  1569. {a label from a backward jump (e.g. a loop), no jump to this label has
  1570. already been processed}
  1571. If GetLastInstruction(p, hp) And
  1572. Not(hp^.typ = ait_labeled_instruction) And
  1573. (pai386_labeled(hp)^.opcode = A_JMP))
  1574. Then
  1575. {previous instruction not a jmp, so keep all the registers' contents from the
  1576. previous instruction}
  1577. Begin
  1578. CurProp^.Regs := PPaiProp(hp^.OptInfo)^.Regs;
  1579. CurProp^.DirFlag := PPaiProp(hp^.OptInfo)^.DirFlag;
  1580. End
  1581. Else
  1582. {previous instruction a jmp and no jump to this label processed yet}
  1583. Begin
  1584. hp := p;
  1585. Cnt := InstrCnt;
  1586. {continue until we find a jump to the label or a label which has already
  1587. been processed}
  1588. While GetNextInstruction(hp, hp) And
  1589. Not((hp^.typ = ait_labeled_instruction) And
  1590. (pai386_labeled(hp)^.lab^.nb = Pai_Label(p)^.l^.nb)) And
  1591. Not((hp^.typ = ait_label) And
  1592. (LTable^[Pai_Label(hp)^.l^.nb-LoLab].RefsFound
  1593. = Pai_Label(hp)^.l^.RefCount) And
  1594. (LTable^[Pai_Label(hp)^.l^.nb-LoLab].JmpsProcessed > 0)) Do
  1595. Inc(Cnt);
  1596. If (hp^.typ = ait_label)
  1597. Then
  1598. {there's a processed label after the current one}
  1599. Begin
  1600. CurProp^.Regs := PaiPropBlock^[Cnt].Regs;
  1601. CurProp^.DirFlag := PaiPropBlock^[Cnt].DirFlag;
  1602. End
  1603. Else
  1604. {there's no label anymore after the current one, or they haven't been
  1605. processed yet}
  1606. Begin
  1607. GetLastInstruction(p, hp);
  1608. CurProp^.Regs := PPaiProp(hp^.OptInfo)^.Regs;
  1609. CurProp^.DirFlag := PPaiProp(hp^.OptInfo)^.DirFlag;
  1610. DestroyAllRegs(PPaiProp(hp^.OptInfo))
  1611. End
  1612. End
  1613. {$EndIf AnalyzeLoops}
  1614. Else
  1615. {not all references to this label have been found, so destroy all registers}
  1616. Begin
  1617. GetLastInstruction(p, hp);
  1618. CurProp^.Regs := PPaiProp(hp^.OptInfo)^.Regs;
  1619. CurProp^.DirFlag := PPaiProp(hp^.OptInfo)^.DirFlag;
  1620. DestroyAllRegs(CurProp)
  1621. End;
  1622. End;
  1623. {$EndIf JumpAnal}
  1624. ait_labeled_instruction:
  1625. {$IfNDef JumpAnal}
  1626. ;
  1627. {$Else JumpAnal}
  1628. With LTable^[pai386_labeled(p)^.lab^.nb-LoLab] Do
  1629. If (RefsFound = pai386_labeled(p)^.lab^.RefCount) Then
  1630. Begin
  1631. If (InstrCnt < InstrNr)
  1632. Then
  1633. {forward jump}
  1634. If (JmpsProcessed = 0) Then
  1635. {no jump to this label has been processed yet}
  1636. Begin
  1637. PaiPropBlock^[InstrNr].Regs := CurProp^.Regs;
  1638. PaiPropBlock^[InstrNr].DirFlag := CurProp^.DirFlag;
  1639. Inc(JmpsProcessed);
  1640. End
  1641. Else
  1642. Begin
  1643. For TmpReg := R_EAX to R_EDI Do
  1644. If (PaiPropBlock^[InstrNr].Regs[TmpReg].WState <>
  1645. CurProp^.Regs[TmpReg].WState) Then
  1646. DestroyReg(@PaiPropBlock^[InstrNr], TmpReg);
  1647. Inc(JmpsProcessed);
  1648. End
  1649. {$ifdef AnalyzeLoops}
  1650. Else
  1651. { backward jump, a loop for example}
  1652. { If (JmpsProcessed > 0) Or
  1653. Not(GetLastInstruction(PaiObj, hp) And
  1654. (hp^.typ = ait_labeled_instruction) And
  1655. (pai386_labeled(hp)^.opcode = A_JMP))
  1656. Then}
  1657. {instruction prior to label is not a jmp, or at least one jump to the label
  1658. has yet been processed}
  1659. Begin
  1660. Inc(JmpsProcessed);
  1661. For TmpReg := R_EAX to R_EDI Do
  1662. If (PaiPropBlock^[InstrNr].Regs[TmpReg].WState <>
  1663. CurProp^.Regs[TmpReg].WState)
  1664. Then
  1665. Begin
  1666. TmpState := PaiPropBlock^[InstrNr].Regs[TmpReg].WState;
  1667. Cnt := InstrNr;
  1668. While (TmpState = PaiPropBlock^[Cnt].Regs[TmpReg].WState) Do
  1669. Begin
  1670. DestroyReg(@PaiPropBlock^[Cnt], TmpReg);
  1671. Inc(Cnt);
  1672. End;
  1673. While (Cnt <= InstrCnt) Do
  1674. Begin
  1675. Inc(PaiPropBlock^[Cnt].Regs[TmpReg].WState);
  1676. Inc(Cnt)
  1677. End
  1678. End;
  1679. End
  1680. { Else }
  1681. {instruction prior to label is a jmp and no jumps to the label have yet been
  1682. processed}
  1683. { Begin
  1684. Inc(JmpsProcessed);
  1685. For TmpReg := R_EAX to R_EDI Do
  1686. Begin
  1687. TmpState := PaiPropBlock^[InstrNr].Regs[TmpReg].WState;
  1688. Cnt := InstrNr;
  1689. While (TmpState = PaiPropBlock^[Cnt].Regs[TmpReg].WState) Do
  1690. Begin
  1691. PaiPropBlock^[Cnt].Regs[TmpReg] := CurProp^.Regs[TmpReg];
  1692. Inc(Cnt);
  1693. End;
  1694. TmpState := PaiPropBlock^[InstrNr].Regs[TmpReg].WState;
  1695. While (TmpState = PaiPropBlock^[Cnt].Regs[TmpReg].WState) Do
  1696. Begin
  1697. DestroyReg(@PaiPropBlock^[Cnt], TmpReg);
  1698. Inc(Cnt);
  1699. End;
  1700. While (Cnt <= InstrCnt) Do
  1701. Begin
  1702. Inc(PaiPropBlock^[Cnt].Regs[TmpReg].WState);
  1703. Inc(Cnt)
  1704. End
  1705. End
  1706. End}
  1707. {$endif AnalyzeLoops}
  1708. End;
  1709. {$EndIf JumpAnal}
  1710. {$ifdef GDB}
  1711. ait_stabs, ait_stabn, ait_stab_function_name:;
  1712. {$endif GDB}
  1713. ait_instruction:
  1714. Begin
  1715. InstrProp := AsmInstr[Pai386(p)^.opcode];
  1716. Case Pai386(p)^.opcode Of
  1717. A_MOV, A_MOVZX, A_MOVSX:
  1718. Begin
  1719. Case Pai386(p)^.oper[0].typ Of
  1720. Top_Reg:
  1721. Case Pai386(p)^.oper[1].typ Of
  1722. Top_Reg:
  1723. Begin
  1724. DestroyReg(CurProp, Pai386(p)^.oper[1].reg);
  1725. ReadReg(CurProp, Pai386(p)^.oper[0].reg);
  1726. { CurProp^.Regs[Pai386(p)^.oper[1].reg] :=
  1727. CurProp^.Regs[Pai386(p)^.oper[0].reg];
  1728. If (CurProp^.Regs[Pai386(p)^.oper[1].reg].ModReg = R_NO) Then
  1729. CurProp^.Regs[Pai386(p)^.oper[1].reg].ModReg :=
  1730. Pai386(p)^.oper[0].reg;}
  1731. End;
  1732. Top_Ref:
  1733. Begin
  1734. ReadReg(CurProp, Pai386(p)^.oper[0].reg);
  1735. ReadRef(CurProp, Pai386(p)^.oper[1].ref);
  1736. DestroyRefs(p, Pai386(p)^.oper[1].ref^, Pai386(p)^.oper[0].reg);
  1737. End;
  1738. End;
  1739. Top_Ref:
  1740. Begin {destination is always a register in this case}
  1741. ReadRef(CurProp, Pai386(p)^.oper[0].ref);
  1742. ReadReg(CurProp, Pai386(p)^.oper[1].reg);
  1743. TmpReg := Reg32(Pai386(p)^.oper[1].reg);
  1744. If RegInRef(TmpReg, Pai386(p)^.oper[0].ref^) And
  1745. (CurProp^.Regs[TmpReg].Typ = Con_Ref)
  1746. Then
  1747. Begin
  1748. With CurProp^.Regs[TmpReg] Do
  1749. Begin
  1750. IncState(WState);
  1751. {also store how many instructions are part of the sequence in the first
  1752. instructions PPaiProp, so it can be easily accessed from within
  1753. CheckSequence}
  1754. Inc(NrOfMods, NrOfInstrSinceLastMod[TmpReg]);
  1755. PPaiProp(Pai(StartMod)^.OptInfo)^.Regs[TmpReg].NrOfMods := NrOfMods;
  1756. NrOfInstrSinceLastMod[TmpReg] := 0;
  1757. End;
  1758. End
  1759. Else
  1760. Begin
  1761. DestroyReg(CurProp, TmpReg);
  1762. If Not(RegInRef(TmpReg, Pai386(p)^.oper[0].ref^)) Then
  1763. With CurProp^.Regs[TmpReg] Do
  1764. Begin
  1765. Typ := Con_Ref;
  1766. StartMod := p;
  1767. NrOfMods := 1;
  1768. End
  1769. End;
  1770. {$ifdef StateDebug}
  1771. hp := new(pai_asm_comment,init(strpnew(att_reg2str[TmpReg]+': '+tostr(CurProp^.Regs[TmpReg].WState))));
  1772. InsertLLItem(AsmL, p, p^.next, hp);
  1773. {$endif StateDebug}
  1774. End;
  1775. Top_Const:
  1776. Begin
  1777. Case Pai386(p)^.oper[1].typ Of
  1778. Top_Reg:
  1779. Begin
  1780. TmpReg := Reg32(Pai386(p)^.oper[1].reg);
  1781. With CurProp^.Regs[TmpReg] Do
  1782. Begin
  1783. DestroyReg(CurProp, TmpReg);
  1784. typ := Con_Const;
  1785. StartMod := p;
  1786. End
  1787. End;
  1788. Top_Ref:
  1789. Begin
  1790. ReadRef(CurProp, Pai386(p)^.oper[1].ref);
  1791. DestroyRefs(P, Pai386(p)^.oper[1].ref^, R_NO);
  1792. End;
  1793. End;
  1794. End;
  1795. End;
  1796. End;
  1797. A_IMUL:
  1798. Begin
  1799. ReadOp(CurProp, Pai386(p)^.oper[0]);
  1800. ReadOp(CurProp, Pai386(p)^.oper[1]);
  1801. If (Pai386(p)^.oper[2].typ = top_none) Then
  1802. If (Pai386(p)^.oper[1].typ = top_none) Then
  1803. Begin
  1804. DestroyReg(CurProp, R_EAX);
  1805. DestroyReg(CurProp, R_EDX)
  1806. End
  1807. Else
  1808. DestroyOp(p, Pai386(p)^.oper[1])
  1809. Else
  1810. DestroyReg(CurProp, Pai386(p)^.oper[2].reg);
  1811. End;
  1812. A_XOR:
  1813. Begin
  1814. ReadOp(CurProp, Pai386(p)^.oper[0]);
  1815. ReadOp(CurProp, Pai386(p)^.oper[1]);
  1816. If (Pai386(p)^.oper[0].typ = top_reg) And
  1817. (Pai386(p)^.oper[1].typ = top_reg) And
  1818. (Pai386(p)^.oper[0].reg = Pai386(p)^.oper[1].reg)
  1819. Then
  1820. Begin
  1821. DestroyReg(CurProp, Pai386(p)^.oper[0].reg);
  1822. CurProp^.Regs[Reg32(Pai386(p)^.oper[0].reg)].typ := Con_Const;
  1823. CurProp^.Regs[Reg32(Pai386(p)^.oper[0].reg)].StartMod := Pointer(0)
  1824. End
  1825. Else
  1826. DestroyOp(p, Pai386(p)^.oper[1]);
  1827. End
  1828. Else
  1829. Begin
  1830. Cnt := 1;
  1831. While (Cnt <= MaxCh) And
  1832. (InstrProp.Ch[Cnt] <> C_None) Do
  1833. Begin
  1834. Case InstrProp.Ch[Cnt] Of
  1835. C_REAX..C_REDI: ReadReg(CurProp,TCh2Reg(InstrProp.Ch[Cnt]));
  1836. C_WEAX..C_RWEDI:
  1837. Begin
  1838. If (InstrProp.Ch[Cnt] >= C_RWEAX) Then
  1839. ReadReg(CurProp, TCh2Reg(InstrProp.Ch[Cnt]));
  1840. DestroyReg(CurProp, TCh2Reg(InstrProp.Ch[Cnt]));
  1841. End;
  1842. C_CDirFlag: CurProp^.DirFlag := F_NotSet;
  1843. C_SDirFlag: CurProp^.DirFlag := F_Set;
  1844. C_Rop1: ReadOp(CurProp, Pai386(p)^.oper[0]);
  1845. C_Rop2: ReadOp(CurProp, Pai386(p)^.oper[1]);
  1846. C_ROp3: ReadOp(CurProp, Pai386(p)^.oper[2]);
  1847. C_Wop1..C_RWop1:
  1848. Begin
  1849. If (InstrProp.Ch[Cnt] = C_RWop1) Then
  1850. ReadOp(CurProp, Pai386(p)^.oper[0]);
  1851. DestroyOp(p, Pai386(p)^.oper[0]);
  1852. End;
  1853. C_Wop2..C_RWop2:
  1854. Begin
  1855. If (InstrProp.Ch[Cnt] = C_RWop2) Then
  1856. ReadOp(CurProp, Pai386(p)^.oper[1]);
  1857. DestroyOp(p, Pai386(p)^.oper[1]);
  1858. End;
  1859. C_WOp3..C_RWOp3:
  1860. Begin
  1861. If (InstrProp.Ch[Cnt] = C_RWOp3) Then
  1862. ReadOp(CurProp, Pai386(p)^.oper[2]);
  1863. DestroyOp(p, Pai386(p)^.oper[2]);
  1864. End;
  1865. C_WMemEDI:
  1866. Begin
  1867. ReadReg(CurProp, R_EDI);
  1868. FillChar(TmpRef, SizeOf(TmpRef), 0);
  1869. TmpRef.Base := R_EDI;
  1870. DestroyRefs(p, TmpRef, R_NO)
  1871. End;
  1872. C_RFlags, C_WFlags, C_RWFlags, C_FPU:
  1873. Else
  1874. Begin
  1875. DestroyAllRegs(CurProp);
  1876. End;
  1877. End;
  1878. Inc(Cnt);
  1879. End
  1880. End;
  1881. End;
  1882. End
  1883. Else
  1884. Begin
  1885. DestroyAllRegs(CurProp);
  1886. End;
  1887. End;
  1888. Inc(InstrCnt);
  1889. GetNextInstruction(p, p);
  1890. End;
  1891. End;
  1892. Function InitDFAPass2(BlockStart, BlockEnd: Pai): Boolean;
  1893. {reserves memory for the PPaiProps in one big memory block when not using
  1894. TP, returns False if not enough memory is available for the optimizer in all
  1895. cases}
  1896. Var p: Pai;
  1897. Count: Longint;
  1898. { TmpStr: String; }
  1899. Begin
  1900. P := BlockStart;
  1901. SkipHead(P);
  1902. NrOfPaiObjs := 0;
  1903. While (P <> BlockEnd) Do
  1904. Begin
  1905. {$IfDef JumpAnal}
  1906. Case P^.Typ Of
  1907. ait_labeled_instruction:
  1908. begin
  1909. If (pai386_labeled(P)^.lab^.nb >= LoLab) And
  1910. (pai386_labeled(P)^.lab^.nb <= HiLab) Then
  1911. Inc(LTable^[pai386_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)^.opcode = A_PUSH) And
  1921. (Pai386(p)^.oper[0].typ = top_symbol) And
  1922. (PCSymbol(Pai386(p)^.oper[0])^.offset = 0) Then
  1923. Begin
  1924. TmpStr := StrPas(PCSymbol(Pai386(p)^.oper[0])^.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 := BlockStart;
  1946. SkipHead(p);
  1947. For Count := 1 To NrOfPaiObjs Do
  1948. Begin
  1949. PPaiProp(p^.OptInfo) := @PaiPropBlock^[Count];
  1950. GetNextInstruction(p, p);
  1951. End;
  1952. End
  1953. Else InitDFAPass2 := False;
  1954. {$EndIf TP}
  1955. End;
  1956. Function DFAPass2(
  1957. {$ifdef statedebug}
  1958. AsmL: PAasmOutPut;
  1959. {$endif statedebug}
  1960. BlockStart, BlockEnd: Pai): Boolean;
  1961. Begin
  1962. If InitDFAPass2(BlockStart, BlockEnd) Then
  1963. Begin
  1964. DoDFAPass2(
  1965. {$ifdef statedebug}
  1966. asml,
  1967. {$endif statedebug}
  1968. BlockStart, BlockEnd);
  1969. DFAPass2 := True
  1970. End
  1971. Else DFAPass2 := False;
  1972. End;
  1973. Procedure ShutDownDFA;
  1974. Begin
  1975. If LabDif <> 0 Then
  1976. FreeMem(LTable, LabDif*SizeOf(TLabelTableItem));
  1977. End;
  1978. End.
  1979. {
  1980. $Log$
  1981. Revision 1.46 1999-05-08 20:40:02 jonas
  1982. * seperate OPTimizer INFO pointer field in tai object
  1983. * fix to GetLastInstruction that sometimes caused a crash
  1984. Revision 1.45 1999/05/01 13:48:37 peter
  1985. * merged nasm compiler
  1986. Revision 1.6 1999/04/18 17:57:21 jonas
  1987. * fix for crash when the first instruction of a sequence that gets
  1988. optimized is removed (this situation can't occur aymore now)
  1989. Revision 1.5 1999/04/16 11:49:50 peter
  1990. + tempalloc
  1991. + -at to show temp alloc info in .s file
  1992. Revision 1.4 1999/04/14 09:07:42 peter
  1993. * asm reader improvements
  1994. Revision 1.3 1999/03/31 13:55:29 peter
  1995. * assembler inlining working for ag386bin
  1996. Revision 1.2 1999/03/29 16:05:46 peter
  1997. * optimizer working for ag386bin
  1998. Revision 1.1 1999/03/26 00:01:10 peter
  1999. * first things for optimizer (compiles but cycle crashes)
  2000. Revision 1.39 1999/02/26 00:48:18 peter
  2001. * assembler writers fixed for ag386bin
  2002. Revision 1.38 1999/02/25 21:02:34 peter
  2003. * ag386bin updates
  2004. + coff writer
  2005. Revision 1.37 1999/02/22 02:15:20 peter
  2006. * updates for ag386bin
  2007. Revision 1.36 1999/01/20 17:41:26 jonas
  2008. * small bugfix (memory corruption could occur when certain fpu instructions
  2009. were encountered)
  2010. Revision 1.35 1999/01/08 12:39:22 florian
  2011. Changes of Alexander Stohr integrated:
  2012. + added KNI opcodes
  2013. + added KNI registers
  2014. + added 3DNow! opcodes
  2015. + added 64 bit and 128 bit register flags
  2016. * translated a few comments into english
  2017. Revision 1.34 1998/12/29 18:48:19 jonas
  2018. + optimize pascal code surrounding assembler blocks
  2019. Revision 1.33 1998/12/17 16:37:38 jonas
  2020. + extra checks in RegsEquivalent so some more optimizations can be done (which
  2021. where disabled by the second fix from revision 1.22)
  2022. Revision 1.32 1998/12/15 19:33:58 jonas
  2023. * uncommented OpsEqual & added to interface because popt386 uses it now
  2024. Revision 1.31 1998/12/11 00:03:13 peter
  2025. + globtype,tokens,version unit splitted from globals
  2026. Revision 1.30 1998/12/02 16:23:39 jonas
  2027. * changed "if longintvar in set" to case or "if () or () .." statements
  2028. * tree.pas: changed inlinenumber (and associated constructor/vars) to a byte
  2029. Revision 1.29 1998/11/26 21:45:31 jonas
  2030. - removed A_CLTD opcode (use A_CDQ instead)
  2031. * changed cbw, cwde and cwd to cbtw, cwtl and cwtd in att_.oper[1]str array
  2032. * in daopt386: adapted AsmInstr array to reflect changes + fixed line too long
  2033. Revision 1.27 1998/11/24 19:47:22 jonas
  2034. * fixed problems posible with 3 operand instructions
  2035. Revision 1.26 1998/11/24 12:50:09 peter
  2036. * fixed crash
  2037. Revision 1.25 1998/11/18 17:58:22 jonas
  2038. + gathering of register reading data, nowhere used yet (necessary for instruction scheduling)
  2039. Revision 1.24 1998/11/13 10:13:44 peter
  2040. + cpuid,emms support for asm readers
  2041. Revision 1.23 1998/11/09 19:40:46 jonas
  2042. * fixed comments from last commit (apparently there's still a 255 char limit :( )
  2043. Revision 1.22 1998/11/09 19:33:40 jonas
  2044. * changed specific bugfix (which was actually wrong implemented, but
  2045. did the right thing in most cases nevertheless) to general bugfix
  2046. * fixed bug that caused
  2047. mov (ebp), edx mov (ebp), edx
  2048. mov (edx), edx mov (edx), edx
  2049. ... being changed to ...
  2050. mov (ebp), edx mov edx, eax
  2051. mov (eax), eax
  2052. but this disabled another small correct optimization...
  2053. Revision 1.21 1998/11/02 23:17:49 jonas
  2054. * fixed bug shown in sortbug program from fpc-devel list
  2055. Revision 1.20 1998/10/22 13:24:51 jonas
  2056. * changed TRegSet to a small set
  2057. Revision 1.19 1998/10/20 09:29:24 peter
  2058. * bugfix so that code like
  2059. movl 48(%esi),%esi movl 48(%esi),%esi
  2060. pushl %esi doesn't get changed to pushl %esi
  2061. movl 48(%esi),%edi movl %esi,%edi
  2062. Revision 1.18 1998/10/07 16:27:02 jonas
  2063. * changed state to WState (WriteState), added RState for future use in
  2064. instruction scheduling
  2065. * RegAlloc data from the CG is now completely being patched and corrected (I
  2066. think)
  2067. Revision 1.17 1998/10/02 17:30:20 jonas
  2068. * small patches to regdealloc data
  2069. Revision 1.16 1998/10/01 20:21:47 jonas
  2070. * inter-register CSE, still requires some tweaks (peepholeoptpass2, better RegAlloc)
  2071. Revision 1.15 1998/09/20 18:00:20 florian
  2072. * small compiling problems fixed
  2073. Revision 1.14 1998/09/20 17:12:36 jonas
  2074. * small fix for uncertain optimizations & more cleaning up
  2075. Revision 1.12 1998/09/16 18:00:01 jonas
  2076. * optimizer now completely dependant on GetNext/GetLast instruction, works again with -dRegAlloc
  2077. Revision 1.11 1998/09/15 14:05:27 jonas
  2078. * fixed optimizer incompatibilities with freelabel code in psub
  2079. Revision 1.10 1998/09/09 15:33:58 peter
  2080. * removed warnings
  2081. Revision 1.9 1998/09/03 16:24:51 florian
  2082. * bug of type conversation from dword to real fixed
  2083. * bug fix of Jonas applied
  2084. Revision 1.8 1998/08/28 10:56:59 peter
  2085. * removed warnings
  2086. Revision 1.7 1998/08/19 16:07:44 jonas
  2087. * changed optimizer switches + cleanup of DestroyRefs in daopt386.pas
  2088. Revision 1.6 1998/08/10 14:49:57 peter
  2089. + localswitches, moduleswitches, globalswitches splitting
  2090. Revision 1.5 1998/08/09 13:56:24 jonas
  2091. * small bugfix for uncertain optimizations in DestroyRefs
  2092. Revision 1.4 1998/08/06 19:40:25 jonas
  2093. * removed $ before and after Log in comment
  2094. Revision 1.3 1998/08/05 16:00:14 florian
  2095. * some fixes for ansi strings
  2096. * log to Log changed
  2097. }