rgobj.pas 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816
  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_debug2}
  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,{$ifdef ra_debug2}fmodule,{$endif}
  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 newra}
  667. add_edges_used(r shr 8);
  668. {$endif}
  669. {$ifdef TEMPREGDEBUG}
  670. testregisters32;
  671. {$endif TEMPREGDEBUG}
  672. end
  673. else
  674. internalerror(200301103);
  675. getexplicitregisterint:=r2;
  676. end;
  677. {$ifdef newra}
  678. procedure Trgobj.allocexplicitregistersint(list:Taasmoutput;r:Tsupregset);
  679. var reg:Tregister;
  680. i:Tsuperregister;
  681. begin
  682. if unusedregsint*r=r then
  683. begin
  684. unusedregsint:=unusedregsint-r;
  685. used_in_proc_int:=used_in_proc_int+r;
  686. for i:=first_supreg to last_supreg do
  687. if i in r then
  688. begin
  689. add_edges_used(i);
  690. reg.enum:=R_INTREGISTER;
  691. reg.number:=i shl 8 or R_SUBWHOLE;
  692. list.concat(Tai_regalloc.alloc(reg));
  693. end;
  694. end
  695. else
  696. internalerror(200305061);
  697. end;
  698. procedure Trgobj.deallocexplicitregistersint(list:Taasmoutput;r:Tsupregset);
  699. var reg:Tregister;
  700. i:Tsuperregister;
  701. begin
  702. if unusedregsint*r=[] then
  703. begin
  704. unusedregsint:=unusedregsint+r;
  705. for i:=last_supreg downto first_supreg do
  706. if i in r then
  707. begin
  708. reg.enum:=R_INTREGISTER;
  709. reg.number:=i shl 8 or R_SUBWHOLE;
  710. list.concat(Tai_regalloc.dealloc(reg));
  711. end;
  712. end
  713. else
  714. internalerror(200305062);
  715. end;
  716. {$endif}
  717. { tries to allocate the passed register, if possible }
  718. function trgobj.getexplicitregisterfpu(list : taasmoutput; r : Toldregister) : tregister;
  719. var r2:Tregister;
  720. begin
  721. if r in unusedregsfpu then
  722. begin
  723. dec(countunusedregsfpu);
  724. {$ifdef TEMPREGDEBUG}
  725. if curptree^^.usableregs-countunusedregsint>curptree^^.registers32 then
  726. internalerror(10);
  727. reg_user[r]:=curptree^;
  728. {$endif TEMPREGDEBUG}
  729. exclude(unusedregsfpu,r);
  730. include(used_in_proc_other,r);
  731. r2.enum:=r;
  732. list.concat(tai_regalloc.alloc(r2));
  733. getexplicitregisterfpu:=r2;
  734. {$ifdef TEMPREGDEBUG}
  735. testregisters32;
  736. {$endif TEMPREGDEBUG}
  737. end
  738. else
  739. {$warning Size for FPU reg is maybe not correct}
  740. getexplicitregisterfpu:=getregisterfpu(list,OS_F32);
  741. end;
  742. function trgobj.getregisterfpu(list: taasmoutput;size:Tcgsize) : tregister;
  743. begin
  744. if countunusedregsfpu=0 then
  745. internalerror(10);
  746. result := getregistergenother(list,firstsavefpureg,lastsavefpureg,
  747. unusedregsfpu,countunusedregsfpu);
  748. end;
  749. procedure trgobj.ungetregisterfpu(list : taasmoutput; r : tregister;size:TCGsize);
  750. begin
  751. ungetregistergen(list,r,usableregsfpu,unusedregsfpu,
  752. countunusedregsfpu);
  753. end;
  754. function trgobj.getregistermm(list: taasmoutput) : tregister;
  755. begin
  756. if countunusedregsmm=0 then
  757. internalerror(10);
  758. result := getregistergenother(list,firstsavemmreg,lastsavemmreg,
  759. unusedregsmm,countunusedregsmm);
  760. end;
  761. procedure trgobj.ungetregistermm(list: taasmoutput; r: tregister);
  762. begin
  763. ungetregistergen(list,r,usableregsmm,unusedregsmm,
  764. countunusedregsmm);
  765. end;
  766. function trgobj.getaddressregister(list:Taasmoutput): tregister;
  767. begin
  768. {An address register is OS_INT per definition.}
  769. result := getregisterint(list,OS_INT);
  770. end;
  771. procedure trgobj.ungetaddressregister(list: taasmoutput; r: tregister);
  772. begin
  773. ungetregisterint(list,r);
  774. end;
  775. function trgobj.isaddressregister(reg: tregister): boolean;
  776. begin
  777. if reg.number<>0 then; { remove warning }
  778. result := true;
  779. end;
  780. procedure trgobj.ungetregister(list: taasmoutput; r : tregister);
  781. begin
  782. if r.enum=R_NO then
  783. exit;
  784. if r.enum>lastreg then
  785. internalerror(200301081);
  786. if r.enum in fpuregs then
  787. ungetregisterfpu(list,r,OS_NO)
  788. else if r.enum in mmregs then
  789. ungetregistermm(list,r)
  790. else if r.enum in addrregs then
  791. ungetaddressregister(list,r)
  792. else internalerror(2002070602);
  793. end;
  794. procedure Trgobj.cleartempgen;
  795. {$ifdef newra}
  796. var i:Tsuperregister;
  797. {$endif newra}
  798. begin
  799. {$ifndef newra}
  800. countunusedregsint:=countusableregsint;
  801. {$endif}
  802. countunusedregsfpu:=countusableregsfpu;
  803. countunusedregsmm:=countusableregsmm;
  804. lastintreg:=0;
  805. maxintreg:=first_imreg;
  806. {$ifdef newra}
  807. unusedregsint:=[0..255];
  808. {$else}
  809. unusedregsint:=usableregsint;
  810. {$endif}
  811. unusedregsfpu:=usableregsfpu;
  812. unusedregsmm:=usableregsmm;
  813. {$ifdef newra}
  814. {$ifdef powerpc}
  815. savedintbyproc:=[RS_R13..RS_R31];
  816. {$else powerpc}
  817. savedintbyproc:=[];
  818. {$endif powerpc}
  819. for i:=low(Tsuperregister) to high(Tsuperregister) do
  820. begin
  821. if igraph.adjlist[i]<>nil then
  822. dispose(igraph.adjlist[i]);
  823. if movelist[i]<>nil then
  824. dispose(movelist[i]);
  825. end;
  826. fillchar(movelist,sizeof(movelist),0);
  827. fillchar(igraph,sizeof(igraph),0);
  828. fillchar(degree,sizeof(degree),0);
  829. worklist_moves.clear;
  830. abtlist:='';
  831. {$endif}
  832. end;
  833. procedure trgobj.ungetreference(list : taasmoutput; const ref : treference);
  834. begin
  835. if (ref.base.number<>NR_NO) and (ref.base.number<>NR_FRAME_POINTER_REG) then
  836. ungetregisterint(list,ref.base);
  837. if (ref.index.number<>NR_NO) and (ref.index.number<>NR_FRAME_POINTER_REG) then
  838. ungetregisterint(list,ref.index);
  839. end;
  840. {$ifndef newra}
  841. procedure trgobj.saveintregvars(list:Taasmoutput;const s:Tsupregset);
  842. var r:Tsuperregister;
  843. hr: tregister;
  844. begin
  845. if not(cs_regalloc in aktglobalswitches) then
  846. exit;
  847. for r:=firstsaveintreg to lastsaveintreg do
  848. if (r in is_reg_var_int) and
  849. (r in s) then
  850. begin
  851. hr.number:=r shl 8;
  852. hr.enum:=R_INTREGISTER;
  853. store_regvar(list,hr);
  854. end;
  855. end;
  856. {$endif}
  857. procedure trgobj.saveotherregvars(list: taasmoutput; const s: tregisterset);
  858. var
  859. r: Tregister;
  860. begin
  861. if not(cs_regalloc in aktglobalswitches) then
  862. exit;
  863. if firstsavefpureg <> R_NO then
  864. for r.enum := firstsavefpureg to lastsavefpureg do
  865. if is_reg_var_other[r.enum] and
  866. (r.enum in s) then
  867. store_regvar(list,r);
  868. if firstsavemmreg <> R_NO then
  869. for r.enum := firstsavemmreg to lastsavemmreg do
  870. if is_reg_var_other[r.enum] and
  871. (r.enum in s) then
  872. store_regvar(list,r);
  873. end;
  874. {$ifndef newra}
  875. procedure trgobj.saveusedintregisters(list:Taasmoutput;
  876. var saved:Tpushedsavedint;
  877. const s:Tsupregset);
  878. var r:Tsuperregister;
  879. r2:Tregister;
  880. hr : treference;
  881. begin
  882. used_in_proc_int:=used_in_proc_int+s;
  883. for r:=firstsaveintreg to lastsaveintreg do
  884. begin
  885. saved[r].ofs:=reg_not_saved;
  886. { if the register is used by the calling subroutine and if }
  887. { it's not a regvar (those are handled separately) }
  888. if not (r in is_reg_var_int) and
  889. (r in s) and
  890. { and is present in use }
  891. not(r in unusedregsint) then
  892. begin
  893. { then save it }
  894. tg.GetTemp(list,sizeof(aword),tt_persistent,hr);
  895. saved[r].ofs:=hr.offset;
  896. r2.enum:=R_INTREGISTER;
  897. r2.number:=r shl 8 or R_SUBWHOLE;
  898. cg.a_load_reg_ref(list,OS_INT,OS_INT,r2,hr);
  899. cg.a_reg_dealloc(list,r2);
  900. include(unusedregsint,r);
  901. inc(countunusedregsint);
  902. end;
  903. end;
  904. {$ifdef TEMPREGDEBUG}
  905. testregisters32;
  906. {$endif TEMPREGDEBUG}
  907. end;
  908. {$endif}
  909. procedure trgobj.saveusedotherregisters(list: taasmoutput;
  910. var saved : tpushedsavedother; const s: tregisterset);
  911. var
  912. r : tregister;
  913. hr : treference;
  914. begin
  915. used_in_proc_other:=used_in_proc_other + s;
  916. { don't try to save the fpu registers if not desired (e.g. for }
  917. { the 80x86) }
  918. if firstsavefpureg <> R_NO then
  919. for r.enum:=firstsavefpureg to lastsavefpureg do
  920. begin
  921. saved[r.enum].ofs:=reg_not_saved;
  922. { if the register is used by the calling subroutine and if }
  923. { it's not a regvar (those are handled separately) }
  924. if not is_reg_var_other[r.enum] and
  925. (r.enum in s) and
  926. { and is present in use }
  927. not(r.enum in unusedregsfpu) then
  928. begin
  929. { then save it }
  930. tg.GetTemp(list,extended_size,tt_persistent,hr);
  931. saved[r.enum].ofs:=hr.offset;
  932. cg.a_loadfpu_reg_ref(list,OS_FLOAT,r,hr);
  933. cg.a_reg_dealloc(list,r);
  934. include(unusedregsfpu,r.enum);
  935. inc(countunusedregsfpu);
  936. end;
  937. end;
  938. { don't save the vector registers if there's no support for them }
  939. if firstsavemmreg <> R_NO then
  940. for r.enum:=firstsavemmreg to lastsavemmreg do
  941. begin
  942. saved[r.enum].ofs:=reg_not_saved;
  943. { if the register is in use and if it's not a regvar (those }
  944. { are handled separately), save it }
  945. if not is_reg_var_other[r.enum] and
  946. (r.enum in s) and
  947. { and is present in use }
  948. not(r.enum in unusedregsmm) then
  949. begin
  950. { then save it }
  951. tg.GetTemp(list,mmreg_size,tt_persistent,hr);
  952. saved[r.enum].ofs:=hr.offset;
  953. cg.a_loadmm_reg_ref(list,r,hr);
  954. cg.a_reg_dealloc(list,r);
  955. include(unusedregsmm,r.enum);
  956. inc(countunusedregsmm);
  957. end;
  958. end;
  959. {$ifdef TEMPREGDEBUG}
  960. testregisters32;
  961. {$endif TEMPREGDEBUG}
  962. end;
  963. {$ifndef newra}
  964. procedure trgobj.restoreusedintregisters(list:Taasmoutput;
  965. const saved:Tpushedsavedint);
  966. var r:Tsuperregister;
  967. r2:Tregister;
  968. hr:Treference;
  969. begin
  970. for r:=lastsaveintreg downto firstsaveintreg do
  971. begin
  972. if saved[r].ofs <> reg_not_saved then
  973. begin
  974. r2.enum:=R_INTREGISTER;
  975. r2.number:=NR_FRAME_POINTER_REG;
  976. reference_reset_base(hr,r2,saved[r].ofs);
  977. r2.enum:=R_INTREGISTER;
  978. r2.number:=r shl 8 or R_SUBWHOLE;
  979. cg.a_reg_alloc(list,r2);
  980. cg.a_load_ref_reg(list,OS_INT,OS_INT,hr,r2);
  981. if not (r in unusedregsint) then
  982. { internalerror(10)
  983. in n386cal we always save/restore the reg *state*
  984. using save/restoreunusedstate -> the current state
  985. may not be real (JM) }
  986. else
  987. begin
  988. dec(countunusedregsint);
  989. exclude(unusedregsint,r);
  990. end;
  991. tg.UnGetTemp(list,hr);
  992. end;
  993. end;
  994. {$ifdef TEMPREGDEBUG}
  995. testregisters32;
  996. {$endif TEMPREGDEBUG}
  997. end;
  998. {$endif}
  999. procedure trgobj.restoreusedotherregisters(list : taasmoutput;
  1000. const saved : tpushedsavedother);
  1001. var
  1002. r,r2 : tregister;
  1003. hr : treference;
  1004. begin
  1005. if firstsavemmreg <> R_NO then
  1006. for r.enum:=lastsavemmreg downto firstsavemmreg do
  1007. begin
  1008. if saved[r.enum].ofs <> reg_not_saved then
  1009. begin
  1010. r2.enum:=R_INTREGISTER;
  1011. r2.number:=NR_FRAME_POINTER_REG;
  1012. reference_reset_base(hr,r2,saved[r.enum].ofs);
  1013. cg.a_reg_alloc(list,r);
  1014. cg.a_loadmm_ref_reg(list,hr,r);
  1015. if not (r.enum in unusedregsmm) then
  1016. { internalerror(10)
  1017. in n386cal we always save/restore the reg *state*
  1018. using save/restoreunusedstate -> the current state
  1019. may not be real (JM) }
  1020. else
  1021. begin
  1022. dec(countunusedregsmm);
  1023. exclude(unusedregsmm,r.enum);
  1024. end;
  1025. tg.UnGetTemp(list,hr);
  1026. end;
  1027. end;
  1028. if firstsavefpureg <> R_NO then
  1029. for r.enum:=lastsavefpureg downto firstsavefpureg do
  1030. begin
  1031. if saved[r.enum].ofs <> reg_not_saved then
  1032. begin
  1033. r2.enum:=R_INTREGISTER;
  1034. r2.number:=NR_FRAME_POINTER_REG;
  1035. reference_reset_base(hr,r2,saved[r.enum].ofs);
  1036. cg.a_reg_alloc(list,r);
  1037. cg.a_loadfpu_ref_reg(list,OS_FLOAT,hr,r);
  1038. if not (r.enum in unusedregsfpu) then
  1039. { internalerror(10)
  1040. in n386cal we always save/restore the reg *state*
  1041. using save/restoreunusedstate -> the current state
  1042. may not be real (JM) }
  1043. else
  1044. begin
  1045. dec(countunusedregsfpu);
  1046. exclude(unusedregsfpu,r.enum);
  1047. end;
  1048. tg.UnGetTemp(list,hr);
  1049. end;
  1050. end;
  1051. {$ifdef TEMPREGDEBUG}
  1052. testregisters32;
  1053. {$endif TEMPREGDEBUG}
  1054. end;
  1055. {$ifndef newra}
  1056. procedure trgobj.incrementintregisterpushed(const s:Tsupregset);
  1057. {$ifdef i386}
  1058. var
  1059. regi:Tsuperregister;
  1060. {$endif i386}
  1061. begin
  1062. {$ifdef i386}
  1063. for regi:=firstsaveintreg to lastsaveintreg do
  1064. begin
  1065. if (regi in s) then
  1066. inc(reg_pushes_int[regi],t_times*2);
  1067. end;
  1068. {$endif i386}
  1069. end;
  1070. {$endif}
  1071. procedure trgobj.incrementotherregisterpushed(const s:Tregisterset);
  1072. {$ifdef i386}
  1073. var
  1074. regi : Toldregister;
  1075. {$endif i386}
  1076. begin
  1077. {$ifdef i386}
  1078. if firstsavefpureg <> R_NO then
  1079. for regi:=firstsavefpureg to lastsavefpureg do
  1080. begin
  1081. if (regi in s) then
  1082. inc(reg_pushes_other[regi],t_times*2);
  1083. end;
  1084. if firstsavemmreg <> R_NO then
  1085. for regi:=firstsavemmreg to lastsavemmreg do
  1086. begin
  1087. if (regi in s) then
  1088. inc(reg_pushes_other[regi],t_times*2);
  1089. end;
  1090. {$endif i386}
  1091. end;
  1092. procedure trgobj.clearregistercount;
  1093. begin
  1094. {$ifndef newra}
  1095. fillchar(reg_pushes_int,sizeof(reg_pushes_int),0);
  1096. {$endif}
  1097. fillchar(reg_pushes_other,sizeof(reg_pushes_other),0);
  1098. {ifndef i386}
  1099. { all used registers will have to be saved at the start and restored }
  1100. { at the end, but otoh regpara's do not have to be saved to memory }
  1101. { at the start (there is a move from regpara to regvar most of the }
  1102. { time though) -> set cost to 100+20 }
  1103. {$ifndef newra}
  1104. filldword(reg_pushes_int[firstsaveintreg],lastsaveintreg-firstsaveintreg+1,120);
  1105. {$endif}
  1106. filldword(reg_pushes_other[firstsavefpureg],ord(lastsavefpureg)-ord(firstsavefpureg)+1,120);
  1107. {endif not i386}
  1108. fillchar(is_reg_var_other,sizeof(is_reg_var_other),false);
  1109. is_reg_var_int:=[];
  1110. fillchar(regvar_loaded_other,sizeof(regvar_loaded_other),false);
  1111. regvar_loaded_int:=[];
  1112. end;
  1113. procedure trgobj.resetusableregisters;
  1114. begin
  1115. { initialize fields with constant values from cpubase }
  1116. countusableregsint := cpubase.c_countusableregsint;
  1117. countusableregsfpu := cpubase.c_countusableregsfpu;
  1118. countusableregsmm := cpubase.c_countusableregsmm;
  1119. usableregsint := cpubase.usableregsint;
  1120. usableregsfpu := cpubase.usableregsfpu;
  1121. usableregsmm := cpubase.usableregsmm;
  1122. clearregistercount;
  1123. end;
  1124. procedure trgobj.makeregvarint(reg:Tsuperregister);
  1125. begin
  1126. dec(countusableregsint);
  1127. {$ifndef newra}
  1128. dec(countunusedregsint);
  1129. {$endif}
  1130. exclude(usableregsint,reg);
  1131. exclude(unusedregsint,reg);
  1132. include(is_reg_var_int,reg);
  1133. include(used_in_proc_int,reg);
  1134. end;
  1135. procedure trgobj.makeregvarother(reg: tregister);
  1136. begin
  1137. if reg.enum>lastreg then
  1138. internalerror(200301081);
  1139. if reg.enum in intregs then
  1140. internalerror(200301151)
  1141. else if reg.enum in fpuregs then
  1142. begin
  1143. dec(countusableregsfpu);
  1144. dec(countunusedregsfpu);
  1145. exclude(usableregsfpu,reg.enum);
  1146. exclude(unusedregsfpu,reg.enum);
  1147. include(used_in_proc_other,reg.enum);
  1148. end
  1149. else if reg.enum in mmregs then
  1150. begin
  1151. dec(countusableregsmm);
  1152. dec(countunusedregsmm);
  1153. exclude(usableregsmm,reg.enum);
  1154. exclude(unusedregsmm,reg.enum);
  1155. include(used_in_proc_other,reg.enum);
  1156. end;
  1157. is_reg_var_other[reg.enum]:=true;
  1158. end;
  1159. {$ifdef TEMPREGDEBUG}
  1160. procedure trgobj.testregisters;
  1161. var
  1162. r: tregister;
  1163. test : byte;
  1164. begin
  1165. test:=0;
  1166. for r := firstsaveintreg to lastsaveintreg do
  1167. inc(test,ord(r in unusedregsint));
  1168. if test<>countunusedregsint then
  1169. internalerror(10);
  1170. end;
  1171. {$endif TEMPREGDEBUG}
  1172. procedure trgobj.saveStateForInline(var state: pointer);
  1173. begin
  1174. new(psavedstate(state));
  1175. psavedstate(state)^.unusedregsint := unusedregsint;
  1176. psavedstate(state)^.usableregsint := usableregsint;
  1177. psavedstate(state)^.unusedregsfpu := unusedregsfpu;
  1178. psavedstate(state)^.usableregsfpu := usableregsfpu;
  1179. psavedstate(state)^.unusedregsmm := unusedregsmm;
  1180. psavedstate(state)^.usableregsmm := usableregsmm;
  1181. {$ifndef newra}
  1182. psavedstate(state)^.countunusedregsint := countunusedregsint;
  1183. {$endif}
  1184. psavedstate(state)^.countunusedregsfpu := countunusedregsfpu;
  1185. psavedstate(state)^.countunusedregsmm := countunusedregsmm;
  1186. psavedstate(state)^.countusableregsint := countusableregsint;
  1187. psavedstate(state)^.countusableregsfpu := countusableregsfpu;
  1188. psavedstate(state)^.countusableregsmm := countusableregsmm;
  1189. psavedstate(state)^.used_in_proc_int := used_in_proc_int;
  1190. psavedstate(state)^.used_in_proc_other := used_in_proc_other;
  1191. {$ifndef newra}
  1192. psavedstate(state)^.reg_pushes_int := reg_pushes_int;
  1193. {$endif}
  1194. psavedstate(state)^.reg_pushes_other := reg_pushes_other;
  1195. psavedstate(state)^.is_reg_var_int := is_reg_var_int;
  1196. psavedstate(state)^.is_reg_var_other := is_reg_var_other;
  1197. psavedstate(state)^.regvar_loaded_int := regvar_loaded_int;
  1198. psavedstate(state)^.regvar_loaded_other := regvar_loaded_other;
  1199. {$ifdef TEMPREGDEBUG}
  1200. psavedstate(state)^.reg_user := reg_user;
  1201. psavedstate(state)^.reg_releaser := reg_releaser;
  1202. {$endif TEMPREGDEBUG}
  1203. end;
  1204. procedure trgobj.restoreStateAfterInline(var state: pointer);
  1205. begin
  1206. unusedregsint := psavedstate(state)^.unusedregsint;
  1207. usableregsint := psavedstate(state)^.usableregsint;
  1208. unusedregsfpu := psavedstate(state)^.unusedregsfpu;
  1209. usableregsfpu := psavedstate(state)^.usableregsfpu;
  1210. unusedregsmm := psavedstate(state)^.unusedregsmm;
  1211. usableregsmm := psavedstate(state)^.usableregsmm;
  1212. {$ifndef newra}
  1213. countunusedregsint := psavedstate(state)^.countunusedregsint;
  1214. {$endif}
  1215. countunusedregsfpu := psavedstate(state)^.countunusedregsfpu;
  1216. countunusedregsmm := psavedstate(state)^.countunusedregsmm;
  1217. countusableregsint := psavedstate(state)^.countusableregsint;
  1218. countusableregsfpu := psavedstate(state)^.countusableregsfpu;
  1219. countusableregsmm := psavedstate(state)^.countusableregsmm;
  1220. used_in_proc_int := psavedstate(state)^.used_in_proc_int;
  1221. used_in_proc_other := psavedstate(state)^.used_in_proc_other;
  1222. {$ifndef newra}
  1223. reg_pushes_int := psavedstate(state)^.reg_pushes_int;
  1224. {$endif}
  1225. reg_pushes_other := psavedstate(state)^.reg_pushes_other;
  1226. is_reg_var_int := psavedstate(state)^.is_reg_var_int;
  1227. is_reg_var_other := psavedstate(state)^.is_reg_var_other;
  1228. regvar_loaded_other := psavedstate(state)^.regvar_loaded_other;
  1229. regvar_loaded_int := psavedstate(state)^.regvar_loaded_int;
  1230. {$ifdef TEMPREGDEBUG}
  1231. reg_user := psavedstate(state)^.reg_user;
  1232. reg_releaser := psavedstate(state)^.reg_releaser;
  1233. {$endif TEMPREGDEBUG}
  1234. dispose(psavedstate(state));
  1235. state := nil;
  1236. end;
  1237. procedure trgobj.saveUnusedState(var state: pointer);
  1238. begin
  1239. new(punusedstate(state));
  1240. {$ifndef newra}
  1241. punusedstate(state)^.unusedregsint := unusedregsint;
  1242. {$endif}
  1243. punusedstate(state)^.unusedregsfpu := unusedregsfpu;
  1244. punusedstate(state)^.unusedregsmm := unusedregsmm;
  1245. {$ifndef newra}
  1246. punusedstate(state)^.countunusedregsint := countunusedregsint;
  1247. {$endif}
  1248. punusedstate(state)^.countunusedregsfpu := countunusedregsfpu;
  1249. punusedstate(state)^.countunusedregsmm := countunusedregsmm;
  1250. end;
  1251. procedure trgobj.restoreUnusedState(var state: pointer);
  1252. begin
  1253. {$ifndef newra}
  1254. unusedregsint := punusedstate(state)^.unusedregsint;
  1255. {$endif}
  1256. unusedregsfpu := punusedstate(state)^.unusedregsfpu;
  1257. unusedregsmm := punusedstate(state)^.unusedregsmm;
  1258. {$ifndef newra}
  1259. countunusedregsint := punusedstate(state)^.countunusedregsint;
  1260. {$endif}
  1261. countunusedregsfpu := punusedstate(state)^.countunusedregsfpu;
  1262. countunusedregsmm := punusedstate(state)^.countunusedregsmm;
  1263. dispose(punusedstate(state));
  1264. state := nil;
  1265. end;
  1266. {$ifdef newra}
  1267. procedure Trgobj.add_edge(u,v:Tsuperregister);
  1268. {This procedure will add an edge to the virtual interference graph.}
  1269. procedure addadj(u,v:Tsuperregister);
  1270. begin
  1271. if igraph.adjlist[u]=nil then
  1272. begin
  1273. getmem(igraph.adjlist[u],16);
  1274. igraph.adjlist[u]^:='';
  1275. end
  1276. else if (length(igraph.adjlist[u]^) and 15)=15 then
  1277. reallocmem(igraph.adjlist[u],length(igraph.adjlist[u]^)+16);
  1278. igraph.adjlist[u]^:=igraph.adjlist[u]^+char(v);
  1279. end;
  1280. begin
  1281. if (u<>v) and not(v in igraph.bitmap[u]) then
  1282. begin
  1283. include(igraph.bitmap[u],v);
  1284. include(igraph.bitmap[v],u);
  1285. {Precoloured nodes are not stored in the interference graph.}
  1286. if not(u in [first_supreg..last_supreg]) then
  1287. begin
  1288. addadj(u,v);
  1289. inc(degree[u]);
  1290. end;
  1291. if not(v in [first_supreg..last_supreg]) then
  1292. begin
  1293. addadj(v,u);
  1294. inc(degree[v]);
  1295. end;
  1296. end;
  1297. end;
  1298. procedure Trgobj.add_edges_used(u:Tsuperregister);
  1299. var i:Tsuperregister;
  1300. begin
  1301. for i:=1 to maxintreg do
  1302. if not(i in unusedregsint) then
  1303. add_edge(u,i);
  1304. end;
  1305. {$ifdef ra_debug2}
  1306. procedure Trgobj.writegraph;
  1307. {This procedure writes out the current interference graph in the
  1308. register allocator.}
  1309. var f:text;
  1310. i,j:Tsuperregister;
  1311. begin
  1312. assign(f,'igraph.'+Lower(current_module.modulename^));
  1313. rewrite(f);
  1314. writeln(f,'Interference graph');
  1315. writeln(f);
  1316. write(f,' ');
  1317. for i:=0 to 15 do
  1318. for j:=0 to 15 do
  1319. write(f,hexstr(i,1));
  1320. writeln(f);
  1321. write(f,' ');
  1322. for i:=0 to 15 do
  1323. write(f,'0123456789ABCDEF');
  1324. writeln(f);
  1325. for i:=0 to 255 do
  1326. begin
  1327. write(f,hexstr(i,2):4);
  1328. for j:=0 to 255 do
  1329. if j in igraph.bitmap[i] then
  1330. write(f,'*')
  1331. else
  1332. write(f,'-');
  1333. writeln(f);
  1334. end;
  1335. close(f);
  1336. end;
  1337. {$endif}
  1338. procedure Trgobj.add_to_movelist(u:Tsuperregister;data:Tlinkedlistitem);
  1339. begin
  1340. if movelist[u]=nil then
  1341. begin
  1342. getmem(movelist[u],64);
  1343. movelist[u]^.count:=0;
  1344. end
  1345. else if (movelist[u]^.count and 15)=15 then
  1346. reallocmem(movelist[u],(movelist[u]^.count+1)*4+64);
  1347. movelist[u]^.data[movelist[u]^.count]:=data;
  1348. inc(movelist[u]^.count);
  1349. end;
  1350. procedure Trgobj.add_move_instruction(instr:Taicpu);
  1351. {This procedure notifies a certain as a move instruction so the
  1352. register allocator can try to eliminate it.}
  1353. var i:Tmoveins;
  1354. ssupreg,dsupreg:Tsuperregister;
  1355. begin
  1356. i:=Tmoveins.create;
  1357. i.moveset:=ms_worklist_moves;
  1358. i.instruction:=instr;
  1359. worklist_moves.insert(i);
  1360. ssupreg:=instr.oper[O_MOV_SOURCE].reg.number shr 8;
  1361. add_to_movelist(ssupreg,i);
  1362. dsupreg:=instr.oper[O_MOV_DEST].reg.number shr 8;
  1363. if ssupreg<>dsupreg then
  1364. {Avoid adding the same move instruction twice to a single register.}
  1365. add_to_movelist(dsupreg,i);
  1366. i.x:=ssupreg;
  1367. i.y:=dsupreg;
  1368. end;
  1369. function Trgobj.move_related(n:Tsuperregister):boolean;
  1370. var i:cardinal;
  1371. begin
  1372. move_related:=false;
  1373. if movelist[n]<>nil then
  1374. begin
  1375. for i:=0 to movelist[n]^.count-1 do
  1376. if Tmoveins(movelist[n]^.data[i]).moveset in
  1377. [ms_worklist_moves,ms_active_moves] then
  1378. begin
  1379. move_related:=true;
  1380. break;
  1381. end;
  1382. end;
  1383. end;
  1384. procedure Trgobj.make_work_list;
  1385. var n:Tsuperregister;
  1386. begin
  1387. {If we have 7 cpu registers, and the degree of a node is 7, we cannot
  1388. assign it to any of the registers, thus it is significant.}
  1389. for n:=first_imreg to maxintreg do
  1390. if degree[n]>=cpu_registers then
  1391. spillworklist:=spillworklist+char(n)
  1392. else if move_related(n) then
  1393. freezeworklist:=freezeworklist+char(n)
  1394. else
  1395. simplifyworklist:=simplifyworklist+char(n);
  1396. end;
  1397. procedure Trgobj.prepare_colouring;
  1398. begin
  1399. make_work_list;
  1400. active_moves:=Tlinkedlist.create;
  1401. frozen_moves:=Tlinkedlist.create;
  1402. coalesced_moves:=Tlinkedlist.create;
  1403. constrained_moves:=Tlinkedlist.create;
  1404. fillchar(alias,sizeof(alias),0);
  1405. coalescednodes:='';
  1406. selectstack:='';
  1407. end;
  1408. procedure Trgobj.enable_moves(n:Tsuperregister);
  1409. var m:Tlinkedlistitem;
  1410. i:cardinal;
  1411. begin
  1412. if movelist[n]<>nil then
  1413. for i:=0 to movelist[n]^.count-1 do
  1414. begin
  1415. m:=movelist[n]^.data[i];
  1416. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  1417. begin
  1418. if Tmoveins(m).moveset=ms_active_moves then
  1419. begin
  1420. {Move m from the set active_moves to the set worklist_moves.}
  1421. active_moves.remove(m);
  1422. Tmoveins(m).moveset:=ms_worklist_moves;
  1423. worklist_moves.concat(m);
  1424. end;
  1425. end;
  1426. end;
  1427. end;
  1428. procedure Trgobj.decrement_degree(m:Tsuperregister);
  1429. var adj:Pstring;
  1430. d:byte;
  1431. i,p:byte;
  1432. n:char;
  1433. begin
  1434. d:=degree[m];
  1435. if degree[m]>0 then
  1436. dec(degree[m]);
  1437. if d=cpu_registers then
  1438. begin
  1439. {Enable moves for m.}
  1440. enable_moves(m);
  1441. {Enable moves for adjacent.}
  1442. adj:=igraph.adjlist[m];
  1443. if adj<>nil then
  1444. for i:=1 to length(adj^) do
  1445. begin
  1446. n:=adj^[i];
  1447. if (pos(n,selectstack) or pos(n,coalescednodes))=0 then
  1448. enable_moves(Tsuperregister(n));
  1449. end;
  1450. {Remove the node from the spillworklist.}
  1451. p:=pos(char(m),spillworklist);
  1452. if p=0 then
  1453. internalerror(200305301); {must be found}
  1454. if length(spillworklist)>1 then
  1455. spillworklist[p]:=spillworklist[length(spillworklist)];
  1456. dec(spillworklist[0]);
  1457. if move_related(m) then
  1458. freezeworklist:=freezeworklist+char(m)
  1459. else
  1460. simplifyworklist:=simplifyworklist+char(m);
  1461. end;
  1462. end;
  1463. procedure Trgobj.simplify;
  1464. var adj:Pstring;
  1465. i,min,p:byte;
  1466. m:char;
  1467. n:Tsuperregister;
  1468. begin
  1469. {We the element with the least interferences out of the
  1470. simplifyworklist.}
  1471. min:=$ff;
  1472. p:=1;
  1473. for i:=1 to length(simplifyworklist) do
  1474. begin
  1475. adj:=igraph.adjlist[Tsuperregister(simplifyworklist[i])];
  1476. if adj=nil then
  1477. begin
  1478. min:=0;
  1479. break; {We won't find smaller ones.}
  1480. end
  1481. else
  1482. if length(adj^)<min then
  1483. begin
  1484. min:=length(adj^);
  1485. if min=0 then
  1486. break; {We won't find smaller ones.}
  1487. p:=i;
  1488. end;
  1489. end;
  1490. n:=Tsuperregister(simplifyworklist[p]);
  1491. if length(simplifyworklist)>1 then
  1492. simplifyworklist[p]:=simplifyworklist[length(simplifyworklist)];
  1493. dec(simplifyworklist[0]);
  1494. {Push it on the selectstack.}
  1495. selectstack:=selectstack+char(n);
  1496. adj:=igraph.adjlist[n];
  1497. if adj<>nil then
  1498. for i:=1 to length(adj^) do
  1499. begin
  1500. m:=adj^[i];
  1501. if (pos(m,selectstack) or pos(m,coalescednodes))=0 then
  1502. decrement_degree(Tsuperregister(m));
  1503. end;
  1504. end;
  1505. function Trgobj.get_alias(n:Tsuperregister):Tsuperregister;
  1506. begin
  1507. while pos(char(n),coalescednodes)<>0 do
  1508. n:=alias[n];
  1509. get_alias:=n;
  1510. end;
  1511. procedure Trgobj.add_worklist(u:Tsuperregister);
  1512. begin
  1513. if not(u in [first_supreg..last_supreg]) and not move_related(u) and
  1514. (degree[u]<cpu_registers) then
  1515. begin
  1516. if length(freezeworklist)>1 then
  1517. freezeworklist[pos(char(u),freezeworklist)]:=freezeworklist[length(freezeworklist)];
  1518. dec(freezeworklist[0]);
  1519. { delete(freezeworklist,pos(char(u),freezeworklist),1);}
  1520. simplifyworklist:=simplifyworklist+char(u);
  1521. end;
  1522. end;
  1523. function Trgobj.adjacent_ok(u,v:Tsuperregister):boolean;
  1524. {Check wether u and v should be coalesced. u is precoloured.}
  1525. function ok(t,r:Tsuperregister):boolean;
  1526. begin
  1527. ok:=(degree[t]<cpu_registers) or
  1528. (t in [first_supreg..last_supreg]) or
  1529. (r in igraph.bitmap[t]);
  1530. end;
  1531. var adj:Pstring;
  1532. i:byte;
  1533. t:char;
  1534. begin
  1535. adjacent_ok:=true;
  1536. adj:=igraph.adjlist[v];
  1537. if adj<>nil then
  1538. for i:=1 to length(adj^) do
  1539. begin
  1540. t:=adj^[i];
  1541. if (pos(t,selectstack) or pos(t,coalescednodes))=0 then
  1542. if not ok(Tsuperregister(t),u) then
  1543. begin
  1544. adjacent_ok:=false;
  1545. break;
  1546. end;
  1547. end;
  1548. end;
  1549. function Trgobj.conservative(u,v:Tsuperregister):boolean;
  1550. var adj:Pstring;
  1551. done:set of char; {To prevent that we count nodes twice.}
  1552. i,k:byte;
  1553. n:char;
  1554. begin
  1555. k:=0;
  1556. done:=[];
  1557. adj:=igraph.adjlist[u];
  1558. if adj<>nil then
  1559. for i:=1 to length(adj^) do
  1560. begin
  1561. n:=adj^[i];
  1562. if (pos(n,selectstack) or pos(n,coalescednodes))=0 then
  1563. begin
  1564. include(done,n);
  1565. if degree[Tsuperregister(n)]>=cpu_registers then
  1566. inc(k);
  1567. end;
  1568. end;
  1569. adj:=igraph.adjlist[v];
  1570. if adj<>nil then
  1571. for i:=1 to length(adj^) do
  1572. begin
  1573. n:=adj^[i];
  1574. if ((pos(n,selectstack) or pos(n,coalescednodes))=0) and
  1575. not (n in done) and
  1576. (degree[Tsuperregister(n)]>=cpu_registers) then
  1577. inc(k);
  1578. end;
  1579. conservative:=(k<cpu_registers);
  1580. end;
  1581. procedure Trgobj.combine(u,v:Tsuperregister);
  1582. var add:boolean;
  1583. adj:Pstring;
  1584. i,p:byte;
  1585. n,o:cardinal;
  1586. t:char;
  1587. begin
  1588. p:=pos(char(v),freezeworklist);
  1589. if p<>0 then
  1590. delete(freezeworklist,p,1)
  1591. else
  1592. delete(spillworklist,pos(char(v),spillworklist),1);
  1593. coalescednodes:=coalescednodes+char(v);
  1594. alias[v]:=u;
  1595. {Combine both movelists. Since the movelists are sets, only add
  1596. elements that are not already present.}
  1597. for n:=0 to movelist[v]^.count-1 do
  1598. begin
  1599. add:=true;
  1600. for o:=0 to movelist[u]^.count-1 do
  1601. if movelist[u]^.data[o]=movelist[v]^.data[n] then
  1602. begin
  1603. add:=false;
  1604. break;
  1605. end;
  1606. if add then
  1607. add_to_movelist(u,movelist[v]^.data[n]);
  1608. end;
  1609. enable_moves(v);
  1610. adj:=igraph.adjlist[v];
  1611. if adj<>nil then
  1612. for i:=1 to length(adj^) do
  1613. begin
  1614. t:=adj^[i];
  1615. if (pos(t,selectstack) or pos(t,coalescednodes))=0 then
  1616. begin
  1617. decrement_degree(Tsuperregister(t));
  1618. add_edge(Tsuperregister(t),u);
  1619. end;
  1620. end;
  1621. p:=pos(char(u),freezeworklist);
  1622. if (degree[u]>=cpu_registers) and (p<>0) then
  1623. begin
  1624. delete(freezeworklist,p,1);
  1625. spillworklist:=spillworklist+char(u);
  1626. end;
  1627. end;
  1628. procedure Trgobj.coalesce;
  1629. var m:Tmoveins;
  1630. x,y,u,v:Tsuperregister;
  1631. begin
  1632. m:=Tmoveins(worklist_moves.getfirst);
  1633. x:=get_alias(m.instruction.oper[0].reg.number shr 8);
  1634. y:=get_alias(m.instruction.oper[1].reg.number shr 8);
  1635. if y in [first_supreg..last_supreg] then
  1636. begin
  1637. u:=y;
  1638. v:=x;
  1639. end
  1640. else
  1641. begin
  1642. u:=x;
  1643. v:=y;
  1644. end;
  1645. if (u=v) then
  1646. begin
  1647. m.moveset:=ms_coalesced_moves; {Already coalesced.}
  1648. coalesced_moves.insert(m);
  1649. add_worklist(u);
  1650. end
  1651. {Do u and v interfere? In that case the move is constrained. Two
  1652. precoloured nodes interfere allways. If v is precoloured, by the above
  1653. code u is precoloured, thus interference...}
  1654. else if (v in [first_supreg..last_supreg]) or (u in igraph.bitmap[v]) then
  1655. begin
  1656. m.moveset:=ms_constrained_moves; {Cannot coalesce yet...}
  1657. constrained_moves.insert(m);
  1658. add_worklist(u);
  1659. add_worklist(v);
  1660. end
  1661. {Next test: is it possible and a good idea to coalesce??}
  1662. else if ((u in [first_supreg..last_supreg]) and adjacent_ok(u,v)) or
  1663. (not(u in [first_supreg..last_supreg]) and conservative(u,v)) then
  1664. begin
  1665. m.moveset:=ms_coalesced_moves; {Move coalesced!}
  1666. coalesced_moves.insert(m);
  1667. combine(u,v);
  1668. add_worklist(u);
  1669. end
  1670. else
  1671. begin
  1672. m.moveset:=ms_active_moves;
  1673. active_moves.insert(m);
  1674. end;
  1675. end;
  1676. procedure Trgobj.freeze_moves(u:Tsuperregister);
  1677. var i:cardinal;
  1678. m:Tlinkedlistitem;
  1679. v,x,y:Tsuperregister;
  1680. begin
  1681. if movelist[u]<>nil then
  1682. for i:=0 to movelist[u]^.count-1 do
  1683. begin
  1684. m:=movelist[u]^.data[i];
  1685. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  1686. begin
  1687. x:=Tmoveins(m).instruction.oper[0].reg.number shr 8;
  1688. y:=Tmoveins(m).instruction.oper[1].reg.number shr 8;
  1689. if get_alias(y)=get_alias(u) then
  1690. v:=get_alias(x)
  1691. else
  1692. v:=get_alias(y);
  1693. {Move m from active_moves/worklist_moves to frozen_moves.}
  1694. if Tmoveins(m).moveset=ms_active_moves then
  1695. active_moves.remove(m)
  1696. else
  1697. worklist_moves.remove(m);
  1698. Tmoveins(m).moveset:=ms_frozen_moves;
  1699. frozen_moves.insert(m);
  1700. if not(move_related(v)) and (degree[v]<cpu_registers) then
  1701. begin
  1702. delete(freezeworklist,pos(char(v),freezeworklist),1);
  1703. simplifyworklist:=simplifyworklist+char(v);
  1704. end;
  1705. end;
  1706. end;
  1707. end;
  1708. procedure Trgobj.freeze;
  1709. var n:Tsuperregister;
  1710. begin
  1711. {We need to take a random element out of the freezeworklist. We take
  1712. the last element. Dirty code!}
  1713. n:=Tsuperregister(freezeworklist[byte(freezeworklist[0])]);
  1714. dec(freezeworklist[0]);
  1715. {Add it to the simplifyworklist.}
  1716. simplifyworklist:=simplifyworklist+char(n);
  1717. freeze_moves(n);
  1718. end;
  1719. procedure Trgobj.select_spill;
  1720. var n:char;
  1721. begin
  1722. {This code is WAY too naive. We need not to select just a register, but
  1723. the register that is used the least...}
  1724. n:=spillworklist[byte(spillworklist[0])];
  1725. dec(spillworklist[0]);
  1726. simplifyworklist:=simplifyworklist+n;
  1727. freeze_moves(Tsuperregister(n));
  1728. end;
  1729. procedure Trgobj.assign_colours;
  1730. {Assign_colours assigns the actual colours to the registers.}
  1731. var adj:Pstring;
  1732. i,j,k:byte;
  1733. n,a:Tsuperregister;
  1734. adj_colours,colourednodes:set of Tsuperregister;
  1735. w:char;
  1736. begin
  1737. spillednodes:='';
  1738. {Colour the cpu registers...}
  1739. colourednodes:=[first_supreg..last_supreg];
  1740. for i:=first_supreg to last_supreg do
  1741. colour[i]:=i;
  1742. {Now colour the imaginary registers on the select-stack.}
  1743. for i:=length(selectstack) downto 1 do
  1744. begin
  1745. n:=Tsuperregister(selectstack[i]);
  1746. {Create a list of colours that we cannot assign to n.}
  1747. adj_colours:=[];
  1748. adj:=igraph.adjlist[n];
  1749. if adj<>nil then
  1750. for j:=1 to length(adj^) do
  1751. begin
  1752. w:=adj^[j];
  1753. a:=get_alias(Tsuperregister(w));
  1754. if a in colourednodes then
  1755. include(adj_colours,colour[a]);
  1756. end;
  1757. include(adj_colours,RS_STACK_POINTER_REG);
  1758. {Assume a spill by default...}
  1759. spillednodes:=spillednodes+char(n);
  1760. {Search for a colour not in this list.}
  1761. for k:=first_supreg to last_supreg do
  1762. if not(k in adj_colours) then
  1763. begin
  1764. colour[n]:=k;
  1765. dec(spillednodes[0]); {Colour found: no spill.}
  1766. include(colourednodes,n);
  1767. if n in used_in_proc_int then
  1768. include(used_in_proc_int,k);
  1769. break;
  1770. end;
  1771. end;
  1772. {Finally colour the nodes that were coalesced.}
  1773. for i:=1 to length(coalescednodes) do
  1774. begin
  1775. n:=Tsuperregister(coalescednodes[i]);
  1776. k:=get_alias(n);
  1777. colour[n]:=colour[k];
  1778. if n in used_in_proc_int then
  1779. include(used_in_proc_int,colour[k]);
  1780. end;
  1781. { registers which aren't available to the register allocator must }
  1782. { retain their value }
  1783. for i := 1 to first_supreg-1 do
  1784. colour[i] := i;
  1785. {$ifdef ra_debug}
  1786. for i:=first_imreg to maxintreg do
  1787. writeln(i:4,' ',colour[i]:4)
  1788. {$endif}
  1789. end;
  1790. procedure Trgobj.colour_registers;
  1791. begin
  1792. repeat
  1793. if length(simplifyworklist)<>0 then
  1794. simplify
  1795. else if not(worklist_moves.empty) then
  1796. coalesce
  1797. else if length(freezeworklist)<>0 then
  1798. freeze
  1799. else if length(spillworklist)<>0 then
  1800. select_spill;
  1801. until (length(simplifyworklist) or
  1802. byte(not(worklist_moves.empty)) or
  1803. length(freezeworklist) or
  1804. length(spillworklist)
  1805. )=0;
  1806. assign_colours;
  1807. end;
  1808. procedure Trgobj.epilogue_colouring;
  1809. {
  1810. procedure move_to_worklist_moves(list:Tlinkedlist);
  1811. var p:Tlinkedlistitem;
  1812. begin
  1813. p:=list.first;
  1814. while p<>nil do
  1815. begin
  1816. Tmoveins(p).moveset:=ms_worklist_moves;
  1817. p:=p.next;
  1818. end;
  1819. worklist_moves.concatlist(list);
  1820. end;
  1821. }
  1822. var i:Tsuperregister;
  1823. begin
  1824. worklist_moves.clear;
  1825. {$ifdef Principle_wrong_by_definition}
  1826. {Move everything back to worklist_moves.}
  1827. move_to_worklist_moves(active_moves);
  1828. move_to_worklist_moves(frozen_moves);
  1829. move_to_worklist_moves(coalesced_moves);
  1830. move_to_worklist_moves(constrained_moves);
  1831. {$endif}
  1832. active_moves.destroy;
  1833. active_moves:=nil;
  1834. frozen_moves.destroy;
  1835. frozen_moves:=nil;
  1836. coalesced_moves.destroy;
  1837. coalesced_moves:=nil;
  1838. constrained_moves.destroy;
  1839. constrained_moves:=nil;
  1840. for i:=0 to 255 do
  1841. if movelist[i]<>nil then
  1842. begin
  1843. dispose(movelist[i]);
  1844. movelist[i]:=0;
  1845. end;
  1846. end;
  1847. procedure Trgobj.clear_interferences(u:Tsuperregister);
  1848. {Remove node u from the interference graph and remove all collected
  1849. move instructions it is associated with.}
  1850. var i:byte;
  1851. j,k,count:cardinal;
  1852. v:Tsuperregister;
  1853. m,n:Tmoveins;
  1854. begin
  1855. if igraph.adjlist[u]<>nil then
  1856. begin
  1857. for i:=1 to length(igraph.adjlist[u]^) do
  1858. begin
  1859. v:=Tsuperregister(igraph.adjlist[u]^[i]);
  1860. {Remove (u,v) and (v,u) from bitmap.}
  1861. exclude(igraph.bitmap[u],v);
  1862. exclude(igraph.bitmap[v],u);
  1863. {Remove (v,u) from adjacency list.}
  1864. if igraph.adjlist[v]<>nil then
  1865. begin
  1866. delete(igraph.adjlist[v]^,pos(char(v),igraph.adjlist[v]^),1);
  1867. if length(igraph.adjlist[v]^)=0 then
  1868. begin
  1869. dispose(igraph.adjlist[v]);
  1870. igraph.adjlist[v]:=nil;
  1871. end;
  1872. end;
  1873. end;
  1874. {Remove ( u,* ) from adjacency list.}
  1875. dispose(igraph.adjlist[u]);
  1876. igraph.adjlist[u]:=nil;
  1877. end;
  1878. {$ifdef Principle_wrong_by_definition}
  1879. {Now remove the moves.}
  1880. if movelist[u]<>nil then
  1881. begin
  1882. for j:=0 to movelist[u]^.count-1 do
  1883. begin
  1884. m:=Tmoveins(movelist[u]^.data[j]);
  1885. {Get the other register of the move instruction.}
  1886. v:=m.instruction.oper[0].reg.number shr 8;
  1887. if v=u then
  1888. v:=m.instruction.oper[1].reg.number shr 8;
  1889. repeat
  1890. repeat
  1891. if (u<>v) and (movelist[v]<>nil) then
  1892. begin
  1893. {Remove the move from it's movelist.}
  1894. count:=movelist[v]^.count-1;
  1895. for k:=0 to count do
  1896. if m=movelist[v]^.data[k] then
  1897. begin
  1898. if k<>count then
  1899. movelist[v]^.data[k]:=movelist[v]^.data[count];
  1900. dec(movelist[v]^.count);
  1901. if count=0 then
  1902. begin
  1903. dispose(movelist[v]);
  1904. movelist[v]:=nil;
  1905. end;
  1906. break;
  1907. end;
  1908. end;
  1909. {The complexity is enourmous: the register might have been
  1910. coalesced. In that case it's movelists have been added to
  1911. it's coalescing alias. (DM)}
  1912. v:=alias[v];
  1913. until v=0;
  1914. {And also register u might have been coalesced.}
  1915. u:=alias[u];
  1916. until u=0;
  1917. case m.moveset of
  1918. ms_coalesced_moves:
  1919. coalesced_moves.remove(m);
  1920. ms_constrained_moves:
  1921. constrained_moves.remove(m);
  1922. ms_frozen_moves:
  1923. frozen_moves.remove(m);
  1924. ms_worklist_moves:
  1925. worklist_moves.remove(m);
  1926. ms_active_moves:
  1927. active_moves.remove(m);
  1928. end;
  1929. end;
  1930. dispose(movelist[u]);
  1931. movelist[u]:=nil;
  1932. end;
  1933. {$endif}
  1934. end;
  1935. procedure Trgobj.getregisterintinline(list:Taasmoutput;position:Tai;subreg:Tsubregister;var result:Tregister);
  1936. var i:Tsuperregister;
  1937. r:Tregister;
  1938. begin
  1939. if not (lastintreg in [first_imreg..last_imreg]) then
  1940. lastintreg:=first_imreg;
  1941. i:=lastintreg;
  1942. repeat
  1943. if i=last_imreg then
  1944. i:=first_imreg
  1945. else
  1946. inc(i);
  1947. if (i in unusedregsint) and (pos(char(i),abtlist)=0) then
  1948. begin
  1949. exclude(unusedregsint,i);
  1950. include(used_in_proc_int,i);
  1951. r.enum:=R_INTREGISTER;
  1952. r.number:=i shl 8 or subreg;
  1953. if position=nil then
  1954. list.insert(Tai_regalloc.alloc(r))
  1955. else
  1956. list.insertafter(Tai_regalloc.alloc(r),position);
  1957. result:=r;
  1958. lastintreg:=i;
  1959. if i>maxintreg then
  1960. maxintreg:=i;
  1961. add_edges_used(i);
  1962. add_constraints(result.number);
  1963. exit;
  1964. end;
  1965. until i=lastintreg;
  1966. internalerror(10);
  1967. end;
  1968. {In some cases we can get in big trouble. See this example:
  1969. ; register reg23d released
  1970. ; register eax allocated
  1971. ; register ebx allocated
  1972. ; register ecx allocated
  1973. ; register edx allocated
  1974. ; register esi allocated
  1975. ; register edi allocated
  1976. call [reg23d]
  1977. This code is ok, *except* when reg23d is spilled. In that case the
  1978. spilled would introduce a help register which can never get
  1979. allocated to a real register because it interferes with all of them.
  1980. To solve this we introduce the ABT ("avoid big trouble :)" ) registers.
  1981. If you allocate an ABT register you get a register that has less
  1982. than cpu_register interferences and will not be allocated ever again
  1983. by the normal register get procedures. In other words it is for sure it
  1984. will never get spilled.}
  1985. function Trgobj.getabtregisterint(list:Taasmoutput;size:Tcgsize):Tregister;
  1986. var i:Tsuperregister;
  1987. r:Tregister;
  1988. found:boolean;
  1989. begin
  1990. if not (lastintreg in [first_imreg..last_imreg]) then
  1991. lastintreg:=first_imreg;
  1992. found:=false;
  1993. for i:=1 to length(abtlist) do
  1994. if Tsuperregister(abtlist[i]) in unusedregsint then
  1995. begin
  1996. found:=true;
  1997. break;
  1998. end;
  1999. i:=lastintreg;
  2000. repeat
  2001. if i=last_imreg then
  2002. i:=first_imreg
  2003. else
  2004. inc(i);
  2005. if (i in unusedregsint) and ((igraph.adjlist[i]=nil) or (length(igraph.adjlist[i]^)<cpu_registers)) then
  2006. begin
  2007. found:=true;
  2008. break;
  2009. end;
  2010. until i=lastintreg;
  2011. if found then
  2012. begin
  2013. exclude(unusedregsint,i);
  2014. include(used_in_proc_int,i);
  2015. r.enum:=R_INTREGISTER;
  2016. r.number:=i shl 8 or cgsize2subreg(size);
  2017. list.concat(Tai_regalloc.alloc(r));
  2018. getabtregisterint:=r;
  2019. lastintreg:=i;
  2020. if i>maxintreg then
  2021. maxintreg:=i;
  2022. add_edges_used(i);
  2023. if pos(char(i),abtlist)=0 then
  2024. abtlist:=abtlist+char(i);
  2025. end
  2026. else
  2027. internalerror(10);
  2028. {$ifdef newra}
  2029. add_constraints(getabtregisterint.number);
  2030. {$endif}
  2031. end;
  2032. procedure Trgobj.ungetregisterintinline(list:Taasmoutput;position:Tai;const r:Tregister);
  2033. var supreg:Tsuperregister;
  2034. begin
  2035. if r.enum<=lastreg then
  2036. internalerror(200301083);
  2037. supreg:=r.number shr 8;
  2038. include(unusedregsint,supreg);
  2039. if position=nil then
  2040. list.insert(Tai_regalloc.dealloc(r))
  2041. else
  2042. list.insertafter(Tai_regalloc.dealloc(r),position);
  2043. add_edges_used(supreg);
  2044. end;
  2045. function Trgobj.spill_registers(list:Taasmoutput;const regs_to_spill:string):boolean;
  2046. {Returns true if any help registers have been used.}
  2047. var i:byte;
  2048. r:Tsuperregister;
  2049. p,q:Tai;
  2050. regs_to_spill_set:Tsupregset;
  2051. spill_temps:^Tspill_temp_list;
  2052. begin
  2053. spill_registers:=false;
  2054. unusedregsint:=[0..255];
  2055. fillchar(degree,sizeof(degree),0);
  2056. { exclude(unusedregsint,RS_STACK_POINTER_REG);}
  2057. if current_procinfo.framepointer.number=NR_FRAME_POINTER_REG then
  2058. {Make sure the register allocator won't allocate registers into ebp.}
  2059. exclude(unusedregsint,RS_FRAME_POINTER_REG);
  2060. new(spill_temps);
  2061. fillchar(spill_temps^,sizeof(spill_temps^),0);
  2062. regs_to_spill_set:=[];
  2063. for i:=1 to length(regs_to_spill) do
  2064. begin
  2065. {Alternative representation.}
  2066. include(regs_to_spill_set,Tsuperregister(regs_to_spill[i]));
  2067. {Clear all interferences of the spilled register.}
  2068. clear_interferences(Tsuperregister(regs_to_spill[i]));
  2069. {Get a temp for the spilled register.}
  2070. tg.gettemp(list,4,tt_noreuse,spill_temps^[Tsuperregister(regs_to_spill[i])]);
  2071. end;
  2072. p:=Tai(list.first);
  2073. while assigned(p) do
  2074. begin
  2075. case p.typ of
  2076. ait_regalloc:
  2077. begin
  2078. {A register allocation of a spilled register can be removed.}
  2079. if (Tai_regalloc(p).reg.number shr 8) in regs_to_spill_set then
  2080. begin
  2081. q:=p;
  2082. p:=Tai(p.next);
  2083. list.remove(q);
  2084. continue;
  2085. end
  2086. else
  2087. if Tai_regalloc(p).allocation then
  2088. exclude(unusedregsint,Tai_regalloc(p).reg.number shr 8)
  2089. else
  2090. include(unusedregsint,Tai_regalloc(p).reg.number shr 8);
  2091. end;
  2092. ait_instruction:
  2093. begin
  2094. if Taicpu_abstract(p).spill_registers(list,@getregisterintinline,
  2095. @ungetregisterintinline,
  2096. regs_to_spill_set,
  2097. unusedregsint,
  2098. spill_temps^) then
  2099. spill_registers:=true;
  2100. if Taicpu_abstract(p).is_move then
  2101. add_move_instruction(Taicpu(p));
  2102. end;
  2103. end;
  2104. p:=Tai(p.next);
  2105. end;
  2106. for i:=1 to length(regs_to_spill) do
  2107. begin
  2108. tg.ungettemp(list,spill_temps^[Tsuperregister(regs_to_spill[i])]);
  2109. end;
  2110. dispose(spill_temps);
  2111. end;
  2112. {$endif newra}
  2113. {****************************************************************************
  2114. TReference
  2115. ****************************************************************************}
  2116. procedure reference_reset(var ref : treference);
  2117. begin
  2118. FillChar(ref,sizeof(treference),0);
  2119. ref.base.enum:=R_INTREGISTER;
  2120. ref.index.enum:=R_INTREGISTER;
  2121. {$ifdef i386}
  2122. ref.segment.enum:=R_INTREGISTER;
  2123. {$endif}
  2124. end;
  2125. procedure reference_reset_old(var ref : treference);
  2126. begin
  2127. FillChar(ref,sizeof(treference),0);
  2128. end;
  2129. procedure reference_reset_base(var ref : treference;base : tregister;offset : longint);
  2130. begin
  2131. reference_reset(ref);
  2132. ref.base:=base;
  2133. ref.offset:=offset;
  2134. end;
  2135. procedure reference_reset_symbol(var ref : treference;sym : tasmsymbol;offset : longint);
  2136. begin
  2137. reference_reset(ref);
  2138. ref.symbol:=sym;
  2139. ref.offset:=offset;
  2140. end;
  2141. procedure reference_release(list: taasmoutput; const ref : treference);
  2142. begin
  2143. rg.ungetreference(list,ref);
  2144. end;
  2145. function references_equal(sref : treference;dref : treference):boolean;
  2146. begin
  2147. references_equal:=CompareByte(sref,dref,sizeof(treference))=0;
  2148. end;
  2149. { on most processors , this routine does nothing, overriden currently }
  2150. { only by 80x86 processor. }
  2151. function trgobj.makeregsize(reg: tregister; size: tcgsize): tregister;
  2152. begin
  2153. makeregsize := reg;
  2154. end;
  2155. {****************************************************************************
  2156. TLocation
  2157. ****************************************************************************}
  2158. procedure location_reset(var l : tlocation;lt:TCGLoc;lsize:TCGSize);
  2159. begin
  2160. FillChar(l,sizeof(tlocation),0);
  2161. l.loc:=lt;
  2162. l.size:=lsize;
  2163. case l.loc of
  2164. LOC_REGISTER,LOC_CREGISTER:
  2165. begin
  2166. l.register.enum:=R_INTREGISTER;
  2167. l.registerhigh.enum:=R_INTREGISTER;
  2168. end;
  2169. LOC_REFERENCE,LOC_CREFERENCE:
  2170. begin
  2171. l.reference.base.enum:=R_INTREGISTER;
  2172. l.reference.index.enum:=R_INTREGISTER;
  2173. {$ifdef i386}
  2174. l.reference.segment.enum:=R_INTREGISTER;
  2175. {$endif}
  2176. end;
  2177. end;
  2178. end;
  2179. procedure location_release(list: taasmoutput; const l : tlocation);
  2180. begin
  2181. case l.loc of
  2182. LOC_REGISTER,LOC_CREGISTER :
  2183. begin
  2184. rg.ungetregisterint(list,l.register);
  2185. if l.size in [OS_64,OS_S64] then
  2186. rg.ungetregisterint(list,l.registerhigh);
  2187. end;
  2188. LOC_FPUREGISTER,LOC_CFPUREGISTER :
  2189. rg.ungetregisterfpu(list,l.register,l.size);
  2190. LOC_CREFERENCE,LOC_REFERENCE :
  2191. rg.ungetreference(list, l.reference);
  2192. end;
  2193. end;
  2194. procedure location_freetemp(list:taasmoutput; const l : tlocation);
  2195. begin
  2196. if (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  2197. tg.ungetiftemp(list,l.reference);
  2198. end;
  2199. procedure location_copy(var destloc:tlocation; const sourceloc : tlocation);
  2200. begin
  2201. destloc:=sourceloc;
  2202. end;
  2203. procedure location_swap(var destloc,sourceloc : tlocation);
  2204. var
  2205. swapl : tlocation;
  2206. begin
  2207. swapl := destloc;
  2208. destloc := sourceloc;
  2209. sourceloc := swapl;
  2210. end;
  2211. initialization
  2212. ;
  2213. finalization
  2214. rg.free;
  2215. end.
  2216. {
  2217. $Log$
  2218. Revision 1.61 2003-07-21 13:32:39 jonas
  2219. * add_edges_used() is now also called for registers allocated with
  2220. getexplicitregisterint()
  2221. * writing the intereference graph is now only done with -dradebug2 and
  2222. the created files are now called "igraph.<module_name>"
  2223. Revision 1.60 2003/07/06 15:31:21 daniel
  2224. * Fixed register allocator. *Lots* of fixes.
  2225. Revision 1.59 2003/07/06 15:00:47 jonas
  2226. * fixed my previous completely broken commit. It's not perfect though,
  2227. registers > last_supreg and < max_intreg may still be "translated"
  2228. Revision 1.58 2003/07/06 14:45:05 jonas
  2229. * support integer registers that are not managed by newra (ie. don't
  2230. translate register numbers that fall outside the range
  2231. first_supreg..last_supreg)
  2232. Revision 1.57 2003/07/02 22:18:04 peter
  2233. * paraloc splitted in callerparaloc,calleeparaloc
  2234. * sparc calling convention updates
  2235. Revision 1.56 2003/06/17 16:34:44 jonas
  2236. * lots of newra fixes (need getfuncretparaloc implementation for i386)!
  2237. * renamed all_intregisters to volatile_intregisters and made it
  2238. processor dependent
  2239. Revision 1.55 2003/06/14 14:53:50 jonas
  2240. * fixed newra cycle for x86
  2241. * added constants for indicating source and destination operands of the
  2242. "move reg,reg" instruction to aasmcpu (and use those in rgobj)
  2243. Revision 1.54 2003/06/13 21:19:31 peter
  2244. * current_procdef removed, use current_procinfo.procdef instead
  2245. Revision 1.53 2003/06/12 21:11:10 peter
  2246. * ungetregisterfpu gets size parameter
  2247. Revision 1.52 2003/06/12 16:43:07 peter
  2248. * newra compiles for sparc
  2249. Revision 1.51 2003/06/09 14:54:26 jonas
  2250. * (de)allocation of registers for parameters is now performed properly
  2251. (and checked on the ppc)
  2252. - removed obsolete allocation of all parameter registers at the start
  2253. of a procedure (and deallocation at the end)
  2254. Revision 1.50 2003/06/03 21:11:09 peter
  2255. * cg.a_load_* get a from and to size specifier
  2256. * makeregsize only accepts newregister
  2257. * i386 uses generic tcgnotnode,tcgunaryminus
  2258. Revision 1.49 2003/06/03 13:01:59 daniel
  2259. * Register allocator finished
  2260. Revision 1.48 2003/06/01 21:38:06 peter
  2261. * getregisterfpu size parameter added
  2262. * op_const_reg size parameter added
  2263. * sparc updates
  2264. Revision 1.47 2003/05/31 20:31:11 jonas
  2265. * set inital costs of assigning a variable to a register to 120 for
  2266. non-i386, because the used register must be store to memory at the
  2267. start and loaded again at the end
  2268. Revision 1.46 2003/05/30 18:55:21 jonas
  2269. * fixed several regvar related bugs for non-i386. make cycle with -Or now
  2270. works for ppc
  2271. Revision 1.45 2003/05/30 12:36:13 jonas
  2272. * use as little different registers on the ppc until newra is released,
  2273. since every used register must be saved
  2274. Revision 1.44 2003/05/17 13:30:08 jonas
  2275. * changed tt_persistant to tt_persistent :)
  2276. * tempcreatenode now doesn't accept a boolean anymore for persistent
  2277. temps, but a ttemptype, so you can also create ansistring temps etc
  2278. Revision 1.43 2003/05/16 14:33:31 peter
  2279. * regvar fixes
  2280. Revision 1.42 2003/04/26 20:03:49 daniel
  2281. * Bug fix in simplify
  2282. Revision 1.41 2003/04/25 20:59:35 peter
  2283. * removed funcretn,funcretsym, function result is now in varsym
  2284. and aliases for result and function name are added using absolutesym
  2285. * vs_hidden parameter for funcret passed in parameter
  2286. * vs_hidden fixes
  2287. * writenode changed to printnode and released from extdebug
  2288. * -vp option added to generate a tree.log with the nodetree
  2289. * nicer printnode for statements, callnode
  2290. Revision 1.40 2003/04/25 08:25:26 daniel
  2291. * Ifdefs around a lot of calls to cleartempgen
  2292. * Fixed registers that are allocated but not freed in several nodes
  2293. * Tweak to register allocator to cause less spills
  2294. * 8-bit registers now interfere with esi,edi and ebp
  2295. Compiler can now compile rtl successfully when using new register
  2296. allocator
  2297. Revision 1.39 2003/04/23 20:23:06 peter
  2298. * compile fix for no-newra
  2299. Revision 1.38 2003/04/23 14:42:07 daniel
  2300. * Further register allocator work. Compiler now smaller with new
  2301. allocator than without.
  2302. * Somebody forgot to adjust ppu version number
  2303. Revision 1.37 2003/04/22 23:50:23 peter
  2304. * firstpass uses expectloc
  2305. * checks if there are differences between the expectloc and
  2306. location.loc from secondpass in EXTDEBUG
  2307. Revision 1.36 2003/04/22 10:09:35 daniel
  2308. + Implemented the actual register allocator
  2309. + Scratch registers unavailable when new register allocator used
  2310. + maybe_save/maybe_restore unavailable when new register allocator used
  2311. Revision 1.35 2003/04/21 19:16:49 peter
  2312. * count address regs separate
  2313. Revision 1.34 2003/04/17 16:48:21 daniel
  2314. * Added some code to keep track of move instructions in register
  2315. allocator
  2316. Revision 1.33 2003/04/17 07:50:24 daniel
  2317. * Some work on interference graph construction
  2318. Revision 1.32 2003/03/28 19:16:57 peter
  2319. * generic constructor working for i386
  2320. * remove fixed self register
  2321. * esi added as address register for i386
  2322. Revision 1.31 2003/03/11 21:46:24 jonas
  2323. * lots of new regallocator fixes, both in generic and ppc-specific code
  2324. (ppc compiler still can't compile the linux system unit though)
  2325. Revision 1.30 2003/03/09 21:18:59 olle
  2326. + added cutils to the uses clause
  2327. Revision 1.29 2003/03/08 20:36:41 daniel
  2328. + Added newra version of Ti386shlshrnode
  2329. + Added interference graph construction code
  2330. Revision 1.28 2003/03/08 13:59:16 daniel
  2331. * Work to handle new register notation in ag386nsm
  2332. + Added newra version of Ti386moddivnode
  2333. Revision 1.27 2003/03/08 10:53:48 daniel
  2334. * Created newra version of secondmul in n386add.pas
  2335. Revision 1.26 2003/03/08 08:59:07 daniel
  2336. + $define newra will enable new register allocator
  2337. + getregisterint will return imaginary registers with $newra
  2338. + -sr switch added, will skip register allocation so you can see
  2339. the direct output of the code generator before register allocation
  2340. Revision 1.25 2003/02/26 20:50:45 daniel
  2341. * Fixed ungetreference
  2342. Revision 1.24 2003/02/19 22:39:56 daniel
  2343. * Fixed a few issues
  2344. Revision 1.23 2003/02/19 22:00:14 daniel
  2345. * Code generator converted to new register notation
  2346. - Horribily outdated todo.txt removed
  2347. Revision 1.22 2003/02/02 19:25:54 carl
  2348. * Several bugfixes for m68k target (register alloc., opcode emission)
  2349. + VIS target
  2350. + Generic add more complete (still not verified)
  2351. Revision 1.21 2003/01/08 18:43:57 daniel
  2352. * Tregister changed into a record
  2353. Revision 1.20 2002/10/05 12:43:28 carl
  2354. * fixes for Delphi 6 compilation
  2355. (warning : Some features do not work under Delphi)
  2356. Revision 1.19 2002/08/23 16:14:49 peter
  2357. * tempgen cleanup
  2358. * tt_noreuse temp type added that will be used in genentrycode
  2359. Revision 1.18 2002/08/17 22:09:47 florian
  2360. * result type handling in tcgcal.pass_2 overhauled
  2361. * better tnode.dowrite
  2362. * some ppc stuff fixed
  2363. Revision 1.17 2002/08/17 09:23:42 florian
  2364. * first part of procinfo rewrite
  2365. Revision 1.16 2002/08/06 20:55:23 florian
  2366. * first part of ppc calling conventions fix
  2367. Revision 1.15 2002/08/05 18:27:48 carl
  2368. + more more more documentation
  2369. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  2370. Revision 1.14 2002/08/04 19:06:41 carl
  2371. + added generic exception support (still does not work!)
  2372. + more documentation
  2373. Revision 1.13 2002/07/07 09:52:32 florian
  2374. * powerpc target fixed, very simple units can be compiled
  2375. * some basic stuff for better callparanode handling, far from being finished
  2376. Revision 1.12 2002/07/01 18:46:26 peter
  2377. * internal linker
  2378. * reorganized aasm layer
  2379. Revision 1.11 2002/05/18 13:34:17 peter
  2380. * readded missing revisions
  2381. Revision 1.10 2002/05/16 19:46:44 carl
  2382. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2383. + try to fix temp allocation (still in ifdef)
  2384. + generic constructor calls
  2385. + start of tassembler / tmodulebase class cleanup
  2386. Revision 1.8 2002/04/21 15:23:03 carl
  2387. + makeregsize
  2388. + changeregsize is now a local routine
  2389. Revision 1.7 2002/04/20 21:32:25 carl
  2390. + generic FPC_CHECKPOINTER
  2391. + first parameter offset in stack now portable
  2392. * rename some constants
  2393. + move some cpu stuff to other units
  2394. - remove unused constents
  2395. * fix stacksize for some targets
  2396. * fix generic size problems which depend now on EXTEND_SIZE constant
  2397. Revision 1.6 2002/04/15 19:03:31 carl
  2398. + reg2str -> std_reg2str()
  2399. Revision 1.5 2002/04/06 18:13:01 jonas
  2400. * several powerpc-related additions and fixes
  2401. Revision 1.4 2002/04/04 19:06:04 peter
  2402. * removed unused units
  2403. * use tlocation.size in cg.a_*loc*() routines
  2404. Revision 1.3 2002/04/02 17:11:29 peter
  2405. * tlocation,treference update
  2406. * LOC_CONSTANT added for better constant handling
  2407. * secondadd splitted in multiple routines
  2408. * location_force_reg added for loading a location to a register
  2409. of a specified size
  2410. * secondassignment parses now first the right and then the left node
  2411. (this is compatible with Kylix). This saves a lot of push/pop especially
  2412. with string operations
  2413. * adapted some routines to use the new cg methods
  2414. Revision 1.2 2002/04/01 19:24:25 jonas
  2415. * fixed different parameter name in interface and implementation
  2416. declaration of a method (only 1.0.x detected this)
  2417. Revision 1.1 2002/03/31 20:26:36 jonas
  2418. + a_loadfpu_* and a_loadmm_* methods in tcg
  2419. * register allocation is now handled by a class and is mostly processor
  2420. independent (+rgobj.pas and i386/rgcpu.pas)
  2421. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  2422. * some small improvements and fixes to the optimizer
  2423. * some register allocation fixes
  2424. * some fpuvaroffset fixes in the unary minus node
  2425. * push/popusedregisters is now called rg.save/restoreusedregisters and
  2426. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  2427. also better optimizable)
  2428. * fixed and optimized register saving/restoring for new/dispose nodes
  2429. * LOC_FPU locations now also require their "register" field to be set to
  2430. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  2431. - list field removed of the tnode class because it's not used currently
  2432. and can cause hard-to-find bugs
  2433. }