ncgutil.pas 83 KB

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