ncgutil.pas 107 KB

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