ngenutil.pas 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  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. aasmdata,
  24. node,nbas,symtype,symsym,symconst,symdef;
  25. type
  26. tnodeutils = class
  27. class function call_fail_node:tnode; virtual;
  28. class function initialize_data_node(p:tnode; force: boolean):tnode; virtual;
  29. class function finalize_data_node(p:tnode):tnode; virtual;
  30. { returns true if the unit requires an initialisation section (e.g.,
  31. to force class constructors for the JVM target to initialise global
  32. records/arrays) }
  33. class function force_init: boolean; virtual;
  34. { idem for finalization }
  35. class function force_final: boolean; virtual;
  36. { called after parsing a routine with the code of the entire routine
  37. as argument; can be used to modify the node tree. By default handles
  38. insertion of code for systems that perform the typed constant
  39. initialisation via the node tree }
  40. class function wrap_proc_body(pd: tprocdef; n: tnode): tnode; virtual;
  41. { trashes a paravarsym or localvarsym if possible (not a managed type,
  42. "out" in case of parameter, ...) }
  43. class procedure maybe_trash_variable(var stat: tstatementnode; p: tabstractnormalvarsym; trashn: tnode); virtual;
  44. strict protected
  45. { called from wrap_proc_body to insert the trashing for the wrapped
  46. routine's local variables and parameters }
  47. class function maybe_insert_trashing(pd: tprocdef; n: tnode): tnode;
  48. class function check_insert_trashing(pd: tprocdef): boolean; virtual;
  49. { callback called for every local variable and parameter by
  50. maybe_insert_trashing(), calls through to maybe_trash_variable() }
  51. class procedure maybe_trash_variable_callback(p: TObject; statn: pointer);
  52. { returns whether a particular sym can be trashed. If not,
  53. maybe_trash_variable won't do anything }
  54. class function trashable_sym(p: tsym): boolean; virtual;
  55. { trashing for 1/2/3/4/8-byte sized variables }
  56. class procedure trash_small(var stat: tstatementnode; trashn: tnode; trashvaln: tnode); virtual;
  57. { trashing for differently sized variables that those handled by
  58. trash_small() }
  59. class procedure trash_large(var stat: tstatementnode; trashn, sizen: tnode; trashintval: int64); virtual;
  60. { insert a single bss sym, called by insert bssdata (factored out
  61. non-common part for llvm) }
  62. class procedure insertbsssym(list: tasmlist; sym: tstaticvarsym; size: asizeint; varalign: shortint); virtual;
  63. { initialization of iso styled program parameters }
  64. class procedure initialize_textrec(p : TObject; statn : pointer);
  65. { finalization of iso styled program parameters }
  66. class procedure finalize_textrec(p : TObject; statn : pointer);
  67. public
  68. class procedure insertbssdata(sym : tstaticvarsym); virtual;
  69. class function create_main_procdef(const name: string; potype:tproctypeoption; ps: tprocsym):tdef; virtual;
  70. class procedure InsertInitFinalTable; virtual;
  71. protected
  72. class procedure InsertRuntimeInits(const prefix:string;list:TLinkedList;unitflag:cardinal); virtual;
  73. class procedure InsertRuntimeInitsTablesTable(const prefix,tablename:string;unitflag:cardinal); virtual;
  74. public
  75. class procedure InsertThreadvarTablesTable; virtual;
  76. class procedure InsertThreadvars; virtual;
  77. class procedure InsertWideInitsTablesTable; virtual;
  78. class procedure InsertWideInits; virtual;
  79. class procedure InsertResStrInits; virtual;
  80. class procedure InsertResStrTablesTable; virtual;
  81. class procedure InsertResourceTablesTable; virtual;
  82. class procedure InsertResourceInfo(ResourcesUsed : boolean); virtual;
  83. class procedure InsertMemorySizes; virtual;
  84. { called right before an object is assembled, can be used to insert
  85. global information into the assembler list (used by LLVM to insert type
  86. info) }
  87. class procedure InsertObjectInfo; virtual;
  88. strict protected
  89. class procedure add_main_procdef_paras(pd: tdef); virtual;
  90. end;
  91. tnodeutilsclass = class of tnodeutils;
  92. const
  93. cnodeutils: tnodeutilsclass = tnodeutils;
  94. implementation
  95. uses
  96. verbose,version,globals,cutils,constexp,
  97. scanner,systems,procinfo,fmodule,
  98. aasmbase,aasmtai,aasmcnst,
  99. symbase,symtable,defutil,symcreat,
  100. nadd,ncal,ncnv,ncon,nflw,ninl,nld,nmem,nobj,nutils,
  101. ppu,
  102. pass_1;
  103. class function tnodeutils.call_fail_node:tnode;
  104. var
  105. para : tcallparanode;
  106. newstatement : tstatementnode;
  107. srsym : tsym;
  108. begin
  109. result:=internalstatements(newstatement);
  110. { call fail helper and exit normal }
  111. if is_class(current_structdef) then
  112. begin
  113. srsym:=search_struct_member(current_structdef,'FREEINSTANCE');
  114. if assigned(srsym) and
  115. (srsym.typ=procsym) then
  116. begin
  117. { if self<>0 and vmt<>0 then freeinstance }
  118. addstatement(newstatement,cifnode.create(
  119. caddnode.create(andn,
  120. caddnode.create(unequaln,
  121. load_self_pointer_node,
  122. cnilnode.create),
  123. caddnode.create(unequaln,
  124. load_vmt_pointer_node,
  125. cnilnode.create)),
  126. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[],nil),
  127. nil));
  128. end
  129. else
  130. internalerror(200305108);
  131. end
  132. else
  133. if is_object(current_structdef) then
  134. begin
  135. { parameter 3 : vmt_offset }
  136. { parameter 2 : pointer to vmt }
  137. { parameter 1 : self pointer }
  138. para:=ccallparanode.create(
  139. cordconstnode.create(tobjectdef(current_structdef).vmt_offset,s32inttype,false),
  140. ccallparanode.create(
  141. ctypeconvnode.create_internal(
  142. load_vmt_pointer_node,
  143. voidpointertype),
  144. ccallparanode.create(
  145. ctypeconvnode.create_internal(
  146. load_self_pointer_node,
  147. voidpointertype),
  148. nil)));
  149. addstatement(newstatement,
  150. ccallnode.createintern('fpc_help_fail',para));
  151. end
  152. else
  153. internalerror(200305132);
  154. { self:=nil }
  155. addstatement(newstatement,cassignmentnode.create(
  156. load_self_pointer_node,
  157. cnilnode.create));
  158. { exit }
  159. addstatement(newstatement,cexitnode.create(nil));
  160. end;
  161. class function tnodeutils.initialize_data_node(p:tnode; force: boolean):tnode;
  162. begin
  163. if not assigned(p.resultdef) then
  164. typecheckpass(p);
  165. if is_ansistring(p.resultdef) or
  166. is_wide_or_unicode_string(p.resultdef) or
  167. is_interfacecom_or_dispinterface(p.resultdef) or
  168. is_dynamic_array(p.resultdef) then
  169. begin
  170. result:=cassignmentnode.create(
  171. ctypeconvnode.create_internal(p,voidpointertype),
  172. cnilnode.create
  173. );
  174. end
  175. else if (p.resultdef.typ=variantdef) then
  176. begin
  177. result:=ccallnode.createintern('fpc_variant_init',
  178. ccallparanode.create(
  179. ctypeconvnode.create_internal(p,search_system_type('TVARDATA').typedef),
  180. nil));
  181. end
  182. else
  183. begin
  184. result:=ccallnode.createintern('fpc_initialize',
  185. ccallparanode.create(
  186. caddrnode.create_internal(
  187. crttinode.create(
  188. tstoreddef(p.resultdef),initrtti,rdt_normal)),
  189. ccallparanode.create(
  190. caddrnode.create_internal(p),
  191. nil)));
  192. end;
  193. end;
  194. class function tnodeutils.finalize_data_node(p:tnode):tnode;
  195. var
  196. hs : string;
  197. begin
  198. if not assigned(p.resultdef) then
  199. typecheckpass(p);
  200. { 'decr_ref' suffix is somewhat misleading, all these helpers
  201. set the passed pointer to nil now }
  202. if is_ansistring(p.resultdef) then
  203. hs:='fpc_ansistr_decr_ref'
  204. else if is_widestring(p.resultdef) then
  205. hs:='fpc_widestr_decr_ref'
  206. else if is_unicodestring(p.resultdef) then
  207. hs:='fpc_unicodestr_decr_ref'
  208. else if is_interfacecom_or_dispinterface(p.resultdef) then
  209. hs:='fpc_intf_decr_ref'
  210. else
  211. hs:='';
  212. if hs<>'' then
  213. result:=ccallnode.createintern(hs,
  214. ccallparanode.create(
  215. ctypeconvnode.create_internal(p,voidpointertype),
  216. nil))
  217. else if p.resultdef.typ=variantdef then
  218. begin
  219. result:=ccallnode.createintern('fpc_variant_clear',
  220. ccallparanode.create(
  221. ctypeconvnode.create_internal(p,search_system_type('TVARDATA').typedef),
  222. nil));
  223. end
  224. else
  225. result:=ccallnode.createintern('fpc_finalize',
  226. ccallparanode.create(
  227. caddrnode.create_internal(
  228. crttinode.create(
  229. tstoreddef(p.resultdef),initrtti,rdt_normal)),
  230. ccallparanode.create(
  231. caddrnode.create_internal(p),
  232. nil)));
  233. end;
  234. class function tnodeutils.force_init: boolean;
  235. begin
  236. result:=
  237. (target_info.system in systems_typed_constants_node_init) and
  238. assigned(current_module.tcinitcode);
  239. end;
  240. class function tnodeutils.force_final: boolean;
  241. begin
  242. result:=false;
  243. end;
  244. class procedure tnodeutils.initialize_textrec(p:TObject;statn:pointer);
  245. var
  246. stat: ^tstatementnode absolute statn;
  247. begin
  248. if (tsym(p).typ=staticvarsym) and
  249. (tstaticvarsym(p).vardef.typ=filedef) and
  250. (tfiledef(tstaticvarsym(p).vardef).filetyp=ft_text) and
  251. (tstaticvarsym(p).isoindex<>0) then
  252. begin
  253. addstatement(stat^,ccallnode.createintern('fpc_textinit_iso',
  254. ccallparanode.create(
  255. cordconstnode.create(tstaticvarsym(p).isoindex,uinttype,false),
  256. ccallparanode.create(
  257. cloadnode.create(tstaticvarsym(p),tstaticvarsym(p).Owner),
  258. nil))));
  259. end;
  260. end;
  261. class procedure tnodeutils.finalize_textrec(p:TObject;statn:pointer);
  262. var
  263. stat: ^tstatementnode absolute statn;
  264. begin
  265. if (tsym(p).typ=staticvarsym) and
  266. (tstaticvarsym(p).vardef.typ=filedef) and
  267. (tfiledef(tstaticvarsym(p).vardef).filetyp=ft_text) and
  268. (tstaticvarsym(p).isoindex<>0) then
  269. begin
  270. addstatement(stat^,ccallnode.createintern('fpc_textclose_iso',
  271. ccallparanode.create(
  272. cloadnode.create(tstaticvarsym(p),tstaticvarsym(p).Owner),
  273. nil)));
  274. end;
  275. end;
  276. class function tnodeutils.wrap_proc_body(pd: tprocdef; n: tnode): tnode;
  277. var
  278. stat: tstatementnode;
  279. block,
  280. target: tnode;
  281. psym: tsym;
  282. begin
  283. result:=maybe_insert_trashing(pd,n);
  284. if (m_isolike_program_para in current_settings.modeswitches) and
  285. (pd.proctypeoption=potype_proginit) then
  286. begin
  287. block:=internalstatements(stat);
  288. pd.localst.SymList.ForEachCall(@initialize_textrec,@stat);
  289. addstatement(stat,result);
  290. pd.localst.SymList.ForEachCall(@finalize_textrec,@stat);
  291. result:=block;
  292. end;
  293. if target_info.system in systems_typed_constants_node_init then
  294. begin
  295. case pd.proctypeoption of
  296. potype_class_constructor:
  297. begin
  298. { even though the initialisation code for typed constants may
  299. not yet be complete at this point (there may be more inside
  300. method definitions coming after this class constructor), the
  301. ones from inside the class definition have already been parsed.
  302. in case of $j-, these are marked "final" in Java and such
  303. static fields must be initialsed in the class constructor
  304. itself -> add them here }
  305. block:=internalstatements(stat);
  306. if assigned(pd.struct.tcinitcode) then
  307. begin
  308. addstatement(stat,pd.struct.tcinitcode);
  309. pd.struct.tcinitcode:=nil;
  310. end;
  311. psym:=tsym(pd.struct.symtable.find('FPC_INIT_TYPED_CONSTS_HELPER'));
  312. if assigned(psym) then
  313. begin
  314. if (psym.typ<>procsym) or
  315. (tprocsym(psym).procdeflist.count<>1) then
  316. internalerror(2011040301);
  317. addstatement(stat,ccallnode.create(nil,tprocsym(psym),
  318. pd.struct.symtable,nil,[],nil));
  319. end;
  320. addstatement(stat,result);
  321. result:=block
  322. end;
  323. potype_unitinit:
  324. begin
  325. if assigned(current_module.tcinitcode) then
  326. begin
  327. block:=internalstatements(stat);
  328. addstatement(stat,tnode(current_module.tcinitcode));
  329. current_module.tcinitcode:=nil;
  330. addstatement(stat,result);
  331. result:=block;
  332. end;
  333. end;
  334. else case pd.synthetickind of
  335. tsk_tcinit:
  336. begin
  337. if assigned(pd.struct.tcinitcode) then
  338. begin
  339. block:=internalstatements(stat);
  340. addstatement(stat,pd.struct.tcinitcode);
  341. pd.struct.tcinitcode:=nil;
  342. addstatement(stat,result);
  343. result:=block
  344. end
  345. end;
  346. end;
  347. end;
  348. end;
  349. if target_info.system in systems_fpnestedstruct then
  350. begin
  351. { if the funcretsym was moved to the parentfpstruct, move its value
  352. back into the funcretsym now, as the code generator is hardcoded
  353. to use the funcretsym when loading the value to be returned;
  354. replacing it with an absolutevarsym that redirects to the field in
  355. the parentfpstruct doesn't work, as the code generator cannot deal
  356. with such symbols }
  357. if assigned(pd.funcretsym) and
  358. tabstractnormalvarsym(pd.funcretsym).inparentfpstruct then
  359. begin
  360. block:=internalstatements(stat);
  361. addstatement(stat,result);
  362. target:=cloadnode.create(pd.funcretsym,pd.funcretsym.owner);
  363. { ensure the target of this assignment doesn't translate the
  364. funcretsym also to its alias in the parentfpstruct }
  365. include(target.flags,nf_internal);
  366. addstatement(stat,
  367. cassignmentnode.create(
  368. target,cloadnode.create(pd.funcretsym,pd.funcretsym.owner)
  369. )
  370. );
  371. result:=block;
  372. end;
  373. end;
  374. end;
  375. class function tnodeutils.maybe_insert_trashing(pd: tprocdef; n: tnode): tnode;
  376. var
  377. stat: tstatementnode;
  378. begin
  379. result:=n;
  380. if check_insert_trashing(pd) then
  381. begin
  382. result:=internalstatements(stat);
  383. pd.parast.SymList.ForEachCall(@maybe_trash_variable_callback,@stat);
  384. pd.localst.SymList.ForEachCall(@maybe_trash_variable_callback,@stat);
  385. addstatement(stat,n);
  386. end;
  387. end;
  388. class function tnodeutils.check_insert_trashing(pd: tprocdef): boolean;
  389. begin
  390. result:=
  391. (localvartrashing<>-1) and
  392. not(po_assembler in pd.procoptions);
  393. end;
  394. class function tnodeutils.trashable_sym(p: tsym): boolean;
  395. begin
  396. result:=
  397. ((p.typ=localvarsym) or
  398. ((p.typ=paravarsym) and
  399. ((vo_is_funcret in tabstractnormalvarsym(p).varoptions) or
  400. (tabstractnormalvarsym(p).varspez=vs_out)))) and
  401. not (vo_is_default_var in tabstractnormalvarsym(p).varoptions) and
  402. not is_managed_type(tabstractnormalvarsym(p).vardef) and
  403. not assigned(tabstractnormalvarsym(p).defaultconstsym);
  404. end;
  405. class procedure tnodeutils.maybe_trash_variable(var stat: tstatementnode; p: tabstractnormalvarsym; trashn: tnode);
  406. var
  407. size: asizeint;
  408. trashintval: int64;
  409. begin
  410. if trashable_sym(p) then
  411. begin
  412. trashintval:=trashintvalues[localvartrashing];
  413. if (p.vardef.typ=procvardef) and
  414. ([m_tp_procvar,m_mac_procvar]*current_settings.modeswitches<>[]) then
  415. begin
  416. if tprocvardef(p.vardef).is_addressonly then
  417. { in tp/delphi mode, you need @procvar to get at the contents of
  418. a procvar ... }
  419. trashn:=caddrnode.create(trashn)
  420. else
  421. { ... but if it's a procedure of object, that will only return
  422. the procedure address -> cast to tmethod instead }
  423. trashn:=ctypeconvnode.create_explicit(trashn,methodpointertype);
  424. end;
  425. if ((p.typ=localvarsym) and
  426. (not(vo_is_funcret in p.varoptions) or
  427. not is_shortstring(p.vardef))) or
  428. ((p.typ=paravarsym) and
  429. not is_shortstring(p.vardef)) then
  430. begin
  431. size:=p.getsize;
  432. case size of
  433. 0:
  434. begin
  435. { open array -> at least size 1. Can also be zero-sized
  436. record, so check it's actually an array }
  437. if p.vardef.typ=arraydef then
  438. trash_large(stat,trashn,caddnode.create(addn,cinlinenode.create(in_high_x,false,trashn.getcopy),genintconstnode(1)),trashintval)
  439. else
  440. trashn.free;
  441. end;
  442. 1: trash_small(stat,
  443. ctypeconvnode.create_internal(trashn,s8inttype),
  444. genintconstnode(shortint(trashintval)));
  445. 2: trash_small(stat,
  446. ctypeconvnode.create_internal(trashn,s16inttype),
  447. genintconstnode(smallint(trashintval)));
  448. 4: trash_small(stat,
  449. ctypeconvnode.create_internal(trashn,s32inttype),
  450. genintconstnode(longint(trashintval)));
  451. 8: trash_small(stat,
  452. ctypeconvnode.create_internal(trashn,s64inttype),
  453. genintconstnode(int64(trashintval)));
  454. else
  455. trash_large(stat,trashn,genintconstnode(size),trashintval);
  456. end;
  457. end
  458. else
  459. begin
  460. { may be an open string, even if is_open_string() returns false
  461. (for some helpers in the system unit) }
  462. { an open string has at least size 2 }
  463. trash_small(stat,
  464. cvecnode.create(trashn.getcopy,genintconstnode(0)),
  465. cordconstnode.create(tconstexprint(byte(trashintval)),cansichartype,false));
  466. trash_small(stat,
  467. cvecnode.create(trashn,genintconstnode(1)),
  468. cordconstnode.create(tconstexprint(byte(trashintval)),cansichartype,false));
  469. end;
  470. end
  471. else
  472. trashn.free;
  473. end;
  474. class procedure tnodeutils.maybe_trash_variable_callback(p:TObject;statn:pointer);
  475. var
  476. stat: ^tstatementnode absolute statn;
  477. begin
  478. if not(tsym(p).typ in [localvarsym,paravarsym]) then
  479. exit;
  480. maybe_trash_variable(stat^,tabstractnormalvarsym(p),cloadnode.create(tsym(p),tsym(p).owner));
  481. end;
  482. class procedure tnodeutils.trash_small(var stat: tstatementnode; trashn: tnode; trashvaln: tnode);
  483. begin
  484. addstatement(stat,cassignmentnode.create(trashn,trashvaln));
  485. end;
  486. class procedure tnodeutils.trash_large(var stat: tstatementnode; trashn, sizen: tnode; trashintval: int64);
  487. begin
  488. addstatement(stat,ccallnode.createintern('fpc_fillmem',
  489. ccallparanode.Create(cordconstnode.create(tconstexprint(byte(trashintval)),u8inttype,false),
  490. ccallparanode.Create(sizen,
  491. ccallparanode.Create(trashn,nil)))
  492. ));
  493. end;
  494. class procedure tnodeutils.insertbsssym(list: tasmlist; sym: tstaticvarsym; size: asizeint; varalign: shortint);
  495. begin
  496. if sym.globalasmsym then
  497. begin
  498. { on AIX/stabx, we cannot generate debug information that encodes
  499. the address of a global symbol, you need a symbol with the same
  500. name as the identifier -> create an extra *local* symbol.
  501. Moreover, such a local symbol will be removed if it's not
  502. referenced anywhere, so also create a reference }
  503. if (target_dbg.id=dbg_stabx) and
  504. (cs_debuginfo in current_settings.moduleswitches) and
  505. not assigned(current_asmdata.GetAsmSymbol(sym.name)) then
  506. begin
  507. list.concat(tai_symbol.Create(current_asmdata.DefineAsmSymbol(sym.name,AB_LOCAL,AT_DATA),0));
  508. list.concat(tai_directive.Create(asd_reference,sym.name));
  509. end;
  510. list.concat(Tai_datablock.create_global(sym.mangledname,size));
  511. end
  512. else
  513. list.concat(Tai_datablock.create(sym.mangledname,size));
  514. end;
  515. class procedure tnodeutils.insertbssdata(sym: tstaticvarsym);
  516. var
  517. l : asizeint;
  518. varalign : shortint;
  519. storefilepos : tfileposinfo;
  520. list : TAsmList;
  521. sectype : TAsmSectiontype;
  522. begin
  523. storefilepos:=current_filepos;
  524. current_filepos:=sym.fileinfo;
  525. l:=sym.getsize;
  526. varalign:=sym.vardef.alignment;
  527. if (varalign=0) then
  528. varalign:=var_align_size(l)
  529. else
  530. varalign:=var_align(varalign);
  531. if tf_section_threadvars in target_info.flags then
  532. begin
  533. if (vo_is_thread_var in sym.varoptions) then
  534. begin
  535. list:=current_asmdata.asmlists[al_threadvars];
  536. sectype:=sec_threadvar;
  537. end
  538. else
  539. begin
  540. list:=current_asmdata.asmlists[al_globals];
  541. sectype:=sec_bss;
  542. end;
  543. end
  544. else
  545. begin
  546. if (vo_is_thread_var in sym.varoptions) then
  547. begin
  548. inc(l,sizeof(pint));
  549. { it doesn't help to set a higher alignment, as }
  550. { the first sizeof(pint) bytes field will offset }
  551. { everything anyway }
  552. varalign:=sizeof(pint);
  553. end;
  554. list:=current_asmdata.asmlists[al_globals];
  555. sectype:=sec_bss;
  556. end;
  557. maybe_new_object_file(list);
  558. if vo_has_section in sym.varoptions then
  559. new_section(list,sec_user,sym.section,varalign)
  560. else
  561. new_section(list,sectype,lower(sym.mangledname),varalign);
  562. insertbsssym(list,sym,l,varalign);
  563. current_filepos:=storefilepos;
  564. end;
  565. class function tnodeutils.create_main_procdef(const name: string; potype: tproctypeoption; ps: tprocsym): tdef;
  566. var
  567. pd: tprocdef;
  568. begin
  569. if potype<>potype_mainstub then
  570. pd:=cprocdef.create(main_program_level,true)
  571. else
  572. pd:=cprocdef.create(normal_function_level,true);
  573. pd.procsym:=ps;
  574. ps.ProcdefList.Add(pd);
  575. include(pd.procoptions,po_global);
  576. { set procdef options }
  577. pd.proctypeoption:=potype;
  578. pd.proccalloption:=pocall_default;
  579. include(pd.procoptions,po_hascallingconvention);
  580. pd.forwarddef:=false;
  581. { may be required to calculate the mangled name }
  582. add_main_procdef_paras(pd);
  583. pd.setmangledname(name);
  584. { the mainstub is generated via a synthetic proc -> parsed via
  585. psub.read_proc_body() -> that one will insert the mangled name in the
  586. alias names already }
  587. if potype<>potype_mainstub then
  588. pd.aliasnames.insert(pd.mangledname);
  589. result:=pd;
  590. end;
  591. procedure AddToStructInits(p:TObject;arg:pointer);
  592. var
  593. StructList: TFPList absolute arg;
  594. begin
  595. if (tdef(p).typ in [objectdef,recorddef]) and
  596. not (df_generic in tdef(p).defoptions) then
  597. begin
  598. { first add the class... }
  599. if ([oo_has_class_constructor,oo_has_class_destructor] * tabstractrecorddef(p).objectoptions <> []) then
  600. StructList.Add(p);
  601. { ... and then also add all subclasses }
  602. tabstractrecorddef(p).symtable.deflist.foreachcall(@AddToStructInits,arg);
  603. end;
  604. end;
  605. class procedure tnodeutils.InsertInitFinalTable;
  606. var
  607. hp : tused_unit;
  608. unitinits : ttai_typedconstbuilder;
  609. count : aint;
  610. tablecountplaceholder: ttypedconstplaceholder;
  611. procedure write_struct_inits(u: tmodule);
  612. var
  613. i: integer;
  614. structlist: TFPList;
  615. pd: tprocdef;
  616. begin
  617. structlist := TFPList.Create;
  618. if assigned(u.globalsymtable) then
  619. u.globalsymtable.DefList.ForEachCall(@AddToStructInits,structlist);
  620. u.localsymtable.DefList.ForEachCall(@AddToStructInits,structlist);
  621. { write structures }
  622. for i:=0 to structlist.Count-1 do
  623. begin
  624. pd:=tabstractrecorddef(structlist[i]).find_procdef_bytype(potype_class_constructor);
  625. if assigned(pd) then
  626. unitinits.emit_procdef_const(pd)
  627. else
  628. unitinits.emit_tai(Tai_const.Create_nil_codeptr,voidcodepointertype);
  629. pd := tabstractrecorddef(structlist[i]).find_procdef_bytype(potype_class_destructor);
  630. if assigned(pd) then
  631. unitinits.emit_procdef_const(pd)
  632. else
  633. unitinits.emit_tai(Tai_const.Create_nil_codeptr,voidcodepointertype);
  634. inc(count);
  635. end;
  636. structlist.free;
  637. end;
  638. begin
  639. unitinits:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable,tcalo_new_section]);
  640. unitinits.begin_anonymous_record('',default_settings.packrecords,sizeof(pint),
  641. targetinfos[target_info.system]^.alignment.recordalignmin,
  642. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  643. { placeholder for tablecount }
  644. tablecountplaceholder:=unitinits.emit_placeholder(sinttype);
  645. { initcount (initialised at run time }
  646. unitinits.emit_ord_const(0,sinttype);
  647. count:=0;
  648. hp:=tused_unit(usedunits.first);
  649. while assigned(hp) do
  650. begin
  651. { insert class constructors/destructors of the unit }
  652. if (hp.u.flags and uf_classinits) <> 0 then
  653. write_struct_inits(hp.u);
  654. { call the unit init code and make it external }
  655. if (hp.u.flags and (uf_init or uf_finalize))<>0 then
  656. begin
  657. if count=high(aint) then
  658. Message1(cg_f_max_units_reached,tostr(count));
  659. if (hp.u.flags and uf_init)<>0 then
  660. unitinits.emit_tai(
  661. Tai_const.Createname(make_mangledname('INIT$',hp.u.globalsymtable,''),AT_FUNCTION,0),
  662. voidcodepointertype)
  663. else
  664. unitinits.emit_tai(Tai_const.Create_nil_codeptr,voidcodepointertype);
  665. if (hp.u.flags and uf_finalize)<>0 then
  666. unitinits.emit_tai(
  667. Tai_const.Createname(make_mangledname('FINALIZE$',hp.u.globalsymtable,''),AT_FUNCTION,0),
  668. voidcodepointertype)
  669. else
  670. unitinits.emit_tai(Tai_const.Create_nil_codeptr,voidcodepointertype);
  671. inc(count);
  672. end;
  673. hp:=tused_unit(hp.next);
  674. end;
  675. { insert class constructors/destructor of the program }
  676. if (current_module.flags and uf_classinits) <> 0 then
  677. write_struct_inits(current_module);
  678. { Insert initialization/finalization of the program }
  679. if (current_module.flags and (uf_init or uf_finalize))<>0 then
  680. begin
  681. if (current_module.flags and uf_init)<>0 then
  682. unitinits.emit_tai(
  683. Tai_const.Createname(make_mangledname('INIT$',current_module.localsymtable,''),AT_FUNCTION,0),
  684. voidcodepointertype)
  685. else
  686. unitinits.emit_tai(Tai_const.Create_nil_codeptr,voidcodepointertype);
  687. if (current_module.flags and uf_finalize)<>0 then
  688. unitinits.emit_tai(
  689. Tai_const.Createname(make_mangledname('FINALIZE$',current_module.localsymtable,''),AT_FUNCTION,0),
  690. voidcodepointertype)
  691. else
  692. unitinits.emit_tai(Tai_const.Create_nil_codeptr,voidcodepointertype);
  693. inc(count);
  694. end;
  695. { fill in tablecount }
  696. tablecountplaceholder.replace(tai_const.Create_aint(count),sinttype);
  697. tablecountplaceholder.free;
  698. { Add to data segment }
  699. current_asmdata.asmlists[al_globals].concatlist(
  700. unitinits.get_final_asmlist(
  701. current_asmdata.DefineAsmSymbol('INITFINAL',AB_GLOBAL,AT_DATA),
  702. unitinits.end_anonymous_record,
  703. sec_data,'INITFINAL',sizeof(pint)
  704. )
  705. );
  706. unitinits.free;
  707. end;
  708. class procedure tnodeutils.InsertThreadvarTablesTable;
  709. var
  710. hp : tused_unit;
  711. tcb: ttai_typedconstbuilder;
  712. count: longint;
  713. sym: tasmsymbol;
  714. placeholder: ttypedconstplaceholder;
  715. begin
  716. if (tf_section_threadvars in target_info.flags) then
  717. exit;
  718. count:=0;
  719. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable,tcalo_new_section]);
  720. tcb.begin_anonymous_record('',1,sizeof(pint),
  721. targetinfos[target_info.system]^.alignment.recordalignmin,
  722. targetinfos[target_info.system]^.alignment.maxCrecordalign
  723. );
  724. placeholder:=tcb.emit_placeholder(u32inttype);
  725. hp:=tused_unit(usedunits.first);
  726. while assigned(hp) do
  727. begin
  728. if (hp.u.flags and uf_threadvars)=uf_threadvars then
  729. begin
  730. tcb.emit_tai(
  731. tai_const.Createname(make_mangledname('THREADVARLIST',hp.u.globalsymtable,''),0),
  732. voidpointertype);
  733. inc(count);
  734. end;
  735. hp:=tused_unit(hp.next);
  736. end;
  737. { Add program threadvars, if any }
  738. if (current_module.flags and uf_threadvars)=uf_threadvars then
  739. begin
  740. tcb.emit_tai(
  741. Tai_const.Createname(make_mangledname('THREADVARLIST',current_module.localsymtable,''),0),
  742. voidpointertype);
  743. inc(count);
  744. end;
  745. { set the count at the start }
  746. placeholder.replace(tai_const.Create_32bit(count),u32inttype);
  747. placeholder.free;
  748. { insert in data segment }
  749. sym:=current_asmdata.DefineAsmSymbol('FPC_THREADVARTABLES',AB_GLOBAL,AT_DATA);
  750. current_asmdata.asmlists[al_globals].concatlist(
  751. tcb.get_final_asmlist(
  752. sym,tcb.end_anonymous_record,sec_data,'FPC_THREADVARTABLES',sizeof(pint)
  753. )
  754. );
  755. tcb.free;
  756. end;
  757. procedure AddToThreadvarList(p:TObject;arg:pointer);
  758. var
  759. tcb: ttai_typedconstbuilder;
  760. field1, field2: tsym;
  761. begin
  762. if (tsym(p).typ=staticvarsym) and
  763. (vo_is_thread_var in tstaticvarsym(p).varoptions) then
  764. begin
  765. tcb:=ttai_typedconstbuilder(arg);
  766. { address of threadvar }
  767. tcb.emit_tai(tai_const.Createname(tstaticvarsym(p).mangledname,0),
  768. cpointerdef.getreusable(
  769. get_threadvar_record(tstaticvarsym(p).vardef,field1,field2)
  770. )
  771. );
  772. { size of threadvar }
  773. tcb.emit_ord_const(tstaticvarsym(p).getsize,u32inttype);
  774. end;
  775. end;
  776. class procedure tnodeutils.InsertThreadvars;
  777. var
  778. s : string;
  779. tcb: ttai_typedconstbuilder;
  780. sym: tasmsymbol;
  781. tabledef: trecorddef;
  782. begin
  783. if (tf_section_threadvars in target_info.flags) then
  784. exit;
  785. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable,tcalo_new_section]);
  786. tabledef:=tcb.begin_anonymous_record('',1,sizeof(pint),
  787. targetinfos[target_info.system]^.alignment.recordalignmin,
  788. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  789. if assigned(current_module.globalsymtable) then
  790. current_module.globalsymtable.SymList.ForEachCall(@AddToThreadvarList,tcb);
  791. current_module.localsymtable.SymList.ForEachCall(@AddToThreadvarList,tcb);
  792. if trecordsymtable(tabledef.symtable).datasize<>0 then
  793. { terminator }
  794. tcb.emit_tai(tai_const.Create_nil_dataptr,voidpointertype);
  795. tcb.end_anonymous_record;
  796. if trecordsymtable(tabledef.symtable).datasize<>0 then
  797. begin
  798. s:=make_mangledname('THREADVARLIST',current_module.localsymtable,'');
  799. sym:=current_asmdata.DefineAsmSymbol(s,AB_GLOBAL,AT_DATA);
  800. current_asmdata.asmlists[al_globals].concatlist(
  801. tcb.get_final_asmlist(sym,tabledef,sec_data,s,sizeof(pint)));
  802. current_module.flags:=current_module.flags or uf_threadvars;
  803. end;
  804. tcb.Free;
  805. end;
  806. class procedure tnodeutils.InsertRuntimeInitsTablesTable(const prefix,tablename:string;unitflag:cardinal);
  807. var
  808. hp: tused_unit;
  809. tcb: ttai_typedconstbuilder;
  810. countplaceholder: ttypedconstplaceholder;
  811. count: longint;
  812. begin
  813. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable,tcalo_new_section]);
  814. tcb.begin_anonymous_record('',default_settings.packrecords,sizeof(pint),
  815. targetinfos[target_info.system]^.alignment.recordalignmin,
  816. targetinfos[target_info.system]^.alignment.maxCrecordalign
  817. );
  818. { placeholder for the count }
  819. countplaceholder:=tcb.emit_placeholder(ptruinttype);
  820. count:=0;
  821. hp:=tused_unit(usedunits.first);
  822. while assigned(hp) do
  823. begin
  824. if (hp.u.flags and unitflag)=unitflag then
  825. begin
  826. tcb.emit_tai(
  827. Tai_const.Createname(make_mangledname(prefix,hp.u.globalsymtable,''),0),
  828. voidcodepointertype);
  829. inc(count);
  830. end;
  831. hp:=tused_unit(hp.next);
  832. end;
  833. { Add items from program, if any }
  834. if (current_module.flags and unitflag)=unitflag then
  835. begin
  836. tcb.emit_tai(
  837. Tai_const.Createname(make_mangledname(prefix,current_module.localsymtable,''),0),
  838. voidcodepointertype);
  839. inc(count);
  840. end;
  841. { Insert TableCount at start }
  842. countplaceholder.replace(Tai_const.Create_pint(count),ptruinttype);
  843. countplaceholder.free;
  844. { insert in data segment }
  845. current_asmdata.asmlists[al_globals].concatlist(
  846. tcb.get_final_asmlist(
  847. current_asmdata.DefineAsmSymbol(tablename,AB_GLOBAL,AT_DATA),
  848. tcb.end_anonymous_record,
  849. sec_data,tablename,sizeof(pint)
  850. )
  851. );
  852. tcb.free;
  853. end;
  854. class procedure tnodeutils.InsertRuntimeInits(const prefix:string;list:TLinkedList;unitflag:cardinal);
  855. var
  856. s: string;
  857. item: TTCInitItem;
  858. begin
  859. item:=TTCInitItem(list.First);
  860. if item=nil then
  861. exit;
  862. s:=make_mangledname(prefix,current_module.localsymtable,'');
  863. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  864. new_section(current_asmdata.asmlists[al_globals],sec_data,s,sizeof(pint));
  865. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  866. repeat
  867. { optimize away unused local/static symbols }
  868. if (item.sym.refs>0) or (item.sym.owner.symtabletype=globalsymtable) then
  869. begin
  870. { address to initialize }
  871. current_asmdata.asmlists[al_globals].concat(Tai_const.createname(item.sym.mangledname, item.offset));
  872. { value with which to initialize }
  873. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(item.datalabel));
  874. end;
  875. item:=TTCInitItem(item.Next);
  876. until item=nil;
  877. { end-of-list marker }
  878. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  879. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname(s));
  880. current_module.flags:=current_module.flags or unitflag;
  881. end;
  882. class procedure tnodeutils.InsertWideInits;
  883. begin
  884. InsertRuntimeInits('WIDEINITS',current_asmdata.WideInits,uf_wideinits);
  885. end;
  886. class procedure tnodeutils.InsertResStrInits;
  887. begin
  888. InsertRuntimeInits('RESSTRINITS',current_asmdata.ResStrInits,uf_resstrinits);
  889. end;
  890. class procedure tnodeutils.InsertWideInitsTablesTable;
  891. begin
  892. InsertRuntimeInitsTablesTable('WIDEINITS','FPC_WIDEINITTABLES',uf_wideinits);
  893. end;
  894. class procedure tnodeutils.InsertResStrTablesTable;
  895. begin
  896. InsertRuntimeInitsTablesTable('RESSTRINITS','FPC_RESSTRINITTABLES',uf_resstrinits);
  897. end;
  898. class procedure tnodeutils.InsertResourceTablesTable;
  899. var
  900. hp : tmodule;
  901. count : longint;
  902. tcb : ttai_typedconstbuilder;
  903. countplaceholder : ttypedconstplaceholder;
  904. begin
  905. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable,tcalo_new_section]);
  906. count:=0;
  907. hp:=tmodule(loaded_units.first);
  908. tcb.begin_anonymous_record('',default_settings.packrecords,sizeof(pint),
  909. targetinfos[target_info.system]^.alignment.recordalignmin,
  910. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  911. countplaceholder:=tcb.emit_placeholder(ptruinttype);
  912. while assigned(hp) do
  913. begin
  914. If (hp.flags and uf_has_resourcestrings)=uf_has_resourcestrings then
  915. begin
  916. tcb.emit_tai(Tai_const.Create_sym(
  917. ctai_typedconstbuilder.get_vectorized_dead_strip_section_symbol_start('RESSTR',hp.localsymtable,false)),
  918. voidpointertype
  919. );
  920. tcb.emit_tai(Tai_const.Create_sym(
  921. ctai_typedconstbuilder.get_vectorized_dead_strip_section_symbol_end('RESSTR',hp.localsymtable,false)),
  922. voidpointertype
  923. );
  924. inc(count);
  925. end;
  926. hp:=tmodule(hp.next);
  927. end;
  928. { Insert TableCount at start }
  929. countplaceholder.replace(Tai_const.Create_pint(count),ptruinttype);
  930. countplaceholder.free;
  931. { Add to data segment }
  932. current_asmdata.AsmLists[al_globals].concatList(
  933. tcb.get_final_asmlist(
  934. current_asmdata.DefineAsmSymbol('FPC_RESOURCESTRINGTABLES',AB_GLOBAL,AT_DATA),
  935. tcb.end_anonymous_record,sec_rodata,'FPC_RESOURCESTRINGTABLES',sizeof(pint)
  936. )
  937. );
  938. tcb.free;
  939. end;
  940. class procedure tnodeutils.InsertResourceInfo(ResourcesUsed: boolean);
  941. var
  942. tcb: ttai_typedconstbuilder;
  943. begin
  944. if (target_res.id in [res_elf,res_macho,res_xcoff]) then
  945. begin
  946. tcb:=ctai_typedconstbuilder.create([tcalo_new_section,tcalo_make_dead_strippable]);
  947. if ResourcesUsed then
  948. tcb.emit_tai(Tai_const.Createname('FPC_RESSYMBOL',0),voidpointertype)
  949. else
  950. { Nil pointer to resource information }
  951. tcb.emit_tai(tai_const.Create_nil_dataptr,voidpointertype);
  952. current_asmdata.asmlists[al_globals].concatList(
  953. tcb.get_final_asmlist(
  954. current_asmdata.DefineAsmSymbol('FPC_RESLOCATION',AB_GLOBAL,AT_DATA),
  955. voidpointertype,
  956. sec_rodata,
  957. 'FPC_RESLOCATION',
  958. sizeof(puint)
  959. )
  960. );
  961. tcb.free;
  962. end;
  963. end;
  964. class procedure tnodeutils.InsertMemorySizes;
  965. var
  966. tcb: ttai_typedconstbuilder;
  967. s: shortstring;
  968. sym: tasmsymbol;
  969. def: tdef;
  970. begin
  971. { Insert Ident of the compiler in the .fpc.version section }
  972. tcb:=ctai_typedconstbuilder.create([tcalo_no_dead_strip]);
  973. s:='FPC '+full_version_string+
  974. ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname;
  975. def:=carraydef.getreusable(cansichartype,length(s));
  976. tcb.maybe_begin_aggregate(def);
  977. tcb.emit_tai(Tai_string.Create(s),def);
  978. tcb.maybe_end_aggregate(def);
  979. sym:=current_asmdata.DefineAsmSymbol('__fpc_ident',AB_LOCAL,AT_DATA);
  980. current_asmdata.asmlists[al_globals].concatlist(
  981. tcb.get_final_asmlist(sym,def,sec_fpc,'version',const_align(32))
  982. );
  983. tcb.free;
  984. if not(tf_no_generic_stackcheck in target_info.flags) then
  985. begin
  986. { stacksize can be specified and is now simulated }
  987. tcb:=ctai_typedconstbuilder.create([tcalo_new_section,tcalo_make_dead_strippable]);
  988. tcb.emit_tai(Tai_const.Create_pint(stacksize),ptruinttype);
  989. sym:=current_asmdata.DefineAsmSymbol('__stklen',AB_GLOBAL,AT_DATA);
  990. current_asmdata.asmlists[al_globals].concatlist(
  991. tcb.get_final_asmlist(sym,ptruinttype,sec_data,'__stklen',sizeof(pint))
  992. );
  993. tcb.free;
  994. end;
  995. {$IFDEF POWERPC}
  996. { AmigaOS4 "stack cookie" support }
  997. if ( target_info.system = system_powerpc_amiga ) then
  998. begin
  999. { this symbol is needed to ignite powerpc amigaos' }
  1000. { stack allocation magic for us with the given stack size. }
  1001. { note: won't work for m68k amigaos or morphos. (KB) }
  1002. str(stacksize,s);
  1003. s:='$STACK: '+s+#0;
  1004. def:=carraydef.getreusable(cansichartype,length(s));
  1005. tcb:=ctai_typedconstbuilder.create([tcalo_new_section]);
  1006. tcb.maybe_begin_aggregate(def);
  1007. tcb.emit_tai(Tai_string.Create(s),def);
  1008. tcb.maybe_end_aggregate(def);
  1009. sym:=current_asmdata.DefineAsmSymbol('__stack_cookie',AB_GLOBAL,AT_DATA);
  1010. current_asmdata.asmlists[al_globals].concatlist(
  1011. tcb.get_final_asmlist(sym,def,sec_data,'__stack_cookie',sizeof(pint))
  1012. );
  1013. tcb.free;
  1014. end;
  1015. {$ENDIF POWERPC}
  1016. { Initial heapsize }
  1017. tcb:=ctai_typedconstbuilder.create([tcalo_new_section,tcalo_make_dead_strippable]);
  1018. tcb.emit_tai(Tai_const.Create_pint(heapsize),ptruinttype);
  1019. sym:=current_asmdata.DefineAsmSymbol('__heapsize',AB_GLOBAL,AT_DATA);
  1020. current_asmdata.asmlists[al_globals].concatlist(
  1021. tcb.get_final_asmlist(sym,ptruinttype,sec_data,'__heapsize',sizeof(pint))
  1022. );
  1023. tcb.free;
  1024. { allocate an initial heap on embedded systems }
  1025. if target_info.system in systems_embedded then
  1026. begin
  1027. { tai_datablock cannot yet be handled via the high level typed const
  1028. builder, because it implies the generation of a symbol, while this
  1029. is separate in the builder }
  1030. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  1031. new_section(current_asmdata.asmlists[al_globals],sec_bss,'__fpc_initialheap',current_settings.alignment.varalignmax);
  1032. current_asmdata.asmlists[al_globals].concat(tai_datablock.Create_global('__fpc_initialheap',heapsize));
  1033. end;
  1034. { Valgrind usage }
  1035. tcb:=ctai_typedconstbuilder.create([tcalo_new_section,tcalo_make_dead_strippable]);
  1036. tcb.emit_ord_const(byte(cs_gdb_valgrind in current_settings.globalswitches),u8inttype);
  1037. sym:=current_asmdata.DefineAsmSymbol('__fpc_valgrind',AB_GLOBAL,AT_DATA);
  1038. current_asmdata.asmlists[al_globals].concatlist(
  1039. tcb.get_final_asmlist(sym,ptruinttype,sec_data,'__fpc_valgrind',sizeof(pint))
  1040. );
  1041. tcb.free;
  1042. end;
  1043. class procedure tnodeutils.InsertObjectInfo;
  1044. begin
  1045. { don't do anything by default }
  1046. end;
  1047. class procedure tnodeutils.add_main_procdef_paras(pd: tdef);
  1048. var
  1049. pvs: tparavarsym;
  1050. begin
  1051. { stub for calling FPC_SYSTEMMAIN from the C main -> add argc/argv/argp }
  1052. if (tprocdef(pd).proctypeoption=potype_mainstub) and
  1053. (target_info.system in (systems_darwin+[system_powerpc_macos]+systems_aix)) then
  1054. begin
  1055. pvs:=cparavarsym.create('ARGC',1,vs_const,s32inttype,[]);
  1056. tprocdef(pd).parast.insert(pvs);
  1057. pvs:=cparavarsym.create('ARGV',2,vs_const,cpointerdef.getreusable(charpointertype),[]);
  1058. tprocdef(pd).parast.insert(pvs);
  1059. pvs:=cparavarsym.create('ARGP',3,vs_const,cpointerdef.getreusable(charpointertype),[]);
  1060. tprocdef(pd).parast.insert(pvs);
  1061. tprocdef(pd).calcparas;
  1062. end;
  1063. end;
  1064. end.