rgobj.pas 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements the base class for the register allocator
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. {$i fpcdefs.inc}
  19. {# @abstract(Abstract register allocator unit)
  20. This unit contains services to allocate, free
  21. references and registers which are used by
  22. the code generator.
  23. }
  24. {*******************************************************************************
  25. (applies to new register allocator)
  26. Register allocator introduction.
  27. Free Pascal uses a Chaitin style register allocator similair to the one
  28. described in the book "Modern compiler implementation in C" by Andrew W. Appel.,
  29. published by Cambridge University Press.
  30. Reading this book is recommended for a complete understanding. Here is a small
  31. introduction.
  32. The code generator thinks it has an infinite amount of registers. Our processorhas a limited amount of registers. Therefore we must reduce the amount of
  33. registers until there are less enough to fit into the processors registers.
  34. Registers can interfere or not interfere. If two imaginary registers interfere
  35. they cannot be placed into the same psysical register. Reduction of registers
  36. is done by:
  37. - "coalescing" Two registers that do not interfere are combined
  38. into one register.
  39. - "spilling" A register is changed into a memory location and the generated
  40. code is modified to use the memory location instead of the register.
  41. Register allocation is a graph colouring problem. Each register is a colour, and
  42. if two registers interfere there is a connection between them in the graph.
  43. In addition to the imaginary registers in the code generator, the psysical
  44. CPU registers are also present in this graph. This allows us to make
  45. interferences between imaginary registers and cpu registers. This is very
  46. usefull for describing archtectural constraints, like for example that
  47. the div instruction modifies edx, so variables that are in use at that time
  48. cannot be stored into edx. This can be modelled by making edx interfere
  49. with those variables.
  50. Graph colouring is an NP complete problem. Therefore we use an approximation
  51. that pushes registers to colour on to a stack. This is done in the "simplify"
  52. procedure.
  53. *******************************************************************************}
  54. unit rgobj;
  55. interface
  56. uses
  57. cutils, cpubase,
  58. cpuinfo,
  59. aasmbase,aasmtai,aasmcpu,
  60. cclasses,globtype,cginfo,cgbase,node
  61. {$ifdef delphi}
  62. ,dmisc
  63. {$endif}
  64. ;
  65. const
  66. ALL_INTREGISTERS=[first_supreg..last_supreg]-[RS_STACK_POINTER_REG];
  67. ALL_OTHERREGISTERS=[firstreg..lastreg];
  68. type
  69. regvarother_longintarray = array[firstreg..lastreg] of longint;
  70. regvarother_booleanarray = array[firstreg..lastreg] of boolean;
  71. regvarint_longintarray = array[first_supreg..last_supreg] of longint;
  72. regvarint_ptreearray = array[first_supreg..last_supreg] of tnode;
  73. tpushedsavedloc = record
  74. case byte of
  75. 0: (pushed: boolean);
  76. 1: (ofs: longint);
  77. end;
  78. tpushedsavedother = array[firstreg..lastreg] of tpushedsavedloc;
  79. {$ifndef newra}
  80. Tpushedsavedint = array[first_supreg..last_supreg] of Tpushedsavedloc;
  81. {$endif}
  82. Tinterferencebitmap=array[Tsuperregister] of set of Tsuperregister;
  83. Tinterferenceadjlist=array[Tsuperregister] of Pstring;
  84. Tinterferencegraph=record
  85. bitmap:Tinterferencebitmap;
  86. adjlist:Tinterferenceadjlist;
  87. end;
  88. Pinterferencegraph=^Tinterferencegraph;
  89. Tmovelist=record
  90. count:cardinal;
  91. data:array[0..$ffff] of Tlinkedlistitem;
  92. end;
  93. Pmovelist=^Tmovelist;
  94. {In the register allocator we keep track of move instructions.
  95. These instructions are moved between five linked lists. There
  96. is also a linked list per register to keep track about the moves
  97. it is associated with. Because we need to determine quickly in
  98. which of the five lists it is we add anu enumeradtion to each
  99. move instruction.}
  100. Tmoveset=(ms_coalesced_moves,ms_constrained_moves,ms_frozen_moves,
  101. ms_worklist_moves,ms_active_moves);
  102. Tmoveins=class(Tlinkedlistitem)
  103. moveset:Tmoveset;
  104. { $ifdef ra_debug}
  105. x,y:Tsuperregister;
  106. { $endif}
  107. instruction:Taicpu;
  108. end;
  109. {#
  110. This class implements the abstract register allocator
  111. It is used by the code generator to allocate and free
  112. registers which might be valid across nodes. It also
  113. contains utility routines related to registers.
  114. Some of the methods in this class should be overriden
  115. by cpu-specific implementations.
  116. }
  117. trgobj = class
  118. { The "usableregsxxx" contain all registers of type "xxx" that }
  119. { aren't currently allocated to a regvar. The "unusedregsxxx" }
  120. { contain all registers of type "xxx" that aren't currently }
  121. { allocated }
  122. lastintreg,maxintreg:Tsuperregister;
  123. unusedregsint,usableregsint:Tsupregset;
  124. unusedregsaddr,usableregsaddr:Tsupregset;
  125. unusedregsfpu,usableregsfpu : tregisterset;
  126. unusedregsmm,usableregsmm : tregisterset;
  127. { these counters contain the number of elements in the }
  128. { unusedregsxxx/usableregsxxx sets }
  129. {$ifndef newra}
  130. countunusedregsint,
  131. countunusedregsaddr,
  132. {$endif}
  133. countunusedregsfpu,
  134. countunusedregsmm : byte;
  135. countusableregsint,
  136. countusableregsaddr,
  137. countusableregsfpu,
  138. countusableregsmm : byte;
  139. { Contains the registers which are really used by the proc itself.
  140. It doesn't take care of registers used by called procedures
  141. }
  142. {$ifdef newra}
  143. savedintbyproc,
  144. {$endif}
  145. used_in_proc_int,
  146. usedaddrinproc : tsupregset;
  147. used_in_proc_other : tregisterset;
  148. reg_pushes_other : regvarother_longintarray;
  149. {$ifndef newra}
  150. reg_pushes_int : regvarint_longintarray;
  151. {$endif}
  152. is_reg_var_other : regvarother_booleanarray;
  153. is_reg_var_int : Tsupregset;
  154. regvar_loaded_other : regvarother_booleanarray;
  155. regvar_loaded_int : Tsupregset;
  156. {$ifdef newra}
  157. colour : array[Tsuperregister] of Tsuperregister;
  158. spillednodes : string;
  159. {$endif}
  160. { tries to hold the amount of times which the current tree is processed }
  161. t_times: longint;
  162. constructor create(Acpu_registers:byte);
  163. {# Allocate a general purpose register
  164. An internalerror will be generated if there
  165. is no more free registers which can be allocated
  166. }
  167. function getregisterint(list:Taasmoutput;size:Tcgsize):Tregister;{$ifndef newra}virtual;{$endif}
  168. {$ifdef newra}
  169. procedure add_constraints(reg:Tnewregister);virtual;
  170. {# Allocate an ABT register
  171. An internalerror will be generated if there
  172. is no more free registers which can be allocated
  173. An explanantion of abt registers can be found near the implementation.
  174. }
  175. function getabtregisterint(list:Taasmoutput;size:Tcgsize):Tregister;
  176. {$endif}
  177. {# Free a general purpose register
  178. @param(r register to free)
  179. }
  180. procedure ungetregisterint(list: taasmoutput; r : tregister); virtual;
  181. {# Allocate a floating point register
  182. An internalerror will be generated if there
  183. is no more free registers which can be allocated
  184. }
  185. function getregisterfpu(list: taasmoutput;size:Tcgsize) : tregister; virtual;
  186. {# Free a floating point register
  187. @param(r register to free)
  188. }
  189. procedure ungetregisterfpu(list: taasmoutput; r : tregister;size:TCGsize); virtual;
  190. function getregistermm(list: taasmoutput) : tregister; virtual;
  191. procedure ungetregistermm(list: taasmoutput; r : tregister); virtual;
  192. {# Allocate an address register.
  193. Address registers are the only registers which can
  194. be used as a base register in references (treference).
  195. On most cpu's this is the same as a general purpose
  196. register.
  197. An internalerror will be generated if there
  198. is no more free registers which can be allocated
  199. }
  200. function getaddressregister(list:Taasmoutput):Tregister;virtual;
  201. procedure ungetaddressregister(list: taasmoutput; r: tregister); virtual;
  202. {# Verify if the specified register is an address or
  203. general purpose register. Returns TRUE if @var(reg)
  204. is an adress register.
  205. This routine should only be used to check on
  206. general purpose or address register. It will
  207. not work on multimedia or floating point
  208. registers
  209. @param(reg register to verify)
  210. }
  211. function isaddressregister(reg: tregister): boolean; virtual;
  212. {# Tries to allocate the passed register, if possible
  213. @param(r specific register to allocate)
  214. }
  215. function getexplicitregisterint(list:Taasmoutput;r:Tnewregister):Tregister;virtual;
  216. {# Tries to allocate the passed fpu register, if possible
  217. @param(r specific register to allocate)
  218. }
  219. function getexplicitregisterfpu(list : taasmoutput; r : Toldregister) : tregister;virtual;
  220. {# Deallocate any kind of register }
  221. procedure ungetregister(list: taasmoutput; r : tregister); virtual;
  222. {# Deallocate all registers which are allocated
  223. in the specified reference. On most systems,
  224. this will free the base and index registers
  225. of the specified reference.
  226. @param(ref reference which must have its registers freed)
  227. }
  228. procedure ungetreference(list: taasmoutput; const ref : treference); virtual;
  229. {# Reset the register allocator information (usable registers etc).
  230. Please note that it is mortal sins to call cleartempgen during
  231. graph colouring (that is between prepare_colouring and
  232. epilogue_colouring).
  233. }
  234. procedure cleartempgen;virtual;
  235. {# Convert a register to a specified register size, and return that register size }
  236. function makeregsize(reg: tregister; size: tcgsize): tregister; virtual;
  237. {# saves register variables (restoring happens automatically) }
  238. {$ifndef newra}
  239. procedure saveintregvars(list:Taasmoutput;const s:Tsupregset);
  240. {$endif}
  241. procedure saveotherregvars(list:Taasmoutput;const s:Tregisterset);
  242. {# Saves in temporary references (allocated via the temp. allocator)
  243. the registers defined in @var(s). The registers are only saved
  244. if they are currently in use, otherwise they are left as is.
  245. On processors which have instructions which manipulate the stack,
  246. this routine should be overriden for performance reasons.
  247. @param(list) List to add the instruction to
  248. @param(saved) Array of saved register information
  249. @param(s) Registers which might require saving
  250. }
  251. {$ifndef newra}
  252. procedure saveusedintregisters(list:Taasmoutput;
  253. var saved:Tpushedsavedint;
  254. const s:Tsupregset);virtual;
  255. {$endif}
  256. procedure saveusedotherregisters(list:Taasmoutput;
  257. var saved:Tpushedsavedother;
  258. const s:Tregisterset);virtual;
  259. {# Restores the registers which were saved with a call
  260. to @var(saveusedregisters).
  261. On processors which have instructions which manipulate the stack,
  262. this routine should be overriden for performance reasons.
  263. }
  264. {$ifndef newra}
  265. procedure restoreusedintregisters(list:Taasmoutput;
  266. const saved:Tpushedsavedint);virtual;
  267. {$endif}
  268. procedure restoreusedotherregisters(list:Taasmoutput;
  269. const saved:Tpushedsavedother);virtual;
  270. { used when deciding which registers to use for regvars }
  271. {$ifndef newra}
  272. procedure incrementintregisterpushed(const s:Tsupregset);
  273. {$endif}
  274. procedure incrementotherregisterpushed(const s: tregisterset);
  275. procedure clearregistercount;
  276. procedure resetusableregisters;virtual;
  277. procedure makeregvarint(reg:Tsuperregister);
  278. procedure makeregvarother(reg:Tregister);
  279. procedure saveStateForInline(var state: pointer);virtual;
  280. procedure restoreStateAfterInline(var state: pointer);virtual;
  281. procedure saveUnusedState(var state: pointer);virtual;
  282. procedure restoreUnusedState(var state: pointer);virtual;
  283. {$ifdef newra}
  284. {$ifdef ra_debug}
  285. procedure writegraph;
  286. {$endif}
  287. procedure add_move_instruction(instr:Taicpu);
  288. procedure prepare_colouring;
  289. procedure epilogue_colouring;
  290. procedure colour_registers;
  291. function spill_registers(list:Taasmoutput;const regs_to_spill:string):boolean;
  292. {$endif newra}
  293. protected
  294. cpu_registers:byte;
  295. {$ifdef newra}
  296. igraph:Tinterferencegraph;
  297. degree:array[0..255] of byte;
  298. alias:array[Tsuperregister] of Tsuperregister;
  299. simplifyworklist,freezeworklist,spillworklist:string;
  300. coalescednodes:string;
  301. selectstack:string;
  302. abtlist:string;
  303. movelist:array[Tsuperregister] of Pmovelist;
  304. worklist_moves,active_moves,frozen_moves,
  305. coalesced_moves,constrained_moves:Tlinkedlist;
  306. {$endif}
  307. { the following two contain the common (generic) code for all }
  308. { get- and ungetregisterxxx functions/procedures }
  309. function getregistergenother(list: taasmoutput; const lowreg, highreg: Toldregister;
  310. var unusedregs:Tregisterset;var countunusedregs:byte): tregister;
  311. function getregistergenint(list:Taasmoutput;subreg:Tsubregister;
  312. const lowreg,highreg:Tsuperregister;
  313. var fusedinproc,unusedregs:Tsupregset
  314. {$ifndef newra};var countunusedregs:byte{$endif}):Tregister;
  315. procedure ungetregistergen(list: taasmoutput; const r: tregister;
  316. const usableregs:tregisterset;var unusedregs: tregisterset; var countunusedregs: byte);
  317. procedure ungetregistergenint(list:taasmoutput;const r:Tregister;
  318. const usableregs:Tsupregset;
  319. var unusedregs:Tsupregset
  320. {$ifndef newra};var countunusedregs:byte{$endif});
  321. {$ifdef newra}
  322. procedure getregisterintinline(list:Taasmoutput;position:Tai;subreg:Tsubregister;var result:Tregister);
  323. procedure ungetregisterintinline(list:Taasmoutput;position:Tai;const r:Tregister);
  324. {$endif}
  325. {$ifdef TEMPREGDEBUG}
  326. reg_user : regvar_ptreearray;
  327. reg_releaser : regvar_ptreearray;
  328. {$endif TEMPREGDEBUG}
  329. {$ifdef TEMPREGDEBUG}
  330. procedure testregisters;
  331. {$endif TEMPREGDEBUGx}
  332. {$ifdef newra}
  333. procedure add_edge(u,v:Tsuperregister);
  334. procedure add_edges_used(u:Tsuperregister);
  335. procedure add_to_movelist(u:Tsuperregister;data:Tlinkedlistitem);
  336. function move_related(n:Tsuperregister):boolean;
  337. procedure make_work_list;
  338. procedure enable_moves(n:Tsuperregister);
  339. procedure decrement_degree(m:Tsuperregister);
  340. procedure simplify;
  341. function get_alias(n:Tsuperregister):Tsuperregister;
  342. procedure add_worklist(u:Tsuperregister);
  343. function adjacent_ok(u,v:Tsuperregister):boolean;
  344. function conservative(u,v:Tsuperregister):boolean;
  345. procedure combine(u,v:Tsuperregister);
  346. procedure coalesce;
  347. procedure freeze_moves(u:Tsuperregister);
  348. procedure freeze;
  349. procedure select_spill;
  350. procedure assign_colours;
  351. procedure clear_interferences(u:Tsuperregister);
  352. {$endif}
  353. end;
  354. const
  355. {# This value is used in tsaved. If the array value is equal
  356. to this, then this means that this register is not used.
  357. }
  358. reg_not_saved = $7fffffff;
  359. var
  360. {# This is the class instance used to access the register allocator class }
  361. rg: trgobj;
  362. { trerefence handling }
  363. {# Clear to zero a treference }
  364. procedure reference_reset(var ref : treference);
  365. procedure reference_reset_old(var ref : treference);
  366. {# Clear to zero a treference, and set is base address
  367. to base register.
  368. }
  369. procedure reference_reset_base(var ref : treference;base : tregister;offset : longint);
  370. procedure reference_reset_symbol(var ref : treference;sym : tasmsymbol;offset : longint);
  371. procedure reference_release(list: taasmoutput; const ref : treference);
  372. { This routine verifies if two references are the same, and
  373. if so, returns TRUE, otherwise returns false.
  374. }
  375. function references_equal(sref : treference;dref : treference) : boolean;
  376. { tlocation handling }
  377. procedure location_reset(var l : tlocation;lt:TCGLoc;lsize:TCGSize);
  378. procedure location_release(list: taasmoutput; const l : tlocation);
  379. procedure location_freetemp(list: taasmoutput; const l : tlocation);
  380. procedure location_copy(var destloc:tlocation; const sourceloc : tlocation);
  381. procedure location_swap(var destloc,sourceloc : tlocation);
  382. type
  383. psavedstate = ^tsavedstate;
  384. tsavedstate = record
  385. unusedregsint,usableregsint : Tsupregset;
  386. unusedregsaddr,usableregsaddr : Tsupregset;
  387. unusedregsfpu,usableregsfpu : tregisterset;
  388. unusedregsmm,usableregsmm : tregisterset;
  389. {$ifndef newra}
  390. countunusedregsint,
  391. countunusedregsaddr,
  392. {$endif}
  393. countunusedregsfpu,
  394. countunusedregsmm : byte;
  395. countusableregsint,
  396. countusableregsfpu,
  397. countusableregsmm : byte;
  398. { contains the registers which are really used by the proc itself }
  399. used_in_proc_int : tsupregset;
  400. used_in_proc_other : tregisterset;
  401. reg_pushes_other : regvarother_longintarray;
  402. reg_pushes_int : regvarint_longintarray;
  403. is_reg_var_other : regvarother_booleanarray;
  404. is_reg_var_int : Tsupregset;
  405. regvar_loaded_other: regvarother_booleanarray;
  406. regvar_loaded_int: Tsupregset;
  407. {$ifdef TEMPREGDEBUG}
  408. reg_user : regvar_ptreearray;
  409. reg_releaser : regvar_ptreearray;
  410. {$endif TEMPREGDEBUG}
  411. end;
  412. punusedstate = ^tunusedstate;
  413. tunusedstate = record
  414. unusedregsint : Tsupregset;
  415. unusedregsaddr : Tsupregset;
  416. unusedregsfpu : tregisterset;
  417. unusedregsmm : tregisterset;
  418. {$ifndef newra}
  419. countunusedregsint,
  420. countunusedregsaddr,
  421. {$endif}
  422. countunusedregsfpu,
  423. countunusedregsmm : byte;
  424. end;
  425. implementation
  426. uses
  427. systems,
  428. globals,verbose,
  429. cgobj,tgobj,regvars;
  430. constructor Trgobj.create(Acpu_registers:byte);
  431. begin
  432. used_in_proc_int := [];
  433. used_in_proc_other:=[];
  434. t_times := 0;
  435. resetusableregisters;
  436. lastintreg:=0;
  437. maxintreg:=first_imreg;
  438. cpu_registers:=Acpu_registers;
  439. {$ifdef TEMPREGDEBUG}
  440. fillchar(reg_user,sizeof(reg_user),0);
  441. fillchar(reg_releaser,sizeof(reg_releaser),0);
  442. {$endif TEMPREGDEBUG}
  443. {$ifdef newra}
  444. fillchar(igraph,sizeof(igraph),0);
  445. fillchar(degree,sizeof(degree),0);
  446. fillchar(movelist,sizeof(movelist),0);
  447. worklist_moves:=Tlinkedlist.create;
  448. abtlist:='';
  449. {$endif}
  450. end;
  451. function trgobj.getregistergenother(list: taasmoutput; const lowreg, highreg: Toldregister;
  452. var unusedregs: tregisterset; var countunusedregs: byte): tregister;
  453. var
  454. i: Toldregister;
  455. r: Tregister;
  456. begin
  457. for i:=lowreg to highreg do
  458. begin
  459. if i in unusedregs then
  460. begin
  461. exclude(unusedregs,i);
  462. include(used_in_proc_other,i);
  463. dec(countunusedregs);
  464. r.enum:=i;
  465. list.concat(tai_regalloc.alloc(r));
  466. result := r;
  467. exit;
  468. end;
  469. end;
  470. internalerror(10);
  471. end;
  472. function Trgobj.getregistergenint(list:Taasmoutput;
  473. subreg:Tsubregister;
  474. const lowreg,highreg:Tsuperregister;
  475. var fusedinproc,unusedregs:Tsupregset
  476. {$ifndef newra};var countunusedregs:byte{$endif}):Tregister;
  477. {$ifdef powerpc}
  478. {$ifndef newra}
  479. {$define reuseregs}
  480. {$endif newra}
  481. {$endif powerpc}
  482. var i:Tsuperregister;
  483. r:Tregister;
  484. begin
  485. {$ifdef reuseregs}
  486. i := lowreg;
  487. lastintreg := highreg;
  488. {$else reuseregs}
  489. if not (lastintreg in [lowreg..highreg]) then
  490. lastintreg:=lowreg;
  491. i:=lastintreg;
  492. {$endif reuseregs}
  493. repeat
  494. if i=highreg then
  495. i:=lowreg
  496. else
  497. inc(i);
  498. if (i in unusedregs) {$ifdef newra} and (pos(char(i),abtlist)=0) {$endif} then
  499. begin
  500. exclude(unusedregs,i);
  501. include(fusedinproc,i);
  502. {$ifndef newra}
  503. dec(countunusedregs);
  504. {$endif}
  505. r.enum:=R_INTREGISTER;
  506. r.number:=i shl 8 or subreg;
  507. list.concat(Tai_regalloc.alloc(r));
  508. result:=r;
  509. lastintreg:=i;
  510. if i>maxintreg then
  511. maxintreg:=i;
  512. {$ifdef newra}
  513. add_edges_used(i);
  514. {$endif}
  515. exit;
  516. end;
  517. until i=lastintreg;
  518. internalerror(10);
  519. end;
  520. procedure trgobj.ungetregistergen(list: taasmoutput; const r: tregister;
  521. const usableregs: tregisterset; var unusedregs: tregisterset; var countunusedregs: byte);
  522. begin
  523. if r.enum>lastreg then
  524. internalerror(2003010801);
  525. { takes much time }
  526. if not(r.enum in usableregs) then
  527. exit;
  528. {$ifdef TEMPREGDEBUG}
  529. if (r.enum in unusedregs) then
  530. {$ifdef EXTTEMPREGDEBUG}
  531. begin
  532. Comment(V_Debug,'register freed twice '+std_reg2str[r.enum]);
  533. testregisters32;
  534. exit;
  535. end
  536. {$else EXTTEMPREGDEBUG}
  537. exit
  538. {$endif EXTTEMPREGDEBUG}
  539. else
  540. {$endif TEMPREGDEBUG}
  541. inc(countunusedregs);
  542. include(unusedregs,r.enum);
  543. list.concat(tai_regalloc.dealloc(r));
  544. end;
  545. procedure trgobj.ungetregistergenint(list:taasmoutput;const r:Tregister;
  546. const usableregs:Tsupregset;
  547. var unusedregs:Tsupregset
  548. {$ifndef newra};var countunusedregs:byte{$endif});
  549. var supreg:Tsuperregister;
  550. begin
  551. if r.enum<=lastreg then
  552. internalerror(2003010803);
  553. supreg:=r.number shr 8;
  554. { takes much time }
  555. {$ifndef newra}
  556. if not(supreg in usableregs) then
  557. exit;
  558. {$endif}
  559. {$ifdef TEMPREGDEBUG}
  560. if (supreg in unusedregs) then
  561. {$ifdef EXTTEMPREGDEBUG}
  562. begin
  563. comment(v_debug,'register freed twice '+supreg_name(supreg));
  564. testregisters32
  565. exit;
  566. end
  567. {$else EXTTEMPREGDEBUG}
  568. exit
  569. {$endif EXTTEMPREGDEBUG}
  570. else
  571. {$endif TEMPREGDEBUG}
  572. {$ifndef newra}inc(countunusedregs){$endif};
  573. include(unusedregs,supreg);
  574. list.concat(tai_regalloc.dealloc(r));
  575. {$ifdef newra}
  576. add_edges_used(supreg);
  577. {$endif newra}
  578. end;
  579. function trgobj.getregisterint(list:taasmoutput;size:Tcgsize):Tregister;
  580. var subreg:Tsubregister;
  581. begin
  582. {$ifndef newra}
  583. if countunusedregsint=0 then
  584. internalerror(10);
  585. {$ifdef TEMPREGDEBUG}
  586. if curptree^^.usableregs-countunusedregsint>curptree^^.registers32 then
  587. internalerror(10);
  588. {$endif TEMPREGDEBUG}
  589. {$ifdef EXTTEMPREGDEBUG}
  590. if curptree^^.usableregs-countunusedregsint>curptree^^.reallyusedregs then
  591. curptree^^.reallyusedregs:=curptree^^.usableregs-countunusedregsint;
  592. {$endif EXTTEMPREGDEBUG}
  593. {$endif}
  594. subreg:=cgsize2subreg(size);
  595. result:=getregistergenint(list,
  596. subreg,
  597. {$ifdef newra}
  598. first_imreg,
  599. last_imreg,
  600. {$else}
  601. first_supreg,
  602. last_supreg,
  603. {$endif}
  604. used_in_proc_int,
  605. unusedregsint{$ifndef newra},
  606. countunusedregsint{$endif});
  607. {$ifdef TEMPREGDEBUG}
  608. reg_user[result]:=curptree^;
  609. testregisters32;
  610. {$endif TEMPREGDEBUG}
  611. {$ifdef newra}
  612. add_constraints(getregisterint.number);
  613. {$endif}
  614. end;
  615. {$ifdef newra}
  616. procedure Trgobj.add_constraints(reg:Tnewregister);
  617. begin
  618. end;
  619. {$endif}
  620. procedure trgobj.ungetregisterint(list : taasmoutput; r : tregister);
  621. begin
  622. ungetregistergenint(list,r,usableregsint,unusedregsint{$ifndef newra},
  623. countunusedregsint{$endif});
  624. {$ifdef TEMPREGDEBUG}
  625. reg_releaser[r]:=curptree^;
  626. testregisters32;
  627. {$endif TEMPREGDEBUG}
  628. end;
  629. { tries to allocate the passed register, if possible }
  630. function trgobj.getexplicitregisterint(list:Taasmoutput;r:Tnewregister):Tregister;
  631. var r2:Tregister;
  632. begin
  633. if (r shr 8) in unusedregsint then
  634. begin
  635. {$ifndef newra}
  636. dec(countunusedregsint);
  637. {$ifdef TEMPREGDEBUG}
  638. if curptree^^.usableregs-countunusedregsint>curptree^^.registers32 then
  639. internalerror(10);
  640. reg_user[r shr 8]:=curptree^;
  641. {$endif TEMPREGDEBUG}
  642. {$endif newra}
  643. exclude(unusedregsint,r shr 8);
  644. include(used_in_proc_int,r shr 8);
  645. r2.enum:=R_INTREGISTER;
  646. r2.number:=r;
  647. list.concat(tai_regalloc.alloc(r2));
  648. {$ifdef TEMPREGDEBUG}
  649. testregisters32;
  650. {$endif TEMPREGDEBUG}
  651. end
  652. else
  653. internalerror(200301103);
  654. getexplicitregisterint:=r2;
  655. end;
  656. { tries to allocate the passed register, if possible }
  657. function trgobj.getexplicitregisterfpu(list : taasmoutput; r : Toldregister) : tregister;
  658. var r2:Tregister;
  659. begin
  660. if r in unusedregsfpu then
  661. begin
  662. dec(countunusedregsfpu);
  663. {$ifdef TEMPREGDEBUG}
  664. if curptree^^.usableregs-countunusedregsint>curptree^^.registers32 then
  665. internalerror(10);
  666. reg_user[r]:=curptree^;
  667. {$endif TEMPREGDEBUG}
  668. exclude(unusedregsfpu,r);
  669. include(used_in_proc_other,r);
  670. r2.enum:=r;
  671. list.concat(tai_regalloc.alloc(r2));
  672. getexplicitregisterfpu:=r2;
  673. {$ifdef TEMPREGDEBUG}
  674. testregisters32;
  675. {$endif TEMPREGDEBUG}
  676. end
  677. else
  678. {$warning Size for FPU reg is maybe not correct}
  679. getexplicitregisterfpu:=getregisterfpu(list,OS_F32);
  680. end;
  681. function trgobj.getregisterfpu(list: taasmoutput;size:Tcgsize) : tregister;
  682. begin
  683. if countunusedregsfpu=0 then
  684. internalerror(10);
  685. result := getregistergenother(list,firstsavefpureg,lastsavefpureg,
  686. unusedregsfpu,countunusedregsfpu);
  687. end;
  688. procedure trgobj.ungetregisterfpu(list : taasmoutput; r : tregister;size:TCGsize);
  689. begin
  690. ungetregistergen(list,r,usableregsfpu,unusedregsfpu,
  691. countunusedregsfpu);
  692. end;
  693. function trgobj.getregistermm(list: taasmoutput) : tregister;
  694. begin
  695. if countunusedregsmm=0 then
  696. internalerror(10);
  697. result := getregistergenother(list,firstsavemmreg,lastsavemmreg,
  698. unusedregsmm,countunusedregsmm);
  699. end;
  700. procedure trgobj.ungetregistermm(list: taasmoutput; r: tregister);
  701. begin
  702. ungetregistergen(list,r,usableregsmm,unusedregsmm,
  703. countunusedregsmm);
  704. end;
  705. function trgobj.getaddressregister(list:Taasmoutput): tregister;
  706. begin
  707. {An address register is OS_INT per definition.}
  708. result := getregisterint(list,OS_INT);
  709. end;
  710. procedure trgobj.ungetaddressregister(list: taasmoutput; r: tregister);
  711. begin
  712. ungetregisterint(list,r);
  713. end;
  714. function trgobj.isaddressregister(reg: tregister): boolean;
  715. begin
  716. if reg.number<>0 then; { remove warning }
  717. result := true;
  718. end;
  719. procedure trgobj.ungetregister(list: taasmoutput; r : tregister);
  720. begin
  721. if r.enum=R_NO then
  722. exit;
  723. if r.enum>lastreg then
  724. internalerror(200301081);
  725. if r.enum in intregs then
  726. ungetregisterint(list,r)
  727. else if r.enum in fpuregs then
  728. ungetregisterfpu(list,r,OS_NO)
  729. else if r.enum in mmregs then
  730. ungetregistermm(list,r)
  731. else if r.enum in addrregs then
  732. ungetaddressregister(list,r)
  733. else internalerror(2002070602);
  734. end;
  735. procedure Trgobj.cleartempgen;
  736. {$ifdef newra}
  737. var i:Tsuperregister;
  738. {$endif newra}
  739. begin
  740. {$ifndef newra}
  741. countunusedregsint:=countusableregsint;
  742. {$endif}
  743. countunusedregsfpu:=countusableregsfpu;
  744. countunusedregsmm:=countusableregsmm;
  745. lastintreg:=0;
  746. maxintreg:=first_imreg;
  747. {$ifdef newra}
  748. unusedregsint:=[0..255];
  749. {$else}
  750. unusedregsint:=usableregsint;
  751. {$endif}
  752. unusedregsfpu:=usableregsfpu;
  753. unusedregsmm:=usableregsmm;
  754. {$ifdef newra}
  755. saved_by_proc_int:=[];
  756. for i:=low(Tsuperregister) to high(Tsuperregister) do
  757. begin
  758. if igraph.adjlist[i]<>nil then
  759. dispose(igraph.adjlist[i]);
  760. if movelist[i]<>nil then
  761. dispose(movelist[i]);
  762. end;
  763. fillchar(movelist,sizeof(movelist),0);
  764. fillchar(igraph,sizeof(igraph),0);
  765. fillchar(degree,sizeof(degree),0);
  766. worklist_moves.clear;
  767. abtlist:='';
  768. {$endif}
  769. end;
  770. procedure trgobj.ungetreference(list : taasmoutput; const ref : treference);
  771. begin
  772. if ref.base.number<>NR_NO then
  773. ungetregisterint(list,ref.base);
  774. if ref.index.number<>NR_NO then
  775. ungetregisterint(list,ref.index);
  776. end;
  777. {$ifndef newra}
  778. procedure trgobj.saveintregvars(list:Taasmoutput;const s:Tsupregset);
  779. var r:Tsuperregister;
  780. hr: tregister;
  781. begin
  782. if not(cs_regalloc in aktglobalswitches) then
  783. exit;
  784. for r:=firstsaveintreg to lastsaveintreg do
  785. if (r in is_reg_var_int) and
  786. (r in s) then
  787. begin
  788. hr.number:=r shl 8;
  789. hr.enum:=R_INTREGISTER;
  790. store_regvar(list,hr);
  791. end;
  792. end;
  793. {$endif}
  794. procedure trgobj.saveotherregvars(list: taasmoutput; const s: tregisterset);
  795. var
  796. r: Tregister;
  797. begin
  798. if not(cs_regalloc in aktglobalswitches) then
  799. exit;
  800. if firstsavefpureg <> R_NO then
  801. for r.enum := firstsavefpureg to lastsavefpureg do
  802. if is_reg_var_other[r.enum] and
  803. (r.enum in s) then
  804. store_regvar(list,r);
  805. if firstsavemmreg <> R_NO then
  806. for r.enum := firstsavemmreg to lastsavemmreg do
  807. if is_reg_var_other[r.enum] and
  808. (r.enum in s) then
  809. store_regvar(list,r);
  810. end;
  811. {$ifndef newra}
  812. procedure trgobj.saveusedintregisters(list:Taasmoutput;
  813. var saved:Tpushedsavedint;
  814. const s:Tsupregset);
  815. var r:Tsuperregister;
  816. r2:Tregister;
  817. hr : treference;
  818. begin
  819. used_in_proc_int:=used_in_proc_int+s;
  820. for r:=firstsaveintreg to lastsaveintreg do
  821. begin
  822. saved[r].ofs:=reg_not_saved;
  823. { if the register is used by the calling subroutine and if }
  824. { it's not a regvar (those are handled separately) }
  825. if not (r in is_reg_var_int) and
  826. (r in s) and
  827. { and is present in use }
  828. not(r in unusedregsint) then
  829. begin
  830. { then save it }
  831. tg.GetTemp(list,sizeof(aword),tt_persistent,hr);
  832. saved[r].ofs:=hr.offset;
  833. r2.enum:=R_INTREGISTER;
  834. r2.number:=r shl 8 or R_SUBWHOLE;
  835. cg.a_load_reg_ref(list,OS_INT,OS_INT,r2,hr);
  836. cg.a_reg_dealloc(list,r2);
  837. include(unusedregsint,r);
  838. inc(countunusedregsint);
  839. end;
  840. end;
  841. {$ifdef TEMPREGDEBUG}
  842. testregisters32;
  843. {$endif TEMPREGDEBUG}
  844. end;
  845. {$endif}
  846. procedure trgobj.saveusedotherregisters(list: taasmoutput;
  847. var saved : tpushedsavedother; const s: tregisterset);
  848. var
  849. r : tregister;
  850. hr : treference;
  851. begin
  852. used_in_proc_other:=used_in_proc_other + s;
  853. { don't try to save the fpu registers if not desired (e.g. for }
  854. { the 80x86) }
  855. if firstsavefpureg <> R_NO then
  856. for r.enum:=firstsavefpureg to lastsavefpureg do
  857. begin
  858. saved[r.enum].ofs:=reg_not_saved;
  859. { if the register is used by the calling subroutine and if }
  860. { it's not a regvar (those are handled separately) }
  861. if not is_reg_var_other[r.enum] and
  862. (r.enum in s) and
  863. { and is present in use }
  864. not(r.enum in unusedregsfpu) then
  865. begin
  866. { then save it }
  867. tg.GetTemp(list,extended_size,tt_persistent,hr);
  868. saved[r.enum].ofs:=hr.offset;
  869. cg.a_loadfpu_reg_ref(list,OS_FLOAT,r,hr);
  870. cg.a_reg_dealloc(list,r);
  871. include(unusedregsfpu,r.enum);
  872. inc(countunusedregsfpu);
  873. end;
  874. end;
  875. { don't save the vector registers if there's no support for them }
  876. if firstsavemmreg <> R_NO then
  877. for r.enum:=firstsavemmreg to lastsavemmreg do
  878. begin
  879. saved[r.enum].ofs:=reg_not_saved;
  880. { if the register is in use and if it's not a regvar (those }
  881. { are handled separately), save it }
  882. if not is_reg_var_other[r.enum] and
  883. (r.enum in s) and
  884. { and is present in use }
  885. not(r.enum in unusedregsmm) then
  886. begin
  887. { then save it }
  888. tg.GetTemp(list,mmreg_size,tt_persistent,hr);
  889. saved[r.enum].ofs:=hr.offset;
  890. cg.a_loadmm_reg_ref(list,r,hr);
  891. cg.a_reg_dealloc(list,r);
  892. include(unusedregsmm,r.enum);
  893. inc(countunusedregsmm);
  894. end;
  895. end;
  896. {$ifdef TEMPREGDEBUG}
  897. testregisters32;
  898. {$endif TEMPREGDEBUG}
  899. end;
  900. {$ifndef newra}
  901. procedure trgobj.restoreusedintregisters(list:Taasmoutput;
  902. const saved:Tpushedsavedint);
  903. var r:Tsuperregister;
  904. r2:Tregister;
  905. hr:Treference;
  906. begin
  907. for r:=lastsaveintreg downto firstsaveintreg do
  908. begin
  909. if saved[r].ofs <> reg_not_saved then
  910. begin
  911. r2.enum:=R_INTREGISTER;
  912. r2.number:=NR_FRAME_POINTER_REG;
  913. reference_reset_base(hr,r2,saved[r].ofs);
  914. r2.enum:=R_INTREGISTER;
  915. r2.number:=r shl 8 or R_SUBWHOLE;
  916. cg.a_reg_alloc(list,r2);
  917. cg.a_load_ref_reg(list,OS_INT,OS_INT,hr,r2);
  918. if not (r in unusedregsint) then
  919. { internalerror(10)
  920. in n386cal we always save/restore the reg *state*
  921. using save/restoreunusedstate -> the current state
  922. may not be real (JM) }
  923. else
  924. begin
  925. dec(countunusedregsint);
  926. exclude(unusedregsint,r);
  927. end;
  928. tg.UnGetTemp(list,hr);
  929. end;
  930. end;
  931. {$ifdef TEMPREGDEBUG}
  932. testregisters32;
  933. {$endif TEMPREGDEBUG}
  934. end;
  935. {$endif}
  936. procedure trgobj.restoreusedotherregisters(list : taasmoutput;
  937. const saved : tpushedsavedother);
  938. var
  939. r,r2 : tregister;
  940. hr : treference;
  941. begin
  942. if firstsavemmreg <> R_NO then
  943. for r.enum:=lastsavemmreg downto firstsavemmreg do
  944. begin
  945. if saved[r.enum].ofs <> reg_not_saved then
  946. begin
  947. r2.enum:=R_INTREGISTER;
  948. r2.number:=NR_FRAME_POINTER_REG;
  949. reference_reset_base(hr,r2,saved[r.enum].ofs);
  950. cg.a_reg_alloc(list,r);
  951. cg.a_loadmm_ref_reg(list,hr,r);
  952. if not (r.enum in unusedregsmm) then
  953. { internalerror(10)
  954. in n386cal we always save/restore the reg *state*
  955. using save/restoreunusedstate -> the current state
  956. may not be real (JM) }
  957. else
  958. begin
  959. dec(countunusedregsmm);
  960. exclude(unusedregsmm,r.enum);
  961. end;
  962. tg.UnGetTemp(list,hr);
  963. end;
  964. end;
  965. if firstsavefpureg <> R_NO then
  966. for r.enum:=lastsavefpureg downto firstsavefpureg do
  967. begin
  968. if saved[r.enum].ofs <> reg_not_saved then
  969. begin
  970. r2.enum:=R_INTREGISTER;
  971. r2.number:=NR_FRAME_POINTER_REG;
  972. reference_reset_base(hr,r2,saved[r.enum].ofs);
  973. cg.a_reg_alloc(list,r);
  974. cg.a_loadfpu_ref_reg(list,OS_FLOAT,hr,r);
  975. if not (r.enum in unusedregsfpu) then
  976. { internalerror(10)
  977. in n386cal we always save/restore the reg *state*
  978. using save/restoreunusedstate -> the current state
  979. may not be real (JM) }
  980. else
  981. begin
  982. dec(countunusedregsfpu);
  983. exclude(unusedregsfpu,r.enum);
  984. end;
  985. tg.UnGetTemp(list,hr);
  986. end;
  987. end;
  988. {$ifdef TEMPREGDEBUG}
  989. testregisters32;
  990. {$endif TEMPREGDEBUG}
  991. end;
  992. {$ifndef newra}
  993. procedure trgobj.incrementintregisterpushed(const s:Tsupregset);
  994. {$ifdef i386}
  995. var
  996. regi:Tsuperregister;
  997. {$endif i386}
  998. begin
  999. {$ifdef i386}
  1000. for regi:=firstsaveintreg to lastsaveintreg do
  1001. begin
  1002. if (regi in s) then
  1003. inc(reg_pushes_int[regi],t_times*2);
  1004. end;
  1005. {$endif i386}
  1006. end;
  1007. {$endif}
  1008. procedure trgobj.incrementotherregisterpushed(const s:Tregisterset);
  1009. {$ifdef i386}
  1010. var
  1011. regi : Toldregister;
  1012. {$endif i386}
  1013. begin
  1014. {$ifdef i386}
  1015. if firstsavefpureg <> R_NO then
  1016. for regi:=firstsavefpureg to lastsavefpureg do
  1017. begin
  1018. if (regi in s) then
  1019. inc(reg_pushes_other[regi],t_times*2);
  1020. end;
  1021. if firstsavemmreg <> R_NO then
  1022. for regi:=firstsavemmreg to lastsavemmreg do
  1023. begin
  1024. if (regi in s) then
  1025. inc(reg_pushes_other[regi],t_times*2);
  1026. end;
  1027. {$endif i386}
  1028. end;
  1029. procedure trgobj.clearregistercount;
  1030. begin
  1031. {$ifndef newra}
  1032. fillchar(reg_pushes_int,sizeof(reg_pushes_int),0);
  1033. {$endif}
  1034. fillchar(reg_pushes_other,sizeof(reg_pushes_other),0);
  1035. {ifndef i386}
  1036. { all used registers will have to be saved at the start and restored }
  1037. { at the end, but otoh regpara's do not have to be saved to memory }
  1038. { at the start (there is a move from regpara to regvar most of the }
  1039. { time though) -> set cost to 100+20 }
  1040. {$ifndef newra}
  1041. filldword(reg_pushes_int[firstsaveintreg],lastsaveintreg-firstsaveintreg+1,120);
  1042. {$endif}
  1043. filldword(reg_pushes_other[firstsavefpureg],ord(lastsavefpureg)-ord(firstsavefpureg)+1,120);
  1044. {endif not i386}
  1045. fillchar(is_reg_var_other,sizeof(is_reg_var_other),false);
  1046. is_reg_var_int:=[];
  1047. fillchar(regvar_loaded_other,sizeof(regvar_loaded_other),false);
  1048. regvar_loaded_int:=[];
  1049. end;
  1050. procedure trgobj.resetusableregisters;
  1051. begin
  1052. { initialize fields with constant values from cpubase }
  1053. countusableregsint := cpubase.c_countusableregsint;
  1054. countusableregsfpu := cpubase.c_countusableregsfpu;
  1055. countusableregsmm := cpubase.c_countusableregsmm;
  1056. usableregsint := cpubase.usableregsint;
  1057. usableregsfpu := cpubase.usableregsfpu;
  1058. usableregsmm := cpubase.usableregsmm;
  1059. clearregistercount;
  1060. end;
  1061. procedure trgobj.makeregvarint(reg:Tsuperregister);
  1062. begin
  1063. dec(countusableregsint);
  1064. {$ifndef newra}
  1065. dec(countunusedregsint);
  1066. {$endif}
  1067. exclude(usableregsint,reg);
  1068. exclude(unusedregsint,reg);
  1069. include(is_reg_var_int,reg);
  1070. include(used_in_proc_int,reg);
  1071. end;
  1072. procedure trgobj.makeregvarother(reg: tregister);
  1073. begin
  1074. if reg.enum>lastreg then
  1075. internalerror(200301081);
  1076. if reg.enum in intregs then
  1077. internalerror(200301151)
  1078. else if reg.enum in fpuregs then
  1079. begin
  1080. dec(countusableregsfpu);
  1081. dec(countunusedregsfpu);
  1082. exclude(usableregsfpu,reg.enum);
  1083. exclude(unusedregsfpu,reg.enum);
  1084. include(used_in_proc_other,reg.enum);
  1085. end
  1086. else if reg.enum in mmregs then
  1087. begin
  1088. dec(countusableregsmm);
  1089. dec(countunusedregsmm);
  1090. exclude(usableregsmm,reg.enum);
  1091. exclude(unusedregsmm,reg.enum);
  1092. include(used_in_proc_other,reg.enum);
  1093. end;
  1094. is_reg_var_other[reg.enum]:=true;
  1095. end;
  1096. {$ifdef TEMPREGDEBUG}
  1097. procedure trgobj.testregisters;
  1098. var
  1099. r: tregister;
  1100. test : byte;
  1101. begin
  1102. test:=0;
  1103. for r := firstsaveintreg to lastsaveintreg do
  1104. inc(test,ord(r in unusedregsint));
  1105. if test<>countunusedregsint then
  1106. internalerror(10);
  1107. end;
  1108. {$endif TEMPREGDEBUG}
  1109. procedure trgobj.saveStateForInline(var state: pointer);
  1110. begin
  1111. new(psavedstate(state));
  1112. psavedstate(state)^.unusedregsint := unusedregsint;
  1113. psavedstate(state)^.usableregsint := usableregsint;
  1114. psavedstate(state)^.unusedregsfpu := unusedregsfpu;
  1115. psavedstate(state)^.usableregsfpu := usableregsfpu;
  1116. psavedstate(state)^.unusedregsmm := unusedregsmm;
  1117. psavedstate(state)^.usableregsmm := usableregsmm;
  1118. {$ifndef newra}
  1119. psavedstate(state)^.countunusedregsint := countunusedregsint;
  1120. {$endif}
  1121. psavedstate(state)^.countunusedregsfpu := countunusedregsfpu;
  1122. psavedstate(state)^.countunusedregsmm := countunusedregsmm;
  1123. psavedstate(state)^.countusableregsint := countusableregsint;
  1124. psavedstate(state)^.countusableregsfpu := countusableregsfpu;
  1125. psavedstate(state)^.countusableregsmm := countusableregsmm;
  1126. psavedstate(state)^.used_in_proc_int := used_in_proc_int;
  1127. psavedstate(state)^.used_in_proc_other := used_in_proc_other;
  1128. {$ifndef newra}
  1129. psavedstate(state)^.reg_pushes_int := reg_pushes_int;
  1130. {$endif}
  1131. psavedstate(state)^.reg_pushes_other := reg_pushes_other;
  1132. psavedstate(state)^.is_reg_var_int := is_reg_var_int;
  1133. psavedstate(state)^.is_reg_var_other := is_reg_var_other;
  1134. psavedstate(state)^.regvar_loaded_int := regvar_loaded_int;
  1135. psavedstate(state)^.regvar_loaded_other := regvar_loaded_other;
  1136. {$ifdef TEMPREGDEBUG}
  1137. psavedstate(state)^.reg_user := reg_user;
  1138. psavedstate(state)^.reg_releaser := reg_releaser;
  1139. {$endif TEMPREGDEBUG}
  1140. end;
  1141. procedure trgobj.restoreStateAfterInline(var state: pointer);
  1142. begin
  1143. unusedregsint := psavedstate(state)^.unusedregsint;
  1144. usableregsint := psavedstate(state)^.usableregsint;
  1145. unusedregsfpu := psavedstate(state)^.unusedregsfpu;
  1146. usableregsfpu := psavedstate(state)^.usableregsfpu;
  1147. unusedregsmm := psavedstate(state)^.unusedregsmm;
  1148. usableregsmm := psavedstate(state)^.usableregsmm;
  1149. {$ifndef newra}
  1150. countunusedregsint := psavedstate(state)^.countunusedregsint;
  1151. {$endif}
  1152. countunusedregsfpu := psavedstate(state)^.countunusedregsfpu;
  1153. countunusedregsmm := psavedstate(state)^.countunusedregsmm;
  1154. countusableregsint := psavedstate(state)^.countusableregsint;
  1155. countusableregsfpu := psavedstate(state)^.countusableregsfpu;
  1156. countusableregsmm := psavedstate(state)^.countusableregsmm;
  1157. used_in_proc_int := psavedstate(state)^.used_in_proc_int;
  1158. used_in_proc_other := psavedstate(state)^.used_in_proc_other;
  1159. {$ifndef newra}
  1160. reg_pushes_int := psavedstate(state)^.reg_pushes_int;
  1161. {$endif}
  1162. reg_pushes_other := psavedstate(state)^.reg_pushes_other;
  1163. is_reg_var_int := psavedstate(state)^.is_reg_var_int;
  1164. is_reg_var_other := psavedstate(state)^.is_reg_var_other;
  1165. regvar_loaded_other := psavedstate(state)^.regvar_loaded_other;
  1166. regvar_loaded_int := psavedstate(state)^.regvar_loaded_int;
  1167. {$ifdef TEMPREGDEBUG}
  1168. reg_user := psavedstate(state)^.reg_user;
  1169. reg_releaser := psavedstate(state)^.reg_releaser;
  1170. {$endif TEMPREGDEBUG}
  1171. dispose(psavedstate(state));
  1172. state := nil;
  1173. end;
  1174. procedure trgobj.saveUnusedState(var state: pointer);
  1175. begin
  1176. new(punusedstate(state));
  1177. punusedstate(state)^.unusedregsint := unusedregsint;
  1178. punusedstate(state)^.unusedregsfpu := unusedregsfpu;
  1179. punusedstate(state)^.unusedregsmm := unusedregsmm;
  1180. {$ifndef newra}
  1181. punusedstate(state)^.countunusedregsint := countunusedregsint;
  1182. {$endif}
  1183. punusedstate(state)^.countunusedregsfpu := countunusedregsfpu;
  1184. punusedstate(state)^.countunusedregsmm := countunusedregsmm;
  1185. end;
  1186. procedure trgobj.restoreUnusedState(var state: pointer);
  1187. begin
  1188. unusedregsint := punusedstate(state)^.unusedregsint;
  1189. unusedregsfpu := punusedstate(state)^.unusedregsfpu;
  1190. unusedregsmm := punusedstate(state)^.unusedregsmm;
  1191. {$ifndef newra}
  1192. countunusedregsint := punusedstate(state)^.countunusedregsint;
  1193. {$endif}
  1194. countunusedregsfpu := punusedstate(state)^.countunusedregsfpu;
  1195. countunusedregsmm := punusedstate(state)^.countunusedregsmm;
  1196. dispose(punusedstate(state));
  1197. state := nil;
  1198. end;
  1199. {$ifdef newra}
  1200. procedure Trgobj.add_edge(u,v:Tsuperregister);
  1201. {This procedure will add an edge to the virtual interference graph.}
  1202. procedure addadj(u,v:Tsuperregister);
  1203. begin
  1204. if igraph.adjlist[u]=nil then
  1205. begin
  1206. getmem(igraph.adjlist[u],16);
  1207. igraph.adjlist[u]^:='';
  1208. end
  1209. else if (length(igraph.adjlist[u]^) and 15)=15 then
  1210. reallocmem(igraph.adjlist[u],length(igraph.adjlist[u]^)+16);
  1211. igraph.adjlist[u]^:=igraph.adjlist[u]^+char(v);
  1212. end;
  1213. begin
  1214. if (u<>v) and not(v in igraph.bitmap[u]) then
  1215. begin
  1216. include(igraph.bitmap[u],v);
  1217. include(igraph.bitmap[v],u);
  1218. {Precoloured nodes are not stored in the interference graph.}
  1219. if not(u in [first_supreg..last_supreg]) then
  1220. begin
  1221. addadj(u,v);
  1222. inc(degree[u]);
  1223. end;
  1224. if not(v in [first_supreg..last_supreg]) then
  1225. begin
  1226. addadj(v,u);
  1227. inc(degree[v]);
  1228. end;
  1229. end;
  1230. end;
  1231. procedure Trgobj.add_edges_used(u:Tsuperregister);
  1232. var i:Tsuperregister;
  1233. begin
  1234. for i:=1 to maxintreg do
  1235. if not(i in unusedregsint) then
  1236. add_edge(u,i);
  1237. end;
  1238. {$ifdef ra_debug}
  1239. procedure Trgobj.writegraph;
  1240. {This procedure writes out the current interference graph in the
  1241. register allocator.}
  1242. var f:text;
  1243. i,j:Tsuperregister;
  1244. begin
  1245. assign(f,'igraph'+char(48+random(10))+char(48+random(10)));
  1246. rewrite(f);
  1247. writeln(f,'Interference graph');
  1248. writeln(f);
  1249. write(f,' ');
  1250. for i:=0 to 15 do
  1251. for j:=0 to 15 do
  1252. write(f,hexstr(i,1));
  1253. writeln(f);
  1254. write(f,' ');
  1255. for i:=0 to 15 do
  1256. write(f,'0123456789ABCDEF');
  1257. writeln(f);
  1258. for i:=0 to 255 do
  1259. begin
  1260. write(f,hexstr(i,2):4);
  1261. for j:=0 to 255 do
  1262. if j in igraph.bitmap[i] then
  1263. write(f,'*')
  1264. else
  1265. write(f,'-');
  1266. writeln(f);
  1267. end;
  1268. close(f);
  1269. end;
  1270. {$endif}
  1271. procedure Trgobj.add_to_movelist(u:Tsuperregister;data:Tlinkedlistitem);
  1272. begin
  1273. if movelist[u]=nil then
  1274. begin
  1275. getmem(movelist[u],64);
  1276. movelist[u]^.count:=0;
  1277. end
  1278. else if (movelist[u]^.count and 15)=15 then
  1279. reallocmem(movelist[u],(movelist[u]^.count+1)*4+64);
  1280. movelist[u]^.data[movelist[u]^.count]:=data;
  1281. inc(movelist[u]^.count);
  1282. end;
  1283. procedure Trgobj.add_move_instruction(instr:Taicpu);
  1284. {This procedure notifies a certain as a move instruction so the
  1285. register allocator can try to eliminate it.}
  1286. var i:Tmoveins;
  1287. ssupreg,dsupreg:Tsuperregister;
  1288. begin
  1289. i:=Tmoveins.create;
  1290. i.moveset:=ms_worklist_moves;
  1291. i.instruction:=instr;
  1292. worklist_moves.insert(i);
  1293. ssupreg:=instr.oper[0].reg.number shr 8;
  1294. add_to_movelist(ssupreg,i);
  1295. dsupreg:=instr.oper[1].reg.number shr 8;
  1296. if ssupreg<>dsupreg then
  1297. {Avoid adding the same move instruction twice to a single register.}
  1298. add_to_movelist(dsupreg,i);
  1299. i.x:=ssupreg;
  1300. i.y:=dsupreg;
  1301. end;
  1302. function Trgobj.move_related(n:Tsuperregister):boolean;
  1303. var i:cardinal;
  1304. begin
  1305. move_related:=false;
  1306. if movelist[n]<>nil then
  1307. begin
  1308. for i:=0 to movelist[n]^.count-1 do
  1309. if Tmoveins(movelist[n]^.data[i]).moveset in
  1310. [ms_worklist_moves,ms_active_moves] then
  1311. begin
  1312. move_related:=true;
  1313. break;
  1314. end;
  1315. end;
  1316. end;
  1317. procedure Trgobj.make_work_list;
  1318. var n:Tsuperregister;
  1319. begin
  1320. {If we have 7 cpu registers, and the degree of a node is 7, we cannot
  1321. assign it to any of the registers, thus it is significant.}
  1322. for n:=first_imreg to maxintreg do
  1323. if degree[n]>=cpu_registers then
  1324. spillworklist:=spillworklist+char(n)
  1325. else if move_related(n) then
  1326. freezeworklist:=freezeworklist+char(n)
  1327. else
  1328. simplifyworklist:=simplifyworklist+char(n);
  1329. end;
  1330. procedure Trgobj.prepare_colouring;
  1331. begin
  1332. make_work_list;
  1333. active_moves:=Tlinkedlist.create;
  1334. frozen_moves:=Tlinkedlist.create;
  1335. coalesced_moves:=Tlinkedlist.create;
  1336. constrained_moves:=Tlinkedlist.create;
  1337. fillchar(alias,sizeof(alias),0);
  1338. coalescednodes:='';
  1339. selectstack:='';
  1340. end;
  1341. procedure Trgobj.enable_moves(n:Tsuperregister);
  1342. var m:Tlinkedlistitem;
  1343. i:cardinal;
  1344. begin
  1345. if movelist[n]<>nil then
  1346. for i:=0 to movelist[n]^.count-1 do
  1347. begin
  1348. m:=movelist[n]^.data[i];
  1349. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  1350. begin
  1351. if Tmoveins(m).moveset=ms_active_moves then
  1352. begin
  1353. {Move m from the set active_moves to the set worklist_moves.}
  1354. active_moves.remove(m);
  1355. Tmoveins(m).moveset:=ms_worklist_moves;
  1356. worklist_moves.concat(m);
  1357. end;
  1358. end;
  1359. end;
  1360. end;
  1361. procedure Trgobj.decrement_degree(m:Tsuperregister);
  1362. var adj:Pstring;
  1363. d:byte;
  1364. i,p:byte;
  1365. n:char;
  1366. begin
  1367. d:=degree[m];
  1368. if degree[m]>0 then
  1369. dec(degree[m]);
  1370. if d=cpu_registers then
  1371. begin
  1372. {Enable moves for m.}
  1373. enable_moves(m);
  1374. {Enable moves for adjacent.}
  1375. adj:=igraph.adjlist[m];
  1376. if adj<>nil then
  1377. for i:=1 to length(adj^) do
  1378. begin
  1379. n:=adj^[i];
  1380. if (pos(n,selectstack) or pos(n,coalescednodes))=0 then
  1381. enable_moves(Tsuperregister(n));
  1382. end;
  1383. {Remove the node from the spillworklist.}
  1384. p:=pos(char(m),spillworklist);
  1385. if p=0 then
  1386. internalerror(200305301); {must be found}
  1387. if length(spillworklist)>1 then
  1388. spillworklist[p]:=spillworklist[length(spillworklist)];
  1389. dec(spillworklist[0]);
  1390. if move_related(m) then
  1391. freezeworklist:=freezeworklist+char(m)
  1392. else
  1393. simplifyworklist:=simplifyworklist+char(m);
  1394. end;
  1395. end;
  1396. procedure Trgobj.simplify;
  1397. var adj:Pstring;
  1398. i,min,p:byte;
  1399. m:char;
  1400. n:Tsuperregister;
  1401. begin
  1402. {We the element with the least interferences out of the
  1403. simplifyworklist.}
  1404. min:=$ff;
  1405. p:=1;
  1406. for i:=1 to length(simplifyworklist) do
  1407. begin
  1408. adj:=igraph.adjlist[Tsuperregister(simplifyworklist[i])];
  1409. if adj=nil then
  1410. begin
  1411. min:=0;
  1412. break; {We won't find smaller ones.}
  1413. end
  1414. else
  1415. if length(adj^)<min then
  1416. begin
  1417. min:=length(adj^);
  1418. if min=0 then
  1419. break; {We won't find smaller ones.}
  1420. p:=i;
  1421. end;
  1422. end;
  1423. n:=Tsuperregister(simplifyworklist[p]);
  1424. delete(simplifyworklist,p,1);
  1425. {Push it on the selectstack.}
  1426. selectstack:=selectstack+char(n);
  1427. adj:=igraph.adjlist[n];
  1428. if adj<>nil then
  1429. for i:=1 to length(adj^) do
  1430. begin
  1431. m:=adj^[i];
  1432. if (pos(m,selectstack) or pos(m,coalescednodes))=0 then
  1433. decrement_degree(Tsuperregister(m));
  1434. end;
  1435. end;
  1436. function Trgobj.get_alias(n:Tsuperregister):Tsuperregister;
  1437. begin
  1438. while pos(char(n),coalescednodes)<>0 do
  1439. n:=alias[n];
  1440. get_alias:=n;
  1441. end;
  1442. procedure Trgobj.add_worklist(u:Tsuperregister);
  1443. begin
  1444. if not(u in [first_supreg..last_supreg]) and not move_related(u) and
  1445. (degree[u]<cpu_registers) then
  1446. begin
  1447. delete(freezeworklist,pos(char(u),freezeworklist),1);
  1448. simplifyworklist:=simplifyworklist+char(u);
  1449. end;
  1450. end;
  1451. function Trgobj.adjacent_ok(u,v:Tsuperregister):boolean;
  1452. {Check wether u and v should be coalesced. u is precoloured.}
  1453. function ok(t,r:Tsuperregister):boolean;
  1454. begin
  1455. ok:=(degree[t]<cpu_registers) or
  1456. (t in [first_supreg..last_supreg]) or
  1457. (r in igraph.bitmap[t]);
  1458. end;
  1459. var adj:Pstring;
  1460. i:byte;
  1461. t:char;
  1462. begin
  1463. adjacent_ok:=true;
  1464. adj:=igraph.adjlist[v];
  1465. if adj<>nil then
  1466. for i:=1 to length(adj^) do
  1467. begin
  1468. t:=adj^[i];
  1469. if (pos(t,selectstack) or pos(t,coalescednodes))=0 then
  1470. if not ok(Tsuperregister(t),u) then
  1471. begin
  1472. adjacent_ok:=false;
  1473. break;
  1474. end;
  1475. end;
  1476. end;
  1477. function Trgobj.conservative(u,v:Tsuperregister):boolean;
  1478. var adj:Pstring;
  1479. done:set of char; {To prevent that we count nodes twice.}
  1480. i,k:byte;
  1481. n:char;
  1482. begin
  1483. k:=0;
  1484. done:=[];
  1485. adj:=igraph.adjlist[u];
  1486. if adj<>nil then
  1487. for i:=1 to length(adj^) do
  1488. begin
  1489. n:=adj^[i];
  1490. if (pos(n,selectstack) or pos(n,coalescednodes))=0 then
  1491. begin
  1492. include(done,n);
  1493. if degree[Tsuperregister(n)]>=cpu_registers then
  1494. inc(k);
  1495. end;
  1496. end;
  1497. adj:=igraph.adjlist[v];
  1498. if adj<>nil then
  1499. for i:=1 to length(adj^) do
  1500. begin
  1501. n:=adj^[i];
  1502. if ((pos(n,selectstack) or pos(n,coalescednodes))=0) and
  1503. not (n in done) and
  1504. (degree[Tsuperregister(n)]>=cpu_registers) then
  1505. inc(k);
  1506. end;
  1507. conservative:=(k<cpu_registers);
  1508. end;
  1509. procedure Trgobj.combine(u,v:Tsuperregister);
  1510. var add:boolean;
  1511. adj:Pstring;
  1512. i,p:byte;
  1513. n,o:cardinal;
  1514. t:char;
  1515. begin
  1516. p:=pos(char(v),freezeworklist);
  1517. if p<>0 then
  1518. delete(freezeworklist,p,1)
  1519. else
  1520. delete(spillworklist,pos(char(v),spillworklist),1);
  1521. coalescednodes:=coalescednodes+char(v);
  1522. alias[v]:=u;
  1523. {Combine both movelists. Since the movelists are sets, only add
  1524. elements that are not already present.}
  1525. for n:=0 to movelist[v]^.count-1 do
  1526. begin
  1527. add:=true;
  1528. for o:=0 to movelist[u]^.count-1 do
  1529. if movelist[u]^.data[o]=movelist[v]^.data[n] then
  1530. begin
  1531. add:=false;
  1532. break;
  1533. end;
  1534. if add then
  1535. add_to_movelist(u,movelist[v]^.data[n]);
  1536. end;
  1537. enable_moves(v);
  1538. adj:=igraph.adjlist[v];
  1539. if adj<>nil then
  1540. for i:=1 to length(adj^) do
  1541. begin
  1542. t:=adj^[i];
  1543. if (pos(t,selectstack) or pos(t,coalescednodes))=0 then
  1544. begin
  1545. decrement_degree(Tsuperregister(t));
  1546. add_edge(Tsuperregister(t),u);
  1547. end;
  1548. end;
  1549. p:=pos(char(u),freezeworklist);
  1550. if (degree[u]>=cpu_registers) and (p<>0) then
  1551. begin
  1552. delete(freezeworklist,p,1);
  1553. spillworklist:=spillworklist+char(u);
  1554. end;
  1555. end;
  1556. procedure Trgobj.coalesce;
  1557. var m:Tmoveins;
  1558. x,y,u,v:Tsuperregister;
  1559. begin
  1560. m:=Tmoveins(worklist_moves.getfirst);
  1561. x:=get_alias(m.instruction.oper[0].reg.number shr 8);
  1562. y:=get_alias(m.instruction.oper[1].reg.number shr 8);
  1563. if y in [first_supreg..last_supreg] then
  1564. begin
  1565. u:=y;
  1566. v:=x;
  1567. end
  1568. else
  1569. begin
  1570. u:=x;
  1571. v:=y;
  1572. end;
  1573. if (u=v) then
  1574. begin
  1575. m.moveset:=ms_coalesced_moves; {Already coalesced.}
  1576. coalesced_moves.insert(m);
  1577. add_worklist(u);
  1578. end
  1579. {Do u and v interfere? In that case the move is constrained. Two
  1580. precoloured nodes interfere allways. If v is precoloured, by the above
  1581. code u is precoloured, thus interference...}
  1582. else if (v in [first_supreg..last_supreg]) or (u in igraph.bitmap[v]) then
  1583. begin
  1584. m.moveset:=ms_constrained_moves; {Cannot coalesce yet...}
  1585. constrained_moves.insert(m);
  1586. add_worklist(u);
  1587. add_worklist(v);
  1588. end
  1589. {Next test: is it possible and a good idea to coalesce??}
  1590. else if ((u in [first_supreg..last_supreg]) and adjacent_ok(u,v)) or
  1591. (not(u in [first_supreg..last_supreg]) and conservative(u,v)) then
  1592. begin
  1593. m.moveset:=ms_coalesced_moves; {Move coalesced!}
  1594. coalesced_moves.insert(m);
  1595. combine(u,v);
  1596. add_worklist(u);
  1597. end
  1598. else
  1599. begin
  1600. m.moveset:=ms_active_moves;
  1601. active_moves.insert(m);
  1602. end;
  1603. end;
  1604. procedure Trgobj.freeze_moves(u:Tsuperregister);
  1605. var i:cardinal;
  1606. m:Tlinkedlistitem;
  1607. v,x,y:Tsuperregister;
  1608. begin
  1609. if movelist[u]<>nil then
  1610. for i:=0 to movelist[u]^.count-1 do
  1611. begin
  1612. m:=movelist[u]^.data[i];
  1613. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  1614. begin
  1615. x:=Tmoveins(m).instruction.oper[0].reg.number shr 8;
  1616. y:=Tmoveins(m).instruction.oper[1].reg.number shr 8;
  1617. if get_alias(y)=get_alias(u) then
  1618. v:=get_alias(x)
  1619. else
  1620. v:=get_alias(y);
  1621. {Move m from active_moves/worklist_moves to frozen_moves.}
  1622. if Tmoveins(m).moveset=ms_active_moves then
  1623. active_moves.remove(m)
  1624. else
  1625. worklist_moves.remove(m);
  1626. Tmoveins(m).moveset:=ms_frozen_moves;
  1627. frozen_moves.insert(m);
  1628. if not(move_related(v)) and (degree[v]<cpu_registers) then
  1629. begin
  1630. delete(freezeworklist,pos(char(v),freezeworklist),1);
  1631. simplifyworklist:=simplifyworklist+char(v);
  1632. end;
  1633. end;
  1634. end;
  1635. end;
  1636. procedure Trgobj.freeze;
  1637. var n:Tsuperregister;
  1638. begin
  1639. {We need to take a random element out of the freezeworklist. We take
  1640. the last element. Dirty code!}
  1641. n:=Tsuperregister(freezeworklist[byte(freezeworklist[0])]);
  1642. dec(freezeworklist[0]);
  1643. {Add it to the simplifyworklist.}
  1644. simplifyworklist:=simplifyworklist+char(n);
  1645. freeze_moves(n);
  1646. end;
  1647. procedure Trgobj.select_spill;
  1648. var n:char;
  1649. begin
  1650. {This code is WAY too naive. We need not to select just a register, but
  1651. the register that is used the least...}
  1652. n:=spillworklist[byte(spillworklist[0])];
  1653. dec(spillworklist[0]);
  1654. simplifyworklist:=simplifyworklist+n;
  1655. freeze_moves(Tsuperregister(n));
  1656. end;
  1657. procedure Trgobj.assign_colours;
  1658. {Assign_colours assigns the actual colours to the registers.}
  1659. var adj:Pstring;
  1660. i,j,k:byte;
  1661. n,a:Tsuperregister;
  1662. adj_colours,colourednodes:set of Tsuperregister;
  1663. w:char;
  1664. begin
  1665. spillednodes:='';
  1666. {Colour the cpu registers...}
  1667. colourednodes:=[first_supreg..last_supreg];
  1668. for i:=first_supreg to last_supreg do
  1669. colour[i]:=i;
  1670. {Now colour the imaginary registers on the select-stack.}
  1671. for i:=length(selectstack) downto 1 do
  1672. begin
  1673. n:=Tsuperregister(selectstack[i]);
  1674. {Create a list of colours that we cannot assign to n.}
  1675. adj_colours:=[];
  1676. adj:=igraph.adjlist[n];
  1677. if adj<>nil then
  1678. for j:=1 to length(adj^) do
  1679. begin
  1680. w:=adj^[j];
  1681. a:=get_alias(Tsuperregister(w));
  1682. if a in colourednodes then
  1683. include(adj_colours,colour[a]);
  1684. end;
  1685. {Assume a spill by default...}
  1686. spillednodes:=spillednodes+char(n);
  1687. {Search for a colour not in this list.}
  1688. for k:=1 to cpu_registers do
  1689. if not(k in adj_colours) then
  1690. begin
  1691. colour[n]:=k;
  1692. dec(spillednodes[0]); {Colour found: no spill.}
  1693. include(colourednodes,n);
  1694. if n in used_in_proc_int then
  1695. include(used_in_proc_int,k);
  1696. break;
  1697. end;
  1698. end;
  1699. {Finally colour the nodes that were coalesced.}
  1700. for i:=1 to length(coalescednodes) do
  1701. begin
  1702. n:=Tsuperregister(coalescednodes[i]);
  1703. k:=get_alias(n);
  1704. colour[n]:=colour[k];
  1705. if n in used_in_proc_int then
  1706. include(used_in_proc_int,colour[k]);
  1707. end;
  1708. {$ifdef ra_debug}
  1709. for i:=first_imreg to maxintreg do
  1710. writeln(i:4,' ',colour[i]:4)
  1711. {$endif}
  1712. end;
  1713. procedure Trgobj.colour_registers;
  1714. begin
  1715. repeat
  1716. if length(simplifyworklist)<>0 then
  1717. simplify
  1718. else if not(worklist_moves.empty) then
  1719. coalesce
  1720. else if length(freezeworklist)<>0 then
  1721. freeze
  1722. else if length(spillworklist)<>0 then
  1723. select_spill;
  1724. until (length(simplifyworklist) or
  1725. byte(not(worklist_moves.empty)) or
  1726. length(freezeworklist) or
  1727. length(spillworklist)
  1728. )=0;
  1729. assign_colours;
  1730. end;
  1731. procedure Trgobj.epilogue_colouring;
  1732. {
  1733. procedure move_to_worklist_moves(list:Tlinkedlist);
  1734. var p:Tlinkedlistitem;
  1735. begin
  1736. p:=list.first;
  1737. while p<>nil do
  1738. begin
  1739. Tmoveins(p).moveset:=ms_worklist_moves;
  1740. p:=p.next;
  1741. end;
  1742. worklist_moves.concatlist(list);
  1743. end;
  1744. }
  1745. var i:Tsuperregister;
  1746. begin
  1747. worklist_moves.clear;
  1748. {$ifdef Principle_wrong_by_definition}
  1749. {Move everything back to worklist_moves.}
  1750. move_to_worklist_moves(active_moves);
  1751. move_to_worklist_moves(frozen_moves);
  1752. move_to_worklist_moves(coalesced_moves);
  1753. move_to_worklist_moves(constrained_moves);
  1754. {$endif}
  1755. active_moves.destroy;
  1756. active_moves:=nil;
  1757. frozen_moves.destroy;
  1758. frozen_moves:=nil;
  1759. coalesced_moves.destroy;
  1760. coalesced_moves:=nil;
  1761. constrained_moves.destroy;
  1762. constrained_moves:=nil;
  1763. for i:=0 to 255 do
  1764. if movelist[i]<>nil then
  1765. begin
  1766. dispose(movelist[i]);
  1767. movelist[i]:=0;
  1768. end;
  1769. end;
  1770. procedure Trgobj.clear_interferences(u:Tsuperregister);
  1771. {Remove node u from the interference graph and remove all collected
  1772. move instructions it is associated with.}
  1773. var i:byte;
  1774. j,k,count:cardinal;
  1775. v:Tsuperregister;
  1776. m,n:Tmoveins;
  1777. begin
  1778. if igraph.adjlist[u]<>nil then
  1779. begin
  1780. for i:=1 to length(igraph.adjlist[u]^) do
  1781. begin
  1782. v:=Tsuperregister(igraph.adjlist[u]^[i]);
  1783. {Remove (u,v) and (v,u) from bitmap.}
  1784. exclude(igraph.bitmap[u],v);
  1785. exclude(igraph.bitmap[v],u);
  1786. {Remove (v,u) from adjacency list.}
  1787. if igraph.adjlist[v]<>nil then
  1788. begin
  1789. delete(igraph.adjlist[v]^,pos(char(v),igraph.adjlist[v]^),1);
  1790. if length(igraph.adjlist[v]^)=0 then
  1791. begin
  1792. dispose(igraph.adjlist[v]);
  1793. igraph.adjlist[v]:=nil;
  1794. end;
  1795. end;
  1796. end;
  1797. {Remove ( u,* ) from adjacency list.}
  1798. dispose(igraph.adjlist[u]);
  1799. igraph.adjlist[u]:=nil;
  1800. end;
  1801. {$ifdef Principle_wrong_by_definition}
  1802. {Now remove the moves.}
  1803. if movelist[u]<>nil then
  1804. begin
  1805. for j:=0 to movelist[u]^.count-1 do
  1806. begin
  1807. m:=Tmoveins(movelist[u]^.data[j]);
  1808. {Get the other register of the move instruction.}
  1809. v:=m.instruction.oper[0].reg.number shr 8;
  1810. if v=u then
  1811. v:=m.instruction.oper[1].reg.number shr 8;
  1812. repeat
  1813. repeat
  1814. if (u<>v) and (movelist[v]<>nil) then
  1815. begin
  1816. {Remove the move from it's movelist.}
  1817. count:=movelist[v]^.count-1;
  1818. for k:=0 to count do
  1819. if m=movelist[v]^.data[k] then
  1820. begin
  1821. if k<>count then
  1822. movelist[v]^.data[k]:=movelist[v]^.data[count];
  1823. dec(movelist[v]^.count);
  1824. if count=0 then
  1825. begin
  1826. dispose(movelist[v]);
  1827. movelist[v]:=nil;
  1828. end;
  1829. break;
  1830. end;
  1831. end;
  1832. {The complexity is enourmous: the register might have been
  1833. coalesced. In that case it's movelists have been added to
  1834. it's coalescing alias. (DM)}
  1835. v:=alias[v];
  1836. until v=0;
  1837. {And also register u might have been coalesced.}
  1838. u:=alias[u];
  1839. until u=0;
  1840. case m.moveset of
  1841. ms_coalesced_moves:
  1842. coalesced_moves.remove(m);
  1843. ms_constrained_moves:
  1844. constrained_moves.remove(m);
  1845. ms_frozen_moves:
  1846. frozen_moves.remove(m);
  1847. ms_worklist_moves:
  1848. worklist_moves.remove(m);
  1849. ms_active_moves:
  1850. active_moves.remove(m);
  1851. end;
  1852. end;
  1853. dispose(movelist[u]);
  1854. movelist[u]:=nil;
  1855. end;
  1856. {$endif}
  1857. end;
  1858. procedure Trgobj.getregisterintinline(list:Taasmoutput;position:Tai;subreg:Tsubregister;var result:Tregister);
  1859. var i:Tsuperregister;
  1860. r:Tregister;
  1861. begin
  1862. if not (lastintreg in [first_imreg..last_imreg]) then
  1863. lastintreg:=first_imreg;
  1864. i:=lastintreg;
  1865. repeat
  1866. if i=last_imreg then
  1867. i:=first_imreg
  1868. else
  1869. inc(i);
  1870. if (i in unusedregsint) and (pos(char(i),abtlist)=0) then
  1871. begin
  1872. exclude(unusedregsint,i);
  1873. include(used_in_proc_int,i);
  1874. r.enum:=R_INTREGISTER;
  1875. r.number:=i shl 8 or subreg;
  1876. if position=nil then
  1877. list.insert(Tai_regalloc.alloc(r))
  1878. else
  1879. list.insertafter(Tai_regalloc.alloc(r),position);
  1880. result:=r;
  1881. lastintreg:=i;
  1882. if i>maxintreg then
  1883. maxintreg:=i;
  1884. add_edges_used(i);
  1885. add_constraints(result.number);
  1886. exit;
  1887. end;
  1888. until i=lastintreg;
  1889. internalerror(10);
  1890. end;
  1891. {In some cases we can get in big trouble. See this example:
  1892. ; register reg23d released
  1893. ; register eax allocated
  1894. ; register ebx allocated
  1895. ; register ecx allocated
  1896. ; register edx allocated
  1897. ; register esi allocated
  1898. ; register edi allocated
  1899. call [reg23d]
  1900. This code is ok, *except* when reg23d is spilled. In that case the
  1901. spilled would introduce a help register which can never get
  1902. allocated to a real register because it interferes with all of them.
  1903. To solve this we introduce the ABT ("avoid big trouble :)" ) registers.
  1904. If you allocate an ABT register you get a register that has less
  1905. than cpu_register interferences and will not be allocated ever again
  1906. by the normal register get procedures. In other words it is for sure it
  1907. will never get spilled.}
  1908. function Trgobj.getabtregisterint(list:Taasmoutput;size:Tcgsize):Tregister;
  1909. var i:Tsuperregister;
  1910. r:Tregister;
  1911. found:boolean;
  1912. begin
  1913. if not (lastintreg in [first_imreg..last_imreg]) then
  1914. lastintreg:=first_imreg;
  1915. found:=false;
  1916. for i:=1 to length(abtlist) do
  1917. if Tsuperregister(abtlist[i]) in unusedregsint then
  1918. begin
  1919. found:=true;
  1920. break;
  1921. end;
  1922. i:=lastintreg;
  1923. repeat
  1924. if i=last_imreg then
  1925. i:=first_imreg
  1926. else
  1927. inc(i);
  1928. if (i in unusedregsint) and ((igraph.adjlist[i]=nil) or (length(igraph.adjlist[i]^)<cpu_registers)) then
  1929. begin
  1930. found:=true;
  1931. break;
  1932. end;
  1933. until i=lastintreg;
  1934. if found then
  1935. begin
  1936. exclude(unusedregsint,i);
  1937. include(used_in_proc_int,i);
  1938. r.enum:=R_INTREGISTER;
  1939. r.number:=i shl 8 or cgsize2subreg(size);
  1940. list.concat(Tai_regalloc.alloc(r));
  1941. getabtregisterint:=r;
  1942. lastintreg:=i;
  1943. if i>maxintreg then
  1944. maxintreg:=i;
  1945. add_edges_used(i);
  1946. if pos(char(i),abtlist)=0 then
  1947. abtlist:=abtlist+char(i);
  1948. end
  1949. else
  1950. internalerror(10);
  1951. {$ifdef newra}
  1952. add_constraints(getabtregisterint.number);
  1953. {$endif}
  1954. end;
  1955. procedure Trgobj.ungetregisterintinline(list:Taasmoutput;position:Tai;const r:Tregister);
  1956. var supreg:Tsuperregister;
  1957. begin
  1958. if r.enum<=lastreg then
  1959. internalerror(2003010803);
  1960. supreg:=r.number shr 8;
  1961. { takes much time }
  1962. include(unusedregsint,supreg);
  1963. if position=nil then
  1964. list.insert(Tai_regalloc.dealloc(r))
  1965. else
  1966. list.insertafter(Tai_regalloc.dealloc(r),position);
  1967. add_edges_used(supreg);
  1968. end;
  1969. function Trgobj.spill_registers(list:Taasmoutput;const regs_to_spill:string):boolean;
  1970. {Returns true if any help registers have been used.}
  1971. var i:byte;
  1972. r:Tsuperregister;
  1973. p,q:Tai;
  1974. regs_to_spill_set:Tsupregset;
  1975. spill_temps:^Tspill_temp_list;
  1976. begin
  1977. spill_registers:=false;
  1978. unusedregsint:=[0..255];
  1979. fillchar(degree,sizeof(degree),0);
  1980. if current_procinfo.framepointer.number=NR_FRAME_POINTER_REG then
  1981. {Make sure the register allocator won't allocate registers into ebp.}
  1982. exclude(rg.unusedregsint,RS_FRAME_POINTER_REG);
  1983. new(spill_temps);
  1984. fillchar(spill_temps^,sizeof(spill_temps^),0);
  1985. regs_to_spill_set:=[];
  1986. for i:=1 to length(regs_to_spill) do
  1987. begin
  1988. {Alternative representation.}
  1989. include(regs_to_spill_set,Tsuperregister(regs_to_spill[i]));
  1990. {Clear all interferences of the spilled register.}
  1991. clear_interferences(Tsuperregister(regs_to_spill[i]));
  1992. {Get a temp for the spilled register.}
  1993. tg.gettemp(list,4,tt_noreuse,spill_temps^[Tsuperregister(regs_to_spill[i])]);
  1994. end;
  1995. p:=Tai(list.first);
  1996. while assigned(p) do
  1997. begin
  1998. case p.typ of
  1999. ait_regalloc:
  2000. begin
  2001. {A register allocation of a spilled register can be removed.}
  2002. if (Tai_regalloc(p).reg.number shr 8) in regs_to_spill_set then
  2003. begin
  2004. q:=p;
  2005. p:=Tai(p.next);
  2006. list.remove(q);
  2007. continue;
  2008. end
  2009. else
  2010. if Tai_regalloc(p).allocation then
  2011. exclude(unusedregsint,Tai_regalloc(p).reg.number shr 8)
  2012. else
  2013. include(unusedregsint,Tai_regalloc(p).reg.number shr 8);
  2014. end;
  2015. ait_instruction:
  2016. begin
  2017. if Taicpu_abstract(p).spill_registers(list,@getregisterintinline,
  2018. @ungetregisterintinline,
  2019. regs_to_spill_set,
  2020. unusedregsint,
  2021. spill_temps^) then
  2022. spill_registers:=true;
  2023. if Taicpu_abstract(p).is_move then
  2024. add_move_instruction(Taicpu(p));
  2025. end;
  2026. end;
  2027. p:=Tai(p.next);
  2028. end;
  2029. for i:=1 to length(regs_to_spill) do
  2030. begin
  2031. tg.ungettemp(list,spill_temps^[Tsuperregister(regs_to_spill[i])]);
  2032. end;
  2033. dispose(spill_temps);
  2034. end;
  2035. {$endif newra}
  2036. {****************************************************************************
  2037. TReference
  2038. ****************************************************************************}
  2039. procedure reference_reset(var ref : treference);
  2040. begin
  2041. FillChar(ref,sizeof(treference),0);
  2042. ref.base.enum:=R_INTREGISTER;
  2043. ref.index.enum:=R_INTREGISTER;
  2044. {$ifdef i386}
  2045. ref.segment.enum:=R_INTREGISTER;
  2046. {$endif}
  2047. end;
  2048. procedure reference_reset_old(var ref : treference);
  2049. begin
  2050. FillChar(ref,sizeof(treference),0);
  2051. end;
  2052. procedure reference_reset_base(var ref : treference;base : tregister;offset : longint);
  2053. begin
  2054. reference_reset(ref);
  2055. ref.base:=base;
  2056. ref.offset:=offset;
  2057. end;
  2058. procedure reference_reset_symbol(var ref : treference;sym : tasmsymbol;offset : longint);
  2059. begin
  2060. reference_reset(ref);
  2061. ref.symbol:=sym;
  2062. ref.offset:=offset;
  2063. end;
  2064. procedure reference_release(list: taasmoutput; const ref : treference);
  2065. begin
  2066. rg.ungetreference(list,ref);
  2067. end;
  2068. function references_equal(sref : treference;dref : treference):boolean;
  2069. begin
  2070. references_equal:=CompareByte(sref,dref,sizeof(treference))=0;
  2071. end;
  2072. { on most processors , this routine does nothing, overriden currently }
  2073. { only by 80x86 processor. }
  2074. function trgobj.makeregsize(reg: tregister; size: tcgsize): tregister;
  2075. begin
  2076. makeregsize := reg;
  2077. end;
  2078. {****************************************************************************
  2079. TLocation
  2080. ****************************************************************************}
  2081. procedure location_reset(var l : tlocation;lt:TCGLoc;lsize:TCGSize);
  2082. begin
  2083. FillChar(l,sizeof(tlocation),0);
  2084. l.loc:=lt;
  2085. l.size:=lsize;
  2086. case l.loc of
  2087. LOC_REGISTER,LOC_CREGISTER:
  2088. begin
  2089. l.register.enum:=R_INTREGISTER;
  2090. l.registerhigh.enum:=R_INTREGISTER;
  2091. end;
  2092. LOC_REFERENCE,LOC_CREFERENCE:
  2093. begin
  2094. l.reference.base.enum:=R_INTREGISTER;
  2095. l.reference.index.enum:=R_INTREGISTER;
  2096. {$ifdef i386}
  2097. l.reference.segment.enum:=R_INTREGISTER;
  2098. {$endif}
  2099. end;
  2100. end;
  2101. end;
  2102. procedure location_release(list: taasmoutput; const l : tlocation);
  2103. begin
  2104. case l.loc of
  2105. LOC_REGISTER,LOC_CREGISTER :
  2106. begin
  2107. rg.ungetregisterint(list,l.register);
  2108. if l.size in [OS_64,OS_S64] then
  2109. rg.ungetregisterint(list,l.registerhigh);
  2110. end;
  2111. LOC_FPUREGISTER,LOC_CFPUREGISTER :
  2112. rg.ungetregisterfpu(list,l.register,l.size);
  2113. LOC_CREFERENCE,LOC_REFERENCE :
  2114. rg.ungetreference(list, l.reference);
  2115. end;
  2116. end;
  2117. procedure location_freetemp(list:taasmoutput; const l : tlocation);
  2118. begin
  2119. if (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  2120. tg.ungetiftemp(list,l.reference);
  2121. end;
  2122. procedure location_copy(var destloc:tlocation; const sourceloc : tlocation);
  2123. begin
  2124. destloc:=sourceloc;
  2125. end;
  2126. procedure location_swap(var destloc,sourceloc : tlocation);
  2127. var
  2128. swapl : tlocation;
  2129. begin
  2130. swapl := destloc;
  2131. destloc := sourceloc;
  2132. sourceloc := swapl;
  2133. end;
  2134. initialization
  2135. ;
  2136. finalization
  2137. rg.free;
  2138. end.
  2139. {
  2140. $Log$
  2141. Revision 1.54 2003-06-13 21:19:31 peter
  2142. * current_procdef removed, use current_procinfo.procdef instead
  2143. Revision 1.53 2003/06/12 21:11:10 peter
  2144. * ungetregisterfpu gets size parameter
  2145. Revision 1.52 2003/06/12 16:43:07 peter
  2146. * newra compiles for sparc
  2147. Revision 1.51 2003/06/09 14:54:26 jonas
  2148. * (de)allocation of registers for parameters is now performed properly
  2149. (and checked on the ppc)
  2150. - removed obsolete allocation of all parameter registers at the start
  2151. of a procedure (and deallocation at the end)
  2152. Revision 1.50 2003/06/03 21:11:09 peter
  2153. * cg.a_load_* get a from and to size specifier
  2154. * makeregsize only accepts newregister
  2155. * i386 uses generic tcgnotnode,tcgunaryminus
  2156. Revision 1.49 2003/06/03 13:01:59 daniel
  2157. * Register allocator finished
  2158. Revision 1.48 2003/06/01 21:38:06 peter
  2159. * getregisterfpu size parameter added
  2160. * op_const_reg size parameter added
  2161. * sparc updates
  2162. Revision 1.47 2003/05/31 20:31:11 jonas
  2163. * set inital costs of assigning a variable to a register to 120 for
  2164. non-i386, because the used register must be store to memory at the
  2165. start and loaded again at the end
  2166. Revision 1.46 2003/05/30 18:55:21 jonas
  2167. * fixed several regvar related bugs for non-i386. make cycle with -Or now
  2168. works for ppc
  2169. Revision 1.45 2003/05/30 12:36:13 jonas
  2170. * use as little different registers on the ppc until newra is released,
  2171. since every used register must be saved
  2172. Revision 1.44 2003/05/17 13:30:08 jonas
  2173. * changed tt_persistant to tt_persistent :)
  2174. * tempcreatenode now doesn't accept a boolean anymore for persistent
  2175. temps, but a ttemptype, so you can also create ansistring temps etc
  2176. Revision 1.43 2003/05/16 14:33:31 peter
  2177. * regvar fixes
  2178. Revision 1.42 2003/04/26 20:03:49 daniel
  2179. * Bug fix in simplify
  2180. Revision 1.41 2003/04/25 20:59:35 peter
  2181. * removed funcretn,funcretsym, function result is now in varsym
  2182. and aliases for result and function name are added using absolutesym
  2183. * vs_hidden parameter for funcret passed in parameter
  2184. * vs_hidden fixes
  2185. * writenode changed to printnode and released from extdebug
  2186. * -vp option added to generate a tree.log with the nodetree
  2187. * nicer printnode for statements, callnode
  2188. Revision 1.40 2003/04/25 08:25:26 daniel
  2189. * Ifdefs around a lot of calls to cleartempgen
  2190. * Fixed registers that are allocated but not freed in several nodes
  2191. * Tweak to register allocator to cause less spills
  2192. * 8-bit registers now interfere with esi,edi and ebp
  2193. Compiler can now compile rtl successfully when using new register
  2194. allocator
  2195. Revision 1.39 2003/04/23 20:23:06 peter
  2196. * compile fix for no-newra
  2197. Revision 1.38 2003/04/23 14:42:07 daniel
  2198. * Further register allocator work. Compiler now smaller with new
  2199. allocator than without.
  2200. * Somebody forgot to adjust ppu version number
  2201. Revision 1.37 2003/04/22 23:50:23 peter
  2202. * firstpass uses expectloc
  2203. * checks if there are differences between the expectloc and
  2204. location.loc from secondpass in EXTDEBUG
  2205. Revision 1.36 2003/04/22 10:09:35 daniel
  2206. + Implemented the actual register allocator
  2207. + Scratch registers unavailable when new register allocator used
  2208. + maybe_save/maybe_restore unavailable when new register allocator used
  2209. Revision 1.35 2003/04/21 19:16:49 peter
  2210. * count address regs separate
  2211. Revision 1.34 2003/04/17 16:48:21 daniel
  2212. * Added some code to keep track of move instructions in register
  2213. allocator
  2214. Revision 1.33 2003/04/17 07:50:24 daniel
  2215. * Some work on interference graph construction
  2216. Revision 1.32 2003/03/28 19:16:57 peter
  2217. * generic constructor working for i386
  2218. * remove fixed self register
  2219. * esi added as address register for i386
  2220. Revision 1.31 2003/03/11 21:46:24 jonas
  2221. * lots of new regallocator fixes, both in generic and ppc-specific code
  2222. (ppc compiler still can't compile the linux system unit though)
  2223. Revision 1.30 2003/03/09 21:18:59 olle
  2224. + added cutils to the uses clause
  2225. Revision 1.29 2003/03/08 20:36:41 daniel
  2226. + Added newra version of Ti386shlshrnode
  2227. + Added interference graph construction code
  2228. Revision 1.28 2003/03/08 13:59:16 daniel
  2229. * Work to handle new register notation in ag386nsm
  2230. + Added newra version of Ti386moddivnode
  2231. Revision 1.27 2003/03/08 10:53:48 daniel
  2232. * Created newra version of secondmul in n386add.pas
  2233. Revision 1.26 2003/03/08 08:59:07 daniel
  2234. + $define newra will enable new register allocator
  2235. + getregisterint will return imaginary registers with $newra
  2236. + -sr switch added, will skip register allocation so you can see
  2237. the direct output of the code generator before register allocation
  2238. Revision 1.25 2003/02/26 20:50:45 daniel
  2239. * Fixed ungetreference
  2240. Revision 1.24 2003/02/19 22:39:56 daniel
  2241. * Fixed a few issues
  2242. Revision 1.23 2003/02/19 22:00:14 daniel
  2243. * Code generator converted to new register notation
  2244. - Horribily outdated todo.txt removed
  2245. Revision 1.22 2003/02/02 19:25:54 carl
  2246. * Several bugfixes for m68k target (register alloc., opcode emission)
  2247. + VIS target
  2248. + Generic add more complete (still not verified)
  2249. Revision 1.21 2003/01/08 18:43:57 daniel
  2250. * Tregister changed into a record
  2251. Revision 1.20 2002/10/05 12:43:28 carl
  2252. * fixes for Delphi 6 compilation
  2253. (warning : Some features do not work under Delphi)
  2254. Revision 1.19 2002/08/23 16:14:49 peter
  2255. * tempgen cleanup
  2256. * tt_noreuse temp type added that will be used in genentrycode
  2257. Revision 1.18 2002/08/17 22:09:47 florian
  2258. * result type handling in tcgcal.pass_2 overhauled
  2259. * better tnode.dowrite
  2260. * some ppc stuff fixed
  2261. Revision 1.17 2002/08/17 09:23:42 florian
  2262. * first part of procinfo rewrite
  2263. Revision 1.16 2002/08/06 20:55:23 florian
  2264. * first part of ppc calling conventions fix
  2265. Revision 1.15 2002/08/05 18:27:48 carl
  2266. + more more more documentation
  2267. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  2268. Revision 1.14 2002/08/04 19:06:41 carl
  2269. + added generic exception support (still does not work!)
  2270. + more documentation
  2271. Revision 1.13 2002/07/07 09:52:32 florian
  2272. * powerpc target fixed, very simple units can be compiled
  2273. * some basic stuff for better callparanode handling, far from being finished
  2274. Revision 1.12 2002/07/01 18:46:26 peter
  2275. * internal linker
  2276. * reorganized aasm layer
  2277. Revision 1.11 2002/05/18 13:34:17 peter
  2278. * readded missing revisions
  2279. Revision 1.10 2002/05/16 19:46:44 carl
  2280. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2281. + try to fix temp allocation (still in ifdef)
  2282. + generic constructor calls
  2283. + start of tassembler / tmodulebase class cleanup
  2284. Revision 1.8 2002/04/21 15:23:03 carl
  2285. + makeregsize
  2286. + changeregsize is now a local routine
  2287. Revision 1.7 2002/04/20 21:32:25 carl
  2288. + generic FPC_CHECKPOINTER
  2289. + first parameter offset in stack now portable
  2290. * rename some constants
  2291. + move some cpu stuff to other units
  2292. - remove unused constents
  2293. * fix stacksize for some targets
  2294. * fix generic size problems which depend now on EXTEND_SIZE constant
  2295. Revision 1.6 2002/04/15 19:03:31 carl
  2296. + reg2str -> std_reg2str()
  2297. Revision 1.5 2002/04/06 18:13:01 jonas
  2298. * several powerpc-related additions and fixes
  2299. Revision 1.4 2002/04/04 19:06:04 peter
  2300. * removed unused units
  2301. * use tlocation.size in cg.a_*loc*() routines
  2302. Revision 1.3 2002/04/02 17:11:29 peter
  2303. * tlocation,treference update
  2304. * LOC_CONSTANT added for better constant handling
  2305. * secondadd splitted in multiple routines
  2306. * location_force_reg added for loading a location to a register
  2307. of a specified size
  2308. * secondassignment parses now first the right and then the left node
  2309. (this is compatible with Kylix). This saves a lot of push/pop especially
  2310. with string operations
  2311. * adapted some routines to use the new cg methods
  2312. Revision 1.2 2002/04/01 19:24:25 jonas
  2313. * fixed different parameter name in interface and implementation
  2314. declaration of a method (only 1.0.x detected this)
  2315. Revision 1.1 2002/03/31 20:26:36 jonas
  2316. + a_loadfpu_* and a_loadmm_* methods in tcg
  2317. * register allocation is now handled by a class and is mostly processor
  2318. independent (+rgobj.pas and i386/rgcpu.pas)
  2319. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  2320. * some small improvements and fixes to the optimizer
  2321. * some register allocation fixes
  2322. * some fpuvaroffset fixes in the unary minus node
  2323. * push/popusedregisters is now called rg.save/restoreusedregisters and
  2324. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  2325. also better optimizable)
  2326. * fixed and optimized register saving/restoring for new/dispose nodes
  2327. * LOC_FPU locations now also require their "register" field to be set to
  2328. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  2329. - list field removed of the tnode class because it's not used currently
  2330. and can cause hard-to-find bugs
  2331. }