ncgutil.pas 54 KB

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