aoptobj.pas 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. {
  2. $Id$
  3. Copyright (c) 1999 by Jonas Maebe, member of the Free Pascal
  4. Development Team
  5. This unit contains the processor independent assembler optimizer
  6. object.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ****************************************************************************
  19. }
  20. Unit AoptObj;
  21. { general, processor independent objects for use by the assembler optimizer }
  22. Interface
  23. uses Cobjects, cpubase, aoptcpub;
  24. {***************** Constants *****************}
  25. Const
  26. { ait_* types which don't result in executable code or which don't influence }
  27. { the way the program runs/behaves }
  28. SkipInstr = [ait_comment, ait_align, ait_symbol
  29. {$ifdef GDB}
  30. ,ait_stabs, ait_stabn, ait_stab_function_name
  31. {$endif GDB}
  32. ,ait_regalloc, ait_tempalloc
  33. ];
  34. {Possible register content types}
  35. con_Unknown = 0;
  36. con_ref = 1;
  37. con_const = 2;
  38. {***************** Types ****************}
  39. Type
  40. TRegArray = Array[LoReg..HiReg] of TRegister;
  41. TRegSet = Set of LoReg..HiReg;
  42. PRegInfo = ^TReginfo;
  43. { info about the equivalence of registers when comparing two code sequences}
  44. TRegInfo = Object
  45. { registers encountered in the new and old sequence }
  46. NewRegsEncountered, OldRegsEncountered,
  47. { registers which only have been loaded for use as base or }
  48. { index in a reference later on }
  49. RegsLoadedForRef: TRegSet;
  50. { to which register in the old sequence corresponds every }
  51. { register in the new sequence }
  52. New2OldReg: TRegArray;
  53. Constructor init;
  54. { clear all information store in the object }
  55. Procedure Clear;
  56. { the contents of OldReg in the old sequence are now being }
  57. { loaded into NewReg in the new sequence }
  58. Procedure AddReg(OldReg, NewReg: TRegister); Virtual;
  59. { the contents of OldOp in the old sequence are now being }
  60. { loaded into NewOp in the new sequence. It is assumed that }
  61. { OldOp and NewOp are equivalent }
  62. Procedure AddOp(const OldOp, NewOp:Toper);
  63. { check if a register in the old sequence (OldReg) can be }
  64. { equivalent to a register in the new sequence (NewReg) if }
  65. { the operation OpAct is performed on it. The RegInfo is }
  66. { updated (not necessary to call AddReg afterwards }
  67. Function RegsEquivalent(OldReg, NewReg: TRegister;
  68. OpAct: TopAction): Boolean;
  69. { check if a reference in the old sequence (OldRef) can be }
  70. { equivalent to a reference in the new sequence (NewRef) if }
  71. { the operation OpAct is performed on it. The RegInfo is }
  72. { updated (not necessary to call AddOp afterwards }
  73. Function RefsEquivalent(Const OldRef, NewRef: TReference;
  74. OpAct: TOpAction): Boolean;
  75. { check if an operand in the old sequence (OldOp) can be }
  76. { equivalent to an operand in the new sequence (NewOp) if }
  77. { the operation OpAct is performed on it. The RegInfo is }
  78. { updated (not necessary to call AddOp afterwards }
  79. Function OpsEquivalent(const OldOp, NewOp: toper;
  80. OpAct: TopAction): Boolean;
  81. { check if an instruction in the old sequence (OldP) can be }
  82. { equivalent to an instruction in the new sequence (Newp) }
  83. { The RegInfo is updated }
  84. Function InstructionsEquivalent(OldP, NewP: Pai): Boolean;
  85. End;
  86. { possible actions on an operand: read, write or modify (= read & write) }
  87. TOpAction = (OpAct_Read, OpAct_Write, OpAct_Modify, OpAct_Unknown);
  88. { the properties of a cpu instruction }
  89. TAsmInstrucProp = Record
  90. { what it changes }
  91. Ch: Array[1..MaxCh] of TChange;
  92. End;
  93. { Object to hold on information on which regiters are in use and which not }
  94. TUsedRegs = Object
  95. Constructor init;
  96. Constructor InitWithValue(_RegSet: TRegSet);
  97. { update the info with the pairegalloc objects coming after }
  98. { p }
  99. Procedure Update(p: Pai);
  100. { is Reg currently in use }
  101. Function IsUsed(Reg: TRegister): Boolean;
  102. { get all the currently used registers }
  103. Function GetUsedRegs: TRegSet;
  104. Destructor Done;
  105. Private
  106. UsedRegs: TRegSet;
  107. End;
  108. { size of the integer that holds the state number of a register. Can be any }
  109. { integer type, so it can be changed to reduce the size of the TContent }
  110. { structure or to improve alignment }
  111. TStateInt = Byte;
  112. TContent = Packed Record
  113. { start and end of block instructions that defines the }
  114. { content of this register. If Typ = con_const, then }
  115. { Longint(StartMod) = value of the constant) } }
  116. StartMod: pai;
  117. { starts at 0, gets increased everytime the register is }
  118. { written to }
  119. WState: TStateInt;
  120. { starts at 0, gets increased everytime the register is read }
  121. { from }
  122. RState: TStateInt;
  123. { how many instructions starting with StarMod does the block }
  124. { consist of }
  125. NrOfMods: Byte;
  126. { the type of the content of the register: unknown, memory }
  127. { (variable) or constant }
  128. Typ: Byte;
  129. End;
  130. { Contents of the integer registers }
  131. TRegContent = Array[LoGPReg..HiGPReg] Of TContent;
  132. PPaiProp = ^TPaiProp;
  133. { information object with the contents of every register. Every Pai object }
  134. { gets one of these assigned: a pointer to it is stored in the OptInfo field }
  135. TPaiProp = Object
  136. Regs: TRegContent;
  137. { info about allocation of general purpose integer registers }
  138. UsedRegs: TUsedRegs;
  139. { info about the conditional registers }
  140. CondRegs: TCondRegs;
  141. { can this instruction be removed? }
  142. CanBeRemoved: Boolean;
  143. Constructor init;
  144. { destroy the contents of a register, as well as those whose }
  145. { contents are based on those of that register }
  146. Procedure DestroyReg(Reg: TRegister; var NrOfInstrSinceLastMod:
  147. TInstrSinceLastMod);
  148. { if the contents of WhichReg (can be R_NO in case of a }
  149. { constant) are written to memory at the location Ref, the }
  150. { contents of the registers that depend on Ref have to be }
  151. { destroyed }
  152. Procedure DestroyRefs(Const Ref: TReference; WhichReg: TRegister);
  153. { an instruction reads from operand o }
  154. Procedure ReadOp(const o:toper);
  155. { an instruction reads from reference Ref }
  156. Procedure ReadRef(Ref: PReference);
  157. { an instruction reads from register Reg }
  158. Procedure ReadReg(Reg: TRegister);
  159. { an instruction writes/modifies operand o and this has }
  160. { special side-effects or modifies the contents in such a }
  161. { way that we can't simply add this instruction to the }
  162. { sequence of instructions that describe the contents of the }
  163. { operand, so destroy it }
  164. Procedure DestroyOp(const o:Toper);
  165. { destroy the contetns of all registers }
  166. Procedure DestroyAllRegs;
  167. {$ifdef arithopt}
  168. { a register's contents are modified, but not destroyed }
  169. { (the new value depends on the old one) }
  170. Procedure ModifyReg(reg: TRegister);
  171. { an operand's contents are modified, but not destroyed }
  172. { (the new value depends on the old one) }
  173. Procedure ModifyOp(const oper: TOper);
  174. {$endif arithopt}
  175. { increase the write state of a register (call every time a }
  176. { register is written to) }
  177. Procedure IncWState(Reg: TRegister);
  178. { increase the read state of a register (call every time a }
  179. { register is read from }
  180. Procedure IncRState(Reg: TRegister);
  181. { get the write state of a register }
  182. Function GetWState(Reg: TRegister): TStateInt;
  183. { get the read state of a register }
  184. Function GetRState(Reg: TRegister): TStateInt;
  185. { get the type of contents of a register }
  186. Function GetRegContentKind(Reg: TRegister): Byte;
  187. Destructor Done;
  188. Private
  189. Procedure IncState(Reg: TRegister);
  190. End;
  191. { the number of instructions that we have encountered since the last }
  192. { modification of a register }
  193. TInstrSinceLastMod = Array[LoGPReg..HiGPReg] Of Byte;
  194. TLabelTableItem = Record
  195. PaiObj: Pai;
  196. {$IfDef JumpAnal}
  197. InstrNr: Longint;
  198. RefsFound: Word;
  199. JmpsProcessed: Word
  200. {$EndIf JumpAnal}
  201. End;
  202. TLabelTable = Array[0..2500000] Of TLabelTableItem;
  203. PLabelTable = ^TLabelTable;
  204. TLabelInfo = Record
  205. { the highest and lowest label number occurring in the current code }
  206. { fragment }
  207. LowLabel, HighLabel: AWord;
  208. LabelDif: AWord;
  209. { table that contains the addresses of the Pai_Label objects associated }
  210. { with each label number }
  211. LabelTable: PLableTable;
  212. End;
  213. {***** General optimizer object, used to derive others from *****}
  214. Type TAOptObj = Object
  215. { the PAasmOutput list this optimizer instance works on }
  216. AsmL: PAasmOutput;
  217. { The labelinfo record contains the addresses of the Pai objects }
  218. { that are labels, how many labels there are and the min and max }
  219. { label numbers }
  220. LabelInfo: PLabelInfo;
  221. { Start and end of the block that is currently being optimized }
  222. BlockStart, BlockEnd: Pai;
  223. { _AsmL is the PAasmOutpout list that has to be optimized, }
  224. { _BlockStart and _BlockEnd the start and the end of the block }
  225. { that has to be optimized and _LabelInfo a pointer to a }
  226. { TLabelInfo record }
  227. Constructor Init(_AsmL: PAasmOutput; _BlockStart, _BlockEnd: Pai;
  228. _LabelInfo: PLabelInfo);
  229. { processor independent methods }
  230. { returns true if the label L is found between hp and the next }
  231. { instruction }
  232. Function FindLabel(L: PasmLabel; Var hp: Pai): Boolean;
  233. { inserts new_one between prev and foll in AsmL }
  234. Procedure InsertLLItem(AsmL: PAasmOutput; prev, foll, new_one:
  235. PLinkedList_Item);
  236. { returns true if register Reg is used by instruction p1 }
  237. Function RegInInstruction(Reg: TRegister; p1: Pai): Boolean;
  238. { returns true if register Reg is used in the reference Ref }
  239. Function RegInRef(Reg: TRegister; Const Ref: TReference): Boolean;
  240. { returns whether the reference Ref is used somewhere in the loading }
  241. { sequence Constent }
  242. Function TAOptObj.RefInSequence(Const Ref: TReference;
  243. Content: TContent): Boolean;
  244. { returns whether the instruction P reads from and/or writes to Reg }
  245. Function TAOptObj.RefInInstruction(Const Ref: TReference; p: Pai): Boolean;
  246. { returns true if the instruction p1 modifies the register Reg }
  247. Function RegModifiedByInstruction(Reg: TRegister; p1: Pai): Boolean;
  248. { gets the next Pai object after current that contains info relevant }
  249. { to the optimizer in p1. If there is none, it returns false and }
  250. { sets p1 to nil }
  251. Function GetNextInstruction(Current: Pai; Var Next: Pai): Boolean;
  252. { gets the previous Pai object after current that contains info }
  253. { relevant to the optimizer in last. If there is none, it retuns }
  254. { false and sets last to nil }
  255. Function GetLastInstruction(Current: Pai; Var Last: Pai): Boolean;
  256. { If P is a Pai object releveant to the optimizer, it is left }
  257. { unchanged. If it is not relevant tot he optimizer, the first }
  258. { object after P that is relevant is stored in P }
  259. Procedure SkipHead(var P: Pai);
  260. { returns true if the operands o1 and o2 are completely equal }
  261. Function OpsEqual(const o1,o2:toper): Boolean;
  262. { Returns true if a ait_alloc object for Reg is found in the block }
  263. { of Pai's starting with StartPai and ending with the next "real" }
  264. { instruction }
  265. Function FindRegAlloc(Reg: TRegister; StartPai: Pai): Boolean;
  266. { processor dependent methods }
  267. { returns the maximum width component of Reg. Only has to be }
  268. { overridden for the 80x86 (afaik) }
  269. Function RegMaxSize(Reg: TRegister): TRegister; Virtual;
  270. { returns true if Reg1 and Reg2 are of the samae width. Only has to }
  271. { overridden for the 80x86 (afaik) }
  272. Function RegsSameSize(Reg1, Reg2: TRegister): Boolean; Virtual;
  273. { returns whether P is a load instruction (load contents from a }
  274. { memory location or (register) variable into a register) }
  275. Function IsLoadInstr(p: pai): Boolean; Virtual;
  276. { returns whether P is a store instruction (store contents from a }
  277. { register to a memory location or to a (register) variable) }
  278. Function IsStoreInstr(p: pai): Boolean; Virtual;
  279. { returns whether the instruction P reads from register Reg }
  280. Function RegReadByInstr(Reg: TRegister; p: Pai); Virtual;
  281. { convert a TChange value into the corresponding register }
  282. Function TCh2Reg(Ch: TChange): TRegister; Virtual;
  283. End;
  284. {***************** Implementation *****************}
  285. Implementation
  286. {*********************** TReginfo ***********************}
  287. Constructor TRegInfo.Init;
  288. Begin
  289. Clear;
  290. End;
  291. Constructor TRegInfo.Init;
  292. Begin
  293. RegsLoadedForRef := [];
  294. NewRegsEncountered := [ProcInfo.FramePointer, R_ESP];
  295. OldRegsEncountered := [ProcInfo.FramePointer, R_ESP];
  296. New2OldReg[ProcInfo.FramePointer] := ProcInfo.FramePointer;
  297. New2OldReg[R_ESP] := R_ESP;
  298. End;
  299. Procedure TRegInfo.AddReg(OldReg, NewReg: TRegister);
  300. { updates the ???RegsEncountered and ???2???Reg fields of RegInfo. Assumes }
  301. { that OldReg and NewReg have the same size (has to be chcked in advance }
  302. { with RegsSameSize) and that neither equals R_NO }
  303. { has to be overridden for architectures like the 80x86 when not all GP }
  304. { regs are of the same size }
  305. Begin
  306. NewRegsEncountered := NewRegsEncountered + [NewReg];
  307. OldRegsEncountered := OldRegsEncountered + [OldReg];
  308. New2OldReg[NewReg] := OldReg;
  309. End;
  310. Procedure TRegInfo.AddOp(const OldOp, NewOp:Toper);
  311. Begin
  312. Case OldOp.typ Of
  313. Top_Reg:
  314. If (OldOp.reg <> R_NO) Then
  315. AddReg(OldOp.reg, NewOp.reg);
  316. Top_Ref:
  317. Begin
  318. If OldOp.ref^.base <> R_NO Then
  319. AddReg(OldOp.ref^.base, NewOp.ref^.base);
  320. {$ifdef RefsHaveIndexReg}
  321. If OldOp.ref^.index <> R_NO Then
  322. AddReg(OldOp.ref^.index, NewOp.ref^.index);
  323. {$endif RefsHaveIndexReg}
  324. End;
  325. End;
  326. End;
  327. Function TRegInfo.RegsEquivalent(OldReg, NewReg: TRegister;
  328. OPAct: TOpAction): Boolean;
  329. Begin
  330. If Not((OldReg = R_NO) Or (NewReg = R_NO)) Then
  331. If RegsSameSize(OldReg, NewReg) Then
  332. {here we always check for the 32 bit component, because it is possible that
  333. the 8 bit component has not been set, event though NewReg already has been
  334. processed. This happens if it has been compared with a register that doesn't
  335. have an 8 bit component (such as EDI). In that case the 8 bit component is
  336. still set to R_NO and the comparison in the Else-part will fail}
  337. If (RegMaxSize(OldReg) in OldRegsEncountered) Then
  338. If (RegMaxSize(NewReg) in NewRegsEncountered) Then
  339. RegsEquivalent := (OldReg = New2OldReg[NewReg])
  340. { If we haven't encountered the new register yet, but we have encountered the
  341. old one already, the new one can only be correct if it's being written to
  342. (and consequently the old one is also being written to), otherwise
  343. movl -8(%ebp), %eax and movl -8(%ebp), %eax
  344. movl (%eax), %eax movl (%edx), %edx
  345. are considered equivalent}
  346. Else
  347. If (OpAct = OpAct_Write) Then
  348. Begin
  349. AddReg(OldReg, NewReg);
  350. RegsEquivalent := True
  351. End
  352. Else Regsequivalent := False
  353. Else
  354. If Not(RegMaxSize(NewReg) in NewRegsEncountered) Then
  355. Begin
  356. AddReg(OldReg, NewReg);
  357. RegsEquivalent := True
  358. End
  359. Else RegsEquivalent := False
  360. Else RegsEquivalent := False
  361. Else RegsEquivalent := OldReg = NewReg
  362. End;
  363. Function TRegInfo.RefsEquivalent(Const OldRef, NewRef: TReference;
  364. OpAct: TOpAction): Boolean;
  365. Begin
  366. If OldRef.is_immediate Then
  367. RefsEquivalent := NewRef.is_immediate and (OldRef.Offset = NewRef.Offset)
  368. Else
  369. RefsEquivalent := (OldRef.Offset+OldRef.OffsetFixup =
  370. NewRef.Offset+NewRef.OffsetFixup) And
  371. RegsEquivalent(OldRef.Base, NewRef.Base, OpAct)
  372. {$ifdef RefsHaveindex}
  373. And RegsEquivalent(OldRef.Index, NewRef.Index, OpAct)
  374. {$endif RefsHaveIndex}
  375. {$ifdef RefsHaveScale}
  376. And (OldRef.ScaleFactor = NewRef.ScaleFactor)
  377. {$endif RefsHaveScale}
  378. And (OldRef.Symbol = NewRef.Symbol)
  379. {$ifdef RefsHaveSegment}
  380. And (OldRef.Segment = NewRef.Segment)
  381. {$endif RefsHaveSegment}
  382. ;
  383. End;
  384. Function TRegInfo.OpsEquivalent(const OldOp, NewOp: toper; OpAct: TopAction):
  385. Boolean;
  386. Begin {checks whether the two ops are equivalent}
  387. OpsEquivalent := False;
  388. if OldOp.typ=NewOp.typ then
  389. Case OldOp.typ Of
  390. Top_Const: OpsEquivalent := OldOp.value = NewOp.value;
  391. Top_Reg:
  392. OpsEquivalent := RegsEquivalent(OldOp.reg,NewOp.reg, OpAct);
  393. Top_Ref:
  394. OpsEquivalent := RefsEquivalent(OldOp.ref^, NewOp.ref^, OpAct);
  395. Top_None:
  396. OpsEquivalent := True
  397. End;
  398. End;
  399. Function TRegInfo.InstructionsEquivalent(OldP, NewP: Pai): Boolean;
  400. { checks whether two PInstr instructions are equal }
  401. Var Count: TNatInt;
  402. TmpResult: Boolean;
  403. Begin
  404. If Assigned(OldP) And Assigned(NewP) And
  405. (Pai(OldP)^.typ = ait_instruction) And
  406. (Pai(NewP)^.typ = ait_instruction) And
  407. (PInstr(OldP)^.opcode = PInstr(NewP)^.opcode) And
  408. (PInstr(OldP)^.oper[0].typ = PInstr(NewP)^.oper[0].typ) And
  409. (PInstr(OldP)^.oper[1].typ = PInstr(NewP)^.oper[1].typ) And
  410. (PInstr(OldP)^.oper[2].typ = PInstr(NewP)^.oper[2].typ)
  411. Then
  412. { both instructions have the same structure: }
  413. { "<operator> <operand of type1>, <operand of type 2>" }
  414. If IsLoadInstr(OldP) {then also NewP = loadinstr} Then
  415. If Not(RegInRef(PInstr(OldP)^.oper[LoadDst].reg,
  416. PInstr(OldP)^.oper[LoadSrc].ref^)) Then
  417. { the "old" instruction is a load of a register with a new value, not with }
  418. { a value based on the contents of this register (so no "mov (reg), reg") }
  419. If Not(RegInRef(PInstr(NewP)^.oper[LoadDst].reg,
  420. PInstr(NewP)^.oper[LoadSrc].ref^)) And
  421. RefsEqual(PInstr(OldP)^.oper[LoadSrc].ref^,
  422. PInstr(NewP)^.oper[LoadSrc].ref^)
  423. Then
  424. { the "new" instruction is also a load of a register with a new value, and }
  425. { this value is fetched from the same memory location }
  426. Begin
  427. With PInstr(NewP)^.oper[LoadSrc].ref^ Do
  428. Begin
  429. If Not(Base in [ProcInfo.FramePointer, R_NO, StackPtr])
  430. { it won't do any harm if the register is already in RegsLoadedForRef }
  431. Then RegsLoadedForRef := RegsLoadedForRef + [Base];
  432. {$ifdef RefsHaveIndex}
  433. If Not(Index in [ProcInfo.FramePointer, R_NO, R_StackPtr])
  434. Then RegsLoadedForRef := RegsLoadedForRef + [Index];
  435. {$endif RefsHaveIndex}
  436. End;
  437. { add the registers from the reference (.oper[Src]) to the RegInfo, all }
  438. { registers from the reference are the same in the old and in the new }
  439. { instruction sequence }
  440. AddOp(PInstr(OldP)^.oper[Src], PInstr(OldP)^.oper[Src]);
  441. { the registers from .oper[Dest] have to be equivalent, but not necessarily }
  442. { equal }
  443. InstructionsEquivalent :=
  444. RegsEquivalent(PInstr(OldP)^.oper[Dest].reg,
  445. PInstr(NewP)^.oper[Dest].reg, OpAct_Write);
  446. End
  447. { the registers are loaded with values from different memory locations. If }
  448. { this were allowed, the instructions "mov -4(%esi),%eax" and }
  449. { "mov -4(%ebp),%eax" would be considered equivalent }
  450. Else InstructionsEquivalent := False
  451. Else
  452. { load register with a value based on the current value of this register }
  453. Begin
  454. With PInstr(NewP)^.oper[0].ref^ Do
  455. { Assume the registers occurring in the reference have only been loaded with }
  456. { the value they contain now to calculate an address (so the value they have }
  457. { now, won't be stored to memory later on)}
  458. Begin
  459. If Not(Base in [ProcInfo.FramePointer,
  460. RegMaxSize(PInstr(NewP)^.oper[LoadDst].reg),
  461. R_NO,StackPtr])
  462. { It won't do any harm if the register is already in RegsLoadedForRef }
  463. Then
  464. Begin
  465. RegsLoadedForRef := RegsLoadedForRef + [Base];
  466. {$ifdef csdebug}
  467. Writeln(att_reg2str[base], ' added');
  468. {$endif csdebug}
  469. end;
  470. {$Ifdef RefsHaveIndex}
  471. If Not(Index in [ProcInfo.FramePointer,
  472. RegMaxSize(PInstr(NewP)^.oper[LoadDst].reg),
  473. R_NO,StackPtr])
  474. Then
  475. Begin
  476. RegsLoadedForRef := RegsLoadedForRef + [Index];
  477. {$ifdef csdebug}
  478. Writeln(att_reg2str[index], ' added');
  479. {$endif csdebug}
  480. end;
  481. {$endif RefsHaveIndex}
  482. End;
  483. { now, remove the destination register of the load from the }
  484. { RegsLoadedForReg, since if it's loaded with a new value, it certainly }
  485. { will still be used later on }
  486. If Not(RegMaxSize(PInstr(NewP)^.oper[LoadDst].reg) In
  487. [ProcInfo.FramePointer,R_NO,StackPtr])
  488. Then
  489. Begin
  490. RegsLoadedForRef := RegsLoadedForRef -
  491. [RegMaxSize(PInstr(NewP)^.oper[LoadDst].reg)];
  492. {$ifdef csdebug}
  493. Writeln(att_reg2str[Reg32(Pai386(NewP)^.oper[1].reg)], ' removed');
  494. {$endif csdebug}
  495. end;
  496. InstructionsEquivalent :=
  497. OpsEquivalent(PInstr(OldP)^.oper[LoadSrc],
  498. PInstr(NewP)^.oper[LoadSrc], OpAct_Read) And
  499. OpsEquivalent(PInstr(OldP)^.oper[LoadDst],
  500. PInstr(NewP)^.oper[LoadDst], OpAct_Write)
  501. End
  502. Else
  503. {an instruction that's not a load instruction}
  504. Begin
  505. Count := 0;
  506. TmpResult := true;
  507. Repeat
  508. TmpResult :=
  509. OpsEquivalent(PInstr(OldP)^.oper[Count], PInstr(NewP)^.oper[Count],
  510. OpAct_Unknown);
  511. Inc(Count)
  512. Until (Count = MaxOps) or not(TmpResult);
  513. InstructionsEquivalent := TmpResult
  514. End
  515. { the instructions haven't even got the same structure, so they're certainly }
  516. { not equivalent }
  517. Else InstructionsEquivalent := False;
  518. End;
  519. {*************************** TUsedRegs ***************************}
  520. Constructor TUsedRegs.init;
  521. Begin
  522. UsedRegs := [];
  523. End;
  524. Constructor TUsedRegisters.InitWithValue(Const _RegSet: TRegSet);
  525. Begin
  526. RegSet := _RegSet;
  527. End;
  528. Procedure TUsedRegs.Update(p: Pai);
  529. {updates UsedRegs with the RegAlloc Information coming after P}
  530. Begin
  531. Repeat
  532. While Assigned(p) And
  533. ((p^.typ in (SkipInstr - [ait_RegAlloc])) or
  534. ((p^.typ = ait_label) And
  535. Not(Pai_Label(p)^.l^.is_used))) Do
  536. p := Pai(p^.next);
  537. While Assigned(p) And
  538. (p^.typ=ait_RegAlloc) Do
  539. Begin
  540. if pairegalloc(p)^.allocation then
  541. UsedRegs := UsedRegs + [PaiRegAlloc(p)^.Reg]
  542. else
  543. UsedRegs := UsedRegs - [PaiRegAlloc(p)^.Reg];
  544. p := pai(p^.next);
  545. End;
  546. Until Not(Assigned(p)) Or
  547. (Not(p^.typ in SkipInstr) And
  548. Not((p^.typ = ait_label) And
  549. Not(Pai_Label(p)^.l^.is_used)));
  550. End;
  551. Function TUsedRegs.IsUsed(Reg: TRegister): Boolean;
  552. Begin
  553. IsUsed := Reg in UsedRegs
  554. End;
  555. Function TUsedRegs.GetUsedRegs: TRegSet;
  556. Begin
  557. GetUsedRegs := UsedRegs;
  558. End;
  559. {*************************** TPaiProp ***************************}
  560. Constructor TPaiProp.Init;
  561. Begin
  562. UsedRegs.Init;
  563. CondRegs.init;
  564. { DirFlag: TFlagContents; I386 specific}
  565. End;
  566. Procedure TPaiProp.DestroyReg(Reg: TRegister; var NrOfInstrSinceLastMod:
  567. TInstrSinceLastMod);
  568. { Destroys the contents of the register Reg in the PPaiProp p1, as well as }
  569. { the contents of registers are loaded with a memory location based on Reg }
  570. Var TmpWState, TmpRState: Byte;
  571. Counter: TRegister;
  572. Begin
  573. Reg := Reg32(Reg);
  574. NrOfInstrSinceLastMod[Reg] := 0;
  575. If (Reg >= R_EAX) And (Reg <= R_EDI)
  576. Then
  577. Begin
  578. With Regs[Reg] Do
  579. Begin
  580. IncState(WState);
  581. TmpWState := WState;
  582. TmpRState := RState;
  583. FillChar(Regs[Reg], SizeOf(TContent), 0);
  584. WState := TmpWState;
  585. RState := TmpRState;
  586. End;
  587. For Counter := R_EAX to R_EDI Do
  588. With Regs[Counter] Do
  589. If (Typ = Con_Ref) And
  590. RegInSequence(Reg, Regs[Counter])
  591. Then
  592. Begin
  593. IncState(WState);
  594. TmpWState := WState;
  595. TmpRState := RState;
  596. FillChar(Regs[Counter], SizeOf(TContent), 0);
  597. WState := TmpWState;
  598. RState := TmpRState;
  599. End;
  600. End;
  601. End;
  602. Procedure TPaiProp.DestroyRefs(Const Ref: TReference; WhichReg: TRegister);
  603. {destroys all registers which possibly contain a reference to Ref, WhichReg
  604. is the register whose contents are being written to memory (if this proc
  605. is called because of a "mov?? %reg, (mem)" instruction)}
  606. Var Counter: TRegister;
  607. Begin
  608. WhichReg := RegMaxSize(WhichReg);
  609. If ((Ref.base = ProcInfo.FramePointer) And
  610. {$ifdef refsHaveIndex}
  611. (Ref.Index = R_NO)
  612. {$endif refsHaveIndex}
  613. ) Or
  614. Assigned(Ref.Symbol)
  615. Then
  616. {write something to a parameter, a local or global variable, so
  617. * with uncertain optimizations on:
  618. - destroy the contents of registers whose contents have somewhere a
  619. "mov?? (Ref), %reg". WhichReg (this is the register whose contents
  620. are being written to memory) is not destroyed if it's StartMod is
  621. of that form and NrOfMods = 1 (so if it holds ref, but is not a
  622. pointer or value based on Ref)
  623. * with uncertain optimizations off:
  624. - also destroy registers that contain any pointer}
  625. For Counter := LoGPReg to HiGPReg Do
  626. With Regs[Counter] Do
  627. Begin
  628. If (typ = Con_Ref) And
  629. ((Not(cs_UncertainOpts in aktglobalswitches) And
  630. (NrOfMods <> 1)
  631. ) Or
  632. (RefInSequence(Ref,Regs[Counter]) And
  633. ((Counter <> WhichReg) Or
  634. ((NrOfMods <> 1) And
  635. {StarMod is always of the type ait_instruction}
  636. (PInstr(StartMod)^.oper[0].typ = top_ref) And
  637. RefsEqual(PInstr(StartMod)^.oper[0].ref^, Ref)
  638. )
  639. )
  640. )
  641. )
  642. Then
  643. DestroyReg(Counter)
  644. End
  645. Else
  646. {write something to a pointer location, so
  647. * with uncertain optimzations on:
  648. - do not destroy registers which contain a local/global variable or a
  649. parameter, except if DestroyRefs is called because of a "movsl"
  650. * with uncertain optimzations off:
  651. - destroy every register which contains a memory location
  652. }
  653. For Counter := LoGPReg to HiGPReg Do
  654. With Regs[Counter] Do
  655. If (typ = Con_Ref) And
  656. (Not(cs_UncertainOpts in aktglobalswitches) Or
  657. {$ifdef i386}
  658. {for movsl}
  659. (Ref.Base = R_EDI) Or
  660. {$endif}
  661. {don't destroy if reg contains a parameter, local or global variable}
  662. Not((NrOfMods = 1) And
  663. (PInstr(StartMod)^.oper[0].typ = top_ref) And
  664. ((PInstr(StartMod)^.oper[0].ref^.base = ProcInfo.FramePointer) Or
  665. Assigned(PInstr(StartMod)^.oper[0].ref^.Symbol)
  666. )
  667. )
  668. )
  669. Then DestroyReg(Counter)
  670. End;
  671. Procedure TPaiProp.DestroyAllRegs;
  672. Var Counter: TRegister;
  673. Begin {initializes/desrtoys all registers}
  674. For Counter := R_EAX To R_EDI Do
  675. DestroyReg(Counter);
  676. CondRegs.Init;
  677. { FPURegs.Init; }
  678. End;
  679. Procedure TPaiProp.DestroyOp(const o:Toper);
  680. Begin
  681. Case o.typ Of
  682. top_reg: DestroyReg(o.reg);
  683. top_ref: DestroyRefs(o.ref^, R_NO);
  684. top_symbol:;
  685. End;
  686. End;
  687. Procedure TPaiProp.ReadReg(Reg: TRegister);
  688. Begin
  689. Reg := RegMaxSize(Reg);
  690. If Reg in General_Registers Then
  691. IncRState(RegMaxSize(Reg))
  692. End;
  693. Procedure TPaiProp.ReadRef(Ref: PReference);
  694. Begin
  695. If Ref^.Base <> R_NO Then
  696. ReadReg(Ref^.Base);
  697. If Ref^.Index <> R_NO Then
  698. ReadReg(Ref^.Index);
  699. End;
  700. Procedure TPaiProp.ReadOp(const o:toper);
  701. Begin
  702. Case o.typ Of
  703. top_reg: ReadReg(o.reg);
  704. top_ref: ReadRef(o.ref);
  705. top_symbol : ;
  706. End;
  707. End;
  708. {$ifdef arithopt}
  709. Procedure TPaiProp.ModifyReg(reg: TRegister; Var NrOfInstrSinceLastMod:
  710. TInstrSinceLastMod);
  711. Begin
  712. With Regs[reg] Do
  713. If (Typ = Con_Ref)
  714. Then
  715. Begin
  716. IncState(WState);
  717. {also store how many instructions are part of the sequence in the first
  718. instructions PPaiProp, so it can be easily accessed from within
  719. CheckSequence}
  720. Inc(NrOfMods, NrOfInstrSinceLastMod[Reg]);
  721. PPaiProp(StartMod^.OptInfo)^.Regs[Reg].NrOfMods := NrOfMods;
  722. NrOfInstrSinceLastMod[Reg] := 0;
  723. End
  724. Else
  725. DestroyReg(Reg, NrOfInstrSinceLastMod);
  726. End;
  727. Procedure TPaiProp.ModifyOp(const oper: TOper);
  728. Begin
  729. If oper.typ = top_reg Then
  730. ModifyReg(RegMaxSize(oper.reg))
  731. Else
  732. Begin
  733. ReadOp(oper);
  734. DestroyOp(oper);
  735. End
  736. End;
  737. {$endif arithopt}
  738. Procedure TPaiProp.IncWState(Reg: TRegister);{$ifdef fpc} inline;{$endif fpc}
  739. Begin
  740. IncState(Regs[Reg].WState);
  741. End;
  742. Procedure TPaiProp.IncRState(Reg: TRegister);{$ifdef fpc} inline;{$endif fpc}
  743. Begin
  744. IncState(Regs[Reg].RState);
  745. End;
  746. Procedure TPaiProp.IncState(Var s: TStateInt); {$ifdef fpc} inline;{$endif fpc}
  747. Begin
  748. If s <> $ff Then Inc(s)
  749. Else s := 0
  750. End;
  751. Function TPaiProp.GetWState(Reg: TRegister): TStateInt;
  752. Begin
  753. GetWState := Regs[Reg].WState
  754. End;
  755. Function TPaiProp.GetRState(Reg: TRegister): TStateInt;
  756. Begin
  757. GetRState := Regs[Reg].RState
  758. End;
  759. Function TPaiProp.GetRegContentKind(Reg: TRegister): Byte;
  760. Begin
  761. GetRegContentKind := Regs[Reg].typ
  762. End;
  763. {******************* TAOptObj *******************}
  764. Constructor TAoptObj.Init(_AsmL: PAasmOutput; _BlockStart, _BlockEnd: Pai;
  765. _LabelInfo: PLabelInfo);
  766. Begin
  767. AsmL := _AsmL;
  768. BlockStart := _BlockStart;
  769. BlockEnd := _BlockEnd;
  770. LabelInfo := _LabelInfo
  771. End;
  772. Function TAOptObj.FindLabel(L: PasmLabel; Var hp: Pai): Boolean;
  773. Var TempP: Pai;
  774. Begin
  775. TempP := hp;
  776. While Assigned(TempP) and
  777. (TempP^.typ In SkipInstr + [ait_label]) Do
  778. If (TempP^.typ <> ait_Label) Or
  779. (pai_label(TempP)^.l <> L)
  780. Then GetNextInstruction(TempP, TempP)
  781. Else
  782. Begin
  783. hp := TempP;
  784. FindLabel := True;
  785. exit
  786. End;
  787. FindLabel := False;
  788. End;
  789. Procedure TAOptObj.InsertLLItem(AsmL: PAasmOutput; prev, foll, new_one:
  790. PLinkedList_Item);
  791. Begin
  792. If Assigned(prev) Then
  793. If Assigned(foll) Then
  794. Begin
  795. If Assigned(new_one) Then
  796. Begin
  797. new_one^.previous := prev;
  798. new_one^.next := foll;
  799. prev^.next := new_one;
  800. foll^.previous := new_one;
  801. new_one^.fileinfo := foll^.fileinfo
  802. End
  803. End
  804. Else AsmL^.Concat(new_one)
  805. Else If Assigned(Foll) Then AsmL^.Insert(new_one)
  806. End;
  807. Function TAOptObj.RegInInstruction(Reg: TRegister; p1: Pai): Boolean;
  808. Var Count: TNatInt;
  809. TmpResult: Boolean;
  810. Begin
  811. TmpResult := False;
  812. Count := 0;
  813. If (p1^.typ = ait_instruction) Then
  814. Repeat
  815. TmpResult := RegInOp(Reg, PInstr(p1)^.oper[Count]);
  816. Inc(Count)
  817. Until (Count = MaxOps) or TmpResult;
  818. RegInInstruction := TmpResult
  819. End;
  820. Function TAOptObj.RegInOp(Reg: TRegister; const op: toper): Boolean;
  821. Begin
  822. Case op.typ Of
  823. Top_Reg: RegInOp := Reg = op.reg;
  824. Top_Ref: RegInOp := RegInRef(Reg, op.ref^)
  825. Else RegInOp := False
  826. End
  827. End;
  828. Function TAOptObj.RegModifiedByInstruction(Reg: TRegister; p1: Pai): Boolean;
  829. Var hp: Pai;
  830. Begin
  831. If GetLastInstruction(p1, hp)
  832. Then
  833. RegModifiedByInstruction :=
  834. PPAiProp(p1^.OptInfo)^.Regs[Reg].WState <>
  835. PPAiProp(hp^.OptInfo)^.Regs[Reg].WState
  836. Else RegModifiedByInstruction := True;
  837. End;
  838. Function TAOptObj.GetNextInstruction(Current: Pai; Var Next: Pai): Boolean;
  839. Begin
  840. Repeat
  841. Current := Pai(Current^.Next);
  842. While Assigned(Current) And
  843. ((Current^.typ In SkipInstr) or
  844. ((Current^.typ = ait_label) And
  845. Not(Pai_Label(Current)^.l^.is_used))) Do
  846. Current := Pai(Current^.Next);
  847. If Assigned(Current) And
  848. (Current^.typ = ait_Marker) And
  849. (Pai_Marker(Current)^.Kind = NoPropInfoStart) Then
  850. Begin
  851. While Assigned(Current) And
  852. ((Current^.typ <> ait_Marker) Or
  853. (Pai_Marker(Current)^.Kind <> NoPropInfoEnd)) Do
  854. Current := Pai(Current^.Next);
  855. End;
  856. Until Not(Assigned(Current)) Or
  857. (Current^.typ <> ait_Marker) Or
  858. (Pai_Marker(Current)^.Kind <> NoPropInfoEnd);
  859. Next := Current;
  860. If Assigned(Current) And
  861. Not((Current^.typ In SkipInstr) or
  862. ((Current^.typ = ait_label) And
  863. Not(Pai_Label(Current)^.l^.is_used)))
  864. Then GetNextInstruction := True
  865. Else
  866. Begin
  867. Next := Nil;
  868. GetNextInstruction := False;
  869. End;
  870. End;
  871. Function TAOptObj.GetLastInstruction(Current: Pai; Var Last: Pai): Boolean;
  872. Begin
  873. Repeat
  874. Current := Pai(Current^.previous);
  875. While Assigned(Current) And
  876. (((Current^.typ = ait_Marker) And
  877. Not(Pai_Marker(Current)^.Kind in [AsmBlockEnd,NoPropInfoEnd])) or
  878. (Current^.typ In SkipInstr) or
  879. ((Current^.typ = ait_label) And
  880. Not(Pai_Label(Current)^.l^.is_used))) Do
  881. Current := Pai(Current^.previous);
  882. If Assigned(Current) And
  883. (Current^.typ = ait_Marker) And
  884. (Pai_Marker(Current)^.Kind = NoPropInfoEnd) Then
  885. Begin
  886. While Assigned(Current) And
  887. ((Current^.typ <> ait_Marker) Or
  888. (Pai_Marker(Current)^.Kind <> NoPropInfoStart)) Do
  889. Current := Pai(Current^.previous);
  890. End;
  891. Until Not(Assigned(Current)) Or
  892. (Current^.typ <> ait_Marker) Or
  893. (Pai_Marker(Current)^.Kind <> NoPropInfoStart);
  894. If Not(Assigned(Current)) or
  895. (Current^.typ In SkipInstr) or
  896. ((Current^.typ = ait_label) And
  897. Not(Pai_Label(Current)^.l^.is_used)) or
  898. ((Current^.typ = ait_Marker) And
  899. (Pai_Marker(Current)^.Kind = AsmBlockEnd))
  900. Then
  901. Begin
  902. Last := Nil;
  903. GetLastInstruction := False
  904. End
  905. Else
  906. Begin
  907. Last := Current;
  908. GetLastInstruction := True;
  909. End;
  910. End;
  911. Procedure TAOptObj.SkipHead(var P: Pai);
  912. { skips Pai objects at the start of a block that don't do anything }
  913. Var OldP: Pai;
  914. Begin
  915. Repeat
  916. OldP := P;
  917. If (P^.typ in SkipInstr) Or
  918. ((P^.typ = ait_marker) And
  919. (Pai_Marker(P)^.Kind = AsmBlockEnd)) Then
  920. GetNextInstruction(P, P)
  921. Else If ((P^.Typ = Ait_Marker) And
  922. (Pai_Marker(P)^.Kind = NoPropInfoStart)) Then
  923. { a marker of the type NoPropInfoStart can't be the first instruction of a }
  924. { paasmoutput list }
  925. GetNextInstruction(Pai(P^.Previous),P);
  926. If (P^.Typ = Ait_Marker) And
  927. (Pai_Marker(P)^.Kind = AsmBlockStart) Then
  928. Begin
  929. P := Pai(P^.Next);
  930. While (P^.typ <> Ait_Marker) Or
  931. (Pai_Marker(P)^.Kind <> AsmBlockEnd) Do
  932. P := Pai(P^.Next)
  933. End;
  934. Until P = OldP
  935. End;
  936. Function TAOptObj.OpsEqual(const o1,o2:toper): Boolean;
  937. Begin
  938. if o1.typ=o2.typ then
  939. Case o1.typ Of
  940. Top_Reg :
  941. OpsEqual:=o1.reg=o2.reg;
  942. Top_Ref :
  943. OpsEqual := RefsEqual(o1.ref^, o2.ref^);
  944. Top_Const :
  945. OpsEqual:=o1.val=o2.val;
  946. Top_Symbol :
  947. OpsEqual:=(o1.sym=o2.sym) and (o1.symofs=o2.symofs);
  948. Top_None :
  949. OpsEqual := True
  950. else OpsEqual := False
  951. End;
  952. End;
  953. Function TAOptObj.FindRegAlloc(Reg: TRegister; StartPai: Pai): Boolean;
  954. Begin
  955. FindRegAlloc:=False;
  956. Repeat
  957. While Assigned(StartPai) And
  958. ((StartPai^.typ in (SkipInstr - [ait_regAlloc])) Or
  959. ((StartPai^.typ = ait_label) and
  960. Not(Pai_Label(StartPai)^.l^.Is_Used))) Do
  961. StartPai := Pai(StartPai^.Next);
  962. If Assigned(StartPai) And
  963. (StartPai^.typ = ait_regAlloc) and (PairegAlloc(StartPai)^.allocation) Then
  964. Begin
  965. if PairegAlloc(StartPai)^.Reg = Reg then
  966. begin
  967. FindRegAlloc:=true;
  968. exit;
  969. end;
  970. StartPai := Pai(StartPai^.Next);
  971. End
  972. else
  973. exit;
  974. Until false;
  975. End;
  976. Function TAOptObj.RefsEqual(Const R1, R2: TReference): Boolean;
  977. Begin
  978. If R1.is_immediate Then
  979. RefsEqual := R2.is_immediate and (R1.Offset = R2.Offset)
  980. Else
  981. RefsEqual := (R1.Offset+R1.OffsetFixup = R2.Offset+R2.OffsetFixup)
  982. And (R1.Base = R2.Base)
  983. {$ifdef RefsHaveindex}
  984. And (R1.Index = R2.Index)
  985. {$endif RefsHaveindex}
  986. {$ifdef RefsHaveScale}
  987. And (R1.ScaleFactor = R2.ScaleFactor)
  988. {$endif RefsHaveScale}
  989. And (R1.Symbol = R2.Symbol)
  990. {$ifdef RefsHaveSegment}
  991. And (R1.Segment = R2.Segment)
  992. {$endif RefsHaveSegment}
  993. ;
  994. Function TAOptObj.RegInRef(Reg: TRegister; Const Ref: TReference): Boolean;
  995. Begin
  996. Reg := RegMaxSize(Reg);
  997. RegInRef := (Ref.Base = Reg)
  998. {$ifdef RefsHaveIndexReg}
  999. Or (Ref.Index = Reg)
  1000. {$endif RefsHaveIndexReg}
  1001. End;
  1002. Function TAOptObj.RegModifiedByInstruction(Reg: TRegister; p1: Pai): Boolean;
  1003. Var hp: Pai;
  1004. Begin
  1005. If GetLastInstruction(p1, hp)
  1006. Then
  1007. RegModifiedByInstruction :=
  1008. PPAiProp(p1^.OptInfo)^.GetWState <>
  1009. PPAiProp(hp^.OptInfo)^.GetWState
  1010. Else RegModifiedByInstruction := True;
  1011. End;
  1012. Function TAOptObj.RefInInstruction(Const Ref: TReference; p: Pai): Boolean;
  1013. Var Count: TNatInt;
  1014. TmpResult: Boolean;
  1015. Begin
  1016. TmpResult := False;
  1017. If (p^.typ = ait_instruction) Then
  1018. Begin
  1019. Count := 0;
  1020. Repeat
  1021. If (PInstr(p)^.oper[Count].typ = Top_Ref) Then
  1022. TmpResult := RefsEqual(Ref, PInstr(p)^.oper[Count].ref^);
  1023. Inc(Count);
  1024. Until (Count = MaxOps) or TmpResult;
  1025. End;
  1026. RefInInstruction := TmpResult;
  1027. End;
  1028. Function TAOptObj.RefInSequence(Const Ref: TReference; Content: TContent):
  1029. Boolean;
  1030. Var p: Pai;
  1031. Counter: Byte;
  1032. TmpResult: Boolean;
  1033. Begin
  1034. p := Content.StartMod;
  1035. TmpResult := False;
  1036. Counter := 1;
  1037. While Not(TmpResult) And
  1038. (Counter <= Content.NrOfMods) Do
  1039. Begin
  1040. If (p^.typ = ait_instruction) And
  1041. RefInInstruction(Ref, p)
  1042. Then TmpResult := True;
  1043. Inc(Counter);
  1044. GetNextInstruction(p,p)
  1045. End;
  1046. RefInSequence := TmpResult
  1047. End;
  1048. Function TAOptObj.RegMaxSize(Reg: TRegister): TRegister;
  1049. Begin
  1050. RegMaxSize := Reg
  1051. End;
  1052. Function TAOptObj.RegsSameSize(Reg1, Reg2: TRegister): Boolean;
  1053. Begin
  1054. RegsSameSize := True
  1055. End;
  1056. Function TAOptObj.IsLoadInstr(p: pai): Boolean;
  1057. Begin
  1058. Abstract
  1059. End;
  1060. Function TAOptObj.RegReadByInstr(Reg: TRegister; p: Pai);
  1061. Begin
  1062. Abstract
  1063. End;
  1064. Function TAOptObj.IsStoreInstr(p: pai): Boolean;
  1065. Begin
  1066. Abstract
  1067. End;
  1068. Function TAOptObj.TCh2Reg(Ch: TChange): TRegister;
  1069. Begin
  1070. Abstract
  1071. End;
  1072. End.
  1073. {
  1074. $Log$
  1075. Revision 1.2 1999-08-09 14:07:24 jonas
  1076. commit.msg
  1077. Revision 1.1 1999/08/08 13:24:50 jonas
  1078. + added copyright header/GNU license info
  1079. * made the assembler optimizer almost completely OOP
  1080. * some code style clean up and extra comments
  1081. * moved from the new/aopt to the /new and /new/i386 dirs
  1082. }