ncgutil.pas 106 KB

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