aoptobj.pas 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Jonas Maebe, member of the Free Pascal
  4. Development Team
  5. This unit contains the processor independent assembler optimizer
  6. object, base for the dataflow analyzer, peepholeoptimizer and
  7. common subexpression elimination objects.
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. ****************************************************************************
  20. }
  21. Unit AoptObj;
  22. { general, processor independent objects for use by the assembler optimizer }
  23. Interface
  24. uses aasmbase,aasmcpu,aasmtai,
  25. cclasses, cpuinfo, cpubase, aoptbase, aoptcpub;
  26. { ************************************************************************* }
  27. { ********************************* Constants ***************************** }
  28. { ************************************************************************* }
  29. Const
  30. {Possible register content types}
  31. con_Unknown = 0;
  32. con_ref = 1;
  33. con_const = 2;
  34. {***************** Types ****************}
  35. Type
  36. { ************************************************************************* }
  37. { ************************* Some general type definitions ***************** }
  38. { ************************************************************************* }
  39. TRefCompare = Function(const r1, r2: TReference): Boolean;
  40. TRegArray = Array[LoReg..HiReg] of TRegister;
  41. TRegSet = Set of LoReg..HiReg;
  42. { possible actions on an operand: read, write or modify (= read & write) }
  43. TOpAction = (OpAct_Read, OpAct_Write, OpAct_Modify, OpAct_Unknown);
  44. { ************************************************************************* }
  45. { * Object to hold information on which regiters are in use and which not * }
  46. { ************************************************************************* }
  47. TUsedRegs = Object
  48. Constructor init;
  49. Constructor InitWithValue(Const _RegSet: TRegSet);
  50. { update the info with the pairegalloc objects coming after }
  51. { p }
  52. Procedure Update(p: Tai);
  53. { is Reg currently in use }
  54. Function IsUsed(Reg: TRegister): Boolean;
  55. { get all the currently used registers }
  56. Function GetUsedRegs: TRegSet;
  57. Destructor Done;
  58. Private
  59. UsedRegs: TRegSet;
  60. End;
  61. { ************************************************************************* }
  62. { ******************* Contents of the integer registers ******************* }
  63. { ************************************************************************* }
  64. { size of the integer that holds the state number of a register. Can be any }
  65. { integer type, so it can be changed to reduce the size of the TContent }
  66. { structure or to improve alignment }
  67. TStateInt = Byte;
  68. TContent = Packed Record
  69. { start and end of block instructions that defines the }
  70. { content of this register. If Typ = con_const, then }
  71. { Longint(StartMod) = value of the constant) }
  72. StartMod: Tai;
  73. { starts at 0, gets increased everytime the register is }
  74. { written to }
  75. WState: TStateInt;
  76. { starts at 0, gets increased everytime the register is read }
  77. { from }
  78. RState: TStateInt;
  79. { how many instructions starting with StarMod does the block }
  80. { consist of }
  81. NrOfMods: Byte;
  82. { the type of the content of the register: unknown, memory }
  83. { (variable) or constant }
  84. Typ: Byte;
  85. End;
  86. TRegContent = Array[LoGPReg..HiGPReg] Of TContent;
  87. { ************************************************************************** }
  88. { information object with the contents of every register. Every Tai object }
  89. { gets one of these assigned: a pointer to it is stored in the OptInfo field }
  90. { ************************************************************************** }
  91. PPaiProp = ^TPaiProp;
  92. TPaiProp = Object(TAoptBaseCpu)
  93. Regs: TRegContent;
  94. { info about allocation of general purpose integer registers }
  95. UsedRegs: TUsedRegs;
  96. { info about the conditional registers }
  97. CondRegs: TCondRegs;
  98. { can this instruction be removed? }
  99. CanBeRemoved: Boolean;
  100. Constructor init;
  101. { checks the whole sequence of which (so regs[which].StartMod and and }
  102. { the next NrOfMods Tai objects) to see whether Reg is used somewhere, }
  103. { without it being loaded with something else first }
  104. Function RegInSequence(Reg, which: TRegister): Boolean;
  105. { destroy the contents of a register, as well as those whose contents }
  106. { are based on those of that register }
  107. Procedure DestroyReg(Reg: TRegister; var InstrSinceLastMod:
  108. TInstrSinceLastMod);
  109. { if the contents of WhichReg (can be R_NO in case of a constant) are }
  110. { written to memory at the location Ref, the contents of the registers }
  111. { that depend on Ref have to be destroyed }
  112. Procedure DestroyRefs(Const Ref: TReference; WhichReg: TRegister; var
  113. InstrSinceLastMod: TInstrSinceLastMod);
  114. { an instruction reads from operand o }
  115. Procedure ReadOp(const o:toper);
  116. { an instruction reads from reference Ref }
  117. Procedure ReadRef(Ref: PReference);
  118. { an instruction reads from register Reg }
  119. Procedure ReadReg(Reg: TRegister);
  120. { an instruction writes/modifies operand o and this has special }
  121. { side-effects or modifies the contents in such a way that we can't }
  122. { simply add this instruction to the sequence of instructions that }
  123. { describe the contents of the operand, so destroy it }
  124. Procedure DestroyOp(const o:Toper; var InstrSinceLastMod:
  125. TInstrSinceLastMod);
  126. { destroy the contents of all registers }
  127. Procedure DestroyAllRegs(var InstrSinceLastMod: TInstrSinceLastMod);
  128. { a register's contents are modified, but not destroyed (the new value }
  129. { depends on the old one) }
  130. Procedure ModifyReg(reg: TRegister; var InstrSinceLastMod:
  131. TInstrSinceLastMod);
  132. { an operand's contents are modified, but not destroyed (the new value }
  133. { depends on the old one) }
  134. Procedure ModifyOp(const oper: TOper; var InstrSinceLastMod:
  135. TInstrSinceLastMod);
  136. { increase the write state of a register (call every time a register is }
  137. { written to) }
  138. Procedure IncWState(Reg: TRegister);
  139. { increase the read state of a register (call every time a register is }
  140. { read from) }
  141. Procedure IncRState(Reg: TRegister);
  142. { get the write state of a register }
  143. Function GetWState(Reg: TRegister): TStateInt;
  144. { get the read state of a register }
  145. Function GetRState(Reg: TRegister): TStateInt;
  146. { get the type of contents of a register }
  147. Function GetRegContentType(Reg: TRegister): Byte;
  148. Destructor Done;
  149. Private
  150. Procedure IncState(var s: TStateInt);
  151. { returns whether the reference Ref is used somewhere in the loading }
  152. { sequence Content }
  153. Function RefInSequence(Const Ref: TReference; Content: TContent;
  154. RefsEq: TRefCompare): Boolean;
  155. { returns whether the instruction P reads from and/or writes }
  156. { to Reg }
  157. Function RefInInstruction(Const Ref: TReference; p: Tai;
  158. RefsEq: TRefCompare): Boolean;
  159. { returns whether two references with at least one pointing to an array }
  160. { may point to the same memory location }
  161. End;
  162. { ************************************************************************* }
  163. { ************************ Label information ****************************** }
  164. { ************************************************************************* }
  165. TLabelTableItem = Record
  166. PaiObj: Tai;
  167. End;
  168. {$ifndef TP}
  169. TLabelTable = Array[0..2500000] Of TLabelTableItem;
  170. {$else TP}
  171. TLabelTable = Array[0..(65520 div sizeof(TLabelTableItem))] Of TLabelTableItem;
  172. {$endif TP}
  173. PLabelTable = ^TLabelTable;
  174. PLabelInfo = ^TLabelInfo;
  175. TLabelInfo = Record
  176. { the highest and lowest label number occurring in the current code }
  177. { fragment }
  178. LowLabel, HighLabel: AWord;
  179. LabelDif: AWord;
  180. { table that contains the addresses of the Pai_Label objects associated }
  181. { with each label number }
  182. LabelTable: PLabelTable;
  183. End;
  184. { ************************************************************************* }
  185. { ********** General optimizer object, used to derive others from ********* }
  186. { ************************************************************************* }
  187. TAOptObj = Object(TAoptBaseCpu)
  188. { the PAasmOutput list this optimizer instance works on }
  189. AsmL: TAasmOutput;
  190. { The labelinfo record contains the addresses of the Tai objects }
  191. { that are labels, how many labels there are and the min and max }
  192. { label numbers }
  193. LabelInfo: PLabelInfo;
  194. { Start and end of the block that is currently being optimized }
  195. BlockStart, BlockEnd: Tai;
  196. { _AsmL is the PAasmOutpout list that has to be optimized, }
  197. { _BlockStart and _BlockEnd the start and the end of the block }
  198. { that has to be optimized and _LabelInfo a pointer to a }
  199. { TLabelInfo record }
  200. Constructor Init(_AsmL: TAasmOutput; _BlockStart, _BlockEnd: Tai;
  201. _LabelInfo: PLabelInfo);
  202. { processor independent methods }
  203. { returns true if the label L is found between hp and the next }
  204. { instruction }
  205. Function FindLabel(L: TasmLabel; Var hp: Tai): Boolean;
  206. { inserts new_one between prev and foll in AsmL }
  207. Procedure InsertLLItem(prev, foll, new_one: TLinkedListItem);
  208. { If P is a Tai object releveant to the optimizer, P is returned }
  209. { If it is not relevant tot he optimizer, the first object after P }
  210. { that is relevant is returned }
  211. Function SkipHead(P: Tai): Tai;
  212. { returns true if the operands o1 and o2 are completely equal }
  213. Function OpsEqual(const o1,o2:toper): Boolean;
  214. { Returns true if a ait_alloc object for Reg is found in the block }
  215. { of Tai's starting with StartPai and ending with the next "real" }
  216. { instruction }
  217. Function FindRegAlloc(Reg: TRegister; StartPai: Tai): Boolean;
  218. { processor dependent methods }
  219. End;
  220. Function ArrayRefsEq(const r1, r2: TReference): Boolean;
  221. { ***************************** Implementation **************************** }
  222. Implementation
  223. uses globtype, globals, cgbase;
  224. { ************************************************************************* }
  225. { ******************************** TUsedRegs ****************************** }
  226. { ************************************************************************* }
  227. Constructor TUsedRegs.init;
  228. Begin
  229. UsedRegs := [];
  230. End;
  231. Constructor TUsedRegs.InitWithValue(Const _RegSet: TRegSet);
  232. Begin
  233. UsedRegs := _RegSet;
  234. End;
  235. Procedure TUsedRegs.Update(p: Tai);
  236. {updates UsedRegs with the RegAlloc Information coming after P}
  237. Begin
  238. Repeat
  239. While Assigned(p) And
  240. ((p.typ in (SkipInstr - [ait_RegAlloc])) or
  241. ((p.typ = ait_label) And
  242. Not(Tai_Label(p).l.is_used))) Do
  243. p := Tai(p.next);
  244. While Assigned(p) And
  245. (p.typ=ait_RegAlloc) Do
  246. Begin
  247. if tai_regalloc(p).allocation then
  248. UsedRegs := UsedRegs + [tai_regalloc(p).Reg]
  249. else
  250. UsedRegs := UsedRegs - [tai_regalloc(p).Reg];
  251. p := Tai(p.next);
  252. End;
  253. Until Not(Assigned(p)) Or
  254. (Not(p.typ in SkipInstr) And
  255. Not((p.typ = ait_label) And
  256. Not(Tai_Label(p).l.is_used)));
  257. End;
  258. Function TUsedRegs.IsUsed(Reg: TRegister): Boolean;
  259. Begin
  260. IsUsed := Reg in UsedRegs
  261. End;
  262. Function TUsedRegs.GetUsedRegs: TRegSet;
  263. Begin
  264. GetUsedRegs := UsedRegs;
  265. End;
  266. Destructor TUsedRegs.Done; {$ifdef inl} inline; {$endif inl}
  267. Begin
  268. end;
  269. { ************************************************************************* }
  270. { **************************** TPaiProp *********************************** }
  271. { ************************************************************************* }
  272. Constructor TPaiProp.Init;
  273. Begin
  274. UsedRegs.Init;
  275. CondRegs.init;
  276. { DirFlag: TFlagContents; I386 specific}
  277. End;
  278. Function TPaiProp.RegInSequence(Reg, which: TRegister): Boolean;
  279. Var p: Tai;
  280. RegsChecked: TRegSet;
  281. content: TContent;
  282. Counter: Byte;
  283. TmpResult: Boolean;
  284. Begin
  285. RegsChecked := [];
  286. content := regs[which];
  287. p := content.StartMod;
  288. TmpResult := False;
  289. Counter := 1;
  290. While Not(TmpResult) And
  291. (Counter <= Content.NrOfMods) Do
  292. Begin
  293. If IsLoadMemReg(p) Then
  294. With PInstr(p)^.oper[LoadSrc].ref^ Do
  295. If (Base = ProcInfo.FramePointer)
  296. {$ifdef RefsHaveIndexReg}
  297. And (Index = R_NO)
  298. {$endif RefsHaveIndexReg} Then
  299. Begin
  300. RegsChecked := RegsChecked +
  301. [RegMaxSize(PInstr(p)^.oper[LoadDst].reg)];
  302. If Reg = RegMaxSize(PInstr(p)^.oper[LoadDst].reg) Then
  303. Break;
  304. End
  305. Else
  306. Begin
  307. If (Base = Reg) And
  308. Not(Base In RegsChecked)
  309. Then TmpResult := True;
  310. {$ifdef RefsHaveIndexReg}
  311. If Not(TmpResult) And
  312. (Index = Reg) And
  313. Not(Index In RegsChecked)
  314. Then TmpResult := True;
  315. {$Endif RefsHaveIndexReg}
  316. End
  317. Else TmpResult := RegInInstruction(Reg, p);
  318. Inc(Counter);
  319. GetNextInstruction(p,p)
  320. End;
  321. RegInSequence := TmpResult
  322. End;
  323. Procedure TPaiProp.DestroyReg(Reg: TRegister; var InstrSinceLastMod:
  324. TInstrSinceLastMod);
  325. { Destroys the contents of the register Reg in the PPaiProp p1, as well as }
  326. { the contents of registers are loaded with a memory location based on Reg }
  327. Var TmpWState, TmpRState: Byte;
  328. Counter: TRegister;
  329. Begin
  330. Reg := RegMaxSize(Reg);
  331. If (Reg in [LoGPReg..HiGPReg]) Then
  332. For Counter := LoGPReg to HiGPReg Do
  333. With Regs[Counter] Do
  334. If (Counter = reg) Or
  335. ((Typ = Con_Ref) And
  336. RegInSequence(Reg, Counter)) Then
  337. Begin
  338. InstrSinceLastMod[Counter] := 0;
  339. IncWState(Counter);
  340. TmpWState := GetWState(Counter);
  341. TmpRState := GetRState(Counter);
  342. FillChar(Regs[Counter], SizeOf(TContent), 0);
  343. WState := TmpWState;
  344. RState := TmpRState
  345. End
  346. End;
  347. Function ArrayRefsEq(const r1, r2: TReference): Boolean;
  348. Begin
  349. ArrayRefsEq := (R1.Offset+R1.OffsetFixup = R2.Offset+R2.OffsetFixup) And
  350. {$ifdef refsHaveSegmentReg}
  351. (R1.Segment = R2.Segment) And
  352. {$endif}
  353. (R1.Base = R2.Base) And
  354. (R1.Symbol=R2.Symbol);
  355. End;
  356. Procedure TPaiProp.DestroyRefs(Const Ref: TReference; WhichReg: TRegister;
  357. var InstrSinceLastMod: TInstrSinceLastMod);
  358. { destroys all registers which possibly contain a reference to Ref, WhichReg }
  359. { is the register whose contents are being written to memory (if this proc }
  360. { is called because of a "mov?? %reg, (mem)" instruction) }
  361. Var RefsEq: TRefCompare;
  362. Counter: TRegister;
  363. Begin
  364. WhichReg := RegMaxSize(WhichReg);
  365. If (Ref.base = procinfo.FramePointer) or
  366. Assigned(Ref.Symbol) Then
  367. Begin
  368. If
  369. {$ifdef refsHaveIndexReg}
  370. (Ref.Index = R_NO) And
  371. {$endif refsHaveIndexReg}
  372. (Not(Assigned(Ref.Symbol)) or
  373. (Ref.base = R_NO)) Then
  374. { local variable which is not an array }
  375. RefsEq := {$ifdef fpc}@{$endif}RefsEqual
  376. Else
  377. { local variable which is an array }
  378. RefsEq := {$ifdef fpc}@{$endif}ArrayRefsEq;
  379. {write something to a parameter, a local or global variable, so
  380. * with uncertain optimizations on:
  381. - destroy the contents of registers whose contents have somewhere a
  382. "mov?? (Ref), %reg". WhichReg (this is the register whose contents
  383. are being written to memory) is not destroyed if it's StartMod is
  384. of that form and NrOfMods = 1 (so if it holds ref, but is not a
  385. pointer or value based on Ref)
  386. * with uncertain optimizations off:
  387. - also destroy registers that contain any pointer}
  388. For Counter := LoGPReg to HiGPReg Do
  389. With Regs[Counter] Do
  390. Begin
  391. If (typ = Con_Ref) And
  392. ((Not(cs_UncertainOpts in aktglobalswitches) And
  393. (NrOfMods <> 1)
  394. ) Or
  395. (RefInSequence(Ref,Regs[Counter], RefsEq) And
  396. ((Counter <> WhichReg) Or
  397. ((NrOfMods <> 1) And
  398. {StarMod is always of the type ait_instruction}
  399. (PInstr(StartMod)^.oper[0].typ = top_ref) And
  400. RefsEq(PInstr(StartMod)^.oper[0].ref^, Ref)
  401. )
  402. )
  403. )
  404. )
  405. Then
  406. DestroyReg(Counter, InstrSinceLastMod)
  407. End
  408. End
  409. Else
  410. {write something to a pointer location, so
  411. * with uncertain optimzations on:
  412. - do not destroy registers which contain a local/global variable or a
  413. parameter, except if DestroyRefs is called because of a "movsl"
  414. * with uncertain optimzations off:
  415. - destroy every register which contains a memory location
  416. }
  417. For Counter := LoGPReg to HiGPReg Do
  418. With Regs[Counter] Do
  419. If (typ = Con_Ref) And
  420. (Not(cs_UncertainOpts in aktglobalswitches) Or
  421. {$ifdef i386}
  422. {for movsl}
  423. (Ref.Base = R_EDI) Or
  424. {$endif}
  425. {don't destroy if reg contains a parameter, local or global variable}
  426. Not((NrOfMods = 1) And
  427. (PInstr(StartMod)^.oper[0].typ = top_ref) And
  428. ((PInstr(StartMod)^.oper[0].ref^.base = ProcInfo.FramePointer) Or
  429. Assigned(PInstr(StartMod)^.oper[0].ref^.Symbol)
  430. )
  431. )
  432. )
  433. Then DestroyReg(Counter, InstrSinceLastMod)
  434. End;
  435. Procedure TPaiProp.DestroyAllRegs(var InstrSinceLastMod: TInstrSinceLastMod);
  436. Var Counter: TRegister;
  437. Begin {initializes/desrtoys all registers}
  438. For Counter := LoGPReg To HiGPReg Do
  439. Begin
  440. ReadReg(Counter);
  441. DestroyReg(Counter, InstrSinceLastMod);
  442. End;
  443. CondRegs.Init;
  444. { FPURegs.Init; }
  445. End;
  446. Procedure TPaiProp.DestroyOp(const o:Toper; var InstrSinceLastMod:
  447. TInstrSinceLastMod);
  448. Begin
  449. Case o.typ Of
  450. top_reg: DestroyReg(o.reg, InstrSinceLastMod);
  451. top_ref:
  452. Begin
  453. ReadRef(o.ref);
  454. DestroyRefs(o.ref^, R_NO, InstrSinceLastMod);
  455. End;
  456. top_symbol:;
  457. End;
  458. End;
  459. Procedure TPaiProp.ReadReg(Reg: TRegister);
  460. Begin
  461. Reg := RegMaxSize(Reg);
  462. If Reg in General_Registers Then
  463. IncRState(RegMaxSize(Reg))
  464. End;
  465. Procedure TPaiProp.ReadRef(Ref: PReference);
  466. Begin
  467. If Ref^.Base <> R_NO Then
  468. ReadReg(Ref^.Base);
  469. {$ifdef refsHaveIndexReg}
  470. If Ref^.Index <> R_NO Then
  471. ReadReg(Ref^.Index);
  472. {$endif}
  473. End;
  474. Procedure TPaiProp.ReadOp(const o:toper);
  475. Begin
  476. Case o.typ Of
  477. top_reg: ReadReg(o.reg);
  478. top_ref: ReadRef(o.ref);
  479. top_symbol : ;
  480. End;
  481. End;
  482. Procedure TPaiProp.ModifyReg(reg: TRegister; Var InstrSinceLastMod:
  483. TInstrSinceLastMod);
  484. Begin
  485. With Regs[reg] Do
  486. If (Typ = Con_Ref)
  487. Then
  488. Begin
  489. IncState(WState);
  490. {also store how many instructions are part of the sequence in the first
  491. instructions PPaiProp, so it can be easily accessed from within
  492. CheckSequence}
  493. Inc(NrOfMods, InstrSinceLastMod[Reg]);
  494. PPaiProp(StartMod.OptInfo)^.Regs[Reg].NrOfMods := NrOfMods;
  495. InstrSinceLastMod[Reg] := 0;
  496. End
  497. Else
  498. DestroyReg(Reg, InstrSinceLastMod);
  499. End;
  500. Procedure TPaiProp.ModifyOp(const oper: TOper; var InstrSinceLastMod:
  501. TInstrSinceLastMod);
  502. Begin
  503. If oper.typ = top_reg Then
  504. ModifyReg(RegMaxSize(oper.reg),InstrSinceLastMod)
  505. Else
  506. Begin
  507. ReadOp(oper);
  508. DestroyOp(oper, InstrSinceLastMod);
  509. End
  510. End;
  511. Procedure TPaiProp.IncWState(Reg: TRegister);{$ifdef inl} inline;{$endif inl}
  512. Begin
  513. IncState(Regs[Reg].WState);
  514. End;
  515. Procedure TPaiProp.IncRState(Reg: TRegister);{$ifdef inl} inline;{$endif inl}
  516. Begin
  517. IncState(Regs[Reg].RState);
  518. End;
  519. Function TPaiProp.GetWState(Reg: TRegister): TStateInt; {$ifdef inl} inline;{$endif inl}
  520. Begin
  521. GetWState := Regs[Reg].WState
  522. End;
  523. Function TPaiProp.GetRState(Reg: TRegister): TStateInt; {$ifdef inl} inline;{$endif inl}
  524. Begin
  525. GetRState := Regs[Reg].RState
  526. End;
  527. Function TPaiProp.GetRegContentType(Reg: TRegister): Byte; {$ifdef inl} inline;{$endif inl}
  528. Begin
  529. GetRegContentType := Regs[Reg].typ
  530. End;
  531. Destructor TPaiProp.Done;
  532. Begin
  533. UsedRegs.Done;
  534. CondRegs.Done;
  535. { DirFlag: TFlagContents; I386 specific}
  536. End;
  537. { ************************ private TPaiProp stuff ************************* }
  538. Procedure TPaiProp.IncState(Var s: TStateInt); {$ifdef inl} inline;{$endif inl}
  539. Begin
  540. If s <> High(TStateInt) Then Inc(s)
  541. Else s := 0
  542. End;
  543. Function TPaiProp.RefInInstruction(Const Ref: TReference; p: Tai;
  544. RefsEq: TRefCompare): Boolean;
  545. Var Count: AWord;
  546. TmpResult: Boolean;
  547. Begin
  548. TmpResult := False;
  549. If (p.typ = ait_instruction) Then
  550. Begin
  551. Count := 0;
  552. Repeat
  553. If (TInstr(p).oper[Count].typ = Top_Ref) Then
  554. TmpResult := RefsEq(Ref, PInstr(p)^.oper[Count].ref^);
  555. Inc(Count);
  556. Until (Count = MaxOps) or TmpResult;
  557. End;
  558. RefInInstruction := TmpResult;
  559. End;
  560. Function TPaiProp.RefInSequence(Const Ref: TReference; Content: TContent;
  561. RefsEq: TRefCompare): Boolean;
  562. Var p: Tai;
  563. Counter: Byte;
  564. TmpResult: Boolean;
  565. Begin
  566. p := Content.StartMod;
  567. TmpResult := False;
  568. Counter := 1;
  569. While Not(TmpResult) And
  570. (Counter <= Content.NrOfMods) Do
  571. Begin
  572. If (p.typ = ait_instruction) And
  573. RefInInstruction(Ref, p, {$ifdef fpc}@{$endif}RefsEqual)
  574. Then TmpResult := True;
  575. Inc(Counter);
  576. GetNextInstruction(p,p)
  577. End;
  578. RefInSequence := TmpResult
  579. End;
  580. { ************************************************************************* }
  581. { ***************************** TAoptObj ********************************** }
  582. { ************************************************************************* }
  583. Constructor TAoptObj.Init(_AsmL: TAasmOutput; _BlockStart, _BlockEnd: Tai;
  584. _LabelInfo: PLabelInfo);
  585. Begin
  586. AsmL := _AsmL;
  587. BlockStart := _BlockStart;
  588. BlockEnd := _BlockEnd;
  589. LabelInfo := _LabelInfo
  590. End;
  591. Function TAOptObj.FindLabel(L: TasmLabel; Var hp: Tai): Boolean;
  592. Var TempP: Tai;
  593. Begin
  594. TempP := hp;
  595. While Assigned(TempP) and
  596. (TempP.typ In SkipInstr + [ait_label]) Do
  597. If (TempP.typ <> ait_Label) Or
  598. (Tai_label(TempP).l <> L)
  599. Then GetNextInstruction(TempP, TempP)
  600. Else
  601. Begin
  602. hp := TempP;
  603. FindLabel := True;
  604. exit
  605. End;
  606. FindLabel := False;
  607. End;
  608. Procedure TAOptObj.InsertLLItem(prev, foll, new_one : TLinkedListItem);
  609. Begin
  610. If Assigned(prev) Then
  611. If Assigned(foll) Then
  612. Begin
  613. If Assigned(new_one) Then
  614. Begin
  615. new_one.previous := prev;
  616. new_one.next := foll;
  617. prev.next := new_one;
  618. foll.previous := new_one;
  619. Tailineinfo(new_one).fileinfo := Tailineinfo(foll).fileinfo
  620. End
  621. End
  622. Else AsmL.Concat(new_one)
  623. Else If Assigned(Foll) Then AsmL.Insert(new_one)
  624. End;
  625. Function TAOptObj.SkipHead(P: Tai): Tai;
  626. Var OldP: Tai;
  627. Begin
  628. Repeat
  629. OldP := P;
  630. If (P.typ in SkipInstr) Or
  631. ((P.typ = ait_marker) And
  632. (Tai_Marker(P).Kind = AsmBlockEnd)) Then
  633. GetNextInstruction(P, P)
  634. Else If ((P.Typ = Ait_Marker) And
  635. (Tai_Marker(P).Kind = NoPropInfoStart)) Then
  636. { a marker of the type NoPropInfoStart can't be the first instruction of a }
  637. { paasmoutput list }
  638. GetNextInstruction(Tai(P.Previous),P);
  639. If (P.Typ = Ait_Marker) And
  640. (Tai_Marker(P).Kind = AsmBlockStart) Then
  641. Begin
  642. P := Tai(P.Next);
  643. While (P.typ <> Ait_Marker) Or
  644. (Tai_Marker(P).Kind <> AsmBlockEnd) Do
  645. P := Tai(P.Next)
  646. End;
  647. Until P = OldP;
  648. SkipHead := P;
  649. End;
  650. Function TAOptObj.OpsEqual(const o1,o2:toper): Boolean;
  651. Begin
  652. if o1.typ=o2.typ then
  653. Case o1.typ Of
  654. Top_Reg :
  655. OpsEqual:=o1.reg=o2.reg;
  656. Top_Ref :
  657. OpsEqual := RefsEqual(o1.ref^, o2.ref^);
  658. Top_Const :
  659. OpsEqual:=o1.val=o2.val;
  660. Top_Symbol :
  661. OpsEqual:=(o1.sym=o2.sym) and (o1.symofs=o2.symofs);
  662. Top_None :
  663. OpsEqual := True
  664. else OpsEqual := False
  665. End;
  666. End;
  667. Function TAOptObj.FindRegAlloc(Reg: TRegister; StartPai: Tai): Boolean;
  668. Begin
  669. FindRegAlloc:=False;
  670. Repeat
  671. While Assigned(StartPai) And
  672. ((StartPai.typ in (SkipInstr - [ait_regAlloc])) Or
  673. ((StartPai.typ = ait_label) and
  674. Not(Tai_Label(StartPai).l.Is_Used))) Do
  675. StartPai := Tai(StartPai.Next);
  676. If Assigned(StartPai) And
  677. (StartPai.typ = ait_regAlloc) and (tai_regalloc(StartPai).allocation) Then
  678. Begin
  679. if tai_regalloc(StartPai).Reg = Reg then
  680. begin
  681. FindRegAlloc:=true;
  682. exit;
  683. end;
  684. StartPai := Tai(StartPai.Next);
  685. End
  686. else
  687. exit;
  688. Until false;
  689. End;
  690. End.
  691. {
  692. $Log$
  693. Revision 1.8 2002-11-18 17:31:54 peter
  694. * pass proccalloption to ret_in_xxx and push_xxx functions
  695. Revision 1.7 2002/08/18 18:16:55 florian
  696. * fixed compilation error
  697. Revision 1.6 2002/07/07 09:52:32 florian
  698. * powerpc target fixed, very simple units can be compiled
  699. * some basic stuff for better callparanode handling, far from being finished
  700. Revision 1.5 2002/07/01 18:46:21 peter
  701. * internal linker
  702. * reorganized aasm layer
  703. Revision 1.4 2002/05/18 13:34:05 peter
  704. * readded missing revisions
  705. Revision 1.3 2002/05/16 19:46:35 carl
  706. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  707. + try to fix temp allocation (still in ifdef)
  708. + generic constructor calls
  709. + start of tassembler / tmodulebase class cleanup
  710. }