ncgutil.pas 85 KB

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