rgobj.pas 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680
  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. { Allow duplicate allocations, can be used to get the .s file written }
  20. { $define ALLOWDUPREG}
  21. {# @abstract(Abstract register allocator unit)
  22. This unit contains services to allocate, free
  23. references and registers which are used by
  24. the code generator.
  25. }
  26. {*******************************************************************************
  27. (applies to new register allocator)
  28. Register allocator introduction.
  29. Free Pascal uses a Chaitin style register allocator. We use a variant similair
  30. to the one described in the book "Modern compiler implementation in C" by
  31. Andrew W. Appel., published by Cambridge University Press.
  32. The register allocator that is described by Appel uses a much improved way
  33. of register coalescing, called "iterated register coalescing". Instead
  34. of doing coalescing as a prepass to the register allocation, the coalescing
  35. is done inside the register allocator. This has the advantage that the
  36. register allocator can coalesce very aggresively without introducing spills.
  37. Reading this book is recommended for a complete understanding. Here is a small
  38. introduction.
  39. The code generator thinks it has an infinite amount of registers. Our processor
  40. has a limited amount of registers. Therefore we must reduce the amount of
  41. registers until there are less enough to fit into the processors registers.
  42. Registers can interfere or not interfere. If two imaginary registers interfere
  43. they cannot be placed into the same psysical register. Reduction of registers
  44. is done by:
  45. - "coalescing" Two registers that do not interfere are combined
  46. into one register.
  47. - "spilling" A register is changed into a memory location and the generated
  48. code is modified to use the memory location instead of the register.
  49. Register allocation is a graph colouring problem. Each register is a colour, and
  50. if two registers interfere there is a connection between them in the graph.
  51. In addition to the imaginary registers in the code generator, the psysical
  52. CPU registers are also present in this graph. This allows us to make
  53. interferences between imaginary registers and cpu registers. This is very
  54. usefull for describing archtectural constraints, like for example that
  55. the div instruction modifies edx, so variables that are in use at that time
  56. cannot be stored into edx. This can be modelled by making edx interfere
  57. with those variables.
  58. Graph colouring is an NP complete problem. Therefore we use an approximation
  59. that pushes registers to colour on to a stack. This is done in the "simplify"
  60. procedure.
  61. The register allocator first checks which registers are a candidate for
  62. coalescing.
  63. *******************************************************************************}
  64. unit rgobj;
  65. interface
  66. uses
  67. {$ifdef delphi}
  68. dmisc,
  69. {$endif}
  70. cutils,cclasses,
  71. globtype,
  72. cpubase,cpuinfo,cgbase,
  73. aasmbase,aasmtai,aasmcpu,
  74. node
  75. ;
  76. const
  77. ALL_OTHERREGISTERS=[low(tregisterindex)..high(tregisterindex)];
  78. type
  79. regvarother_longintarray = array[tregisterindex] of longint;
  80. regvarother_booleanarray = array[tregisterindex] of boolean;
  81. regvarint_longintarray = array[first_int_supreg..last_int_supreg] of longint;
  82. regvarint_ptreearray = array[first_int_supreg..last_int_supreg] of tnode;
  83. tpushedsavedloc = record
  84. case byte of
  85. 0: (pushed: boolean);
  86. 1: (ofs: longint);
  87. end;
  88. tpushedsavedother = array[tregisterindex] of tpushedsavedloc;
  89. Tinterferencebitmap=array[Tsuperregister] of set of Tsuperregister;
  90. Tinterferenceadjlist=array[Tsuperregister] of Pstring;
  91. Tinterferencegraph=record
  92. bitmap:Tinterferencebitmap;
  93. adjlist:Tinterferenceadjlist;
  94. end;
  95. Pinterferencegraph=^Tinterferencegraph;
  96. Tmovelist=record
  97. count:cardinal;
  98. data:array[0..$ffff] of Tlinkedlistitem;
  99. end;
  100. Pmovelist=^Tmovelist;
  101. {In the register allocator we keep track of move instructions.
  102. These instructions are moved between five linked lists. There
  103. is also a linked list per register to keep track about the moves
  104. it is associated with. Because we need to determine quickly in
  105. which of the five lists it is we add anu enumeradtion to each
  106. move instruction.}
  107. Tmoveset=(ms_coalesced_moves,ms_constrained_moves,ms_frozen_moves,
  108. ms_worklist_moves,ms_active_moves);
  109. Tmoveins=class(Tlinkedlistitem)
  110. moveset:Tmoveset;
  111. { $ifdef ra_debug}
  112. x,y:Tsuperregister;
  113. { $endif}
  114. instruction:Taicpu;
  115. end;
  116. {#
  117. This class implements the abstract register allocator
  118. It is used by the code generator to allocate and free
  119. registers which might be valid across nodes. It also
  120. contains utility routines related to registers.
  121. Some of the methods in this class should be overriden
  122. by cpu-specific implementations.
  123. }
  124. trgobj = class
  125. { The "usableregsxxx" contain all registers of type "xxx" that }
  126. { aren't currently allocated to a regvar. The "unusedregsxxx" }
  127. { contain all registers of type "xxx" that aren't currently }
  128. { allocated }
  129. maxintreg:Tsuperregister;
  130. usable_registers:string[32];
  131. unusedregsint,usableregsint:Tsuperregisterset;
  132. unusedregsaddr,usableregsaddr:Tsuperregisterset;
  133. unusedregsfpu,usableregsfpu : Tsuperregisterset;
  134. unusedregsmm,usableregsmm : Tsuperregisterset;
  135. { these counters contain the number of elements in the }
  136. { unusedregsxxx/usableregsxxx sets }
  137. countunusedregsfpu,
  138. countunusedregsmm : byte;
  139. countusableregsint,
  140. countusableregsaddr,
  141. countusableregsfpu,
  142. countusableregsmm : byte;
  143. { Contains the registers which are really used by the proc itself.
  144. It doesn't take care of registers used by called procedures
  145. }
  146. preserved_by_proc_int,
  147. used_in_proc_int : Tsuperregisterset;
  148. used_in_proc_other : totherregisterset;
  149. reg_pushes_other : regvarother_longintarray;
  150. is_reg_var_other : regvarother_booleanarray;
  151. is_reg_var_int : Tsuperregisterset;
  152. regvar_loaded_other : regvarother_booleanarray;
  153. regvar_loaded_int : Tsuperregisterset;
  154. colour : array[Tsuperregister] of Tsuperregister;
  155. spillednodes : string;
  156. { tries to hold the amount of times which the current tree is processed }
  157. t_times: longint;
  158. constructor create(Acpu_registers:byte;const Ausable:string);
  159. destructor destroy;override;
  160. {# Allocate a general purpose register
  161. An internalerror will be generated if there
  162. is no more free registers which can be allocated
  163. }
  164. function getregisterint(list:Taasmoutput;size:Tcgsize):Tregister;
  165. procedure add_constraints(reg:Tregister);virtual;
  166. {# Allocate an ABT register
  167. An internalerror will be generated if there
  168. is no more free registers which can be allocated
  169. An explanantion of abt registers can be found near the implementation.
  170. }
  171. function getabtregisterint(list:Taasmoutput;size:Tcgsize):Tregister;
  172. {# Free a general purpose register
  173. @param(r register to free)
  174. }
  175. procedure ungetregisterint(list: taasmoutput; r : tregister); virtual;
  176. {# Allocate a floating point register
  177. An internalerror will be generated if there
  178. is no more free registers which can be allocated
  179. }
  180. function getregisterfpu(list: taasmoutput;size:Tcgsize) : tregister; virtual;
  181. {# Free a floating point register
  182. @param(r register to free)
  183. }
  184. procedure ungetregisterfpu(list: taasmoutput; r : tregister;size:TCGsize); virtual;
  185. function getregistermm(list: taasmoutput) : tregister; virtual;
  186. procedure ungetregistermm(list: taasmoutput; r : tregister); virtual;
  187. {# Allocate an address register.
  188. Address registers are the only registers which can
  189. be used as a base register in references (treference).
  190. On most cpu's this is the same as a general purpose
  191. register.
  192. An internalerror will be generated if there
  193. is no more free registers which can be allocated
  194. }
  195. function getaddressregister(list:Taasmoutput):Tregister;virtual;
  196. procedure ungetaddressregister(list: taasmoutput; r: tregister); virtual;
  197. {# Verify if the specified register is an address or
  198. general purpose register. Returns TRUE if @var(reg)
  199. is an adress register.
  200. This routine should only be used to check on
  201. general purpose or address register. It will
  202. not work on multimedia or floating point
  203. registers
  204. @param(reg register to verify)
  205. }
  206. function isaddressregister(reg: tregister): boolean; virtual;
  207. {# Tries to allocate the passed register, if possible
  208. @param(r specific register to allocate)
  209. }
  210. function getexplicitregisterint(list:Taasmoutput;r:Tregister):Tregister;virtual;
  211. {# Tries to allocate the passed fpu register, if possible
  212. @param(r specific register to allocate)
  213. }
  214. function getexplicitregisterfpu(list : taasmoutput; r : Tregister) : tregister;virtual;
  215. procedure allocexplicitregistersint(list:Taasmoutput;r:Tsuperregisterset);
  216. procedure deallocexplicitregistersint(list:Taasmoutput;r:Tsuperregisterset);
  217. {# Deallocate any kind of register }
  218. procedure ungetregister(list: taasmoutput; r : tregister); virtual;
  219. {# Deallocate all registers which are allocated
  220. in the specified reference. On most systems,
  221. this will free the base and index registers
  222. of the specified reference.
  223. @param(ref reference which must have its registers freed)
  224. }
  225. procedure ungetreference(list: taasmoutput; const ref : treference); virtual;
  226. {# Convert a register to a specified register size, and return that register size }
  227. function makeregsize(reg: tregister; size: tcgsize): tregister; virtual;
  228. {# saves register variables (restoring happens automatically) }
  229. procedure saveotherregvars(list:Taasmoutput;const s:Totherregisterset);
  230. {# Saves in temporary references (allocated via the temp. allocator)
  231. the registers defined in @var(s). The registers are only saved
  232. if they are currently in use, otherwise they are left as is.
  233. On processors which have instructions which manipulate the stack,
  234. this routine should be overriden for performance reasons.
  235. @param(list) List to add the instruction to
  236. @param(saved) Array of saved register information
  237. @param(s) Registers which might require saving
  238. }
  239. procedure saveusedotherregisters(list:Taasmoutput;
  240. var saved:Tpushedsavedother;
  241. const s:Totherregisterset);virtual;
  242. {# Restores the registers which were saved with a call
  243. to @var(saveusedregisters).
  244. On processors which have instructions which manipulate the stack,
  245. this routine should be overriden for performance reasons.
  246. }
  247. procedure restoreusedotherregisters(list:Taasmoutput;
  248. const saved:Tpushedsavedother);virtual;
  249. { used when deciding which registers to use for regvars }
  250. procedure incrementotherregisterpushed(const s: totherregisterset);
  251. procedure clearregistercount;
  252. procedure resetusableregisters;virtual;
  253. procedure makeregvarint(reg:Tsuperregister);
  254. procedure makeregvarother(reg:Tregister);
  255. procedure saveStateForInline(var state: pointer);virtual;
  256. procedure restoreStateAfterInline(var state: pointer);virtual;
  257. procedure saveUnusedState(var state: pointer);virtual;
  258. procedure restoreUnusedState(var state: pointer);virtual;
  259. {$ifdef EXTDEBUG}
  260. procedure writegraph(loopidx:longint);
  261. {$endif EXTDEBUG}
  262. procedure add_move_instruction(instr:Taicpu);
  263. procedure prepare_colouring;
  264. procedure epilogue_colouring;
  265. procedure colour_registers;
  266. function spill_registers(list:Taasmoutput;headertai:tai;const regs_to_spill:string):boolean;
  267. procedure add_edge(u,v:Tsuperregister);
  268. protected
  269. cpu_registers:byte;
  270. igraph:Tinterferencegraph;
  271. degree:array[0..255] of byte;
  272. alias:array[Tsuperregister] of Tsuperregister;
  273. simplifyworklist,freezeworklist,spillworklist:string;
  274. coalescednodes:string;
  275. selectstack:string;
  276. abtlist:string;
  277. movelist:array[Tsuperregister] of Pmovelist;
  278. worklist_moves,active_moves,frozen_moves,
  279. coalesced_moves,constrained_moves:Tlinkedlist;
  280. { the following two contain the common (generic) code for all }
  281. { get- and ungetregisterxxx functions/procedures }
  282. function getregistergenother(list: taasmoutput; const lowreg, highreg: tsuperregister;
  283. var unusedregs:Tsuperregisterset;var countunusedregs:byte): tregister;
  284. function getregistergenint(list:Taasmoutput;subreg:Tsubregister;
  285. const lowreg,highreg:Tsuperregister;
  286. var fusedinproc,unusedregs:Tsuperregisterset):Tregister;
  287. procedure ungetregistergen(list: taasmoutput; r: tregister;
  288. const usableregs:tsuperregisterset;var unusedregs: tsuperregisterset; var countunusedregs: byte);
  289. procedure ungetregistergenint(list:taasmoutput;r:Tregister;
  290. const usableregs:Tsuperregisterset;
  291. var unusedregs:Tsuperregisterset);
  292. procedure getregisterintinline(list:Taasmoutput;position:Tai;subreg:Tsubregister;var result:Tregister);
  293. procedure ungetregisterintinline(list:Taasmoutput;position:Tai;r:Tregister);
  294. procedure add_edges_used(u:Tsuperregister);
  295. procedure add_to_movelist(u:Tsuperregister;data:Tlinkedlistitem);
  296. function move_related(n:Tsuperregister):boolean;
  297. procedure make_work_list;
  298. procedure enable_moves(n:Tsuperregister);
  299. procedure decrement_degree(m:Tsuperregister);
  300. procedure simplify;
  301. function get_alias(n:Tsuperregister):Tsuperregister;
  302. procedure add_worklist(u:Tsuperregister);
  303. function adjacent_ok(u,v:Tsuperregister):boolean;
  304. function conservative(u,v:Tsuperregister):boolean;
  305. procedure combine(u,v:Tsuperregister);
  306. procedure coalesce;
  307. procedure freeze_moves(u:Tsuperregister);
  308. procedure freeze;
  309. procedure select_spill;
  310. procedure assign_colours;
  311. procedure clear_interferences(u:Tsuperregister);
  312. end;
  313. trgobjclass = class of trgobj;
  314. const
  315. {# This value is used in tsaved. If the array value is equal
  316. to this, then this means that this register is not used.
  317. }
  318. reg_not_saved = $7fffffff;
  319. var
  320. rg : trgobj;
  321. { trerefence handling }
  322. {# Clear to zero a treference }
  323. procedure reference_reset(var ref : treference);
  324. {# Clear to zero a treference, and set is base address
  325. to base register.
  326. }
  327. procedure reference_reset_base(var ref : treference;base : tregister;offset : longint);
  328. procedure reference_reset_symbol(var ref : treference;sym : tasmsymbol;offset : longint);
  329. procedure reference_release(list: taasmoutput; const ref : treference);
  330. { This routine verifies if two references are the same, and
  331. if so, returns TRUE, otherwise returns false.
  332. }
  333. function references_equal(sref : treference;dref : treference) : boolean;
  334. { tlocation handling }
  335. procedure location_reset(var l : tlocation;lt:TCGLoc;lsize:TCGSize);
  336. procedure location_release(list: taasmoutput; const l : tlocation);
  337. procedure location_freetemp(list: taasmoutput; const l : tlocation);
  338. procedure location_copy(var destloc:tlocation; const sourceloc : tlocation);
  339. procedure location_swap(var destloc,sourceloc : tlocation);
  340. type
  341. psavedstate = ^tsavedstate;
  342. tsavedstate = record
  343. unusedregsint,usableregsint : Tsuperregisterset;
  344. unusedregsaddr,usableregsaddr : Tsuperregisterset;
  345. unusedregsfpu,usableregsfpu : totherregisterset;
  346. unusedregsmm,usableregsmm : totherregisterset;
  347. countunusedregsfpu,
  348. countunusedregsmm : byte;
  349. countusableregsint,
  350. countusableregsfpu,
  351. countusableregsmm : byte;
  352. { contains the registers which are really used by the proc itself }
  353. used_in_proc_int : Tsuperregisterset;
  354. used_in_proc_other : totherregisterset;
  355. reg_pushes_other : regvarother_longintarray;
  356. reg_pushes_int : regvarint_longintarray;
  357. is_reg_var_other : regvarother_booleanarray;
  358. is_reg_var_int : Tsuperregisterset;
  359. regvar_loaded_other: regvarother_booleanarray;
  360. regvar_loaded_int: Tsuperregisterset;
  361. end;
  362. punusedstate = ^tunusedstate;
  363. tunusedstate = record
  364. unusedregsaddr : Tsuperregisterset;
  365. unusedregsfpu : totherregisterset;
  366. unusedregsmm : totherregisterset;
  367. countunusedregsfpu,
  368. countunusedregsmm : byte;
  369. end;
  370. implementation
  371. uses
  372. systems,
  373. globals,verbose,
  374. cgobj,tgobj,
  375. procinfo;
  376. constructor Trgobj.create(Acpu_registers:byte;const Ausable:string);
  377. var i:Tsuperregister;
  378. begin
  379. used_in_proc_int := [];
  380. used_in_proc_other:=[];
  381. t_times := 0;
  382. resetusableregisters;
  383. maxintreg:=first_int_imreg;
  384. cpu_registers:=Acpu_registers;
  385. unusedregsint:=[0..254]; { 255 (RS_INVALID) can't be used }
  386. unusedregsfpu:=usableregsfpu;
  387. unusedregsmm:=usableregsmm;
  388. countunusedregsfpu:=countusableregsfpu;
  389. countunusedregsmm:=countusableregsmm;
  390. {$ifdef powerpc}
  391. preserved_by_proc_int:=[RS_R13..RS_R31];
  392. {$else powerpc}
  393. preserved_by_proc_int:=[];
  394. {$endif powerpc}
  395. fillchar(igraph,sizeof(igraph),0);
  396. fillchar(degree,sizeof(degree),0);
  397. {Precoloured nodes should have an infinite degree, which we can approach
  398. by 255.}
  399. for i:=first_int_supreg to last_int_supreg do
  400. degree[i]:=255;
  401. fillchar(movelist,sizeof(movelist),0);
  402. worklist_moves:=Tlinkedlist.create;
  403. usable_registers:=Ausable;
  404. abtlist:='';
  405. fillchar(colour,sizeof(colour),RS_INVALID);
  406. end;
  407. destructor Trgobj.destroy;
  408. var i:Tsuperregister;
  409. begin
  410. for i:=low(Tsuperregister) to high(Tsuperregister) do
  411. begin
  412. if igraph.adjlist[i]<>nil then
  413. dispose(igraph.adjlist[i]);
  414. if movelist[i]<>nil then
  415. dispose(movelist[i]);
  416. end;
  417. worklist_moves.free;
  418. end;
  419. function trgobj.getregistergenother(list: taasmoutput; const lowreg, highreg: tsuperregister;
  420. var unusedregs: tsuperregisterset; var countunusedregs: byte): tregister;
  421. var
  422. i: tsuperregister;
  423. r: Tregister;
  424. begin
  425. for i:=lowreg to highreg do
  426. begin
  427. if i in unusedregs then
  428. begin
  429. exclude(unusedregs,i);
  430. include(used_in_proc_other,i);
  431. dec(countunusedregs);
  432. {$warning Only FPU Registers supported}
  433. r:=newreg(R_FPUREGISTER,i,R_SUBNONE);
  434. list.concat(tai_regalloc.alloc(r));
  435. result := r;
  436. exit;
  437. end;
  438. end;
  439. internalerror(10);
  440. end;
  441. function Trgobj.getregistergenint(list:Taasmoutput;
  442. subreg:Tsubregister;
  443. const lowreg,highreg:Tsuperregister;
  444. var fusedinproc,unusedregs:Tsuperregisterset):Tregister;
  445. var i,p:Tsuperregister;
  446. r:Tregister;
  447. min : byte;
  448. adj : pstring;
  449. begin
  450. if maxintreg<highreg then
  451. begin
  452. inc(maxintreg);
  453. p:=maxintreg;
  454. min:=0;
  455. end
  456. else
  457. begin
  458. min:=$ff;
  459. p:=first_int_imreg;
  460. for i:=lowreg to maxintreg do
  461. if (i in unusedregs) and
  462. (pos(char(i),abtlist)=0) then
  463. begin
  464. adj:=igraph.adjlist[Tsuperregister(i)];
  465. if adj=nil then
  466. begin
  467. p:=i;
  468. min:=0;
  469. break; {We won't find smaller ones.}
  470. end
  471. else
  472. if length(adj^)<min then
  473. begin
  474. p:=i;
  475. min:=length(adj^);
  476. if min=0 then
  477. break; {We won't find smaller ones.}
  478. end;
  479. end;
  480. if min=$ff then
  481. begin
  482. {$ifdef ALLOWDUPREG}
  483. result:=newreg(R_INTREGISTER,RS_INVALID,subreg);
  484. exit;
  485. {$else}
  486. internalerror(10);
  487. {$endif}
  488. end;
  489. end;
  490. exclude(unusedregs,p);
  491. include(fusedinproc,p);
  492. r:=newreg(R_INTREGISTER,p,subreg);
  493. list.concat(Tai_regalloc.alloc(r));
  494. add_edges_used(p);
  495. add_constraints(r);
  496. result:=r;
  497. end;
  498. procedure trgobj.ungetregistergen(list: taasmoutput; r: tregister;
  499. const usableregs: tsuperregisterset; var unusedregs: tsuperregisterset; var countunusedregs: byte);
  500. var
  501. supreg : tsuperregister;
  502. begin
  503. supreg:=getsupreg(r);
  504. { takes much time }
  505. if not(supreg in usableregs) then
  506. exit;
  507. if (supreg in unusedregs) then
  508. exit
  509. else
  510. inc(countunusedregs);
  511. include(unusedregs,supreg);
  512. list.concat(tai_regalloc.dealloc(r));
  513. end;
  514. procedure trgobj.ungetregistergenint(list:taasmoutput;r:Tregister;
  515. const usableregs:Tsuperregisterset;
  516. var unusedregs:Tsuperregisterset);
  517. var
  518. supreg:Tsuperregister;
  519. begin
  520. supreg:=getsupreg(r);
  521. { takes much time }
  522. if supreg in is_reg_var_int then
  523. exit;
  524. if (supreg in unusedregs) then
  525. exit;
  526. include(unusedregs,supreg);
  527. list.concat(tai_regalloc.dealloc(r));
  528. add_edges_used(supreg);
  529. add_constraints(r);
  530. end;
  531. function trgobj.getregisterint(list:taasmoutput;size:Tcgsize):Tregister;
  532. var subreg:Tsubregister;
  533. begin
  534. subreg:=cgsize2subreg(size);
  535. result:=getregistergenint(list,
  536. subreg,
  537. first_int_imreg,
  538. last_int_imreg,
  539. used_in_proc_int,
  540. unusedregsint);
  541. add_constraints(getregisterint);
  542. end;
  543. procedure Trgobj.add_constraints(reg:Tregister);
  544. begin
  545. end;
  546. procedure trgobj.ungetregisterint(list : taasmoutput; r : tregister);
  547. begin
  548. ungetregistergenint(list,r,usableregsint,unusedregsint);
  549. end;
  550. { tries to allocate the passed register, if possible }
  551. function trgobj.getexplicitregisterint(list:Taasmoutput;r:Tregister):Tregister;
  552. var supreg : tsuperregister;
  553. begin
  554. supreg:=getsupreg(r);
  555. if supreg in unusedregsint then
  556. begin
  557. exclude(unusedregsint,supreg);
  558. include(used_in_proc_int,supreg);
  559. list.concat(tai_regalloc.alloc(r));
  560. add_edges_used(supreg);
  561. add_constraints(r);
  562. end
  563. else
  564. {$ifndef ALLOWDUPREG}
  565. internalerror(200301103)
  566. {$endif ALLOWDUPREG}
  567. ;
  568. getexplicitregisterint:=r;
  569. end;
  570. procedure Trgobj.allocexplicitregistersint(list:Taasmoutput;r:Tsuperregisterset);
  571. var reg:Tregister;
  572. i:Tsuperregister;
  573. begin
  574. if unusedregsint*r=r then
  575. begin
  576. unusedregsint:=unusedregsint-r;
  577. used_in_proc_int:=used_in_proc_int+r;
  578. for i:=first_int_supreg to last_int_supreg do
  579. if i in r then
  580. begin
  581. add_edges_used(i);
  582. reg:=newreg(R_INTREGISTER,i,R_SUBWHOLE);
  583. list.concat(Tai_regalloc.alloc(reg));
  584. end;
  585. end
  586. else
  587. {$ifndef ALLOWDUPREG}
  588. internalerror(200305061)
  589. {$endif ALLOWDUPREG}
  590. ;
  591. end;
  592. procedure Trgobj.deallocexplicitregistersint(list:Taasmoutput;r:Tsuperregisterset);
  593. var reg:Tregister;
  594. i:Tsuperregister;
  595. begin
  596. if unusedregsint*r=[] then
  597. begin
  598. unusedregsint:=unusedregsint+r;
  599. for i:=last_int_supreg downto first_int_supreg do
  600. if i in r then
  601. begin
  602. reg:=newreg(R_INTREGISTER,i,R_SUBWHOLE);
  603. list.concat(Tai_regalloc.dealloc(reg));
  604. end;
  605. end
  606. else
  607. {$ifndef ALLOWDUPREG}
  608. internalerror(200305061)
  609. {$endif ALLOWDUPREG}
  610. ;
  611. end;
  612. { tries to allocate the passed register, if possible }
  613. function trgobj.getexplicitregisterfpu(list : taasmoutput; r : Tregister) : tregister;
  614. var
  615. supreg : tsuperregister;
  616. begin
  617. supreg:=getsupreg(r);
  618. if supreg in unusedregsfpu then
  619. begin
  620. dec(countunusedregsfpu);
  621. exclude(unusedregsfpu,supreg);
  622. include(used_in_proc_other,supreg);
  623. list.concat(tai_regalloc.alloc(r));
  624. getexplicitregisterfpu:=r;
  625. end
  626. else
  627. {$warning Size for FPU reg is maybe not correct}
  628. getexplicitregisterfpu:=getregisterfpu(list,OS_F32);
  629. end;
  630. function trgobj.getregisterfpu(list: taasmoutput;size:Tcgsize) : tregister;
  631. begin
  632. if countunusedregsfpu=0 then
  633. internalerror(10);
  634. {$warning TODO firstsavefpureg}
  635. result := getregistergenother(list,firstsavefpureg,lastsavefpureg,
  636. unusedregsfpu,countunusedregsfpu);
  637. end;
  638. procedure trgobj.ungetregisterfpu(list : taasmoutput; r : tregister;size:TCGsize);
  639. begin
  640. ungetregistergen(list,r,usableregsfpu,unusedregsfpu,
  641. countunusedregsfpu);
  642. end;
  643. function trgobj.getregistermm(list: taasmoutput) : tregister;
  644. begin
  645. if countunusedregsmm=0 then
  646. internalerror(10);
  647. result := getregistergenother(list,firstsavemmreg,lastsavemmreg,
  648. unusedregsmm,countunusedregsmm);
  649. end;
  650. procedure trgobj.ungetregistermm(list: taasmoutput; r: tregister);
  651. begin
  652. ungetregistergen(list,r,usableregsmm,unusedregsmm,
  653. countunusedregsmm);
  654. end;
  655. function trgobj.getaddressregister(list:Taasmoutput): tregister;
  656. begin
  657. {An address register is OS_INT per definition.}
  658. result := getregisterint(list,OS_INT);
  659. end;
  660. procedure trgobj.ungetaddressregister(list: taasmoutput; r: tregister);
  661. begin
  662. ungetregisterint(list,r);
  663. end;
  664. function trgobj.isaddressregister(reg: tregister): boolean;
  665. begin
  666. result := true;
  667. end;
  668. procedure trgobj.ungetregister(list: taasmoutput; r : tregister);
  669. begin
  670. if r=NR_NO then
  671. exit;
  672. if getregtype(r)=R_FPUREGISTER then
  673. ungetregisterfpu(list,r,OS_NO)
  674. else if getregtype(r)=R_MMXREGISTER then
  675. ungetregistermm(list,r)
  676. else if getregtype(r)=R_ADDRESSREGISTER then
  677. ungetaddressregister(list,r)
  678. else internalerror(2002070602);
  679. end;
  680. procedure trgobj.ungetreference(list : taasmoutput; const ref : treference);
  681. begin
  682. if (ref.base<>NR_NO) and (ref.base<>NR_FRAME_POINTER_REG) then
  683. ungetregisterint(list,ref.base);
  684. if (ref.index<>NR_NO) and (ref.index<>NR_FRAME_POINTER_REG) then
  685. ungetregisterint(list,ref.index);
  686. end;
  687. procedure trgobj.saveotherregvars(list: taasmoutput; const s: totherregisterset);
  688. var
  689. r: Tregister;
  690. begin
  691. if not(cs_regvars in aktglobalswitches) then
  692. exit;
  693. {$warning TODO firstsavefpureg}
  694. {
  695. if firstsavefpureg <> NR_NO then
  696. for r.enum := firstsavefpureg to lastsavefpureg do
  697. if is_reg_var_other[r.enum] and
  698. (r.enum in s) then
  699. store_regvar(list,r);
  700. if firstsavemmreg <> R_NO then
  701. for r.enum := firstsavemmreg to lastsavemmreg do
  702. if is_reg_var_other[r.enum] and
  703. (r.enum in s) then
  704. store_regvar(list,r);
  705. }
  706. end;
  707. procedure trgobj.saveusedotherregisters(list: taasmoutput;
  708. var saved : tpushedsavedother; const s: totherregisterset);
  709. var
  710. r : tregister;
  711. hr : treference;
  712. begin
  713. used_in_proc_other:=used_in_proc_other + s;
  714. {$warning TODO firstsavefpureg}
  715. (*
  716. { don't try to save the fpu registers if not desired (e.g. for }
  717. { the 80x86) }
  718. if firstsavefpureg <> R_NO then
  719. for r.enum:=firstsavefpureg to lastsavefpureg do
  720. begin
  721. saved[r.enum].ofs:=reg_not_saved;
  722. { if the register is used by the calling subroutine and if }
  723. { it's not a regvar (those are handled separately) }
  724. if not is_reg_var_other[r.enum] and
  725. (r.enum in s) and
  726. { and is present in use }
  727. not(r.enum in unusedregsfpu) then
  728. begin
  729. { then save it }
  730. tg.GetTemp(list,extended_size,tt_persistent,hr);
  731. saved[r.enum].ofs:=hr.offset;
  732. cg.a_loadfpu_reg_ref(list,OS_FLOAT,r,hr);
  733. cg.a_reg_dealloc(list,r);
  734. include(unusedregsfpu,r.enum);
  735. inc(countunusedregsfpu);
  736. end;
  737. end;
  738. { don't save the vector registers if there's no support for them }
  739. if firstsavemmreg <> R_NO then
  740. for r.enum:=firstsavemmreg to lastsavemmreg do
  741. begin
  742. saved[r.enum].ofs:=reg_not_saved;
  743. { if the register is in use and if it's not a regvar (those }
  744. { are handled separately), save it }
  745. if not is_reg_var_other[r.enum] and
  746. (r.enum in s) and
  747. { and is present in use }
  748. not(r.enum in unusedregsmm) then
  749. begin
  750. { then save it }
  751. tg.GetTemp(list,mmreg_size,tt_persistent,hr);
  752. saved[r.enum].ofs:=hr.offset;
  753. cg.a_loadmm_reg_ref(list,r,hr);
  754. cg.a_reg_dealloc(list,r);
  755. include(unusedregsmm,r.enum);
  756. inc(countunusedregsmm);
  757. end;
  758. end;
  759. *)
  760. end;
  761. procedure trgobj.restoreusedotherregisters(list : taasmoutput;
  762. const saved : tpushedsavedother);
  763. var
  764. r,r2 : tregister;
  765. hr : treference;
  766. begin
  767. {$warning TODO firstsavefpureg}
  768. (*
  769. if firstsavemmreg <> R_NO then
  770. for r.enum:=lastsavemmreg downto firstsavemmreg do
  771. begin
  772. if saved[r.enum].ofs <> reg_not_saved then
  773. begin
  774. r2.enum:=R_INTREGISTER;
  775. r2.number:=NR_FRAME_POINTER_REG;
  776. reference_reset_base(hr,r2,saved[r.enum].ofs);
  777. cg.a_reg_alloc(list,r);
  778. cg.a_loadmm_ref_reg(list,hr,r);
  779. if not (r.enum in unusedregsmm) then
  780. { internalerror(10)
  781. in n386cal we always save/restore the reg *state*
  782. using save/restoreunusedstate -> the current state
  783. may not be real (JM) }
  784. else
  785. begin
  786. dec(countunusedregsmm);
  787. exclude(unusedregsmm,r.enum);
  788. end;
  789. tg.UnGetTemp(list,hr);
  790. end;
  791. end;
  792. if firstsavefpureg <> R_NO then
  793. for r.enum:=lastsavefpureg downto firstsavefpureg do
  794. begin
  795. if saved[r.enum].ofs <> reg_not_saved then
  796. begin
  797. r2.enum:=R_INTREGISTER;
  798. r2.number:=NR_FRAME_POINTER_REG;
  799. reference_reset_base(hr,r2,saved[r.enum].ofs);
  800. cg.a_reg_alloc(list,r);
  801. cg.a_loadfpu_ref_reg(list,OS_FLOAT,hr,r);
  802. if not (r.enum in unusedregsfpu) then
  803. { internalerror(10)
  804. in n386cal we always save/restore the reg *state*
  805. using save/restoreunusedstate -> the current state
  806. may not be real (JM) }
  807. else
  808. begin
  809. dec(countunusedregsfpu);
  810. exclude(unusedregsfpu,r.enum);
  811. end;
  812. tg.UnGetTemp(list,hr);
  813. end;
  814. end;
  815. *)
  816. end;
  817. procedure trgobj.incrementotherregisterpushed(const s:Totherregisterset);
  818. {$ifdef i386}
  819. var
  820. regi : Tregister;
  821. {$endif i386}
  822. begin
  823. {$warning TODO firstsavefpureg}
  824. (*
  825. {$ifdef i386}
  826. if firstsavefpureg <> R_NO then
  827. for regi:=firstsavefpureg to lastsavefpureg do
  828. begin
  829. if (regi in s) then
  830. inc(reg_pushes_other[regi],t_times*2);
  831. end;
  832. if firstsavemmreg <> R_NO then
  833. for regi:=firstsavemmreg to lastsavemmreg do
  834. begin
  835. if (regi in s) then
  836. inc(reg_pushes_other[regi],t_times*2);
  837. end;
  838. {$endif i386}
  839. *)
  840. end;
  841. procedure trgobj.clearregistercount;
  842. begin
  843. fillchar(reg_pushes_other,sizeof(reg_pushes_other),0);
  844. {ifndef i386}
  845. { all used registers will have to be saved at the start and restored }
  846. { at the end, but otoh regpara's do not have to be saved to memory }
  847. { at the start (there is a move from regpara to regvar most of the }
  848. { time though) -> set cost to 100+20 }
  849. {$warning TODO firstsavefpureg}
  850. (*
  851. filldword(reg_pushes_other[firstsavefpureg],ord(lastsavefpureg)-ord(firstsavefpureg)+1,120);
  852. *)
  853. {endif not i386}
  854. fillchar(is_reg_var_other,sizeof(is_reg_var_other),false);
  855. is_reg_var_int:=[];
  856. fillchar(regvar_loaded_other,sizeof(regvar_loaded_other),false);
  857. regvar_loaded_int:=[];
  858. end;
  859. procedure trgobj.resetusableregisters;
  860. begin
  861. { initialize fields with constant values from cpubase }
  862. countusableregsint := cpubase.c_countusableregsint;
  863. countusableregsfpu := cpubase.c_countusableregsfpu;
  864. countusableregsmm := cpubase.c_countusableregsmm;
  865. usableregsint := cpubase.usableregsint;
  866. usableregsfpu := cpubase.usableregsfpu;
  867. usableregsmm := cpubase.usableregsmm;
  868. clearregistercount;
  869. end;
  870. procedure trgobj.makeregvarint(reg:Tsuperregister);
  871. begin
  872. dec(countusableregsint);
  873. include(is_reg_var_int,reg);
  874. end;
  875. procedure trgobj.makeregvarother(reg: tregister);
  876. begin
  877. (*
  878. if reg.enum>lastreg then
  879. internalerror(200301081);
  880. if reg.enum in intregs then
  881. internalerror(200301151)
  882. else if reg.enum in fpuregs then
  883. begin
  884. dec(countusableregsfpu);
  885. dec(countunusedregsfpu);
  886. exclude(usableregsfpu,reg.enum);
  887. exclude(unusedregsfpu,reg.enum);
  888. include(used_in_proc_other,reg.enum);
  889. end
  890. else if reg.enum in mmregs then
  891. begin
  892. dec(countusableregsmm);
  893. dec(countunusedregsmm);
  894. exclude(usableregsmm,reg.enum);
  895. exclude(unusedregsmm,reg.enum);
  896. include(used_in_proc_other,reg.enum);
  897. end;
  898. is_reg_var_other[reg.enum]:=true;
  899. *)
  900. end;
  901. procedure trgobj.saveStateForInline(var state: pointer);
  902. begin
  903. new(psavedstate(state));
  904. psavedstate(state)^.unusedregsint := unusedregsint;
  905. psavedstate(state)^.usableregsint := usableregsint;
  906. psavedstate(state)^.unusedregsfpu := unusedregsfpu;
  907. psavedstate(state)^.usableregsfpu := usableregsfpu;
  908. psavedstate(state)^.unusedregsmm := unusedregsmm;
  909. psavedstate(state)^.usableregsmm := usableregsmm;
  910. psavedstate(state)^.countunusedregsfpu := countunusedregsfpu;
  911. psavedstate(state)^.countunusedregsmm := countunusedregsmm;
  912. psavedstate(state)^.countusableregsint := countusableregsint;
  913. psavedstate(state)^.countusableregsfpu := countusableregsfpu;
  914. psavedstate(state)^.countusableregsmm := countusableregsmm;
  915. psavedstate(state)^.used_in_proc_int := used_in_proc_int;
  916. psavedstate(state)^.used_in_proc_other := used_in_proc_other;
  917. psavedstate(state)^.reg_pushes_other := reg_pushes_other;
  918. psavedstate(state)^.is_reg_var_int := is_reg_var_int;
  919. psavedstate(state)^.is_reg_var_other := is_reg_var_other;
  920. psavedstate(state)^.regvar_loaded_int := regvar_loaded_int;
  921. psavedstate(state)^.regvar_loaded_other := regvar_loaded_other;
  922. end;
  923. procedure trgobj.restoreStateAfterInline(var state: pointer);
  924. begin
  925. unusedregsint := psavedstate(state)^.unusedregsint;
  926. usableregsint := psavedstate(state)^.usableregsint;
  927. unusedregsfpu := psavedstate(state)^.unusedregsfpu;
  928. usableregsfpu := psavedstate(state)^.usableregsfpu;
  929. unusedregsmm := psavedstate(state)^.unusedregsmm;
  930. usableregsmm := psavedstate(state)^.usableregsmm;
  931. countunusedregsfpu := psavedstate(state)^.countunusedregsfpu;
  932. countunusedregsmm := psavedstate(state)^.countunusedregsmm;
  933. countusableregsint := psavedstate(state)^.countusableregsint;
  934. countusableregsfpu := psavedstate(state)^.countusableregsfpu;
  935. countusableregsmm := psavedstate(state)^.countusableregsmm;
  936. used_in_proc_int := psavedstate(state)^.used_in_proc_int;
  937. used_in_proc_other := psavedstate(state)^.used_in_proc_other;
  938. reg_pushes_other := psavedstate(state)^.reg_pushes_other;
  939. is_reg_var_int := psavedstate(state)^.is_reg_var_int;
  940. is_reg_var_other := psavedstate(state)^.is_reg_var_other;
  941. regvar_loaded_other := psavedstate(state)^.regvar_loaded_other;
  942. regvar_loaded_int := psavedstate(state)^.regvar_loaded_int;
  943. dispose(psavedstate(state));
  944. state := nil;
  945. end;
  946. procedure trgobj.saveUnusedState(var state: pointer);
  947. begin
  948. new(punusedstate(state));
  949. punusedstate(state)^.unusedregsfpu := unusedregsfpu;
  950. punusedstate(state)^.unusedregsmm := unusedregsmm;
  951. punusedstate(state)^.countunusedregsfpu := countunusedregsfpu;
  952. punusedstate(state)^.countunusedregsmm := countunusedregsmm;
  953. end;
  954. procedure trgobj.restoreUnusedState(var state: pointer);
  955. begin
  956. unusedregsfpu := punusedstate(state)^.unusedregsfpu;
  957. unusedregsmm := punusedstate(state)^.unusedregsmm;
  958. countunusedregsfpu := punusedstate(state)^.countunusedregsfpu;
  959. countunusedregsmm := punusedstate(state)^.countunusedregsmm;
  960. dispose(punusedstate(state));
  961. state := nil;
  962. end;
  963. procedure Trgobj.add_edge(u,v:Tsuperregister);
  964. {This procedure will add an edge to the virtual interference graph.}
  965. procedure addadj(u,v:Tsuperregister);
  966. begin
  967. if igraph.adjlist[u]=nil then
  968. begin
  969. getmem(igraph.adjlist[u],16);
  970. igraph.adjlist[u]^:='';
  971. end
  972. else if (length(igraph.adjlist[u]^) and 15)=15 then
  973. reallocmem(igraph.adjlist[u],length(igraph.adjlist[u]^)+16);
  974. igraph.adjlist[u]^:=igraph.adjlist[u]^+char(v);
  975. end;
  976. begin
  977. if (u<>v) and not(v in igraph.bitmap[u]) then
  978. begin
  979. include(igraph.bitmap[u],v);
  980. include(igraph.bitmap[v],u);
  981. {Precoloured nodes are not stored in the interference graph.}
  982. if not(u in [first_int_supreg..last_int_supreg]) then
  983. begin
  984. addadj(u,v);
  985. inc(degree[u]);
  986. end;
  987. if not(v in [first_int_supreg..last_int_supreg]) then
  988. begin
  989. addadj(v,u);
  990. inc(degree[v]);
  991. end;
  992. end;
  993. end;
  994. procedure Trgobj.add_edges_used(u:Tsuperregister);
  995. var i:Tsuperregister;
  996. begin
  997. for i:=0 to maxintreg do
  998. if not(i in unusedregsint) then
  999. add_edge(u,i);
  1000. end;
  1001. {$ifdef EXTDEBUG}
  1002. procedure Trgobj.writegraph(loopidx:longint);
  1003. {This procedure writes out the current interference graph in the
  1004. register allocator.}
  1005. var f:text;
  1006. i,j:Tsuperregister;
  1007. begin
  1008. assign(f,'igraph'+tostr(loopidx));
  1009. rewrite(f);
  1010. writeln(f,'Interference graph');
  1011. writeln(f);
  1012. write(f,' ');
  1013. for i:=0 to 15 do
  1014. for j:=0 to 15 do
  1015. write(f,hexstr(i,1));
  1016. writeln(f);
  1017. write(f,' ');
  1018. for i:=0 to 15 do
  1019. write(f,'0123456789ABCDEF');
  1020. writeln(f);
  1021. for i:=0 to 255 do
  1022. begin
  1023. write(f,hexstr(i,2):4);
  1024. for j:=0 to 255 do
  1025. if j in igraph.bitmap[i] then
  1026. write(f,'*')
  1027. else
  1028. write(f,'-');
  1029. writeln(f);
  1030. end;
  1031. close(f);
  1032. end;
  1033. {$endif EXTDEBUG}
  1034. procedure Trgobj.add_to_movelist(u:Tsuperregister;data:Tlinkedlistitem);
  1035. begin
  1036. if movelist[u]=nil then
  1037. begin
  1038. getmem(movelist[u],64);
  1039. movelist[u]^.count:=0;
  1040. end
  1041. else if (movelist[u]^.count and 15)=15 then
  1042. reallocmem(movelist[u],(movelist[u]^.count+1)*4+64);
  1043. movelist[u]^.data[movelist[u]^.count]:=data;
  1044. inc(movelist[u]^.count);
  1045. end;
  1046. procedure Trgobj.add_move_instruction(instr:Taicpu);
  1047. {This procedure notifies a certain as a move instruction so the
  1048. register allocator can try to eliminate it.}
  1049. var i:Tmoveins;
  1050. ssupreg,dsupreg:Tsuperregister;
  1051. begin
  1052. i:=Tmoveins.create;
  1053. i.moveset:=ms_worklist_moves;
  1054. i.instruction:=instr;
  1055. worklist_moves.insert(i);
  1056. ssupreg:=getsupreg(instr.oper[O_MOV_SOURCE].reg);
  1057. add_to_movelist(ssupreg,i);
  1058. dsupreg:=getsupreg(instr.oper[O_MOV_DEST].reg);
  1059. if ssupreg<>dsupreg then
  1060. {Avoid adding the same move instruction twice to a single register.}
  1061. add_to_movelist(dsupreg,i);
  1062. i.x:=ssupreg;
  1063. i.y:=dsupreg;
  1064. end;
  1065. function Trgobj.move_related(n:Tsuperregister):boolean;
  1066. var i:cardinal;
  1067. begin
  1068. move_related:=false;
  1069. if movelist[n]<>nil then
  1070. begin
  1071. for i:=0 to movelist[n]^.count-1 do
  1072. if Tmoveins(movelist[n]^.data[i]).moveset in
  1073. [ms_worklist_moves,ms_active_moves] then
  1074. begin
  1075. move_related:=true;
  1076. break;
  1077. end;
  1078. end;
  1079. end;
  1080. procedure Trgobj.make_work_list;
  1081. var n:Tsuperregister;
  1082. begin
  1083. {If we have 7 cpu registers, and the degree of a node is 7, we cannot
  1084. assign it to any of the registers, thus it is significant.}
  1085. for n:=first_int_imreg to maxintreg do
  1086. if degree[n]>=cpu_registers then
  1087. spillworklist:=spillworklist+char(n)
  1088. else if move_related(n) then
  1089. freezeworklist:=freezeworklist+char(n)
  1090. else
  1091. simplifyworklist:=simplifyworklist+char(n);
  1092. end;
  1093. procedure Trgobj.prepare_colouring;
  1094. begin
  1095. make_work_list;
  1096. active_moves:=Tlinkedlist.create;
  1097. frozen_moves:=Tlinkedlist.create;
  1098. coalesced_moves:=Tlinkedlist.create;
  1099. constrained_moves:=Tlinkedlist.create;
  1100. fillchar(alias,sizeof(alias),0);
  1101. coalescednodes:='';
  1102. selectstack:='';
  1103. end;
  1104. procedure Trgobj.enable_moves(n:Tsuperregister);
  1105. var m:Tlinkedlistitem;
  1106. i:cardinal;
  1107. begin
  1108. if movelist[n]<>nil then
  1109. for i:=0 to movelist[n]^.count-1 do
  1110. begin
  1111. m:=movelist[n]^.data[i];
  1112. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  1113. begin
  1114. if Tmoveins(m).moveset=ms_active_moves then
  1115. begin
  1116. {Move m from the set active_moves to the set worklist_moves.}
  1117. active_moves.remove(m);
  1118. Tmoveins(m).moveset:=ms_worklist_moves;
  1119. worklist_moves.concat(m);
  1120. end;
  1121. end;
  1122. end;
  1123. end;
  1124. procedure Trgobj.decrement_degree(m:Tsuperregister);
  1125. var adj:Pstring;
  1126. d:byte;
  1127. i,p:byte;
  1128. n:char;
  1129. begin
  1130. {$ifdef ALLOWDUPREG}
  1131. if m=RS_INVALID then
  1132. exit;
  1133. {$endif}
  1134. d:=degree[m];
  1135. if degree[m]>0 then
  1136. dec(degree[m]);
  1137. if d=cpu_registers then
  1138. begin
  1139. {Enable moves for m.}
  1140. enable_moves(m);
  1141. {Enable moves for adjacent.}
  1142. adj:=igraph.adjlist[m];
  1143. if adj<>nil then
  1144. for i:=1 to length(adj^) do
  1145. begin
  1146. n:=adj^[i];
  1147. if (pos(n,selectstack) or pos(n,coalescednodes))=0 then
  1148. enable_moves(Tsuperregister(n));
  1149. end;
  1150. {Remove the node from the spillworklist.}
  1151. p:=pos(char(m),spillworklist);
  1152. if p=0 then
  1153. internalerror(200305301); {must be found}
  1154. if length(spillworklist)>1 then
  1155. spillworklist[p]:=spillworklist[length(spillworklist)];
  1156. dec(spillworklist[0]);
  1157. if move_related(m) then
  1158. freezeworklist:=freezeworklist+char(m)
  1159. else
  1160. simplifyworklist:=simplifyworklist+char(m);
  1161. end;
  1162. end;
  1163. procedure Trgobj.simplify;
  1164. var adj:Pstring;
  1165. i,min,p:byte;
  1166. m:char;
  1167. n:Tsuperregister;
  1168. begin
  1169. {We the element with the least interferences out of the
  1170. simplifyworklist.}
  1171. min:=$ff;
  1172. p:=1;
  1173. for i:=1 to length(simplifyworklist) do
  1174. begin
  1175. adj:=igraph.adjlist[Tsuperregister(simplifyworklist[i])];
  1176. if adj=nil then
  1177. begin
  1178. min:=0;
  1179. break; {We won't find smaller ones.}
  1180. end
  1181. else
  1182. if length(adj^)<min then
  1183. begin
  1184. min:=length(adj^);
  1185. if min=0 then
  1186. break; {We won't find smaller ones.}
  1187. p:=i;
  1188. end;
  1189. end;
  1190. n:=Tsuperregister(simplifyworklist[p]);
  1191. if length(simplifyworklist)>1 then
  1192. simplifyworklist[p]:=simplifyworklist[length(simplifyworklist)];
  1193. dec(simplifyworklist[0]);
  1194. {Push it on the selectstack.}
  1195. selectstack:=selectstack+char(n);
  1196. adj:=igraph.adjlist[n];
  1197. if adj<>nil then
  1198. for i:=1 to length(adj^) do
  1199. begin
  1200. m:=adj^[i];
  1201. if ((pos(m,selectstack) or pos(m,coalescednodes))=0) and
  1202. not (Tsuperregister(m) in [first_int_supreg..last_int_supreg]) then
  1203. decrement_degree(Tsuperregister(m));
  1204. end;
  1205. end;
  1206. function Trgobj.get_alias(n:Tsuperregister):Tsuperregister;
  1207. begin
  1208. while pos(char(n),coalescednodes)<>0 do
  1209. n:=alias[n];
  1210. get_alias:=n;
  1211. end;
  1212. procedure Trgobj.add_worklist(u:Tsuperregister);
  1213. var p:byte;
  1214. begin
  1215. if not(u in [first_int_supreg..last_int_supreg]) and
  1216. not move_related(u) and
  1217. (degree[u]<cpu_registers) then
  1218. begin
  1219. p:=pos(char(u),freezeworklist);
  1220. if p=0 then
  1221. internalerror(200308161); {must be found}
  1222. if length(freezeworklist)>1 then
  1223. freezeworklist[p]:=freezeworklist[length(freezeworklist)];
  1224. dec(freezeworklist[0]);
  1225. simplifyworklist:=simplifyworklist+char(u);
  1226. end;
  1227. end;
  1228. function Trgobj.adjacent_ok(u,v:Tsuperregister):boolean;
  1229. {Check wether u and v should be coalesced. u is precoloured.}
  1230. function ok(t,r:Tsuperregister):boolean;
  1231. begin
  1232. ok:=(degree[t]<cpu_registers) or
  1233. (t in [first_int_supreg..last_int_supreg]) or
  1234. (r in igraph.bitmap[t]);
  1235. end;
  1236. var adj:Pstring;
  1237. i:byte;
  1238. t:char;
  1239. begin
  1240. adjacent_ok:=true;
  1241. adj:=igraph.adjlist[v];
  1242. if adj<>nil then
  1243. for i:=1 to length(adj^) do
  1244. begin
  1245. t:=adj^[i];
  1246. if (pos(t,selectstack) or pos(t,coalescednodes))=0 then
  1247. if not ok(Tsuperregister(t),u) then
  1248. begin
  1249. adjacent_ok:=false;
  1250. break;
  1251. end;
  1252. end;
  1253. end;
  1254. function Trgobj.conservative(u,v:Tsuperregister):boolean;
  1255. var adj:Pstring;
  1256. done:set of char; {To prevent that we count nodes twice.}
  1257. i,k:byte;
  1258. n:char;
  1259. begin
  1260. k:=0;
  1261. done:=[];
  1262. adj:=igraph.adjlist[u];
  1263. if adj<>nil then
  1264. for i:=1 to length(adj^) do
  1265. begin
  1266. n:=adj^[i];
  1267. if (pos(n,selectstack) or pos(n,coalescednodes))=0 then
  1268. begin
  1269. include(done,n);
  1270. if degree[Tsuperregister(n)]>=cpu_registers then
  1271. inc(k);
  1272. end;
  1273. end;
  1274. adj:=igraph.adjlist[v];
  1275. if adj<>nil then
  1276. for i:=1 to length(adj^) do
  1277. begin
  1278. n:=adj^[i];
  1279. if ((pos(n,selectstack) or pos(n,coalescednodes))=0) and
  1280. not (n in done) and
  1281. (degree[Tsuperregister(n)]>=cpu_registers) then
  1282. inc(k);
  1283. end;
  1284. conservative:=(k<cpu_registers);
  1285. end;
  1286. procedure Trgobj.combine(u,v:Tsuperregister);
  1287. var add:boolean;
  1288. adj:Pstring;
  1289. i,p:byte;
  1290. n,o:cardinal;
  1291. t:char;
  1292. decrement:boolean;
  1293. begin
  1294. p:=pos(char(v),freezeworklist);
  1295. if p<>0 then
  1296. delete(freezeworklist,p,1)
  1297. else
  1298. delete(spillworklist,pos(char(v),spillworklist),1);
  1299. coalescednodes:=coalescednodes+char(v);
  1300. alias[v]:=u;
  1301. {Combine both movelists. Since the movelists are sets, only add
  1302. elements that are not already present.}
  1303. if assigned(movelist[v]) then
  1304. begin
  1305. for n:=0 to movelist[v]^.count-1 do
  1306. begin
  1307. add:=true;
  1308. for o:=0 to movelist[u]^.count-1 do
  1309. if movelist[u]^.data[o]=movelist[v]^.data[n] then
  1310. begin
  1311. add:=false;
  1312. break;
  1313. end;
  1314. if add then
  1315. add_to_movelist(u,movelist[v]^.data[n]);
  1316. end;
  1317. enable_moves(v);
  1318. end;
  1319. adj:=igraph.adjlist[v];
  1320. if adj<>nil then
  1321. for i:=1 to length(adj^) do
  1322. begin
  1323. t:=adj^[i];
  1324. if (pos(t,selectstack) or pos(t,coalescednodes))=0 then
  1325. begin
  1326. decrement:=(Tsuperregister(t)<>u) and not(u in igraph.bitmap[Tsuperregister(t)]);
  1327. add_edge(Tsuperregister(t),u);
  1328. {Do not call decrement_degree because it might move nodes between
  1329. lists while the degree does not change (add_edge will increase it).
  1330. Instead, we will decrement manually. (Only if the degree has been
  1331. increased.)}
  1332. if decrement and not (Tsuperregister(t) in [first_int_supreg..last_int_supreg])
  1333. and (degree[Tsuperregister(t)]>0) then
  1334. dec(degree[Tsuperregister(t)]);
  1335. end;
  1336. end;
  1337. p:=pos(char(u),freezeworklist);
  1338. if (degree[u]>=cpu_registers) and (p<>0) then
  1339. begin
  1340. delete(freezeworklist,p,1);
  1341. spillworklist:=spillworklist+char(u);
  1342. end;
  1343. end;
  1344. procedure Trgobj.coalesce;
  1345. var m:Tmoveins;
  1346. x,y,u,v:Tsuperregister;
  1347. begin
  1348. m:=Tmoveins(worklist_moves.getfirst);
  1349. x:=get_alias(getsupreg(m.instruction.oper[0].reg));
  1350. y:=get_alias(getsupreg(m.instruction.oper[1].reg));
  1351. if y in [first_int_supreg..last_int_supreg] then
  1352. begin
  1353. u:=y;
  1354. v:=x;
  1355. end
  1356. else
  1357. begin
  1358. u:=x;
  1359. v:=y;
  1360. end;
  1361. if (u=v) then
  1362. begin
  1363. m.moveset:=ms_coalesced_moves; {Already coalesced.}
  1364. coalesced_moves.insert(m);
  1365. add_worklist(u);
  1366. end
  1367. {Do u and v interfere? In that case the move is constrained. Two
  1368. precoloured nodes interfere allways. If v is precoloured, by the above
  1369. code u is precoloured, thus interference...}
  1370. else if (v in [first_int_supreg..last_int_supreg]) or (u in igraph.bitmap[v]) then
  1371. begin
  1372. m.moveset:=ms_constrained_moves; {Cannot coalesce yet...}
  1373. constrained_moves.insert(m);
  1374. add_worklist(u);
  1375. add_worklist(v);
  1376. end
  1377. {Next test: is it possible and a good idea to coalesce??}
  1378. else if ((u in [first_int_supreg..last_int_supreg]) and adjacent_ok(u,v)) or
  1379. (not(u in [first_int_supreg..last_int_supreg]) and conservative(u,v)) then
  1380. begin
  1381. m.moveset:=ms_coalesced_moves; {Move coalesced!}
  1382. coalesced_moves.insert(m);
  1383. combine(u,v);
  1384. add_worklist(u);
  1385. end
  1386. else
  1387. begin
  1388. m.moveset:=ms_active_moves;
  1389. active_moves.insert(m);
  1390. end;
  1391. end;
  1392. procedure Trgobj.freeze_moves(u:Tsuperregister);
  1393. var i:cardinal;
  1394. m:Tlinkedlistitem;
  1395. v,x,y:Tsuperregister;
  1396. begin
  1397. if movelist[u]<>nil then
  1398. for i:=0 to movelist[u]^.count-1 do
  1399. begin
  1400. m:=movelist[u]^.data[i];
  1401. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  1402. begin
  1403. x:=getsupreg(Tmoveins(m).instruction.oper[0].reg);
  1404. y:=getsupreg(Tmoveins(m).instruction.oper[1].reg);
  1405. if get_alias(y)=get_alias(u) then
  1406. v:=get_alias(x)
  1407. else
  1408. v:=get_alias(y);
  1409. {Move m from active_moves/worklist_moves to frozen_moves.}
  1410. if Tmoveins(m).moveset=ms_active_moves then
  1411. active_moves.remove(m)
  1412. else
  1413. worklist_moves.remove(m);
  1414. Tmoveins(m).moveset:=ms_frozen_moves;
  1415. frozen_moves.insert(m);
  1416. if not(v in [first_int_supreg..last_int_supreg]) and
  1417. not(move_related(v)) and
  1418. (degree[v]<cpu_registers) then
  1419. begin
  1420. delete(freezeworklist,pos(char(v),freezeworklist),1);
  1421. simplifyworklist:=simplifyworklist+char(v);
  1422. end;
  1423. end;
  1424. end;
  1425. end;
  1426. procedure Trgobj.freeze;
  1427. var n:Tsuperregister;
  1428. begin
  1429. {We need to take a random element out of the freezeworklist. We take
  1430. the last element. Dirty code!}
  1431. n:=Tsuperregister(freezeworklist[byte(freezeworklist[0])]);
  1432. dec(freezeworklist[0]);
  1433. {Add it to the simplifyworklist.}
  1434. simplifyworklist:=simplifyworklist+char(n);
  1435. freeze_moves(n);
  1436. end;
  1437. procedure Trgobj.select_spill;
  1438. var
  1439. n : char;
  1440. begin
  1441. {This code is WAY too naive. We need not to select just a register, but
  1442. the register that is used the least...}
  1443. n:=spillworklist[byte(spillworklist[0])];
  1444. dec(spillworklist[0]);
  1445. simplifyworklist:=simplifyworklist+n;
  1446. freeze_moves(Tsuperregister(n));
  1447. end;
  1448. procedure Trgobj.assign_colours;
  1449. {Assign_colours assigns the actual colours to the registers.}
  1450. var adj:Pstring;
  1451. i,j,k:byte;
  1452. n,a,c:Tsuperregister;
  1453. adj_colours,colourednodes:set of Tsuperregister;
  1454. w:char;
  1455. begin
  1456. spillednodes:='';
  1457. {Reset colours}
  1458. for i:=0 to maxintreg do
  1459. colour[i]:=i;
  1460. {Colour the cpu registers...}
  1461. colourednodes:=[first_int_supreg..last_int_supreg];
  1462. {Now colour the imaginary registers on the select-stack.}
  1463. for i:=length(selectstack) downto 1 do
  1464. begin
  1465. n:=Tsuperregister(selectstack[i]);
  1466. {Create a list of colours that we cannot assign to n.}
  1467. adj_colours:=[];
  1468. adj:=igraph.adjlist[n];
  1469. if adj<>nil then
  1470. for j:=1 to length(adj^) do
  1471. begin
  1472. w:=adj^[j];
  1473. a:=get_alias(Tsuperregister(w));
  1474. if a in colourednodes then
  1475. include(adj_colours,colour[a]);
  1476. end;
  1477. include(adj_colours,RS_STACK_POINTER_REG);
  1478. {Assume a spill by default...}
  1479. spillednodes:=spillednodes+char(n);
  1480. {Search for a colour not in this list.}
  1481. for k:=1 to length(usable_registers) do
  1482. begin
  1483. c:=Tsuperregister(usable_registers[k]);
  1484. if not(c in adj_colours) then
  1485. begin
  1486. colour[n]:=c;
  1487. dec(spillednodes[0]); {Colour found: no spill.}
  1488. include(colourednodes,n);
  1489. if n in used_in_proc_int then
  1490. include(used_in_proc_int,c);
  1491. break;
  1492. end;
  1493. end;
  1494. end;
  1495. {Finally colour the nodes that were coalesced.}
  1496. for i:=1 to length(coalescednodes) do
  1497. begin
  1498. n:=Tsuperregister(coalescednodes[i]);
  1499. k:=get_alias(n);
  1500. colour[n]:=colour[k];
  1501. if n in used_in_proc_int then
  1502. include(used_in_proc_int,colour[k]);
  1503. end;
  1504. {$ifdef ra_debug}
  1505. if aktfilepos.line=-1 then
  1506. begin
  1507. writeln('colourlist ',length(freezeworklist));
  1508. for i:=0 to maxintreg do
  1509. writeln(i:4,' ',colour[i]:4)
  1510. end;
  1511. {$endif ra_debug}
  1512. end;
  1513. procedure Trgobj.colour_registers;
  1514. begin
  1515. repeat
  1516. if length(simplifyworklist)<>0 then
  1517. simplify
  1518. else if not(worklist_moves.empty) then
  1519. coalesce
  1520. else if length(freezeworklist)<>0 then
  1521. freeze
  1522. else if length(spillworklist)<>0 then
  1523. select_spill;
  1524. until (length(simplifyworklist)=0) and
  1525. worklist_moves.empty and
  1526. (length(freezeworklist)=0) and
  1527. (length(spillworklist)=0);
  1528. assign_colours;
  1529. end;
  1530. procedure Trgobj.epilogue_colouring;
  1531. {
  1532. procedure move_to_worklist_moves(list:Tlinkedlist);
  1533. var p:Tlinkedlistitem;
  1534. begin
  1535. p:=list.first;
  1536. while p<>nil do
  1537. begin
  1538. Tmoveins(p).moveset:=ms_worklist_moves;
  1539. p:=p.next;
  1540. end;
  1541. worklist_moves.concatlist(list);
  1542. end;
  1543. }
  1544. var i:Tsuperregister;
  1545. begin
  1546. worklist_moves.clear;
  1547. {$ifdef Principle_wrong_by_definition}
  1548. {Move everything back to worklist_moves.}
  1549. move_to_worklist_moves(active_moves);
  1550. move_to_worklist_moves(frozen_moves);
  1551. move_to_worklist_moves(coalesced_moves);
  1552. move_to_worklist_moves(constrained_moves);
  1553. {$endif Principle_wrong_by_definition}
  1554. active_moves.destroy;
  1555. active_moves:=nil;
  1556. frozen_moves.destroy;
  1557. frozen_moves:=nil;
  1558. coalesced_moves.destroy;
  1559. coalesced_moves:=nil;
  1560. constrained_moves.destroy;
  1561. constrained_moves:=nil;
  1562. for i:=0 to 255 do
  1563. if movelist[i]<>nil then
  1564. begin
  1565. dispose(movelist[i]);
  1566. movelist[i]:=0;
  1567. end;
  1568. end;
  1569. procedure Trgobj.clear_interferences(u:Tsuperregister);
  1570. {Remove node u from the interference graph and remove all collected
  1571. move instructions it is associated with.}
  1572. var i:byte;
  1573. j,k,count:cardinal;
  1574. v:Tsuperregister;
  1575. m,n:Tmoveins;
  1576. begin
  1577. if igraph.adjlist[u]<>nil then
  1578. begin
  1579. for i:=1 to length(igraph.adjlist[u]^) do
  1580. begin
  1581. v:=Tsuperregister(igraph.adjlist[u]^[i]);
  1582. {Remove (u,v) and (v,u) from bitmap.}
  1583. exclude(igraph.bitmap[u],v);
  1584. exclude(igraph.bitmap[v],u);
  1585. {Remove (v,u) from adjacency list.}
  1586. if igraph.adjlist[v]<>nil then
  1587. begin
  1588. delete(igraph.adjlist[v]^,pos(char(v),igraph.adjlist[v]^),1);
  1589. if length(igraph.adjlist[v]^)=0 then
  1590. begin
  1591. dispose(igraph.adjlist[v]);
  1592. igraph.adjlist[v]:=nil;
  1593. end;
  1594. end;
  1595. end;
  1596. {Remove ( u,* ) from adjacency list.}
  1597. dispose(igraph.adjlist[u]);
  1598. igraph.adjlist[u]:=nil;
  1599. end;
  1600. {$ifdef Principle_wrong_by_definition}
  1601. {Now remove the moves.}
  1602. if movelist[u]<>nil then
  1603. begin
  1604. for j:=0 to movelist[u]^.count-1 do
  1605. begin
  1606. m:=Tmoveins(movelist[u]^.data[j]);
  1607. {Get the other register of the move instruction.}
  1608. v:=m.instruction.oper[0].reg.number shr 8;
  1609. if v=u then
  1610. v:=m.instruction.oper[1].reg.number shr 8;
  1611. repeat
  1612. repeat
  1613. if (u<>v) and (movelist[v]<>nil) then
  1614. begin
  1615. {Remove the move from it's movelist.}
  1616. count:=movelist[v]^.count-1;
  1617. for k:=0 to count do
  1618. if m=movelist[v]^.data[k] then
  1619. begin
  1620. if k<>count then
  1621. movelist[v]^.data[k]:=movelist[v]^.data[count];
  1622. dec(movelist[v]^.count);
  1623. if count=0 then
  1624. begin
  1625. dispose(movelist[v]);
  1626. movelist[v]:=nil;
  1627. end;
  1628. break;
  1629. end;
  1630. end;
  1631. {The complexity is enourmous: the register might have been
  1632. coalesced. In that case it's movelists have been added to
  1633. it's coalescing alias. (DM)}
  1634. v:=alias[v];
  1635. until v=0;
  1636. {And also register u might have been coalesced.}
  1637. u:=alias[u];
  1638. until u=0;
  1639. case m.moveset of
  1640. ms_coalesced_moves:
  1641. coalesced_moves.remove(m);
  1642. ms_constrained_moves:
  1643. constrained_moves.remove(m);
  1644. ms_frozen_moves:
  1645. frozen_moves.remove(m);
  1646. ms_worklist_moves:
  1647. worklist_moves.remove(m);
  1648. ms_active_moves:
  1649. active_moves.remove(m);
  1650. end;
  1651. end;
  1652. dispose(movelist[u]);
  1653. movelist[u]:=nil;
  1654. end;
  1655. {$endif Principle_wrong_by_definition}
  1656. end;
  1657. procedure Trgobj.getregisterintinline(list:Taasmoutput;position:Tai;subreg:Tsubregister;var result:Tregister);
  1658. var min,p,i:Tsuperregister;
  1659. r:Tregister;
  1660. adj:Pstring;
  1661. begin
  1662. if maxintreg<last_int_imreg then
  1663. begin
  1664. inc(maxintreg);
  1665. p:=maxintreg;
  1666. min:=0;
  1667. end
  1668. else
  1669. begin
  1670. min:=$ff;
  1671. p:=first_int_imreg;
  1672. for i:=first_int_imreg to maxintreg do
  1673. if (i in unusedregsint) and
  1674. (pos(char(i),abtlist)=0) and
  1675. (pos(char(i),spillednodes)=0) then
  1676. begin
  1677. adj:=igraph.adjlist[Tsuperregister(i)];
  1678. if adj=nil then
  1679. begin
  1680. p:=i;
  1681. min:=0;
  1682. break; {We won't find smaller ones.}
  1683. end
  1684. else
  1685. if length(adj^)<min then
  1686. begin
  1687. p:=i;
  1688. min:=length(adj^);
  1689. if min=0 then
  1690. break; {We won't find smaller ones.}
  1691. end;
  1692. end;
  1693. if min=$ff then
  1694. begin
  1695. {$ifdef ALLOWDUPREG}
  1696. result:=newreg(R_INTREGISTER,RS_INVALID,subreg);
  1697. exit;
  1698. {$else}
  1699. internalerror(10);
  1700. {$endif}
  1701. end;
  1702. end;
  1703. {$ifdef ra_debug}
  1704. writeln('Spilling temp: ',p,' min ',min);
  1705. {$endif ra_debug}
  1706. exclude(unusedregsint,p);
  1707. include(used_in_proc_int,p);
  1708. r:=newreg(R_INTREGISTER,p,subreg);
  1709. if position=nil then
  1710. list.insert(Tai_regalloc.alloc(r))
  1711. else
  1712. list.insertafter(Tai_regalloc.alloc(r),position);
  1713. add_edges_used(p);
  1714. add_constraints(r);
  1715. result:=r;
  1716. end;
  1717. {In some cases we can get in big trouble. See this example:
  1718. ; register reg23d released
  1719. ; register eax allocated
  1720. ; register ebx allocated
  1721. ; register ecx allocated
  1722. ; register edx allocated
  1723. ; register esi allocated
  1724. ; register edi allocated
  1725. call [reg23d]
  1726. This code is ok, *except* when reg23d is spilled. In that case the
  1727. spilled would introduce a help register which can never get
  1728. allocated to a real register because it interferes with all of them.
  1729. To solve this we introduce the ABT ("avoid big trouble :)" ) registers.
  1730. If you allocate an ABT register you get a register that has less
  1731. than cpu_register interferences and will not be allocated ever again
  1732. by the normal register get procedures. In other words it is for sure it
  1733. will never get spilled.}
  1734. function Trgobj.getabtregisterint(list:Taasmoutput;size:Tcgsize):Tregister;
  1735. var p,i:Tsuperregister;
  1736. r:Tregister;
  1737. subreg:tsubregister;
  1738. found:boolean;
  1739. min : byte;
  1740. adj:Pstring;
  1741. begin
  1742. min:=$ff;
  1743. for i:=1 to length(abtlist) do
  1744. if Tsuperregister(abtlist[i]) in unusedregsint then
  1745. begin
  1746. p:=tsuperregister(abtlist[i]);
  1747. min:=0;
  1748. break;
  1749. end;
  1750. if min>0 then
  1751. begin
  1752. if maxintreg<last_int_imreg then
  1753. begin
  1754. inc(maxintreg);
  1755. p:=maxintreg;
  1756. min:=0;
  1757. end
  1758. else
  1759. begin
  1760. p:=first_int_imreg;
  1761. for i:=first_int_imreg to maxintreg do
  1762. if (i in unusedregsint) and
  1763. ((igraph.adjlist[i]=nil) or
  1764. (length(igraph.adjlist[i]^)<cpu_registers)) then
  1765. begin
  1766. adj:=igraph.adjlist[i];
  1767. if adj=nil then
  1768. begin
  1769. p:=i;
  1770. min:=0;
  1771. break; {We won't find smaller ones.}
  1772. end
  1773. else
  1774. if length(adj^)<min then
  1775. begin
  1776. p:=i;
  1777. min:=length(adj^);
  1778. if min=0 then
  1779. break; {We won't find smaller ones.}
  1780. end;
  1781. end;
  1782. end;
  1783. if min=$ff then
  1784. begin
  1785. {$ifdef ALLOWDUPREG}
  1786. result:=newreg(R_INTREGISTER,RS_INVALID,cgsize2subreg(size));
  1787. exit;
  1788. {$else}
  1789. internalerror(10);
  1790. {$endif}
  1791. end;
  1792. end;
  1793. exclude(unusedregsint,p);
  1794. include(used_in_proc_int,p);
  1795. subreg:=cgsize2subreg(size);
  1796. r:=newreg(R_INTREGISTER,p,subreg);
  1797. list.concat(Tai_regalloc.alloc(r));
  1798. getabtregisterint:=r;
  1799. add_edges_used(p);
  1800. add_constraints(r);
  1801. if pos(char(p),abtlist)=0 then
  1802. abtlist:=abtlist+char(p);
  1803. end;
  1804. procedure Trgobj.ungetregisterintinline(list:Taasmoutput;position:Tai;r:Tregister);
  1805. var supreg:Tsuperregister;
  1806. begin
  1807. supreg:=getsupreg(r);
  1808. include(unusedregsint,supreg);
  1809. if position=nil then
  1810. list.insert(Tai_regalloc.dealloc(r))
  1811. else
  1812. list.insertafter(Tai_regalloc.dealloc(r),position);
  1813. add_edges_used(supreg);
  1814. add_constraints(r);
  1815. end;
  1816. function Trgobj.spill_registers(list:Taasmoutput;headertai:tai;const regs_to_spill:string):boolean;
  1817. {Returns true if any help registers have been used.}
  1818. var i:byte;
  1819. p,q:Tai;
  1820. regs_to_spill_set:Tsuperregisterset;
  1821. spill_temps:^Tspill_temp_list;
  1822. templist : taasmoutput;
  1823. supreg : tsuperregister;
  1824. begin
  1825. aktfilepos:=current_procinfo.entrypos;
  1826. spill_registers:=false;
  1827. unusedregsint:=[0..255];
  1828. fillchar(degree,sizeof(degree),0);
  1829. {Precoloured nodes should have an infinite degree, which we can approach
  1830. by 255.}
  1831. for i:=first_int_supreg to last_int_supreg do
  1832. degree[i]:=255;
  1833. { exclude(unusedregsint,RS_STACK_POINTER_REG);}
  1834. if current_procinfo.framepointer=NR_FRAME_POINTER_REG then
  1835. {Make sure the register allocator won't allocate registers into ebp.}
  1836. exclude(unusedregsint,RS_FRAME_POINTER_REG);
  1837. new(spill_temps);
  1838. fillchar(spill_temps^,sizeof(spill_temps^),0);
  1839. regs_to_spill_set:=[];
  1840. { Allocate temps and insert in front of the list }
  1841. templist:=taasmoutput.create;
  1842. for i:=1 to length(regs_to_spill) do
  1843. begin
  1844. {Alternative representation.}
  1845. include(regs_to_spill_set,Tsuperregister(regs_to_spill[i]));
  1846. {Clear all interferences of the spilled register.}
  1847. clear_interferences(Tsuperregister(regs_to_spill[i]));
  1848. {Get a temp for the spilled register}
  1849. tg.gettemp(templist,4,tt_noreuse,spill_temps^[Tsuperregister(regs_to_spill[i])]);
  1850. end;
  1851. list.insertlistafter(headertai,templist);
  1852. templist.free;
  1853. { Walk through all instructions, we can start with the headertai,
  1854. because before the header tai is only symbols }
  1855. p:=headertai;
  1856. while assigned(p) do
  1857. begin
  1858. case p.typ of
  1859. ait_regalloc:
  1860. begin
  1861. {A register allocation of a spilled register can be removed.}
  1862. supreg:=getsupreg(Tai_regalloc(p).reg);
  1863. if supreg in regs_to_spill_set then
  1864. begin
  1865. q:=p;
  1866. p:=Tai(p.next);
  1867. list.remove(q);
  1868. continue;
  1869. end
  1870. else
  1871. if Tai_regalloc(p).allocation then
  1872. exclude(unusedregsint,supreg)
  1873. else
  1874. include(unusedregsint,supreg);
  1875. end;
  1876. ait_instruction:
  1877. begin
  1878. aktfilepos:=Taicpu_abstract(p).fileinfo
  1879. ;
  1880. if Taicpu_abstract(p).spill_registers(list,@getregisterintinline,
  1881. @ungetregisterintinline,
  1882. regs_to_spill_set,
  1883. unusedregsint,
  1884. spill_temps^) then
  1885. spill_registers:=true;
  1886. if Taicpu_abstract(p).is_move then
  1887. add_move_instruction(Taicpu(p));
  1888. end;
  1889. end;
  1890. p:=Tai(p.next);
  1891. end;
  1892. aktfilepos:=current_procinfo.exitpos;
  1893. for i:=1 to length(regs_to_spill) do
  1894. begin
  1895. tg.ungettemp(list,spill_temps^[Tsuperregister(regs_to_spill[i])]);
  1896. end;
  1897. dispose(spill_temps);
  1898. end;
  1899. {****************************************************************************
  1900. TReference
  1901. ****************************************************************************}
  1902. procedure reference_reset(var ref : treference);
  1903. begin
  1904. FillChar(ref,sizeof(treference),0);
  1905. {$ifdef arm}
  1906. ref.signindex:=1;
  1907. {$endif arm}
  1908. end;
  1909. procedure reference_reset_base(var ref : treference;base : tregister;offset : longint);
  1910. begin
  1911. reference_reset(ref);
  1912. ref.base:=base;
  1913. ref.offset:=offset;
  1914. end;
  1915. procedure reference_reset_symbol(var ref : treference;sym : tasmsymbol;offset : longint);
  1916. begin
  1917. reference_reset(ref);
  1918. ref.symbol:=sym;
  1919. ref.offset:=offset;
  1920. end;
  1921. procedure reference_release(list: taasmoutput; const ref : treference);
  1922. begin
  1923. rg.ungetreference(list,ref);
  1924. end;
  1925. function references_equal(sref : treference;dref : treference):boolean;
  1926. begin
  1927. references_equal:=CompareByte(sref,dref,sizeof(treference))=0;
  1928. end;
  1929. { on most processors , this routine does nothing, overriden currently }
  1930. { only by 80x86 processor. }
  1931. function trgobj.makeregsize(reg: tregister; size: tcgsize): tregister;
  1932. begin
  1933. makeregsize := reg;
  1934. end;
  1935. {****************************************************************************
  1936. TLocation
  1937. ****************************************************************************}
  1938. procedure location_reset(var l : tlocation;lt:TCGLoc;lsize:TCGSize);
  1939. begin
  1940. FillChar(l,sizeof(tlocation),0);
  1941. l.loc:=lt;
  1942. l.size:=lsize;
  1943. {$ifdef arm}
  1944. if l.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  1945. l.reference.signindex:=1;
  1946. {$endif arm}
  1947. end;
  1948. procedure location_release(list: taasmoutput; const l : tlocation);
  1949. begin
  1950. case l.loc of
  1951. LOC_REGISTER,LOC_CREGISTER :
  1952. begin
  1953. rg.ungetregisterint(list,l.register);
  1954. if l.size in [OS_64,OS_S64] then
  1955. rg.ungetregisterint(list,l.registerhigh);
  1956. end;
  1957. LOC_FPUREGISTER,LOC_CFPUREGISTER :
  1958. rg.ungetregisterfpu(list,l.register,l.size);
  1959. LOC_CREFERENCE,LOC_REFERENCE :
  1960. rg.ungetreference(list, l.reference);
  1961. end;
  1962. end;
  1963. procedure location_freetemp(list:taasmoutput; const l : tlocation);
  1964. begin
  1965. if (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  1966. tg.ungetiftemp(list,l.reference);
  1967. end;
  1968. procedure location_copy(var destloc:tlocation; const sourceloc : tlocation);
  1969. begin
  1970. destloc:=sourceloc;
  1971. end;
  1972. procedure location_swap(var destloc,sourceloc : tlocation);
  1973. var
  1974. swapl : tlocation;
  1975. begin
  1976. swapl := destloc;
  1977. destloc := sourceloc;
  1978. sourceloc := swapl;
  1979. end;
  1980. end.
  1981. {
  1982. $Log$
  1983. Revision 1.81 2003-10-01 20:34:49 peter
  1984. * procinfo unit contains tprocinfo
  1985. * cginfo renamed to cgbase
  1986. * moved cgmessage to verbose
  1987. * fixed ppc and sparc compiles
  1988. Revision 1.80 2003/09/30 19:54:42 peter
  1989. * reuse registers with the least conflicts
  1990. Revision 1.79 2003/09/29 20:58:56 peter
  1991. * optimized releasing of registers
  1992. Revision 1.78 2003/09/28 13:41:12 peter
  1993. * return reg 255 when allowdupreg is defined
  1994. Revision 1.77 2003/09/25 16:19:32 peter
  1995. * fix filepositions
  1996. * insert spill temp allocations at the start of the proc
  1997. Revision 1.76 2003/09/16 16:17:01 peter
  1998. * varspez in calls to push_addr_param
  1999. Revision 1.75 2003/09/12 19:07:42 daniel
  2000. * Fixed fast spilling functionality by re-adding the code that initializes
  2001. precoloured nodes to degree 255. I would like to play hangman on the one
  2002. who removed that code.
  2003. Revision 1.74 2003/09/11 11:54:59 florian
  2004. * improved arm code generation
  2005. * move some protected and private field around
  2006. * the temp. register for register parameters/arguments are now released
  2007. before the move to the parameter register is done. This improves
  2008. the code in a lot of cases.
  2009. Revision 1.73 2003/09/09 20:59:27 daniel
  2010. * Adding register allocation order
  2011. Revision 1.72 2003/09/09 15:55:44 peter
  2012. * use register with least interferences in spillregister
  2013. Revision 1.71 2003/09/07 22:09:35 peter
  2014. * preparations for different default calling conventions
  2015. * various RA fixes
  2016. Revision 1.70 2003/09/03 21:06:45 peter
  2017. * fixes for FPU register allocation
  2018. Revision 1.69 2003/09/03 15:55:01 peter
  2019. * NEWRA branch merged
  2020. Revision 1.68 2003/09/03 11:18:37 florian
  2021. * fixed arm concatcopy
  2022. + arm support in the common compiler sources added
  2023. * moved some generic cg code around
  2024. + tfputype added
  2025. * ...
  2026. Revision 1.67.2.5 2003/08/31 20:44:07 peter
  2027. * fixed getexplicitregisterint tregister value
  2028. Revision 1.67.2.4 2003/08/31 20:40:50 daniel
  2029. * Fixed add_edges_used
  2030. Revision 1.67.2.3 2003/08/29 17:28:59 peter
  2031. * next batch of updates
  2032. Revision 1.67.2.2 2003/08/28 18:35:08 peter
  2033. * tregister changed to cardinal
  2034. Revision 1.67.2.1 2003/08/27 19:55:54 peter
  2035. * first tregister patch
  2036. Revision 1.67 2003/08/23 10:46:21 daniel
  2037. * Register allocator bugfix for h2pas
  2038. Revision 1.66 2003/08/17 16:59:20 jonas
  2039. * fixed regvars so they work with newra (at least for ppc)
  2040. * fixed some volatile register bugs
  2041. + -dnotranslation option for -dnewra, which causes the registers not to
  2042. be translated from virtual to normal registers. Requires support in
  2043. the assembler writer as well, which is only implemented in aggas/
  2044. agppcgas currently
  2045. Revision 1.65 2003/08/17 14:32:48 daniel
  2046. * Precoloured nodes now have an infinite degree approached with 255,
  2047. like they should.
  2048. Revision 1.64 2003/08/17 08:48:02 daniel
  2049. * Another register allocator bug fixed.
  2050. * cpu_registers set to 6 for i386
  2051. Revision 1.63 2003/08/09 18:56:54 daniel
  2052. * cs_regalloc renamed to cs_regvars to avoid confusion with register
  2053. allocator
  2054. * Some preventive changes to i386 spillinh code
  2055. Revision 1.62 2003/08/03 14:09:50 daniel
  2056. * Fixed a register allocator bug
  2057. * Figured out why -dnewra generates superfluous "mov reg1,reg2"
  2058. statements: changes in location_force. These moves are now no longer
  2059. constrained so they are optimized away.
  2060. Revision 1.61 2003/07/21 13:32:39 jonas
  2061. * add_edges_used() is now also called for registers allocated with
  2062. getexplicitregisterint()
  2063. * writing the intereference graph is now only done with -dradebug2 and
  2064. the created files are now called "igraph.<module_name>"
  2065. Revision 1.60 2003/07/06 15:31:21 daniel
  2066. * Fixed register allocator. *Lots* of fixes.
  2067. Revision 1.59 2003/07/06 15:00:47 jonas
  2068. * fixed my previous completely broken commit. It's not perfect though,
  2069. registers > last_int_supreg and < max_intreg may still be "translated"
  2070. Revision 1.58 2003/07/06 14:45:05 jonas
  2071. * support integer registers that are not managed by newra (ie. don't
  2072. translate register numbers that fall outside the range
  2073. first_int_supreg..last_int_supreg)
  2074. Revision 1.57 2003/07/02 22:18:04 peter
  2075. * paraloc splitted in callerparaloc,calleeparaloc
  2076. * sparc calling convention updates
  2077. Revision 1.56 2003/06/17 16:34:44 jonas
  2078. * lots of newra fixes (need getfuncretparaloc implementation for i386)!
  2079. * renamed all_intregisters to volatile_intregisters and made it
  2080. processor dependent
  2081. Revision 1.55 2003/06/14 14:53:50 jonas
  2082. * fixed newra cycle for x86
  2083. * added constants for indicating source and destination operands of the
  2084. "move reg,reg" instruction to aasmcpu (and use those in rgobj)
  2085. Revision 1.54 2003/06/13 21:19:31 peter
  2086. * current_procdef removed, use current_procinfo.procdef instead
  2087. Revision 1.53 2003/06/12 21:11:10 peter
  2088. * ungetregisterfpu gets size parameter
  2089. Revision 1.52 2003/06/12 16:43:07 peter
  2090. * newra compiles for sparc
  2091. Revision 1.51 2003/06/09 14:54:26 jonas
  2092. * (de)allocation of registers for parameters is now performed properly
  2093. (and checked on the ppc)
  2094. - removed obsolete allocation of all parameter registers at the start
  2095. of a procedure (and deallocation at the end)
  2096. Revision 1.50 2003/06/03 21:11:09 peter
  2097. * cg.a_load_* get a from and to size specifier
  2098. * makeregsize only accepts newregister
  2099. * i386 uses generic tcgnotnode,tcgunaryminus
  2100. Revision 1.49 2003/06/03 13:01:59 daniel
  2101. * Register allocator finished
  2102. Revision 1.48 2003/06/01 21:38:06 peter
  2103. * getregisterfpu size parameter added
  2104. * op_const_reg size parameter added
  2105. * sparc updates
  2106. Revision 1.47 2003/05/31 20:31:11 jonas
  2107. * set inital costs of assigning a variable to a register to 120 for
  2108. non-i386, because the used register must be store to memory at the
  2109. start and loaded again at the end
  2110. Revision 1.46 2003/05/30 18:55:21 jonas
  2111. * fixed several regvar related bugs for non-i386. make cycle with -Or now
  2112. works for ppc
  2113. Revision 1.45 2003/05/30 12:36:13 jonas
  2114. * use as little different registers on the ppc until newra is released,
  2115. since every used register must be saved
  2116. Revision 1.44 2003/05/17 13:30:08 jonas
  2117. * changed tt_persistant to tt_persistent :)
  2118. * tempcreatenode now doesn't accept a boolean anymore for persistent
  2119. temps, but a ttemptype, so you can also create ansistring temps etc
  2120. Revision 1.43 2003/05/16 14:33:31 peter
  2121. * regvar fixes
  2122. Revision 1.42 2003/04/26 20:03:49 daniel
  2123. * Bug fix in simplify
  2124. Revision 1.41 2003/04/25 20:59:35 peter
  2125. * removed funcretn,funcretsym, function result is now in varsym
  2126. and aliases for result and function name are added using absolutesym
  2127. * vs_hidden parameter for funcret passed in parameter
  2128. * vs_hidden fixes
  2129. * writenode changed to printnode and released from extdebug
  2130. * -vp option added to generate a tree.log with the nodetree
  2131. * nicer printnode for statements, callnode
  2132. Revision 1.40 2003/04/25 08:25:26 daniel
  2133. * Ifdefs around a lot of calls to cleartempgen
  2134. * Fixed registers that are allocated but not freed in several nodes
  2135. * Tweak to register allocator to cause less spills
  2136. * 8-bit registers now interfere with esi,edi and ebp
  2137. Compiler can now compile rtl successfully when using new register
  2138. allocator
  2139. Revision 1.39 2003/04/23 20:23:06 peter
  2140. * compile fix for no-newra
  2141. Revision 1.38 2003/04/23 14:42:07 daniel
  2142. * Further register allocator work. Compiler now smaller with new
  2143. allocator than without.
  2144. * Somebody forgot to adjust ppu version number
  2145. Revision 1.37 2003/04/22 23:50:23 peter
  2146. * firstpass uses expectloc
  2147. * checks if there are differences between the expectloc and
  2148. location.loc from secondpass in EXTDEBUG
  2149. Revision 1.36 2003/04/22 10:09:35 daniel
  2150. + Implemented the actual register allocator
  2151. + Scratch registers unavailable when new register allocator used
  2152. + maybe_save/maybe_restore unavailable when new register allocator used
  2153. Revision 1.35 2003/04/21 19:16:49 peter
  2154. * count address regs separate
  2155. Revision 1.34 2003/04/17 16:48:21 daniel
  2156. * Added some code to keep track of move instructions in register
  2157. allocator
  2158. Revision 1.33 2003/04/17 07:50:24 daniel
  2159. * Some work on interference graph construction
  2160. Revision 1.32 2003/03/28 19:16:57 peter
  2161. * generic constructor working for i386
  2162. * remove fixed self register
  2163. * esi added as address register for i386
  2164. Revision 1.31 2003/03/11 21:46:24 jonas
  2165. * lots of new regallocator fixes, both in generic and ppc-specific code
  2166. (ppc compiler still can't compile the linux system unit though)
  2167. Revision 1.30 2003/03/09 21:18:59 olle
  2168. + added cutils to the uses clause
  2169. Revision 1.29 2003/03/08 20:36:41 daniel
  2170. + Added newra version of Ti386shlshrnode
  2171. + Added interference graph construction code
  2172. Revision 1.28 2003/03/08 13:59:16 daniel
  2173. * Work to handle new register notation in ag386nsm
  2174. + Added newra version of Ti386moddivnode
  2175. Revision 1.27 2003/03/08 10:53:48 daniel
  2176. * Created newra version of secondmul in n386add.pas
  2177. Revision 1.26 2003/03/08 08:59:07 daniel
  2178. + $define newra will enable new register allocator
  2179. + getregisterint will return imaginary registers with $newra
  2180. + -sr switch added, will skip register allocation so you can see
  2181. the direct output of the code generator before register allocation
  2182. Revision 1.25 2003/02/26 20:50:45 daniel
  2183. * Fixed ungetreference
  2184. Revision 1.24 2003/02/19 22:39:56 daniel
  2185. * Fixed a few issues
  2186. Revision 1.23 2003/02/19 22:00:14 daniel
  2187. * Code generator converted to new register notation
  2188. - Horribily outdated todo.txt removed
  2189. Revision 1.22 2003/02/02 19:25:54 carl
  2190. * Several bugfixes for m68k target (register alloc., opcode emission)
  2191. + VIS target
  2192. + Generic add more complete (still not verified)
  2193. Revision 1.21 2003/01/08 18:43:57 daniel
  2194. * Tregister changed into a record
  2195. Revision 1.20 2002/10/05 12:43:28 carl
  2196. * fixes for Delphi 6 compilation
  2197. (warning : Some features do not work under Delphi)
  2198. Revision 1.19 2002/08/23 16:14:49 peter
  2199. * tempgen cleanup
  2200. * tt_noreuse temp type added that will be used in genentrycode
  2201. Revision 1.18 2002/08/17 22:09:47 florian
  2202. * result type handling in tcgcal.pass_2 overhauled
  2203. * better tnode.dowrite
  2204. * some ppc stuff fixed
  2205. Revision 1.17 2002/08/17 09:23:42 florian
  2206. * first part of procinfo rewrite
  2207. Revision 1.16 2002/08/06 20:55:23 florian
  2208. * first part of ppc calling conventions fix
  2209. Revision 1.15 2002/08/05 18:27:48 carl
  2210. + more more more documentation
  2211. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  2212. Revision 1.14 2002/08/04 19:06:41 carl
  2213. + added generic exception support (still does not work!)
  2214. + more documentation
  2215. Revision 1.13 2002/07/07 09:52:32 florian
  2216. * powerpc target fixed, very simple units can be compiled
  2217. * some basic stuff for better callparanode handling, far from being finished
  2218. Revision 1.12 2002/07/01 18:46:26 peter
  2219. * internal linker
  2220. * reorganized aasm layer
  2221. Revision 1.11 2002/05/18 13:34:17 peter
  2222. * readded missing revisions
  2223. Revision 1.10 2002/05/16 19:46:44 carl
  2224. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2225. + try to fix temp allocation (still in ifdef)
  2226. + generic constructor calls
  2227. + start of tassembler / tmodulebase class cleanup
  2228. Revision 1.8 2002/04/21 15:23:03 carl
  2229. + makeregsize
  2230. + changeregsize is now a local routine
  2231. Revision 1.7 2002/04/20 21:32:25 carl
  2232. + generic FPC_CHECKPOINTER
  2233. + first parameter offset in stack now portable
  2234. * rename some constants
  2235. + move some cpu stuff to other units
  2236. - remove unused constents
  2237. * fix stacksize for some targets
  2238. * fix generic size problems which depend now on EXTEND_SIZE constant
  2239. Revision 1.6 2002/04/15 19:03:31 carl
  2240. + reg2str -> std_reg2str()
  2241. Revision 1.5 2002/04/06 18:13:01 jonas
  2242. * several powerpc-related additions and fixes
  2243. Revision 1.4 2002/04/04 19:06:04 peter
  2244. * removed unused units
  2245. * use tlocation.size in cg.a_*loc*() routines
  2246. Revision 1.3 2002/04/02 17:11:29 peter
  2247. * tlocation,treference update
  2248. * LOC_CONSTANT added for better constant handling
  2249. * secondadd splitted in multiple routines
  2250. * location_force_reg added for loading a location to a register
  2251. of a specified size
  2252. * secondassignment parses now first the right and then the left node
  2253. (this is compatible with Kylix). This saves a lot of push/pop especially
  2254. with string operations
  2255. * adapted some routines to use the new cg methods
  2256. Revision 1.2 2002/04/01 19:24:25 jonas
  2257. * fixed different parameter name in interface and implementation
  2258. declaration of a method (only 1.0.x detected this)
  2259. Revision 1.1 2002/03/31 20:26:36 jonas
  2260. + a_loadfpu_* and a_loadmm_* methods in tcg
  2261. * register allocation is now handled by a class and is mostly processor
  2262. independent (+rgobj.pas and i386/rgcpu.pas)
  2263. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  2264. * some small improvements and fixes to the optimizer
  2265. * some register allocation fixes
  2266. * some fpuvaroffset fixes in the unary minus node
  2267. * push/popusedregisters is now called rg.save/restoreusedregisters and
  2268. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  2269. also better optimizable)
  2270. * fixed and optimized register saving/restoring for new/dispose nodes
  2271. * LOC_FPU locations now also require their "register" field to be set to
  2272. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  2273. - list field removed of the tnode class because it's not used currently
  2274. and can cause hard-to-find bugs
  2275. }