ncgutil.pas 118 KB

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