daopt386.pas 57 KB

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