ncgutil.pas 126 KB

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