ncgutil.pas 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347
  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,aasmcpu,
  25. symconst,symbase,symdef,symsym,symtype,symtable
  26. {$ifndef cpu64bit}
  27. ,cg64f32
  28. {$endif cpu64bit}
  29. ;
  30. type
  31. tloadregvars = (lr_dont_load_regvars, lr_load_regvars);
  32. procedure firstcomplex(p : tbinarynode);
  33. procedure maketojumpbool(list:TAAsmoutput; p : tnode; loadregvars: tloadregvars);
  34. // procedure remove_non_regvars_from_loc(const t: tlocation; var regs:Tsuperregisterset);
  35. procedure location_force_reg(list:TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  36. procedure location_force_fpureg(list:TAAsmoutput;var l: tlocation;maybeconst:boolean);
  37. procedure location_force_mem(list:TAAsmoutput;var l:tlocation);
  38. procedure location_force_mmregscalar(list:TAAsmoutput;var l: tlocation;maybeconst:boolean);
  39. { Retrieve the location of the data pointed to in location l, when the location is
  40. a register it is expected to contain the address of the data }
  41. procedure location_get_data_ref(list:TAAsmoutput;const l:tlocation;var ref:treference;loadref:boolean);
  42. function maybe_pushfpu(list:taasmoutput;needed : byte;var l:tlocation) : boolean;
  43. procedure gen_proc_symbol(list:Taasmoutput);
  44. procedure gen_proc_symbol_end(list:Taasmoutput);
  45. procedure gen_proc_entry_code(list:Taasmoutput);
  46. procedure gen_proc_exit_code(list:Taasmoutput);
  47. procedure gen_stack_check_size_para(list:Taasmoutput);
  48. procedure gen_stack_check_call(list:Taasmoutput);
  49. procedure gen_save_used_regs(list:TAAsmoutput);
  50. procedure gen_restore_used_regs(list:TAAsmoutput);
  51. procedure gen_initialize_code(list:TAAsmoutput);
  52. procedure gen_finalize_code(list:TAAsmoutput);
  53. procedure gen_entry_code(list:TAAsmoutput);
  54. procedure gen_exit_code(list:TAAsmoutput);
  55. procedure gen_load_para_value(list:TAAsmoutput);
  56. procedure gen_load_return_value(list:TAAsmoutput);
  57. procedure gen_external_stub(list:taasmoutput;pd:tprocdef;const externalname:string);
  58. procedure gen_intf_wrappers(list:taasmoutput;st:tsymtable);
  59. {#
  60. Allocate the buffers for exception management and setjmp environment.
  61. Return a pointer to these buffers, send them to the utility routine
  62. so they are registered, and then call setjmp.
  63. Then compare the result of setjmp with 0, and if not equal
  64. to zero, then jump to exceptlabel.
  65. Also store the result of setjmp to a temporary space by calling g_save_exception_reason
  66. It is to note that this routine may be called *after* the stackframe of a
  67. routine has been called, therefore on machines where the stack cannot
  68. be modified, all temps should be allocated on the heap instead of the
  69. stack.
  70. }
  71. const
  72. EXCEPT_BUF_SIZE = 3*sizeof(aint);
  73. type
  74. texceptiontemps=record
  75. jmpbuf,
  76. envbuf,
  77. reasonbuf : treference;
  78. end;
  79. procedure get_exception_temps(list:taasmoutput;var t:texceptiontemps);
  80. procedure unget_exception_temps(list:taasmoutput;const t:texceptiontemps);
  81. procedure new_exception(list:TAAsmoutput;const t:texceptiontemps;exceptlabel:tasmlabel);
  82. procedure free_exception(list:TAAsmoutput;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  83. procedure insertbssdata(sym : tglobalvarsym);
  84. procedure gen_alloc_symtable(list:TAAsmoutput;st:tsymtable);
  85. procedure gen_free_symtable(list:TAAsmoutput;st:tsymtable);
  86. {$ifdef PASS2INLINE}
  87. procedure gen_alloc_inline_parast(list:TAAsmoutput;pd:tprocdef);
  88. procedure gen_alloc_inline_funcret(list:TAAsmoutput;pd:tprocdef);
  89. {$endif PASS2INLINE}
  90. { rtti and init/final }
  91. procedure generate_rtti(p:Ttypesym);
  92. procedure generate_inittable(p:tsym);
  93. procedure location_free(list: taasmoutput; const location : TLocation);
  94. implementation
  95. uses
  96. strings,
  97. cutils,cclasses,
  98. globals,systems,verbose,
  99. ppu,defutil,
  100. procinfo,paramgr,fmodule,
  101. regvars,dwarf,
  102. {$ifdef GDB}
  103. gdb,
  104. {$endif GDB}
  105. pass_1,pass_2,
  106. ncon,nld,nutils,
  107. tgobj,cgobj;
  108. {*****************************************************************************
  109. Misc Helpers
  110. *****************************************************************************}
  111. procedure location_free(list: taasmoutput; const location : TLocation);
  112. begin
  113. case location.loc of
  114. LOC_VOID:
  115. ;
  116. LOC_REGISTER,
  117. LOC_CREGISTER:
  118. begin
  119. if getsupreg(location.register)<first_int_imreg then
  120. cg.ungetcpuregister(list,location.register);
  121. end;
  122. LOC_FPUREGISTER,
  123. LOC_CFPUREGISTER:
  124. begin
  125. if getsupreg(location.register)<first_fpu_imreg then
  126. cg.ungetcpuregister(list,location.register);
  127. end;
  128. LOC_MMREGISTER,
  129. LOC_CMMREGISTER :
  130. begin
  131. if getsupreg(location.register)<first_mm_imreg then
  132. cg.ungetcpuregister(list,location.register);
  133. end;
  134. LOC_REFERENCE,
  135. LOC_CREFERENCE :
  136. begin
  137. {$ifdef cputargethasfixedstack}
  138. location_freetemp(list,location);
  139. {$endif cputargethasfixedstack}
  140. end;
  141. else
  142. internalerror(2004110211);
  143. end;
  144. end;
  145. { DO NOT RELY on the fact that the tnode is not yet swaped
  146. because of inlining code PM }
  147. procedure firstcomplex(p : tbinarynode);
  148. var
  149. hp : tnode;
  150. begin
  151. { always calculate boolean AND and OR from left to right }
  152. if (p.nodetype in [orn,andn]) and
  153. is_boolean(p.left.resulttype.def) then
  154. begin
  155. if nf_swaped in p.flags then
  156. internalerror(234234);
  157. end
  158. else
  159. if (
  160. (p.expectloc=LOC_FPUREGISTER) and
  161. (p.right.registersfpu > p.left.registersfpu)
  162. ) or
  163. (
  164. (
  165. (
  166. ((p.left.registersfpu = 0) and (p.right.registersfpu = 0)) or
  167. (p.expectloc<>LOC_FPUREGISTER)
  168. ) and
  169. (p.left.registersint<p.right.registersint)
  170. )
  171. ) then
  172. begin
  173. hp:=p.left;
  174. p.left:=p.right;
  175. p.right:=hp;
  176. if nf_swaped in p.flags then
  177. exclude(p.flags,nf_swaped)
  178. else
  179. include(p.flags,nf_swaped);
  180. end;
  181. end;
  182. procedure maketojumpbool(list:TAAsmoutput; p : tnode; loadregvars: tloadregvars);
  183. {
  184. produces jumps to true respectively false labels using boolean expressions
  185. depending on whether the loading of regvars is currently being
  186. synchronized manually (such as in an if-node) or automatically (most of
  187. the other cases where this procedure is called), loadregvars can be
  188. "lr_load_regvars" or "lr_dont_load_regvars"
  189. }
  190. var
  191. opsize : tcgsize;
  192. storepos : tfileposinfo;
  193. begin
  194. if nf_error in p.flags then
  195. exit;
  196. storepos:=aktfilepos;
  197. aktfilepos:=p.fileinfo;
  198. if is_boolean(p.resulttype.def) then
  199. begin
  200. {$ifdef OLDREGVARS}
  201. if loadregvars = lr_load_regvars then
  202. load_all_regvars(list);
  203. {$endif OLDREGVARS}
  204. if is_constboolnode(p) then
  205. begin
  206. if tordconstnode(p).value<>0 then
  207. cg.a_jmp_always(list,truelabel)
  208. else
  209. cg.a_jmp_always(list,falselabel)
  210. end
  211. else
  212. begin
  213. opsize:=def_cgsize(p.resulttype.def);
  214. case p.location.loc of
  215. LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE :
  216. begin
  217. {$ifdef OLDREGVARS}
  218. if (p.location.loc = LOC_CREGISTER) then
  219. load_regvar_reg(list,p.location.register);
  220. {$endif OLDREGVARS}
  221. cg.a_cmp_const_loc_label(list,opsize,OC_NE,0,p.location,truelabel);
  222. cg.a_jmp_always(list,falselabel);
  223. end;
  224. LOC_JUMP:
  225. ;
  226. {$ifdef cpuflags}
  227. LOC_FLAGS :
  228. begin
  229. cg.a_jmp_flags(list,p.location.resflags,truelabel);
  230. cg.a_jmp_always(list,falselabel);
  231. end;
  232. {$endif cpuflags}
  233. else
  234. begin
  235. printnode(output,p);
  236. internalerror(200308241);
  237. end;
  238. end;
  239. end;
  240. end
  241. else
  242. internalerror(200112305);
  243. aktfilepos:=storepos;
  244. end;
  245. (*
  246. This code needs fixing. It is not safe to use rgint; on the m68000 it
  247. would be rgaddr.
  248. procedure remove_non_regvars_from_loc(const t: tlocation; var regs:Tsuperregisterset);
  249. begin
  250. case t.loc of
  251. LOC_REGISTER:
  252. begin
  253. { can't be a regvar, since it would be LOC_CREGISTER then }
  254. exclude(regs,getsupreg(t.register));
  255. if t.register64.reghi<>NR_NO then
  256. exclude(regs,getsupreg(t.register64.reghi));
  257. end;
  258. LOC_CREFERENCE,LOC_REFERENCE:
  259. begin
  260. if not(cs_regvars in aktglobalswitches) or
  261. (getsupreg(t.reference.base) in cg.rgint.usableregs) then
  262. exclude(regs,getsupreg(t.reference.base));
  263. if not(cs_regvars in aktglobalswitches) or
  264. (getsupreg(t.reference.index) in cg.rgint.usableregs) then
  265. exclude(regs,getsupreg(t.reference.index));
  266. end;
  267. end;
  268. end;
  269. *)
  270. {*****************************************************************************
  271. EXCEPTION MANAGEMENT
  272. *****************************************************************************}
  273. procedure get_exception_temps(list:taasmoutput;var t:texceptiontemps);
  274. var
  275. sym : ttypesym;
  276. begin
  277. if jmp_buf_size=-1 then
  278. begin
  279. searchsystype('JMP_BUF',sym);
  280. jmp_buf_size:=sym.restype.def.size;
  281. end;
  282. tg.GetTemp(list,EXCEPT_BUF_SIZE,tt_persistent,t.envbuf);
  283. tg.GetTemp(list,jmp_buf_size,tt_persistent,t.jmpbuf);
  284. tg.GetTemp(list,sizeof(aint),tt_persistent,t.reasonbuf);
  285. end;
  286. procedure unget_exception_temps(list:taasmoutput;const t:texceptiontemps);
  287. begin
  288. tg.Ungettemp(list,t.jmpbuf);
  289. tg.ungettemp(list,t.envbuf);
  290. tg.ungettemp(list,t.reasonbuf);
  291. end;
  292. procedure new_exception(list:TAAsmoutput;const t:texceptiontemps;exceptlabel:tasmlabel);
  293. var
  294. paraloc1,paraloc2,paraloc3 : tcgpara;
  295. begin
  296. paraloc1.init;
  297. paraloc2.init;
  298. paraloc3.init;
  299. paramanager.getintparaloc(pocall_default,1,paraloc1);
  300. paramanager.getintparaloc(pocall_default,2,paraloc2);
  301. paramanager.getintparaloc(pocall_default,3,paraloc3);
  302. paramanager.allocparaloc(list,paraloc3);
  303. cg.a_paramaddr_ref(list,t.envbuf,paraloc3);
  304. paramanager.allocparaloc(list,paraloc2);
  305. cg.a_paramaddr_ref(list,t.jmpbuf,paraloc2);
  306. { push type of exceptionframe }
  307. paramanager.allocparaloc(list,paraloc1);
  308. cg.a_param_const(list,OS_S32,1,paraloc1);
  309. paramanager.freeparaloc(list,paraloc3);
  310. paramanager.freeparaloc(list,paraloc2);
  311. paramanager.freeparaloc(list,paraloc1);
  312. cg.alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  313. cg.a_call_name(list,'FPC_PUSHEXCEPTADDR');
  314. cg.dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  315. paramanager.getintparaloc(pocall_default,1,paraloc1);
  316. paramanager.allocparaloc(list,paraloc1);
  317. cg.a_param_reg(list,OS_ADDR,NR_FUNCTION_RESULT_REG,paraloc1);
  318. paramanager.freeparaloc(list,paraloc1);
  319. cg.alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  320. cg.a_call_name(list,'FPC_SETJMP');
  321. cg.dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  322. cg.g_exception_reason_save(list, t.reasonbuf);
  323. cg.a_cmp_const_reg_label(list,OS_S32,OC_NE,0,cg.makeregsize(list,NR_FUNCTION_RESULT_REG,OS_S32),exceptlabel);
  324. paraloc1.done;
  325. paraloc2.done;
  326. paraloc3.done;
  327. end;
  328. procedure free_exception(list:TAAsmoutput;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  329. begin
  330. cg.alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  331. cg.a_call_name(list,'FPC_POPADDRSTACK');
  332. cg.dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  333. if not onlyfree then
  334. begin
  335. cg.g_exception_reason_load(list, t.reasonbuf);
  336. cg.a_cmp_const_reg_label(list,OS_INT,OC_EQ,a,NR_FUNCTION_RESULT_REG,endexceptlabel);
  337. end;
  338. end;
  339. {*****************************************************************************
  340. TLocation
  341. *****************************************************************************}
  342. {$ifndef cpu64bit}
  343. { 32-bit version }
  344. procedure location_force_reg(list:TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  345. var
  346. hregister,
  347. hregisterhi : tregister;
  348. hreg64 : tregister64;
  349. hl : tasmlabel;
  350. oldloc : tlocation;
  351. const_location: boolean;
  352. begin
  353. oldloc:=l;
  354. if dst_size=OS_NO then
  355. internalerror(200309144);
  356. { handle transformations to 64bit separate }
  357. if dst_size in [OS_64,OS_S64] then
  358. begin
  359. if not (l.size in [OS_64,OS_S64]) then
  360. begin
  361. { load a smaller size to OS_64 }
  362. if l.loc=LOC_REGISTER then
  363. begin
  364. hregister:=cg.makeregsize(list,l.register64.reglo,OS_32);
  365. cg.a_load_reg_reg(list,l.size,OS_32,l.register64.reglo,hregister);
  366. end
  367. else
  368. hregister:=cg.getintregister(list,OS_INT);
  369. { load value in low register }
  370. case l.loc of
  371. LOC_FLAGS :
  372. cg.g_flags2reg(list,OS_INT,l.resflags,hregister);
  373. LOC_JUMP :
  374. begin
  375. cg.a_label(list,truelabel);
  376. cg.a_load_const_reg(list,OS_INT,1,hregister);
  377. objectlibrary.getlabel(hl);
  378. cg.a_jmp_always(list,hl);
  379. cg.a_label(list,falselabel);
  380. cg.a_load_const_reg(list,OS_INT,0,hregister);
  381. cg.a_label(list,hl);
  382. end;
  383. else
  384. cg.a_load_loc_reg(list,OS_INT,l,hregister);
  385. end;
  386. { reset hi part, take care of the signed bit of the current value }
  387. hregisterhi:=cg.getintregister(list,OS_INT);
  388. if (l.size in [OS_S8,OS_S16,OS_S32]) then
  389. begin
  390. if l.loc=LOC_CONSTANT then
  391. begin
  392. if (longint(l.value)<0) then
  393. cg.a_load_const_reg(list,OS_32,aint($ffffffff),hregisterhi)
  394. else
  395. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  396. end
  397. else
  398. begin
  399. cg.a_op_const_reg_reg(list,OP_SAR,OS_32,31,hregister,
  400. hregisterhi);
  401. end;
  402. end
  403. else
  404. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  405. location_reset(l,LOC_REGISTER,dst_size);
  406. l.register64.reglo:=hregister;
  407. l.register64.reghi:=hregisterhi;
  408. end
  409. else
  410. begin
  411. { 64bit to 64bit }
  412. if ((l.loc=LOC_CREGISTER) and maybeconst) then
  413. begin
  414. hregister:=l.register64.reglo;
  415. hregisterhi:=l.register64.reghi;
  416. const_location := true;
  417. end
  418. else
  419. begin
  420. hregister:=cg.getintregister(list,OS_INT);
  421. hregisterhi:=cg.getintregister(list,OS_INT);
  422. const_location := false;
  423. end;
  424. hreg64.reglo:=hregister;
  425. hreg64.reghi:=hregisterhi;
  426. { load value in new register }
  427. cg64.a_load64_loc_reg(list,l,hreg64);
  428. if not const_location then
  429. location_reset(l,LOC_REGISTER,dst_size)
  430. else
  431. location_reset(l,LOC_CREGISTER,dst_size);
  432. l.register64.reglo:=hregister;
  433. l.register64.reghi:=hregisterhi;
  434. end;
  435. end
  436. else
  437. begin
  438. {Do not bother to recycle the existing register. The register
  439. allocator eliminates unnecessary moves, so it's not needed
  440. and trying to recycle registers can cause problems because
  441. the registers changes size and may need aditional constraints.
  442. Not if it's about LOC_CREGISTER's (JM)
  443. }
  444. const_location :=
  445. (maybeconst) and
  446. (l.loc = LOC_CREGISTER) and
  447. (TCGSize2Size[l.size] = TCGSize2Size[dst_size]) and
  448. ((l.size = dst_size) or
  449. (TCGSize2Size[l.size] = TCGSize2Size[OS_INT]));
  450. if not const_location then
  451. hregister:=cg.getintregister(list,dst_size)
  452. else
  453. hregister := l.register;
  454. { load value in new register }
  455. case l.loc of
  456. LOC_FLAGS :
  457. cg.g_flags2reg(list,dst_size,l.resflags,hregister);
  458. LOC_JUMP :
  459. begin
  460. cg.a_label(list,truelabel);
  461. cg.a_load_const_reg(list,dst_size,1,hregister);
  462. objectlibrary.getlabel(hl);
  463. cg.a_jmp_always(list,hl);
  464. cg.a_label(list,falselabel);
  465. cg.a_load_const_reg(list,dst_size,0,hregister);
  466. cg.a_label(list,hl);
  467. end;
  468. else
  469. begin
  470. { load_loc_reg can only handle size >= l.size, when the
  471. new size is smaller then we need to adjust the size
  472. of the orignal and maybe recalculate l.register for i386 }
  473. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  474. begin
  475. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  476. l.register:=cg.makeregsize(list,l.register,dst_size);
  477. { for big endian systems, the reference's offset must }
  478. { be increased in this case, since they have the }
  479. { MSB first in memory and e.g. byte(word_var) should }
  480. { return the second byte in this case (JM) }
  481. if (target_info.endian = ENDIAN_BIG) and
  482. (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  483. inc(l.reference.offset,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  484. {$ifdef x86}
  485. l.size:=dst_size;
  486. {$endif x86}
  487. end;
  488. cg.a_load_loc_reg(list,dst_size,l,hregister);
  489. {$ifndef x86}
  490. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  491. l.size:=dst_size;
  492. {$endif not x86}
  493. end;
  494. end;
  495. if not const_location then
  496. location_reset(l,LOC_REGISTER,dst_size)
  497. else
  498. location_reset(l,LOC_CREGISTER,dst_size);
  499. l.register:=hregister;
  500. end;
  501. { Release temp when it was a reference }
  502. if oldloc.loc=LOC_REFERENCE then
  503. location_freetemp(list,oldloc);
  504. end;
  505. {$else cpu64bit}
  506. { 64-bit version }
  507. procedure location_force_reg(list:TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  508. var
  509. hregister : tregister;
  510. hl : tasmlabel;
  511. oldloc : tlocation;
  512. begin
  513. oldloc:=l;
  514. hregister:=cg.getintregister(list,dst_size);
  515. { load value in new register }
  516. case l.loc of
  517. LOC_FLAGS :
  518. cg.g_flags2reg(list,dst_size,l.resflags,hregister);
  519. LOC_JUMP :
  520. begin
  521. cg.a_label(list,truelabel);
  522. cg.a_load_const_reg(list,dst_size,1,hregister);
  523. objectlibrary.getlabel(hl);
  524. cg.a_jmp_always(list,hl);
  525. cg.a_label(list,falselabel);
  526. cg.a_load_const_reg(list,dst_size,0,hregister);
  527. cg.a_label(list,hl);
  528. end;
  529. else
  530. begin
  531. { load_loc_reg can only handle size >= l.size, when the
  532. new size is smaller then we need to adjust the size
  533. of the orignal and maybe recalculate l.register for i386 }
  534. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  535. begin
  536. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  537. l.register:=cg.makeregsize(list,l.register,dst_size);
  538. { for big endian systems, the reference's offset must }
  539. { be increased in this case, since they have the }
  540. { MSB first in memory and e.g. byte(word_var) should }
  541. { return the second byte in this case (JM) }
  542. if (target_info.endian = ENDIAN_BIG) and
  543. (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  544. inc(l.reference.offset,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  545. {$ifdef x86}
  546. l.size:=dst_size;
  547. {$endif x86}
  548. end;
  549. cg.a_load_loc_reg(list,dst_size,l,hregister);
  550. {$ifndef x86}
  551. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  552. l.size:=dst_size;
  553. {$endif not x86}
  554. end;
  555. end;
  556. if (l.loc <> LOC_CREGISTER) or
  557. not maybeconst then
  558. location_reset(l,LOC_REGISTER,dst_size)
  559. else
  560. location_reset(l,LOC_CREGISTER,dst_size);
  561. l.register:=hregister;
  562. { Release temp when it was a reference }
  563. if oldloc.loc=LOC_REFERENCE then
  564. location_freetemp(list,oldloc);
  565. end;
  566. {$endif cpu64bit}
  567. procedure location_force_fpureg(list:TAAsmoutput;var l: tlocation;maybeconst:boolean);
  568. var
  569. reg : tregister;
  570. href : treference;
  571. begin
  572. if (l.loc<>LOC_FPUREGISTER) and
  573. ((l.loc<>LOC_CFPUREGISTER) or (not maybeconst)) then
  574. begin
  575. { if it's in an mm register, store to memory first }
  576. if (l.loc in [LOC_MMREGISTER,LOC_CMMREGISTER]) then
  577. begin
  578. tg.GetTemp(list,tcgsize2size[l.size],tt_normal,href);
  579. cg.a_loadmm_reg_ref(list,l.size,l.size,l.register,href,mms_movescalar);
  580. location_reset(l,LOC_REFERENCE,l.size);
  581. l.reference:=href;
  582. end;
  583. reg:=cg.getfpuregister(list,l.size);
  584. cg.a_loadfpu_loc_reg(list,l,reg);
  585. location_freetemp(list,l);
  586. location_reset(l,LOC_FPUREGISTER,l.size);
  587. l.register:=reg;
  588. end;
  589. end;
  590. procedure location_force_mmregscalar(list:TAAsmoutput;var l: tlocation;maybeconst:boolean);
  591. var
  592. reg : tregister;
  593. href : treference;
  594. begin
  595. if (l.loc<>LOC_MMREGISTER) and
  596. ((l.loc<>LOC_CMMREGISTER) or (not maybeconst)) then
  597. begin
  598. { if it's in an fpu register, store to memory first }
  599. if (l.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  600. begin
  601. tg.GetTemp(list,tcgsize2size[l.size],tt_normal,href);
  602. cg.a_loadfpu_reg_ref(list,l.size,l.register,href);
  603. location_reset(l,LOC_REFERENCE,l.size);
  604. l.reference:=href;
  605. end;
  606. reg:=cg.getmmregister(list,l.size);
  607. cg.a_loadmm_loc_reg(list,l.size,l,reg,mms_movescalar);
  608. location_freetemp(list,l);
  609. location_reset(l,LOC_MMREGISTER,l.size);
  610. l.register:=reg;
  611. end;
  612. end;
  613. procedure location_force_mem(list:TAAsmoutput;var l:tlocation);
  614. var
  615. r : treference;
  616. begin
  617. case l.loc of
  618. LOC_FPUREGISTER,
  619. LOC_CFPUREGISTER :
  620. begin
  621. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  622. cg.a_loadfpu_reg_ref(list,l.size,l.register,r);
  623. location_reset(l,LOC_REFERENCE,l.size);
  624. l.reference:=r;
  625. end;
  626. LOC_MMREGISTER,
  627. LOC_CMMREGISTER:
  628. begin
  629. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  630. cg.a_loadmm_reg_ref(list,l.size,l.size,l.register,r,mms_movescalar);
  631. location_reset(l,LOC_REFERENCE,l.size);
  632. l.reference:=r;
  633. end;
  634. LOC_CONSTANT,
  635. LOC_REGISTER,
  636. LOC_CREGISTER :
  637. begin
  638. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  639. {$ifndef cpu64bit}
  640. if l.size in [OS_64,OS_S64] then
  641. cg64.a_load64_loc_ref(list,l,r)
  642. else
  643. {$endif cpu64bit}
  644. cg.a_load_loc_ref(list,l.size,l,r);
  645. location_reset(l,LOC_REFERENCE,l.size);
  646. l.reference:=r;
  647. end;
  648. LOC_CREFERENCE,
  649. LOC_REFERENCE : ;
  650. else
  651. internalerror(200203219);
  652. end;
  653. end;
  654. procedure location_get_data_ref(list:TAAsmoutput;const l:tlocation;var ref:treference;loadref:boolean);
  655. begin
  656. case l.loc of
  657. LOC_REGISTER,
  658. LOC_CREGISTER :
  659. begin
  660. if not loadref then
  661. internalerror(200410231);
  662. reference_reset_base(ref,l.register,0);
  663. end;
  664. LOC_REFERENCE,
  665. LOC_CREFERENCE :
  666. begin
  667. if loadref then
  668. begin
  669. reference_reset_base(ref,cg.getaddressregister(list),0);
  670. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,l.reference,ref.base);
  671. end
  672. else
  673. ref:=l.reference;
  674. end;
  675. else
  676. internalerror(200309181);
  677. end;
  678. end;
  679. {*****************************************************************************
  680. Maybe_Save
  681. *****************************************************************************}
  682. function maybe_pushfpu(list:taasmoutput;needed : byte;var l:tlocation) : boolean;
  683. begin
  684. {$ifdef i386}
  685. if (needed>=maxfpuregs) and
  686. (l.loc = LOC_FPUREGISTER) then
  687. begin
  688. location_force_mem(list,l);
  689. maybe_pushfpu:=true;
  690. end
  691. else
  692. maybe_pushfpu:=false;
  693. {$else i386}
  694. maybe_pushfpu:=false;
  695. {$endif i386}
  696. end;
  697. {****************************************************************************
  698. Init/Finalize Code
  699. ****************************************************************************}
  700. procedure copyvalueparas(p : tnamedindexitem;arg:pointer);
  701. var
  702. href : treference;
  703. hreg : tregister;
  704. list : TAAsmoutput;
  705. hsym : tparavarsym;
  706. l : longint;
  707. localcopyloc : tlocation;
  708. begin
  709. list:=taasmoutput(arg);
  710. if (tsym(p).typ=paravarsym) and
  711. (tparavarsym(p).varspez=vs_value) and
  712. (paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vartype.def,current_procinfo.procdef.proccalloption)) then
  713. begin
  714. location_get_data_ref(list,tparavarsym(p).localloc,href,true);
  715. if is_open_array(tparavarsym(p).vartype.def) or
  716. is_array_of_const(tparavarsym(p).vartype.def) then
  717. begin
  718. { cdecl functions don't have a high pointer so it is not possible to generate
  719. a local copy }
  720. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  721. begin
  722. hsym:=tparavarsym(tsym(p).owner.search('high'+p.name));
  723. if not assigned(hsym) then
  724. internalerror(200306061);
  725. hreg:=cg.getaddressregister(list);
  726. cg.g_copyvaluepara_openarray(list,href,hsym.localloc,tarraydef(tparavarsym(p).vartype.def).elesize,hreg);
  727. cg.a_load_reg_loc(list,OS_ADDR,hreg,tparavarsym(p).localloc);
  728. end;
  729. end
  730. else
  731. begin
  732. { Allocate space for the local copy }
  733. l:=tparavarsym(p).getsize;
  734. localcopyloc.loc:=LOC_REFERENCE;
  735. localcopyloc.size:=int_cgsize(l);
  736. tg.GetLocal(list,l,tparavarsym(p).vartype.def,localcopyloc.reference);
  737. { Copy data }
  738. if is_shortstring(tparavarsym(p).vartype.def) then
  739. begin
  740. { this code is only executed before the code for the body and the entry/exit code is generated
  741. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  742. }
  743. include(current_procinfo.flags,pi_do_call);
  744. cg.g_copyshortstring(list,href,localcopyloc.reference,tstringdef(tparavarsym(p).vartype.def).len)
  745. end
  746. else
  747. cg.g_concatcopy(list,href,localcopyloc.reference,tparavarsym(p).vartype.def.size);
  748. { update localloc of varsym }
  749. tg.Ungetlocal(list,tparavarsym(p).localloc.reference);
  750. tparavarsym(p).localloc:=localcopyloc;
  751. end;
  752. end;
  753. end;
  754. { initializes the regvars from staticsymtable with 0 }
  755. procedure initialize_regvars(p : tnamedindexitem;arg:pointer);
  756. begin
  757. if (tsym(p).typ=globalvarsym) then
  758. begin
  759. case tglobalvarsym(p).localloc.loc of
  760. LOC_CREGISTER :
  761. cg.a_load_const_reg(taasmoutput(arg),reg_cgsize(tglobalvarsym(p).localloc.register),0,
  762. tglobalvarsym(p).localloc.register);
  763. LOC_REFERENCE : ;
  764. else
  765. internalerror(200410124);
  766. end;
  767. end;
  768. end;
  769. { generates the code for initialisation of local data }
  770. procedure initialize_data(p : tnamedindexitem;arg:pointer);
  771. var
  772. oldexprasmlist : TAAsmoutput;
  773. hp : tnode;
  774. begin
  775. if (tsym(p).typ in [globalvarsym,localvarsym]) and
  776. (tabstractvarsym(p).refs>0) and
  777. not(is_class(tabstractvarsym(p).vartype.def)) and
  778. tabstractvarsym(p).vartype.def.needs_inittable then
  779. begin
  780. oldexprasmlist:=exprasmlist;
  781. exprasmlist:=taasmoutput(arg);
  782. hp:=initialize_data_node(cloadnode.create(tsym(p),tsym(p).owner));
  783. firstpass(hp);
  784. secondpass(hp);
  785. hp.free;
  786. exprasmlist:=oldexprasmlist;
  787. end;
  788. end;
  789. procedure finalize_sym(asmlist:taasmoutput;sym:tsym);
  790. var
  791. hp : tnode;
  792. oldexprasmlist : TAAsmoutput;
  793. begin
  794. include(current_procinfo.flags,pi_needs_implicit_finally);
  795. oldexprasmlist:=exprasmlist;
  796. exprasmlist:=asmlist;
  797. hp:=finalize_data_node(cloadnode.create(sym,sym.owner));
  798. firstpass(hp);
  799. secondpass(hp);
  800. hp.free;
  801. exprasmlist:=oldexprasmlist;
  802. end;
  803. { generates the code for finalisation of local variables }
  804. procedure finalize_local_vars(p : tnamedindexitem;arg:pointer);
  805. begin
  806. if (tsym(p).typ=localvarsym) and
  807. (tlocalvarsym(p).refs>0) and
  808. not(vo_is_funcret in tlocalvarsym(p).varoptions) and
  809. not(is_class(tlocalvarsym(p).vartype.def)) and
  810. tlocalvarsym(p).vartype.def.needs_inittable then
  811. finalize_sym(taasmoutput(arg),tsym(p));
  812. end;
  813. { generates the code for finalisation of local typedconsts }
  814. procedure finalize_local_typedconst(p : tnamedindexitem;arg:pointer);
  815. var
  816. i : longint;
  817. pd : tprocdef;
  818. begin
  819. case tsym(p).typ of
  820. typedconstsym :
  821. begin
  822. if ttypedconstsym(p).is_writable and
  823. ttypedconstsym(p).typedconsttype.def.needs_inittable then
  824. finalize_sym(taasmoutput(arg),tsym(p));
  825. end;
  826. procsym :
  827. begin
  828. for i:=1 to tprocsym(p).procdef_count do
  829. begin
  830. pd:=tprocsym(p).procdef[i];
  831. if assigned(pd.localst) and
  832. (pd.procsym=tprocsym(p)) and
  833. (pd.localst.symtabletype<>staticsymtable) then
  834. pd.localst.foreach_static(@finalize_local_typedconst,arg);
  835. end;
  836. end;
  837. end;
  838. end;
  839. { generates the code for finalization of static symtable and
  840. all local (static) typedconsts }
  841. procedure finalize_static_data(p : tnamedindexitem;arg:pointer);
  842. var
  843. i : longint;
  844. pd : tprocdef;
  845. begin
  846. case tsym(p).typ of
  847. globalvarsym :
  848. begin
  849. if (tglobalvarsym(p).refs>0) and
  850. not(vo_is_funcret in tglobalvarsym(p).varoptions) and
  851. not(is_class(tglobalvarsym(p).vartype.def)) and
  852. tglobalvarsym(p).vartype.def.needs_inittable then
  853. finalize_sym(taasmoutput(arg),tsym(p));
  854. end;
  855. typedconstsym :
  856. begin
  857. if ttypedconstsym(p).is_writable and
  858. ttypedconstsym(p).typedconsttype.def.needs_inittable then
  859. finalize_sym(taasmoutput(arg),tsym(p));
  860. end;
  861. procsym :
  862. begin
  863. for i:=1 to tprocsym(p).procdef_count do
  864. begin
  865. pd:=tprocsym(p).procdef[i];
  866. if assigned(pd.localst) and
  867. (pd.procsym=tprocsym(p)) and
  868. (pd.localst.symtabletype<>staticsymtable) then
  869. pd.localst.foreach_static(@finalize_local_typedconst,arg);
  870. end;
  871. end;
  872. end;
  873. end;
  874. { generates the code for incrementing the reference count of parameters and
  875. initialize out parameters }
  876. procedure init_paras(p : tnamedindexitem;arg:pointer);
  877. var
  878. href : treference;
  879. tmpreg : tregister;
  880. list : TAAsmoutput;
  881. begin
  882. list:=taasmoutput(arg);
  883. if (tsym(p).typ=paravarsym) and
  884. not is_class_or_interface(tparavarsym(p).vartype.def) and
  885. tparavarsym(p).vartype.def.needs_inittable then
  886. begin
  887. case tparavarsym(p).varspez of
  888. vs_value :
  889. begin
  890. location_get_data_ref(list,tparavarsym(p).localloc,href,is_open_array(tparavarsym(p).vartype.def));
  891. cg.g_incrrefcount(list,tparavarsym(p).vartype.def,href);
  892. end;
  893. vs_out :
  894. begin
  895. tmpreg:=cg.getaddressregister(list);
  896. cg.a_load_loc_reg(list,OS_ADDR,tparavarsym(p).localloc,tmpreg);
  897. reference_reset_base(href,tmpreg,0);
  898. cg.g_initialize(list,tparavarsym(p).vartype.def,href);
  899. end;
  900. end;
  901. end;
  902. end;
  903. { generates the code for decrementing the reference count of parameters }
  904. procedure final_paras(p : tnamedindexitem;arg:pointer);
  905. var
  906. list : TAAsmoutput;
  907. href : treference;
  908. begin
  909. if not(tsym(p).typ=paravarsym) then
  910. exit;
  911. list:=taasmoutput(arg);
  912. if not is_class_or_interface(tparavarsym(p).vartype.def) and
  913. tparavarsym(p).vartype.def.needs_inittable then
  914. begin
  915. if (tparavarsym(p).varspez=vs_value) then
  916. begin
  917. include(current_procinfo.flags,pi_needs_implicit_finally);
  918. location_get_data_ref(list,tparavarsym(p).localloc,href,is_open_array(tparavarsym(p).vartype.def));
  919. cg.g_decrrefcount(list,tparavarsym(p).vartype.def,href);
  920. end;
  921. end
  922. else
  923. if (tparavarsym(p).varspez=vs_value) and
  924. (is_open_array(tparavarsym(p).vartype.def) or
  925. is_array_of_const(tparavarsym(p).vartype.def)) then
  926. begin
  927. { cdecl functions don't have a high pointer so it is not possible to generate
  928. a local copy }
  929. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  930. cg.g_releasevaluepara_openarray(list,tparavarsym(p).localloc);
  931. end;
  932. end;
  933. { Initialize temp ansi/widestrings,interfaces }
  934. procedure inittempvariables(list:taasmoutput);
  935. var
  936. hp : ptemprecord;
  937. href : treference;
  938. begin
  939. hp:=tg.templist;
  940. while assigned(hp) do
  941. begin
  942. if assigned(hp^.def) and
  943. hp^.def.needs_inittable then
  944. begin
  945. reference_reset_base(href,current_procinfo.framepointer,hp^.pos);
  946. cg.g_initialize(list,hp^.def,href);
  947. end;
  948. hp:=hp^.next;
  949. end;
  950. end;
  951. procedure finalizetempvariables(list:taasmoutput);
  952. var
  953. hp : ptemprecord;
  954. href : treference;
  955. begin
  956. hp:=tg.templist;
  957. while assigned(hp) do
  958. begin
  959. if assigned(hp^.def) and
  960. hp^.def.needs_inittable then
  961. begin
  962. include(current_procinfo.flags,pi_needs_implicit_finally);
  963. reference_reset_base(href,current_procinfo.framepointer,hp^.pos);
  964. cg.g_finalize(list,hp^.def,href);
  965. end;
  966. hp:=hp^.next;
  967. end;
  968. end;
  969. procedure gen_load_return_value(list:TAAsmoutput);
  970. var
  971. {$ifndef cpu64bit}
  972. href : treference;
  973. {$endif cpu64bit}
  974. ressym : tabstractnormalvarsym;
  975. resloc,
  976. restmploc : tlocation;
  977. hreg : tregister;
  978. funcretloc : tlocation;
  979. begin
  980. { Is the loading needed? }
  981. if (current_procinfo.procdef.funcretloc[calleeside].loc=LOC_VOID) or
  982. (
  983. (po_assembler in current_procinfo.procdef.procoptions) and
  984. (not(assigned(current_procinfo.procdef.funcretsym)) or
  985. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs=0))
  986. ) then
  987. exit;
  988. funcretloc:=current_procinfo.procdef.funcretloc[calleeside];
  989. { constructors return self }
  990. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  991. ressym:=tabstractnormalvarsym(current_procinfo.procdef.parast.search('self'))
  992. else
  993. ressym:=tabstractnormalvarsym(current_procinfo.procdef.funcretsym);
  994. if (ressym.refs>0) then
  995. begin
  996. {$ifdef OLDREGVARS}
  997. case ressym.localloc.loc of
  998. LOC_CFPUREGISTER,
  999. LOC_FPUREGISTER:
  1000. begin
  1001. location_reset(restmploc,LOC_CFPUREGISTER,funcretloc^.size);
  1002. restmploc.register:=ressym.localloc.register;
  1003. end;
  1004. LOC_CREGISTER,
  1005. LOC_REGISTER:
  1006. begin
  1007. location_reset(restmploc,LOC_CREGISTER,funcretloc^.size);
  1008. restmploc.register:=ressym.localloc.register;
  1009. end;
  1010. LOC_MMREGISTER:
  1011. begin
  1012. location_reset(restmploc,LOC_CMMREGISTER,funcretloc^.size);
  1013. restmploc.register:=ressym.localloc.register;
  1014. end;
  1015. LOC_REFERENCE:
  1016. begin
  1017. location_reset(restmploc,LOC_REFERENCE,funcretloc^.size);
  1018. restmploc.reference:=ressym.localloc.reference;
  1019. end;
  1020. else
  1021. internalerror(200309184);
  1022. end;
  1023. {$else}
  1024. restmploc:=ressym.localloc;
  1025. {$endif}
  1026. { Here, we return the function result. In most architectures, the value is
  1027. passed into the FUNCTION_RETURN_REG, but in a windowed architecure like sparc a
  1028. function returns in a register and the caller receives it in an other one }
  1029. case funcretloc.loc of
  1030. LOC_REGISTER:
  1031. begin
  1032. {$ifndef cpu64bit}
  1033. if current_procinfo.procdef.funcretloc[calleeside].size in [OS_64,OS_S64] then
  1034. begin
  1035. resloc:=current_procinfo.procdef.funcretloc[calleeside];
  1036. if resloc.loc<>LOC_REGISTER then
  1037. internalerror(200409141);
  1038. { Load low and high register separate to generate better register
  1039. allocation info }
  1040. if getsupreg(resloc.register64.reglo)<first_int_imreg then
  1041. begin
  1042. cg.getcpuregister(list,resloc.register64.reglo);
  1043. cg.ungetcpuregister(list,resloc.register64.reglo);
  1044. { for the optimizer }
  1045. cg.a_reg_alloc(list,resloc.register64.reglo);
  1046. end;
  1047. case restmploc.loc of
  1048. LOC_REFERENCE :
  1049. begin
  1050. href:=restmploc.reference;
  1051. if target_info.endian=ENDIAN_BIG then
  1052. inc(href.offset,4);
  1053. cg.a_load_ref_reg(list,OS_32,OS_32,href,resloc.register64.reglo);
  1054. end;
  1055. LOC_CREGISTER :
  1056. cg.a_load_reg_reg(list,OS_32,OS_32,restmploc.register64.reglo,resloc.register64.reglo);
  1057. else
  1058. internalerror(200409203);
  1059. end;
  1060. if getsupreg(resloc.register64.reghi)<first_int_imreg then
  1061. begin
  1062. cg.getcpuregister(list,resloc.register64.reghi);
  1063. cg.ungetcpuregister(list,resloc.register64.reghi);
  1064. { for the optimizer }
  1065. cg.a_reg_alloc(list,resloc.register64.reghi);
  1066. end;
  1067. case restmploc.loc of
  1068. LOC_REFERENCE :
  1069. begin
  1070. href:=restmploc.reference;
  1071. if target_info.endian=ENDIAN_LITTLE then
  1072. inc(href.offset,4);
  1073. cg.a_load_ref_reg(list,OS_32,OS_32,href,resloc.register64.reghi);
  1074. end;
  1075. LOC_CREGISTER :
  1076. cg.a_load_reg_reg(list,OS_32,OS_32,restmploc.register64.reghi,resloc.register64.reghi);
  1077. else
  1078. internalerror(200409204);
  1079. end;
  1080. end
  1081. else
  1082. {$endif cpu64bit}
  1083. begin
  1084. hreg:=cg.makeregsize(list,funcretloc.register,funcretloc.size);
  1085. if getsupreg(funcretloc.register)<first_int_imreg then
  1086. begin
  1087. cg.getcpuregister(list,funcretloc.register);
  1088. cg.ungetcpuregister(list,hreg);
  1089. { for the optimizer }
  1090. cg.a_reg_alloc(list,funcretloc.register);
  1091. end;
  1092. { it could be that a structure is passed in memory but the function is expected to
  1093. return a pointer to this memory }
  1094. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1095. cg.a_load_loc_reg(list,OS_ADDR,restmploc,hreg)
  1096. else
  1097. cg.a_load_loc_reg(list,restmploc.size,restmploc,hreg);
  1098. end;
  1099. end;
  1100. LOC_FPUREGISTER:
  1101. begin
  1102. if getsupreg(funcretloc.register)<first_fpu_imreg then
  1103. begin
  1104. cg.getcpuregister(list,funcretloc.register);
  1105. cg.ungetcpuregister(list,funcretloc.register);
  1106. end;
  1107. cg.a_loadfpu_loc_reg(list,restmploc,funcretloc.register);
  1108. end;
  1109. LOC_MMREGISTER:
  1110. begin
  1111. if getsupreg(funcretloc.register)<first_mm_imreg then
  1112. begin
  1113. cg.getcpuregister(list,funcretloc.register);
  1114. cg.ungetcpuregister(list,funcretloc.register);
  1115. end;
  1116. cg.a_loadmm_loc_reg(list,restmploc.size,restmploc,funcretloc.register,mms_movescalar);
  1117. end;
  1118. LOC_INVALID,
  1119. LOC_REFERENCE:
  1120. ;
  1121. else
  1122. internalerror(200405025);
  1123. end;
  1124. end;
  1125. end;
  1126. procedure gen_load_para_value(list:TAAsmoutput);
  1127. procedure get_para(const paraloc:TCGParaLocation);
  1128. begin
  1129. case paraloc.loc of
  1130. LOC_REGISTER :
  1131. begin
  1132. if getsupreg(paraloc.register)<first_int_imreg then
  1133. cg.getcpuregister(list,paraloc.register);
  1134. end;
  1135. LOC_MMREGISTER :
  1136. begin
  1137. if getsupreg(paraloc.register)<first_mm_imreg then
  1138. cg.getcpuregister(list,paraloc.register);
  1139. end;
  1140. LOC_FPUREGISTER :
  1141. begin
  1142. if getsupreg(paraloc.register)<first_fpu_imreg then
  1143. cg.getcpuregister(list,paraloc.register);
  1144. end;
  1145. end;
  1146. end;
  1147. procedure unget_para(const paraloc:TCGParaLocation);
  1148. begin
  1149. case paraloc.loc of
  1150. LOC_REGISTER :
  1151. begin
  1152. if getsupreg(paraloc.register)<first_int_imreg then
  1153. cg.ungetcpuregister(list,paraloc.register);
  1154. end;
  1155. LOC_MMREGISTER :
  1156. begin
  1157. if getsupreg(paraloc.register)<first_mm_imreg then
  1158. cg.ungetcpuregister(list,paraloc.register);
  1159. end;
  1160. LOC_FPUREGISTER :
  1161. begin
  1162. if getsupreg(paraloc.register)<first_fpu_imreg then
  1163. cg.ungetcpuregister(list,paraloc.register);
  1164. end;
  1165. end;
  1166. end;
  1167. procedure gen_load_ref(const paraloc:TCGParaLocation;const ref:treference;sizeleft:aint);
  1168. var
  1169. href : treference;
  1170. begin
  1171. case paraloc.loc of
  1172. LOC_REGISTER :
  1173. cg.a_load_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref);
  1174. LOC_MMREGISTER :
  1175. cg.a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,mms_movescalar);
  1176. LOC_FPUREGISTER :
  1177. cg.a_loadfpu_reg_ref(list,paraloc.size,paraloc.register,ref);
  1178. LOC_REFERENCE :
  1179. begin
  1180. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset);
  1181. { use concatcopy, because it can also be a float which fails when
  1182. load_ref_ref is used. Don't copy data when the references are equal }
  1183. if not((href.base=ref.base) and (href.offset=ref.offset)) then
  1184. cg.g_concatcopy(list,href,ref,sizeleft);
  1185. end;
  1186. else
  1187. internalerror(2002081302);
  1188. end;
  1189. end;
  1190. procedure gen_load_reg(const paraloc:TCGParaLocation;reg:tregister);
  1191. var
  1192. href : treference;
  1193. begin
  1194. case paraloc.loc of
  1195. LOC_REGISTER :
  1196. cg.a_load_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg);
  1197. LOC_MMREGISTER :
  1198. cg.a_loadmm_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg,mms_movescalar);
  1199. LOC_FPUREGISTER :
  1200. cg.a_loadfpu_reg_reg(list,paraloc.size,paraloc.register,reg);
  1201. LOC_REFERENCE :
  1202. begin
  1203. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset);
  1204. case getregtype(reg) of
  1205. R_INTREGISTER :
  1206. cg.a_load_ref_reg(list,paraloc.size,paraloc.size,href,reg);
  1207. R_FPUREGISTER :
  1208. cg.a_loadfpu_ref_reg(list,paraloc.size,href,reg);
  1209. R_MMREGISTER :
  1210. cg.a_loadmm_ref_reg(list,paraloc.size,paraloc.size,href,reg,mms_movescalar);
  1211. else
  1212. internalerror(2004101012);
  1213. end;
  1214. end;
  1215. else
  1216. internalerror(2002081302);
  1217. end;
  1218. end;
  1219. var
  1220. i : longint;
  1221. currpara : tparavarsym;
  1222. paraloc : pcgparalocation;
  1223. href : treference;
  1224. sizeleft : aint;
  1225. {$ifdef sparc}
  1226. tempref : treference;
  1227. {$endif sparc}
  1228. begin
  1229. if (po_assembler in current_procinfo.procdef.procoptions) then
  1230. exit;
  1231. { Allocate registers used by parameters }
  1232. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1233. begin
  1234. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1235. paraloc:=currpara.paraloc[calleeside].location;
  1236. while assigned(paraloc) do
  1237. begin
  1238. if paraloc^.loc in [LOC_REGISTER,LOC_FPUREGISTER,LOC_MMREGISTER] then
  1239. get_para(paraloc^);
  1240. paraloc:=paraloc^.next;
  1241. end;
  1242. end;
  1243. { Copy parameters to local references/registers }
  1244. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1245. begin
  1246. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1247. paraloc:=currpara.paraloc[calleeside].location;
  1248. { skip e.g. empty records }
  1249. if (paraloc^.loc = LOC_VOID) then
  1250. continue;
  1251. if not assigned(paraloc) then
  1252. internalerror(200408203);
  1253. case currpara.localloc.loc of
  1254. LOC_REFERENCE :
  1255. begin
  1256. { If the parameter location is reused we don't need to copy
  1257. anything }
  1258. if not paramanager.param_use_paraloc(currpara.paraloc[calleeside]) then
  1259. begin
  1260. href:=currpara.localloc.reference;
  1261. sizeleft:=currpara.paraloc[calleeside].intsize;
  1262. while assigned(paraloc) do
  1263. begin
  1264. unget_para(paraloc^);
  1265. if (paraloc^.size=OS_NO) then
  1266. begin
  1267. { Can only be a reference that contains the rest
  1268. of the parameter }
  1269. if (paraloc^.loc<>LOC_REFERENCE) or
  1270. assigned(paraloc^.next) then
  1271. internalerror(2005013010);
  1272. gen_load_ref(paraloc^,href,sizeleft);
  1273. inc(href.offset,sizeleft);
  1274. sizeleft:=0;
  1275. end
  1276. else
  1277. begin
  1278. gen_load_ref(paraloc^,href,tcgsize2size[paraloc^.size]);
  1279. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1280. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1281. end;
  1282. paraloc:=paraloc^.next;
  1283. end;
  1284. end;
  1285. end;
  1286. LOC_CREGISTER :
  1287. begin
  1288. {$ifndef cpu64bit}
  1289. if (currpara.paraloc[calleeside].size in [OS_64,OS_S64]) and
  1290. is_64bit(currpara.vartype.def) then
  1291. begin
  1292. if not assigned(paraloc^.next) then
  1293. internalerror(200410104);
  1294. if (target_info.endian=ENDIAN_BIG) then
  1295. begin
  1296. { paraloc^ -> high
  1297. paraloc^.next -> low }
  1298. unget_para(paraloc^);
  1299. gen_load_reg(paraloc^,currpara.localloc.register64.reghi);
  1300. unget_para(paraloc^.next^);
  1301. gen_load_reg(paraloc^.next^,currpara.localloc.register64.reglo);
  1302. end
  1303. else
  1304. begin
  1305. { paraloc^ -> low
  1306. paraloc^.next -> high }
  1307. unget_para(paraloc^);
  1308. gen_load_reg(paraloc^,currpara.localloc.register64.reglo);
  1309. unget_para(paraloc^.next^);
  1310. gen_load_reg(paraloc^.next^,currpara.localloc.register64.reghi);
  1311. end;
  1312. end
  1313. else
  1314. {$endif cpu64bit}
  1315. begin
  1316. if assigned(paraloc^.next) then
  1317. internalerror(200410105);
  1318. unget_para(paraloc^);
  1319. gen_load_reg(paraloc^,currpara.localloc.register);
  1320. end;
  1321. end;
  1322. LOC_CFPUREGISTER :
  1323. begin
  1324. {$ifdef sparc}
  1325. { Sparc passes floats in int registers, when loading to fpu register
  1326. we need a temp }
  1327. sizeleft := TCGSize2Size[currpara.localloc.size];
  1328. tg.GetTemp(list,sizeleft,tt_normal,tempref);
  1329. href:=tempref;
  1330. while assigned(paraloc) do
  1331. begin
  1332. unget_para(paraloc^);
  1333. gen_load_ref(paraloc^,href,sizeleft);
  1334. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1335. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1336. paraloc:=paraloc^.next;
  1337. end;
  1338. cg.a_loadfpu_ref_reg(list,currpara.localloc.size,tempref,currpara.localloc.register);
  1339. tg.UnGetTemp(list,tempref);
  1340. {$else sparc}
  1341. unget_para(paraloc^);
  1342. gen_load_reg(paraloc^,currpara.localloc.register);
  1343. if assigned(paraloc^.next) then
  1344. internalerror(200410109);
  1345. {$endif sparc}
  1346. end;
  1347. LOC_CMMREGISTER :
  1348. begin
  1349. unget_para(paraloc^);
  1350. gen_load_reg(paraloc^,currpara.localloc.register);
  1351. if assigned(paraloc^.next) then
  1352. internalerror(200410108);
  1353. end;
  1354. end;
  1355. end;
  1356. { generate copies of call by value parameters, must be done before
  1357. the initialization and body is parsed because the refcounts are
  1358. incremented using the local copies }
  1359. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}copyvalueparas,list);
  1360. {$ifdef powerpc}
  1361. { unget the register that contains the stack pointer before the procedure entry, }
  1362. { which is used to access the parameters in their original callee-side location }
  1363. cg.a_reg_dealloc(list,NR_R12);
  1364. {$endif powerpc}
  1365. end;
  1366. procedure gen_initialize_code(list:TAAsmoutput);
  1367. begin
  1368. { initialize local data like ansistrings }
  1369. case current_procinfo.procdef.proctypeoption of
  1370. potype_unitinit:
  1371. begin
  1372. { this is also used for initialization of variables in a
  1373. program which does not have a globalsymtable }
  1374. if assigned(current_module.globalsymtable) then
  1375. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1376. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1377. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_regvars,list);
  1378. end;
  1379. { units have seperate code for initilization and finalization }
  1380. potype_unitfinalize: ;
  1381. { program init/final is generated in separate procedure }
  1382. potype_proginit:
  1383. begin
  1384. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_regvars,list);
  1385. end;
  1386. else
  1387. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1388. end;
  1389. { initialisizes temp. ansi/wide string data }
  1390. inittempvariables(list);
  1391. { initialize ansi/widesstring para's }
  1392. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1393. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}init_paras,list);
  1394. {$ifdef OLDREGVARS}
  1395. load_regvars(list,nil);
  1396. {$endif OLDREGVARS}
  1397. end;
  1398. procedure gen_finalize_code(list:TAAsmoutput);
  1399. begin
  1400. {$ifdef OLDREGVARS}
  1401. cleanup_regvars(list);
  1402. {$endif OLDREGVARS}
  1403. { finalize temporary data }
  1404. finalizetempvariables(list);
  1405. { finalize local data like ansistrings}
  1406. case current_procinfo.procdef.proctypeoption of
  1407. potype_unitfinalize:
  1408. begin
  1409. { this is also used for initialization of variables in a
  1410. program which does not have a globalsymtable }
  1411. if assigned(current_module.globalsymtable) then
  1412. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_static_data,list);
  1413. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_static_data,list);
  1414. end;
  1415. { units/progs have separate code for initialization and finalization }
  1416. potype_unitinit: ;
  1417. { program init/final is generated in separate procedure }
  1418. potype_proginit: ;
  1419. else
  1420. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}finalize_local_vars,list);
  1421. end;
  1422. { finalize paras data }
  1423. if assigned(current_procinfo.procdef.parast) and
  1424. not(po_assembler in current_procinfo.procdef.procoptions) then
  1425. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}final_paras,list);
  1426. end;
  1427. procedure gen_entry_code(list:TAAsmoutput);
  1428. var
  1429. href : treference;
  1430. paraloc1,
  1431. paraloc2 : tcgpara;
  1432. hp : tused_unit;
  1433. begin
  1434. paraloc1.init;
  1435. paraloc2.init;
  1436. { the actual profile code can clobber some registers,
  1437. therefore if the context must be saved, do it before
  1438. the actual call to the profile code
  1439. }
  1440. if (cs_profile in aktmoduleswitches) and
  1441. not(po_assembler in current_procinfo.procdef.procoptions) then
  1442. begin
  1443. { non-win32 can call mcout even in main }
  1444. if not (target_info.system in [system_i386_win32,system_i386_wdosx]) or
  1445. not (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1446. begin
  1447. cg.alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_cdecl));
  1448. cg.g_profilecode(list);
  1449. cg.dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_cdecl));
  1450. end;
  1451. end;
  1452. { call startup helpers from main program }
  1453. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1454. begin
  1455. { initialize units }
  1456. cg.alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1457. cg.a_call_name(list,'FPC_INITIALIZEUNITS');
  1458. cg.dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1459. {$ifdef GDB}
  1460. if (cs_debuginfo in aktmoduleswitches) then
  1461. if target_info.system <> system_powerpc_macos then
  1462. begin
  1463. { include reference to all debuginfo sections of used units }
  1464. hp:=tused_unit(usedunits.first);
  1465. while assigned(hp) do
  1466. begin
  1467. If (hp.u.flags and uf_has_debuginfo)=uf_has_debuginfo then
  1468. current_procinfo.aktlocaldata.concat(Tai_const.Createname(make_mangledname('DEBUGINFO',hp.u.globalsymtable,''),AT_DATA,0));
  1469. hp:=tused_unit(hp.next);
  1470. end;
  1471. { include reference to debuginfo for this program }
  1472. current_procinfo.aktlocaldata.concat(Tai_const.Createname(make_mangledname('DEBUGINFO',current_module.localsymtable,''),AT_DATA,0));
  1473. end;
  1474. {$endif GDB}
  1475. end;
  1476. {$ifdef GDB}
  1477. if (cs_debuginfo in aktmoduleswitches) then
  1478. list.concat(Tai_force_line.Create);
  1479. {$endif GDB}
  1480. {$ifdef OLDREGVARS}
  1481. load_regvars(list,nil);
  1482. {$endif OLDREGVARS}
  1483. paraloc1.done;
  1484. paraloc2.done;
  1485. end;
  1486. procedure gen_exit_code(list:TAAsmoutput);
  1487. begin
  1488. { call __EXIT for main program }
  1489. if (not DLLsource) and
  1490. (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1491. cg.a_call_name(list,'FPC_DO_EXIT');
  1492. end;
  1493. {****************************************************************************
  1494. Entry/Exit
  1495. ****************************************************************************}
  1496. procedure gen_proc_symbol(list:Taasmoutput);
  1497. var
  1498. hs : string;
  1499. begin
  1500. { add symbol entry point as well as debug information }
  1501. { will be inserted in front of the rest of this list. }
  1502. { Insert alignment and assembler names }
  1503. { Align, gprof uses 16 byte granularity }
  1504. if (cs_profile in aktmoduleswitches) then
  1505. list.concat(Tai_align.create(16))
  1506. else
  1507. list.concat(Tai_align.create(aktalignment.procalign));
  1508. {$ifdef GDB}
  1509. if (cs_debuginfo in aktmoduleswitches) then
  1510. begin
  1511. if (po_global in current_procinfo.procdef.procoptions) then
  1512. Tprocsym(current_procinfo.procdef.procsym).is_global:=true;
  1513. current_procinfo.procdef.concatstabto(list);
  1514. Tprocsym(current_procinfo.procdef.procsym).isstabwritten:=true;
  1515. end;
  1516. {$endif GDB}
  1517. repeat
  1518. hs:=current_procinfo.procdef.aliasnames.getfirst;
  1519. if hs='' then
  1520. break;
  1521. {$ifdef GDB}
  1522. if (cs_debuginfo in aktmoduleswitches) and
  1523. target_info.use_function_relative_addresses then
  1524. list.concat(Tai_stab_function_name.create(strpnew(hs)));
  1525. {$endif GDB}
  1526. if (cs_profile in aktmoduleswitches) or
  1527. (po_global in current_procinfo.procdef.procoptions) then
  1528. list.concat(Tai_symbol.createname_global(hs,AT_FUNCTION,0))
  1529. else
  1530. list.concat(Tai_symbol.createname(hs,AT_FUNCTION,0));
  1531. until false;
  1532. end;
  1533. procedure gen_proc_symbol_end(list:Taasmoutput);
  1534. {$ifdef GDB}
  1535. var
  1536. stabsendlabel : tasmlabel;
  1537. mangled_length : longint;
  1538. p : pchar;
  1539. {$endif GDB}
  1540. begin
  1541. list.concat(Tai_symbol_end.Createname(current_procinfo.procdef.mangledname));
  1542. {$ifdef GDB}
  1543. if (cs_debuginfo in aktmoduleswitches) then
  1544. begin
  1545. objectlibrary.getlabel(stabsendlabel);
  1546. cg.a_label(list,stabsendlabel);
  1547. { define calling EBP as pseudo local var PM }
  1548. { this enables test if the function is a local one !! }
  1549. {if assigned(current_procinfo.parent) and
  1550. (current_procinfo.procdef.parast.symtablelevel>normal_function_level) then
  1551. list.concat(Tai_stabs.Create(strpnew(
  1552. '"parent_ebp:'+tstoreddef(voidpointertype.def).numberstring+'",'+
  1553. tostr(N_LSYM)+',0,0,'+tostr(current_procinfo.parent_framepointer_offset)))); }
  1554. if assigned(current_procinfo.procdef.funcretsym) and
  1555. (tabstractnormalvarsym(current_procinfo.procdef.funcretsym).refs>0) then
  1556. begin
  1557. if tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.loc=LOC_REFERENCE then
  1558. begin
  1559. {$warning Need to add gdb support for ret in param register calling}
  1560. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1561. begin
  1562. list.concat(Tai_stabs.Create(strpnew(
  1563. '"'+current_procinfo.procdef.procsym.name+':X*'+tstoreddef(current_procinfo.procdef.rettype.def).numberstring+'",'+
  1564. tostr(N_tsym)+',0,0,'+tostr(tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset))));
  1565. if (m_result in aktmodeswitches) then
  1566. list.concat(Tai_stabs.Create(strpnew(
  1567. '"RESULT:X*'+tstoreddef(current_procinfo.procdef.rettype.def).numberstring+'",'+
  1568. tostr(N_tsym)+',0,0,'+tostr(tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset))))
  1569. end
  1570. else
  1571. begin
  1572. list.concat(Tai_stabs.Create(strpnew(
  1573. '"'+current_procinfo.procdef.procsym.name+':X'+tstoreddef(current_procinfo.procdef.rettype.def).numberstring+'",'+
  1574. tostr(N_tsym)+',0,0,'+tostr(tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset))));
  1575. if (m_result in aktmodeswitches) then
  1576. list.concat(Tai_stabs.Create(strpnew(
  1577. '"RESULT:X'+tstoreddef(current_procinfo.procdef.rettype.def).numberstring+'",'+
  1578. tostr(N_tsym)+',0,0,'+tostr(tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset))));
  1579. end;
  1580. end;
  1581. end;
  1582. mangled_length:=length(current_procinfo.procdef.mangledname);
  1583. getmem(p,2*mangled_length+50);
  1584. strpcopy(p,'192,0,0,');
  1585. strpcopy(strend(p),current_procinfo.procdef.mangledname);
  1586. if (target_info.use_function_relative_addresses) then
  1587. begin
  1588. strpcopy(strend(p),'-');
  1589. strpcopy(strend(p),current_procinfo.procdef.mangledname);
  1590. end;
  1591. list.concat(Tai_stabn.Create(strnew(p)));
  1592. {List.concat(Tai_stabn.Create(strpnew('192,0,0,'
  1593. +current_procinfo.procdef.mangledname))));
  1594. p[0]:='2';p[1]:='2';p[2]:='4';
  1595. strpcopy(strend(p),'_end');}
  1596. strpcopy(p,'224,0,0,'+stabsendlabel.name);
  1597. if (target_info.use_function_relative_addresses) then
  1598. begin
  1599. strpcopy(strend(p),'-');
  1600. strpcopy(strend(p),current_procinfo.procdef.mangledname);
  1601. end;
  1602. list.concatlist(withdebuglist);
  1603. list.concat(Tai_stabn.Create(strnew(p)));
  1604. { strpnew('224,0,0,'
  1605. +current_procinfo.procdef.mangledname+'_end'))));}
  1606. freemem(p,2*mangled_length+50);
  1607. end;
  1608. {$endif GDB}
  1609. end;
  1610. procedure gen_proc_entry_code(list:Taasmoutput);
  1611. var
  1612. hitemp,
  1613. lotemp : longint;
  1614. begin
  1615. { generate call frame marker for dwarf call frame info }
  1616. dwarfcfi.start_frame(list);
  1617. { All temps are know, write offsets used for information }
  1618. if (cs_asm_source in aktglobalswitches) then
  1619. begin
  1620. if tg.direction>0 then
  1621. begin
  1622. lotemp:=current_procinfo.tempstart;
  1623. hitemp:=tg.lasttemp;
  1624. end
  1625. else
  1626. begin
  1627. lotemp:=tg.lasttemp;
  1628. hitemp:=current_procinfo.tempstart;
  1629. end;
  1630. list.concat(Tai_comment.Create(strpnew('Temps allocated between '+std_regname(current_procinfo.framepointer)+
  1631. tostr_with_plus(lotemp)+' and '+std_regname(current_procinfo.framepointer)+tostr_with_plus(hitemp))));
  1632. end;
  1633. { generate target specific proc entry code }
  1634. cg.g_proc_entry(list,current_procinfo.calc_stackframe_size,(po_nostackframe in current_procinfo.procdef.procoptions));
  1635. end;
  1636. procedure gen_proc_exit_code(list:Taasmoutput);
  1637. var
  1638. parasize : longint;
  1639. begin
  1640. { c style clearstack does not need to remove parameters from the stack, only the
  1641. return value when it was pushed by arguments }
  1642. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1643. begin
  1644. parasize:=0;
  1645. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1646. inc(parasize,sizeof(aint));
  1647. end
  1648. else
  1649. parasize:=current_procinfo.para_stack_size;
  1650. { generate target specific proc exit code }
  1651. cg.g_proc_exit(list,parasize,(po_nostackframe in current_procinfo.procdef.procoptions));
  1652. { release return registers, needed for optimizer }
  1653. if not is_void(current_procinfo.procdef.rettype.def) then
  1654. location_free(list,current_procinfo.procdef.funcretloc[calleeside]);
  1655. { end of frame marker for call frame info }
  1656. dwarfcfi.end_frame(list);
  1657. end;
  1658. procedure gen_stack_check_size_para(list:Taasmoutput);
  1659. var
  1660. paraloc1 : tcgpara;
  1661. begin
  1662. paraloc1.init;
  1663. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1664. paramanager.allocparaloc(list,paraloc1);
  1665. cg.a_param_const(list,OS_INT,current_procinfo.calc_stackframe_size,paraloc1);
  1666. paramanager.freeparaloc(list,paraloc1);
  1667. paraloc1.done;
  1668. end;
  1669. procedure gen_stack_check_call(list:Taasmoutput);
  1670. var
  1671. paraloc1 : tcgpara;
  1672. begin
  1673. paraloc1.init;
  1674. { Also alloc the register needed for the parameter }
  1675. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1676. paramanager.allocparaloc(list,paraloc1);
  1677. paramanager.freeparaloc(list,paraloc1);
  1678. { Call the helper }
  1679. cg.alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1680. cg.a_call_name(list,'FPC_STACKCHECK');
  1681. cg.dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1682. paraloc1.done;
  1683. end;
  1684. procedure gen_save_used_regs(list:TAAsmoutput);
  1685. begin
  1686. { Pure assembler routines need to save the registers themselves }
  1687. if (po_assembler in current_procinfo.procdef.procoptions) then
  1688. exit;
  1689. { oldfpccall expects all registers to be destroyed }
  1690. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1691. cg.g_save_standard_registers(list);
  1692. end;
  1693. procedure gen_restore_used_regs(list:TAAsmoutput);
  1694. begin
  1695. { Pure assembler routines need to save the registers themselves }
  1696. if (po_assembler in current_procinfo.procdef.procoptions) then
  1697. exit;
  1698. { oldfpccall expects all registers to be destroyed }
  1699. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1700. cg.g_restore_standard_registers(list);
  1701. end;
  1702. {****************************************************************************
  1703. External handling
  1704. ****************************************************************************}
  1705. procedure gen_external_stub(list:taasmoutput;pd:tprocdef;const externalname:string);
  1706. begin
  1707. { add the procedure to the codesegment }
  1708. maybe_new_object_file(list);
  1709. new_section(list,sec_code,lower(pd.mangledname),aktalignment.procalign);
  1710. list.concat(Tai_align.create(aktalignment.procalign));
  1711. if (po_global in pd.procoptions) then
  1712. list.concat(Tai_symbol.createname_global(pd.mangledname,AT_FUNCTION,0))
  1713. else
  1714. list.concat(Tai_symbol.createname(pd.mangledname,AT_FUNCTION,0));
  1715. cg.a_jmp_name(list,externalname);
  1716. end;
  1717. {****************************************************************************
  1718. Const Data
  1719. ****************************************************************************}
  1720. procedure insertbssdata(sym : tglobalvarsym);
  1721. var
  1722. l,varalign : longint;
  1723. storefilepos : tfileposinfo;
  1724. begin
  1725. storefilepos:=aktfilepos;
  1726. aktfilepos:=sym.fileinfo;
  1727. l:=sym.getsize;
  1728. if (vo_is_thread_var in sym.varoptions) then
  1729. inc(l,sizeof(aint));
  1730. varalign:=var_align(l);
  1731. maybe_new_object_file(bssSegment);
  1732. new_section(bssSegment,sec_bss,lower(sym.mangledname),varalign);
  1733. {$ifdef GDB}
  1734. if (cs_debuginfo in aktmoduleswitches) then
  1735. sym.concatstabto(bssSegment);
  1736. {$endif GDB}
  1737. if (sym.owner.symtabletype=globalsymtable) or
  1738. maybe_smartlink_symbol or
  1739. DLLSource or
  1740. (assigned(current_procinfo) and
  1741. (po_inline in current_procinfo.procdef.procoptions)) or
  1742. (vo_is_exported in sym.varoptions) or
  1743. (vo_is_C_var in sym.varoptions) then
  1744. bssSegment.concat(Tai_datablock.Create_global(sym.mangledname,l))
  1745. else
  1746. bssSegment.concat(Tai_datablock.Create(sym.mangledname,l));
  1747. aktfilepos:=storefilepos;
  1748. end;
  1749. procedure gen_alloc_symtable(list:TAAsmoutput;st:tsymtable);
  1750. var
  1751. sym : tsym;
  1752. isaddr : boolean;
  1753. cgsize : tcgsize;
  1754. begin
  1755. sym:=tsym(st.symindex.first);
  1756. while assigned(sym) do
  1757. begin
  1758. if (sym.typ in [globalvarsym,localvarsym,paravarsym]) then
  1759. begin
  1760. with tabstractnormalvarsym(sym) do
  1761. begin
  1762. { Parameters passed to assembler procedures need to be kept
  1763. in the original location }
  1764. if (sym.typ=paravarsym) and
  1765. (po_assembler in current_procinfo.procdef.procoptions) then
  1766. begin
  1767. tparavarsym(sym).paraloc[calleeside].get_location(localloc);
  1768. end
  1769. else
  1770. begin
  1771. isaddr:=(st.symtabletype=parasymtable) and
  1772. paramanager.push_addr_param(varspez,vartype.def,current_procinfo.procdef.proccalloption);
  1773. if isaddr then
  1774. cgsize:=OS_ADDR
  1775. else
  1776. cgsize:=def_cgsize(vartype.def);
  1777. {$ifndef OLDREGVARS}
  1778. { When there is assembler code we can't use regvars }
  1779. if is_regvar then
  1780. begin
  1781. localloc.size:=cgsize;
  1782. case varregable of
  1783. vr_intreg :
  1784. begin
  1785. localloc.loc:=LOC_CREGISTER;
  1786. {$ifndef cpu64bit}
  1787. if cgsize in [OS_64,OS_S64] then
  1788. begin
  1789. localloc.register64.reglo:=cg.getintregister(list,OS_32);
  1790. localloc.register64.reghi:=cg.getintregister(list,OS_32);
  1791. end
  1792. else
  1793. {$endif cpu64bit}
  1794. localloc.register:=cg.getintregister(list,cgsize);
  1795. end;
  1796. vr_fpureg :
  1797. begin
  1798. localloc.loc:=LOC_CFPUREGISTER;
  1799. localloc.register:=cg.getfpuregister(list,cgsize);
  1800. end;
  1801. vr_mmreg :
  1802. begin
  1803. localloc.loc:=LOC_CMMREGISTER;
  1804. localloc.register:=cg.getmmregister(list,cgsize);
  1805. end;
  1806. else
  1807. internalerror(2004101010);
  1808. end;
  1809. { Allocate register already, to prevent first allocation to be
  1810. inside a loop }
  1811. {$ifndef cpu64bit}
  1812. if cgsize in [OS_64,OS_S64] then
  1813. begin
  1814. cg.a_reg_sync(list,localloc.register64.reglo);
  1815. cg.a_reg_sync(list,localloc.register64.reghi);
  1816. end
  1817. else
  1818. {$endif cpu64bit}
  1819. cg.a_reg_sync(list,localloc.register);
  1820. end
  1821. else
  1822. {$endif NOT OLDREGVARS}
  1823. begin
  1824. localloc.loc:=LOC_REFERENCE;
  1825. localloc.size:=cgsize;
  1826. case st.symtabletype of
  1827. parasymtable :
  1828. begin
  1829. { Reuse the parameter location for values to are at a single location on the stack }
  1830. if paramanager.param_use_paraloc(tparavarsym(sym).paraloc[calleeside]) then
  1831. begin
  1832. reference_reset_base(localloc.reference,tparavarsym(sym).paraloc[calleeside].location^.reference.index,
  1833. tparavarsym(sym).paraloc[calleeside].location^.reference.offset);
  1834. end
  1835. else
  1836. begin
  1837. if isaddr then
  1838. tg.GetLocal(list,sizeof(aint),voidpointertype.def,localloc.reference)
  1839. else
  1840. tg.GetLocal(list,getsize,vartype.def,localloc.reference);
  1841. end;
  1842. end;
  1843. localsymtable,
  1844. stt_exceptsymtable :
  1845. begin
  1846. tg.GetLocal(list,getsize,vartype.def,localloc.reference);
  1847. end;
  1848. staticsymtable :
  1849. begin
  1850. { PIC, DLL and Threadvar need extra code and are handled in ncgld }
  1851. if not((target_info.system=system_powerpc_darwin) and
  1852. (cs_create_pic in aktmoduleswitches)) and
  1853. not(vo_is_dll_var in varoptions) and
  1854. not(vo_is_thread_var in varoptions) then
  1855. reference_reset_symbol(localloc.reference,objectlibrary.newasmsymbol(mangledname,AB_EXTERNAL,AT_DATA),0);
  1856. end;
  1857. else
  1858. internalerror(200410103);
  1859. end;
  1860. end;
  1861. end;
  1862. if cs_asm_source in aktglobalswitches then
  1863. begin
  1864. case localloc.loc of
  1865. LOC_REGISTER,
  1866. LOC_CREGISTER :
  1867. begin
  1868. if (cs_no_regalloc in aktglobalswitches) then
  1869. list.concat(Tai_comment.Create(strpnew('Var '+realname+' located in register '+
  1870. std_regname(localloc.register))))
  1871. else
  1872. list.concat(Tai_comment.Create(strpnew('Var '+realname+' located in register')));
  1873. end;
  1874. LOC_REFERENCE :
  1875. begin
  1876. if not assigned(localloc.reference.symbol) then
  1877. list.concat(Tai_comment.Create(strpnew('Var '+realname+' located at '+
  1878. std_regname(localloc.reference.base)+tostr_with_plus(localloc.reference.offset))));
  1879. end;
  1880. end;
  1881. end;
  1882. end;
  1883. end;
  1884. sym:=tsym(sym.indexnext);
  1885. end;
  1886. end;
  1887. procedure gen_free_symtable(list:TAAsmoutput;st:tsymtable);
  1888. var
  1889. sym : tsym;
  1890. begin
  1891. sym:=tsym(st.symindex.first);
  1892. while assigned(sym) do
  1893. begin
  1894. if (sym.typ in [globalvarsym,localvarsym,paravarsym]) then
  1895. begin
  1896. with tabstractnormalvarsym(sym) do
  1897. begin
  1898. { Note: We need to keep the data available in memory
  1899. for the sub procedures that can access local data
  1900. in the parent procedures }
  1901. case localloc.loc of
  1902. LOC_CREGISTER :
  1903. {$ifndef cpu64bit}
  1904. if def_cgsize(vartype.def) in [OS_64,OS_S64] then
  1905. begin
  1906. cg.a_reg_sync(list,localloc.register64.reglo);
  1907. cg.a_reg_sync(list,localloc.register64.reghi);
  1908. end
  1909. else
  1910. {$endif cpu64bit}
  1911. cg.a_reg_sync(list,localloc.register);
  1912. LOC_REFERENCE :
  1913. begin
  1914. case st.symtabletype of
  1915. localsymtable,
  1916. parasymtable,
  1917. stt_exceptsymtable :
  1918. tg.Ungetlocal(list,localloc.reference);
  1919. end;
  1920. end;
  1921. end;
  1922. end;
  1923. end;
  1924. sym:=tsym(sym.indexnext);
  1925. end;
  1926. end;
  1927. {$ifdef PASS2INLINE}
  1928. procedure gen_alloc_inline_parast(list:TAAsmoutput;pd:tprocdef);
  1929. var
  1930. sym : tsym;
  1931. calleeparaloc,
  1932. callerparaloc : pcgparalocation;
  1933. begin
  1934. if (po_assembler in pd.procoptions) then
  1935. exit;
  1936. sym:=tsym(pd.parast.symindex.first);
  1937. while assigned(sym) do
  1938. begin
  1939. if sym.typ=paravarsym then
  1940. begin
  1941. with tparavarsym(sym) do
  1942. begin
  1943. { for localloc <> LOC_REFERENCE, we need regvar support inside inlined procedures }
  1944. localloc.loc:=LOC_REFERENCE;
  1945. localloc.size:=int_cgsize(paramanager.push_size(varspez,vartype.def,pd.proccalloption));
  1946. tg.GetLocal(list,tcgsize2size[localloc.size],vartype.def,localloc.reference);
  1947. calleeparaloc:=paraloc[calleeside].location;
  1948. callerparaloc:=paraloc[callerside].location;
  1949. while assigned(calleeparaloc) do
  1950. begin
  1951. if not assigned(callerparaloc) then
  1952. internalerror(200408281);
  1953. if calleeparaloc^.loc<>callerparaloc^.loc then
  1954. internalerror(200408282);
  1955. case calleeparaloc^.loc of
  1956. LOC_FPUREGISTER:
  1957. begin
  1958. calleeparaloc^.register:=cg.getfpuregister(list,calleeparaloc^.size);
  1959. callerparaloc^.register:=calleeparaloc^.register;
  1960. end;
  1961. LOC_REGISTER:
  1962. begin
  1963. calleeparaloc^.register:=cg.getintregister(list,calleeparaloc^.size);
  1964. callerparaloc^.register:=calleeparaloc^.register;
  1965. end;
  1966. LOC_MMREGISTER:
  1967. begin
  1968. calleeparaloc^.register:=cg.getmmregister(list,calleeparaloc^.size);
  1969. callerparaloc^.register:=calleeparaloc^.register;
  1970. end;
  1971. LOC_REFERENCE:
  1972. begin
  1973. calleeparaloc^.reference.offset := localloc.reference.offset;
  1974. calleeparaloc^.reference.index := localloc.reference.base;
  1975. callerparaloc^.reference.offset := localloc.reference.offset;
  1976. callerparaloc^.reference.index := localloc.reference.base;
  1977. end;
  1978. end;
  1979. calleeparaloc:=calleeparaloc^.next;
  1980. callerparaloc:=callerparaloc^.next;
  1981. end;
  1982. if cs_asm_source in aktglobalswitches then
  1983. begin
  1984. case localloc.loc of
  1985. LOC_REFERENCE :
  1986. list.concat(Tai_comment.Create(strpnew('Para '+realname+' allocated at '+
  1987. std_regname(localloc.reference.base)+tostr_with_plus(localloc.reference.offset))));
  1988. end;
  1989. end;
  1990. end;
  1991. end;
  1992. sym:=tsym(sym.indexnext);
  1993. end;
  1994. end;
  1995. procedure gen_alloc_inline_funcret(list:TAAsmoutput;pd:tprocdef);
  1996. var
  1997. callerparaloc : tlocation;
  1998. begin
  1999. if not assigned(pd.funcretsym) or
  2000. (po_assembler in pd.procoptions) then
  2001. exit;
  2002. { for localloc <> LOC_REFERENCE, we need regvar support inside inlined procedures }
  2003. with tabstractnormalvarsym(pd.funcretsym) do
  2004. begin
  2005. localloc.loc:=LOC_REFERENCE;
  2006. localloc.size:=int_cgsize(paramanager.push_size(varspez,vartype.def,pd.proccalloption));
  2007. tg.GetLocal(list,tcgsize2size[localloc.size],vartype.def,localloc.reference);
  2008. callerparaloc:=pd.funcretloc[callerside];
  2009. case pd.funcretloc[calleeside].loc of
  2010. LOC_FPUREGISTER:
  2011. begin
  2012. pd.funcretloc[calleeside].register:=cg.getfpuregister(list,pd.funcretloc[calleeside].size);
  2013. pd.funcretloc[callerside].register:=pd.funcretloc[calleeside].register;
  2014. end;
  2015. LOC_REGISTER:
  2016. begin
  2017. {$ifndef cpu64bit}
  2018. if callerparaloc.size in [OS_64,OS_S64] then
  2019. begin
  2020. end
  2021. else
  2022. {$endif cpu64bit}
  2023. begin
  2024. pd.funcretloc[calleeside].register:=cg.getintregister(list,pd.funcretloc[calleeside].size);
  2025. pd.funcretloc[callerside].register:=pd.funcretloc[calleeside].register;
  2026. end;
  2027. end;
  2028. LOC_MMREGISTER:
  2029. begin
  2030. pd.funcretloc[calleeside].register:=cg.getmmregister(list,pd.funcretloc[calleeside].size);
  2031. pd.funcretloc[callerside].register:=pd.funcretloc[calleeside].register;
  2032. end;
  2033. LOC_REFERENCE:
  2034. begin
  2035. pd.funcretloc[calleeside].reference.offset := localloc.reference.offset;
  2036. pd.funcretloc[calleeside].reference.index := localloc.reference.base;
  2037. pd.funcretloc[callerside].reference.offset := localloc.reference.offset;
  2038. pd.funcretloc[callerside].reference.index := localloc.reference.base;
  2039. end;
  2040. LOC_VOID:
  2041. ;
  2042. else
  2043. internalerror(200411191);
  2044. end;
  2045. if cs_asm_source in aktglobalswitches then
  2046. begin
  2047. case localloc.loc of
  2048. LOC_REFERENCE :
  2049. list.concat(Tai_comment.Create(strpnew('Funcret '+realname+' allocated at '+
  2050. std_regname(localloc.reference.base)+tostr_with_plus(localloc.reference.offset))));
  2051. end;
  2052. end;
  2053. end;
  2054. end;
  2055. {$endif PASS2INLINE}
  2056. { persistent rtti generation }
  2057. procedure generate_rtti(p:Ttypesym);
  2058. var
  2059. rsym : trttisym;
  2060. def : tstoreddef;
  2061. begin
  2062. { rtti can only be generated for classes that are always typesyms }
  2063. def:=tstoreddef(ttypesym(p).restype.def);
  2064. { there is an error, skip rtti info }
  2065. if (def.deftype=errordef) or (Errorcount>0) then
  2066. exit;
  2067. { only create rtti once for each definition }
  2068. if not(df_has_rttitable in def.defoptions) then
  2069. begin
  2070. { definition should be in the same symtable as the symbol }
  2071. if p.owner<>def.owner then
  2072. internalerror(200108262);
  2073. { create rttisym }
  2074. rsym:=trttisym.create(p.name,fullrtti);
  2075. p.owner.insert(rsym);
  2076. { register rttisym in definition }
  2077. include(def.defoptions,df_has_rttitable);
  2078. def.rttitablesym:=rsym;
  2079. { write rtti data }
  2080. def.write_child_rtti_data(fullrtti);
  2081. maybe_new_object_file(rttilist);
  2082. new_section(rttilist,sec_rodata,rsym.get_label.name,const_align(sizeof(aint)));
  2083. rttiList.concat(Tai_symbol.Create_global(rsym.get_label,0));
  2084. def.write_rtti_data(fullrtti);
  2085. rttiList.concat(Tai_symbol_end.Create(rsym.get_label));
  2086. end;
  2087. end;
  2088. { persistent init table generation }
  2089. procedure generate_inittable(p:tsym);
  2090. var
  2091. rsym : trttisym;
  2092. def : tstoreddef;
  2093. begin
  2094. { anonymous types are also allowed for records that can be varsym }
  2095. case p.typ of
  2096. typesym :
  2097. def:=tstoreddef(ttypesym(p).restype.def);
  2098. globalvarsym,
  2099. localvarsym,
  2100. paravarsym :
  2101. def:=tstoreddef(tabstractvarsym(p).vartype.def);
  2102. else
  2103. internalerror(200108263);
  2104. end;
  2105. { only create inittable once for each definition }
  2106. if not(df_has_inittable in def.defoptions) then
  2107. begin
  2108. { definition should be in the same symtable as the symbol }
  2109. if p.owner<>def.owner then
  2110. internalerror(200108264);
  2111. { create rttisym }
  2112. rsym:=trttisym.create(p.name,initrtti);
  2113. p.owner.insert(rsym);
  2114. { register rttisym in definition }
  2115. include(def.defoptions,df_has_inittable);
  2116. def.inittablesym:=rsym;
  2117. { write inittable data }
  2118. def.write_child_rtti_data(initrtti);
  2119. maybe_new_object_file(rttilist);
  2120. new_section(rttilist,sec_rodata,rsym.get_label.name,const_align(sizeof(aint)));
  2121. rttiList.concat(Tai_symbol.Create_global(rsym.get_label,0));
  2122. def.write_rtti_data(initrtti);
  2123. rttiList.concat(Tai_symbol_end.Create(rsym.get_label));
  2124. end;
  2125. end;
  2126. procedure gen_intf_wrapper(list:taasmoutput;_class:tobjectdef);
  2127. var
  2128. i,j,
  2129. proccount : longint;
  2130. tmps : string;
  2131. begin
  2132. for i:=1 to _class.implementedinterfaces.count do
  2133. begin
  2134. { only if implemented by this class }
  2135. if _class.implementedinterfaces.implindex(i)=i then
  2136. begin
  2137. proccount:=_class.implementedinterfaces.implproccount(i);
  2138. for j:=1 to proccount do
  2139. begin
  2140. tmps:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+
  2141. _class.implementedinterfaces.interfaces(i).objname^+'_$_'+
  2142. tostr(j)+'_$_'+_class.implementedinterfaces.implprocs(i,j).mangledname);
  2143. { create wrapper code }
  2144. new_section(list,sec_code,lower(tmps),0);
  2145. cg.g_intf_wrapper(list,_class.implementedinterfaces.implprocs(i,j),tmps,_class.implementedinterfaces.ioffsets(i));
  2146. end;
  2147. end;
  2148. end;
  2149. end;
  2150. procedure gen_intf_wrappers(list:taasmoutput;st:tsymtable);
  2151. var
  2152. def : tstoreddef;
  2153. begin
  2154. def:=tstoreddef(st.defindex.first);
  2155. while assigned(def) do
  2156. begin
  2157. if is_class(def) then
  2158. gen_intf_wrapper(list,tobjectdef(def));
  2159. def:=tstoreddef(def.indexnext);
  2160. end;
  2161. end;
  2162. end.