ncgutil.pas 87 KB

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