daopt386.pas 99 KB

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