rgobj.pas 87 KB

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