ngenutil.pas 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747
  1. {
  2. Copyright (c) 1998-2011 by Florian Klaempfl
  3. Generic version of some node tree helper routines that can be overridden
  4. by cpu-specific versions
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ngenutil;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses,globtype,
  23. fmodule,
  24. aasmbase,aasmdata,
  25. node,nbas,symtype,symsym,symconst,symdef;
  26. type
  27. tinitfinalentry = record
  28. initfunc : TSymStr;
  29. finifunc : TSymStr;
  30. initpd : tprocdef;
  31. finipd : tprocdef;
  32. module : tmodule;
  33. end;
  34. pinitfinalentry = ^tinitfinalentry;
  35. tnodeutils = class
  36. class function call_fail_node:tnode; virtual;
  37. class function initialize_data_node(p:tnode; force: boolean):tnode; virtual;
  38. class function finalize_data_node(p:tnode):tnode; virtual;
  39. strict protected
  40. type
  41. tstructinifinipotype = potype_class_constructor..potype_class_destructor;
  42. class procedure sym_maybe_initialize(p: TObject; arg: pointer);
  43. { generates the code for finalisation of local variables }
  44. class procedure local_varsyms_finalize(p:TObject;arg:pointer);
  45. { generates the code for finalization of static symtable and
  46. all local (static) typed consts }
  47. class procedure static_syms_finalize(p: TObject; arg: pointer);
  48. class procedure sym_maybe_finalize(var stat: tstatementnode; sym: tsym);
  49. class procedure append_struct_initfinis(u: tmodule; initfini: tstructinifinipotype; var stat: tstatementnode); virtual;
  50. public
  51. class procedure procdef_block_add_implicit_initialize_nodes(pd: tprocdef; var stat: tstatementnode);
  52. class procedure procdef_block_add_implicit_finalize_nodes(pd: tprocdef; var stat: tstatementnode);
  53. { returns true if the unit requires an initialisation section (e.g.,
  54. to force class constructors for the JVM target to initialise global
  55. records/arrays) }
  56. class function force_init: boolean; virtual;
  57. { idem for finalization }
  58. class function force_final: boolean; virtual;
  59. { if the funcretsym was moved to the parentfpstruct, use this method to
  60. move its value back back into the funcretsym before the function exit, as
  61. the code generator is hardcoded to use to use the funcretsym when loading
  62. the value to be returned; replacing it with an absolutevarsym that
  63. redirects to the field in the parentfpstruct doesn't work, as the code
  64. generator cannot deal with such symbols }
  65. class procedure load_parentfpstruct_nested_funcret(ressym: tsym; var stat: tstatementnode);
  66. { called after parsing a routine with the code of the entire routine
  67. as argument; can be used to modify the node tree. By default handles
  68. insertion of code for systems that perform the typed constant
  69. initialisation via the node tree }
  70. class function wrap_proc_body(pd: tprocdef; n: tnode): tnode; virtual;
  71. { trashes a paravarsym or localvarsym if possible (not a managed type,
  72. "out" in case of parameter, ...) }
  73. class procedure maybe_trash_variable(var stat: tstatementnode; p: tabstractnormalvarsym; trashn: tnode); virtual;
  74. class function check_insert_trashing(pd: tprocdef): boolean; virtual;
  75. strict protected
  76. { called from wrap_proc_body to insert the trashing for the wrapped
  77. routine's local variables and parameters }
  78. class function maybe_insert_trashing(pd: tprocdef; n: tnode): tnode;
  79. { callback called for every local variable and parameter by
  80. maybe_insert_trashing(), calls through to maybe_trash_variable() }
  81. class procedure maybe_trash_variable_callback(p: TObject; statn: pointer);
  82. { returns whether a particular sym can be trashed. If not,
  83. maybe_trash_variable won't do anything }
  84. class function trashable_sym(p: tsym): boolean; virtual;
  85. { trashing for 1/2/3/4/8-byte sized variables }
  86. class procedure trash_small(var stat: tstatementnode; trashn: tnode; trashvaln: tnode); virtual;
  87. { trashing for differently sized variables that those handled by
  88. trash_small() }
  89. class procedure trash_large(var stat: tstatementnode; trashn, sizen: tnode; trashintval: int64); virtual;
  90. { insert a single bss sym, called by insert bssdata (factored out
  91. non-common part for llvm) }
  92. class procedure insertbsssym(list: tasmlist; sym: tstaticvarsym; size: asizeint; varalign: shortint; _typ: Tasmsymtype); virtual;
  93. { initialization of iso styled program parameters }
  94. class procedure initialize_filerecs(p : TObject; statn : pointer);
  95. { finalization of iso styled program parameters }
  96. class procedure finalize_filerecs(p : TObject; statn : pointer);
  97. public
  98. class procedure insertbssdata(sym : tstaticvarsym); virtual;
  99. class function create_main_procdef(const name: string; potype:tproctypeoption; ps: tprocsym):tdef; virtual;
  100. class procedure InsertInitFinalTable(main : tmodule);
  101. protected
  102. class procedure InsertRuntimeInits(const prefix:string;list:TLinkedList;unitflag:tmoduleflag); virtual;
  103. class procedure InsertRuntimeInitsTablesTable(const prefix,tablename:string;unitflag:tmoduleflag); virtual;
  104. class procedure insert_init_final_table(main: tmodule; entries:tfplist); virtual;
  105. class function get_init_final_list(main : tmodule): tfplist;
  106. class procedure release_init_final_list(list:tfplist);
  107. public
  108. class procedure InsertThreadvarTablesTable; virtual;
  109. class procedure InsertThreadvars; virtual;
  110. class procedure InsertWideInitsTablesTable; virtual;
  111. class procedure InsertWideInits; virtual;
  112. class procedure InsertResStrInits; virtual;
  113. class procedure InsertResStrTablesTable; virtual;
  114. class procedure InsertResourceTablesTable; virtual;
  115. class procedure InsertResourceInfo(ResourcesUsed : boolean); virtual;
  116. class procedure InsertMemorySizes; virtual;
  117. { Call this to check if init code is required }
  118. class function has_init_list: boolean; static;
  119. { called right before an object is assembled, can be used to insert
  120. global information into the assembler list (used by LLVM to insert type
  121. info) }
  122. class procedure InsertObjectInfo; virtual;
  123. { register that asm symbol sym with type def has to be considered as "used" even if not
  124. references to it can be found. If compileronly, this is only for the compiler, otherwise
  125. also for the linker }
  126. class procedure RegisterUsedAsmSym(sym: TAsmSymbol; def: tdef; compileronly: boolean); virtual;
  127. class procedure RegisterModuleInitFunction(pd: tprocdef); virtual;
  128. class procedure RegisterModuleFiniFunction(pd: tprocdef); virtual;
  129. strict protected
  130. class procedure add_main_procdef_paras(pd: tdef); virtual;
  131. end;
  132. tnodeutilsclass = class of tnodeutils;
  133. const
  134. cnodeutils: tnodeutilsclass = tnodeutils;
  135. implementation
  136. uses
  137. verbose,version,globals,cutils,constexp,compinnr,
  138. systems,procinfo,pparautl,
  139. aasmtai,aasmcnst,
  140. symbase,symtable,defutil,
  141. nadd,ncal,ncnv,ncon,nflw,ninl,nld,nmem,nutils,
  142. ppu,
  143. pass_1,
  144. export;
  145. class function tnodeutils.call_fail_node:tnode;
  146. var
  147. para : tcallparanode;
  148. newstatement : tstatementnode;
  149. srsym : tsym;
  150. begin
  151. result:=internalstatements(newstatement);
  152. { call fail helper and exit normal }
  153. if is_class(current_structdef) then
  154. begin
  155. srsym:=search_struct_member(current_structdef,'FREEINSTANCE');
  156. if assigned(srsym) and
  157. (srsym.typ=procsym) then
  158. begin
  159. { if self<>0 and vmt<>0 then freeinstance }
  160. addstatement(newstatement,cifnode.create(
  161. caddnode.create(andn,
  162. caddnode.create(unequaln,
  163. load_self_pointer_node,
  164. cnilnode.create),
  165. caddnode.create(unequaln,
  166. load_vmt_pointer_node,
  167. cnilnode.create)),
  168. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[],nil),
  169. nil));
  170. end
  171. else
  172. internalerror(2003051002);
  173. end
  174. else
  175. if is_object(current_structdef) then
  176. begin
  177. { parameter 3 : vmt_offset }
  178. { parameter 2 : pointer to vmt }
  179. { parameter 1 : self pointer }
  180. para:=ccallparanode.create(
  181. cordconstnode.create(tobjectdef(current_structdef).vmt_offset,s32inttype,false),
  182. ccallparanode.create(
  183. ctypeconvnode.create_internal(
  184. load_vmt_pointer_node,
  185. voidpointertype),
  186. ccallparanode.create(
  187. ctypeconvnode.create_internal(
  188. load_self_pointer_node,
  189. voidpointertype),
  190. nil)));
  191. addstatement(newstatement,
  192. ccallnode.createintern('fpc_help_fail',para));
  193. end
  194. else
  195. internalerror(200305132);
  196. { self:=nil }
  197. addstatement(newstatement,cassignmentnode.create(
  198. load_self_pointer_node,
  199. cnilnode.create));
  200. { exit }
  201. addstatement(newstatement,cexitnode.create(nil));
  202. end;
  203. class function tnodeutils.initialize_data_node(p:tnode; force: boolean):tnode;
  204. begin
  205. { prevent initialisation of hidden syms that were moved to
  206. parentfpstructs: the original symbol isn't used anymore, the version
  207. in parentfpstruct will be initialised when that struct gets initialised,
  208. and references to it will actually be translated into references to the
  209. field in the parentfpstruct (so we'll initialise it twice) }
  210. if (target_info.system in systems_fpnestedstruct) and
  211. (p.nodetype=loadn) and
  212. (tloadnode(p).symtableentry.typ=localvarsym) and
  213. tlocalvarsym(tloadnode(p).symtableentry).inparentfpstruct then
  214. begin
  215. p.free;
  216. result:=cnothingnode.create;
  217. end
  218. else
  219. begin
  220. if not assigned(p.resultdef) then
  221. typecheckpass(p);
  222. if is_ansistring(p.resultdef) or
  223. is_wide_or_unicode_string(p.resultdef) or
  224. is_interfacecom_or_dispinterface(p.resultdef) or
  225. is_dynamic_array(p.resultdef) then
  226. begin
  227. result:=cassignmentnode.create(
  228. ctypeconvnode.create_internal(p,voidpointertype),
  229. cnilnode.create
  230. );
  231. end
  232. else if (p.resultdef.typ=variantdef) then
  233. begin
  234. result:=ccallnode.createintern('fpc_variant_init',
  235. ccallparanode.create(
  236. ctypeconvnode.create_internal(p,search_system_type('TVARDATA').typedef),
  237. nil));
  238. end
  239. else
  240. begin
  241. result:=ccallnode.createintern('fpc_initialize',
  242. ccallparanode.create(
  243. caddrnode.create_internal(
  244. crttinode.create(
  245. tstoreddef(p.resultdef),initrtti,rdt_normal)),
  246. ccallparanode.create(
  247. caddrnode.create_internal(p),
  248. nil)));
  249. end;
  250. end;
  251. end;
  252. class function tnodeutils.finalize_data_node(p:tnode):tnode;
  253. var
  254. hs : string;
  255. begin
  256. { see comment in initialize_data_node above }
  257. if (target_info.system in systems_fpnestedstruct) and
  258. (p.nodetype=loadn) and
  259. (tloadnode(p).symtableentry.typ=localvarsym) and
  260. tlocalvarsym(tloadnode(p).symtableentry).inparentfpstruct then
  261. begin
  262. p.free;
  263. result:=cnothingnode.create;
  264. end
  265. else
  266. begin
  267. if not assigned(p.resultdef) then
  268. typecheckpass(p);
  269. { 'decr_ref' suffix is somewhat misleading, all these helpers
  270. set the passed pointer to nil now }
  271. if is_ansistring(p.resultdef) then
  272. hs:='fpc_ansistr_decr_ref'
  273. else if is_widestring(p.resultdef) then
  274. hs:='fpc_widestr_decr_ref'
  275. else if is_unicodestring(p.resultdef) then
  276. hs:='fpc_unicodestr_decr_ref'
  277. else if is_interfacecom_or_dispinterface(p.resultdef) then
  278. hs:='fpc_intf_decr_ref'
  279. else
  280. hs:='';
  281. if hs<>'' then
  282. result:=ccallnode.createintern(hs,
  283. ccallparanode.create(
  284. ctypeconvnode.create_internal(p,voidpointertype),
  285. nil))
  286. else if p.resultdef.typ=variantdef then
  287. begin
  288. result:=ccallnode.createintern('fpc_variant_clear',
  289. ccallparanode.create(
  290. ctypeconvnode.create_internal(p,search_system_type('TVARDATA').typedef),
  291. nil));
  292. end
  293. else
  294. result:=ccallnode.createintern('fpc_finalize',
  295. ccallparanode.create(
  296. caddrnode.create_internal(
  297. crttinode.create(
  298. tstoreddef(p.resultdef),initrtti,rdt_normal)),
  299. ccallparanode.create(
  300. caddrnode.create_internal(p),
  301. nil)));
  302. end;
  303. end;
  304. class procedure tnodeutils.sym_maybe_initialize(p: TObject; arg: pointer);
  305. var
  306. hp : tnode;
  307. begin
  308. if ((tsym(p).typ = localvarsym) or
  309. { check staticvarsym for record management opeators and for objects
  310. which might contain record with management operators }
  311. ((tsym(p).typ = staticvarsym) and
  312. (
  313. is_record(tabstractvarsym(p).vardef) or
  314. is_object(tabstractvarsym(p).vardef)
  315. )
  316. )
  317. ) and
  318. { local (procedure or unit) variables only need initialization if
  319. they are used }
  320. ((tabstractvarsym(p).refs>0) or
  321. { managed return symbols must be inited }
  322. ((tsym(p).typ=localvarsym) and (vo_is_funcret in tlocalvarsym(p).varoptions))
  323. ) and
  324. not(vo_is_typed_const in tabstractvarsym(p).varoptions) and
  325. not(vo_is_external in tabstractvarsym(p).varoptions) and
  326. (
  327. not (vo_is_default_var in tabstractvarsym(p).varoptions) or
  328. (tabstractvarsym(p).varspez<>vs_const)
  329. ) and
  330. (is_managed_type(tabstractvarsym(p).vardef) or
  331. ((m_iso in current_settings.modeswitches) and (tabstractvarsym(p).vardef.typ=filedef))
  332. ) then
  333. begin
  334. hp:=cloadnode.create(tsym(p),tsym(p).owner);
  335. { ensure that a function reference is not converted to a call }
  336. include(hp.flags,nf_load_procvar);
  337. addstatement(tstatementnode(arg^),initialize_data_node(hp,false));
  338. end;
  339. end;
  340. class procedure tnodeutils.local_varsyms_finalize(p: TObject; arg: pointer);
  341. begin
  342. if (tsym(p).typ=localvarsym) and
  343. (tlocalvarsym(p).refs>0) and
  344. not(vo_is_typed_const in tlocalvarsym(p).varoptions) and
  345. not(vo_is_external in tlocalvarsym(p).varoptions) and
  346. not(vo_is_funcret in tlocalvarsym(p).varoptions) and
  347. (
  348. not(vo_is_default_var in tabstractvarsym(p).varoptions) or
  349. (tabstractvarsym(p).varspez<>vs_const)
  350. ) and
  351. is_managed_type(tlocalvarsym(p).vardef) then
  352. sym_maybe_finalize(tstatementnode(arg^),tsym(p));
  353. end;
  354. class procedure tnodeutils.static_syms_finalize(p: TObject; arg: pointer);
  355. var
  356. i : longint;
  357. pd : tprocdef;
  358. begin
  359. case tsym(p).typ of
  360. staticvarsym :
  361. begin
  362. { local (procedure or unit) variables only need finalization
  363. if they are used
  364. }
  365. if ((tstaticvarsym(p).refs>0) or
  366. { global (unit) variables always need finalization, since
  367. they may also be used in another unit
  368. }
  369. (tstaticvarsym(p).owner.symtabletype=globalsymtable)) and
  370. (
  371. (tstaticvarsym(p).varspez<>vs_const) or
  372. (vo_force_finalize in tstaticvarsym(p).varoptions)
  373. ) and
  374. not(vo_is_funcret in tstaticvarsym(p).varoptions) and
  375. not(vo_is_external in tstaticvarsym(p).varoptions) and
  376. is_managed_type(tstaticvarsym(p).vardef) and
  377. not (
  378. assigned(tstaticvarsym(p).fieldvarsym) and
  379. assigned(tstaticvarsym(p).fieldvarsym.owner.defowner) and
  380. (df_generic in tdef(tstaticvarsym(p).fieldvarsym.owner.defowner).defoptions)
  381. )
  382. then
  383. sym_maybe_finalize(tstatementnode(arg^),tsym(p));
  384. end;
  385. procsym :
  386. begin
  387. for i:=0 to tprocsym(p).ProcdefList.Count-1 do
  388. begin
  389. pd:=tprocdef(tprocsym(p).ProcdefList[i]);
  390. if assigned(pd.localst) and
  391. (pd.procsym=tprocsym(p)) and
  392. (pd.localst.symtabletype<>staticsymtable) then
  393. pd.localst.SymList.ForEachCall(@static_syms_finalize,arg);
  394. end;
  395. end;
  396. else
  397. ;
  398. end;
  399. end;
  400. class procedure tnodeutils.sym_maybe_finalize(var stat: tstatementnode; sym: tsym);
  401. var
  402. hp: tnode;
  403. begin
  404. include(current_procinfo.flags,pi_needs_implicit_finally);
  405. hp:=cloadnode.create(sym,sym.owner);
  406. if (sym.typ=staticvarsym) and (vo_force_finalize in tstaticvarsym(sym).varoptions) then
  407. include(tloadnode(hp).loadnodeflags,loadnf_isinternal_ignoreconst);
  408. { ensure that a function reference interface is not converted to a call }
  409. include(hp.flags,nf_load_procvar);
  410. addstatement(stat,finalize_data_node(hp));
  411. end;
  412. procedure AddToStructInits(p:TObject;arg:pointer);
  413. var
  414. StructList: TFPList absolute arg;
  415. begin
  416. if (tdef(p).typ in [objectdef,recorddef]) and
  417. not (df_generic in tdef(p).defoptions) then
  418. begin
  419. { first add the class... }
  420. if ([oo_has_class_constructor,oo_has_class_destructor] * tabstractrecorddef(p).objectoptions <> []) then
  421. StructList.Add(p);
  422. { ... and then also add all subclasses }
  423. tabstractrecorddef(p).symtable.deflist.foreachcall(@AddToStructInits,arg);
  424. end;
  425. end;
  426. class procedure tnodeutils.append_struct_initfinis(u: tmodule; initfini: tstructinifinipotype; var stat: tstatementnode);
  427. var
  428. structlist: tfplist;
  429. i: integer;
  430. pd: tprocdef;
  431. begin
  432. structlist:=tfplist.Create;
  433. if assigned(u.globalsymtable) then
  434. u.globalsymtable.DefList.ForEachCall(@AddToStructInits,structlist);
  435. u.localsymtable.DefList.ForEachCall(@AddToStructInits,structlist);
  436. { write structures }
  437. for i:=0 to structlist.Count-1 do
  438. begin
  439. pd:=tabstractrecorddef(structlist[i]).find_procdef_bytype(initfini);
  440. if assigned(pd) then
  441. begin
  442. { class constructors are private -> ignore visibility checks }
  443. addstatement(stat,
  444. ccallnode.create(nil,tprocsym(pd.procsym),pd.owner,nil,[cnf_ignore_visibility],nil))
  445. end;
  446. end;
  447. structlist.free;
  448. end;
  449. class procedure tnodeutils.procdef_block_add_implicit_initialize_nodes(pd: tprocdef; var stat: tstatementnode);
  450. begin
  451. { initialize local data like ansistrings }
  452. case pd.proctypeoption of
  453. potype_unitinit:
  454. begin
  455. { this is also used for initialization of variables in a
  456. program which does not have a globalsymtable }
  457. if assigned(current_module.globalsymtable) then
  458. TSymtable(current_module.globalsymtable).SymList.ForEachCall(@sym_maybe_initialize,@stat);
  459. TSymtable(current_module.localsymtable).SymList.ForEachCall(@sym_maybe_initialize,@stat);
  460. { insert class constructors }
  461. if mf_classinits in current_module.moduleflags then
  462. append_struct_initfinis(current_module, potype_class_constructor, stat);
  463. end;
  464. { units have separate code for initilization and finalization }
  465. potype_unitfinalize: ;
  466. { program init/final is generated in separate procedure }
  467. potype_proginit: ;
  468. else
  469. current_procinfo.procdef.localst.SymList.ForEachCall(@sym_maybe_initialize,@stat);
  470. end;
  471. end;
  472. class procedure tnodeutils.procdef_block_add_implicit_finalize_nodes(pd: tprocdef; var stat: tstatementnode);
  473. begin
  474. { no finalization in exceptfilters, they /are/ the finalization code }
  475. if current_procinfo.procdef.proctypeoption=potype_exceptfilter then
  476. exit;
  477. { finalize local data like ansistrings}
  478. case current_procinfo.procdef.proctypeoption of
  479. potype_unitfinalize:
  480. begin
  481. { insert class destructors }
  482. if mf_classinits in current_module.moduleflags then
  483. append_struct_initfinis(current_module, potype_class_destructor, stat);
  484. { this is also used for initialization of variables in a
  485. program which does not have a globalsymtable }
  486. if assigned(current_module.globalsymtable) then
  487. TSymtable(current_module.globalsymtable).SymList.ForEachCall(@static_syms_finalize,@stat);
  488. TSymtable(current_module.localsymtable).SymList.ForEachCall(@static_syms_finalize,@stat);
  489. end;
  490. { units/progs have separate code for initialization and finalization }
  491. potype_unitinit: ;
  492. { program init/final is generated in separate procedure }
  493. potype_proginit: ;
  494. else
  495. current_procinfo.procdef.localst.SymList.ForEachCall(@local_varsyms_finalize,@stat);
  496. end;
  497. end;
  498. class function tnodeutils.force_init: boolean;
  499. begin
  500. result:=
  501. (target_info.system in systems_typed_constants_node_init) and
  502. assigned(current_module.tcinitcode);
  503. end;
  504. class function tnodeutils.force_final: boolean;
  505. begin
  506. result:=false;
  507. end;
  508. class procedure tnodeutils.initialize_filerecs(p:TObject;statn:pointer);
  509. var
  510. stat: ^tstatementnode absolute statn;
  511. begin
  512. if (tsym(p).typ=staticvarsym) and
  513. (tstaticvarsym(p).vardef.typ=filedef) and
  514. (tstaticvarsym(p).isoindex<>0) then
  515. case tfiledef(tstaticvarsym(p).vardef).filetyp of
  516. ft_text:
  517. begin
  518. if cs_transparent_file_names in current_settings.globalswitches then
  519. addstatement(stat^,ccallnode.createintern('fpc_textinit_filename_iso',
  520. ccallparanode.create(
  521. cstringconstnode.createstr(tstaticvarsym(p).Name),
  522. ccallparanode.create(
  523. cordconstnode.create(tstaticvarsym(p).isoindex,uinttype,false),
  524. ccallparanode.create(
  525. cloadnode.create(tstaticvarsym(p),tstaticvarsym(p).Owner),
  526. nil)))))
  527. else
  528. addstatement(stat^,ccallnode.createintern('fpc_textinit_iso',
  529. ccallparanode.create(
  530. cordconstnode.create(tstaticvarsym(p).isoindex,uinttype,false),
  531. ccallparanode.create(
  532. cloadnode.create(tstaticvarsym(p),tstaticvarsym(p).Owner),
  533. nil))));
  534. end;
  535. ft_typed:
  536. begin
  537. if cs_transparent_file_names in current_settings.globalswitches then
  538. addstatement(stat^,ccallnode.createintern('fpc_typedfile_init_filename_iso',
  539. ccallparanode.create(
  540. cstringconstnode.createstr(tstaticvarsym(p).Name),
  541. ccallparanode.create(
  542. cordconstnode.create(tstaticvarsym(p).isoindex,uinttype,false),
  543. ccallparanode.create(
  544. cloadnode.create(tstaticvarsym(p),tstaticvarsym(p).Owner),
  545. nil)))))
  546. else
  547. addstatement(stat^,ccallnode.createintern('fpc_typedfile_init_iso',
  548. ccallparanode.create(
  549. cordconstnode.create(tstaticvarsym(p).isoindex,uinttype,false),
  550. ccallparanode.create(
  551. cloadnode.create(tstaticvarsym(p),tstaticvarsym(p).Owner),
  552. nil))));
  553. end;
  554. else
  555. ;
  556. end;
  557. end;
  558. class procedure tnodeutils.finalize_filerecs(p:TObject;statn:pointer);
  559. var
  560. stat: ^tstatementnode absolute statn;
  561. begin
  562. if (tsym(p).typ=staticvarsym) and
  563. (tstaticvarsym(p).vardef.typ=filedef) and
  564. (tstaticvarsym(p).isoindex<>0) then
  565. case tfiledef(tstaticvarsym(p).vardef).filetyp of
  566. ft_text:
  567. begin
  568. addstatement(stat^,ccallnode.createintern('fpc_textclose_iso',
  569. ccallparanode.create(
  570. cloadnode.create(tstaticvarsym(p),tstaticvarsym(p).Owner),
  571. nil)));
  572. end;
  573. ft_typed:
  574. begin
  575. addstatement(stat^,ccallnode.createintern('fpc_typedfile_close_iso',
  576. ccallparanode.create(
  577. cloadnode.create(tstaticvarsym(p),tstaticvarsym(p).Owner),
  578. nil)));
  579. end;
  580. else
  581. ;
  582. end;
  583. end;
  584. class procedure tnodeutils.load_parentfpstruct_nested_funcret(ressym: tsym; var stat: tstatementnode);
  585. var
  586. target: tnode;
  587. begin
  588. target:=cloadnode.create(ressym, ressym.owner);
  589. { ensure the target of this assignment doesn't translate the
  590. funcretsym also to its alias in the parentfpstruct }
  591. include(target.flags, nf_internal);
  592. addstatement(stat,
  593. cassignmentnode.create(
  594. target, cloadnode.create(ressym, ressym.owner)
  595. )
  596. );
  597. end;
  598. class function tnodeutils.wrap_proc_body(pd: tprocdef; n: tnode): tnode;
  599. var
  600. stat: tstatementnode;
  601. block: tnode;
  602. ressym,
  603. psym: tsym;
  604. resdef: tdef;
  605. begin
  606. result:=maybe_insert_trashing(pd,n);
  607. { initialise safecall result variable }
  608. if pd.generate_safecall_wrapper then
  609. begin
  610. ressym:=tsym(pd.localst.Find('safecallresult'));
  611. block:=internalstatements(stat);
  612. addstatement(stat,
  613. cassignmentnode.create(
  614. cloadnode.create(ressym,ressym.owner),
  615. genintconstnode(0)
  616. )
  617. );
  618. addstatement(stat,result);
  619. result:=block;
  620. end;
  621. if (m_isolike_program_para in current_settings.modeswitches) and
  622. (pd.proctypeoption=potype_proginit) then
  623. begin
  624. block:=internalstatements(stat);
  625. pd.localst.SymList.ForEachCall(@initialize_filerecs,@stat);
  626. addstatement(stat,result);
  627. pd.localst.SymList.ForEachCall(@finalize_filerecs,@stat);
  628. result:=block;
  629. end;
  630. if target_info.system in systems_typed_constants_node_init then
  631. begin
  632. case pd.proctypeoption of
  633. potype_class_constructor:
  634. begin
  635. { even though the initialisation code for typed constants may
  636. not yet be complete at this point (there may be more inside
  637. method definitions coming after this class constructor), the
  638. ones from inside the class definition have already been parsed.
  639. in case of $j-, these are marked "final" in Java and such
  640. static fields must be initialsed in the class constructor
  641. itself -> add them here }
  642. block:=internalstatements(stat);
  643. if assigned(pd.struct.tcinitcode) then
  644. begin
  645. addstatement(stat,pd.struct.tcinitcode);
  646. pd.struct.tcinitcode:=nil;
  647. end;
  648. psym:=tsym(pd.struct.symtable.find('FPC_INIT_TYPED_CONSTS_HELPER'));
  649. if assigned(psym) then
  650. begin
  651. if (psym.typ<>procsym) or
  652. (tprocsym(psym).procdeflist.count<>1) then
  653. internalerror(2011040301);
  654. addstatement(stat,ccallnode.create(nil,tprocsym(psym),
  655. pd.struct.symtable,nil,[],nil));
  656. end;
  657. addstatement(stat,result);
  658. result:=block
  659. end;
  660. potype_unitinit:
  661. begin
  662. if assigned(current_module.tcinitcode) then
  663. begin
  664. block:=internalstatements(stat);
  665. addstatement(stat,tnode(current_module.tcinitcode));
  666. current_module.tcinitcode:=nil;
  667. addstatement(stat,result);
  668. result:=block;
  669. end;
  670. end;
  671. else case pd.synthetickind of
  672. tsk_tcinit:
  673. begin
  674. if assigned(pd.struct.tcinitcode) then
  675. begin
  676. block:=internalstatements(stat);
  677. addstatement(stat,pd.struct.tcinitcode);
  678. pd.struct.tcinitcode:=nil;
  679. addstatement(stat,result);
  680. result:=block
  681. end
  682. end;
  683. else
  684. ;
  685. end;
  686. end;
  687. end;
  688. if (target_info.system in systems_fpnestedstruct) and
  689. pd.get_funcretsym_info(ressym,resdef) and
  690. (tabstractnormalvarsym(ressym).inparentfpstruct) then
  691. begin
  692. block:=internalstatements(stat);
  693. addstatement(stat,result);
  694. load_parentfpstruct_nested_funcret(ressym,stat);
  695. result:=block;
  696. end;
  697. end;
  698. class function tnodeutils.maybe_insert_trashing(pd: tprocdef; n: tnode): tnode;
  699. var
  700. stat: tstatementnode;
  701. begin
  702. result:=n;
  703. if check_insert_trashing(pd) then
  704. begin
  705. result:=internalstatements(stat);
  706. pd.parast.SymList.ForEachCall(@maybe_trash_variable_callback,@stat);
  707. pd.localst.SymList.ForEachCall(@maybe_trash_variable_callback,@stat);
  708. addstatement(stat,n);
  709. end;
  710. end;
  711. class function tnodeutils.check_insert_trashing(pd: tprocdef): boolean;
  712. begin
  713. result:=
  714. (localvartrashing<>-1) and
  715. not(po_assembler in pd.procoptions);
  716. end;
  717. class function tnodeutils.trashable_sym(p: tsym): boolean;
  718. begin
  719. result:=
  720. ((p.typ=localvarsym) or
  721. ((p.typ=paravarsym) and
  722. ((vo_is_funcret in tabstractnormalvarsym(p).varoptions) or
  723. (tabstractnormalvarsym(p).varspez=vs_out)))) and
  724. not (vo_is_default_var in tabstractnormalvarsym(p).varoptions) and
  725. (not is_managed_type(tabstractnormalvarsym(p).vardef) or
  726. (is_string(tabstractnormalvarsym(p).vardef) and
  727. (vo_is_funcret in tabstractnormalvarsym(p).varoptions)
  728. )
  729. ) and
  730. (tabstractnormalvarsym(p).varoptions*[vo_is_parentfp,vo_is_internal]=[]) and
  731. not assigned(tabstractnormalvarsym(p).defaultconstsym);
  732. end;
  733. class procedure tnodeutils.maybe_trash_variable(var stat: tstatementnode; p: tabstractnormalvarsym; trashn: tnode);
  734. var
  735. size: asizeint;
  736. trashintval: int64;
  737. stringres: tstringconstnode;
  738. begin
  739. if trashable_sym(p) then
  740. begin
  741. trashintval:=trashintvalues[localvartrashing];
  742. if (p.vardef.typ=procvardef) and
  743. ([m_tp_procvar,m_mac_procvar]*current_settings.modeswitches<>[]) then
  744. begin
  745. if tprocvardef(p.vardef).is_addressonly then
  746. { in tp/delphi mode, you need @procvar to get at the contents of
  747. a procvar ... }
  748. trashn:=caddrnode.create(trashn)
  749. else
  750. { ... but if it's a procedure of object, that will only return
  751. the procedure address -> cast to tmethod instead }
  752. trashn:=ctypeconvnode.create_explicit(trashn,methodpointertype);
  753. end;
  754. if is_managed_type(p.vardef) then
  755. begin
  756. if is_string(p.vardef) then
  757. begin
  758. stringres:=
  759. cstringconstnode.createstr(
  760. 'uninitialized function result in '+
  761. tprocdef(p.owner.defowner).customprocname([pno_proctypeoption, pno_paranames,pno_ownername, pno_noclassmarker])
  762. );
  763. { prevent attempts to convert the string to the specified
  764. code page at compile time, as it may not be available (and
  765. it does not matter) }
  766. if is_ansistring(p.vardef) then
  767. stringres.changestringtype(search_system_type('RAWBYTESTRING').typedef);
  768. trash_small(stat,trashn,stringres);
  769. end
  770. else
  771. internalerror(2016030601);
  772. end
  773. else if ((p.typ=localvarsym) and
  774. (not(vo_is_funcret in p.varoptions) or
  775. not is_shortstring(p.vardef))) or
  776. ((p.typ=paravarsym) and
  777. not is_shortstring(p.vardef)) then
  778. begin
  779. size:=p.getsize;
  780. case size of
  781. 0:
  782. begin
  783. { open array -> at least size 1. Can also be zero-sized
  784. record, so check it's actually an array }
  785. if p.vardef.typ=arraydef then
  786. trash_large(stat,trashn,caddnode.create(addn,cinlinenode.create(in_high_x,false,trashn.getcopy),genintconstnode(1)),trashintval)
  787. else
  788. trashn.free;
  789. end;
  790. 1: trash_small(stat,
  791. ctypeconvnode.create_internal(trashn,s8inttype),
  792. genintconstnode(shortint(trashintval)));
  793. 2: trash_small(stat,
  794. ctypeconvnode.create_internal(trashn,s16inttype),
  795. genintconstnode(smallint(trashintval)));
  796. 4: trash_small(stat,
  797. ctypeconvnode.create_internal(trashn,s32inttype),
  798. genintconstnode(longint(trashintval)));
  799. 8: trash_small(stat,
  800. ctypeconvnode.create_internal(trashn,s64inttype),
  801. genintconstnode(int64(trashintval)));
  802. else
  803. trash_large(stat,trashn,genintconstnode(size),trashintval);
  804. end;
  805. end
  806. else
  807. begin
  808. { may be an open string, even if is_open_string() returns false
  809. (for some helpers in the system unit) }
  810. { an open string has at least size 2 }
  811. trash_small(stat,
  812. cvecnode.create(trashn.getcopy,genintconstnode(0)),
  813. cordconstnode.create(tconstexprint(byte(trashintval)),cansichartype,false));
  814. trash_small(stat,
  815. cvecnode.create(trashn,genintconstnode(1)),
  816. cordconstnode.create(tconstexprint(byte(trashintval)),cansichartype,false));
  817. end;
  818. end
  819. else
  820. trashn.free;
  821. end;
  822. class procedure tnodeutils.maybe_trash_variable_callback(p:TObject;statn:pointer);
  823. var
  824. stat: ^tstatementnode absolute statn;
  825. begin
  826. if not(tsym(p).typ in [localvarsym,paravarsym]) then
  827. exit;
  828. if sp_internal in tsym(p).symoptions then
  829. exit;
  830. maybe_trash_variable(stat^,tabstractnormalvarsym(p),cloadnode.create(tsym(p),tsym(p).owner));
  831. end;
  832. class procedure tnodeutils.trash_small(var stat: tstatementnode; trashn: tnode; trashvaln: tnode);
  833. begin
  834. addstatement(stat,cassignmentnode.create(trashn,trashvaln));
  835. end;
  836. class procedure tnodeutils.trash_large(var stat: tstatementnode; trashn, sizen: tnode; trashintval: int64);
  837. begin
  838. addstatement(stat,ccallnode.createintern('fpc_fillmem',
  839. ccallparanode.Create(cordconstnode.create(tconstexprint(byte(trashintval)),u8inttype,false),
  840. ccallparanode.Create(sizen,
  841. ccallparanode.Create(trashn,nil)))
  842. ));
  843. end;
  844. class procedure tnodeutils.insertbsssym(list: tasmlist; sym: tstaticvarsym; size: asizeint; varalign: shortint; _typ:Tasmsymtype);
  845. begin
  846. if sym.globalasmsym then
  847. begin
  848. { on AIX/stabx, we cannot generate debug information that encodes
  849. the address of a global symbol, you need a symbol with the same
  850. name as the identifier -> create an extra *local* symbol.
  851. Moreover, such a local symbol will be removed if it's not
  852. referenced anywhere, so also create a reference }
  853. if (target_dbg.id=dbg_stabx) and
  854. (cs_debuginfo in current_settings.moduleswitches) and
  855. not assigned(current_asmdata.GetAsmSymbol(sym.name)) then
  856. begin
  857. list.concat(tai_symbol.Create(current_asmdata.DefineAsmSymbol(sym.name,AB_LOCAL,AT_DATA,sym.vardef),0));
  858. list.concat(tai_directive.Create(asd_reference,sym.name));
  859. end;
  860. list.concat(Tai_datablock.create_global(sym.mangledname,size,sym.vardef,_typ));
  861. end
  862. else
  863. list.concat(Tai_datablock.create_hidden(sym.mangledname,size,sym.vardef,_typ));
  864. end;
  865. class procedure tnodeutils.insertbssdata(sym: tstaticvarsym);
  866. var
  867. l : asizeint;
  868. varalign : shortint;
  869. storefilepos : tfileposinfo;
  870. list : TAsmList;
  871. sectype : TAsmSectiontype;
  872. asmtype: TAsmsymtype;
  873. begin
  874. storefilepos:=current_filepos;
  875. current_filepos:=sym.fileinfo;
  876. l:=sym.getsize;
  877. varalign:=sym.vardef.alignment;
  878. if (varalign=0) then
  879. varalign:=var_align_size(l)
  880. else
  881. varalign:=var_align(varalign);
  882. asmtype:=AT_DATA;
  883. if tf_section_threadvars in target_info.flags then
  884. begin
  885. if (vo_is_thread_var in sym.varoptions) then
  886. begin
  887. list:=current_asmdata.asmlists[al_threadvars];
  888. sectype:=sec_threadvar;
  889. asmtype:=AT_TLS;
  890. end
  891. else
  892. begin
  893. list:=current_asmdata.asmlists[al_globals];
  894. sectype:=sec_bss;
  895. end;
  896. end
  897. else
  898. begin
  899. if (vo_is_thread_var in sym.varoptions) then
  900. begin
  901. inc(l,sizeof(pint));
  902. { it doesn't help to set a higher alignment, as }
  903. { the first sizeof(pint) bytes field will offset }
  904. { everything anyway }
  905. varalign:=sizeof(pint);
  906. end;
  907. list:=current_asmdata.asmlists[al_globals];
  908. sectype:=sec_bss;
  909. end;
  910. maybe_new_object_file(list);
  911. if vo_has_section in sym.varoptions then
  912. new_section(list,sec_user,sym.section,varalign)
  913. else
  914. new_section(list,sectype,lower(sym.mangledname),varalign);
  915. insertbsssym(list,sym,l,varalign,asmtype);
  916. current_filepos:=storefilepos;
  917. end;
  918. class function tnodeutils.create_main_procdef(const name: string; potype: tproctypeoption; ps: tprocsym): tdef;
  919. var
  920. pd: tprocdef;
  921. begin
  922. if potype<>potype_mainstub then
  923. pd:=cprocdef.create(main_program_level,true)
  924. else
  925. pd:=cprocdef.create(normal_function_level,true);
  926. { always register the def }
  927. pd.register_def;
  928. pd.procsym:=ps;
  929. ps.ProcdefList.Add(pd);
  930. include(pd.procoptions,po_global);
  931. { set procdef options }
  932. pd.proctypeoption:=potype;
  933. pd.proccalloption:=pocall_default;
  934. include(pd.procoptions,po_hascallingconvention);
  935. pd.forwarddef:=false;
  936. { may be required to calculate the mangled name }
  937. add_main_procdef_paras(pd);
  938. pd.setmangledname(name);
  939. { the mainstub is generated via a synthetic proc -> parsed via
  940. psub.read_proc_body() -> that one will insert the mangled name in the
  941. alias names already }
  942. if not(potype in [potype_mainstub,potype_libmainstub]) then
  943. pd.aliasnames.insert(pd.mangledname);
  944. result:=pd;
  945. end;
  946. class function tnodeutils.get_init_final_list(main : tmodule):tfplist;
  947. procedure addusedunits(m : tmodule);
  948. var
  949. hp : tused_unit;
  950. entry : pinitfinalentry;
  951. begin
  952. hp:=tused_unit(m.used_units.first);
  953. while assigned(hp) do
  954. begin
  955. if (not hp.u.initfinalchecked) then
  956. begin
  957. hp.u.initfinalchecked:=True;
  958. addusedunits(hp.u);
  959. if ((hp.u.moduleflags * [mf_init,mf_finalize])<>[]) then
  960. begin
  961. new(entry);
  962. entry^.module:=hp.u;
  963. entry^.initpd:=nil;
  964. entry^.finipd:=nil;
  965. if mf_init in hp.u.moduleflags then
  966. entry^.initfunc:=make_mangledname('INIT$',hp.u.globalsymtable,'')
  967. else
  968. entry^.initfunc:='';
  969. if mf_finalize in hp.u.moduleflags then
  970. entry^.finifunc:=make_mangledname('FINALIZE$',hp.u.globalsymtable,'')
  971. else
  972. entry^.finifunc:='';
  973. result.add(entry);
  974. end;
  975. end;
  976. hp:=tused_unit(hp.next);
  977. end;
  978. end;
  979. var
  980. entry : pinitfinalentry;
  981. begin
  982. result:=tfplist.create;
  983. { Insert initialization/finalization of the used units }
  984. addusedunits(main);
  985. { Insert initialization/finalization of the program }
  986. if (main.moduleflags * [mf_init,mf_finalize])<>[] then
  987. begin
  988. new(entry);
  989. entry^.module:=current_module;
  990. entry^.initpd:=nil;
  991. entry^.finipd:=nil;
  992. if mf_init in current_module.moduleflags then
  993. entry^.initfunc:=make_mangledname('INIT$',current_module.localsymtable,'')
  994. else
  995. entry^.initfunc:='';
  996. if mf_finalize in current_module.moduleflags then
  997. entry^.finifunc:=make_mangledname('FINALIZE$',current_module.localsymtable,'')
  998. else
  999. entry^.finifunc:='';
  1000. result.add(entry);
  1001. end;
  1002. end;
  1003. class procedure tnodeutils.release_init_final_list(list:tfplist);
  1004. var
  1005. i : longint;
  1006. begin
  1007. if not assigned(list) then
  1008. internalerror(2017051901);
  1009. for i:=0 to list.count-1 do
  1010. dispose(pinitfinalentry(list[i]));
  1011. list.free;
  1012. end;
  1013. class procedure tnodeutils.InsertInitFinalTable(main : tmodule);
  1014. var
  1015. entries : tfplist;
  1016. begin
  1017. entries := get_init_final_list(main);
  1018. insert_init_final_table(main,entries);
  1019. release_init_final_list(entries);
  1020. end;
  1021. class procedure tnodeutils.insert_init_final_table(main : tmodule; entries:tfplist);
  1022. var
  1023. i : longint;
  1024. unitinits : ttai_typedconstbuilder;
  1025. nameinit,namefini : TSymStr;
  1026. tabledef: tdef;
  1027. entry : pinitfinalentry;
  1028. procedure add_initfinal_import(symtable:tsymtable);
  1029. var
  1030. i,j : longint;
  1031. foundinit,foundfini : boolean;
  1032. sym : TSymEntry;
  1033. pd : tprocdef;
  1034. begin
  1035. if (nameinit='') and (namefini='') then
  1036. exit;
  1037. foundinit:=nameinit='';
  1038. foundfini:=namefini='';
  1039. for i:=0 to symtable.SymList.Count-1 do
  1040. begin
  1041. sym:=tsymentry(symtable.SymList[i]);
  1042. if sym.typ<>procsym then
  1043. continue;
  1044. for j:=0 to tprocsym(sym).procdeflist.count-1 do
  1045. begin
  1046. pd:=tprocdef(tprocsym(sym).procdeflist[j]);
  1047. if (nameinit<>'') and not foundinit and pd.has_alias_name(nameinit) then
  1048. begin
  1049. current_module.addimportedsym(sym);
  1050. foundinit:=true;
  1051. end;
  1052. if (namefini<>'') and not foundfini and pd.has_alias_name(namefini) then
  1053. begin
  1054. current_module.addimportedsym(sym);
  1055. foundfini:=true;
  1056. end;
  1057. if foundinit and foundfini then
  1058. break;
  1059. end;
  1060. if foundinit and foundfini then
  1061. break;
  1062. end;
  1063. if not foundinit or not foundfini then
  1064. internalerror(2016041401);
  1065. end;
  1066. begin
  1067. unitinits:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable,tcalo_new_section]);
  1068. unitinits.begin_anonymous_record('',default_settings.packrecords,sizeof(pint),
  1069. targetinfos[target_info.system]^.alignment.recordalignmin);
  1070. { tablecount }
  1071. unitinits.emit_ord_const(entries.count,aluuinttype);
  1072. { initcount (initialised at run time }
  1073. unitinits.emit_ord_const(0,aluuinttype);
  1074. for i:=0 to entries.count-1 do
  1075. begin
  1076. entry:=pinitfinalentry(entries[i]);
  1077. if assigned(entry^.initpd) or assigned(entry^.finipd) then
  1078. begin
  1079. if assigned(entry^.initpd) then
  1080. begin
  1081. unitinits.emit_procdef_const(entry^.initpd);
  1082. if entry^.module<>current_module then
  1083. current_module.addimportedsym(entry^.initpd.procsym);
  1084. end
  1085. else
  1086. unitinits.emit_tai(Tai_const.Create_nil_codeptr,voidcodepointertype);
  1087. if assigned(entry^.finipd) then
  1088. begin
  1089. unitinits.emit_procdef_const(entry^.finipd);
  1090. if entry^.module<>current_module then
  1091. current_module.addimportedsym(entry^.finipd.procsym);
  1092. end
  1093. else
  1094. unitinits.emit_tai(Tai_const.Create_nil_codeptr,voidcodepointertype);
  1095. end
  1096. else
  1097. begin
  1098. nameinit:='';
  1099. namefini:='';
  1100. if entry^.initfunc<>'' then
  1101. begin
  1102. nameinit:=entry^.initfunc;
  1103. unitinits.emit_tai(
  1104. Tai_const.Createname(nameinit,AT_FUNCTION,0),
  1105. voidcodepointertype);
  1106. end
  1107. else
  1108. unitinits.emit_tai(Tai_const.Create_nil_codeptr,voidcodepointertype);
  1109. if entry^.finifunc<>'' then
  1110. begin
  1111. namefini:=entry^.finifunc;
  1112. unitinits.emit_tai(
  1113. Tai_const.Createname(namefini,AT_FUNCTION,0),
  1114. voidcodepointertype);
  1115. end
  1116. else
  1117. unitinits.emit_tai(Tai_const.Create_nil_codeptr,voidcodepointertype);
  1118. if entry^.module<>current_module then
  1119. add_initfinal_import(entry^.module.localsymtable);
  1120. end;
  1121. end;
  1122. { Add to data segment }
  1123. tabledef:=unitinits.end_anonymous_record;
  1124. current_asmdata.asmlists[al_globals].concatlist(
  1125. unitinits.get_final_asmlist(
  1126. current_asmdata.DefineAsmSymbol('INITFINAL',AB_GLOBAL,AT_DATA,tabledef),
  1127. tabledef,
  1128. sec_data,'INITFINAL',const_align(sizeof(pint))
  1129. )
  1130. );
  1131. unitinits.free;
  1132. end;
  1133. class function tnodeutils.has_init_list: boolean;
  1134. var
  1135. hp : tused_unit;
  1136. begin
  1137. Result:=True;
  1138. { Check current module first }
  1139. if mf_init in current_module.moduleflags then
  1140. Exit;
  1141. { Check used units }
  1142. hp:=tused_unit(usedunits.first);
  1143. while assigned(hp) do
  1144. begin
  1145. if mf_init in hp.u.moduleflags then
  1146. Exit;
  1147. hp:=tused_unit(hp.next);
  1148. end;
  1149. { We reach this point, none of the used units have an initialisation list }
  1150. Result:=False;
  1151. end;
  1152. class procedure tnodeutils.InsertThreadvarTablesTable;
  1153. var
  1154. hp : tused_unit;
  1155. tcb: ttai_typedconstbuilder;
  1156. count: longint;
  1157. sym: tasmsymbol;
  1158. placeholder: ttypedconstplaceholder;
  1159. tabledef: tdef;
  1160. begin
  1161. if (tf_section_threadvars in target_info.flags) then
  1162. exit;
  1163. count:=0;
  1164. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable,tcalo_new_section]);
  1165. tcb.begin_anonymous_record('',default_settings.packrecords,voidpointertype.alignment,targetinfos[target_info.system]^.alignment.recordalignmin);
  1166. placeholder:=tcb.emit_placeholder(u32inttype);
  1167. hp:=tused_unit(usedunits.first);
  1168. while assigned(hp) do
  1169. begin
  1170. if mf_threadvars in hp.u.moduleflags then
  1171. begin
  1172. sym:=current_asmdata.RefAsmSymbol(make_mangledname('THREADVARLIST',hp.u.globalsymtable,''),AT_DATA,true);
  1173. tcb.emit_tai(
  1174. tai_const.Create_sym(sym),
  1175. voidpointertype);
  1176. current_module.add_extern_asmsym(sym);
  1177. inc(count);
  1178. end;
  1179. hp:=tused_unit(hp.next);
  1180. end;
  1181. { Add program threadvars, if any }
  1182. if mf_threadvars in current_module.moduleflags then
  1183. begin
  1184. sym:=current_asmdata.RefAsmSymbol(make_mangledname('THREADVARLIST',current_module.localsymtable,''),AT_DATA,true);
  1185. tcb.emit_tai(
  1186. Tai_const.Create_sym(sym),
  1187. voidpointertype);
  1188. inc(count);
  1189. end;
  1190. { set the count at the start }
  1191. placeholder.replace(tai_const.Create_32bit(count),u32inttype);
  1192. placeholder.free;
  1193. { insert in data segment }
  1194. tabledef:=tcb.end_anonymous_record;
  1195. sym:=current_asmdata.DefineAsmSymbol('FPC_THREADVARTABLES',AB_GLOBAL,AT_DATA,tabledef);
  1196. current_asmdata.asmlists[al_globals].concatlist(
  1197. tcb.get_final_asmlist(
  1198. sym,tabledef,sec_data,'FPC_THREADVARTABLES',const_align(sizeof(pint))
  1199. )
  1200. );
  1201. tcb.free;
  1202. end;
  1203. procedure AddToThreadvarList(p:TObject;arg:pointer);
  1204. var
  1205. tcb: ttai_typedconstbuilder;
  1206. field1, field2: tsym;
  1207. begin
  1208. if (tsym(p).typ=staticvarsym) and
  1209. (vo_is_thread_var in tstaticvarsym(p).varoptions) then
  1210. begin
  1211. tcb:=ttai_typedconstbuilder(arg);
  1212. { address of threadvar }
  1213. tcb.emit_tai(tai_const.Createname(tstaticvarsym(p).mangledname,0),
  1214. cpointerdef.getreusable(
  1215. get_threadvar_record(tstaticvarsym(p).vardef,field1,field2)
  1216. )
  1217. );
  1218. { size of threadvar }
  1219. tcb.emit_ord_const(tstaticvarsym(p).getsize,u32inttype);
  1220. end;
  1221. end;
  1222. class procedure tnodeutils.InsertThreadvars;
  1223. var
  1224. s : TSymStr;
  1225. tcb: ttai_typedconstbuilder;
  1226. sym: tasmsymbol;
  1227. tabledef: trecorddef;
  1228. add : boolean;
  1229. begin
  1230. if (tf_section_threadvars in target_info.flags) then
  1231. exit;
  1232. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable,tcalo_new_section]);
  1233. tabledef:=tcb.begin_anonymous_record('',default_settings.packrecords,voidpointertype.alignment,targetinfos[target_info.system]^.alignment.recordalignmin);
  1234. if assigned(current_module.globalsymtable) then
  1235. current_module.globalsymtable.SymList.ForEachCall(@AddToThreadvarList,tcb);
  1236. current_module.localsymtable.SymList.ForEachCall(@AddToThreadvarList,tcb);
  1237. if trecordsymtable(tabledef.symtable).datasize<>0 then
  1238. { terminator }
  1239. tcb.emit_tai(tai_const.Create_nil_dataptr,voidpointertype);
  1240. tcb.end_anonymous_record;
  1241. add:=trecordsymtable(tabledef.symtable).datasize<>0;
  1242. if add then
  1243. begin
  1244. s:=make_mangledname('THREADVARLIST',current_module.localsymtable,'');
  1245. sym:=current_asmdata.DefineAsmSymbol(s,AB_GLOBAL,AT_DATA_FORCEINDIRECT,tabledef);
  1246. current_asmdata.asmlists[al_globals].concatlist(
  1247. tcb.get_final_asmlist(sym,tabledef,sec_data,s,const_align(sizeof(pint))));
  1248. include(current_module.moduleflags,mf_threadvars);
  1249. current_module.add_public_asmsym(sym);
  1250. end;
  1251. tcb.Free;
  1252. end;
  1253. class procedure tnodeutils.InsertRuntimeInitsTablesTable(const prefix,tablename:string;unitflag:tmoduleflag);
  1254. var
  1255. hp: tused_unit;
  1256. tcb: ttai_typedconstbuilder;
  1257. countplaceholder: ttypedconstplaceholder;
  1258. tabledef: tdef;
  1259. count: longint;
  1260. begin
  1261. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable,tcalo_new_section]);
  1262. tcb.begin_anonymous_record('',default_settings.packrecords,sizeof(pint),
  1263. targetinfos[target_info.system]^.alignment.recordalignmin
  1264. );
  1265. { placeholder for the count }
  1266. countplaceholder:=tcb.emit_placeholder(sizesinttype);
  1267. count:=0;
  1268. hp:=tused_unit(usedunits.first);
  1269. while assigned(hp) do
  1270. begin
  1271. if unitflag in hp.u.moduleflags then
  1272. begin
  1273. tcb.emit_tai(
  1274. Tai_const.Createname(make_mangledname(prefix,hp.u.globalsymtable,''),0),
  1275. voidcodepointertype);
  1276. inc(count);
  1277. end;
  1278. hp:=tused_unit(hp.next);
  1279. end;
  1280. { Add items from program, if any }
  1281. if unitflag in current_module.moduleflags then
  1282. begin
  1283. tcb.emit_tai(
  1284. Tai_const.Createname(make_mangledname(prefix,current_module.localsymtable,''),0),
  1285. voidcodepointertype);
  1286. inc(count);
  1287. end;
  1288. { Insert TableCount at start }
  1289. countplaceholder.replace(Tai_const.Create_sizeint(count),sizesinttype);
  1290. countplaceholder.free;
  1291. { insert in data segment }
  1292. tabledef:=tcb.end_anonymous_record;
  1293. current_asmdata.asmlists[al_globals].concatlist(
  1294. tcb.get_final_asmlist(
  1295. current_asmdata.DefineAsmSymbol(tablename,AB_GLOBAL,AT_DATA,tabledef),
  1296. tabledef,
  1297. sec_data,tablename,const_align(sizeof(pint))
  1298. )
  1299. );
  1300. tcb.free;
  1301. end;
  1302. class procedure tnodeutils.InsertRuntimeInits(const prefix:string;list:TLinkedList;unitflag:tmoduleflag);
  1303. var
  1304. s: string;
  1305. item: TTCInitItem;
  1306. tcb: ttai_typedconstbuilder;
  1307. rawdatadef: tdef;
  1308. begin
  1309. item:=TTCInitItem(list.First);
  1310. if item=nil then
  1311. exit;
  1312. s:=make_mangledname(prefix,current_module.localsymtable,'');
  1313. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable,tcalo_new_section]);
  1314. tcb.begin_anonymous_record('',default_settings.packrecords,sizeof(pint),
  1315. targetinfos[target_info.system]^.alignment.recordalignmin);
  1316. repeat
  1317. { optimize away unused local/static symbols }
  1318. if (item.sym.refs>0) or (item.sym.owner.symtabletype=globalsymtable) then
  1319. begin
  1320. { address to initialize }
  1321. tcb.queue_init(voidpointertype);
  1322. rawdatadef:=carraydef.getreusable(cansichartype,tstaticvarsym(item.sym).vardef.size);
  1323. tcb.queue_vecn(rawdatadef,item.offset);
  1324. tcb.queue_typeconvn(cpointerdef.getreusable(tstaticvarsym(item.sym).vardef),cpointerdef.getreusable(rawdatadef));
  1325. tcb.queue_emit_staticvar(tstaticvarsym(item.sym));
  1326. { value with which to initialize }
  1327. tcb.emit_tai(Tai_const.Create_sym(item.datalabel),item.datadef)
  1328. end;
  1329. item:=TTCInitItem(item.Next);
  1330. until item=nil;
  1331. { end-of-list marker }
  1332. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1333. rawdatadef:=tcb.end_anonymous_record;
  1334. current_asmdata.asmlists[al_globals].concatList(
  1335. tcb.get_final_asmlist(
  1336. current_asmdata.DefineAsmSymbol(s,AB_GLOBAL,AT_DATA,rawdatadef),
  1337. rawdatadef,sec_data,s,const_align(sizeof(pint))));
  1338. tcb.free;
  1339. include(current_module.moduleflags,unitflag);
  1340. end;
  1341. class procedure tnodeutils.InsertWideInits;
  1342. begin
  1343. InsertRuntimeInits('WIDEINITS',current_asmdata.WideInits,mf_wideinits);
  1344. end;
  1345. class procedure tnodeutils.InsertResStrInits;
  1346. begin
  1347. InsertRuntimeInits('RESSTRINITS',current_asmdata.ResStrInits,mf_resstrinits);
  1348. end;
  1349. class procedure tnodeutils.InsertWideInitsTablesTable;
  1350. begin
  1351. InsertRuntimeInitsTablesTable('WIDEINITS','FPC_WIDEINITTABLES',mf_wideinits);
  1352. end;
  1353. class procedure tnodeutils.InsertResStrTablesTable;
  1354. begin
  1355. InsertRuntimeInitsTablesTable('RESSTRINITS','FPC_RESSTRINITTABLES',mf_resstrinits);
  1356. end;
  1357. class procedure tnodeutils.InsertResourceTablesTable;
  1358. var
  1359. hp : tmodule;
  1360. count : longint;
  1361. tcb : ttai_typedconstbuilder;
  1362. countplaceholder : ttypedconstplaceholder;
  1363. tabledef: tdef;
  1364. begin
  1365. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable,tcalo_new_section]);
  1366. count:=0;
  1367. hp:=tmodule(loaded_units.first);
  1368. tcb.begin_anonymous_record('',default_settings.packrecords,sizeof(pint),
  1369. targetinfos[target_info.system]^.alignment.recordalignmin);
  1370. countplaceholder:=tcb.emit_placeholder(sizesinttype);
  1371. while assigned(hp) do
  1372. begin
  1373. if mf_has_resourcestrings in hp.moduleflags then
  1374. begin
  1375. tcb.emit_tai(Tai_const.Create_sym(
  1376. ctai_typedconstbuilder.get_vectorized_dead_strip_section_symbol_start('RESSTR',hp.localsymtable,[tcdssso_register_asmsym,tcdssso_use_indirect])),
  1377. voidpointertype
  1378. );
  1379. tcb.emit_tai(Tai_const.Create_sym(
  1380. ctai_typedconstbuilder.get_vectorized_dead_strip_section_symbol_end('RESSTR',hp.localsymtable,[tcdssso_register_asmsym,tcdssso_use_indirect])),
  1381. voidpointertype
  1382. );
  1383. inc(count);
  1384. end;
  1385. hp:=tmodule(hp.next);
  1386. end;
  1387. { Insert TableCount at start }
  1388. countplaceholder.replace(Tai_const.Create_sizeint(count),sizesinttype);
  1389. countplaceholder.free;
  1390. { Add to data segment }
  1391. tabledef:=tcb.end_anonymous_record;
  1392. current_asmdata.AsmLists[al_globals].concatList(
  1393. tcb.get_final_asmlist(
  1394. current_asmdata.DefineAsmSymbol('FPC_RESOURCESTRINGTABLES',AB_GLOBAL,AT_DATA,tabledef),
  1395. tabledef,sec_rodata,'FPC_RESOURCESTRINGTABLES',const_align(sizeof(pint))
  1396. )
  1397. );
  1398. tcb.free;
  1399. end;
  1400. class procedure tnodeutils.InsertResourceInfo(ResourcesUsed: boolean);
  1401. var
  1402. tcb: ttai_typedconstbuilder;
  1403. begin
  1404. if (target_res.id in [res_elf,res_macho,res_xcoff,res_wasm]) or
  1405. { generate the FPC_RESLOCATION symbol even when using external resources,
  1406. because in SysInit we can only reference it unconditionally }
  1407. ((target_res.id=res_ext) and (target_info.system in systems_darwin)) then
  1408. begin
  1409. tcb:=ctai_typedconstbuilder.create([tcalo_new_section,tcalo_make_dead_strippable]);
  1410. if ResourcesUsed and (target_res.id<>res_ext) then
  1411. tcb.emit_tai(Tai_const.Createname('FPC_RESSYMBOL',0),voidpointertype)
  1412. else
  1413. { Nil pointer to resource information }
  1414. tcb.emit_tai(tai_const.Create_nil_dataptr,voidpointertype);
  1415. current_asmdata.asmlists[al_globals].concatList(
  1416. tcb.get_final_asmlist(
  1417. current_asmdata.DefineAsmSymbol('FPC_RESLOCATION',AB_GLOBAL,AT_DATA,voidpointertype),
  1418. voidpointertype,
  1419. sec_rodata,
  1420. 'FPC_RESLOCATION',
  1421. const_align(sizeof(puint))
  1422. )
  1423. );
  1424. tcb.free;
  1425. end;
  1426. end;
  1427. class procedure tnodeutils.InsertMemorySizes;
  1428. var
  1429. tcb: ttai_typedconstbuilder;
  1430. s: shortstring;
  1431. sym: tasmsymbol;
  1432. def: tdef;
  1433. begin
  1434. { Insert Ident of the compiler in the .fpc.version section }
  1435. tcb:=ctai_typedconstbuilder.create([tcalo_no_dead_strip]);
  1436. s:='FPC '+full_version_string+
  1437. ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname;
  1438. {$ifdef m68k}
  1439. { Ensure that the size of s is multiple of 2 to avoid problems
  1440. like on m68k-amiga which has a .balignw just after,
  1441. causes an assembler error }
  1442. while (length(s) mod 2) <> 0 do
  1443. s:=s+' ';
  1444. {$endif m68k}
  1445. def:=carraydef.getreusable(cansichartype,length(s));
  1446. tcb.maybe_begin_aggregate(def);
  1447. tcb.emit_tai(Tai_string.Create(s),def);
  1448. tcb.maybe_end_aggregate(def);
  1449. sym:=current_asmdata.DefineAsmSymbol('__fpc_ident',AB_LOCAL,AT_DATA,def);
  1450. current_asmdata.asmlists[al_globals].concatlist(
  1451. tcb.get_final_asmlist(sym,def,sec_fpc,'version',const_align(32))
  1452. );
  1453. tcb.free;
  1454. if (tf_emit_stklen in target_info.flags) or
  1455. not(tf_no_generic_stackcheck in target_info.flags) then
  1456. begin
  1457. { stacksize can be specified and is now simulated }
  1458. tcb:=ctai_typedconstbuilder.create([tcalo_new_section,tcalo_make_dead_strippable]);
  1459. tcb.emit_tai(Tai_const.Create_int_dataptr(stacksize),ptruinttype);
  1460. sym:=current_asmdata.DefineAsmSymbol('__stklen',AB_GLOBAL,AT_DATA,ptruinttype);
  1461. current_asmdata.asmlists[al_globals].concatlist(
  1462. tcb.get_final_asmlist(sym,ptruinttype,sec_data,'__stklen',const_align(sizeof(pint)))
  1463. );
  1464. tcb.free;
  1465. end;
  1466. { allocate the stack on the ZX Spectrum system }
  1467. if target_info.system in [system_z80_zxspectrum] then
  1468. begin
  1469. { tai_datablock cannot yet be handled via the high level typed const
  1470. builder, because it implies the generation of a symbol, while this
  1471. is separate in the builder }
  1472. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  1473. new_section(current_asmdata.asmlists[al_globals],sec_stack,'__fpc_stackarea_start',current_settings.alignment.varalignmax);
  1474. current_asmdata.asmlists[al_globals].concat(tai_datablock.Create_global('__fpc_stackarea_start',stacksize-1,carraydef.getreusable(u8inttype,stacksize-1),AT_DATA));
  1475. current_asmdata.asmlists[al_globals].concat(tai_datablock.Create_global('__fpc_stackarea_end',1,carraydef.getreusable(u8inttype,1),AT_DATA));
  1476. end;
  1477. {$IFDEF POWERPC}
  1478. { AmigaOS4 "stack cookie" support }
  1479. if ( target_info.system = system_powerpc_amiga ) then
  1480. begin
  1481. { this symbol is needed to ignite powerpc amigaos' }
  1482. { stack allocation magic for us with the given stack size. }
  1483. { note: won't work for m68k amigaos or morphos. (KB) }
  1484. str(stacksize,s);
  1485. s:='$STACK: '+s+#0;
  1486. def:=carraydef.getreusable(cansichartype,length(s));
  1487. tcb:=ctai_typedconstbuilder.create([tcalo_new_section]);
  1488. tcb.maybe_begin_aggregate(def);
  1489. tcb.emit_tai(Tai_string.Create(s),def);
  1490. tcb.maybe_end_aggregate(def);
  1491. sym:=current_asmdata.DefineAsmSymbol('__stack_cookie',AB_GLOBAL,AT_DATA,def);
  1492. current_asmdata.asmlists[al_globals].concatlist(
  1493. tcb.get_final_asmlist(sym,def,sec_data,'__stack_cookie',sizeof(pint))
  1494. );
  1495. tcb.free;
  1496. end;
  1497. {$ENDIF POWERPC}
  1498. { Initial heapsize }
  1499. tcb:=ctai_typedconstbuilder.create([tcalo_new_section,tcalo_make_dead_strippable]);
  1500. tcb.emit_tai(Tai_const.Create_int_dataptr(heapsize),ptruinttype);
  1501. sym:=current_asmdata.DefineAsmSymbol('__heapsize',AB_GLOBAL,AT_DATA,ptruinttype);
  1502. current_asmdata.asmlists[al_globals].concatlist(
  1503. tcb.get_final_asmlist(sym,ptruinttype,sec_data,'__heapsize',const_align(sizeof(pint)))
  1504. );
  1505. tcb.free;
  1506. { allocate an initial heap on embedded systems }
  1507. if target_info.system in (systems_embedded+systems_freertos+[system_z80_zxspectrum,system_z80_msxdos]) then
  1508. begin
  1509. { tai_datablock cannot yet be handled via the high level typed const
  1510. builder, because it implies the generation of a symbol, while this
  1511. is separate in the builder }
  1512. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  1513. new_section(current_asmdata.asmlists[al_globals],sec_bss,'__fpc_initialheap',current_settings.alignment.varalignmax);
  1514. current_asmdata.asmlists[al_globals].concat(tai_datablock.Create_global('__fpc_initialheap',heapsize,carraydef.getreusable(u8inttype,heapsize),AT_DATA));
  1515. end;
  1516. { Valgrind usage }
  1517. tcb:=ctai_typedconstbuilder.create([tcalo_new_section,tcalo_make_dead_strippable]);
  1518. tcb.emit_ord_const(byte(cs_gdb_valgrind in current_settings.globalswitches),u8inttype);
  1519. sym:=current_asmdata.DefineAsmSymbol('__fpc_valgrind',AB_GLOBAL,AT_DATA,u8inttype);
  1520. current_asmdata.asmlists[al_globals].concatlist(
  1521. tcb.get_final_asmlist(sym,u8inttype,sec_data,'__fpc_valgrind',const_align(sizeof(pint)))
  1522. );
  1523. tcb.free;
  1524. end;
  1525. class procedure tnodeutils.InsertObjectInfo;
  1526. var
  1527. tcb: ttai_typedconstbuilder;
  1528. begin
  1529. if (m_objectivec1 in current_settings.modeswitches) then
  1530. begin
  1531. { first 4 bytes contain version information about this section (currently version 0),
  1532. next 4 bytes contain flags (currently only regarding whether the code in the object
  1533. file supports or requires garbage collection)
  1534. }
  1535. tcb:=ctai_typedconstbuilder.create([tcalo_new_section,tcalo_no_dead_strip]);
  1536. tcb.emit_ord_const(0,u64inttype);
  1537. current_asmdata.asmlists[al_objc_data].concatList(
  1538. tcb.get_final_asmlist(
  1539. current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'_OBJC_IMAGE_INFO',AB_LOCAL,AT_DATA,u64inttype),
  1540. u64inttype,sec_objc_image_info,'_OBJC_IMAGE_INFO',const_align(sizeof(pint))
  1541. )
  1542. );
  1543. tcb.free;
  1544. end;
  1545. end;
  1546. class procedure tnodeutils.RegisterUsedAsmSym(sym: TAsmSymbol; def: tdef; compileronly: boolean);
  1547. begin
  1548. { don't do anything by default }
  1549. end;
  1550. class procedure tnodeutils.RegisterModuleInitFunction(pd: tprocdef);
  1551. begin
  1552. { setinitname may generate a new section -> don't add to the
  1553. current list, because we assume this remains a text section }
  1554. exportlib.setinitname(current_asmdata.AsmLists[al_pure_assembler],pd.mangledname);
  1555. end;
  1556. class procedure tnodeutils.RegisterModuleFiniFunction(pd: tprocdef);
  1557. begin
  1558. exportlib.setfininame(current_asmdata.AsmLists[al_pure_assembler],pd.mangledname);
  1559. end;
  1560. class procedure tnodeutils.add_main_procdef_paras(pd: tdef);
  1561. var
  1562. pvs: tparavarsym;
  1563. begin
  1564. { stub for calling FPC_SYSTEMMAIN from the C main -> add argc/argv/argp }
  1565. if (tprocdef(pd).proctypeoption=potype_mainstub) and
  1566. (target_info.system in (systems_darwin+[system_powerpc_macosclassic]+systems_aix)) then
  1567. begin
  1568. pvs:=cparavarsym.create('ARGC',1,vs_const,s32inttype,[]);
  1569. tprocdef(pd).parast.insertsym(pvs);
  1570. pvs:=cparavarsym.create('ARGV',2,vs_const,cpointerdef.getreusable(charpointertype),[]);
  1571. tprocdef(pd).parast.insertsym(pvs);
  1572. pvs:=cparavarsym.create('ARGP',3,vs_const,cpointerdef.getreusable(charpointertype),[]);
  1573. tprocdef(pd).parast.insertsym(pvs);
  1574. tprocdef(pd).calcparas;
  1575. end
  1576. { package stub for Windows is a DLLMain }
  1577. else if (tprocdef(pd).proctypeoption=potype_pkgstub) and
  1578. (target_info.system in systems_all_windows+systems_nativent) then
  1579. begin
  1580. pvs:=cparavarsym.create('HINSTANCE',1,vs_const,uinttype,[]);
  1581. tprocdef(pd).parast.insertsym(pvs);
  1582. pvs:=cparavarsym.create('DLLREASON',2,vs_const,u32inttype,[]);
  1583. tprocdef(pd).parast.insertsym(pvs);
  1584. pvs:=cparavarsym.create('DLLPARAM',3,vs_const,voidpointertype,[]);
  1585. tprocdef(pd).parast.insertsym(pvs);
  1586. tprocdef(pd).returndef:=bool32type;
  1587. insert_funcret_para(tprocdef(pd));
  1588. tprocdef(pd).calcparas;
  1589. end;
  1590. end;
  1591. end.