ncgutil.pas 110 KB

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