ncgutil.pas 118 KB

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