2
0

ncgutil.pas 57 KB

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