ncgutil.pas 88 KB

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