ncgutil.pas 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993
  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,cpuinfo,
  22. globtype,
  23. cpubase,cgbase,parabase,cgutils,
  24. aasmbase,aasmtai,aasmdata,aasmcpu,
  25. symconst,symbase,symdef,symsym,symtype,symtable
  26. {$ifndef cpu64bitalu}
  27. ,cg64f32
  28. {$endif not cpu64bitalu}
  29. ;
  30. type
  31. tloadregvars = (lr_dont_load_regvars, lr_load_regvars);
  32. pusedregvars = ^tusedregvars;
  33. tusedregvars = record
  34. intregvars, 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 maketojumpbool(list:TAsmList; p : tnode; loadregvars: tloadregvars);
  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. { loads a cgpara into a tlocation; assumes that loc.loc is already
  52. initialised }
  53. procedure gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);
  54. { allocate registers for a tlocation; assumes that loc.loc is already
  55. set to LOC_CREGISTER/LOC_CFPUREGISTER/... }
  56. procedure gen_alloc_regloc(list:TAsmList;var loc: tlocation);
  57. procedure register_maybe_adjust_setbase(list: TAsmList; var l: tlocation; setbase: aint);
  58. function has_alias_name(pd:tprocdef;const s:string):boolean;
  59. procedure alloc_proc_symbol(pd: tprocdef);
  60. procedure gen_proc_entry_code(list:TAsmList);
  61. procedure gen_proc_exit_code(list:TAsmList);
  62. procedure gen_stack_check_size_para(list:TAsmList);
  63. procedure gen_stack_check_call(list:TAsmList);
  64. procedure gen_save_used_regs(list:TAsmList);
  65. procedure gen_restore_used_regs(list:TAsmList);
  66. procedure gen_load_para_value(list:TAsmList);
  67. procedure gen_external_stub(list:TAsmList;pd:tprocdef;const externalname:string);
  68. procedure gen_load_vmt_register(list:TAsmList;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  69. procedure get_used_regvars(n: tnode; var rv: tusedregvars);
  70. { adds the regvars used in n and its children to rv.allregvars,
  71. those which were already in rv.allregvars to rv.commonregvars and
  72. uses rv.myregvars as scratch (so that two uses of the same regvar
  73. in a single tree to make it appear in commonregvars). Useful to
  74. find out which regvars are used in two different node trees
  75. e.g. in the "else" and "then" path, or in various case blocks }
  76. // procedure get_used_regvars_common(n: tnode; var rv: tusedregvarscommon);
  77. procedure gen_sync_regvars(list:TAsmList; var rv: tusedregvars);
  78. { Allocate the buffers for exception management and setjmp environment.
  79. Return a pointer to these buffers, send them to the utility routine
  80. so they are registered, and then call setjmp.
  81. Then compare the result of setjmp with 0, and if not equal
  82. to zero, then jump to exceptlabel.
  83. Also store the result of setjmp to a temporary space by calling g_save_exception_reason
  84. It is to note that this routine may be called *after* the stackframe of a
  85. routine has been called, therefore on machines where the stack cannot
  86. be modified, all temps should be allocated on the heap instead of the
  87. stack. }
  88. const
  89. EXCEPT_BUF_SIZE = 3*sizeof(pint);
  90. type
  91. texceptiontemps=record
  92. jmpbuf,
  93. envbuf,
  94. reasonbuf : treference;
  95. end;
  96. procedure get_exception_temps(list:TAsmList;var t:texceptiontemps);
  97. procedure unget_exception_temps(list:TAsmList;const t:texceptiontemps);
  98. procedure new_exception(list:TAsmList;const t:texceptiontemps;exceptlabel:tasmlabel);
  99. procedure free_exception(list:TAsmList;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  100. procedure gen_alloc_symtable(list:TAsmList;pd:tprocdef;st:TSymtable);
  101. procedure gen_free_symtable(list:TAsmList;st:TSymtable);
  102. procedure location_free(list: TAsmList; const location : TLocation);
  103. function getprocalign : shortint;
  104. procedure gen_fpc_dummy(list : TAsmList);
  105. procedure gen_load_frame_for_exceptfilter(list : TAsmList);
  106. implementation
  107. uses
  108. version,
  109. cutils,cclasses,
  110. globals,systems,verbose,export,
  111. ppu,defutil,
  112. procinfo,paramgr,fmodule,
  113. regvars,dbgbase,
  114. pass_1,pass_2,
  115. nbas,ncon,nld,nmem,nutils,ngenutil,
  116. tgobj,cgobj,cgcpu,hlcgobj,hlcgcpu
  117. {$ifdef powerpc}
  118. , cpupi
  119. {$endif}
  120. {$ifdef powerpc64}
  121. , cpupi
  122. {$endif}
  123. {$ifdef SUPPORT_MMX}
  124. , cgx86
  125. {$endif SUPPORT_MMX}
  126. ;
  127. {*****************************************************************************
  128. Misc Helpers
  129. *****************************************************************************}
  130. {$if first_mm_imreg = 0}
  131. {$WARN 4044 OFF} { Comparison might be always false ... }
  132. {$endif}
  133. procedure location_free(list: TAsmList; const location : TLocation);
  134. begin
  135. case location.loc of
  136. LOC_VOID:
  137. ;
  138. LOC_REGISTER,
  139. LOC_CREGISTER:
  140. begin
  141. {$ifdef cpu64bitalu}
  142. { x86-64 system v abi:
  143. structs with up to 16 bytes are returned in registers }
  144. if location.size in [OS_128,OS_S128] then
  145. begin
  146. if getsupreg(location.register)<first_int_imreg then
  147. cg.ungetcpuregister(list,location.register);
  148. if getsupreg(location.registerhi)<first_int_imreg then
  149. cg.ungetcpuregister(list,location.registerhi);
  150. end
  151. {$else cpu64bitalu}
  152. if location.size in [OS_64,OS_S64] then
  153. begin
  154. if getsupreg(location.register64.reglo)<first_int_imreg then
  155. cg.ungetcpuregister(list,location.register64.reglo);
  156. if getsupreg(location.register64.reghi)<first_int_imreg then
  157. cg.ungetcpuregister(list,location.register64.reghi);
  158. end
  159. {$endif cpu64bitalu}
  160. else
  161. if getsupreg(location.register)<first_int_imreg then
  162. cg.ungetcpuregister(list,location.register);
  163. end;
  164. LOC_FPUREGISTER,
  165. LOC_CFPUREGISTER:
  166. begin
  167. if getsupreg(location.register)<first_fpu_imreg then
  168. cg.ungetcpuregister(list,location.register);
  169. end;
  170. LOC_MMREGISTER,
  171. LOC_CMMREGISTER :
  172. begin
  173. if getsupreg(location.register)<first_mm_imreg then
  174. cg.ungetcpuregister(list,location.register);
  175. end;
  176. LOC_REFERENCE,
  177. LOC_CREFERENCE :
  178. begin
  179. if paramanager.use_fixed_stack then
  180. location_freetemp(list,location);
  181. end;
  182. else
  183. internalerror(2004110211);
  184. end;
  185. end;
  186. procedure firstcomplex(p : tbinarynode);
  187. var
  188. fcl, fcr: longint;
  189. ncl, ncr: longint;
  190. begin
  191. { always calculate boolean AND and OR from left to right }
  192. if (p.nodetype in [orn,andn]) and
  193. is_boolean(p.left.resultdef) then
  194. begin
  195. if nf_swapped in p.flags then
  196. internalerror(200709253);
  197. end
  198. else
  199. begin
  200. fcl:=node_resources_fpu(p.left);
  201. fcr:=node_resources_fpu(p.right);
  202. ncl:=node_complexity(p.left);
  203. ncr:=node_complexity(p.right);
  204. { We swap left and right if
  205. a) right needs more floating point registers than left, and
  206. left needs more than 0 floating point registers (if it
  207. doesn't need any, swapping won't change the floating
  208. point register pressure)
  209. b) both left and right need an equal amount of floating
  210. point registers or right needs no floating point registers,
  211. and in addition right has a higher complexity than left
  212. (+- needs more integer registers, but not necessarily)
  213. }
  214. if ((fcr>fcl) and
  215. (fcl>0)) or
  216. (((fcr=fcl) or
  217. (fcr=0)) and
  218. (ncr>ncl)) then
  219. p.swapleftright
  220. end;
  221. end;
  222. procedure maketojumpbool(list:TAsmList; p : tnode; loadregvars: tloadregvars);
  223. {
  224. produces jumps to true respectively false labels using boolean expressions
  225. depending on whether the loading of regvars is currently being
  226. synchronized manually (such as in an if-node) or automatically (most of
  227. the other cases where this procedure is called), loadregvars can be
  228. "lr_load_regvars" or "lr_dont_load_regvars"
  229. }
  230. var
  231. opsize : tcgsize;
  232. storepos : tfileposinfo;
  233. tmpreg : tregister;
  234. begin
  235. if nf_error in p.flags then
  236. exit;
  237. storepos:=current_filepos;
  238. current_filepos:=p.fileinfo;
  239. if is_boolean(p.resultdef) then
  240. begin
  241. {$ifdef OLDREGVARS}
  242. if loadregvars = lr_load_regvars then
  243. load_all_regvars(list);
  244. {$endif OLDREGVARS}
  245. if is_constboolnode(p) then
  246. begin
  247. if Tordconstnode(p).value.uvalue<>0 then
  248. cg.a_jmp_always(list,current_procinfo.CurrTrueLabel)
  249. else
  250. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel)
  251. end
  252. else
  253. begin
  254. opsize:=def_cgsize(p.resultdef);
  255. case p.location.loc of
  256. LOC_SUBSETREG,LOC_CSUBSETREG,
  257. LOC_SUBSETREF,LOC_CSUBSETREF:
  258. begin
  259. tmpreg := cg.getintregister(list,OS_INT);
  260. hlcg.a_load_loc_reg(list,p.resultdef,osuinttype,p.location,tmpreg);
  261. cg.a_cmp_const_reg_label(list,OS_INT,OC_NE,0,tmpreg,current_procinfo.CurrTrueLabel);
  262. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel);
  263. end;
  264. LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE :
  265. begin
  266. {$ifdef cpu64bitalu}
  267. if opsize in [OS_128,OS_S128] then
  268. begin
  269. hlcg.location_force_reg(list,p.location,p.resultdef,cgsize_orddef(opsize),true);
  270. tmpreg:=cg.getintregister(list,OS_64);
  271. cg.a_op_reg_reg_reg(list,OP_OR,OS_64,p.location.register128.reglo,p.location.register128.reghi,tmpreg);
  272. location_reset(p.location,LOC_REGISTER,OS_64);
  273. p.location.register:=tmpreg;
  274. opsize:=OS_64;
  275. end;
  276. {$else cpu64bitalu}
  277. if opsize in [OS_64,OS_S64] then
  278. begin
  279. hlcg.location_force_reg(list,p.location,p.resultdef,cgsize_orddef(opsize),true);
  280. tmpreg:=cg.getintregister(list,OS_32);
  281. cg.a_op_reg_reg_reg(list,OP_OR,OS_32,p.location.register64.reglo,p.location.register64.reghi,tmpreg);
  282. location_reset(p.location,LOC_REGISTER,OS_32);
  283. p.location.register:=tmpreg;
  284. opsize:=OS_32;
  285. end;
  286. {$endif cpu64bitalu}
  287. cg.a_cmp_const_loc_label(list,opsize,OC_NE,0,p.location,current_procinfo.CurrTrueLabel);
  288. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel);
  289. end;
  290. LOC_JUMP:
  291. ;
  292. {$ifdef cpuflags}
  293. LOC_FLAGS :
  294. begin
  295. cg.a_jmp_flags(list,p.location.resflags,current_procinfo.CurrTrueLabel);
  296. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  297. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel);
  298. end;
  299. {$endif cpuflags}
  300. else
  301. begin
  302. printnode(output,p);
  303. internalerror(200308241);
  304. end;
  305. end;
  306. end;
  307. end
  308. else
  309. internalerror(200112305);
  310. current_filepos:=storepos;
  311. end;
  312. (*
  313. This code needs fixing. It is not safe to use rgint; on the m68000 it
  314. would be rgaddr.
  315. procedure remove_non_regvars_from_loc(const t: tlocation; var regs:Tsuperregisterset);
  316. begin
  317. case t.loc of
  318. LOC_REGISTER:
  319. begin
  320. { can't be a regvar, since it would be LOC_CREGISTER then }
  321. exclude(regs,getsupreg(t.register));
  322. if t.register64.reghi<>NR_NO then
  323. exclude(regs,getsupreg(t.register64.reghi));
  324. end;
  325. LOC_CREFERENCE,LOC_REFERENCE:
  326. begin
  327. if not(cs_opt_regvar in current_settings.optimizerswitches) or
  328. (getsupreg(t.reference.base) in cg.rgint.usableregs) then
  329. exclude(regs,getsupreg(t.reference.base));
  330. if not(cs_opt_regvar in current_settings.optimizerswitches) or
  331. (getsupreg(t.reference.index) in cg.rgint.usableregs) then
  332. exclude(regs,getsupreg(t.reference.index));
  333. end;
  334. end;
  335. end;
  336. *)
  337. {*****************************************************************************
  338. EXCEPTION MANAGEMENT
  339. *****************************************************************************}
  340. procedure get_exception_temps(list:TAsmList;var t:texceptiontemps);
  341. begin
  342. get_jumpbuf_size;
  343. tg.GetTemp(list,EXCEPT_BUF_SIZE,sizeof(pint),tt_persistent,t.envbuf);
  344. tg.GetTemp(list,jmp_buf_size,jmp_buf_align,tt_persistent,t.jmpbuf);
  345. tg.GetTemp(list,sizeof(pint),sizeof(pint),tt_persistent,t.reasonbuf);
  346. end;
  347. procedure unget_exception_temps(list:TAsmList;const t:texceptiontemps);
  348. begin
  349. tg.Ungettemp(list,t.jmpbuf);
  350. tg.ungettemp(list,t.envbuf);
  351. tg.ungettemp(list,t.reasonbuf);
  352. end;
  353. procedure new_exception(list:TAsmList;const t:texceptiontemps;exceptlabel:tasmlabel);
  354. const
  355. {$ifdef cpu16bitaddr}
  356. pushexceptaddr_frametype_cgsize = OS_S16;
  357. setjmp_result_cgsize = OS_S16;
  358. {$else cpu16bitaddr}
  359. pushexceptaddr_frametype_cgsize = OS_S32;
  360. setjmp_result_cgsize = OS_S32;
  361. {$endif cpu16bitaddr}
  362. var
  363. paraloc1,paraloc2,paraloc3 : tcgpara;
  364. pd: tprocdef;
  365. {$ifdef i8086}
  366. tmpreg: TRegister;
  367. {$endif i8086}
  368. begin
  369. pd:=search_system_proc('fpc_pushexceptaddr');
  370. paraloc1.init;
  371. paraloc2.init;
  372. paraloc3.init;
  373. paramanager.getintparaloc(pd,1,paraloc1);
  374. paramanager.getintparaloc(pd,2,paraloc2);
  375. paramanager.getintparaloc(pd,3,paraloc3);
  376. if pd.is_pushleftright then
  377. begin
  378. { push type of exceptionframe }
  379. cg.a_load_const_cgpara(list,pushexceptaddr_frametype_cgsize,1,paraloc1);
  380. cg.a_loadaddr_ref_cgpara(list,t.jmpbuf,paraloc2);
  381. cg.a_loadaddr_ref_cgpara(list,t.envbuf,paraloc3);
  382. end
  383. else
  384. begin
  385. cg.a_loadaddr_ref_cgpara(list,t.envbuf,paraloc3);
  386. cg.a_loadaddr_ref_cgpara(list,t.jmpbuf,paraloc2);
  387. { push type of exceptionframe }
  388. cg.a_load_const_cgpara(list,pushexceptaddr_frametype_cgsize,1,paraloc1);
  389. end;
  390. paramanager.freecgpara(list,paraloc3);
  391. paramanager.freecgpara(list,paraloc2);
  392. paramanager.freecgpara(list,paraloc1);
  393. cg.allocallcpuregisters(list);
  394. cg.a_call_name(list,'FPC_PUSHEXCEPTADDR',false);
  395. cg.deallocallcpuregisters(list);
  396. pd:=search_system_proc('fpc_setjmp');
  397. paramanager.getintparaloc(pd,1,paraloc1);
  398. {$ifdef i8086}
  399. if current_settings.x86memorymodel in x86_far_data_models then
  400. begin
  401. tmpreg:=cg.getintregister(list,OS_32);
  402. cg.a_load_reg_reg(list,OS_16,OS_16,NR_FUNCTION_RESULT32_LOW_REG,tmpreg);
  403. cg.a_load_reg_reg(list,OS_16,OS_16,NR_FUNCTION_RESULT32_HIGH_REG,GetNextReg(tmpreg));
  404. cg.a_load_reg_cgpara(list,OS_32,tmpreg,paraloc1);
  405. end
  406. else
  407. {$endif i8086}
  408. cg.a_load_reg_cgpara(list,OS_ADDR,NR_FUNCTION_RESULT_REG,paraloc1);
  409. paramanager.freecgpara(list,paraloc1);
  410. cg.allocallcpuregisters(list);
  411. cg.a_call_name(list,'FPC_SETJMP',false);
  412. cg.deallocallcpuregisters(list);
  413. cg.alloccpuregisters(list,R_INTREGISTER,[RS_FUNCTION_RESULT_REG]);
  414. cg.g_exception_reason_save(list, t.reasonbuf);
  415. cg.a_cmp_const_reg_label(list,setjmp_result_cgsize,OC_NE,0,cg.makeregsize(list,NR_FUNCTION_RESULT_REG,setjmp_result_cgsize),exceptlabel);
  416. cg.dealloccpuregisters(list,R_INTREGISTER,[RS_FUNCTION_RESULT_REG]);
  417. paraloc1.done;
  418. paraloc2.done;
  419. paraloc3.done;
  420. end;
  421. procedure free_exception(list:TAsmList;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  422. begin
  423. cg.allocallcpuregisters(list);
  424. cg.a_call_name(list,'FPC_POPADDRSTACK',false);
  425. cg.deallocallcpuregisters(list);
  426. if not onlyfree then
  427. begin
  428. { g_exception_reason_load already allocates NR_FUNCTION_RESULT_REG }
  429. cg.g_exception_reason_load(list, t.reasonbuf);
  430. cg.a_cmp_const_reg_label(list,OS_INT,OC_EQ,a,NR_FUNCTION_RESULT_REG,endexceptlabel);
  431. cg.a_reg_dealloc(list,NR_FUNCTION_RESULT_REG);
  432. end;
  433. end;
  434. {*****************************************************************************
  435. TLocation
  436. *****************************************************************************}
  437. procedure register_maybe_adjust_setbase(list: TAsmList; var l: tlocation; setbase: aint);
  438. var
  439. tmpreg: tregister;
  440. begin
  441. if (setbase<>0) then
  442. begin
  443. if not(l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  444. internalerror(2007091502);
  445. { subtract the setbase }
  446. case l.loc of
  447. LOC_CREGISTER:
  448. begin
  449. tmpreg := cg.getintregister(list,l.size);
  450. cg.a_op_const_reg_reg(list,OP_SUB,l.size,setbase,l.register,tmpreg);
  451. l.loc:=LOC_REGISTER;
  452. l.register:=tmpreg;
  453. end;
  454. LOC_REGISTER:
  455. begin
  456. cg.a_op_const_reg(list,OP_SUB,l.size,setbase,l.register);
  457. end;
  458. end;
  459. end;
  460. end;
  461. procedure location_force_mmreg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  462. var
  463. reg : tregister;
  464. begin
  465. if (l.loc<>LOC_MMREGISTER) and
  466. ((l.loc<>LOC_CMMREGISTER) or (not maybeconst)) then
  467. begin
  468. reg:=cg.getmmregister(list,OS_VECTOR);
  469. cg.a_loadmm_loc_reg(list,OS_VECTOR,l,reg,nil);
  470. location_freetemp(list,l);
  471. location_reset(l,LOC_MMREGISTER,OS_VECTOR);
  472. l.register:=reg;
  473. end;
  474. end;
  475. procedure location_allocate_register(list: TAsmList;out l: tlocation;def: tdef;constant: boolean);
  476. begin
  477. l.size:=def_cgsize(def);
  478. if (def.typ=floatdef) and
  479. not(cs_fp_emulation in current_settings.moduleswitches) then
  480. begin
  481. if use_vectorfpu(def) then
  482. begin
  483. if constant then
  484. location_reset(l,LOC_CMMREGISTER,l.size)
  485. else
  486. location_reset(l,LOC_MMREGISTER,l.size);
  487. l.register:=cg.getmmregister(list,l.size);
  488. end
  489. else
  490. begin
  491. if constant then
  492. location_reset(l,LOC_CFPUREGISTER,l.size)
  493. else
  494. location_reset(l,LOC_FPUREGISTER,l.size);
  495. l.register:=cg.getfpuregister(list,l.size);
  496. end;
  497. end
  498. else
  499. begin
  500. if constant then
  501. location_reset(l,LOC_CREGISTER,l.size)
  502. else
  503. location_reset(l,LOC_REGISTER,l.size);
  504. {$ifdef cpu64bitalu}
  505. if l.size in [OS_128,OS_S128,OS_F128] then
  506. begin
  507. l.register128.reglo:=cg.getintregister(list,OS_64);
  508. l.register128.reghi:=cg.getintregister(list,OS_64);
  509. end
  510. else
  511. {$else cpu64bitalu}
  512. if l.size in [OS_64,OS_S64,OS_F64] then
  513. begin
  514. l.register64.reglo:=cg.getintregister(list,OS_32);
  515. l.register64.reghi:=cg.getintregister(list,OS_32);
  516. end
  517. else
  518. {$endif cpu64bitalu}
  519. { Note: for widths of records (and maybe objects, classes, etc.) an
  520. address register could be set here, but that is later
  521. changed to an intregister neverthless when in the
  522. tcgassignmentnode thlcgobj.maybe_change_load_node_reg is
  523. called for the temporary node; so the workaround for now is
  524. to fix the symptoms... }
  525. l.register:=cg.getintregister(list,l.size);
  526. end;
  527. end;
  528. {****************************************************************************
  529. Init/Finalize Code
  530. ****************************************************************************}
  531. procedure copyvalueparas(p:TObject;arg:pointer);
  532. var
  533. href : treference;
  534. hreg : tregister;
  535. list : TAsmList;
  536. hsym : tparavarsym;
  537. l : longint;
  538. localcopyloc : tlocation;
  539. sizedef : tdef;
  540. begin
  541. list:=TAsmList(arg);
  542. if (tsym(p).typ=paravarsym) and
  543. (tparavarsym(p).varspez=vs_value) and
  544. (paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  545. begin
  546. { we have no idea about the alignment at the caller side }
  547. hlcg.location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  548. if is_open_array(tparavarsym(p).vardef) or
  549. is_array_of_const(tparavarsym(p).vardef) then
  550. begin
  551. { cdecl functions don't have a high pointer so it is not possible to generate
  552. a local copy }
  553. if not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  554. begin
  555. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  556. if not assigned(hsym) then
  557. internalerror(200306061);
  558. hreg:=cg.getaddressregister(list);
  559. if not is_packed_array(tparavarsym(p).vardef) then
  560. hlcg.g_copyvaluepara_openarray(list,href,hsym.initialloc,tarraydef(tparavarsym(p).vardef),hreg)
  561. else
  562. internalerror(2006080401);
  563. // cg.g_copyvaluepara_packedopenarray(list,href,hsym.intialloc,tarraydef(tparavarsym(p).vardef).elepackedbitsize,hreg);
  564. sizedef:=getpointerdef(tparavarsym(p).vardef);
  565. hlcg.a_load_reg_loc(list,sizedef,sizedef,hreg,tparavarsym(p).initialloc);
  566. end;
  567. end
  568. else
  569. begin
  570. { Allocate space for the local copy }
  571. l:=tparavarsym(p).getsize;
  572. localcopyloc.loc:=LOC_REFERENCE;
  573. localcopyloc.size:=int_cgsize(l);
  574. tg.GetLocal(list,l,tparavarsym(p).vardef,localcopyloc.reference);
  575. { Copy data }
  576. if is_shortstring(tparavarsym(p).vardef) then
  577. begin
  578. { this code is only executed before the code for the body and the entry/exit code is generated
  579. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  580. }
  581. include(current_procinfo.flags,pi_do_call);
  582. hlcg.g_copyshortstring(list,href,localcopyloc.reference,tstringdef(tparavarsym(p).vardef));
  583. end
  584. else if tparavarsym(p).vardef.typ = variantdef then
  585. begin
  586. { this code is only executed before the code for the body and the entry/exit code is generated
  587. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  588. }
  589. include(current_procinfo.flags,pi_do_call);
  590. hlcg.g_copyvariant(list,href,localcopyloc.reference,tvariantdef(tparavarsym(p).vardef))
  591. end
  592. else
  593. begin
  594. { pass proper alignment info }
  595. localcopyloc.reference.alignment:=tparavarsym(p).vardef.alignment;
  596. cg.g_concatcopy(list,href,localcopyloc.reference,tparavarsym(p).vardef.size);
  597. end;
  598. { update localloc of varsym }
  599. tg.Ungetlocal(list,tparavarsym(p).localloc.reference);
  600. tparavarsym(p).localloc:=localcopyloc;
  601. tparavarsym(p).initialloc:=localcopyloc;
  602. end;
  603. end;
  604. end;
  605. { generates the code for incrementing the reference count of parameters and
  606. initialize out parameters }
  607. procedure init_paras(p:TObject;arg:pointer);
  608. var
  609. href : treference;
  610. hsym : tparavarsym;
  611. eldef : tdef;
  612. list : TAsmList;
  613. needs_inittable : boolean;
  614. begin
  615. list:=TAsmList(arg);
  616. if (tsym(p).typ=paravarsym) then
  617. begin
  618. needs_inittable:=is_managed_type(tparavarsym(p).vardef);
  619. if not needs_inittable then
  620. exit;
  621. case tparavarsym(p).varspez of
  622. vs_value :
  623. begin
  624. { variants are already handled by the call to fpc_variant_copy_overwrite if
  625. they are passed by reference }
  626. if not((tparavarsym(p).vardef.typ=variantdef) and
  627. paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  628. begin
  629. hlcg.location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,is_open_array(tparavarsym(p).vardef),sizeof(pint));
  630. if is_open_array(tparavarsym(p).vardef) then
  631. begin
  632. { open arrays do not contain correct element count in their rtti,
  633. the actual count must be passed separately. }
  634. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  635. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  636. if not assigned(hsym) then
  637. internalerror(201003031);
  638. hlcg.g_array_rtti_helper(list,eldef,href,hsym.initialloc,'fpc_addref_array');
  639. end
  640. else
  641. hlcg.g_incrrefcount(list,tparavarsym(p).vardef,href);
  642. end;
  643. end;
  644. vs_out :
  645. begin
  646. { we have no idea about the alignment at the callee side,
  647. and the user also cannot specify "unaligned" here, so
  648. assume worst case }
  649. hlcg.location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  650. if is_open_array(tparavarsym(p).vardef) then
  651. begin
  652. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  653. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  654. if not assigned(hsym) then
  655. internalerror(201103033);
  656. hlcg.g_array_rtti_helper(list,eldef,href,hsym.initialloc,'fpc_initialize_array');
  657. end
  658. else
  659. hlcg.g_initialize(list,tparavarsym(p).vardef,href);
  660. end;
  661. end;
  662. end;
  663. end;
  664. procedure gen_alloc_regloc(list:TAsmList;var loc: tlocation);
  665. begin
  666. case loc.loc of
  667. LOC_CREGISTER:
  668. begin
  669. {$ifdef cpu64bitalu}
  670. if loc.size in [OS_128,OS_S128] then
  671. begin
  672. loc.register128.reglo:=cg.getintregister(list,OS_64);
  673. loc.register128.reghi:=cg.getintregister(list,OS_64);
  674. end
  675. else
  676. {$else cpu64bitalu}
  677. if loc.size in [OS_64,OS_S64] then
  678. begin
  679. loc.register64.reglo:=cg.getintregister(list,OS_32);
  680. loc.register64.reghi:=cg.getintregister(list,OS_32);
  681. end
  682. else
  683. {$endif cpu64bitalu}
  684. loc.register:=cg.getintregister(list,loc.size);
  685. end;
  686. LOC_CFPUREGISTER:
  687. begin
  688. loc.register:=cg.getfpuregister(list,loc.size);
  689. end;
  690. LOC_CMMREGISTER:
  691. begin
  692. loc.register:=cg.getmmregister(list,loc.size);
  693. end;
  694. end;
  695. end;
  696. procedure gen_alloc_regvar(list:TAsmList;sym: tabstractnormalvarsym; allocreg: boolean);
  697. begin
  698. if allocreg then
  699. gen_alloc_regloc(list,sym.initialloc);
  700. if (pi_has_label in current_procinfo.flags) then
  701. begin
  702. { Allocate register already, to prevent first allocation to be
  703. inside a loop }
  704. {$if defined(cpu64bitalu)}
  705. if sym.initialloc.size in [OS_128,OS_S128] then
  706. begin
  707. cg.a_reg_sync(list,sym.initialloc.register128.reglo);
  708. cg.a_reg_sync(list,sym.initialloc.register128.reghi);
  709. end
  710. else
  711. {$elseif defined(cpu32bitalu)}
  712. if sym.initialloc.size in [OS_64,OS_S64] then
  713. begin
  714. cg.a_reg_sync(list,sym.initialloc.register64.reglo);
  715. cg.a_reg_sync(list,sym.initialloc.register64.reghi);
  716. end
  717. else
  718. {$elseif defined(cpu16bitalu)}
  719. if sym.initialloc.size in [OS_64,OS_S64] then
  720. begin
  721. cg.a_reg_sync(list,sym.initialloc.register64.reglo);
  722. cg.a_reg_sync(list,GetNextReg(sym.initialloc.register64.reglo));
  723. cg.a_reg_sync(list,sym.initialloc.register64.reghi);
  724. cg.a_reg_sync(list,GetNextReg(sym.initialloc.register64.reghi));
  725. end
  726. else
  727. if sym.initialloc.size in [OS_32,OS_S32] then
  728. begin
  729. cg.a_reg_sync(list,sym.initialloc.register);
  730. cg.a_reg_sync(list,GetNextReg(sym.initialloc.register));
  731. end
  732. else
  733. {$elseif defined(cpu8bitalu)}
  734. if sym.initialloc.size in [OS_64,OS_S64] then
  735. begin
  736. cg.a_reg_sync(list,sym.initialloc.register64.reglo);
  737. cg.a_reg_sync(list,GetNextReg(sym.initialloc.register64.reglo));
  738. cg.a_reg_sync(list,GetNextReg(GetNextReg(sym.initialloc.register64.reglo)));
  739. cg.a_reg_sync(list,GetNextReg(GetNextReg(GetNextReg(sym.initialloc.register64.reglo))));
  740. cg.a_reg_sync(list,sym.initialloc.register64.reghi);
  741. cg.a_reg_sync(list,GetNextReg(sym.initialloc.register64.reghi));
  742. cg.a_reg_sync(list,GetNextReg(GetNextReg(sym.initialloc.register64.reghi)));
  743. cg.a_reg_sync(list,GetNextReg(GetNextReg(GetNextReg(sym.initialloc.register64.reghi))));
  744. end
  745. else
  746. if sym.initialloc.size in [OS_32,OS_S32] then
  747. begin
  748. cg.a_reg_sync(list,sym.initialloc.register);
  749. cg.a_reg_sync(list,GetNextReg(sym.initialloc.register));
  750. cg.a_reg_sync(list,GetNextReg(GetNextReg(sym.initialloc.register)));
  751. cg.a_reg_sync(list,GetNextReg(GetNextReg(GetNextReg(sym.initialloc.register))));
  752. end
  753. else
  754. if sym.initialloc.size in [OS_16,OS_S16] then
  755. begin
  756. cg.a_reg_sync(list,sym.initialloc.register);
  757. cg.a_reg_sync(list,GetNextReg(sym.initialloc.register));
  758. end
  759. else
  760. {$endif}
  761. cg.a_reg_sync(list,sym.initialloc.register);
  762. end;
  763. sym.localloc:=sym.initialloc;
  764. end;
  765. procedure gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);
  766. procedure unget_para(const paraloc:TCGParaLocation);
  767. begin
  768. case paraloc.loc of
  769. LOC_REGISTER :
  770. begin
  771. if getsupreg(paraloc.register)<first_int_imreg then
  772. cg.ungetcpuregister(list,paraloc.register);
  773. end;
  774. LOC_MMREGISTER :
  775. begin
  776. if getsupreg(paraloc.register)<first_mm_imreg then
  777. cg.ungetcpuregister(list,paraloc.register);
  778. end;
  779. LOC_FPUREGISTER :
  780. begin
  781. if getsupreg(paraloc.register)<first_fpu_imreg then
  782. cg.ungetcpuregister(list,paraloc.register);
  783. end;
  784. end;
  785. end;
  786. var
  787. paraloc : pcgparalocation;
  788. href : treference;
  789. sizeleft : aint;
  790. {$if defined(sparc) or defined(arm) or defined(mips)}
  791. tempref : treference;
  792. {$endif defined(sparc) or defined(arm) or defined(mips)}
  793. {$ifdef mips}
  794. tmpreg : tregister;
  795. {$endif mips}
  796. {$ifndef cpu64bitalu}
  797. tempreg : tregister;
  798. reg64 : tregister64;
  799. {$endif not cpu64bitalu}
  800. begin
  801. paraloc:=para.location;
  802. if not assigned(paraloc) then
  803. internalerror(200408203);
  804. { skip e.g. empty records }
  805. if (paraloc^.loc = LOC_VOID) then
  806. exit;
  807. case destloc.loc of
  808. LOC_REFERENCE :
  809. begin
  810. { If the parameter location is reused we don't need to copy
  811. anything }
  812. if not reusepara then
  813. begin
  814. href:=destloc.reference;
  815. sizeleft:=para.intsize;
  816. while assigned(paraloc) do
  817. begin
  818. if (paraloc^.size=OS_NO) then
  819. begin
  820. { Can only be a reference that contains the rest
  821. of the parameter }
  822. if (paraloc^.loc<>LOC_REFERENCE) or
  823. assigned(paraloc^.next) then
  824. internalerror(2005013010);
  825. cg.a_load_cgparaloc_ref(list,paraloc^,href,sizeleft,destloc.reference.alignment);
  826. inc(href.offset,sizeleft);
  827. sizeleft:=0;
  828. end
  829. else
  830. begin
  831. cg.a_load_cgparaloc_ref(list,paraloc^,href,tcgsize2size[paraloc^.size],destloc.reference.alignment);
  832. inc(href.offset,TCGSize2Size[paraloc^.size]);
  833. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  834. end;
  835. unget_para(paraloc^);
  836. paraloc:=paraloc^.next;
  837. end;
  838. end;
  839. end;
  840. LOC_REGISTER,
  841. LOC_CREGISTER :
  842. begin
  843. {$ifdef cpu64bitalu}
  844. if (para.size in [OS_128,OS_S128,OS_F128]) and
  845. ({ in case of fpu emulation, or abi's that pass fpu values
  846. via integer registers }
  847. (vardef.typ=floatdef) or
  848. is_methodpointer(vardef) or
  849. is_record(vardef)) then
  850. begin
  851. case paraloc^.loc of
  852. LOC_REGISTER:
  853. begin
  854. if not assigned(paraloc^.next) then
  855. internalerror(200410104);
  856. if (target_info.endian=ENDIAN_BIG) then
  857. begin
  858. { paraloc^ -> high
  859. paraloc^.next -> low }
  860. unget_para(paraloc^);
  861. gen_alloc_regloc(list,destloc);
  862. { reg->reg, alignment is irrelevant }
  863. cg.a_load_cgparaloc_anyreg(list,OS_64,paraloc^,destloc.register128.reghi,8);
  864. unget_para(paraloc^.next^);
  865. cg.a_load_cgparaloc_anyreg(list,OS_64,paraloc^.next^,destloc.register128.reglo,8);
  866. end
  867. else
  868. begin
  869. { paraloc^ -> low
  870. paraloc^.next -> high }
  871. unget_para(paraloc^);
  872. gen_alloc_regloc(list,destloc);
  873. cg.a_load_cgparaloc_anyreg(list,OS_64,paraloc^,destloc.register128.reglo,8);
  874. unget_para(paraloc^.next^);
  875. cg.a_load_cgparaloc_anyreg(list,OS_64,paraloc^.next^,destloc.register128.reghi,8);
  876. end;
  877. end;
  878. LOC_REFERENCE:
  879. begin
  880. gen_alloc_regloc(list,destloc);
  881. reference_reset_base(href,paraloc^.reference.index,paraloc^.reference.offset,para.alignment);
  882. cg128.a_load128_ref_reg(list,href,destloc.register128);
  883. unget_para(paraloc^);
  884. end;
  885. else
  886. internalerror(2012090607);
  887. end
  888. end
  889. else
  890. {$else cpu64bitalu}
  891. if (para.size in [OS_64,OS_S64,OS_F64]) and
  892. (is_64bit(vardef) or
  893. { in case of fpu emulation, or abi's that pass fpu values
  894. via integer registers }
  895. (vardef.typ=floatdef) or
  896. is_methodpointer(vardef) or
  897. is_record(vardef)) then
  898. begin
  899. case paraloc^.loc of
  900. LOC_REGISTER:
  901. begin
  902. case para.locations_count of
  903. {$if defined(cpu16bitalu) or defined(cpu8bitalu)}
  904. { 4 paralocs? }
  905. 4:
  906. if (target_info.endian=ENDIAN_BIG) then
  907. begin
  908. { paraloc^ -> high
  909. paraloc^.next^.next -> low }
  910. unget_para(paraloc^);
  911. gen_alloc_regloc(list,destloc);
  912. { reg->reg, alignment is irrelevant }
  913. cg.a_load_cgparaloc_anyreg(list,OS_16,paraloc^,GetNextReg(destloc.register64.reghi),2);
  914. unget_para(paraloc^.next^);
  915. cg.a_load_cgparaloc_anyreg(list,OS_16,paraloc^.next^,destloc.register64.reghi,2);
  916. unget_para(paraloc^.next^.next^);
  917. cg.a_load_cgparaloc_anyreg(list,OS_16,paraloc^.next^.next^,GetNextReg(destloc.register64.reglo),2);
  918. unget_para(paraloc^.next^.next^.next^);
  919. cg.a_load_cgparaloc_anyreg(list,OS_16,paraloc^.next^.next^.next^,destloc.register64.reglo,2);
  920. end
  921. else
  922. begin
  923. { paraloc^ -> low
  924. paraloc^.next^.next -> high }
  925. unget_para(paraloc^);
  926. gen_alloc_regloc(list,destloc);
  927. cg.a_load_cgparaloc_anyreg(list,OS_16,paraloc^,destloc.register64.reglo,2);
  928. unget_para(paraloc^.next^);
  929. cg.a_load_cgparaloc_anyreg(list,OS_16,paraloc^.next^,GetNextReg(destloc.register64.reglo),2);
  930. unget_para(paraloc^.next^.next^);
  931. cg.a_load_cgparaloc_anyreg(list,OS_16,paraloc^.next^.next^,destloc.register64.reghi,2);
  932. unget_para(paraloc^.next^.next^.next^);
  933. cg.a_load_cgparaloc_anyreg(list,OS_16,paraloc^.next^.next^.next^,GetNextReg(destloc.register64.reghi),2);
  934. end;
  935. {$endif defined(cpu16bitalu) or defined(cpu8bitalu)}
  936. 2:
  937. if (target_info.endian=ENDIAN_BIG) then
  938. begin
  939. { paraloc^ -> high
  940. paraloc^.next -> low }
  941. unget_para(paraloc^);
  942. gen_alloc_regloc(list,destloc);
  943. { reg->reg, alignment is irrelevant }
  944. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^,destloc.register64.reghi,4);
  945. unget_para(paraloc^.next^);
  946. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^.next^,destloc.register64.reglo,4);
  947. end
  948. else
  949. begin
  950. { paraloc^ -> low
  951. paraloc^.next -> high }
  952. unget_para(paraloc^);
  953. gen_alloc_regloc(list,destloc);
  954. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^,destloc.register64.reglo,4);
  955. unget_para(paraloc^.next^);
  956. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^.next^,destloc.register64.reghi,4);
  957. end;
  958. else
  959. { unexpected number of paralocs }
  960. internalerror(200410104);
  961. end;
  962. end;
  963. LOC_REFERENCE:
  964. begin
  965. gen_alloc_regloc(list,destloc);
  966. reference_reset_base(href,paraloc^.reference.index,paraloc^.reference.offset,para.alignment);
  967. cg64.a_load64_ref_reg(list,href,destloc.register64);
  968. unget_para(paraloc^);
  969. end;
  970. else
  971. internalerror(2005101501);
  972. end
  973. end
  974. else
  975. {$endif cpu64bitalu}
  976. begin
  977. if assigned(paraloc^.next) then
  978. begin
  979. if (destloc.size in [OS_PAIR,OS_SPAIR]) and
  980. (para.Size in [OS_PAIR,OS_SPAIR]) then
  981. begin
  982. unget_para(paraloc^);
  983. gen_alloc_regloc(list,destloc);
  984. cg.a_load_cgparaloc_anyreg(list,OS_INT,paraloc^,destloc.register,sizeof(aint));
  985. unget_para(paraloc^.Next^);
  986. {$if defined(cpu16bitalu) or defined(cpu8bitalu)}
  987. cg.a_load_cgparaloc_anyreg(list,OS_INT,paraloc^.Next^,GetNextReg(destloc.register),sizeof(aint));
  988. {$else}
  989. cg.a_load_cgparaloc_anyreg(list,OS_INT,paraloc^.Next^,destloc.registerhi,sizeof(aint));
  990. {$endif}
  991. end
  992. {$if defined(cpu8bitalu)}
  993. else if (destloc.size in [OS_32,OS_S32]) and
  994. (para.Size in [OS_32,OS_S32]) then
  995. begin
  996. unget_para(paraloc^);
  997. gen_alloc_regloc(list,destloc);
  998. cg.a_load_cgparaloc_anyreg(list,OS_INT,paraloc^,destloc.register,sizeof(aint));
  999. unget_para(paraloc^.Next^);
  1000. cg.a_load_cgparaloc_anyreg(list,OS_INT,paraloc^.Next^,GetNextReg(destloc.register),sizeof(aint));
  1001. unget_para(paraloc^.Next^.Next^);
  1002. cg.a_load_cgparaloc_anyreg(list,OS_INT,paraloc^.Next^.Next^,GetNextReg(GetNextReg(destloc.register)),sizeof(aint));
  1003. unget_para(paraloc^.Next^.Next^.Next^);
  1004. cg.a_load_cgparaloc_anyreg(list,OS_INT,paraloc^.Next^.Next^.Next^,GetNextReg(GetNextReg(GetNextReg(destloc.register))),sizeof(aint));
  1005. end
  1006. {$endif defined(cpu8bitalu)}
  1007. else
  1008. internalerror(200410105);
  1009. end
  1010. else
  1011. begin
  1012. unget_para(paraloc^);
  1013. gen_alloc_regloc(list,destloc);
  1014. cg.a_load_cgparaloc_anyreg(list,destloc.size,paraloc^,destloc.register,sizeof(aint));
  1015. end;
  1016. end;
  1017. end;
  1018. LOC_FPUREGISTER,
  1019. LOC_CFPUREGISTER :
  1020. begin
  1021. {$ifdef mips}
  1022. if (destloc.size = paraloc^.Size) and
  1023. (paraloc^.Loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER,LOC_REFERENCE,LOC_CREFERENCE]) then
  1024. begin
  1025. unget_para(paraloc^);
  1026. gen_alloc_regloc(list,destloc);
  1027. cg.a_load_cgparaloc_anyreg(list,destloc.size,paraloc^,destloc.register,para.alignment);
  1028. end
  1029. else if (destloc.size = OS_F32) and
  1030. (paraloc^.Loc in [LOC_REGISTER,LOC_CREGISTER]) then
  1031. begin
  1032. gen_alloc_regloc(list,destloc);
  1033. unget_para(paraloc^);
  1034. list.Concat(taicpu.op_reg_reg(A_MTC1,paraloc^.register,destloc.register));
  1035. end
  1036. { TODO: Produces invalid code, needs fixing together with regalloc setup. }
  1037. {
  1038. else if (destloc.size = OS_F64) and
  1039. (paraloc^.Loc in [LOC_REGISTER,LOC_CREGISTER]) and
  1040. (paraloc^.next^.Loc in [LOC_REGISTER,LOC_CREGISTER]) then
  1041. begin
  1042. gen_alloc_regloc(list,destloc);
  1043. tmpreg:=destloc.register;
  1044. unget_para(paraloc^);
  1045. list.Concat(taicpu.op_reg_reg(A_MTC1,paraloc^.register,tmpreg));
  1046. setsupreg(tmpreg,getsupreg(tmpreg)+1);
  1047. unget_para(paraloc^.next^);
  1048. list.Concat(taicpu.op_reg_reg(A_MTC1,paraloc^.Next^.register,tmpreg));
  1049. end
  1050. }
  1051. else
  1052. begin
  1053. sizeleft := TCGSize2Size[destloc.size];
  1054. tg.GetTemp(list,sizeleft,sizeleft,tt_normal,tempref);
  1055. href:=tempref;
  1056. while assigned(paraloc) do
  1057. begin
  1058. unget_para(paraloc^);
  1059. cg.a_load_cgparaloc_ref(list,paraloc^,href,sizeleft,destloc.reference.alignment);
  1060. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1061. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1062. paraloc:=paraloc^.next;
  1063. end;
  1064. gen_alloc_regloc(list,destloc);
  1065. cg.a_loadfpu_ref_reg(list,destloc.size,destloc.size,tempref,destloc.register);
  1066. tg.UnGetTemp(list,tempref);
  1067. end;
  1068. {$else mips}
  1069. {$if defined(sparc) or defined(arm)}
  1070. { Arm and Sparc passes floats in int registers, when loading to fpu register
  1071. we need a temp }
  1072. sizeleft := TCGSize2Size[destloc.size];
  1073. tg.GetTemp(list,sizeleft,sizeleft,tt_normal,tempref);
  1074. href:=tempref;
  1075. while assigned(paraloc) do
  1076. begin
  1077. unget_para(paraloc^);
  1078. cg.a_load_cgparaloc_ref(list,paraloc^,href,sizeleft,destloc.reference.alignment);
  1079. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1080. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1081. paraloc:=paraloc^.next;
  1082. end;
  1083. gen_alloc_regloc(list,destloc);
  1084. cg.a_loadfpu_ref_reg(list,destloc.size,destloc.size,tempref,destloc.register);
  1085. tg.UnGetTemp(list,tempref);
  1086. {$else defined(sparc) or defined(arm)}
  1087. unget_para(paraloc^);
  1088. gen_alloc_regloc(list,destloc);
  1089. { from register to register -> alignment is irrelevant }
  1090. cg.a_load_cgparaloc_anyreg(list,destloc.size,paraloc^,destloc.register,0);
  1091. if assigned(paraloc^.next) then
  1092. internalerror(200410109);
  1093. {$endif defined(sparc) or defined(arm)}
  1094. {$endif mips}
  1095. end;
  1096. LOC_MMREGISTER,
  1097. LOC_CMMREGISTER :
  1098. begin
  1099. {$ifndef cpu64bitalu}
  1100. { ARM vfp floats are passed in integer registers }
  1101. if (para.size=OS_F64) and
  1102. (paraloc^.size in [OS_32,OS_S32]) and
  1103. use_vectorfpu(vardef) then
  1104. begin
  1105. { we need 2x32bit reg }
  1106. if not assigned(paraloc^.next) or
  1107. assigned(paraloc^.next^.next) then
  1108. internalerror(2009112421);
  1109. unget_para(paraloc^.next^);
  1110. case paraloc^.next^.loc of
  1111. LOC_REGISTER:
  1112. tempreg:=paraloc^.next^.register;
  1113. LOC_REFERENCE:
  1114. begin
  1115. tempreg:=cg.getintregister(list,OS_32);
  1116. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^.next^,tempreg,4);
  1117. end;
  1118. else
  1119. internalerror(2012051301);
  1120. end;
  1121. { don't free before the above, because then the getintregister
  1122. could reallocate this register and overwrite it }
  1123. unget_para(paraloc^);
  1124. gen_alloc_regloc(list,destloc);
  1125. if (target_info.endian=endian_big) then
  1126. { paraloc^ -> high
  1127. paraloc^.next -> low }
  1128. reg64:=joinreg64(tempreg,paraloc^.register)
  1129. else
  1130. reg64:=joinreg64(paraloc^.register,tempreg);
  1131. cg64.a_loadmm_intreg64_reg(list,OS_F64,reg64,destloc.register);
  1132. end
  1133. else
  1134. {$endif not cpu64bitalu}
  1135. begin
  1136. unget_para(paraloc^);
  1137. gen_alloc_regloc(list,destloc);
  1138. { from register to register -> alignment is irrelevant }
  1139. cg.a_load_cgparaloc_anyreg(list,destloc.size,paraloc^,destloc.register,0);
  1140. { data could come in two memory locations, for now
  1141. we simply ignore the sanity check (FK)
  1142. if assigned(paraloc^.next) then
  1143. internalerror(200410108);
  1144. }
  1145. end;
  1146. end;
  1147. else
  1148. internalerror(2010052903);
  1149. end;
  1150. end;
  1151. procedure gen_load_para_value(list:TAsmList);
  1152. procedure get_para(const paraloc:TCGParaLocation);
  1153. begin
  1154. case paraloc.loc of
  1155. LOC_REGISTER :
  1156. begin
  1157. if getsupreg(paraloc.register)<first_int_imreg then
  1158. cg.getcpuregister(list,paraloc.register);
  1159. end;
  1160. LOC_MMREGISTER :
  1161. begin
  1162. if getsupreg(paraloc.register)<first_mm_imreg then
  1163. cg.getcpuregister(list,paraloc.register);
  1164. end;
  1165. LOC_FPUREGISTER :
  1166. begin
  1167. if getsupreg(paraloc.register)<first_fpu_imreg then
  1168. cg.getcpuregister(list,paraloc.register);
  1169. end;
  1170. end;
  1171. end;
  1172. var
  1173. i : longint;
  1174. currpara : tparavarsym;
  1175. paraloc : pcgparalocation;
  1176. begin
  1177. if (po_assembler in current_procinfo.procdef.procoptions) or
  1178. { exceptfilters have a single hidden 'parentfp' parameter, which
  1179. is handled by tcg.g_proc_entry. }
  1180. (current_procinfo.procdef.proctypeoption=potype_exceptfilter) then
  1181. exit;
  1182. { Allocate registers used by parameters }
  1183. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1184. begin
  1185. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1186. paraloc:=currpara.paraloc[calleeside].location;
  1187. while assigned(paraloc) do
  1188. begin
  1189. if paraloc^.loc in [LOC_REGISTER,LOC_FPUREGISTER,LOC_MMREGISTER] then
  1190. get_para(paraloc^);
  1191. paraloc:=paraloc^.next;
  1192. end;
  1193. end;
  1194. { Copy parameters to local references/registers }
  1195. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1196. begin
  1197. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1198. gen_load_cgpara_loc(list,currpara.vardef,currpara.paraloc[calleeside],currpara.initialloc,paramanager.param_use_paraloc(currpara.paraloc[calleeside]));
  1199. { gen_load_cgpara_loc() already allocated the initialloc
  1200. -> don't allocate again }
  1201. if currpara.initialloc.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMREGISTER] then
  1202. gen_alloc_regvar(list,currpara,false);
  1203. end;
  1204. { generate copies of call by value parameters, must be done before
  1205. the initialization and body is parsed because the refcounts are
  1206. incremented using the local copies }
  1207. current_procinfo.procdef.parast.SymList.ForEachCall(@copyvalueparas,list);
  1208. {$ifdef powerpc}
  1209. { unget the register that contains the stack pointer before the procedure entry, }
  1210. { which is used to access the parameters in their original callee-side location }
  1211. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1212. cg.a_reg_dealloc(list,NR_R12);
  1213. {$endif powerpc}
  1214. {$ifdef powerpc64}
  1215. { unget the register that contains the stack pointer before the procedure entry, }
  1216. { which is used to access the parameters in their original callee-side location }
  1217. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1218. cg.a_reg_dealloc(list, NR_OLD_STACK_POINTER_REG);
  1219. {$endif powerpc64}
  1220. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1221. begin
  1222. { initialize refcounted paras, and trash others. Needed here
  1223. instead of in gen_initialize_code, because when a reference is
  1224. intialised or trashed while the pointer to that reference is kept
  1225. in a regvar, we add a register move and that one again has to
  1226. come after the parameter loading code as far as the register
  1227. allocator is concerned }
  1228. current_procinfo.procdef.parast.SymList.ForEachCall(@init_paras,list);
  1229. end;
  1230. end;
  1231. {****************************************************************************
  1232. Entry/Exit
  1233. ****************************************************************************}
  1234. function has_alias_name(pd:tprocdef;const s:string):boolean;
  1235. var
  1236. item : TCmdStrListItem;
  1237. begin
  1238. result:=true;
  1239. if pd.mangledname=s then
  1240. exit;
  1241. item := TCmdStrListItem(pd.aliasnames.first);
  1242. while assigned(item) do
  1243. begin
  1244. if item.str=s then
  1245. exit;
  1246. item := TCmdStrListItem(item.next);
  1247. end;
  1248. result:=false;
  1249. end;
  1250. procedure alloc_proc_symbol(pd: tprocdef);
  1251. var
  1252. item : TCmdStrListItem;
  1253. begin
  1254. item := TCmdStrListItem(pd.aliasnames.first);
  1255. while assigned(item) do
  1256. begin
  1257. current_asmdata.DefineAsmSymbol(item.str,AB_GLOBAL,AT_FUNCTION);
  1258. item := TCmdStrListItem(item.next);
  1259. end;
  1260. end;
  1261. procedure gen_proc_entry_code(list:TAsmList);
  1262. var
  1263. hitemp,
  1264. lotemp, stack_frame_size : longint;
  1265. begin
  1266. { generate call frame marker for dwarf call frame info }
  1267. current_asmdata.asmcfi.start_frame(list);
  1268. { All temps are know, write offsets used for information }
  1269. if (cs_asm_source in current_settings.globalswitches) and
  1270. (current_procinfo.tempstart<>tg.lasttemp) then
  1271. begin
  1272. if tg.direction>0 then
  1273. begin
  1274. lotemp:=current_procinfo.tempstart;
  1275. hitemp:=tg.lasttemp;
  1276. end
  1277. else
  1278. begin
  1279. lotemp:=tg.lasttemp;
  1280. hitemp:=current_procinfo.tempstart;
  1281. end;
  1282. list.concat(Tai_comment.Create(strpnew('Temps allocated between '+std_regname(current_procinfo.framepointer)+
  1283. tostr_with_plus(lotemp)+' and '+std_regname(current_procinfo.framepointer)+tostr_with_plus(hitemp))));
  1284. end;
  1285. { generate target specific proc entry code }
  1286. stack_frame_size := current_procinfo.calc_stackframe_size;
  1287. if (stack_frame_size <> 0) and
  1288. (po_nostackframe in current_procinfo.procdef.procoptions) then
  1289. message1(parser_e_nostackframe_with_locals,tostr(stack_frame_size));
  1290. hlcg.g_proc_entry(list,stack_frame_size,(po_nostackframe in current_procinfo.procdef.procoptions));
  1291. end;
  1292. procedure gen_proc_exit_code(list:TAsmList);
  1293. var
  1294. parasize : longint;
  1295. begin
  1296. { c style clearstack does not need to remove parameters from the stack, only the
  1297. return value when it was pushed by arguments }
  1298. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1299. begin
  1300. parasize:=0;
  1301. if paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef) then
  1302. inc(parasize,sizeof(pint));
  1303. end
  1304. else
  1305. begin
  1306. parasize:=current_procinfo.para_stack_size;
  1307. { the parent frame pointer para has to be removed by the caller in
  1308. case of Delphi-style parent frame pointer passing }
  1309. if not paramanager.use_fixed_stack and
  1310. (po_delphi_nested_cc in current_procinfo.procdef.procoptions) then
  1311. dec(parasize,sizeof(pint));
  1312. end;
  1313. { generate target specific proc exit code }
  1314. hlcg.g_proc_exit(list,parasize,(po_nostackframe in current_procinfo.procdef.procoptions));
  1315. { release return registers, needed for optimizer }
  1316. if not is_void(current_procinfo.procdef.returndef) then
  1317. paramanager.freecgpara(list,current_procinfo.procdef.funcretloc[calleeside]);
  1318. { end of frame marker for call frame info }
  1319. current_asmdata.asmcfi.end_frame(list);
  1320. end;
  1321. procedure gen_stack_check_size_para(list:TAsmList);
  1322. var
  1323. paraloc1 : tcgpara;
  1324. pd : tprocdef;
  1325. begin
  1326. pd:=search_system_proc('fpc_stackcheck');
  1327. paraloc1.init;
  1328. paramanager.getintparaloc(pd,1,paraloc1);
  1329. cg.a_load_const_cgpara(list,OS_INT,current_procinfo.calc_stackframe_size,paraloc1);
  1330. paramanager.freecgpara(list,paraloc1);
  1331. paraloc1.done;
  1332. end;
  1333. procedure gen_stack_check_call(list:TAsmList);
  1334. var
  1335. paraloc1 : tcgpara;
  1336. pd : tprocdef;
  1337. begin
  1338. pd:=search_system_proc('fpc_stackcheck');
  1339. paraloc1.init;
  1340. { Also alloc the register needed for the parameter }
  1341. paramanager.getintparaloc(pd,1,paraloc1);
  1342. paramanager.freecgpara(list,paraloc1);
  1343. { Call the helper }
  1344. cg.allocallcpuregisters(list);
  1345. cg.a_call_name(list,'FPC_STACKCHECK',false);
  1346. cg.deallocallcpuregisters(list);
  1347. paraloc1.done;
  1348. end;
  1349. procedure gen_save_used_regs(list:TAsmList);
  1350. begin
  1351. { Pure assembler routines need to save the registers themselves }
  1352. if (po_assembler in current_procinfo.procdef.procoptions) then
  1353. exit;
  1354. { oldfpccall expects all registers to be destroyed }
  1355. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1356. cg.g_save_registers(list);
  1357. end;
  1358. procedure gen_restore_used_regs(list:TAsmList);
  1359. begin
  1360. { Pure assembler routines need to save the registers themselves }
  1361. if (po_assembler in current_procinfo.procdef.procoptions) then
  1362. exit;
  1363. { oldfpccall expects all registers to be destroyed }
  1364. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1365. cg.g_restore_registers(list);
  1366. end;
  1367. {****************************************************************************
  1368. External handling
  1369. ****************************************************************************}
  1370. procedure gen_external_stub(list:TAsmList;pd:tprocdef;const externalname:string);
  1371. begin
  1372. create_hlcodegen;
  1373. { add the procedure to the al_procedures }
  1374. maybe_new_object_file(list);
  1375. new_section(list,sec_code,lower(pd.mangledname),current_settings.alignment.procalign);
  1376. list.concat(Tai_align.create(current_settings.alignment.procalign));
  1377. if (po_global in pd.procoptions) then
  1378. list.concat(Tai_symbol.createname_global(pd.mangledname,AT_FUNCTION,0))
  1379. else
  1380. list.concat(Tai_symbol.createname(pd.mangledname,AT_FUNCTION,0));
  1381. cg.g_external_wrapper(list,pd,externalname);
  1382. destroy_hlcodegen;
  1383. end;
  1384. {****************************************************************************
  1385. Const Data
  1386. ****************************************************************************}
  1387. procedure gen_alloc_symtable(list:TAsmList;pd:tprocdef;st:TSymtable);
  1388. procedure setlocalloc(vs:tabstractnormalvarsym);
  1389. begin
  1390. if cs_asm_source in current_settings.globalswitches then
  1391. begin
  1392. case vs.initialloc.loc of
  1393. LOC_REFERENCE :
  1394. begin
  1395. if not assigned(vs.initialloc.reference.symbol) then
  1396. list.concat(Tai_comment.Create(strpnew('Var '+vs.realname+' located at '+
  1397. std_regname(vs.initialloc.reference.base)+tostr_with_plus(vs.initialloc.reference.offset))));
  1398. end;
  1399. end;
  1400. end;
  1401. vs.localloc:=vs.initialloc;
  1402. FillChar(vs.currentregloc,sizeof(vs.currentregloc),0);
  1403. end;
  1404. var
  1405. i : longint;
  1406. sym : tsym;
  1407. vs : tabstractnormalvarsym;
  1408. isaddr : boolean;
  1409. begin
  1410. for i:=0 to st.SymList.Count-1 do
  1411. begin
  1412. sym:=tsym(st.SymList[i]);
  1413. case sym.typ of
  1414. staticvarsym :
  1415. begin
  1416. vs:=tabstractnormalvarsym(sym);
  1417. { The code in loadnode.pass_generatecode will create the
  1418. LOC_REFERENCE instead for all none register variables. This is
  1419. required because we can't store an asmsymbol in the localloc because
  1420. the asmsymbol is invalid after an unit is compiled. This gives
  1421. problems when this procedure is inlined in another unit (PFV) }
  1422. if vs.is_regvar(false) then
  1423. begin
  1424. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable];
  1425. vs.initialloc.size:=def_cgsize(vs.vardef);
  1426. gen_alloc_regvar(list,vs,true);
  1427. setlocalloc(vs);
  1428. end;
  1429. end;
  1430. paravarsym :
  1431. begin
  1432. vs:=tabstractnormalvarsym(sym);
  1433. { Parameters passed to assembler procedures need to be kept
  1434. in the original location }
  1435. if (po_assembler in pd.procoptions) then
  1436. tparavarsym(vs).paraloc[calleeside].get_location(vs.initialloc)
  1437. { exception filters receive their frame pointer as a parameter }
  1438. else if (pd.proctypeoption=potype_exceptfilter) and
  1439. (vo_is_parentfp in vs.varoptions) then
  1440. begin
  1441. location_reset(vs.initialloc,LOC_REGISTER,OS_ADDR);
  1442. vs.initialloc.register:=NR_FRAME_POINTER_REG;
  1443. end
  1444. else
  1445. begin
  1446. isaddr:=paramanager.push_addr_param(vs.varspez,vs.vardef,pd.proccalloption);
  1447. if isaddr then
  1448. vs.initialloc.size:=OS_ADDR
  1449. else
  1450. vs.initialloc.size:=def_cgsize(vs.vardef);
  1451. if vs.is_regvar(isaddr) then
  1452. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable]
  1453. else
  1454. begin
  1455. vs.initialloc.loc:=LOC_REFERENCE;
  1456. { Reuse the parameter location for values to are at a single location on the stack }
  1457. if paramanager.param_use_paraloc(tparavarsym(sym).paraloc[calleeside]) then
  1458. begin
  1459. reference_reset_base(vs.initialloc.reference,tparavarsym(sym).paraloc[calleeside].location^.reference.index,
  1460. tparavarsym(sym).paraloc[calleeside].location^.reference.offset,tparavarsym(sym).paraloc[calleeside].alignment);
  1461. end
  1462. else
  1463. begin
  1464. if isaddr then
  1465. tg.GetLocal(list,sizeof(pint),voidpointertype,vs.initialloc.reference)
  1466. else
  1467. tg.GetLocal(list,vs.getsize,tparavarsym(sym).paraloc[calleeside].alignment,vs.vardef,vs.initialloc.reference);
  1468. end;
  1469. end;
  1470. end;
  1471. setlocalloc(vs);
  1472. end;
  1473. localvarsym :
  1474. begin
  1475. vs:=tabstractnormalvarsym(sym);
  1476. vs.initialloc.size:=def_cgsize(vs.vardef);
  1477. if ([po_assembler,po_nostackframe] * pd.procoptions = [po_assembler,po_nostackframe]) and
  1478. (vo_is_funcret in vs.varoptions) then
  1479. begin
  1480. paramanager.create_funcretloc_info(pd,calleeside);
  1481. if assigned(pd.funcretloc[calleeside].location^.next) then
  1482. begin
  1483. { can't replace references to "result" with a complex
  1484. location expression inside assembler code }
  1485. location_reset(vs.initialloc,LOC_INVALID,OS_NO);
  1486. end
  1487. else
  1488. pd.funcretloc[calleeside].get_location(vs.initialloc);
  1489. end
  1490. else if (m_delphi in current_settings.modeswitches) and
  1491. (po_assembler in pd.procoptions) and
  1492. (vo_is_funcret in vs.varoptions) and
  1493. (vs.refs=0) then
  1494. begin
  1495. { not referenced, so don't allocate. Use dummy to }
  1496. { avoid ie's later on because of LOC_INVALID }
  1497. vs.initialloc.loc:=LOC_REGISTER;
  1498. vs.initialloc.size:=OS_INT;
  1499. vs.initialloc.register:=NR_FUNCTION_RESULT_REG;
  1500. end
  1501. else if vs.is_regvar(false) then
  1502. begin
  1503. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable];
  1504. gen_alloc_regvar(list,vs,true);
  1505. end
  1506. else
  1507. begin
  1508. vs.initialloc.loc:=LOC_REFERENCE;
  1509. tg.GetLocal(list,vs.getsize,vs.vardef,vs.initialloc.reference);
  1510. end;
  1511. setlocalloc(vs);
  1512. end;
  1513. end;
  1514. end;
  1515. end;
  1516. procedure add_regvars(var rv: tusedregvars; const location: tlocation);
  1517. begin
  1518. case location.loc of
  1519. LOC_CREGISTER:
  1520. {$if defined(cpu64bitalu)}
  1521. if location.size in [OS_128,OS_S128] then
  1522. begin
  1523. rv.intregvars.addnodup(getsupreg(location.register128.reglo));
  1524. rv.intregvars.addnodup(getsupreg(location.register128.reghi));
  1525. end
  1526. else
  1527. {$elseif defined(cpu32bitalu)}
  1528. if location.size in [OS_64,OS_S64] then
  1529. begin
  1530. rv.intregvars.addnodup(getsupreg(location.register64.reglo));
  1531. rv.intregvars.addnodup(getsupreg(location.register64.reghi));
  1532. end
  1533. else
  1534. {$elseif defined(cpu16bitalu)}
  1535. if location.size in [OS_64,OS_S64] then
  1536. begin
  1537. rv.intregvars.addnodup(getsupreg(location.register64.reglo));
  1538. rv.intregvars.addnodup(getsupreg(GetNextReg(location.register64.reglo)));
  1539. rv.intregvars.addnodup(getsupreg(location.register64.reghi));
  1540. rv.intregvars.addnodup(getsupreg(GetNextReg(location.register64.reghi)));
  1541. end
  1542. else
  1543. if location.size in [OS_32,OS_S32] then
  1544. begin
  1545. rv.intregvars.addnodup(getsupreg(location.register));
  1546. rv.intregvars.addnodup(getsupreg(GetNextReg(location.register)));
  1547. end
  1548. else
  1549. {$elseif defined(cpu8bitalu)}
  1550. if location.size in [OS_64,OS_S64] then
  1551. begin
  1552. rv.intregvars.addnodup(getsupreg(location.register64.reglo));
  1553. rv.intregvars.addnodup(getsupreg(GetNextReg(location.register64.reglo)));
  1554. rv.intregvars.addnodup(getsupreg(GetNextReg(GetNextReg(location.register64.reglo))));
  1555. rv.intregvars.addnodup(getsupreg(GetNextReg(GetNextReg(GetNextReg(location.register64.reglo)))));
  1556. rv.intregvars.addnodup(getsupreg(location.register64.reghi));
  1557. rv.intregvars.addnodup(getsupreg(GetNextReg(location.register64.reghi)));
  1558. rv.intregvars.addnodup(getsupreg(GetNextReg(GetNextReg(location.register64.reghi))));
  1559. rv.intregvars.addnodup(getsupreg(GetNextReg(GetNextReg(GetNextReg(location.register64.reghi)))));
  1560. end
  1561. else
  1562. if location.size in [OS_32,OS_S32] then
  1563. begin
  1564. rv.intregvars.addnodup(getsupreg(location.register));
  1565. rv.intregvars.addnodup(getsupreg(GetNextReg(location.register)));
  1566. rv.intregvars.addnodup(getsupreg(GetNextReg(GetNextReg(location.register))));
  1567. rv.intregvars.addnodup(getsupreg(GetNextReg(GetNextReg(GetNextReg(location.register)))));
  1568. end
  1569. else
  1570. if location.size in [OS_16,OS_S16] then
  1571. begin
  1572. rv.intregvars.addnodup(getsupreg(location.register));
  1573. rv.intregvars.addnodup(getsupreg(GetNextReg(location.register)));
  1574. end
  1575. else
  1576. {$endif}
  1577. rv.intregvars.addnodup(getsupreg(location.register));
  1578. LOC_CFPUREGISTER:
  1579. rv.fpuregvars.addnodup(getsupreg(location.register));
  1580. LOC_CMMREGISTER:
  1581. rv.mmregvars.addnodup(getsupreg(location.register));
  1582. end;
  1583. end;
  1584. function do_get_used_regvars(var n: tnode; arg: pointer): foreachnoderesult;
  1585. var
  1586. rv: pusedregvars absolute arg;
  1587. begin
  1588. case (n.nodetype) of
  1589. temprefn:
  1590. { We only have to synchronise a tempnode before a loop if it is }
  1591. { not created inside the loop, and only synchronise after the }
  1592. { loop if it's not destroyed inside the loop. If it's created }
  1593. { before the loop and not yet destroyed, then before the loop }
  1594. { is secondpassed tempinfo^.valid will be true, and we get the }
  1595. { correct registers. If it's not destroyed inside the loop, }
  1596. { then after the loop has been secondpassed tempinfo^.valid }
  1597. { be true and we also get the right registers. In other cases, }
  1598. { tempinfo^.valid will be false and so we do not add }
  1599. { unnecessary registers. This way, we don't have to look at }
  1600. { tempcreate and tempdestroy nodes to get this info (JM) }
  1601. if (ti_valid in ttemprefnode(n).tempinfo^.flags) then
  1602. add_regvars(rv^,ttemprefnode(n).tempinfo^.location);
  1603. loadn:
  1604. if (tloadnode(n).symtableentry.typ in [staticvarsym,localvarsym,paravarsym]) then
  1605. add_regvars(rv^,tabstractnormalvarsym(tloadnode(n).symtableentry).localloc);
  1606. vecn:
  1607. { range checks sometimes need the high parameter }
  1608. if (cs_check_range in current_settings.localswitches) and
  1609. (is_open_array(tvecnode(n).left.resultdef) or
  1610. is_array_of_const(tvecnode(n).left.resultdef)) and
  1611. not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  1612. add_regvars(rv^,tabstractnormalvarsym(get_high_value_sym(tparavarsym(tloadnode(tvecnode(n).left).symtableentry))).localloc)
  1613. end;
  1614. result := fen_true;
  1615. end;
  1616. procedure get_used_regvars(n: tnode; var rv: tusedregvars);
  1617. begin
  1618. foreachnodestatic(n,@do_get_used_regvars,@rv);
  1619. end;
  1620. (*
  1621. See comments at declaration of pusedregvarscommon
  1622. function do_get_used_regvars_common(var n: tnode; arg: pointer): foreachnoderesult;
  1623. var
  1624. rv: pusedregvarscommon absolute arg;
  1625. begin
  1626. if (n.nodetype = loadn) and
  1627. (tloadnode(n).symtableentry.typ in [staticvarsym,localvarsym,paravarsym]) then
  1628. with tabstractnormalvarsym(tloadnode(n).symtableentry).localloc do
  1629. case loc of
  1630. LOC_CREGISTER:
  1631. { if not yet encountered in this node tree }
  1632. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  1633. { but nevertheless already encountered somewhere }
  1634. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  1635. { then it's a regvar used in two or more node trees }
  1636. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  1637. LOC_CFPUREGISTER:
  1638. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  1639. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  1640. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  1641. LOC_CMMREGISTER:
  1642. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  1643. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  1644. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  1645. end;
  1646. result := fen_true;
  1647. end;
  1648. procedure get_used_regvars_common(n: tnode; var rv: tusedregvarscommon);
  1649. begin
  1650. rv.myregvars.intregvars.clear;
  1651. rv.myregvars.fpuregvars.clear;
  1652. rv.myregvars.mmregvars.clear;
  1653. foreachnodestatic(n,@do_get_used_regvars_common,@rv);
  1654. end;
  1655. *)
  1656. procedure gen_sync_regvars(list:TAsmList; var rv: tusedregvars);
  1657. var
  1658. count: longint;
  1659. begin
  1660. for count := 1 to rv.intregvars.length do
  1661. cg.a_reg_sync(list,newreg(R_INTREGISTER,rv.intregvars.readidx(count-1),R_SUBWHOLE));
  1662. for count := 1 to rv.fpuregvars.length do
  1663. cg.a_reg_sync(list,newreg(R_FPUREGISTER,rv.fpuregvars.readidx(count-1),R_SUBWHOLE));
  1664. for count := 1 to rv.mmregvars.length do
  1665. cg.a_reg_sync(list,newreg(R_MMREGISTER,rv.mmregvars.readidx(count-1),R_SUBWHOLE));
  1666. end;
  1667. procedure gen_free_symtable(list:TAsmList;st:TSymtable);
  1668. var
  1669. i : longint;
  1670. sym : tsym;
  1671. begin
  1672. for i:=0 to st.SymList.Count-1 do
  1673. begin
  1674. sym:=tsym(st.SymList[i]);
  1675. if (sym.typ in [staticvarsym,localvarsym,paravarsym]) then
  1676. begin
  1677. with tabstractnormalvarsym(sym) do
  1678. begin
  1679. { Note: We need to keep the data available in memory
  1680. for the sub procedures that can access local data
  1681. in the parent procedures }
  1682. case localloc.loc of
  1683. LOC_CREGISTER :
  1684. if (pi_has_label in current_procinfo.flags) then
  1685. {$if defined(cpu64bitalu)}
  1686. if def_cgsize(vardef) in [OS_128,OS_S128] then
  1687. begin
  1688. cg.a_reg_sync(list,localloc.register128.reglo);
  1689. cg.a_reg_sync(list,localloc.register128.reghi);
  1690. end
  1691. else
  1692. {$elseif defined(cpu32bitalu)}
  1693. if def_cgsize(vardef) in [OS_64,OS_S64] then
  1694. begin
  1695. cg.a_reg_sync(list,localloc.register64.reglo);
  1696. cg.a_reg_sync(list,localloc.register64.reghi);
  1697. end
  1698. else
  1699. {$elseif defined(cpu16bitalu)}
  1700. if def_cgsize(vardef) in [OS_64,OS_S64] then
  1701. begin
  1702. cg.a_reg_sync(list,localloc.register64.reglo);
  1703. cg.a_reg_sync(list,GetNextReg(localloc.register64.reglo));
  1704. cg.a_reg_sync(list,localloc.register64.reghi);
  1705. cg.a_reg_sync(list,GetNextReg(localloc.register64.reghi));
  1706. end
  1707. else
  1708. if def_cgsize(vardef) in [OS_32,OS_S32] then
  1709. begin
  1710. cg.a_reg_sync(list,localloc.register);
  1711. cg.a_reg_sync(list,GetNextReg(localloc.register));
  1712. end
  1713. else
  1714. {$elseif defined(cpu8bitalu)}
  1715. if def_cgsize(vardef) in [OS_64,OS_S64] then
  1716. begin
  1717. cg.a_reg_sync(list,localloc.register64.reglo);
  1718. cg.a_reg_sync(list,GetNextReg(localloc.register64.reglo));
  1719. cg.a_reg_sync(list,GetNextReg(GetNextReg(localloc.register64.reglo)));
  1720. cg.a_reg_sync(list,GetNextReg(GetNextReg(GetNextReg(localloc.register64.reglo))));
  1721. cg.a_reg_sync(list,localloc.register64.reghi);
  1722. cg.a_reg_sync(list,GetNextReg(localloc.register64.reghi));
  1723. cg.a_reg_sync(list,GetNextReg(GetNextReg(localloc.register64.reghi)));
  1724. cg.a_reg_sync(list,GetNextReg(GetNextReg(GetNextReg(localloc.register64.reghi))));
  1725. end
  1726. else
  1727. if def_cgsize(vardef) in [OS_32,OS_S32] then
  1728. begin
  1729. cg.a_reg_sync(list,localloc.register);
  1730. cg.a_reg_sync(list,GetNextReg(localloc.register));
  1731. cg.a_reg_sync(list,GetNextReg(GetNextReg(localloc.register)));
  1732. cg.a_reg_sync(list,GetNextReg(GetNextReg(GetNextReg(localloc.register))));
  1733. end
  1734. else
  1735. if def_cgsize(vardef) in [OS_16,OS_S16] then
  1736. begin
  1737. cg.a_reg_sync(list,localloc.register);
  1738. cg.a_reg_sync(list,GetNextReg(localloc.register));
  1739. end
  1740. else
  1741. {$endif}
  1742. cg.a_reg_sync(list,localloc.register);
  1743. LOC_CFPUREGISTER,
  1744. LOC_CMMREGISTER:
  1745. if (pi_has_label in current_procinfo.flags) then
  1746. cg.a_reg_sync(list,localloc.register);
  1747. LOC_REFERENCE :
  1748. begin
  1749. if typ in [localvarsym,paravarsym] then
  1750. tg.Ungetlocal(list,localloc.reference);
  1751. end;
  1752. end;
  1753. end;
  1754. end;
  1755. end;
  1756. end;
  1757. procedure gen_load_vmt_register(list:TAsmList;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  1758. var
  1759. href : treference;
  1760. selfdef: tdef;
  1761. begin
  1762. if is_object(objdef) then
  1763. begin
  1764. case selfloc.loc of
  1765. LOC_CREFERENCE,
  1766. LOC_REFERENCE:
  1767. begin
  1768. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset,sizeof(pint));
  1769. cg.a_loadaddr_ref_reg(list,selfloc.reference,href.base);
  1770. selfdef:=getpointerdef(objdef);
  1771. end;
  1772. else
  1773. internalerror(200305056);
  1774. end;
  1775. end
  1776. else
  1777. { This is also valid for Objective-C classes: vmt_offset is 0 there,
  1778. and the first "field" of an Objective-C class instance is a pointer
  1779. to its "meta-class". }
  1780. begin
  1781. selfdef:=objdef;
  1782. case selfloc.loc of
  1783. LOC_REGISTER:
  1784. begin
  1785. {$ifdef cpu_uses_separate_address_registers}
  1786. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  1787. begin
  1788. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset,sizeof(pint));
  1789. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,selfloc.register,href.base);
  1790. end
  1791. else
  1792. {$endif cpu_uses_separate_address_registers}
  1793. reference_reset_base(href,selfloc.register,objdef.vmt_offset,sizeof(pint));
  1794. end;
  1795. LOC_CONSTANT,
  1796. LOC_CREGISTER,
  1797. LOC_CREFERENCE,
  1798. LOC_REFERENCE,
  1799. LOC_CSUBSETREG,
  1800. LOC_SUBSETREG,
  1801. LOC_CSUBSETREF,
  1802. LOC_SUBSETREF:
  1803. begin
  1804. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset,sizeof(pint));
  1805. { todo: pass actual vmt pointer type to hlcg }
  1806. hlcg.a_load_loc_reg(list,voidpointertype,voidpointertype,selfloc,href.base);
  1807. end;
  1808. else
  1809. internalerror(200305057);
  1810. end;
  1811. end;
  1812. vmtreg:=cg.getaddressregister(list);
  1813. hlcg.g_maybe_testself(list,selfdef,href.base);
  1814. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,vmtreg);
  1815. { test validity of VMT }
  1816. if not(is_interface(objdef)) and
  1817. not(is_cppclass(objdef)) and
  1818. not(is_objc_class_or_protocol(objdef)) then
  1819. cg.g_maybe_testvmt(list,vmtreg,objdef);
  1820. end;
  1821. function getprocalign : shortint;
  1822. begin
  1823. { gprof uses 16 byte granularity }
  1824. if (cs_profile in current_settings.moduleswitches) then
  1825. result:=16
  1826. else
  1827. result:=current_settings.alignment.procalign;
  1828. end;
  1829. procedure gen_fpc_dummy(list : TAsmList);
  1830. begin
  1831. {$ifdef i386}
  1832. { fix me! }
  1833. list.concat(Taicpu.Op_const_reg(A_MOV,S_L,1,NR_EAX));
  1834. list.concat(Taicpu.Op_const(A_RET,S_W,12));
  1835. {$endif i386}
  1836. end;
  1837. procedure gen_load_frame_for_exceptfilter(list : TAsmList);
  1838. var
  1839. para: tparavarsym;
  1840. begin
  1841. para:=tparavarsym(current_procinfo.procdef.paras[0]);
  1842. if not (vo_is_parentfp in para.varoptions) then
  1843. InternalError(201201142);
  1844. if (para.paraloc[calleeside].location^.loc<>LOC_REGISTER) or
  1845. (para.paraloc[calleeside].location^.next<>nil) then
  1846. InternalError(201201143);
  1847. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,para.paraloc[calleeside].location^.register,
  1848. NR_FRAME_POINTER_REG);
  1849. end;
  1850. end.