rgobj.pas 73 KB

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