ngenutil.pas 63 KB

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