rgobj.pas 72 KB

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