daopt386.pas 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Jonas Maebe, member of the Freepascal
  4. development team
  5. This unit contains the data flow analyzer and several helper procedures
  6. and functions.
  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 DAOpt386;
  21. {$i fpcdefs.inc}
  22. Interface
  23. Uses
  24. GlobType,
  25. CClasses,Aasmbase,aasmtai,aasmcpu,
  26. cpubase,optbase;
  27. {******************************* Constants *******************************}
  28. Const
  29. {Possible register content types}
  30. con_Unknown = 0;
  31. con_ref = 1;
  32. con_const = 2;
  33. { The contents aren't usable anymore for CSE, but they may still be }
  34. { usefull for detecting whether the result of a load is actually used }
  35. con_invalid = 3;
  36. { the reverse of the above (in case a (conditional) jump is encountered): }
  37. { CSE is still possible, but the original instruction can't be removed }
  38. con_noRemoveRef = 4;
  39. { same, but for constants }
  40. con_noRemoveConst = 5;
  41. {********************************* Types *********************************}
  42. type
  43. TRegArray = Array[R_EAX..R_BL] of TRegister;
  44. TRegSet = Set of R_EAX..R_BL;
  45. TRegInfo = Record
  46. NewRegsEncountered, OldRegsEncountered: TRegSet;
  47. RegsLoadedForRef: TRegSet;
  48. regsStillUsedAfterSeq: TRegSet;
  49. lastReload: array[R_EAX..R_EDI] of Tai;
  50. New2OldReg: TRegArray;
  51. End;
  52. {possible actions on an operand: read, write or modify (= read & write)}
  53. TOpAction = (OpAct_Read, OpAct_Write, OpAct_Modify, OpAct_Unknown);
  54. {the possible states of a flag}
  55. TFlagContents = (F_Unknown, F_NotSet, F_Set);
  56. TContent = Packed Record
  57. {start and end of block instructions that defines the
  58. content of this register.}
  59. StartMod: Tai;
  60. MemWrite: Taicpu;
  61. {how many instructions starting with StarMod does the block consist of}
  62. NrOfMods: Byte;
  63. {the type of the content of the register: unknown, memory, constant}
  64. Typ: Byte;
  65. case byte of
  66. {starts at 0, gets increased everytime the register is written to}
  67. 1: (WState: Byte;
  68. {starts at 0, gets increased everytime the register is read from}
  69. RState: Byte);
  70. { to compare both states in one operation }
  71. 2: (state: word);
  72. End;
  73. {Contents of the integer registers}
  74. TRegContent = Array[R_EAX..R_EDI] Of TContent;
  75. {contents of the FPU registers}
  76. TRegFPUContent = Array[R_ST..R_ST7] Of TContent;
  77. {$ifdef tempOpts}
  78. { linked list which allows searching/deleting based on value, no extra frills}
  79. PSearchLinkedListItem = ^TSearchLinkedListItem;
  80. TSearchLinkedListItem = object(TLinkedList_Item)
  81. constructor init;
  82. function equals(p: PSearchLinkedListItem): boolean; virtual;
  83. end;
  84. PSearchDoubleIntItem = ^TSearchDoubleInttem;
  85. TSearchDoubleIntItem = object(TLinkedList_Item)
  86. constructor init(_int1,_int2: longint);
  87. function equals(p: PSearchLinkedListItem): boolean; virtual;
  88. private
  89. int1, int2: longint;
  90. end;
  91. PSearchLinkedList = ^TSearchLinkedList;
  92. TSearchLinkedList = object(TLinkedList)
  93. function searchByValue(p: PSearchLinkedListItem): boolean;
  94. procedure removeByValue(p: PSearchLinkedListItem);
  95. end;
  96. {$endif tempOpts}
  97. {information record with the contents of every register. Every Tai object
  98. gets one of these assigned: a pointer to it is stored in the OptInfo field}
  99. TTaiProp = Record
  100. Regs: TRegContent;
  101. { FPURegs: TRegFPUContent;} {currently not yet used}
  102. { allocated Registers }
  103. UsedRegs: TRegSet;
  104. { status of the direction flag }
  105. DirFlag: TFlagContents;
  106. {$ifdef tempOpts}
  107. { currently used temps }
  108. tempAllocs: PSearchLinkedList;
  109. {$endif tempOpts}
  110. { can this instruction be removed? }
  111. CanBeRemoved: Boolean;
  112. { are the resultflags set by this instruction used? }
  113. FlagsUsed: Boolean;
  114. End;
  115. PTaiProp = ^TTaiProp;
  116. TTaiPropBlock = Array[1..250000] Of TTaiProp;
  117. PTaiPropBlock = ^TTaiPropBlock;
  118. TInstrSinceLastMod = Array[R_EAX..R_EDI] Of Byte;
  119. TLabelTableItem = Record
  120. TaiObj: Tai;
  121. {$IfDef JumpAnal}
  122. InstrNr: Longint;
  123. RefsFound: Word;
  124. JmpsProcessed: Word
  125. {$EndIf JumpAnal}
  126. End;
  127. TLabelTable = Array[0..2500000] Of TLabelTableItem;
  128. PLabelTable = ^TLabelTable;
  129. {*********************** Procedures and Functions ************************}
  130. Procedure InsertLLItem(AsmL: TAAsmOutput; prev, foll, new_one: TLinkedListItem);
  131. Function Reg32(Reg: TRegister): TRegister;
  132. Function RefsEquivalent(Const R1, R2: TReference; Var RegInfo: TRegInfo; OpAct: TOpAction): Boolean;
  133. Function RefsEqual(Const R1, R2: TReference): Boolean;
  134. Function IsGP32Reg(Reg: TRegister): Boolean;
  135. Function RegInRef(Reg: TRegister; Const Ref: TReference): Boolean;
  136. function RegReadByInstruction(reg: TRegister; hp: Tai): boolean;
  137. function RegModifiedByInstruction(Reg: TRegister; p1: Tai): Boolean;
  138. function RegInInstruction(Reg: TRegister; p1: Tai): Boolean;
  139. function RegInOp(Reg: TRegister; const o:toper): Boolean;
  140. function instrWritesFlags(p: Tai): boolean;
  141. function instrReadsFlags(p: Tai): boolean;
  142. function writeToMemDestroysContents(regWritten: tregister; const ref: treference;
  143. reg: tregister; const c: tcontent; var invalsmemwrite: boolean): boolean;
  144. function writeToRegDestroysContents(destReg: tregister; reg: tregister;
  145. const c: tcontent): boolean;
  146. function writeDestroysContents(const op: toper; reg: tregister;
  147. const c: tcontent): boolean;
  148. Function GetNextInstruction(Current: Tai; Var Next: Tai): Boolean;
  149. Function GetLastInstruction(Current: Tai; Var Last: Tai): Boolean;
  150. Procedure SkipHead(var P: Tai);
  151. function labelCanBeSkipped(p: Tai_label): boolean;
  152. Procedure RemoveLastDeallocForFuncRes(asmL: TAAsmOutput; p: Tai);
  153. Function regLoadedWithNewValue(reg: tregister; canDependOnPrevValue: boolean;
  154. hp: Tai): boolean;
  155. Procedure UpdateUsedRegs(Var UsedRegs: TRegSet; p: Tai);
  156. Procedure AllocRegBetween(AsmL: TAAsmOutput; Reg: TRegister; p1, p2: Tai);
  157. function FindRegDealloc(reg: tregister; p: Tai): boolean;
  158. Function RegsEquivalent(OldReg, NewReg: TRegister; Var RegInfo: TRegInfo; OpAct: TopAction): Boolean;
  159. Function InstructionsEquivalent(p1, p2: Tai; Var RegInfo: TRegInfo): Boolean;
  160. function sizescompatible(loadsize,newsize: topsize): boolean;
  161. Function OpsEqual(const o1,o2:toper): Boolean;
  162. Function DFAPass1(AsmL: TAAsmOutput; BlockStart: Tai): Tai;
  163. Function DFAPass2(
  164. {$ifdef statedebug}
  165. AsmL: TAAsmOutPut;
  166. {$endif statedebug}
  167. BlockStart, BlockEnd: Tai): Boolean;
  168. Procedure ShutDownDFA;
  169. Function FindLabel(L: tasmlabel; Var hp: Tai): Boolean;
  170. Procedure IncState(Var S: Byte; amount: longint);
  171. {******************************* Variables *******************************}
  172. Var
  173. {the amount of TaiObjects in the current assembler list}
  174. NrOfTaiObjs: Longint;
  175. {Array which holds all TTaiProps}
  176. TaiPropBlock: PTaiPropBlock;
  177. LoLab, HiLab, LabDif: Longint;
  178. LTable: PLabelTable;
  179. {*********************** End of Interface section ************************}
  180. Implementation
  181. Uses
  182. globals, systems, verbose, cgbase, symconst, symsym, cginfo, cgobj,
  183. rgobj;
  184. Type
  185. TRefCompare = function(const r1, r2: TReference): Boolean;
  186. Var
  187. {How many instructions are between the current instruction and the last one
  188. that modified the register}
  189. NrOfInstrSinceLastMod: TInstrSinceLastMod;
  190. {$ifdef tempOpts}
  191. constructor TSearchLinkedListItem.init;
  192. begin
  193. end;
  194. function TSearchLinkedListItem.equals(p: PSearchLinkedListItem): boolean;
  195. begin
  196. equals := false;
  197. end;
  198. constructor TSearchDoubleIntItem.init(_int1,_int2: longint);
  199. begin
  200. int1 := _int1;
  201. int2 := _int2;
  202. end;
  203. function TSearchDoubleIntItem.equals(p: PSearchLinkedListItem): boolean;
  204. begin
  205. equals := (TSearchDoubleIntItem(p).int1 = int1) and
  206. (TSearchDoubleIntItem(p).int2 = int2);
  207. end;
  208. function TSearchLinkedList.searchByValue(p: PSearchLinkedListItem): boolean;
  209. var temp: PSearchLinkedListItem;
  210. begin
  211. temp := first;
  212. while (temp <> last.next) and
  213. not(temp.equals(p)) do
  214. temp := temp.next;
  215. searchByValue := temp <> last.next;
  216. end;
  217. procedure TSearchLinkedList.removeByValue(p: PSearchLinkedListItem);
  218. begin
  219. temp := first;
  220. while (temp <> last.next) and
  221. not(temp.equals(p)) do
  222. temp := temp.next;
  223. if temp <> last.next then
  224. begin
  225. remove(temp);
  226. dispose(temp,done);
  227. end;
  228. end;
  229. Procedure updateTempAllocs(Var UsedRegs: TRegSet; p: Tai);
  230. {updates UsedRegs with the RegAlloc Information coming after P}
  231. Begin
  232. Repeat
  233. While Assigned(p) And
  234. ((p.typ in (SkipInstr - [ait_RegAlloc])) or
  235. ((p.typ = ait_label) And
  236. labelCanBeSkipped(Tai_label(current)))) Do
  237. p := Tai(p.next);
  238. While Assigned(p) And
  239. (p.typ=ait_RegAlloc) Do
  240. Begin
  241. if tai_regalloc(p).allocation then
  242. UsedRegs := UsedRegs + [tai_regalloc(p).Reg]
  243. else
  244. UsedRegs := UsedRegs - [tai_regalloc(p).Reg];
  245. p := Tai(p.next);
  246. End;
  247. Until Not(Assigned(p)) Or
  248. (Not(p.typ in SkipInstr) And
  249. Not((p.typ = ait_label) And
  250. labelCanBeSkipped(Tai_label(current))));
  251. End;
  252. {$endif tempOpts}
  253. {************************ Create the Label table ************************}
  254. Function FindLoHiLabels(Var LowLabel, HighLabel, LabelDif: Longint; BlockStart: Tai): Tai;
  255. {Walks through the TAAsmlist to find the lowest and highest label number}
  256. Var LabelFound: Boolean;
  257. P, lastP: Tai;
  258. Begin
  259. LabelFound := False;
  260. LowLabel := MaxLongint;
  261. HighLabel := 0;
  262. P := BlockStart;
  263. lastP := p;
  264. While Assigned(P) Do
  265. Begin
  266. If (Tai(p).typ = ait_label) Then
  267. If not labelCanBeSkipped(Tai_label(p))
  268. Then
  269. Begin
  270. LabelFound := True;
  271. If (Tai_Label(p).l.labelnr < LowLabel) Then
  272. LowLabel := Tai_Label(p).l.labelnr;
  273. If (Tai_Label(p).l.labelnr > HighLabel) Then
  274. HighLabel := Tai_Label(p).l.labelnr;
  275. End;
  276. lastP := p;
  277. GetNextInstruction(p, p);
  278. End;
  279. if (lastP.typ = ait_marker) and
  280. (Tai_marker(lastp).kind = asmBlockStart) then
  281. FindLoHiLabels := lastP
  282. else FindLoHiLabels := nil;
  283. If LabelFound
  284. Then LabelDif := HighLabel+1-LowLabel
  285. Else LabelDif := 0;
  286. End;
  287. Function FindRegAlloc(Reg: TRegister; StartTai: Tai; alloc: boolean): Boolean;
  288. { Returns true if a ait_alloc object for Reg is found in the block of Tai's }
  289. { starting with StartTai and ending with the next "real" instruction }
  290. Begin
  291. FindRegAlloc := false;
  292. Repeat
  293. While Assigned(StartTai) And
  294. ((StartTai.typ in (SkipInstr - [ait_regAlloc])) Or
  295. ((StartTai.typ = ait_label) and
  296. labelCanBeSkipped(Tai_label(startTai)))) Do
  297. StartTai := Tai(StartTai.Next);
  298. If Assigned(StartTai) and
  299. (StartTai.typ = ait_regAlloc) then
  300. begin
  301. if (tai_regalloc(StartTai).allocation = alloc) and
  302. (tai_regalloc(StartTai).Reg = Reg) then
  303. begin
  304. FindRegAlloc:=true;
  305. break;
  306. end;
  307. StartTai := Tai(StartTai.Next);
  308. end
  309. else
  310. break;
  311. Until false;
  312. End;
  313. Procedure RemoveLastDeallocForFuncRes(asmL: TAAsmOutput; p: Tai);
  314. Procedure DoRemoveLastDeallocForFuncRes(asmL: TAAsmOutput; reg: TRegister);
  315. var
  316. hp2: Tai;
  317. begin
  318. hp2 := p;
  319. repeat
  320. hp2 := Tai(hp2.previous);
  321. if assigned(hp2) and
  322. (hp2.typ = ait_regalloc) and
  323. not(tai_regalloc(hp2).allocation) and
  324. (tai_regalloc(hp2).reg = reg) then
  325. begin
  326. asml.remove(hp2);
  327. hp2.free;
  328. break;
  329. end;
  330. until not(assigned(hp2)) or
  331. regInInstruction(reg,hp2);
  332. end;
  333. begin
  334. case aktprocdef.rettype.def.deftype of
  335. arraydef,recorddef,pointerdef,
  336. stringdef,enumdef,procdef,objectdef,errordef,
  337. filedef,setdef,procvardef,
  338. classrefdef,forwarddef:
  339. DoRemoveLastDeallocForFuncRes(asmL,R_EAX);
  340. orddef:
  341. if aktprocdef.rettype.def.size <> 0 then
  342. begin
  343. DoRemoveLastDeallocForFuncRes(asmL,R_EAX);
  344. { for int64/qword }
  345. if aktprocdef.rettype.def.size = 8 then
  346. DoRemoveLastDeallocForFuncRes(asmL,R_EDX);
  347. end;
  348. end;
  349. end;
  350. procedure getNoDeallocRegs(var regs: TRegSet);
  351. var regCounter: TRegister;
  352. begin
  353. regs := [];
  354. case aktprocdef.rettype.def.deftype of
  355. arraydef,recorddef,pointerdef,
  356. stringdef,enumdef,procdef,objectdef,errordef,
  357. filedef,setdef,procvardef,
  358. classrefdef,forwarddef:
  359. regs := [R_EAX];
  360. orddef:
  361. if aktprocdef.rettype.def.size <> 0 then
  362. begin
  363. regs := [R_EAX];
  364. { for int64/qword }
  365. if aktprocdef.rettype.def.size = 8 then
  366. regs := regs + [R_EDX];
  367. end;
  368. end;
  369. for regCounter := R_EAX to R_EBX do
  370. if not(regCounter in rg.usableregsint) then
  371. include(regs,regCounter);
  372. end;
  373. Procedure AddRegDeallocFor(asmL: TAAsmOutput; reg: TRegister; p: Tai);
  374. var hp1: Tai;
  375. funcResRegs: TRegset;
  376. funcResReg: boolean;
  377. begin
  378. if not(reg in rg.usableregsint) then
  379. exit;
  380. getNoDeallocRegs(funcResRegs);
  381. funcResRegs := funcResRegs - rg.usableregsint;
  382. funcResReg := reg in funcResRegs;
  383. hp1 := p;
  384. while not(funcResReg and
  385. (p.typ = ait_instruction) and
  386. (Taicpu(p).opcode = A_JMP) and
  387. (tasmlabel(Taicpu(p).oper[0].sym) = aktexit2label)) and
  388. getLastInstruction(p, p) And
  389. not(regInInstruction(reg, p)) Do
  390. hp1 := p;
  391. { don't insert a dealloc for registers which contain the function result }
  392. { if they are followed by a jump to the exit label (for exit(...)) }
  393. if not(funcResReg) or
  394. not((hp1.typ = ait_instruction) and
  395. (Taicpu(hp1).opcode = A_JMP) and
  396. (tasmlabel(Taicpu(hp1).oper[0].sym) = aktexit2label)) then
  397. begin
  398. p := tai_regalloc.deAlloc(reg);
  399. insertLLItem(AsmL, hp1.previous, hp1, p);
  400. end;
  401. end;
  402. Procedure BuildLabelTableAndFixRegAlloc(asmL: TAAsmOutput; Var LabelTable: PLabelTable; LowLabel: Longint;
  403. Var LabelDif: Longint; BlockStart, BlockEnd: Tai);
  404. {Builds a table with the locations of the labels in the TAAsmoutput.
  405. Also fixes some RegDeallocs like "# %eax released; push (%eax)"}
  406. Var p, hp1, hp2, lastP: Tai;
  407. regCounter: TRegister;
  408. UsedRegs, noDeallocRegs: TRegSet;
  409. Begin
  410. UsedRegs := [];
  411. If (LabelDif <> 0) Then
  412. Begin
  413. GetMem(LabelTable, LabelDif*SizeOf(TLabelTableItem));
  414. FillChar(LabelTable^, LabelDif*SizeOf(TLabelTableItem), 0);
  415. End;
  416. p := BlockStart;
  417. lastP := p;
  418. While (P <> BlockEnd) Do
  419. Begin
  420. Case p.typ Of
  421. ait_Label:
  422. If not labelCanBeSkipped(Tai_label(p)) Then
  423. LabelTable^[Tai_Label(p).l.labelnr-LowLabel].TaiObj := p;
  424. ait_regAlloc:
  425. { ESI and EDI are (de)allocated manually, don't mess with them }
  426. if not(tai_regalloc(p).Reg in [R_EDI,R_ESI]) then
  427. begin
  428. if tai_regalloc(p).Allocation then
  429. Begin
  430. If Not(tai_regalloc(p).Reg in UsedRegs) Then
  431. UsedRegs := UsedRegs + [tai_regalloc(p).Reg]
  432. Else
  433. addRegDeallocFor(asmL, tai_regalloc(p).reg, p);
  434. End
  435. else
  436. begin
  437. UsedRegs := UsedRegs - [tai_regalloc(p).Reg];
  438. hp1 := p;
  439. hp2 := nil;
  440. While Not(FindRegAlloc(tai_regalloc(p).Reg, Tai(hp1.Next),true)) And
  441. GetNextInstruction(hp1, hp1) And
  442. RegInInstruction(tai_regalloc(p).Reg, hp1) Do
  443. hp2 := hp1;
  444. If hp2 <> nil Then
  445. Begin
  446. hp1 := Tai(p.previous);
  447. AsmL.Remove(p);
  448. InsertLLItem(AsmL, hp2, Tai(hp2.Next), p);
  449. p := hp1;
  450. end;
  451. end;
  452. end;
  453. end;
  454. repeat
  455. lastP := p;
  456. P := Tai(P.Next);
  457. until not(Assigned(p)) or
  458. not(p.typ in (SkipInstr - [ait_regalloc]));
  459. End;
  460. { don't add deallocation for function result variable or for regvars}
  461. getNoDeallocRegs(noDeallocRegs);
  462. usedRegs := usedRegs - noDeallocRegs;
  463. for regCounter := R_EAX to R_EDI do
  464. if regCounter in usedRegs then
  465. addRegDeallocFor(asmL,regCounter,lastP);
  466. End;
  467. {************************ Search the Label table ************************}
  468. Function FindLabel(L: tasmlabel; Var hp: Tai): Boolean;
  469. {searches for the specified label starting from hp as long as the
  470. encountered instructions are labels, to be able to optimize constructs like
  471. jne l2 jmp l2
  472. jmp l3 and l1:
  473. l1: l2:
  474. l2:}
  475. Var TempP: Tai;
  476. Begin
  477. TempP := hp;
  478. While Assigned(TempP) and
  479. (Tempp.typ In SkipInstr + [ait_label,ait_align]) Do
  480. If (Tempp.typ <> ait_Label) Or
  481. (Tai_label(Tempp).l <> L)
  482. Then GetNextInstruction(TempP, TempP)
  483. Else
  484. Begin
  485. hp := TempP;
  486. FindLabel := True;
  487. exit
  488. End;
  489. FindLabel := False;
  490. End;
  491. {************************ Some general functions ************************}
  492. Function TCh2Reg(Ch: TInsChange): TRegister;
  493. {converts a TChange variable to a TRegister}
  494. Begin
  495. If (Ch <= Ch_REDI) Then
  496. TCh2Reg := TRegister(Byte(Ch))
  497. Else
  498. If (Ch <= Ch_WEDI) Then
  499. TCh2Reg := TRegister(Byte(Ch) - Byte(Ch_REDI))
  500. Else
  501. If (Ch <= Ch_RWEDI) Then
  502. TCh2Reg := TRegister(Byte(Ch) - Byte(Ch_WEDI))
  503. Else
  504. If (Ch <= Ch_MEDI) Then
  505. TCh2Reg := TRegister(Byte(Ch) - Byte(Ch_RWEDI))
  506. Else InternalError($db)
  507. End;
  508. Function Reg32(Reg: TRegister): TRegister;
  509. {Returns the 32 bit component of Reg if it exists, otherwise Reg is returned}
  510. Begin
  511. Reg32 := Reg;
  512. If (Reg >= R_AX)
  513. Then
  514. If (Reg <= R_DI)
  515. Then Reg32 := rg.makeregsize(Reg,OS_INT)
  516. Else
  517. If (Reg <= R_BL)
  518. Then Reg32 := rg.makeregsize(Reg,OS_INT);
  519. End;
  520. { inserts new_one between prev and foll }
  521. Procedure InsertLLItem(AsmL: TAAsmOutput; prev, foll, new_one: TLinkedListItem);
  522. Begin
  523. If Assigned(prev) Then
  524. If Assigned(foll) Then
  525. Begin
  526. If Assigned(new_one) Then
  527. Begin
  528. new_one.previous := prev;
  529. new_one.next := foll;
  530. prev.next := new_one;
  531. foll.previous := new_one;
  532. { shgould we update line information }
  533. if (not (Tai(new_one).typ in SkipLineInfo)) and
  534. (not (Tai(foll).typ in SkipLineInfo)) then
  535. Tailineinfo(new_one).fileinfo := Tailineinfo(foll).fileinfo;
  536. End;
  537. End
  538. Else asml.Concat(new_one)
  539. Else If Assigned(Foll) Then asml.Insert(new_one)
  540. End;
  541. {********************* Compare parts of Tai objects *********************}
  542. Function RegsSameSize(Reg1, Reg2: TRegister): Boolean;
  543. {returns true if Reg1 and Reg2 are of the same size (so if they're both
  544. 8bit, 16bit or 32bit)}
  545. Begin
  546. If (Reg1 <= R_EDI)
  547. Then RegsSameSize := (Reg2 <= R_EDI)
  548. Else
  549. If (Reg1 <= R_DI)
  550. Then RegsSameSize := (Reg2 in [R_AX..R_DI])
  551. Else
  552. If (Reg1 <= R_BL)
  553. Then RegsSameSize := (Reg2 in [R_AL..R_BL])
  554. Else RegsSameSize := False
  555. End;
  556. Procedure AddReg2RegInfo(OldReg, NewReg: TRegister; Var RegInfo: TRegInfo);
  557. {updates the ???RegsEncountered and ???2???Reg fields of RegInfo. Assumes that
  558. OldReg and NewReg have the same size (has to be chcked in advance with
  559. RegsSameSize) and that neither equals R_NO}
  560. Begin
  561. With RegInfo Do
  562. Begin
  563. NewRegsEncountered := NewRegsEncountered + [NewReg];
  564. OldRegsEncountered := OldRegsEncountered + [OldReg];
  565. New2OldReg[NewReg] := OldReg;
  566. Case OldReg Of
  567. R_EAX..R_EDI:
  568. Begin
  569. NewRegsEncountered := NewRegsEncountered + [rg.makeregsize(NewReg,OS_16)];
  570. OldRegsEncountered := OldRegsEncountered + [rg.makeregsize(OldReg,OS_16)];
  571. New2OldReg[rg.makeregsize(NewReg,OS_16)] := rg.makeregsize(OldReg,OS_16);
  572. If (NewReg in [R_EAX..R_EBX]) And
  573. (OldReg in [R_EAX..R_EBX]) Then
  574. Begin
  575. NewRegsEncountered := NewRegsEncountered + [rg.makeregsize(NewReg,OS_8)];
  576. OldRegsEncountered := OldRegsEncountered + [rg.makeregsize(OldReg,OS_8)];
  577. New2OldReg[rg.makeregsize(NewReg,OS_8)] := rg.makeregsize(OldReg,OS_8);
  578. End;
  579. End;
  580. R_AX..R_DI:
  581. Begin
  582. NewRegsEncountered := NewRegsEncountered + [rg.makeregsize(NewReg,OS_32)];
  583. OldRegsEncountered := OldRegsEncountered + [rg.makeregsize(OldReg,OS_32)];
  584. New2OldReg[rg.makeregsize(NewReg,OS_32)] := rg.makeregsize(OldReg,OS_32);
  585. If (NewReg in [R_AX..R_BX]) And
  586. (OldReg in [R_AX..R_BX]) Then
  587. Begin
  588. NewRegsEncountered := NewRegsEncountered + [rg.makeregsize(NewReg,OS_8)];
  589. OldRegsEncountered := OldRegsEncountered + [rg.makeregsize(OldReg,OS_8)];
  590. New2OldReg[rg.makeregsize(NewReg,OS_8)] := rg.makeregsize(OldReg,OS_8);
  591. End;
  592. End;
  593. R_AL..R_BL:
  594. Begin
  595. NewRegsEncountered := NewRegsEncountered + [rg.makeregsize(NewReg,OS_32)]
  596. + [rg.makeregsize(NewReg,OS_16)];
  597. OldRegsEncountered := OldRegsEncountered + [rg.makeregsize(OldReg,OS_32)]
  598. + [rg.makeregsize(OldReg,OS_8)];
  599. New2OldReg[rg.makeregsize(NewReg,OS_32)] := rg.makeregsize(OldReg,OS_32);
  600. End;
  601. End;
  602. End;
  603. End;
  604. Procedure AddOp2RegInfo(const o:Toper; Var RegInfo: TRegInfo);
  605. Begin
  606. Case o.typ Of
  607. Top_Reg:
  608. If (o.reg <> R_NO) Then
  609. AddReg2RegInfo(o.reg, o.reg, RegInfo);
  610. Top_Ref:
  611. Begin
  612. If o.ref^.base <> R_NO Then
  613. AddReg2RegInfo(o.ref^.base, o.ref^.base, RegInfo);
  614. If o.ref^.index <> R_NO Then
  615. AddReg2RegInfo(o.ref^.index, o.ref^.index, RegInfo);
  616. End;
  617. End;
  618. End;
  619. Function RegsEquivalent(OldReg, NewReg: TRegister; Var RegInfo: TRegInfo; OPAct: TOpAction): Boolean;
  620. Begin
  621. If Not((OldReg = R_NO) Or (NewReg = R_NO)) Then
  622. If RegsSameSize(OldReg, NewReg) Then
  623. With RegInfo Do
  624. {here we always check for the 32 bit component, because it is possible that
  625. the 8 bit component has not been set, event though NewReg already has been
  626. processed. This happens if it has been compared with a register that doesn't
  627. have an 8 bit component (such as EDI). In that case the 8 bit component is
  628. still set to R_NO and the comparison in the Else-part will fail}
  629. If (Reg32(OldReg) in OldRegsEncountered) Then
  630. If (Reg32(NewReg) in NewRegsEncountered) Then
  631. RegsEquivalent := (OldReg = New2OldReg[NewReg])
  632. { If we haven't encountered the new register yet, but we have encountered the
  633. old one already, the new one can only be correct if it's being written to
  634. (and consequently the old one is also being written to), otherwise
  635. movl -8(%ebp), %eax and movl -8(%ebp), %eax
  636. movl (%eax), %eax movl (%edx), %edx
  637. are considered equivalent}
  638. Else
  639. If (OpAct = OpAct_Write) Then
  640. Begin
  641. AddReg2RegInfo(OldReg, NewReg, RegInfo);
  642. RegsEquivalent := True
  643. End
  644. Else Regsequivalent := False
  645. Else
  646. If Not(Reg32(NewReg) in NewRegsEncountered) and
  647. ((OpAct = OpAct_Write) or
  648. (newReg = oldReg)) Then
  649. Begin
  650. AddReg2RegInfo(OldReg, NewReg, RegInfo);
  651. RegsEquivalent := True
  652. End
  653. Else RegsEquivalent := False
  654. Else RegsEquivalent := False
  655. Else RegsEquivalent := OldReg = NewReg
  656. End;
  657. Function RefsEquivalent(Const R1, R2: TReference; var RegInfo: TRegInfo; OpAct: TOpAction): Boolean;
  658. Begin
  659. RefsEquivalent := (R1.Offset+R1.OffsetFixup = R2.Offset+R2.OffsetFixup) And
  660. RegsEquivalent(R1.Base, R2.Base, RegInfo, OpAct) And
  661. RegsEquivalent(R1.Index, R2.Index, RegInfo, OpAct) And
  662. (R1.Segment = R2.Segment) And (R1.ScaleFactor = R2.ScaleFactor) And
  663. (R1.Symbol = R2.Symbol);
  664. End;
  665. Function RefsEqual(Const R1, R2: TReference): Boolean;
  666. Begin
  667. RefsEqual := (R1.Offset+R1.OffsetFixup = R2.Offset+R2.OffsetFixup) And
  668. (R1.Segment = R2.Segment) And (R1.Base = R2.Base) And
  669. (R1.Index = R2.Index) And (R1.ScaleFactor = R2.ScaleFactor) And
  670. (R1.Symbol=R2.Symbol);
  671. End;
  672. Function IsGP32Reg(Reg: TRegister): Boolean;
  673. {Checks if the register is a 32 bit general purpose register}
  674. Begin
  675. If (Reg >= R_EAX) and (Reg <= R_EBX)
  676. Then IsGP32Reg := True
  677. Else IsGP32reg := False
  678. End;
  679. Function RegInRef(Reg: TRegister; Const Ref: TReference): Boolean;
  680. Begin {checks whether Ref contains a reference to Reg}
  681. Reg := Reg32(Reg);
  682. RegInRef := (Ref.Base = Reg) Or (Ref.Index = Reg)
  683. End;
  684. function RegReadByInstruction(reg: TRegister; hp: Tai): boolean;
  685. var p: Taicpu;
  686. opCount: byte;
  687. begin
  688. RegReadByInstruction := false;
  689. reg := reg32(reg);
  690. if hp.typ <> ait_instruction then
  691. exit;
  692. p := Taicpu(hp);
  693. case p.opcode of
  694. A_IMUL:
  695. case p.ops of
  696. 1: regReadByInstruction := (reg = R_EAX) or reginOp(reg,p.oper[0]);
  697. 2,3:
  698. regReadByInstruction := regInOp(reg,p.oper[0]) or
  699. regInOp(reg,p.oper[1]);
  700. end;
  701. A_IDIV,A_DIV,A_MUL:
  702. begin
  703. regReadByInstruction :=
  704. regInOp(reg,p.oper[0]) or (reg = R_EAX) or (reg = R_EDX);
  705. end;
  706. else
  707. begin
  708. for opCount := 0 to 2 do
  709. if (p.oper[opCount].typ = top_ref) and
  710. RegInRef(reg,p.oper[opCount].ref^) then
  711. begin
  712. RegReadByInstruction := true;
  713. exit
  714. end;
  715. for opCount := 1 to MaxCh do
  716. case InsProp[p.opcode].Ch[opCount] of
  717. Ch_REAX..CH_REDI,CH_RWEAX..Ch_MEDI:
  718. if reg = TCh2Reg(InsProp[p.opcode].Ch[opCount]) then
  719. begin
  720. RegReadByInstruction := true;
  721. exit
  722. end;
  723. Ch_RWOp1,Ch_ROp1,Ch_MOp1:
  724. if (p.oper[0].typ = top_reg) and
  725. (reg32(p.oper[0].reg) = reg) then
  726. begin
  727. RegReadByInstruction := true;
  728. exit
  729. end;
  730. Ch_RWOp2,Ch_ROp2,Ch_MOp2:
  731. if (p.oper[1].typ = top_reg) and
  732. (reg32(p.oper[1].reg) = reg) then
  733. begin
  734. RegReadByInstruction := true;
  735. exit
  736. end;
  737. Ch_RWOp3,Ch_ROp3,Ch_MOp3:
  738. if (p.oper[2].typ = top_reg) and
  739. (reg32(p.oper[2].reg) = reg) then
  740. begin
  741. RegReadByInstruction := true;
  742. exit
  743. end;
  744. end;
  745. end;
  746. end;
  747. end;
  748. function regInInstruction(Reg: TRegister; p1: Tai): Boolean;
  749. { Checks if Reg is used by the instruction p1 }
  750. { Difference with "regReadBysinstruction() or regModifiedByInstruction()": }
  751. { this one ignores CH_ALL opcodes, while regModifiedByInstruction doesn't }
  752. var p: Taicpu;
  753. opCount: byte;
  754. begin
  755. reg := reg32(reg);
  756. regInInstruction := false;
  757. if p1.typ <> ait_instruction then
  758. exit;
  759. p := Taicpu(p1);
  760. case p.opcode of
  761. A_IMUL:
  762. case p.ops of
  763. 1: regInInstruction := (reg = R_EAX) or reginOp(reg,p.oper[0]);
  764. 2,3:
  765. regInInstruction := regInOp(reg,p.oper[0]) or
  766. regInOp(reg,p.oper[1]) or regInOp(reg,p.oper[2]);
  767. end;
  768. A_IDIV,A_DIV,A_MUL:
  769. regInInstruction :=
  770. regInOp(reg,p.oper[0]) or
  771. (reg = R_EAX) or (reg = R_EDX)
  772. else
  773. begin
  774. for opCount := 1 to MaxCh do
  775. case InsProp[p.opcode].Ch[opCount] of
  776. CH_REAX..CH_MEDI:
  777. if tch2reg(InsProp[p.opcode].Ch[opCount]) = reg then
  778. begin
  779. regInInstruction := true;
  780. exit;
  781. end;
  782. Ch_ROp1..Ch_MOp1:
  783. if regInOp(reg,p.oper[0]) then
  784. begin
  785. regInInstruction := true;
  786. exit
  787. end;
  788. Ch_ROp2..Ch_MOp2:
  789. if regInOp(reg,p.oper[1]) then
  790. begin
  791. regInInstruction := true;
  792. exit
  793. end;
  794. Ch_ROp3..Ch_MOp3:
  795. if regInOp(reg,p.oper[2]) then
  796. begin
  797. regInInstruction := true;
  798. exit
  799. end;
  800. end;
  801. end;
  802. end;
  803. end;
  804. Function RegInOp(Reg: TRegister; const o:toper): Boolean;
  805. Begin
  806. RegInOp := False;
  807. reg := reg32(reg);
  808. Case o.typ Of
  809. top_reg: RegInOp := Reg = reg32(o.reg);
  810. top_ref: RegInOp := (Reg = o.ref^.Base) Or
  811. (Reg = o.ref^.Index);
  812. End;
  813. End;
  814. Function RegModifiedByInstruction(Reg: TRegister; p1: Tai): Boolean;
  815. Var InstrProp: TInsProp;
  816. TmpResult: Boolean;
  817. Cnt: Byte;
  818. Begin
  819. TmpResult := False;
  820. Reg := Reg32(Reg);
  821. If (p1.typ = ait_instruction) Then
  822. Case Taicpu(p1).opcode of
  823. A_IMUL:
  824. With Taicpu(p1) Do
  825. TmpResult :=
  826. ((ops = 1) and (reg in [R_EAX,R_EDX])) or
  827. ((ops = 2) and (Reg32(oper[1].reg) = reg)) or
  828. ((ops = 3) and (Reg32(oper[2].reg) = reg));
  829. A_DIV, A_IDIV, A_MUL:
  830. With Taicpu(p1) Do
  831. TmpResult :=
  832. (Reg = R_EAX) or
  833. (Reg = R_EDX);
  834. Else
  835. Begin
  836. Cnt := 1;
  837. InstrProp := InsProp[Taicpu(p1).OpCode];
  838. While (Cnt <= MaxCh) And
  839. (InstrProp.Ch[Cnt] <> Ch_None) And
  840. Not(TmpResult) Do
  841. Begin
  842. Case InstrProp.Ch[Cnt] Of
  843. Ch_WEAX..Ch_MEDI:
  844. TmpResult := Reg = TCh2Reg(InstrProp.Ch[Cnt]);
  845. Ch_RWOp1,Ch_WOp1,Ch_Mop1:
  846. TmpResult := (Taicpu(p1).oper[0].typ = top_reg) and
  847. (Reg32(Taicpu(p1).oper[0].reg) = reg);
  848. Ch_RWOp2,Ch_WOp2,Ch_Mop2:
  849. TmpResult := (Taicpu(p1).oper[1].typ = top_reg) and
  850. (Reg32(Taicpu(p1).oper[1].reg) = reg);
  851. Ch_RWOp3,Ch_WOp3,Ch_Mop3:
  852. TmpResult := (Taicpu(p1).oper[2].typ = top_reg) and
  853. (Reg32(Taicpu(p1).oper[2].reg) = reg);
  854. Ch_FPU: TmpResult := Reg in [R_ST..R_ST7,R_MM0..R_MM7];
  855. Ch_ALL: TmpResult := true;
  856. End;
  857. Inc(Cnt)
  858. End
  859. End
  860. End;
  861. RegModifiedByInstruction := TmpResult
  862. End;
  863. function instrWritesFlags(p: Tai): boolean;
  864. var
  865. l: longint;
  866. begin
  867. instrWritesFlags := true;
  868. case p.typ of
  869. ait_instruction:
  870. begin
  871. for l := 1 to MaxCh do
  872. if InsProp[Taicpu(p).opcode].Ch[l] in [Ch_WFlags,Ch_RWFlags,Ch_All] then
  873. exit;
  874. end;
  875. ait_label:
  876. exit;
  877. else
  878. instrWritesFlags := false;
  879. end;
  880. end;
  881. function instrReadsFlags(p: Tai): boolean;
  882. var
  883. l: longint;
  884. begin
  885. instrReadsFlags := true;
  886. case p.typ of
  887. ait_instruction:
  888. begin
  889. for l := 1 to MaxCh do
  890. if InsProp[Taicpu(p).opcode].Ch[l] in [Ch_RFlags,Ch_RWFlags,Ch_All] then
  891. exit;
  892. end;
  893. ait_label:
  894. exit;
  895. else
  896. instrReadsFlags := false;
  897. end;
  898. end;
  899. {********************* GetNext and GetLastInstruction *********************}
  900. Function GetNextInstruction(Current: Tai; Var Next: Tai): Boolean;
  901. { skips ait_regalloc, ait_regdealloc and ait_stab* objects and puts the }
  902. { next Tai object in Next. Returns false if there isn't any }
  903. Begin
  904. Repeat
  905. If (Current.typ = ait_marker) And
  906. (Tai_Marker(current).Kind = AsmBlockStart) Then
  907. Begin
  908. GetNextInstruction := False;
  909. Next := Nil;
  910. Exit
  911. End;
  912. Current := Tai(current.Next);
  913. While Assigned(Current) And
  914. ((current.typ In skipInstr) or
  915. ((current.typ = ait_label) and
  916. labelCanBeSkipped(Tai_label(current)))) do
  917. Current := Tai(current.Next);
  918. { If Assigned(Current) And
  919. (current.typ = ait_Marker) And
  920. (Tai_Marker(current).Kind = NoPropInfoStart) Then
  921. Begin
  922. While Assigned(Current) And
  923. ((current.typ <> ait_Marker) Or
  924. (Tai_Marker(current).Kind <> NoPropInfoEnd)) Do
  925. Current := Tai(current.Next);
  926. End;}
  927. Until Not(Assigned(Current)) Or
  928. (current.typ <> ait_Marker) Or
  929. not(Tai_Marker(current).Kind in [NoPropInfoStart,NoPropInfoEnd]);
  930. Next := Current;
  931. If Assigned(Current) And
  932. Not((current.typ In SkipInstr) or
  933. ((current.typ = ait_label) And
  934. labelCanBeSkipped(Tai_label(current))))
  935. Then
  936. GetNextInstruction :=
  937. not((current.typ = ait_marker) and
  938. (Tai_marker(current).kind = asmBlockStart))
  939. Else
  940. Begin
  941. GetNextInstruction := False;
  942. Next := nil;
  943. End;
  944. End;
  945. Function GetLastInstruction(Current: Tai; Var Last: Tai): Boolean;
  946. {skips the ait-types in SkipInstr puts the previous Tai object in
  947. Last. Returns false if there isn't any}
  948. Begin
  949. Repeat
  950. Current := Tai(current.previous);
  951. While Assigned(Current) And
  952. (((current.typ = ait_Marker) And
  953. Not(Tai_Marker(current).Kind in [AsmBlockEnd{,NoPropInfoEnd}])) or
  954. (current.typ In SkipInstr) or
  955. ((current.typ = ait_label) And
  956. labelCanBeSkipped(Tai_label(current)))) Do
  957. Current := Tai(current.previous);
  958. { If Assigned(Current) And
  959. (current.typ = ait_Marker) And
  960. (Tai_Marker(current).Kind = NoPropInfoEnd) Then
  961. Begin
  962. While Assigned(Current) And
  963. ((current.typ <> ait_Marker) Or
  964. (Tai_Marker(current).Kind <> NoPropInfoStart)) Do
  965. Current := Tai(current.previous);
  966. End;}
  967. Until Not(Assigned(Current)) Or
  968. (current.typ <> ait_Marker) Or
  969. not(Tai_Marker(current).Kind in [NoPropInfoStart,NoPropInfoEnd]);
  970. If Not(Assigned(Current)) or
  971. (current.typ In SkipInstr) or
  972. ((current.typ = ait_label) And
  973. labelCanBeSkipped(Tai_label(current))) or
  974. ((current.typ = ait_Marker) And
  975. (Tai_Marker(current).Kind = AsmBlockEnd))
  976. Then
  977. Begin
  978. Last := nil;
  979. GetLastInstruction := False
  980. End
  981. Else
  982. Begin
  983. Last := Current;
  984. GetLastInstruction := True;
  985. End;
  986. End;
  987. Procedure SkipHead(var P: Tai);
  988. Var OldP: Tai;
  989. Begin
  990. Repeat
  991. OldP := P;
  992. If (p.typ in SkipInstr) Or
  993. ((p.typ = ait_marker) And
  994. (Tai_Marker(p).Kind in [AsmBlockEnd,inlinestart,inlineend])) Then
  995. GetNextInstruction(P, P)
  996. Else If ((p.Typ = Ait_Marker) And
  997. (Tai_Marker(p).Kind = nopropinfostart)) Then
  998. {a marker of the NoPropInfoStart can't be the first instruction of a
  999. TAAsmoutput list}
  1000. GetNextInstruction(Tai(p.Previous),P);
  1001. Until P = OldP
  1002. End;
  1003. function labelCanBeSkipped(p: Tai_label): boolean;
  1004. begin
  1005. labelCanBeSkipped := not(p.l.is_used) or p.l.is_addr;
  1006. end;
  1007. {******************* The Data Flow Analyzer functions ********************}
  1008. function regLoadedWithNewValue(reg: tregister; canDependOnPrevValue: boolean;
  1009. hp: Tai): boolean;
  1010. { assumes reg is a 32bit register }
  1011. var p: Taicpu;
  1012. begin
  1013. if not assigned(hp) or
  1014. (hp.typ <> ait_instruction) then
  1015. begin
  1016. regLoadedWithNewValue := false;
  1017. exit;
  1018. end;
  1019. p := Taicpu(hp);
  1020. regLoadedWithNewValue :=
  1021. (((p.opcode = A_MOV) or
  1022. (p.opcode = A_MOVZX) or
  1023. (p.opcode = A_MOVSX) or
  1024. (p.opcode = A_LEA)) and
  1025. (p.oper[1].typ = top_reg) and
  1026. (Reg32(p.oper[1].reg) = reg) and
  1027. (canDependOnPrevValue or
  1028. (p.oper[0].typ <> top_ref) or
  1029. not regInRef(reg,p.oper[0].ref^)) or
  1030. ((p.opcode = A_POP) and
  1031. (Reg32(p.oper[0].reg) = reg)));
  1032. end;
  1033. Procedure UpdateUsedRegs(Var UsedRegs: TRegSet; p: Tai);
  1034. {updates UsedRegs with the RegAlloc Information coming after P}
  1035. Begin
  1036. Repeat
  1037. While Assigned(p) And
  1038. ((p.typ in (SkipInstr - [ait_RegAlloc])) or
  1039. ((p.typ = ait_label) And
  1040. labelCanBeSkipped(Tai_label(p)))) Do
  1041. p := Tai(p.next);
  1042. While Assigned(p) And
  1043. (p.typ=ait_RegAlloc) Do
  1044. Begin
  1045. if tai_regalloc(p).allocation then
  1046. UsedRegs := UsedRegs + [tai_regalloc(p).Reg]
  1047. else
  1048. UsedRegs := UsedRegs - [tai_regalloc(p).Reg];
  1049. p := Tai(p.next);
  1050. End;
  1051. Until Not(Assigned(p)) Or
  1052. (Not(p.typ in SkipInstr) And
  1053. Not((p.typ = ait_label) And
  1054. labelCanBeSkipped(Tai_label(p))));
  1055. End;
  1056. Procedure AllocRegBetween(AsmL: TAAsmOutput; Reg: TRegister; p1, p2: Tai);
  1057. { allocates register Reg between (and including) instructions p1 and p2 }
  1058. { the type of p1 and p2 must not be in SkipInstr }
  1059. var
  1060. hp, start: Tai;
  1061. lastRemovedWasDealloc, firstRemovedWasAlloc, first: boolean;
  1062. Begin
  1063. If not(reg in rg.usableregsint+[R_EDI,R_ESI]) or
  1064. not(assigned(p1)) then
  1065. { this happens with registers which are loaded implicitely, outside the }
  1066. { current block (e.g. esi with self) }
  1067. exit;
  1068. { make sure we allocate it for this instruction }
  1069. if p1 = p2 then
  1070. getnextinstruction(p2,p2);
  1071. lastRemovedWasDealloc := false;
  1072. firstRemovedWasAlloc := false;
  1073. first := true;
  1074. {$ifdef allocregdebug}
  1075. hp := tai_comment.Create(strpnew('allocating '+std_reg2str[reg]+
  1076. ' from here...')));
  1077. insertllitem(asml,p1.previous,p1,hp);
  1078. hp := tai_comment.Create(strpnew('allocated '+std_reg2str[reg]+
  1079. ' till here...')));
  1080. insertllitem(asml,p2,p1.next,hp);
  1081. {$endif allocregdebug}
  1082. start := p1;
  1083. Repeat
  1084. If Assigned(p1.OptInfo) Then
  1085. Include(PTaiProp(p1.OptInfo)^.UsedRegs,Reg);
  1086. p1 := Tai(p1.next);
  1087. Repeat
  1088. While assigned(p1) and
  1089. (p1.typ in (SkipInstr-[ait_regalloc])) Do
  1090. p1 := Tai(p1.next);
  1091. { remove all allocation/deallocation info about the register in between }
  1092. If assigned(p1) and
  1093. (p1.typ = ait_regalloc) Then
  1094. If (tai_regalloc(p1).Reg = Reg) Then
  1095. Begin
  1096. if first then
  1097. begin
  1098. firstRemovedWasAlloc := tai_regalloc(p1).allocation;
  1099. first := false;
  1100. end;
  1101. lastRemovedWasDealloc := not tai_regalloc(p1).allocation;
  1102. hp := Tai(p1.Next);
  1103. asml.Remove(p1);
  1104. p1.free;
  1105. p1 := hp;
  1106. End
  1107. Else p1 := Tai(p1.next);
  1108. Until not(assigned(p1)) or
  1109. Not(p1.typ in SkipInstr);
  1110. Until not(assigned(p1)) or
  1111. (p1 = p2);
  1112. if assigned(p1) then
  1113. begin
  1114. if assigned(p1.optinfo) then
  1115. include(PTaiProp(p1.OptInfo)^.UsedRegs,Reg);
  1116. if lastRemovedWasDealloc then
  1117. begin
  1118. hp := tai_regalloc.DeAlloc(reg);
  1119. insertLLItem(asmL,p1,p1.next,hp);
  1120. end;
  1121. end;
  1122. if firstRemovedWasAlloc then
  1123. begin
  1124. hp := tai_regalloc.Alloc(reg);
  1125. insertLLItem(asmL,start.previous,start,hp);
  1126. end;
  1127. End;
  1128. function FindRegDealloc(reg: tregister; p: Tai): boolean;
  1129. { assumes reg is a 32bit register }
  1130. var
  1131. hp: Tai;
  1132. first: boolean;
  1133. begin
  1134. findregdealloc := false;
  1135. first := true;
  1136. while assigned(p.previous) and
  1137. ((Tai(p.previous).typ in (skipinstr+[ait_align])) or
  1138. ((Tai(p.previous).typ = ait_label) and
  1139. labelCanBeSkipped(Tai_label(p.previous)))) do
  1140. begin
  1141. p := Tai(p.previous);
  1142. if (p.typ = ait_regalloc) and
  1143. (tai_regalloc(p).reg = reg) then
  1144. if not(tai_regalloc(p).allocation) then
  1145. if first then
  1146. begin
  1147. findregdealloc := true;
  1148. break;
  1149. end
  1150. else
  1151. begin
  1152. findRegDealloc :=
  1153. getNextInstruction(p,hp) and
  1154. regLoadedWithNewValue(reg,false,hp);
  1155. break
  1156. end
  1157. else
  1158. first := false;
  1159. end
  1160. end;
  1161. Procedure IncState(Var S: Byte; amount: longint);
  1162. {Increases S by 1, wraps around at $ffff to 0 (so we won't get overflow
  1163. errors}
  1164. Begin
  1165. if (s <= $ff - amount) then
  1166. inc(s, amount)
  1167. else s := longint(s) + amount - $ff;
  1168. End;
  1169. Function sequenceDependsonReg(Const Content: TContent; seqReg, Reg: TRegister): Boolean;
  1170. { Content is the sequence of instructions that describes the contents of }
  1171. { seqReg. Reg is being overwritten by the current instruction. If the }
  1172. { content of seqReg depends on reg (ie. because of a }
  1173. { "movl (seqreg,reg), seqReg" instruction), this function returns true }
  1174. Var p: Tai;
  1175. Counter: Byte;
  1176. TmpResult: Boolean;
  1177. RegsChecked: TRegSet;
  1178. Begin
  1179. RegsChecked := [];
  1180. p := Content.StartMod;
  1181. TmpResult := False;
  1182. Counter := 1;
  1183. While Not(TmpResult) And
  1184. (Counter <= Content.NrOfMods) Do
  1185. Begin
  1186. If (p.typ = ait_instruction) and
  1187. ((Taicpu(p).opcode = A_MOV) or
  1188. (Taicpu(p).opcode = A_MOVZX) or
  1189. (Taicpu(p).opcode = A_MOVSX) or
  1190. (Taicpu(p).opcode = A_LEA)) and
  1191. (Taicpu(p).oper[0].typ = top_ref) Then
  1192. With Taicpu(p).oper[0].ref^ Do
  1193. If ((Base = procinfo.FramePointer) or
  1194. (assigned(symbol) and (base = R_NO))) And
  1195. (Index = R_NO) Then
  1196. Begin
  1197. RegsChecked := RegsChecked + [Reg32(Taicpu(p).oper[1].reg)];
  1198. If Reg = Reg32(Taicpu(p).oper[1].reg) Then
  1199. Break;
  1200. End
  1201. Else
  1202. tmpResult :=
  1203. regReadByInstruction(reg,p) and
  1204. regModifiedByInstruction(seqReg,p)
  1205. Else
  1206. tmpResult :=
  1207. regReadByInstruction(reg,p) and
  1208. regModifiedByInstruction(seqReg,p);
  1209. Inc(Counter);
  1210. GetNextInstruction(p,p)
  1211. End;
  1212. sequenceDependsonReg := TmpResult
  1213. End;
  1214. procedure invalidateDependingRegs(p1: pTaiProp; reg: tregister);
  1215. var
  1216. counter: tregister;
  1217. begin
  1218. for counter := R_EAX to R_EDI Do
  1219. if counter <> reg then
  1220. with p1^.regs[counter] Do
  1221. begin
  1222. if (typ in [con_ref,con_noRemoveRef]) and
  1223. sequenceDependsOnReg(p1^.Regs[counter],counter,reg) then
  1224. if typ in [con_ref,con_invalid] then
  1225. typ := con_invalid
  1226. { con_invalid and con_noRemoveRef = con_unknown }
  1227. else typ := con_unknown;
  1228. if assigned(memwrite) and
  1229. regInRef(counter,memwrite.oper[1].ref^) then
  1230. memwrite := nil;
  1231. end;
  1232. end;
  1233. Procedure DestroyReg(p1: PTaiProp; Reg: TRegister; doIncState:Boolean);
  1234. {Destroys the contents of the register Reg in the PTaiProp p1, as well as the
  1235. contents of registers are loaded with a memory location based on Reg.
  1236. doIncState is false when this register has to be destroyed not because
  1237. it's contents are directly modified/overwritten, but because of an indirect
  1238. action (e.g. this register holds the contents of a variable and the value
  1239. of the variable in memory is changed) }
  1240. Begin
  1241. Reg := Reg32(Reg);
  1242. { the following happens for fpu registers }
  1243. if (reg < low(NrOfInstrSinceLastMod)) or
  1244. (reg > high(NrOfInstrSinceLastMod)) then
  1245. exit;
  1246. NrOfInstrSinceLastMod[Reg] := 0;
  1247. with p1^.regs[reg] do
  1248. begin
  1249. if doIncState then
  1250. begin
  1251. incState(wstate,1);
  1252. typ := con_unknown;
  1253. startmod := nil;
  1254. end
  1255. else
  1256. if typ in [con_ref,con_const,con_invalid] then
  1257. typ := con_invalid
  1258. { con_invalid and con_noRemoveRef = con_unknown }
  1259. else typ := con_unknown;
  1260. memwrite := nil;
  1261. end;
  1262. invalidateDependingRegs(p1,reg);
  1263. End;
  1264. {Procedure AddRegsToSet(p: Tai; Var RegSet: TRegSet);
  1265. Begin
  1266. If (p.typ = ait_instruction) Then
  1267. Begin
  1268. Case Taicpu(p).oper[0].typ Of
  1269. top_reg:
  1270. If Not(Taicpu(p).oper[0].reg in [R_NO,R_ESP,procinfo.FramePointer]) Then
  1271. RegSet := RegSet + [Taicpu(p).oper[0].reg];
  1272. top_ref:
  1273. With TReference(Taicpu(p).oper[0]^) Do
  1274. Begin
  1275. If Not(Base in [procinfo.FramePointer,R_NO,R_ESP])
  1276. Then RegSet := RegSet + [Base];
  1277. If Not(Index in [procinfo.FramePointer,R_NO,R_ESP])
  1278. Then RegSet := RegSet + [Index];
  1279. End;
  1280. End;
  1281. Case Taicpu(p).oper[1].typ Of
  1282. top_reg:
  1283. If Not(Taicpu(p).oper[1].reg in [R_NO,R_ESP,procinfo.FramePointer]) Then
  1284. If RegSet := RegSet + [TRegister(TwoWords(Taicpu(p).oper[1]).Word1];
  1285. top_ref:
  1286. With TReference(Taicpu(p).oper[1]^) Do
  1287. Begin
  1288. If Not(Base in [procinfo.FramePointer,R_NO,R_ESP])
  1289. Then RegSet := RegSet + [Base];
  1290. If Not(Index in [procinfo.FramePointer,R_NO,R_ESP])
  1291. Then RegSet := RegSet + [Index];
  1292. End;
  1293. End;
  1294. End;
  1295. End;}
  1296. Function OpsEquivalent(const o1, o2: toper; Var RegInfo: TRegInfo; OpAct: TopAction): Boolean;
  1297. Begin {checks whether the two ops are equivalent}
  1298. OpsEquivalent := False;
  1299. if o1.typ=o2.typ then
  1300. Case o1.typ Of
  1301. Top_Reg:
  1302. OpsEquivalent :=RegsEquivalent(o1.reg,o2.reg, RegInfo, OpAct);
  1303. Top_Ref:
  1304. OpsEquivalent := RefsEquivalent(o1.ref^, o2.ref^, RegInfo, OpAct);
  1305. Top_Const:
  1306. OpsEquivalent := o1.val = o2.val;
  1307. Top_None:
  1308. OpsEquivalent := True
  1309. End;
  1310. End;
  1311. Function OpsEqual(const o1,o2:toper): Boolean;
  1312. Begin {checks whether the two ops are equal}
  1313. OpsEqual := False;
  1314. if o1.typ=o2.typ then
  1315. Case o1.typ Of
  1316. Top_Reg :
  1317. OpsEqual:=o1.reg=o2.reg;
  1318. Top_Ref :
  1319. OpsEqual := RefsEqual(o1.ref^, o2.ref^);
  1320. Top_Const :
  1321. OpsEqual:=o1.val=o2.val;
  1322. Top_Symbol :
  1323. OpsEqual:=(o1.sym=o2.sym) and (o1.symofs=o2.symofs);
  1324. Top_None :
  1325. OpsEqual := True
  1326. End;
  1327. End;
  1328. function sizescompatible(loadsize,newsize: topsize): boolean;
  1329. begin
  1330. case loadsize of
  1331. S_B,S_BW,S_BL:
  1332. sizescompatible := (newsize = loadsize) or (newsize = S_B);
  1333. S_W,S_WL:
  1334. sizescompatible := (newsize = loadsize) or (newsize = S_W);
  1335. else
  1336. sizescompatible := newsize = S_L;
  1337. end;
  1338. end;
  1339. function opscompatible(p1,p2: Taicpu): boolean;
  1340. begin
  1341. case p1.opcode of
  1342. A_MOVZX,A_MOVSX:
  1343. opscompatible :=
  1344. ((p2.opcode = p1.opcode) or (p2.opcode = A_MOV)) and
  1345. sizescompatible(p1.opsize,p2.opsize);
  1346. else
  1347. opscompatible :=
  1348. (p1.opcode = p2.opcode) and
  1349. (p1.opsize = p2.opsize);
  1350. end;
  1351. end;
  1352. Function InstructionsEquivalent(p1, p2: Tai; Var RegInfo: TRegInfo): Boolean;
  1353. {$ifdef csdebug}
  1354. var
  1355. hp: Tai;
  1356. {$endif csdebug}
  1357. Begin {checks whether two Taicpu instructions are equal}
  1358. If Assigned(p1) And Assigned(p2) And
  1359. (Tai(p1).typ = ait_instruction) And
  1360. (Tai(p2).typ = ait_instruction) And
  1361. opscompatible(Taicpu(p1),Taicpu(p2)) and
  1362. (Taicpu(p1).oper[0].typ = Taicpu(p2).oper[0].typ) And
  1363. (Taicpu(p1).oper[1].typ = Taicpu(p2).oper[1].typ) And
  1364. (Taicpu(p1).oper[2].typ = Taicpu(p2).oper[2].typ)
  1365. Then
  1366. {both instructions have the same structure:
  1367. "<operator> <operand of type1>, <operand of type 2>"}
  1368. If ((Taicpu(p1).opcode = A_MOV) or
  1369. (Taicpu(p1).opcode = A_MOVZX) or
  1370. (Taicpu(p1).opcode = A_MOVSX) or
  1371. (Taicpu(p1).opcode = A_LEA)) And
  1372. (Taicpu(p1).oper[0].typ = top_ref) {then .oper[1]t = top_reg} Then
  1373. If Not(RegInRef(Taicpu(p1).oper[1].reg, Taicpu(p1).oper[0].ref^)) Then
  1374. {the "old" instruction is a load of a register with a new value, not with
  1375. a value based on the contents of this register (so no "mov (reg), reg")}
  1376. If Not(RegInRef(Taicpu(p2).oper[1].reg, Taicpu(p2).oper[0].ref^)) And
  1377. RefsEqual(Taicpu(p1).oper[0].ref^, Taicpu(p2).oper[0].ref^)
  1378. Then
  1379. {the "new" instruction is also a load of a register with a new value, and
  1380. this value is fetched from the same memory location}
  1381. Begin
  1382. With Taicpu(p2).oper[0].ref^ Do
  1383. Begin
  1384. If Not(Base in [procinfo.FramePointer, R_NO, R_ESP]) Then
  1385. RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef + [Base];
  1386. If Not(Index in [procinfo.FramePointer, R_NO, R_ESP]) Then
  1387. RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef + [Index];
  1388. End;
  1389. {add the registers from the reference (.oper[0]) to the RegInfo, all registers
  1390. from the reference are the same in the old and in the new instruction
  1391. sequence}
  1392. AddOp2RegInfo(Taicpu(p1).oper[0], RegInfo);
  1393. {the registers from .oper[1] have to be equivalent, but not necessarily equal}
  1394. InstructionsEquivalent :=
  1395. RegsEquivalent(reg32(Taicpu(p1).oper[1].reg),
  1396. reg32(Taicpu(p2).oper[1].reg), RegInfo, OpAct_Write);
  1397. End
  1398. {the registers are loaded with values from different memory locations. If
  1399. this was allowed, the instructions "mov -4(esi),eax" and "mov -4(ebp),eax"
  1400. would be considered equivalent}
  1401. Else InstructionsEquivalent := False
  1402. Else
  1403. {load register with a value based on the current value of this register}
  1404. Begin
  1405. With Taicpu(p2).oper[0].ref^ Do
  1406. Begin
  1407. If Not(Base in [procinfo.FramePointer,
  1408. Reg32(Taicpu(p2).oper[1].reg),R_NO,R_ESP]) Then
  1409. {it won't do any harm if the register is already in RegsLoadedForRef}
  1410. Begin
  1411. RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef + [Base];
  1412. {$ifdef csdebug}
  1413. Writeln(std_reg2str[base], ' added');
  1414. {$endif csdebug}
  1415. end;
  1416. If Not(Index in [procinfo.FramePointer,
  1417. Reg32(Taicpu(p2).oper[1].reg),R_NO,R_ESP]) Then
  1418. Begin
  1419. RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef + [Index];
  1420. {$ifdef csdebug}
  1421. Writeln(std_reg2str[index], ' added');
  1422. {$endif csdebug}
  1423. end;
  1424. End;
  1425. If Not(Reg32(Taicpu(p2).oper[1].reg) In [procinfo.FramePointer,R_NO,R_ESP])
  1426. Then
  1427. Begin
  1428. RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef -
  1429. [Reg32(Taicpu(p2).oper[1].reg)];
  1430. {$ifdef csdebug}
  1431. Writeln(std_reg2str[Reg32(Taicpu(p2).oper[1].reg)], ' removed');
  1432. {$endif csdebug}
  1433. end;
  1434. InstructionsEquivalent :=
  1435. OpsEquivalent(Taicpu(p1).oper[0], Taicpu(p2).oper[0], RegInfo, OpAct_Read) And
  1436. OpsEquivalent(Taicpu(p1).oper[1], Taicpu(p2).oper[1], RegInfo, OpAct_Write)
  1437. End
  1438. Else
  1439. {an instruction <> mov, movzx, movsx}
  1440. begin
  1441. {$ifdef csdebug}
  1442. hp := tai_comment.Create(strpnew('checking if equivalent'));
  1443. hp.previous := p2;
  1444. hp.next := p2^.next;
  1445. p2^.next^.previous := hp;
  1446. p2^.next := hp;
  1447. {$endif csdebug}
  1448. InstructionsEquivalent :=
  1449. OpsEquivalent(Taicpu(p1).oper[0], Taicpu(p2).oper[0], RegInfo, OpAct_Unknown) And
  1450. OpsEquivalent(Taicpu(p1).oper[1], Taicpu(p2).oper[1], RegInfo, OpAct_Unknown) And
  1451. OpsEquivalent(Taicpu(p1).oper[2], Taicpu(p2).oper[2], RegInfo, OpAct_Unknown)
  1452. end
  1453. {the instructions haven't even got the same structure, so they're certainly
  1454. not equivalent}
  1455. Else
  1456. begin
  1457. {$ifdef csdebug}
  1458. hp := tai_comment.Create(strpnew('different opcodes/format'));
  1459. hp.previous := p2;
  1460. hp.next := p2^.next;
  1461. p2^.next^.previous := hp;
  1462. p2^.next := hp;
  1463. {$endif csdebug}
  1464. InstructionsEquivalent := False;
  1465. end;
  1466. {$ifdef csdebug}
  1467. hp := tai_comment.Create(strpnew('instreq: '+tostr(byte(instructionsequivalent))));
  1468. hp.previous := p2;
  1469. hp.next := p2^.next;
  1470. p2^.next^.previous := hp;
  1471. p2^.next := hp;
  1472. {$endif csdebug}
  1473. End;
  1474. (*
  1475. Function InstructionsEqual(p1, p2: Tai): Boolean;
  1476. Begin {checks whether two Taicpu instructions are equal}
  1477. InstructionsEqual :=
  1478. Assigned(p1) And Assigned(p2) And
  1479. ((Tai(p1).typ = ait_instruction) And
  1480. (Tai(p1).typ = ait_instruction) And
  1481. (Taicpu(p1).opcode = Taicpu(p2).opcode) And
  1482. (Taicpu(p1).oper[0].typ = Taicpu(p2).oper[0].typ) And
  1483. (Taicpu(p1).oper[1].typ = Taicpu(p2).oper[1].typ) And
  1484. OpsEqual(Taicpu(p1).oper[0].typ, Taicpu(p1).oper[0], Taicpu(p2).oper[0]) And
  1485. OpsEqual(Taicpu(p1).oper[1].typ, Taicpu(p1).oper[1], Taicpu(p2).oper[1]))
  1486. End;
  1487. *)
  1488. Procedure ReadReg(p: PTaiProp; Reg: TRegister);
  1489. Begin
  1490. Reg := Reg32(Reg);
  1491. If Reg in [R_EAX..R_EDI] Then
  1492. incState(p^.regs[Reg].rstate,1)
  1493. End;
  1494. Procedure ReadRef(p: PTaiProp; Const Ref: PReference);
  1495. Begin
  1496. If Ref^.Base <> R_NO Then
  1497. ReadReg(p, Ref^.Base);
  1498. If Ref^.Index <> R_NO Then
  1499. ReadReg(p, Ref^.Index);
  1500. End;
  1501. Procedure ReadOp(P: PTaiProp;const o:toper);
  1502. Begin
  1503. Case o.typ Of
  1504. top_reg: ReadReg(P, o.reg);
  1505. top_ref: ReadRef(P, o.ref);
  1506. top_symbol : ;
  1507. End;
  1508. End;
  1509. Function RefInInstruction(Const Ref: TReference; p: Tai;
  1510. RefsEq: TRefCompare): Boolean;
  1511. {checks whehter Ref is used in P}
  1512. Var TmpResult: Boolean;
  1513. Begin
  1514. TmpResult := False;
  1515. If (p.typ = ait_instruction) Then
  1516. Begin
  1517. If (Taicpu(p).oper[0].typ = Top_Ref) Then
  1518. TmpResult := RefsEq(Ref, Taicpu(p).oper[0].ref^);
  1519. If Not(TmpResult) And (Taicpu(p).oper[1].typ = Top_Ref) Then
  1520. TmpResult := RefsEq(Ref, Taicpu(p).oper[1].ref^);
  1521. If Not(TmpResult) And (Taicpu(p).oper[2].typ = Top_Ref) Then
  1522. TmpResult := RefsEq(Ref, Taicpu(p).oper[2].ref^);
  1523. End;
  1524. RefInInstruction := TmpResult;
  1525. End;
  1526. Function RefInSequence(Const Ref: TReference; Content: TContent;
  1527. RefsEq: TRefCompare): Boolean;
  1528. {checks the whole sequence of Content (so StartMod and and the next NrOfMods
  1529. Tai objects) to see whether Ref is used somewhere}
  1530. Var p: Tai;
  1531. Counter: Byte;
  1532. TmpResult: Boolean;
  1533. Begin
  1534. p := Content.StartMod;
  1535. TmpResult := False;
  1536. Counter := 1;
  1537. While Not(TmpResult) And
  1538. (Counter <= Content.NrOfMods) Do
  1539. Begin
  1540. If (p.typ = ait_instruction) And
  1541. RefInInstruction(Ref, p, RefsEq)
  1542. Then TmpResult := True;
  1543. Inc(Counter);
  1544. GetNextInstruction(p,p)
  1545. End;
  1546. RefInSequence := TmpResult
  1547. End;
  1548. Function ArrayRefsEq(const r1, r2: TReference): Boolean;
  1549. Begin
  1550. ArrayRefsEq := (R1.Offset+R1.OffsetFixup = R2.Offset+R2.OffsetFixup) And
  1551. (R1.Segment = R2.Segment) And
  1552. (R1.Symbol=R2.Symbol) And
  1553. (R1.Base = R2.Base)
  1554. End;
  1555. function isSimpleRef(const ref: treference): boolean;
  1556. { returns true if ref is reference to a local or global variable, to a }
  1557. { parameter or to an object field (this includes arrays). Returns false }
  1558. { otherwise. }
  1559. begin
  1560. isSimpleRef :=
  1561. assigned(ref.symbol) or
  1562. (ref.base = procinfo.framepointer) or
  1563. (assigned(procinfo._class) and
  1564. (ref.base = R_ESI));
  1565. end;
  1566. function containsPointerRef(p: Tai): boolean;
  1567. { checks if an instruction contains a reference which is a pointer location }
  1568. var
  1569. hp: Taicpu;
  1570. count: longint;
  1571. begin
  1572. containsPointerRef := false;
  1573. if p.typ <> ait_instruction then
  1574. exit;
  1575. hp := Taicpu(p);
  1576. for count := low(hp.oper) to high(hp.oper) do
  1577. begin
  1578. case hp.oper[count].typ of
  1579. top_ref:
  1580. if not isSimpleRef(hp.oper[count].ref^) then
  1581. begin
  1582. containsPointerRef := true;
  1583. exit;
  1584. end;
  1585. top_none:
  1586. exit;
  1587. end;
  1588. end;
  1589. end;
  1590. function containsPointerLoad(c: tcontent): boolean;
  1591. { checks whether the contents of a register contain a pointer reference }
  1592. var
  1593. p: Tai;
  1594. count: longint;
  1595. begin
  1596. containsPointerLoad := false;
  1597. p := c.startmod;
  1598. for count := c.nrOfMods downto 1 do
  1599. begin
  1600. if containsPointerRef(p) then
  1601. begin
  1602. containsPointerLoad := true;
  1603. exit;
  1604. end;
  1605. getnextinstruction(p,p);
  1606. end;
  1607. end;
  1608. function writeToMemDestroysContents(regWritten: tregister; const ref: treference;
  1609. reg: tregister; const c: tcontent; var invalsmemwrite: boolean): boolean;
  1610. { returns whether the contents c of reg are invalid after regWritten is }
  1611. { is written to ref }
  1612. var
  1613. refsEq: trefCompare;
  1614. begin
  1615. reg := reg32(reg);
  1616. regWritten := reg32(regWritten);
  1617. if isSimpleRef(ref) then
  1618. begin
  1619. if (ref.index <> R_NO) or
  1620. (assigned(ref.symbol) and
  1621. (ref.base <> R_NO)) then
  1622. { local/global variable or parameter which is an array }
  1623. refsEq := {$ifdef fpc}@{$endif}arrayRefsEq
  1624. else
  1625. { local/global variable or parameter which is not an array }
  1626. refsEq := {$ifdef fpc}@{$endif}refsEqual;
  1627. invalsmemwrite :=
  1628. assigned(c.memwrite) and
  1629. ((not(cs_uncertainOpts in aktglobalswitches) and
  1630. containsPointerRef(c.memwrite)) or
  1631. refsEq(c.memwrite.oper[1].ref^,ref));
  1632. if not(c.typ in [con_ref,con_noRemoveRef,con_invalid]) then
  1633. begin
  1634. writeToMemDestroysContents := false;
  1635. exit;
  1636. end;
  1637. { write something to a parameter, a local or global variable, so }
  1638. { * with uncertain optimizations on: }
  1639. { - destroy the contents of registers whose contents have somewhere a }
  1640. { "mov?? (Ref), %reg". WhichReg (this is the register whose contents }
  1641. { are being written to memory) is not destroyed if it's StartMod is }
  1642. { of that form and NrOfMods = 1 (so if it holds ref, but is not a }
  1643. { expression based on Ref) }
  1644. { * with uncertain optimizations off: }
  1645. { - also destroy registers that contain any pointer }
  1646. with c do
  1647. writeToMemDestroysContents :=
  1648. (typ in [con_ref,con_noRemoveRef]) and
  1649. ((not(cs_uncertainOpts in aktglobalswitches) and
  1650. containsPointerLoad(c)
  1651. ) or
  1652. (refInSequence(ref,c,refsEq) and
  1653. ((reg <> regWritten) or
  1654. not((nrOfMods = 1) and
  1655. {StarMod is always of the type ait_instruction}
  1656. (Taicpu(StartMod).oper[0].typ = top_ref) and
  1657. refsEq(Taicpu(StartMod).oper[0].ref^, ref)
  1658. )
  1659. )
  1660. )
  1661. );
  1662. end
  1663. else
  1664. { write something to a pointer location, so }
  1665. { * with uncertain optimzations on: }
  1666. { - do not destroy registers which contain a local/global variable or }
  1667. { a parameter, except if DestroyRefs is called because of a "movsl" }
  1668. { * with uncertain optimzations off: }
  1669. { - destroy every register which contains a memory location }
  1670. begin
  1671. invalsmemwrite :=
  1672. assigned(c.memwrite) and
  1673. (not(cs_UncertainOpts in aktglobalswitches) or
  1674. containsPointerRef(c.memwrite));
  1675. if not(c.typ in [con_ref,con_noRemoveRef,con_invalid]) then
  1676. begin
  1677. writeToMemDestroysContents := false;
  1678. exit;
  1679. end;
  1680. with c do
  1681. writeToMemDestroysContents :=
  1682. (typ in [con_ref,con_noRemoveRef]) and
  1683. (not(cs_UncertainOpts in aktglobalswitches) or
  1684. { for movsl }
  1685. ((ref.base = R_EDI) and (ref.index = R_EDI)) or
  1686. { don't destroy if reg contains a parameter, local or global variable }
  1687. containsPointerLoad(c)
  1688. );
  1689. end;
  1690. end;
  1691. function writeToRegDestroysContents(destReg: tregister; reg: tregister;
  1692. const c: tcontent): boolean;
  1693. { returns whether the contents c of reg are invalid after destReg is }
  1694. { modified }
  1695. begin
  1696. writeToRegDestroysContents :=
  1697. (c.typ in [con_ref,con_noRemoveRef,con_invalid]) and
  1698. sequenceDependsOnReg(c,reg,reg32(destReg));
  1699. end;
  1700. function writeDestroysContents(const op: toper; reg: tregister;
  1701. const c: tcontent): boolean;
  1702. { returns whether the contents c of reg are invalid after regWritten is }
  1703. { is written to op }
  1704. var
  1705. dummy: boolean;
  1706. begin
  1707. reg := reg32(reg);
  1708. case op.typ of
  1709. top_reg:
  1710. writeDestroysContents :=
  1711. writeToRegDestroysContents(op.reg,reg,c);
  1712. top_ref:
  1713. writeDestroysContents :=
  1714. writeToMemDestroysContents(R_NO,op.ref^,reg,c,dummy);
  1715. else
  1716. writeDestroysContents := false;
  1717. end;
  1718. end;
  1719. procedure destroyRefs(p: Tai; const ref: treference; regWritten: tregister);
  1720. { destroys all registers which possibly contain a reference to Ref, regWritten }
  1721. { is the register whose contents are being written to memory (if this proc }
  1722. { is called because of a "mov?? %reg, (mem)" instruction) }
  1723. var
  1724. counter: TRegister;
  1725. destroymemwrite: boolean;
  1726. begin
  1727. for counter := R_EAX to R_EDI Do
  1728. begin
  1729. if writeToMemDestroysContents(regWritten,ref,counter,
  1730. pTaiProp(p.optInfo)^.regs[counter],destroymemwrite) then
  1731. destroyReg(pTaiProp(p.optInfo), counter, false)
  1732. else if destroymemwrite then
  1733. pTaiProp(p.optinfo)^.regs[counter].MemWrite := nil;
  1734. end;
  1735. End;
  1736. Procedure DestroyAllRegs(p: PTaiProp; read, written: boolean);
  1737. Var Counter: TRegister;
  1738. Begin {initializes/desrtoys all registers}
  1739. For Counter := R_EAX To R_EDI Do
  1740. Begin
  1741. if read then
  1742. ReadReg(p, Counter);
  1743. DestroyReg(p, Counter, written);
  1744. p^.regs[counter].MemWrite := nil;
  1745. End;
  1746. p^.DirFlag := F_Unknown;
  1747. End;
  1748. Procedure DestroyOp(TaiObj: Tai; const o:Toper);
  1749. {$ifdef statedebug}
  1750. var hp: Tai;
  1751. {$endif statedebug}
  1752. Begin
  1753. Case o.typ Of
  1754. top_reg:
  1755. begin
  1756. {$ifdef statedebug}
  1757. hp := tai_comment.Create(strpnew('destroying '+std_reg2str[o.reg]));
  1758. hp.next := Taiobj^.next;
  1759. hp.previous := Taiobj;
  1760. Taiobj^.next := hp;
  1761. if assigned(hp.next) then
  1762. hp.next^.previous := hp;
  1763. {$endif statedebug}
  1764. DestroyReg(PTaiProp(TaiObj.OptInfo), reg32(o.reg), true);
  1765. end;
  1766. top_ref:
  1767. Begin
  1768. ReadRef(PTaiProp(TaiObj.OptInfo), o.ref);
  1769. DestroyRefs(TaiObj, o.ref^, R_NO);
  1770. End;
  1771. top_symbol:;
  1772. End;
  1773. End;
  1774. Function DFAPass1(AsmL: TAAsmOutput; BlockStart: Tai): Tai;
  1775. {gathers the RegAlloc data... still need to think about where to store it to
  1776. avoid global vars}
  1777. Var BlockEnd: Tai;
  1778. Begin
  1779. BlockEnd := FindLoHiLabels(LoLab, HiLab, LabDif, BlockStart);
  1780. BuildLabelTableAndFixRegAlloc(AsmL, LTable, LoLab, LabDif, BlockStart, BlockEnd);
  1781. DFAPass1 := BlockEnd;
  1782. End;
  1783. Procedure AddInstr2RegContents({$ifdef statedebug} asml: TAAsmoutput; {$endif}
  1784. p: Taicpu; reg: TRegister);
  1785. {$ifdef statedebug}
  1786. var hp: Tai;
  1787. {$endif statedebug}
  1788. Begin
  1789. Reg := Reg32(Reg);
  1790. With PTaiProp(p.optinfo)^.Regs[reg] Do
  1791. if (typ in [con_ref,con_noRemoveRef])
  1792. Then
  1793. Begin
  1794. incState(wstate,1);
  1795. {also store how many instructions are part of the sequence in the first
  1796. instructions PTaiProp, so it can be easily accessed from within
  1797. CheckSequence}
  1798. Inc(NrOfMods, NrOfInstrSinceLastMod[Reg]);
  1799. PTaiProp(Tai(StartMod).OptInfo)^.Regs[Reg].NrOfMods := NrOfMods;
  1800. NrOfInstrSinceLastMod[Reg] := 0;
  1801. invalidateDependingRegs(p.optinfo,reg);
  1802. pTaiprop(p.optinfo)^.regs[reg].memwrite := nil;
  1803. {$ifdef StateDebug}
  1804. hp := tai_comment.Create(strpnew(std_reg2str[reg]+': '+tostr(PTaiProp(p.optinfo)^.Regs[reg].WState)
  1805. + ' -- ' + tostr(PTaiProp(p.optinfo)^.Regs[reg].nrofmods))));
  1806. InsertLLItem(AsmL, p, p.next, hp);
  1807. {$endif StateDebug}
  1808. End
  1809. Else
  1810. Begin
  1811. {$ifdef statedebug}
  1812. hp := tai_comment.Create(strpnew('destroying '+std_reg2str[reg]));
  1813. insertllitem(asml,p,p.next,hp);
  1814. {$endif statedebug}
  1815. DestroyReg(PTaiProp(p.optinfo), Reg, true);
  1816. {$ifdef StateDebug}
  1817. hp := tai_comment.Create(strpnew(std_reg2str[reg]+': '+tostr(PTaiProp(p.optinfo)^.Regs[reg].WState)));
  1818. InsertLLItem(AsmL, p, p.next, hp);
  1819. {$endif StateDebug}
  1820. End
  1821. End;
  1822. Procedure AddInstr2OpContents({$ifdef statedebug} asml: TAAsmoutput; {$endif}
  1823. p: Taicpu; const oper: TOper);
  1824. Begin
  1825. If oper.typ = top_reg Then
  1826. AddInstr2RegContents({$ifdef statedebug} asml, {$endif}p, oper.reg)
  1827. Else
  1828. Begin
  1829. ReadOp(PTaiProp(p.optinfo), oper);
  1830. DestroyOp(p, oper);
  1831. End
  1832. End;
  1833. Procedure DoDFAPass2(
  1834. {$Ifdef StateDebug}
  1835. AsmL: TAAsmOutput;
  1836. {$endif statedebug}
  1837. BlockStart, BlockEnd: Tai);
  1838. {Analyzes the Data Flow of an assembler list. Starts creating the reg
  1839. contents for the instructions starting with p. Returns the last Tai which has
  1840. been processed}
  1841. Var
  1842. CurProp, LastFlagsChangeProp: PTaiProp;
  1843. Cnt, InstrCnt : Longint;
  1844. InstrProp: TInsProp;
  1845. UsedRegs: TRegSet;
  1846. prev,p : Tai;
  1847. TmpRef: TReference;
  1848. TmpReg: TRegister;
  1849. {$ifdef AnalyzeLoops}
  1850. hp : Tai;
  1851. TmpState: Byte;
  1852. {$endif AnalyzeLoops}
  1853. Begin
  1854. p := BlockStart;
  1855. LastFlagsChangeProp := nil;
  1856. prev := nil;
  1857. UsedRegs := [];
  1858. UpdateUsedregs(UsedRegs, p);
  1859. SkipHead(P);
  1860. BlockStart := p;
  1861. InstrCnt := 1;
  1862. FillChar(NrOfInstrSinceLastMod, SizeOf(NrOfInstrSinceLastMod), 0);
  1863. While (P <> BlockEnd) Do
  1864. Begin
  1865. CurProp := @TaiPropBlock^[InstrCnt];
  1866. If assigned(prev)
  1867. Then
  1868. Begin
  1869. {$ifdef JumpAnal}
  1870. If (p.Typ <> ait_label) Then
  1871. {$endif JumpAnal}
  1872. Begin
  1873. CurProp^.regs := PTaiProp(prev.OptInfo)^.Regs;
  1874. CurProp^.DirFlag := PTaiProp(prev.OptInfo)^.DirFlag;
  1875. CurProp^.FlagsUsed := false;
  1876. End
  1877. End
  1878. Else
  1879. Begin
  1880. FillChar(CurProp^, SizeOf(CurProp^), 0);
  1881. { For TmpReg := R_EAX to R_EDI Do
  1882. CurProp^.regs[TmpReg].WState := 1;}
  1883. End;
  1884. CurProp^.UsedRegs := UsedRegs;
  1885. CurProp^.CanBeRemoved := False;
  1886. UpdateUsedRegs(UsedRegs, Tai(p.Next));
  1887. For TmpReg := R_EAX To R_EDI Do
  1888. if NrOfInstrSinceLastMod[TmpReg] < 255 then
  1889. Inc(NrOfInstrSinceLastMod[TmpReg])
  1890. else
  1891. begin
  1892. NrOfInstrSinceLastMod[TmpReg] := 0;
  1893. curprop^.regs[TmpReg].typ := con_unknown;
  1894. end;
  1895. Case p.typ Of
  1896. ait_marker:;
  1897. ait_label:
  1898. {$Ifndef JumpAnal}
  1899. if not labelCanBeSkipped(Tai_label(p)) then
  1900. DestroyAllRegs(CurProp,false,false);
  1901. {$Else JumpAnal}
  1902. Begin
  1903. If not labelCanBeSkipped(Tai_label(p)) Then
  1904. With LTable^[Tai_Label(p).l^.labelnr-LoLab] Do
  1905. {$IfDef AnalyzeLoops}
  1906. If (RefsFound = Tai_Label(p).l^.RefCount)
  1907. {$Else AnalyzeLoops}
  1908. If (JmpsProcessed = Tai_Label(p).l^.RefCount)
  1909. {$EndIf AnalyzeLoops}
  1910. Then
  1911. {all jumps to this label have been found}
  1912. {$IfDef AnalyzeLoops}
  1913. If (JmpsProcessed > 0)
  1914. Then
  1915. {$EndIf AnalyzeLoops}
  1916. {we've processed at least one jump to this label}
  1917. Begin
  1918. If (GetLastInstruction(p, hp) And
  1919. Not(((hp.typ = ait_instruction)) And
  1920. (Taicpu_labeled(hp).is_jmp))
  1921. Then
  1922. {previous instruction not a JMP -> the contents of the registers after the
  1923. previous intruction has been executed have to be taken into account as well}
  1924. For TmpReg := R_EAX to R_EDI Do
  1925. Begin
  1926. If (CurProp^.regs[TmpReg].WState <>
  1927. PTaiProp(hp.OptInfo)^.Regs[TmpReg].WState)
  1928. Then DestroyReg(CurProp, TmpReg, true)
  1929. End
  1930. End
  1931. {$IfDef AnalyzeLoops}
  1932. Else
  1933. {a label from a backward jump (e.g. a loop), no jump to this label has
  1934. already been processed}
  1935. If GetLastInstruction(p, hp) And
  1936. Not(hp.typ = ait_instruction) And
  1937. (Taicpu_labeled(hp).opcode = A_JMP))
  1938. Then
  1939. {previous instruction not a jmp, so keep all the registers' contents from the
  1940. previous instruction}
  1941. Begin
  1942. CurProp^.regs := PTaiProp(hp.OptInfo)^.Regs;
  1943. CurProp.DirFlag := PTaiProp(hp.OptInfo)^.DirFlag;
  1944. End
  1945. Else
  1946. {previous instruction a jmp and no jump to this label processed yet}
  1947. Begin
  1948. hp := p;
  1949. Cnt := InstrCnt;
  1950. {continue until we find a jump to the label or a label which has already
  1951. been processed}
  1952. While GetNextInstruction(hp, hp) And
  1953. Not((hp.typ = ait_instruction) And
  1954. (Taicpu(hp).is_jmp) and
  1955. (tasmlabel(Taicpu(hp).oper[0].sym).labelnr = Tai_Label(p).l^.labelnr)) And
  1956. Not((hp.typ = ait_label) And
  1957. (LTable^[Tai_Label(hp).l^.labelnr-LoLab].RefsFound
  1958. = Tai_Label(hp).l^.RefCount) And
  1959. (LTable^[Tai_Label(hp).l^.labelnr-LoLab].JmpsProcessed > 0)) Do
  1960. Inc(Cnt);
  1961. If (hp.typ = ait_label)
  1962. Then
  1963. {there's a processed label after the current one}
  1964. Begin
  1965. CurProp^.regs := TaiPropBlock^[Cnt].Regs;
  1966. CurProp.DirFlag := TaiPropBlock^[Cnt].DirFlag;
  1967. End
  1968. Else
  1969. {there's no label anymore after the current one, or they haven't been
  1970. processed yet}
  1971. Begin
  1972. GetLastInstruction(p, hp);
  1973. CurProp^.regs := PTaiProp(hp.OptInfo)^.Regs;
  1974. CurProp.DirFlag := PTaiProp(hp.OptInfo)^.DirFlag;
  1975. DestroyAllRegs(PTaiProp(hp.OptInfo),true,true)
  1976. End
  1977. End
  1978. {$EndIf AnalyzeLoops}
  1979. Else
  1980. {not all references to this label have been found, so destroy all registers}
  1981. Begin
  1982. GetLastInstruction(p, hp);
  1983. CurProp^.regs := PTaiProp(hp.OptInfo)^.Regs;
  1984. CurProp.DirFlag := PTaiProp(hp.OptInfo)^.DirFlag;
  1985. DestroyAllRegs(CurProp,true,true)
  1986. End;
  1987. End;
  1988. {$EndIf JumpAnal}
  1989. {$ifdef GDB}
  1990. ait_stabs, ait_stabn, ait_stab_function_name:;
  1991. {$endif GDB}
  1992. ait_align: ; { may destroy flags !!! }
  1993. ait_instruction:
  1994. Begin
  1995. if Taicpu(p).is_jmp or
  1996. (Taicpu(p).opcode = A_JMP) then
  1997. begin
  1998. {$IfNDef JumpAnal}
  1999. for tmpReg := R_EAX to R_EDI do
  2000. with curProp^.regs[tmpReg] do
  2001. case typ of
  2002. con_ref: typ := con_noRemoveRef;
  2003. con_const: typ := con_noRemoveConst;
  2004. con_invalid: typ := con_unknown;
  2005. end;
  2006. {$Else JumpAnal}
  2007. With LTable^[tasmlabel(Taicpu(p).oper[0].sym).labelnr-LoLab] Do
  2008. If (RefsFound = tasmlabel(Taicpu(p).oper[0].sym).RefCount) Then
  2009. Begin
  2010. If (InstrCnt < InstrNr)
  2011. Then
  2012. {forward jump}
  2013. If (JmpsProcessed = 0) Then
  2014. {no jump to this label has been processed yet}
  2015. Begin
  2016. TaiPropBlock^[InstrNr].Regs := CurProp^.regs;
  2017. TaiPropBlock^[InstrNr].DirFlag := CurProp.DirFlag;
  2018. Inc(JmpsProcessed);
  2019. End
  2020. Else
  2021. Begin
  2022. For TmpReg := R_EAX to R_EDI Do
  2023. If (TaiPropBlock^[InstrNr].Regs[TmpReg].WState <>
  2024. CurProp^.regs[TmpReg].WState) Then
  2025. DestroyReg(@TaiPropBlock^[InstrNr], TmpReg, true);
  2026. Inc(JmpsProcessed);
  2027. End
  2028. {$ifdef AnalyzeLoops}
  2029. Else
  2030. { backward jump, a loop for example}
  2031. { If (JmpsProcessed > 0) Or
  2032. Not(GetLastInstruction(TaiObj, hp) And
  2033. (hp.typ = ait_labeled_instruction) And
  2034. (Taicpu_labeled(hp).opcode = A_JMP))
  2035. Then}
  2036. {instruction prior to label is not a jmp, or at least one jump to the label
  2037. has yet been processed}
  2038. Begin
  2039. Inc(JmpsProcessed);
  2040. For TmpReg := R_EAX to R_EDI Do
  2041. If (TaiPropBlock^[InstrNr].Regs[TmpReg].WState <>
  2042. CurProp^.regs[TmpReg].WState)
  2043. Then
  2044. Begin
  2045. TmpState := TaiPropBlock^[InstrNr].Regs[TmpReg].WState;
  2046. Cnt := InstrNr;
  2047. While (TmpState = TaiPropBlock^[Cnt].Regs[TmpReg].WState) Do
  2048. Begin
  2049. DestroyReg(@TaiPropBlock^[Cnt], TmpReg, true);
  2050. Inc(Cnt);
  2051. End;
  2052. While (Cnt <= InstrCnt) Do
  2053. Begin
  2054. Inc(TaiPropBlock^[Cnt].Regs[TmpReg].WState);
  2055. Inc(Cnt)
  2056. End
  2057. End;
  2058. End
  2059. { Else }
  2060. {instruction prior to label is a jmp and no jumps to the label have yet been
  2061. processed}
  2062. { Begin
  2063. Inc(JmpsProcessed);
  2064. For TmpReg := R_EAX to R_EDI Do
  2065. Begin
  2066. TmpState := TaiPropBlock^[InstrNr].Regs[TmpReg].WState;
  2067. Cnt := InstrNr;
  2068. While (TmpState = TaiPropBlock^[Cnt].Regs[TmpReg].WState) Do
  2069. Begin
  2070. TaiPropBlock^[Cnt].Regs[TmpReg] := CurProp^.regs[TmpReg];
  2071. Inc(Cnt);
  2072. End;
  2073. TmpState := TaiPropBlock^[InstrNr].Regs[TmpReg].WState;
  2074. While (TmpState = TaiPropBlock^[Cnt].Regs[TmpReg].WState) Do
  2075. Begin
  2076. DestroyReg(@TaiPropBlock^[Cnt], TmpReg, true);
  2077. Inc(Cnt);
  2078. End;
  2079. While (Cnt <= InstrCnt) Do
  2080. Begin
  2081. Inc(TaiPropBlock^[Cnt].Regs[TmpReg].WState);
  2082. Inc(Cnt)
  2083. End
  2084. End
  2085. End}
  2086. {$endif AnalyzeLoops}
  2087. End;
  2088. {$EndIf JumpAnal}
  2089. end
  2090. else
  2091. begin
  2092. InstrProp := InsProp[Taicpu(p).opcode];
  2093. Case Taicpu(p).opcode Of
  2094. A_MOV, A_MOVZX, A_MOVSX:
  2095. Begin
  2096. Case Taicpu(p).oper[0].typ Of
  2097. top_ref, top_reg:
  2098. case Taicpu(p).oper[1].typ Of
  2099. top_reg:
  2100. Begin
  2101. {$ifdef statedebug}
  2102. hp := tai_comment.Create(strpnew('destroying '+
  2103. std_reg2str[Taicpu(p).oper[1].reg])));
  2104. insertllitem(asml,p,p.next,hp);
  2105. {$endif statedebug}
  2106. readOp(curprop, Taicpu(p).oper[0]);
  2107. tmpreg := reg32(Taicpu(p).oper[1].reg);
  2108. if regInOp(tmpreg, Taicpu(p).oper[0]) and
  2109. (curProp^.regs[tmpReg].typ in [con_ref,con_noRemoveRef]) then
  2110. begin
  2111. with curprop^.regs[tmpreg] Do
  2112. begin
  2113. incState(wstate,1);
  2114. { also store how many instructions are part of the sequence in the first }
  2115. { instruction's PTaiProp, so it can be easily accessed from within }
  2116. { CheckSequence }
  2117. inc(nrOfMods, nrOfInstrSinceLastMod[tmpreg]);
  2118. pTaiprop(startmod.optinfo)^.regs[tmpreg].nrOfMods := nrOfMods;
  2119. nrOfInstrSinceLastMod[tmpreg] := 0;
  2120. { Destroy the contents of the registers }
  2121. { that depended on the previous value of }
  2122. { this register }
  2123. invalidateDependingRegs(curprop,tmpreg);
  2124. curprop^.regs[tmpreg].memwrite := nil;
  2125. end;
  2126. end
  2127. else
  2128. begin
  2129. {$ifdef statedebug}
  2130. hp := tai_comment.Create(strpnew('destroying & initing '+std_reg2str[tmpreg]));
  2131. insertllitem(asml,p,p.next,hp);
  2132. {$endif statedebug}
  2133. destroyReg(curprop, tmpreg, true);
  2134. if not(reginop(tmpreg, Taicpu(p).oper[0])) then
  2135. with curprop^.regs[tmpreg] Do
  2136. begin
  2137. typ := con_ref;
  2138. startmod := p;
  2139. nrOfMods := 1;
  2140. end
  2141. end;
  2142. {$ifdef StateDebug}
  2143. hp := tai_comment.Create(strpnew(std_reg2str[TmpReg]+': '+tostr(CurProp^.regs[TmpReg].WState)));
  2144. InsertLLItem(AsmL, p, p.next, hp);
  2145. {$endif StateDebug}
  2146. End;
  2147. Top_Ref:
  2148. Begin
  2149. ReadRef(CurProp, Taicpu(p).oper[1].ref);
  2150. if taicpu(p).oper[0].typ = top_reg then
  2151. begin
  2152. ReadReg(CurProp, Taicpu(p).oper[0].reg);
  2153. DestroyRefs(p, Taicpu(p).oper[1].ref^, Taicpu(p).oper[0].reg);
  2154. pTaiProp(p.optinfo)^.regs[reg32(Taicpu(p).oper[0].reg)].memwrite :=
  2155. Taicpu(p);
  2156. end
  2157. else
  2158. DestroyRefs(p, Taicpu(p).oper[1].ref^, R_NO);
  2159. End;
  2160. End;
  2161. top_symbol,Top_Const:
  2162. Begin
  2163. Case Taicpu(p).oper[1].typ Of
  2164. Top_Reg:
  2165. Begin
  2166. TmpReg := Reg32(Taicpu(p).oper[1].reg);
  2167. {$ifdef statedebug}
  2168. hp := tai_comment.Create(strpnew('destroying '+std_reg2str[tmpreg]));
  2169. insertllitem(asml,p,p.next,hp);
  2170. {$endif statedebug}
  2171. With CurProp^.regs[TmpReg] Do
  2172. Begin
  2173. DestroyReg(CurProp, TmpReg, true);
  2174. typ := Con_Const;
  2175. StartMod := p;
  2176. End
  2177. End;
  2178. Top_Ref:
  2179. Begin
  2180. ReadRef(CurProp, Taicpu(p).oper[1].ref);
  2181. DestroyRefs(P, Taicpu(p).oper[1].ref^, R_NO);
  2182. End;
  2183. End;
  2184. End;
  2185. End;
  2186. End;
  2187. A_DIV, A_IDIV, A_MUL:
  2188. Begin
  2189. ReadOp(Curprop, Taicpu(p).oper[0]);
  2190. ReadReg(CurProp,R_EAX);
  2191. If (Taicpu(p).OpCode = A_IDIV) or
  2192. (Taicpu(p).OpCode = A_DIV) Then
  2193. ReadReg(CurProp,R_EDX);
  2194. {$ifdef statedebug}
  2195. hp := tai_comment.Create(strpnew('destroying eax and edx'));
  2196. insertllitem(asml,p,p.next,hp);
  2197. {$endif statedebug}
  2198. { DestroyReg(CurProp, R_EAX, true);}
  2199. AddInstr2RegContents({$ifdef statedebug}asml,{$endif}
  2200. Taicpu(p), R_EAX);
  2201. DestroyReg(CurProp, R_EDX, true)
  2202. End;
  2203. A_IMUL:
  2204. Begin
  2205. ReadOp(CurProp,Taicpu(p).oper[0]);
  2206. ReadOp(CurProp,Taicpu(p).oper[1]);
  2207. If (Taicpu(p).oper[2].typ = top_none) Then
  2208. If (Taicpu(p).oper[1].typ = top_none) Then
  2209. Begin
  2210. ReadReg(CurProp,R_EAX);
  2211. {$ifdef statedebug}
  2212. hp := tai_comment.Create(strpnew('destroying eax and edx'));
  2213. insertllitem(asml,p,p.next,hp);
  2214. {$endif statedebug}
  2215. { DestroyReg(CurProp, R_EAX, true); }
  2216. AddInstr2RegContents({$ifdef statedebug}asml,{$endif}
  2217. Taicpu(p), R_EAX);
  2218. DestroyReg(CurProp, R_EDX, true)
  2219. End
  2220. Else
  2221. AddInstr2OpContents(
  2222. {$ifdef statedebug}asml,{$endif}
  2223. Taicpu(p), Taicpu(p).oper[1])
  2224. Else
  2225. AddInstr2OpContents({$ifdef statedebug}asml,{$endif}
  2226. Taicpu(p), Taicpu(p).oper[2]);
  2227. End;
  2228. A_LEA:
  2229. begin
  2230. readop(curprop,Taicpu(p).oper[0]);
  2231. if reginref(Taicpu(p).oper[1].reg,Taicpu(p).oper[0].ref^) then
  2232. AddInstr2RegContents({$ifdef statedebug}asml,{$endif}
  2233. Taicpu(p), Taicpu(p).oper[1].reg)
  2234. else
  2235. begin
  2236. {$ifdef statedebug}
  2237. hp := tai_comment.Create(strpnew('destroying & initing'+
  2238. std_reg2str[Taicpu(p).oper[1].reg])));
  2239. insertllitem(asml,p,p.next,hp);
  2240. {$endif statedebug}
  2241. destroyreg(curprop,Taicpu(p).oper[1].reg,true);
  2242. with curprop^.regs[Taicpu(p).oper[1].reg] Do
  2243. begin
  2244. typ := con_ref;
  2245. startmod := p;
  2246. nrOfMods := 1;
  2247. end
  2248. end;
  2249. end;
  2250. Else
  2251. Begin
  2252. Cnt := 1;
  2253. While (Cnt <= MaxCh) And
  2254. (InstrProp.Ch[Cnt] <> Ch_None) Do
  2255. Begin
  2256. Case InstrProp.Ch[Cnt] Of
  2257. Ch_REAX..Ch_REDI: ReadReg(CurProp,TCh2Reg(InstrProp.Ch[Cnt]));
  2258. Ch_WEAX..Ch_RWEDI:
  2259. Begin
  2260. If (InstrProp.Ch[Cnt] >= Ch_RWEAX) Then
  2261. ReadReg(CurProp, TCh2Reg(InstrProp.Ch[Cnt]));
  2262. {$ifdef statedebug}
  2263. hp := tai_comment.Create(strpnew('destroying '+
  2264. std_reg2str[TCh2Reg(InstrProp.Ch[Cnt])])));
  2265. insertllitem(asml,p,p.next,hp);
  2266. {$endif statedebug}
  2267. DestroyReg(CurProp, TCh2Reg(InstrProp.Ch[Cnt]), true);
  2268. End;
  2269. Ch_MEAX..Ch_MEDI:
  2270. AddInstr2RegContents({$ifdef statedebug} asml,{$endif}
  2271. Taicpu(p),TCh2Reg(InstrProp.Ch[Cnt]));
  2272. Ch_CDirFlag: CurProp^.DirFlag := F_NotSet;
  2273. Ch_SDirFlag: CurProp^.DirFlag := F_Set;
  2274. Ch_Rop1: ReadOp(CurProp, Taicpu(p).oper[0]);
  2275. Ch_Rop2: ReadOp(CurProp, Taicpu(p).oper[1]);
  2276. Ch_ROp3: ReadOp(CurProp, Taicpu(p).oper[2]);
  2277. Ch_Wop1..Ch_RWop1:
  2278. Begin
  2279. If (InstrProp.Ch[Cnt] in [Ch_RWop1]) Then
  2280. ReadOp(CurProp, Taicpu(p).oper[0]);
  2281. DestroyOp(p, Taicpu(p).oper[0]);
  2282. End;
  2283. Ch_Mop1:
  2284. AddInstr2OpContents({$ifdef statedebug} asml, {$endif}
  2285. Taicpu(p), Taicpu(p).oper[0]);
  2286. Ch_Wop2..Ch_RWop2:
  2287. Begin
  2288. If (InstrProp.Ch[Cnt] = Ch_RWop2) Then
  2289. ReadOp(CurProp, Taicpu(p).oper[1]);
  2290. DestroyOp(p, Taicpu(p).oper[1]);
  2291. End;
  2292. Ch_Mop2:
  2293. AddInstr2OpContents({$ifdef statedebug} asml, {$endif}
  2294. Taicpu(p), Taicpu(p).oper[1]);
  2295. Ch_WOp3..Ch_RWOp3:
  2296. Begin
  2297. If (InstrProp.Ch[Cnt] = Ch_RWOp3) Then
  2298. ReadOp(CurProp, Taicpu(p).oper[2]);
  2299. DestroyOp(p, Taicpu(p).oper[2]);
  2300. End;
  2301. Ch_Mop3:
  2302. AddInstr2OpContents({$ifdef statedebug} asml, {$endif}
  2303. Taicpu(p), Taicpu(p).oper[2]);
  2304. Ch_WMemEDI:
  2305. Begin
  2306. ReadReg(CurProp, R_EDI);
  2307. FillChar(TmpRef, SizeOf(TmpRef), 0);
  2308. TmpRef.Base := R_EDI;
  2309. tmpRef.index := R_EDI;
  2310. DestroyRefs(p, TmpRef, R_NO)
  2311. End;
  2312. Ch_RFlags:
  2313. if assigned(LastFlagsChangeProp) then
  2314. LastFlagsChangeProp^.FlagsUsed := true;
  2315. Ch_WFlags:
  2316. LastFlagsChangeProp := CurProp;
  2317. Ch_RWFlags:
  2318. begin
  2319. if assigned(LastFlagsChangeProp) then
  2320. LastFlagsChangeProp^.FlagsUsed := true;
  2321. LastFlagsChangeProp := CurProp;
  2322. end;
  2323. Ch_FPU:;
  2324. Else
  2325. Begin
  2326. {$ifdef statedebug}
  2327. hp := tai_comment.Create(strpnew(
  2328. 'destroying all regs for prev instruction')));
  2329. insertllitem(asml,p, p.next,hp);
  2330. {$endif statedebug}
  2331. DestroyAllRegs(CurProp,true,true);
  2332. LastFlagsChangeProp := CurProp;
  2333. End;
  2334. End;
  2335. Inc(Cnt);
  2336. End
  2337. End;
  2338. end;
  2339. End;
  2340. End
  2341. Else
  2342. Begin
  2343. {$ifdef statedebug}
  2344. hp := tai_comment.Create(strpnew(
  2345. 'destroying all regs: unknown Tai: '+tostr(ord(p.typ)))));
  2346. insertllitem(asml,p, p.next,hp);
  2347. {$endif statedebug}
  2348. DestroyAllRegs(CurProp,true,true);
  2349. End;
  2350. End;
  2351. Inc(InstrCnt);
  2352. prev := p;
  2353. GetNextInstruction(p, p);
  2354. End;
  2355. End;
  2356. Function InitDFAPass2(BlockStart, BlockEnd: Tai): Boolean;
  2357. {reserves memory for the PTaiProps in one big memory block when not using
  2358. TP, returns False if not enough memory is available for the optimizer in all
  2359. cases}
  2360. Var p: Tai;
  2361. Count: Longint;
  2362. { TmpStr: String; }
  2363. Begin
  2364. P := BlockStart;
  2365. SkipHead(P);
  2366. NrOfTaiObjs := 0;
  2367. While (P <> BlockEnd) Do
  2368. Begin
  2369. {$IfDef JumpAnal}
  2370. Case p.Typ Of
  2371. ait_label:
  2372. Begin
  2373. If not labelCanBeSkipped(Tai_label(p)) Then
  2374. LTable^[Tai_Label(p).l^.labelnr-LoLab].InstrNr := NrOfTaiObjs
  2375. End;
  2376. ait_instruction:
  2377. begin
  2378. if Taicpu(p).is_jmp then
  2379. begin
  2380. If (tasmlabel(Taicpu(p).oper[0].sym).labelnr >= LoLab) And
  2381. (tasmlabel(Taicpu(p).oper[0].sym).labelnr <= HiLab) Then
  2382. Inc(LTable^[tasmlabel(Taicpu(p).oper[0].sym).labelnr-LoLab].RefsFound);
  2383. end;
  2384. end;
  2385. { ait_instruction:
  2386. Begin
  2387. If (Taicpu(p).opcode = A_PUSH) And
  2388. (Taicpu(p).oper[0].typ = top_symbol) And
  2389. (PCSymbol(Taicpu(p).oper[0])^.offset = 0) Then
  2390. Begin
  2391. TmpStr := StrPas(PCSymbol(Taicpu(p).oper[0])^.symbol);
  2392. If}
  2393. End;
  2394. {$EndIf JumpAnal}
  2395. Inc(NrOfTaiObjs);
  2396. GetNextInstruction(p, p);
  2397. End;
  2398. {Uncomment the next line to see how much memory the reloading optimizer needs}
  2399. { Writeln(NrOfTaiObjs*SizeOf(TTaiProp));}
  2400. {no need to check mem/maxavail, we've got as much virtual memory as we want}
  2401. If NrOfTaiObjs <> 0 Then
  2402. Begin
  2403. InitDFAPass2 := True;
  2404. GetMem(TaiPropBlock, NrOfTaiObjs*SizeOf(TTaiProp));
  2405. fillchar(TaiPropBlock^,NrOfTaiObjs*SizeOf(TTaiProp),0);
  2406. p := BlockStart;
  2407. SkipHead(p);
  2408. For Count := 1 To NrOfTaiObjs Do
  2409. Begin
  2410. PTaiProp(p.OptInfo) := @TaiPropBlock^[Count];
  2411. GetNextInstruction(p, p);
  2412. End;
  2413. End
  2414. Else InitDFAPass2 := False;
  2415. End;
  2416. Function DFAPass2(
  2417. {$ifdef statedebug}
  2418. AsmL: TAAsmOutPut;
  2419. {$endif statedebug}
  2420. BlockStart, BlockEnd: Tai): Boolean;
  2421. Begin
  2422. If InitDFAPass2(BlockStart, BlockEnd) Then
  2423. Begin
  2424. DoDFAPass2(
  2425. {$ifdef statedebug}
  2426. asml,
  2427. {$endif statedebug}
  2428. BlockStart, BlockEnd);
  2429. DFAPass2 := True
  2430. End
  2431. Else DFAPass2 := False;
  2432. End;
  2433. Procedure ShutDownDFA;
  2434. Begin
  2435. If LabDif <> 0 Then
  2436. FreeMem(LTable, LabDif*SizeOf(TLabelTableItem));
  2437. End;
  2438. End.
  2439. {
  2440. $Log$
  2441. Revision 1.44 2002-11-17 16:31:59 carl
  2442. * memory optimization (3-4%) : cleanup of tai fields,
  2443. cleanup of tdef and tsym fields.
  2444. * make it work for m68k
  2445. Revision 1.43 2002/08/18 20:06:29 peter
  2446. * inlining is now also allowed in interface
  2447. * renamed write/load to ppuwrite/ppuload
  2448. * tnode storing in ppu
  2449. * nld,ncon,nbas are already updated for storing in ppu
  2450. Revision 1.42 2002/08/17 09:23:44 florian
  2451. * first part of procinfo rewrite
  2452. Revision 1.41 2002/07/01 18:46:31 peter
  2453. * internal linker
  2454. * reorganized aasm layer
  2455. Revision 1.40 2002/06/24 12:43:00 jonas
  2456. * fixed errors found with new -CR code from Peter when cycling with -O2p3r
  2457. Revision 1.39 2002/06/09 12:56:04 jonas
  2458. * IDIV reads edx too (but now the div/mod optimization fails :/ )
  2459. Revision 1.38 2002/05/18 13:34:22 peter
  2460. * readded missing revisions
  2461. Revision 1.37 2002/05/16 19:46:51 carl
  2462. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2463. + try to fix temp allocation (still in ifdef)
  2464. + generic constructor calls
  2465. + start of tassembler / tmodulebase class cleanup
  2466. Revision 1.34 2002/05/12 16:53:16 peter
  2467. * moved entry and exitcode to ncgutil and cgobj
  2468. * foreach gets extra argument for passing local data to the
  2469. iterator function
  2470. * -CR checks also class typecasts at runtime by changing them
  2471. into as
  2472. * fixed compiler to cycle with the -CR option
  2473. * fixed stabs with elf writer, finally the global variables can
  2474. be watched
  2475. * removed a lot of routines from cga unit and replaced them by
  2476. calls to cgobj
  2477. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  2478. u32bit then the other is typecasted also to u32bit without giving
  2479. a rangecheck warning/error.
  2480. * fixed pascal calling method with reversing also the high tree in
  2481. the parast, detected by tcalcst3 test
  2482. Revision 1.33 2002/04/21 15:32:59 carl
  2483. * changeregsize -> rg.makeregsize
  2484. Revision 1.32 2002/04/20 21:37:07 carl
  2485. + generic FPC_CHECKPOINTER
  2486. + first parameter offset in stack now portable
  2487. * rename some constants
  2488. + move some cpu stuff to other units
  2489. - remove unused constents
  2490. * fix stacksize for some targets
  2491. * fix generic size problems which depend now on EXTEND_SIZE constant
  2492. * removing frame pointer in routines is only available for : i386,m68k and vis targets
  2493. Revision 1.31 2002/04/15 19:44:20 peter
  2494. * fixed stackcheck that would be called recursively when a stack
  2495. error was found
  2496. * generic changeregsize(reg,size) for i386 register resizing
  2497. * removed some more routines from cga unit
  2498. * fixed returnvalue handling
  2499. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  2500. Revision 1.30 2002/04/15 19:12:09 carl
  2501. + target_info.size_of_pointer -> pointer_size
  2502. + some cleanup of unused types/variables
  2503. * move several constants from cpubase to their specific units
  2504. (where they are used)
  2505. + att_Reg2str -> gas_reg2str
  2506. + int_reg2str -> std_reg2str
  2507. Revision 1.29 2002/04/14 17:00:49 carl
  2508. + att_reg2str -> std_reg2str
  2509. Revision 1.28 2002/04/02 17:11:34 peter
  2510. * tlocation,treference update
  2511. * LOC_CONSTANT added for better constant handling
  2512. * secondadd splitted in multiple routines
  2513. * location_force_reg added for loading a location to a register
  2514. of a specified size
  2515. * secondassignment parses now first the right and then the left node
  2516. (this is compatible with Kylix). This saves a lot of push/pop especially
  2517. with string operations
  2518. * adapted some routines to use the new cg methods
  2519. Revision 1.27 2002/03/31 20:26:38 jonas
  2520. + a_loadfpu_* and a_loadmm_* methods in tcg
  2521. * register allocation is now handled by a class and is mostly processor
  2522. independent (+rgobj.pas and i386/rgcpu.pas)
  2523. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  2524. * some small improvements and fixes to the optimizer
  2525. * some register allocation fixes
  2526. * some fpuvaroffset fixes in the unary minus node
  2527. * push/popusedregisters is now called rg.save/restoreusedregisters and
  2528. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  2529. also better optimizable)
  2530. * fixed and optimized register saving/restoring for new/dispose nodes
  2531. * LOC_FPU locations now also require their "register" field to be set to
  2532. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  2533. - list field removed of the tnode class because it's not used currently
  2534. and can cause hard-to-find bugs
  2535. Revision 1.26 2002/03/04 19:10:13 peter
  2536. * removed compiler warnings
  2537. }