ngenutil.pas 66 KB

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