daopt386.pas 94 KB

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