daopt386.pas 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277
  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: PasmLabel; 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 VER70}
  138. TPaiPropBlock = Array[1..250000] Of TPaiProp;
  139. PPaiPropBlock = ^TPaiPropBlock;
  140. {$EndIf VER70}
  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 VER70}
  151. TLabelTable = Array[0..10000] Of TLabelTableItem;
  152. {$Else VER70}
  153. TLabelTable = Array[0..2500000] Of TLabelTableItem;
  154. {$Endif VER70}
  155. PLabelTable = ^TLabelTable;
  156. {******************************* Variables *******************************}
  157. Var
  158. {the amount of PaiObjects in the current assembler list}
  159. NrOfPaiObjs: Longint;
  160. {$IfNDef VER70}
  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(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^.labelnr < LowLabel) Then
  557. LowLabel := Pai_Label(p)^.l^.labelnr;
  558. If (Pai_Label(p)^.l^.labelnr > HighLabel) Then
  559. HighLabel := Pai_Label(p)^.l^.labelnr;
  560. End;
  561. GetNextInstruction(p, p);
  562. End;
  563. FindLoHiLabels := p;
  564. If LabelFound
  565. Then LabelDif := HighLabel+1-LowLabel
  566. Else LabelDif := 0;
  567. End;
  568. Function FindRegAlloc(Reg: TRegister; StartPai: Pai): Boolean;
  569. {Returns true if a ait_alloc object for Reg is found in the block of Pai's
  570. starting with StartPai and ending with the next "real" instruction}
  571. Begin
  572. FindRegAlloc:=False;
  573. Repeat
  574. While Assigned(StartPai) And
  575. ((StartPai^.typ in (SkipInstr - [ait_regAlloc])) Or
  576. ((StartPai^.typ = ait_label) and
  577. Not(Pai_Label(StartPai)^.l^.Is_Used))) Do
  578. StartPai := Pai(StartPai^.Next);
  579. If Assigned(StartPai) And
  580. (StartPai^.typ = ait_regAlloc) and (PairegAlloc(StartPai)^.allocation) Then
  581. Begin
  582. if PairegAlloc(StartPai)^.Reg = Reg then
  583. begin
  584. FindRegAlloc:=true;
  585. exit;
  586. end;
  587. StartPai := Pai(StartPai^.Next);
  588. End
  589. else
  590. exit;
  591. Until false;
  592. End;
  593. Procedure BuildLabelTableAndFixRegAlloc(AsmL: PAasmOutput; Var LabelTable: PLabelTable; LowLabel: Longint;
  594. Var LabelDif: Longint; BlockStart, BlockEnd: Pai);
  595. {Builds a table with the locations of the labels in the paasmoutput.
  596. Also fixes some RegDeallocs like "# %eax released; push (%eax)"}
  597. Var p, hp1, hp2: Pai;
  598. UsedRegs: TRegSet;
  599. Begin
  600. UsedRegs := [];
  601. If (LabelDif <> 0) Then
  602. Begin
  603. {$IfDef TP}
  604. {$ifndef Delphi}
  605. If (MaxAvail >= LabelDif*SizeOf(Pai))
  606. Then
  607. Begin
  608. {$endif Delphi}
  609. {$EndIf TP}
  610. GetMem(LabelTable, LabelDif*SizeOf(TLabelTableItem));
  611. FillChar(LabelTable^, LabelDif*SizeOf(TLabelTableItem), 0);
  612. p := BlockStart;
  613. While (P <> BlockEnd) Do
  614. Begin
  615. Case p^.typ Of
  616. ait_Label:
  617. If Pai_Label(p)^.l^.is_used Then
  618. LabelTable^[Pai_Label(p)^.l^.labelnr-LowLabel].PaiObj := p;
  619. ait_regAlloc:
  620. begin
  621. if PairegAlloc(p)^.Allocation then
  622. Begin
  623. If Not(PaiRegAlloc(p)^.Reg in UsedRegs) Then
  624. UsedRegs := UsedRegs + [PaiRegAlloc(p)^.Reg]
  625. Else
  626. Begin
  627. hp1 := p;
  628. hp2 := nil;
  629. While GetLastInstruction(hp1, hp1) And
  630. Not(RegInInstruction(PaiRegAlloc(p)^.Reg, hp1)) Do
  631. hp2 := hp1;
  632. If hp2 <> nil Then
  633. Begin
  634. hp1 := New(PaiRegAlloc, DeAlloc(PaiRegAlloc(p)^.Reg));
  635. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp1);
  636. End;
  637. End;
  638. End
  639. else
  640. Begin
  641. UsedRegs := UsedRegs - [PaiRegAlloc(p)^.Reg];
  642. hp1 := p;
  643. hp2 := nil;
  644. While Not(FindRegAlloc(PaiRegAlloc(p)^.Reg, Pai(hp1^.Next))) And
  645. GetNextInstruction(hp1, hp1) And
  646. RegInInstruction(PaiRegAlloc(p)^.Reg, hp1) Do
  647. hp2 := hp1;
  648. If hp2 <> nil Then
  649. Begin
  650. hp1 := Pai(p^.previous);
  651. AsmL^.Remove(p);
  652. InsertLLItem(AsmL, hp2, Pai(hp2^.Next), p);
  653. p := hp1;
  654. End;
  655. End;
  656. end;
  657. End;
  658. P := Pai(p^.Next);
  659. While Assigned(p) And
  660. (p^.typ in (SkipInstr - [ait_regalloc])) Do
  661. P := Pai(P^.Next);
  662. End;
  663. {$IfDef TP}
  664. {$ifndef Delphi}
  665. End
  666. Else LabelDif := 0;
  667. {$endif Delphi}
  668. {$EndIf TP}
  669. End;
  670. End;
  671. {************************ Search the Label table ************************}
  672. Function FindLabel(L: PasmLabel; Var hp: Pai): Boolean;
  673. {searches for the specified label starting from hp as long as the
  674. encountered instructions are labels, to be able to optimize constructs like
  675. jne l2 jmp l2
  676. jmp l3 and l1:
  677. l1: l2:
  678. l2:}
  679. Var TempP: Pai;
  680. Begin
  681. TempP := hp;
  682. While Assigned(TempP) and
  683. (TempP^.typ In SkipInstr + [ait_label]) Do
  684. If (TempP^.typ <> ait_Label) Or
  685. (pai_label(TempP)^.l <> L)
  686. Then GetNextInstruction(TempP, TempP)
  687. Else
  688. Begin
  689. hp := TempP;
  690. FindLabel := True;
  691. exit
  692. End;
  693. FindLabel := False;
  694. End;
  695. {************************ Some general functions ************************}
  696. Function Reg32(Reg: TRegister): TRegister;
  697. {Returns the 32 bit component of Reg if it exists, otherwise Reg is returned}
  698. Begin
  699. Reg32 := Reg;
  700. If (Reg >= R_AX)
  701. Then
  702. If (Reg <= R_DI)
  703. Then Reg32 := Reg16ToReg32(Reg)
  704. Else
  705. If (Reg <= R_BL)
  706. Then Reg32 := Reg8toReg32(Reg);
  707. End;
  708. { inserts new_one between prev and foll }
  709. Procedure InsertLLItem(AsmL: PAasmOutput; prev, foll, new_one: PLinkedList_Item);
  710. Begin
  711. If Assigned(prev) Then
  712. If Assigned(foll) Then
  713. Begin
  714. If Assigned(new_one) Then
  715. Begin
  716. new_one^.previous := prev;
  717. new_one^.next := foll;
  718. prev^.next := new_one;
  719. foll^.previous := new_one;
  720. End;
  721. End
  722. Else AsmL^.Concat(new_one)
  723. Else If Assigned(Foll) Then AsmL^.Insert(new_one)
  724. End;
  725. {********************* Compare parts of Pai objects *********************}
  726. Function RegsSameSize(Reg1, Reg2: TRegister): Boolean;
  727. {returns true if Reg1 and Reg2 are of the same size (so if they're both
  728. 8bit, 16bit or 32bit)}
  729. Begin
  730. If (Reg1 <= R_EDI)
  731. Then RegsSameSize := (Reg2 <= R_EDI)
  732. Else
  733. If (Reg1 <= R_DI)
  734. Then RegsSameSize := (Reg2 in [R_AX..R_DI])
  735. Else
  736. If (Reg1 <= R_BL)
  737. Then RegsSameSize := (Reg2 in [R_AL..R_BL])
  738. Else RegsSameSize := False
  739. End;
  740. Procedure AddReg2RegInfo(OldReg, NewReg: TRegister; Var RegInfo: TRegInfo);
  741. {updates the ???RegsEncountered and ???2???Reg fields of RegInfo. Assumes that
  742. OldReg and NewReg have the same size (has to be chcked in advance with
  743. RegsSameSize) and that neither equals R_NO}
  744. Begin
  745. With RegInfo Do
  746. Begin
  747. NewRegsEncountered := NewRegsEncountered + [NewReg];
  748. OldRegsEncountered := OldRegsEncountered + [OldReg];
  749. New2OldReg[NewReg] := OldReg;
  750. Case OldReg Of
  751. R_EAX..R_EDI:
  752. Begin
  753. NewRegsEncountered := NewRegsEncountered + [Reg32toReg16(NewReg)];
  754. OldRegsEncountered := OldRegsEncountered + [Reg32toReg16(OldReg)];
  755. New2OldReg[Reg32toReg16(NewReg)] := Reg32toReg16(OldReg);
  756. If (NewReg in [R_EAX..R_EBX]) And
  757. (OldReg in [R_EAX..R_EBX]) Then
  758. Begin
  759. NewRegsEncountered := NewRegsEncountered + [Reg32toReg8(NewReg)];
  760. OldRegsEncountered := OldRegsEncountered + [Reg32toReg8(OldReg)];
  761. New2OldReg[Reg32toReg8(NewReg)] := Reg32toReg8(OldReg);
  762. End;
  763. End;
  764. R_AX..R_DI:
  765. Begin
  766. NewRegsEncountered := NewRegsEncountered + [Reg16toReg32(NewReg)];
  767. OldRegsEncountered := OldRegsEncountered + [Reg16toReg32(OldReg)];
  768. New2OldReg[Reg16toReg32(NewReg)] := Reg16toReg32(OldReg);
  769. If (NewReg in [R_AX..R_BX]) And
  770. (OldReg in [R_AX..R_BX]) Then
  771. Begin
  772. NewRegsEncountered := NewRegsEncountered + [Reg16toReg8(NewReg)];
  773. OldRegsEncountered := OldRegsEncountered + [Reg16toReg8(OldReg)];
  774. New2OldReg[Reg16toReg8(NewReg)] := Reg16toReg8(OldReg);
  775. End;
  776. End;
  777. R_AL..R_BL:
  778. Begin
  779. NewRegsEncountered := NewRegsEncountered + [Reg8toReg32(NewReg)]
  780. + [Reg8toReg16(NewReg)];
  781. OldRegsEncountered := OldRegsEncountered + [Reg8toReg32(OldReg)]
  782. + [Reg8toReg16(OldReg)];
  783. New2OldReg[Reg8toReg32(NewReg)] := Reg8toReg32(OldReg);
  784. End;
  785. End;
  786. End;
  787. End;
  788. Procedure AddOpRegInfo(const o:Toper; Var RegInfo: TRegInfo);
  789. Begin
  790. Case o.typ Of
  791. Top_Reg:
  792. If (o.reg <> R_NO) Then
  793. AddReg2RegInfo(o.reg, o.reg, RegInfo);
  794. Top_Ref:
  795. Begin
  796. If o.ref^.base <> R_NO Then
  797. AddReg2RegInfo(o.ref^.base, o.ref^.base, RegInfo);
  798. If o.ref^.index <> R_NO Then
  799. AddReg2RegInfo(o.ref^.index, o.ref^.index, RegInfo);
  800. End;
  801. End;
  802. End;
  803. Function RegsEquivalent(OldReg, NewReg: TRegister; Var RegInfo: TRegInfo; OPAct: TOpAction): Boolean;
  804. Begin
  805. If Not((OldReg = R_NO) Or (NewReg = R_NO)) Then
  806. If RegsSameSize(OldReg, NewReg) Then
  807. With RegInfo Do
  808. {here we always check for the 32 bit component, because it is possible that
  809. the 8 bit component has not been set, event though NewReg already has been
  810. processed. This happens if it has been compared with a register that doesn't
  811. have an 8 bit component (such as EDI). In that case the 8 bit component is
  812. still set to R_NO and the comparison in the Else-part will fail}
  813. If (Reg32(OldReg) in OldRegsEncountered) Then
  814. If (Reg32(NewReg) in NewRegsEncountered) Then
  815. RegsEquivalent := (OldReg = New2OldReg[NewReg])
  816. { If we haven't encountered the new register yet, but we have encountered the
  817. old one already, the new one can only be correct if it's being written to
  818. (and consequently the old one is also being written to), otherwise
  819. movl -8(%ebp), %eax and movl -8(%ebp), %eax
  820. movl (%eax), %eax movl (%edx), %edx
  821. are considered equivalent}
  822. Else
  823. If (OpAct = OpAct_Write) Then
  824. Begin
  825. AddReg2RegInfo(OldReg, NewReg, RegInfo);
  826. RegsEquivalent := True
  827. End
  828. Else Regsequivalent := False
  829. Else
  830. If Not(Reg32(NewReg) in NewRegsEncountered) Then
  831. Begin
  832. AddReg2RegInfo(OldReg, NewReg, RegInfo);
  833. RegsEquivalent := True
  834. End
  835. Else RegsEquivalent := False
  836. Else RegsEquivalent := False
  837. Else RegsEquivalent := OldReg = NewReg
  838. End;
  839. Function RefsEquivalent(Const R1, R2: TReference; var RegInfo: TRegInfo; OpAct: TOpAction): Boolean;
  840. Begin
  841. If R1.is_immediate Then
  842. RefsEquivalent := R2.is_immediate and (R1.Offset = R2.Offset)
  843. Else
  844. RefsEquivalent := (R1.Offset+R1.OffsetFixup = R2.Offset+R2.OffsetFixup) And
  845. RegsEquivalent(R1.Base, R2.Base, RegInfo, OpAct) And
  846. RegsEquivalent(R1.Index, R2.Index, RegInfo, OpAct) And
  847. (R1.Segment = R2.Segment) And (R1.ScaleFactor = R2.ScaleFactor) And
  848. (R1.Symbol = R2.Symbol);
  849. End;
  850. Function RefsEqual(Const R1, R2: TReference): Boolean;
  851. Begin
  852. If R1.is_immediate Then
  853. RefsEqual := R2.is_immediate and (R1.Offset = R2.Offset)
  854. Else
  855. RefsEqual := (R1.Offset+R1.OffsetFixup = R2.Offset+R2.OffsetFixup) And
  856. (R1.Segment = R2.Segment) And (R1.Base = R2.Base) And
  857. (R1.Index = R2.Index) And (R1.ScaleFactor = R2.ScaleFactor) And
  858. (R1.Symbol=R2.Symbol);
  859. End;
  860. Function IsGP32Reg(Reg: TRegister): Boolean;
  861. {Checks if the register is a 32 bit general purpose register}
  862. Begin
  863. If (Reg >= R_EAX) and (Reg <= R_EBX)
  864. Then IsGP32Reg := True
  865. Else IsGP32reg := False
  866. End;
  867. Function RegInRef(Reg: TRegister; Const Ref: TReference): Boolean;
  868. Begin {checks whether Ref contains a reference to Reg}
  869. Reg := Reg32(Reg);
  870. RegInRef := (Ref.Base = Reg) Or (Ref.Index = Reg)
  871. End;
  872. Function RegInInstruction(Reg: TRegister; p1: Pai): Boolean;
  873. {checks if Reg is used by the instruction p1}
  874. Var TmpResult: Boolean;
  875. Begin
  876. TmpResult := False;
  877. If (Pai(p1)^.typ = ait_instruction) Then
  878. Begin
  879. Case Pai386(p1)^.oper[0].typ Of
  880. Top_Reg: TmpResult := Reg = Pai386(p1)^.oper[0].reg;
  881. Top_Ref: TmpResult := RegInRef(Reg, Pai386(p1)^.oper[0].ref^);
  882. End;
  883. If Not(TmpResult) Then
  884. Case Pai386(p1)^.oper[1].typ Of
  885. Top_Reg: TmpResult := (Reg = Pai386(p1)^.oper[1].reg);
  886. Top_Ref: TmpResult := RegInRef(Reg, Pai386(p1)^.oper[1].ref^)
  887. End;
  888. If Not(TmpResult) Then
  889. Case Pai386(p1)^.oper[2].typ Of
  890. Top_Reg: TmpResult := (Reg = Pai386(p1)^.oper[2].reg);
  891. Top_Ref: TmpResult := RegInRef(Reg, Pai386(p1)^.oper[2].ref^)
  892. End
  893. End;
  894. RegInInstruction := TmpResult
  895. End;
  896. {Function RegInOp(Reg: TRegister; const o:toper): Boolean;
  897. Begin
  898. RegInOp := False;
  899. Case opt Of
  900. top_reg: RegInOp := Reg = o.reg;
  901. top_ref: RegInOp := (Reg = o.ref^.Base) Or
  902. (Reg = o.ref^.Index);
  903. End;
  904. End;}
  905. Function RegModifiedByInstruction(Reg: TRegister; p1: Pai): Boolean;
  906. {returns true if Reg is modified by the instruction p1. P1 is assumed to be
  907. of the type ait_instruction}
  908. Var hp: Pai;
  909. Begin
  910. If GetLastInstruction(p1, hp)
  911. Then
  912. RegModifiedByInstruction :=
  913. PPAiProp(p1^.OptInfo)^.Regs[Reg].WState <>
  914. PPAiProp(hp^.OptInfo)^.Regs[Reg].WState
  915. Else RegModifiedByInstruction := True;
  916. End;
  917. {********************* GetNext and GetLastInstruction *********************}
  918. Function GetNextInstruction(Current: Pai; Var Next: Pai): Boolean;
  919. {skips ait_regalloc, ait_regdealloc and ait_stab* objects and puts the
  920. next pai object in Next. Returns false if there isn't any}
  921. Begin
  922. Repeat
  923. Current := Pai(Current^.Next);
  924. While Assigned(Current) And
  925. ((Current^.typ In SkipInstr) or
  926. ((Current^.typ = ait_label) And
  927. Not(Pai_Label(Current)^.l^.is_used))) Do
  928. Current := Pai(Current^.Next);
  929. If Assigned(Current) And
  930. (Current^.typ = ait_Marker) And
  931. (Pai_Marker(Current)^.Kind = NoPropInfoStart) Then
  932. Begin
  933. While Assigned(Current) And
  934. ((Current^.typ <> ait_Marker) Or
  935. (Pai_Marker(Current)^.Kind <> NoPropInfoEnd)) Do
  936. Current := Pai(Current^.Next);
  937. End;
  938. Until Not(Assigned(Current)) Or
  939. (Current^.typ <> ait_Marker) Or
  940. (Pai_Marker(Current)^.Kind <> NoPropInfoEnd);
  941. Next := Current;
  942. If Assigned(Current) And
  943. Not((Current^.typ In SkipInstr) or
  944. ((Current^.typ = ait_label) And
  945. Not(Pai_Label(Current)^.l^.is_used)))
  946. Then GetNextInstruction := True
  947. Else
  948. Begin
  949. Next := Nil;
  950. GetNextInstruction := False;
  951. End;
  952. End;
  953. Function GetLastInstruction(Current: Pai; Var Last: Pai): Boolean;
  954. {skips the ait-types in SkipInstr puts the previous pai object in
  955. Last. Returns false if there isn't any}
  956. Begin
  957. Repeat
  958. Current := Pai(Current^.previous);
  959. While Assigned(Current) And
  960. (((Current^.typ = ait_Marker) And
  961. Not(Pai_Marker(Current)^.Kind in [AsmBlockEnd,NoPropInfoEnd])) or
  962. (Current^.typ In SkipInstr) or
  963. ((Current^.typ = ait_label) And
  964. Not(Pai_Label(Current)^.l^.is_used))) Do
  965. Current := Pai(Current^.previous);
  966. If Assigned(Current) And
  967. (Current^.typ = ait_Marker) And
  968. (Pai_Marker(Current)^.Kind = NoPropInfoEnd) Then
  969. Begin
  970. While Assigned(Current) And
  971. ((Current^.typ <> ait_Marker) Or
  972. (Pai_Marker(Current)^.Kind <> NoPropInfoStart)) Do
  973. Current := Pai(Current^.previous);
  974. End;
  975. Until Not(Assigned(Current)) Or
  976. (Current^.typ <> ait_Marker) Or
  977. (Pai_Marker(Current)^.Kind <> NoPropInfoStart);
  978. If Not(Assigned(Current)) or
  979. (Current^.typ In SkipInstr) or
  980. ((Current^.typ = ait_label) And
  981. Not(Pai_Label(Current)^.l^.is_used)) or
  982. ((Current^.typ = ait_Marker) And
  983. (Pai_Marker(Current)^.Kind = AsmBlockEnd))
  984. Then
  985. Begin
  986. Last := Nil;
  987. GetLastInstruction := False
  988. End
  989. Else
  990. Begin
  991. Last := Current;
  992. GetLastInstruction := True;
  993. End;
  994. End;
  995. Procedure SkipHead(var P: Pai);
  996. Var OldP: Pai;
  997. Begin
  998. Repeat
  999. OldP := P;
  1000. If (P^.typ in SkipInstr) Or
  1001. ((P^.typ = ait_marker) And
  1002. (Pai_Marker(P)^.Kind = AsmBlockEnd)) Then
  1003. GetNextInstruction(P, P)
  1004. Else If ((P^.Typ = Ait_Marker) And
  1005. (Pai_Marker(P)^.Kind = NoPropInfoStart)) Then
  1006. {a marker of the NoPropInfoStart can't be the first instruction of a
  1007. paasmoutput list}
  1008. GetNextInstruction(Pai(P^.Previous),P);
  1009. If (P^.Typ = Ait_Marker) And
  1010. (Pai_Marker(P)^.Kind = AsmBlockStart) Then
  1011. Begin
  1012. P := Pai(P^.Next);
  1013. While (P^.typ <> Ait_Marker) Or
  1014. (Pai_Marker(P)^.Kind <> AsmBlockEnd) Do
  1015. P := Pai(P^.Next)
  1016. End;
  1017. Until P = OldP
  1018. End;
  1019. {******************* The Data Flow Analyzer functions ********************}
  1020. Procedure UpdateUsedRegs(Var UsedRegs: TRegSet; p: Pai);
  1021. {updates UsedRegs with the RegAlloc Information coming after P}
  1022. Begin
  1023. Repeat
  1024. While Assigned(p) And
  1025. ((p^.typ in (SkipInstr - [ait_RegAlloc])) or
  1026. ((p^.typ = ait_label) And
  1027. Not(Pai_Label(p)^.l^.is_used))) Do
  1028. p := Pai(p^.next);
  1029. While Assigned(p) And
  1030. (p^.typ=ait_RegAlloc) Do
  1031. Begin
  1032. if pairegalloc(p)^.allocation then
  1033. UsedRegs := UsedRegs + [PaiRegAlloc(p)^.Reg]
  1034. else
  1035. UsedRegs := UsedRegs - [PaiRegAlloc(p)^.Reg];
  1036. p := pai(p^.next);
  1037. End;
  1038. Until Not(Assigned(p)) Or
  1039. (Not(p^.typ in SkipInstr) And
  1040. Not((p^.typ = ait_label) And
  1041. Not(Pai_Label(p)^.l^.is_used)));
  1042. End;
  1043. (*Function FindZeroreg(p: Pai; Var Result: TRegister): Boolean;
  1044. {Finds a register which contains the constant zero}
  1045. Var Counter: TRegister;
  1046. Begin
  1047. Counter := R_EAX;
  1048. FindZeroReg := True;
  1049. While (Counter <= R_EDI) And
  1050. ((PPaiProp(p^.OptInfo)^.Regs[Counter].Typ <> Con_Const) or
  1051. (PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod <> Pointer(0))) Do
  1052. Inc(Byte(Counter));
  1053. If (PPaiProp(p^.OptInfo)^.Regs[Counter].Typ = Con_Const) And
  1054. (PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod = Pointer(0))
  1055. Then Result := Counter
  1056. Else FindZeroReg := False;
  1057. End;*)
  1058. Function TCh2Reg(Ch: TChange): TRegister;
  1059. {converts a TChange variable to a TRegister}
  1060. Begin
  1061. If (Ch <= C_REDI) Then
  1062. TCh2Reg := TRegister(Byte(Ch))
  1063. Else
  1064. If (Ch <= C_WEDI) Then
  1065. TCh2Reg := TRegister(Byte(Ch) - Byte(C_REDI))
  1066. Else
  1067. If (Ch <= C_RWEDI) Then
  1068. TCh2Reg := TRegister(Byte(Ch) - Byte(C_WEDI))
  1069. Else InternalError($db)
  1070. End;
  1071. Procedure IncState(Var S: Byte);
  1072. {Increases S by 1, wraps around at $ffff to 0 (so we won't get overflow
  1073. errors}
  1074. Begin
  1075. If (s <> $ff)
  1076. Then Inc(s)
  1077. Else s := 0
  1078. End;
  1079. Function RegInSequence(Reg: TRegister; Const Content: TContent): Boolean;
  1080. {checks the whole sequence of Content (so StartMod and and the next NrOfMods
  1081. Pai objects) to see whether Reg is used somewhere, without it being loaded
  1082. with something else first}
  1083. Var p: Pai;
  1084. Counter: Byte;
  1085. TmpResult: Boolean;
  1086. RegsChecked: TRegSet;
  1087. Begin
  1088. RegsChecked := [];
  1089. p := Content.StartMod;
  1090. TmpResult := False;
  1091. Counter := 1;
  1092. While Not(TmpResult) And
  1093. (Counter <= Content.NrOfMods) Do
  1094. Begin
  1095. If (p^.typ = ait_instruction) and
  1096. ((Pai386(p)^.opcode = A_MOV) or
  1097. (Pai386(p)^.opcode = A_MOVZX) or
  1098. (Pai386(p)^.opcode = A_MOVSX))
  1099. Then
  1100. If (Pai386(p)^.oper[0].typ = top_ref)
  1101. Then
  1102. With Pai386(p)^.oper[0].ref^ Do
  1103. If (Base = ProcInfo.FramePointer) And
  1104. (Index = R_NO)
  1105. Then RegsChecked := RegsChecked + [Reg32(Pai386(p)^.oper[1].reg)]
  1106. Else
  1107. Begin
  1108. If (Base = Reg) And
  1109. Not(Base In RegsChecked)
  1110. Then TmpResult := True;
  1111. If Not(TmpResult) And
  1112. (Index = Reg) And
  1113. Not(Index In RegsChecked)
  1114. Then TmpResult := True;
  1115. End;
  1116. Inc(Counter);
  1117. GetNextInstruction(p,p)
  1118. End;
  1119. RegInSequence := TmpResult
  1120. End;
  1121. Procedure DestroyReg(p1: PPaiProp; Reg: TRegister);
  1122. {Destroys the contents of the register Reg in the PPaiProp p1, as well as the
  1123. contents of registers are loaded with a memory location based on Reg}
  1124. Var TmpWState, TmpRState: Byte;
  1125. Counter: TRegister;
  1126. Begin
  1127. Reg := Reg32(Reg);
  1128. NrOfInstrSinceLastMod[Reg] := 0;
  1129. If (Reg >= R_EAX) And (Reg <= R_EDI)
  1130. Then
  1131. Begin
  1132. With p1^.Regs[Reg] Do
  1133. Begin
  1134. IncState(WState);
  1135. TmpWState := WState;
  1136. TmpRState := RState;
  1137. FillChar(p1^.Regs[Reg], SizeOf(TContent), 0);
  1138. WState := TmpWState;
  1139. RState := TmpRState;
  1140. End;
  1141. For Counter := R_EAX to R_EDI Do
  1142. With p1^.Regs[Counter] Do
  1143. If (Typ = Con_Ref) And
  1144. RegInSequence(Reg, p1^.Regs[Counter])
  1145. Then
  1146. Begin
  1147. IncState(WState);
  1148. TmpWState := WState;
  1149. TmpRState := RState;
  1150. FillChar(p1^.Regs[Counter], SizeOf(TContent), 0);
  1151. WState := TmpWState;
  1152. RState := TmpRState;
  1153. End;
  1154. End;
  1155. End;
  1156. {Procedure AddRegsToSet(p: Pai; Var RegSet: TRegSet);
  1157. Begin
  1158. If (p^.typ = ait_instruction) Then
  1159. Begin
  1160. Case Pai386(p)^.oper[0].typ Of
  1161. top_reg:
  1162. If Not(Pai386(p)^.oper[0].reg in [R_NO,R_ESP,ProcInfo.FramePointer]) Then
  1163. RegSet := RegSet + [Pai386(p)^.oper[0].reg];
  1164. top_ref:
  1165. With TReference(Pai386(p)^.oper[0]^) Do
  1166. Begin
  1167. If Not(Base in [ProcInfo.FramePointer,R_NO,R_ESP])
  1168. Then RegSet := RegSet + [Base];
  1169. If Not(Index in [ProcInfo.FramePointer,R_NO,R_ESP])
  1170. Then RegSet := RegSet + [Index];
  1171. End;
  1172. End;
  1173. Case Pai386(p)^.oper[1].typ Of
  1174. top_reg:
  1175. If Not(Pai386(p)^.oper[1].reg in [R_NO,R_ESP,ProcInfo.FramePointer]) Then
  1176. If RegSet := RegSet + [TRegister(TwoWords(Pai386(p)^.oper[1]).Word1];
  1177. top_ref:
  1178. With TReference(Pai386(p)^.oper[1]^) Do
  1179. Begin
  1180. If Not(Base in [ProcInfo.FramePointer,R_NO,R_ESP])
  1181. Then RegSet := RegSet + [Base];
  1182. If Not(Index in [ProcInfo.FramePointer,R_NO,R_ESP])
  1183. Then RegSet := RegSet + [Index];
  1184. End;
  1185. End;
  1186. End;
  1187. End;}
  1188. Function OpsEquivalent(const o1, o2: toper; Var RegInfo: TRegInfo; OpAct: TopAction): Boolean;
  1189. Begin {checks whether the two ops are equivalent}
  1190. OpsEquivalent := False;
  1191. if o1.typ=o2.typ then
  1192. Case o1.typ Of
  1193. Top_Reg:
  1194. OpsEquivalent :=RegsEquivalent(o1.reg,o2.reg, RegInfo, OpAct);
  1195. Top_Ref:
  1196. OpsEquivalent := RefsEquivalent(o1.ref^, o2.ref^, RegInfo, OpAct);
  1197. Top_None:
  1198. OpsEquivalent := True
  1199. End;
  1200. End;
  1201. Function OpsEqual(const o1,o2:toper): Boolean;
  1202. Begin {checks whether the two ops are equal}
  1203. OpsEqual := False;
  1204. if o1.typ=o2.typ then
  1205. Case o1.typ Of
  1206. Top_Reg :
  1207. OpsEqual:=o1.reg=o2.reg;
  1208. Top_Ref :
  1209. OpsEqual := RefsEqual(o1.ref^, o2.ref^);
  1210. Top_Const :
  1211. OpsEqual:=o1.val=o2.val;
  1212. Top_Symbol :
  1213. OpsEqual:=(o1.sym=o2.sym) and (o1.symofs=o2.symofs);
  1214. Top_None :
  1215. OpsEqual := True
  1216. End;
  1217. End;
  1218. Function InstructionsEquivalent(p1, p2: Pai; Var RegInfo: TRegInfo): Boolean;
  1219. Begin {checks whether two Pai386 instructions are equal}
  1220. If Assigned(p1) And Assigned(p2) And
  1221. (Pai(p1)^.typ = ait_instruction) And
  1222. (Pai(p1)^.typ = ait_instruction) And
  1223. (Pai386(p1)^.opcode = Pai386(p2)^.opcode) And
  1224. (Pai386(p1)^.oper[0].typ = Pai386(p2)^.oper[0].typ) And
  1225. (Pai386(p1)^.oper[1].typ = Pai386(p2)^.oper[1].typ) And
  1226. (Pai386(p1)^.oper[2].typ = Pai386(p2)^.oper[2].typ)
  1227. Then
  1228. {both instructions have the same structure:
  1229. "<operator> <operand of type1>, <operand of type 2>"}
  1230. If ((Pai386(p1)^.opcode = A_MOV) or
  1231. (Pai386(p1)^.opcode = A_MOVZX) or
  1232. (Pai386(p1)^.opcode = A_MOVSX)) And
  1233. (Pai386(p1)^.oper[0].typ = top_ref) {then .oper[1]t = top_reg} Then
  1234. If Not(RegInRef(Pai386(p1)^.oper[1].reg, Pai386(p1)^.oper[0].ref^)) Then
  1235. {the "old" instruction is a load of a register with a new value, not with
  1236. a value based on the contents of this register (so no "mov (reg), reg")}
  1237. If Not(RegInRef(Pai386(p2)^.oper[1].reg, Pai386(p2)^.oper[0].ref^)) And
  1238. RefsEqual(Pai386(p1)^.oper[0].ref^, Pai386(p2)^.oper[0].ref^)
  1239. Then
  1240. {the "new" instruction is also a load of a register with a new value, and
  1241. this value is fetched from the same memory location}
  1242. Begin
  1243. With Pai386(p2)^.oper[0].ref^ Do
  1244. Begin
  1245. If Not(Base in [ProcInfo.FramePointer, R_NO, R_ESP])
  1246. {it won't do any harm if the register is already in RegsLoadedForRef}
  1247. Then RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef + [Base];
  1248. If Not(Index in [ProcInfo.FramePointer, R_NO, R_ESP])
  1249. Then RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef + [Index];
  1250. End;
  1251. {add the registers from the reference (.oper[0]) to the RegInfo, all registers
  1252. from the reference are the same in the old and in the new instruction
  1253. sequence}
  1254. AddOpRegInfo(Pai386(p1)^.oper[0], RegInfo);
  1255. {the registers from .oper[1] have to be equivalent, but not necessarily equal}
  1256. InstructionsEquivalent :=
  1257. RegsEquivalent(Pai386(p1)^.oper[1].reg, Pai386(p2)^.oper[1].reg, RegInfo, OpAct_Write);
  1258. End
  1259. {the registers are loaded with values from different memory locations. If
  1260. this was allowed, the instructions "mov -4(esi),eax" and "mov -4(ebp),eax"
  1261. would be considered equivalent}
  1262. Else InstructionsEquivalent := False
  1263. Else
  1264. {load register with a value based on the current value of this register}
  1265. Begin
  1266. With Pai386(p2)^.oper[0].ref^ Do
  1267. Begin
  1268. If Not(Base in [ProcInfo.FramePointer,
  1269. Reg32(Pai386(p2)^.oper[1].reg),R_NO,R_ESP])
  1270. {it won't do any harm if the register is already in RegsLoadedForRef}
  1271. Then
  1272. Begin
  1273. RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef + [Base];
  1274. {$ifdef csdebug}
  1275. Writeln(att_reg2str[base], ' added');
  1276. {$endif csdebug}
  1277. end;
  1278. If Not(Index in [ProcInfo.FramePointer,
  1279. Reg32(Pai386(p2)^.oper[1].reg),R_NO,R_ESP])
  1280. Then
  1281. Begin
  1282. RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef + [Index];
  1283. {$ifdef csdebug}
  1284. Writeln(att_reg2str[index], ' added');
  1285. {$endif csdebug}
  1286. end;
  1287. End;
  1288. If Not(Reg32(Pai386(p2)^.oper[1].reg) In [ProcInfo.FramePointer,R_NO,R_ESP])
  1289. Then
  1290. Begin
  1291. RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef -
  1292. [Reg32(Pai386(p2)^.oper[1].reg)];
  1293. {$ifdef csdebug}
  1294. Writeln(att_reg2str[Reg32(Pai386(p2)^.oper[1].reg)], ' removed');
  1295. {$endif csdebug}
  1296. end;
  1297. InstructionsEquivalent :=
  1298. OpsEquivalent(Pai386(p1)^.oper[0], Pai386(p2)^.oper[0], RegInfo, OpAct_Read) And
  1299. OpsEquivalent(Pai386(p1)^.oper[1], Pai386(p2)^.oper[1], RegInfo, OpAct_Write)
  1300. End
  1301. Else
  1302. {an instruction <> mov, movzx, movsx}
  1303. InstructionsEquivalent :=
  1304. OpsEquivalent(Pai386(p1)^.oper[0], Pai386(p2)^.oper[0], RegInfo, OpAct_Unknown) And
  1305. OpsEquivalent(Pai386(p1)^.oper[1], Pai386(p2)^.oper[1], RegInfo, OpAct_Unknown)
  1306. {the instructions haven't even got the same structure, so they're certainly
  1307. not equivalent}
  1308. Else
  1309. InstructionsEquivalent := False;
  1310. End;
  1311. (*
  1312. Function InstructionsEqual(p1, p2: Pai): Boolean;
  1313. Begin {checks whether two Pai386 instructions are equal}
  1314. InstructionsEqual :=
  1315. Assigned(p1) And Assigned(p2) And
  1316. ((Pai(p1)^.typ = ait_instruction) And
  1317. (Pai(p1)^.typ = ait_instruction) And
  1318. (Pai386(p1)^.opcode = Pai386(p2)^.opcode) And
  1319. (Pai386(p1)^.oper[0].typ = Pai386(p2)^.oper[0].typ) And
  1320. (Pai386(p1)^.oper[1].typ = Pai386(p2)^.oper[1].typ) And
  1321. OpsEqual(Pai386(p1)^.oper[0].typ, Pai386(p1)^.oper[0], Pai386(p2)^.oper[0]) And
  1322. OpsEqual(Pai386(p1)^.oper[1].typ, Pai386(p1)^.oper[1], Pai386(p2)^.oper[1]))
  1323. End;
  1324. *)
  1325. Function RefInInstruction(Const Ref: TReference; p: Pai): Boolean;
  1326. {checks whehter Ref is used in P}
  1327. Var TmpResult: Boolean;
  1328. Begin
  1329. TmpResult := False;
  1330. If (p^.typ = ait_instruction) Then
  1331. Begin
  1332. If (Pai386(p)^.oper[0].typ = Top_Ref) Then
  1333. TmpResult := RefsEqual(Ref, Pai386(p)^.oper[0].ref^);
  1334. If Not(TmpResult) And (Pai386(p)^.oper[1].typ = Top_Ref) Then
  1335. TmpResult := RefsEqual(Ref, Pai386(p)^.oper[1].ref^);
  1336. End;
  1337. RefInInstruction := TmpResult;
  1338. End;
  1339. Function RefInSequence(Const Ref: TReference; Content: TContent): Boolean;
  1340. {checks the whole sequence of Content (so StartMod and and the next NrOfMods
  1341. Pai objects) to see whether Ref is used somewhere}
  1342. Var p: Pai;
  1343. Counter: Byte;
  1344. TmpResult: Boolean;
  1345. Begin
  1346. p := Content.StartMod;
  1347. TmpResult := False;
  1348. Counter := 1;
  1349. While Not(TmpResult) And
  1350. (Counter <= Content.NrOfMods) Do
  1351. Begin
  1352. If (p^.typ = ait_instruction) And
  1353. RefInInstruction(Ref, p)
  1354. Then TmpResult := True;
  1355. Inc(Counter);
  1356. GetNextInstruction(p,p)
  1357. End;
  1358. RefInSequence := TmpResult
  1359. End;
  1360. Procedure DestroyRefs(p: pai; Const Ref: TReference; WhichReg: TRegister);
  1361. {destroys all registers which possibly contain a reference to Ref, WhichReg
  1362. is the register whose contents are being written to memory (if this proc
  1363. is called because of a "mov?? %reg, (mem)" instruction)}
  1364. Var Counter: TRegister;
  1365. Begin
  1366. WhichReg := Reg32(WhichReg);
  1367. If ((Ref.base = ProcInfo.FramePointer) And
  1368. (Ref.Index = R_NO)) Or
  1369. Assigned(Ref.Symbol)
  1370. Then
  1371. {write something to a parameter, a local or global variable, so
  1372. * with uncertzain optimizations on:
  1373. - destroy the contents of registers whose contents have somewhere a
  1374. "mov?? (Ref), %reg". WhichReg (this is the register whose contents
  1375. are being written to memory) is not destroyed if it's StartMod is
  1376. of that form and NrOfMods = 1 (so if it holds ref, but is not a
  1377. pointer based on Ref)
  1378. * with uncertain optimizations off:
  1379. - also destroy registers that contain any pointer}
  1380. For Counter := R_EAX to R_EDI Do
  1381. With PPaiProp(p^.OptInfo)^.Regs[Counter] Do
  1382. Begin
  1383. If (typ = Con_Ref) And
  1384. (Not(cs_UncertainOpts in aktglobalswitches) And
  1385. (NrOfMods <> 1)
  1386. ) Or
  1387. (RefInSequence(Ref,PPaiProp(p^.OptInfo)^.Regs[Counter]) And
  1388. ((Counter <> WhichReg) Or
  1389. ((NrOfMods = 1) And
  1390. {StarMod is always of the type ait_instruction}
  1391. (Pai386(StartMod)^.oper[0].typ = top_ref) And
  1392. RefsEqual(Pai386(StartMod)^.oper[0].ref^, Ref)
  1393. )
  1394. )
  1395. )
  1396. Then DestroyReg(PPaiProp(p^.OptInfo), Counter)
  1397. End
  1398. Else
  1399. {write something to a pointer location, so
  1400. * with uncertain optimzations on:
  1401. - do not destroy registers which contain a local/global variable or a
  1402. parameter, except if DestroyRefs is called because of a "movsl"
  1403. * with uncertain optimzations off:
  1404. - destroy every register which contains a memory location
  1405. }
  1406. For Counter := R_EAX to R_EDI Do
  1407. With PPaiProp(p^.OptInfo)^.Regs[Counter] Do
  1408. If (typ = Con_Ref) And
  1409. (Not(cs_UncertainOpts in aktglobalswitches) Or
  1410. {for movsl}
  1411. (Ref.Base = R_EDI) Or
  1412. {don't destroy if reg contains a parameter, local or global variable}
  1413. Not((NrOfMods = 1) And
  1414. (Pai386(StartMod)^.oper[0].typ = top_ref) And
  1415. ((Pai386(StartMod)^.oper[0].ref^.base = ProcInfo.FramePointer) Or
  1416. Assigned(Pai386(StartMod)^.oper[0].ref^.Symbol)
  1417. )
  1418. )
  1419. )
  1420. Then DestroyReg(PPaiProp(p^.OptInfo), Counter)
  1421. End;
  1422. Procedure DestroyAllRegs(p: PPaiProp);
  1423. Var Counter: TRegister;
  1424. Begin {initializes/desrtoys all registers}
  1425. For Counter := R_EAX To R_EDI Do
  1426. DestroyReg(p, Counter);
  1427. p^.DirFlag := F_Unknown;
  1428. End;
  1429. Procedure DestroyOp(PaiObj: Pai; const o:Toper);
  1430. Begin
  1431. Case o.typ Of
  1432. top_reg: DestroyReg(PPaiProp(PaiObj^.OptInfo), o.reg);
  1433. top_ref: DestroyRefs(PaiObj, o.ref^, R_NO);
  1434. top_symbol:;
  1435. End;
  1436. End;
  1437. Procedure ReadReg(p: PPaiProp; Reg: TRegister);
  1438. Begin
  1439. Reg := Reg32(Reg);
  1440. If Reg in [R_EAX..R_EDI] Then
  1441. IncState(p^.Regs[Reg32(Reg)].RState)
  1442. End;
  1443. Procedure ReadRef(p: PPaiProp; Ref: PReference);
  1444. Begin
  1445. If Ref^.Base <> R_NO Then
  1446. ReadReg(p, Ref^.Base);
  1447. If Ref^.Index <> R_NO Then
  1448. ReadReg(p, Ref^.Index);
  1449. End;
  1450. Procedure ReadOp(P: PPaiProp;const o:toper);
  1451. Begin
  1452. Case o.typ Of
  1453. top_reg: ReadReg(P, o.reg);
  1454. top_ref: ReadRef(P, o.ref);
  1455. top_symbol : ;
  1456. End;
  1457. End;
  1458. Function DFAPass1(AsmL: PAasmOutput; BlockStart: Pai): Pai;
  1459. {gathers the RegAlloc data... still need to think about where to store it to
  1460. avoid global vars}
  1461. Var BlockEnd: Pai;
  1462. Begin
  1463. BlockEnd := FindLoHiLabels(LoLab, HiLab, LabDif, BlockStart);
  1464. BuildLabelTableAndFixRegAlloc(AsmL, LTable, LoLab, LabDif, BlockStart, BlockEnd);
  1465. DFAPass1 := BlockEnd;
  1466. End;
  1467. Procedure DoDFAPass2(
  1468. {$Ifdef StateDebug}
  1469. AsmL: PAasmOutput;
  1470. {$endif statedebug}
  1471. BlockStart, BlockEnd: Pai);
  1472. {Analyzes the Data Flow of an assembler list. Starts creating the reg
  1473. contents for the instructions starting with p. Returns the last pai which has
  1474. been processed}
  1475. Var
  1476. CurProp: PPaiProp;
  1477. {$ifdef AnalyzeLoops}
  1478. TmpState: Byte;
  1479. {$endif AnalyzeLoops}
  1480. Cnt, InstrCnt : Longint;
  1481. InstrProp: TAsmInstrucProp;
  1482. UsedRegs: TRegSet;
  1483. p, hp : Pai;
  1484. TmpRef: TReference;
  1485. TmpReg: TRegister;
  1486. Begin
  1487. p := BlockStart;
  1488. UsedRegs := [];
  1489. UpdateUsedregs(UsedRegs, p);
  1490. SkipHead(P);
  1491. BlockStart := p;
  1492. InstrCnt := 1;
  1493. FillChar(NrOfInstrSinceLastMod, SizeOf(NrOfInstrSinceLastMod), 0);
  1494. While (P <> BlockEnd) Do
  1495. Begin
  1496. {$IfDef TP}
  1497. New(CurProp);
  1498. {$Else TP}
  1499. CurProp := @PaiPropBlock^[InstrCnt];
  1500. {$EndIf TP}
  1501. If (p <> BlockStart)
  1502. Then
  1503. Begin
  1504. {$ifdef JumpAnal}
  1505. If (p^.Typ <> ait_label) Then
  1506. {$endif JumpAnal}
  1507. Begin
  1508. GetLastInstruction(p, hp);
  1509. CurProp^.Regs := PPaiProp(hp^.OptInfo)^.Regs;
  1510. CurProp^.DirFlag := PPaiProp(hp^.OptInfo)^.DirFlag;
  1511. End
  1512. End
  1513. Else
  1514. Begin
  1515. FillChar(CurProp^, SizeOf(CurProp^), 0);
  1516. { For TmpReg := R_EAX to R_EDI Do
  1517. CurProp^.Regs[TmpReg].WState := 1;}
  1518. End;
  1519. CurProp^.UsedRegs := UsedRegs;
  1520. CurProp^.CanBeRemoved := False;
  1521. UpdateUsedRegs(UsedRegs, Pai(p^.Next));
  1522. {$ifdef TP}
  1523. PPaiProp(p^.OptInfo) := CurProp;
  1524. {$Endif TP}
  1525. For TmpReg := R_EAX To R_EDI Do
  1526. Inc(NrOfInstrSinceLastMod[TmpReg]);
  1527. Case p^.typ Of
  1528. ait_label:
  1529. {$Ifndef JumpAnal}
  1530. If (Pai_label(p)^.l^.is_used) Then
  1531. DestroyAllRegs(CurProp);
  1532. {$Else JumpAnal}
  1533. Begin
  1534. If (Pai_Label(p)^.is_used) Then
  1535. With LTable^[Pai_Label(p)^.l^.labelnr-LoLab] Do
  1536. {$IfDef AnalyzeLoops}
  1537. If (RefsFound = Pai_Label(p)^.l^.RefCount)
  1538. {$Else AnalyzeLoops}
  1539. If (JmpsProcessed = Pai_Label(p)^.l^.RefCount)
  1540. {$EndIf AnalyzeLoops}
  1541. Then
  1542. {all jumps to this label have been found}
  1543. {$IfDef AnalyzeLoops}
  1544. If (JmpsProcessed > 0)
  1545. Then
  1546. {$EndIf AnalyzeLoops}
  1547. {we've processed at least one jump to this label}
  1548. Begin
  1549. If (GetLastInstruction(p, hp) And
  1550. Not(((hp^.typ = ait_instruction)) And
  1551. (pai386_labeled(hp)^.is_jmp))
  1552. Then
  1553. {previous instruction not a JMP -> the contents of the registers after the
  1554. previous intruction has been executed have to be taken into account as well}
  1555. For TmpReg := R_EAX to R_EDI Do
  1556. Begin
  1557. If (CurProp^.Regs[TmpReg].WState <>
  1558. PPaiProp(hp^.OptInfo)^.Regs[TmpReg].WState)
  1559. Then DestroyReg(CurProp, TmpReg)
  1560. End
  1561. End
  1562. {$IfDef AnalyzeLoops}
  1563. Else
  1564. {a label from a backward jump (e.g. a loop), no jump to this label has
  1565. already been processed}
  1566. If GetLastInstruction(p, hp) And
  1567. Not(hp^.typ = ait_instruction) And
  1568. (pai386_labeled(hp)^.opcode = A_JMP))
  1569. Then
  1570. {previous instruction not a jmp, so keep all the registers' contents from the
  1571. previous instruction}
  1572. Begin
  1573. CurProp^.Regs := PPaiProp(hp^.OptInfo)^.Regs;
  1574. CurProp^.DirFlag := PPaiProp(hp^.OptInfo)^.DirFlag;
  1575. End
  1576. Else
  1577. {previous instruction a jmp and no jump to this label processed yet}
  1578. Begin
  1579. hp := p;
  1580. Cnt := InstrCnt;
  1581. {continue until we find a jump to the label or a label which has already
  1582. been processed}
  1583. While GetNextInstruction(hp, hp) And
  1584. Not((hp^.typ = ait_instruction) And
  1585. (pai386(hp)^.is_jmp) and
  1586. (pasmlabel(pai386(hp)^.oper[0].sym)^.labelnr = Pai_Label(p)^.l^.labelnr)) And
  1587. Not((hp^.typ = ait_label) And
  1588. (LTable^[Pai_Label(hp)^.l^.labelnr-LoLab].RefsFound
  1589. = Pai_Label(hp)^.l^.RefCount) And
  1590. (LTable^[Pai_Label(hp)^.l^.labelnr-LoLab].JmpsProcessed > 0)) Do
  1591. Inc(Cnt);
  1592. If (hp^.typ = ait_label)
  1593. Then
  1594. {there's a processed label after the current one}
  1595. Begin
  1596. CurProp^.Regs := PaiPropBlock^[Cnt].Regs;
  1597. CurProp^.DirFlag := PaiPropBlock^[Cnt].DirFlag;
  1598. End
  1599. Else
  1600. {there's no label anymore after the current one, or they haven't been
  1601. processed yet}
  1602. Begin
  1603. GetLastInstruction(p, hp);
  1604. CurProp^.Regs := PPaiProp(hp^.OptInfo)^.Regs;
  1605. CurProp^.DirFlag := PPaiProp(hp^.OptInfo)^.DirFlag;
  1606. DestroyAllRegs(PPaiProp(hp^.OptInfo))
  1607. End
  1608. End
  1609. {$EndIf AnalyzeLoops}
  1610. Else
  1611. {not all references to this label have been found, so destroy all registers}
  1612. Begin
  1613. GetLastInstruction(p, hp);
  1614. CurProp^.Regs := PPaiProp(hp^.OptInfo)^.Regs;
  1615. CurProp^.DirFlag := PPaiProp(hp^.OptInfo)^.DirFlag;
  1616. DestroyAllRegs(CurProp)
  1617. End;
  1618. End;
  1619. {$EndIf JumpAnal}
  1620. {$ifdef GDB}
  1621. ait_stabs, ait_stabn, ait_stab_function_name:;
  1622. {$endif GDB}
  1623. ait_instruction:
  1624. Begin
  1625. if pai386(p)^.is_jmp then
  1626. begin
  1627. {$IfNDef JumpAnal}
  1628. ;
  1629. {$Else JumpAnal}
  1630. With LTable^[pasmlabel(pai386(p)^.oper[0].sym)^.labelnr-LoLab] Do
  1631. If (RefsFound = pasmlabel(pai386(p)^.oper[0].sym)^.RefCount) Then
  1632. Begin
  1633. If (InstrCnt < InstrNr)
  1634. Then
  1635. {forward jump}
  1636. If (JmpsProcessed = 0) Then
  1637. {no jump to this label has been processed yet}
  1638. Begin
  1639. PaiPropBlock^[InstrNr].Regs := CurProp^.Regs;
  1640. PaiPropBlock^[InstrNr].DirFlag := CurProp^.DirFlag;
  1641. Inc(JmpsProcessed);
  1642. End
  1643. Else
  1644. Begin
  1645. For TmpReg := R_EAX to R_EDI Do
  1646. If (PaiPropBlock^[InstrNr].Regs[TmpReg].WState <>
  1647. CurProp^.Regs[TmpReg].WState) Then
  1648. DestroyReg(@PaiPropBlock^[InstrNr], TmpReg);
  1649. Inc(JmpsProcessed);
  1650. End
  1651. {$ifdef AnalyzeLoops}
  1652. Else
  1653. { backward jump, a loop for example}
  1654. { If (JmpsProcessed > 0) Or
  1655. Not(GetLastInstruction(PaiObj, hp) And
  1656. (hp^.typ = ait_labeled_instruction) And
  1657. (pai386_labeled(hp)^.opcode = A_JMP))
  1658. Then}
  1659. {instruction prior to label is not a jmp, or at least one jump to the label
  1660. has yet been processed}
  1661. Begin
  1662. Inc(JmpsProcessed);
  1663. For TmpReg := R_EAX to R_EDI Do
  1664. If (PaiPropBlock^[InstrNr].Regs[TmpReg].WState <>
  1665. CurProp^.Regs[TmpReg].WState)
  1666. Then
  1667. Begin
  1668. TmpState := PaiPropBlock^[InstrNr].Regs[TmpReg].WState;
  1669. Cnt := InstrNr;
  1670. While (TmpState = PaiPropBlock^[Cnt].Regs[TmpReg].WState) Do
  1671. Begin
  1672. DestroyReg(@PaiPropBlock^[Cnt], TmpReg);
  1673. Inc(Cnt);
  1674. End;
  1675. While (Cnt <= InstrCnt) Do
  1676. Begin
  1677. Inc(PaiPropBlock^[Cnt].Regs[TmpReg].WState);
  1678. Inc(Cnt)
  1679. End
  1680. End;
  1681. End
  1682. { Else }
  1683. {instruction prior to label is a jmp and no jumps to the label have yet been
  1684. processed}
  1685. { Begin
  1686. Inc(JmpsProcessed);
  1687. For TmpReg := R_EAX to R_EDI Do
  1688. Begin
  1689. TmpState := PaiPropBlock^[InstrNr].Regs[TmpReg].WState;
  1690. Cnt := InstrNr;
  1691. While (TmpState = PaiPropBlock^[Cnt].Regs[TmpReg].WState) Do
  1692. Begin
  1693. PaiPropBlock^[Cnt].Regs[TmpReg] := CurProp^.Regs[TmpReg];
  1694. Inc(Cnt);
  1695. End;
  1696. TmpState := PaiPropBlock^[InstrNr].Regs[TmpReg].WState;
  1697. While (TmpState = PaiPropBlock^[Cnt].Regs[TmpReg].WState) Do
  1698. Begin
  1699. DestroyReg(@PaiPropBlock^[Cnt], TmpReg);
  1700. Inc(Cnt);
  1701. End;
  1702. While (Cnt <= InstrCnt) Do
  1703. Begin
  1704. Inc(PaiPropBlock^[Cnt].Regs[TmpReg].WState);
  1705. Inc(Cnt)
  1706. End
  1707. End
  1708. End}
  1709. {$endif AnalyzeLoops}
  1710. End;
  1711. {$EndIf JumpAnal}
  1712. end
  1713. else
  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. End
  1884. Else
  1885. Begin
  1886. DestroyAllRegs(CurProp);
  1887. End;
  1888. End;
  1889. Inc(InstrCnt);
  1890. GetNextInstruction(p, p);
  1891. End;
  1892. End;
  1893. Function InitDFAPass2(BlockStart, BlockEnd: Pai): Boolean;
  1894. {reserves memory for the PPaiProps in one big memory block when not using
  1895. TP, returns False if not enough memory is available for the optimizer in all
  1896. cases}
  1897. Var p: Pai;
  1898. Count: Longint;
  1899. { TmpStr: String; }
  1900. Begin
  1901. P := BlockStart;
  1902. SkipHead(P);
  1903. NrOfPaiObjs := 0;
  1904. While (P <> BlockEnd) Do
  1905. Begin
  1906. {$IfDef JumpAnal}
  1907. Case P^.Typ Of
  1908. ait_label:
  1909. Begin
  1910. If (Pai_Label(p)^.l^.is_used) Then
  1911. LTable^[Pai_Label(P)^.l^.labelnr-LoLab].InstrNr := NrOfPaiObjs
  1912. End;
  1913. ait_instruction:
  1914. begin
  1915. if pai386(p)^.is_jmp then
  1916. begin
  1917. If (pasmlabel(pai386(P)^.oper[0].sym)^.labelnr >= LoLab) And
  1918. (pasmlabel(pai386(P)^.oper[0].sym)^.labelnr <= HiLab) Then
  1919. Inc(LTable^[pasmlabel(pai386(P)^.oper[0].sym)^.labelnr-LoLab].RefsFound);
  1920. end;
  1921. end;
  1922. { ait_instruction:
  1923. Begin
  1924. If (Pai386(p)^.opcode = A_PUSH) And
  1925. (Pai386(p)^.oper[0].typ = top_symbol) And
  1926. (PCSymbol(Pai386(p)^.oper[0])^.offset = 0) Then
  1927. Begin
  1928. TmpStr := StrPas(PCSymbol(Pai386(p)^.oper[0])^.symbol);
  1929. If}
  1930. End;
  1931. {$EndIf JumpAnal}
  1932. Inc(NrOfPaiObjs);
  1933. GetNextInstruction(p, p);
  1934. End;
  1935. {$IfDef VER70}
  1936. If (MemAvail < (SizeOf(TPaiProp)*NrOfPaiObjs))
  1937. Or (NrOfPaiObjs = 0)
  1938. {this doesn't have to be one contiguous block}
  1939. Then InitDFAPass2 := False
  1940. Else InitDFAPass2 := True;
  1941. {$Else}
  1942. {Uncomment the next line to see how much memory the reloading optimizer needs}
  1943. { Writeln((NrOfPaiObjs*(((SizeOf(TPaiProp)+3)div 4)*4)));}
  1944. {no need to check mem/maxavail, we've got as much virtual memory as we want}
  1945. If NrOfPaiObjs <> 0 Then
  1946. Begin
  1947. InitDFAPass2 := True;
  1948. GetMem(PaiPropBlock, NrOfPaiObjs*(((SizeOf(TPaiProp)+3)div 4)*4));
  1949. p := BlockStart;
  1950. SkipHead(p);
  1951. For Count := 1 To NrOfPaiObjs Do
  1952. Begin
  1953. PPaiProp(p^.OptInfo) := @PaiPropBlock^[Count];
  1954. GetNextInstruction(p, p);
  1955. End;
  1956. End
  1957. Else InitDFAPass2 := False;
  1958. {$EndIf TP}
  1959. End;
  1960. Function DFAPass2(
  1961. {$ifdef statedebug}
  1962. AsmL: PAasmOutPut;
  1963. {$endif statedebug}
  1964. BlockStart, BlockEnd: Pai): Boolean;
  1965. Begin
  1966. If InitDFAPass2(BlockStart, BlockEnd) Then
  1967. Begin
  1968. DoDFAPass2(
  1969. {$ifdef statedebug}
  1970. asml,
  1971. {$endif statedebug}
  1972. BlockStart, BlockEnd);
  1973. DFAPass2 := True
  1974. End
  1975. Else DFAPass2 := False;
  1976. End;
  1977. Procedure ShutDownDFA;
  1978. Begin
  1979. If LabDif <> 0 Then
  1980. FreeMem(LTable, LabDif*SizeOf(TLabelTableItem));
  1981. End;
  1982. End.
  1983. {
  1984. $Log$
  1985. Revision 1.49 1999-07-18 14:47:23 florian
  1986. * bug 487 fixed, (inc(<property>) isn't allowed)
  1987. * more fixes to compile with Delphi
  1988. Revision 1.48 1999/07/01 18:21:21 jonas
  1989. * removed unused AsmL parameter from FindLoHiLabels
  1990. Revision 1.47 1999/05/27 19:44:24 peter
  1991. * removed oldasm
  1992. * plabel -> pasmlabel
  1993. * -a switches to source writing automaticly
  1994. * assembler readers OOPed
  1995. * asmsymbol automaticly external
  1996. * jumptables and other label fixes for asm readers
  1997. Revision 1.46 1999/05/08 20:40:02 jonas
  1998. * seperate OPTimizer INFO pointer field in tai object
  1999. * fix to GetLastInstruction that sometimes caused a crash
  2000. Revision 1.45 1999/05/01 13:48:37 peter
  2001. * merged nasm compiler
  2002. Revision 1.6 1999/04/18 17:57:21 jonas
  2003. * fix for crash when the first instruction of a sequence that gets
  2004. optimized is removed (this situation can't occur aymore now)
  2005. Revision 1.5 1999/04/16 11:49:50 peter
  2006. + tempalloc
  2007. + -at to show temp alloc info in .s file
  2008. Revision 1.4 1999/04/14 09:07:42 peter
  2009. * asm reader improvements
  2010. Revision 1.3 1999/03/31 13:55:29 peter
  2011. * assembler inlining working for ag386bin
  2012. Revision 1.2 1999/03/29 16:05:46 peter
  2013. * optimizer working for ag386bin
  2014. Revision 1.1 1999/03/26 00:01:10 peter
  2015. * first things for optimizer (compiles but cycle crashes)
  2016. Revision 1.39 1999/02/26 00:48:18 peter
  2017. * assembler writers fixed for ag386bin
  2018. Revision 1.38 1999/02/25 21:02:34 peter
  2019. * ag386bin updates
  2020. + coff writer
  2021. Revision 1.37 1999/02/22 02:15:20 peter
  2022. * updates for ag386bin
  2023. Revision 1.36 1999/01/20 17:41:26 jonas
  2024. * small bugfix (memory corruption could occur when certain fpu instructions
  2025. were encountered)
  2026. Revision 1.35 1999/01/08 12:39:22 florian
  2027. Changes of Alexander Stohr integrated:
  2028. + added KNI opcodes
  2029. + added KNI registers
  2030. + added 3DNow! opcodes
  2031. + added 64 bit and 128 bit register flags
  2032. * translated a few comments into english
  2033. Revision 1.34 1998/12/29 18:48:19 jonas
  2034. + optimize pascal code surrounding assembler blocks
  2035. Revision 1.33 1998/12/17 16:37:38 jonas
  2036. + extra checks in RegsEquivalent so some more optimizations can be done (which
  2037. where disabled by the second fix from revision 1.22)
  2038. Revision 1.32 1998/12/15 19:33:58 jonas
  2039. * uncommented OpsEqual & added to interface because popt386 uses it now
  2040. Revision 1.31 1998/12/11 00:03:13 peter
  2041. + globtype,tokens,version unit splitted from globals
  2042. Revision 1.30 1998/12/02 16:23:39 jonas
  2043. * changed "if longintvar in set" to case or "if () or () .." statements
  2044. * tree.pas: changed inlinenumber (and associated constructor/vars) to a byte
  2045. Revision 1.29 1998/11/26 21:45:31 jonas
  2046. - removed A_CLTD opcode (use A_CDQ instead)
  2047. * changed cbw, cwde and cwd to cbtw, cwtl and cwtd in att_.oper[1]str array
  2048. * in daopt386: adapted AsmInstr array to reflect changes + fixed line too long
  2049. Revision 1.27 1998/11/24 19:47:22 jonas
  2050. * fixed problems posible with 3 operand instructions
  2051. Revision 1.26 1998/11/24 12:50:09 peter
  2052. * fixed crash
  2053. Revision 1.25 1998/11/18 17:58:22 jonas
  2054. + gathering of register reading data, nowhere used yet (necessary for instruction scheduling)
  2055. Revision 1.24 1998/11/13 10:13:44 peter
  2056. + cpuid,emms support for asm readers
  2057. Revision 1.23 1998/11/09 19:40:46 jonas
  2058. * fixed comments from last commit (apparently there's still a 255 char limit :( )
  2059. Revision 1.22 1998/11/09 19:33:40 jonas
  2060. * changed specific bugfix (which was actually wrong implemented, but
  2061. did the right thing in most cases nevertheless) to general bugfix
  2062. * fixed bug that caused
  2063. mov (ebp), edx mov (ebp), edx
  2064. mov (edx), edx mov (edx), edx
  2065. ... being changed to ...
  2066. mov (ebp), edx mov edx, eax
  2067. mov (eax), eax
  2068. but this disabled another small correct optimization...
  2069. Revision 1.21 1998/11/02 23:17:49 jonas
  2070. * fixed bug shown in sortbug program from fpc-devel list
  2071. Revision 1.20 1998/10/22 13:24:51 jonas
  2072. * changed TRegSet to a small set
  2073. Revision 1.19 1998/10/20 09:29:24 peter
  2074. * bugfix so that code like
  2075. movl 48(%esi),%esi movl 48(%esi),%esi
  2076. pushl %esi doesn't get changed to pushl %esi
  2077. movl 48(%esi),%edi movl %esi,%edi
  2078. Revision 1.18 1998/10/07 16:27:02 jonas
  2079. * changed state to WState (WriteState), added RState for future use in
  2080. instruction scheduling
  2081. * RegAlloc data from the CG is now completely being patched and corrected (I
  2082. think)
  2083. Revision 1.17 1998/10/02 17:30:20 jonas
  2084. * small patches to regdealloc data
  2085. Revision 1.16 1998/10/01 20:21:47 jonas
  2086. * inter-register CSE, still requires some tweaks (peepholeoptpass2, better RegAlloc)
  2087. Revision 1.15 1998/09/20 18:00:20 florian
  2088. * small compiling problems fixed
  2089. Revision 1.14 1998/09/20 17:12:36 jonas
  2090. * small fix for uncertain optimizations & more cleaning up
  2091. Revision 1.12 1998/09/16 18:00:01 jonas
  2092. * optimizer now completely dependant on GetNext/GetLast instruction, works again with -dRegAlloc
  2093. Revision 1.11 1998/09/15 14:05:27 jonas
  2094. * fixed optimizer incompatibilities with freelabel code in psub
  2095. Revision 1.10 1998/09/09 15:33:58 peter
  2096. * removed warnings
  2097. Revision 1.9 1998/09/03 16:24:51 florian
  2098. * bug of type conversation from dword to real fixed
  2099. * bug fix of Jonas applied
  2100. Revision 1.8 1998/08/28 10:56:59 peter
  2101. * removed warnings
  2102. Revision 1.7 1998/08/19 16:07:44 jonas
  2103. * changed optimizer switches + cleanup of DestroyRefs in daopt386.pas
  2104. Revision 1.6 1998/08/10 14:49:57 peter
  2105. + localswitches, moduleswitches, globalswitches splitting
  2106. Revision 1.5 1998/08/09 13:56:24 jonas
  2107. * small bugfix for uncertain optimizations in DestroyRefs
  2108. Revision 1.4 1998/08/06 19:40:25 jonas
  2109. * removed $ before and after Log in comment
  2110. Revision 1.3 1998/08/05 16:00:14 florian
  2111. * some fixes for ansi strings
  2112. * log to Log changed
  2113. }