ncgutil.pas 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242
  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_fpureg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  50. procedure location_force_mmregscalar(list:TAsmList;var l: tlocation;maybeconst:boolean);
  51. procedure location_force_mmreg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  52. procedure location_allocate_register(list:TAsmList;out l: tlocation;def: tdef;constant: boolean);
  53. { loads a cgpara into a tlocation; assumes that loc.loc is already
  54. initialised }
  55. procedure gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);
  56. { allocate registers for a tlocation; assumes that loc.loc is already
  57. set to LOC_CREGISTER/LOC_CFPUREGISTER/... }
  58. procedure gen_alloc_regloc(list:TAsmList;var loc: tlocation);
  59. procedure register_maybe_adjust_setbase(list: TAsmList; var l: tlocation; setbase: aint);
  60. function has_alias_name(pd:tprocdef;const s:string):boolean;
  61. procedure alloc_proc_symbol(pd: tprocdef);
  62. procedure gen_proc_symbol(list:TAsmList);
  63. procedure gen_proc_entry_code(list:TAsmList);
  64. procedure gen_proc_exit_code(list:TAsmList);
  65. procedure gen_stack_check_size_para(list:TAsmList);
  66. procedure gen_stack_check_call(list:TAsmList);
  67. procedure gen_save_used_regs(list:TAsmList);
  68. procedure gen_restore_used_regs(list:TAsmList);
  69. procedure gen_load_para_value(list:TAsmList);
  70. procedure gen_external_stub(list:TAsmList;pd:tprocdef;const externalname:string);
  71. procedure gen_intf_wrappers(list:TAsmList;st:TSymtable;nested:boolean);
  72. procedure gen_load_vmt_register(list:TAsmList;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  73. procedure get_used_regvars(n: tnode; var rv: tusedregvars);
  74. { adds the regvars used in n and its children to rv.allregvars,
  75. those which were already in rv.allregvars to rv.commonregvars and
  76. uses rv.myregvars as scratch (so that two uses of the same regvar
  77. in a single tree to make it appear in commonregvars). Useful to
  78. find out which regvars are used in two different node trees
  79. (e.g. in the "else" and "then" path, or in various case blocks }
  80. // procedure get_used_regvars_common(n: tnode; var rv: tusedregvarscommon);
  81. procedure gen_sync_regvars(list:TAsmList; var rv: tusedregvars);
  82. { if the result of n is a LOC_C(..)REGISTER, try to find the corresponding }
  83. { loadn and change its location to a new register (= SSA). In case reload }
  84. { is true, transfer the old to the new register }
  85. procedure maybechangeloadnodereg(list: TAsmList; var n: tnode; reload: boolean);
  86. {#
  87. Allocate the buffers for exception management and setjmp environment.
  88. Return a pointer to these buffers, send them to the utility routine
  89. so they are registered, and then call setjmp.
  90. Then compare the result of setjmp with 0, and if not equal
  91. to zero, then jump to exceptlabel.
  92. Also store the result of setjmp to a temporary space by calling g_save_exception_reason
  93. It is to note that this routine may be called *after* the stackframe of a
  94. routine has been called, therefore on machines where the stack cannot
  95. be modified, all temps should be allocated on the heap instead of the
  96. stack.
  97. }
  98. const
  99. EXCEPT_BUF_SIZE = 3*sizeof(pint);
  100. type
  101. texceptiontemps=record
  102. jmpbuf,
  103. envbuf,
  104. reasonbuf : treference;
  105. end;
  106. procedure get_exception_temps(list:TAsmList;var t:texceptiontemps);
  107. procedure unget_exception_temps(list:TAsmList;const t:texceptiontemps);
  108. procedure new_exception(list:TAsmList;const t:texceptiontemps;exceptlabel:tasmlabel);
  109. procedure free_exception(list:TAsmList;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  110. procedure gen_alloc_symtable(list:TAsmList;pd:tprocdef;st:TSymtable);
  111. procedure gen_free_symtable(list:TAsmList;st:TSymtable);
  112. procedure location_free(list: TAsmList; const location : TLocation);
  113. function getprocalign : shortint;
  114. procedure gen_fpc_dummy(list : TAsmList);
  115. procedure InsertInterruptTable;
  116. implementation
  117. uses
  118. version,
  119. cutils,cclasses,
  120. globals,systems,verbose,export,
  121. ppu,defutil,
  122. procinfo,paramgr,fmodule,
  123. regvars,dbgbase,
  124. pass_1,pass_2,
  125. nbas,ncon,nld,nmem,nutils,ngenutil,
  126. tgobj,cgobj,cgcpu,hlcgobj,hlcgcpu
  127. {$ifdef powerpc}
  128. , cpupi
  129. {$endif}
  130. {$ifdef powerpc64}
  131. , cpupi
  132. {$endif}
  133. {$ifdef SUPPORT_MMX}
  134. , cgx86
  135. {$endif SUPPORT_MMX}
  136. ;
  137. {*****************************************************************************
  138. Misc Helpers
  139. *****************************************************************************}
  140. {$if first_mm_imreg = 0}
  141. {$WARN 4044 OFF} { Comparison might be always false ... }
  142. {$endif}
  143. procedure location_free(list: TAsmList; const location : TLocation);
  144. begin
  145. case location.loc of
  146. LOC_VOID:
  147. ;
  148. LOC_REGISTER,
  149. LOC_CREGISTER:
  150. begin
  151. {$ifdef cpu64bitalu}
  152. { x86-64 system v abi:
  153. structs with up to 16 bytes are returned in registers }
  154. if location.size in [OS_128,OS_S128] then
  155. begin
  156. if getsupreg(location.register)<first_int_imreg then
  157. cg.ungetcpuregister(list,location.register);
  158. if getsupreg(location.registerhi)<first_int_imreg then
  159. cg.ungetcpuregister(list,location.registerhi);
  160. end
  161. {$else cpu64bitalu}
  162. if location.size in [OS_64,OS_S64] then
  163. begin
  164. if getsupreg(location.register64.reglo)<first_int_imreg then
  165. cg.ungetcpuregister(list,location.register64.reglo);
  166. if getsupreg(location.register64.reghi)<first_int_imreg then
  167. cg.ungetcpuregister(list,location.register64.reghi);
  168. end
  169. {$endif cpu64bitalu}
  170. else
  171. if getsupreg(location.register)<first_int_imreg then
  172. cg.ungetcpuregister(list,location.register);
  173. end;
  174. LOC_FPUREGISTER,
  175. LOC_CFPUREGISTER:
  176. begin
  177. if getsupreg(location.register)<first_fpu_imreg then
  178. cg.ungetcpuregister(list,location.register);
  179. end;
  180. LOC_MMREGISTER,
  181. LOC_CMMREGISTER :
  182. begin
  183. if getsupreg(location.register)<first_mm_imreg then
  184. cg.ungetcpuregister(list,location.register);
  185. end;
  186. LOC_REFERENCE,
  187. LOC_CREFERENCE :
  188. begin
  189. if paramanager.use_fixed_stack then
  190. location_freetemp(list,location);
  191. end;
  192. else
  193. internalerror(2004110211);
  194. end;
  195. end;
  196. procedure firstcomplex(p : tbinarynode);
  197. var
  198. fcl, fcr: longint;
  199. ncl, ncr: longint;
  200. begin
  201. { always calculate boolean AND and OR from left to right }
  202. if (p.nodetype in [orn,andn]) and
  203. is_boolean(p.left.resultdef) then
  204. begin
  205. if nf_swapped in p.flags then
  206. internalerror(200709253);
  207. end
  208. else
  209. begin
  210. fcl:=node_resources_fpu(p.left);
  211. fcr:=node_resources_fpu(p.right);
  212. ncl:=node_complexity(p.left);
  213. ncr:=node_complexity(p.right);
  214. { We swap left and right if
  215. a) right needs more floating point registers than left, and
  216. left needs more than 0 floating point registers (if it
  217. doesn't need any, swapping won't change the floating
  218. point register pressure)
  219. b) both left and right need an equal amount of floating
  220. point registers or right needs no floating point registers,
  221. and in addition right has a higher complexity than left
  222. (+- needs more integer registers, but not necessarily)
  223. }
  224. if ((fcr>fcl) and
  225. (fcl>0)) or
  226. (((fcr=fcl) or
  227. (fcr=0)) and
  228. (ncr>ncl)) then
  229. p.swapleftright
  230. end;
  231. end;
  232. procedure maketojumpbool(list:TAsmList; p : tnode; loadregvars: tloadregvars);
  233. {
  234. produces jumps to true respectively false labels using boolean expressions
  235. depending on whether the loading of regvars is currently being
  236. synchronized manually (such as in an if-node) or automatically (most of
  237. the other cases where this procedure is called), loadregvars can be
  238. "lr_load_regvars" or "lr_dont_load_regvars"
  239. }
  240. var
  241. opsize : tcgsize;
  242. storepos : tfileposinfo;
  243. tmpreg : tregister;
  244. begin
  245. if nf_error in p.flags then
  246. exit;
  247. storepos:=current_filepos;
  248. current_filepos:=p.fileinfo;
  249. if is_boolean(p.resultdef) then
  250. begin
  251. {$ifdef OLDREGVARS}
  252. if loadregvars = lr_load_regvars then
  253. load_all_regvars(list);
  254. {$endif OLDREGVARS}
  255. if is_constboolnode(p) then
  256. begin
  257. if Tordconstnode(p).value.uvalue<>0 then
  258. cg.a_jmp_always(list,current_procinfo.CurrTrueLabel)
  259. else
  260. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel)
  261. end
  262. else
  263. begin
  264. opsize:=def_cgsize(p.resultdef);
  265. case p.location.loc of
  266. LOC_SUBSETREG,LOC_CSUBSETREG,
  267. LOC_SUBSETREF,LOC_CSUBSETREF:
  268. begin
  269. tmpreg := cg.getintregister(list,OS_INT);
  270. hlcg.a_load_loc_reg(list,p.resultdef,osuinttype,p.location,tmpreg);
  271. cg.a_cmp_const_reg_label(list,OS_INT,OC_NE,0,tmpreg,current_procinfo.CurrTrueLabel);
  272. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel);
  273. end;
  274. LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE :
  275. begin
  276. {$ifdef cpu64bitalu}
  277. if opsize in [OS_128,OS_S128] then
  278. begin
  279. hlcg.location_force_reg(list,p.location,p.resultdef,hlcg.tcgsize2orddef(opsize),true);
  280. tmpreg:=cg.getintregister(list,OS_64);
  281. cg.a_op_reg_reg_reg(list,OP_OR,OS_64,p.location.register128.reglo,p.location.register128.reghi,tmpreg);
  282. location_reset(p.location,LOC_REGISTER,OS_64);
  283. p.location.register:=tmpreg;
  284. opsize:=OS_64;
  285. end;
  286. {$else cpu64bitalu}
  287. if opsize in [OS_64,OS_S64] then
  288. begin
  289. hlcg.location_force_reg(list,p.location,p.resultdef,hlcg.tcgsize2orddef(opsize),true);
  290. tmpreg:=cg.getintregister(list,OS_32);
  291. cg.a_op_reg_reg_reg(list,OP_OR,OS_32,p.location.register64.reglo,p.location.register64.reghi,tmpreg);
  292. location_reset(p.location,LOC_REGISTER,OS_32);
  293. p.location.register:=tmpreg;
  294. opsize:=OS_32;
  295. end;
  296. {$endif cpu64bitalu}
  297. cg.a_cmp_const_loc_label(list,opsize,OC_NE,0,p.location,current_procinfo.CurrTrueLabel);
  298. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel);
  299. end;
  300. LOC_JUMP:
  301. ;
  302. {$ifdef cpuflags}
  303. LOC_FLAGS :
  304. begin
  305. cg.a_jmp_flags(list,p.location.resflags,current_procinfo.CurrTrueLabel);
  306. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  307. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel);
  308. end;
  309. {$endif cpuflags}
  310. else
  311. begin
  312. printnode(output,p);
  313. internalerror(200308241);
  314. end;
  315. end;
  316. end;
  317. end
  318. else
  319. internalerror(200112305);
  320. current_filepos:=storepos;
  321. end;
  322. (*
  323. This code needs fixing. It is not safe to use rgint; on the m68000 it
  324. would be rgaddr.
  325. procedure remove_non_regvars_from_loc(const t: tlocation; var regs:Tsuperregisterset);
  326. begin
  327. case t.loc of
  328. LOC_REGISTER:
  329. begin
  330. { can't be a regvar, since it would be LOC_CREGISTER then }
  331. exclude(regs,getsupreg(t.register));
  332. if t.register64.reghi<>NR_NO then
  333. exclude(regs,getsupreg(t.register64.reghi));
  334. end;
  335. LOC_CREFERENCE,LOC_REFERENCE:
  336. begin
  337. if not(cs_opt_regvar in current_settings.optimizerswitches) or
  338. (getsupreg(t.reference.base) in cg.rgint.usableregs) then
  339. exclude(regs,getsupreg(t.reference.base));
  340. if not(cs_opt_regvar in current_settings.optimizerswitches) or
  341. (getsupreg(t.reference.index) in cg.rgint.usableregs) then
  342. exclude(regs,getsupreg(t.reference.index));
  343. end;
  344. end;
  345. end;
  346. *)
  347. {*****************************************************************************
  348. EXCEPTION MANAGEMENT
  349. *****************************************************************************}
  350. procedure get_exception_temps(list:TAsmList;var t:texceptiontemps);
  351. var
  352. srsym : ttypesym;
  353. begin
  354. if jmp_buf_size=-1 then
  355. begin
  356. srsym:=search_system_type('JMP_BUF');
  357. jmp_buf_size:=srsym.typedef.size;
  358. jmp_buf_align:=srsym.typedef.alignment;
  359. end;
  360. tg.GetTemp(list,EXCEPT_BUF_SIZE,sizeof(pint),tt_persistent,t.envbuf);
  361. tg.GetTemp(list,jmp_buf_size,jmp_buf_align,tt_persistent,t.jmpbuf);
  362. tg.GetTemp(list,sizeof(pint),sizeof(pint),tt_persistent,t.reasonbuf);
  363. end;
  364. procedure unget_exception_temps(list:TAsmList;const t:texceptiontemps);
  365. begin
  366. tg.Ungettemp(list,t.jmpbuf);
  367. tg.ungettemp(list,t.envbuf);
  368. tg.ungettemp(list,t.reasonbuf);
  369. end;
  370. procedure new_exception(list:TAsmList;const t:texceptiontemps;exceptlabel:tasmlabel);
  371. var
  372. paraloc1,paraloc2,paraloc3 : tcgpara;
  373. begin
  374. paraloc1.init;
  375. paraloc2.init;
  376. paraloc3.init;
  377. paramanager.getintparaloc(pocall_default,1,s32inttype,paraloc1);
  378. paramanager.getintparaloc(pocall_default,2,voidpointertype,paraloc2);
  379. paramanager.getintparaloc(pocall_default,3,voidpointertype,paraloc3);
  380. cg.a_loadaddr_ref_cgpara(list,t.envbuf,paraloc3);
  381. cg.a_loadaddr_ref_cgpara(list,t.jmpbuf,paraloc2);
  382. { push type of exceptionframe }
  383. cg.a_load_const_cgpara(list,OS_S32,1,paraloc1);
  384. paramanager.freecgpara(list,paraloc3);
  385. paramanager.freecgpara(list,paraloc2);
  386. paramanager.freecgpara(list,paraloc1);
  387. cg.allocallcpuregisters(list);
  388. cg.a_call_name(list,'FPC_PUSHEXCEPTADDR',false);
  389. cg.deallocallcpuregisters(list);
  390. paramanager.getintparaloc(pocall_default,1,search_system_type('PJMP_BUF').typedef,paraloc1);
  391. cg.a_load_reg_cgpara(list,OS_ADDR,NR_FUNCTION_RESULT_REG,paraloc1);
  392. paramanager.freecgpara(list,paraloc1);
  393. cg.allocallcpuregisters(list);
  394. cg.a_call_name(list,'FPC_SETJMP',false);
  395. cg.deallocallcpuregisters(list);
  396. cg.alloccpuregisters(list,R_INTREGISTER,[RS_FUNCTION_RESULT_REG]);
  397. cg.g_exception_reason_save(list, t.reasonbuf);
  398. cg.a_cmp_const_reg_label(list,OS_S32,OC_NE,0,cg.makeregsize(list,NR_FUNCTION_RESULT_REG,OS_S32),exceptlabel);
  399. cg.dealloccpuregisters(list,R_INTREGISTER,[RS_FUNCTION_RESULT_REG]);
  400. paraloc1.done;
  401. paraloc2.done;
  402. paraloc3.done;
  403. end;
  404. procedure free_exception(list:TAsmList;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  405. begin
  406. cg.allocallcpuregisters(list);
  407. cg.a_call_name(list,'FPC_POPADDRSTACK',false);
  408. cg.deallocallcpuregisters(list);
  409. if not onlyfree then
  410. begin
  411. { g_exception_reason_load already allocates NR_FUNCTION_RESULT_REG }
  412. cg.g_exception_reason_load(list, t.reasonbuf);
  413. cg.a_cmp_const_reg_label(list,OS_INT,OC_EQ,a,NR_FUNCTION_RESULT_REG,endexceptlabel);
  414. cg.a_reg_dealloc(list,NR_FUNCTION_RESULT_REG);
  415. end;
  416. end;
  417. {*****************************************************************************
  418. TLocation
  419. *****************************************************************************}
  420. procedure location_force_fpureg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  421. var
  422. reg : tregister;
  423. href : treference;
  424. begin
  425. if (l.loc<>LOC_FPUREGISTER) and
  426. ((l.loc<>LOC_CFPUREGISTER) or (not maybeconst)) then
  427. begin
  428. { if it's in an mm register, store to memory first }
  429. if (l.loc in [LOC_MMREGISTER,LOC_CMMREGISTER]) then
  430. begin
  431. tg.GetTemp(list,tcgsize2size[l.size],tcgsize2size[l.size],tt_normal,href);
  432. cg.a_loadmm_reg_ref(list,l.size,l.size,l.register,href,mms_movescalar);
  433. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  434. l.reference:=href;
  435. end;
  436. reg:=cg.getfpuregister(list,l.size);
  437. cg.a_loadfpu_loc_reg(list,l.size,l,reg);
  438. location_freetemp(list,l);
  439. location_reset(l,LOC_FPUREGISTER,l.size);
  440. l.register:=reg;
  441. end;
  442. end;
  443. procedure location_force_mmregscalar(list:TAsmList;var l: tlocation;maybeconst:boolean);
  444. var
  445. reg : tregister;
  446. href : treference;
  447. newsize : tcgsize;
  448. begin
  449. if (l.loc<>LOC_MMREGISTER) and
  450. ((l.loc<>LOC_CMMREGISTER) or (not maybeconst)) then
  451. begin
  452. { if it's in an fpu register, store to memory first }
  453. if (l.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  454. begin
  455. tg.GetTemp(list,tcgsize2size[l.size],tcgsize2size[l.size],tt_normal,href);
  456. cg.a_loadfpu_reg_ref(list,l.size,l.size,l.register,href);
  457. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  458. l.reference:=href;
  459. end;
  460. {$ifndef cpu64bitalu}
  461. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  462. (l.size in [OS_64,OS_S64]) then
  463. begin
  464. reg:=cg.getmmregister(list,OS_F64);
  465. cg64.a_loadmm_intreg64_reg(list,OS_F64,l.register64,reg);
  466. l.size:=OS_F64
  467. end
  468. else
  469. {$endif not cpu64bitalu}
  470. begin
  471. { on ARM, CFP values may be located in integer registers,
  472. and its second_int_to_real() also uses this routine to
  473. force integer (memory) values in an mmregister }
  474. if (l.size in [OS_32,OS_S32]) then
  475. newsize:=OS_F32
  476. else if (l.size in [OS_64,OS_S64]) then
  477. newsize:=OS_F64
  478. else
  479. newsize:=l.size;
  480. reg:=cg.getmmregister(list,newsize);
  481. hlcg.a_loadmm_loc_reg(list,l.size,newsize,l,reg,mms_movescalar);
  482. l.size:=newsize;
  483. end;
  484. location_freetemp(list,l);
  485. location_reset(l,LOC_MMREGISTER,l.size);
  486. l.register:=reg;
  487. end;
  488. end;
  489. procedure register_maybe_adjust_setbase(list: TAsmList; var l: tlocation; setbase: aint);
  490. var
  491. tmpreg: tregister;
  492. begin
  493. if (setbase<>0) then
  494. begin
  495. if not(l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  496. internalerror(2007091502);
  497. { subtract the setbase }
  498. case l.loc of
  499. LOC_CREGISTER:
  500. begin
  501. tmpreg := cg.getintregister(list,l.size);
  502. cg.a_op_const_reg_reg(list,OP_SUB,l.size,setbase,l.register,tmpreg);
  503. l.loc:=LOC_REGISTER;
  504. l.register:=tmpreg;
  505. end;
  506. LOC_REGISTER:
  507. begin
  508. cg.a_op_const_reg(list,OP_SUB,l.size,setbase,l.register);
  509. end;
  510. end;
  511. end;
  512. end;
  513. procedure location_force_mmreg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  514. var
  515. reg : tregister;
  516. begin
  517. if (l.loc<>LOC_MMREGISTER) and
  518. ((l.loc<>LOC_CMMREGISTER) or (not maybeconst)) then
  519. begin
  520. reg:=cg.getmmregister(list,OS_VECTOR);
  521. hlcg.a_loadmm_loc_reg(list,l.size,OS_VECTOR,l,reg,nil);
  522. location_freetemp(list,l);
  523. location_reset(l,LOC_MMREGISTER,OS_VECTOR);
  524. l.register:=reg;
  525. end;
  526. end;
  527. procedure location_allocate_register(list: TAsmList;out l: tlocation;def: tdef;constant: boolean);
  528. begin
  529. l.size:=def_cgsize(def);
  530. if (def.typ=floatdef) and
  531. not(cs_fp_emulation in current_settings.moduleswitches) then
  532. begin
  533. if use_vectorfpu(def) then
  534. begin
  535. if constant then
  536. location_reset(l,LOC_CMMREGISTER,l.size)
  537. else
  538. location_reset(l,LOC_MMREGISTER,l.size);
  539. l.register:=cg.getmmregister(list,l.size);
  540. end
  541. else
  542. begin
  543. if constant then
  544. location_reset(l,LOC_CFPUREGISTER,l.size)
  545. else
  546. location_reset(l,LOC_FPUREGISTER,l.size);
  547. l.register:=cg.getfpuregister(list,l.size);
  548. end;
  549. end
  550. else
  551. begin
  552. if constant then
  553. location_reset(l,LOC_CREGISTER,l.size)
  554. else
  555. location_reset(l,LOC_REGISTER,l.size);
  556. {$ifdef cpu64bitalu}
  557. if l.size in [OS_128,OS_S128,OS_F128] then
  558. begin
  559. l.register128.reglo:=cg.getintregister(list,OS_64);
  560. l.register128.reghi:=cg.getintregister(list,OS_64);
  561. end
  562. else
  563. {$else cpu64bitalu}
  564. if l.size in [OS_64,OS_S64,OS_F64] then
  565. begin
  566. l.register64.reglo:=cg.getintregister(list,OS_32);
  567. l.register64.reghi:=cg.getintregister(list,OS_32);
  568. end
  569. else
  570. {$endif cpu64bitalu}
  571. { Note: for withs of records (and maybe objects, classes, etc.) an
  572. address register could be set here, but that is later
  573. changed to an intregister neverthless when in the
  574. tcgassignmentnode maybechangeloadnodereg is called for the
  575. temporary node; so the workaround for now is to fix the
  576. symptoms... }
  577. l.register:=cg.getintregister(list,l.size);
  578. end;
  579. end;
  580. {****************************************************************************
  581. Init/Finalize Code
  582. ****************************************************************************}
  583. procedure copyvalueparas(p:TObject;arg:pointer);
  584. var
  585. href : treference;
  586. hreg : tregister;
  587. list : TAsmList;
  588. hsym : tparavarsym;
  589. l : longint;
  590. localcopyloc : tlocation;
  591. sizedef : tdef;
  592. begin
  593. list:=TAsmList(arg);
  594. if (tsym(p).typ=paravarsym) and
  595. (tparavarsym(p).varspez=vs_value) and
  596. (paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  597. begin
  598. { we have no idea about the alignment at the caller side }
  599. hlcg.location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  600. if is_open_array(tparavarsym(p).vardef) or
  601. is_array_of_const(tparavarsym(p).vardef) then
  602. begin
  603. { cdecl functions don't have a high pointer so it is not possible to generate
  604. a local copy }
  605. if not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  606. begin
  607. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  608. if not assigned(hsym) then
  609. internalerror(200306061);
  610. hreg:=cg.getaddressregister(list);
  611. if not is_packed_array(tparavarsym(p).vardef) then
  612. hlcg.g_copyvaluepara_openarray(list,href,hsym.initialloc,tarraydef(tparavarsym(p).vardef),hreg)
  613. else
  614. internalerror(2006080401);
  615. // cg.g_copyvaluepara_packedopenarray(list,href,hsym.intialloc,tarraydef(tparavarsym(p).vardef).elepackedbitsize,hreg);
  616. sizedef:=getpointerdef(tparavarsym(p).vardef);
  617. hlcg.a_load_reg_loc(list,sizedef,sizedef,hreg,tparavarsym(p).initialloc);
  618. end;
  619. end
  620. else
  621. begin
  622. { Allocate space for the local copy }
  623. l:=tparavarsym(p).getsize;
  624. localcopyloc.loc:=LOC_REFERENCE;
  625. localcopyloc.size:=int_cgsize(l);
  626. tg.GetLocal(list,l,tparavarsym(p).vardef,localcopyloc.reference);
  627. { Copy data }
  628. if is_shortstring(tparavarsym(p).vardef) then
  629. begin
  630. { this code is only executed before the code for the body and the entry/exit code is generated
  631. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  632. }
  633. include(current_procinfo.flags,pi_do_call);
  634. hlcg.g_copyshortstring(list,href,localcopyloc.reference,tstringdef(tparavarsym(p).vardef));
  635. end
  636. else if tparavarsym(p).vardef.typ = variantdef then
  637. begin
  638. { this code is only executed before the code for the body and the entry/exit code is generated
  639. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  640. }
  641. include(current_procinfo.flags,pi_do_call);
  642. hlcg.g_copyvariant(list,href,localcopyloc.reference,tvariantdef(tparavarsym(p).vardef))
  643. end
  644. else
  645. begin
  646. { pass proper alignment info }
  647. localcopyloc.reference.alignment:=tparavarsym(p).vardef.alignment;
  648. cg.g_concatcopy(list,href,localcopyloc.reference,tparavarsym(p).vardef.size);
  649. end;
  650. { update localloc of varsym }
  651. tg.Ungetlocal(list,tparavarsym(p).localloc.reference);
  652. tparavarsym(p).localloc:=localcopyloc;
  653. tparavarsym(p).initialloc:=localcopyloc;
  654. end;
  655. end;
  656. end;
  657. { generates the code for incrementing the reference count of parameters and
  658. initialize out parameters }
  659. procedure init_paras(p:TObject;arg:pointer);
  660. var
  661. href : treference;
  662. hsym : tparavarsym;
  663. eldef : tdef;
  664. list : TAsmList;
  665. needs_inittable : boolean;
  666. begin
  667. list:=TAsmList(arg);
  668. if (tsym(p).typ=paravarsym) then
  669. begin
  670. needs_inittable:=is_managed_type(tparavarsym(p).vardef);
  671. if not needs_inittable then
  672. exit;
  673. case tparavarsym(p).varspez of
  674. vs_value :
  675. begin
  676. { variants are already handled by the call to fpc_variant_copy_overwrite if
  677. they are passed by reference }
  678. if not((tparavarsym(p).vardef.typ=variantdef) and
  679. paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  680. begin
  681. hlcg.location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,is_open_array(tparavarsym(p).vardef),sizeof(pint));
  682. if is_open_array(tparavarsym(p).vardef) then
  683. begin
  684. { open arrays do not contain correct element count in their rtti,
  685. the actual count must be passed separately. }
  686. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  687. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  688. if not assigned(hsym) then
  689. internalerror(201003031);
  690. hlcg.g_array_rtti_helper(list,eldef,href,hsym.initialloc,'fpc_addref_array');
  691. end
  692. else
  693. hlcg.g_incrrefcount(list,tparavarsym(p).vardef,href);
  694. end;
  695. end;
  696. vs_out :
  697. begin
  698. { we have no idea about the alignment at the callee side,
  699. and the user also cannot specify "unaligned" here, so
  700. assume worst case }
  701. hlcg.location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  702. if is_open_array(tparavarsym(p).vardef) then
  703. begin
  704. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  705. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  706. if not assigned(hsym) then
  707. internalerror(201103033);
  708. hlcg.g_array_rtti_helper(list,eldef,href,hsym.initialloc,'fpc_initialize_array');
  709. end
  710. else
  711. hlcg.g_initialize(list,tparavarsym(p).vardef,href);
  712. end;
  713. end;
  714. end;
  715. end;
  716. procedure gen_alloc_regloc(list:TAsmList;var loc: tlocation);
  717. begin
  718. case loc.loc of
  719. LOC_CREGISTER:
  720. begin
  721. {$ifdef cpu64bitalu}
  722. if loc.size in [OS_128,OS_S128] then
  723. begin
  724. loc.register128.reglo:=cg.getintregister(list,OS_64);
  725. loc.register128.reghi:=cg.getintregister(list,OS_64);
  726. end
  727. else
  728. {$else cpu64bitalu}
  729. if loc.size in [OS_64,OS_S64] then
  730. begin
  731. loc.register64.reglo:=cg.getintregister(list,OS_32);
  732. loc.register64.reghi:=cg.getintregister(list,OS_32);
  733. end
  734. else
  735. {$endif cpu64bitalu}
  736. loc.register:=cg.getintregister(list,loc.size);
  737. end;
  738. LOC_CFPUREGISTER:
  739. begin
  740. loc.register:=cg.getfpuregister(list,loc.size);
  741. end;
  742. LOC_CMMREGISTER:
  743. begin
  744. loc.register:=cg.getmmregister(list,loc.size);
  745. end;
  746. end;
  747. end;
  748. procedure gen_alloc_regvar(list:TAsmList;sym: tabstractnormalvarsym; allocreg: boolean);
  749. begin
  750. if allocreg then
  751. gen_alloc_regloc(list,sym.initialloc);
  752. if (pi_has_label in current_procinfo.flags) then
  753. begin
  754. { Allocate register already, to prevent first allocation to be
  755. inside a loop }
  756. {$ifdef cpu64bitalu}
  757. if sym.initialloc.size in [OS_128,OS_S128] then
  758. begin
  759. cg.a_reg_sync(list,sym.initialloc.register128.reglo);
  760. cg.a_reg_sync(list,sym.initialloc.register128.reghi);
  761. end
  762. else
  763. {$else cpu64bitalu}
  764. if sym.initialloc.size in [OS_64,OS_S64] then
  765. begin
  766. cg.a_reg_sync(list,sym.initialloc.register64.reglo);
  767. cg.a_reg_sync(list,sym.initialloc.register64.reghi);
  768. end
  769. else
  770. {$endif cpu64bitalu}
  771. cg.a_reg_sync(list,sym.initialloc.register);
  772. end;
  773. sym.localloc:=sym.initialloc;
  774. end;
  775. procedure gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);
  776. procedure unget_para(const paraloc:TCGParaLocation);
  777. begin
  778. case paraloc.loc of
  779. LOC_REGISTER :
  780. begin
  781. if getsupreg(paraloc.register)<first_int_imreg then
  782. cg.ungetcpuregister(list,paraloc.register);
  783. end;
  784. LOC_MMREGISTER :
  785. begin
  786. if getsupreg(paraloc.register)<first_mm_imreg then
  787. cg.ungetcpuregister(list,paraloc.register);
  788. end;
  789. LOC_FPUREGISTER :
  790. begin
  791. if getsupreg(paraloc.register)<first_fpu_imreg then
  792. cg.ungetcpuregister(list,paraloc.register);
  793. end;
  794. end;
  795. end;
  796. var
  797. paraloc : pcgparalocation;
  798. href : treference;
  799. sizeleft : aint;
  800. {$if defined(sparc) or defined(arm) or defined(mips)}
  801. tempref : treference;
  802. {$endif defined(sparc) or defined(arm) or defined(mips)}
  803. {$ifdef mips}
  804. tmpreg : tregister;
  805. {$endif mips}
  806. {$ifndef cpu64bitalu}
  807. tempreg : tregister;
  808. reg64 : tregister64;
  809. {$endif not cpu64bitalu}
  810. begin
  811. paraloc:=para.location;
  812. if not assigned(paraloc) then
  813. internalerror(200408203);
  814. { skip e.g. empty records }
  815. if (paraloc^.loc = LOC_VOID) then
  816. exit;
  817. case destloc.loc of
  818. LOC_REFERENCE :
  819. begin
  820. { If the parameter location is reused we don't need to copy
  821. anything }
  822. if not reusepara then
  823. begin
  824. href:=destloc.reference;
  825. sizeleft:=para.intsize;
  826. while assigned(paraloc) do
  827. begin
  828. if (paraloc^.size=OS_NO) then
  829. begin
  830. { Can only be a reference that contains the rest
  831. of the parameter }
  832. if (paraloc^.loc<>LOC_REFERENCE) or
  833. assigned(paraloc^.next) then
  834. internalerror(2005013010);
  835. cg.a_load_cgparaloc_ref(list,paraloc^,href,sizeleft,destloc.reference.alignment);
  836. inc(href.offset,sizeleft);
  837. sizeleft:=0;
  838. end
  839. else
  840. begin
  841. cg.a_load_cgparaloc_ref(list,paraloc^,href,tcgsize2size[paraloc^.size],destloc.reference.alignment);
  842. inc(href.offset,TCGSize2Size[paraloc^.size]);
  843. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  844. end;
  845. unget_para(paraloc^);
  846. paraloc:=paraloc^.next;
  847. end;
  848. end;
  849. end;
  850. LOC_REGISTER,
  851. LOC_CREGISTER :
  852. begin
  853. {$ifdef cpu64bitalu}
  854. if (para.size in [OS_128,OS_S128,OS_F128]) and
  855. ({ in case of fpu emulation, or abi's that pass fpu values
  856. via integer registers }
  857. (vardef.typ=floatdef) or
  858. is_methodpointer(vardef)) then
  859. begin
  860. case paraloc^.loc of
  861. LOC_REGISTER:
  862. begin
  863. if not assigned(paraloc^.next) then
  864. internalerror(200410104);
  865. if (target_info.endian=ENDIAN_BIG) then
  866. begin
  867. { paraloc^ -> high
  868. paraloc^.next -> low }
  869. unget_para(paraloc^);
  870. gen_alloc_regloc(list,destloc);
  871. { reg->reg, alignment is irrelevant }
  872. cg.a_load_cgparaloc_anyreg(list,OS_64,paraloc^,destloc.register128.reghi,8);
  873. unget_para(paraloc^.next^);
  874. cg.a_load_cgparaloc_anyreg(list,OS_64,paraloc^.next^,destloc.register128.reglo,8);
  875. end
  876. else
  877. begin
  878. { paraloc^ -> low
  879. paraloc^.next -> high }
  880. unget_para(paraloc^);
  881. gen_alloc_regloc(list,destloc);
  882. cg.a_load_cgparaloc_anyreg(list,OS_64,paraloc^,destloc.register128.reglo,8);
  883. unget_para(paraloc^.next^);
  884. cg.a_load_cgparaloc_anyreg(list,OS_64,paraloc^.next^,destloc.register128.reghi,8);
  885. end;
  886. end;
  887. LOC_REFERENCE:
  888. begin
  889. gen_alloc_regloc(list,destloc);
  890. reference_reset_base(href,paraloc^.reference.index,paraloc^.reference.offset,para.alignment);
  891. cg128.a_load128_ref_reg(list,href,destloc.register128);
  892. unget_para(paraloc^);
  893. end;
  894. else
  895. internalerror(2012090607);
  896. end
  897. end
  898. else
  899. {$else cpu64bitalu}
  900. if (para.size in [OS_64,OS_S64,OS_F64]) and
  901. (is_64bit(vardef) or
  902. { in case of fpu emulation, or abi's that pass fpu values
  903. via integer registers }
  904. (vardef.typ=floatdef) or
  905. is_methodpointer(vardef)) then
  906. begin
  907. case paraloc^.loc of
  908. LOC_REGISTER:
  909. begin
  910. if not assigned(paraloc^.next) then
  911. internalerror(200410104);
  912. if (target_info.endian=ENDIAN_BIG) then
  913. begin
  914. { paraloc^ -> high
  915. paraloc^.next -> low }
  916. unget_para(paraloc^);
  917. gen_alloc_regloc(list,destloc);
  918. { reg->reg, alignment is irrelevant }
  919. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^,destloc.register64.reghi,4);
  920. unget_para(paraloc^.next^);
  921. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^.next^,destloc.register64.reglo,4);
  922. end
  923. else
  924. begin
  925. { paraloc^ -> low
  926. paraloc^.next -> high }
  927. unget_para(paraloc^);
  928. gen_alloc_regloc(list,destloc);
  929. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^,destloc.register64.reglo,4);
  930. unget_para(paraloc^.next^);
  931. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^.next^,destloc.register64.reghi,4);
  932. end;
  933. end;
  934. LOC_REFERENCE:
  935. begin
  936. gen_alloc_regloc(list,destloc);
  937. reference_reset_base(href,paraloc^.reference.index,paraloc^.reference.offset,para.alignment);
  938. cg64.a_load64_ref_reg(list,href,destloc.register64);
  939. unget_para(paraloc^);
  940. end;
  941. else
  942. internalerror(2005101501);
  943. end
  944. end
  945. else
  946. {$endif cpu64bitalu}
  947. begin
  948. if assigned(paraloc^.next) then
  949. internalerror(200410105);
  950. unget_para(paraloc^);
  951. gen_alloc_regloc(list,destloc);
  952. cg.a_load_cgparaloc_anyreg(list,destloc.size,paraloc^,destloc.register,sizeof(aint));
  953. end;
  954. end;
  955. LOC_FPUREGISTER,
  956. LOC_CFPUREGISTER :
  957. begin
  958. {$ifdef mips}
  959. if (destloc.size = paraloc^.Size) and
  960. (paraloc^.Loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  961. begin
  962. gen_alloc_regloc(list,destloc);
  963. cg.a_loadfpu_reg_reg(list,paraloc^.Size, destloc.size, paraloc^.register, destloc.register);
  964. end
  965. else if (destloc.size = OS_F32) and
  966. (paraloc^.Loc in [LOC_REGISTER,LOC_CREGISTER]) then
  967. begin
  968. gen_alloc_regloc(list,destloc);
  969. list.Concat(taicpu.op_reg_reg(A_MTC1,paraloc^.register,destloc.register));
  970. end
  971. else if (destloc.size = OS_F64) and
  972. (paraloc^.Loc in [LOC_REGISTER,LOC_CREGISTER]) and
  973. (paraloc^.next^.Loc in [LOC_REGISTER,LOC_CREGISTER]) then
  974. begin
  975. gen_alloc_regloc(list,destloc);
  976. tmpreg:=destloc.register;
  977. list.Concat(taicpu.op_reg_reg(A_MTC1,paraloc^.register,tmpreg));
  978. setsupreg(tmpreg,getsupreg(tmpreg)+1);
  979. list.Concat(taicpu.op_reg_reg(A_MTC1,paraloc^.Next^.register,tmpreg));
  980. end
  981. else
  982. begin
  983. sizeleft := TCGSize2Size[destloc.size];
  984. tg.GetTemp(list,sizeleft,sizeleft,tt_normal,tempref);
  985. href:=tempref;
  986. while assigned(paraloc) do
  987. begin
  988. unget_para(paraloc^);
  989. cg.a_load_cgparaloc_ref(list,paraloc^,href,sizeleft,destloc.reference.alignment);
  990. inc(href.offset,TCGSize2Size[paraloc^.size]);
  991. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  992. paraloc:=paraloc^.next;
  993. end;
  994. gen_alloc_regloc(list,destloc);
  995. cg.a_loadfpu_ref_reg(list,destloc.size,destloc.size,tempref,destloc.register);
  996. tg.UnGetTemp(list,tempref);
  997. end;
  998. {$else mips}
  999. {$if defined(sparc) or defined(arm)}
  1000. { Arm and Sparc passes floats in int registers, when loading to fpu register
  1001. we need a temp }
  1002. sizeleft := TCGSize2Size[destloc.size];
  1003. tg.GetTemp(list,sizeleft,sizeleft,tt_normal,tempref);
  1004. href:=tempref;
  1005. while assigned(paraloc) do
  1006. begin
  1007. unget_para(paraloc^);
  1008. cg.a_load_cgparaloc_ref(list,paraloc^,href,sizeleft,destloc.reference.alignment);
  1009. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1010. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1011. paraloc:=paraloc^.next;
  1012. end;
  1013. gen_alloc_regloc(list,destloc);
  1014. cg.a_loadfpu_ref_reg(list,destloc.size,destloc.size,tempref,destloc.register);
  1015. tg.UnGetTemp(list,tempref);
  1016. {$else defined(sparc) or defined(arm)}
  1017. unget_para(paraloc^);
  1018. gen_alloc_regloc(list,destloc);
  1019. { from register to register -> alignment is irrelevant }
  1020. cg.a_load_cgparaloc_anyreg(list,destloc.size,paraloc^,destloc.register,0);
  1021. if assigned(paraloc^.next) then
  1022. internalerror(200410109);
  1023. {$endif defined(sparc) or defined(arm)}
  1024. {$endif mips}
  1025. end;
  1026. LOC_MMREGISTER,
  1027. LOC_CMMREGISTER :
  1028. begin
  1029. {$ifndef cpu64bitalu}
  1030. { ARM vfp floats are passed in integer registers }
  1031. if (para.size=OS_F64) and
  1032. (paraloc^.size in [OS_32,OS_S32]) and
  1033. use_vectorfpu(vardef) then
  1034. begin
  1035. { we need 2x32bit reg }
  1036. if not assigned(paraloc^.next) or
  1037. assigned(paraloc^.next^.next) then
  1038. internalerror(2009112421);
  1039. unget_para(paraloc^.next^);
  1040. case paraloc^.next^.loc of
  1041. LOC_REGISTER:
  1042. tempreg:=paraloc^.next^.register;
  1043. LOC_REFERENCE:
  1044. begin
  1045. tempreg:=cg.getintregister(list,OS_32);
  1046. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^.next^,tempreg,4);
  1047. end;
  1048. else
  1049. internalerror(2012051301);
  1050. end;
  1051. { don't free before the above, because then the getintregister
  1052. could reallocate this register and overwrite it }
  1053. unget_para(paraloc^);
  1054. gen_alloc_regloc(list,destloc);
  1055. if (target_info.endian=endian_big) then
  1056. { paraloc^ -> high
  1057. paraloc^.next -> low }
  1058. reg64:=joinreg64(tempreg,paraloc^.register)
  1059. else
  1060. reg64:=joinreg64(paraloc^.register,tempreg);
  1061. cg64.a_loadmm_intreg64_reg(list,OS_F64,reg64,destloc.register);
  1062. end
  1063. else
  1064. {$endif not cpu64bitalu}
  1065. begin
  1066. unget_para(paraloc^);
  1067. gen_alloc_regloc(list,destloc);
  1068. { from register to register -> alignment is irrelevant }
  1069. cg.a_load_cgparaloc_anyreg(list,destloc.size,paraloc^,destloc.register,0);
  1070. { data could come in two memory locations, for now
  1071. we simply ignore the sanity check (FK)
  1072. if assigned(paraloc^.next) then
  1073. internalerror(200410108);
  1074. }
  1075. end;
  1076. end;
  1077. else
  1078. internalerror(2010052903);
  1079. end;
  1080. end;
  1081. procedure gen_load_para_value(list:TAsmList);
  1082. procedure get_para(const paraloc:TCGParaLocation);
  1083. begin
  1084. case paraloc.loc of
  1085. LOC_REGISTER :
  1086. begin
  1087. if getsupreg(paraloc.register)<first_int_imreg then
  1088. cg.getcpuregister(list,paraloc.register);
  1089. end;
  1090. LOC_MMREGISTER :
  1091. begin
  1092. if getsupreg(paraloc.register)<first_mm_imreg then
  1093. cg.getcpuregister(list,paraloc.register);
  1094. end;
  1095. LOC_FPUREGISTER :
  1096. begin
  1097. if getsupreg(paraloc.register)<first_fpu_imreg then
  1098. cg.getcpuregister(list,paraloc.register);
  1099. end;
  1100. end;
  1101. end;
  1102. var
  1103. i : longint;
  1104. currpara : tparavarsym;
  1105. paraloc : pcgparalocation;
  1106. begin
  1107. if (po_assembler in current_procinfo.procdef.procoptions) or
  1108. { exceptfilters have a single hidden 'parentfp' parameter, which
  1109. is handled by tcg.g_proc_entry. }
  1110. (current_procinfo.procdef.proctypeoption=potype_exceptfilter) then
  1111. exit;
  1112. { Allocate registers used by parameters }
  1113. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1114. begin
  1115. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1116. paraloc:=currpara.paraloc[calleeside].location;
  1117. while assigned(paraloc) do
  1118. begin
  1119. if paraloc^.loc in [LOC_REGISTER,LOC_FPUREGISTER,LOC_MMREGISTER] then
  1120. get_para(paraloc^);
  1121. paraloc:=paraloc^.next;
  1122. end;
  1123. end;
  1124. { Copy parameters to local references/registers }
  1125. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1126. begin
  1127. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1128. gen_load_cgpara_loc(list,currpara.vardef,currpara.paraloc[calleeside],currpara.initialloc,paramanager.param_use_paraloc(currpara.paraloc[calleeside]));
  1129. { gen_load_cgpara_loc() already allocated the initialloc
  1130. -> don't allocate again }
  1131. if currpara.initialloc.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMREGISTER] then
  1132. gen_alloc_regvar(list,currpara,false);
  1133. end;
  1134. { generate copies of call by value parameters, must be done before
  1135. the initialization and body is parsed because the refcounts are
  1136. incremented using the local copies }
  1137. current_procinfo.procdef.parast.SymList.ForEachCall(@copyvalueparas,list);
  1138. {$ifdef powerpc}
  1139. { unget the register that contains the stack pointer before the procedure entry, }
  1140. { which is used to access the parameters in their original callee-side location }
  1141. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1142. cg.a_reg_dealloc(list,NR_R12);
  1143. {$endif powerpc}
  1144. {$ifdef powerpc64}
  1145. { unget the register that contains the stack pointer before the procedure entry, }
  1146. { which is used to access the parameters in their original callee-side location }
  1147. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1148. cg.a_reg_dealloc(list, NR_OLD_STACK_POINTER_REG);
  1149. {$endif powerpc64}
  1150. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1151. begin
  1152. { initialize refcounted paras, and trash others. Needed here
  1153. instead of in gen_initialize_code, because when a reference is
  1154. intialised or trashed while the pointer to that reference is kept
  1155. in a regvar, we add a register move and that one again has to
  1156. come after the parameter loading code as far as the register
  1157. allocator is concerned }
  1158. current_procinfo.procdef.parast.SymList.ForEachCall(@init_paras,list);
  1159. end;
  1160. end;
  1161. {****************************************************************************
  1162. Entry/Exit
  1163. ****************************************************************************}
  1164. function has_alias_name(pd:tprocdef;const s:string):boolean;
  1165. var
  1166. item : TCmdStrListItem;
  1167. begin
  1168. result:=true;
  1169. if pd.mangledname=s then
  1170. exit;
  1171. item := TCmdStrListItem(pd.aliasnames.first);
  1172. while assigned(item) do
  1173. begin
  1174. if item.str=s then
  1175. exit;
  1176. item := TCmdStrListItem(item.next);
  1177. end;
  1178. result:=false;
  1179. end;
  1180. procedure alloc_proc_symbol(pd: tprocdef);
  1181. var
  1182. item : TCmdStrListItem;
  1183. begin
  1184. item := TCmdStrListItem(pd.aliasnames.first);
  1185. while assigned(item) do
  1186. begin
  1187. current_asmdata.DefineAsmSymbol(item.str,AB_GLOBAL,AT_FUNCTION);
  1188. item := TCmdStrListItem(item.next);
  1189. end;
  1190. end;
  1191. procedure gen_proc_symbol(list:TAsmList);
  1192. var
  1193. item,
  1194. previtem : TCmdStrListItem;
  1195. begin
  1196. previtem:=nil;
  1197. item := TCmdStrListItem(current_procinfo.procdef.aliasnames.first);
  1198. while assigned(item) do
  1199. begin
  1200. {$ifdef arm}
  1201. if current_settings.cputype in cpu_thumb2 then
  1202. list.concat(tai_thumb_func.create);
  1203. {$endif arm}
  1204. { "double link" all procedure entry symbols via .reference }
  1205. { directives on darwin, because otherwise the linker }
  1206. { sometimes strips the procedure if only on of the symbols }
  1207. { is referenced }
  1208. if assigned(previtem) and
  1209. (target_info.system in systems_darwin) then
  1210. list.concat(tai_directive.create(asd_reference,item.str));
  1211. if (cs_profile in current_settings.moduleswitches) or
  1212. (po_global in current_procinfo.procdef.procoptions) then
  1213. list.concat(Tai_symbol.createname_global(item.str,AT_FUNCTION,0))
  1214. else
  1215. list.concat(Tai_symbol.createname(item.str,AT_FUNCTION,0));
  1216. if assigned(previtem) and
  1217. (target_info.system in systems_darwin) then
  1218. list.concat(tai_directive.create(asd_reference,previtem.str));
  1219. if not(af_stabs_use_function_absolute_addresses in target_asm.flags) then
  1220. list.concat(Tai_function_name.create(item.str));
  1221. previtem:=item;
  1222. item := TCmdStrListItem(item.next);
  1223. end;
  1224. current_procinfo.procdef.procstarttai:=tai(list.last);
  1225. end;
  1226. procedure gen_proc_entry_code(list:TAsmList);
  1227. var
  1228. hitemp,
  1229. lotemp, stack_frame_size : longint;
  1230. begin
  1231. { generate call frame marker for dwarf call frame info }
  1232. current_asmdata.asmcfi.start_frame(list);
  1233. { All temps are know, write offsets used for information }
  1234. if (cs_asm_source in current_settings.globalswitches) then
  1235. begin
  1236. if tg.direction>0 then
  1237. begin
  1238. lotemp:=current_procinfo.tempstart;
  1239. hitemp:=tg.lasttemp;
  1240. end
  1241. else
  1242. begin
  1243. lotemp:=tg.lasttemp;
  1244. hitemp:=current_procinfo.tempstart;
  1245. end;
  1246. list.concat(Tai_comment.Create(strpnew('Temps allocated between '+std_regname(current_procinfo.framepointer)+
  1247. tostr_with_plus(lotemp)+' and '+std_regname(current_procinfo.framepointer)+tostr_with_plus(hitemp))));
  1248. end;
  1249. { generate target specific proc entry code }
  1250. stack_frame_size := current_procinfo.calc_stackframe_size;
  1251. if (stack_frame_size <> 0) and
  1252. (po_nostackframe in current_procinfo.procdef.procoptions) then
  1253. message1(parser_e_nostackframe_with_locals,tostr(stack_frame_size));
  1254. hlcg.g_proc_entry(list,stack_frame_size,(po_nostackframe in current_procinfo.procdef.procoptions));
  1255. end;
  1256. procedure gen_proc_exit_code(list:TAsmList);
  1257. var
  1258. parasize : longint;
  1259. begin
  1260. { c style clearstack does not need to remove parameters from the stack, only the
  1261. return value when it was pushed by arguments }
  1262. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1263. begin
  1264. parasize:=0;
  1265. if paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef.proccalloption) then
  1266. inc(parasize,sizeof(pint));
  1267. end
  1268. else
  1269. begin
  1270. parasize:=current_procinfo.para_stack_size;
  1271. { the parent frame pointer para has to be removed by the caller in
  1272. case of Delphi-style parent frame pointer passing }
  1273. if not paramanager.use_fixed_stack and
  1274. (po_delphi_nested_cc in current_procinfo.procdef.procoptions) then
  1275. dec(parasize,sizeof(pint));
  1276. end;
  1277. { generate target specific proc exit code }
  1278. hlcg.g_proc_exit(list,parasize,(po_nostackframe in current_procinfo.procdef.procoptions));
  1279. { release return registers, needed for optimizer }
  1280. if not is_void(current_procinfo.procdef.returndef) then
  1281. paramanager.freecgpara(list,current_procinfo.procdef.funcretloc[calleeside]);
  1282. { end of frame marker for call frame info }
  1283. current_asmdata.asmcfi.end_frame(list);
  1284. end;
  1285. procedure gen_stack_check_size_para(list:TAsmList);
  1286. var
  1287. paraloc1 : tcgpara;
  1288. begin
  1289. paraloc1.init;
  1290. paramanager.getintparaloc(pocall_default,1,ptruinttype,paraloc1);
  1291. cg.a_load_const_cgpara(list,OS_INT,current_procinfo.calc_stackframe_size,paraloc1);
  1292. paramanager.freecgpara(list,paraloc1);
  1293. paraloc1.done;
  1294. end;
  1295. procedure gen_stack_check_call(list:TAsmList);
  1296. var
  1297. paraloc1 : tcgpara;
  1298. begin
  1299. paraloc1.init;
  1300. { Also alloc the register needed for the parameter }
  1301. paramanager.getintparaloc(pocall_default,1,ptruinttype,paraloc1);
  1302. paramanager.freecgpara(list,paraloc1);
  1303. { Call the helper }
  1304. cg.allocallcpuregisters(list);
  1305. cg.a_call_name(list,'FPC_STACKCHECK',false);
  1306. cg.deallocallcpuregisters(list);
  1307. paraloc1.done;
  1308. end;
  1309. procedure gen_save_used_regs(list:TAsmList);
  1310. begin
  1311. { Pure assembler routines need to save the registers themselves }
  1312. if (po_assembler in current_procinfo.procdef.procoptions) then
  1313. exit;
  1314. { oldfpccall expects all registers to be destroyed }
  1315. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1316. cg.g_save_registers(list);
  1317. end;
  1318. procedure gen_restore_used_regs(list:TAsmList);
  1319. begin
  1320. { Pure assembler routines need to save the registers themselves }
  1321. if (po_assembler in current_procinfo.procdef.procoptions) then
  1322. exit;
  1323. { oldfpccall expects all registers to be destroyed }
  1324. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1325. cg.g_restore_registers(list);
  1326. end;
  1327. {****************************************************************************
  1328. External handling
  1329. ****************************************************************************}
  1330. procedure gen_external_stub(list:TAsmList;pd:tprocdef;const externalname:string);
  1331. begin
  1332. create_hlcodegen;
  1333. { add the procedure to the al_procedures }
  1334. maybe_new_object_file(list);
  1335. new_section(list,sec_code,lower(pd.mangledname),current_settings.alignment.procalign);
  1336. list.concat(Tai_align.create(current_settings.alignment.procalign));
  1337. if (po_global in pd.procoptions) then
  1338. list.concat(Tai_symbol.createname_global(pd.mangledname,AT_FUNCTION,0))
  1339. else
  1340. list.concat(Tai_symbol.createname(pd.mangledname,AT_FUNCTION,0));
  1341. cg.g_external_wrapper(list,pd,externalname);
  1342. destroy_hlcodegen;
  1343. end;
  1344. {****************************************************************************
  1345. Const Data
  1346. ****************************************************************************}
  1347. procedure gen_alloc_symtable(list:TAsmList;pd:tprocdef;st:TSymtable);
  1348. procedure setlocalloc(vs:tabstractnormalvarsym);
  1349. begin
  1350. if cs_asm_source in current_settings.globalswitches then
  1351. begin
  1352. case vs.initialloc.loc of
  1353. LOC_REFERENCE :
  1354. begin
  1355. if not assigned(vs.initialloc.reference.symbol) then
  1356. list.concat(Tai_comment.Create(strpnew('Var '+vs.realname+' located at '+
  1357. std_regname(vs.initialloc.reference.base)+tostr_with_plus(vs.initialloc.reference.offset))));
  1358. end;
  1359. end;
  1360. end;
  1361. vs.localloc:=vs.initialloc;
  1362. end;
  1363. var
  1364. i : longint;
  1365. sym : tsym;
  1366. vs : tabstractnormalvarsym;
  1367. isaddr : boolean;
  1368. begin
  1369. for i:=0 to st.SymList.Count-1 do
  1370. begin
  1371. sym:=tsym(st.SymList[i]);
  1372. case sym.typ of
  1373. staticvarsym :
  1374. begin
  1375. vs:=tabstractnormalvarsym(sym);
  1376. { The code in loadnode.pass_generatecode will create the
  1377. LOC_REFERENCE instead for all none register variables. This is
  1378. required because we can't store an asmsymbol in the localloc because
  1379. the asmsymbol is invalid after an unit is compiled. This gives
  1380. problems when this procedure is inlined in another unit (PFV) }
  1381. if vs.is_regvar(false) then
  1382. begin
  1383. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable];
  1384. vs.initialloc.size:=def_cgsize(vs.vardef);
  1385. gen_alloc_regvar(list,vs,true);
  1386. setlocalloc(vs);
  1387. end;
  1388. end;
  1389. paravarsym :
  1390. begin
  1391. vs:=tabstractnormalvarsym(sym);
  1392. { Parameters passed to assembler procedures need to be kept
  1393. in the original location }
  1394. if (po_assembler in current_procinfo.procdef.procoptions) then
  1395. tparavarsym(vs).paraloc[calleeside].get_location(vs.initialloc)
  1396. { exception filters receive their frame pointer as a parameter }
  1397. else if (current_procinfo.procdef.proctypeoption=potype_exceptfilter) and
  1398. (vo_is_parentfp in vs.varoptions) then
  1399. begin
  1400. location_reset(vs.initialloc,LOC_REGISTER,OS_ADDR);
  1401. vs.initialloc.register:=NR_FRAME_POINTER_REG;
  1402. end
  1403. else
  1404. begin
  1405. isaddr:=paramanager.push_addr_param(vs.varspez,vs.vardef,current_procinfo.procdef.proccalloption);
  1406. if isaddr then
  1407. vs.initialloc.size:=OS_ADDR
  1408. else
  1409. vs.initialloc.size:=def_cgsize(vs.vardef);
  1410. if vs.is_regvar(isaddr) then
  1411. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable]
  1412. else
  1413. begin
  1414. vs.initialloc.loc:=LOC_REFERENCE;
  1415. { Reuse the parameter location for values to are at a single location on the stack }
  1416. if paramanager.param_use_paraloc(tparavarsym(sym).paraloc[calleeside]) then
  1417. begin
  1418. reference_reset_base(vs.initialloc.reference,tparavarsym(sym).paraloc[calleeside].location^.reference.index,
  1419. tparavarsym(sym).paraloc[calleeside].location^.reference.offset,tparavarsym(sym).paraloc[calleeside].alignment);
  1420. end
  1421. else
  1422. begin
  1423. if isaddr then
  1424. tg.GetLocal(list,sizeof(pint),voidpointertype,vs.initialloc.reference)
  1425. else
  1426. tg.GetLocal(list,vs.getsize,tparavarsym(sym).paraloc[calleeside].alignment,vs.vardef,vs.initialloc.reference);
  1427. end;
  1428. end;
  1429. end;
  1430. setlocalloc(vs);
  1431. end;
  1432. localvarsym :
  1433. begin
  1434. vs:=tabstractnormalvarsym(sym);
  1435. vs.initialloc.size:=def_cgsize(vs.vardef);
  1436. if ([po_assembler,po_nostackframe] * current_procinfo.procdef.procoptions = [po_assembler,po_nostackframe]) and
  1437. (vo_is_funcret in vs.varoptions) then
  1438. begin
  1439. paramanager.create_funcretloc_info(pd,calleeside);
  1440. if assigned(pd.funcretloc[calleeside].location^.next) then
  1441. begin
  1442. { can't replace references to "result" with a complex
  1443. location expression inside assembler code }
  1444. location_reset(vs.initialloc,LOC_INVALID,OS_NO);
  1445. end
  1446. else
  1447. pd.funcretloc[calleeside].get_location(vs.initialloc);
  1448. end
  1449. else if (m_delphi in current_settings.modeswitches) and
  1450. (po_assembler in current_procinfo.procdef.procoptions) and
  1451. (vo_is_funcret in vs.varoptions) and
  1452. (vs.refs=0) then
  1453. begin
  1454. { not referenced, so don't allocate. Use dummy to }
  1455. { avoid ie's later on because of LOC_INVALID }
  1456. vs.initialloc.loc:=LOC_REGISTER;
  1457. vs.initialloc.size:=OS_INT;
  1458. vs.initialloc.register:=NR_FUNCTION_RESULT_REG;
  1459. end
  1460. else if vs.is_regvar(false) then
  1461. begin
  1462. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable];
  1463. gen_alloc_regvar(list,vs,true);
  1464. end
  1465. else
  1466. begin
  1467. vs.initialloc.loc:=LOC_REFERENCE;
  1468. tg.GetLocal(list,vs.getsize,vs.vardef,vs.initialloc.reference);
  1469. end;
  1470. setlocalloc(vs);
  1471. end;
  1472. end;
  1473. end;
  1474. end;
  1475. procedure add_regvars(var rv: tusedregvars; const location: tlocation);
  1476. begin
  1477. case location.loc of
  1478. LOC_CREGISTER:
  1479. {$ifdef cpu64bitalu}
  1480. if location.size in [OS_128,OS_S128] then
  1481. begin
  1482. rv.intregvars.addnodup(getsupreg(location.register128.reglo));
  1483. rv.intregvars.addnodup(getsupreg(location.register128.reghi));
  1484. end
  1485. else
  1486. {$else cpu64bitalu}
  1487. if location.size in [OS_64,OS_S64] then
  1488. begin
  1489. rv.intregvars.addnodup(getsupreg(location.register64.reglo));
  1490. rv.intregvars.addnodup(getsupreg(location.register64.reghi));
  1491. end
  1492. else
  1493. {$endif cpu64bitalu}
  1494. rv.intregvars.addnodup(getsupreg(location.register));
  1495. LOC_CFPUREGISTER:
  1496. rv.fpuregvars.addnodup(getsupreg(location.register));
  1497. LOC_CMMREGISTER:
  1498. rv.mmregvars.addnodup(getsupreg(location.register));
  1499. end;
  1500. end;
  1501. function do_get_used_regvars(var n: tnode; arg: pointer): foreachnoderesult;
  1502. var
  1503. rv: pusedregvars absolute arg;
  1504. begin
  1505. case (n.nodetype) of
  1506. temprefn:
  1507. { We only have to synchronise a tempnode before a loop if it is }
  1508. { not created inside the loop, and only synchronise after the }
  1509. { loop if it's not destroyed inside the loop. If it's created }
  1510. { before the loop and not yet destroyed, then before the loop }
  1511. { is secondpassed tempinfo^.valid will be true, and we get the }
  1512. { correct registers. If it's not destroyed inside the loop, }
  1513. { then after the loop has been secondpassed tempinfo^.valid }
  1514. { be true and we also get the right registers. In other cases, }
  1515. { tempinfo^.valid will be false and so we do not add }
  1516. { unnecessary registers. This way, we don't have to look at }
  1517. { tempcreate and tempdestroy nodes to get this info (JM) }
  1518. if (ti_valid in ttemprefnode(n).tempinfo^.flags) then
  1519. add_regvars(rv^,ttemprefnode(n).tempinfo^.location);
  1520. loadn:
  1521. if (tloadnode(n).symtableentry.typ in [staticvarsym,localvarsym,paravarsym]) then
  1522. add_regvars(rv^,tabstractnormalvarsym(tloadnode(n).symtableentry).localloc);
  1523. vecn:
  1524. { range checks sometimes need the high parameter }
  1525. if (cs_check_range in current_settings.localswitches) and
  1526. (is_open_array(tvecnode(n).left.resultdef) or
  1527. is_array_of_const(tvecnode(n).left.resultdef)) and
  1528. not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  1529. add_regvars(rv^,tabstractnormalvarsym(get_high_value_sym(tparavarsym(tloadnode(tvecnode(n).left).symtableentry))).localloc)
  1530. end;
  1531. result := fen_true;
  1532. end;
  1533. procedure get_used_regvars(n: tnode; var rv: tusedregvars);
  1534. begin
  1535. foreachnodestatic(n,@do_get_used_regvars,@rv);
  1536. end;
  1537. (*
  1538. See comments at declaration of pusedregvarscommon
  1539. function do_get_used_regvars_common(var n: tnode; arg: pointer): foreachnoderesult;
  1540. var
  1541. rv: pusedregvarscommon absolute arg;
  1542. begin
  1543. if (n.nodetype = loadn) and
  1544. (tloadnode(n).symtableentry.typ in [staticvarsym,localvarsym,paravarsym]) then
  1545. with tabstractnormalvarsym(tloadnode(n).symtableentry).localloc do
  1546. case loc of
  1547. LOC_CREGISTER:
  1548. { if not yet encountered in this node tree }
  1549. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  1550. { but nevertheless already encountered somewhere }
  1551. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  1552. { then it's a regvar used in two or more node trees }
  1553. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  1554. LOC_CFPUREGISTER:
  1555. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  1556. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  1557. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  1558. LOC_CMMREGISTER:
  1559. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  1560. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  1561. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  1562. end;
  1563. result := fen_true;
  1564. end;
  1565. procedure get_used_regvars_common(n: tnode; var rv: tusedregvarscommon);
  1566. begin
  1567. rv.myregvars.intregvars.clear;
  1568. rv.myregvars.fpuregvars.clear;
  1569. rv.myregvars.mmregvars.clear;
  1570. foreachnodestatic(n,@do_get_used_regvars_common,@rv);
  1571. end;
  1572. *)
  1573. procedure gen_sync_regvars(list:TAsmList; var rv: tusedregvars);
  1574. var
  1575. count: longint;
  1576. begin
  1577. for count := 1 to rv.intregvars.length do
  1578. cg.a_reg_sync(list,newreg(R_INTREGISTER,rv.intregvars.readidx(count-1),R_SUBWHOLE));
  1579. for count := 1 to rv.fpuregvars.length do
  1580. cg.a_reg_sync(list,newreg(R_FPUREGISTER,rv.fpuregvars.readidx(count-1),R_SUBWHOLE));
  1581. for count := 1 to rv.mmregvars.length do
  1582. cg.a_reg_sync(list,newreg(R_MMREGISTER,rv.mmregvars.readidx(count-1),R_SUBWHOLE));
  1583. end;
  1584. {*****************************************************************************
  1585. SSA support
  1586. *****************************************************************************}
  1587. type
  1588. preplaceregrec = ^treplaceregrec;
  1589. treplaceregrec = record
  1590. old, new: tregister;
  1591. oldhi, newhi: tregister;
  1592. ressym: tsym;
  1593. { moved sym }
  1594. sym : tabstractnormalvarsym;
  1595. end;
  1596. function doreplace(var n: tnode; para: pointer): foreachnoderesult;
  1597. var
  1598. rr: preplaceregrec absolute para;
  1599. begin
  1600. result := fen_false;
  1601. if (nf_is_funcret in n.flags) and (fc_exit in flowcontrol) then
  1602. exit;
  1603. case n.nodetype of
  1604. loadn:
  1605. begin
  1606. if (tloadnode(n).symtableentry.typ in [localvarsym,paravarsym,staticvarsym]) and
  1607. (tabstractvarsym(tloadnode(n).symtableentry).varoptions * [vo_is_dll_var, vo_is_thread_var] = []) and
  1608. not assigned(tloadnode(n).left) and
  1609. ((tloadnode(n).symtableentry <> rr^.ressym) or
  1610. not(fc_exit in flowcontrol)
  1611. ) and
  1612. (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) and
  1613. (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register = rr^.old) then
  1614. begin
  1615. {$ifdef cpu64bitalu}
  1616. { it's possible a 128 bit location was shifted and/xor typecasted }
  1617. { in a 64 bit value, so only 1 register was left in the location }
  1618. if (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.size in [OS_128,OS_S128]) then
  1619. if (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register128.reghi = rr^.oldhi) then
  1620. tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register128.reghi := rr^.newhi
  1621. else
  1622. exit;
  1623. {$else cpu64bitalu}
  1624. { it's possible a 64 bit location was shifted and/xor typecasted }
  1625. { in a 32 bit value, so only 1 register was left in the location }
  1626. if (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.size in [OS_64,OS_S64]) then
  1627. if (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register64.reghi = rr^.oldhi) then
  1628. tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register64.reghi := rr^.newhi
  1629. else
  1630. exit;
  1631. {$endif cpu64bitalu}
  1632. tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register := rr^.new;
  1633. rr^.sym := tabstractnormalvarsym(tloadnode(n).symtableentry);
  1634. result := fen_norecurse_true;
  1635. end;
  1636. end;
  1637. temprefn:
  1638. begin
  1639. if (ti_valid in ttemprefnode(n).tempinfo^.flags) and
  1640. (ttemprefnode(n).tempinfo^.location.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) and
  1641. (ttemprefnode(n).tempinfo^.location.register = rr^.old) then
  1642. begin
  1643. {$ifdef cpu64bitalu}
  1644. { it's possible a 128 bit location was shifted and/xor typecasted }
  1645. { in a 64 bit value, so only 1 register was left in the location }
  1646. if (ttemprefnode(n).tempinfo^.location.size in [OS_128,OS_S128]) then
  1647. if (ttemprefnode(n).tempinfo^.location.register128.reghi = rr^.oldhi) then
  1648. ttemprefnode(n).tempinfo^.location.register128.reghi := rr^.newhi
  1649. else
  1650. exit;
  1651. {$else cpu64bitalu}
  1652. { it's possible a 64 bit location was shifted and/xor typecasted }
  1653. { in a 32 bit value, so only 1 register was left in the location }
  1654. if (ttemprefnode(n).tempinfo^.location.size in [OS_64,OS_S64]) then
  1655. if (ttemprefnode(n).tempinfo^.location.register64.reghi = rr^.oldhi) then
  1656. ttemprefnode(n).tempinfo^.location.register64.reghi := rr^.newhi
  1657. else
  1658. exit;
  1659. {$endif cpu64bitalu}
  1660. ttemprefnode(n).tempinfo^.location.register := rr^.new;
  1661. result := fen_norecurse_true;
  1662. end;
  1663. end;
  1664. { optimize the searching a bit }
  1665. derefn,addrn,
  1666. calln,inlinen,casen,
  1667. addn,subn,muln,
  1668. andn,orn,xorn,
  1669. ltn,lten,gtn,gten,equaln,unequaln,
  1670. slashn,divn,shrn,shln,notn,
  1671. inn,
  1672. asn,isn:
  1673. result := fen_norecurse_false;
  1674. end;
  1675. end;
  1676. procedure maybechangeloadnodereg(list: TAsmList; var n: tnode; reload: boolean);
  1677. var
  1678. rr: treplaceregrec;
  1679. varloc : tai_varloc;
  1680. begin
  1681. {$ifdef jvm}
  1682. exit;
  1683. {$endif}
  1684. if not (n.location.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) or
  1685. ([fc_inflowcontrol,fc_gotolabel,fc_lefthandled] * flowcontrol <> []) then
  1686. exit;
  1687. rr.old := n.location.register;
  1688. rr.ressym := nil;
  1689. rr.sym := nil;
  1690. rr.oldhi := NR_NO;
  1691. case n.location.loc of
  1692. LOC_CREGISTER:
  1693. begin
  1694. {$ifdef cpu64bitalu}
  1695. if (n.location.size in [OS_128,OS_S128]) then
  1696. begin
  1697. rr.oldhi := n.location.register128.reghi;
  1698. rr.new := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  1699. rr.newhi := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  1700. end
  1701. else
  1702. {$else cpu64bitalu}
  1703. if (n.location.size in [OS_64,OS_S64]) then
  1704. begin
  1705. rr.oldhi := n.location.register64.reghi;
  1706. rr.new := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  1707. rr.newhi := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  1708. end
  1709. else
  1710. {$endif cpu64bitalu}
  1711. rr.new := cg.getintregister(current_asmdata.CurrAsmList,n.location.size);
  1712. end;
  1713. LOC_CFPUREGISTER:
  1714. rr.new := cg.getfpuregister(current_asmdata.CurrAsmList,n.location.size);
  1715. {$ifdef SUPPORT_MMX}
  1716. LOC_CMMXREGISTER:
  1717. rr.new := tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  1718. {$endif SUPPORT_MMX}
  1719. LOC_CMMREGISTER:
  1720. rr.new := cg.getmmregister(current_asmdata.CurrAsmList,n.location.size);
  1721. else
  1722. exit;
  1723. end;
  1724. if not is_void(current_procinfo.procdef.returndef) and
  1725. assigned(current_procinfo.procdef.funcretsym) and
  1726. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs <> 0) then
  1727. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  1728. rr.ressym:=tsym(current_procinfo.procdef.parast.Find('self'))
  1729. else
  1730. rr.ressym:=current_procinfo.procdef.funcretsym;
  1731. if not foreachnodestatic(n,@doreplace,@rr) then
  1732. exit;
  1733. if reload then
  1734. case n.location.loc of
  1735. LOC_CREGISTER:
  1736. begin
  1737. {$ifdef cpu64bitalu}
  1738. if (n.location.size in [OS_128,OS_S128]) then
  1739. cg128.a_load128_reg_reg(list,n.location.register128,joinreg128(rr.new,rr.newhi))
  1740. else
  1741. {$else cpu64bitalu}
  1742. if (n.location.size in [OS_64,OS_S64]) then
  1743. cg64.a_load64_reg_reg(list,n.location.register64,joinreg64(rr.new,rr.newhi))
  1744. else
  1745. {$endif cpu64bitalu}
  1746. cg.a_load_reg_reg(list,n.location.size,n.location.size,n.location.register,rr.new);
  1747. end;
  1748. LOC_CFPUREGISTER:
  1749. cg.a_loadfpu_reg_reg(list,n.location.size,n.location.size,n.location.register,rr.new);
  1750. {$ifdef SUPPORT_MMX}
  1751. LOC_CMMXREGISTER:
  1752. cg.a_loadmm_reg_reg(list,OS_M64,OS_M64,n.location.register,rr.new,nil);
  1753. {$endif SUPPORT_MMX}
  1754. LOC_CMMREGISTER:
  1755. cg.a_loadmm_reg_reg(list,n.location.size,n.location.size,n.location.register,rr.new,nil);
  1756. else
  1757. internalerror(2006090920);
  1758. end;
  1759. { now that we've change the loadn/temp, also change the node result location }
  1760. {$ifdef cpu64bitalu}
  1761. if (n.location.size in [OS_128,OS_S128]) then
  1762. begin
  1763. n.location.register128.reglo := rr.new;
  1764. n.location.register128.reghi := rr.newhi;
  1765. if assigned(rr.sym) and
  1766. ((rr.sym.currentregloc.register<>rr.new) or
  1767. (rr.sym.currentregloc.registerhi<>rr.newhi)) then
  1768. begin
  1769. varloc:=tai_varloc.create128(rr.sym,rr.new,rr.newhi);
  1770. varloc.oldlocation:=rr.sym.currentregloc.register;
  1771. varloc.oldlocationhi:=rr.sym.currentregloc.registerhi;
  1772. rr.sym.currentregloc.register:=rr.new;
  1773. rr.sym.currentregloc.registerHI:=rr.newhi;
  1774. list.concat(varloc);
  1775. end;
  1776. end
  1777. else
  1778. {$else cpu64bitalu}
  1779. if (n.location.size in [OS_64,OS_S64]) then
  1780. begin
  1781. n.location.register64.reglo := rr.new;
  1782. n.location.register64.reghi := rr.newhi;
  1783. if assigned(rr.sym) and
  1784. ((rr.sym.currentregloc.register<>rr.new) or
  1785. (rr.sym.currentregloc.registerhi<>rr.newhi)) then
  1786. begin
  1787. varloc:=tai_varloc.create64(rr.sym,rr.new,rr.newhi);
  1788. varloc.oldlocation:=rr.sym.currentregloc.register;
  1789. varloc.oldlocationhi:=rr.sym.currentregloc.registerhi;
  1790. rr.sym.currentregloc.register:=rr.new;
  1791. rr.sym.currentregloc.registerHI:=rr.newhi;
  1792. list.concat(varloc);
  1793. end;
  1794. end
  1795. else
  1796. {$endif cpu64bitalu}
  1797. begin
  1798. n.location.register := rr.new;
  1799. if assigned(rr.sym) and (rr.sym.currentregloc.register<>rr.new) then
  1800. begin
  1801. varloc:=tai_varloc.create(rr.sym,rr.new);
  1802. varloc.oldlocation:=rr.sym.currentregloc.register;
  1803. rr.sym.currentregloc.register:=rr.new;
  1804. list.concat(varloc);
  1805. end;
  1806. end;
  1807. end;
  1808. procedure gen_free_symtable(list:TAsmList;st:TSymtable);
  1809. var
  1810. i : longint;
  1811. sym : tsym;
  1812. begin
  1813. for i:=0 to st.SymList.Count-1 do
  1814. begin
  1815. sym:=tsym(st.SymList[i]);
  1816. if (sym.typ in [staticvarsym,localvarsym,paravarsym]) then
  1817. begin
  1818. with tabstractnormalvarsym(sym) do
  1819. begin
  1820. { Note: We need to keep the data available in memory
  1821. for the sub procedures that can access local data
  1822. in the parent procedures }
  1823. case localloc.loc of
  1824. LOC_CREGISTER :
  1825. if (pi_has_label in current_procinfo.flags) then
  1826. {$ifdef cpu64bitalu}
  1827. if def_cgsize(vardef) in [OS_128,OS_S128] then
  1828. begin
  1829. cg.a_reg_sync(list,localloc.register128.reglo);
  1830. cg.a_reg_sync(list,localloc.register128.reghi);
  1831. end
  1832. else
  1833. {$else cpu64bitalu}
  1834. if def_cgsize(vardef) in [OS_64,OS_S64] then
  1835. begin
  1836. cg.a_reg_sync(list,localloc.register64.reglo);
  1837. cg.a_reg_sync(list,localloc.register64.reghi);
  1838. end
  1839. else
  1840. {$endif cpu64bitalu}
  1841. cg.a_reg_sync(list,localloc.register);
  1842. LOC_CFPUREGISTER,
  1843. LOC_CMMREGISTER:
  1844. if (pi_has_label in current_procinfo.flags) then
  1845. cg.a_reg_sync(list,localloc.register);
  1846. LOC_REFERENCE :
  1847. begin
  1848. if typ in [localvarsym,paravarsym] then
  1849. tg.Ungetlocal(list,localloc.reference);
  1850. end;
  1851. end;
  1852. end;
  1853. end;
  1854. end;
  1855. end;
  1856. procedure gen_intf_wrapper(list:TAsmList;_class:tobjectdef);
  1857. var
  1858. i,j : longint;
  1859. tmps : string;
  1860. pd : TProcdef;
  1861. ImplIntf : TImplementedInterface;
  1862. begin
  1863. for i:=0 to _class.ImplementedInterfaces.count-1 do
  1864. begin
  1865. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  1866. if (ImplIntf=ImplIntf.VtblImplIntf) and
  1867. assigned(ImplIntf.ProcDefs) then
  1868. begin
  1869. maybe_new_object_file(list);
  1870. for j:=0 to ImplIntf.ProcDefs.Count-1 do
  1871. begin
  1872. pd:=TProcdef(ImplIntf.ProcDefs[j]);
  1873. { we don't track method calls via interfaces yet ->
  1874. assume that every method called via an interface call
  1875. is reachable for now }
  1876. if (po_virtualmethod in pd.procoptions) and
  1877. not is_objectpascal_helper(tprocdef(pd).struct) then
  1878. tobjectdef(tprocdef(pd).struct).register_vmt_call(tprocdef(pd).extnumber);
  1879. tmps:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+
  1880. ImplIntf.IntfDef.objname^+'_$_'+tostr(j)+'_$_'+pd.mangledname);
  1881. { create wrapper code }
  1882. new_section(list,sec_code,tmps,0);
  1883. hlcg.init_register_allocators;
  1884. cg.g_intf_wrapper(list,pd,tmps,ImplIntf.ioffset);
  1885. hlcg.done_register_allocators;
  1886. end;
  1887. end;
  1888. end;
  1889. end;
  1890. procedure gen_intf_wrappers(list:TAsmList;st:TSymtable;nested:boolean);
  1891. var
  1892. i : longint;
  1893. def : tdef;
  1894. begin
  1895. if not nested then
  1896. create_hlcodegen;
  1897. for i:=0 to st.DefList.Count-1 do
  1898. begin
  1899. def:=tdef(st.DefList[i]);
  1900. { if def can contain nested types then handle it symtable }
  1901. if def.typ in [objectdef,recorddef] then
  1902. gen_intf_wrappers(list,tabstractrecorddef(def).symtable,true);
  1903. if is_class(def) then
  1904. gen_intf_wrapper(list,tobjectdef(def));
  1905. end;
  1906. if not nested then
  1907. destroy_hlcodegen;
  1908. end;
  1909. procedure gen_load_vmt_register(list:TAsmList;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  1910. var
  1911. href : treference;
  1912. selfdef: tdef;
  1913. begin
  1914. if is_object(objdef) then
  1915. begin
  1916. case selfloc.loc of
  1917. LOC_CREFERENCE,
  1918. LOC_REFERENCE:
  1919. begin
  1920. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset,sizeof(pint));
  1921. cg.a_loadaddr_ref_reg(list,selfloc.reference,href.base);
  1922. selfdef:=getpointerdef(objdef);
  1923. end;
  1924. else
  1925. internalerror(200305056);
  1926. end;
  1927. end
  1928. else
  1929. { This is also valid for Objective-C classes: vmt_offset is 0 there,
  1930. and the first "field" of an Objective-C class instance is a pointer
  1931. to its "meta-class". }
  1932. begin
  1933. selfdef:=objdef;
  1934. case selfloc.loc of
  1935. LOC_REGISTER:
  1936. begin
  1937. {$ifdef cpu_uses_separate_address_registers}
  1938. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  1939. begin
  1940. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset,sizeof(pint));
  1941. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,selfloc.register,href.base);
  1942. end
  1943. else
  1944. {$endif cpu_uses_separate_address_registers}
  1945. reference_reset_base(href,selfloc.register,objdef.vmt_offset,sizeof(pint));
  1946. end;
  1947. LOC_CONSTANT,
  1948. LOC_CREGISTER,
  1949. LOC_CREFERENCE,
  1950. LOC_REFERENCE,
  1951. LOC_CSUBSETREG,
  1952. LOC_SUBSETREG,
  1953. LOC_CSUBSETREF,
  1954. LOC_SUBSETREF:
  1955. begin
  1956. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset,sizeof(pint));
  1957. { todo: pass actual vmt pointer type to hlcg }
  1958. hlcg.a_load_loc_reg(list,voidpointertype,voidpointertype,selfloc,href.base);
  1959. end;
  1960. else
  1961. internalerror(200305057);
  1962. end;
  1963. end;
  1964. vmtreg:=cg.getaddressregister(list);
  1965. hlcg.g_maybe_testself(list,selfdef,href.base);
  1966. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,vmtreg);
  1967. { test validity of VMT }
  1968. if not(is_interface(objdef)) and
  1969. not(is_cppclass(objdef)) and
  1970. not(is_objc_class_or_protocol(objdef)) then
  1971. cg.g_maybe_testvmt(list,vmtreg,objdef);
  1972. end;
  1973. function getprocalign : shortint;
  1974. begin
  1975. { gprof uses 16 byte granularity }
  1976. if (cs_profile in current_settings.moduleswitches) then
  1977. result:=16
  1978. else
  1979. result:=current_settings.alignment.procalign;
  1980. end;
  1981. procedure gen_fpc_dummy(list : TAsmList);
  1982. begin
  1983. {$ifdef i386}
  1984. { fix me! }
  1985. list.concat(Taicpu.Op_const_reg(A_MOV,S_L,1,NR_EAX));
  1986. list.concat(Taicpu.Op_const(A_RET,S_W,12));
  1987. {$endif i386}
  1988. end;
  1989. procedure InsertInterruptTable;
  1990. procedure WriteVector(const name: string);
  1991. {$IFDEF arm}
  1992. var
  1993. ai: taicpu;
  1994. {$ENDIF arm}
  1995. begin
  1996. {$IFDEF arm}
  1997. if current_settings.cputype in [cpu_armv7m] then
  1998. current_asmdata.asmlists[al_globals].concat(tai_const.Createname(name,0))
  1999. else
  2000. begin
  2001. ai:=taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(name));
  2002. ai.is_jmp:=true;
  2003. current_asmdata.asmlists[al_globals].concat(ai);
  2004. end;
  2005. {$ENDIF arm}
  2006. end;
  2007. function GetInterruptTableLength: longint;
  2008. begin
  2009. {$if defined(ARM)}
  2010. result:=embedded_controllers[current_settings.controllertype].interruptvectors;
  2011. {$else}
  2012. result:=0;
  2013. {$endif}
  2014. end;
  2015. var
  2016. hp: tused_unit;
  2017. sym: tsym;
  2018. i, i2: longint;
  2019. interruptTable: array of tprocdef;
  2020. pd: tprocdef;
  2021. begin
  2022. SetLength(interruptTable, GetInterruptTableLength);
  2023. FillChar(interruptTable[0], length(interruptTable)*sizeof(pointer), 0);
  2024. hp:=tused_unit(usedunits.first);
  2025. while assigned(hp) do
  2026. begin
  2027. for i := 0 to hp.u.symlist.Count-1 do
  2028. begin
  2029. sym:=tsym(hp.u.symlist[i]);
  2030. if not assigned(sym) then
  2031. continue;
  2032. if sym.typ = procsym then
  2033. begin
  2034. for i2 := 0 to tprocsym(sym).ProcdefList.Count-1 do
  2035. begin
  2036. pd:=tprocdef(tprocsym(sym).ProcdefList[i2]);
  2037. if pd.interruptvector >= 0 then
  2038. begin
  2039. if pd.interruptvector > high(interruptTable) then
  2040. Internalerror(2011030602);
  2041. if interruptTable[pd.interruptvector] <> nil then
  2042. internalerror(2011030601);
  2043. interruptTable[pd.interruptvector]:=pd;
  2044. break;
  2045. end;
  2046. end;
  2047. end;
  2048. end;
  2049. hp:=tused_unit(hp.next);
  2050. end;
  2051. new_section(current_asmdata.asmlists[al_globals],sec_init,'VECTORS',sizeof(pint));
  2052. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('VECTORS',AT_DATA,0));
  2053. {$IFDEF arm}
  2054. if current_settings.cputype in [cpu_armv7m] then
  2055. current_asmdata.asmlists[al_globals].concat(tai_const.Createname('_stack_top',0)); { ARMv7-M processors have the initial stack value at address 0 }
  2056. {$ENDIF arm}
  2057. for i:=0 to high(interruptTable) do
  2058. begin
  2059. if interruptTable[i]<>nil then
  2060. writeVector(interruptTable[i].mangledname)
  2061. else
  2062. writeVector('DefaultHandler'); { Default handler name }
  2063. end;
  2064. end;
  2065. end.