ncgutil.pas 89 KB

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