ncgutil.pas 54 KB

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