ncgutil.pas 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236
  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. l.register:=cg.getintregister(list,l.size);
  572. end;
  573. end;
  574. {****************************************************************************
  575. Init/Finalize Code
  576. ****************************************************************************}
  577. procedure copyvalueparas(p:TObject;arg:pointer);
  578. var
  579. href : treference;
  580. hreg : tregister;
  581. list : TAsmList;
  582. hsym : tparavarsym;
  583. l : longint;
  584. localcopyloc : tlocation;
  585. sizedef : tdef;
  586. begin
  587. list:=TAsmList(arg);
  588. if (tsym(p).typ=paravarsym) and
  589. (tparavarsym(p).varspez=vs_value) and
  590. (paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  591. begin
  592. { we have no idea about the alignment at the caller side }
  593. hlcg.location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  594. if is_open_array(tparavarsym(p).vardef) or
  595. is_array_of_const(tparavarsym(p).vardef) then
  596. begin
  597. { cdecl functions don't have a high pointer so it is not possible to generate
  598. a local copy }
  599. if not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  600. begin
  601. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  602. if not assigned(hsym) then
  603. internalerror(200306061);
  604. hreg:=cg.getaddressregister(list);
  605. if not is_packed_array(tparavarsym(p).vardef) then
  606. hlcg.g_copyvaluepara_openarray(list,href,hsym.initialloc,tarraydef(tparavarsym(p).vardef),hreg)
  607. else
  608. internalerror(2006080401);
  609. // cg.g_copyvaluepara_packedopenarray(list,href,hsym.intialloc,tarraydef(tparavarsym(p).vardef).elepackedbitsize,hreg);
  610. sizedef:=getpointerdef(tparavarsym(p).vardef);
  611. hlcg.a_load_reg_loc(list,sizedef,sizedef,hreg,tparavarsym(p).initialloc);
  612. end;
  613. end
  614. else
  615. begin
  616. { Allocate space for the local copy }
  617. l:=tparavarsym(p).getsize;
  618. localcopyloc.loc:=LOC_REFERENCE;
  619. localcopyloc.size:=int_cgsize(l);
  620. tg.GetLocal(list,l,tparavarsym(p).vardef,localcopyloc.reference);
  621. { Copy data }
  622. if is_shortstring(tparavarsym(p).vardef) then
  623. begin
  624. { this code is only executed before the code for the body and the entry/exit code is generated
  625. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  626. }
  627. include(current_procinfo.flags,pi_do_call);
  628. hlcg.g_copyshortstring(list,href,localcopyloc.reference,tstringdef(tparavarsym(p).vardef));
  629. end
  630. else if tparavarsym(p).vardef.typ = variantdef then
  631. begin
  632. { this code is only executed before the code for the body and the entry/exit code is generated
  633. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  634. }
  635. include(current_procinfo.flags,pi_do_call);
  636. hlcg.g_copyvariant(list,href,localcopyloc.reference,tvariantdef(tparavarsym(p).vardef))
  637. end
  638. else
  639. begin
  640. { pass proper alignment info }
  641. localcopyloc.reference.alignment:=tparavarsym(p).vardef.alignment;
  642. cg.g_concatcopy(list,href,localcopyloc.reference,tparavarsym(p).vardef.size);
  643. end;
  644. { update localloc of varsym }
  645. tg.Ungetlocal(list,tparavarsym(p).localloc.reference);
  646. tparavarsym(p).localloc:=localcopyloc;
  647. tparavarsym(p).initialloc:=localcopyloc;
  648. end;
  649. end;
  650. end;
  651. { generates the code for incrementing the reference count of parameters and
  652. initialize out parameters }
  653. procedure init_paras(p:TObject;arg:pointer);
  654. var
  655. href : treference;
  656. hsym : tparavarsym;
  657. eldef : tdef;
  658. list : TAsmList;
  659. needs_inittable : boolean;
  660. begin
  661. list:=TAsmList(arg);
  662. if (tsym(p).typ=paravarsym) then
  663. begin
  664. needs_inittable:=is_managed_type(tparavarsym(p).vardef);
  665. if not needs_inittable then
  666. exit;
  667. case tparavarsym(p).varspez of
  668. vs_value :
  669. begin
  670. { variants are already handled by the call to fpc_variant_copy_overwrite if
  671. they are passed by reference }
  672. if not((tparavarsym(p).vardef.typ=variantdef) and
  673. paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  674. begin
  675. hlcg.location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,is_open_array(tparavarsym(p).vardef),sizeof(pint));
  676. if is_open_array(tparavarsym(p).vardef) then
  677. begin
  678. { open arrays do not contain correct element count in their rtti,
  679. the actual count must be passed separately. }
  680. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  681. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  682. if not assigned(hsym) then
  683. internalerror(201003031);
  684. hlcg.g_array_rtti_helper(list,eldef,href,hsym.initialloc,'fpc_addref_array');
  685. end
  686. else
  687. hlcg.g_incrrefcount(list,tparavarsym(p).vardef,href);
  688. end;
  689. end;
  690. vs_out :
  691. begin
  692. { we have no idea about the alignment at the callee side,
  693. and the user also cannot specify "unaligned" here, so
  694. assume worst case }
  695. hlcg.location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  696. if is_open_array(tparavarsym(p).vardef) then
  697. begin
  698. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  699. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  700. if not assigned(hsym) then
  701. internalerror(201103033);
  702. hlcg.g_array_rtti_helper(list,eldef,href,hsym.initialloc,'fpc_initialize_array');
  703. end
  704. else
  705. hlcg.g_initialize(list,tparavarsym(p).vardef,href);
  706. end;
  707. end;
  708. end;
  709. end;
  710. procedure gen_alloc_regloc(list:TAsmList;var loc: tlocation);
  711. begin
  712. case loc.loc of
  713. LOC_CREGISTER:
  714. begin
  715. {$ifdef cpu64bitalu}
  716. if loc.size in [OS_128,OS_S128] then
  717. begin
  718. loc.register128.reglo:=cg.getintregister(list,OS_64);
  719. loc.register128.reghi:=cg.getintregister(list,OS_64);
  720. end
  721. else
  722. {$else cpu64bitalu}
  723. if loc.size in [OS_64,OS_S64] then
  724. begin
  725. loc.register64.reglo:=cg.getintregister(list,OS_32);
  726. loc.register64.reghi:=cg.getintregister(list,OS_32);
  727. end
  728. else
  729. {$endif cpu64bitalu}
  730. loc.register:=cg.getintregister(list,loc.size);
  731. end;
  732. LOC_CFPUREGISTER:
  733. begin
  734. loc.register:=cg.getfpuregister(list,loc.size);
  735. end;
  736. LOC_CMMREGISTER:
  737. begin
  738. loc.register:=cg.getmmregister(list,loc.size);
  739. end;
  740. end;
  741. end;
  742. procedure gen_alloc_regvar(list:TAsmList;sym: tabstractnormalvarsym; allocreg: boolean);
  743. begin
  744. if allocreg then
  745. gen_alloc_regloc(list,sym.initialloc);
  746. if (pi_has_label in current_procinfo.flags) then
  747. begin
  748. { Allocate register already, to prevent first allocation to be
  749. inside a loop }
  750. {$ifdef cpu64bitalu}
  751. if sym.initialloc.size in [OS_128,OS_S128] then
  752. begin
  753. cg.a_reg_sync(list,sym.initialloc.register128.reglo);
  754. cg.a_reg_sync(list,sym.initialloc.register128.reghi);
  755. end
  756. else
  757. {$else cpu64bitalu}
  758. if sym.initialloc.size in [OS_64,OS_S64] then
  759. begin
  760. cg.a_reg_sync(list,sym.initialloc.register64.reglo);
  761. cg.a_reg_sync(list,sym.initialloc.register64.reghi);
  762. end
  763. else
  764. {$endif cpu64bitalu}
  765. cg.a_reg_sync(list,sym.initialloc.register);
  766. end;
  767. sym.localloc:=sym.initialloc;
  768. end;
  769. procedure gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);
  770. procedure unget_para(const paraloc:TCGParaLocation);
  771. begin
  772. case paraloc.loc of
  773. LOC_REGISTER :
  774. begin
  775. if getsupreg(paraloc.register)<first_int_imreg then
  776. cg.ungetcpuregister(list,paraloc.register);
  777. end;
  778. LOC_MMREGISTER :
  779. begin
  780. if getsupreg(paraloc.register)<first_mm_imreg then
  781. cg.ungetcpuregister(list,paraloc.register);
  782. end;
  783. LOC_FPUREGISTER :
  784. begin
  785. if getsupreg(paraloc.register)<first_fpu_imreg then
  786. cg.ungetcpuregister(list,paraloc.register);
  787. end;
  788. end;
  789. end;
  790. var
  791. paraloc : pcgparalocation;
  792. href : treference;
  793. sizeleft : aint;
  794. {$if defined(sparc) or defined(arm) or defined(mips)}
  795. tempref : treference;
  796. {$endif defined(sparc) or defined(arm) or defined(mips)}
  797. {$ifdef mips}
  798. tmpreg : tregister;
  799. {$endif mips}
  800. {$ifndef cpu64bitalu}
  801. tempreg : tregister;
  802. reg64 : tregister64;
  803. {$endif not cpu64bitalu}
  804. begin
  805. paraloc:=para.location;
  806. if not assigned(paraloc) then
  807. internalerror(200408203);
  808. { skip e.g. empty records }
  809. if (paraloc^.loc = LOC_VOID) then
  810. exit;
  811. case destloc.loc of
  812. LOC_REFERENCE :
  813. begin
  814. { If the parameter location is reused we don't need to copy
  815. anything }
  816. if not reusepara then
  817. begin
  818. href:=destloc.reference;
  819. sizeleft:=para.intsize;
  820. while assigned(paraloc) do
  821. begin
  822. if (paraloc^.size=OS_NO) then
  823. begin
  824. { Can only be a reference that contains the rest
  825. of the parameter }
  826. if (paraloc^.loc<>LOC_REFERENCE) or
  827. assigned(paraloc^.next) then
  828. internalerror(2005013010);
  829. cg.a_load_cgparaloc_ref(list,paraloc^,href,sizeleft,destloc.reference.alignment);
  830. inc(href.offset,sizeleft);
  831. sizeleft:=0;
  832. end
  833. else
  834. begin
  835. cg.a_load_cgparaloc_ref(list,paraloc^,href,tcgsize2size[paraloc^.size],destloc.reference.alignment);
  836. inc(href.offset,TCGSize2Size[paraloc^.size]);
  837. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  838. end;
  839. unget_para(paraloc^);
  840. paraloc:=paraloc^.next;
  841. end;
  842. end;
  843. end;
  844. LOC_REGISTER,
  845. LOC_CREGISTER :
  846. begin
  847. {$ifdef cpu64bitalu}
  848. if (para.size in [OS_128,OS_S128,OS_F128]) and
  849. ({ in case of fpu emulation, or abi's that pass fpu values
  850. via integer registers }
  851. (vardef.typ=floatdef) or
  852. is_methodpointer(vardef)) then
  853. begin
  854. case paraloc^.loc of
  855. LOC_REGISTER:
  856. begin
  857. if not assigned(paraloc^.next) then
  858. internalerror(200410104);
  859. if (target_info.endian=ENDIAN_BIG) then
  860. begin
  861. { paraloc^ -> high
  862. paraloc^.next -> low }
  863. unget_para(paraloc^);
  864. gen_alloc_regloc(list,destloc);
  865. { reg->reg, alignment is irrelevant }
  866. cg.a_load_cgparaloc_anyreg(list,OS_64,paraloc^,destloc.register128.reghi,8);
  867. unget_para(paraloc^.next^);
  868. cg.a_load_cgparaloc_anyreg(list,OS_64,paraloc^.next^,destloc.register128.reglo,8);
  869. end
  870. else
  871. begin
  872. { paraloc^ -> low
  873. paraloc^.next -> high }
  874. unget_para(paraloc^);
  875. gen_alloc_regloc(list,destloc);
  876. cg.a_load_cgparaloc_anyreg(list,OS_64,paraloc^,destloc.register128.reglo,8);
  877. unget_para(paraloc^.next^);
  878. cg.a_load_cgparaloc_anyreg(list,OS_64,paraloc^.next^,destloc.register128.reghi,8);
  879. end;
  880. end;
  881. LOC_REFERENCE:
  882. begin
  883. gen_alloc_regloc(list,destloc);
  884. reference_reset_base(href,paraloc^.reference.index,paraloc^.reference.offset,para.alignment);
  885. cg128.a_load128_ref_reg(list,href,destloc.register128);
  886. unget_para(paraloc^);
  887. end;
  888. else
  889. internalerror(2012090607);
  890. end
  891. end
  892. else
  893. {$else cpu64bitalu}
  894. if (para.size in [OS_64,OS_S64,OS_F64]) and
  895. (is_64bit(vardef) or
  896. { in case of fpu emulation, or abi's that pass fpu values
  897. via integer registers }
  898. (vardef.typ=floatdef) or
  899. is_methodpointer(vardef)) then
  900. begin
  901. case paraloc^.loc of
  902. LOC_REGISTER:
  903. begin
  904. if not assigned(paraloc^.next) then
  905. internalerror(200410104);
  906. if (target_info.endian=ENDIAN_BIG) then
  907. begin
  908. { paraloc^ -> high
  909. paraloc^.next -> low }
  910. unget_para(paraloc^);
  911. gen_alloc_regloc(list,destloc);
  912. { reg->reg, alignment is irrelevant }
  913. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^,destloc.register64.reghi,4);
  914. unget_para(paraloc^.next^);
  915. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^.next^,destloc.register64.reglo,4);
  916. end
  917. else
  918. begin
  919. { paraloc^ -> low
  920. paraloc^.next -> high }
  921. unget_para(paraloc^);
  922. gen_alloc_regloc(list,destloc);
  923. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^,destloc.register64.reglo,4);
  924. unget_para(paraloc^.next^);
  925. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^.next^,destloc.register64.reghi,4);
  926. end;
  927. end;
  928. LOC_REFERENCE:
  929. begin
  930. gen_alloc_regloc(list,destloc);
  931. reference_reset_base(href,paraloc^.reference.index,paraloc^.reference.offset,para.alignment);
  932. cg64.a_load64_ref_reg(list,href,destloc.register64);
  933. unget_para(paraloc^);
  934. end;
  935. else
  936. internalerror(2005101501);
  937. end
  938. end
  939. else
  940. {$endif cpu64bitalu}
  941. begin
  942. if assigned(paraloc^.next) then
  943. internalerror(200410105);
  944. unget_para(paraloc^);
  945. gen_alloc_regloc(list,destloc);
  946. cg.a_load_cgparaloc_anyreg(list,destloc.size,paraloc^,destloc.register,sizeof(aint));
  947. end;
  948. end;
  949. LOC_FPUREGISTER,
  950. LOC_CFPUREGISTER :
  951. begin
  952. {$ifdef mips}
  953. if (destloc.size = paraloc^.Size) and
  954. (paraloc^.Loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  955. begin
  956. gen_alloc_regloc(list,destloc);
  957. cg.a_loadfpu_reg_reg(list,paraloc^.Size, destloc.size, paraloc^.register, destloc.register);
  958. end
  959. else if (destloc.size = OS_F32) and
  960. (paraloc^.Loc in [LOC_REGISTER,LOC_CREGISTER]) then
  961. begin
  962. gen_alloc_regloc(list,destloc);
  963. list.Concat(taicpu.op_reg_reg(A_MTC1,paraloc^.register,destloc.register));
  964. end
  965. else if (destloc.size = OS_F64) and
  966. (paraloc^.Loc in [LOC_REGISTER,LOC_CREGISTER]) and
  967. (paraloc^.next^.Loc in [LOC_REGISTER,LOC_CREGISTER]) then
  968. begin
  969. gen_alloc_regloc(list,destloc);
  970. tmpreg:=destloc.register;
  971. list.Concat(taicpu.op_reg_reg(A_MTC1,paraloc^.register,tmpreg));
  972. setsupreg(tmpreg,getsupreg(tmpreg)+1);
  973. list.Concat(taicpu.op_reg_reg(A_MTC1,paraloc^.Next^.register,tmpreg));
  974. end
  975. else
  976. begin
  977. sizeleft := TCGSize2Size[destloc.size];
  978. tg.GetTemp(list,sizeleft,sizeleft,tt_normal,tempref);
  979. href:=tempref;
  980. while assigned(paraloc) do
  981. begin
  982. unget_para(paraloc^);
  983. cg.a_load_cgparaloc_ref(list,paraloc^,href,sizeleft,destloc.reference.alignment);
  984. inc(href.offset,TCGSize2Size[paraloc^.size]);
  985. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  986. paraloc:=paraloc^.next;
  987. end;
  988. gen_alloc_regloc(list,destloc);
  989. cg.a_loadfpu_ref_reg(list,destloc.size,destloc.size,tempref,destloc.register);
  990. tg.UnGetTemp(list,tempref);
  991. end;
  992. {$else mips}
  993. {$if defined(sparc) or defined(arm)}
  994. { Arm and Sparc passes floats in int registers, when loading to fpu register
  995. we need a temp }
  996. sizeleft := TCGSize2Size[destloc.size];
  997. tg.GetTemp(list,sizeleft,sizeleft,tt_normal,tempref);
  998. href:=tempref;
  999. while assigned(paraloc) do
  1000. begin
  1001. unget_para(paraloc^);
  1002. cg.a_load_cgparaloc_ref(list,paraloc^,href,sizeleft,destloc.reference.alignment);
  1003. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1004. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1005. paraloc:=paraloc^.next;
  1006. end;
  1007. gen_alloc_regloc(list,destloc);
  1008. cg.a_loadfpu_ref_reg(list,destloc.size,destloc.size,tempref,destloc.register);
  1009. tg.UnGetTemp(list,tempref);
  1010. {$else defined(sparc) or defined(arm)}
  1011. unget_para(paraloc^);
  1012. gen_alloc_regloc(list,destloc);
  1013. { from register to register -> alignment is irrelevant }
  1014. cg.a_load_cgparaloc_anyreg(list,destloc.size,paraloc^,destloc.register,0);
  1015. if assigned(paraloc^.next) then
  1016. internalerror(200410109);
  1017. {$endif defined(sparc) or defined(arm)}
  1018. {$endif mips}
  1019. end;
  1020. LOC_MMREGISTER,
  1021. LOC_CMMREGISTER :
  1022. begin
  1023. {$ifndef cpu64bitalu}
  1024. { ARM vfp floats are passed in integer registers }
  1025. if (para.size=OS_F64) and
  1026. (paraloc^.size in [OS_32,OS_S32]) and
  1027. use_vectorfpu(vardef) then
  1028. begin
  1029. { we need 2x32bit reg }
  1030. if not assigned(paraloc^.next) or
  1031. assigned(paraloc^.next^.next) then
  1032. internalerror(2009112421);
  1033. unget_para(paraloc^.next^);
  1034. case paraloc^.next^.loc of
  1035. LOC_REGISTER:
  1036. tempreg:=paraloc^.next^.register;
  1037. LOC_REFERENCE:
  1038. begin
  1039. tempreg:=cg.getintregister(list,OS_32);
  1040. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^.next^,tempreg,4);
  1041. end;
  1042. else
  1043. internalerror(2012051301);
  1044. end;
  1045. { don't free before the above, because then the getintregister
  1046. could reallocate this register and overwrite it }
  1047. unget_para(paraloc^);
  1048. gen_alloc_regloc(list,destloc);
  1049. if (target_info.endian=endian_big) then
  1050. { paraloc^ -> high
  1051. paraloc^.next -> low }
  1052. reg64:=joinreg64(tempreg,paraloc^.register)
  1053. else
  1054. reg64:=joinreg64(paraloc^.register,tempreg);
  1055. cg64.a_loadmm_intreg64_reg(list,OS_F64,reg64,destloc.register);
  1056. end
  1057. else
  1058. {$endif not cpu64bitalu}
  1059. begin
  1060. unget_para(paraloc^);
  1061. gen_alloc_regloc(list,destloc);
  1062. { from register to register -> alignment is irrelevant }
  1063. cg.a_load_cgparaloc_anyreg(list,destloc.size,paraloc^,destloc.register,0);
  1064. { data could come in two memory locations, for now
  1065. we simply ignore the sanity check (FK)
  1066. if assigned(paraloc^.next) then
  1067. internalerror(200410108);
  1068. }
  1069. end;
  1070. end;
  1071. else
  1072. internalerror(2010052903);
  1073. end;
  1074. end;
  1075. procedure gen_load_para_value(list:TAsmList);
  1076. procedure get_para(const paraloc:TCGParaLocation);
  1077. begin
  1078. case paraloc.loc of
  1079. LOC_REGISTER :
  1080. begin
  1081. if getsupreg(paraloc.register)<first_int_imreg then
  1082. cg.getcpuregister(list,paraloc.register);
  1083. end;
  1084. LOC_MMREGISTER :
  1085. begin
  1086. if getsupreg(paraloc.register)<first_mm_imreg then
  1087. cg.getcpuregister(list,paraloc.register);
  1088. end;
  1089. LOC_FPUREGISTER :
  1090. begin
  1091. if getsupreg(paraloc.register)<first_fpu_imreg then
  1092. cg.getcpuregister(list,paraloc.register);
  1093. end;
  1094. end;
  1095. end;
  1096. var
  1097. i : longint;
  1098. currpara : tparavarsym;
  1099. paraloc : pcgparalocation;
  1100. begin
  1101. if (po_assembler in current_procinfo.procdef.procoptions) or
  1102. { exceptfilters have a single hidden 'parentfp' parameter, which
  1103. is handled by tcg.g_proc_entry. }
  1104. (current_procinfo.procdef.proctypeoption=potype_exceptfilter) then
  1105. exit;
  1106. { Allocate registers used by parameters }
  1107. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1108. begin
  1109. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1110. paraloc:=currpara.paraloc[calleeside].location;
  1111. while assigned(paraloc) do
  1112. begin
  1113. if paraloc^.loc in [LOC_REGISTER,LOC_FPUREGISTER,LOC_MMREGISTER] then
  1114. get_para(paraloc^);
  1115. paraloc:=paraloc^.next;
  1116. end;
  1117. end;
  1118. { Copy parameters to local references/registers }
  1119. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1120. begin
  1121. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1122. gen_load_cgpara_loc(list,currpara.vardef,currpara.paraloc[calleeside],currpara.initialloc,paramanager.param_use_paraloc(currpara.paraloc[calleeside]));
  1123. { gen_load_cgpara_loc() already allocated the initialloc
  1124. -> don't allocate again }
  1125. if currpara.initialloc.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMREGISTER] then
  1126. gen_alloc_regvar(list,currpara,false);
  1127. end;
  1128. { generate copies of call by value parameters, must be done before
  1129. the initialization and body is parsed because the refcounts are
  1130. incremented using the local copies }
  1131. current_procinfo.procdef.parast.SymList.ForEachCall(@copyvalueparas,list);
  1132. {$ifdef powerpc}
  1133. { unget the register that contains the stack pointer before the procedure entry, }
  1134. { which is used to access the parameters in their original callee-side location }
  1135. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1136. cg.a_reg_dealloc(list,NR_R12);
  1137. {$endif powerpc}
  1138. {$ifdef powerpc64}
  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_OLD_STACK_POINTER_REG);
  1143. {$endif powerpc64}
  1144. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1145. begin
  1146. { initialize refcounted paras, and trash others. Needed here
  1147. instead of in gen_initialize_code, because when a reference is
  1148. intialised or trashed while the pointer to that reference is kept
  1149. in a regvar, we add a register move and that one again has to
  1150. come after the parameter loading code as far as the register
  1151. allocator is concerned }
  1152. current_procinfo.procdef.parast.SymList.ForEachCall(@init_paras,list);
  1153. end;
  1154. end;
  1155. {****************************************************************************
  1156. Entry/Exit
  1157. ****************************************************************************}
  1158. function has_alias_name(pd:tprocdef;const s:string):boolean;
  1159. var
  1160. item : TCmdStrListItem;
  1161. begin
  1162. result:=true;
  1163. if pd.mangledname=s then
  1164. exit;
  1165. item := TCmdStrListItem(pd.aliasnames.first);
  1166. while assigned(item) do
  1167. begin
  1168. if item.str=s then
  1169. exit;
  1170. item := TCmdStrListItem(item.next);
  1171. end;
  1172. result:=false;
  1173. end;
  1174. procedure alloc_proc_symbol(pd: tprocdef);
  1175. var
  1176. item : TCmdStrListItem;
  1177. begin
  1178. item := TCmdStrListItem(pd.aliasnames.first);
  1179. while assigned(item) do
  1180. begin
  1181. current_asmdata.DefineAsmSymbol(item.str,AB_GLOBAL,AT_FUNCTION);
  1182. item := TCmdStrListItem(item.next);
  1183. end;
  1184. end;
  1185. procedure gen_proc_symbol(list:TAsmList);
  1186. var
  1187. item,
  1188. previtem : TCmdStrListItem;
  1189. begin
  1190. previtem:=nil;
  1191. item := TCmdStrListItem(current_procinfo.procdef.aliasnames.first);
  1192. while assigned(item) do
  1193. begin
  1194. {$ifdef arm}
  1195. if current_settings.cputype in cpu_thumb2 then
  1196. list.concat(tai_thumb_func.create);
  1197. {$endif arm}
  1198. { "double link" all procedure entry symbols via .reference }
  1199. { directives on darwin, because otherwise the linker }
  1200. { sometimes strips the procedure if only on of the symbols }
  1201. { is referenced }
  1202. if assigned(previtem) and
  1203. (target_info.system in systems_darwin) then
  1204. list.concat(tai_directive.create(asd_reference,item.str));
  1205. if (cs_profile in current_settings.moduleswitches) or
  1206. (po_global in current_procinfo.procdef.procoptions) then
  1207. list.concat(Tai_symbol.createname_global(item.str,AT_FUNCTION,0))
  1208. else
  1209. list.concat(Tai_symbol.createname(item.str,AT_FUNCTION,0));
  1210. if assigned(previtem) and
  1211. (target_info.system in systems_darwin) then
  1212. list.concat(tai_directive.create(asd_reference,previtem.str));
  1213. if not(af_stabs_use_function_absolute_addresses in target_asm.flags) then
  1214. list.concat(Tai_function_name.create(item.str));
  1215. previtem:=item;
  1216. item := TCmdStrListItem(item.next);
  1217. end;
  1218. current_procinfo.procdef.procstarttai:=tai(list.last);
  1219. end;
  1220. procedure gen_proc_entry_code(list:TAsmList);
  1221. var
  1222. hitemp,
  1223. lotemp, stack_frame_size : longint;
  1224. begin
  1225. { generate call frame marker for dwarf call frame info }
  1226. current_asmdata.asmcfi.start_frame(list);
  1227. { All temps are know, write offsets used for information }
  1228. if (cs_asm_source in current_settings.globalswitches) then
  1229. begin
  1230. if tg.direction>0 then
  1231. begin
  1232. lotemp:=current_procinfo.tempstart;
  1233. hitemp:=tg.lasttemp;
  1234. end
  1235. else
  1236. begin
  1237. lotemp:=tg.lasttemp;
  1238. hitemp:=current_procinfo.tempstart;
  1239. end;
  1240. list.concat(Tai_comment.Create(strpnew('Temps allocated between '+std_regname(current_procinfo.framepointer)+
  1241. tostr_with_plus(lotemp)+' and '+std_regname(current_procinfo.framepointer)+tostr_with_plus(hitemp))));
  1242. end;
  1243. { generate target specific proc entry code }
  1244. stack_frame_size := current_procinfo.calc_stackframe_size;
  1245. if (stack_frame_size <> 0) and
  1246. (po_nostackframe in current_procinfo.procdef.procoptions) then
  1247. message1(parser_e_nostackframe_with_locals,tostr(stack_frame_size));
  1248. hlcg.g_proc_entry(list,stack_frame_size,(po_nostackframe in current_procinfo.procdef.procoptions));
  1249. end;
  1250. procedure gen_proc_exit_code(list:TAsmList);
  1251. var
  1252. parasize : longint;
  1253. begin
  1254. { c style clearstack does not need to remove parameters from the stack, only the
  1255. return value when it was pushed by arguments }
  1256. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1257. begin
  1258. parasize:=0;
  1259. if paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef.proccalloption) then
  1260. inc(parasize,sizeof(pint));
  1261. end
  1262. else
  1263. begin
  1264. parasize:=current_procinfo.para_stack_size;
  1265. { the parent frame pointer para has to be removed by the caller in
  1266. case of Delphi-style parent frame pointer passing }
  1267. if not paramanager.use_fixed_stack and
  1268. (po_delphi_nested_cc in current_procinfo.procdef.procoptions) then
  1269. dec(parasize,sizeof(pint));
  1270. end;
  1271. { generate target specific proc exit code }
  1272. hlcg.g_proc_exit(list,parasize,(po_nostackframe in current_procinfo.procdef.procoptions));
  1273. { release return registers, needed for optimizer }
  1274. if not is_void(current_procinfo.procdef.returndef) then
  1275. paramanager.freecgpara(list,current_procinfo.procdef.funcretloc[calleeside]);
  1276. { end of frame marker for call frame info }
  1277. current_asmdata.asmcfi.end_frame(list);
  1278. end;
  1279. procedure gen_stack_check_size_para(list:TAsmList);
  1280. var
  1281. paraloc1 : tcgpara;
  1282. begin
  1283. paraloc1.init;
  1284. paramanager.getintparaloc(pocall_default,1,ptruinttype,paraloc1);
  1285. cg.a_load_const_cgpara(list,OS_INT,current_procinfo.calc_stackframe_size,paraloc1);
  1286. paramanager.freecgpara(list,paraloc1);
  1287. paraloc1.done;
  1288. end;
  1289. procedure gen_stack_check_call(list:TAsmList);
  1290. var
  1291. paraloc1 : tcgpara;
  1292. begin
  1293. paraloc1.init;
  1294. { Also alloc the register needed for the parameter }
  1295. paramanager.getintparaloc(pocall_default,1,ptruinttype,paraloc1);
  1296. paramanager.freecgpara(list,paraloc1);
  1297. { Call the helper }
  1298. cg.allocallcpuregisters(list);
  1299. cg.a_call_name(list,'FPC_STACKCHECK',false);
  1300. cg.deallocallcpuregisters(list);
  1301. paraloc1.done;
  1302. end;
  1303. procedure gen_save_used_regs(list:TAsmList);
  1304. begin
  1305. { Pure assembler routines need to save the registers themselves }
  1306. if (po_assembler in current_procinfo.procdef.procoptions) then
  1307. exit;
  1308. { oldfpccall expects all registers to be destroyed }
  1309. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1310. cg.g_save_registers(list);
  1311. end;
  1312. procedure gen_restore_used_regs(list:TAsmList);
  1313. begin
  1314. { Pure assembler routines need to save the registers themselves }
  1315. if (po_assembler in current_procinfo.procdef.procoptions) then
  1316. exit;
  1317. { oldfpccall expects all registers to be destroyed }
  1318. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1319. cg.g_restore_registers(list);
  1320. end;
  1321. {****************************************************************************
  1322. External handling
  1323. ****************************************************************************}
  1324. procedure gen_external_stub(list:TAsmList;pd:tprocdef;const externalname:string);
  1325. begin
  1326. create_hlcodegen;
  1327. { add the procedure to the al_procedures }
  1328. maybe_new_object_file(list);
  1329. new_section(list,sec_code,lower(pd.mangledname),current_settings.alignment.procalign);
  1330. list.concat(Tai_align.create(current_settings.alignment.procalign));
  1331. if (po_global in pd.procoptions) then
  1332. list.concat(Tai_symbol.createname_global(pd.mangledname,AT_FUNCTION,0))
  1333. else
  1334. list.concat(Tai_symbol.createname(pd.mangledname,AT_FUNCTION,0));
  1335. cg.g_external_wrapper(list,pd,externalname);
  1336. destroy_hlcodegen;
  1337. end;
  1338. {****************************************************************************
  1339. Const Data
  1340. ****************************************************************************}
  1341. procedure gen_alloc_symtable(list:TAsmList;pd:tprocdef;st:TSymtable);
  1342. procedure setlocalloc(vs:tabstractnormalvarsym);
  1343. begin
  1344. if cs_asm_source in current_settings.globalswitches then
  1345. begin
  1346. case vs.initialloc.loc of
  1347. LOC_REFERENCE :
  1348. begin
  1349. if not assigned(vs.initialloc.reference.symbol) then
  1350. list.concat(Tai_comment.Create(strpnew('Var '+vs.realname+' located at '+
  1351. std_regname(vs.initialloc.reference.base)+tostr_with_plus(vs.initialloc.reference.offset))));
  1352. end;
  1353. end;
  1354. end;
  1355. vs.localloc:=vs.initialloc;
  1356. end;
  1357. var
  1358. i : longint;
  1359. sym : tsym;
  1360. vs : tabstractnormalvarsym;
  1361. isaddr : boolean;
  1362. begin
  1363. for i:=0 to st.SymList.Count-1 do
  1364. begin
  1365. sym:=tsym(st.SymList[i]);
  1366. case sym.typ of
  1367. staticvarsym :
  1368. begin
  1369. vs:=tabstractnormalvarsym(sym);
  1370. { The code in loadnode.pass_generatecode will create the
  1371. LOC_REFERENCE instead for all none register variables. This is
  1372. required because we can't store an asmsymbol in the localloc because
  1373. the asmsymbol is invalid after an unit is compiled. This gives
  1374. problems when this procedure is inlined in another unit (PFV) }
  1375. if vs.is_regvar(false) then
  1376. begin
  1377. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable];
  1378. vs.initialloc.size:=def_cgsize(vs.vardef);
  1379. gen_alloc_regvar(list,vs,true);
  1380. setlocalloc(vs);
  1381. end;
  1382. end;
  1383. paravarsym :
  1384. begin
  1385. vs:=tabstractnormalvarsym(sym);
  1386. { Parameters passed to assembler procedures need to be kept
  1387. in the original location }
  1388. if (po_assembler in current_procinfo.procdef.procoptions) then
  1389. tparavarsym(vs).paraloc[calleeside].get_location(vs.initialloc)
  1390. { exception filters receive their frame pointer as a parameter }
  1391. else if (current_procinfo.procdef.proctypeoption=potype_exceptfilter) and
  1392. (vo_is_parentfp in vs.varoptions) then
  1393. begin
  1394. location_reset(vs.initialloc,LOC_REGISTER,OS_ADDR);
  1395. vs.initialloc.register:=NR_FRAME_POINTER_REG;
  1396. end
  1397. else
  1398. begin
  1399. isaddr:=paramanager.push_addr_param(vs.varspez,vs.vardef,current_procinfo.procdef.proccalloption);
  1400. if isaddr then
  1401. vs.initialloc.size:=OS_ADDR
  1402. else
  1403. vs.initialloc.size:=def_cgsize(vs.vardef);
  1404. if vs.is_regvar(isaddr) then
  1405. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable]
  1406. else
  1407. begin
  1408. vs.initialloc.loc:=LOC_REFERENCE;
  1409. { Reuse the parameter location for values to are at a single location on the stack }
  1410. if paramanager.param_use_paraloc(tparavarsym(sym).paraloc[calleeside]) then
  1411. begin
  1412. reference_reset_base(vs.initialloc.reference,tparavarsym(sym).paraloc[calleeside].location^.reference.index,
  1413. tparavarsym(sym).paraloc[calleeside].location^.reference.offset,tparavarsym(sym).paraloc[calleeside].alignment);
  1414. end
  1415. else
  1416. begin
  1417. if isaddr then
  1418. tg.GetLocal(list,sizeof(pint),voidpointertype,vs.initialloc.reference)
  1419. else
  1420. tg.GetLocal(list,vs.getsize,tparavarsym(sym).paraloc[calleeside].alignment,vs.vardef,vs.initialloc.reference);
  1421. end;
  1422. end;
  1423. end;
  1424. setlocalloc(vs);
  1425. end;
  1426. localvarsym :
  1427. begin
  1428. vs:=tabstractnormalvarsym(sym);
  1429. vs.initialloc.size:=def_cgsize(vs.vardef);
  1430. if ([po_assembler,po_nostackframe] * current_procinfo.procdef.procoptions = [po_assembler,po_nostackframe]) and
  1431. (vo_is_funcret in vs.varoptions) then
  1432. begin
  1433. paramanager.create_funcretloc_info(pd,calleeside);
  1434. if assigned(pd.funcretloc[calleeside].location^.next) then
  1435. begin
  1436. { can't replace references to "result" with a complex
  1437. location expression inside assembler code }
  1438. location_reset(vs.initialloc,LOC_INVALID,OS_NO);
  1439. end
  1440. else
  1441. pd.funcretloc[calleeside].get_location(vs.initialloc);
  1442. end
  1443. else if (m_delphi in current_settings.modeswitches) and
  1444. (po_assembler in current_procinfo.procdef.procoptions) and
  1445. (vo_is_funcret in vs.varoptions) and
  1446. (vs.refs=0) then
  1447. begin
  1448. { not referenced, so don't allocate. Use dummy to }
  1449. { avoid ie's later on because of LOC_INVALID }
  1450. vs.initialloc.loc:=LOC_REGISTER;
  1451. vs.initialloc.size:=OS_INT;
  1452. vs.initialloc.register:=NR_FUNCTION_RESULT_REG;
  1453. end
  1454. else if vs.is_regvar(false) then
  1455. begin
  1456. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable];
  1457. gen_alloc_regvar(list,vs,true);
  1458. end
  1459. else
  1460. begin
  1461. vs.initialloc.loc:=LOC_REFERENCE;
  1462. tg.GetLocal(list,vs.getsize,vs.vardef,vs.initialloc.reference);
  1463. end;
  1464. setlocalloc(vs);
  1465. end;
  1466. end;
  1467. end;
  1468. end;
  1469. procedure add_regvars(var rv: tusedregvars; const location: tlocation);
  1470. begin
  1471. case location.loc of
  1472. LOC_CREGISTER:
  1473. {$ifdef cpu64bitalu}
  1474. if location.size in [OS_128,OS_S128] then
  1475. begin
  1476. rv.intregvars.addnodup(getsupreg(location.register128.reglo));
  1477. rv.intregvars.addnodup(getsupreg(location.register128.reghi));
  1478. end
  1479. else
  1480. {$else cpu64bitalu}
  1481. if location.size in [OS_64,OS_S64] then
  1482. begin
  1483. rv.intregvars.addnodup(getsupreg(location.register64.reglo));
  1484. rv.intregvars.addnodup(getsupreg(location.register64.reghi));
  1485. end
  1486. else
  1487. {$endif cpu64bitalu}
  1488. rv.intregvars.addnodup(getsupreg(location.register));
  1489. LOC_CFPUREGISTER:
  1490. rv.fpuregvars.addnodup(getsupreg(location.register));
  1491. LOC_CMMREGISTER:
  1492. rv.mmregvars.addnodup(getsupreg(location.register));
  1493. end;
  1494. end;
  1495. function do_get_used_regvars(var n: tnode; arg: pointer): foreachnoderesult;
  1496. var
  1497. rv: pusedregvars absolute arg;
  1498. begin
  1499. case (n.nodetype) of
  1500. temprefn:
  1501. { We only have to synchronise a tempnode before a loop if it is }
  1502. { not created inside the loop, and only synchronise after the }
  1503. { loop if it's not destroyed inside the loop. If it's created }
  1504. { before the loop and not yet destroyed, then before the loop }
  1505. { is secondpassed tempinfo^.valid will be true, and we get the }
  1506. { correct registers. If it's not destroyed inside the loop, }
  1507. { then after the loop has been secondpassed tempinfo^.valid }
  1508. { be true and we also get the right registers. In other cases, }
  1509. { tempinfo^.valid will be false and so we do not add }
  1510. { unnecessary registers. This way, we don't have to look at }
  1511. { tempcreate and tempdestroy nodes to get this info (JM) }
  1512. if (ti_valid in ttemprefnode(n).tempinfo^.flags) then
  1513. add_regvars(rv^,ttemprefnode(n).tempinfo^.location);
  1514. loadn:
  1515. if (tloadnode(n).symtableentry.typ in [staticvarsym,localvarsym,paravarsym]) then
  1516. add_regvars(rv^,tabstractnormalvarsym(tloadnode(n).symtableentry).localloc);
  1517. vecn:
  1518. { range checks sometimes need the high parameter }
  1519. if (cs_check_range in current_settings.localswitches) and
  1520. (is_open_array(tvecnode(n).left.resultdef) or
  1521. is_array_of_const(tvecnode(n).left.resultdef)) and
  1522. not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  1523. add_regvars(rv^,tabstractnormalvarsym(get_high_value_sym(tparavarsym(tloadnode(tvecnode(n).left).symtableentry))).localloc)
  1524. end;
  1525. result := fen_true;
  1526. end;
  1527. procedure get_used_regvars(n: tnode; var rv: tusedregvars);
  1528. begin
  1529. foreachnodestatic(n,@do_get_used_regvars,@rv);
  1530. end;
  1531. (*
  1532. See comments at declaration of pusedregvarscommon
  1533. function do_get_used_regvars_common(var n: tnode; arg: pointer): foreachnoderesult;
  1534. var
  1535. rv: pusedregvarscommon absolute arg;
  1536. begin
  1537. if (n.nodetype = loadn) and
  1538. (tloadnode(n).symtableentry.typ in [staticvarsym,localvarsym,paravarsym]) then
  1539. with tabstractnormalvarsym(tloadnode(n).symtableentry).localloc do
  1540. case loc of
  1541. LOC_CREGISTER:
  1542. { if not yet encountered in this node tree }
  1543. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  1544. { but nevertheless already encountered somewhere }
  1545. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  1546. { then it's a regvar used in two or more node trees }
  1547. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  1548. LOC_CFPUREGISTER:
  1549. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  1550. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  1551. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  1552. LOC_CMMREGISTER:
  1553. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  1554. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  1555. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  1556. end;
  1557. result := fen_true;
  1558. end;
  1559. procedure get_used_regvars_common(n: tnode; var rv: tusedregvarscommon);
  1560. begin
  1561. rv.myregvars.intregvars.clear;
  1562. rv.myregvars.fpuregvars.clear;
  1563. rv.myregvars.mmregvars.clear;
  1564. foreachnodestatic(n,@do_get_used_regvars_common,@rv);
  1565. end;
  1566. *)
  1567. procedure gen_sync_regvars(list:TAsmList; var rv: tusedregvars);
  1568. var
  1569. count: longint;
  1570. begin
  1571. for count := 1 to rv.intregvars.length do
  1572. cg.a_reg_sync(list,newreg(R_INTREGISTER,rv.intregvars.readidx(count-1),R_SUBWHOLE));
  1573. for count := 1 to rv.fpuregvars.length do
  1574. cg.a_reg_sync(list,newreg(R_FPUREGISTER,rv.fpuregvars.readidx(count-1),R_SUBWHOLE));
  1575. for count := 1 to rv.mmregvars.length do
  1576. cg.a_reg_sync(list,newreg(R_MMREGISTER,rv.mmregvars.readidx(count-1),R_SUBWHOLE));
  1577. end;
  1578. {*****************************************************************************
  1579. SSA support
  1580. *****************************************************************************}
  1581. type
  1582. preplaceregrec = ^treplaceregrec;
  1583. treplaceregrec = record
  1584. old, new: tregister;
  1585. oldhi, newhi: tregister;
  1586. ressym: tsym;
  1587. { moved sym }
  1588. sym : tabstractnormalvarsym;
  1589. end;
  1590. function doreplace(var n: tnode; para: pointer): foreachnoderesult;
  1591. var
  1592. rr: preplaceregrec absolute para;
  1593. begin
  1594. result := fen_false;
  1595. if (nf_is_funcret in n.flags) and (fc_exit in flowcontrol) then
  1596. exit;
  1597. case n.nodetype of
  1598. loadn:
  1599. begin
  1600. if (tloadnode(n).symtableentry.typ in [localvarsym,paravarsym,staticvarsym]) and
  1601. (tabstractvarsym(tloadnode(n).symtableentry).varoptions * [vo_is_dll_var, vo_is_thread_var] = []) and
  1602. not assigned(tloadnode(n).left) and
  1603. ((tloadnode(n).symtableentry <> rr^.ressym) or
  1604. not(fc_exit in flowcontrol)
  1605. ) and
  1606. (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) and
  1607. (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register = rr^.old) then
  1608. begin
  1609. {$ifdef cpu64bitalu}
  1610. { it's possible a 128 bit location was shifted and/xor typecasted }
  1611. { in a 64 bit value, so only 1 register was left in the location }
  1612. if (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.size in [OS_128,OS_S128]) then
  1613. if (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register128.reghi = rr^.oldhi) then
  1614. tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register128.reghi := rr^.newhi
  1615. else
  1616. exit;
  1617. {$else cpu64bitalu}
  1618. { it's possible a 64 bit location was shifted and/xor typecasted }
  1619. { in a 32 bit value, so only 1 register was left in the location }
  1620. if (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.size in [OS_64,OS_S64]) then
  1621. if (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register64.reghi = rr^.oldhi) then
  1622. tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register64.reghi := rr^.newhi
  1623. else
  1624. exit;
  1625. {$endif cpu64bitalu}
  1626. tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register := rr^.new;
  1627. rr^.sym := tabstractnormalvarsym(tloadnode(n).symtableentry);
  1628. result := fen_norecurse_true;
  1629. end;
  1630. end;
  1631. temprefn:
  1632. begin
  1633. if (ti_valid in ttemprefnode(n).tempinfo^.flags) and
  1634. (ttemprefnode(n).tempinfo^.location.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) and
  1635. (ttemprefnode(n).tempinfo^.location.register = rr^.old) then
  1636. begin
  1637. {$ifdef cpu64bitalu}
  1638. { it's possible a 128 bit location was shifted and/xor typecasted }
  1639. { in a 64 bit value, so only 1 register was left in the location }
  1640. if (ttemprefnode(n).tempinfo^.location.size in [OS_128,OS_S128]) then
  1641. if (ttemprefnode(n).tempinfo^.location.register128.reghi = rr^.oldhi) then
  1642. ttemprefnode(n).tempinfo^.location.register128.reghi := rr^.newhi
  1643. else
  1644. exit;
  1645. {$else cpu64bitalu}
  1646. { it's possible a 64 bit location was shifted and/xor typecasted }
  1647. { in a 32 bit value, so only 1 register was left in the location }
  1648. if (ttemprefnode(n).tempinfo^.location.size in [OS_64,OS_S64]) then
  1649. if (ttemprefnode(n).tempinfo^.location.register64.reghi = rr^.oldhi) then
  1650. ttemprefnode(n).tempinfo^.location.register64.reghi := rr^.newhi
  1651. else
  1652. exit;
  1653. {$endif cpu64bitalu}
  1654. ttemprefnode(n).tempinfo^.location.register := rr^.new;
  1655. result := fen_norecurse_true;
  1656. end;
  1657. end;
  1658. { optimize the searching a bit }
  1659. derefn,addrn,
  1660. calln,inlinen,casen,
  1661. addn,subn,muln,
  1662. andn,orn,xorn,
  1663. ltn,lten,gtn,gten,equaln,unequaln,
  1664. slashn,divn,shrn,shln,notn,
  1665. inn,
  1666. asn,isn:
  1667. result := fen_norecurse_false;
  1668. end;
  1669. end;
  1670. procedure maybechangeloadnodereg(list: TAsmList; var n: tnode; reload: boolean);
  1671. var
  1672. rr: treplaceregrec;
  1673. varloc : tai_varloc;
  1674. begin
  1675. {$ifdef jvm}
  1676. exit;
  1677. {$endif}
  1678. if not (n.location.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) or
  1679. ([fc_inflowcontrol,fc_gotolabel,fc_lefthandled] * flowcontrol <> []) then
  1680. exit;
  1681. rr.old := n.location.register;
  1682. rr.ressym := nil;
  1683. rr.sym := nil;
  1684. rr.oldhi := NR_NO;
  1685. case n.location.loc of
  1686. LOC_CREGISTER:
  1687. begin
  1688. {$ifdef cpu64bitalu}
  1689. if (n.location.size in [OS_128,OS_S128]) then
  1690. begin
  1691. rr.oldhi := n.location.register128.reghi;
  1692. rr.new := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  1693. rr.newhi := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  1694. end
  1695. else
  1696. {$else cpu64bitalu}
  1697. if (n.location.size in [OS_64,OS_S64]) then
  1698. begin
  1699. rr.oldhi := n.location.register64.reghi;
  1700. rr.new := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  1701. rr.newhi := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  1702. end
  1703. else
  1704. {$endif cpu64bitalu}
  1705. rr.new := cg.getintregister(current_asmdata.CurrAsmList,n.location.size);
  1706. end;
  1707. LOC_CFPUREGISTER:
  1708. rr.new := cg.getfpuregister(current_asmdata.CurrAsmList,n.location.size);
  1709. {$ifdef SUPPORT_MMX}
  1710. LOC_CMMXREGISTER:
  1711. rr.new := tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  1712. {$endif SUPPORT_MMX}
  1713. LOC_CMMREGISTER:
  1714. rr.new := cg.getmmregister(current_asmdata.CurrAsmList,n.location.size);
  1715. else
  1716. exit;
  1717. end;
  1718. if not is_void(current_procinfo.procdef.returndef) and
  1719. assigned(current_procinfo.procdef.funcretsym) and
  1720. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs <> 0) then
  1721. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  1722. rr.ressym:=tsym(current_procinfo.procdef.parast.Find('self'))
  1723. else
  1724. rr.ressym:=current_procinfo.procdef.funcretsym;
  1725. if not foreachnodestatic(n,@doreplace,@rr) then
  1726. exit;
  1727. if reload then
  1728. case n.location.loc of
  1729. LOC_CREGISTER:
  1730. begin
  1731. {$ifdef cpu64bitalu}
  1732. if (n.location.size in [OS_128,OS_S128]) then
  1733. cg128.a_load128_reg_reg(list,n.location.register128,joinreg128(rr.new,rr.newhi))
  1734. else
  1735. {$else cpu64bitalu}
  1736. if (n.location.size in [OS_64,OS_S64]) then
  1737. cg64.a_load64_reg_reg(list,n.location.register64,joinreg64(rr.new,rr.newhi))
  1738. else
  1739. {$endif cpu64bitalu}
  1740. cg.a_load_reg_reg(list,n.location.size,n.location.size,n.location.register,rr.new);
  1741. end;
  1742. LOC_CFPUREGISTER:
  1743. cg.a_loadfpu_reg_reg(list,n.location.size,n.location.size,n.location.register,rr.new);
  1744. {$ifdef SUPPORT_MMX}
  1745. LOC_CMMXREGISTER:
  1746. cg.a_loadmm_reg_reg(list,OS_M64,OS_M64,n.location.register,rr.new,nil);
  1747. {$endif SUPPORT_MMX}
  1748. LOC_CMMREGISTER:
  1749. cg.a_loadmm_reg_reg(list,n.location.size,n.location.size,n.location.register,rr.new,nil);
  1750. else
  1751. internalerror(2006090920);
  1752. end;
  1753. { now that we've change the loadn/temp, also change the node result location }
  1754. {$ifdef cpu64bitalu}
  1755. if (n.location.size in [OS_128,OS_S128]) then
  1756. begin
  1757. n.location.register128.reglo := rr.new;
  1758. n.location.register128.reghi := rr.newhi;
  1759. if assigned(rr.sym) and
  1760. ((rr.sym.currentregloc.register<>rr.new) or
  1761. (rr.sym.currentregloc.registerhi<>rr.newhi)) then
  1762. begin
  1763. varloc:=tai_varloc.create128(rr.sym,rr.new,rr.newhi);
  1764. varloc.oldlocation:=rr.sym.currentregloc.register;
  1765. varloc.oldlocationhi:=rr.sym.currentregloc.registerhi;
  1766. rr.sym.currentregloc.register:=rr.new;
  1767. rr.sym.currentregloc.registerHI:=rr.newhi;
  1768. list.concat(varloc);
  1769. end;
  1770. end
  1771. else
  1772. {$else cpu64bitalu}
  1773. if (n.location.size in [OS_64,OS_S64]) then
  1774. begin
  1775. n.location.register64.reglo := rr.new;
  1776. n.location.register64.reghi := rr.newhi;
  1777. if assigned(rr.sym) and
  1778. ((rr.sym.currentregloc.register<>rr.new) or
  1779. (rr.sym.currentregloc.registerhi<>rr.newhi)) then
  1780. begin
  1781. varloc:=tai_varloc.create64(rr.sym,rr.new,rr.newhi);
  1782. varloc.oldlocation:=rr.sym.currentregloc.register;
  1783. varloc.oldlocationhi:=rr.sym.currentregloc.registerhi;
  1784. rr.sym.currentregloc.register:=rr.new;
  1785. rr.sym.currentregloc.registerHI:=rr.newhi;
  1786. list.concat(varloc);
  1787. end;
  1788. end
  1789. else
  1790. {$endif cpu64bitalu}
  1791. begin
  1792. n.location.register := rr.new;
  1793. if assigned(rr.sym) and (rr.sym.currentregloc.register<>rr.new) then
  1794. begin
  1795. varloc:=tai_varloc.create(rr.sym,rr.new);
  1796. varloc.oldlocation:=rr.sym.currentregloc.register;
  1797. rr.sym.currentregloc.register:=rr.new;
  1798. list.concat(varloc);
  1799. end;
  1800. end;
  1801. end;
  1802. procedure gen_free_symtable(list:TAsmList;st:TSymtable);
  1803. var
  1804. i : longint;
  1805. sym : tsym;
  1806. begin
  1807. for i:=0 to st.SymList.Count-1 do
  1808. begin
  1809. sym:=tsym(st.SymList[i]);
  1810. if (sym.typ in [staticvarsym,localvarsym,paravarsym]) then
  1811. begin
  1812. with tabstractnormalvarsym(sym) do
  1813. begin
  1814. { Note: We need to keep the data available in memory
  1815. for the sub procedures that can access local data
  1816. in the parent procedures }
  1817. case localloc.loc of
  1818. LOC_CREGISTER :
  1819. if (pi_has_label in current_procinfo.flags) then
  1820. {$ifdef cpu64bitalu}
  1821. if def_cgsize(vardef) in [OS_128,OS_S128] then
  1822. begin
  1823. cg.a_reg_sync(list,localloc.register128.reglo);
  1824. cg.a_reg_sync(list,localloc.register128.reghi);
  1825. end
  1826. else
  1827. {$else cpu64bitalu}
  1828. if def_cgsize(vardef) in [OS_64,OS_S64] then
  1829. begin
  1830. cg.a_reg_sync(list,localloc.register64.reglo);
  1831. cg.a_reg_sync(list,localloc.register64.reghi);
  1832. end
  1833. else
  1834. {$endif cpu64bitalu}
  1835. cg.a_reg_sync(list,localloc.register);
  1836. LOC_CFPUREGISTER,
  1837. LOC_CMMREGISTER:
  1838. if (pi_has_label in current_procinfo.flags) then
  1839. cg.a_reg_sync(list,localloc.register);
  1840. LOC_REFERENCE :
  1841. begin
  1842. if typ in [localvarsym,paravarsym] then
  1843. tg.Ungetlocal(list,localloc.reference);
  1844. end;
  1845. end;
  1846. end;
  1847. end;
  1848. end;
  1849. end;
  1850. procedure gen_intf_wrapper(list:TAsmList;_class:tobjectdef);
  1851. var
  1852. i,j : longint;
  1853. tmps : string;
  1854. pd : TProcdef;
  1855. ImplIntf : TImplementedInterface;
  1856. begin
  1857. for i:=0 to _class.ImplementedInterfaces.count-1 do
  1858. begin
  1859. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  1860. if (ImplIntf=ImplIntf.VtblImplIntf) and
  1861. assigned(ImplIntf.ProcDefs) then
  1862. begin
  1863. maybe_new_object_file(list);
  1864. for j:=0 to ImplIntf.ProcDefs.Count-1 do
  1865. begin
  1866. pd:=TProcdef(ImplIntf.ProcDefs[j]);
  1867. { we don't track method calls via interfaces yet ->
  1868. assume that every method called via an interface call
  1869. is reachable for now }
  1870. if (po_virtualmethod in pd.procoptions) and
  1871. not is_objectpascal_helper(tprocdef(pd).struct) then
  1872. tobjectdef(tprocdef(pd).struct).register_vmt_call(tprocdef(pd).extnumber);
  1873. tmps:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+
  1874. ImplIntf.IntfDef.objname^+'_$_'+tostr(j)+'_$_'+pd.mangledname);
  1875. { create wrapper code }
  1876. new_section(list,sec_code,tmps,0);
  1877. hlcg.init_register_allocators;
  1878. cg.g_intf_wrapper(list,pd,tmps,ImplIntf.ioffset);
  1879. hlcg.done_register_allocators;
  1880. end;
  1881. end;
  1882. end;
  1883. end;
  1884. procedure gen_intf_wrappers(list:TAsmList;st:TSymtable;nested:boolean);
  1885. var
  1886. i : longint;
  1887. def : tdef;
  1888. begin
  1889. if not nested then
  1890. create_hlcodegen;
  1891. for i:=0 to st.DefList.Count-1 do
  1892. begin
  1893. def:=tdef(st.DefList[i]);
  1894. { if def can contain nested types then handle it symtable }
  1895. if def.typ in [objectdef,recorddef] then
  1896. gen_intf_wrappers(list,tabstractrecorddef(def).symtable,true);
  1897. if is_class(def) then
  1898. gen_intf_wrapper(list,tobjectdef(def));
  1899. end;
  1900. if not nested then
  1901. destroy_hlcodegen;
  1902. end;
  1903. procedure gen_load_vmt_register(list:TAsmList;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  1904. var
  1905. href : treference;
  1906. selfdef: tdef;
  1907. begin
  1908. if is_object(objdef) then
  1909. begin
  1910. case selfloc.loc of
  1911. LOC_CREFERENCE,
  1912. LOC_REFERENCE:
  1913. begin
  1914. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset,sizeof(pint));
  1915. cg.a_loadaddr_ref_reg(list,selfloc.reference,href.base);
  1916. selfdef:=getpointerdef(objdef);
  1917. end;
  1918. else
  1919. internalerror(200305056);
  1920. end;
  1921. end
  1922. else
  1923. { This is also valid for Objective-C classes: vmt_offset is 0 there,
  1924. and the first "field" of an Objective-C class instance is a pointer
  1925. to its "meta-class". }
  1926. begin
  1927. selfdef:=objdef;
  1928. case selfloc.loc of
  1929. LOC_REGISTER:
  1930. begin
  1931. {$ifdef cpu_uses_separate_address_registers}
  1932. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  1933. begin
  1934. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset,sizeof(pint));
  1935. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,selfloc.register,href.base);
  1936. end
  1937. else
  1938. {$endif cpu_uses_separate_address_registers}
  1939. reference_reset_base(href,selfloc.register,objdef.vmt_offset,sizeof(pint));
  1940. end;
  1941. LOC_CONSTANT,
  1942. LOC_CREGISTER,
  1943. LOC_CREFERENCE,
  1944. LOC_REFERENCE,
  1945. LOC_CSUBSETREG,
  1946. LOC_SUBSETREG,
  1947. LOC_CSUBSETREF,
  1948. LOC_SUBSETREF:
  1949. begin
  1950. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset,sizeof(pint));
  1951. { todo: pass actual vmt pointer type to hlcg }
  1952. hlcg.a_load_loc_reg(list,voidpointertype,voidpointertype,selfloc,href.base);
  1953. end;
  1954. else
  1955. internalerror(200305057);
  1956. end;
  1957. end;
  1958. vmtreg:=cg.getaddressregister(list);
  1959. hlcg.g_maybe_testself(list,selfdef,href.base);
  1960. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,vmtreg);
  1961. { test validity of VMT }
  1962. if not(is_interface(objdef)) and
  1963. not(is_cppclass(objdef)) and
  1964. not(is_objc_class_or_protocol(objdef)) then
  1965. cg.g_maybe_testvmt(list,vmtreg,objdef);
  1966. end;
  1967. function getprocalign : shortint;
  1968. begin
  1969. { gprof uses 16 byte granularity }
  1970. if (cs_profile in current_settings.moduleswitches) then
  1971. result:=16
  1972. else
  1973. result:=current_settings.alignment.procalign;
  1974. end;
  1975. procedure gen_fpc_dummy(list : TAsmList);
  1976. begin
  1977. {$ifdef i386}
  1978. { fix me! }
  1979. list.concat(Taicpu.Op_const_reg(A_MOV,S_L,1,NR_EAX));
  1980. list.concat(Taicpu.Op_const(A_RET,S_W,12));
  1981. {$endif i386}
  1982. end;
  1983. procedure InsertInterruptTable;
  1984. procedure WriteVector(const name: string);
  1985. {$IFDEF arm}
  1986. var
  1987. ai: taicpu;
  1988. {$ENDIF arm}
  1989. begin
  1990. {$IFDEF arm}
  1991. if current_settings.cputype in [cpu_armv7m] then
  1992. current_asmdata.asmlists[al_globals].concat(tai_const.Createname(name,0))
  1993. else
  1994. begin
  1995. ai:=taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(name));
  1996. ai.is_jmp:=true;
  1997. current_asmdata.asmlists[al_globals].concat(ai);
  1998. end;
  1999. {$ENDIF arm}
  2000. end;
  2001. function GetInterruptTableLength: longint;
  2002. begin
  2003. {$if defined(ARM)}
  2004. result:=embedded_controllers[current_settings.controllertype].interruptvectors;
  2005. {$else}
  2006. result:=0;
  2007. {$endif}
  2008. end;
  2009. var
  2010. hp: tused_unit;
  2011. sym: tsym;
  2012. i, i2: longint;
  2013. interruptTable: array of tprocdef;
  2014. pd: tprocdef;
  2015. begin
  2016. SetLength(interruptTable, GetInterruptTableLength);
  2017. FillChar(interruptTable[0], length(interruptTable)*sizeof(pointer), 0);
  2018. hp:=tused_unit(usedunits.first);
  2019. while assigned(hp) do
  2020. begin
  2021. for i := 0 to hp.u.symlist.Count-1 do
  2022. begin
  2023. sym:=tsym(hp.u.symlist[i]);
  2024. if not assigned(sym) then
  2025. continue;
  2026. if sym.typ = procsym then
  2027. begin
  2028. for i2 := 0 to tprocsym(sym).ProcdefList.Count-1 do
  2029. begin
  2030. pd:=tprocdef(tprocsym(sym).ProcdefList[i2]);
  2031. if pd.interruptvector >= 0 then
  2032. begin
  2033. if pd.interruptvector > high(interruptTable) then
  2034. Internalerror(2011030602);
  2035. if interruptTable[pd.interruptvector] <> nil then
  2036. internalerror(2011030601);
  2037. interruptTable[pd.interruptvector]:=pd;
  2038. break;
  2039. end;
  2040. end;
  2041. end;
  2042. end;
  2043. hp:=tused_unit(hp.next);
  2044. end;
  2045. new_section(current_asmdata.asmlists[al_globals],sec_init,'VECTORS',sizeof(pint));
  2046. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('VECTORS',AT_DATA,0));
  2047. {$IFDEF arm}
  2048. if current_settings.cputype in [cpu_armv7m] then
  2049. current_asmdata.asmlists[al_globals].concat(tai_const.Createname('_stack_top',0)); { ARMv7-M processors have the initial stack value at address 0 }
  2050. {$ENDIF arm}
  2051. for i:=0 to high(interruptTable) do
  2052. begin
  2053. if interruptTable[i]<>nil then
  2054. writeVector(interruptTable[i].mangledname)
  2055. else
  2056. writeVector('DefaultHandler'); { Default handler name }
  2057. end;
  2058. end;
  2059. end.