rgobj.pas 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283
  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;size:Tcgsize) : 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. {$warning Size for FPU reg is maybe not correct}
  641. getexplicitregisterfpu:=getregisterfpu(list,OS_F32);
  642. end;
  643. function trgobj.getregisterfpu(list: taasmoutput;size:Tcgsize) : tregister;
  644. begin
  645. if countunusedregsfpu=0 then
  646. internalerror(10);
  647. result := getregistergen(list,firstsavefpureg,lastsavefpureg,
  648. unusedregsfpu,countunusedregsfpu);
  649. end;
  650. procedure trgobj.ungetregisterfpu(list : taasmoutput; r : tregister);
  651. begin
  652. ungetregistergen(list,r,usableregsfpu,unusedregsfpu,
  653. countunusedregsfpu);
  654. end;
  655. function trgobj.getregistermm(list: taasmoutput) : tregister;
  656. begin
  657. if countunusedregsmm=0 then
  658. internalerror(10);
  659. result := getregistergen(list,firstsavemmreg,lastsavemmreg,
  660. unusedregsmm,countunusedregsmm);
  661. end;
  662. procedure trgobj.ungetregistermm(list: taasmoutput; r: tregister);
  663. begin
  664. ungetregistergen(list,r,usableregsmm,unusedregsmm,
  665. countunusedregsmm);
  666. end;
  667. function trgobj.getaddressregister(list:Taasmoutput): tregister;
  668. begin
  669. {An address register is OS_INT per definition.}
  670. result := getregisterint(list,OS_INT);
  671. end;
  672. procedure trgobj.ungetaddressregister(list: taasmoutput; r: tregister);
  673. begin
  674. ungetregisterint(list,r);
  675. end;
  676. function trgobj.isaddressregister(reg: tregister): boolean;
  677. begin
  678. if reg.number<>0 then; { remove warning }
  679. result := true;
  680. end;
  681. procedure trgobj.ungetregister(list: taasmoutput; r : tregister);
  682. begin
  683. if r.enum=R_NO then
  684. exit;
  685. if r.enum>lastreg then
  686. internalerror(200301081);
  687. if r.enum in intregs then
  688. ungetregisterint(list,r)
  689. else if r.enum in fpuregs then
  690. ungetregisterfpu(list,r)
  691. else if r.enum in mmregs then
  692. ungetregistermm(list,r)
  693. else if r.enum in addrregs then
  694. ungetaddressregister(list,r)
  695. else internalerror(2002070602);
  696. end;
  697. procedure Trgobj.cleartempgen;
  698. {$ifdef newra}
  699. var i:Tsuperregister;
  700. {$endif newra}
  701. begin
  702. {$ifndef newra}
  703. countunusedregsint:=countusableregsint;
  704. {$endif}
  705. countunusedregsfpu:=countusableregsfpu;
  706. countunusedregsmm:=countusableregsmm;
  707. lastintreg:=0;
  708. maxintreg:=first_imreg;
  709. {$ifdef newra}
  710. unusedregsint:=[0..255];
  711. {$else}
  712. unusedregsint:=usableregsint;
  713. {$endif}
  714. unusedregsfpu:=usableregsfpu;
  715. unusedregsmm:=usableregsmm;
  716. {$ifdef newra}
  717. for i:=low(Tsuperregister) to high(Tsuperregister) do
  718. begin
  719. if igraph.adjlist[i]<>nil then
  720. dispose(igraph.adjlist[i]);
  721. if movelist[i]<>nil then
  722. dispose(movelist[i]);
  723. end;
  724. fillchar(movelist,sizeof(movelist),0);
  725. fillchar(igraph,sizeof(igraph),0);
  726. fillchar(degree,sizeof(degree),0);
  727. worklist_moves.clear;
  728. {$endif}
  729. end;
  730. procedure trgobj.ungetreference(list : taasmoutput; const ref : treference);
  731. begin
  732. if ref.base.number<>NR_NO then
  733. ungetregisterint(list,ref.base);
  734. if ref.index.number<>NR_NO then
  735. ungetregisterint(list,ref.index);
  736. end;
  737. procedure trgobj.saveintregvars(list:Taasmoutput;const s:Tsupregset);
  738. var r:Tsuperregister;
  739. hr: tregister;
  740. begin
  741. if not(cs_regalloc in aktglobalswitches) then
  742. exit;
  743. for r:=firstsaveintreg to lastsaveintreg do
  744. if (r in is_reg_var_int) and
  745. (r in s) then
  746. begin
  747. hr.number:=r shl 8;
  748. hr.enum:=R_INTREGISTER;
  749. store_regvar(list,hr);
  750. end;
  751. end;
  752. procedure trgobj.saveotherregvars(list: taasmoutput; const s: tregisterset);
  753. var
  754. r: Tregister;
  755. begin
  756. if not(cs_regalloc in aktglobalswitches) then
  757. exit;
  758. if firstsavefpureg <> R_NO then
  759. for r.enum := firstsavefpureg to lastsavefpureg do
  760. if is_reg_var_other[r.enum] and
  761. (r.enum in s) then
  762. store_regvar(list,r);
  763. if firstsavemmreg <> R_NO then
  764. for r.enum := firstsavemmreg to lastsavemmreg do
  765. if is_reg_var_other[r.enum] and
  766. (r.enum in s) then
  767. store_regvar(list,r);
  768. end;
  769. procedure trgobj.saveusedintregisters(list:Taasmoutput;
  770. var saved:Tpushedsavedint;
  771. const s:Tsupregset);
  772. var r:Tsuperregister;
  773. r2:Tregister;
  774. hr : treference;
  775. begin
  776. usedintinproc:=usedintinproc+s;
  777. for r:=firstsaveintreg to lastsaveintreg do
  778. begin
  779. saved[r].ofs:=reg_not_saved;
  780. { if the register is used by the calling subroutine and if }
  781. { it's not a regvar (those are handled separately) }
  782. if not (r in is_reg_var_int) and
  783. (r in s) and
  784. { and is present in use }
  785. not(r in unusedregsint) then
  786. begin
  787. { then save it }
  788. tg.GetTemp(list,sizeof(aword),tt_persistent,hr);
  789. saved[r].ofs:=hr.offset;
  790. r2.enum:=R_INTREGISTER;
  791. r2.number:=r shl 8 or R_SUBWHOLE;
  792. cg.a_load_reg_ref(list,OS_INT,r2,hr);
  793. cg.a_reg_dealloc(list,r2);
  794. include(unusedregsint,r);
  795. {$ifndef newra}
  796. inc(countunusedregsint);
  797. {$endif}
  798. end;
  799. end;
  800. {$ifdef TEMPREGDEBUG}
  801. testregisters32;
  802. {$endif TEMPREGDEBUG}
  803. end;
  804. procedure trgobj.saveusedotherregisters(list: taasmoutput;
  805. var saved : tpushedsavedother; const s: tregisterset);
  806. var
  807. r : tregister;
  808. hr : treference;
  809. begin
  810. usedinproc:=usedinproc + s;
  811. { don't try to save the fpu registers if not desired (e.g. for }
  812. { the 80x86) }
  813. if firstsavefpureg <> R_NO then
  814. for r.enum:=firstsavefpureg to lastsavefpureg do
  815. begin
  816. saved[r.enum].ofs:=reg_not_saved;
  817. { if the register is used by the calling subroutine and if }
  818. { it's not a regvar (those are handled separately) }
  819. if not is_reg_var_other[r.enum] and
  820. (r.enum in s) and
  821. { and is present in use }
  822. not(r.enum in unusedregsfpu) then
  823. begin
  824. { then save it }
  825. tg.GetTemp(list,extended_size,tt_persistent,hr);
  826. saved[r.enum].ofs:=hr.offset;
  827. cg.a_loadfpu_reg_ref(list,OS_FLOAT,r,hr);
  828. cg.a_reg_dealloc(list,r);
  829. include(unusedregsfpu,r.enum);
  830. inc(countunusedregsfpu);
  831. end;
  832. end;
  833. { don't save the vector registers if there's no support for them }
  834. if firstsavemmreg <> R_NO then
  835. for r.enum:=firstsavemmreg to lastsavemmreg do
  836. begin
  837. saved[r.enum].ofs:=reg_not_saved;
  838. { if the register is in use and if it's not a regvar (those }
  839. { are handled separately), save it }
  840. if not is_reg_var_other[r.enum] and
  841. (r.enum in s) and
  842. { and is present in use }
  843. not(r.enum in unusedregsmm) then
  844. begin
  845. { then save it }
  846. tg.GetTemp(list,mmreg_size,tt_persistent,hr);
  847. saved[r.enum].ofs:=hr.offset;
  848. cg.a_loadmm_reg_ref(list,r,hr);
  849. cg.a_reg_dealloc(list,r);
  850. include(unusedregsmm,r.enum);
  851. inc(countunusedregsmm);
  852. end;
  853. end;
  854. {$ifdef TEMPREGDEBUG}
  855. testregisters32;
  856. {$endif TEMPREGDEBUG}
  857. end;
  858. procedure trgobj.restoreusedintregisters(list:Taasmoutput;
  859. const saved:Tpushedsavedint);
  860. var r:Tsuperregister;
  861. r2:Tregister;
  862. hr:Treference;
  863. begin
  864. for r:=lastsaveintreg downto firstsaveintreg do
  865. begin
  866. if saved[r].ofs <> reg_not_saved then
  867. begin
  868. r2.enum:=R_INTREGISTER;
  869. r2.number:=NR_FRAME_POINTER_REG;
  870. reference_reset_base(hr,r2,saved[r].ofs);
  871. r2.enum:=R_INTREGISTER;
  872. r2.number:=r shl 8 or R_SUBWHOLE;
  873. cg.a_reg_alloc(list,r2);
  874. cg.a_load_ref_reg(list,OS_INT,hr,r2);
  875. if not (r in unusedregsint) then
  876. { internalerror(10)
  877. in n386cal we always save/restore the reg *state*
  878. using save/restoreunusedstate -> the current state
  879. may not be real (JM) }
  880. else
  881. begin
  882. {$ifndef newra}
  883. dec(countunusedregsint);
  884. {$endif}
  885. exclude(unusedregsint,r);
  886. end;
  887. tg.UnGetTemp(list,hr);
  888. end;
  889. end;
  890. {$ifdef TEMPREGDEBUG}
  891. testregisters32;
  892. {$endif TEMPREGDEBUG}
  893. end;
  894. procedure trgobj.restoreusedotherregisters(list : taasmoutput;
  895. const saved : tpushedsavedother);
  896. var
  897. r,r2 : tregister;
  898. hr : treference;
  899. begin
  900. if firstsavemmreg <> R_NO then
  901. for r.enum:=lastsavemmreg downto firstsavemmreg do
  902. begin
  903. if saved[r.enum].ofs <> reg_not_saved then
  904. begin
  905. r2.enum:=R_INTREGISTER;
  906. r2.number:=NR_FRAME_POINTER_REG;
  907. reference_reset_base(hr,r2,saved[r.enum].ofs);
  908. cg.a_reg_alloc(list,r);
  909. cg.a_loadmm_ref_reg(list,hr,r);
  910. if not (r.enum in unusedregsmm) then
  911. { internalerror(10)
  912. in n386cal we always save/restore the reg *state*
  913. using save/restoreunusedstate -> the current state
  914. may not be real (JM) }
  915. else
  916. begin
  917. dec(countunusedregsmm);
  918. exclude(unusedregsmm,r.enum);
  919. end;
  920. tg.UnGetTemp(list,hr);
  921. end;
  922. end;
  923. if firstsavefpureg <> R_NO then
  924. for r.enum:=lastsavefpureg downto firstsavefpureg do
  925. begin
  926. if saved[r.enum].ofs <> reg_not_saved then
  927. begin
  928. r2.enum:=R_INTREGISTER;
  929. r2.number:=NR_FRAME_POINTER_REG;
  930. reference_reset_base(hr,r2,saved[r.enum].ofs);
  931. cg.a_reg_alloc(list,r);
  932. cg.a_loadfpu_ref_reg(list,OS_FLOAT,hr,r);
  933. if not (r.enum in unusedregsfpu) then
  934. { internalerror(10)
  935. in n386cal we always save/restore the reg *state*
  936. using save/restoreunusedstate -> the current state
  937. may not be real (JM) }
  938. else
  939. begin
  940. dec(countunusedregsfpu);
  941. exclude(unusedregsfpu,r.enum);
  942. end;
  943. tg.UnGetTemp(list,hr);
  944. end;
  945. end;
  946. {$ifdef TEMPREGDEBUG}
  947. testregisters32;
  948. {$endif TEMPREGDEBUG}
  949. end;
  950. procedure trgobj.incrementintregisterpushed(const s:Tsupregset);
  951. var regi:Tsuperregister;
  952. begin
  953. {$ifdef i386}
  954. for regi:=firstsaveintreg to lastsaveintreg do
  955. begin
  956. if (regi in s) then
  957. inc(reg_pushes_int[regi],t_times*2);
  958. end;
  959. {$endif i386}
  960. end;
  961. procedure trgobj.incrementotherregisterpushed(const s:Tregisterset);
  962. var
  963. regi : Toldregister;
  964. begin
  965. {$ifdef i386}
  966. if firstsavefpureg <> R_NO then
  967. for regi:=firstsavefpureg to lastsavefpureg do
  968. begin
  969. if (regi in s) then
  970. inc(reg_pushes_other[regi],t_times*2);
  971. end;
  972. if firstsavemmreg <> R_NO then
  973. for regi:=firstsavemmreg to lastsavemmreg do
  974. begin
  975. if (regi in s) then
  976. inc(reg_pushes_other[regi],t_times*2);
  977. end;
  978. {$endif i386}
  979. end;
  980. procedure trgobj.clearregistercount;
  981. begin
  982. fillchar(reg_pushes_int,sizeof(reg_pushes_int),0);
  983. fillchar(reg_pushes_other,sizeof(reg_pushes_other),0);
  984. {ifndef i386}
  985. { all used registers will have to be saved at the start and restored }
  986. { at the end, but otoh regpara's do not have to be saved to memory }
  987. { at the start (there is a move from regpara to regvar most of the }
  988. { time though) -> set cost to 100+20 }
  989. filldword(reg_pushes_int[firstsaveintreg],lastsaveintreg-firstsaveintreg+1,120);
  990. filldword(reg_pushes_other[firstsavefpureg],ord(lastsavefpureg)-ord(firstsavefpureg)+1,120);
  991. {endif not i386}
  992. fillchar(is_reg_var_other,sizeof(is_reg_var_other),false);
  993. is_reg_var_int:=[];
  994. fillchar(regvar_loaded_other,sizeof(regvar_loaded_other),false);
  995. regvar_loaded_int:=[];
  996. end;
  997. procedure trgobj.resetusableregisters;
  998. begin
  999. { initialize fields with constant values from cpubase }
  1000. countusableregsint := cpubase.c_countusableregsint;
  1001. countusableregsfpu := cpubase.c_countusableregsfpu;
  1002. countusableregsmm := cpubase.c_countusableregsmm;
  1003. usableregsint := cpubase.usableregsint;
  1004. usableregsfpu := cpubase.usableregsfpu;
  1005. usableregsmm := cpubase.usableregsmm;
  1006. clearregistercount;
  1007. end;
  1008. procedure trgobj.makeregvarint(reg:Tsuperregister);
  1009. begin
  1010. dec(countusableregsint);
  1011. {$ifndef newra}
  1012. dec(countunusedregsint);
  1013. {$endif}
  1014. exclude(usableregsint,reg);
  1015. exclude(unusedregsint,reg);
  1016. include(is_reg_var_int,reg);
  1017. {$ifndef i386}
  1018. include(usedintbyproc,reg);
  1019. {$endif not i386}
  1020. end;
  1021. procedure trgobj.makeregvarother(reg: tregister);
  1022. begin
  1023. if reg.enum>lastreg then
  1024. internalerror(200301081);
  1025. if reg.enum in intregs then
  1026. internalerror(200301151)
  1027. else if reg.enum in fpuregs then
  1028. begin
  1029. dec(countusableregsfpu);
  1030. dec(countunusedregsfpu);
  1031. exclude(usableregsfpu,reg.enum);
  1032. exclude(unusedregsfpu,reg.enum);
  1033. {$ifndef i386}
  1034. include(usedbyproc,reg.enum);
  1035. {$endif not i386}
  1036. end
  1037. else if reg.enum in mmregs then
  1038. begin
  1039. dec(countusableregsmm);
  1040. dec(countunusedregsmm);
  1041. exclude(usableregsmm,reg.enum);
  1042. exclude(unusedregsmm,reg.enum);
  1043. {$ifndef i386}
  1044. include(usedbyproc,reg.enum);
  1045. {$endif not i386}
  1046. end;
  1047. is_reg_var_other[reg.enum]:=true;
  1048. end;
  1049. {$ifdef TEMPREGDEBUG}
  1050. procedure trgobj.testregisters;
  1051. var
  1052. r: tregister;
  1053. test : byte;
  1054. begin
  1055. test:=0;
  1056. for r := firstsaveintreg to lastsaveintreg do
  1057. inc(test,ord(r in unusedregsint));
  1058. if test<>countunusedregsint then
  1059. internalerror(10);
  1060. end;
  1061. {$endif TEMPREGDEBUG}
  1062. procedure trgobj.saveStateForInline(var state: pointer);
  1063. begin
  1064. new(psavedstate(state));
  1065. psavedstate(state)^.unusedregsint := unusedregsint;
  1066. psavedstate(state)^.usableregsint := usableregsint;
  1067. psavedstate(state)^.unusedregsfpu := unusedregsfpu;
  1068. psavedstate(state)^.usableregsfpu := usableregsfpu;
  1069. psavedstate(state)^.unusedregsmm := unusedregsmm;
  1070. psavedstate(state)^.usableregsmm := usableregsmm;
  1071. {$ifndef newra}
  1072. psavedstate(state)^.countunusedregsint := countunusedregsint;
  1073. {$endif}
  1074. psavedstate(state)^.countunusedregsfpu := countunusedregsfpu;
  1075. psavedstate(state)^.countunusedregsmm := countunusedregsmm;
  1076. psavedstate(state)^.countusableregsint := countusableregsint;
  1077. psavedstate(state)^.countusableregsfpu := countusableregsfpu;
  1078. psavedstate(state)^.countusableregsmm := countusableregsmm;
  1079. psavedstate(state)^.usedinproc := usedinproc;
  1080. psavedstate(state)^.usedbyproc := usedbyproc;
  1081. psavedstate(state)^.reg_pushes_int := reg_pushes_int;
  1082. psavedstate(state)^.reg_pushes_other := reg_pushes_other;
  1083. psavedstate(state)^.is_reg_var_int := is_reg_var_int;
  1084. psavedstate(state)^.is_reg_var_other := is_reg_var_other;
  1085. psavedstate(state)^.regvar_loaded_int := regvar_loaded_int;
  1086. psavedstate(state)^.regvar_loaded_other := regvar_loaded_other;
  1087. {$ifdef TEMPREGDEBUG}
  1088. psavedstate(state)^.reg_user := reg_user;
  1089. psavedstate(state)^.reg_releaser := reg_releaser;
  1090. {$endif TEMPREGDEBUG}
  1091. end;
  1092. procedure trgobj.restoreStateAfterInline(var state: pointer);
  1093. begin
  1094. unusedregsint := psavedstate(state)^.unusedregsint;
  1095. usableregsint := psavedstate(state)^.usableregsint;
  1096. unusedregsfpu := psavedstate(state)^.unusedregsfpu;
  1097. usableregsfpu := psavedstate(state)^.usableregsfpu;
  1098. unusedregsmm := psavedstate(state)^.unusedregsmm;
  1099. usableregsmm := psavedstate(state)^.usableregsmm;
  1100. {$ifndef newra}
  1101. countunusedregsint := psavedstate(state)^.countunusedregsint;
  1102. {$endif}
  1103. countunusedregsfpu := psavedstate(state)^.countunusedregsfpu;
  1104. countunusedregsmm := psavedstate(state)^.countunusedregsmm;
  1105. countusableregsint := psavedstate(state)^.countusableregsint;
  1106. countusableregsfpu := psavedstate(state)^.countusableregsfpu;
  1107. countusableregsmm := psavedstate(state)^.countusableregsmm;
  1108. usedinproc := psavedstate(state)^.usedinproc;
  1109. usedbyproc := psavedstate(state)^.usedbyproc;
  1110. reg_pushes_int := psavedstate(state)^.reg_pushes_int;
  1111. reg_pushes_other := psavedstate(state)^.reg_pushes_other;
  1112. is_reg_var_int := psavedstate(state)^.is_reg_var_int;
  1113. is_reg_var_other := psavedstate(state)^.is_reg_var_other;
  1114. regvar_loaded_other := psavedstate(state)^.regvar_loaded_other;
  1115. regvar_loaded_int := psavedstate(state)^.regvar_loaded_int;
  1116. {$ifdef TEMPREGDEBUG}
  1117. reg_user := psavedstate(state)^.reg_user;
  1118. reg_releaser := psavedstate(state)^.reg_releaser;
  1119. {$endif TEMPREGDEBUG}
  1120. dispose(psavedstate(state));
  1121. state := nil;
  1122. end;
  1123. procedure trgobj.saveUnusedState(var state: pointer);
  1124. begin
  1125. new(punusedstate(state));
  1126. punusedstate(state)^.unusedregsint := unusedregsint;
  1127. punusedstate(state)^.unusedregsfpu := unusedregsfpu;
  1128. punusedstate(state)^.unusedregsmm := unusedregsmm;
  1129. {$ifndef newra}
  1130. punusedstate(state)^.countunusedregsint := countunusedregsint;
  1131. {$endif}
  1132. punusedstate(state)^.countunusedregsfpu := countunusedregsfpu;
  1133. punusedstate(state)^.countunusedregsmm := countunusedregsmm;
  1134. end;
  1135. procedure trgobj.restoreUnusedState(var state: pointer);
  1136. begin
  1137. unusedregsint := punusedstate(state)^.unusedregsint;
  1138. unusedregsfpu := punusedstate(state)^.unusedregsfpu;
  1139. unusedregsmm := punusedstate(state)^.unusedregsmm;
  1140. {$ifndef newra}
  1141. countunusedregsint := punusedstate(state)^.countunusedregsint;
  1142. {$endif}
  1143. countunusedregsfpu := punusedstate(state)^.countunusedregsfpu;
  1144. countunusedregsmm := punusedstate(state)^.countunusedregsmm;
  1145. dispose(punusedstate(state));
  1146. state := nil;
  1147. end;
  1148. {$ifdef newra}
  1149. procedure Trgobj.add_edge(u,v:Tsuperregister);
  1150. {This procedure will add an edge to the virtual interference graph.}
  1151. procedure addadj(u,v:Tsuperregister);
  1152. begin
  1153. if igraph.adjlist[u]=nil then
  1154. begin
  1155. getmem(igraph.adjlist[u],16);
  1156. igraph.adjlist[u]^:='';
  1157. end
  1158. else if (length(igraph.adjlist[u]^) and 15)=15 then
  1159. reallocmem(igraph.adjlist[u],length(igraph.adjlist[u]^)+16);
  1160. igraph.adjlist[u]^:=igraph.adjlist[u]^+char(v);
  1161. end;
  1162. begin
  1163. if (u<>v) and not(v in igraph.bitmap[u]) then
  1164. begin
  1165. include(igraph.bitmap[u],v);
  1166. include(igraph.bitmap[v],u);
  1167. {Precoloured nodes are not stored in the interference graph.}
  1168. if not(u in [first_supreg..last_supreg]) then
  1169. begin
  1170. addadj(u,v);
  1171. inc(degree[u]);
  1172. end;
  1173. if not(v in [first_supreg..last_supreg]) then
  1174. begin
  1175. addadj(v,u);
  1176. inc(degree[v]);
  1177. end;
  1178. end;
  1179. end;
  1180. procedure Trgobj.add_edges_used(u:Tsuperregister);
  1181. var i:Tsuperregister;
  1182. begin
  1183. for i:=1 to 255 do
  1184. if not(i in unusedregsint) then
  1185. add_edge(u,i);
  1186. end;
  1187. {$ifdef ra_debug}
  1188. procedure Trgobj.writegraph;
  1189. {This procedure writes out the current interference graph in the
  1190. register allocator.}
  1191. var f:text;
  1192. i,j:Tsuperregister;
  1193. begin
  1194. assign(f,'igraph'+char(48+random(10))+char(48+random(10)));
  1195. rewrite(f);
  1196. writeln(f,'Interference graph');
  1197. writeln(f);
  1198. write(f,' ');
  1199. for i:=0 to 15 do
  1200. for j:=0 to 15 do
  1201. write(f,hexstr(i,1));
  1202. writeln(f);
  1203. write(f,' ');
  1204. for i:=0 to 15 do
  1205. write(f,'0123456789ABCDEF');
  1206. writeln(f);
  1207. for i:=0 to 255 do
  1208. begin
  1209. write(f,hexstr(i,2):4);
  1210. for j:=0 to 255 do
  1211. if j in igraph.bitmap[i] then
  1212. write(f,'*')
  1213. else
  1214. write(f,'-');
  1215. writeln(f);
  1216. end;
  1217. close(f);
  1218. end;
  1219. {$endif}
  1220. procedure Trgobj.add_to_movelist(u:Tsuperregister;data:Tlinkedlistitem);
  1221. begin
  1222. if movelist[u]=nil then
  1223. begin
  1224. getmem(movelist[u],64);
  1225. movelist[u]^.count:=0;
  1226. end
  1227. else if (movelist[u]^.count and 15)=15 then
  1228. reallocmem(movelist[u],(movelist[u]^.count+1)*4+64);
  1229. movelist[u]^.data[movelist[u]^.count]:=data;
  1230. inc(movelist[u]^.count);
  1231. end;
  1232. procedure Trgobj.add_move_instruction(instr:Taicpu);
  1233. {This procedure notifies a certain as a move instruction so the
  1234. register allocator can try to eliminate it.}
  1235. var i:Tmoveins;
  1236. ssupreg,dsupreg:Tsuperregister;
  1237. begin
  1238. i:=Tmoveins.create;
  1239. i.moveset:=ms_worklist_moves;
  1240. i.instruction:=instr;
  1241. worklist_moves.insert(i);
  1242. ssupreg:=instr.oper[0].reg.number shr 8;
  1243. add_to_movelist(ssupreg,i);
  1244. dsupreg:=instr.oper[1].reg.number shr 8;
  1245. add_to_movelist(dsupreg,i);
  1246. end;
  1247. function Trgobj.move_related(n:Tsuperregister):boolean;
  1248. var i:cardinal;
  1249. begin
  1250. move_related:=false;
  1251. if movelist[n]<>nil then
  1252. begin
  1253. for i:=0 to movelist[n]^.count-1 do
  1254. if Tmoveins(movelist[n]^.data[i]).moveset in
  1255. [ms_worklist_moves,ms_active_moves] then
  1256. begin
  1257. move_related:=true;
  1258. break;
  1259. end;
  1260. end;
  1261. end;
  1262. procedure Trgobj.make_work_list;
  1263. var n:Tsuperregister;
  1264. begin
  1265. for n:=first_imreg to maxintreg do
  1266. if degree[n]>cpu_registers then
  1267. spillworklist:=spillworklist+char(n)
  1268. else if move_related(n) then
  1269. freezeworklist:=freezeworklist+char(n)
  1270. else
  1271. simplifyworklist:=simplifyworklist+char(n);
  1272. end;
  1273. procedure Trgobj.prepare_colouring;
  1274. begin
  1275. make_work_list;
  1276. active_moves:=Tlinkedlist.create;
  1277. frozen_moves:=Tlinkedlist.create;
  1278. coalesced_moves:=Tlinkedlist.create;
  1279. constrained_moves:=Tlinkedlist.create;
  1280. fillchar(alias,sizeof(alias),0);
  1281. coalescednodes:='';
  1282. selectstack:='';
  1283. end;
  1284. procedure Trgobj.enable_moves(n:Tsuperregister);
  1285. var m:Tlinkedlistitem;
  1286. i:cardinal;
  1287. begin
  1288. if movelist[n]<>nil then
  1289. for i:=0 to movelist[n]^.count-1 do
  1290. begin
  1291. m:=movelist[n]^.data[i];
  1292. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  1293. begin
  1294. if Tmoveins(m).moveset=ms_active_moves then
  1295. begin
  1296. {Move m from the set active_moves to the set worklist_moves.}
  1297. active_moves.remove(m);
  1298. Tmoveins(m).moveset:=ms_worklist_moves;
  1299. worklist_moves.concat(m);
  1300. end;
  1301. end;
  1302. end;
  1303. end;
  1304. procedure Trgobj.decrement_degree(m:Tsuperregister);
  1305. var adj:Pstring;
  1306. d:byte;
  1307. i:byte;
  1308. n:char;
  1309. begin
  1310. d:=degree[m];
  1311. dec(degree[m]);
  1312. if d=cpu_registers then
  1313. begin
  1314. {Enable moves for m.}
  1315. enable_moves(m);
  1316. {Enable moves for adjacent.}
  1317. adj:=igraph.adjlist[m];
  1318. if adj<>nil then
  1319. for i:=1 to length(adj^) do
  1320. begin
  1321. n:=adj^[i];
  1322. if (pos(n,selectstack) or pos(n,coalescednodes))=0 then
  1323. enable_moves(Tsuperregister(n));
  1324. end;
  1325. {In case the node is in the spillworklist, delete it.}
  1326. delete(spillworklist,pos(char(m),spillworklist),1);
  1327. if move_related(m) then
  1328. freezeworklist:=freezeworklist+char(m)
  1329. else
  1330. simplifyworklist:=simplifyworklist+char(m);
  1331. end;
  1332. end;
  1333. procedure Trgobj.simplify;
  1334. var adj:Pstring;
  1335. i,min,p:byte;
  1336. m:char;
  1337. n:Tsuperregister;
  1338. begin
  1339. {We the element with the least interferences out of the
  1340. simplifyworklist.}
  1341. min:=$ff;
  1342. p:=1;
  1343. for i:=1 to length(simplifyworklist) do
  1344. begin
  1345. adj:=igraph.adjlist[Tsuperregister(simplifyworklist[i])];
  1346. if adj=nil then
  1347. begin
  1348. min:=0;
  1349. break; {We won't find smaller ones.}
  1350. end
  1351. else
  1352. if length(adj^)<min then
  1353. begin
  1354. min:=length(adj^);
  1355. if min=0 then
  1356. break; {We won't find smaller ones.}
  1357. p:=i;
  1358. end;
  1359. end;
  1360. n:=Tsuperregister(simplifyworklist[p]);
  1361. delete(simplifyworklist,p,1);
  1362. {Push it on the selectstack.}
  1363. selectstack:=selectstack+char(n);
  1364. adj:=igraph.adjlist[n];
  1365. if adj<>nil then
  1366. for i:=1 to length(adj^) do
  1367. begin
  1368. m:=adj^[i];
  1369. if (pos(m,selectstack) or pos(m,coalescednodes))=0 then
  1370. decrement_degree(Tsuperregister(m));
  1371. end;
  1372. end;
  1373. function Trgobj.get_alias(n:Tsuperregister):Tsuperregister;
  1374. begin
  1375. while pos(char(n),coalescednodes)<>0 do
  1376. n:=alias[n];
  1377. get_alias:=n;
  1378. end;
  1379. procedure Trgobj.add_worklist(u:Tsuperregister);
  1380. begin
  1381. if not(u in [first_supreg..last_supreg]) and not move_related(u) and
  1382. (degree[u]<cpu_registers) then
  1383. begin
  1384. delete(freezeworklist,pos(char(u),freezeworklist),1);
  1385. simplifyworklist:=simplifyworklist+char(u);
  1386. end;
  1387. end;
  1388. function Trgobj.adjacent_ok(u,v:Tsuperregister):boolean;
  1389. {Check wether u and v should be coalesced. u is precoloured.}
  1390. function ok(t,r:Tsuperregister):boolean;
  1391. begin
  1392. ok:=(degree[t]<cpu_registers) or
  1393. (t in [first_supreg..last_supreg]) or
  1394. (r in igraph.bitmap[t]);
  1395. end;
  1396. var adj:Pstring;
  1397. i:byte;
  1398. t:char;
  1399. begin
  1400. adjacent_ok:=true;
  1401. adj:=igraph.adjlist[v];
  1402. if adj<>nil then
  1403. for i:=1 to length(adj^) do
  1404. begin
  1405. t:=adj^[i];
  1406. if (pos(t,selectstack) or pos(t,coalescednodes))=0 then
  1407. if not ok(Tsuperregister(t),u) then
  1408. begin
  1409. adjacent_ok:=false;
  1410. break;
  1411. end;
  1412. end;
  1413. end;
  1414. function Trgobj.conservative(u,v:Tsuperregister):boolean;
  1415. var adj:Pstring;
  1416. done:set of char; {To prevent that we count nodes twice.}
  1417. i,k:byte;
  1418. n:char;
  1419. begin
  1420. k:=0;
  1421. done:=[];
  1422. adj:=igraph.adjlist[u];
  1423. if adj<>nil then
  1424. for i:=1 to length(adj^) do
  1425. begin
  1426. n:=adj^[i];
  1427. if (pos(n,selectstack) or pos(n,coalescednodes))=0 then
  1428. begin
  1429. include(done,n);
  1430. if degree[Tsuperregister(n)]>=cpu_registers then
  1431. inc(k);
  1432. end;
  1433. end;
  1434. adj:=igraph.adjlist[v];
  1435. if adj<>nil then
  1436. for i:=1 to length(adj^) do
  1437. begin
  1438. n:=adj^[i];
  1439. if ((pos(n,selectstack) or pos(n,coalescednodes))=0) and
  1440. not (n in done) and
  1441. (degree[Tsuperregister(n)]>=cpu_registers) then
  1442. inc(k);
  1443. end;
  1444. conservative:=(k<cpu_registers);
  1445. end;
  1446. procedure Trgobj.combine(u,v:Tsuperregister);
  1447. var add:boolean;
  1448. adj:Pstring;
  1449. i,p:byte;
  1450. n,o:cardinal;
  1451. t:char;
  1452. begin
  1453. p:=pos(char(v),freezeworklist);
  1454. if p<>0 then
  1455. delete(freezeworklist,p,1)
  1456. else
  1457. delete(spillworklist,pos(char(v),spillworklist),1);
  1458. coalescednodes:=coalescednodes+char(v);
  1459. alias[v]:=u;
  1460. {Combine both movelists. Since the movelists are sets, only add
  1461. elements that are not already present.}
  1462. for n:=0 to movelist[v]^.count-1 do
  1463. begin
  1464. add:=true;
  1465. for o:=0 to movelist[u]^.count-1 do
  1466. if movelist[u]^.data[o]=movelist[v]^.data[n] then
  1467. begin
  1468. add:=false;
  1469. break;
  1470. end;
  1471. if add then
  1472. add_to_movelist(u,movelist[v]^.data[n]);
  1473. end;
  1474. enable_moves(v);
  1475. adj:=igraph.adjlist[v];
  1476. if adj<>nil then
  1477. for i:=1 to length(adj^) do
  1478. begin
  1479. t:=adj^[i];
  1480. if (pos(t,selectstack) or pos(t,coalescednodes))=0 then
  1481. begin
  1482. add_edge(Tsuperregister(t),u);
  1483. decrement_degree(Tsuperregister(t));
  1484. end;
  1485. end;
  1486. p:=pos(char(u),freezeworklist);
  1487. if (degree[u]>=cpu_registers) and (p<>0) then
  1488. begin
  1489. delete(freezeworklist,p,1);
  1490. spillworklist:=spillworklist+char(u);
  1491. end;
  1492. end;
  1493. procedure Trgobj.coalesce;
  1494. var m:Tmoveins;
  1495. x,y,u,v:Tsuperregister;
  1496. begin
  1497. m:=Tmoveins(worklist_moves.getfirst);
  1498. x:=get_alias(m.instruction.oper[0].reg.number shr 8);
  1499. y:=get_alias(m.instruction.oper[1].reg.number shr 8);
  1500. if y in [first_supreg..last_supreg] then
  1501. begin
  1502. u:=y;
  1503. v:=x;
  1504. end
  1505. else
  1506. begin
  1507. u:=x;
  1508. v:=y;
  1509. end;
  1510. if (u=v) then
  1511. begin
  1512. m.moveset:=ms_coalesced_moves; {Already coalesced.}
  1513. coalesced_moves.insert(m);
  1514. add_worklist(u);
  1515. end
  1516. {Do u and v interfere? In that case the move is constrained. Two
  1517. precoloured nodes interfere allways. If v is precoloured, by the above
  1518. code u is precoloured, thus interference...}
  1519. else if (v in [first_supreg..last_supreg]) or (u in igraph.bitmap[v]) then
  1520. begin
  1521. m.moveset:=ms_constrained_moves; {Cannot coalesce yet...}
  1522. constrained_moves.insert(m);
  1523. add_worklist(u);
  1524. add_worklist(v);
  1525. end
  1526. {Next test: is it possible and a good idea to coalesce??}
  1527. else if ((u in [first_supreg..last_supreg]) and adjacent_ok(u,v)) or
  1528. (not(u in [first_supreg..last_supreg]) and conservative(u,v)) then
  1529. begin
  1530. m.moveset:=ms_coalesced_moves; {Move coalesced!}
  1531. coalesced_moves.insert(m);
  1532. combine(u,v);
  1533. add_worklist(u);
  1534. end
  1535. else
  1536. begin
  1537. m.moveset:=ms_active_moves;
  1538. active_moves.insert(m);
  1539. end;
  1540. end;
  1541. procedure Trgobj.freeze_moves(u:Tsuperregister);
  1542. var i:cardinal;
  1543. m:Tlinkedlistitem;
  1544. v,x,y:Tsuperregister;
  1545. begin
  1546. if movelist[u]<>nil then
  1547. for i:=0 to movelist[u]^.count-1 do
  1548. begin
  1549. m:=movelist[u]^.data[i];
  1550. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  1551. begin
  1552. x:=Tmoveins(m).instruction.oper[0].reg.number shr 8;
  1553. y:=Tmoveins(m).instruction.oper[1].reg.number shr 8;
  1554. if get_alias(y)=get_alias(u) then
  1555. v:=get_alias(x)
  1556. else
  1557. v:=get_alias(y);
  1558. {Move m from active_moves/worklist_moves to frozen_moves.}
  1559. if Tmoveins(m).moveset=ms_active_moves then
  1560. active_moves.remove(m)
  1561. else
  1562. worklist_moves.remove(m);
  1563. Tmoveins(m).moveset:=ms_frozen_moves;
  1564. frozen_moves.insert(m);
  1565. if not(move_related(v)) and (degree[v]<cpu_registers) then
  1566. begin
  1567. delete(freezeworklist,pos(char(v),freezeworklist),1);
  1568. simplifyworklist:=simplifyworklist+char(v);
  1569. end;
  1570. end;
  1571. end;
  1572. end;
  1573. procedure Trgobj.freeze;
  1574. var n:Tsuperregister;
  1575. begin
  1576. {We need to take a random element out of the freezeworklist. We take
  1577. the last element. Dirty code!}
  1578. n:=Tsuperregister(freezeworklist[byte(freezeworklist[0])]);
  1579. dec(freezeworklist[0]);
  1580. {Add it to the simplifyworklist.}
  1581. simplifyworklist:=simplifyworklist+char(n);
  1582. freeze_moves(n);
  1583. end;
  1584. procedure Trgobj.select_spill;
  1585. var n:char;
  1586. begin
  1587. {This code is WAY too naive. We need not to select just a register, but
  1588. the register that is used the least...}
  1589. n:=spillworklist[byte(spillworklist[0])];
  1590. dec(spillworklist[0]);
  1591. simplifyworklist:=simplifyworklist+n;
  1592. freeze_moves(Tsuperregister(n));
  1593. end;
  1594. procedure Trgobj.assign_colours;
  1595. {Assign_colours assigns the actual colours to the registers.}
  1596. var adj:Pstring;
  1597. i,j,k:byte;
  1598. n,a:Tsuperregister;
  1599. adj_colours,colourednodes:set of Tsuperregister;
  1600. w:char;
  1601. begin
  1602. spillednodes:='';
  1603. {Colour the cpu registers...}
  1604. colourednodes:=[first_supreg..last_supreg];
  1605. for i:=first_supreg to last_supreg do
  1606. colour[i]:=i;
  1607. {Now colour the imaginary registers on the select-stack.}
  1608. for i:=length(selectstack) downto 1 do
  1609. begin
  1610. n:=Tsuperregister(selectstack[i]);
  1611. {Create a list of colours that we cannot assign to n.}
  1612. adj_colours:=[];
  1613. adj:=igraph.adjlist[n];
  1614. if adj<>nil then
  1615. for j:=1 to length(adj^) do
  1616. begin
  1617. w:=adj^[j];
  1618. a:=get_alias(Tsuperregister(w));
  1619. if a in colourednodes then
  1620. include(adj_colours,colour[a]);
  1621. end;
  1622. {Assume a spill by default...}
  1623. spillednodes:=spillednodes+char(n);
  1624. {Search for a colour not in this list.}
  1625. for k:=1 to cpu_registers do
  1626. if not(k in adj_colours) then
  1627. begin
  1628. colour[n]:=k;
  1629. dec(spillednodes[0]); {Colour found: no spill.}
  1630. include(colourednodes,n);
  1631. break;
  1632. end;
  1633. end;
  1634. {Finally colour the nodes that were coalesced.}
  1635. for i:=1 to length(coalescednodes) do
  1636. begin
  1637. n:=Tsuperregister(coalescednodes[i]);
  1638. colour[n]:=colour[get_alias(n)];
  1639. end;
  1640. for i:=first_imreg to maxintreg do
  1641. writeln(i:4,' ',colour[i]:4)
  1642. end;
  1643. procedure Trgobj.colour_registers;
  1644. begin
  1645. repeat
  1646. if length(simplifyworklist)<>0 then
  1647. simplify
  1648. else if not(worklist_moves.empty) then
  1649. coalesce
  1650. else if length(freezeworklist)<>0 then
  1651. freeze
  1652. else if length(spillworklist)<>0 then
  1653. select_spill;
  1654. until (length(simplifyworklist) or
  1655. byte(not(worklist_moves.empty)) or
  1656. length(freezeworklist) or
  1657. length(spillworklist)
  1658. )=0;
  1659. assign_colours;
  1660. end;
  1661. procedure Trgobj.epilogue_colouring;
  1662. begin
  1663. active_moves.destroy;
  1664. active_moves:=nil;
  1665. frozen_moves.destroy;
  1666. frozen_moves:=nil;
  1667. coalesced_moves.destroy;
  1668. coalesced_moves:=nil;
  1669. constrained_moves.destroy;
  1670. constrained_moves:=nil;
  1671. end;
  1672. {$endif newra}
  1673. {****************************************************************************
  1674. TReference
  1675. ****************************************************************************}
  1676. procedure reference_reset(var ref : treference);
  1677. begin
  1678. FillChar(ref,sizeof(treference),0);
  1679. ref.base.enum:=R_INTREGISTER;
  1680. ref.index.enum:=R_INTREGISTER;
  1681. {$ifdef i386}
  1682. ref.segment.enum:=R_INTREGISTER;
  1683. {$endif}
  1684. end;
  1685. procedure reference_reset_old(var ref : treference);
  1686. begin
  1687. FillChar(ref,sizeof(treference),0);
  1688. end;
  1689. procedure reference_reset_base(var ref : treference;base : tregister;offset : longint);
  1690. begin
  1691. reference_reset(ref);
  1692. ref.base:=base;
  1693. ref.offset:=offset;
  1694. end;
  1695. procedure reference_reset_symbol(var ref : treference;sym : tasmsymbol;offset : longint);
  1696. begin
  1697. reference_reset(ref);
  1698. ref.symbol:=sym;
  1699. ref.offset:=offset;
  1700. end;
  1701. procedure reference_release(list: taasmoutput; const ref : treference);
  1702. begin
  1703. rg.ungetreference(list,ref);
  1704. end;
  1705. function references_equal(sref : treference;dref : treference):boolean;
  1706. begin
  1707. references_equal:=CompareByte(sref,dref,sizeof(treference))=0;
  1708. end;
  1709. { on most processors , this routine does nothing, overriden currently }
  1710. { only by 80x86 processor. }
  1711. function trgobj.makeregsize(reg: tregister; size: tcgsize): tregister;
  1712. begin
  1713. makeregsize := reg;
  1714. end;
  1715. {****************************************************************************
  1716. TLocation
  1717. ****************************************************************************}
  1718. procedure location_reset(var l : tlocation;lt:TCGLoc;lsize:TCGSize);
  1719. begin
  1720. FillChar(l,sizeof(tlocation),0);
  1721. l.loc:=lt;
  1722. l.size:=lsize;
  1723. case l.loc of
  1724. LOC_REGISTER,LOC_CREGISTER:
  1725. begin
  1726. l.register.enum:=R_INTREGISTER;
  1727. l.registerhigh.enum:=R_INTREGISTER;
  1728. end;
  1729. LOC_REFERENCE,LOC_CREFERENCE:
  1730. begin
  1731. l.reference.base.enum:=R_INTREGISTER;
  1732. l.reference.index.enum:=R_INTREGISTER;
  1733. {$ifdef i386}
  1734. l.reference.segment.enum:=R_INTREGISTER;
  1735. {$endif}
  1736. end;
  1737. end;
  1738. end;
  1739. procedure location_release(list: taasmoutput; const l : tlocation);
  1740. begin
  1741. case l.loc of
  1742. LOC_REGISTER,LOC_CREGISTER :
  1743. begin
  1744. rg.ungetregisterint(list,l.register);
  1745. if l.size in [OS_64,OS_S64] then
  1746. rg.ungetregisterint(list,l.registerhigh);
  1747. end;
  1748. LOC_CREFERENCE,LOC_REFERENCE :
  1749. rg.ungetreference(list, l.reference);
  1750. end;
  1751. end;
  1752. procedure location_freetemp(list:taasmoutput; const l : tlocation);
  1753. begin
  1754. if (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  1755. tg.ungetiftemp(list,l.reference);
  1756. end;
  1757. procedure location_copy(var destloc,sourceloc : tlocation);
  1758. begin
  1759. destloc:=sourceloc;
  1760. end;
  1761. procedure location_swap(var destloc,sourceloc : tlocation);
  1762. var
  1763. swapl : tlocation;
  1764. begin
  1765. swapl := destloc;
  1766. destloc := sourceloc;
  1767. sourceloc := swapl;
  1768. end;
  1769. initialization
  1770. ;
  1771. finalization
  1772. rg.free;
  1773. end.
  1774. {
  1775. $Log$
  1776. Revision 1.48 2003-06-01 21:38:06 peter
  1777. * getregisterfpu size parameter added
  1778. * op_const_reg size parameter added
  1779. * sparc updates
  1780. Revision 1.47 2003/05/31 20:31:11 jonas
  1781. * set inital costs of assigning a variable to a register to 120 for
  1782. non-i386, because the used register must be store to memory at the
  1783. start and loaded again at the end
  1784. Revision 1.46 2003/05/30 18:55:21 jonas
  1785. * fixed several regvar related bugs for non-i386. make cycle with -Or now
  1786. works for ppc
  1787. Revision 1.45 2003/05/30 12:36:13 jonas
  1788. * use as little different registers on the ppc until newra is released,
  1789. since every used register must be saved
  1790. Revision 1.44 2003/05/17 13:30:08 jonas
  1791. * changed tt_persistant to tt_persistent :)
  1792. * tempcreatenode now doesn't accept a boolean anymore for persistent
  1793. temps, but a ttemptype, so you can also create ansistring temps etc
  1794. Revision 1.43 2003/05/16 14:33:31 peter
  1795. * regvar fixes
  1796. Revision 1.42 2003/04/26 20:03:49 daniel
  1797. * Bug fix in simplify
  1798. Revision 1.41 2003/04/25 20:59:35 peter
  1799. * removed funcretn,funcretsym, function result is now in varsym
  1800. and aliases for result and function name are added using absolutesym
  1801. * vs_hidden parameter for funcret passed in parameter
  1802. * vs_hidden fixes
  1803. * writenode changed to printnode and released from extdebug
  1804. * -vp option added to generate a tree.log with the nodetree
  1805. * nicer printnode for statements, callnode
  1806. Revision 1.40 2003/04/25 08:25:26 daniel
  1807. * Ifdefs around a lot of calls to cleartempgen
  1808. * Fixed registers that are allocated but not freed in several nodes
  1809. * Tweak to register allocator to cause less spills
  1810. * 8-bit registers now interfere with esi,edi and ebp
  1811. Compiler can now compile rtl successfully when using new register
  1812. allocator
  1813. Revision 1.39 2003/04/23 20:23:06 peter
  1814. * compile fix for no-newra
  1815. Revision 1.38 2003/04/23 14:42:07 daniel
  1816. * Further register allocator work. Compiler now smaller with new
  1817. allocator than without.
  1818. * Somebody forgot to adjust ppu version number
  1819. Revision 1.37 2003/04/22 23:50:23 peter
  1820. * firstpass uses expectloc
  1821. * checks if there are differences between the expectloc and
  1822. location.loc from secondpass in EXTDEBUG
  1823. Revision 1.36 2003/04/22 10:09:35 daniel
  1824. + Implemented the actual register allocator
  1825. + Scratch registers unavailable when new register allocator used
  1826. + maybe_save/maybe_restore unavailable when new register allocator used
  1827. Revision 1.35 2003/04/21 19:16:49 peter
  1828. * count address regs separate
  1829. Revision 1.34 2003/04/17 16:48:21 daniel
  1830. * Added some code to keep track of move instructions in register
  1831. allocator
  1832. Revision 1.33 2003/04/17 07:50:24 daniel
  1833. * Some work on interference graph construction
  1834. Revision 1.32 2003/03/28 19:16:57 peter
  1835. * generic constructor working for i386
  1836. * remove fixed self register
  1837. * esi added as address register for i386
  1838. Revision 1.31 2003/03/11 21:46:24 jonas
  1839. * lots of new regallocator fixes, both in generic and ppc-specific code
  1840. (ppc compiler still can't compile the linux system unit though)
  1841. Revision 1.30 2003/03/09 21:18:59 olle
  1842. + added cutils to the uses clause
  1843. Revision 1.29 2003/03/08 20:36:41 daniel
  1844. + Added newra version of Ti386shlshrnode
  1845. + Added interference graph construction code
  1846. Revision 1.28 2003/03/08 13:59:16 daniel
  1847. * Work to handle new register notation in ag386nsm
  1848. + Added newra version of Ti386moddivnode
  1849. Revision 1.27 2003/03/08 10:53:48 daniel
  1850. * Created newra version of secondmul in n386add.pas
  1851. Revision 1.26 2003/03/08 08:59:07 daniel
  1852. + $define newra will enable new register allocator
  1853. + getregisterint will return imaginary registers with $newra
  1854. + -sr switch added, will skip register allocation so you can see
  1855. the direct output of the code generator before register allocation
  1856. Revision 1.25 2003/02/26 20:50:45 daniel
  1857. * Fixed ungetreference
  1858. Revision 1.24 2003/02/19 22:39:56 daniel
  1859. * Fixed a few issues
  1860. Revision 1.23 2003/02/19 22:00:14 daniel
  1861. * Code generator converted to new register notation
  1862. - Horribily outdated todo.txt removed
  1863. Revision 1.22 2003/02/02 19:25:54 carl
  1864. * Several bugfixes for m68k target (register alloc., opcode emission)
  1865. + VIS target
  1866. + Generic add more complete (still not verified)
  1867. Revision 1.21 2003/01/08 18:43:57 daniel
  1868. * Tregister changed into a record
  1869. Revision 1.20 2002/10/05 12:43:28 carl
  1870. * fixes for Delphi 6 compilation
  1871. (warning : Some features do not work under Delphi)
  1872. Revision 1.19 2002/08/23 16:14:49 peter
  1873. * tempgen cleanup
  1874. * tt_noreuse temp type added that will be used in genentrycode
  1875. Revision 1.18 2002/08/17 22:09:47 florian
  1876. * result type handling in tcgcal.pass_2 overhauled
  1877. * better tnode.dowrite
  1878. * some ppc stuff fixed
  1879. Revision 1.17 2002/08/17 09:23:42 florian
  1880. * first part of procinfo rewrite
  1881. Revision 1.16 2002/08/06 20:55:23 florian
  1882. * first part of ppc calling conventions fix
  1883. Revision 1.15 2002/08/05 18:27:48 carl
  1884. + more more more documentation
  1885. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  1886. Revision 1.14 2002/08/04 19:06:41 carl
  1887. + added generic exception support (still does not work!)
  1888. + more documentation
  1889. Revision 1.13 2002/07/07 09:52:32 florian
  1890. * powerpc target fixed, very simple units can be compiled
  1891. * some basic stuff for better callparanode handling, far from being finished
  1892. Revision 1.12 2002/07/01 18:46:26 peter
  1893. * internal linker
  1894. * reorganized aasm layer
  1895. Revision 1.11 2002/05/18 13:34:17 peter
  1896. * readded missing revisions
  1897. Revision 1.10 2002/05/16 19:46:44 carl
  1898. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1899. + try to fix temp allocation (still in ifdef)
  1900. + generic constructor calls
  1901. + start of tassembler / tmodulebase class cleanup
  1902. Revision 1.8 2002/04/21 15:23:03 carl
  1903. + makeregsize
  1904. + changeregsize is now a local routine
  1905. Revision 1.7 2002/04/20 21:32:25 carl
  1906. + generic FPC_CHECKPOINTER
  1907. + first parameter offset in stack now portable
  1908. * rename some constants
  1909. + move some cpu stuff to other units
  1910. - remove unused constents
  1911. * fix stacksize for some targets
  1912. * fix generic size problems which depend now on EXTEND_SIZE constant
  1913. Revision 1.6 2002/04/15 19:03:31 carl
  1914. + reg2str -> std_reg2str()
  1915. Revision 1.5 2002/04/06 18:13:01 jonas
  1916. * several powerpc-related additions and fixes
  1917. Revision 1.4 2002/04/04 19:06:04 peter
  1918. * removed unused units
  1919. * use tlocation.size in cg.a_*loc*() routines
  1920. Revision 1.3 2002/04/02 17:11:29 peter
  1921. * tlocation,treference update
  1922. * LOC_CONSTANT added for better constant handling
  1923. * secondadd splitted in multiple routines
  1924. * location_force_reg added for loading a location to a register
  1925. of a specified size
  1926. * secondassignment parses now first the right and then the left node
  1927. (this is compatible with Kylix). This saves a lot of push/pop especially
  1928. with string operations
  1929. * adapted some routines to use the new cg methods
  1930. Revision 1.2 2002/04/01 19:24:25 jonas
  1931. * fixed different parameter name in interface and implementation
  1932. declaration of a method (only 1.0.x detected this)
  1933. Revision 1.1 2002/03/31 20:26:36 jonas
  1934. + a_loadfpu_* and a_loadmm_* methods in tcg
  1935. * register allocation is now handled by a class and is mostly processor
  1936. independent (+rgobj.pas and i386/rgcpu.pas)
  1937. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  1938. * some small improvements and fixes to the optimizer
  1939. * some register allocation fixes
  1940. * some fpuvaroffset fixes in the unary minus node
  1941. * push/popusedregisters is now called rg.save/restoreusedregisters and
  1942. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  1943. also better optimizable)
  1944. * fixed and optimized register saving/restoring for new/dispose nodes
  1945. * LOC_FPU locations now also require their "register" field to be set to
  1946. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  1947. - list field removed of the tnode class because it's not used currently
  1948. and can cause hard-to-find bugs
  1949. }