rgobj.pas 88 KB

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