ngenutil.pas 68 KB

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