rgobj.pas 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements the base class for the register allocator
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. {$i fpcdefs.inc}
  19. {# @abstract(Abstract register allocator unit)
  20. This unit contains services to allocate, free
  21. references and registers which are used by
  22. the code generator.
  23. }
  24. {*******************************************************************************
  25. (applies to new register allocator)
  26. Register allocator introduction.
  27. Free Pascal uses a Chaitin style register allocator similair to the one
  28. described in the book "Modern compiler implementation in C" by Andrew W. Appel.,
  29. published by Cambridge University Press.
  30. Reading this book is recommended for a complete understanding. Here is a small
  31. introduction.
  32. The code generator thinks it has an infinite amount of registers. Our processor
  33. has a limited amount of registers. Therefore we must reduce the amount of
  34. registers until there are less enough to fit into the processors registers.
  35. Registers can interfere or not interfere. If two imaginary registers interfere
  36. they cannot be placed into the same psysical register. Reduction of registers
  37. is done by:
  38. - "coalescing" Two registers that do not interfere are combined
  39. into one register.
  40. - "spilling" A register is changed into a memory location and the generated
  41. code is modified to use the memory location instead of the register.
  42. Register allocation is a graph colouring problem. Each register is a colour, and
  43. if two registers interfere there is a connection between them in the graph.
  44. In addition to the imaginary registers in the code generator, the psysical
  45. CPU registers are also present in this graph. This allows us to make
  46. interferences between imaginary registers and cpu registers. This is very
  47. usefull for describing archtectural constrains, like for example that
  48. the div instruction modifies edx, so variables that are in use at that time
  49. cannot be stored into edx. This can be modelled by making edx interfere
  50. with those variables.
  51. *******************************************************************************}
  52. unit rgobj;
  53. interface
  54. uses
  55. cutils, cpubase,
  56. cpuinfo,
  57. aasmbase,aasmtai,aasmcpu,
  58. cclasses,globtype,cginfo,cgbase,node
  59. {$ifdef delphi}
  60. ,dmisc
  61. {$endif}
  62. ;
  63. type
  64. regvar_longintarray = array[firstreg..lastreg] of longint;
  65. regvarint_longintarray = array[first_supreg..last_supreg] of longint;
  66. regvar_booleanarray = array[firstreg..lastreg] of boolean;
  67. regvar_ptreearray = array[firstreg..lastreg] of tnode;
  68. tpushedsavedloc = record
  69. case byte of
  70. 0: (pushed: boolean);
  71. 1: (ofs: longint);
  72. end;
  73. tpushedsaved = array[firstreg..lastreg] of tpushedsavedloc;
  74. Tpushedsavedint = array[first_supreg..last_supreg] of Tpushedsavedloc;
  75. Tinterferencebitmap=array[Tsuperregister] of set of Tsuperregister;
  76. Tinterferenceadjlist=array[Tsuperregister] of Pstring;
  77. Tinterferencegraph=record
  78. bitmap:Tinterferencebitmap;
  79. adjlist:Tinterferenceadjlist;
  80. end;
  81. Pinterferencegraph=^Tinterferencegraph;
  82. {#
  83. This class implements the abstract register allocator
  84. It is used by the code generator to allocate and free
  85. registers which might be valid across nodes. It also
  86. contains utility routines related to registers.
  87. Some of the methods in this class should be overriden
  88. by cpu-specific implementations.
  89. }
  90. trgobj = class
  91. { The "usableregsxxx" contain all registers of type "xxx" that }
  92. { aren't currently allocated to a regvar. The "unusedregsxxx" }
  93. { contain all registers of type "xxx" that aren't currenly }
  94. { allocated }
  95. lastintreg:Tsuperregister;
  96. unusedregsint,usableregsint:Tsupregset;
  97. unusedregsfpu,usableregsfpu : tregisterset;
  98. unusedregsmm,usableregsmm : tregisterset;
  99. { these counters contain the number of elements in the }
  100. { unusedregsxxx/usableregsxxx sets }
  101. countunusedregsint,
  102. countunusedregsfpu,
  103. countunusedregsmm : byte;
  104. countusableregsint,
  105. countusableregsfpu,
  106. countusableregsmm : byte;
  107. { Contains the registers which are really used by the proc itself.
  108. It doesn't take care of registers used by called procedures
  109. }
  110. usedbyproc,
  111. usedinproc : tregisterset;
  112. usedintbyproc,usedintinproc:Tsupregset;
  113. reg_pushes : regvar_longintarray;
  114. reg_pushes_int : regvarint_longintarray;
  115. is_reg_var : regvar_booleanarray;
  116. is_reg_var_int:Tsupregset;
  117. regvar_loaded: regvar_booleanarray;
  118. regvar_loaded_int: Tsupregset;
  119. { tries to hold the amount of times which the current tree is processed }
  120. t_times: longint;
  121. constructor create;
  122. {# Allocate a general purpose register
  123. An internalerror will be generated if there
  124. is no more free registers which can be allocated
  125. }
  126. function getregisterint(list:Taasmoutput;size:Tcgsize):Tregister;virtual;
  127. {# Free a general purpose register
  128. @param(r register to free)
  129. }
  130. procedure ungetregisterint(list: taasmoutput; r : tregister); virtual;
  131. {# Allocate a floating point register
  132. An internalerror will be generated if there
  133. is no more free registers which can be allocated
  134. }
  135. function getregisterfpu(list: taasmoutput) : tregister; virtual;
  136. {# Free a floating point register
  137. @param(r register to free)
  138. }
  139. procedure ungetregisterfpu(list: taasmoutput; r : tregister); virtual;
  140. function getregistermm(list: taasmoutput) : tregister; virtual;
  141. procedure ungetregistermm(list: taasmoutput; r : tregister); virtual;
  142. {# Allocate an address register.
  143. Address registers are the only registers which can
  144. be used as a base register in references (treference).
  145. On most cpu's this is the same as a general purpose
  146. register.
  147. An internalerror will be generated if there
  148. is no more free registers which can be allocated
  149. }
  150. function getaddressregister(list:Taasmoutput):Tregister;virtual;
  151. procedure ungetaddressregister(list: taasmoutput; r: tregister); virtual;
  152. {# Verify if the specified register is an address or
  153. general purpose register. Returns TRUE if @var(reg)
  154. is an adress register.
  155. This routine should only be used to check on
  156. general purpose or address register. It will
  157. not work on multimedia or floating point
  158. registers
  159. @param(reg register to verify)
  160. }
  161. function isaddressregister(reg: tregister): boolean; virtual;
  162. {# Tries to allocate the passed register, if possible
  163. @param(r specific register to allocate)
  164. }
  165. function getexplicitregisterint(list:Taasmoutput;r:Tnewregister):Tregister;virtual;
  166. {# Tries to allocate the passed fpu register, if possible
  167. @param(r specific register to allocate)
  168. }
  169. function getexplicitregisterfpu(list : taasmoutput; r : Toldregister) : tregister;
  170. {# Deallocate any kind of register }
  171. procedure ungetregister(list: taasmoutput; r : tregister); virtual;
  172. {# Deallocate all registers which are allocated
  173. in the specified reference. On most systems,
  174. this will free the base and index registers
  175. of the specified reference.
  176. @param(ref reference which must have its registers freed)
  177. }
  178. procedure ungetreference(list: taasmoutput; const ref : treference); virtual;
  179. {# Reset the register allocator information (usable registers etc) }
  180. procedure cleartempgen;virtual;
  181. {# Convert a register to a specified register size, and return that register size }
  182. function makeregsize(reg: tregister; size: tcgsize): tregister; virtual;
  183. {# saves register variables (restoring happens automatically) }
  184. procedure saveintregvars(list:Taasmoutput;const s:Tsupregset);
  185. procedure saveotherregvars(list:Taasmoutput;const s:Tregisterset);
  186. {# Saves in temporary references (allocated via the temp. allocator)
  187. the registers defined in @var(s). The registers are only saved
  188. if they are currently in use, otherwise they are left as is.
  189. On processors which have instructions which manipulate the stack,
  190. this routine should be overriden for performance reasons.
  191. @param(list) List to add the instruction to
  192. @param(saved) Array of saved register information
  193. @param(s) Registers which might require saving
  194. }
  195. procedure saveusedintregisters(list:Taasmoutput;
  196. var saved:Tpushedsavedint;
  197. const s:Tsupregset);virtual;
  198. procedure saveusedotherregisters(list:Taasmoutput;
  199. var saved:Tpushedsaved;
  200. const s:Tregisterset);virtual;
  201. {# Restores the registers which were saved with a call
  202. to @var(saveusedregisters).
  203. On processors which have instructions which manipulate the stack,
  204. this routine should be overriden for performance reasons.
  205. }
  206. procedure restoreusedintregisters(list:Taasmoutput;
  207. const saved:Tpushedsavedint);virtual;
  208. procedure restoreusedotherregisters(list:Taasmoutput;
  209. const saved:Tpushedsaved);virtual;
  210. { used when deciding which registers to use for regvars }
  211. procedure incrementintregisterpushed(const s:Tsupregset);
  212. procedure incrementotherregisterpushed(const s: tregisterset);
  213. procedure clearregistercount;
  214. procedure resetusableregisters;virtual;
  215. procedure makeregvarint(reg:Tnewregister);
  216. procedure makeregvarother(reg:Tregister);
  217. procedure saveStateForInline(var state: pointer);virtual;
  218. procedure restoreStateAfterInline(var state: pointer);virtual;
  219. procedure saveUnusedState(var state: pointer);virtual;
  220. procedure restoreUnusedState(var state: pointer);virtual;
  221. protected
  222. {$ifdef newra}
  223. igraph:Tinterferencegraph;
  224. {$endif}
  225. { the following two contain the common (generic) code for all }
  226. { get- and ungetregisterxxx functions/procedures }
  227. function getregistergen(list: taasmoutput; const lowreg, highreg: Toldregister;
  228. var unusedregs:Tregisterset; var countunusedregs: byte): tregister;
  229. function getregistergenint(list:Taasmoutput;subreg:Tsubregister;
  230. const lowreg,highreg:Tsuperregister;
  231. var fusedinproc,fusedbyproc,unusedregs:Tsupregset;
  232. var countunusedregs:byte):Tregister;
  233. procedure ungetregistergen(list: taasmoutput; const r: tregister;
  234. const usableregs: tregisterset; var unusedregs: tregisterset; var countunusedregs: byte);
  235. procedure ungetregistergenint(list:taasmoutput;const r:Tregister;
  236. const usableregs:Tsupregset;
  237. var unusedregs:Tsupregset;
  238. var countunusedregs:byte);
  239. {$ifdef TEMPREGDEBUG}
  240. reg_user : regvar_ptreearray;
  241. reg_releaser : regvar_ptreearray;
  242. {$endif TEMPREGDEBUG}
  243. {$ifdef TEMPREGDEBUG}
  244. procedure testregisters;
  245. {$endif TEMPREGDEBUGx}
  246. {$ifdef newra}
  247. procedure add_edge(u,v:Tsuperregister);
  248. procedure add_edges_used(u:Tsuperregister);
  249. {$endif}
  250. end;
  251. const
  252. {# This value is used in tsaved. If the array value is equal
  253. to this, then this means that this register is not used.
  254. }
  255. reg_not_saved = $7fffffff;
  256. var
  257. {# This is the class instance used to access the register allocator class }
  258. rg: trgobj;
  259. { trerefence handling }
  260. {# Clear to zero a treference }
  261. procedure reference_reset(var ref : treference);
  262. procedure reference_reset_old(var ref : treference);
  263. {# Clear to zero a treference, and set is base address
  264. to base register.
  265. }
  266. procedure reference_reset_base(var ref : treference;base : tregister;offset : longint);
  267. procedure reference_reset_symbol(var ref : treference;sym : tasmsymbol;offset : longint);
  268. procedure reference_release(list: taasmoutput; const ref : treference);
  269. { This routine verifies if two references are the same, and
  270. if so, returns TRUE, otherwise returns false.
  271. }
  272. function references_equal(sref : treference;dref : treference) : boolean;
  273. { tlocation handling }
  274. procedure location_reset(var l : tlocation;lt:TLoc;lsize:TCGSize);
  275. procedure location_release(list: taasmoutput; const l : tlocation);
  276. procedure location_freetemp(list: taasmoutput; const l : tlocation);
  277. procedure location_copy(var destloc,sourceloc : tlocation);
  278. procedure location_swap(var destloc,sourceloc : tlocation);
  279. type
  280. psavedstate = ^tsavedstate;
  281. tsavedstate = record
  282. unusedregsint,usableregsint : Tsupregset;
  283. unusedregsaddr,usableregsaddr : Tsupregset;
  284. unusedregsfpu,usableregsfpu : tregisterset;
  285. unusedregsmm,usableregsmm : tregisterset;
  286. countunusedregsint,
  287. countunusedregsaddr,
  288. countunusedregsfpu,
  289. countunusedregsmm : byte;
  290. countusableregsint,
  291. countusableregsfpu,
  292. countusableregsmm : byte;
  293. { contains the registers which are really used by the proc itself }
  294. usedbyproc,
  295. usedinproc : tregisterset;
  296. reg_pushes : regvar_longintarray;
  297. is_reg_var : regvar_booleanarray;
  298. is_reg_var_int : Tsupregset;
  299. regvar_loaded: regvar_booleanarray;
  300. regvar_loaded_int: Tsupregset;
  301. {$ifdef TEMPREGDEBUG}
  302. reg_user : regvar_ptreearray;
  303. reg_releaser : regvar_ptreearray;
  304. {$endif TEMPREGDEBUG}
  305. end;
  306. punusedstate = ^tunusedstate;
  307. tunusedstate = record
  308. unusedregsint : Tsupregset;
  309. unusedregsaddr : Tsupregset;
  310. unusedregsfpu : tregisterset;
  311. unusedregsmm : tregisterset;
  312. countunusedregsint,
  313. countunusedregsaddr,
  314. countunusedregsfpu,
  315. countunusedregsmm : byte;
  316. end;
  317. implementation
  318. uses
  319. systems,
  320. globals,verbose,
  321. cgobj,tgobj,regvars;
  322. constructor trgobj.create;
  323. begin
  324. usedinproc := [];
  325. usedbyproc:=[];
  326. t_times := 0;
  327. resetusableregisters;
  328. lastintreg:=0;
  329. {$ifdef TEMPREGDEBUG}
  330. fillchar(reg_user,sizeof(reg_user),0);
  331. fillchar(reg_releaser,sizeof(reg_releaser),0);
  332. {$endif TEMPREGDEBUG}
  333. {$ifdef newra}
  334. fillchar(igraph,sizeof(igraph),0);
  335. {$endif}
  336. end;
  337. function trgobj.getregistergen(list: taasmoutput; const lowreg, highreg: Toldregister;
  338. var unusedregs: tregisterset; var countunusedregs: byte): tregister;
  339. var
  340. i: Toldregister;
  341. r: Tregister;
  342. begin
  343. for i:=lowreg to highreg do
  344. begin
  345. if i in unusedregs then
  346. begin
  347. exclude(unusedregs,i);
  348. include(usedinproc,i);
  349. include(usedbyproc,i);
  350. dec(countunusedregs);
  351. r.enum:=i;
  352. list.concat(tai_regalloc.alloc(r));
  353. result := r;
  354. exit;
  355. end;
  356. end;
  357. internalerror(10);
  358. end;
  359. function Trgobj.getregistergenint(list:Taasmoutput;
  360. subreg:Tsubregister;
  361. const lowreg,highreg:Tsuperregister;
  362. var fusedinproc,fusedbyproc,unusedregs:Tsupregset;
  363. var countunusedregs:byte):Tregister;
  364. var i:Tsuperregister;
  365. r:Tregister;
  366. begin
  367. if not (lastintreg in [lowreg..highreg]) then
  368. lastintreg:=lowreg;
  369. i:=lastintreg;
  370. repeat
  371. if i=highreg then
  372. i:=lowreg
  373. else
  374. inc(i);
  375. if i in unusedregs then
  376. begin
  377. exclude(unusedregs,i);
  378. include(fusedinproc,i);
  379. include(fusedbyproc,i);
  380. dec(countunusedregs);
  381. r.enum:=R_INTREGISTER;
  382. r.number:=i shl 8 or subreg;
  383. list.concat(Tai_regalloc.alloc(r));
  384. result:=r;
  385. lastintreg:=i;
  386. {$ifdef newra}
  387. add_edges_used(i);
  388. {$endif}
  389. exit;
  390. end;
  391. until i=lastintreg;
  392. internalerror(10);
  393. end;
  394. procedure trgobj.ungetregistergen(list: taasmoutput; const r: tregister;
  395. const usableregs: tregisterset; var unusedregs: tregisterset; var countunusedregs: byte);
  396. begin
  397. if r.enum>lastreg then
  398. internalerror(2003010801);
  399. { takes much time }
  400. if not(r.enum in usableregs) then
  401. exit;
  402. {$ifdef TEMPREGDEBUG}
  403. if (r.enum in unusedregs) then
  404. {$ifdef EXTTEMPREGDEBUG}
  405. begin
  406. Comment(V_Debug,'register freed twice '+std_reg2str[r.enum]);
  407. testregisters32;
  408. exit;
  409. end
  410. {$else EXTTEMPREGDEBUG}
  411. exit
  412. {$endif EXTTEMPREGDEBUG}
  413. else
  414. {$endif TEMPREGDEBUG}
  415. inc(countunusedregs);
  416. include(unusedregs,r.enum);
  417. list.concat(tai_regalloc.dealloc(r));
  418. end;
  419. procedure trgobj.ungetregistergenint(list:taasmoutput;const r:Tregister;
  420. const usableregs:Tsupregset;
  421. var unusedregs:Tsupregset;
  422. var countunusedregs:byte);
  423. var supreg:Tsuperregister;
  424. begin
  425. if r.enum<=lastreg then
  426. internalerror(2003010803);
  427. supreg:=r.number shr 8;
  428. { takes much time }
  429. {$ifndef newra}
  430. if not(supreg in usableregs) then
  431. exit;
  432. {$endif}
  433. {$ifdef TEMPREGDEBUG}
  434. if (supreg in unusedregs) then
  435. {$ifdef EXTTEMPREGDEBUG}
  436. begin
  437. comment(v_debug,'register freed twice '+supreg_name(supreg));
  438. testregisters32;
  439. exit;
  440. end
  441. {$else EXTTEMPREGDEBUG}
  442. exit
  443. {$endif EXTTEMPREGDEBUG}
  444. else
  445. {$endif TEMPREGDEBUG}
  446. inc(countunusedregs);
  447. include(unusedregs,supreg);
  448. list.concat(tai_regalloc.dealloc(r));
  449. end;
  450. function trgobj.getregisterint(list:taasmoutput;size:Tcgsize):Tregister;
  451. var subreg:Tsubregister;
  452. begin
  453. if countunusedregsint=0 then
  454. internalerror(10);
  455. {$ifdef TEMPREGDEBUG}
  456. if curptree^^.usableregs-countunusedregsint>curptree^^.registers32 then
  457. internalerror(10);
  458. {$endif TEMPREGDEBUG}
  459. {$ifdef EXTTEMPREGDEBUG}
  460. if curptree^^.usableregs-countunusedregsint>curptree^^.reallyusedregs then
  461. curptree^^.reallyusedregs:=curptree^^.usableregs-countunusedregsint;
  462. {$endif EXTTEMPREGDEBUG}
  463. subreg:=cgsize2subreg(size);
  464. result:=getregistergenint(list,
  465. subreg,
  466. {$ifdef newra}
  467. first_imreg,
  468. last_imreg,
  469. {$else}
  470. first_supreg,
  471. last_supreg,
  472. {$endif}
  473. usedintbyproc,
  474. usedintinproc,
  475. unusedregsint,
  476. countunusedregsint);
  477. {$ifdef TEMPREGDEBUG}
  478. reg_user[result]:=curptree^;
  479. testregisters32;
  480. {$endif TEMPREGDEBUG}
  481. end;
  482. procedure trgobj.ungetregisterint(list : taasmoutput; r : tregister);
  483. begin
  484. ungetregistergenint(list,r,usableregsint,unusedregsint,
  485. countunusedregsint);
  486. {$ifdef TEMPREGDEBUG}
  487. reg_releaser[r]:=curptree^;
  488. testregisters32;
  489. {$endif TEMPREGDEBUG}
  490. end;
  491. { tries to allocate the passed register, if possible }
  492. function trgobj.getexplicitregisterint(list:Taasmoutput;r:Tnewregister):Tregister;
  493. var r2:Tregister;
  494. begin
  495. if (r shr 8) in unusedregsint then
  496. begin
  497. dec(countunusedregsint);
  498. {$ifdef TEMPREGDEBUG}
  499. if curptree^^.usableregs-countunusedregsint>curptree^^.registers32 then
  500. internalerror(10);
  501. reg_user[r shr 8]:=curptree^;
  502. {$endif TEMPREGDEBUG}
  503. exclude(unusedregsint,r shr 8);
  504. include(usedintinproc,r shr 8);
  505. include(usedintbyproc,r shr 8);
  506. r2.enum:=R_INTREGISTER;
  507. r2.number:=r;
  508. list.concat(tai_regalloc.alloc(r2));
  509. getexplicitregisterint:=r2;
  510. {$ifdef TEMPREGDEBUG}
  511. testregisters32;
  512. {$endif TEMPREGDEBUG}
  513. end
  514. else
  515. { getexplicitregisterint:=getregisterint(list,r and $ff);}
  516. internalerror(200301103);
  517. end;
  518. { tries to allocate the passed register, if possible }
  519. function trgobj.getexplicitregisterfpu(list : taasmoutput; r : Toldregister) : tregister;
  520. var r2:Tregister;
  521. begin
  522. if r in unusedregsfpu then
  523. begin
  524. dec(countunusedregsfpu);
  525. {$ifdef TEMPREGDEBUG}
  526. if curptree^^.usableregs-countunusedregsint>curptree^^.registers32 then
  527. internalerror(10);
  528. reg_user[r]:=curptree^;
  529. {$endif TEMPREGDEBUG}
  530. exclude(unusedregsfpu,r);
  531. include(usedinproc,r);
  532. include(usedbyproc,r);
  533. r2.enum:=r;
  534. list.concat(tai_regalloc.alloc(r2));
  535. getexplicitregisterfpu:=r2;
  536. {$ifdef TEMPREGDEBUG}
  537. testregisters32;
  538. {$endif TEMPREGDEBUG}
  539. end
  540. else
  541. getexplicitregisterfpu:=getregisterfpu(list);
  542. end;
  543. function trgobj.getregisterfpu(list: taasmoutput) : tregister;
  544. begin
  545. if countunusedregsfpu=0 then
  546. internalerror(10);
  547. result := getregistergen(list,firstsavefpureg,lastsavefpureg,
  548. unusedregsfpu,countunusedregsfpu);
  549. end;
  550. procedure trgobj.ungetregisterfpu(list : taasmoutput; r : tregister);
  551. begin
  552. ungetregistergen(list,r,usableregsfpu,unusedregsfpu,
  553. countunusedregsfpu);
  554. end;
  555. function trgobj.getregistermm(list: taasmoutput) : tregister;
  556. begin
  557. if countunusedregsmm=0 then
  558. internalerror(10);
  559. result := getregistergen(list,firstsavemmreg,lastsavemmreg,
  560. unusedregsmm,countunusedregsmm);
  561. end;
  562. procedure trgobj.ungetregistermm(list: taasmoutput; r: tregister);
  563. begin
  564. ungetregistergen(list,r,usableregsmm,unusedregsmm,
  565. countunusedregsmm);
  566. end;
  567. function trgobj.getaddressregister(list:Taasmoutput): tregister;
  568. begin
  569. {An address register is OS_INT per definition.}
  570. result := getregisterint(list,OS_INT);
  571. end;
  572. procedure trgobj.ungetaddressregister(list: taasmoutput; r: tregister);
  573. begin
  574. ungetregisterint(list,r);
  575. end;
  576. function trgobj.isaddressregister(reg: tregister): boolean;
  577. begin
  578. result := true;
  579. end;
  580. procedure trgobj.ungetregister(list: taasmoutput; r : tregister);
  581. begin
  582. if r.enum=R_NO then
  583. exit;
  584. if r.enum>lastreg then
  585. internalerror(200301081);
  586. if r.enum in intregs then
  587. ungetregisterint(list,r)
  588. else if r.enum in fpuregs then
  589. ungetregisterfpu(list,r)
  590. else if r.enum in mmregs then
  591. ungetregistermm(list,r)
  592. else if r.enum in addrregs then
  593. ungetaddressregister(list,r)
  594. else internalerror(2002070602);
  595. end;
  596. procedure Trgobj.cleartempgen;
  597. var i:Tsuperregister;
  598. begin
  599. countunusedregsint:=countusableregsint;
  600. countunusedregsfpu:=countusableregsfpu;
  601. countunusedregsmm:=countusableregsmm;
  602. {$ifdef newra}
  603. unusedregsint:=[0..255];
  604. {$else}
  605. unusedregsint:=usableregsint;
  606. {$endif}
  607. unusedregsfpu:=usableregsfpu;
  608. unusedregsmm:=usableregsmm;
  609. {$ifdef newra}
  610. for i:=low(Tsuperregister) to high(Tsuperregister) do
  611. if igraph.adjlist[i]<>nil then
  612. dispose(igraph.adjlist[i]);
  613. fillchar(igraph,sizeof(igraph),0);
  614. {$endif}
  615. end;
  616. procedure trgobj.ungetreference(list : taasmoutput; const ref : treference);
  617. begin
  618. if ref.base.number<>NR_NO then
  619. ungetregisterint(list,ref.base);
  620. if ref.index.number<>NR_NO then
  621. ungetregisterint(list,ref.index);
  622. end;
  623. procedure trgobj.saveintregvars(list:Taasmoutput;const s:Tsupregset);
  624. var r:Tsuperregister;
  625. begin
  626. if not(cs_regalloc in aktglobalswitches) then
  627. exit;
  628. for r:=firstsaveintreg to lastsaveintreg do
  629. if (r in is_reg_var_int) and
  630. (r in s) then
  631. store_regvar_int(list,r);
  632. end;
  633. procedure trgobj.saveotherregvars(list: taasmoutput; const s: tregisterset);
  634. var
  635. r: Tregister;
  636. begin
  637. if not(cs_regalloc in aktglobalswitches) then
  638. exit;
  639. if firstsavefpureg <> R_NO then
  640. for r.enum := firstsavefpureg to lastsavefpureg do
  641. if is_reg_var[r.enum] and
  642. (r.enum in s) then
  643. store_regvar(list,r);
  644. if firstsavemmreg <> R_NO then
  645. for r.enum := firstsavemmreg to lastsavemmreg do
  646. if is_reg_var[r.enum] and
  647. (r.enum in s) then
  648. store_regvar(list,r);
  649. end;
  650. procedure trgobj.saveusedintregisters(list:Taasmoutput;
  651. var saved:Tpushedsavedint;
  652. const s:Tsupregset);
  653. var r:Tsuperregister;
  654. r2:Tregister;
  655. hr : treference;
  656. begin
  657. usedintinproc:=usedintinproc+s;
  658. for r:=firstsaveintreg to lastsaveintreg do
  659. begin
  660. saved[r].ofs:=reg_not_saved;
  661. { if the register is used by the calling subroutine and if }
  662. { it's not a regvar (those are handled separately) }
  663. if not (r in is_reg_var_int) and
  664. (r in s) and
  665. { and is present in use }
  666. not(r in unusedregsint) then
  667. begin
  668. { then save it }
  669. tg.GetTemp(list,sizeof(aword),tt_persistant,hr);
  670. saved[r].ofs:=hr.offset;
  671. r2.enum:=R_INTREGISTER;
  672. r2.number:=r shl 8 or R_SUBWHOLE;
  673. cg.a_load_reg_ref(list,OS_INT,r2,hr);
  674. cg.a_reg_dealloc(list,r2);
  675. include(unusedregsint,r);
  676. inc(countunusedregsint);
  677. end;
  678. end;
  679. {$ifdef TEMPREGDEBUG}
  680. testregisters32;
  681. {$endif TEMPREGDEBUG}
  682. end;
  683. procedure trgobj.saveusedotherregisters(list: taasmoutput;
  684. var saved : tpushedsaved; const s: tregisterset);
  685. var
  686. r : tregister;
  687. hr : treference;
  688. begin
  689. usedinproc:=usedinproc + s;
  690. { don't try to save the fpu registers if not desired (e.g. for }
  691. { the 80x86) }
  692. if firstsavefpureg <> R_NO then
  693. for r.enum:=firstsavefpureg to lastsavefpureg do
  694. begin
  695. saved[r.enum].ofs:=reg_not_saved;
  696. { if the register is used by the calling subroutine and if }
  697. { it's not a regvar (those are handled separately) }
  698. if not is_reg_var[r.enum] and
  699. (r.enum in s) and
  700. { and is present in use }
  701. not(r.enum in unusedregsfpu) then
  702. begin
  703. { then save it }
  704. tg.GetTemp(list,extended_size,tt_persistant,hr);
  705. saved[r.enum].ofs:=hr.offset;
  706. cg.a_loadfpu_reg_ref(list,OS_FLOAT,r,hr);
  707. cg.a_reg_dealloc(list,r);
  708. include(unusedregsfpu,r.enum);
  709. inc(countunusedregsfpu);
  710. end;
  711. end;
  712. { don't save the vector registers if there's no support for them }
  713. if firstsavemmreg <> R_NO then
  714. for r.enum:=firstsavemmreg to lastsavemmreg do
  715. begin
  716. saved[r.enum].ofs:=reg_not_saved;
  717. { if the register is in use and if it's not a regvar (those }
  718. { are handled separately), save it }
  719. if not is_reg_var[r.enum] and
  720. (r.enum in s) and
  721. { and is present in use }
  722. not(r.enum in unusedregsmm) then
  723. begin
  724. { then save it }
  725. tg.GetTemp(list,mmreg_size,tt_persistant,hr);
  726. saved[r.enum].ofs:=hr.offset;
  727. cg.a_loadmm_reg_ref(list,r,hr);
  728. cg.a_reg_dealloc(list,r);
  729. include(unusedregsmm,r.enum);
  730. inc(countunusedregsmm);
  731. end;
  732. end;
  733. {$ifdef TEMPREGDEBUG}
  734. testregisters32;
  735. {$endif TEMPREGDEBUG}
  736. end;
  737. procedure trgobj.restoreusedintregisters(list:Taasmoutput;
  738. const saved:Tpushedsavedint);
  739. var r:Tsuperregister;
  740. r2:Tregister;
  741. hr:Treference;
  742. begin
  743. for r:=lastsaveintreg downto firstsaveintreg do
  744. begin
  745. if saved[r].ofs <> reg_not_saved then
  746. begin
  747. r2.enum:=R_INTREGISTER;
  748. r2.number:=NR_FRAME_POINTER_REG;
  749. reference_reset_base(hr,r2,saved[r].ofs);
  750. r2.enum:=R_INTREGISTER;
  751. r2.number:=r shl 8 or R_SUBWHOLE;
  752. cg.a_reg_alloc(list,r2);
  753. cg.a_load_ref_reg(list,OS_INT,hr,r2);
  754. if not (r in unusedregsint) then
  755. { internalerror(10)
  756. in n386cal we always save/restore the reg *state*
  757. using save/restoreunusedstate -> the current state
  758. may not be real (JM) }
  759. else
  760. begin
  761. dec(countunusedregsint);
  762. exclude(unusedregsint,r);
  763. end;
  764. tg.UnGetTemp(list,hr);
  765. end;
  766. end;
  767. {$ifdef TEMPREGDEBUG}
  768. testregisters32;
  769. {$endif TEMPREGDEBUG}
  770. end;
  771. procedure trgobj.restoreusedotherregisters(list : taasmoutput;
  772. const saved : tpushedsaved);
  773. var
  774. r,r2 : tregister;
  775. hr : treference;
  776. begin
  777. if firstsavemmreg <> R_NO then
  778. for r.enum:=lastsavemmreg downto firstsavemmreg do
  779. begin
  780. if saved[r.enum].ofs <> reg_not_saved then
  781. begin
  782. r2.enum:=R_INTREGISTER;
  783. r2.number:=NR_FRAME_POINTER_REG;
  784. reference_reset_base(hr,r2,saved[r.enum].ofs);
  785. cg.a_reg_alloc(list,r);
  786. cg.a_loadmm_ref_reg(list,hr,r);
  787. if not (r.enum in unusedregsmm) then
  788. { internalerror(10)
  789. in n386cal we always save/restore the reg *state*
  790. using save/restoreunusedstate -> the current state
  791. may not be real (JM) }
  792. else
  793. begin
  794. dec(countunusedregsmm);
  795. exclude(unusedregsmm,r.enum);
  796. end;
  797. tg.UnGetTemp(list,hr);
  798. end;
  799. end;
  800. if firstsavefpureg <> R_NO then
  801. for r.enum:=lastsavefpureg downto firstsavefpureg do
  802. begin
  803. if saved[r.enum].ofs <> reg_not_saved then
  804. begin
  805. r2.enum:=R_INTREGISTER;
  806. r2.number:=NR_FRAME_POINTER_REG;
  807. reference_reset_base(hr,r2,saved[r.enum].ofs);
  808. cg.a_reg_alloc(list,r);
  809. cg.a_loadfpu_ref_reg(list,OS_FLOAT,hr,r);
  810. if not (r.enum in unusedregsfpu) then
  811. { internalerror(10)
  812. in n386cal we always save/restore the reg *state*
  813. using save/restoreunusedstate -> the current state
  814. may not be real (JM) }
  815. else
  816. begin
  817. dec(countunusedregsfpu);
  818. exclude(unusedregsfpu,r.enum);
  819. end;
  820. tg.UnGetTemp(list,hr);
  821. end;
  822. end;
  823. {$ifdef TEMPREGDEBUG}
  824. testregisters32;
  825. {$endif TEMPREGDEBUG}
  826. end;
  827. procedure trgobj.incrementintregisterpushed(const s:Tsupregset);
  828. var regi:Tsuperregister;
  829. begin
  830. for regi:=firstsaveintreg to lastsaveintreg do
  831. begin
  832. if (regi in s) then
  833. inc(reg_pushes_int[regi],t_times*2);
  834. end;
  835. end;
  836. procedure trgobj.incrementotherregisterpushed(const s:Tregisterset);
  837. var
  838. regi : Toldregister;
  839. begin
  840. if firstsavefpureg <> R_NO then
  841. for regi:=firstsavefpureg to lastsavefpureg do
  842. begin
  843. if (regi in s) then
  844. inc(reg_pushes[regi],t_times*2);
  845. end;
  846. if firstsavemmreg <> R_NO then
  847. for regi:=firstsavemmreg to lastsavemmreg do
  848. begin
  849. if (regi in s) then
  850. inc(reg_pushes[regi],t_times*2);
  851. end;
  852. end;
  853. procedure trgobj.clearregistercount;
  854. begin
  855. fillchar(reg_pushes,sizeof(reg_pushes),0);
  856. fillchar(is_reg_var,sizeof(is_reg_var),false);
  857. is_reg_var_int:=[];
  858. fillchar(regvar_loaded,sizeof(regvar_loaded),false);
  859. regvar_loaded_int:=[];
  860. end;
  861. procedure trgobj.resetusableregisters;
  862. begin
  863. { initialize fields with constant values from cpubase }
  864. countusableregsint := cpubase.c_countusableregsint;
  865. countusableregsfpu := cpubase.c_countusableregsfpu;
  866. countusableregsmm := cpubase.c_countusableregsmm;
  867. usableregsint := cpubase.usableregsint;
  868. usableregsfpu := cpubase.usableregsfpu;
  869. usableregsmm := cpubase.usableregsmm;
  870. clearregistercount;
  871. end;
  872. procedure trgobj.makeregvarint(reg:Tnewregister);
  873. var supreg:Tsuperregister;
  874. begin
  875. supreg:=reg shr 8;
  876. dec(countusableregsint);
  877. dec(countunusedregsint);
  878. exclude(usableregsint,reg);
  879. exclude(unusedregsint,reg);
  880. include(is_reg_var_int,supreg);
  881. end;
  882. procedure trgobj.makeregvarother(reg: tregister);
  883. begin
  884. if reg.enum>lastreg then
  885. internalerror(200301081);
  886. if reg.enum in intregs then
  887. internalerror(200301151)
  888. else if reg.enum in fpuregs then
  889. begin
  890. dec(countusableregsfpu);
  891. dec(countunusedregsfpu);
  892. exclude(usableregsfpu,reg.enum);
  893. exclude(unusedregsfpu,reg.enum);
  894. end
  895. else if reg.enum in mmregs then
  896. begin
  897. dec(countusableregsmm);
  898. dec(countunusedregsmm);
  899. exclude(usableregsmm,reg.enum);
  900. exclude(unusedregsmm,reg.enum);
  901. end;
  902. is_reg_var[reg.enum]:=true;
  903. end;
  904. {$ifdef TEMPREGDEBUG}
  905. procedure trgobj.testregisters;
  906. var
  907. r: tregister;
  908. test : byte;
  909. begin
  910. test:=0;
  911. for r := firstsaveintreg to lastsaveintreg do
  912. inc(test,ord(r in unusedregsint));
  913. if test<>countunusedregsint then
  914. internalerror(10);
  915. end;
  916. {$endif TEMPREGDEBUG}
  917. procedure trgobj.saveStateForInline(var state: pointer);
  918. begin
  919. new(psavedstate(state));
  920. psavedstate(state)^.unusedregsint := unusedregsint;
  921. psavedstate(state)^.usableregsint := usableregsint;
  922. psavedstate(state)^.unusedregsfpu := unusedregsfpu;
  923. psavedstate(state)^.usableregsfpu := usableregsfpu;
  924. psavedstate(state)^.unusedregsmm := unusedregsmm;
  925. psavedstate(state)^.usableregsmm := usableregsmm;
  926. psavedstate(state)^.countunusedregsint := countunusedregsint;
  927. psavedstate(state)^.countunusedregsfpu := countunusedregsfpu;
  928. psavedstate(state)^.countunusedregsmm := countunusedregsmm;
  929. psavedstate(state)^.countusableregsint := countusableregsint;
  930. psavedstate(state)^.countusableregsfpu := countusableregsfpu;
  931. psavedstate(state)^.countusableregsmm := countusableregsmm;
  932. psavedstate(state)^.usedinproc := usedinproc;
  933. psavedstate(state)^.usedbyproc := usedbyproc;
  934. psavedstate(state)^.reg_pushes := reg_pushes;
  935. psavedstate(state)^.is_reg_var := is_reg_var;
  936. psavedstate(state)^.is_reg_var_int := is_reg_var_int;
  937. psavedstate(state)^.regvar_loaded := regvar_loaded;
  938. psavedstate(state)^.regvar_loaded_int := regvar_loaded_int;
  939. {$ifdef TEMPREGDEBUG}
  940. psavedstate(state)^.reg_user := reg_user;
  941. psavedstate(state)^.reg_releaser := reg_releaser;
  942. {$endif TEMPREGDEBUG}
  943. end;
  944. procedure trgobj.restoreStateAfterInline(var state: pointer);
  945. begin
  946. unusedregsint := psavedstate(state)^.unusedregsint;
  947. usableregsint := psavedstate(state)^.usableregsint;
  948. unusedregsfpu := psavedstate(state)^.unusedregsfpu;
  949. usableregsfpu := psavedstate(state)^.usableregsfpu;
  950. unusedregsmm := psavedstate(state)^.unusedregsmm;
  951. usableregsmm := psavedstate(state)^.usableregsmm;
  952. countunusedregsint := psavedstate(state)^.countunusedregsint;
  953. countunusedregsfpu := psavedstate(state)^.countunusedregsfpu;
  954. countunusedregsmm := psavedstate(state)^.countunusedregsmm;
  955. countusableregsint := psavedstate(state)^.countusableregsint;
  956. countusableregsfpu := psavedstate(state)^.countusableregsfpu;
  957. countusableregsmm := psavedstate(state)^.countusableregsmm;
  958. usedinproc := psavedstate(state)^.usedinproc;
  959. usedbyproc := psavedstate(state)^.usedbyproc;
  960. reg_pushes := psavedstate(state)^.reg_pushes;
  961. is_reg_var := psavedstate(state)^.is_reg_var;
  962. is_reg_var_int := psavedstate(state)^.is_reg_var_int;
  963. regvar_loaded := psavedstate(state)^.regvar_loaded;
  964. regvar_loaded_int := psavedstate(state)^.regvar_loaded_int;
  965. {$ifdef TEMPREGDEBUG}
  966. reg_user := psavedstate(state)^.reg_user;
  967. reg_releaser := psavedstate(state)^.reg_releaser;
  968. {$endif TEMPREGDEBUG}
  969. dispose(psavedstate(state));
  970. state := nil;
  971. end;
  972. procedure trgobj.saveUnusedState(var state: pointer);
  973. begin
  974. new(punusedstate(state));
  975. punusedstate(state)^.unusedregsint := unusedregsint;
  976. punusedstate(state)^.unusedregsfpu := unusedregsfpu;
  977. punusedstate(state)^.unusedregsmm := unusedregsmm;
  978. punusedstate(state)^.countunusedregsint := countunusedregsint;
  979. punusedstate(state)^.countunusedregsfpu := countunusedregsfpu;
  980. punusedstate(state)^.countunusedregsmm := countunusedregsmm;
  981. end;
  982. procedure trgobj.restoreUnusedState(var state: pointer);
  983. begin
  984. unusedregsint := punusedstate(state)^.unusedregsint;
  985. unusedregsfpu := punusedstate(state)^.unusedregsfpu;
  986. unusedregsmm := punusedstate(state)^.unusedregsmm;
  987. countunusedregsint := punusedstate(state)^.countunusedregsint;
  988. countunusedregsfpu := punusedstate(state)^.countunusedregsfpu;
  989. countunusedregsmm := punusedstate(state)^.countunusedregsmm;
  990. dispose(punusedstate(state));
  991. state := nil;
  992. end;
  993. {$ifdef newra}
  994. procedure Trgobj.add_edge(u,v:Tsuperregister);
  995. {This procedure will add an edge to the virtual interference graph.}
  996. procedure addadj(u,v:Tsuperregister);
  997. begin
  998. if igraph.adjlist[u]=nil then
  999. begin
  1000. getmem(igraph.adjlist[u],16);
  1001. igraph.adjlist[u]^:='';
  1002. end
  1003. else if (length(igraph.adjlist[u]^) and 15)=15 then
  1004. reallocmem(igraph.adjlist[u],length(igraph.adjlist[u]^)+16);
  1005. igraph.adjlist[u]^:=igraph.adjlist[u]^+char(v);
  1006. end;
  1007. begin
  1008. if (u<>v) and not(v in igraph.bitmap[u]) then
  1009. begin
  1010. include(igraph.bitmap[u],v);
  1011. include(igraph.bitmap[v],u);
  1012. {Precoloured nodes are not stored in the interference graph.}
  1013. if not(u in [first_supreg..last_supreg]) then
  1014. addadj(u,v);
  1015. if not(v in [first_supreg..last_supreg]) then
  1016. addadj(v,u);
  1017. end;
  1018. end;
  1019. procedure Trgobj.add_edges_used(u:Tsuperregister);
  1020. var i:Tsuperregister;
  1021. begin
  1022. for i:=1 to 255 do
  1023. if not(i in unusedregsint) then
  1024. add_edge(u,i);
  1025. end;
  1026. {$endif}
  1027. {****************************************************************************
  1028. TReference
  1029. ****************************************************************************}
  1030. procedure reference_reset(var ref : treference);
  1031. begin
  1032. FillChar(ref,sizeof(treference),0);
  1033. ref.base.enum:=R_INTREGISTER;
  1034. ref.index.enum:=R_INTREGISTER;
  1035. {$ifdef i386}
  1036. ref.segment.enum:=R_INTREGISTER;
  1037. {$endif}
  1038. end;
  1039. procedure reference_reset_old(var ref : treference);
  1040. begin
  1041. FillChar(ref,sizeof(treference),0);
  1042. end;
  1043. procedure reference_reset_base(var ref : treference;base : tregister;offset : longint);
  1044. begin
  1045. reference_reset(ref);
  1046. ref.base:=base;
  1047. ref.offset:=offset;
  1048. end;
  1049. procedure reference_reset_symbol(var ref : treference;sym : tasmsymbol;offset : longint);
  1050. begin
  1051. reference_reset(ref);
  1052. ref.symbol:=sym;
  1053. ref.offset:=offset;
  1054. end;
  1055. procedure reference_release(list: taasmoutput; const ref : treference);
  1056. begin
  1057. rg.ungetreference(list,ref);
  1058. end;
  1059. function references_equal(sref : treference;dref : treference):boolean;
  1060. begin
  1061. references_equal:=CompareByte(sref,dref,sizeof(treference))=0;
  1062. end;
  1063. { on most processors , this routine does nothing, overriden currently }
  1064. { only by 80x86 processor. }
  1065. function trgobj.makeregsize(reg: tregister; size: tcgsize): tregister;
  1066. begin
  1067. makeregsize := reg;
  1068. end;
  1069. {****************************************************************************
  1070. TLocation
  1071. ****************************************************************************}
  1072. procedure location_reset(var l : tlocation;lt:TLoc;lsize:TCGSize);
  1073. begin
  1074. FillChar(l,sizeof(tlocation),0);
  1075. l.loc:=lt;
  1076. l.size:=lsize;
  1077. case l.loc of
  1078. LOC_REGISTER,LOC_CREGISTER:
  1079. begin
  1080. l.register.enum:=R_INTREGISTER;
  1081. l.registerhigh.enum:=R_INTREGISTER;
  1082. end;
  1083. LOC_REFERENCE,LOC_CREFERENCE:
  1084. begin
  1085. l.reference.base.enum:=R_INTREGISTER;
  1086. l.reference.index.enum:=R_INTREGISTER;
  1087. {$ifdef i386}
  1088. l.reference.segment.enum:=R_INTREGISTER;
  1089. {$endif}
  1090. end;
  1091. end;
  1092. end;
  1093. procedure location_release(list: taasmoutput; const l : tlocation);
  1094. begin
  1095. case l.loc of
  1096. LOC_REGISTER,LOC_CREGISTER :
  1097. begin
  1098. rg.ungetregisterint(list,l.register);
  1099. if l.size in [OS_64,OS_S64] then
  1100. rg.ungetregisterint(list,l.registerhigh);
  1101. end;
  1102. LOC_CREFERENCE,LOC_REFERENCE :
  1103. rg.ungetreference(list, l.reference);
  1104. end;
  1105. end;
  1106. procedure location_freetemp(list:taasmoutput; const l : tlocation);
  1107. begin
  1108. if (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  1109. tg.ungetiftemp(list,l.reference);
  1110. end;
  1111. procedure location_copy(var destloc,sourceloc : tlocation);
  1112. begin
  1113. destloc:=sourceloc;
  1114. end;
  1115. procedure location_swap(var destloc,sourceloc : tlocation);
  1116. var
  1117. swapl : tlocation;
  1118. begin
  1119. swapl := destloc;
  1120. destloc := sourceloc;
  1121. sourceloc := swapl;
  1122. end;
  1123. initialization
  1124. ;
  1125. finalization
  1126. rg.free;
  1127. end.
  1128. {
  1129. $Log$
  1130. Revision 1.30 2003-03-09 21:18:59 olle
  1131. + added cutils to the uses clause
  1132. Revision 1.29 2003/03/08 20:36:41 daniel
  1133. + Added newra version of Ti386shlshrnode
  1134. + Added interference graph construction code
  1135. Revision 1.28 2003/03/08 13:59:16 daniel
  1136. * Work to handle new register notation in ag386nsm
  1137. + Added newra version of Ti386moddivnode
  1138. Revision 1.27 2003/03/08 10:53:48 daniel
  1139. * Created newra version of secondmul in n386add.pas
  1140. Revision 1.26 2003/03/08 08:59:07 daniel
  1141. + $define newra will enable new register allocator
  1142. + getregisterint will return imaginary registers with $newra
  1143. + -sr switch added, will skip register allocation so you can see
  1144. the direct output of the code generator before register allocation
  1145. Revision 1.25 2003/02/26 20:50:45 daniel
  1146. * Fixed ungetreference
  1147. Revision 1.24 2003/02/19 22:39:56 daniel
  1148. * Fixed a few issues
  1149. Revision 1.23 2003/02/19 22:00:14 daniel
  1150. * Code generator converted to new register notation
  1151. - Horribily outdated todo.txt removed
  1152. Revision 1.22 2003/02/02 19:25:54 carl
  1153. * Several bugfixes for m68k target (register alloc., opcode emission)
  1154. + VIS target
  1155. + Generic add more complete (still not verified)
  1156. Revision 1.21 2003/01/08 18:43:57 daniel
  1157. * Tregister changed into a record
  1158. Revision 1.20 2002/10/05 12:43:28 carl
  1159. * fixes for Delphi 6 compilation
  1160. (warning : Some features do not work under Delphi)
  1161. Revision 1.19 2002/08/23 16:14:49 peter
  1162. * tempgen cleanup
  1163. * tt_noreuse temp type added that will be used in genentrycode
  1164. Revision 1.18 2002/08/17 22:09:47 florian
  1165. * result type handling in tcgcal.pass_2 overhauled
  1166. * better tnode.dowrite
  1167. * some ppc stuff fixed
  1168. Revision 1.17 2002/08/17 09:23:42 florian
  1169. * first part of procinfo rewrite
  1170. Revision 1.16 2002/08/06 20:55:23 florian
  1171. * first part of ppc calling conventions fix
  1172. Revision 1.15 2002/08/05 18:27:48 carl
  1173. + more more more documentation
  1174. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  1175. Revision 1.14 2002/08/04 19:06:41 carl
  1176. + added generic exception support (still does not work!)
  1177. + more documentation
  1178. Revision 1.13 2002/07/07 09:52:32 florian
  1179. * powerpc target fixed, very simple units can be compiled
  1180. * some basic stuff for better callparanode handling, far from being finished
  1181. Revision 1.12 2002/07/01 18:46:26 peter
  1182. * internal linker
  1183. * reorganized aasm layer
  1184. Revision 1.11 2002/05/18 13:34:17 peter
  1185. * readded missing revisions
  1186. Revision 1.10 2002/05/16 19:46:44 carl
  1187. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1188. + try to fix temp allocation (still in ifdef)
  1189. + generic constructor calls
  1190. + start of tassembler / tmodulebase class cleanup
  1191. Revision 1.8 2002/04/21 15:23:03 carl
  1192. + makeregsize
  1193. + changeregsize is now a local routine
  1194. Revision 1.7 2002/04/20 21:32:25 carl
  1195. + generic FPC_CHECKPOINTER
  1196. + first parameter offset in stack now portable
  1197. * rename some constants
  1198. + move some cpu stuff to other units
  1199. - remove unused constents
  1200. * fix stacksize for some targets
  1201. * fix generic size problems which depend now on EXTEND_SIZE constant
  1202. Revision 1.6 2002/04/15 19:03:31 carl
  1203. + reg2str -> std_reg2str()
  1204. Revision 1.5 2002/04/06 18:13:01 jonas
  1205. * several powerpc-related additions and fixes
  1206. Revision 1.4 2002/04/04 19:06:04 peter
  1207. * removed unused units
  1208. * use tlocation.size in cg.a_*loc*() routines
  1209. Revision 1.3 2002/04/02 17:11:29 peter
  1210. * tlocation,treference update
  1211. * LOC_CONSTANT added for better constant handling
  1212. * secondadd splitted in multiple routines
  1213. * location_force_reg added for loading a location to a register
  1214. of a specified size
  1215. * secondassignment parses now first the right and then the left node
  1216. (this is compatible with Kylix). This saves a lot of push/pop especially
  1217. with string operations
  1218. * adapted some routines to use the new cg methods
  1219. Revision 1.2 2002/04/01 19:24:25 jonas
  1220. * fixed different parameter name in interface and implementation
  1221. declaration of a method (only 1.0.x detected this)
  1222. Revision 1.1 2002/03/31 20:26:36 jonas
  1223. + a_loadfpu_* and a_loadmm_* methods in tcg
  1224. * register allocation is now handled by a class and is mostly processor
  1225. independent (+rgobj.pas and i386/rgcpu.pas)
  1226. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  1227. * some small improvements and fixes to the optimizer
  1228. * some register allocation fixes
  1229. * some fpuvaroffset fixes in the unary minus node
  1230. * push/popusedregisters is now called rg.save/restoreusedregisters and
  1231. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  1232. also better optimizable)
  1233. * fixed and optimized register saving/restoring for new/dispose nodes
  1234. * LOC_FPU locations now also require their "register" field to be set to
  1235. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  1236. - list field removed of the tnode class because it's not used currently
  1237. and can cause hard-to-find bugs
  1238. }