rgobj.pas 88 KB

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