ncgutil.pas 124 KB

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