ncgutil.pas 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491
  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 cpu64bit}
  27. ,cg64f32
  28. {$endif cpu64bit}
  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_reg(list:TAsmList;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  50. procedure location_force_fpureg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  51. procedure location_force_mem(list:TAsmList;var l:tlocation);
  52. procedure location_force_mmregscalar(list:TAsmList;var l: tlocation;maybeconst:boolean);
  53. { Retrieve the location of the data pointed to in location l, when the location is
  54. a register it is expected to contain the address of the data }
  55. procedure location_get_data_ref(list:TAsmList;const l:tlocation;var ref:treference;loadref:boolean);
  56. function maybe_pushfpu(list:TAsmList;needed : byte;var l:tlocation) : boolean;
  57. procedure alloc_proc_symbol(pd: tprocdef);
  58. procedure gen_proc_symbol(list:TAsmList);
  59. procedure gen_proc_symbol_end(list:TAsmList);
  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_initialize_code(list:TAsmList);
  67. procedure gen_finalize_code(list:TAsmList);
  68. procedure gen_entry_code(list:TAsmList);
  69. procedure gen_exit_code(list:TAsmList);
  70. procedure gen_load_para_value(list:TAsmList);
  71. procedure gen_load_return_value(list:TAsmList);
  72. procedure gen_external_stub(list:TAsmList;pd:tprocdef;const externalname:string);
  73. procedure gen_intf_wrappers(list:TAsmList;st:tsymtable);
  74. procedure gen_load_vmt_register(list:TAsmList;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  75. procedure get_used_regvars(n: tnode; var rv: tusedregvars);
  76. { adds the regvars used in n and its children to rv.allregvars,
  77. those which were already in rv.allregvars to rv.commonregvars and
  78. uses rv.myregvars as scratch (so that two uses of the same regvar
  79. in a single tree to make it appear in commonregvars). Useful to
  80. find out which regvars are used in two different node trees
  81. (e.g. in the "else" and "then" path, or in various case blocks }
  82. // procedure get_used_regvars_common(n: tnode; var rv: tusedregvarscommon);
  83. procedure gen_sync_regvars(list:TAsmList; var rv: tusedregvars);
  84. {#
  85. Allocate the buffers for exception management and setjmp environment.
  86. Return a pointer to these buffers, send them to the utility routine
  87. so they are registered, and then call setjmp.
  88. Then compare the result of setjmp with 0, and if not equal
  89. to zero, then jump to exceptlabel.
  90. Also store the result of setjmp to a temporary space by calling g_save_exception_reason
  91. It is to note that this routine may be called *after* the stackframe of a
  92. routine has been called, therefore on machines where the stack cannot
  93. be modified, all temps should be allocated on the heap instead of the
  94. stack.
  95. }
  96. const
  97. EXCEPT_BUF_SIZE = 3*sizeof(aint);
  98. type
  99. texceptiontemps=record
  100. jmpbuf,
  101. envbuf,
  102. reasonbuf : treference;
  103. end;
  104. procedure get_exception_temps(list:TAsmList;var t:texceptiontemps);
  105. procedure unget_exception_temps(list:TAsmList;const t:texceptiontemps);
  106. procedure new_exception(list:TAsmList;const t:texceptiontemps;exceptlabel:tasmlabel);
  107. procedure free_exception(list:TAsmList;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  108. procedure insertbssdata(sym : tglobalvarsym);
  109. procedure gen_alloc_symtable(list:TAsmList;st:tsymtable);
  110. procedure gen_free_symtable(list:TAsmList;st:tsymtable);
  111. { rtti and init/final }
  112. procedure generate_rtti(p:Ttypesym);
  113. procedure generate_inittable(p:tsym);
  114. procedure location_free(list: TAsmList; const location : TLocation);
  115. function getprocalign : shortint;
  116. procedure gen_pic_helpers(list : TAsmList);
  117. procedure gen_got_load(list : TAsmList);
  118. implementation
  119. uses
  120. version,
  121. cutils,cclasses,
  122. globals,systems,verbose,
  123. ppu,defutil,
  124. procinfo,paramgr,fmodule,
  125. regvars,dbgbase,
  126. pass_1,pass_2,
  127. nbas,ncon,nld,nutils,
  128. tgobj,cgobj
  129. {$ifdef powerpc}
  130. , cpupi
  131. {$endif}
  132. {$ifdef powerpc64}
  133. , cpupi
  134. {$endif}
  135. ;
  136. {*****************************************************************************
  137. Misc Helpers
  138. *****************************************************************************}
  139. procedure location_free(list: TAsmList; const location : TLocation);
  140. begin
  141. case location.loc of
  142. LOC_VOID:
  143. ;
  144. LOC_REGISTER,
  145. LOC_CREGISTER:
  146. begin
  147. if getsupreg(location.register)<first_int_imreg then
  148. cg.ungetcpuregister(list,location.register);
  149. end;
  150. LOC_FPUREGISTER,
  151. LOC_CFPUREGISTER:
  152. begin
  153. if getsupreg(location.register)<first_fpu_imreg then
  154. cg.ungetcpuregister(list,location.register);
  155. end;
  156. LOC_MMREGISTER,
  157. LOC_CMMREGISTER :
  158. begin
  159. if getsupreg(location.register)<first_mm_imreg then
  160. cg.ungetcpuregister(list,location.register);
  161. end;
  162. LOC_REFERENCE,
  163. LOC_CREFERENCE :
  164. begin
  165. if use_fixed_stack then
  166. location_freetemp(list,location);
  167. end;
  168. else
  169. internalerror(2004110211);
  170. end;
  171. end;
  172. { DO NOT RELY on the fact that the tnode is not yet swaped
  173. because of inlining code PM }
  174. procedure firstcomplex(p : tbinarynode);
  175. var
  176. hp : tnode;
  177. begin
  178. { always calculate boolean AND and OR from left to right }
  179. if (p.nodetype in [orn,andn]) and
  180. is_boolean(p.left.resulttype.def) then
  181. begin
  182. if nf_swaped in p.flags then
  183. internalerror(234234);
  184. end
  185. else
  186. if (
  187. (p.expectloc=LOC_FPUREGISTER) and
  188. (p.right.registersfpu > p.left.registersfpu)
  189. ) or
  190. (
  191. (
  192. (
  193. ((p.left.registersfpu = 0) and (p.right.registersfpu = 0)) or
  194. (p.expectloc<>LOC_FPUREGISTER)
  195. ) and
  196. (p.left.registersint<p.right.registersint)
  197. )
  198. ) then
  199. begin
  200. hp:=p.left;
  201. p.left:=p.right;
  202. p.right:=hp;
  203. if nf_swaped in p.flags then
  204. exclude(p.flags,nf_swaped)
  205. else
  206. include(p.flags,nf_swaped);
  207. end;
  208. end;
  209. procedure maketojumpbool(list:TAsmList; p : tnode; loadregvars: tloadregvars);
  210. {
  211. produces jumps to true respectively false labels using boolean expressions
  212. depending on whether the loading of regvars is currently being
  213. synchronized manually (such as in an if-node) or automatically (most of
  214. the other cases where this procedure is called), loadregvars can be
  215. "lr_load_regvars" or "lr_dont_load_regvars"
  216. }
  217. var
  218. opsize : tcgsize;
  219. storepos : tfileposinfo;
  220. begin
  221. if nf_error in p.flags then
  222. exit;
  223. storepos:=aktfilepos;
  224. aktfilepos:=p.fileinfo;
  225. if is_boolean(p.resulttype.def) then
  226. begin
  227. {$ifdef OLDREGVARS}
  228. if loadregvars = lr_load_regvars then
  229. load_all_regvars(list);
  230. {$endif OLDREGVARS}
  231. if is_constboolnode(p) then
  232. begin
  233. if tordconstnode(p).value<>0 then
  234. cg.a_jmp_always(list,current_procinfo.CurrTrueLabel)
  235. else
  236. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel)
  237. end
  238. else
  239. begin
  240. opsize:=def_cgsize(p.resulttype.def);
  241. case p.location.loc of
  242. LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE :
  243. begin
  244. {$ifdef OLDREGVARS}
  245. if (p.location.loc = LOC_CREGISTER) then
  246. load_regvar_reg(list,p.location.register);
  247. {$endif OLDREGVARS}
  248. cg.a_cmp_const_loc_label(list,opsize,OC_NE,0,p.location,current_procinfo.CurrTrueLabel);
  249. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel);
  250. end;
  251. LOC_JUMP:
  252. ;
  253. {$ifdef cpuflags}
  254. LOC_FLAGS :
  255. begin
  256. cg.a_jmp_flags(list,p.location.resflags,current_procinfo.CurrTrueLabel);
  257. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel);
  258. end;
  259. {$endif cpuflags}
  260. else
  261. begin
  262. printnode(output,p);
  263. internalerror(200308241);
  264. end;
  265. end;
  266. end;
  267. end
  268. else
  269. internalerror(200112305);
  270. aktfilepos:=storepos;
  271. end;
  272. (*
  273. This code needs fixing. It is not safe to use rgint; on the m68000 it
  274. would be rgaddr.
  275. procedure remove_non_regvars_from_loc(const t: tlocation; var regs:Tsuperregisterset);
  276. begin
  277. case t.loc of
  278. LOC_REGISTER:
  279. begin
  280. { can't be a regvar, since it would be LOC_CREGISTER then }
  281. exclude(regs,getsupreg(t.register));
  282. if t.register64.reghi<>NR_NO then
  283. exclude(regs,getsupreg(t.register64.reghi));
  284. end;
  285. LOC_CREFERENCE,LOC_REFERENCE:
  286. begin
  287. if not(cs_opt_regvar in aktoptimizerswitches) or
  288. (getsupreg(t.reference.base) in cg.rgint.usableregs) then
  289. exclude(regs,getsupreg(t.reference.base));
  290. if not(cs_opt_regvar in aktoptimizerswitches) or
  291. (getsupreg(t.reference.index) in cg.rgint.usableregs) then
  292. exclude(regs,getsupreg(t.reference.index));
  293. end;
  294. end;
  295. end;
  296. *)
  297. {*****************************************************************************
  298. EXCEPTION MANAGEMENT
  299. *****************************************************************************}
  300. procedure get_exception_temps(list:TAsmList;var t:texceptiontemps);
  301. var
  302. srsym : ttypesym;
  303. begin
  304. if jmp_buf_size=-1 then
  305. begin
  306. srsym:=search_system_type('JMP_BUF');
  307. jmp_buf_size:=srsym.restype.def.size;
  308. end;
  309. tg.GetTemp(list,EXCEPT_BUF_SIZE,tt_persistent,t.envbuf);
  310. tg.GetTemp(list,jmp_buf_size,tt_persistent,t.jmpbuf);
  311. tg.GetTemp(list,sizeof(aint),tt_persistent,t.reasonbuf);
  312. end;
  313. procedure unget_exception_temps(list:TAsmList;const t:texceptiontemps);
  314. begin
  315. tg.Ungettemp(list,t.jmpbuf);
  316. tg.ungettemp(list,t.envbuf);
  317. tg.ungettemp(list,t.reasonbuf);
  318. end;
  319. procedure new_exception(list:TAsmList;const t:texceptiontemps;exceptlabel:tasmlabel);
  320. var
  321. paraloc1,paraloc2,paraloc3 : tcgpara;
  322. begin
  323. paraloc1.init;
  324. paraloc2.init;
  325. paraloc3.init;
  326. paramanager.getintparaloc(pocall_default,1,paraloc1);
  327. paramanager.getintparaloc(pocall_default,2,paraloc2);
  328. paramanager.getintparaloc(pocall_default,3,paraloc3);
  329. paramanager.allocparaloc(list,paraloc3);
  330. cg.a_paramaddr_ref(list,t.envbuf,paraloc3);
  331. paramanager.allocparaloc(list,paraloc2);
  332. cg.a_paramaddr_ref(list,t.jmpbuf,paraloc2);
  333. { push type of exceptionframe }
  334. paramanager.allocparaloc(list,paraloc1);
  335. cg.a_param_const(list,OS_S32,1,paraloc1);
  336. paramanager.freeparaloc(list,paraloc3);
  337. paramanager.freeparaloc(list,paraloc2);
  338. paramanager.freeparaloc(list,paraloc1);
  339. cg.allocallcpuregisters(list);
  340. cg.a_call_name(list,'FPC_PUSHEXCEPTADDR');
  341. cg.deallocallcpuregisters(list);
  342. paramanager.getintparaloc(pocall_default,1,paraloc1);
  343. paramanager.allocparaloc(list,paraloc1);
  344. cg.a_param_reg(list,OS_ADDR,NR_FUNCTION_RESULT_REG,paraloc1);
  345. paramanager.freeparaloc(list,paraloc1);
  346. cg.allocallcpuregisters(list);
  347. cg.a_call_name(list,'FPC_SETJMP');
  348. cg.deallocallcpuregisters(list);
  349. cg.g_exception_reason_save(list, t.reasonbuf);
  350. cg.a_cmp_const_reg_label(list,OS_S32,OC_NE,0,cg.makeregsize(list,NR_FUNCTION_RESULT_REG,OS_S32),exceptlabel);
  351. paraloc1.done;
  352. paraloc2.done;
  353. paraloc3.done;
  354. end;
  355. procedure free_exception(list:TAsmList;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  356. begin
  357. cg.allocallcpuregisters(list);
  358. cg.a_call_name(list,'FPC_POPADDRSTACK');
  359. cg.deallocallcpuregisters(list);
  360. if not onlyfree then
  361. begin
  362. cg.g_exception_reason_load(list, t.reasonbuf);
  363. cg.a_cmp_const_reg_label(list,OS_INT,OC_EQ,a,NR_FUNCTION_RESULT_REG,endexceptlabel);
  364. end;
  365. end;
  366. {*****************************************************************************
  367. TLocation
  368. *****************************************************************************}
  369. {$ifndef cpu64bit}
  370. { 32-bit version }
  371. procedure location_force_reg(list:TAsmList;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  372. var
  373. hregister,
  374. hregisterhi : tregister;
  375. hreg64 : tregister64;
  376. hl : tasmlabel;
  377. oldloc : tlocation;
  378. const_location: boolean;
  379. begin
  380. oldloc:=l;
  381. if dst_size=OS_NO then
  382. internalerror(200309144);
  383. { handle transformations to 64bit separate }
  384. if dst_size in [OS_64,OS_S64] then
  385. begin
  386. if not (l.size in [OS_64,OS_S64]) then
  387. begin
  388. { load a smaller size to OS_64 }
  389. if l.loc=LOC_REGISTER then
  390. begin
  391. hregister:=cg.makeregsize(list,l.register64.reglo,OS_32);
  392. cg.a_load_reg_reg(list,l.size,OS_32,l.register64.reglo,hregister);
  393. end
  394. else
  395. hregister:=cg.getintregister(list,OS_INT);
  396. { load value in low register }
  397. case l.loc of
  398. LOC_FLAGS :
  399. cg.g_flags2reg(list,OS_INT,l.resflags,hregister);
  400. LOC_JUMP :
  401. begin
  402. cg.a_label(list,current_procinfo.CurrTrueLabel);
  403. cg.a_load_const_reg(list,OS_INT,1,hregister);
  404. current_asmdata.getjumplabel(hl);
  405. cg.a_jmp_always(list,hl);
  406. cg.a_label(list,current_procinfo.CurrFalseLabel);
  407. cg.a_load_const_reg(list,OS_INT,0,hregister);
  408. cg.a_label(list,hl);
  409. end;
  410. else
  411. cg.a_load_loc_reg(list,OS_INT,l,hregister);
  412. end;
  413. { reset hi part, take care of the signed bit of the current value }
  414. hregisterhi:=cg.getintregister(list,OS_INT);
  415. if (l.size in [OS_S8,OS_S16,OS_S32]) then
  416. begin
  417. if l.loc=LOC_CONSTANT then
  418. begin
  419. if (longint(l.value)<0) then
  420. cg.a_load_const_reg(list,OS_32,aint($ffffffff),hregisterhi)
  421. else
  422. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  423. end
  424. else
  425. begin
  426. cg.a_op_const_reg_reg(list,OP_SAR,OS_32,31,hregister,
  427. hregisterhi);
  428. end;
  429. end
  430. else
  431. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  432. location_reset(l,LOC_REGISTER,dst_size);
  433. l.register64.reglo:=hregister;
  434. l.register64.reghi:=hregisterhi;
  435. end
  436. else
  437. begin
  438. { 64bit to 64bit }
  439. if ((l.loc=LOC_CREGISTER) and maybeconst) then
  440. begin
  441. hregister:=l.register64.reglo;
  442. hregisterhi:=l.register64.reghi;
  443. const_location := true;
  444. end
  445. else
  446. begin
  447. hregister:=cg.getintregister(list,OS_INT);
  448. hregisterhi:=cg.getintregister(list,OS_INT);
  449. const_location := false;
  450. end;
  451. hreg64.reglo:=hregister;
  452. hreg64.reghi:=hregisterhi;
  453. { load value in new register }
  454. cg64.a_load64_loc_reg(list,l,hreg64);
  455. if not const_location then
  456. location_reset(l,LOC_REGISTER,dst_size)
  457. else
  458. location_reset(l,LOC_CREGISTER,dst_size);
  459. l.register64.reglo:=hregister;
  460. l.register64.reghi:=hregisterhi;
  461. end;
  462. end
  463. else
  464. begin
  465. {Do not bother to recycle the existing register. The register
  466. allocator eliminates unnecessary moves, so it's not needed
  467. and trying to recycle registers can cause problems because
  468. the registers changes size and may need aditional constraints.
  469. Not if it's about LOC_CREGISTER's (JM)
  470. }
  471. const_location :=
  472. (maybeconst) and
  473. (l.loc = LOC_CREGISTER) and
  474. (TCGSize2Size[l.size] = TCGSize2Size[dst_size]) and
  475. ((l.size = dst_size) or
  476. (TCGSize2Size[l.size] = TCGSize2Size[OS_INT]));
  477. if not const_location then
  478. hregister:=cg.getintregister(list,dst_size)
  479. else
  480. hregister := l.register;
  481. { load value in new register }
  482. case l.loc of
  483. LOC_FLAGS :
  484. cg.g_flags2reg(list,dst_size,l.resflags,hregister);
  485. LOC_JUMP :
  486. begin
  487. cg.a_label(list,current_procinfo.CurrTrueLabel);
  488. cg.a_load_const_reg(list,dst_size,1,hregister);
  489. current_asmdata.getjumplabel(hl);
  490. cg.a_jmp_always(list,hl);
  491. cg.a_label(list,current_procinfo.CurrFalseLabel);
  492. cg.a_load_const_reg(list,dst_size,0,hregister);
  493. cg.a_label(list,hl);
  494. end;
  495. else
  496. begin
  497. { load_loc_reg can only handle size >= l.size, when the
  498. new size is smaller then we need to adjust the size
  499. of the orignal and maybe recalculate l.register for i386 }
  500. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  501. begin
  502. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  503. l.register:=cg.makeregsize(list,l.register,dst_size);
  504. { for big endian systems, the reference's offset must }
  505. { be increased in this case, since they have the }
  506. { MSB first in memory and e.g. byte(word_var) should }
  507. { return the second byte in this case (JM) }
  508. if (target_info.endian = ENDIAN_BIG) and
  509. (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  510. inc(l.reference.offset,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  511. {$ifdef x86}
  512. l.size:=dst_size;
  513. {$endif x86}
  514. end;
  515. cg.a_load_loc_reg(list,dst_size,l,hregister);
  516. {$ifndef x86}
  517. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  518. l.size:=dst_size;
  519. {$endif not x86}
  520. end;
  521. end;
  522. if not const_location then
  523. location_reset(l,LOC_REGISTER,dst_size)
  524. else
  525. location_reset(l,LOC_CREGISTER,dst_size);
  526. l.register:=hregister;
  527. end;
  528. { Release temp when it was a reference }
  529. if oldloc.loc=LOC_REFERENCE then
  530. location_freetemp(list,oldloc);
  531. end;
  532. {$else cpu64bit}
  533. { 64-bit version }
  534. procedure location_force_reg(list:TAsmList;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  535. var
  536. hregister : tregister;
  537. hl : tasmlabel;
  538. oldloc : tlocation;
  539. begin
  540. oldloc:=l;
  541. hregister:=cg.getintregister(list,dst_size);
  542. { load value in new register }
  543. case l.loc of
  544. LOC_FLAGS :
  545. cg.g_flags2reg(list,dst_size,l.resflags,hregister);
  546. LOC_JUMP :
  547. begin
  548. cg.a_label(list,current_procinfo.CurrTrueLabel);
  549. cg.a_load_const_reg(list,dst_size,1,hregister);
  550. current_asmdata.getjumplabel(hl);
  551. cg.a_jmp_always(list,hl);
  552. cg.a_label(list,current_procinfo.CurrFalseLabel);
  553. cg.a_load_const_reg(list,dst_size,0,hregister);
  554. cg.a_label(list,hl);
  555. end;
  556. else
  557. begin
  558. { load_loc_reg can only handle size >= l.size, when the
  559. new size is smaller then we need to adjust the size
  560. of the orignal and maybe recalculate l.register for i386 }
  561. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  562. begin
  563. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  564. l.register:=cg.makeregsize(list,l.register,dst_size);
  565. { for big endian systems, the reference's offset must }
  566. { be increased in this case, since they have the }
  567. { MSB first in memory and e.g. byte(word_var) should }
  568. { return the second byte in this case (JM) }
  569. if (target_info.endian = ENDIAN_BIG) and
  570. (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  571. inc(l.reference.offset,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  572. {$ifdef x86}
  573. l.size:=dst_size;
  574. {$endif x86}
  575. end;
  576. cg.a_load_loc_reg(list,dst_size,l,hregister);
  577. {$ifndef x86}
  578. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  579. l.size:=dst_size;
  580. {$endif not x86}
  581. end;
  582. end;
  583. if (l.loc <> LOC_CREGISTER) or
  584. not maybeconst then
  585. location_reset(l,LOC_REGISTER,dst_size)
  586. else
  587. location_reset(l,LOC_CREGISTER,dst_size);
  588. l.register:=hregister;
  589. { Release temp when it was a reference }
  590. if oldloc.loc=LOC_REFERENCE then
  591. location_freetemp(list,oldloc);
  592. end;
  593. {$endif cpu64bit}
  594. procedure location_force_fpureg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  595. var
  596. reg : tregister;
  597. href : treference;
  598. begin
  599. if (l.loc<>LOC_FPUREGISTER) and
  600. ((l.loc<>LOC_CFPUREGISTER) or (not maybeconst)) then
  601. begin
  602. { if it's in an mm register, store to memory first }
  603. if (l.loc in [LOC_MMREGISTER,LOC_CMMREGISTER]) then
  604. begin
  605. tg.GetTemp(list,tcgsize2size[l.size],tt_normal,href);
  606. cg.a_loadmm_reg_ref(list,l.size,l.size,l.register,href,mms_movescalar);
  607. location_reset(l,LOC_REFERENCE,l.size);
  608. l.reference:=href;
  609. end;
  610. reg:=cg.getfpuregister(list,l.size);
  611. cg.a_loadfpu_loc_reg(list,l,reg);
  612. location_freetemp(list,l);
  613. location_reset(l,LOC_FPUREGISTER,l.size);
  614. l.register:=reg;
  615. end;
  616. end;
  617. procedure location_force_mmregscalar(list:TAsmList;var l: tlocation;maybeconst:boolean);
  618. var
  619. reg : tregister;
  620. href : treference;
  621. begin
  622. if (l.loc<>LOC_MMREGISTER) and
  623. ((l.loc<>LOC_CMMREGISTER) or (not maybeconst)) then
  624. begin
  625. { if it's in an fpu register, store to memory first }
  626. if (l.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  627. begin
  628. tg.GetTemp(list,tcgsize2size[l.size],tt_normal,href);
  629. cg.a_loadfpu_reg_ref(list,l.size,l.register,href);
  630. location_reset(l,LOC_REFERENCE,l.size);
  631. l.reference:=href;
  632. end;
  633. reg:=cg.getmmregister(list,l.size);
  634. cg.a_loadmm_loc_reg(list,l.size,l,reg,mms_movescalar);
  635. location_freetemp(list,l);
  636. location_reset(l,LOC_MMREGISTER,l.size);
  637. l.register:=reg;
  638. end;
  639. end;
  640. procedure location_force_mem(list:TAsmList;var l:tlocation);
  641. var
  642. r : treference;
  643. begin
  644. case l.loc of
  645. LOC_FPUREGISTER,
  646. LOC_CFPUREGISTER :
  647. begin
  648. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  649. cg.a_loadfpu_reg_ref(list,l.size,l.register,r);
  650. location_reset(l,LOC_REFERENCE,l.size);
  651. l.reference:=r;
  652. end;
  653. LOC_MMREGISTER,
  654. LOC_CMMREGISTER:
  655. begin
  656. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  657. cg.a_loadmm_reg_ref(list,l.size,l.size,l.register,r,mms_movescalar);
  658. location_reset(l,LOC_REFERENCE,l.size);
  659. l.reference:=r;
  660. end;
  661. LOC_CONSTANT,
  662. LOC_REGISTER,
  663. LOC_CREGISTER :
  664. begin
  665. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  666. {$ifndef cpu64bit}
  667. if l.size in [OS_64,OS_S64] then
  668. cg64.a_load64_loc_ref(list,l,r)
  669. else
  670. {$endif cpu64bit}
  671. cg.a_load_loc_ref(list,l.size,l,r);
  672. location_reset(l,LOC_REFERENCE,l.size);
  673. l.reference:=r;
  674. end;
  675. LOC_CREFERENCE,
  676. LOC_REFERENCE : ;
  677. else
  678. internalerror(200203219);
  679. end;
  680. end;
  681. procedure location_get_data_ref(list:TAsmList;const l:tlocation;var ref:treference;loadref:boolean);
  682. begin
  683. case l.loc of
  684. LOC_REGISTER,
  685. LOC_CREGISTER :
  686. begin
  687. if not loadref then
  688. internalerror(200410231);
  689. reference_reset_base(ref,l.register,0);
  690. end;
  691. LOC_REFERENCE,
  692. LOC_CREFERENCE :
  693. begin
  694. if loadref then
  695. begin
  696. reference_reset_base(ref,cg.getaddressregister(list),0);
  697. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,l.reference,ref.base);
  698. end
  699. else
  700. ref:=l.reference;
  701. end;
  702. else
  703. internalerror(200309181);
  704. end;
  705. end;
  706. {*****************************************************************************
  707. Maybe_Save
  708. *****************************************************************************}
  709. function maybe_pushfpu(list:TAsmList;needed : byte;var l:tlocation) : boolean;
  710. begin
  711. {$ifdef i386}
  712. if (needed>=maxfpuregs) and
  713. (l.loc = LOC_FPUREGISTER) then
  714. begin
  715. location_force_mem(list,l);
  716. maybe_pushfpu:=true;
  717. end
  718. else
  719. maybe_pushfpu:=false;
  720. {$else i386}
  721. maybe_pushfpu:=false;
  722. {$endif i386}
  723. end;
  724. {****************************************************************************
  725. Init/Finalize Code
  726. ****************************************************************************}
  727. procedure copyvalueparas(p : tnamedindexitem;arg:pointer);
  728. var
  729. href : treference;
  730. hreg : tregister;
  731. list : TAsmList;
  732. hsym : tparavarsym;
  733. l : longint;
  734. localcopyloc : tlocation;
  735. begin
  736. list:=TAsmList(arg);
  737. if (tsym(p).typ=paravarsym) and
  738. (tparavarsym(p).varspez=vs_value) and
  739. (paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vartype.def,current_procinfo.procdef.proccalloption)) then
  740. begin
  741. location_get_data_ref(list,tparavarsym(p).localloc,href,true);
  742. if is_open_array(tparavarsym(p).vartype.def) or
  743. is_array_of_const(tparavarsym(p).vartype.def) then
  744. begin
  745. { cdecl functions don't have a high pointer so it is not possible to generate
  746. a local copy }
  747. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  748. begin
  749. hsym:=tparavarsym(tsym(p).owner.search('high'+p.name));
  750. if not assigned(hsym) then
  751. internalerror(200306061);
  752. hreg:=cg.getaddressregister(list);
  753. cg.g_copyvaluepara_openarray(list,href,hsym.localloc,tarraydef(tparavarsym(p).vartype.def).elesize,hreg);
  754. cg.a_load_reg_loc(list,OS_ADDR,hreg,tparavarsym(p).localloc);
  755. end;
  756. end
  757. else
  758. begin
  759. { Allocate space for the local copy }
  760. l:=tparavarsym(p).getsize;
  761. localcopyloc.loc:=LOC_REFERENCE;
  762. localcopyloc.size:=int_cgsize(l);
  763. tg.GetLocal(list,l,tparavarsym(p).vartype.def,localcopyloc.reference);
  764. { Copy data }
  765. if is_shortstring(tparavarsym(p).vartype.def) then
  766. begin
  767. { this code is only executed before the code for the body and the entry/exit code is generated
  768. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  769. }
  770. include(current_procinfo.flags,pi_do_call);
  771. cg.g_copyshortstring(list,href,localcopyloc.reference,tstringdef(tparavarsym(p).vartype.def).len)
  772. end
  773. else
  774. cg.g_concatcopy(list,href,localcopyloc.reference,tparavarsym(p).vartype.def.size);
  775. { update localloc of varsym }
  776. tg.Ungetlocal(list,tparavarsym(p).localloc.reference);
  777. tparavarsym(p).localloc:=localcopyloc;
  778. end;
  779. end;
  780. end;
  781. { initializes the regvars from staticsymtable with 0 }
  782. procedure initialize_regvars(p : tnamedindexitem;arg:pointer);
  783. begin
  784. if (tsym(p).typ=globalvarsym) then
  785. begin
  786. case tglobalvarsym(p).localloc.loc of
  787. LOC_CREGISTER :
  788. cg.a_load_const_reg(TAsmList(arg),reg_cgsize(tglobalvarsym(p).localloc.register),0,
  789. tglobalvarsym(p).localloc.register);
  790. LOC_REFERENCE : ;
  791. LOC_CMMREGISTER :
  792. { clear the whole register }
  793. cg.a_opmm_reg_reg(TAsmList(arg),OP_XOR,reg_cgsize(tglobalvarsym(p).localloc.register),
  794. tglobalvarsym(p).localloc.register,
  795. tglobalvarsym(p).localloc.register,
  796. nil);
  797. LOC_CFPUREGISTER :
  798. ;
  799. else
  800. internalerror(200410124);
  801. end;
  802. end;
  803. end;
  804. { generates the code for initialisation of local data }
  805. procedure initialize_data(p : tnamedindexitem;arg:pointer);
  806. var
  807. OldAsmList : TAsmList;
  808. hp : tnode;
  809. begin
  810. if (tsym(p).typ in [globalvarsym,localvarsym]) and
  811. (tabstractvarsym(p).refs>0) and
  812. not(vo_is_external in tabstractvarsym(p).varoptions) and
  813. not(is_class(tabstractvarsym(p).vartype.def)) and
  814. tabstractvarsym(p).vartype.def.needs_inittable then
  815. begin
  816. OldAsmList:=current_asmdata.CurrAsmList;
  817. current_asmdata.CurrAsmList:=TAsmList(arg);
  818. hp:=initialize_data_node(cloadnode.create(tsym(p),tsym(p).owner));
  819. firstpass(hp);
  820. secondpass(hp);
  821. hp.free;
  822. current_asmdata.CurrAsmList:=OldAsmList;
  823. end;
  824. end;
  825. procedure finalize_sym(asmlist:TAsmList;sym:tsym);
  826. var
  827. hp : tnode;
  828. OldAsmList : TAsmList;
  829. begin
  830. include(current_procinfo.flags,pi_needs_implicit_finally);
  831. OldAsmList:=current_asmdata.CurrAsmList;
  832. current_asmdata.CurrAsmList:=asmlist;
  833. hp:=finalize_data_node(cloadnode.create(sym,sym.owner));
  834. firstpass(hp);
  835. secondpass(hp);
  836. hp.free;
  837. current_asmdata.CurrAsmList:=OldAsmList;
  838. end;
  839. { generates the code for finalisation of local variables }
  840. procedure finalize_local_vars(p : tnamedindexitem;arg:pointer);
  841. begin
  842. if (tsym(p).typ=localvarsym) and
  843. (tlocalvarsym(p).refs>0) and
  844. not(vo_is_external in tlocalvarsym(p).varoptions) and
  845. not(vo_is_funcret in tlocalvarsym(p).varoptions) and
  846. not(is_class(tlocalvarsym(p).vartype.def)) and
  847. tlocalvarsym(p).vartype.def.needs_inittable then
  848. finalize_sym(TAsmList(arg),tsym(p));
  849. end;
  850. { generates the code for finalisation of local typedconsts }
  851. procedure finalize_local_typedconst(p : tnamedindexitem;arg:pointer);
  852. var
  853. i : longint;
  854. pd : tprocdef;
  855. begin
  856. case tsym(p).typ of
  857. typedconstsym :
  858. begin
  859. if ttypedconstsym(p).is_writable and
  860. ttypedconstsym(p).typedconsttype.def.needs_inittable then
  861. finalize_sym(TAsmList(arg),tsym(p));
  862. end;
  863. procsym :
  864. begin
  865. for i:=1 to tprocsym(p).procdef_count do
  866. begin
  867. pd:=tprocsym(p).procdef[i];
  868. if assigned(pd.localst) and
  869. (pd.procsym=tprocsym(p)) and
  870. (pd.localst.symtabletype<>staticsymtable) then
  871. pd.localst.foreach_static(@finalize_local_typedconst,arg);
  872. end;
  873. end;
  874. end;
  875. end;
  876. { generates the code for finalization of static symtable and
  877. all local (static) typedconsts }
  878. procedure finalize_static_data(p : tnamedindexitem;arg:pointer);
  879. var
  880. i : longint;
  881. pd : tprocdef;
  882. begin
  883. case tsym(p).typ of
  884. globalvarsym :
  885. begin
  886. if (tglobalvarsym(p).refs>0) and
  887. not(vo_is_funcret in tglobalvarsym(p).varoptions) and
  888. not(vo_is_external in tglobalvarsym(p).varoptions) and
  889. not(is_class(tglobalvarsym(p).vartype.def)) and
  890. tglobalvarsym(p).vartype.def.needs_inittable then
  891. finalize_sym(TAsmList(arg),tsym(p));
  892. end;
  893. typedconstsym :
  894. begin
  895. if ttypedconstsym(p).is_writable and
  896. ttypedconstsym(p).typedconsttype.def.needs_inittable then
  897. finalize_sym(TAsmList(arg),tsym(p));
  898. end;
  899. procsym :
  900. begin
  901. for i:=1 to tprocsym(p).procdef_count do
  902. begin
  903. pd:=tprocsym(p).procdef[i];
  904. if assigned(pd.localst) and
  905. (pd.procsym=tprocsym(p)) and
  906. (pd.localst.symtabletype<>staticsymtable) then
  907. pd.localst.foreach_static(@finalize_local_typedconst,arg);
  908. end;
  909. end;
  910. end;
  911. end;
  912. { generates the code for incrementing the reference count of parameters and
  913. initialize out parameters }
  914. procedure init_paras(p : tnamedindexitem;arg:pointer);
  915. var
  916. href : treference;
  917. tmpreg : tregister;
  918. list : TAsmList;
  919. begin
  920. list:=TAsmList(arg);
  921. if (tsym(p).typ=paravarsym) and
  922. not is_class_or_interface(tparavarsym(p).vartype.def) and
  923. tparavarsym(p).vartype.def.needs_inittable then
  924. begin
  925. case tparavarsym(p).varspez of
  926. vs_value :
  927. begin
  928. location_get_data_ref(list,tparavarsym(p).localloc,href,is_open_array(tparavarsym(p).vartype.def));
  929. cg.g_incrrefcount(list,tparavarsym(p).vartype.def,href);
  930. end;
  931. vs_out :
  932. begin
  933. tmpreg:=cg.getaddressregister(list);
  934. cg.a_load_loc_reg(list,OS_ADDR,tparavarsym(p).localloc,tmpreg);
  935. reference_reset_base(href,tmpreg,0);
  936. cg.g_initialize(list,tparavarsym(p).vartype.def,href);
  937. end;
  938. end;
  939. end;
  940. end;
  941. { generates the code for decrementing the reference count of parameters }
  942. procedure final_paras(p : tnamedindexitem;arg:pointer);
  943. var
  944. list : TAsmList;
  945. href : treference;
  946. begin
  947. if not(tsym(p).typ=paravarsym) then
  948. exit;
  949. list:=TAsmList(arg);
  950. if not is_class_or_interface(tparavarsym(p).vartype.def) and
  951. tparavarsym(p).vartype.def.needs_inittable then
  952. begin
  953. if (tparavarsym(p).varspez=vs_value) then
  954. begin
  955. include(current_procinfo.flags,pi_needs_implicit_finally);
  956. location_get_data_ref(list,tparavarsym(p).localloc,href,is_open_array(tparavarsym(p).vartype.def));
  957. cg.g_decrrefcount(list,tparavarsym(p).vartype.def,href);
  958. end;
  959. end
  960. else
  961. if (tparavarsym(p).varspez=vs_value) and
  962. (is_open_array(tparavarsym(p).vartype.def) or
  963. is_array_of_const(tparavarsym(p).vartype.def)) then
  964. begin
  965. { cdecl functions don't have a high pointer so it is not possible to generate
  966. a local copy }
  967. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  968. cg.g_releasevaluepara_openarray(list,tparavarsym(p).localloc);
  969. end;
  970. end;
  971. { Initialize temp ansi/widestrings,interfaces }
  972. procedure inittempvariables(list:TAsmList);
  973. var
  974. hp : ptemprecord;
  975. href : treference;
  976. begin
  977. hp:=tg.templist;
  978. while assigned(hp) do
  979. begin
  980. if assigned(hp^.def) and
  981. hp^.def.needs_inittable then
  982. begin
  983. reference_reset_base(href,current_procinfo.framepointer,hp^.pos);
  984. cg.g_initialize(list,hp^.def,href);
  985. end;
  986. hp:=hp^.next;
  987. end;
  988. end;
  989. procedure finalizetempvariables(list:TAsmList);
  990. var
  991. hp : ptemprecord;
  992. href : treference;
  993. begin
  994. hp:=tg.templist;
  995. while assigned(hp) do
  996. begin
  997. if assigned(hp^.def) and
  998. hp^.def.needs_inittable then
  999. begin
  1000. include(current_procinfo.flags,pi_needs_implicit_finally);
  1001. reference_reset_base(href,current_procinfo.framepointer,hp^.pos);
  1002. cg.g_finalize(list,hp^.def,href);
  1003. end;
  1004. hp:=hp^.next;
  1005. end;
  1006. end;
  1007. procedure gen_load_return_value(list:TAsmList);
  1008. var
  1009. {$ifndef cpu64bit}
  1010. href : treference;
  1011. {$endif cpu64bit}
  1012. ressym : tabstractnormalvarsym;
  1013. resloc,
  1014. restmploc : tlocation;
  1015. hreg : tregister;
  1016. funcretloc : tlocation;
  1017. begin
  1018. { Is the loading needed? }
  1019. if (current_procinfo.procdef.funcretloc[calleeside].loc=LOC_VOID) or
  1020. (
  1021. (po_assembler in current_procinfo.procdef.procoptions) and
  1022. (not(assigned(current_procinfo.procdef.funcretsym)) or
  1023. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs=0))
  1024. ) then
  1025. exit;
  1026. funcretloc:=current_procinfo.procdef.funcretloc[calleeside];
  1027. { constructors return self }
  1028. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  1029. ressym:=tabstractnormalvarsym(current_procinfo.procdef.parast.search('self'))
  1030. else
  1031. ressym:=tabstractnormalvarsym(current_procinfo.procdef.funcretsym);
  1032. if (ressym.refs>0) then
  1033. begin
  1034. {$ifdef OLDREGVARS}
  1035. case ressym.localloc.loc of
  1036. LOC_CFPUREGISTER,
  1037. LOC_FPUREGISTER:
  1038. begin
  1039. location_reset(restmploc,LOC_CFPUREGISTER,funcretloc^.size);
  1040. restmploc.register:=ressym.localloc.register;
  1041. end;
  1042. LOC_CREGISTER,
  1043. LOC_REGISTER:
  1044. begin
  1045. location_reset(restmploc,LOC_CREGISTER,funcretloc^.size);
  1046. restmploc.register:=ressym.localloc.register;
  1047. end;
  1048. LOC_MMREGISTER:
  1049. begin
  1050. location_reset(restmploc,LOC_CMMREGISTER,funcretloc^.size);
  1051. restmploc.register:=ressym.localloc.register;
  1052. end;
  1053. LOC_REFERENCE:
  1054. begin
  1055. location_reset(restmploc,LOC_REFERENCE,funcretloc^.size);
  1056. restmploc.reference:=ressym.localloc.reference;
  1057. end;
  1058. else
  1059. internalerror(200309184);
  1060. end;
  1061. {$else}
  1062. restmploc:=ressym.localloc;
  1063. {$endif}
  1064. { Here, we return the function result. In most architectures, the value is
  1065. passed into the FUNCTION_RETURN_REG, but in a windowed architecure like sparc a
  1066. function returns in a register and the caller receives it in an other one }
  1067. case funcretloc.loc of
  1068. LOC_REGISTER:
  1069. begin
  1070. {$ifndef cpu64bit}
  1071. if current_procinfo.procdef.funcretloc[calleeside].size in [OS_64,OS_S64] then
  1072. begin
  1073. resloc:=current_procinfo.procdef.funcretloc[calleeside];
  1074. if resloc.loc<>LOC_REGISTER then
  1075. internalerror(200409141);
  1076. { Load low and high register separate to generate better register
  1077. allocation info }
  1078. if getsupreg(resloc.register64.reglo)<first_int_imreg then
  1079. begin
  1080. cg.getcpuregister(list,resloc.register64.reglo);
  1081. end;
  1082. case restmploc.loc of
  1083. LOC_REFERENCE :
  1084. begin
  1085. href:=restmploc.reference;
  1086. if target_info.endian=ENDIAN_BIG then
  1087. inc(href.offset,4);
  1088. cg.a_load_ref_reg(list,OS_32,OS_32,href,resloc.register64.reglo);
  1089. end;
  1090. LOC_CREGISTER :
  1091. cg.a_load_reg_reg(list,OS_32,OS_32,restmploc.register64.reglo,resloc.register64.reglo);
  1092. else
  1093. internalerror(200409203);
  1094. end;
  1095. if getsupreg(resloc.register64.reghi)<first_int_imreg then
  1096. begin
  1097. cg.getcpuregister(list,resloc.register64.reghi);
  1098. end;
  1099. case restmploc.loc of
  1100. LOC_REFERENCE :
  1101. begin
  1102. href:=restmploc.reference;
  1103. if target_info.endian=ENDIAN_LITTLE then
  1104. inc(href.offset,4);
  1105. cg.a_load_ref_reg(list,OS_32,OS_32,href,resloc.register64.reghi);
  1106. end;
  1107. LOC_CREGISTER :
  1108. cg.a_load_reg_reg(list,OS_32,OS_32,restmploc.register64.reghi,resloc.register64.reghi);
  1109. else
  1110. internalerror(200409204);
  1111. end;
  1112. end
  1113. else
  1114. {$endif cpu64bit}
  1115. begin
  1116. hreg:=cg.makeregsize(list,funcretloc.register,funcretloc.size);
  1117. if getsupreg(funcretloc.register)<first_int_imreg then
  1118. begin
  1119. cg.getcpuregister(list,funcretloc.register);
  1120. end;
  1121. { it could be that a structure is passed in memory but the function is expected to
  1122. return a pointer to this memory }
  1123. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1124. cg.a_load_loc_reg(list,OS_ADDR,restmploc,hreg)
  1125. else
  1126. cg.a_load_loc_reg(list,restmploc.size,restmploc,hreg);
  1127. end;
  1128. end;
  1129. LOC_FPUREGISTER:
  1130. begin
  1131. if getsupreg(funcretloc.register)<first_fpu_imreg then
  1132. begin
  1133. cg.getcpuregister(list,funcretloc.register);
  1134. end;
  1135. { we can't do direct moves between fpu and mm registers }
  1136. if restmploc.loc in [LOC_MMREGISTER,LOC_CMMREGISTER] then
  1137. location_force_fpureg(list,restmploc,false);
  1138. cg.a_loadfpu_loc_reg(list,restmploc,funcretloc.register);
  1139. end;
  1140. LOC_MMREGISTER:
  1141. begin
  1142. if getsupreg(funcretloc.register)<first_mm_imreg then
  1143. begin
  1144. cg.getcpuregister(list,funcretloc.register);
  1145. end;
  1146. cg.a_loadmm_loc_reg(list,restmploc.size,restmploc,funcretloc.register,mms_movescalar);
  1147. end;
  1148. LOC_INVALID,
  1149. LOC_REFERENCE:
  1150. ;
  1151. else
  1152. internalerror(200405025);
  1153. end;
  1154. end;
  1155. end;
  1156. procedure gen_alloc_regvar(list:TAsmList;sym: tabstractnormalvarsym);
  1157. begin
  1158. case sym.localloc.loc of
  1159. LOC_CREGISTER:
  1160. begin
  1161. {$ifndef cpu64bit}
  1162. if sym.localloc.size in [OS_64,OS_S64] then
  1163. begin
  1164. sym.localloc.register64.reglo:=cg.getintregister(list,OS_32);
  1165. sym.localloc.register64.reghi:=cg.getintregister(list,OS_32);
  1166. end
  1167. else
  1168. {$endif cpu64bit}
  1169. sym.localloc.register:=cg.getintregister(list,sym.localloc.size);
  1170. end;
  1171. LOC_CFPUREGISTER:
  1172. begin
  1173. sym.localloc.register:=cg.getfpuregister(list,sym.localloc.size);
  1174. end;
  1175. LOC_CMMREGISTER:
  1176. begin
  1177. sym. localloc.register:=cg.getmmregister(list,sym.localloc.size);
  1178. end;
  1179. end;
  1180. if (pi_has_goto in current_procinfo.flags) then
  1181. begin
  1182. { Allocate register already, to prevent first allocation to be
  1183. inside a loop }
  1184. {$ifndef cpu64bit}
  1185. if sym.localloc.size in [OS_64,OS_S64] then
  1186. begin
  1187. cg.a_reg_sync(list,sym.localloc.register64.reglo);
  1188. cg.a_reg_sync(list,sym.localloc.register64.reghi);
  1189. end
  1190. else
  1191. {$endif cpu64bit}
  1192. cg.a_reg_sync(list,sym.localloc.register);
  1193. end;
  1194. end;
  1195. procedure gen_load_para_value(list:TAsmList);
  1196. procedure get_para(const paraloc:TCGParaLocation);
  1197. begin
  1198. case paraloc.loc of
  1199. LOC_REGISTER :
  1200. begin
  1201. if getsupreg(paraloc.register)<first_int_imreg then
  1202. cg.getcpuregister(list,paraloc.register);
  1203. end;
  1204. LOC_MMREGISTER :
  1205. begin
  1206. if getsupreg(paraloc.register)<first_mm_imreg then
  1207. cg.getcpuregister(list,paraloc.register);
  1208. end;
  1209. LOC_FPUREGISTER :
  1210. begin
  1211. if getsupreg(paraloc.register)<first_fpu_imreg then
  1212. cg.getcpuregister(list,paraloc.register);
  1213. end;
  1214. end;
  1215. end;
  1216. procedure unget_para(const paraloc:TCGParaLocation);
  1217. begin
  1218. case paraloc.loc of
  1219. LOC_REGISTER :
  1220. begin
  1221. if getsupreg(paraloc.register)<first_int_imreg then
  1222. cg.ungetcpuregister(list,paraloc.register);
  1223. end;
  1224. LOC_MMREGISTER :
  1225. begin
  1226. if getsupreg(paraloc.register)<first_mm_imreg then
  1227. cg.ungetcpuregister(list,paraloc.register);
  1228. end;
  1229. LOC_FPUREGISTER :
  1230. begin
  1231. if getsupreg(paraloc.register)<first_fpu_imreg then
  1232. cg.ungetcpuregister(list,paraloc.register);
  1233. end;
  1234. end;
  1235. end;
  1236. procedure gen_load_ref(const paraloc:TCGParaLocation;const ref:treference;sizeleft:aint);
  1237. var
  1238. href : treference;
  1239. begin
  1240. case paraloc.loc of
  1241. LOC_REGISTER :
  1242. begin
  1243. {$IFDEF POWERPC64}
  1244. if (paraloc.shiftval <> 0) then
  1245. cg.a_op_const_reg_reg(list, OP_SHL, OS_INT, paraloc.shiftval, paraloc.register, paraloc.register);
  1246. {$ENDIF POWERPC64}
  1247. cg.a_load_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref);
  1248. end;
  1249. LOC_MMREGISTER :
  1250. cg.a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,mms_movescalar);
  1251. LOC_FPUREGISTER :
  1252. cg.a_loadfpu_reg_ref(list,paraloc.size,paraloc.register,ref);
  1253. LOC_REFERENCE :
  1254. begin
  1255. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset);
  1256. { use concatcopy, because it can also be a float which fails when
  1257. load_ref_ref is used. Don't copy data when the references are equal }
  1258. if not((href.base=ref.base) and (href.offset=ref.offset)) then
  1259. cg.g_concatcopy(list,href,ref,sizeleft);
  1260. end;
  1261. else
  1262. internalerror(2002081302);
  1263. end;
  1264. end;
  1265. procedure gen_load_reg(const paraloc:TCGParaLocation;reg:tregister);
  1266. var
  1267. href : treference;
  1268. begin
  1269. case paraloc.loc of
  1270. LOC_REGISTER :
  1271. cg.a_load_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg);
  1272. LOC_MMREGISTER :
  1273. cg.a_loadmm_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg,mms_movescalar);
  1274. LOC_FPUREGISTER :
  1275. cg.a_loadfpu_reg_reg(list,paraloc.size,paraloc.register,reg);
  1276. LOC_REFERENCE :
  1277. begin
  1278. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset);
  1279. case getregtype(reg) of
  1280. R_INTREGISTER :
  1281. cg.a_load_ref_reg(list,paraloc.size,paraloc.size,href,reg);
  1282. R_FPUREGISTER :
  1283. cg.a_loadfpu_ref_reg(list,paraloc.size,href,reg);
  1284. R_MMREGISTER :
  1285. cg.a_loadmm_ref_reg(list,paraloc.size,paraloc.size,href,reg,mms_movescalar);
  1286. else
  1287. internalerror(2004101012);
  1288. end;
  1289. end;
  1290. else
  1291. internalerror(2002081302);
  1292. end;
  1293. end;
  1294. var
  1295. i : longint;
  1296. currpara : tparavarsym;
  1297. paraloc : pcgparalocation;
  1298. href : treference;
  1299. sizeleft : aint;
  1300. {$ifdef sparc}
  1301. tempref : treference;
  1302. {$endif sparc}
  1303. begin
  1304. if (po_assembler in current_procinfo.procdef.procoptions) then
  1305. exit;
  1306. { Allocate registers used by parameters }
  1307. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1308. begin
  1309. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1310. paraloc:=currpara.paraloc[calleeside].location;
  1311. while assigned(paraloc) do
  1312. begin
  1313. if paraloc^.loc in [LOC_REGISTER,LOC_FPUREGISTER,LOC_MMREGISTER] then
  1314. get_para(paraloc^);
  1315. paraloc:=paraloc^.next;
  1316. end;
  1317. end;
  1318. { Copy parameters to local references/registers }
  1319. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1320. begin
  1321. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1322. paraloc:=currpara.paraloc[calleeside].location;
  1323. { skip e.g. empty records }
  1324. if not assigned(paraloc) then
  1325. internalerror(200408203);
  1326. if (paraloc^.loc = LOC_VOID) then
  1327. continue;
  1328. case currpara.localloc.loc of
  1329. LOC_REFERENCE :
  1330. begin
  1331. { If the parameter location is reused we don't need to copy
  1332. anything }
  1333. if not paramanager.param_use_paraloc(currpara.paraloc[calleeside]) then
  1334. begin
  1335. href:=currpara.localloc.reference;
  1336. sizeleft:=currpara.paraloc[calleeside].intsize;
  1337. while assigned(paraloc) do
  1338. begin
  1339. unget_para(paraloc^);
  1340. if (paraloc^.size=OS_NO) then
  1341. begin
  1342. { Can only be a reference that contains the rest
  1343. of the parameter }
  1344. if (paraloc^.loc<>LOC_REFERENCE) or
  1345. assigned(paraloc^.next) then
  1346. internalerror(2005013010);
  1347. gen_load_ref(paraloc^,href,sizeleft);
  1348. inc(href.offset,sizeleft);
  1349. sizeleft:=0;
  1350. end
  1351. else
  1352. begin
  1353. gen_load_ref(paraloc^,href,tcgsize2size[paraloc^.size]);
  1354. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1355. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1356. end;
  1357. paraloc:=paraloc^.next;
  1358. end;
  1359. end;
  1360. end;
  1361. LOC_CREGISTER :
  1362. begin
  1363. {$ifndef cpu64bit}
  1364. if (currpara.paraloc[calleeside].size in [OS_64,OS_S64]) and
  1365. is_64bit(currpara.vartype.def) then
  1366. begin
  1367. case paraloc^.loc of
  1368. LOC_REGISTER:
  1369. begin
  1370. if not assigned(paraloc^.next) then
  1371. internalerror(200410104);
  1372. if (target_info.endian=ENDIAN_BIG) then
  1373. begin
  1374. { paraloc^ -> high
  1375. paraloc^.next -> low }
  1376. unget_para(paraloc^);
  1377. gen_alloc_regvar(list,currpara);
  1378. gen_load_reg(paraloc^,currpara.localloc.register64.reghi);
  1379. unget_para(paraloc^.next^);
  1380. gen_load_reg(paraloc^.next^,currpara.localloc.register64.reglo);
  1381. end
  1382. else
  1383. begin
  1384. { paraloc^ -> low
  1385. paraloc^.next -> high }
  1386. unget_para(paraloc^);
  1387. gen_alloc_regvar(list,currpara);
  1388. gen_load_reg(paraloc^,currpara.localloc.register64.reglo);
  1389. unget_para(paraloc^.next^);
  1390. gen_load_reg(paraloc^.next^,currpara.localloc.register64.reghi);
  1391. end;
  1392. end;
  1393. LOC_REFERENCE:
  1394. begin
  1395. gen_alloc_regvar(list,currpara);
  1396. reference_reset_base(href,paraloc^.reference.index,paraloc^.reference.offset);
  1397. cg64.a_load64_ref_reg(list,href,currpara.localloc.register64);
  1398. unget_para(paraloc^);
  1399. end;
  1400. else
  1401. internalerror(2005101501);
  1402. end
  1403. end
  1404. else
  1405. {$endif cpu64bit}
  1406. begin
  1407. if assigned(paraloc^.next) then
  1408. internalerror(200410105);
  1409. unget_para(paraloc^);
  1410. gen_alloc_regvar(list,currpara);
  1411. gen_load_reg(paraloc^,currpara.localloc.register);
  1412. end;
  1413. end;
  1414. LOC_CFPUREGISTER :
  1415. begin
  1416. {$ifdef sparc}
  1417. { Sparc passes floats in int registers, when loading to fpu register
  1418. we need a temp }
  1419. sizeleft := TCGSize2Size[currpara.localloc.size];
  1420. tg.GetTemp(list,sizeleft,tt_normal,tempref);
  1421. href:=tempref;
  1422. while assigned(paraloc) do
  1423. begin
  1424. unget_para(paraloc^);
  1425. gen_load_ref(paraloc^,href,sizeleft);
  1426. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1427. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1428. paraloc:=paraloc^.next;
  1429. end;
  1430. gen_alloc_regvar(list,currpara);
  1431. cg.a_loadfpu_ref_reg(list,currpara.localloc.size,tempref,currpara.localloc.register);
  1432. tg.UnGetTemp(list,tempref);
  1433. {$else sparc}
  1434. unget_para(paraloc^);
  1435. gen_alloc_regvar(list,currpara);
  1436. gen_load_reg(paraloc^,currpara.localloc.register);
  1437. if assigned(paraloc^.next) then
  1438. internalerror(200410109);
  1439. {$endif sparc}
  1440. end;
  1441. LOC_CMMREGISTER :
  1442. begin
  1443. unget_para(paraloc^);
  1444. gen_alloc_regvar(list,currpara);
  1445. gen_load_reg(paraloc^,currpara.localloc.register);
  1446. { data could come in two memory locations, for now
  1447. we simply ignore the sanity check (FK)
  1448. if assigned(paraloc^.next) then
  1449. internalerror(200410108);
  1450. }
  1451. end;
  1452. end;
  1453. end;
  1454. { generate copies of call by value parameters, must be done before
  1455. the initialization and body is parsed because the refcounts are
  1456. incremented using the local copies }
  1457. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}copyvalueparas,list);
  1458. {$ifdef powerpc}
  1459. { unget the register that contains the stack pointer before the procedure entry, }
  1460. { which is used to access the parameters in their original callee-side location }
  1461. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1462. cg.a_reg_dealloc(list,NR_R12);
  1463. {$endif powerpc}
  1464. {$ifdef powerpc64}
  1465. { unget the register that contains the stack pointer before the procedure entry, }
  1466. { which is used to access the parameters in their original callee-side location }
  1467. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1468. cg.a_reg_dealloc(list, NR_OLD_STACK_POINTER_REG);
  1469. {$endif powerpc64}
  1470. end;
  1471. procedure gen_initialize_code(list:TAsmList);
  1472. begin
  1473. { initialize local data like ansistrings }
  1474. case current_procinfo.procdef.proctypeoption of
  1475. potype_unitinit:
  1476. begin
  1477. { this is also used for initialization of variables in a
  1478. program which does not have a globalsymtable }
  1479. if assigned(current_module.globalsymtable) then
  1480. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1481. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1482. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_regvars,list);
  1483. end;
  1484. { units have seperate code for initilization and finalization }
  1485. potype_unitfinalize: ;
  1486. { program init/final is generated in separate procedure }
  1487. potype_proginit:
  1488. begin
  1489. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_regvars,list);
  1490. end;
  1491. else
  1492. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1493. end;
  1494. { initialisizes temp. ansi/wide string data }
  1495. inittempvariables(list);
  1496. { initialize ansi/widesstring para's }
  1497. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1498. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}init_paras,list);
  1499. {$ifdef OLDREGVARS}
  1500. load_regvars(list,nil);
  1501. {$endif OLDREGVARS}
  1502. end;
  1503. procedure gen_finalize_code(list:TAsmList);
  1504. begin
  1505. {$ifdef OLDREGVARS}
  1506. cleanup_regvars(list);
  1507. {$endif OLDREGVARS}
  1508. { finalize temporary data }
  1509. finalizetempvariables(list);
  1510. { finalize local data like ansistrings}
  1511. case current_procinfo.procdef.proctypeoption of
  1512. potype_unitfinalize:
  1513. begin
  1514. { this is also used for initialization of variables in a
  1515. program which does not have a globalsymtable }
  1516. if assigned(current_module.globalsymtable) then
  1517. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_static_data,list);
  1518. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_static_data,list);
  1519. end;
  1520. { units/progs have separate code for initialization and finalization }
  1521. potype_unitinit: ;
  1522. { program init/final is generated in separate procedure }
  1523. potype_proginit: ;
  1524. else
  1525. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}finalize_local_vars,list);
  1526. end;
  1527. { finalize paras data }
  1528. if assigned(current_procinfo.procdef.parast) and
  1529. not(po_assembler in current_procinfo.procdef.procoptions) then
  1530. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}final_paras,list);
  1531. end;
  1532. procedure gen_entry_code(list:TAsmList);
  1533. var
  1534. paraloc1,
  1535. paraloc2 : tcgpara;
  1536. begin
  1537. paraloc1.init;
  1538. paraloc2.init;
  1539. { the actual profile code can clobber some registers,
  1540. therefore if the context must be saved, do it before
  1541. the actual call to the profile code
  1542. }
  1543. if (cs_profile in aktmoduleswitches) and
  1544. not(po_assembler in current_procinfo.procdef.procoptions) then
  1545. begin
  1546. { non-win32 can call mcout even in main }
  1547. if not (target_info.system in [system_i386_win32,system_i386_wdosx]) or
  1548. not (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1549. begin
  1550. cg.allocallcpuregisters(list);
  1551. cg.g_profilecode(list);
  1552. cg.deallocallcpuregisters(list);
  1553. end;
  1554. end;
  1555. { call startup helpers from main program }
  1556. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1557. begin
  1558. { initialize units }
  1559. cg.allocallcpuregisters(list);
  1560. cg.a_call_name(list,'FPC_INITIALIZEUNITS');
  1561. cg.deallocallcpuregisters(list);
  1562. end;
  1563. list.concat(Tai_force_line.Create);
  1564. {$ifdef OLDREGVARS}
  1565. load_regvars(list,nil);
  1566. {$endif OLDREGVARS}
  1567. paraloc1.done;
  1568. paraloc2.done;
  1569. end;
  1570. procedure gen_exit_code(list:TAsmList);
  1571. begin
  1572. { call __EXIT for main program }
  1573. if (not DLLsource) and
  1574. (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1575. cg.a_call_name(list,'FPC_DO_EXIT');
  1576. end;
  1577. {****************************************************************************
  1578. Entry/Exit
  1579. ****************************************************************************}
  1580. procedure alloc_proc_symbol(pd: tprocdef);
  1581. var
  1582. item: tstringlistitem;
  1583. begin
  1584. item := tstringlistitem(pd.aliasnames.first);
  1585. while assigned(item) do
  1586. begin
  1587. if (cs_profile in aktmoduleswitches) or
  1588. (po_global in current_procinfo.procdef.procoptions) then
  1589. current_asmdata.newasmsymbol(item.str,AB_GLOBAL,AT_FUNCTION)
  1590. else
  1591. current_asmdata.newasmsymbol(item.str,AB_GLOBAL,AT_FUNCTION);
  1592. item := tstringlistitem(item.next);
  1593. end;
  1594. end;
  1595. procedure gen_proc_symbol(list:TAsmList);
  1596. var
  1597. hs : string;
  1598. begin
  1599. repeat
  1600. hs:=current_procinfo.procdef.aliasnames.getfirst;
  1601. if hs='' then
  1602. break;
  1603. if (cs_profile in aktmoduleswitches) or
  1604. (po_global in current_procinfo.procdef.procoptions) then
  1605. list.concat(Tai_symbol.createname_global(hs,AT_FUNCTION,0))
  1606. else
  1607. list.concat(Tai_symbol.createname(hs,AT_FUNCTION,0));
  1608. if tf_use_function_relative_addresses in target_info.flags then
  1609. list.concat(Tai_function_name.create(hs));
  1610. until false;
  1611. current_procinfo.procdef.procstarttai:=tai(list.last);
  1612. end;
  1613. procedure gen_proc_symbol_end(list:TAsmList);
  1614. begin
  1615. list.concat(Tai_symbol_end.Createname(current_procinfo.procdef.mangledname));
  1616. current_procinfo.procdef.procendtai:=tai(list.last);
  1617. { finalisation marker for Mac OS X }
  1618. if (target_info.system in [system_powerpc_darwin,system_i386_darwin]) and
  1619. (current_module.islibrary) and
  1620. (((current_module.flags and uf_finalize)<>0) or
  1621. (current_procinfo.procdef.proctypeoption = potype_proginit)) then
  1622. begin
  1623. if (current_procinfo.procdef.proctypeoption = potype_proginit) then
  1624. list.concat(tai_directive.create(asd_mod_init_func,''))
  1625. else
  1626. list.concat(tai_directive.create(asd_mod_term_func,''));
  1627. list.concat(tai_align.create(4));
  1628. list.concat(Tai_const.Createname(current_procinfo.procdef.mangledname,AT_FUNCTION,0));
  1629. end;
  1630. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1631. begin
  1632. if (target_info.system in [system_powerpc_darwin,system_i386_darwin,system_powerpc_macos]) and
  1633. not(current_module.islibrary) then
  1634. begin
  1635. list.concat(tai_section.create(sec_code,'',4));
  1636. list.concat(tai_symbol.createname_global(
  1637. target_info.cprefix+mainaliasname,AT_FUNCTION,0));
  1638. { keep argc, argv and envp properly on the stack }
  1639. cg.a_jmp_name(list,target_info.cprefix+'FPC_SYSTEMMAIN');
  1640. end;
  1641. { Reference all DEBUGINFO sections from the main .text section }
  1642. if (cs_debuginfo in aktmoduleswitches) then
  1643. debuginfo.referencesections(list);
  1644. { Insert Ident of the compiler in the main .text section }
  1645. if (not (cs_create_smart in aktmoduleswitches)) then
  1646. begin
  1647. list.concat(Tai_section.create(sec_data,'',0));
  1648. list.concat(Tai_align.Create(const_align(32)));
  1649. list.concat(Tai_string.Create('FPC '+full_version_string+
  1650. ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname));
  1651. end;
  1652. end;
  1653. end;
  1654. procedure gen_proc_entry_code(list:TAsmList);
  1655. var
  1656. hitemp,
  1657. lotemp : longint;
  1658. begin
  1659. { generate call frame marker for dwarf call frame info }
  1660. current_asmdata.asmcfi.start_frame(list);
  1661. { All temps are know, write offsets used for information }
  1662. if (cs_asm_source in aktglobalswitches) then
  1663. begin
  1664. if tg.direction>0 then
  1665. begin
  1666. lotemp:=current_procinfo.tempstart;
  1667. hitemp:=tg.lasttemp;
  1668. end
  1669. else
  1670. begin
  1671. lotemp:=tg.lasttemp;
  1672. hitemp:=current_procinfo.tempstart;
  1673. end;
  1674. list.concat(Tai_comment.Create(strpnew('Temps allocated between '+std_regname(current_procinfo.framepointer)+
  1675. tostr_with_plus(lotemp)+' and '+std_regname(current_procinfo.framepointer)+tostr_with_plus(hitemp))));
  1676. end;
  1677. { generate target specific proc entry code }
  1678. cg.g_proc_entry(list,current_procinfo.calc_stackframe_size,(po_nostackframe in current_procinfo.procdef.procoptions));
  1679. end;
  1680. procedure gen_proc_exit_code(list:TAsmList);
  1681. var
  1682. parasize : longint;
  1683. begin
  1684. { c style clearstack does not need to remove parameters from the stack, only the
  1685. return value when it was pushed by arguments }
  1686. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1687. begin
  1688. parasize:=0;
  1689. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1690. inc(parasize,sizeof(aint));
  1691. end
  1692. else
  1693. parasize:=current_procinfo.para_stack_size;
  1694. { generate target specific proc exit code }
  1695. cg.g_proc_exit(list,parasize,(po_nostackframe in current_procinfo.procdef.procoptions));
  1696. { release return registers, needed for optimizer }
  1697. if not is_void(current_procinfo.procdef.rettype.def) then
  1698. location_free(list,current_procinfo.procdef.funcretloc[calleeside]);
  1699. { end of frame marker for call frame info }
  1700. current_asmdata.asmcfi.end_frame(list);
  1701. end;
  1702. procedure gen_stack_check_size_para(list:TAsmList);
  1703. var
  1704. paraloc1 : tcgpara;
  1705. begin
  1706. paraloc1.init;
  1707. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1708. paramanager.allocparaloc(list,paraloc1);
  1709. cg.a_param_const(list,OS_INT,current_procinfo.calc_stackframe_size,paraloc1);
  1710. paramanager.freeparaloc(list,paraloc1);
  1711. paraloc1.done;
  1712. end;
  1713. procedure gen_stack_check_call(list:TAsmList);
  1714. var
  1715. paraloc1 : tcgpara;
  1716. begin
  1717. paraloc1.init;
  1718. { Also alloc the register needed for the parameter }
  1719. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1720. paramanager.allocparaloc(list,paraloc1);
  1721. paramanager.freeparaloc(list,paraloc1);
  1722. { Call the helper }
  1723. cg.allocallcpuregisters(list);
  1724. cg.a_call_name(list,'FPC_STACKCHECK');
  1725. cg.deallocallcpuregisters(list);
  1726. paraloc1.done;
  1727. end;
  1728. procedure gen_save_used_regs(list:TAsmList);
  1729. begin
  1730. { Pure assembler routines need to save the registers themselves }
  1731. if (po_assembler in current_procinfo.procdef.procoptions) then
  1732. exit;
  1733. { oldfpccall expects all registers to be destroyed }
  1734. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1735. cg.g_save_standard_registers(list);
  1736. end;
  1737. procedure gen_restore_used_regs(list:TAsmList);
  1738. begin
  1739. { Pure assembler routines need to save the registers themselves }
  1740. if (po_assembler in current_procinfo.procdef.procoptions) then
  1741. exit;
  1742. { oldfpccall expects all registers to be destroyed }
  1743. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1744. cg.g_restore_standard_registers(list);
  1745. end;
  1746. procedure gen_got_load(list : TAsmList);
  1747. begin
  1748. { if loading got is necessary for more cpus, it can be moved
  1749. to the cg }
  1750. {$ifdef i386}
  1751. { allocate PIC register }
  1752. if (cs_create_pic in aktmoduleswitches) and
  1753. (tf_pic_uses_got in target_info.flags) and
  1754. (pi_needs_got in current_procinfo.flags) then
  1755. begin
  1756. current_module.requires_ebx_pic_helper:=true;
  1757. cg.a_call_name_static(list,'fpc_geteipasebx');
  1758. list.concat(taicpu.op_sym_ofs_reg(A_ADD,S_L,current_asmdata.newasmsymbol('_GLOBAL_OFFSET_TABLE_',AB_EXTERNAL,AT_DATA),0,NR_PIC_OFFSET_REG));
  1759. list.concat(tai_regalloc.alloc(NR_PIC_OFFSET_REG,nil));
  1760. { ecx could be used in leave procedures }
  1761. current_procinfo.got:=NR_EBX;
  1762. end;
  1763. {$endif i386}
  1764. end;
  1765. {****************************************************************************
  1766. External handling
  1767. ****************************************************************************}
  1768. procedure gen_external_stub(list:TAsmList;pd:tprocdef;const externalname:string);
  1769. begin
  1770. { add the procedure to the al_procedures }
  1771. maybe_new_object_file(list);
  1772. new_section(list,sec_code,lower(pd.mangledname),aktalignment.procalign);
  1773. list.concat(Tai_align.create(aktalignment.procalign));
  1774. if (po_global in pd.procoptions) then
  1775. list.concat(Tai_symbol.createname_global(pd.mangledname,AT_FUNCTION,0))
  1776. else
  1777. list.concat(Tai_symbol.createname(pd.mangledname,AT_FUNCTION,0));
  1778. cg.a_jmp_name(list,externalname);
  1779. end;
  1780. {****************************************************************************
  1781. Const Data
  1782. ****************************************************************************}
  1783. procedure insertbssdata(sym : tglobalvarsym);
  1784. var
  1785. l : aint;
  1786. varalign : shortint;
  1787. storefilepos : tfileposinfo;
  1788. list : TAsmList;
  1789. sectype : TAsmSectiontype;
  1790. begin
  1791. storefilepos:=aktfilepos;
  1792. aktfilepos:=sym.fileinfo;
  1793. l:=sym.getsize;
  1794. if tf_section_threadvars in target_info.flags then
  1795. begin
  1796. if (vo_is_thread_var in sym.varoptions) then
  1797. begin
  1798. list:=current_asmdata.asmlists[al_threadvars];
  1799. sectype:=sec_threadvar;
  1800. end
  1801. else
  1802. begin
  1803. list:=current_asmdata.asmlists[al_globals];
  1804. sectype:=sec_bss;
  1805. end;
  1806. end
  1807. else
  1808. begin
  1809. if (vo_is_thread_var in sym.varoptions) then
  1810. inc(l,sizeof(aint));
  1811. list:=current_asmdata.asmlists[al_globals];
  1812. sectype:=sec_bss;
  1813. end;
  1814. varalign:=var_align(size_2_align(l));
  1815. maybe_new_object_file(list);
  1816. new_section(list,sectype,lower(sym.mangledname),varalign);
  1817. if (sym.owner.symtabletype=globalsymtable) or
  1818. maybe_smartlink_symbol or
  1819. DLLSource or
  1820. (assigned(current_procinfo) and
  1821. (po_inline in current_procinfo.procdef.procoptions)) or
  1822. (vo_is_exported in sym.varoptions) or
  1823. (vo_is_C_var in sym.varoptions) then
  1824. list.concat(Tai_datablock.create_global(sym.mangledname,l))
  1825. else
  1826. list.concat(Tai_datablock.create(sym.mangledname,l));
  1827. aktfilepos:=storefilepos;
  1828. end;
  1829. procedure init_regvar_loc(sym:tabstractnormalvarsym;cgsize:tcgsize);
  1830. begin
  1831. with sym do
  1832. begin
  1833. localloc.size:=cgsize;
  1834. case varregable of
  1835. vr_intreg :
  1836. begin
  1837. localloc.loc:=LOC_CREGISTER;
  1838. end;
  1839. vr_fpureg :
  1840. begin
  1841. localloc.loc:=LOC_CFPUREGISTER;
  1842. end;
  1843. vr_mmreg :
  1844. begin
  1845. localloc.loc:=LOC_CMMREGISTER;
  1846. end;
  1847. else
  1848. internalerror(2004101010);
  1849. end;
  1850. end;
  1851. end;
  1852. procedure gen_alloc_symtable(list:TAsmList;st:tsymtable);
  1853. var
  1854. sym : tsym;
  1855. isaddr : boolean;
  1856. cgsize : tcgsize;
  1857. begin
  1858. sym:=tsym(st.symindex.first);
  1859. while assigned(sym) do
  1860. begin
  1861. if (sym.typ in [globalvarsym,localvarsym,paravarsym]) then
  1862. begin
  1863. with tabstractnormalvarsym(sym) do
  1864. begin
  1865. { Parameters passed to assembler procedures need to be kept
  1866. in the original location }
  1867. if (sym.typ=paravarsym) and
  1868. (po_assembler in current_procinfo.procdef.procoptions) then
  1869. begin
  1870. tparavarsym(sym).paraloc[calleeside].get_location(localloc);
  1871. end
  1872. else
  1873. begin
  1874. isaddr:=(st.symtabletype=parasymtable) and
  1875. paramanager.push_addr_param(varspez,vartype.def,current_procinfo.procdef.proccalloption);
  1876. if isaddr then
  1877. cgsize:=OS_ADDR
  1878. else
  1879. cgsize:=def_cgsize(vartype.def);
  1880. {$ifndef OLDREGVARS}
  1881. { When there is assembler code we can't use regvars }
  1882. if is_regvar then
  1883. begin
  1884. init_regvar_loc(tabstractnormalvarsym(sym),cgsize);
  1885. if (st.symtabletype <> parasymtable) then
  1886. gen_alloc_regvar(list,tabstractnormalvarsym(sym));
  1887. end
  1888. else
  1889. {$endif NOT OLDREGVARS}
  1890. begin
  1891. localloc.loc:=LOC_REFERENCE;
  1892. localloc.size:=cgsize;
  1893. case st.symtabletype of
  1894. parasymtable :
  1895. begin
  1896. { Reuse the parameter location for values to are at a single location on the stack }
  1897. if paramanager.param_use_paraloc(tparavarsym(sym).paraloc[calleeside]) then
  1898. begin
  1899. reference_reset_base(localloc.reference,tparavarsym(sym).paraloc[calleeside].location^.reference.index,
  1900. tparavarsym(sym).paraloc[calleeside].location^.reference.offset);
  1901. end
  1902. else
  1903. begin
  1904. if isaddr then
  1905. tg.GetLocal(list,sizeof(aint),voidpointertype.def,localloc.reference)
  1906. else
  1907. tg.GetLocal(list,getsize,vartype.def,localloc.reference);
  1908. end;
  1909. end;
  1910. localsymtable,
  1911. stt_exceptsymtable :
  1912. begin
  1913. tg.GetLocal(list,getsize,vartype.def,localloc.reference);
  1914. end;
  1915. staticsymtable :
  1916. begin
  1917. { PIC, DLL and Threadvar need extra code and are handled in ncgld }
  1918. if not(vo_is_dll_var in varoptions) and ((tf_section_threadvars in target_info.flags) or
  1919. not(vo_is_thread_var in varoptions)) then
  1920. reference_reset_symbol(localloc.reference,current_asmdata.newasmsymbol(mangledname,AB_EXTERNAL,AT_DATA),0);
  1921. end;
  1922. else
  1923. internalerror(200410103);
  1924. end;
  1925. end;
  1926. end;
  1927. if cs_asm_source in aktglobalswitches then
  1928. begin
  1929. case localloc.loc of
  1930. LOC_REFERENCE :
  1931. begin
  1932. if not assigned(localloc.reference.symbol) then
  1933. list.concat(Tai_comment.Create(strpnew('Var '+realname+' located at '+
  1934. std_regname(localloc.reference.base)+tostr_with_plus(localloc.reference.offset))));
  1935. end;
  1936. end;
  1937. end;
  1938. end;
  1939. end;
  1940. sym:=tsym(sym.indexnext);
  1941. end;
  1942. end;
  1943. procedure add_regvars(var rv: tusedregvars; const location: tlocation);
  1944. begin
  1945. case location.loc of
  1946. LOC_CREGISTER:
  1947. {$ifndef cpu64bit}
  1948. if location.size in [OS_64,OS_S64] then
  1949. begin
  1950. rv.intregvars.addnodup(getsupreg(location.register64.reglo));
  1951. rv.intregvars.addnodup(getsupreg(location.register64.reghi));
  1952. end
  1953. else
  1954. {$endif cpu64bit}
  1955. rv.intregvars.addnodup(getsupreg(location.register));
  1956. LOC_CFPUREGISTER:
  1957. rv.fpuregvars.addnodup(getsupreg(location.register));
  1958. LOC_CMMREGISTER:
  1959. rv.mmregvars.addnodup(getsupreg(location.register));
  1960. end;
  1961. end;
  1962. function do_get_used_regvars(var n: tnode; arg: pointer): foreachnoderesult;
  1963. var
  1964. rv: pusedregvars absolute arg;
  1965. begin
  1966. case (n.nodetype) of
  1967. temprefn:
  1968. { We only have to synchronise a tempnode before a loop if it is }
  1969. { not created inside the loop, and only synchronise after the }
  1970. { loop if it's not destroyed inside the loop. If it's created }
  1971. { before the loop and not yet destroyed, then before the loop }
  1972. { is secondpassed tempinfo^.valid will be true, and we get the }
  1973. { correct registers. If it's not destroyed inside the loop, }
  1974. { then after the loop has been secondpassed tempinfo^.valid }
  1975. { be true and we also get the right registers. In other cases, }
  1976. { tempinfo^.valid will be false and so we do not add }
  1977. { unnecessary registers. This way, we don't have to look at }
  1978. { tempcreate and tempdestroy nodes to get this info (JM) }
  1979. if (ttemprefnode(n).tempinfo^.valid) then
  1980. add_regvars(rv^,ttemprefnode(n).tempinfo^.location);
  1981. loadn:
  1982. if (tloadnode(n).symtableentry.typ in [globalvarsym,localvarsym,paravarsym]) then
  1983. add_regvars(rv^,tabstractnormalvarsym(tloadnode(n).symtableentry).localloc);
  1984. end;
  1985. result := fen_true;
  1986. end;
  1987. procedure get_used_regvars(n: tnode; var rv: tusedregvars);
  1988. begin
  1989. foreachnodestatic(n,@do_get_used_regvars,@rv);
  1990. end;
  1991. (*
  1992. See comments at declaration of pusedregvarscommon
  1993. function do_get_used_regvars_common(var n: tnode; arg: pointer): foreachnoderesult;
  1994. var
  1995. rv: pusedregvarscommon absolute arg;
  1996. begin
  1997. if (n.nodetype = loadn) and
  1998. (tloadnode(n).symtableentry.typ in [globalvarsym,localvarsym,paravarsym]) then
  1999. with tabstractnormalvarsym(tloadnode(n).symtableentry).localloc do
  2000. case loc of
  2001. LOC_CREGISTER:
  2002. { if not yet encountered in this node tree }
  2003. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  2004. { but nevertheless already encountered somewhere }
  2005. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  2006. { then it's a regvar used in two or more node trees }
  2007. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  2008. LOC_CFPUREGISTER:
  2009. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  2010. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  2011. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  2012. LOC_CMMREGISTER:
  2013. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  2014. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  2015. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  2016. end;
  2017. result := fen_true;
  2018. end;
  2019. procedure get_used_regvars_common(n: tnode; var rv: tusedregvarscommon);
  2020. begin
  2021. rv.myregvars.intregvars.clear;
  2022. rv.myregvars.fpuregvars.clear;
  2023. rv.myregvars.mmregvars.clear;
  2024. foreachnodestatic(n,@do_get_used_regvars_common,@rv);
  2025. end;
  2026. *)
  2027. procedure gen_sync_regvars(list:TAsmList; var rv: tusedregvars);
  2028. var
  2029. count: longint;
  2030. begin
  2031. for count := 1 to rv.intregvars.length do
  2032. cg.a_reg_sync(list,newreg(R_INTREGISTER,rv.intregvars.readidx(count-1),R_SUBWHOLE));
  2033. for count := 1 to rv.fpuregvars.length do
  2034. cg.a_reg_sync(list,newreg(R_FPUREGISTER,rv.fpuregvars.readidx(count-1),R_SUBWHOLE));
  2035. for count := 1 to rv.mmregvars.length do
  2036. cg.a_reg_sync(list,newreg(R_MMREGISTER,rv.mmregvars.readidx(count-1),R_SUBWHOLE));
  2037. end;
  2038. procedure gen_free_symtable(list:TAsmList;st:tsymtable);
  2039. var
  2040. sym : tsym;
  2041. begin
  2042. sym:=tsym(st.symindex.first);
  2043. while assigned(sym) do
  2044. begin
  2045. if (sym.typ in [globalvarsym,localvarsym,paravarsym]) then
  2046. begin
  2047. with tabstractnormalvarsym(sym) do
  2048. begin
  2049. { Note: We need to keep the data available in memory
  2050. for the sub procedures that can access local data
  2051. in the parent procedures }
  2052. case localloc.loc of
  2053. LOC_CREGISTER :
  2054. {$ifndef cpu64bit}
  2055. if (pi_has_goto in current_procinfo.flags) then
  2056. if def_cgsize(vartype.def) in [OS_64,OS_S64] then
  2057. begin
  2058. cg.a_reg_sync(list,localloc.register64.reglo);
  2059. cg.a_reg_sync(list,localloc.register64.reghi);
  2060. end
  2061. else
  2062. {$endif cpu64bit}
  2063. cg.a_reg_sync(list,localloc.register);
  2064. LOC_CFPUREGISTER,
  2065. LOC_CMMREGISTER:
  2066. if (pi_has_goto in current_procinfo.flags) then
  2067. cg.a_reg_sync(list,localloc.register);
  2068. LOC_REFERENCE :
  2069. begin
  2070. case st.symtabletype of
  2071. localsymtable,
  2072. parasymtable,
  2073. stt_exceptsymtable :
  2074. tg.Ungetlocal(list,localloc.reference);
  2075. end;
  2076. end;
  2077. end;
  2078. end;
  2079. end;
  2080. sym:=tsym(sym.indexnext);
  2081. end;
  2082. end;
  2083. { persistent rtti generation }
  2084. procedure generate_rtti(p:Ttypesym);
  2085. var
  2086. rsym : trttisym;
  2087. def : tstoreddef;
  2088. begin
  2089. { rtti can only be generated for classes that are always typesyms }
  2090. def:=tstoreddef(ttypesym(p).restype.def);
  2091. { there is an error, skip rtti info }
  2092. if (def.deftype=errordef) or (Errorcount>0) then
  2093. exit;
  2094. { only create rtti once for each definition }
  2095. if not(df_has_rttitable in def.defoptions) then
  2096. begin
  2097. { definition should be in the same symtable as the symbol }
  2098. if p.owner<>def.owner then
  2099. internalerror(200108262);
  2100. { create rttisym }
  2101. rsym:=trttisym.create(p.name,fullrtti);
  2102. p.owner.insert(rsym);
  2103. { register rttisym in definition }
  2104. include(def.defoptions,df_has_rttitable);
  2105. def.rttitablesym:=rsym;
  2106. { write rtti data }
  2107. def.write_child_rtti_data(fullrtti);
  2108. maybe_new_object_file(current_asmdata.asmlists[al_rtti]);
  2109. new_section(current_asmdata.asmlists[al_rtti],sec_rodata,rsym.get_label.name,const_align(sizeof(aint)));
  2110. current_asmdata.asmlists[al_rtti].concat(Tai_symbol.Create_global(rsym.get_label,0));
  2111. def.write_rtti_data(fullrtti);
  2112. current_asmdata.asmlists[al_rtti].concat(Tai_symbol_end.Create(rsym.get_label));
  2113. end;
  2114. end;
  2115. { persistent init table generation }
  2116. procedure generate_inittable(p:tsym);
  2117. var
  2118. rsym : trttisym;
  2119. def : tstoreddef;
  2120. begin
  2121. { anonymous types are also allowed for records that can be varsym }
  2122. case p.typ of
  2123. typesym :
  2124. def:=tstoreddef(ttypesym(p).restype.def);
  2125. globalvarsym,
  2126. localvarsym,
  2127. paravarsym :
  2128. def:=tstoreddef(tabstractvarsym(p).vartype.def);
  2129. else
  2130. internalerror(200108263);
  2131. end;
  2132. { only create inittable once for each definition }
  2133. if not(df_has_inittable in def.defoptions) then
  2134. begin
  2135. { definition should be in the same symtable as the symbol }
  2136. if p.owner<>def.owner then
  2137. internalerror(200108264);
  2138. { create rttisym }
  2139. rsym:=trttisym.create(p.name,initrtti);
  2140. p.owner.insert(rsym);
  2141. { register rttisym in definition }
  2142. include(def.defoptions,df_has_inittable);
  2143. def.inittablesym:=rsym;
  2144. { write inittable data }
  2145. def.write_child_rtti_data(initrtti);
  2146. maybe_new_object_file(current_asmdata.asmlists[al_rtti]);
  2147. new_section(current_asmdata.asmlists[al_rtti],sec_rodata,rsym.get_label.name,const_align(sizeof(aint)));
  2148. current_asmdata.asmlists[al_rtti].concat(Tai_symbol.Create_global(rsym.get_label,0));
  2149. def.write_rtti_data(initrtti);
  2150. current_asmdata.asmlists[al_rtti].concat(Tai_symbol_end.Create(rsym.get_label));
  2151. end;
  2152. end;
  2153. procedure gen_intf_wrapper(list:TAsmList;_class:tobjectdef);
  2154. var
  2155. i,j,
  2156. proccount : longint;
  2157. tmps : string;
  2158. begin
  2159. for i:=1 to _class.implementedinterfaces.count do
  2160. begin
  2161. { only if implemented by this class }
  2162. if _class.implementedinterfaces.implindex(i)=i then
  2163. begin
  2164. proccount:=_class.implementedinterfaces.implproccount(i);
  2165. for j:=1 to proccount do
  2166. begin
  2167. tmps:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+
  2168. _class.implementedinterfaces.interfaces(i).objname^+'_$_'+
  2169. tostr(j)+'_$_'+_class.implementedinterfaces.implprocs(i,j).mangledname);
  2170. { create wrapper code }
  2171. new_section(list,sec_code,lower(tmps),0);
  2172. cg.init_register_allocators;
  2173. cg.g_intf_wrapper(list,_class.implementedinterfaces.implprocs(i,j),tmps,_class.implementedinterfaces.ioffsets(i));
  2174. cg.done_register_allocators;
  2175. end;
  2176. end;
  2177. end;
  2178. end;
  2179. procedure gen_intf_wrappers(list:TAsmList;st:tsymtable);
  2180. var
  2181. def : tstoreddef;
  2182. begin
  2183. def:=tstoreddef(st.defindex.first);
  2184. while assigned(def) do
  2185. begin
  2186. if is_class(def) then
  2187. gen_intf_wrapper(list,tobjectdef(def));
  2188. def:=tstoreddef(def.indexnext);
  2189. end;
  2190. end;
  2191. procedure gen_load_vmt_register(list:TAsmList;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  2192. var
  2193. href : treference;
  2194. begin
  2195. if is_object(objdef) then
  2196. begin
  2197. case selfloc.loc of
  2198. LOC_CREFERENCE,
  2199. LOC_REFERENCE:
  2200. begin
  2201. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset);
  2202. cg.a_loadaddr_ref_reg(list,selfloc.reference,href.base);
  2203. end;
  2204. else
  2205. internalerror(200305056);
  2206. end;
  2207. end
  2208. else
  2209. begin
  2210. case selfloc.loc of
  2211. LOC_REGISTER:
  2212. begin
  2213. {$ifdef cpu_uses_separate_address_registers}
  2214. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  2215. begin
  2216. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset);
  2217. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,selfloc.register,href.base);
  2218. end
  2219. else
  2220. {$endif cpu_uses_separate_address_registers}
  2221. reference_reset_base(href,selfloc.register,objdef.vmt_offset);
  2222. end;
  2223. LOC_CREGISTER,
  2224. LOC_CREFERENCE,
  2225. LOC_REFERENCE:
  2226. begin
  2227. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset);
  2228. cg.a_load_loc_reg(list,OS_ADDR,selfloc,href.base);
  2229. end;
  2230. else
  2231. internalerror(200305057);
  2232. end;
  2233. end;
  2234. vmtreg:=cg.getaddressregister(list);
  2235. cg.g_maybe_testself(list,href.base);
  2236. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,vmtreg);
  2237. { test validity of VMT }
  2238. if not(is_interface(objdef)) and
  2239. not(is_cppclass(objdef)) then
  2240. cg.g_maybe_testvmt(list,vmtreg,objdef);
  2241. end;
  2242. function getprocalign : shortint;
  2243. begin
  2244. { gprof uses 16 byte granularity }
  2245. if (cs_profile in aktmoduleswitches) then
  2246. result:=16
  2247. else
  2248. result:=aktalignment.procalign;
  2249. end;
  2250. procedure gen_pic_helpers(list : TAsmList);
  2251. var
  2252. href : treference;
  2253. begin
  2254. { if other cpus require such helpers as well, it can be solved more cleaner }
  2255. {$ifdef i386}
  2256. if current_module.requires_ebx_pic_helper then
  2257. begin
  2258. new_section(list,sec_code,'fpc_geteipasebx',0);
  2259. list.concat(tai_symbol.Createname('fpc_geteipasebx',AT_FUNCTION,getprocalign));
  2260. reference_reset(href);
  2261. href.base:=NR_ESP;
  2262. list.concat(taicpu.op_ref_reg(A_MOV,S_L,href,NR_EBX));
  2263. list.concat(taicpu.op_none(A_RET,S_NO));
  2264. end;
  2265. if current_module.requires_ecx_pic_helper then
  2266. begin
  2267. new_section(list,sec_code,'fpc_geteipasecx',0);
  2268. list.concat(tai_symbol.Createname('fpc_geteipasecx',AT_FUNCTION,getprocalign));
  2269. reference_reset(href);
  2270. href.base:=NR_ESP;
  2271. list.concat(taicpu.op_ref_reg(A_MOV,S_L,href,NR_ECX));
  2272. list.concat(taicpu.op_none(A_RET,S_NO));
  2273. end;
  2274. {$endif i386}
  2275. end;
  2276. end.