ncgutil.pas 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230
  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. {$ifdef cpu16bitalu}
  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 cpu16bitalu}
  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. else
  1008. internalerror(200410105);
  1009. end
  1010. else
  1011. begin
  1012. unget_para(paraloc^);
  1013. gen_alloc_regloc(list,destloc);
  1014. cg.a_load_cgparaloc_anyreg(list,destloc.size,paraloc^,destloc.register,sizeof(aint));
  1015. end;
  1016. end;
  1017. end;
  1018. LOC_FPUREGISTER,
  1019. LOC_CFPUREGISTER :
  1020. begin
  1021. {$ifdef mips}
  1022. if (destloc.size = paraloc^.Size) and
  1023. (paraloc^.Loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER,LOC_REFERENCE,LOC_CREFERENCE]) then
  1024. begin
  1025. unget_para(paraloc^);
  1026. gen_alloc_regloc(list,destloc);
  1027. cg.a_load_cgparaloc_anyreg(list,destloc.size,paraloc^,destloc.register,para.alignment);
  1028. end
  1029. else if (destloc.size = OS_F32) and
  1030. (paraloc^.Loc in [LOC_REGISTER,LOC_CREGISTER]) then
  1031. begin
  1032. gen_alloc_regloc(list,destloc);
  1033. unget_para(paraloc^);
  1034. list.Concat(taicpu.op_reg_reg(A_MTC1,paraloc^.register,destloc.register));
  1035. end
  1036. { TODO: Produces invalid code, needs fixing together with regalloc setup. }
  1037. {
  1038. else if (destloc.size = OS_F64) and
  1039. (paraloc^.Loc in [LOC_REGISTER,LOC_CREGISTER]) and
  1040. (paraloc^.next^.Loc in [LOC_REGISTER,LOC_CREGISTER]) then
  1041. begin
  1042. gen_alloc_regloc(list,destloc);
  1043. tmpreg:=destloc.register;
  1044. unget_para(paraloc^);
  1045. list.Concat(taicpu.op_reg_reg(A_MTC1,paraloc^.register,tmpreg));
  1046. setsupreg(tmpreg,getsupreg(tmpreg)+1);
  1047. unget_para(paraloc^.next^);
  1048. list.Concat(taicpu.op_reg_reg(A_MTC1,paraloc^.Next^.register,tmpreg));
  1049. end
  1050. }
  1051. else
  1052. begin
  1053. sizeleft := TCGSize2Size[destloc.size];
  1054. tg.GetTemp(list,sizeleft,sizeleft,tt_normal,tempref);
  1055. href:=tempref;
  1056. while assigned(paraloc) do
  1057. begin
  1058. unget_para(paraloc^);
  1059. cg.a_load_cgparaloc_ref(list,paraloc^,href,sizeleft,destloc.reference.alignment);
  1060. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1061. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1062. paraloc:=paraloc^.next;
  1063. end;
  1064. gen_alloc_regloc(list,destloc);
  1065. cg.a_loadfpu_ref_reg(list,destloc.size,destloc.size,tempref,destloc.register);
  1066. tg.UnGetTemp(list,tempref);
  1067. end;
  1068. {$else mips}
  1069. {$if defined(sparc) or defined(arm)}
  1070. { Arm and Sparc passes floats in int registers, when loading to fpu register
  1071. we need a temp }
  1072. sizeleft := TCGSize2Size[destloc.size];
  1073. tg.GetTemp(list,sizeleft,sizeleft,tt_normal,tempref);
  1074. href:=tempref;
  1075. while assigned(paraloc) do
  1076. begin
  1077. unget_para(paraloc^);
  1078. cg.a_load_cgparaloc_ref(list,paraloc^,href,sizeleft,destloc.reference.alignment);
  1079. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1080. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1081. paraloc:=paraloc^.next;
  1082. end;
  1083. gen_alloc_regloc(list,destloc);
  1084. cg.a_loadfpu_ref_reg(list,destloc.size,destloc.size,tempref,destloc.register);
  1085. tg.UnGetTemp(list,tempref);
  1086. {$else defined(sparc) or defined(arm)}
  1087. unget_para(paraloc^);
  1088. gen_alloc_regloc(list,destloc);
  1089. { from register to register -> alignment is irrelevant }
  1090. cg.a_load_cgparaloc_anyreg(list,destloc.size,paraloc^,destloc.register,0);
  1091. if assigned(paraloc^.next) then
  1092. internalerror(200410109);
  1093. {$endif defined(sparc) or defined(arm)}
  1094. {$endif mips}
  1095. end;
  1096. LOC_MMREGISTER,
  1097. LOC_CMMREGISTER :
  1098. begin
  1099. {$ifndef cpu64bitalu}
  1100. { ARM vfp floats are passed in integer registers }
  1101. if (para.size=OS_F64) and
  1102. (paraloc^.size in [OS_32,OS_S32]) and
  1103. use_vectorfpu(vardef) then
  1104. begin
  1105. { we need 2x32bit reg }
  1106. if not assigned(paraloc^.next) or
  1107. assigned(paraloc^.next^.next) then
  1108. internalerror(2009112421);
  1109. unget_para(paraloc^.next^);
  1110. case paraloc^.next^.loc of
  1111. LOC_REGISTER:
  1112. tempreg:=paraloc^.next^.register;
  1113. LOC_REFERENCE:
  1114. begin
  1115. tempreg:=cg.getintregister(list,OS_32);
  1116. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^.next^,tempreg,4);
  1117. end;
  1118. else
  1119. internalerror(2012051301);
  1120. end;
  1121. { don't free before the above, because then the getintregister
  1122. could reallocate this register and overwrite it }
  1123. unget_para(paraloc^);
  1124. gen_alloc_regloc(list,destloc);
  1125. if (target_info.endian=endian_big) then
  1126. { paraloc^ -> high
  1127. paraloc^.next -> low }
  1128. reg64:=joinreg64(tempreg,paraloc^.register)
  1129. else
  1130. reg64:=joinreg64(paraloc^.register,tempreg);
  1131. cg64.a_loadmm_intreg64_reg(list,OS_F64,reg64,destloc.register);
  1132. end
  1133. else
  1134. {$endif not cpu64bitalu}
  1135. begin
  1136. unget_para(paraloc^);
  1137. gen_alloc_regloc(list,destloc);
  1138. { from register to register -> alignment is irrelevant }
  1139. cg.a_load_cgparaloc_anyreg(list,destloc.size,paraloc^,destloc.register,0);
  1140. { data could come in two memory locations, for now
  1141. we simply ignore the sanity check (FK)
  1142. if assigned(paraloc^.next) then
  1143. internalerror(200410108);
  1144. }
  1145. end;
  1146. end;
  1147. else
  1148. internalerror(2010052903);
  1149. end;
  1150. end;
  1151. procedure gen_load_para_value(list:TAsmList);
  1152. procedure get_para(const paraloc:TCGParaLocation);
  1153. begin
  1154. case paraloc.loc of
  1155. LOC_REGISTER :
  1156. begin
  1157. if getsupreg(paraloc.register)<first_int_imreg then
  1158. cg.getcpuregister(list,paraloc.register);
  1159. end;
  1160. LOC_MMREGISTER :
  1161. begin
  1162. if getsupreg(paraloc.register)<first_mm_imreg then
  1163. cg.getcpuregister(list,paraloc.register);
  1164. end;
  1165. LOC_FPUREGISTER :
  1166. begin
  1167. if getsupreg(paraloc.register)<first_fpu_imreg then
  1168. cg.getcpuregister(list,paraloc.register);
  1169. end;
  1170. end;
  1171. end;
  1172. var
  1173. i : longint;
  1174. currpara : tparavarsym;
  1175. paraloc : pcgparalocation;
  1176. begin
  1177. if (po_assembler in current_procinfo.procdef.procoptions) or
  1178. { exceptfilters have a single hidden 'parentfp' parameter, which
  1179. is handled by tcg.g_proc_entry. }
  1180. (current_procinfo.procdef.proctypeoption=potype_exceptfilter) then
  1181. exit;
  1182. { Allocate registers used by parameters }
  1183. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1184. begin
  1185. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1186. paraloc:=currpara.paraloc[calleeside].location;
  1187. while assigned(paraloc) do
  1188. begin
  1189. if paraloc^.loc in [LOC_REGISTER,LOC_FPUREGISTER,LOC_MMREGISTER] then
  1190. get_para(paraloc^);
  1191. paraloc:=paraloc^.next;
  1192. end;
  1193. end;
  1194. { Copy parameters to local references/registers }
  1195. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1196. begin
  1197. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1198. gen_load_cgpara_loc(list,currpara.vardef,currpara.paraloc[calleeside],currpara.initialloc,paramanager.param_use_paraloc(currpara.paraloc[calleeside]));
  1199. { gen_load_cgpara_loc() already allocated the initialloc
  1200. -> don't allocate again }
  1201. if currpara.initialloc.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMREGISTER] then
  1202. gen_alloc_regvar(list,currpara,false);
  1203. end;
  1204. { generate copies of call by value parameters, must be done before
  1205. the initialization and body is parsed because the refcounts are
  1206. incremented using the local copies }
  1207. current_procinfo.procdef.parast.SymList.ForEachCall(@copyvalueparas,list);
  1208. {$ifdef powerpc}
  1209. { unget the register that contains the stack pointer before the procedure entry, }
  1210. { which is used to access the parameters in their original callee-side location }
  1211. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1212. cg.a_reg_dealloc(list,NR_R12);
  1213. {$endif powerpc}
  1214. {$ifdef powerpc64}
  1215. { unget the register that contains the stack pointer before the procedure entry, }
  1216. { which is used to access the parameters in their original callee-side location }
  1217. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1218. cg.a_reg_dealloc(list, NR_OLD_STACK_POINTER_REG);
  1219. {$endif powerpc64}
  1220. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1221. begin
  1222. { initialize refcounted paras, and trash others. Needed here
  1223. instead of in gen_initialize_code, because when a reference is
  1224. intialised or trashed while the pointer to that reference is kept
  1225. in a regvar, we add a register move and that one again has to
  1226. come after the parameter loading code as far as the register
  1227. allocator is concerned }
  1228. current_procinfo.procdef.parast.SymList.ForEachCall(@init_paras,list);
  1229. end;
  1230. end;
  1231. {****************************************************************************
  1232. Entry/Exit
  1233. ****************************************************************************}
  1234. function has_alias_name(pd:tprocdef;const s:string):boolean;
  1235. var
  1236. item : TCmdStrListItem;
  1237. begin
  1238. result:=true;
  1239. if pd.mangledname=s then
  1240. exit;
  1241. item := TCmdStrListItem(pd.aliasnames.first);
  1242. while assigned(item) do
  1243. begin
  1244. if item.str=s then
  1245. exit;
  1246. item := TCmdStrListItem(item.next);
  1247. end;
  1248. result:=false;
  1249. end;
  1250. procedure alloc_proc_symbol(pd: tprocdef);
  1251. var
  1252. item : TCmdStrListItem;
  1253. begin
  1254. item := TCmdStrListItem(pd.aliasnames.first);
  1255. while assigned(item) do
  1256. begin
  1257. current_asmdata.DefineAsmSymbol(item.str,AB_GLOBAL,AT_FUNCTION);
  1258. item := TCmdStrListItem(item.next);
  1259. end;
  1260. end;
  1261. procedure gen_proc_entry_code(list:TAsmList);
  1262. var
  1263. hitemp,
  1264. lotemp, stack_frame_size : longint;
  1265. begin
  1266. { generate call frame marker for dwarf call frame info }
  1267. current_asmdata.asmcfi.start_frame(list);
  1268. { All temps are know, write offsets used for information }
  1269. if (cs_asm_source in current_settings.globalswitches) then
  1270. begin
  1271. if tg.direction>0 then
  1272. begin
  1273. lotemp:=current_procinfo.tempstart;
  1274. hitemp:=tg.lasttemp;
  1275. end
  1276. else
  1277. begin
  1278. lotemp:=tg.lasttemp;
  1279. hitemp:=current_procinfo.tempstart;
  1280. end;
  1281. list.concat(Tai_comment.Create(strpnew('Temps allocated between '+std_regname(current_procinfo.framepointer)+
  1282. tostr_with_plus(lotemp)+' and '+std_regname(current_procinfo.framepointer)+tostr_with_plus(hitemp))));
  1283. end;
  1284. { generate target specific proc entry code }
  1285. stack_frame_size := current_procinfo.calc_stackframe_size;
  1286. if (stack_frame_size <> 0) and
  1287. (po_nostackframe in current_procinfo.procdef.procoptions) then
  1288. message1(parser_e_nostackframe_with_locals,tostr(stack_frame_size));
  1289. hlcg.g_proc_entry(list,stack_frame_size,(po_nostackframe in current_procinfo.procdef.procoptions));
  1290. end;
  1291. procedure gen_proc_exit_code(list:TAsmList);
  1292. var
  1293. parasize : longint;
  1294. begin
  1295. { c style clearstack does not need to remove parameters from the stack, only the
  1296. return value when it was pushed by arguments }
  1297. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1298. begin
  1299. parasize:=0;
  1300. if paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef) then
  1301. inc(parasize,sizeof(pint));
  1302. end
  1303. else
  1304. begin
  1305. parasize:=current_procinfo.para_stack_size;
  1306. { the parent frame pointer para has to be removed by the caller in
  1307. case of Delphi-style parent frame pointer passing }
  1308. if not paramanager.use_fixed_stack and
  1309. (po_delphi_nested_cc in current_procinfo.procdef.procoptions) then
  1310. dec(parasize,sizeof(pint));
  1311. end;
  1312. { generate target specific proc exit code }
  1313. hlcg.g_proc_exit(list,parasize,(po_nostackframe in current_procinfo.procdef.procoptions));
  1314. { release return registers, needed for optimizer }
  1315. if not is_void(current_procinfo.procdef.returndef) then
  1316. paramanager.freecgpara(list,current_procinfo.procdef.funcretloc[calleeside]);
  1317. { end of frame marker for call frame info }
  1318. current_asmdata.asmcfi.end_frame(list);
  1319. end;
  1320. procedure gen_stack_check_size_para(list:TAsmList);
  1321. var
  1322. paraloc1 : tcgpara;
  1323. pd : tprocdef;
  1324. begin
  1325. pd:=search_system_proc('fpc_stackcheck');
  1326. paraloc1.init;
  1327. paramanager.getintparaloc(pd,1,paraloc1);
  1328. cg.a_load_const_cgpara(list,OS_INT,current_procinfo.calc_stackframe_size,paraloc1);
  1329. paramanager.freecgpara(list,paraloc1);
  1330. paraloc1.done;
  1331. end;
  1332. procedure gen_stack_check_call(list:TAsmList);
  1333. var
  1334. paraloc1 : tcgpara;
  1335. pd : tprocdef;
  1336. begin
  1337. pd:=search_system_proc('fpc_stackcheck');
  1338. paraloc1.init;
  1339. { Also alloc the register needed for the parameter }
  1340. paramanager.getintparaloc(pd,1,paraloc1);
  1341. paramanager.freecgpara(list,paraloc1);
  1342. { Call the helper }
  1343. cg.allocallcpuregisters(list);
  1344. cg.a_call_name(list,'FPC_STACKCHECK',false);
  1345. cg.deallocallcpuregisters(list);
  1346. paraloc1.done;
  1347. end;
  1348. procedure gen_save_used_regs(list:TAsmList);
  1349. begin
  1350. { Pure assembler routines need to save the registers themselves }
  1351. if (po_assembler in current_procinfo.procdef.procoptions) then
  1352. exit;
  1353. { oldfpccall expects all registers to be destroyed }
  1354. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1355. cg.g_save_registers(list);
  1356. end;
  1357. procedure gen_restore_used_regs(list:TAsmList);
  1358. begin
  1359. { Pure assembler routines need to save the registers themselves }
  1360. if (po_assembler in current_procinfo.procdef.procoptions) then
  1361. exit;
  1362. { oldfpccall expects all registers to be destroyed }
  1363. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1364. cg.g_restore_registers(list);
  1365. end;
  1366. {****************************************************************************
  1367. External handling
  1368. ****************************************************************************}
  1369. procedure gen_external_stub(list:TAsmList;pd:tprocdef;const externalname:string);
  1370. begin
  1371. create_hlcodegen;
  1372. { add the procedure to the al_procedures }
  1373. maybe_new_object_file(list);
  1374. new_section(list,sec_code,lower(pd.mangledname),current_settings.alignment.procalign);
  1375. list.concat(Tai_align.create(current_settings.alignment.procalign));
  1376. if (po_global in pd.procoptions) then
  1377. list.concat(Tai_symbol.createname_global(pd.mangledname,AT_FUNCTION,0))
  1378. else
  1379. list.concat(Tai_symbol.createname(pd.mangledname,AT_FUNCTION,0));
  1380. cg.g_external_wrapper(list,pd,externalname);
  1381. destroy_hlcodegen;
  1382. end;
  1383. {****************************************************************************
  1384. Const Data
  1385. ****************************************************************************}
  1386. procedure gen_alloc_symtable(list:TAsmList;pd:tprocdef;st:TSymtable);
  1387. procedure setlocalloc(vs:tabstractnormalvarsym);
  1388. begin
  1389. if cs_asm_source in current_settings.globalswitches then
  1390. begin
  1391. case vs.initialloc.loc of
  1392. LOC_REFERENCE :
  1393. begin
  1394. if not assigned(vs.initialloc.reference.symbol) then
  1395. list.concat(Tai_comment.Create(strpnew('Var '+vs.realname+' located at '+
  1396. std_regname(vs.initialloc.reference.base)+tostr_with_plus(vs.initialloc.reference.offset))));
  1397. end;
  1398. end;
  1399. end;
  1400. vs.localloc:=vs.initialloc;
  1401. FillChar(vs.currentregloc,sizeof(vs.currentregloc),0);
  1402. end;
  1403. var
  1404. i : longint;
  1405. sym : tsym;
  1406. vs : tabstractnormalvarsym;
  1407. isaddr : boolean;
  1408. begin
  1409. for i:=0 to st.SymList.Count-1 do
  1410. begin
  1411. sym:=tsym(st.SymList[i]);
  1412. case sym.typ of
  1413. staticvarsym :
  1414. begin
  1415. vs:=tabstractnormalvarsym(sym);
  1416. { The code in loadnode.pass_generatecode will create the
  1417. LOC_REFERENCE instead for all none register variables. This is
  1418. required because we can't store an asmsymbol in the localloc because
  1419. the asmsymbol is invalid after an unit is compiled. This gives
  1420. problems when this procedure is inlined in another unit (PFV) }
  1421. if vs.is_regvar(false) then
  1422. begin
  1423. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable];
  1424. vs.initialloc.size:=def_cgsize(vs.vardef);
  1425. gen_alloc_regvar(list,vs,true);
  1426. setlocalloc(vs);
  1427. end;
  1428. end;
  1429. paravarsym :
  1430. begin
  1431. vs:=tabstractnormalvarsym(sym);
  1432. { Parameters passed to assembler procedures need to be kept
  1433. in the original location }
  1434. if (po_assembler in current_procinfo.procdef.procoptions) then
  1435. tparavarsym(vs).paraloc[calleeside].get_location(vs.initialloc)
  1436. { exception filters receive their frame pointer as a parameter }
  1437. else if (current_procinfo.procdef.proctypeoption=potype_exceptfilter) and
  1438. (vo_is_parentfp in vs.varoptions) then
  1439. begin
  1440. location_reset(vs.initialloc,LOC_REGISTER,OS_ADDR);
  1441. vs.initialloc.register:=NR_FRAME_POINTER_REG;
  1442. end
  1443. else
  1444. begin
  1445. isaddr:=paramanager.push_addr_param(vs.varspez,vs.vardef,current_procinfo.procdef.proccalloption);
  1446. if isaddr then
  1447. vs.initialloc.size:=OS_ADDR
  1448. else
  1449. vs.initialloc.size:=def_cgsize(vs.vardef);
  1450. if vs.is_regvar(isaddr) then
  1451. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable]
  1452. else
  1453. begin
  1454. vs.initialloc.loc:=LOC_REFERENCE;
  1455. { Reuse the parameter location for values to are at a single location on the stack }
  1456. if paramanager.param_use_paraloc(tparavarsym(sym).paraloc[calleeside]) then
  1457. begin
  1458. reference_reset_base(vs.initialloc.reference,tparavarsym(sym).paraloc[calleeside].location^.reference.index,
  1459. tparavarsym(sym).paraloc[calleeside].location^.reference.offset,tparavarsym(sym).paraloc[calleeside].alignment);
  1460. end
  1461. else
  1462. begin
  1463. if isaddr then
  1464. tg.GetLocal(list,sizeof(pint),voidpointertype,vs.initialloc.reference)
  1465. else
  1466. tg.GetLocal(list,vs.getsize,tparavarsym(sym).paraloc[calleeside].alignment,vs.vardef,vs.initialloc.reference);
  1467. end;
  1468. end;
  1469. end;
  1470. setlocalloc(vs);
  1471. end;
  1472. localvarsym :
  1473. begin
  1474. vs:=tabstractnormalvarsym(sym);
  1475. vs.initialloc.size:=def_cgsize(vs.vardef);
  1476. if ([po_assembler,po_nostackframe] * current_procinfo.procdef.procoptions = [po_assembler,po_nostackframe]) and
  1477. (vo_is_funcret in vs.varoptions) then
  1478. begin
  1479. paramanager.create_funcretloc_info(pd,calleeside);
  1480. if assigned(pd.funcretloc[calleeside].location^.next) then
  1481. begin
  1482. { can't replace references to "result" with a complex
  1483. location expression inside assembler code }
  1484. location_reset(vs.initialloc,LOC_INVALID,OS_NO);
  1485. end
  1486. else
  1487. pd.funcretloc[calleeside].get_location(vs.initialloc);
  1488. end
  1489. else if (m_delphi in current_settings.modeswitches) and
  1490. (po_assembler in current_procinfo.procdef.procoptions) and
  1491. (vo_is_funcret in vs.varoptions) and
  1492. (vs.refs=0) then
  1493. begin
  1494. { not referenced, so don't allocate. Use dummy to }
  1495. { avoid ie's later on because of LOC_INVALID }
  1496. vs.initialloc.loc:=LOC_REGISTER;
  1497. vs.initialloc.size:=OS_INT;
  1498. vs.initialloc.register:=NR_FUNCTION_RESULT_REG;
  1499. end
  1500. else if vs.is_regvar(false) then
  1501. begin
  1502. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable];
  1503. gen_alloc_regvar(list,vs,true);
  1504. end
  1505. else
  1506. begin
  1507. vs.initialloc.loc:=LOC_REFERENCE;
  1508. tg.GetLocal(list,vs.getsize,vs.vardef,vs.initialloc.reference);
  1509. end;
  1510. setlocalloc(vs);
  1511. end;
  1512. end;
  1513. end;
  1514. end;
  1515. procedure add_regvars(var rv: tusedregvars; const location: tlocation);
  1516. begin
  1517. case location.loc of
  1518. LOC_CREGISTER:
  1519. {$if defined(cpu64bitalu)}
  1520. if location.size in [OS_128,OS_S128] then
  1521. begin
  1522. rv.intregvars.addnodup(getsupreg(location.register128.reglo));
  1523. rv.intregvars.addnodup(getsupreg(location.register128.reghi));
  1524. end
  1525. else
  1526. {$elseif defined(cpu32bitalu)}
  1527. if location.size in [OS_64,OS_S64] then
  1528. begin
  1529. rv.intregvars.addnodup(getsupreg(location.register64.reglo));
  1530. rv.intregvars.addnodup(getsupreg(location.register64.reghi));
  1531. end
  1532. else
  1533. {$elseif defined(cpu16bitalu)}
  1534. if location.size in [OS_64,OS_S64] then
  1535. begin
  1536. rv.intregvars.addnodup(getsupreg(location.register64.reglo));
  1537. rv.intregvars.addnodup(getsupreg(GetNextReg(location.register64.reglo)));
  1538. rv.intregvars.addnodup(getsupreg(location.register64.reghi));
  1539. rv.intregvars.addnodup(getsupreg(GetNextReg(location.register64.reghi)));
  1540. end
  1541. else
  1542. if location.size in [OS_32,OS_S32] then
  1543. begin
  1544. rv.intregvars.addnodup(getsupreg(location.register));
  1545. rv.intregvars.addnodup(getsupreg(GetNextReg(location.register)));
  1546. end
  1547. else
  1548. {$elseif defined(cpu8bitalu)}
  1549. if location.size in [OS_64,OS_S64] then
  1550. begin
  1551. rv.intregvars.addnodup(getsupreg(location.register64.reglo));
  1552. rv.intregvars.addnodup(getsupreg(GetNextReg(location.register64.reglo)));
  1553. rv.intregvars.addnodup(getsupreg(GetNextReg(GetNextReg(location.register64.reglo))));
  1554. rv.intregvars.addnodup(getsupreg(GetNextReg(GetNextReg(GetNextReg(location.register64.reglo)))));
  1555. rv.intregvars.addnodup(getsupreg(location.register64.reghi));
  1556. rv.intregvars.addnodup(getsupreg(GetNextReg(location.register64.reghi)));
  1557. rv.intregvars.addnodup(getsupreg(GetNextReg(GetNextReg(location.register64.reghi))));
  1558. rv.intregvars.addnodup(getsupreg(GetNextReg(GetNextReg(GetNextReg(location.register64.reghi)))));
  1559. end
  1560. else
  1561. if location.size in [OS_32,OS_S32] then
  1562. begin
  1563. rv.intregvars.addnodup(getsupreg(location.register));
  1564. rv.intregvars.addnodup(getsupreg(GetNextReg(location.register)));
  1565. rv.intregvars.addnodup(getsupreg(GetNextReg(GetNextReg(location.register))));
  1566. rv.intregvars.addnodup(getsupreg(GetNextReg(GetNextReg(GetNextReg(location.register)))));
  1567. end
  1568. else
  1569. if location.size in [OS_16,OS_S16] then
  1570. begin
  1571. rv.intregvars.addnodup(getsupreg(location.register));
  1572. rv.intregvars.addnodup(getsupreg(GetNextReg(location.register)));
  1573. end
  1574. else
  1575. {$endif}
  1576. rv.intregvars.addnodup(getsupreg(location.register));
  1577. LOC_CFPUREGISTER:
  1578. rv.fpuregvars.addnodup(getsupreg(location.register));
  1579. LOC_CMMREGISTER:
  1580. rv.mmregvars.addnodup(getsupreg(location.register));
  1581. end;
  1582. end;
  1583. function do_get_used_regvars(var n: tnode; arg: pointer): foreachnoderesult;
  1584. var
  1585. rv: pusedregvars absolute arg;
  1586. begin
  1587. case (n.nodetype) of
  1588. temprefn:
  1589. { We only have to synchronise a tempnode before a loop if it is }
  1590. { not created inside the loop, and only synchronise after the }
  1591. { loop if it's not destroyed inside the loop. If it's created }
  1592. { before the loop and not yet destroyed, then before the loop }
  1593. { is secondpassed tempinfo^.valid will be true, and we get the }
  1594. { correct registers. If it's not destroyed inside the loop, }
  1595. { then after the loop has been secondpassed tempinfo^.valid }
  1596. { be true and we also get the right registers. In other cases, }
  1597. { tempinfo^.valid will be false and so we do not add }
  1598. { unnecessary registers. This way, we don't have to look at }
  1599. { tempcreate and tempdestroy nodes to get this info (JM) }
  1600. if (ti_valid in ttemprefnode(n).tempinfo^.flags) then
  1601. add_regvars(rv^,ttemprefnode(n).tempinfo^.location);
  1602. loadn:
  1603. if (tloadnode(n).symtableentry.typ in [staticvarsym,localvarsym,paravarsym]) then
  1604. add_regvars(rv^,tabstractnormalvarsym(tloadnode(n).symtableentry).localloc);
  1605. vecn:
  1606. { range checks sometimes need the high parameter }
  1607. if (cs_check_range in current_settings.localswitches) and
  1608. (is_open_array(tvecnode(n).left.resultdef) or
  1609. is_array_of_const(tvecnode(n).left.resultdef)) and
  1610. not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  1611. add_regvars(rv^,tabstractnormalvarsym(get_high_value_sym(tparavarsym(tloadnode(tvecnode(n).left).symtableentry))).localloc)
  1612. end;
  1613. result := fen_true;
  1614. end;
  1615. procedure get_used_regvars(n: tnode; var rv: tusedregvars);
  1616. begin
  1617. foreachnodestatic(n,@do_get_used_regvars,@rv);
  1618. end;
  1619. (*
  1620. See comments at declaration of pusedregvarscommon
  1621. function do_get_used_regvars_common(var n: tnode; arg: pointer): foreachnoderesult;
  1622. var
  1623. rv: pusedregvarscommon absolute arg;
  1624. begin
  1625. if (n.nodetype = loadn) and
  1626. (tloadnode(n).symtableentry.typ in [staticvarsym,localvarsym,paravarsym]) then
  1627. with tabstractnormalvarsym(tloadnode(n).symtableentry).localloc do
  1628. case loc of
  1629. LOC_CREGISTER:
  1630. { if not yet encountered in this node tree }
  1631. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  1632. { but nevertheless already encountered somewhere }
  1633. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  1634. { then it's a regvar used in two or more node trees }
  1635. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  1636. LOC_CFPUREGISTER:
  1637. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  1638. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  1639. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  1640. LOC_CMMREGISTER:
  1641. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  1642. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  1643. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  1644. end;
  1645. result := fen_true;
  1646. end;
  1647. procedure get_used_regvars_common(n: tnode; var rv: tusedregvarscommon);
  1648. begin
  1649. rv.myregvars.intregvars.clear;
  1650. rv.myregvars.fpuregvars.clear;
  1651. rv.myregvars.mmregvars.clear;
  1652. foreachnodestatic(n,@do_get_used_regvars_common,@rv);
  1653. end;
  1654. *)
  1655. procedure gen_sync_regvars(list:TAsmList; var rv: tusedregvars);
  1656. var
  1657. count: longint;
  1658. begin
  1659. for count := 1 to rv.intregvars.length do
  1660. cg.a_reg_sync(list,newreg(R_INTREGISTER,rv.intregvars.readidx(count-1),R_SUBWHOLE));
  1661. for count := 1 to rv.fpuregvars.length do
  1662. cg.a_reg_sync(list,newreg(R_FPUREGISTER,rv.fpuregvars.readidx(count-1),R_SUBWHOLE));
  1663. for count := 1 to rv.mmregvars.length do
  1664. cg.a_reg_sync(list,newreg(R_MMREGISTER,rv.mmregvars.readidx(count-1),R_SUBWHOLE));
  1665. end;
  1666. {*****************************************************************************
  1667. SSA support
  1668. *****************************************************************************}
  1669. type
  1670. preplaceregrec = ^treplaceregrec;
  1671. treplaceregrec = record
  1672. old, new: tregister;
  1673. oldhi, newhi: tregister;
  1674. ressym: tsym;
  1675. { moved sym }
  1676. sym : tabstractnormalvarsym;
  1677. end;
  1678. function doreplace(var n: tnode; para: pointer): foreachnoderesult;
  1679. var
  1680. rr: preplaceregrec absolute para;
  1681. begin
  1682. result := fen_false;
  1683. if (nf_is_funcret in n.flags) and (fc_exit in flowcontrol) then
  1684. exit;
  1685. case n.nodetype of
  1686. loadn:
  1687. begin
  1688. if (tloadnode(n).symtableentry.typ in [localvarsym,paravarsym,staticvarsym]) and
  1689. (tabstractvarsym(tloadnode(n).symtableentry).varoptions * [vo_is_dll_var, vo_is_thread_var] = []) and
  1690. not assigned(tloadnode(n).left) and
  1691. ((tloadnode(n).symtableentry <> rr^.ressym) or
  1692. not(fc_exit in flowcontrol)
  1693. ) and
  1694. (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) and
  1695. (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register = rr^.old) then
  1696. begin
  1697. {$ifdef cpu64bitalu}
  1698. { it's possible a 128 bit location was shifted and/xor typecasted }
  1699. { in a 64 bit value, so only 1 register was left in the location }
  1700. if (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.size in [OS_128,OS_S128]) then
  1701. if (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register128.reghi = rr^.oldhi) then
  1702. tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register128.reghi := rr^.newhi
  1703. else
  1704. exit;
  1705. {$else cpu64bitalu}
  1706. { it's possible a 64 bit location was shifted and/xor typecasted }
  1707. { in a 32 bit value, so only 1 register was left in the location }
  1708. if (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.size in [OS_64,OS_S64]) then
  1709. if (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register64.reghi = rr^.oldhi) then
  1710. tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register64.reghi := rr^.newhi
  1711. else
  1712. exit;
  1713. {$endif cpu64bitalu}
  1714. tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register := rr^.new;
  1715. rr^.sym := tabstractnormalvarsym(tloadnode(n).symtableentry);
  1716. result := fen_norecurse_true;
  1717. end;
  1718. end;
  1719. temprefn:
  1720. begin
  1721. if (ti_valid in ttemprefnode(n).tempinfo^.flags) and
  1722. (ttemprefnode(n).tempinfo^.location.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) and
  1723. (ttemprefnode(n).tempinfo^.location.register = rr^.old) then
  1724. begin
  1725. {$ifdef cpu64bitalu}
  1726. { it's possible a 128 bit location was shifted and/xor typecasted }
  1727. { in a 64 bit value, so only 1 register was left in the location }
  1728. if (ttemprefnode(n).tempinfo^.location.size in [OS_128,OS_S128]) then
  1729. if (ttemprefnode(n).tempinfo^.location.register128.reghi = rr^.oldhi) then
  1730. ttemprefnode(n).tempinfo^.location.register128.reghi := rr^.newhi
  1731. else
  1732. exit;
  1733. {$else cpu64bitalu}
  1734. { it's possible a 64 bit location was shifted and/xor typecasted }
  1735. { in a 32 bit value, so only 1 register was left in the location }
  1736. if (ttemprefnode(n).tempinfo^.location.size in [OS_64,OS_S64]) then
  1737. if (ttemprefnode(n).tempinfo^.location.register64.reghi = rr^.oldhi) then
  1738. ttemprefnode(n).tempinfo^.location.register64.reghi := rr^.newhi
  1739. else
  1740. exit;
  1741. {$endif cpu64bitalu}
  1742. ttemprefnode(n).tempinfo^.location.register := rr^.new;
  1743. result := fen_norecurse_true;
  1744. end;
  1745. end;
  1746. { optimize the searching a bit }
  1747. derefn,addrn,
  1748. calln,inlinen,casen,
  1749. addn,subn,muln,
  1750. andn,orn,xorn,
  1751. ltn,lten,gtn,gten,equaln,unequaln,
  1752. slashn,divn,shrn,shln,notn,
  1753. inn,
  1754. asn,isn:
  1755. result := fen_norecurse_false;
  1756. end;
  1757. end;
  1758. procedure maybechangeloadnodereg(list: TAsmList; var n: tnode; reload: boolean);
  1759. var
  1760. rr: treplaceregrec;
  1761. varloc : tai_varloc;
  1762. begin
  1763. {$ifdef jvm}
  1764. exit;
  1765. {$endif}
  1766. if not (n.location.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) or
  1767. ([fc_inflowcontrol,fc_gotolabel,fc_lefthandled] * flowcontrol <> []) then
  1768. exit;
  1769. rr.old := n.location.register;
  1770. rr.ressym := nil;
  1771. rr.sym := nil;
  1772. rr.oldhi := NR_NO;
  1773. case n.location.loc of
  1774. LOC_CREGISTER:
  1775. begin
  1776. {$ifdef cpu64bitalu}
  1777. if (n.location.size in [OS_128,OS_S128]) then
  1778. begin
  1779. rr.oldhi := n.location.register128.reghi;
  1780. rr.new := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  1781. rr.newhi := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  1782. end
  1783. else
  1784. {$else cpu64bitalu}
  1785. if (n.location.size in [OS_64,OS_S64]) then
  1786. begin
  1787. rr.oldhi := n.location.register64.reghi;
  1788. rr.new := cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  1789. rr.newhi := cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  1790. end
  1791. else
  1792. {$endif cpu64bitalu}
  1793. rr.new := cg.getintregister(current_asmdata.CurrAsmList,n.location.size);
  1794. end;
  1795. LOC_CFPUREGISTER:
  1796. rr.new := cg.getfpuregister(current_asmdata.CurrAsmList,n.location.size);
  1797. {$ifdef SUPPORT_MMX}
  1798. LOC_CMMXREGISTER:
  1799. rr.new := tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  1800. {$endif SUPPORT_MMX}
  1801. LOC_CMMREGISTER:
  1802. rr.new := cg.getmmregister(current_asmdata.CurrAsmList,n.location.size);
  1803. else
  1804. exit;
  1805. end;
  1806. if not is_void(current_procinfo.procdef.returndef) and
  1807. assigned(current_procinfo.procdef.funcretsym) and
  1808. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs <> 0) then
  1809. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  1810. rr.ressym:=tsym(current_procinfo.procdef.parast.Find('self'))
  1811. else
  1812. rr.ressym:=current_procinfo.procdef.funcretsym;
  1813. if not foreachnodestatic(n,@doreplace,@rr) then
  1814. exit;
  1815. if reload then
  1816. case n.location.loc of
  1817. LOC_CREGISTER:
  1818. begin
  1819. {$ifdef cpu64bitalu}
  1820. if (n.location.size in [OS_128,OS_S128]) then
  1821. cg128.a_load128_reg_reg(list,n.location.register128,joinreg128(rr.new,rr.newhi))
  1822. else
  1823. {$else cpu64bitalu}
  1824. if (n.location.size in [OS_64,OS_S64]) then
  1825. cg64.a_load64_reg_reg(list,n.location.register64,joinreg64(rr.new,rr.newhi))
  1826. else
  1827. {$endif cpu64bitalu}
  1828. cg.a_load_reg_reg(list,n.location.size,n.location.size,n.location.register,rr.new);
  1829. end;
  1830. LOC_CFPUREGISTER:
  1831. cg.a_loadfpu_reg_reg(list,n.location.size,n.location.size,n.location.register,rr.new);
  1832. {$ifdef SUPPORT_MMX}
  1833. LOC_CMMXREGISTER:
  1834. cg.a_loadmm_reg_reg(list,OS_M64,OS_M64,n.location.register,rr.new,nil);
  1835. {$endif SUPPORT_MMX}
  1836. LOC_CMMREGISTER:
  1837. cg.a_loadmm_reg_reg(list,n.location.size,n.location.size,n.location.register,rr.new,nil);
  1838. else
  1839. internalerror(2006090920);
  1840. end;
  1841. { now that we've change the loadn/temp, also change the node result location }
  1842. {$ifdef cpu64bitalu}
  1843. if (n.location.size in [OS_128,OS_S128]) then
  1844. begin
  1845. n.location.register128.reglo := rr.new;
  1846. n.location.register128.reghi := rr.newhi;
  1847. if assigned(rr.sym) and
  1848. ((rr.sym.currentregloc.register<>rr.new) or
  1849. (rr.sym.currentregloc.registerhi<>rr.newhi)) then
  1850. begin
  1851. varloc:=tai_varloc.create128(rr.sym,rr.new,rr.newhi);
  1852. varloc.oldlocation:=rr.sym.currentregloc.register;
  1853. varloc.oldlocationhi:=rr.sym.currentregloc.registerhi;
  1854. rr.sym.currentregloc.register:=rr.new;
  1855. rr.sym.currentregloc.registerHI:=rr.newhi;
  1856. list.concat(varloc);
  1857. end;
  1858. end
  1859. else
  1860. {$else cpu64bitalu}
  1861. if (n.location.size in [OS_64,OS_S64]) then
  1862. begin
  1863. n.location.register64.reglo := rr.new;
  1864. n.location.register64.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.create64(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. {$endif cpu64bitalu}
  1879. begin
  1880. n.location.register := rr.new;
  1881. if assigned(rr.sym) and (rr.sym.currentregloc.register<>rr.new) then
  1882. begin
  1883. varloc:=tai_varloc.create(rr.sym,rr.new);
  1884. varloc.oldlocation:=rr.sym.currentregloc.register;
  1885. rr.sym.currentregloc.register:=rr.new;
  1886. list.concat(varloc);
  1887. end;
  1888. end;
  1889. end;
  1890. procedure gen_free_symtable(list:TAsmList;st:TSymtable);
  1891. var
  1892. i : longint;
  1893. sym : tsym;
  1894. begin
  1895. for i:=0 to st.SymList.Count-1 do
  1896. begin
  1897. sym:=tsym(st.SymList[i]);
  1898. if (sym.typ in [staticvarsym,localvarsym,paravarsym]) then
  1899. begin
  1900. with tabstractnormalvarsym(sym) do
  1901. begin
  1902. { Note: We need to keep the data available in memory
  1903. for the sub procedures that can access local data
  1904. in the parent procedures }
  1905. case localloc.loc of
  1906. LOC_CREGISTER :
  1907. if (pi_has_label in current_procinfo.flags) then
  1908. {$if defined(cpu64bitalu)}
  1909. if def_cgsize(vardef) in [OS_128,OS_S128] then
  1910. begin
  1911. cg.a_reg_sync(list,localloc.register128.reglo);
  1912. cg.a_reg_sync(list,localloc.register128.reghi);
  1913. end
  1914. else
  1915. {$elseif defined(cpu32bitalu)}
  1916. if def_cgsize(vardef) in [OS_64,OS_S64] then
  1917. begin
  1918. cg.a_reg_sync(list,localloc.register64.reglo);
  1919. cg.a_reg_sync(list,localloc.register64.reghi);
  1920. end
  1921. else
  1922. {$elseif defined(cpu16bitalu)}
  1923. if def_cgsize(vardef) in [OS_64,OS_S64] then
  1924. begin
  1925. cg.a_reg_sync(list,localloc.register64.reglo);
  1926. cg.a_reg_sync(list,GetNextReg(localloc.register64.reglo));
  1927. cg.a_reg_sync(list,localloc.register64.reghi);
  1928. cg.a_reg_sync(list,GetNextReg(localloc.register64.reghi));
  1929. end
  1930. else
  1931. if def_cgsize(vardef) in [OS_32,OS_S32] then
  1932. begin
  1933. cg.a_reg_sync(list,localloc.register);
  1934. cg.a_reg_sync(list,GetNextReg(localloc.register));
  1935. end
  1936. else
  1937. {$elseif defined(cpu8bitalu)}
  1938. if def_cgsize(vardef) in [OS_64,OS_S64] then
  1939. begin
  1940. cg.a_reg_sync(list,localloc.register64.reglo);
  1941. cg.a_reg_sync(list,GetNextReg(localloc.register64.reglo));
  1942. cg.a_reg_sync(list,GetNextReg(GetNextReg(localloc.register64.reglo)));
  1943. cg.a_reg_sync(list,GetNextReg(GetNextReg(GetNextReg(localloc.register64.reglo))));
  1944. cg.a_reg_sync(list,localloc.register64.reghi);
  1945. cg.a_reg_sync(list,GetNextReg(localloc.register64.reghi));
  1946. cg.a_reg_sync(list,GetNextReg(GetNextReg(localloc.register64.reghi)));
  1947. cg.a_reg_sync(list,GetNextReg(GetNextReg(GetNextReg(localloc.register64.reghi))));
  1948. end
  1949. else
  1950. if def_cgsize(vardef) in [OS_32,OS_S32] then
  1951. begin
  1952. cg.a_reg_sync(list,localloc.register);
  1953. cg.a_reg_sync(list,GetNextReg(localloc.register));
  1954. cg.a_reg_sync(list,GetNextReg(GetNextReg(localloc.register)));
  1955. cg.a_reg_sync(list,GetNextReg(GetNextReg(GetNextReg(localloc.register))));
  1956. end
  1957. else
  1958. if def_cgsize(vardef) in [OS_16,OS_S16] then
  1959. begin
  1960. cg.a_reg_sync(list,localloc.register);
  1961. cg.a_reg_sync(list,GetNextReg(localloc.register));
  1962. end
  1963. else
  1964. {$endif}
  1965. cg.a_reg_sync(list,localloc.register);
  1966. LOC_CFPUREGISTER,
  1967. LOC_CMMREGISTER:
  1968. if (pi_has_label in current_procinfo.flags) then
  1969. cg.a_reg_sync(list,localloc.register);
  1970. LOC_REFERENCE :
  1971. begin
  1972. if typ in [localvarsym,paravarsym] then
  1973. tg.Ungetlocal(list,localloc.reference);
  1974. end;
  1975. end;
  1976. end;
  1977. end;
  1978. end;
  1979. end;
  1980. procedure gen_load_vmt_register(list:TAsmList;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  1981. var
  1982. href : treference;
  1983. selfdef: tdef;
  1984. begin
  1985. if is_object(objdef) then
  1986. begin
  1987. case selfloc.loc of
  1988. LOC_CREFERENCE,
  1989. LOC_REFERENCE:
  1990. begin
  1991. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset,sizeof(pint));
  1992. cg.a_loadaddr_ref_reg(list,selfloc.reference,href.base);
  1993. selfdef:=getpointerdef(objdef);
  1994. end;
  1995. else
  1996. internalerror(200305056);
  1997. end;
  1998. end
  1999. else
  2000. { This is also valid for Objective-C classes: vmt_offset is 0 there,
  2001. and the first "field" of an Objective-C class instance is a pointer
  2002. to its "meta-class". }
  2003. begin
  2004. selfdef:=objdef;
  2005. case selfloc.loc of
  2006. LOC_REGISTER:
  2007. begin
  2008. {$ifdef cpu_uses_separate_address_registers}
  2009. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  2010. begin
  2011. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset,sizeof(pint));
  2012. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,selfloc.register,href.base);
  2013. end
  2014. else
  2015. {$endif cpu_uses_separate_address_registers}
  2016. reference_reset_base(href,selfloc.register,objdef.vmt_offset,sizeof(pint));
  2017. end;
  2018. LOC_CONSTANT,
  2019. LOC_CREGISTER,
  2020. LOC_CREFERENCE,
  2021. LOC_REFERENCE,
  2022. LOC_CSUBSETREG,
  2023. LOC_SUBSETREG,
  2024. LOC_CSUBSETREF,
  2025. LOC_SUBSETREF:
  2026. begin
  2027. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset,sizeof(pint));
  2028. { todo: pass actual vmt pointer type to hlcg }
  2029. hlcg.a_load_loc_reg(list,voidpointertype,voidpointertype,selfloc,href.base);
  2030. end;
  2031. else
  2032. internalerror(200305057);
  2033. end;
  2034. end;
  2035. vmtreg:=cg.getaddressregister(list);
  2036. hlcg.g_maybe_testself(list,selfdef,href.base);
  2037. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,vmtreg);
  2038. { test validity of VMT }
  2039. if not(is_interface(objdef)) and
  2040. not(is_cppclass(objdef)) and
  2041. not(is_objc_class_or_protocol(objdef)) then
  2042. cg.g_maybe_testvmt(list,vmtreg,objdef);
  2043. end;
  2044. function getprocalign : shortint;
  2045. begin
  2046. { gprof uses 16 byte granularity }
  2047. if (cs_profile in current_settings.moduleswitches) then
  2048. result:=16
  2049. else
  2050. result:=current_settings.alignment.procalign;
  2051. end;
  2052. procedure gen_fpc_dummy(list : TAsmList);
  2053. begin
  2054. {$ifdef i386}
  2055. { fix me! }
  2056. list.concat(Taicpu.Op_const_reg(A_MOV,S_L,1,NR_EAX));
  2057. list.concat(Taicpu.Op_const(A_RET,S_W,12));
  2058. {$endif i386}
  2059. end;
  2060. procedure gen_load_frame_for_exceptfilter(list : TAsmList);
  2061. var
  2062. para: tparavarsym;
  2063. begin
  2064. para:=tparavarsym(current_procinfo.procdef.paras[0]);
  2065. if not (vo_is_parentfp in para.varoptions) then
  2066. InternalError(201201142);
  2067. if (para.paraloc[calleeside].location^.loc<>LOC_REGISTER) or
  2068. (para.paraloc[calleeside].location^.next<>nil) then
  2069. InternalError(201201143);
  2070. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,para.paraloc[calleeside].location^.register,
  2071. NR_FRAME_POINTER_REG);
  2072. end;
  2073. end.