ngenutil.pas 62 KB

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