ncgutil.pas 111 KB

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