ncgutil.pas 85 KB

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