ncgutil.pas 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  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. { e.g. xtensa has no default flags but uses a boolean register }
  301. {$ifndef xtensa}
  302. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  303. {$endif xtensa}
  304. cg.a_jmp_always(list,falselabel);
  305. end;
  306. {$endif cpuflags}
  307. else
  308. begin
  309. printnode(output,p);
  310. internalerror(200308241);
  311. end;
  312. end;
  313. end;
  314. location_reset_jump(p.location,truelabel,falselabel);
  315. end
  316. else
  317. internalerror(200112305);
  318. current_filepos:=storepos;
  319. end;
  320. (*
  321. This code needs fixing. It is not safe to use rgint; on the m68000 it
  322. would be rgaddr.
  323. procedure remove_non_regvars_from_loc(const t: tlocation; var regs:Tsuperregisterset);
  324. begin
  325. case t.loc of
  326. LOC_REGISTER:
  327. begin
  328. { can't be a regvar, since it would be LOC_CREGISTER then }
  329. exclude(regs,getsupreg(t.register));
  330. if t.register64.reghi<>NR_NO then
  331. exclude(regs,getsupreg(t.register64.reghi));
  332. end;
  333. LOC_CREFERENCE,LOC_REFERENCE:
  334. begin
  335. if not(cs_opt_regvar in current_settings.optimizerswitches) or
  336. (getsupreg(t.reference.base) in cg.rgint.usableregs) then
  337. exclude(regs,getsupreg(t.reference.base));
  338. if not(cs_opt_regvar in current_settings.optimizerswitches) or
  339. (getsupreg(t.reference.index) in cg.rgint.usableregs) then
  340. exclude(regs,getsupreg(t.reference.index));
  341. end;
  342. end;
  343. end;
  344. *)
  345. {*****************************************************************************
  346. TLocation
  347. *****************************************************************************}
  348. procedure register_maybe_adjust_setbase(list: TAsmList; opdef: tdef; var l: tlocation; setbase: aint);
  349. var
  350. tmpreg: tregister;
  351. begin
  352. if (setbase<>0) then
  353. begin
  354. { subtract the setbase }
  355. case l.loc of
  356. LOC_CREGISTER:
  357. begin
  358. tmpreg := hlcg.getintregister(list,opdef);
  359. hlcg.a_op_const_reg_reg(list,OP_SUB,opdef,setbase,l.register,tmpreg);
  360. l.loc:=LOC_REGISTER;
  361. l.register:=tmpreg;
  362. end;
  363. LOC_REGISTER:
  364. begin
  365. hlcg.a_op_const_reg(list,OP_SUB,opdef,setbase,l.register);
  366. end;
  367. else
  368. internalerror(2007091502);
  369. end;
  370. end;
  371. end;
  372. procedure location_force_mmreg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  373. var
  374. reg : tregister;
  375. begin
  376. if (l.loc<>LOC_MMREGISTER) and
  377. ((l.loc<>LOC_CMMREGISTER) or (not maybeconst)) then
  378. begin
  379. reg:=cg.getmmregister(list,l.size);
  380. cg.a_loadmm_loc_reg(list,l.size,l,reg,nil);
  381. location_freetemp(list,l);
  382. location_reset(l,LOC_MMREGISTER,l.size);
  383. l.register:=reg;
  384. end;
  385. end;
  386. procedure location_allocate_register(list: TAsmList;out l: tlocation;def: tdef;constant: boolean);
  387. begin
  388. l.size:=def_cgsize(def);
  389. if (def.typ=floatdef) and
  390. not(cs_fp_emulation in current_settings.moduleswitches) then
  391. begin
  392. if use_vectorfpu(def) then
  393. begin
  394. if constant then
  395. location_reset(l,LOC_CMMREGISTER,l.size)
  396. else
  397. location_reset(l,LOC_MMREGISTER,l.size);
  398. l.register:=cg.getmmregister(list,l.size);
  399. end
  400. else
  401. begin
  402. if constant then
  403. location_reset(l,LOC_CFPUREGISTER,l.size)
  404. else
  405. location_reset(l,LOC_FPUREGISTER,l.size);
  406. l.register:=cg.getfpuregister(list,l.size);
  407. end;
  408. end
  409. else
  410. begin
  411. if constant then
  412. location_reset(l,LOC_CREGISTER,l.size)
  413. else
  414. location_reset(l,LOC_REGISTER,l.size);
  415. {$if defined(cpu64bitalu)}
  416. if l.size in [OS_128,OS_S128,OS_F128] then
  417. begin
  418. l.register128.reglo:=cg.getintregister(list,OS_64);
  419. l.register128.reghi:=cg.getintregister(list,OS_64);
  420. end
  421. else
  422. {$elseif not defined(cpuhighleveltarget)}
  423. if l.size in [OS_64,OS_S64,OS_F64] then
  424. begin
  425. l.register64.reglo:=cg.getintregister(list,OS_32);
  426. l.register64.reghi:=cg.getintregister(list,OS_32);
  427. end
  428. else
  429. {$endif cpu64bitalu and not cpuhighleveltarget}
  430. { Note: for widths of records (and maybe objects, classes, etc.) an
  431. address register could be set here, but that is later
  432. changed to an intregister neverthless when in the
  433. tcgassignmentnode thlcgobj.maybe_change_load_node_reg is
  434. called for the temporary node; so the workaround for now is
  435. to fix the symptoms... }
  436. l.register:=hlcg.getregisterfordef(list,def);
  437. end;
  438. end;
  439. {****************************************************************************
  440. Init/Finalize Code
  441. ****************************************************************************}
  442. { generates the code for incrementing the reference count of parameters and
  443. initialize out parameters }
  444. procedure init_paras(p:TObject;arg:pointer);
  445. var
  446. href : treference;
  447. hsym : tparavarsym;
  448. eldef : tdef;
  449. list : TAsmList;
  450. needs_inittable : boolean;
  451. begin
  452. list:=TAsmList(arg);
  453. if (tsym(p).typ=paravarsym) then
  454. begin
  455. needs_inittable:=is_managed_type(tparavarsym(p).vardef);
  456. if not needs_inittable then
  457. exit;
  458. case tparavarsym(p).varspez of
  459. vs_value :
  460. begin
  461. { variants are already handled by the call to fpc_variant_copy_overwrite if
  462. they are passed by reference }
  463. if not((tparavarsym(p).vardef.typ=variantdef) and
  464. paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  465. begin
  466. hlcg.location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,
  467. is_open_array(tparavarsym(p).vardef) or
  468. ((target_info.system in systems_caller_copy_addr_value_para) and
  469. paramanager.push_addr_param(vs_value,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)),
  470. sizeof(pint));
  471. if is_open_array(tparavarsym(p).vardef) then
  472. begin
  473. { open arrays do not contain correct element count in their rtti,
  474. the actual count must be passed separately. }
  475. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  476. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  477. if not assigned(hsym) then
  478. internalerror(201003031);
  479. hlcg.g_array_rtti_helper(list,eldef,href,hsym.initialloc,'fpc_addref_array');
  480. end
  481. else
  482. hlcg.g_incrrefcount(list,tparavarsym(p).vardef,href);
  483. end;
  484. end;
  485. vs_out :
  486. begin
  487. { we have no idea about the alignment at the callee side,
  488. and the user also cannot specify "unaligned" here, so
  489. assume worst case }
  490. hlcg.location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  491. if is_open_array(tparavarsym(p).vardef) then
  492. begin
  493. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  494. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  495. if not assigned(hsym) then
  496. internalerror(201103033);
  497. hlcg.g_array_rtti_helper(list,eldef,href,hsym.initialloc,'fpc_initialize_array');
  498. end
  499. else
  500. hlcg.g_initialize(list,tparavarsym(p).vardef,href);
  501. end;
  502. else
  503. ;
  504. end;
  505. end;
  506. end;
  507. procedure gen_alloc_regloc(list:TAsmList;var loc: tlocation;def: tdef);
  508. begin
  509. case loc.loc of
  510. LOC_CREGISTER:
  511. begin
  512. {$if defined(cpu64bitalu)}
  513. if loc.size in [OS_128,OS_S128] then
  514. begin
  515. loc.register128.reglo:=cg.getintregister(list,OS_64);
  516. loc.register128.reghi:=cg.getintregister(list,OS_64);
  517. end
  518. else
  519. {$elseif not defined(cpuhighleveltarget)}
  520. if loc.size in [OS_64,OS_S64] then
  521. begin
  522. loc.register64.reglo:=cg.getintregister(list,OS_32);
  523. loc.register64.reghi:=cg.getintregister(list,OS_32);
  524. end
  525. else
  526. {$endif cpu64bitalu and not cpuhighleveltarget}
  527. if hlcg.def2regtyp(def)=R_ADDRESSREGISTER then
  528. loc.register:=hlcg.getaddressregister(list,def)
  529. else
  530. loc.register:=cg.getintregister(list,loc.size);
  531. end;
  532. LOC_CFPUREGISTER:
  533. begin
  534. loc.register:=cg.getfpuregister(list,loc.size);
  535. end;
  536. LOC_CMMREGISTER:
  537. begin
  538. loc.register:=cg.getmmregister(list,loc.size);
  539. end;
  540. else
  541. ;
  542. end;
  543. end;
  544. procedure gen_alloc_regvar(list:TAsmList;sym: tabstractnormalvarsym; allocreg: boolean);
  545. var
  546. usedef: tdef;
  547. varloc: tai_varloc;
  548. begin
  549. if allocreg then
  550. begin
  551. if sym.typ=paravarsym then
  552. usedef:=tparavarsym(sym).paraloc[calleeside].def
  553. else
  554. usedef:=sym.vardef;
  555. gen_alloc_regloc(list,sym.initialloc,usedef);
  556. end;
  557. if (pi_has_label in current_procinfo.flags) then
  558. begin
  559. { Allocate register already, to prevent first allocation to be
  560. inside a loop }
  561. {$if defined(cpu64bitalu)}
  562. if sym.initialloc.size in [OS_128,OS_S128] then
  563. begin
  564. cg.a_reg_sync(list,sym.initialloc.register128.reglo);
  565. cg.a_reg_sync(list,sym.initialloc.register128.reghi);
  566. end
  567. else
  568. {$elseif defined(cpu32bitalu) 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,sym.initialloc.register64.reghi);
  573. end
  574. else
  575. {$elseif defined(cpu16bitalu) and not defined(cpuhighleveltarget)}
  576. if sym.initialloc.size in [OS_64,OS_S64] then
  577. begin
  578. cg.a_reg_sync(list,sym.initialloc.register64.reglo);
  579. cg.a_reg_sync(list,cg.GetNextReg(sym.initialloc.register64.reglo));
  580. cg.a_reg_sync(list,sym.initialloc.register64.reghi);
  581. cg.a_reg_sync(list,cg.GetNextReg(sym.initialloc.register64.reghi));
  582. end
  583. else
  584. if sym.initialloc.size in [OS_32,OS_S32] then
  585. begin
  586. cg.a_reg_sync(list,sym.initialloc.register);
  587. cg.a_reg_sync(list,cg.GetNextReg(sym.initialloc.register));
  588. end
  589. else
  590. {$elseif defined(cpu8bitalu) and not defined(cpuhighleveltarget)}
  591. if sym.initialloc.size in [OS_64,OS_S64] then
  592. begin
  593. cg.a_reg_sync(list,sym.initialloc.register64.reglo);
  594. cg.a_reg_sync(list,cg.GetNextReg(sym.initialloc.register64.reglo));
  595. cg.a_reg_sync(list,cg.GetNextReg(cg.GetNextReg(sym.initialloc.register64.reglo)));
  596. cg.a_reg_sync(list,cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(sym.initialloc.register64.reglo))));
  597. cg.a_reg_sync(list,sym.initialloc.register64.reghi);
  598. cg.a_reg_sync(list,cg.GetNextReg(sym.initialloc.register64.reghi));
  599. cg.a_reg_sync(list,cg.GetNextReg(cg.GetNextReg(sym.initialloc.register64.reghi)));
  600. cg.a_reg_sync(list,cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(sym.initialloc.register64.reghi))));
  601. end
  602. else
  603. if sym.initialloc.size in [OS_32,OS_S32] then
  604. begin
  605. cg.a_reg_sync(list,sym.initialloc.register);
  606. cg.a_reg_sync(list,cg.GetNextReg(sym.initialloc.register));
  607. cg.a_reg_sync(list,cg.GetNextReg(cg.GetNextReg(sym.initialloc.register)));
  608. cg.a_reg_sync(list,cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(sym.initialloc.register))));
  609. end
  610. else
  611. if sym.initialloc.size in [OS_16,OS_S16] then
  612. begin
  613. cg.a_reg_sync(list,sym.initialloc.register);
  614. cg.a_reg_sync(list,cg.GetNextReg(sym.initialloc.register));
  615. end
  616. else
  617. {$endif}
  618. cg.a_reg_sync(list,sym.initialloc.register);
  619. end;
  620. {$if defined(cpu64bitalu)}
  621. if (sym.initialloc.size in [OS_128,OS_S128]) then
  622. varloc:=tai_varloc.create128(sym,sym.initialloc.register,sym.initialloc.registerhi)
  623. else
  624. {$elseif not defined(cpuhighleveltarget)}
  625. if (sym.initialloc.size in [OS_64,OS_S64]) then
  626. varloc:=tai_varloc.create64(sym,sym.initialloc.register,sym.initialloc.registerhi)
  627. else
  628. {$endif cpu64bitalu and not cpuhighleveltarget}
  629. varloc:=tai_varloc.create(sym,sym.initialloc.register);
  630. list.concat(varloc);
  631. end;
  632. {****************************************************************************
  633. Entry/Exit
  634. ****************************************************************************}
  635. procedure alloc_proc_symbol(pd: tprocdef);
  636. var
  637. item: TCmdStrListItem;
  638. begin
  639. item:=TCmdStrListItem(pd.aliasnames.first);
  640. while assigned(item) do
  641. begin
  642. current_asmdata.DefineProcAsmSymbol(pd,item.str,pd.needsglobalasmsym);
  643. item:=TCmdStrListItem(item.next);
  644. end;
  645. end;
  646. procedure release_proc_symbol(pd:tprocdef);
  647. var
  648. idx : longint;
  649. item : TCmdStrListItem;
  650. begin
  651. item:=TCmdStrListItem(pd.aliasnames.first);
  652. while assigned(item) do
  653. begin
  654. idx:=current_asmdata.AsmSymbolDict.findindexof(item.str);
  655. if idx>=0 then
  656. current_asmdata.AsmSymbolDict.Delete(idx);
  657. item:=TCmdStrListItem(item.next);
  658. end;
  659. end;
  660. procedure gen_proc_entry_code(list:TAsmList);
  661. var
  662. hitemp,
  663. lotemp, stack_frame_size : longint;
  664. begin
  665. { generate call frame marker for dwarf call frame info }
  666. current_asmdata.asmcfi.start_frame(list);
  667. { labels etc. for exception frames are inserted here }
  668. current_procinfo.start_eh(list);
  669. if current_procinfo.procdef.proctypeoption=potype_proginit then
  670. current_asmdata.asmcfi.outmost_frame(list);
  671. { All temps are know, write offsets used for information }
  672. if (cs_asm_source in current_settings.globalswitches) and
  673. (current_procinfo.tempstart<>tg.lasttemp) then
  674. begin
  675. if tg.direction>0 then
  676. begin
  677. lotemp:=current_procinfo.tempstart;
  678. hitemp:=tg.lasttemp;
  679. end
  680. else
  681. begin
  682. lotemp:=tg.lasttemp;
  683. hitemp:=current_procinfo.tempstart;
  684. end;
  685. list.concat(Tai_comment.Create(strpnew('Temps allocated between '+std_regname(current_procinfo.framepointer)+
  686. tostr_with_plus(lotemp)+' and '+std_regname(current_procinfo.framepointer)+tostr_with_plus(hitemp))));
  687. end;
  688. { generate target specific proc entry code }
  689. stack_frame_size := current_procinfo.calc_stackframe_size;
  690. if (stack_frame_size <> 0) and
  691. (po_nostackframe in current_procinfo.procdef.procoptions) then
  692. message1(parser_e_nostackframe_with_locals,tostr(stack_frame_size));
  693. hlcg.g_proc_entry(list,stack_frame_size,(po_nostackframe in current_procinfo.procdef.procoptions));
  694. end;
  695. procedure gen_proc_exit_code(list:TAsmList);
  696. var
  697. parasize : longint;
  698. begin
  699. { c style clearstack does not need to remove parameters from the stack, only the
  700. return value when it was pushed by arguments }
  701. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  702. begin
  703. parasize:=0;
  704. { For safecall functions with safecall-exceptions enabled the funcret is always returned as a para
  705. which is considered a normal para on the c-side, so the funcret has to be pop'ed normally. }
  706. if not current_procinfo.procdef.generate_safecall_wrapper and
  707. paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef) then
  708. inc(parasize,sizeof(pint));
  709. end
  710. else
  711. begin
  712. parasize:=current_procinfo.para_stack_size;
  713. { the parent frame pointer para has to be removed always by the caller in
  714. case of Delphi-style parent frame pointer passing }
  715. if (not(paramanager.use_fixed_stack) or (target_info.abi=abi_i386_dynalignedstack)) and
  716. (po_delphi_nested_cc in current_procinfo.procdef.procoptions) then
  717. dec(parasize,sizeof(pint));
  718. end;
  719. { generate target specific proc exit code }
  720. hlcg.g_proc_exit(list,parasize,(po_nostackframe in current_procinfo.procdef.procoptions));
  721. { labels etc. for exception frames are inserted here }
  722. current_procinfo.end_eh(list);
  723. { release return registers, needed for optimizer }
  724. if not is_void(current_procinfo.procdef.returndef) then
  725. paramanager.freecgpara(list,current_procinfo.procdef.funcretloc[calleeside]);
  726. { end of frame marker for call frame info }
  727. current_asmdata.asmcfi.end_frame(list);
  728. end;
  729. procedure gen_save_used_regs(list:TAsmList);
  730. begin
  731. { Pure assembler routines need to save the registers themselves }
  732. if (po_assembler in current_procinfo.procdef.procoptions) then
  733. exit;
  734. cg.g_save_registers(list);
  735. end;
  736. procedure gen_restore_used_regs(list:TAsmList);
  737. begin
  738. { Pure assembler routines need to save the registers themselves }
  739. if (po_assembler in current_procinfo.procdef.procoptions) then
  740. exit;
  741. cg.g_restore_registers(list);
  742. end;
  743. {****************************************************************************
  744. Const Data
  745. ****************************************************************************}
  746. procedure gen_alloc_symtable(list:TAsmList;pd:tprocdef;st:TSymtable);
  747. var
  748. i : longint;
  749. highsym,
  750. sym : tsym;
  751. vs : tabstractnormalvarsym;
  752. ptrdef : tdef;
  753. isaddr : boolean;
  754. begin
  755. for i:=0 to st.SymList.Count-1 do
  756. begin
  757. sym:=tsym(st.SymList[i]);
  758. case sym.typ of
  759. staticvarsym :
  760. begin
  761. vs:=tabstractnormalvarsym(sym);
  762. { The code in loadnode.pass_generatecode will create the
  763. LOC_REFERENCE instead for all none register variables. This is
  764. required because we can't store an asmsymbol in the localloc because
  765. the asmsymbol is invalid after an unit is compiled. This gives
  766. problems when this procedure is inlined in another unit (PFV) }
  767. if vs.is_regvar(false) then
  768. begin
  769. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable];
  770. vs.initialloc.size:=def_cgsize(vs.vardef);
  771. gen_alloc_regvar(list,vs,true);
  772. hlcg.varsym_set_localloc(list,vs);
  773. end;
  774. end;
  775. paravarsym :
  776. begin
  777. vs:=tabstractnormalvarsym(sym);
  778. { Parameters passed to assembler procedures need to be kept
  779. in the original location }
  780. if (po_assembler in pd.procoptions) then
  781. tparavarsym(vs).paraloc[calleeside].get_location(vs.initialloc)
  782. { exception filters receive their frame pointer as a parameter }
  783. else if (pd.proctypeoption=potype_exceptfilter) and
  784. (vo_is_parentfp in vs.varoptions) then
  785. begin
  786. location_reset(vs.initialloc,LOC_REGISTER,OS_ADDR);
  787. vs.initialloc.register:=NR_FRAME_POINTER_REG;
  788. end
  789. else
  790. begin
  791. { if an open array is used, also its high parameter is used,
  792. since the hidden high parameters are inserted after the corresponding symbols,
  793. we can increase the ref. count here }
  794. if is_open_array(vs.vardef) or is_array_of_const(vs.vardef) then
  795. begin
  796. highsym:=get_high_value_sym(tparavarsym(vs));
  797. if assigned(highsym) then
  798. inc(highsym.refs);
  799. end;
  800. isaddr:=paramanager.push_addr_param(vs.varspez,vs.vardef,pd.proccalloption);
  801. if isaddr then
  802. vs.initialloc.size:=def_cgsize(voidpointertype)
  803. else
  804. vs.initialloc.size:=def_cgsize(vs.vardef);
  805. if vs.is_regvar(isaddr) then
  806. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable]
  807. else
  808. begin
  809. vs.initialloc.loc:=LOC_REFERENCE;
  810. { Reuse the parameter location for values to are at a single location on the stack }
  811. if paramanager.param_use_paraloc(tparavarsym(vs).paraloc[calleeside]) then
  812. begin
  813. hlcg.paravarsym_set_initialloc_to_paraloc(tparavarsym(vs));
  814. end
  815. else
  816. begin
  817. if isaddr then
  818. begin
  819. ptrdef:=cpointerdef.getreusable(vs.vardef);
  820. tg.GetLocal(list,ptrdef.size,ptrdef,vs.initialloc.reference)
  821. end
  822. else
  823. tg.GetLocal(list,vs.getsize,tparavarsym(vs).paraloc[calleeside].alignment,vs.vardef,vs.initialloc.reference);
  824. end;
  825. end;
  826. end;
  827. hlcg.varsym_set_localloc(list,vs);
  828. end;
  829. localvarsym :
  830. begin
  831. vs:=tabstractnormalvarsym(sym);
  832. if is_vector(vs.vardef) and
  833. fits_in_mm_register(vs.vardef) then
  834. vs.initialloc.size:=def_cgmmsize(vs.vardef)
  835. else
  836. vs.initialloc.size:=def_cgsize(vs.vardef);
  837. if ([po_assembler,po_nostackframe] * pd.procoptions = [po_assembler,po_nostackframe]) and
  838. (vo_is_funcret in vs.varoptions) then
  839. begin
  840. paramanager.create_funcretloc_info(pd,calleeside);
  841. if assigned(pd.funcretloc[calleeside].location^.next) then
  842. begin
  843. { can't replace references to "result" with a complex
  844. location expression inside assembler code }
  845. location_reset(vs.initialloc,LOC_INVALID,OS_NO);
  846. end
  847. else
  848. pd.funcretloc[calleeside].get_location(vs.initialloc);
  849. end
  850. else if (m_delphi in current_settings.modeswitches) and
  851. (po_assembler in pd.procoptions) and
  852. (vo_is_funcret in vs.varoptions) and
  853. (vs.refs=0) then
  854. begin
  855. { not referenced, so don't allocate. Use dummy to }
  856. { avoid ie's later on because of LOC_INVALID }
  857. vs.initialloc.loc:=LOC_REGISTER;
  858. vs.initialloc.size:=OS_INT;
  859. vs.initialloc.register:=NR_FUNCTION_RESULT_REG;
  860. end
  861. else if vs.is_regvar(false) then
  862. begin
  863. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable];
  864. gen_alloc_regvar(list,vs,true);
  865. end
  866. else
  867. begin
  868. vs.initialloc.loc:=LOC_REFERENCE;
  869. tg.GetLocal(list,vs.getsize,vs.vardef,vs.initialloc.reference);
  870. end;
  871. hlcg.varsym_set_localloc(list,vs);
  872. end;
  873. else
  874. ;
  875. end;
  876. end;
  877. end;
  878. procedure add_regvars(var rv: tusedregvars; const location: tlocation);
  879. begin
  880. case location.loc of
  881. LOC_CREGISTER:
  882. {$if defined(cpu64bitalu)}
  883. if location.size in [OS_128,OS_S128] then
  884. begin
  885. rv.intregvars.addnodup(getsupreg(location.register128.reglo));
  886. rv.intregvars.addnodup(getsupreg(location.register128.reghi));
  887. end
  888. else
  889. {$elseif defined(cpu32bitalu)}
  890. if location.size in [OS_64,OS_S64] then
  891. begin
  892. rv.intregvars.addnodup(getsupreg(location.register64.reglo));
  893. rv.intregvars.addnodup(getsupreg(location.register64.reghi));
  894. end
  895. else
  896. {$elseif defined(cpu16bitalu)}
  897. if location.size in [OS_64,OS_S64] then
  898. begin
  899. rv.intregvars.addnodup(getsupreg(location.register64.reglo));
  900. rv.intregvars.addnodup(getsupreg(cg.GetNextReg(location.register64.reglo)));
  901. rv.intregvars.addnodup(getsupreg(location.register64.reghi));
  902. rv.intregvars.addnodup(getsupreg(cg.GetNextReg(location.register64.reghi)));
  903. end
  904. else
  905. if location.size in [OS_32,OS_S32] then
  906. begin
  907. rv.intregvars.addnodup(getsupreg(location.register));
  908. rv.intregvars.addnodup(getsupreg(cg.GetNextReg(location.register)));
  909. end
  910. else
  911. {$elseif defined(cpu8bitalu)}
  912. if location.size in [OS_64,OS_S64] then
  913. begin
  914. rv.intregvars.addnodup(getsupreg(location.register64.reglo));
  915. rv.intregvars.addnodup(getsupreg(cg.GetNextReg(location.register64.reglo)));
  916. rv.intregvars.addnodup(getsupreg(cg.GetNextReg(cg.GetNextReg(location.register64.reglo))));
  917. rv.intregvars.addnodup(getsupreg(cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(location.register64.reglo)))));
  918. rv.intregvars.addnodup(getsupreg(location.register64.reghi));
  919. rv.intregvars.addnodup(getsupreg(cg.GetNextReg(location.register64.reghi)));
  920. rv.intregvars.addnodup(getsupreg(cg.GetNextReg(cg.GetNextReg(location.register64.reghi))));
  921. rv.intregvars.addnodup(getsupreg(cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(location.register64.reghi)))));
  922. end
  923. else
  924. if location.size in [OS_32,OS_S32] then
  925. begin
  926. rv.intregvars.addnodup(getsupreg(location.register));
  927. rv.intregvars.addnodup(getsupreg(cg.GetNextReg(location.register)));
  928. rv.intregvars.addnodup(getsupreg(cg.GetNextReg(cg.GetNextReg(location.register))));
  929. rv.intregvars.addnodup(getsupreg(cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(location.register)))));
  930. end
  931. else
  932. if location.size in [OS_16,OS_S16] then
  933. begin
  934. rv.intregvars.addnodup(getsupreg(location.register));
  935. rv.intregvars.addnodup(getsupreg(cg.GetNextReg(location.register)));
  936. end
  937. else
  938. {$endif}
  939. if getregtype(location.register)=R_INTREGISTER then
  940. rv.intregvars.addnodup(getsupreg(location.register))
  941. else
  942. rv.addrregvars.addnodup(getsupreg(location.register));
  943. LOC_CFPUREGISTER:
  944. rv.fpuregvars.addnodup(getsupreg(location.register));
  945. LOC_CMMREGISTER:
  946. rv.mmregvars.addnodup(getsupreg(location.register));
  947. else
  948. ;
  949. end;
  950. end;
  951. function do_get_used_regvars(var n: tnode; arg: pointer): foreachnoderesult;
  952. var
  953. rv: pusedregvars absolute arg;
  954. begin
  955. case (n.nodetype) of
  956. temprefn:
  957. { We only have to synchronise a tempnode before a loop if it is }
  958. { not created inside the loop, and only synchronise after the }
  959. { loop if it's not destroyed inside the loop. If it's created }
  960. { before the loop and not yet destroyed, then before the loop }
  961. { is secondpassed tempinfo^.valid will be true, and we get the }
  962. { correct registers. If it's not destroyed inside the loop, }
  963. { then after the loop has been secondpassed tempinfo^.valid }
  964. { be true and we also get the right registers. In other cases, }
  965. { tempinfo^.valid will be false and so we do not add }
  966. { unnecessary registers. This way, we don't have to look at }
  967. { tempcreate and tempdestroy nodes to get this info (JM) }
  968. if (ti_valid in ttemprefnode(n).tempflags) then
  969. add_regvars(rv^,ttemprefnode(n).tempinfo^.location);
  970. loadn:
  971. if (tloadnode(n).symtableentry.typ in [staticvarsym,localvarsym,paravarsym]) then
  972. add_regvars(rv^,tabstractnormalvarsym(tloadnode(n).symtableentry).localloc);
  973. vecn:
  974. begin
  975. { range checks sometimes need the high parameter }
  976. if (cs_check_range in current_settings.localswitches) and
  977. (is_open_array(tvecnode(n).left.resultdef) or
  978. is_array_of_const(tvecnode(n).left.resultdef)) and
  979. not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  980. add_regvars(rv^,tabstractnormalvarsym(get_high_value_sym(tparavarsym(tloadnode(tvecnode(n).left).symtableentry))).localloc)
  981. end;
  982. else
  983. ;
  984. end;
  985. result := fen_true;
  986. end;
  987. procedure get_used_regvars(n: tnode; var rv: tusedregvars);
  988. begin
  989. foreachnodestatic(n,@do_get_used_regvars,@rv);
  990. end;
  991. (*
  992. See comments at declaration of pusedregvarscommon
  993. function do_get_used_regvars_common(var n: tnode; arg: pointer): foreachnoderesult;
  994. var
  995. rv: pusedregvarscommon absolute arg;
  996. begin
  997. if (n.nodetype = loadn) and
  998. (tloadnode(n).symtableentry.typ in [staticvarsym,localvarsym,paravarsym]) then
  999. with tabstractnormalvarsym(tloadnode(n).symtableentry).localloc do
  1000. case loc of
  1001. LOC_CREGISTER:
  1002. { if not yet encountered in this node tree }
  1003. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  1004. { but nevertheless already encountered somewhere }
  1005. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  1006. { then it's a regvar used in two or more node trees }
  1007. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  1008. LOC_CFPUREGISTER:
  1009. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  1010. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  1011. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  1012. LOC_CMMREGISTER:
  1013. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  1014. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  1015. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  1016. end;
  1017. result := fen_true;
  1018. end;
  1019. procedure get_used_regvars_common(n: tnode; var rv: tusedregvarscommon);
  1020. begin
  1021. rv.myregvars.intregvars.clear;
  1022. rv.myregvars.fpuregvars.clear;
  1023. rv.myregvars.mmregvars.clear;
  1024. foreachnodestatic(n,@do_get_used_regvars_common,@rv);
  1025. end;
  1026. *)
  1027. procedure gen_sync_regvars(list:TAsmList; var rv: tusedregvars);
  1028. var
  1029. count: longint;
  1030. begin
  1031. for count := 1 to rv.intregvars.length do
  1032. cg.a_reg_sync(list,newreg(R_INTREGISTER,rv.intregvars.readidx(count-1),R_SUBWHOLE));
  1033. for count := 1 to rv.addrregvars.length do
  1034. cg.a_reg_sync(list,newreg(R_ADDRESSREGISTER,rv.addrregvars.readidx(count-1),R_SUBWHOLE));
  1035. for count := 1 to rv.fpuregvars.length do
  1036. cg.a_reg_sync(list,newreg(R_FPUREGISTER,rv.fpuregvars.readidx(count-1),R_SUBWHOLE));
  1037. for count := 1 to rv.mmregvars.length do
  1038. cg.a_reg_sync(list,newreg(R_MMREGISTER,rv.mmregvars.readidx(count-1),R_SUBWHOLE));
  1039. end;
  1040. procedure gen_free_symtable(list:TAsmList;st:TSymtable);
  1041. var
  1042. i : longint;
  1043. sym : tsym;
  1044. begin
  1045. for i:=0 to st.SymList.Count-1 do
  1046. begin
  1047. sym:=tsym(st.SymList[i]);
  1048. if (sym.typ in [staticvarsym,localvarsym,paravarsym]) then
  1049. begin
  1050. with tabstractnormalvarsym(sym) do
  1051. begin
  1052. { Note: We need to keep the data available in memory
  1053. for the sub procedures that can access local data
  1054. in the parent procedures }
  1055. case localloc.loc of
  1056. LOC_CREGISTER :
  1057. if (pi_has_label in current_procinfo.flags) then
  1058. {$if defined(cpu64bitalu)}
  1059. if def_cgsize(vardef) in [OS_128,OS_S128] then
  1060. begin
  1061. cg.a_reg_sync(list,localloc.register128.reglo);
  1062. cg.a_reg_sync(list,localloc.register128.reghi);
  1063. end
  1064. else
  1065. {$elseif defined(cpu32bitalu)}
  1066. if def_cgsize(vardef) in [OS_64,OS_S64] then
  1067. begin
  1068. cg.a_reg_sync(list,localloc.register64.reglo);
  1069. cg.a_reg_sync(list,localloc.register64.reghi);
  1070. end
  1071. else
  1072. {$elseif defined(cpu16bitalu)}
  1073. if def_cgsize(vardef) in [OS_64,OS_S64] then
  1074. begin
  1075. cg.a_reg_sync(list,localloc.register64.reglo);
  1076. cg.a_reg_sync(list,cg.GetNextReg(localloc.register64.reglo));
  1077. cg.a_reg_sync(list,localloc.register64.reghi);
  1078. cg.a_reg_sync(list,cg.GetNextReg(localloc.register64.reghi));
  1079. end
  1080. else
  1081. if def_cgsize(vardef) in [OS_32,OS_S32] then
  1082. begin
  1083. cg.a_reg_sync(list,localloc.register);
  1084. cg.a_reg_sync(list,cg.GetNextReg(localloc.register));
  1085. end
  1086. else
  1087. {$elseif defined(cpu8bitalu)}
  1088. if def_cgsize(vardef) in [OS_64,OS_S64] then
  1089. begin
  1090. cg.a_reg_sync(list,localloc.register64.reglo);
  1091. cg.a_reg_sync(list,cg.GetNextReg(localloc.register64.reglo));
  1092. cg.a_reg_sync(list,cg.GetNextReg(cg.GetNextReg(localloc.register64.reglo)));
  1093. cg.a_reg_sync(list,cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(localloc.register64.reglo))));
  1094. cg.a_reg_sync(list,localloc.register64.reghi);
  1095. cg.a_reg_sync(list,cg.GetNextReg(localloc.register64.reghi));
  1096. cg.a_reg_sync(list,cg.GetNextReg(cg.GetNextReg(localloc.register64.reghi)));
  1097. cg.a_reg_sync(list,cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(localloc.register64.reghi))));
  1098. end
  1099. else
  1100. if def_cgsize(vardef) in [OS_32,OS_S32] then
  1101. begin
  1102. cg.a_reg_sync(list,localloc.register);
  1103. cg.a_reg_sync(list,cg.GetNextReg(localloc.register));
  1104. cg.a_reg_sync(list,cg.GetNextReg(cg.GetNextReg(localloc.register)));
  1105. cg.a_reg_sync(list,cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(localloc.register))));
  1106. end
  1107. else
  1108. if def_cgsize(vardef) in [OS_16,OS_S16] then
  1109. begin
  1110. cg.a_reg_sync(list,localloc.register);
  1111. cg.a_reg_sync(list,cg.GetNextReg(localloc.register));
  1112. end
  1113. else
  1114. {$endif}
  1115. cg.a_reg_sync(list,localloc.register);
  1116. LOC_CFPUREGISTER,
  1117. LOC_CMMREGISTER,
  1118. LOC_CMMXREGISTER:
  1119. if (pi_has_label in current_procinfo.flags) then
  1120. cg.a_reg_sync(list,localloc.register);
  1121. LOC_REFERENCE :
  1122. begin
  1123. { can't free the result, because we load it after
  1124. this call into the function result location
  1125. (gets freed in thlcgobj.gen_load_return_value();) }
  1126. if (typ in [localvarsym,paravarsym]) and
  1127. (([vo_is_funcret,vo_is_result]*varoptions)=[]) and
  1128. ((current_procinfo.procdef.proctypeoption<>potype_constructor) or
  1129. not(vo_is_self in varoptions)) then
  1130. tg.Ungetlocal(list,localloc.reference);
  1131. end;
  1132. { function results in pure assembler routines }
  1133. LOC_REGISTER,
  1134. LOC_FPUREGISTER,
  1135. LOC_MMREGISTER,
  1136. { empty parameter }
  1137. LOC_VOID,
  1138. { global variables in memory and typed constants don't get a location assigned,
  1139. and neither does an unused $result variable in pure assembler routines }
  1140. LOC_INVALID:
  1141. ;
  1142. else
  1143. internalerror(2019050538);
  1144. end;
  1145. end;
  1146. end;
  1147. end;
  1148. end;
  1149. function getprocalign : shortint;
  1150. begin
  1151. { gprof uses 16 byte granularity }
  1152. if (cs_profile in current_settings.moduleswitches) then
  1153. result:=16
  1154. else
  1155. result:=current_settings.alignment.procalign;
  1156. end;
  1157. procedure gen_load_frame_for_exceptfilter(list : TAsmList);
  1158. var
  1159. para: tparavarsym;
  1160. begin
  1161. para:=tparavarsym(current_procinfo.procdef.paras[0]);
  1162. if not (vo_is_parentfp in para.varoptions) then
  1163. InternalError(201201142);
  1164. if (para.paraloc[calleeside].location^.loc<>LOC_REGISTER) or
  1165. (para.paraloc[calleeside].location^.next<>nil) then
  1166. InternalError(201201143);
  1167. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,para.paraloc[calleeside].location^.register,
  1168. NR_FRAME_POINTER_REG);
  1169. end;
  1170. end.