rgobj.pas 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694
  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;
  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 i386}
  1076. include(usedintbyproc,reg);
  1077. {$endif not i386}
  1078. end;
  1079. procedure trgobj.makeregvarother(reg: tregister);
  1080. begin
  1081. if reg.enum>lastreg then
  1082. internalerror(200301081);
  1083. if reg.enum in intregs then
  1084. internalerror(200301151)
  1085. else if reg.enum in fpuregs then
  1086. begin
  1087. dec(countusableregsfpu);
  1088. dec(countunusedregsfpu);
  1089. exclude(usableregsfpu,reg.enum);
  1090. exclude(unusedregsfpu,reg.enum);
  1091. {$ifndef i386}
  1092. include(usedbyproc,reg.enum);
  1093. {$endif not i386}
  1094. end
  1095. else if reg.enum in mmregs then
  1096. begin
  1097. dec(countusableregsmm);
  1098. dec(countunusedregsmm);
  1099. exclude(usableregsmm,reg.enum);
  1100. exclude(unusedregsmm,reg.enum);
  1101. {$ifndef i386}
  1102. include(usedbyproc,reg.enum);
  1103. {$endif not i386}
  1104. end;
  1105. is_reg_var_other[reg.enum]:=true;
  1106. end;
  1107. {$ifdef TEMPREGDEBUG}
  1108. procedure trgobj.testregisters;
  1109. var
  1110. r: tregister;
  1111. test : byte;
  1112. begin
  1113. test:=0;
  1114. for r := firstsaveintreg to lastsaveintreg do
  1115. inc(test,ord(r in unusedregsint));
  1116. if test<>countunusedregsint then
  1117. internalerror(10);
  1118. end;
  1119. {$endif TEMPREGDEBUG}
  1120. procedure trgobj.saveStateForInline(var state: pointer);
  1121. begin
  1122. new(psavedstate(state));
  1123. psavedstate(state)^.unusedregsint := unusedregsint;
  1124. psavedstate(state)^.usableregsint := usableregsint;
  1125. psavedstate(state)^.unusedregsfpu := unusedregsfpu;
  1126. psavedstate(state)^.usableregsfpu := usableregsfpu;
  1127. psavedstate(state)^.unusedregsmm := unusedregsmm;
  1128. psavedstate(state)^.usableregsmm := usableregsmm;
  1129. {$ifndef newra}
  1130. psavedstate(state)^.countunusedregsint := countunusedregsint;
  1131. {$endif}
  1132. psavedstate(state)^.countunusedregsfpu := countunusedregsfpu;
  1133. psavedstate(state)^.countunusedregsmm := countunusedregsmm;
  1134. psavedstate(state)^.countusableregsint := countusableregsint;
  1135. psavedstate(state)^.countusableregsfpu := countusableregsfpu;
  1136. psavedstate(state)^.countusableregsmm := countusableregsmm;
  1137. psavedstate(state)^.usedinproc := usedinproc;
  1138. psavedstate(state)^.usedbyproc := usedbyproc;
  1139. {$ifndef newra}
  1140. psavedstate(state)^.reg_pushes_int := reg_pushes_int;
  1141. {$endif}
  1142. psavedstate(state)^.reg_pushes_other := reg_pushes_other;
  1143. psavedstate(state)^.is_reg_var_int := is_reg_var_int;
  1144. psavedstate(state)^.is_reg_var_other := is_reg_var_other;
  1145. psavedstate(state)^.regvar_loaded_int := regvar_loaded_int;
  1146. psavedstate(state)^.regvar_loaded_other := regvar_loaded_other;
  1147. {$ifdef TEMPREGDEBUG}
  1148. psavedstate(state)^.reg_user := reg_user;
  1149. psavedstate(state)^.reg_releaser := reg_releaser;
  1150. {$endif TEMPREGDEBUG}
  1151. end;
  1152. procedure trgobj.restoreStateAfterInline(var state: pointer);
  1153. begin
  1154. unusedregsint := psavedstate(state)^.unusedregsint;
  1155. usableregsint := psavedstate(state)^.usableregsint;
  1156. unusedregsfpu := psavedstate(state)^.unusedregsfpu;
  1157. usableregsfpu := psavedstate(state)^.usableregsfpu;
  1158. unusedregsmm := psavedstate(state)^.unusedregsmm;
  1159. usableregsmm := psavedstate(state)^.usableregsmm;
  1160. {$ifndef newra}
  1161. countunusedregsint := psavedstate(state)^.countunusedregsint;
  1162. {$endif}
  1163. countunusedregsfpu := psavedstate(state)^.countunusedregsfpu;
  1164. countunusedregsmm := psavedstate(state)^.countunusedregsmm;
  1165. countusableregsint := psavedstate(state)^.countusableregsint;
  1166. countusableregsfpu := psavedstate(state)^.countusableregsfpu;
  1167. countusableregsmm := psavedstate(state)^.countusableregsmm;
  1168. usedinproc := psavedstate(state)^.usedinproc;
  1169. usedbyproc := psavedstate(state)^.usedbyproc;
  1170. {$ifndef newra}
  1171. reg_pushes_int := psavedstate(state)^.reg_pushes_int;
  1172. {$endif}
  1173. reg_pushes_other := psavedstate(state)^.reg_pushes_other;
  1174. is_reg_var_int := psavedstate(state)^.is_reg_var_int;
  1175. is_reg_var_other := psavedstate(state)^.is_reg_var_other;
  1176. regvar_loaded_other := psavedstate(state)^.regvar_loaded_other;
  1177. regvar_loaded_int := psavedstate(state)^.regvar_loaded_int;
  1178. {$ifdef TEMPREGDEBUG}
  1179. reg_user := psavedstate(state)^.reg_user;
  1180. reg_releaser := psavedstate(state)^.reg_releaser;
  1181. {$endif TEMPREGDEBUG}
  1182. dispose(psavedstate(state));
  1183. state := nil;
  1184. end;
  1185. procedure trgobj.saveUnusedState(var state: pointer);
  1186. begin
  1187. new(punusedstate(state));
  1188. punusedstate(state)^.unusedregsint := unusedregsint;
  1189. punusedstate(state)^.unusedregsfpu := unusedregsfpu;
  1190. punusedstate(state)^.unusedregsmm := unusedregsmm;
  1191. {$ifndef newra}
  1192. punusedstate(state)^.countunusedregsint := countunusedregsint;
  1193. {$endif}
  1194. punusedstate(state)^.countunusedregsfpu := countunusedregsfpu;
  1195. punusedstate(state)^.countunusedregsmm := countunusedregsmm;
  1196. end;
  1197. procedure trgobj.restoreUnusedState(var state: pointer);
  1198. begin
  1199. unusedregsint := punusedstate(state)^.unusedregsint;
  1200. unusedregsfpu := punusedstate(state)^.unusedregsfpu;
  1201. unusedregsmm := punusedstate(state)^.unusedregsmm;
  1202. {$ifndef newra}
  1203. countunusedregsint := punusedstate(state)^.countunusedregsint;
  1204. {$endif}
  1205. countunusedregsfpu := punusedstate(state)^.countunusedregsfpu;
  1206. countunusedregsmm := punusedstate(state)^.countunusedregsmm;
  1207. dispose(punusedstate(state));
  1208. state := nil;
  1209. end;
  1210. {$ifdef newra}
  1211. procedure Trgobj.add_edge(u,v:Tsuperregister);
  1212. {This procedure will add an edge to the virtual interference graph.}
  1213. procedure addadj(u,v:Tsuperregister);
  1214. begin
  1215. if igraph.adjlist[u]=nil then
  1216. begin
  1217. getmem(igraph.adjlist[u],16);
  1218. igraph.adjlist[u]^:='';
  1219. end
  1220. else if (length(igraph.adjlist[u]^) and 15)=15 then
  1221. reallocmem(igraph.adjlist[u],length(igraph.adjlist[u]^)+16);
  1222. igraph.adjlist[u]^:=igraph.adjlist[u]^+char(v);
  1223. end;
  1224. begin
  1225. if (u<>v) and not(v in igraph.bitmap[u]) then
  1226. begin
  1227. include(igraph.bitmap[u],v);
  1228. include(igraph.bitmap[v],u);
  1229. {Precoloured nodes are not stored in the interference graph.}
  1230. if not(u in [first_supreg..last_supreg]) then
  1231. begin
  1232. addadj(u,v);
  1233. inc(degree[u]);
  1234. end;
  1235. if not(v in [first_supreg..last_supreg]) then
  1236. begin
  1237. addadj(v,u);
  1238. inc(degree[v]);
  1239. end;
  1240. end;
  1241. end;
  1242. procedure Trgobj.add_edges_used(u:Tsuperregister);
  1243. var i:Tsuperregister;
  1244. begin
  1245. for i:=1 to maxintreg do
  1246. if not(i in unusedregsint) then
  1247. add_edge(u,i);
  1248. end;
  1249. {$ifdef ra_debug}
  1250. procedure Trgobj.writegraph;
  1251. {This procedure writes out the current interference graph in the
  1252. register allocator.}
  1253. var f:text;
  1254. i,j:Tsuperregister;
  1255. begin
  1256. assign(f,'igraph'+char(48+random(10))+char(48+random(10)));
  1257. rewrite(f);
  1258. writeln(f,'Interference graph');
  1259. writeln(f);
  1260. write(f,' ');
  1261. for i:=0 to 15 do
  1262. for j:=0 to 15 do
  1263. write(f,hexstr(i,1));
  1264. writeln(f);
  1265. write(f,' ');
  1266. for i:=0 to 15 do
  1267. write(f,'0123456789ABCDEF');
  1268. writeln(f);
  1269. for i:=0 to 255 do
  1270. begin
  1271. write(f,hexstr(i,2):4);
  1272. for j:=0 to 255 do
  1273. if j in igraph.bitmap[i] then
  1274. write(f,'*')
  1275. else
  1276. write(f,'-');
  1277. writeln(f);
  1278. end;
  1279. close(f);
  1280. end;
  1281. {$endif}
  1282. procedure Trgobj.add_to_movelist(u:Tsuperregister;data:Tlinkedlistitem);
  1283. begin
  1284. if movelist[u]=nil then
  1285. begin
  1286. getmem(movelist[u],64);
  1287. movelist[u]^.count:=0;
  1288. end
  1289. else if (movelist[u]^.count and 15)=15 then
  1290. reallocmem(movelist[u],(movelist[u]^.count+1)*4+64);
  1291. movelist[u]^.data[movelist[u]^.count]:=data;
  1292. inc(movelist[u]^.count);
  1293. end;
  1294. procedure Trgobj.add_move_instruction(instr:Taicpu);
  1295. {This procedure notifies a certain as a move instruction so the
  1296. register allocator can try to eliminate it.}
  1297. var i:Tmoveins;
  1298. ssupreg,dsupreg:Tsuperregister;
  1299. begin
  1300. i:=Tmoveins.create;
  1301. i.moveset:=ms_worklist_moves;
  1302. i.instruction:=instr;
  1303. worklist_moves.insert(i);
  1304. ssupreg:=instr.oper[0].reg.number shr 8;
  1305. add_to_movelist(ssupreg,i);
  1306. dsupreg:=instr.oper[1].reg.number shr 8;
  1307. if ssupreg<>dsupreg then
  1308. {Avoid adding the same move instruction twice to a single register.}
  1309. add_to_movelist(dsupreg,i);
  1310. i.x:=ssupreg;
  1311. i.y:=dsupreg;
  1312. end;
  1313. function Trgobj.move_related(n:Tsuperregister):boolean;
  1314. var i:cardinal;
  1315. begin
  1316. move_related:=false;
  1317. if movelist[n]<>nil then
  1318. begin
  1319. for i:=0 to movelist[n]^.count-1 do
  1320. if Tmoveins(movelist[n]^.data[i]).moveset in
  1321. [ms_worklist_moves,ms_active_moves] then
  1322. begin
  1323. move_related:=true;
  1324. break;
  1325. end;
  1326. end;
  1327. end;
  1328. procedure Trgobj.make_work_list;
  1329. var n:Tsuperregister;
  1330. begin
  1331. {If we have 7 cpu registers, and the degree of a node is 7, we cannot
  1332. assign it to any of the registers, thus it is significant.}
  1333. for n:=first_imreg to maxintreg do
  1334. if degree[n]>=cpu_registers then
  1335. spillworklist:=spillworklist+char(n)
  1336. else if move_related(n) then
  1337. freezeworklist:=freezeworklist+char(n)
  1338. else
  1339. simplifyworklist:=simplifyworklist+char(n);
  1340. end;
  1341. procedure Trgobj.prepare_colouring;
  1342. begin
  1343. make_work_list;
  1344. active_moves:=Tlinkedlist.create;
  1345. frozen_moves:=Tlinkedlist.create;
  1346. coalesced_moves:=Tlinkedlist.create;
  1347. constrained_moves:=Tlinkedlist.create;
  1348. fillchar(alias,sizeof(alias),0);
  1349. coalescednodes:='';
  1350. selectstack:='';
  1351. end;
  1352. procedure Trgobj.enable_moves(n:Tsuperregister);
  1353. var m:Tlinkedlistitem;
  1354. i:cardinal;
  1355. begin
  1356. if movelist[n]<>nil then
  1357. for i:=0 to movelist[n]^.count-1 do
  1358. begin
  1359. m:=movelist[n]^.data[i];
  1360. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  1361. begin
  1362. if Tmoveins(m).moveset=ms_active_moves then
  1363. begin
  1364. {Move m from the set active_moves to the set worklist_moves.}
  1365. active_moves.remove(m);
  1366. Tmoveins(m).moveset:=ms_worklist_moves;
  1367. worklist_moves.concat(m);
  1368. end;
  1369. end;
  1370. end;
  1371. end;
  1372. procedure Trgobj.decrement_degree(m:Tsuperregister);
  1373. var adj:Pstring;
  1374. d:byte;
  1375. i,p:byte;
  1376. n:char;
  1377. begin
  1378. d:=degree[m];
  1379. dec(degree[m]);
  1380. if d=cpu_registers then
  1381. begin
  1382. {Enable moves for m.}
  1383. enable_moves(m);
  1384. {Enable moves for adjacent.}
  1385. adj:=igraph.adjlist[m];
  1386. if adj<>nil then
  1387. for i:=1 to length(adj^) do
  1388. begin
  1389. n:=adj^[i];
  1390. if (pos(n,selectstack) or pos(n,coalescednodes))=0 then
  1391. enable_moves(Tsuperregister(n));
  1392. end;
  1393. {Remove the node from the spillworklist.}
  1394. p:=pos(char(m),spillworklist);
  1395. if p=0 then
  1396. internalerror(200305301); {must be found}
  1397. if length(spillworklist)>1 then
  1398. spillworklist[p]:=spillworklist[length(spillworklist)];
  1399. dec(spillworklist[0]);
  1400. if move_related(m) then
  1401. freezeworklist:=freezeworklist+char(m)
  1402. else
  1403. simplifyworklist:=simplifyworklist+char(m);
  1404. end;
  1405. end;
  1406. procedure Trgobj.simplify;
  1407. var adj:Pstring;
  1408. i,min,p:byte;
  1409. m:char;
  1410. n:Tsuperregister;
  1411. begin
  1412. {We the element with the least interferences out of the
  1413. simplifyworklist.}
  1414. min:=$ff;
  1415. p:=1;
  1416. for i:=1 to length(simplifyworklist) do
  1417. begin
  1418. adj:=igraph.adjlist[Tsuperregister(simplifyworklist[i])];
  1419. if adj=nil then
  1420. begin
  1421. min:=0;
  1422. break; {We won't find smaller ones.}
  1423. end
  1424. else
  1425. if length(adj^)<min then
  1426. begin
  1427. min:=length(adj^);
  1428. if min=0 then
  1429. break; {We won't find smaller ones.}
  1430. p:=i;
  1431. end;
  1432. end;
  1433. n:=Tsuperregister(simplifyworklist[p]);
  1434. delete(simplifyworklist,p,1);
  1435. {Push it on the selectstack.}
  1436. selectstack:=selectstack+char(n);
  1437. adj:=igraph.adjlist[n];
  1438. if adj<>nil then
  1439. for i:=1 to length(adj^) do
  1440. begin
  1441. m:=adj^[i];
  1442. if (pos(m,selectstack) or pos(m,coalescednodes))=0 then
  1443. decrement_degree(Tsuperregister(m));
  1444. end;
  1445. end;
  1446. function Trgobj.get_alias(n:Tsuperregister):Tsuperregister;
  1447. begin
  1448. while pos(char(n),coalescednodes)<>0 do
  1449. n:=alias[n];
  1450. get_alias:=n;
  1451. end;
  1452. procedure Trgobj.add_worklist(u:Tsuperregister);
  1453. begin
  1454. if not(u in [first_supreg..last_supreg]) and not move_related(u) and
  1455. (degree[u]<cpu_registers) then
  1456. begin
  1457. delete(freezeworklist,pos(char(u),freezeworklist),1);
  1458. simplifyworklist:=simplifyworklist+char(u);
  1459. end;
  1460. end;
  1461. function Trgobj.adjacent_ok(u,v:Tsuperregister):boolean;
  1462. {Check wether u and v should be coalesced. u is precoloured.}
  1463. function ok(t,r:Tsuperregister):boolean;
  1464. begin
  1465. ok:=(degree[t]<cpu_registers) or
  1466. (t in [first_supreg..last_supreg]) or
  1467. (r in igraph.bitmap[t]);
  1468. end;
  1469. var adj:Pstring;
  1470. i:byte;
  1471. t:char;
  1472. begin
  1473. adjacent_ok:=true;
  1474. adj:=igraph.adjlist[v];
  1475. if adj<>nil then
  1476. for i:=1 to length(adj^) do
  1477. begin
  1478. t:=adj^[i];
  1479. if (pos(t,selectstack) or pos(t,coalescednodes))=0 then
  1480. if not ok(Tsuperregister(t),u) then
  1481. begin
  1482. adjacent_ok:=false;
  1483. break;
  1484. end;
  1485. end;
  1486. end;
  1487. function Trgobj.conservative(u,v:Tsuperregister):boolean;
  1488. var adj:Pstring;
  1489. done:set of char; {To prevent that we count nodes twice.}
  1490. i,k:byte;
  1491. n:char;
  1492. begin
  1493. k:=0;
  1494. done:=[];
  1495. adj:=igraph.adjlist[u];
  1496. if adj<>nil then
  1497. for i:=1 to length(adj^) do
  1498. begin
  1499. n:=adj^[i];
  1500. if (pos(n,selectstack) or pos(n,coalescednodes))=0 then
  1501. begin
  1502. include(done,n);
  1503. if degree[Tsuperregister(n)]>=cpu_registers then
  1504. inc(k);
  1505. end;
  1506. end;
  1507. adj:=igraph.adjlist[v];
  1508. if adj<>nil then
  1509. for i:=1 to length(adj^) do
  1510. begin
  1511. n:=adj^[i];
  1512. if ((pos(n,selectstack) or pos(n,coalescednodes))=0) and
  1513. not (n in done) and
  1514. (degree[Tsuperregister(n)]>=cpu_registers) then
  1515. inc(k);
  1516. end;
  1517. conservative:=(k<cpu_registers);
  1518. end;
  1519. procedure Trgobj.combine(u,v:Tsuperregister);
  1520. var add:boolean;
  1521. adj:Pstring;
  1522. i,p:byte;
  1523. n,o:cardinal;
  1524. t:char;
  1525. begin
  1526. p:=pos(char(v),freezeworklist);
  1527. if p<>0 then
  1528. delete(freezeworklist,p,1)
  1529. else
  1530. delete(spillworklist,pos(char(v),spillworklist),1);
  1531. coalescednodes:=coalescednodes+char(v);
  1532. alias[v]:=u;
  1533. {Combine both movelists. Since the movelists are sets, only add
  1534. elements that are not already present.}
  1535. for n:=0 to movelist[v]^.count-1 do
  1536. begin
  1537. add:=true;
  1538. for o:=0 to movelist[u]^.count-1 do
  1539. if movelist[u]^.data[o]=movelist[v]^.data[n] then
  1540. begin
  1541. add:=false;
  1542. break;
  1543. end;
  1544. if add then
  1545. add_to_movelist(u,movelist[v]^.data[n]);
  1546. end;
  1547. enable_moves(v);
  1548. adj:=igraph.adjlist[v];
  1549. if adj<>nil then
  1550. for i:=1 to length(adj^) do
  1551. begin
  1552. t:=adj^[i];
  1553. if (pos(t,selectstack) or pos(t,coalescednodes))=0 then
  1554. begin
  1555. decrement_degree(Tsuperregister(t));
  1556. add_edge(Tsuperregister(t),u);
  1557. end;
  1558. end;
  1559. p:=pos(char(u),freezeworklist);
  1560. if (degree[u]>=cpu_registers) and (p<>0) then
  1561. begin
  1562. delete(freezeworklist,p,1);
  1563. spillworklist:=spillworklist+char(u);
  1564. end;
  1565. end;
  1566. procedure Trgobj.coalesce;
  1567. var m:Tmoveins;
  1568. x,y,u,v:Tsuperregister;
  1569. begin
  1570. m:=Tmoveins(worklist_moves.getfirst);
  1571. x:=get_alias(m.instruction.oper[0].reg.number shr 8);
  1572. y:=get_alias(m.instruction.oper[1].reg.number shr 8);
  1573. if y in [first_supreg..last_supreg] then
  1574. begin
  1575. u:=y;
  1576. v:=x;
  1577. end
  1578. else
  1579. begin
  1580. u:=x;
  1581. v:=y;
  1582. end;
  1583. if (u=v) then
  1584. begin
  1585. m.moveset:=ms_coalesced_moves; {Already coalesced.}
  1586. coalesced_moves.insert(m);
  1587. add_worklist(u);
  1588. end
  1589. {Do u and v interfere? In that case the move is constrained. Two
  1590. precoloured nodes interfere allways. If v is precoloured, by the above
  1591. code u is precoloured, thus interference...}
  1592. else if (v in [first_supreg..last_supreg]) or (u in igraph.bitmap[v]) then
  1593. begin
  1594. m.moveset:=ms_constrained_moves; {Cannot coalesce yet...}
  1595. constrained_moves.insert(m);
  1596. add_worklist(u);
  1597. add_worklist(v);
  1598. end
  1599. {Next test: is it possible and a good idea to coalesce??}
  1600. else if ((u in [first_supreg..last_supreg]) and adjacent_ok(u,v)) or
  1601. (not(u in [first_supreg..last_supreg]) and conservative(u,v)) then
  1602. begin
  1603. m.moveset:=ms_coalesced_moves; {Move coalesced!}
  1604. coalesced_moves.insert(m);
  1605. combine(u,v);
  1606. add_worklist(u);
  1607. end
  1608. else
  1609. begin
  1610. m.moveset:=ms_active_moves;
  1611. active_moves.insert(m);
  1612. end;
  1613. end;
  1614. procedure Trgobj.freeze_moves(u:Tsuperregister);
  1615. var i:cardinal;
  1616. m:Tlinkedlistitem;
  1617. v,x,y:Tsuperregister;
  1618. begin
  1619. if movelist[u]<>nil then
  1620. for i:=0 to movelist[u]^.count-1 do
  1621. begin
  1622. m:=movelist[u]^.data[i];
  1623. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  1624. begin
  1625. x:=Tmoveins(m).instruction.oper[0].reg.number shr 8;
  1626. y:=Tmoveins(m).instruction.oper[1].reg.number shr 8;
  1627. if get_alias(y)=get_alias(u) then
  1628. v:=get_alias(x)
  1629. else
  1630. v:=get_alias(y);
  1631. {Move m from active_moves/worklist_moves to frozen_moves.}
  1632. if Tmoveins(m).moveset=ms_active_moves then
  1633. active_moves.remove(m)
  1634. else
  1635. worklist_moves.remove(m);
  1636. Tmoveins(m).moveset:=ms_frozen_moves;
  1637. frozen_moves.insert(m);
  1638. if not(move_related(v)) and (degree[v]<cpu_registers) then
  1639. begin
  1640. delete(freezeworklist,pos(char(v),freezeworklist),1);
  1641. simplifyworklist:=simplifyworklist+char(v);
  1642. end;
  1643. end;
  1644. end;
  1645. end;
  1646. procedure Trgobj.freeze;
  1647. var n:Tsuperregister;
  1648. begin
  1649. {We need to take a random element out of the freezeworklist. We take
  1650. the last element. Dirty code!}
  1651. n:=Tsuperregister(freezeworklist[byte(freezeworklist[0])]);
  1652. dec(freezeworklist[0]);
  1653. {Add it to the simplifyworklist.}
  1654. simplifyworklist:=simplifyworklist+char(n);
  1655. freeze_moves(n);
  1656. end;
  1657. procedure Trgobj.select_spill;
  1658. var n:char;
  1659. begin
  1660. {This code is WAY too naive. We need not to select just a register, but
  1661. the register that is used the least...}
  1662. n:=spillworklist[byte(spillworklist[0])];
  1663. dec(spillworklist[0]);
  1664. simplifyworklist:=simplifyworklist+n;
  1665. freeze_moves(Tsuperregister(n));
  1666. end;
  1667. procedure Trgobj.assign_colours;
  1668. {Assign_colours assigns the actual colours to the registers.}
  1669. var adj:Pstring;
  1670. i,j,k:byte;
  1671. n,a:Tsuperregister;
  1672. adj_colours,colourednodes:set of Tsuperregister;
  1673. w:char;
  1674. begin
  1675. spillednodes:='';
  1676. {Colour the cpu registers...}
  1677. colourednodes:=[first_supreg..last_supreg];
  1678. for i:=first_supreg to last_supreg do
  1679. colour[i]:=i;
  1680. {Now colour the imaginary registers on the select-stack.}
  1681. for i:=length(selectstack) downto 1 do
  1682. begin
  1683. n:=Tsuperregister(selectstack[i]);
  1684. {Create a list of colours that we cannot assign to n.}
  1685. adj_colours:=[];
  1686. adj:=igraph.adjlist[n];
  1687. if adj<>nil then
  1688. for j:=1 to length(adj^) do
  1689. begin
  1690. w:=adj^[j];
  1691. a:=get_alias(Tsuperregister(w));
  1692. if a in colourednodes then
  1693. include(adj_colours,colour[a]);
  1694. end;
  1695. {Assume a spill by default...}
  1696. spillednodes:=spillednodes+char(n);
  1697. {Search for a colour not in this list.}
  1698. for k:=1 to cpu_registers do
  1699. if not(k in adj_colours) then
  1700. begin
  1701. colour[n]:=k;
  1702. dec(spillednodes[0]); {Colour found: no spill.}
  1703. include(colourednodes,n);
  1704. if n in usedintinproc then
  1705. include(usedintinproc,k);
  1706. break;
  1707. end;
  1708. end;
  1709. {Finally colour the nodes that were coalesced.}
  1710. for i:=1 to length(coalescednodes) do
  1711. begin
  1712. n:=Tsuperregister(coalescednodes[i]);
  1713. k:=get_alias(n);
  1714. colour[n]:=colour[k];
  1715. if n in usedintinproc then
  1716. include(usedintinproc,colour[k]);
  1717. end;
  1718. {$ifdef ra_debug}
  1719. for i:=first_imreg to maxintreg do
  1720. writeln(i:4,' ',colour[i]:4)
  1721. {$endif}
  1722. end;
  1723. procedure Trgobj.colour_registers;
  1724. begin
  1725. repeat
  1726. if length(simplifyworklist)<>0 then
  1727. simplify
  1728. else if not(worklist_moves.empty) then
  1729. coalesce
  1730. else if length(freezeworklist)<>0 then
  1731. freeze
  1732. else if length(spillworklist)<>0 then
  1733. select_spill;
  1734. until (length(simplifyworklist) or
  1735. byte(not(worklist_moves.empty)) or
  1736. length(freezeworklist) or
  1737. length(spillworklist)
  1738. )=0;
  1739. assign_colours;
  1740. end;
  1741. procedure Trgobj.epilogue_colouring;
  1742. {
  1743. procedure move_to_worklist_moves(list:Tlinkedlist);
  1744. var p:Tlinkedlistitem;
  1745. begin
  1746. p:=list.first;
  1747. while p<>nil do
  1748. begin
  1749. Tmoveins(p).moveset:=ms_worklist_moves;
  1750. p:=p.next;
  1751. end;
  1752. worklist_moves.concatlist(list);
  1753. end;
  1754. }
  1755. var i:Tsuperregister;
  1756. begin
  1757. worklist_moves.clear;
  1758. {$ifdef Principle_wrong_by_definition}
  1759. {Move everything back to worklist_moves.}
  1760. move_to_worklist_moves(active_moves);
  1761. move_to_worklist_moves(frozen_moves);
  1762. move_to_worklist_moves(coalesced_moves);
  1763. move_to_worklist_moves(constrained_moves);
  1764. {$endif}
  1765. active_moves.destroy;
  1766. active_moves:=nil;
  1767. frozen_moves.destroy;
  1768. frozen_moves:=nil;
  1769. coalesced_moves.destroy;
  1770. coalesced_moves:=nil;
  1771. constrained_moves.destroy;
  1772. constrained_moves:=nil;
  1773. for i:=0 to 255 do
  1774. if movelist[i]<>nil then
  1775. begin
  1776. dispose(movelist[i]);
  1777. movelist[i]:=0;
  1778. end;
  1779. end;
  1780. procedure Trgobj.clear_interferences(u:Tsuperregister);
  1781. {Remove node u from the interference graph and remove all collected
  1782. move instructions it is associated with.}
  1783. var i:byte;
  1784. j,k,count:cardinal;
  1785. v:Tsuperregister;
  1786. m,n:Tmoveins;
  1787. begin
  1788. if igraph.adjlist[u]<>nil then
  1789. begin
  1790. for i:=1 to length(igraph.adjlist[u]^) do
  1791. begin
  1792. v:=Tsuperregister(igraph.adjlist[u]^[i]);
  1793. {Remove (u,v) and (v,u) from bitmap.}
  1794. exclude(igraph.bitmap[u],v);
  1795. exclude(igraph.bitmap[v],u);
  1796. {Remove (v,u) from adjacency list.}
  1797. if igraph.adjlist[v]<>nil then
  1798. begin
  1799. delete(igraph.adjlist[v]^,pos(char(v),igraph.adjlist[v]^),1);
  1800. if length(igraph.adjlist[v]^)=0 then
  1801. begin
  1802. dispose(igraph.adjlist[v]);
  1803. igraph.adjlist[v]:=nil;
  1804. end;
  1805. end;
  1806. end;
  1807. {Remove ( u,* ) from adjacency list.}
  1808. dispose(igraph.adjlist[u]);
  1809. igraph.adjlist[u]:=nil;
  1810. end;
  1811. {$ifdef Principle_wrong_by_definition}
  1812. {Now remove the moves.}
  1813. if movelist[u]<>nil then
  1814. begin
  1815. for j:=0 to movelist[u]^.count-1 do
  1816. begin
  1817. m:=Tmoveins(movelist[u]^.data[j]);
  1818. {Get the other register of the move instruction.}
  1819. v:=m.instruction.oper[0].reg.number shr 8;
  1820. if v=u then
  1821. v:=m.instruction.oper[1].reg.number shr 8;
  1822. repeat
  1823. repeat
  1824. if (u<>v) and (movelist[v]<>nil) then
  1825. begin
  1826. {Remove the move from it's movelist.}
  1827. count:=movelist[v]^.count-1;
  1828. for k:=0 to count do
  1829. if m=movelist[v]^.data[k] then
  1830. begin
  1831. if k<>count then
  1832. movelist[v]^.data[k]:=movelist[v]^.data[count];
  1833. dec(movelist[v]^.count);
  1834. if count=0 then
  1835. begin
  1836. dispose(movelist[v]);
  1837. movelist[v]:=nil;
  1838. end;
  1839. break;
  1840. end;
  1841. end;
  1842. {The complexity is enourmous: the register might have been
  1843. coalesced. In that case it's movelists have been added to
  1844. it's coalescing alias. (DM)}
  1845. v:=alias[v];
  1846. until v=0;
  1847. {And also register u might have been coalesced.}
  1848. u:=alias[u];
  1849. until u=0;
  1850. case m.moveset of
  1851. ms_coalesced_moves:
  1852. coalesced_moves.remove(m);
  1853. ms_constrained_moves:
  1854. constrained_moves.remove(m);
  1855. ms_frozen_moves:
  1856. frozen_moves.remove(m);
  1857. ms_worklist_moves:
  1858. worklist_moves.remove(m);
  1859. ms_active_moves:
  1860. active_moves.remove(m);
  1861. end;
  1862. end;
  1863. dispose(movelist[u]);
  1864. movelist[u]:=nil;
  1865. end;
  1866. {$endif}
  1867. end;
  1868. procedure Trgobj.getregisterintinline(list:Taasmoutput;position:Tai;subreg:Tsubregister;var result:Tregister);
  1869. var i:Tsuperregister;
  1870. r:Tregister;
  1871. begin
  1872. if not (lastintreg in [first_imreg..last_imreg]) then
  1873. lastintreg:=first_imreg;
  1874. i:=lastintreg;
  1875. repeat
  1876. if i=last_imreg then
  1877. i:=first_imreg
  1878. else
  1879. inc(i);
  1880. if (i in unusedregsint) and (pos(char(i),abtlist)=0) then
  1881. begin
  1882. exclude(unusedregsint,i);
  1883. include(usedintinproc,i);
  1884. r.enum:=R_INTREGISTER;
  1885. r.number:=i shl 8 or subreg;
  1886. if position=nil then
  1887. list.insert(Tai_regalloc.alloc(r))
  1888. else
  1889. list.insertafter(Tai_regalloc.alloc(r),position);
  1890. result:=r;
  1891. lastintreg:=i;
  1892. if i>maxintreg then
  1893. maxintreg:=i;
  1894. add_edges_used(i);
  1895. add_constraints(result.number);
  1896. exit;
  1897. end;
  1898. until i=lastintreg;
  1899. internalerror(10);
  1900. end;
  1901. {In some cases we can get in big trouble. See this example:
  1902. ; register reg23d released
  1903. ; register eax allocated
  1904. ; register ebx allocated
  1905. ; register ecx allocated
  1906. ; register edx allocated
  1907. ; register esi allocated
  1908. ; register edi allocated
  1909. call [reg23d]
  1910. This code is ok, *except* when reg23d is spilled. In that case the
  1911. spilled would introduce a help register which can never get
  1912. allocated to a real register because it interferes with all of them.
  1913. To solve this we introduce the ABT ("avoid big trouble :)" ) registers.
  1914. If you allocate an ABT register you get a register that has less
  1915. than cpu_register interferences and will not be allocated ever again
  1916. by the normal register get procedures. In other words it is for sure it
  1917. will never get spilled.}
  1918. function Trgobj.getabtregisterint(list:Taasmoutput;size:Tcgsize):Tregister;
  1919. var i:Tsuperregister;
  1920. r:Tregister;
  1921. found:boolean;
  1922. begin
  1923. if not (lastintreg in [first_imreg..last_imreg]) then
  1924. lastintreg:=first_imreg;
  1925. found:=false;
  1926. for i:=1 to length(abtlist) do
  1927. if Tsuperregister(abtlist[i]) in unusedregsint then
  1928. begin
  1929. found:=true;
  1930. break;
  1931. end;
  1932. i:=lastintreg;
  1933. repeat
  1934. if i=last_imreg then
  1935. i:=first_imreg
  1936. else
  1937. inc(i);
  1938. if (i in unusedregsint) and ((igraph.adjlist[i]=nil) or (length(igraph.adjlist[i]^)<cpu_registers)) then
  1939. begin
  1940. found:=true;
  1941. break;
  1942. end;
  1943. until i=lastintreg;
  1944. if found then
  1945. begin
  1946. exclude(unusedregsint,i);
  1947. include(usedintinproc,i);
  1948. r.enum:=R_INTREGISTER;
  1949. r.number:=i shl 8 or cgsize2subreg(size);
  1950. list.concat(Tai_regalloc.alloc(r));
  1951. getabtregisterint:=r;
  1952. lastintreg:=i;
  1953. if i>maxintreg then
  1954. maxintreg:=i;
  1955. add_edges_used(i);
  1956. if pos(char(i),abtlist)=0 then
  1957. abtlist:=abtlist+char(i);
  1958. end
  1959. else
  1960. internalerror(10);
  1961. {$ifdef newra}
  1962. add_constraints(getabtregisterint.number);
  1963. {$endif}
  1964. end;
  1965. procedure Trgobj.ungetregisterintinline(list:Taasmoutput;position:Tai;const r:Tregister);
  1966. var supreg:Tsuperregister;
  1967. begin
  1968. if r.enum<=lastreg then
  1969. internalerror(2003010803);
  1970. supreg:=r.number shr 8;
  1971. { takes much time }
  1972. include(unusedregsint,supreg);
  1973. if position=nil then
  1974. list.insert(Tai_regalloc.dealloc(r))
  1975. else
  1976. list.insertafter(Tai_regalloc.dealloc(r),position);
  1977. add_edges_used(supreg);
  1978. end;
  1979. function Trgobj.spill_registers(list:Taasmoutput;const regs_to_spill:string):boolean;
  1980. {Returns true if any help registers have been used.}
  1981. var i:byte;
  1982. r:Tsuperregister;
  1983. p,q:Tai;
  1984. regs_to_spill_set:Tsupregset;
  1985. spill_temps:^Tspill_temp_list;
  1986. begin
  1987. spill_registers:=false;
  1988. unusedregsint:=[0..255];
  1989. fillchar(degree,sizeof(degree),0);
  1990. if current_procinfo.framepointer.number=NR_FRAME_POINTER_REG then
  1991. {Make sure the register allocator won't allocate registers into ebp.}
  1992. exclude(rg.unusedregsint,RS_FRAME_POINTER_REG);
  1993. new(spill_temps);
  1994. fillchar(spill_temps^,sizeof(spill_temps^),0);
  1995. regs_to_spill_set:=[];
  1996. for i:=1 to length(regs_to_spill) do
  1997. begin
  1998. {Alternative representation.}
  1999. include(regs_to_spill_set,Tsuperregister(regs_to_spill[i]));
  2000. {Clear all interferences of the spilled register.}
  2001. clear_interferences(Tsuperregister(regs_to_spill[i]));
  2002. {Get a temp for the spilled register.}
  2003. tg.gettemp(list,4,tt_noreuse,spill_temps^[Tsuperregister(regs_to_spill[i])]);
  2004. end;
  2005. p:=Tai(list.first);
  2006. while assigned(p) do
  2007. begin
  2008. case p.typ of
  2009. ait_regalloc:
  2010. begin
  2011. {A register allocation of a spilled register can be removed.}
  2012. if (Tai_regalloc(p).reg.number shr 8) in regs_to_spill_set then
  2013. begin
  2014. q:=p;
  2015. p:=Tai(p.next);
  2016. list.remove(q);
  2017. continue;
  2018. end
  2019. else
  2020. if Tai_regalloc(p).allocation then
  2021. exclude(unusedregsint,Tai_regalloc(p).reg.number shr 8)
  2022. else
  2023. include(unusedregsint,Tai_regalloc(p).reg.number shr 8);
  2024. end;
  2025. ait_instruction:
  2026. begin
  2027. if Taicpu_abstract(p).spill_registers(list,@getregisterintinline,
  2028. @ungetregisterintinline,
  2029. regs_to_spill_set,
  2030. unusedregsint,
  2031. spill_temps^) then
  2032. spill_registers:=true;
  2033. if Taicpu_abstract(p).is_move then
  2034. add_move_instruction(Taicpu(p));
  2035. end;
  2036. end;
  2037. p:=Tai(p.next);
  2038. end;
  2039. for i:=1 to length(regs_to_spill) do
  2040. begin
  2041. tg.ungettemp(list,spill_temps^[Tsuperregister(regs_to_spill[i])]);
  2042. end;
  2043. dispose(spill_temps);
  2044. end;
  2045. {$endif newra}
  2046. {****************************************************************************
  2047. TReference
  2048. ****************************************************************************}
  2049. procedure reference_reset(var ref : treference);
  2050. begin
  2051. FillChar(ref,sizeof(treference),0);
  2052. ref.base.enum:=R_INTREGISTER;
  2053. ref.index.enum:=R_INTREGISTER;
  2054. {$ifdef i386}
  2055. ref.segment.enum:=R_INTREGISTER;
  2056. {$endif}
  2057. end;
  2058. procedure reference_reset_old(var ref : treference);
  2059. begin
  2060. FillChar(ref,sizeof(treference),0);
  2061. end;
  2062. procedure reference_reset_base(var ref : treference;base : tregister;offset : longint);
  2063. begin
  2064. reference_reset(ref);
  2065. ref.base:=base;
  2066. ref.offset:=offset;
  2067. end;
  2068. procedure reference_reset_symbol(var ref : treference;sym : tasmsymbol;offset : longint);
  2069. begin
  2070. reference_reset(ref);
  2071. ref.symbol:=sym;
  2072. ref.offset:=offset;
  2073. end;
  2074. procedure reference_release(list: taasmoutput; const ref : treference);
  2075. begin
  2076. rg.ungetreference(list,ref);
  2077. end;
  2078. function references_equal(sref : treference;dref : treference):boolean;
  2079. begin
  2080. references_equal:=CompareByte(sref,dref,sizeof(treference))=0;
  2081. end;
  2082. { on most processors , this routine does nothing, overriden currently }
  2083. { only by 80x86 processor. }
  2084. function trgobj.makeregsize(reg: tregister; size: tcgsize): tregister;
  2085. begin
  2086. makeregsize := reg;
  2087. end;
  2088. {****************************************************************************
  2089. TLocation
  2090. ****************************************************************************}
  2091. procedure location_reset(var l : tlocation;lt:TCGLoc;lsize:TCGSize);
  2092. begin
  2093. FillChar(l,sizeof(tlocation),0);
  2094. l.loc:=lt;
  2095. l.size:=lsize;
  2096. case l.loc of
  2097. LOC_REGISTER,LOC_CREGISTER:
  2098. begin
  2099. l.register.enum:=R_INTREGISTER;
  2100. l.registerhigh.enum:=R_INTREGISTER;
  2101. end;
  2102. LOC_REFERENCE,LOC_CREFERENCE:
  2103. begin
  2104. l.reference.base.enum:=R_INTREGISTER;
  2105. l.reference.index.enum:=R_INTREGISTER;
  2106. {$ifdef i386}
  2107. l.reference.segment.enum:=R_INTREGISTER;
  2108. {$endif}
  2109. end;
  2110. end;
  2111. end;
  2112. procedure location_release(list: taasmoutput; const l : tlocation);
  2113. begin
  2114. case l.loc of
  2115. LOC_REGISTER,LOC_CREGISTER :
  2116. begin
  2117. rg.ungetregisterint(list,l.register);
  2118. if l.size in [OS_64,OS_S64] then
  2119. rg.ungetregisterint(list,l.registerhigh);
  2120. end;
  2121. LOC_CREFERENCE,LOC_REFERENCE :
  2122. rg.ungetreference(list, l.reference);
  2123. end;
  2124. end;
  2125. procedure location_freetemp(list:taasmoutput; const l : tlocation);
  2126. begin
  2127. if (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  2128. tg.ungetiftemp(list,l.reference);
  2129. end;
  2130. procedure location_copy(var destloc:tlocation; const sourceloc : tlocation);
  2131. begin
  2132. destloc:=sourceloc;
  2133. end;
  2134. procedure location_swap(var destloc,sourceloc : tlocation);
  2135. var
  2136. swapl : tlocation;
  2137. begin
  2138. swapl := destloc;
  2139. destloc := sourceloc;
  2140. sourceloc := swapl;
  2141. end;
  2142. initialization
  2143. ;
  2144. finalization
  2145. rg.free;
  2146. end.
  2147. {
  2148. $Log$
  2149. Revision 1.50 2003-06-03 21:11:09 peter
  2150. * cg.a_load_* get a from and to size specifier
  2151. * makeregsize only accepts newregister
  2152. * i386 uses generic tcgnotnode,tcgunaryminus
  2153. Revision 1.49 2003/06/03 13:01:59 daniel
  2154. * Register allocator finished
  2155. Revision 1.48 2003/06/01 21:38:06 peter
  2156. * getregisterfpu size parameter added
  2157. * op_const_reg size parameter added
  2158. * sparc updates
  2159. Revision 1.47 2003/05/31 20:31:11 jonas
  2160. * set inital costs of assigning a variable to a register to 120 for
  2161. non-i386, because the used register must be store to memory at the
  2162. start and loaded again at the end
  2163. Revision 1.46 2003/05/30 18:55:21 jonas
  2164. * fixed several regvar related bugs for non-i386. make cycle with -Or now
  2165. works for ppc
  2166. Revision 1.45 2003/05/30 12:36:13 jonas
  2167. * use as little different registers on the ppc until newra is released,
  2168. since every used register must be saved
  2169. Revision 1.44 2003/05/17 13:30:08 jonas
  2170. * changed tt_persistant to tt_persistent :)
  2171. * tempcreatenode now doesn't accept a boolean anymore for persistent
  2172. temps, but a ttemptype, so you can also create ansistring temps etc
  2173. Revision 1.43 2003/05/16 14:33:31 peter
  2174. * regvar fixes
  2175. Revision 1.42 2003/04/26 20:03:49 daniel
  2176. * Bug fix in simplify
  2177. Revision 1.41 2003/04/25 20:59:35 peter
  2178. * removed funcretn,funcretsym, function result is now in varsym
  2179. and aliases for result and function name are added using absolutesym
  2180. * vs_hidden parameter for funcret passed in parameter
  2181. * vs_hidden fixes
  2182. * writenode changed to printnode and released from extdebug
  2183. * -vp option added to generate a tree.log with the nodetree
  2184. * nicer printnode for statements, callnode
  2185. Revision 1.40 2003/04/25 08:25:26 daniel
  2186. * Ifdefs around a lot of calls to cleartempgen
  2187. * Fixed registers that are allocated but not freed in several nodes
  2188. * Tweak to register allocator to cause less spills
  2189. * 8-bit registers now interfere with esi,edi and ebp
  2190. Compiler can now compile rtl successfully when using new register
  2191. allocator
  2192. Revision 1.39 2003/04/23 20:23:06 peter
  2193. * compile fix for no-newra
  2194. Revision 1.38 2003/04/23 14:42:07 daniel
  2195. * Further register allocator work. Compiler now smaller with new
  2196. allocator than without.
  2197. * Somebody forgot to adjust ppu version number
  2198. Revision 1.37 2003/04/22 23:50:23 peter
  2199. * firstpass uses expectloc
  2200. * checks if there are differences between the expectloc and
  2201. location.loc from secondpass in EXTDEBUG
  2202. Revision 1.36 2003/04/22 10:09:35 daniel
  2203. + Implemented the actual register allocator
  2204. + Scratch registers unavailable when new register allocator used
  2205. + maybe_save/maybe_restore unavailable when new register allocator used
  2206. Revision 1.35 2003/04/21 19:16:49 peter
  2207. * count address regs separate
  2208. Revision 1.34 2003/04/17 16:48:21 daniel
  2209. * Added some code to keep track of move instructions in register
  2210. allocator
  2211. Revision 1.33 2003/04/17 07:50:24 daniel
  2212. * Some work on interference graph construction
  2213. Revision 1.32 2003/03/28 19:16:57 peter
  2214. * generic constructor working for i386
  2215. * remove fixed self register
  2216. * esi added as address register for i386
  2217. Revision 1.31 2003/03/11 21:46:24 jonas
  2218. * lots of new regallocator fixes, both in generic and ppc-specific code
  2219. (ppc compiler still can't compile the linux system unit though)
  2220. Revision 1.30 2003/03/09 21:18:59 olle
  2221. + added cutils to the uses clause
  2222. Revision 1.29 2003/03/08 20:36:41 daniel
  2223. + Added newra version of Ti386shlshrnode
  2224. + Added interference graph construction code
  2225. Revision 1.28 2003/03/08 13:59:16 daniel
  2226. * Work to handle new register notation in ag386nsm
  2227. + Added newra version of Ti386moddivnode
  2228. Revision 1.27 2003/03/08 10:53:48 daniel
  2229. * Created newra version of secondmul in n386add.pas
  2230. Revision 1.26 2003/03/08 08:59:07 daniel
  2231. + $define newra will enable new register allocator
  2232. + getregisterint will return imaginary registers with $newra
  2233. + -sr switch added, will skip register allocation so you can see
  2234. the direct output of the code generator before register allocation
  2235. Revision 1.25 2003/02/26 20:50:45 daniel
  2236. * Fixed ungetreference
  2237. Revision 1.24 2003/02/19 22:39:56 daniel
  2238. * Fixed a few issues
  2239. Revision 1.23 2003/02/19 22:00:14 daniel
  2240. * Code generator converted to new register notation
  2241. - Horribily outdated todo.txt removed
  2242. Revision 1.22 2003/02/02 19:25:54 carl
  2243. * Several bugfixes for m68k target (register alloc., opcode emission)
  2244. + VIS target
  2245. + Generic add more complete (still not verified)
  2246. Revision 1.21 2003/01/08 18:43:57 daniel
  2247. * Tregister changed into a record
  2248. Revision 1.20 2002/10/05 12:43:28 carl
  2249. * fixes for Delphi 6 compilation
  2250. (warning : Some features do not work under Delphi)
  2251. Revision 1.19 2002/08/23 16:14:49 peter
  2252. * tempgen cleanup
  2253. * tt_noreuse temp type added that will be used in genentrycode
  2254. Revision 1.18 2002/08/17 22:09:47 florian
  2255. * result type handling in tcgcal.pass_2 overhauled
  2256. * better tnode.dowrite
  2257. * some ppc stuff fixed
  2258. Revision 1.17 2002/08/17 09:23:42 florian
  2259. * first part of procinfo rewrite
  2260. Revision 1.16 2002/08/06 20:55:23 florian
  2261. * first part of ppc calling conventions fix
  2262. Revision 1.15 2002/08/05 18:27:48 carl
  2263. + more more more documentation
  2264. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  2265. Revision 1.14 2002/08/04 19:06:41 carl
  2266. + added generic exception support (still does not work!)
  2267. + more documentation
  2268. Revision 1.13 2002/07/07 09:52:32 florian
  2269. * powerpc target fixed, very simple units can be compiled
  2270. * some basic stuff for better callparanode handling, far from being finished
  2271. Revision 1.12 2002/07/01 18:46:26 peter
  2272. * internal linker
  2273. * reorganized aasm layer
  2274. Revision 1.11 2002/05/18 13:34:17 peter
  2275. * readded missing revisions
  2276. Revision 1.10 2002/05/16 19:46:44 carl
  2277. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2278. + try to fix temp allocation (still in ifdef)
  2279. + generic constructor calls
  2280. + start of tassembler / tmodulebase class cleanup
  2281. Revision 1.8 2002/04/21 15:23:03 carl
  2282. + makeregsize
  2283. + changeregsize is now a local routine
  2284. Revision 1.7 2002/04/20 21:32:25 carl
  2285. + generic FPC_CHECKPOINTER
  2286. + first parameter offset in stack now portable
  2287. * rename some constants
  2288. + move some cpu stuff to other units
  2289. - remove unused constents
  2290. * fix stacksize for some targets
  2291. * fix generic size problems which depend now on EXTEND_SIZE constant
  2292. Revision 1.6 2002/04/15 19:03:31 carl
  2293. + reg2str -> std_reg2str()
  2294. Revision 1.5 2002/04/06 18:13:01 jonas
  2295. * several powerpc-related additions and fixes
  2296. Revision 1.4 2002/04/04 19:06:04 peter
  2297. * removed unused units
  2298. * use tlocation.size in cg.a_*loc*() routines
  2299. Revision 1.3 2002/04/02 17:11:29 peter
  2300. * tlocation,treference update
  2301. * LOC_CONSTANT added for better constant handling
  2302. * secondadd splitted in multiple routines
  2303. * location_force_reg added for loading a location to a register
  2304. of a specified size
  2305. * secondassignment parses now first the right and then the left node
  2306. (this is compatible with Kylix). This saves a lot of push/pop especially
  2307. with string operations
  2308. * adapted some routines to use the new cg methods
  2309. Revision 1.2 2002/04/01 19:24:25 jonas
  2310. * fixed different parameter name in interface and implementation
  2311. declaration of a method (only 1.0.x detected this)
  2312. Revision 1.1 2002/03/31 20:26:36 jonas
  2313. + a_loadfpu_* and a_loadmm_* methods in tcg
  2314. * register allocation is now handled by a class and is mostly processor
  2315. independent (+rgobj.pas and i386/rgcpu.pas)
  2316. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  2317. * some small improvements and fixes to the optimizer
  2318. * some register allocation fixes
  2319. * some fpuvaroffset fixes in the unary minus node
  2320. * push/popusedregisters is now called rg.save/restoreusedregisters and
  2321. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  2322. also better optimizable)
  2323. * fixed and optimized register saving/restoring for new/dispose nodes
  2324. * LOC_FPU locations now also require their "register" field to be set to
  2325. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  2326. - list field removed of the tnode class because it's not used currently
  2327. and can cause hard-to-find bugs
  2328. }