rgobj.pas 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247
  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. for regi:=firstsaveintreg to lastsaveintreg do
  953. begin
  954. if (regi in s) then
  955. inc(reg_pushes_int[regi],t_times*2);
  956. end;
  957. end;
  958. procedure trgobj.incrementotherregisterpushed(const s:Tregisterset);
  959. var
  960. regi : Toldregister;
  961. begin
  962. if firstsavefpureg <> R_NO then
  963. for regi:=firstsavefpureg to lastsavefpureg do
  964. begin
  965. if (regi in s) then
  966. inc(reg_pushes_other[regi],t_times*2);
  967. end;
  968. if firstsavemmreg <> R_NO then
  969. for regi:=firstsavemmreg to lastsavemmreg do
  970. begin
  971. if (regi in s) then
  972. inc(reg_pushes_other[regi],t_times*2);
  973. end;
  974. end;
  975. procedure trgobj.clearregistercount;
  976. begin
  977. fillchar(reg_pushes_int,sizeof(reg_pushes_int),0);
  978. fillchar(reg_pushes_other,sizeof(reg_pushes_other),0);
  979. fillchar(is_reg_var_other,sizeof(is_reg_var_other),false);
  980. is_reg_var_int:=[];
  981. fillchar(regvar_loaded_other,sizeof(regvar_loaded_other),false);
  982. regvar_loaded_int:=[];
  983. end;
  984. procedure trgobj.resetusableregisters;
  985. begin
  986. { initialize fields with constant values from cpubase }
  987. countusableregsint := cpubase.c_countusableregsint;
  988. countusableregsfpu := cpubase.c_countusableregsfpu;
  989. countusableregsmm := cpubase.c_countusableregsmm;
  990. usableregsint := cpubase.usableregsint;
  991. usableregsfpu := cpubase.usableregsfpu;
  992. usableregsmm := cpubase.usableregsmm;
  993. clearregistercount;
  994. end;
  995. procedure trgobj.makeregvarint(reg:Tsuperregister);
  996. begin
  997. dec(countusableregsint);
  998. {$ifndef newra}
  999. dec(countunusedregsint);
  1000. {$endif}
  1001. exclude(usableregsint,reg shl 8);
  1002. exclude(unusedregsint,reg shl 8);
  1003. include(is_reg_var_int,reg);
  1004. end;
  1005. procedure trgobj.makeregvarother(reg: tregister);
  1006. begin
  1007. if reg.enum>lastreg then
  1008. internalerror(200301081);
  1009. if reg.enum in intregs then
  1010. internalerror(200301151)
  1011. else if reg.enum in fpuregs then
  1012. begin
  1013. dec(countusableregsfpu);
  1014. dec(countunusedregsfpu);
  1015. exclude(usableregsfpu,reg.enum);
  1016. exclude(unusedregsfpu,reg.enum);
  1017. end
  1018. else if reg.enum in mmregs then
  1019. begin
  1020. dec(countusableregsmm);
  1021. dec(countunusedregsmm);
  1022. exclude(usableregsmm,reg.enum);
  1023. exclude(unusedregsmm,reg.enum);
  1024. end;
  1025. is_reg_var_other[reg.enum]:=true;
  1026. end;
  1027. {$ifdef TEMPREGDEBUG}
  1028. procedure trgobj.testregisters;
  1029. var
  1030. r: tregister;
  1031. test : byte;
  1032. begin
  1033. test:=0;
  1034. for r := firstsaveintreg to lastsaveintreg do
  1035. inc(test,ord(r in unusedregsint));
  1036. if test<>countunusedregsint then
  1037. internalerror(10);
  1038. end;
  1039. {$endif TEMPREGDEBUG}
  1040. procedure trgobj.saveStateForInline(var state: pointer);
  1041. begin
  1042. new(psavedstate(state));
  1043. psavedstate(state)^.unusedregsint := unusedregsint;
  1044. psavedstate(state)^.usableregsint := usableregsint;
  1045. psavedstate(state)^.unusedregsfpu := unusedregsfpu;
  1046. psavedstate(state)^.usableregsfpu := usableregsfpu;
  1047. psavedstate(state)^.unusedregsmm := unusedregsmm;
  1048. psavedstate(state)^.usableregsmm := usableregsmm;
  1049. {$ifndef newra}
  1050. psavedstate(state)^.countunusedregsint := countunusedregsint;
  1051. {$endif}
  1052. psavedstate(state)^.countunusedregsfpu := countunusedregsfpu;
  1053. psavedstate(state)^.countunusedregsmm := countunusedregsmm;
  1054. psavedstate(state)^.countusableregsint := countusableregsint;
  1055. psavedstate(state)^.countusableregsfpu := countusableregsfpu;
  1056. psavedstate(state)^.countusableregsmm := countusableregsmm;
  1057. psavedstate(state)^.usedinproc := usedinproc;
  1058. psavedstate(state)^.usedbyproc := usedbyproc;
  1059. psavedstate(state)^.reg_pushes_int := reg_pushes_int;
  1060. psavedstate(state)^.reg_pushes_other := reg_pushes_other;
  1061. psavedstate(state)^.is_reg_var_int := is_reg_var_int;
  1062. psavedstate(state)^.is_reg_var_other := is_reg_var_other;
  1063. psavedstate(state)^.regvar_loaded_int := regvar_loaded_int;
  1064. psavedstate(state)^.regvar_loaded_other := regvar_loaded_other;
  1065. {$ifdef TEMPREGDEBUG}
  1066. psavedstate(state)^.reg_user := reg_user;
  1067. psavedstate(state)^.reg_releaser := reg_releaser;
  1068. {$endif TEMPREGDEBUG}
  1069. end;
  1070. procedure trgobj.restoreStateAfterInline(var state: pointer);
  1071. begin
  1072. unusedregsint := psavedstate(state)^.unusedregsint;
  1073. usableregsint := psavedstate(state)^.usableregsint;
  1074. unusedregsfpu := psavedstate(state)^.unusedregsfpu;
  1075. usableregsfpu := psavedstate(state)^.usableregsfpu;
  1076. unusedregsmm := psavedstate(state)^.unusedregsmm;
  1077. usableregsmm := psavedstate(state)^.usableregsmm;
  1078. {$ifndef newra}
  1079. countunusedregsint := psavedstate(state)^.countunusedregsint;
  1080. {$endif}
  1081. countunusedregsfpu := psavedstate(state)^.countunusedregsfpu;
  1082. countunusedregsmm := psavedstate(state)^.countunusedregsmm;
  1083. countusableregsint := psavedstate(state)^.countusableregsint;
  1084. countusableregsfpu := psavedstate(state)^.countusableregsfpu;
  1085. countusableregsmm := psavedstate(state)^.countusableregsmm;
  1086. usedinproc := psavedstate(state)^.usedinproc;
  1087. usedbyproc := psavedstate(state)^.usedbyproc;
  1088. reg_pushes_int := psavedstate(state)^.reg_pushes_int;
  1089. reg_pushes_other := psavedstate(state)^.reg_pushes_other;
  1090. is_reg_var_int := psavedstate(state)^.is_reg_var_int;
  1091. is_reg_var_other := psavedstate(state)^.is_reg_var_other;
  1092. regvar_loaded_other := psavedstate(state)^.regvar_loaded_other;
  1093. regvar_loaded_int := psavedstate(state)^.regvar_loaded_int;
  1094. {$ifdef TEMPREGDEBUG}
  1095. reg_user := psavedstate(state)^.reg_user;
  1096. reg_releaser := psavedstate(state)^.reg_releaser;
  1097. {$endif TEMPREGDEBUG}
  1098. dispose(psavedstate(state));
  1099. state := nil;
  1100. end;
  1101. procedure trgobj.saveUnusedState(var state: pointer);
  1102. begin
  1103. new(punusedstate(state));
  1104. punusedstate(state)^.unusedregsint := unusedregsint;
  1105. punusedstate(state)^.unusedregsfpu := unusedregsfpu;
  1106. punusedstate(state)^.unusedregsmm := unusedregsmm;
  1107. {$ifndef newra}
  1108. punusedstate(state)^.countunusedregsint := countunusedregsint;
  1109. {$endif}
  1110. punusedstate(state)^.countunusedregsfpu := countunusedregsfpu;
  1111. punusedstate(state)^.countunusedregsmm := countunusedregsmm;
  1112. end;
  1113. procedure trgobj.restoreUnusedState(var state: pointer);
  1114. begin
  1115. unusedregsint := punusedstate(state)^.unusedregsint;
  1116. unusedregsfpu := punusedstate(state)^.unusedregsfpu;
  1117. unusedregsmm := punusedstate(state)^.unusedregsmm;
  1118. {$ifndef newra}
  1119. countunusedregsint := punusedstate(state)^.countunusedregsint;
  1120. {$endif}
  1121. countunusedregsfpu := punusedstate(state)^.countunusedregsfpu;
  1122. countunusedregsmm := punusedstate(state)^.countunusedregsmm;
  1123. dispose(punusedstate(state));
  1124. state := nil;
  1125. end;
  1126. {$ifdef newra}
  1127. procedure Trgobj.add_edge(u,v:Tsuperregister);
  1128. {This procedure will add an edge to the virtual interference graph.}
  1129. procedure addadj(u,v:Tsuperregister);
  1130. begin
  1131. if igraph.adjlist[u]=nil then
  1132. begin
  1133. getmem(igraph.adjlist[u],16);
  1134. igraph.adjlist[u]^:='';
  1135. end
  1136. else if (length(igraph.adjlist[u]^) and 15)=15 then
  1137. reallocmem(igraph.adjlist[u],length(igraph.adjlist[u]^)+16);
  1138. igraph.adjlist[u]^:=igraph.adjlist[u]^+char(v);
  1139. end;
  1140. begin
  1141. if (u<>v) and not(v in igraph.bitmap[u]) then
  1142. begin
  1143. include(igraph.bitmap[u],v);
  1144. include(igraph.bitmap[v],u);
  1145. {Precoloured nodes are not stored in the interference graph.}
  1146. if not(u in [first_supreg..last_supreg]) then
  1147. begin
  1148. addadj(u,v);
  1149. inc(degree[u]);
  1150. end;
  1151. if not(v in [first_supreg..last_supreg]) then
  1152. begin
  1153. addadj(v,u);
  1154. inc(degree[v]);
  1155. end;
  1156. end;
  1157. end;
  1158. procedure Trgobj.add_edges_used(u:Tsuperregister);
  1159. var i:Tsuperregister;
  1160. begin
  1161. for i:=1 to 255 do
  1162. if not(i in unusedregsint) then
  1163. add_edge(u,i);
  1164. end;
  1165. {$ifdef ra_debug}
  1166. procedure Trgobj.writegraph;
  1167. {This procedure writes out the current interference graph in the
  1168. register allocator.}
  1169. var f:text;
  1170. i,j:Tsuperregister;
  1171. begin
  1172. assign(f,'igraph'+char(48+random(10))+char(48+random(10)));
  1173. rewrite(f);
  1174. writeln(f,'Interference graph');
  1175. writeln(f);
  1176. write(f,' ');
  1177. for i:=0 to 15 do
  1178. for j:=0 to 15 do
  1179. write(f,hexstr(i,1));
  1180. writeln(f);
  1181. write(f,' ');
  1182. for i:=0 to 15 do
  1183. write(f,'0123456789ABCDEF');
  1184. writeln(f);
  1185. for i:=0 to 255 do
  1186. begin
  1187. write(f,hexstr(i,2):4);
  1188. for j:=0 to 255 do
  1189. if j in igraph.bitmap[i] then
  1190. write(f,'*')
  1191. else
  1192. write(f,'-');
  1193. writeln(f);
  1194. end;
  1195. close(f);
  1196. end;
  1197. {$endif}
  1198. procedure Trgobj.add_to_movelist(u:Tsuperregister;data:Tlinkedlistitem);
  1199. begin
  1200. if movelist[u]=nil then
  1201. begin
  1202. getmem(movelist[u],64);
  1203. movelist[u]^.count:=0;
  1204. end
  1205. else if (movelist[u]^.count and 15)=15 then
  1206. reallocmem(movelist[u],(movelist[u]^.count+1)*4+64);
  1207. movelist[u]^.data[movelist[u]^.count]:=data;
  1208. inc(movelist[u]^.count);
  1209. end;
  1210. procedure Trgobj.add_move_instruction(instr:Taicpu);
  1211. {This procedure notifies a certain as a move instruction so the
  1212. register allocator can try to eliminate it.}
  1213. var i:Tmoveins;
  1214. ssupreg,dsupreg:Tsuperregister;
  1215. begin
  1216. i:=Tmoveins.create;
  1217. i.moveset:=ms_worklist_moves;
  1218. i.instruction:=instr;
  1219. worklist_moves.insert(i);
  1220. ssupreg:=instr.oper[0].reg.number shr 8;
  1221. add_to_movelist(ssupreg,i);
  1222. dsupreg:=instr.oper[1].reg.number shr 8;
  1223. add_to_movelist(dsupreg,i);
  1224. end;
  1225. function Trgobj.move_related(n:Tsuperregister):boolean;
  1226. var i:cardinal;
  1227. begin
  1228. move_related:=false;
  1229. if movelist[n]<>nil then
  1230. begin
  1231. for i:=0 to movelist[n]^.count-1 do
  1232. if Tmoveins(movelist[n]^.data[i]).moveset in
  1233. [ms_worklist_moves,ms_active_moves] then
  1234. begin
  1235. move_related:=true;
  1236. break;
  1237. end;
  1238. end;
  1239. end;
  1240. procedure Trgobj.make_work_list;
  1241. var n:Tsuperregister;
  1242. begin
  1243. for n:=first_imreg to maxintreg do
  1244. if degree[n]>cpu_registers then
  1245. spillworklist:=spillworklist+char(n)
  1246. else if move_related(n) then
  1247. freezeworklist:=freezeworklist+char(n)
  1248. else
  1249. simplifyworklist:=simplifyworklist+char(n);
  1250. end;
  1251. procedure Trgobj.prepare_colouring;
  1252. begin
  1253. make_work_list;
  1254. active_moves:=Tlinkedlist.create;
  1255. frozen_moves:=Tlinkedlist.create;
  1256. coalesced_moves:=Tlinkedlist.create;
  1257. constrained_moves:=Tlinkedlist.create;
  1258. fillchar(alias,sizeof(alias),0);
  1259. coalescednodes:='';
  1260. selectstack:='';
  1261. end;
  1262. procedure Trgobj.enable_moves(n:Tsuperregister);
  1263. var m:Tlinkedlistitem;
  1264. i:cardinal;
  1265. begin
  1266. if movelist[n]<>nil then
  1267. for i:=0 to movelist[n]^.count-1 do
  1268. begin
  1269. m:=movelist[n]^.data[i];
  1270. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  1271. begin
  1272. if Tmoveins(m).moveset=ms_active_moves then
  1273. begin
  1274. {Move m from the set active_moves to the set worklist_moves.}
  1275. active_moves.remove(m);
  1276. Tmoveins(m).moveset:=ms_worklist_moves;
  1277. worklist_moves.concat(m);
  1278. end;
  1279. end;
  1280. end;
  1281. end;
  1282. procedure Trgobj.decrement_degree(m:Tsuperregister);
  1283. var adj:Pstring;
  1284. d:byte;
  1285. i:byte;
  1286. n:char;
  1287. begin
  1288. d:=degree[m];
  1289. dec(degree[m]);
  1290. if d=cpu_registers then
  1291. begin
  1292. {Enable moves for m.}
  1293. enable_moves(m);
  1294. {Enable moves for adjacent.}
  1295. adj:=igraph.adjlist[m];
  1296. if adj<>nil then
  1297. for i:=1 to length(adj^) do
  1298. begin
  1299. n:=adj^[i];
  1300. if (pos(n,selectstack) or pos(n,coalescednodes))=0 then
  1301. enable_moves(Tsuperregister(n));
  1302. end;
  1303. {In case the node is in the spillworklist, delete it.}
  1304. delete(spillworklist,pos(char(m),spillworklist),1);
  1305. if move_related(m) then
  1306. freezeworklist:=freezeworklist+char(m)
  1307. else
  1308. simplifyworklist:=simplifyworklist+char(m);
  1309. end;
  1310. end;
  1311. procedure Trgobj.simplify;
  1312. var adj:Pstring;
  1313. i,min,p:byte;
  1314. m:char;
  1315. n:Tsuperregister;
  1316. begin
  1317. {We the element with the least interferences out of the
  1318. simplifyworklist.}
  1319. min:=$ff;
  1320. p:=1;
  1321. for i:=1 to length(simplifyworklist) do
  1322. begin
  1323. adj:=igraph.adjlist[Tsuperregister(simplifyworklist[i])];
  1324. if adj=nil then
  1325. begin
  1326. min:=0;
  1327. break; {We won't find smaller ones.}
  1328. end
  1329. else
  1330. if length(adj^)<min then
  1331. begin
  1332. min:=length(adj^);
  1333. if min=0 then
  1334. break; {We won't find smaller ones.}
  1335. p:=i;
  1336. end;
  1337. end;
  1338. n:=Tsuperregister(simplifyworklist[p]);
  1339. delete(simplifyworklist,p,1);
  1340. {Push it on the selectstack.}
  1341. selectstack:=selectstack+char(n);
  1342. adj:=igraph.adjlist[n];
  1343. if adj<>nil then
  1344. for i:=1 to length(adj^) do
  1345. begin
  1346. m:=adj^[i];
  1347. if (pos(m,selectstack) or pos(m,coalescednodes))=0 then
  1348. decrement_degree(Tsuperregister(m));
  1349. end;
  1350. end;
  1351. function Trgobj.get_alias(n:Tsuperregister):Tsuperregister;
  1352. begin
  1353. while pos(char(n),coalescednodes)<>0 do
  1354. n:=alias[n];
  1355. get_alias:=n;
  1356. end;
  1357. procedure Trgobj.add_worklist(u:Tsuperregister);
  1358. begin
  1359. if not(u in [first_supreg..last_supreg]) and not move_related(u) and
  1360. (degree[u]<cpu_registers) then
  1361. begin
  1362. delete(freezeworklist,pos(char(u),freezeworklist),1);
  1363. simplifyworklist:=simplifyworklist+char(u);
  1364. end;
  1365. end;
  1366. function Trgobj.adjacent_ok(u,v:Tsuperregister):boolean;
  1367. {Check wether u and v should be coalesced. u is precoloured.}
  1368. function ok(t,r:Tsuperregister):boolean;
  1369. begin
  1370. ok:=(degree[t]<cpu_registers) or
  1371. (t in [first_supreg..last_supreg]) or
  1372. (r in igraph.bitmap[t]);
  1373. end;
  1374. var adj:Pstring;
  1375. i:byte;
  1376. t:char;
  1377. begin
  1378. adjacent_ok:=true;
  1379. adj:=igraph.adjlist[v];
  1380. if adj<>nil then
  1381. for i:=1 to length(adj^) do
  1382. begin
  1383. t:=adj^[i];
  1384. if (pos(t,selectstack) or pos(t,coalescednodes))=0 then
  1385. if not ok(Tsuperregister(t),u) then
  1386. begin
  1387. adjacent_ok:=false;
  1388. break;
  1389. end;
  1390. end;
  1391. end;
  1392. function Trgobj.conservative(u,v:Tsuperregister):boolean;
  1393. var adj:Pstring;
  1394. done:set of char; {To prevent that we count nodes twice.}
  1395. i,k:byte;
  1396. n:char;
  1397. begin
  1398. k:=0;
  1399. done:=[];
  1400. adj:=igraph.adjlist[u];
  1401. if adj<>nil then
  1402. for i:=1 to length(adj^) do
  1403. begin
  1404. n:=adj^[i];
  1405. if (pos(n,selectstack) or pos(n,coalescednodes))=0 then
  1406. begin
  1407. include(done,n);
  1408. if degree[Tsuperregister(n)]>=cpu_registers then
  1409. inc(k);
  1410. end;
  1411. end;
  1412. adj:=igraph.adjlist[v];
  1413. if adj<>nil then
  1414. for i:=1 to length(adj^) do
  1415. begin
  1416. n:=adj^[i];
  1417. if ((pos(n,selectstack) or pos(n,coalescednodes))=0) and
  1418. not (n in done) and
  1419. (degree[Tsuperregister(n)]>=cpu_registers) then
  1420. inc(k);
  1421. end;
  1422. conservative:=(k<cpu_registers);
  1423. end;
  1424. procedure Trgobj.combine(u,v:Tsuperregister);
  1425. var add:boolean;
  1426. adj:Pstring;
  1427. i,p:byte;
  1428. n,o:cardinal;
  1429. t:char;
  1430. begin
  1431. p:=pos(char(v),freezeworklist);
  1432. if p<>0 then
  1433. delete(freezeworklist,p,1)
  1434. else
  1435. delete(spillworklist,pos(char(v),spillworklist),1);
  1436. coalescednodes:=coalescednodes+char(v);
  1437. alias[v]:=u;
  1438. {Combine both movelists. Since the movelists are sets, only add
  1439. elements that are not already present.}
  1440. for n:=0 to movelist[v]^.count-1 do
  1441. begin
  1442. add:=true;
  1443. for o:=0 to movelist[u]^.count-1 do
  1444. if movelist[u]^.data[o]=movelist[v]^.data[n] then
  1445. begin
  1446. add:=false;
  1447. break;
  1448. end;
  1449. if add then
  1450. add_to_movelist(u,movelist[v]^.data[n]);
  1451. end;
  1452. enable_moves(v);
  1453. adj:=igraph.adjlist[v];
  1454. if adj<>nil then
  1455. for i:=1 to length(adj^) do
  1456. begin
  1457. t:=adj^[i];
  1458. if (pos(t,selectstack) or pos(t,coalescednodes))=0 then
  1459. begin
  1460. add_edge(Tsuperregister(t),u);
  1461. decrement_degree(Tsuperregister(t));
  1462. end;
  1463. end;
  1464. p:=pos(char(u),freezeworklist);
  1465. if (degree[u]>=cpu_registers) and (p<>0) then
  1466. begin
  1467. delete(freezeworklist,p,1);
  1468. spillworklist:=spillworklist+char(u);
  1469. end;
  1470. end;
  1471. procedure Trgobj.coalesce;
  1472. var m:Tmoveins;
  1473. x,y,u,v:Tsuperregister;
  1474. begin
  1475. m:=Tmoveins(worklist_moves.getfirst);
  1476. x:=get_alias(m.instruction.oper[0].reg.number shr 8);
  1477. y:=get_alias(m.instruction.oper[1].reg.number shr 8);
  1478. if y in [first_supreg..last_supreg] then
  1479. begin
  1480. u:=y;
  1481. v:=x;
  1482. end
  1483. else
  1484. begin
  1485. u:=x;
  1486. v:=y;
  1487. end;
  1488. if (u=v) then
  1489. begin
  1490. m.moveset:=ms_coalesced_moves; {Already coalesced.}
  1491. coalesced_moves.insert(m);
  1492. add_worklist(u);
  1493. end
  1494. {Do u and v interfere? In that case the move is constrained. Two
  1495. precoloured nodes interfere allways. If v is precoloured, by the above
  1496. code u is precoloured, thus interference...}
  1497. else if (v in [first_supreg..last_supreg]) or (u in igraph.bitmap[v]) then
  1498. begin
  1499. m.moveset:=ms_constrained_moves; {Cannot coalesce yet...}
  1500. constrained_moves.insert(m);
  1501. add_worklist(u);
  1502. add_worklist(v);
  1503. end
  1504. {Next test: is it possible and a good idea to coalesce??}
  1505. else if ((u in [first_supreg..last_supreg]) and adjacent_ok(u,v)) or
  1506. (not(u in [first_supreg..last_supreg]) and conservative(u,v)) then
  1507. begin
  1508. m.moveset:=ms_coalesced_moves; {Move coalesced!}
  1509. coalesced_moves.insert(m);
  1510. combine(u,v);
  1511. add_worklist(u);
  1512. end
  1513. else
  1514. begin
  1515. m.moveset:=ms_active_moves;
  1516. active_moves.insert(m);
  1517. end;
  1518. end;
  1519. procedure Trgobj.freeze_moves(u:Tsuperregister);
  1520. var i:cardinal;
  1521. m:Tlinkedlistitem;
  1522. v,x,y:Tsuperregister;
  1523. begin
  1524. if movelist[u]<>nil then
  1525. for i:=0 to movelist[u]^.count-1 do
  1526. begin
  1527. m:=movelist[u]^.data[i];
  1528. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  1529. begin
  1530. x:=Tmoveins(m).instruction.oper[0].reg.number shr 8;
  1531. y:=Tmoveins(m).instruction.oper[1].reg.number shr 8;
  1532. if get_alias(y)=get_alias(u) then
  1533. v:=get_alias(x)
  1534. else
  1535. v:=get_alias(y);
  1536. {Move m from active_moves/worklist_moves to frozen_moves.}
  1537. if Tmoveins(m).moveset=ms_active_moves then
  1538. active_moves.remove(m)
  1539. else
  1540. worklist_moves.remove(m);
  1541. Tmoveins(m).moveset:=ms_frozen_moves;
  1542. frozen_moves.insert(m);
  1543. if not(move_related(v)) and (degree[v]<cpu_registers) then
  1544. begin
  1545. delete(freezeworklist,pos(char(v),freezeworklist),1);
  1546. simplifyworklist:=simplifyworklist+char(v);
  1547. end;
  1548. end;
  1549. end;
  1550. end;
  1551. procedure Trgobj.freeze;
  1552. var n:Tsuperregister;
  1553. begin
  1554. {We need to take a random element out of the freezeworklist. We take
  1555. the last element. Dirty code!}
  1556. n:=Tsuperregister(freezeworklist[byte(freezeworklist[0])]);
  1557. dec(freezeworklist[0]);
  1558. {Add it to the simplifyworklist.}
  1559. simplifyworklist:=simplifyworklist+char(n);
  1560. freeze_moves(n);
  1561. end;
  1562. procedure Trgobj.select_spill;
  1563. var n:char;
  1564. begin
  1565. {This code is WAY too naive. We need not to select just a register, but
  1566. the register that is used the least...}
  1567. n:=spillworklist[byte(spillworklist[0])];
  1568. dec(spillworklist[0]);
  1569. simplifyworklist:=simplifyworklist+n;
  1570. freeze_moves(Tsuperregister(n));
  1571. end;
  1572. procedure Trgobj.assign_colours;
  1573. {Assign_colours assigns the actual colours to the registers.}
  1574. var adj:Pstring;
  1575. i,j,k:byte;
  1576. n,a:Tsuperregister;
  1577. adj_colours,colourednodes:set of Tsuperregister;
  1578. w:char;
  1579. begin
  1580. spillednodes:='';
  1581. {Colour the cpu registers...}
  1582. colourednodes:=[first_supreg..last_supreg];
  1583. for i:=first_supreg to last_supreg do
  1584. colour[i]:=i;
  1585. {Now colour the imaginary registers on the select-stack.}
  1586. for i:=length(selectstack) downto 1 do
  1587. begin
  1588. n:=Tsuperregister(selectstack[i]);
  1589. {Create a list of colours that we cannot assign to n.}
  1590. adj_colours:=[];
  1591. adj:=igraph.adjlist[n];
  1592. if adj<>nil then
  1593. for j:=1 to length(adj^) do
  1594. begin
  1595. w:=adj^[j];
  1596. a:=get_alias(Tsuperregister(w));
  1597. if a in colourednodes then
  1598. include(adj_colours,colour[a]);
  1599. end;
  1600. {Assume a spill by default...}
  1601. spillednodes:=spillednodes+char(n);
  1602. {Search for a colour not in this list.}
  1603. for k:=1 to cpu_registers do
  1604. if not(k in adj_colours) then
  1605. begin
  1606. colour[n]:=k;
  1607. dec(spillednodes[0]); {Colour found: no spill.}
  1608. include(colourednodes,n);
  1609. break;
  1610. end;
  1611. end;
  1612. {Finally colour the nodes that were coalesced.}
  1613. for i:=1 to length(coalescednodes) do
  1614. begin
  1615. n:=Tsuperregister(coalescednodes[i]);
  1616. colour[n]:=colour[get_alias(n)];
  1617. end;
  1618. for i:=first_imreg to maxintreg do
  1619. writeln(i:4,' ',colour[i]:4)
  1620. end;
  1621. procedure Trgobj.colour_registers;
  1622. begin
  1623. repeat
  1624. if length(simplifyworklist)<>0 then
  1625. simplify
  1626. else if not(worklist_moves.empty) then
  1627. coalesce
  1628. else if length(freezeworklist)<>0 then
  1629. freeze
  1630. else if length(spillworklist)<>0 then
  1631. select_spill;
  1632. until (length(simplifyworklist) or
  1633. byte(not(worklist_moves.empty)) or
  1634. length(freezeworklist) or
  1635. length(spillworklist)
  1636. )=0;
  1637. assign_colours;
  1638. end;
  1639. procedure Trgobj.epilogue_colouring;
  1640. begin
  1641. active_moves.destroy;
  1642. active_moves:=nil;
  1643. frozen_moves.destroy;
  1644. frozen_moves:=nil;
  1645. coalesced_moves.destroy;
  1646. coalesced_moves:=nil;
  1647. constrained_moves.destroy;
  1648. constrained_moves:=nil;
  1649. end;
  1650. {$endif newra}
  1651. {****************************************************************************
  1652. TReference
  1653. ****************************************************************************}
  1654. procedure reference_reset(var ref : treference);
  1655. begin
  1656. FillChar(ref,sizeof(treference),0);
  1657. ref.base.enum:=R_INTREGISTER;
  1658. ref.index.enum:=R_INTREGISTER;
  1659. {$ifdef i386}
  1660. ref.segment.enum:=R_INTREGISTER;
  1661. {$endif}
  1662. end;
  1663. procedure reference_reset_old(var ref : treference);
  1664. begin
  1665. FillChar(ref,sizeof(treference),0);
  1666. end;
  1667. procedure reference_reset_base(var ref : treference;base : tregister;offset : longint);
  1668. begin
  1669. reference_reset(ref);
  1670. ref.base:=base;
  1671. ref.offset:=offset;
  1672. end;
  1673. procedure reference_reset_symbol(var ref : treference;sym : tasmsymbol;offset : longint);
  1674. begin
  1675. reference_reset(ref);
  1676. ref.symbol:=sym;
  1677. ref.offset:=offset;
  1678. end;
  1679. procedure reference_release(list: taasmoutput; const ref : treference);
  1680. begin
  1681. rg.ungetreference(list,ref);
  1682. end;
  1683. function references_equal(sref : treference;dref : treference):boolean;
  1684. begin
  1685. references_equal:=CompareByte(sref,dref,sizeof(treference))=0;
  1686. end;
  1687. { on most processors , this routine does nothing, overriden currently }
  1688. { only by 80x86 processor. }
  1689. function trgobj.makeregsize(reg: tregister; size: tcgsize): tregister;
  1690. begin
  1691. makeregsize := reg;
  1692. end;
  1693. {****************************************************************************
  1694. TLocation
  1695. ****************************************************************************}
  1696. procedure location_reset(var l : tlocation;lt:TCGLoc;lsize:TCGSize);
  1697. begin
  1698. FillChar(l,sizeof(tlocation),0);
  1699. l.loc:=lt;
  1700. l.size:=lsize;
  1701. case l.loc of
  1702. LOC_REGISTER,LOC_CREGISTER:
  1703. begin
  1704. l.register.enum:=R_INTREGISTER;
  1705. l.registerhigh.enum:=R_INTREGISTER;
  1706. end;
  1707. LOC_REFERENCE,LOC_CREFERENCE:
  1708. begin
  1709. l.reference.base.enum:=R_INTREGISTER;
  1710. l.reference.index.enum:=R_INTREGISTER;
  1711. {$ifdef i386}
  1712. l.reference.segment.enum:=R_INTREGISTER;
  1713. {$endif}
  1714. end;
  1715. end;
  1716. end;
  1717. procedure location_release(list: taasmoutput; const l : tlocation);
  1718. begin
  1719. case l.loc of
  1720. LOC_REGISTER,LOC_CREGISTER :
  1721. begin
  1722. rg.ungetregisterint(list,l.register);
  1723. if l.size in [OS_64,OS_S64] then
  1724. rg.ungetregisterint(list,l.registerhigh);
  1725. end;
  1726. LOC_CREFERENCE,LOC_REFERENCE :
  1727. rg.ungetreference(list, l.reference);
  1728. end;
  1729. end;
  1730. procedure location_freetemp(list:taasmoutput; const l : tlocation);
  1731. begin
  1732. if (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  1733. tg.ungetiftemp(list,l.reference);
  1734. end;
  1735. procedure location_copy(var destloc,sourceloc : tlocation);
  1736. begin
  1737. destloc:=sourceloc;
  1738. end;
  1739. procedure location_swap(var destloc,sourceloc : tlocation);
  1740. var
  1741. swapl : tlocation;
  1742. begin
  1743. swapl := destloc;
  1744. destloc := sourceloc;
  1745. sourceloc := swapl;
  1746. end;
  1747. initialization
  1748. ;
  1749. finalization
  1750. rg.free;
  1751. end.
  1752. {
  1753. $Log$
  1754. Revision 1.45 2003-05-30 12:36:13 jonas
  1755. * use as little different registers on the ppc until newra is released,
  1756. since every used register must be saved
  1757. Revision 1.44 2003/05/17 13:30:08 jonas
  1758. * changed tt_persistant to tt_persistent :)
  1759. * tempcreatenode now doesn't accept a boolean anymore for persistent
  1760. temps, but a ttemptype, so you can also create ansistring temps etc
  1761. Revision 1.43 2003/05/16 14:33:31 peter
  1762. * regvar fixes
  1763. Revision 1.42 2003/04/26 20:03:49 daniel
  1764. * Bug fix in simplify
  1765. Revision 1.41 2003/04/25 20:59:35 peter
  1766. * removed funcretn,funcretsym, function result is now in varsym
  1767. and aliases for result and function name are added using absolutesym
  1768. * vs_hidden parameter for funcret passed in parameter
  1769. * vs_hidden fixes
  1770. * writenode changed to printnode and released from extdebug
  1771. * -vp option added to generate a tree.log with the nodetree
  1772. * nicer printnode for statements, callnode
  1773. Revision 1.40 2003/04/25 08:25:26 daniel
  1774. * Ifdefs around a lot of calls to cleartempgen
  1775. * Fixed registers that are allocated but not freed in several nodes
  1776. * Tweak to register allocator to cause less spills
  1777. * 8-bit registers now interfere with esi,edi and ebp
  1778. Compiler can now compile rtl successfully when using new register
  1779. allocator
  1780. Revision 1.39 2003/04/23 20:23:06 peter
  1781. * compile fix for no-newra
  1782. Revision 1.38 2003/04/23 14:42:07 daniel
  1783. * Further register allocator work. Compiler now smaller with new
  1784. allocator than without.
  1785. * Somebody forgot to adjust ppu version number
  1786. Revision 1.37 2003/04/22 23:50:23 peter
  1787. * firstpass uses expectloc
  1788. * checks if there are differences between the expectloc and
  1789. location.loc from secondpass in EXTDEBUG
  1790. Revision 1.36 2003/04/22 10:09:35 daniel
  1791. + Implemented the actual register allocator
  1792. + Scratch registers unavailable when new register allocator used
  1793. + maybe_save/maybe_restore unavailable when new register allocator used
  1794. Revision 1.35 2003/04/21 19:16:49 peter
  1795. * count address regs separate
  1796. Revision 1.34 2003/04/17 16:48:21 daniel
  1797. * Added some code to keep track of move instructions in register
  1798. allocator
  1799. Revision 1.33 2003/04/17 07:50:24 daniel
  1800. * Some work on interference graph construction
  1801. Revision 1.32 2003/03/28 19:16:57 peter
  1802. * generic constructor working for i386
  1803. * remove fixed self register
  1804. * esi added as address register for i386
  1805. Revision 1.31 2003/03/11 21:46:24 jonas
  1806. * lots of new regallocator fixes, both in generic and ppc-specific code
  1807. (ppc compiler still can't compile the linux system unit though)
  1808. Revision 1.30 2003/03/09 21:18:59 olle
  1809. + added cutils to the uses clause
  1810. Revision 1.29 2003/03/08 20:36:41 daniel
  1811. + Added newra version of Ti386shlshrnode
  1812. + Added interference graph construction code
  1813. Revision 1.28 2003/03/08 13:59:16 daniel
  1814. * Work to handle new register notation in ag386nsm
  1815. + Added newra version of Ti386moddivnode
  1816. Revision 1.27 2003/03/08 10:53:48 daniel
  1817. * Created newra version of secondmul in n386add.pas
  1818. Revision 1.26 2003/03/08 08:59:07 daniel
  1819. + $define newra will enable new register allocator
  1820. + getregisterint will return imaginary registers with $newra
  1821. + -sr switch added, will skip register allocation so you can see
  1822. the direct output of the code generator before register allocation
  1823. Revision 1.25 2003/02/26 20:50:45 daniel
  1824. * Fixed ungetreference
  1825. Revision 1.24 2003/02/19 22:39:56 daniel
  1826. * Fixed a few issues
  1827. Revision 1.23 2003/02/19 22:00:14 daniel
  1828. * Code generator converted to new register notation
  1829. - Horribily outdated todo.txt removed
  1830. Revision 1.22 2003/02/02 19:25:54 carl
  1831. * Several bugfixes for m68k target (register alloc., opcode emission)
  1832. + VIS target
  1833. + Generic add more complete (still not verified)
  1834. Revision 1.21 2003/01/08 18:43:57 daniel
  1835. * Tregister changed into a record
  1836. Revision 1.20 2002/10/05 12:43:28 carl
  1837. * fixes for Delphi 6 compilation
  1838. (warning : Some features do not work under Delphi)
  1839. Revision 1.19 2002/08/23 16:14:49 peter
  1840. * tempgen cleanup
  1841. * tt_noreuse temp type added that will be used in genentrycode
  1842. Revision 1.18 2002/08/17 22:09:47 florian
  1843. * result type handling in tcgcal.pass_2 overhauled
  1844. * better tnode.dowrite
  1845. * some ppc stuff fixed
  1846. Revision 1.17 2002/08/17 09:23:42 florian
  1847. * first part of procinfo rewrite
  1848. Revision 1.16 2002/08/06 20:55:23 florian
  1849. * first part of ppc calling conventions fix
  1850. Revision 1.15 2002/08/05 18:27:48 carl
  1851. + more more more documentation
  1852. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  1853. Revision 1.14 2002/08/04 19:06:41 carl
  1854. + added generic exception support (still does not work!)
  1855. + more documentation
  1856. Revision 1.13 2002/07/07 09:52:32 florian
  1857. * powerpc target fixed, very simple units can be compiled
  1858. * some basic stuff for better callparanode handling, far from being finished
  1859. Revision 1.12 2002/07/01 18:46:26 peter
  1860. * internal linker
  1861. * reorganized aasm layer
  1862. Revision 1.11 2002/05/18 13:34:17 peter
  1863. * readded missing revisions
  1864. Revision 1.10 2002/05/16 19:46:44 carl
  1865. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1866. + try to fix temp allocation (still in ifdef)
  1867. + generic constructor calls
  1868. + start of tassembler / tmodulebase class cleanup
  1869. Revision 1.8 2002/04/21 15:23:03 carl
  1870. + makeregsize
  1871. + changeregsize is now a local routine
  1872. Revision 1.7 2002/04/20 21:32:25 carl
  1873. + generic FPC_CHECKPOINTER
  1874. + first parameter offset in stack now portable
  1875. * rename some constants
  1876. + move some cpu stuff to other units
  1877. - remove unused constents
  1878. * fix stacksize for some targets
  1879. * fix generic size problems which depend now on EXTEND_SIZE constant
  1880. Revision 1.6 2002/04/15 19:03:31 carl
  1881. + reg2str -> std_reg2str()
  1882. Revision 1.5 2002/04/06 18:13:01 jonas
  1883. * several powerpc-related additions and fixes
  1884. Revision 1.4 2002/04/04 19:06:04 peter
  1885. * removed unused units
  1886. * use tlocation.size in cg.a_*loc*() routines
  1887. Revision 1.3 2002/04/02 17:11:29 peter
  1888. * tlocation,treference update
  1889. * LOC_CONSTANT added for better constant handling
  1890. * secondadd splitted in multiple routines
  1891. * location_force_reg added for loading a location to a register
  1892. of a specified size
  1893. * secondassignment parses now first the right and then the left node
  1894. (this is compatible with Kylix). This saves a lot of push/pop especially
  1895. with string operations
  1896. * adapted some routines to use the new cg methods
  1897. Revision 1.2 2002/04/01 19:24:25 jonas
  1898. * fixed different parameter name in interface and implementation
  1899. declaration of a method (only 1.0.x detected this)
  1900. Revision 1.1 2002/03/31 20:26:36 jonas
  1901. + a_loadfpu_* and a_loadmm_* methods in tcg
  1902. * register allocation is now handled by a class and is mostly processor
  1903. independent (+rgobj.pas and i386/rgcpu.pas)
  1904. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  1905. * some small improvements and fixes to the optimizer
  1906. * some register allocation fixes
  1907. * some fpuvaroffset fixes in the unary minus node
  1908. * push/popusedregisters is now called rg.save/restoreusedregisters and
  1909. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  1910. also better optimizable)
  1911. * fixed and optimized register saving/restoring for new/dispose nodes
  1912. * LOC_FPU locations now also require their "register" field to be set to
  1913. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  1914. - list field removed of the tnode class because it's not used currently
  1915. and can cause hard-to-find bugs
  1916. }