ncgutil.pas 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Helper routines for all code generators
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ncgutil;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,
  22. globtype,
  23. cpubase,cgbase,parabase,cgutils,
  24. aasmbase,aasmtai,aasmdata,aasmcpu,
  25. symconst,symbase,symdef,symsym,symtype
  26. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  27. ,cg64f32
  28. {$endif not cpu64bitalu and not cpuhighleveltarget}
  29. ;
  30. type
  31. tloadregvars = (lr_dont_load_regvars, lr_load_regvars);
  32. pusedregvars = ^tusedregvars;
  33. tusedregvars = record
  34. intregvars, addrregvars, fpuregvars, mmregvars: Tsuperregisterworklist;
  35. end;
  36. {
  37. Not used currently, implemented because I thought we had to
  38. synchronise around if/then/else as well, but not needed. May
  39. still be useful for SSA once we get around to implementing
  40. that (JM)
  41. pusedregvarscommon = ^tusedregvarscommon;
  42. tusedregvarscommon = record
  43. allregvars, commonregvars, myregvars: tusedregvars;
  44. end;
  45. }
  46. procedure firstcomplex(p : tbinarynode);
  47. procedure maketojumpboollabels(list: TAsmList; p: tnode; truelabel, falselabel: tasmlabel);
  48. // procedure remove_non_regvars_from_loc(const t: tlocation; var regs:Tsuperregisterset);
  49. procedure location_force_mmreg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  50. procedure location_allocate_register(list:TAsmList;out l: tlocation;def: tdef;constant: boolean);
  51. { allocate registers for a tlocation; assumes that loc.loc is already
  52. set to LOC_CREGISTER/LOC_CFPUREGISTER/... }
  53. procedure gen_alloc_regloc(list:TAsmList;var loc: tlocation;def: tdef);
  54. procedure register_maybe_adjust_setbase(list: TAsmList; opdef: tdef; var l: tlocation; setbase: aint);
  55. procedure alloc_proc_symbol(pd: tprocdef);
  56. procedure release_proc_symbol(pd:tprocdef);
  57. procedure gen_proc_entry_code(list:TAsmList);
  58. procedure gen_proc_exit_code(list:TAsmList);
  59. procedure gen_save_used_regs(list:TAsmList);
  60. procedure gen_restore_used_regs(list:TAsmList);
  61. procedure get_used_regvars(n: tnode; var rv: tusedregvars);
  62. { adds the regvars used in n and its children to rv.allregvars,
  63. those which were already in rv.allregvars to rv.commonregvars and
  64. uses rv.myregvars as scratch (so that two uses of the same regvar
  65. in a single tree to make it appear in commonregvars). Useful to
  66. find out which regvars are used in two different node trees
  67. e.g. in the "else" and "then" path, or in various case blocks }
  68. // procedure get_used_regvars_common(n: tnode; var rv: tusedregvarscommon);
  69. procedure gen_sync_regvars(list:TAsmList; var rv: tusedregvars);
  70. procedure gen_alloc_symtable(list:TAsmList;pd:tprocdef;st:TSymtable);
  71. procedure gen_free_symtable(list:TAsmList;st:TSymtable);
  72. procedure location_free(list: TAsmList; const location : TLocation);
  73. function getprocalign : shortint;
  74. procedure gen_load_frame_for_exceptfilter(list : TAsmList);
  75. procedure gen_alloc_regvar(list:TAsmList;sym: tabstractnormalvarsym; allocreg: boolean);
  76. implementation
  77. uses
  78. cutils,cclasses,
  79. globals,systems,verbose,
  80. defutil,
  81. procinfo,paramgr,
  82. dbgbase,
  83. nbas,ncon,nld,nmem,nutils,
  84. tgobj,cgobj,hlcgobj,hlcgcpu
  85. {$ifdef powerpc}
  86. , cpupi
  87. {$endif}
  88. {$ifdef powerpc64}
  89. , cpupi
  90. {$endif}
  91. {$ifdef SUPPORT_MMX}
  92. , cgx86
  93. {$endif SUPPORT_MMX}
  94. ;
  95. {*****************************************************************************
  96. Misc Helpers
  97. *****************************************************************************}
  98. {$if first_mm_imreg = 0}
  99. {$WARN 4044 OFF} { Comparison might be always false ... }
  100. {$endif}
  101. procedure location_free(list: TAsmList; const location : TLocation);
  102. begin
  103. case location.loc of
  104. LOC_VOID:
  105. ;
  106. LOC_REGISTER,
  107. LOC_CREGISTER:
  108. begin
  109. {$if defined(cpu64bitalu)}
  110. { x86-64 system v abi:
  111. structs with up to 16 bytes are returned in registers }
  112. if location.size in [OS_128,OS_S128] then
  113. begin
  114. if getsupreg(location.register)<first_int_imreg then
  115. cg.ungetcpuregister(list,location.register);
  116. if getsupreg(location.registerhi)<first_int_imreg then
  117. cg.ungetcpuregister(list,location.registerhi);
  118. end
  119. else
  120. {$elseif not defined(cpuhighleveltarget)}
  121. if location.size in [OS_64,OS_S64] then
  122. begin
  123. if getsupreg(location.register64.reglo)<first_int_imreg then
  124. cg.ungetcpuregister(list,location.register64.reglo);
  125. if getsupreg(location.register64.reghi)<first_int_imreg then
  126. cg.ungetcpuregister(list,location.register64.reghi);
  127. end
  128. else
  129. {$endif cpu64bitalu and not cpuhighleveltarget}
  130. if getsupreg(location.register)<first_int_imreg then
  131. cg.ungetcpuregister(list,location.register);
  132. end;
  133. LOC_FPUREGISTER,
  134. LOC_CFPUREGISTER:
  135. begin
  136. if getsupreg(location.register)<first_fpu_imreg then
  137. cg.ungetcpuregister(list,location.register);
  138. end;
  139. LOC_MMREGISTER,
  140. LOC_CMMREGISTER :
  141. begin
  142. if getsupreg(location.register)<first_mm_imreg then
  143. cg.ungetcpuregister(list,location.register);
  144. end;
  145. LOC_REFERENCE,
  146. LOC_CREFERENCE :
  147. begin
  148. if paramanager.use_fixed_stack then
  149. location_freetemp(list,location);
  150. end;
  151. else
  152. internalerror(2004110211);
  153. end;
  154. end;
  155. procedure firstcomplex(p : tbinarynode);
  156. var
  157. fcl, fcr: longint;
  158. ncl, ncr: longint;
  159. begin
  160. { always calculate boolean AND and OR from left to right }
  161. if (p.nodetype in [orn,andn]) and
  162. is_boolean(p.left.resultdef) then
  163. begin
  164. if nf_swapped in p.flags then
  165. internalerror(200709253);
  166. end
  167. else
  168. begin
  169. fcl:=node_resources_fpu(p.left);
  170. fcr:=node_resources_fpu(p.right);
  171. ncl:=node_complexity(p.left);
  172. ncr:=node_complexity(p.right);
  173. { We swap left and right if
  174. a) right needs more floating point registers than left, and
  175. left needs more than 0 floating point registers (if it
  176. doesn't need any, swapping won't change the floating
  177. point register pressure)
  178. b) both left and right need an equal amount of floating
  179. point registers or right needs no floating point registers,
  180. and in addition right has a higher complexity than left
  181. (+- needs more integer registers, but not necessarily)
  182. }
  183. if ((fcr>fcl) and
  184. (fcl>0)) or
  185. (((fcr=fcl) or
  186. (fcr=0)) and
  187. (ncr>ncl)) and
  188. { if one tree contains nodes being conditionally executated, we cannot swap the trees
  189. as the other tree might depend on all nodes being executed, this applies for example
  190. for temp. create nodes with init part, they must be executed else things break, see
  191. issue #34653
  192. }
  193. not(has_conditional_nodes(p.right)) then
  194. p.swapleftright
  195. end;
  196. end;
  197. procedure maketojumpboollabels(list: TAsmList; p: tnode; truelabel, falselabel: tasmlabel);
  198. {
  199. produces jumps to true respectively false labels using boolean expressions
  200. }
  201. var
  202. opsize : tcgsize;
  203. storepos : tfileposinfo;
  204. tmpreg : tregister;
  205. begin
  206. if nf_error in p.flags then
  207. exit;
  208. storepos:=current_filepos;
  209. current_filepos:=p.fileinfo;
  210. if is_boolean(p.resultdef) then
  211. begin
  212. if is_constboolnode(p) then
  213. begin
  214. if Tordconstnode(p).value.uvalue<>0 then
  215. cg.a_jmp_always(list,truelabel)
  216. else
  217. cg.a_jmp_always(list,falselabel)
  218. end
  219. else
  220. begin
  221. opsize:=def_cgsize(p.resultdef);
  222. case p.location.loc of
  223. LOC_SUBSETREG,LOC_CSUBSETREG:
  224. begin
  225. if p.location.sreg.bitlen=1 then
  226. begin
  227. tmpreg:=cg.getintregister(list,p.location.sreg.subsetregsize);
  228. hlcg.a_op_const_reg_reg(list,OP_AND,cgsize_orddef(p.location.sreg.subsetregsize),1 shl p.location.sreg.startbit,p.location.sreg.subsetreg,tmpreg);
  229. end
  230. else
  231. begin
  232. tmpreg:=cg.getintregister(list,OS_INT);
  233. hlcg.a_load_loc_reg(list,p.resultdef,osuinttype,p.location,tmpreg);
  234. end;
  235. cg.a_cmp_const_reg_label(list,OS_INT,OC_NE,0,tmpreg,truelabel);
  236. cg.a_jmp_always(list,falselabel);
  237. end;
  238. LOC_SUBSETREF,LOC_CSUBSETREF:
  239. begin
  240. if (p.location.sref.bitindexreg=NR_NO) and (p.location.sref.bitlen=1) then
  241. begin
  242. tmpreg:=cg.getintregister(list,OS_INT);
  243. hlcg.a_load_ref_reg(list,u8inttype,osuinttype,p.location.sref.ref,tmpreg);
  244. if target_info.endian=endian_big then
  245. hlcg.a_op_const_reg_reg(list,OP_AND,osuinttype,1 shl (8-(p.location.sref.startbit+1)),tmpreg,tmpreg)
  246. else
  247. hlcg.a_op_const_reg_reg(list,OP_AND,osuinttype,1 shl p.location.sref.startbit,tmpreg,tmpreg);
  248. end
  249. else
  250. begin
  251. tmpreg:=cg.getintregister(list,OS_INT);
  252. hlcg.a_load_loc_reg(list,p.resultdef,osuinttype,p.location,tmpreg);
  253. end;
  254. cg.a_cmp_const_reg_label(list,OS_INT,OC_NE,0,tmpreg,truelabel);
  255. cg.a_jmp_always(list,falselabel);
  256. end;
  257. LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE :
  258. begin
  259. {$if defined(cpu64bitalu)}
  260. if opsize in [OS_128,OS_S128] then
  261. begin
  262. hlcg.location_force_reg(list,p.location,p.resultdef,cgsize_orddef(opsize),true);
  263. tmpreg:=cg.getintregister(list,OS_64);
  264. cg.a_op_reg_reg_reg(list,OP_OR,OS_64,p.location.register128.reglo,p.location.register128.reghi,tmpreg);
  265. location_reset(p.location,LOC_REGISTER,OS_64);
  266. p.location.register:=tmpreg;
  267. opsize:=OS_64;
  268. end;
  269. {$elseif not defined(cpuhighleveltarget)}
  270. if opsize in [OS_64,OS_S64] then
  271. begin
  272. hlcg.location_force_reg(list,p.location,p.resultdef,cgsize_orddef(opsize),true);
  273. tmpreg:=cg.getintregister(list,OS_32);
  274. cg.a_op_reg_reg_reg(list,OP_OR,OS_32,p.location.register64.reglo,p.location.register64.reghi,tmpreg);
  275. location_reset(p.location,LOC_REGISTER,OS_32);
  276. p.location.register:=tmpreg;
  277. opsize:=OS_32;
  278. end;
  279. {$endif cpu64bitalu and not cpuhighleveltarget}
  280. cg.a_cmp_const_loc_label(list,opsize,OC_NE,0,p.location,truelabel);
  281. cg.a_jmp_always(list,falselabel);
  282. end;
  283. LOC_JUMP:
  284. begin
  285. if truelabel<>p.location.truelabel then
  286. begin
  287. cg.a_label(list,p.location.truelabel);
  288. cg.a_jmp_always(list,truelabel);
  289. end;
  290. if falselabel<>p.location.falselabel then
  291. begin
  292. cg.a_label(list,p.location.falselabel);
  293. cg.a_jmp_always(list,falselabel);
  294. end;
  295. end;
  296. {$ifdef cpuflags}
  297. LOC_FLAGS :
  298. begin
  299. cg.a_jmp_flags(list,p.location.resflags,truelabel);
  300. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  301. cg.a_jmp_always(list,falselabel);
  302. end;
  303. {$endif cpuflags}
  304. else
  305. begin
  306. printnode(output,p);
  307. internalerror(200308241);
  308. end;
  309. end;
  310. end;
  311. location_reset_jump(p.location,truelabel,falselabel);
  312. end
  313. else
  314. internalerror(200112305);
  315. current_filepos:=storepos;
  316. end;
  317. (*
  318. This code needs fixing. It is not safe to use rgint; on the m68000 it
  319. would be rgaddr.
  320. procedure remove_non_regvars_from_loc(const t: tlocation; var regs:Tsuperregisterset);
  321. begin
  322. case t.loc of
  323. LOC_REGISTER:
  324. begin
  325. { can't be a regvar, since it would be LOC_CREGISTER then }
  326. exclude(regs,getsupreg(t.register));
  327. if t.register64.reghi<>NR_NO then
  328. exclude(regs,getsupreg(t.register64.reghi));
  329. end;
  330. LOC_CREFERENCE,LOC_REFERENCE:
  331. begin
  332. if not(cs_opt_regvar in current_settings.optimizerswitches) or
  333. (getsupreg(t.reference.base) in cg.rgint.usableregs) then
  334. exclude(regs,getsupreg(t.reference.base));
  335. if not(cs_opt_regvar in current_settings.optimizerswitches) or
  336. (getsupreg(t.reference.index) in cg.rgint.usableregs) then
  337. exclude(regs,getsupreg(t.reference.index));
  338. end;
  339. end;
  340. end;
  341. *)
  342. {*****************************************************************************
  343. TLocation
  344. *****************************************************************************}
  345. procedure register_maybe_adjust_setbase(list: TAsmList; opdef: tdef; var l: tlocation; setbase: aint);
  346. var
  347. tmpreg: tregister;
  348. begin
  349. if (setbase<>0) then
  350. begin
  351. if not(l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  352. internalerror(2007091502);
  353. { subtract the setbase }
  354. case l.loc of
  355. LOC_CREGISTER:
  356. begin
  357. tmpreg := hlcg.getintregister(list,opdef);
  358. hlcg.a_op_const_reg_reg(list,OP_SUB,opdef,setbase,l.register,tmpreg);
  359. l.loc:=LOC_REGISTER;
  360. l.register:=tmpreg;
  361. end;
  362. LOC_REGISTER:
  363. begin
  364. hlcg.a_op_const_reg(list,OP_SUB,opdef,setbase,l.register);
  365. end;
  366. end;
  367. end;
  368. end;
  369. procedure location_force_mmreg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  370. var
  371. reg : tregister;
  372. begin
  373. if (l.loc<>LOC_MMREGISTER) and
  374. ((l.loc<>LOC_CMMREGISTER) or (not maybeconst)) then
  375. begin
  376. reg:=cg.getmmregister(list,OS_VECTOR);
  377. cg.a_loadmm_loc_reg(list,OS_VECTOR,l,reg,nil);
  378. location_freetemp(list,l);
  379. location_reset(l,LOC_MMREGISTER,OS_VECTOR);
  380. l.register:=reg;
  381. end;
  382. end;
  383. procedure location_allocate_register(list: TAsmList;out l: tlocation;def: tdef;constant: boolean);
  384. begin
  385. l.size:=def_cgsize(def);
  386. if (def.typ=floatdef) and
  387. not(cs_fp_emulation in current_settings.moduleswitches) then
  388. begin
  389. if use_vectorfpu(def) then
  390. begin
  391. if constant then
  392. location_reset(l,LOC_CMMREGISTER,l.size)
  393. else
  394. location_reset(l,LOC_MMREGISTER,l.size);
  395. l.register:=cg.getmmregister(list,l.size);
  396. end
  397. else
  398. begin
  399. if constant then
  400. location_reset(l,LOC_CFPUREGISTER,l.size)
  401. else
  402. location_reset(l,LOC_FPUREGISTER,l.size);
  403. l.register:=cg.getfpuregister(list,l.size);
  404. end;
  405. end
  406. else
  407. begin
  408. if constant then
  409. location_reset(l,LOC_CREGISTER,l.size)
  410. else
  411. location_reset(l,LOC_REGISTER,l.size);
  412. {$if defined(cpu64bitalu)}
  413. if l.size in [OS_128,OS_S128,OS_F128] then
  414. begin
  415. l.register128.reglo:=cg.getintregister(list,OS_64);
  416. l.register128.reghi:=cg.getintregister(list,OS_64);
  417. end
  418. else
  419. {$elseif not defined(cpuhighleveltarget)}
  420. if l.size in [OS_64,OS_S64,OS_F64] then
  421. begin
  422. l.register64.reglo:=cg.getintregister(list,OS_32);
  423. l.register64.reghi:=cg.getintregister(list,OS_32);
  424. end
  425. else
  426. {$endif cpu64bitalu and not cpuhighleveltarget}
  427. { Note: for widths of records (and maybe objects, classes, etc.) an
  428. address register could be set here, but that is later
  429. changed to an intregister neverthless when in the
  430. tcgassignmentnode thlcgobj.maybe_change_load_node_reg is
  431. called for the temporary node; so the workaround for now is
  432. to fix the symptoms... }
  433. l.register:=hlcg.getregisterfordef(list,def);
  434. end;
  435. end;
  436. {****************************************************************************
  437. Init/Finalize Code
  438. ****************************************************************************}
  439. { generates the code for incrementing the reference count of parameters and
  440. initialize out parameters }
  441. procedure init_paras(p:TObject;arg:pointer);
  442. var
  443. href : treference;
  444. hsym : tparavarsym;
  445. eldef : tdef;
  446. list : TAsmList;
  447. needs_inittable : boolean;
  448. begin
  449. list:=TAsmList(arg);
  450. if (tsym(p).typ=paravarsym) then
  451. begin
  452. needs_inittable:=is_managed_type(tparavarsym(p).vardef);
  453. if not needs_inittable then
  454. exit;
  455. case tparavarsym(p).varspez of
  456. vs_value :
  457. begin
  458. { variants are already handled by the call to fpc_variant_copy_overwrite if
  459. they are passed by reference }
  460. if not((tparavarsym(p).vardef.typ=variantdef) and
  461. paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  462. begin
  463. hlcg.location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,
  464. is_open_array(tparavarsym(p).vardef) or
  465. ((target_info.system in systems_caller_copy_addr_value_para) and
  466. paramanager.push_addr_param(vs_value,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)),
  467. sizeof(pint));
  468. if is_open_array(tparavarsym(p).vardef) then
  469. begin
  470. { open arrays do not contain correct element count in their rtti,
  471. the actual count must be passed separately. }
  472. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  473. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  474. if not assigned(hsym) then
  475. internalerror(201003031);
  476. hlcg.g_array_rtti_helper(list,eldef,href,hsym.initialloc,'fpc_addref_array');
  477. end
  478. else
  479. hlcg.g_incrrefcount(list,tparavarsym(p).vardef,href);
  480. end;
  481. end;
  482. vs_out :
  483. begin
  484. { we have no idea about the alignment at the callee side,
  485. and the user also cannot specify "unaligned" here, so
  486. assume worst case }
  487. hlcg.location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  488. if is_open_array(tparavarsym(p).vardef) then
  489. begin
  490. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  491. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  492. if not assigned(hsym) then
  493. internalerror(201103033);
  494. hlcg.g_array_rtti_helper(list,eldef,href,hsym.initialloc,'fpc_initialize_array');
  495. end
  496. else
  497. hlcg.g_initialize(list,tparavarsym(p).vardef,href);
  498. end;
  499. end;
  500. end;
  501. end;
  502. procedure gen_alloc_regloc(list:TAsmList;var loc: tlocation;def: tdef);
  503. begin
  504. case loc.loc of
  505. LOC_CREGISTER:
  506. begin
  507. {$if defined(cpu64bitalu)}
  508. if loc.size in [OS_128,OS_S128] then
  509. begin
  510. loc.register128.reglo:=cg.getintregister(list,OS_64);
  511. loc.register128.reghi:=cg.getintregister(list,OS_64);
  512. end
  513. else
  514. {$elseif not defined(cpuhighleveltarget)}
  515. if loc.size in [OS_64,OS_S64] then
  516. begin
  517. loc.register64.reglo:=cg.getintregister(list,OS_32);
  518. loc.register64.reghi:=cg.getintregister(list,OS_32);
  519. end
  520. else
  521. {$endif cpu64bitalu and not cpuhighleveltarget}
  522. if hlcg.def2regtyp(def)=R_ADDRESSREGISTER then
  523. loc.register:=hlcg.getaddressregister(list,def)
  524. else
  525. loc.register:=cg.getintregister(list,loc.size);
  526. end;
  527. LOC_CFPUREGISTER:
  528. begin
  529. loc.register:=cg.getfpuregister(list,loc.size);
  530. end;
  531. LOC_CMMREGISTER:
  532. begin
  533. loc.register:=cg.getmmregister(list,loc.size);
  534. end;
  535. end;
  536. end;
  537. procedure gen_alloc_regvar(list:TAsmList;sym: tabstractnormalvarsym; allocreg: boolean);
  538. var
  539. usedef: tdef;
  540. varloc: tai_varloc;
  541. begin
  542. if allocreg then
  543. begin
  544. if sym.typ=paravarsym then
  545. usedef:=tparavarsym(sym).paraloc[calleeside].def
  546. else
  547. usedef:=sym.vardef;
  548. gen_alloc_regloc(list,sym.initialloc,usedef);
  549. end;
  550. if (pi_has_label in current_procinfo.flags) then
  551. begin
  552. { Allocate register already, to prevent first allocation to be
  553. inside a loop }
  554. {$if defined(cpu64bitalu)}
  555. if sym.initialloc.size in [OS_128,OS_S128] then
  556. begin
  557. cg.a_reg_sync(list,sym.initialloc.register128.reglo);
  558. cg.a_reg_sync(list,sym.initialloc.register128.reghi);
  559. end
  560. else
  561. {$elseif defined(cpu32bitalu) and not defined(cpuhighleveltarget)}
  562. if sym.initialloc.size in [OS_64,OS_S64] then
  563. begin
  564. cg.a_reg_sync(list,sym.initialloc.register64.reglo);
  565. cg.a_reg_sync(list,sym.initialloc.register64.reghi);
  566. end
  567. else
  568. {$elseif defined(cpu16bitalu) and not defined(cpuhighleveltarget)}
  569. if sym.initialloc.size in [OS_64,OS_S64] then
  570. begin
  571. cg.a_reg_sync(list,sym.initialloc.register64.reglo);
  572. cg.a_reg_sync(list,cg.GetNextReg(sym.initialloc.register64.reglo));
  573. cg.a_reg_sync(list,sym.initialloc.register64.reghi);
  574. cg.a_reg_sync(list,cg.GetNextReg(sym.initialloc.register64.reghi));
  575. end
  576. else
  577. if sym.initialloc.size in [OS_32,OS_S32] then
  578. begin
  579. cg.a_reg_sync(list,sym.initialloc.register);
  580. cg.a_reg_sync(list,cg.GetNextReg(sym.initialloc.register));
  581. end
  582. else
  583. {$elseif defined(cpu8bitalu) and not defined(cpuhighleveltarget)}
  584. if sym.initialloc.size in [OS_64,OS_S64] then
  585. begin
  586. cg.a_reg_sync(list,sym.initialloc.register64.reglo);
  587. cg.a_reg_sync(list,cg.GetNextReg(sym.initialloc.register64.reglo));
  588. cg.a_reg_sync(list,cg.GetNextReg(cg.GetNextReg(sym.initialloc.register64.reglo)));
  589. cg.a_reg_sync(list,cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(sym.initialloc.register64.reglo))));
  590. cg.a_reg_sync(list,sym.initialloc.register64.reghi);
  591. cg.a_reg_sync(list,cg.GetNextReg(sym.initialloc.register64.reghi));
  592. cg.a_reg_sync(list,cg.GetNextReg(cg.GetNextReg(sym.initialloc.register64.reghi)));
  593. cg.a_reg_sync(list,cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(sym.initialloc.register64.reghi))));
  594. end
  595. else
  596. if sym.initialloc.size in [OS_32,OS_S32] then
  597. begin
  598. cg.a_reg_sync(list,sym.initialloc.register);
  599. cg.a_reg_sync(list,cg.GetNextReg(sym.initialloc.register));
  600. cg.a_reg_sync(list,cg.GetNextReg(cg.GetNextReg(sym.initialloc.register)));
  601. cg.a_reg_sync(list,cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(sym.initialloc.register))));
  602. end
  603. else
  604. if sym.initialloc.size in [OS_16,OS_S16] then
  605. begin
  606. cg.a_reg_sync(list,sym.initialloc.register);
  607. cg.a_reg_sync(list,cg.GetNextReg(sym.initialloc.register));
  608. end
  609. else
  610. {$endif}
  611. cg.a_reg_sync(list,sym.initialloc.register);
  612. end;
  613. {$if defined(cpu64bitalu)}
  614. if (sym.initialloc.size in [OS_128,OS_S128]) then
  615. varloc:=tai_varloc.create128(sym,sym.initialloc.register,sym.initialloc.registerhi)
  616. else
  617. {$elseif not defined(cpuhighleveltarget)}
  618. if (sym.initialloc.size in [OS_64,OS_S64]) then
  619. varloc:=tai_varloc.create64(sym,sym.initialloc.register,sym.initialloc.registerhi)
  620. else
  621. {$endif cpu64bitalu and not cpuhighleveltarget}
  622. varloc:=tai_varloc.create(sym,sym.initialloc.register);
  623. list.concat(varloc);
  624. end;
  625. {****************************************************************************
  626. Entry/Exit
  627. ****************************************************************************}
  628. procedure alloc_proc_symbol(pd: tprocdef);
  629. var
  630. item : TCmdStrListItem;
  631. begin
  632. item := TCmdStrListItem(pd.aliasnames.first);
  633. while assigned(item) do
  634. begin
  635. { The condition to use global or local symbol must match
  636. the code written in hlcg.gen_proc_symbol to
  637. avoid change from AB_LOCAL to AB_GLOBAL, which generates
  638. erroneous code (at least for targets using GOT) }
  639. if (cs_profile in current_settings.moduleswitches) or
  640. (po_global in current_procinfo.procdef.procoptions) then
  641. current_asmdata.DefineAsmSymbol(item.str,AB_GLOBAL,AT_FUNCTION,pd)
  642. else
  643. current_asmdata.DefineAsmSymbol(item.str,AB_LOCAL,AT_FUNCTION,pd);
  644. item := TCmdStrListItem(item.next);
  645. end;
  646. end;
  647. procedure release_proc_symbol(pd:tprocdef);
  648. var
  649. idx : longint;
  650. item : TCmdStrListItem;
  651. begin
  652. item:=TCmdStrListItem(pd.aliasnames.first);
  653. while assigned(item) do
  654. begin
  655. idx:=current_asmdata.AsmSymbolDict.findindexof(item.str);
  656. if idx>=0 then
  657. current_asmdata.AsmSymbolDict.Delete(idx);
  658. item:=TCmdStrListItem(item.next);
  659. end;
  660. end;
  661. procedure gen_proc_entry_code(list:TAsmList);
  662. var
  663. hitemp,
  664. lotemp, stack_frame_size : longint;
  665. begin
  666. { generate call frame marker for dwarf call frame info }
  667. current_asmdata.asmcfi.start_frame(list);
  668. { labels etc. for exception frames are inserted here }
  669. current_procinfo.start_eh(list);
  670. if current_procinfo.procdef.proctypeoption=potype_proginit then
  671. current_asmdata.asmcfi.outmost_frame(list);
  672. { All temps are know, write offsets used for information }
  673. if (cs_asm_source in current_settings.globalswitches) and
  674. (current_procinfo.tempstart<>tg.lasttemp) then
  675. begin
  676. if tg.direction>0 then
  677. begin
  678. lotemp:=current_procinfo.tempstart;
  679. hitemp:=tg.lasttemp;
  680. end
  681. else
  682. begin
  683. lotemp:=tg.lasttemp;
  684. hitemp:=current_procinfo.tempstart;
  685. end;
  686. list.concat(Tai_comment.Create(strpnew('Temps allocated between '+std_regname(current_procinfo.framepointer)+
  687. tostr_with_plus(lotemp)+' and '+std_regname(current_procinfo.framepointer)+tostr_with_plus(hitemp))));
  688. end;
  689. { generate target specific proc entry code }
  690. stack_frame_size := current_procinfo.calc_stackframe_size;
  691. if (stack_frame_size <> 0) and
  692. (po_nostackframe in current_procinfo.procdef.procoptions) then
  693. message1(parser_e_nostackframe_with_locals,tostr(stack_frame_size));
  694. hlcg.g_proc_entry(list,stack_frame_size,(po_nostackframe in current_procinfo.procdef.procoptions));
  695. end;
  696. procedure gen_proc_exit_code(list:TAsmList);
  697. var
  698. parasize : longint;
  699. begin
  700. { c style clearstack does not need to remove parameters from the stack, only the
  701. return value when it was pushed by arguments }
  702. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  703. begin
  704. parasize:=0;
  705. { For safecall functions with safecall-exceptions enabled the funcret is always returned as a para
  706. which is considered a normal para on the c-side, so the funcret has to be pop'ed normally. }
  707. if not ( (current_procinfo.procdef.proccalloption=pocall_safecall) and
  708. (tf_safecall_exceptions in target_info.flags) ) and
  709. paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef) then
  710. inc(parasize,sizeof(pint));
  711. end
  712. else
  713. begin
  714. parasize:=current_procinfo.para_stack_size;
  715. { the parent frame pointer para has to be removed by the caller in
  716. case of Delphi-style parent frame pointer passing }
  717. if not paramanager.use_fixed_stack and
  718. (po_delphi_nested_cc in current_procinfo.procdef.procoptions) then
  719. dec(parasize,sizeof(pint));
  720. end;
  721. { generate target specific proc exit code }
  722. hlcg.g_proc_exit(list,parasize,(po_nostackframe in current_procinfo.procdef.procoptions));
  723. { labels etc. for exception frames are inserted here }
  724. current_procinfo.end_eh(list);
  725. { release return registers, needed for optimizer }
  726. if not is_void(current_procinfo.procdef.returndef) then
  727. paramanager.freecgpara(list,current_procinfo.procdef.funcretloc[calleeside]);
  728. { end of frame marker for call frame info }
  729. current_asmdata.asmcfi.end_frame(list);
  730. end;
  731. procedure gen_save_used_regs(list:TAsmList);
  732. begin
  733. { Pure assembler routines need to save the registers themselves }
  734. if (po_assembler in current_procinfo.procdef.procoptions) then
  735. exit;
  736. cg.g_save_registers(list);
  737. end;
  738. procedure gen_restore_used_regs(list:TAsmList);
  739. begin
  740. { Pure assembler routines need to save the registers themselves }
  741. if (po_assembler in current_procinfo.procdef.procoptions) then
  742. exit;
  743. cg.g_restore_registers(list);
  744. end;
  745. {****************************************************************************
  746. Const Data
  747. ****************************************************************************}
  748. procedure gen_alloc_symtable(list:TAsmList;pd:tprocdef;st:TSymtable);
  749. var
  750. i : longint;
  751. highsym,
  752. sym : tsym;
  753. vs : tabstractnormalvarsym;
  754. ptrdef : tdef;
  755. isaddr : boolean;
  756. begin
  757. for i:=0 to st.SymList.Count-1 do
  758. begin
  759. sym:=tsym(st.SymList[i]);
  760. case sym.typ of
  761. staticvarsym :
  762. begin
  763. vs:=tabstractnormalvarsym(sym);
  764. { The code in loadnode.pass_generatecode will create the
  765. LOC_REFERENCE instead for all none register variables. This is
  766. required because we can't store an asmsymbol in the localloc because
  767. the asmsymbol is invalid after an unit is compiled. This gives
  768. problems when this procedure is inlined in another unit (PFV) }
  769. if vs.is_regvar(false) then
  770. begin
  771. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable];
  772. vs.initialloc.size:=def_cgsize(vs.vardef);
  773. gen_alloc_regvar(list,vs,true);
  774. hlcg.varsym_set_localloc(list,vs);
  775. end;
  776. end;
  777. paravarsym :
  778. begin
  779. vs:=tabstractnormalvarsym(sym);
  780. { Parameters passed to assembler procedures need to be kept
  781. in the original location }
  782. if (po_assembler in pd.procoptions) then
  783. tparavarsym(vs).paraloc[calleeside].get_location(vs.initialloc)
  784. { exception filters receive their frame pointer as a parameter }
  785. else if (pd.proctypeoption=potype_exceptfilter) and
  786. (vo_is_parentfp in vs.varoptions) then
  787. begin
  788. location_reset(vs.initialloc,LOC_REGISTER,OS_ADDR);
  789. vs.initialloc.register:=NR_FRAME_POINTER_REG;
  790. end
  791. else
  792. begin
  793. { if an open array is used, also its high parameter is used,
  794. since the hidden high parameters are inserted after the corresponding symbols,
  795. we can increase the ref. count here }
  796. if is_open_array(vs.vardef) or is_array_of_const(vs.vardef) then
  797. begin
  798. highsym:=get_high_value_sym(tparavarsym(vs));
  799. if assigned(highsym) then
  800. inc(highsym.refs);
  801. end;
  802. isaddr:=paramanager.push_addr_param(vs.varspez,vs.vardef,pd.proccalloption);
  803. if isaddr then
  804. vs.initialloc.size:=def_cgsize(voidpointertype)
  805. else
  806. vs.initialloc.size:=def_cgsize(vs.vardef);
  807. if vs.is_regvar(isaddr) then
  808. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable]
  809. else
  810. begin
  811. vs.initialloc.loc:=LOC_REFERENCE;
  812. { Reuse the parameter location for values to are at a single location on the stack }
  813. if paramanager.param_use_paraloc(tparavarsym(vs).paraloc[calleeside]) then
  814. begin
  815. hlcg.paravarsym_set_initialloc_to_paraloc(tparavarsym(vs));
  816. end
  817. else
  818. begin
  819. if isaddr then
  820. begin
  821. ptrdef:=cpointerdef.getreusable(vs.vardef);
  822. tg.GetLocal(list,ptrdef.size,ptrdef,vs.initialloc.reference)
  823. end
  824. else
  825. tg.GetLocal(list,vs.getsize,tparavarsym(vs).paraloc[calleeside].alignment,vs.vardef,vs.initialloc.reference);
  826. end;
  827. end;
  828. end;
  829. hlcg.varsym_set_localloc(list,vs);
  830. end;
  831. localvarsym :
  832. begin
  833. vs:=tabstractnormalvarsym(sym);
  834. vs.initialloc.size:=def_cgsize(vs.vardef);
  835. if ([po_assembler,po_nostackframe] * pd.procoptions = [po_assembler,po_nostackframe]) and
  836. (vo_is_funcret in vs.varoptions) then
  837. begin
  838. paramanager.create_funcretloc_info(pd,calleeside);
  839. if assigned(pd.funcretloc[calleeside].location^.next) then
  840. begin
  841. { can't replace references to "result" with a complex
  842. location expression inside assembler code }
  843. location_reset(vs.initialloc,LOC_INVALID,OS_NO);
  844. end
  845. else
  846. pd.funcretloc[calleeside].get_location(vs.initialloc);
  847. end
  848. else if (m_delphi in current_settings.modeswitches) and
  849. (po_assembler in pd.procoptions) and
  850. (vo_is_funcret in vs.varoptions) and
  851. (vs.refs=0) then
  852. begin
  853. { not referenced, so don't allocate. Use dummy to }
  854. { avoid ie's later on because of LOC_INVALID }
  855. vs.initialloc.loc:=LOC_REGISTER;
  856. vs.initialloc.size:=OS_INT;
  857. vs.initialloc.register:=NR_FUNCTION_RESULT_REG;
  858. end
  859. else if vs.is_regvar(false) then
  860. begin
  861. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable];
  862. gen_alloc_regvar(list,vs,true);
  863. end
  864. else
  865. begin
  866. vs.initialloc.loc:=LOC_REFERENCE;
  867. tg.GetLocal(list,vs.getsize,vs.vardef,vs.initialloc.reference);
  868. end;
  869. hlcg.varsym_set_localloc(list,vs);
  870. end;
  871. end;
  872. end;
  873. end;
  874. procedure add_regvars(var rv: tusedregvars; const location: tlocation);
  875. begin
  876. case location.loc of
  877. LOC_CREGISTER:
  878. {$if defined(cpu64bitalu)}
  879. if location.size in [OS_128,OS_S128] then
  880. begin
  881. rv.intregvars.addnodup(getsupreg(location.register128.reglo));
  882. rv.intregvars.addnodup(getsupreg(location.register128.reghi));
  883. end
  884. else
  885. {$elseif defined(cpu32bitalu)}
  886. if location.size in [OS_64,OS_S64] then
  887. begin
  888. rv.intregvars.addnodup(getsupreg(location.register64.reglo));
  889. rv.intregvars.addnodup(getsupreg(location.register64.reghi));
  890. end
  891. else
  892. {$elseif defined(cpu16bitalu)}
  893. if location.size in [OS_64,OS_S64] then
  894. begin
  895. rv.intregvars.addnodup(getsupreg(location.register64.reglo));
  896. rv.intregvars.addnodup(getsupreg(cg.GetNextReg(location.register64.reglo)));
  897. rv.intregvars.addnodup(getsupreg(location.register64.reghi));
  898. rv.intregvars.addnodup(getsupreg(cg.GetNextReg(location.register64.reghi)));
  899. end
  900. else
  901. if location.size in [OS_32,OS_S32] then
  902. begin
  903. rv.intregvars.addnodup(getsupreg(location.register));
  904. rv.intregvars.addnodup(getsupreg(cg.GetNextReg(location.register)));
  905. end
  906. else
  907. {$elseif defined(cpu8bitalu)}
  908. if location.size in [OS_64,OS_S64] then
  909. begin
  910. rv.intregvars.addnodup(getsupreg(location.register64.reglo));
  911. rv.intregvars.addnodup(getsupreg(cg.GetNextReg(location.register64.reglo)));
  912. rv.intregvars.addnodup(getsupreg(cg.GetNextReg(cg.GetNextReg(location.register64.reglo))));
  913. rv.intregvars.addnodup(getsupreg(cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(location.register64.reglo)))));
  914. rv.intregvars.addnodup(getsupreg(location.register64.reghi));
  915. rv.intregvars.addnodup(getsupreg(cg.GetNextReg(location.register64.reghi)));
  916. rv.intregvars.addnodup(getsupreg(cg.GetNextReg(cg.GetNextReg(location.register64.reghi))));
  917. rv.intregvars.addnodup(getsupreg(cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(location.register64.reghi)))));
  918. end
  919. else
  920. if location.size in [OS_32,OS_S32] then
  921. begin
  922. rv.intregvars.addnodup(getsupreg(location.register));
  923. rv.intregvars.addnodup(getsupreg(cg.GetNextReg(location.register)));
  924. rv.intregvars.addnodup(getsupreg(cg.GetNextReg(cg.GetNextReg(location.register))));
  925. rv.intregvars.addnodup(getsupreg(cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(location.register)))));
  926. end
  927. else
  928. if location.size in [OS_16,OS_S16] then
  929. begin
  930. rv.intregvars.addnodup(getsupreg(location.register));
  931. rv.intregvars.addnodup(getsupreg(cg.GetNextReg(location.register)));
  932. end
  933. else
  934. {$endif}
  935. if getregtype(location.register)=R_INTREGISTER then
  936. rv.intregvars.addnodup(getsupreg(location.register))
  937. else
  938. rv.addrregvars.addnodup(getsupreg(location.register));
  939. LOC_CFPUREGISTER:
  940. rv.fpuregvars.addnodup(getsupreg(location.register));
  941. LOC_CMMREGISTER:
  942. rv.mmregvars.addnodup(getsupreg(location.register));
  943. end;
  944. end;
  945. function do_get_used_regvars(var n: tnode; arg: pointer): foreachnoderesult;
  946. var
  947. rv: pusedregvars absolute arg;
  948. begin
  949. case (n.nodetype) of
  950. temprefn:
  951. { We only have to synchronise a tempnode before a loop if it is }
  952. { not created inside the loop, and only synchronise after the }
  953. { loop if it's not destroyed inside the loop. If it's created }
  954. { before the loop and not yet destroyed, then before the loop }
  955. { is secondpassed tempinfo^.valid will be true, and we get the }
  956. { correct registers. If it's not destroyed inside the loop, }
  957. { then after the loop has been secondpassed tempinfo^.valid }
  958. { be true and we also get the right registers. In other cases, }
  959. { tempinfo^.valid will be false and so we do not add }
  960. { unnecessary registers. This way, we don't have to look at }
  961. { tempcreate and tempdestroy nodes to get this info (JM) }
  962. if (ti_valid in ttemprefnode(n).tempflags) then
  963. add_regvars(rv^,ttemprefnode(n).tempinfo^.location);
  964. loadn:
  965. if (tloadnode(n).symtableentry.typ in [staticvarsym,localvarsym,paravarsym]) then
  966. add_regvars(rv^,tabstractnormalvarsym(tloadnode(n).symtableentry).localloc);
  967. vecn:
  968. { range checks sometimes need the high parameter }
  969. if (cs_check_range in current_settings.localswitches) and
  970. (is_open_array(tvecnode(n).left.resultdef) or
  971. is_array_of_const(tvecnode(n).left.resultdef)) and
  972. not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  973. add_regvars(rv^,tabstractnormalvarsym(get_high_value_sym(tparavarsym(tloadnode(tvecnode(n).left).symtableentry))).localloc)
  974. end;
  975. result := fen_true;
  976. end;
  977. procedure get_used_regvars(n: tnode; var rv: tusedregvars);
  978. begin
  979. foreachnodestatic(n,@do_get_used_regvars,@rv);
  980. end;
  981. (*
  982. See comments at declaration of pusedregvarscommon
  983. function do_get_used_regvars_common(var n: tnode; arg: pointer): foreachnoderesult;
  984. var
  985. rv: pusedregvarscommon absolute arg;
  986. begin
  987. if (n.nodetype = loadn) and
  988. (tloadnode(n).symtableentry.typ in [staticvarsym,localvarsym,paravarsym]) then
  989. with tabstractnormalvarsym(tloadnode(n).symtableentry).localloc do
  990. case loc of
  991. LOC_CREGISTER:
  992. { if not yet encountered in this node tree }
  993. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  994. { but nevertheless already encountered somewhere }
  995. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  996. { then it's a regvar used in two or more node trees }
  997. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  998. LOC_CFPUREGISTER:
  999. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  1000. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  1001. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  1002. LOC_CMMREGISTER:
  1003. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  1004. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  1005. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  1006. end;
  1007. result := fen_true;
  1008. end;
  1009. procedure get_used_regvars_common(n: tnode; var rv: tusedregvarscommon);
  1010. begin
  1011. rv.myregvars.intregvars.clear;
  1012. rv.myregvars.fpuregvars.clear;
  1013. rv.myregvars.mmregvars.clear;
  1014. foreachnodestatic(n,@do_get_used_regvars_common,@rv);
  1015. end;
  1016. *)
  1017. procedure gen_sync_regvars(list:TAsmList; var rv: tusedregvars);
  1018. var
  1019. count: longint;
  1020. begin
  1021. for count := 1 to rv.intregvars.length do
  1022. cg.a_reg_sync(list,newreg(R_INTREGISTER,rv.intregvars.readidx(count-1),R_SUBWHOLE));
  1023. for count := 1 to rv.addrregvars.length do
  1024. cg.a_reg_sync(list,newreg(R_ADDRESSREGISTER,rv.addrregvars.readidx(count-1),R_SUBWHOLE));
  1025. for count := 1 to rv.fpuregvars.length do
  1026. cg.a_reg_sync(list,newreg(R_FPUREGISTER,rv.fpuregvars.readidx(count-1),R_SUBWHOLE));
  1027. for count := 1 to rv.mmregvars.length do
  1028. cg.a_reg_sync(list,newreg(R_MMREGISTER,rv.mmregvars.readidx(count-1),R_SUBWHOLE));
  1029. end;
  1030. procedure gen_free_symtable(list:TAsmList;st:TSymtable);
  1031. var
  1032. i : longint;
  1033. sym : tsym;
  1034. begin
  1035. for i:=0 to st.SymList.Count-1 do
  1036. begin
  1037. sym:=tsym(st.SymList[i]);
  1038. if (sym.typ in [staticvarsym,localvarsym,paravarsym]) then
  1039. begin
  1040. with tabstractnormalvarsym(sym) do
  1041. begin
  1042. { Note: We need to keep the data available in memory
  1043. for the sub procedures that can access local data
  1044. in the parent procedures }
  1045. case localloc.loc of
  1046. LOC_CREGISTER :
  1047. if (pi_has_label in current_procinfo.flags) then
  1048. {$if defined(cpu64bitalu)}
  1049. if def_cgsize(vardef) in [OS_128,OS_S128] then
  1050. begin
  1051. cg.a_reg_sync(list,localloc.register128.reglo);
  1052. cg.a_reg_sync(list,localloc.register128.reghi);
  1053. end
  1054. else
  1055. {$elseif defined(cpu32bitalu)}
  1056. if def_cgsize(vardef) in [OS_64,OS_S64] then
  1057. begin
  1058. cg.a_reg_sync(list,localloc.register64.reglo);
  1059. cg.a_reg_sync(list,localloc.register64.reghi);
  1060. end
  1061. else
  1062. {$elseif defined(cpu16bitalu)}
  1063. if def_cgsize(vardef) in [OS_64,OS_S64] then
  1064. begin
  1065. cg.a_reg_sync(list,localloc.register64.reglo);
  1066. cg.a_reg_sync(list,cg.GetNextReg(localloc.register64.reglo));
  1067. cg.a_reg_sync(list,localloc.register64.reghi);
  1068. cg.a_reg_sync(list,cg.GetNextReg(localloc.register64.reghi));
  1069. end
  1070. else
  1071. if def_cgsize(vardef) in [OS_32,OS_S32] then
  1072. begin
  1073. cg.a_reg_sync(list,localloc.register);
  1074. cg.a_reg_sync(list,cg.GetNextReg(localloc.register));
  1075. end
  1076. else
  1077. {$elseif defined(cpu8bitalu)}
  1078. if def_cgsize(vardef) in [OS_64,OS_S64] then
  1079. begin
  1080. cg.a_reg_sync(list,localloc.register64.reglo);
  1081. cg.a_reg_sync(list,cg.GetNextReg(localloc.register64.reglo));
  1082. cg.a_reg_sync(list,cg.GetNextReg(cg.GetNextReg(localloc.register64.reglo)));
  1083. cg.a_reg_sync(list,cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(localloc.register64.reglo))));
  1084. cg.a_reg_sync(list,localloc.register64.reghi);
  1085. cg.a_reg_sync(list,cg.GetNextReg(localloc.register64.reghi));
  1086. cg.a_reg_sync(list,cg.GetNextReg(cg.GetNextReg(localloc.register64.reghi)));
  1087. cg.a_reg_sync(list,cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(localloc.register64.reghi))));
  1088. end
  1089. else
  1090. if def_cgsize(vardef) in [OS_32,OS_S32] then
  1091. begin
  1092. cg.a_reg_sync(list,localloc.register);
  1093. cg.a_reg_sync(list,cg.GetNextReg(localloc.register));
  1094. cg.a_reg_sync(list,cg.GetNextReg(cg.GetNextReg(localloc.register)));
  1095. cg.a_reg_sync(list,cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(localloc.register))));
  1096. end
  1097. else
  1098. if def_cgsize(vardef) in [OS_16,OS_S16] then
  1099. begin
  1100. cg.a_reg_sync(list,localloc.register);
  1101. cg.a_reg_sync(list,cg.GetNextReg(localloc.register));
  1102. end
  1103. else
  1104. {$endif}
  1105. cg.a_reg_sync(list,localloc.register);
  1106. LOC_CFPUREGISTER,
  1107. LOC_CMMREGISTER:
  1108. if (pi_has_label in current_procinfo.flags) then
  1109. cg.a_reg_sync(list,localloc.register);
  1110. LOC_REFERENCE :
  1111. begin
  1112. { can't free the result, because we load it after
  1113. this call into the function result location
  1114. (gets freed in thlcgobj.gen_load_return_value();) }
  1115. if (typ in [localvarsym,paravarsym]) and
  1116. (([vo_is_funcret,vo_is_result]*varoptions)=[]) and
  1117. ((current_procinfo.procdef.proctypeoption<>potype_constructor) or
  1118. not(vo_is_self in varoptions)) then
  1119. tg.Ungetlocal(list,localloc.reference);
  1120. end;
  1121. end;
  1122. end;
  1123. end;
  1124. end;
  1125. end;
  1126. function getprocalign : shortint;
  1127. begin
  1128. { gprof uses 16 byte granularity }
  1129. if (cs_profile in current_settings.moduleswitches) then
  1130. result:=16
  1131. else
  1132. result:=current_settings.alignment.procalign;
  1133. end;
  1134. procedure gen_load_frame_for_exceptfilter(list : TAsmList);
  1135. var
  1136. para: tparavarsym;
  1137. begin
  1138. para:=tparavarsym(current_procinfo.procdef.paras[0]);
  1139. if not (vo_is_parentfp in para.varoptions) then
  1140. InternalError(201201142);
  1141. if (para.paraloc[calleeside].location^.loc<>LOC_REGISTER) or
  1142. (para.paraloc[calleeside].location^.next<>nil) then
  1143. InternalError(201201143);
  1144. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,para.paraloc[calleeside].location^.register,
  1145. NR_FRAME_POINTER_REG);
  1146. end;
  1147. end.