ncgutil.pas 96 KB

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