rgobj.pas 92 KB

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