rgobj.pas 74 KB

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