daopt386.pas 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  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. Cnt, InstrCnt, TmpState: Longint;
  917. InstrProp: TAsmInstrucProp;
  918. p, hp: Pai;
  919. TmpRef: TReference;
  920. TmpReg: TRegister;
  921. Begin
  922. p := First;
  923. InstrCnt := 1;
  924. FillChar(NrOfInstrSinceLastMod, SizeOf(NrOfInstrSinceLastMod), 0);
  925. While Assigned(p) Do
  926. Begin
  927. DoDFAPass2 := p;
  928. {$IfDef TP}
  929. If (InstrCnt <= NrOfPaiFast) Then
  930. {$EndIf TP}
  931. CurProp := @PaiPropBlock^[InstrCnt]
  932. {$IfDef TP}
  933. Else New(CurProp)
  934. {$EndIf TP}
  935. ;
  936. If (p <> First)
  937. Then
  938. {$ifndef TP}
  939. Begin
  940. If (p^.Typ <> ait_label) Then
  941. {$endif TP}
  942. Begin
  943. CurProp^.Regs := PPaiProp(Pai(p^.previous)^.fileinfo.line)^.Regs;
  944. CurProp^.DirFlag := PPaiProp(Pai(p^.previous)^.fileinfo.line)^.DirFlag
  945. End
  946. {$ifndef TP}
  947. End
  948. {$endif TP}
  949. Else
  950. Begin
  951. FillChar(CurProp^, SizeOf(CurProp^), 0);
  952. { For TmpReg := R_EAX to R_EDI Do
  953. CurProp^.Regs[TmpReg].State := 1;}
  954. End;
  955. CurProp^.CanBeRemoved := False;
  956. {$IfDef TP}
  957. CurProp^.linesave := p^.fileinfo.line;
  958. PPaiProp(p^.fileinfo.line) := CurProp;
  959. {$EndIf}
  960. For TmpReg := R_EAX To R_EDI Do
  961. Inc(NrOfInstrSinceLastMod[TmpReg]);
  962. Case p^.typ Of
  963. ait_label:
  964. {$Ifdef TP}
  965. DestroyAllRegs(CurProp);
  966. {$Else TP}
  967. Begin
  968. With LTable^[Pai_Label(p)^.l^.nb-LoLab] Do
  969. {$IfDef AnalyzeLoops}
  970. If (RefsFound = Pai_Label(p)^.l^.RefCount)
  971. {$Else AnalyzeLoops}
  972. If (JmpsProcessed = Pai_Label(p)^.l^.RefCount)
  973. {$EndIf AnalyzeLoops}
  974. Then
  975. {all jumps to this label have been found}
  976. {$IfDef AnalyzeLoops}
  977. If (JmpsProcessed > 0)
  978. Then
  979. {$EndIf}
  980. {we've processed at least one jump to this label}
  981. Begin
  982. If Not(GetLastInstruction(p, hp) And
  983. (hp^.typ = ait_labeled_instruction) And
  984. (Pai_Labeled(hp)^._operator = A_JMP))
  985. Then
  986. {previous instruction not a JMP -> the contents of the registers after the
  987. previous intruction has been executed have to be taken into account as well}
  988. For TmpReg := R_EAX to R_EDI Do
  989. Begin
  990. If (CurProp^.Regs[TmpReg].State <>
  991. PPaiProp(Pai(p^.Previous)^.FileInfo.Line)^.Regs[TmpReg].State)
  992. Then DestroyReg(CurProp, TmpReg)
  993. End
  994. End
  995. {$IfDef AnalyzeLoops}
  996. Else
  997. {a label from a backward jump (e.g. a loop), no jump to this label has
  998. already been processed}
  999. If Not(GetLastInstruction(p, hp) And
  1000. (hp^.typ = ait_labeled_instruction) And
  1001. (Pai_Labeled(hp)^._operator = A_JMP))
  1002. Then
  1003. {previous instruction not a jmp, so keep all the registers' contents from the
  1004. previous instruction}
  1005. Begin
  1006. CurProp^.Regs := PPaiProp(Pai(p^.Previous)^.FileInfo.Line)^.Regs;
  1007. CurProp^.DirFlag := PPaiProp(Pai(p^.Previous)^.FileInfo.Line)^.DirFlag;
  1008. End
  1009. Else
  1010. {previous instruction a jmp and no jump to this label processed yet}
  1011. Begin
  1012. hp := p;
  1013. Cnt := InstrCnt;
  1014. {continue until we find a jump to the label or a label which has already
  1015. been processed}
  1016. While GetNextInstruction(hp, hp) And
  1017. Not((hp^.typ = ait_labeled_instruction) And
  1018. (Pai_Labeled(hp)^.lab^.nb = Pai_Label(p)^.l^.nb)) And
  1019. Not((hp^.typ = ait_label) And
  1020. (LTable^[Pai_Label(hp)^.l^.nb-LoLab].RefsFound
  1021. = Pai_Label(hp)^.l^.RefCount) And
  1022. (LTable^[Pai_Label(hp)^.l^.nb-LoLab].JmpsProcessed > 0)) Do
  1023. Inc(Cnt);
  1024. If (hp^.typ = ait_label)
  1025. Then
  1026. {there's a processed label after the current one}
  1027. Begin
  1028. CurProp^.Regs := PaiPropBlock^[Cnt].Regs;
  1029. CurProp^.DirFlag := PaiPropBlock^[Cnt].DirFlag;
  1030. End
  1031. Else
  1032. {there's no label anymore after the current one, or they haven't been
  1033. processed yet}
  1034. Begin
  1035. CurProp^.Regs := PPaiProp(Pai(p^.Previous)^.FileInfo.Line)^.Regs;
  1036. CurProp^.DirFlag := PPaiProp(Pai(p^.Previous)^.FileInfo.Line)^.DirFlag;
  1037. DestroyAllRegs(PPaiProp(Pai(p^.Previous)^.FileInfo.Line))
  1038. End
  1039. End
  1040. {$EndIf AnalyzeLoops}
  1041. Else
  1042. {not all references to this label have been found, so destroy all registers}
  1043. Begin
  1044. CurProp^.Regs := PPaiProp(Pai(p^.Previous)^.FileInfo.Line)^.Regs;
  1045. CurProp^.DirFlag := PPaiProp(Pai(p^.Previous)^.FileInfo.Line)^.DirFlag;
  1046. DestroyAllRegs(CurProp)
  1047. End;
  1048. End;
  1049. {$EndIf TP}
  1050. ait_labeled_instruction:
  1051. {$IfDef TP}
  1052. ;
  1053. {$Else TP}
  1054. With LTable^[Pai_Labeled(p)^.lab^.nb-LoLab] Do
  1055. If (RefsFound = Pai_Labeled(p)^.lab^.RefCount) Then
  1056. Begin
  1057. If (InstrCnt < InstrNr)
  1058. Then
  1059. {forward jump}
  1060. If (JmpsProcessed = 0) Then
  1061. {no jump to this label has been processed yet}
  1062. Begin
  1063. PaiPropBlock^[InstrNr].Regs := CurProp^.Regs;
  1064. PaiPropBlock^[InstrNr].DirFlag := CurProp^.DirFlag;
  1065. Inc(JmpsProcessed);
  1066. End
  1067. Else
  1068. Begin
  1069. For TmpReg := R_EAX to R_EDI Do
  1070. If (PaiPropBlock^[InstrNr].Regs[TmpReg].State <>
  1071. CurProp^.Regs[TmpReg].State) Then
  1072. DestroyReg(@PaiPropBlock^[InstrNr], TmpReg);
  1073. Inc(JmpsProcessed);
  1074. End
  1075. {$ifdef AnalyzeLoops}
  1076. Else
  1077. {backward jump, a loop for example}
  1078. { If (JmpsProcessed > 0) Or
  1079. Not(GetLastInstruction(PaiObj, hp) And
  1080. (hp^.typ = ait_labeled_instruction) And
  1081. (Pai_Labeled(hp)^._operator = A_JMP))
  1082. Then}
  1083. {instruction prior to label is not a jmp, or at least one jump to the label
  1084. has yet been processed}
  1085. Begin
  1086. Inc(JmpsProcessed);
  1087. For TmpReg := R_EAX to R_EDI Do
  1088. If (PaiPropBlock^[InstrNr].Regs[TmpReg].State <>
  1089. CurProp^.Regs[TmpReg].State)
  1090. Then
  1091. Begin
  1092. TmpState := PaiPropBlock^[InstrNr].Regs[TmpReg].State;
  1093. Cnt := InstrNr;
  1094. While (TmpState = PaiPropBlock^[Cnt].Regs[TmpReg].State) Do
  1095. Begin
  1096. DestroyReg(@PaiPropBlock^[Cnt], TmpReg);
  1097. Inc(Cnt);
  1098. End;
  1099. While (Cnt <= InstrCnt) Do
  1100. Begin
  1101. Inc(PaiPropBlock^[Cnt].Regs[TmpReg].State);
  1102. Inc(Cnt)
  1103. End
  1104. End;
  1105. End
  1106. { Else
  1107. {instruction prior to label is a jmp and no jumps to the label have yet been
  1108. processed}
  1109. Begin
  1110. Inc(JmpsProcessed);
  1111. For TmpReg := R_EAX to R_EDI Do
  1112. Begin
  1113. TmpState := PaiPropBlock^[InstrNr].Regs[TmpReg].State;
  1114. Cnt := InstrNr;
  1115. While (TmpState = PaiPropBlock^[Cnt].Regs[TmpReg].State) Do
  1116. Begin
  1117. PaiPropBlock^[Cnt].Regs[TmpReg] := CurProp^.Regs[TmpReg];
  1118. Inc(Cnt);
  1119. End;
  1120. TmpState := PaiPropBlock^[InstrNr].Regs[TmpReg].State;
  1121. While (TmpState = PaiPropBlock^[Cnt].Regs[TmpReg].State) Do
  1122. Begin
  1123. DestroyReg(@PaiPropBlock^[Cnt], TmpReg);
  1124. Inc(Cnt);
  1125. End;
  1126. While (Cnt <= InstrCnt) Do
  1127. Begin
  1128. Inc(PaiPropBlock^[Cnt].Regs[TmpReg].State);
  1129. Inc(Cnt)
  1130. End
  1131. End
  1132. End}
  1133. {$endif AnalyzeLoops}
  1134. End;
  1135. {$EndIf TP}
  1136. {$ifdef GDB}
  1137. ait_stabs, ait_stabn, ait_stab_function_name:;
  1138. {$endif GDB}
  1139. {$ifdef regalloc}
  1140. ait_regalloc, ait_regdealloc:;
  1141. {$endif regalloc}
  1142. ait_instruction:
  1143. Begin
  1144. InstrProp := AsmInstr[Pai386(p)^._operator];
  1145. Case Pai386(p)^._operator Of
  1146. A_MOV, A_MOVZX, A_MOVSX:
  1147. Begin
  1148. Case Pai386(p)^.op1t Of
  1149. Top_Reg:
  1150. Case Pai386(p)^.op2t Of
  1151. Top_Reg:
  1152. Begin
  1153. DestroyReg(CurProp, TRegister(Pai386(p)^.op2));
  1154. { CurProp^.Regs[TRegister(Pai386(p)^.op2)] :=
  1155. CurProp^.Regs[TRegister(Pai386(p)^.op1)];
  1156. If (CurProp^.Regs[TRegister(Pai386(p)^.op2)].ModReg = R_NO) Then
  1157. CurProp^.Regs[TRegister(Pai386(p)^.op2)].ModReg :=
  1158. Tregister(Pai386(p)^.op1);}
  1159. End;
  1160. Top_Ref: DestroyRefs(p, TReference(Pai386(p)^.op2^), TRegister(Pai386(p)^.op1));
  1161. End;
  1162. Top_Ref:
  1163. Begin {destination is always a register in this case}
  1164. TmpReg := Reg32(TRegister(Pai386(p)^.op2));
  1165. If (RegInRef(TmpReg, TReference(Pai386(p)^.op1^)))
  1166. Then
  1167. Begin
  1168. With CurProp^.Regs[TmpReg] Do
  1169. Begin
  1170. IncState(State);
  1171. If (typ <> Con_Ref) Then
  1172. Begin
  1173. typ := Con_Ref;
  1174. StartMod := p;
  1175. End;
  1176. {also store how many instructions are part of the sequence in the first
  1177. instructions PPaiProp, so it can be easily accessed from within
  1178. CheckSequence}
  1179. Inc(NrOfMods, NrOfInstrSinceLastMod[TmpReg]);
  1180. PPaiProp(Pai(StartMod)^.fileinfo.line)^.Regs[TmpReg].NrOfMods := NrOfMods;
  1181. NrOfInstrSinceLastMod[TmpReg] := 0;
  1182. End;
  1183. End
  1184. Else
  1185. Begin
  1186. DestroyReg(CurProp, TmpReg);
  1187. With CurProp^.Regs[TmpReg] Do
  1188. Begin
  1189. Typ := Con_Ref;
  1190. StartMod := p;
  1191. NrOfMods := 1;
  1192. End;
  1193. End;
  1194. End;
  1195. Top_Const:
  1196. Begin
  1197. Case Pai386(p)^.op2t Of
  1198. Top_Reg:
  1199. Begin
  1200. TmpReg := Reg32(TRegister(Pai386(p)^.op2));
  1201. With CurProp^.Regs[TmpReg] Do
  1202. Begin
  1203. {it doesn't matter that the state is changed,
  1204. it isn't looked at when removing constant reloads}
  1205. DestroyReg(CurProp, TmpReg);
  1206. typ := Con_Const;
  1207. StartMod := Pai386(p)^.op1;
  1208. End
  1209. End;
  1210. Top_Ref: DestroyRefs(P, TReference(Pai386(p)^.op2^), R_NO);
  1211. End;
  1212. End;
  1213. End;
  1214. End;
  1215. A_IMUL:
  1216. Begin
  1217. If (Pai386(p)^.Op3t = top_none)
  1218. Then
  1219. If (Pai386(p)^.Op2t = top_none)
  1220. Then
  1221. Begin
  1222. DestroyReg(CurProp, R_EAX);
  1223. DestroyReg(CurProp, R_EDX)
  1224. End
  1225. Else
  1226. Begin
  1227. If (Pai386(p)^.Op2t = top_reg) Then
  1228. DestroyReg(CurProp, TRegister(Pai386(p)^.Op2));
  1229. End
  1230. Else If (Pai386(p)^.Op3t = top_reg) Then
  1231. DestroyReg(CurProp, TRegister(longint(twowords(Pai386(p)^.Op2).word2)));
  1232. End;
  1233. A_XOR:
  1234. Begin
  1235. If (Pai386(p)^.op1t = top_reg) And
  1236. (Pai386(p)^.op2t = top_reg) And
  1237. (Pai386(p)^.op1 = Pai386(p)^.op2)
  1238. Then
  1239. Begin
  1240. DestroyReg(CurProp, Tregister(Pai386(p)^.op1));
  1241. CurProp^.Regs[Reg32(Tregister(Pai386(p)^.op1))].typ := Con_Const;
  1242. CurProp^.Regs[Reg32(Tregister(Pai386(p)^.op1))].StartMod := Pointer(0)
  1243. End
  1244. Else Destroy(p, Pai386(p)^.op2t, Pai386(p)^.op2);
  1245. End
  1246. Else
  1247. Begin
  1248. If InstrProp.NCh <> 255
  1249. Then
  1250. For Cnt := 1 To InstrProp.NCh Do
  1251. Case InstrProp.Ch[Cnt] Of
  1252. C_None:;
  1253. C_EAX..C_EDI: DestroyReg(CurProp, TCh2Reg(InstrProp.Ch[Cnt]));
  1254. C_CDirFlag: CurProp^.DirFlag := F_NotSet;
  1255. C_SDirFlag: CurProp^.DirFlag := F_Set;
  1256. C_Op1: Destroy(p, Pai386(p)^.op1t, Pai386(p)^.op1);
  1257. C_Op2: Destroy(p, Pai386(p)^.op2t, Pai386(p)^.op2);
  1258. C_Op3: Destroy(p, Pai386(p)^.op2t, Pointer(Longint(TwoWords(Pai386(p)^.op2).word2)));
  1259. C_MemEDI:
  1260. Begin
  1261. FillChar(TmpRef, SizeOf(TmpRef), 0);
  1262. TmpRef.Base := R_EDI;
  1263. DestroyRefs(p, TmpRef, R_NO)
  1264. End;
  1265. C_Flags, C_FPU:;
  1266. End
  1267. Else
  1268. Begin
  1269. DestroyAllRegs(CurProp);
  1270. End;
  1271. End;
  1272. End;
  1273. End
  1274. Else
  1275. Begin
  1276. DestroyAllRegs(CurProp);
  1277. End;
  1278. End;
  1279. Inc(InstrCnt);
  1280. p := Pai(p^.next);
  1281. End;
  1282. End;
  1283. Function InitDFAPass2(AsmL: PAasmOutput): Boolean;
  1284. {reserves memory for the PPaiProps in one big memory block when not using
  1285. TP, returns False if not enough memory is available for the optimizer in all
  1286. cases}
  1287. Var p: Pai;
  1288. Count: Longint;
  1289. TmpStr: String;
  1290. Begin
  1291. P := Pai(AsmL^.First);
  1292. NrOfPaiObjs := 1;
  1293. While (P <> Pai(AsmL^.last)) Do
  1294. Begin
  1295. {$IfNDef TP}
  1296. Case P^.Typ Of
  1297. ait_labeled_instruction:
  1298. begin
  1299. If (Pai_Labeled(P)^.lab^.nb >= LoLab) And
  1300. (Pai_Labeled(P)^.lab^.nb <= HiLab) Then
  1301. Inc(LTable^[Pai_Labeled(P)^.lab^.nb-LoLab].RefsFound);
  1302. end;
  1303. ait_label:
  1304. Begin
  1305. LTable^[Pai_Label(P)^.l^.nb-LoLab].InstrNr := NrOfPaiObjs
  1306. End;
  1307. { ait_instruction:
  1308. Begin
  1309. If (Pai386(p)^._operator = A_PUSH) And
  1310. (Pai386(p)^.op1t = top_symbol) And
  1311. (PCSymbol(Pai386(p)^.op1)^.offset = 0) Then
  1312. Begin
  1313. TmpStr := StrPas(PCSymbol(Pai386(p)^.op1)^.symbol);
  1314. If}
  1315. End;
  1316. {$EndIf TP}
  1317. Inc(NrOfPaiObjs);
  1318. P := Pai(P^.next)
  1319. End;
  1320. {$IfDef TP}
  1321. If (MemAvail < (SizeOf(TPaiProp)*NrOfPaiObjs))
  1322. {this doesn't have to be one contiguous block}
  1323. Then InitDFAPass2 := False
  1324. Else
  1325. Begin
  1326. InitDFAPass2 := True;
  1327. If (MaxAvail < 65520)
  1328. Then NrOfPaiFast := MaxAvail Div (((SizeOf(TPaiProp)+1) div 2)*2)
  1329. Else NrOfPaiFast := 65520 Div (((SizeOf(TPaiProp)+1) div 2)*2);
  1330. If (NrOfPaiFast > 0) Then
  1331. GetMem(PaiPropBlock, NrOfPaiFast*(((SizeOf(TPaiProp)+1) div 2)*2));
  1332. End;
  1333. {$Else}
  1334. {Uncomment the next line to see how much memory the reloading optimizer needs}
  1335. { Writeln((NrOfPaiObjs*(((SizeOf(TPaiProp)+3)div 4)*4)));}
  1336. {no need to check mem/maxavail, we've got as much virtual memory as we want}
  1337. InitDFAPass2 := True;
  1338. GetMem(PaiPropBlock, NrOfPaiObjs*(((SizeOf(TPaiProp)+3)div 4)*4));
  1339. NrOfPaiFast := NrOfPaiObjs;
  1340. p := Pai(AsmL^.First);
  1341. For Count := 1 To NrOfPaiObjs Do
  1342. Begin
  1343. PaiPropBlock^[Count].LineSave := p^.fileinfo.line;
  1344. PPaiProp(p^.fileinfo.line) := @PaiPropBlock^[Count];
  1345. p := Pai(p^.next);
  1346. End;
  1347. {$EndIf TP}
  1348. End;
  1349. Function DFAPass2(AsmL: PAasmOutPut): Pai;
  1350. Begin
  1351. If InitDFAPass2(AsmL)
  1352. Then DFAPass2 := DoDFAPass2(Pai(AsmL^.First))
  1353. Else DFAPass2 := Nil;
  1354. End;
  1355. Procedure ShutDownDFA;
  1356. Begin
  1357. If LabDif <> 0 Then
  1358. FreeMem(LTable, LabDif*SizeOf(TLabelTableItem));
  1359. End;
  1360. End.
  1361. {
  1362. $Log$
  1363. Revision 1.7 1998-08-19 16:07:44 jonas
  1364. * changed optimizer switches + cleanup of DestroyRefs in daopt386.pas
  1365. Revision 1.6 1998/08/10 14:49:57 peter
  1366. + localswitches, moduleswitches, globalswitches splitting
  1367. Revision 1.5 1998/08/09 13:56:24 jonas
  1368. * small bugfix for uncertain optimizations in DestroyRefs
  1369. Revision 1.4 1998/08/06 19:40:25 jonas
  1370. * removed $ before and after Log in comment
  1371. Revision 1.3 1998/08/05 16:00:14 florian
  1372. * some fixes for ansi strings
  1373. * log to Log changed
  1374. }