ncgutil.pas 80 KB

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