ncgvmt.pas 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generates VMT for classes/objects and interface wrappers
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ncgvmt;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. aasmdata,aasmbase,aasmcnst,
  22. symbase,symtype,symdef;
  23. type
  24. pprocdeftree = ^tprocdeftree;
  25. tprocdeftree = record
  26. data : tprocdef;
  27. nl : tasmlabel;
  28. l,r : pprocdeftree;
  29. end;
  30. TVMTWriter=class
  31. private
  32. _Class : tobjectdef;
  33. { message tables }
  34. root : pprocdeftree;
  35. procedure disposeprocdeftree(p : pprocdeftree);
  36. procedure insertmsgint(p:TObject;arg:pointer);
  37. procedure insertmsgstr(p:TObject;arg:pointer);
  38. procedure insertint(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  39. procedure insertstr(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  40. function RedirectToEmpty(procdef: tprocdef): boolean;
  41. procedure writenames(list : TAsmList;p : pprocdeftree);
  42. procedure writeintentry(tcb: ttai_typedconstbuilder; p: pprocdeftree; entrydef: tdef);
  43. procedure writestrentry(tcb: ttai_typedconstbuilder; p: pprocdeftree; entrydef: tdef);
  44. {$ifdef WITHDMT}
  45. { dmt }
  46. procedure insertdmtentry(p:TObject;arg:pointer);
  47. procedure writedmtindexentry(p : pprocdeftree);
  48. procedure writedmtaddressentry(p : pprocdeftree);
  49. {$endif}
  50. { published methods }
  51. procedure do_count_published_methods(p:TObject;arg:pointer);
  52. procedure do_gen_published_methods(p:TObject;arg:pointer);
  53. { virtual methods }
  54. procedure writevirtualmethods(List:TAsmList);
  55. { interface tables }
  56. function intf_get_vtbl_name(AImplIntf:TImplementedInterface): string;
  57. procedure intf_create_vtbl(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  58. procedure intf_gen_intf_ref(tcb: ttai_typedconstbuilder; AImplIntf: TImplementedInterface; interfaceentrydef, interfaceentrytypedef: tdef);
  59. function intf_write_table(list : TAsmList):TAsmLabel;
  60. { get a table def of the form
  61. record
  62. count: countdef;
  63. elements: array[0..count-1] of elementdef
  64. end;
  65. Returns both the outer record and the inner arraydef
  66. }
  67. procedure gettabledef(const basename: string; countdef, elementdef: tdef; count: longint; packrecords: shortint; out recdef: trecorddef; out arrdef: tarraydef);
  68. function getrecorddef(const name: string; const fields: array of tdef; packrecords: shortint): trecorddef;
  69. { generates the message tables for a class }
  70. function genstrmsgtab(list : TAsmList) : tasmlabel;
  71. function genintmsgtab(list : TAsmList) : tasmlabel;
  72. function genpublishedmethodstable(list : TAsmList) : tasmlabel;
  73. function generate_field_table(list : TAsmList) : tasmlabel;
  74. procedure generate_abstract_stub(list:TAsmList;pd:tprocdef);
  75. {$ifdef WITHDMT}
  76. { generates a DMT for _class }
  77. function gendmt : tasmlabel;
  78. {$endif WITHDMT}
  79. public
  80. constructor create(c:tobjectdef); virtual;
  81. { write the VMT to al_globals }
  82. procedure writevmt;
  83. procedure writeinterfaceids(list: TAsmList);
  84. { should the VMT writer be used at all (e.g., not for the JVM target) }
  85. class function use_vmt_writer: boolean; virtual;
  86. end;
  87. TVMTWriterClass = class of TVMTWriter;
  88. { generate persistent type information like VMT, RTTI and inittables }
  89. procedure write_persistent_type_info(st:tsymtable;is_global:boolean);
  90. var
  91. CVMTWriter: TVMTWriterClass = TVMTWriter;
  92. implementation
  93. uses
  94. cutils,cclasses,
  95. globtype,globals,verbose,constexp,
  96. systems,fmodule,
  97. symconst,symsym,symtable,defutil,
  98. aasmtai,
  99. wpobase,
  100. nobj,
  101. cgbase,parabase,paramgr,cgobj,cgcpu,hlcgobj,hlcgcpu,
  102. ncgrtti;
  103. {*****************************************************************************
  104. TVMTWriter
  105. *****************************************************************************}
  106. constructor TVMTWriter.create(c:tobjectdef);
  107. begin
  108. inherited Create;
  109. _Class:=c;
  110. end;
  111. {**************************************
  112. Message Tables
  113. **************************************}
  114. procedure TVMTWriter.disposeprocdeftree(p : pprocdeftree);
  115. begin
  116. if assigned(p^.l) then
  117. disposeprocdeftree(p^.l);
  118. if assigned(p^.r) then
  119. disposeprocdeftree(p^.r);
  120. dispose(p);
  121. end;
  122. procedure TVMTWriter.insertint(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  123. begin
  124. if at=nil then
  125. begin
  126. at:=p;
  127. inc(count);
  128. end
  129. else
  130. begin
  131. if p^.data.messageinf.i<at^.data.messageinf.i then
  132. insertint(p,at^.l,count)
  133. else if p^.data.messageinf.i>at^.data.messageinf.i then
  134. insertint(p,at^.r,count)
  135. else
  136. Message1(parser_e_duplicate_message_label,tostr(p^.data.messageinf.i));
  137. end;
  138. end;
  139. procedure TVMTWriter.insertstr(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  140. var
  141. i : integer;
  142. begin
  143. if at=nil then
  144. begin
  145. at:=p;
  146. inc(count);
  147. end
  148. else
  149. begin
  150. i:=CompareStr(p^.data.messageinf.str^,at^.data.messageinf.str^);
  151. if i<0 then
  152. insertstr(p,at^.l,count)
  153. else if i>0 then
  154. insertstr(p,at^.r,count)
  155. else
  156. Message1(parser_e_duplicate_message_label,p^.data.messageinf.str^);
  157. end;
  158. end;
  159. procedure TVMTWriter.insertmsgint(p:TObject;arg:pointer);
  160. var
  161. i : longint;
  162. pd : Tprocdef;
  163. pt : pprocdeftree;
  164. begin
  165. if tsym(p).typ<>procsym then
  166. exit;
  167. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  168. begin
  169. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  170. if po_msgint in pd.procoptions then
  171. begin
  172. new(pt);
  173. pt^.data:=pd;
  174. pt^.l:=nil;
  175. pt^.r:=nil;
  176. insertint(pt,root,plongint(arg)^);
  177. end;
  178. end;
  179. end;
  180. procedure TVMTWriter.insertmsgstr(p:TObject;arg:pointer);
  181. var
  182. i : longint;
  183. pd : Tprocdef;
  184. pt : pprocdeftree;
  185. begin
  186. if tsym(p).typ<>procsym then
  187. exit;
  188. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  189. begin
  190. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  191. if po_msgstr in pd.procoptions then
  192. begin
  193. new(pt);
  194. pt^.data:=pd;
  195. pt^.l:=nil;
  196. pt^.r:=nil;
  197. insertstr(pt,root,plongint(arg)^);
  198. end;
  199. end;
  200. end;
  201. procedure TVMTWriter.writenames(list : TAsmList;p : pprocdeftree);
  202. var
  203. ca : pchar;
  204. len : byte;
  205. tcb : ttai_typedconstbuilder;
  206. begin
  207. current_asmdata.getglobaldatalabel(p^.nl);
  208. if assigned(p^.l) then
  209. writenames(list,p^.l);
  210. tcb:=ctai_typedconstbuilder.create([tcalo_is_lab]);
  211. len:=length(p^.data.messageinf.str^);
  212. tcb.maybe_begin_aggregate(getarraydef(cansichartype,len+1));
  213. tcb.emit_tai(tai_const.create_8bit(len),cansichartype);
  214. getmem(ca,len+1);
  215. move(p^.data.messageinf.str^[1],ca^,len);
  216. ca[len]:=#0;
  217. tcb.emit_tai(Tai_string.Create_pchar(ca,len),getarraydef(cansichartype,len));
  218. tcb.maybe_end_aggregate(getarraydef(cansichartype,len+1));
  219. list.concatList(tcb.get_final_asmlist(p^.nl,getarraydef(cansichartype,len+1),sec_rodata_norel,'',sizeof(pint)));
  220. tcb.free;
  221. if assigned(p^.r) then
  222. writenames(list,p^.r);
  223. end;
  224. procedure TVMTWriter.writestrentry(tcb: ttai_typedconstbuilder; p: pprocdeftree; entrydef: tdef);
  225. begin
  226. if assigned(p^.l) then
  227. writestrentry(tcb,p^.l,entrydef);
  228. { write name label }
  229. tcb.maybe_begin_aggregate(entrydef);
  230. tcb.emit_tai(Tai_const.Create_sym(p^.nl),getpointerdef(getarraydef(cansichartype,length(p^.data.messageinf.str^)+1)));
  231. tcb.queue_init(voidcodepointertype);
  232. tcb.queue_emit_proc(p^.data);
  233. tcb.maybe_end_aggregate(entrydef);
  234. if assigned(p^.r) then
  235. writestrentry(tcb,p^.r,entrydef);
  236. end;
  237. function TVMTWriter.genstrmsgtab(list : TAsmList) : tasmlabel;
  238. var
  239. count : longint;
  240. tcb: ttai_typedconstbuilder;
  241. msgstrtabdef: trecorddef;
  242. msgstrentry: tdef;
  243. msgarraydef: tarraydef;
  244. begin
  245. root:=nil;
  246. count:=0;
  247. { insert all message handlers into a tree, sorted by name }
  248. _class.symtable.SymList.ForEachCall(@insertmsgstr,@count);
  249. tcb:=ctai_typedconstbuilder.create([tcalo_is_lab]);
  250. { write all names }
  251. if assigned(root) then
  252. writenames(list,root);
  253. { now start writing of the message string table }
  254. current_asmdata.getlabel(result,alt_data);
  255. {
  256. TStringMessageTable = record
  257. count : longint;
  258. msgstrtable : array[0..0] of tmsgstrtable;
  259. end;
  260. Instead of 0 as the upper bound, use the actual upper bound
  261. }
  262. msgstrentry:=search_system_type('TMSGSTRTABLE').typedef;
  263. gettabledef('fpc_intern_TStringMessageTable_',s32inttype,msgstrentry,count,0,msgstrtabdef,msgarraydef);
  264. { outer record (TStringMessageTable) }
  265. tcb.maybe_begin_aggregate(msgstrtabdef);
  266. tcb.emit_tai(Tai_const.Create_32bit(count),s32inttype);
  267. if assigned(root) then
  268. begin
  269. { array of TMsgStrTable }
  270. tcb.maybe_begin_aggregate(msgarraydef);
  271. writestrentry(tcb,root,msgstrentry);
  272. tcb.maybe_end_aggregate(msgarraydef);
  273. disposeprocdeftree(root);
  274. end;
  275. tcb.maybe_end_aggregate(msgstrtabdef);
  276. list.concatList(tcb.get_final_asmlist(result,msgstrtabdef,sec_rodata,'',sizeof(pint)));
  277. tcb.free;
  278. end;
  279. procedure TVMTWriter.writeintentry(tcb: ttai_typedconstbuilder; p: pprocdeftree; entrydef: tdef);
  280. begin
  281. if assigned(p^.l) then
  282. writeintentry(tcb,p^.l,entrydef);
  283. tcb.maybe_begin_aggregate(entrydef);
  284. { write integer dispatch number }
  285. tcb.emit_tai(Tai_const.Create_32bit(p^.data.messageinf.i),u32inttype);
  286. tcb.queue_init(voidcodepointertype);
  287. tcb.queue_emit_proc(p^.data);
  288. tcb.maybe_end_aggregate(entrydef);
  289. if assigned(p^.r) then
  290. writeintentry(tcb,p^.r,entrydef);
  291. end;
  292. function TVMTWriter.genintmsgtab(list : TAsmList) : tasmlabel;
  293. var
  294. r : tasmlabel;
  295. count : longint;
  296. tcb: ttai_typedconstbuilder;
  297. msgintdef: trecorddef;
  298. msginttabledef: trecorddef;
  299. msgintarrdef: tarraydef;
  300. begin
  301. root:=nil;
  302. count:=0;
  303. { insert all message handlers into a tree, sorted by name }
  304. _class.symtable.SymList.ForEachCall(@insertmsgint,@count);
  305. { now start writing of the message string table }
  306. { from objpas.inc:
  307. TMsgIntTable = record
  308. index : dword;
  309. method : codepointer;
  310. end;
  311. }
  312. msginttabledef:=getrecorddef('fpc_intern_msgint_table',[u32inttype,voidcodepointertype],0);
  313. { from objpas.inc:
  314. TMsgInt = record
  315. count : longint;
  316. msgs : array[0..0] of TMsgIntTable;
  317. end;
  318. }
  319. current_asmdata.getlabel(r,alt_data);
  320. tcb:=ctai_typedconstbuilder.create([tcalo_is_lab]);
  321. genintmsgtab:=r;
  322. gettabledef('fpc_msgint_table_entries_',s32inttype,msginttabledef,count,0,msgintdef,msgintarrdef);
  323. tcb.maybe_begin_aggregate(msgintdef);
  324. tcb.emit_tai(Tai_const.Create_32bit(count),s32inttype);
  325. if assigned(root) then
  326. begin
  327. tcb.maybe_begin_aggregate(msgintarrdef);
  328. writeintentry(tcb,root,msginttabledef);
  329. tcb.maybe_end_aggregate(msgintarrdef);
  330. disposeprocdeftree(root);
  331. end;
  332. tcb.maybe_end_aggregate(msgintdef);
  333. list.concatList(tcb.get_final_asmlist(result,msgintdef,sec_rodata,'',sizeof(pint)));
  334. tcb.free;
  335. end;
  336. {$ifdef WITHDMT}
  337. {**************************************
  338. DMT
  339. **************************************}
  340. procedure TVMTWriter.insertdmtentry(p:TObject;arg:pointer);
  341. var
  342. hp : tprocdef;
  343. pt : pprocdeftree;
  344. begin
  345. if tsym(p).typ=procsym then
  346. begin
  347. hp:=tprocsym(p).definition;
  348. while assigned(hp) do
  349. begin
  350. if (po_msgint in hp.procoptions) then
  351. begin
  352. new(pt);
  353. pt^.p:=hp;
  354. pt^.l:=nil;
  355. pt^.r:=nil;
  356. insertint(pt,root);
  357. end;
  358. hp:=hp.nextoverloaded;
  359. end;
  360. end;
  361. end;
  362. procedure TVMTWriter.writedmtindexentry(p : pprocdeftree);
  363. begin
  364. if assigned(p^.l) then
  365. writedmtindexentry(p^.l);
  366. al_globals.concat(Tai_const.Create_32bit(p^.data.messageinf.i));
  367. if assigned(p^.r) then
  368. writedmtindexentry(p^.r);
  369. end;
  370. procedure TVMTWriter.writedmtaddressentry(p : pprocdeftree);
  371. begin
  372. if assigned(p^.l) then
  373. writedmtaddressentry(p^.l);
  374. al_globals.concat(Tai_const_symbol.Createname(p^.data.mangledname,0));
  375. if assigned(p^.r) then
  376. writedmtaddressentry(p^.r);
  377. end;
  378. function TVMTWriter.gendmt : tasmlabel;
  379. var
  380. r : tasmlabel;
  381. begin
  382. root:=nil;
  383. count:=0;
  384. gendmt:=nil;
  385. { insert all message handlers into a tree, sorted by number }
  386. _class.symtable.SymList.ForEachCall(insertdmtentry);
  387. if count>0 then
  388. begin
  389. current_asmdata.getglobaldatalabel(r);
  390. gendmt:=r;
  391. al_globals.concat(cai_align.create(const_align(sizeof(pint))));
  392. al_globals.concat(Tai_label.Create(r));
  393. { entries for caching }
  394. al_globals.concat(Tai_const.Create_ptr(0));
  395. al_globals.concat(Tai_const.Create_ptr(0));
  396. al_globals.concat(Tai_const.Create_32bit(count));
  397. if assigned(root) then
  398. begin
  399. writedmtindexentry(root);
  400. writedmtaddressentry(root);
  401. disposeprocdeftree(root);
  402. end;
  403. end;
  404. end;
  405. {$endif WITHDMT}
  406. {**************************************
  407. Published Methods
  408. **************************************}
  409. procedure TVMTWriter.do_count_published_methods(p:TObject;arg:pointer);
  410. var
  411. i : longint;
  412. pd : tprocdef;
  413. begin
  414. if (tsym(p).typ<>procsym) then
  415. exit;
  416. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  417. begin
  418. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  419. if (pd.procsym=tsym(p)) and
  420. (pd.visibility=vis_published) then
  421. inc(plongint(arg)^);
  422. end;
  423. end;
  424. type
  425. tvmtasmoutput = record
  426. pubmethodstcb: ttai_typedconstbuilder;
  427. list: tasmlist;
  428. methodnamerec: trecorddef;
  429. end;
  430. pvmtasmoutput = ^tvmtasmoutput;
  431. procedure TVMTWriter.do_gen_published_methods(p:TObject;arg:pointer);
  432. var
  433. i : longint;
  434. l : tasmlabel;
  435. pd : tprocdef;
  436. lists: pvmtasmoutput absolute arg;
  437. tcb : ttai_typedconstbuilder;
  438. namedef : tdef;
  439. begin
  440. if (tsym(p).typ<>procsym) then
  441. exit;
  442. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  443. begin
  444. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  445. if (pd.procsym=tsym(p)) and
  446. (pd.visibility=vis_published) then
  447. begin
  448. current_asmdata.getlabel(l,alt_data);
  449. { l: name_of_method }
  450. tcb:=ctai_typedconstbuilder.create([tcalo_is_lab]);
  451. namedef:=tcb.emit_shortstring_const(tsym(p).realname);
  452. lists^.list.concatList(tcb.get_final_asmlist(l,namedef,sec_rodata_norel,'',sizeof(pint)));
  453. tcb.free;
  454. { the tmethodnamerec }
  455. lists^.pubmethodstcb.maybe_begin_aggregate(lists^.methodnamerec);
  456. { convert the pointer to the name into a generic pshortstring,
  457. so all entries can share the same recorddef }
  458. lists^.pubmethodstcb.queue_init(charpointertype);
  459. lists^.pubmethodstcb.queue_emit_asmsym(l,namedef);
  460. if po_abstractmethod in pd.procoptions then
  461. lists^.pubmethodstcb.emit_tai(Tai_const.Create_nil_codeptr,voidcodepointertype)
  462. else
  463. begin
  464. { convert the procdef in a generic voidcodepointer, same
  465. reason as above }
  466. lists^.pubmethodstcb.queue_init(voidcodepointertype);
  467. lists^.pubmethodstcb.queue_emit_proc(pd);
  468. end;
  469. lists^.pubmethodstcb.maybe_end_aggregate(lists^.methodnamerec);
  470. end;
  471. end;
  472. end;
  473. function TVMTWriter.genpublishedmethodstable(list : TAsmList) : tasmlabel;
  474. var
  475. l : tasmlabel;
  476. count : longint;
  477. lists : tvmtasmoutput;
  478. pubmethodsdef: trecorddef;
  479. pubmethodsarraydef: tarraydef;
  480. begin
  481. count:=0;
  482. _class.symtable.SymList.ForEachCall(@do_count_published_methods,@count);
  483. if count>0 then
  484. begin
  485. lists.list:=list;
  486. { in the list of the published methods (from objpas.inc):
  487. tmethodnamerec = packed record
  488. name : pshortstring;
  489. addr : codepointer;
  490. end;
  491. }
  492. lists.methodnamerec:=getrecorddef('fpc_intern_tmethodnamerec',[getpointerdef(cshortstringtype),voidcodepointertype],1);
  493. { from objpas.inc:
  494. tmethodnametable = packed record
  495. count : dword;
  496. entries : packed array[0..0] of tmethodnamerec;
  497. end;
  498. }
  499. lists.pubmethodstcb:=ctai_typedconstbuilder.create([tcalo_is_lab]);
  500. current_asmdata.getlabel(l,alt_data);
  501. gettabledef('fpc_intern_tmethodnametable_',u32inttype,lists.methodnamerec,count,1,pubmethodsdef,pubmethodsarraydef);
  502. { begin tmethodnametable }
  503. lists.pubmethodstcb.maybe_begin_aggregate(pubmethodsdef);
  504. { emit count field }
  505. lists.pubmethodstcb.emit_tai(Tai_const.Create_32bit(count),u32inttype);
  506. { begin entries field (array) }
  507. lists.pubmethodstcb.maybe_begin_aggregate(pubmethodsarraydef);
  508. { add all entries elements }
  509. _class.symtable.SymList.ForEachCall(@do_gen_published_methods,@lists);
  510. { end entries field (array) }
  511. lists.pubmethodstcb.maybe_end_aggregate(pubmethodsarraydef);
  512. { end methodnametable }
  513. lists.pubmethodstcb.maybe_end_aggregate(pubmethodsdef);
  514. list.concatlist(lists.pubmethodstcb.get_final_asmlist(l,pubmethodsdef,sec_rodata,'',sizeof(pint)));
  515. lists.pubmethodstcb.free;
  516. genpublishedmethodstable:=l;
  517. end
  518. else
  519. genpublishedmethodstable:=nil;
  520. end;
  521. function TVMTWriter.generate_field_table(list : TAsmList) : tasmlabel;
  522. var
  523. i : longint;
  524. sym : tsym;
  525. fieldtable,
  526. classtable : tasmlabel;
  527. classindex,
  528. fieldcount : longint;
  529. classtablelist : TFPList;
  530. tcb: ttai_typedconstbuilder;
  531. packrecords: longint;
  532. classdef: tobjectdef;
  533. classtabledef,
  534. fieldtabledef: trecorddef;
  535. begin
  536. classtablelist:=TFPList.Create;
  537. { retrieve field info fields }
  538. fieldcount:=0;
  539. for i:=0 to _class.symtable.SymList.Count-1 do
  540. begin
  541. sym:=tsym(_class.symtable.SymList[i]);
  542. if (sym.typ=fieldvarsym) and
  543. (sym.visibility=vis_published) then
  544. begin
  545. if tfieldvarsym(sym).vardef.typ<>objectdef then
  546. internalerror(200611032);
  547. classindex:=classtablelist.IndexOf(tfieldvarsym(sym).vardef);
  548. if classindex=-1 then
  549. classtablelist.Add(tfieldvarsym(sym).vardef);
  550. inc(fieldcount);
  551. end;
  552. end;
  553. if fieldcount>0 then
  554. begin
  555. current_asmdata.getlabel(fieldtable,alt_data);
  556. current_asmdata.getlabel(classtable,alt_data);
  557. if (tf_requires_proper_alignment in target_info.flags) then
  558. packrecords:=0
  559. else
  560. packrecords:=1;
  561. { generate the class table }
  562. tcb:=ctai_typedconstbuilder.create([tcalo_is_lab]);
  563. tcb.begin_anonymous_record('$fpc_intern_classtable_'+tostr(classtablelist.Count-1),
  564. packrecords,
  565. targetinfos[target_info.system]^.alignment.recordalignmin,
  566. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  567. tcb.emit_tai(Tai_const.Create_16bit(classtablelist.count),u16inttype);
  568. for i:=0 to classtablelist.Count-1 do
  569. begin
  570. classdef:=tobjectdef(classtablelist[i]);
  571. { type of the field }
  572. tcb.queue_init(voidpointertype);
  573. { reference to the vmt }
  574. tcb.queue_emit_asmsym(
  575. current_asmdata.RefAsmSymbol(classdef.vmt_mangledname,AT_DATA),
  576. tfieldvarsym(classdef.vmt_field).vardef);
  577. end;
  578. classtabledef:=tcb.end_anonymous_record;
  579. list.concatlist(tcb.get_final_asmlist(classtable,classtabledef,sec_rodata,'',sizeof(pint)));
  580. tcb.free;
  581. { write fields }
  582. {
  583. TFieldTable =
  584. $ifndef FPC_REQUIRES_PROPER_ALIGNMENT
  585. packed
  586. $endif FPC_REQUIRES_PROPER_ALIGNMENT
  587. record
  588. FieldCount: Word;
  589. ClassTable: Pointer;
  590. Fields: array[0..0] of TFieldInfo
  591. end;
  592. }
  593. tcb:=ctai_typedconstbuilder.create([tcalo_is_lab]);
  594. { can't easily specify a name here for reuse of the constructed def,
  595. since it's full of variable length shortstrings (-> all of those
  596. lengths and their order would have to incorporated in the name,
  597. plus there would be very little chance that it could actually be
  598. reused }
  599. tcb.begin_anonymous_record('',packrecords,
  600. targetinfos[target_info.system]^.alignment.recordalignmin,
  601. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  602. tcb.emit_tai(Tai_const.Create_16bit(fieldcount),u16inttype);
  603. tcb.emit_tai(Tai_const.Create_sym(classtable),getpointerdef(classtabledef));
  604. for i:=0 to _class.symtable.SymList.Count-1 do
  605. begin
  606. sym:=tsym(_class.symtable.SymList[i]);
  607. if (sym.typ=fieldvarsym) and
  608. (sym.visibility=vis_published) then
  609. begin
  610. {
  611. TFieldInfo =
  612. $ifndef FPC_REQUIRES_PROPER_ALIGNMENT
  613. packed
  614. $endif FPC_REQUIRES_PROPER_ALIGNMENT
  615. record
  616. FieldOffset: PtrUInt;
  617. ClassTypeIndex: Word;
  618. Name: ShortString;
  619. end;
  620. }
  621. tcb.begin_anonymous_record('$fpc_intern_fieldinfo_'+tostr(length(tfieldvarsym(sym).realname)),packrecords,
  622. targetinfos[target_info.system]^.alignment.recordalignmin,
  623. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  624. tcb.emit_tai(Tai_const.Create_pint(tfieldvarsym(sym).fieldoffset),ptruinttype);
  625. classindex:=classtablelist.IndexOf(tfieldvarsym(sym).vardef);
  626. if classindex=-1 then
  627. internalerror(200611033);
  628. tcb.emit_tai(Tai_const.Create_16bit(classindex+1),u16inttype);
  629. tcb.emit_shortstring_const(tfieldvarsym(sym).realname);
  630. tcb.end_anonymous_record;
  631. end;
  632. end;
  633. fieldtabledef:=tcb.end_anonymous_record;
  634. list.concatlist(tcb.get_final_asmlist(fieldtable,fieldtabledef,sec_rodata,'',sizeof(pint)));
  635. tcb.free;
  636. result:=fieldtable;
  637. end
  638. else
  639. result:=nil;
  640. classtablelist.free;
  641. end;
  642. {**************************************
  643. Interface tables
  644. **************************************}
  645. function TVMTWriter.intf_get_vtbl_name(AImplIntf:TImplementedInterface): string;
  646. begin
  647. result:=make_mangledname('VTBL',_class.owner,_class.objname^+'_$_'+AImplIntf.IntfDef.objname^);
  648. end;
  649. procedure TVMTWriter.intf_create_vtbl(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  650. var
  651. pd : tprocdef;
  652. vtblstr,
  653. hs : string;
  654. i : longint;
  655. begin
  656. vtblstr:=intf_get_vtbl_name(AImplIntf);
  657. rawdata.concat(cai_align.create(const_align(sizeof(pint))));
  658. rawdata.concat(tai_symbol.createname(vtblstr,AT_DATA,0));
  659. if assigned(AImplIntf.procdefs) then
  660. begin
  661. for i:=0 to AImplIntf.procdefs.count-1 do
  662. begin
  663. pd:=tprocdef(AImplIntf.procdefs[i]);
  664. hs:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+AImplIntf.IntfDef.objname^+'_$_'+
  665. tostr(i)+'_$_'+pd.mangledname);
  666. { create reference }
  667. rawdata.concat(Tai_const.Createname(hs,AT_FUNCTION,0));
  668. end;
  669. end;
  670. rawdata.concat(tai_symbol_end.createname(vtblstr));
  671. end;
  672. procedure TVMTWriter.intf_gen_intf_ref(tcb: ttai_typedconstbuilder; AImplIntf: TImplementedInterface; interfaceentrydef, interfaceentrytypedef: tdef);
  673. var
  674. pd: tprocdef;
  675. begin
  676. tcb.maybe_begin_aggregate(interfaceentrydef);
  677. { GUID (or nil for Corba interfaces) }
  678. if AImplIntf.IntfDef.objecttype in [odt_interfacecom] then
  679. tcb.emit_tai(Tai_const.CreateName(
  680. make_mangledname('IID',AImplIntf.IntfDef.owner,AImplIntf.IntfDef.objname^),AT_DATA,0),getpointerdef(rec_tguid))
  681. else
  682. tcb.emit_tai(Tai_const.Create_nil_dataptr,getpointerdef(rec_tguid));
  683. { VTable }
  684. tcb.queue_init(voidpointertype);
  685. tcb.queue_emit_asmsym(current_asmdata.RefAsmSymbol(intf_get_vtbl_name(AImplIntf.VtblImplIntf),AT_DATA),AImplIntf.VtblImplIntf.IntfDef);
  686. { IOffset field }
  687. case AImplIntf.VtblImplIntf.IType of
  688. etFieldValue, etFieldValueClass,
  689. etStandard:
  690. tcb.emit_tai(Tai_const.Create_pint(AImplIntf.VtblImplIntf.IOffset),ptruinttype);
  691. etStaticMethodResult, etStaticMethodClass:
  692. begin
  693. pd:=tprocdef(tpropertysym(AImplIntf.ImplementsGetter).propaccesslist[palt_read].procdef);
  694. tcb.queue_init(ptruinttype);
  695. tcb.queue_emit_proc(pd);
  696. end;
  697. etVirtualMethodResult, etVirtualMethodClass:
  698. begin
  699. pd:=tprocdef(tpropertysym(AImplIntf.ImplementsGetter).propaccesslist[palt_read].procdef);
  700. tcb.emit_tai(Tai_const.Create_pint(tobjectdef(pd.struct).vmtmethodoffset(pd.extnumber)),ptruinttype);
  701. end;
  702. else
  703. internalerror(200802162);
  704. end;
  705. { IIDStr }
  706. tcb.emit_tai(Tai_const.CreateName(
  707. make_mangledname('IIDSTR',AImplIntf.IntfDef.owner,AImplIntf.IntfDef.objname^),AT_DATA,0),getpointerdef(getarraydef(cansichartype,length(AImplIntf.IntfDef.iidstr^)+1)));
  708. { IType }
  709. tcb.emit_ord_const(aint(AImplIntf.VtblImplIntf.IType),interfaceentrytypedef);
  710. tcb.maybe_end_aggregate(interfaceentrydef);
  711. end;
  712. function TVMTWriter.intf_write_table(list : TAsmList):TAsmLabel;
  713. var
  714. i : longint;
  715. ImplIntf : TImplementedInterface;
  716. tcb : ttai_typedconstbuilder;
  717. tabledef : tdef;
  718. interfaceentrydef : tdef;
  719. interfaceentrytypedef: tdef;
  720. interfacearray: tdef;
  721. begin
  722. current_asmdata.getlabel(result,alt_data);
  723. tcb:=ctai_typedconstbuilder.create([tcalo_is_lab]);
  724. tcb.begin_anonymous_record('',0,
  725. targetinfos[target_info.system]^.alignment.recordalignmin,
  726. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  727. tcb.emit_tai(Tai_const.Create_pint(_class.ImplementedInterfaces.count),search_system_type('SIZEUINT').typedef);
  728. interfaceentrydef:=search_system_type('TINTERFACEENTRY').typedef;
  729. interfaceentrytypedef:=search_system_type('TINTERFACEENTRYTYPE').typedef;
  730. interfacearray:=getarraydef(interfaceentrydef,_class.ImplementedInterfaces.count);
  731. tcb.maybe_begin_aggregate(interfacearray);
  732. { Write vtbl references }
  733. for i:=0 to _class.ImplementedInterfaces.count-1 do
  734. begin
  735. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  736. intf_gen_intf_ref(tcb,ImplIntf,interfaceentrydef,interfaceentrytypedef);
  737. end;
  738. tcb.maybe_end_aggregate(interfacearray);
  739. tabledef:=tcb.end_anonymous_record;
  740. list.concatlist(tcb.get_final_asmlist(result,tabledef,sec_rodata,'',tabledef.alignment));
  741. { Write vtbls }
  742. for i:=0 to _class.ImplementedInterfaces.count-1 do
  743. begin
  744. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  745. if ImplIntf.VtblImplIntf=ImplIntf then
  746. intf_create_vtbl(list,ImplIntf);
  747. end;
  748. tcb.free;
  749. end;
  750. procedure TVMTWriter.gettabledef(const basename: string; countdef, elementdef: tdef; count: longint; packrecords: shortint; out recdef: trecorddef; out arrdef: tarraydef);
  751. var
  752. fields: tfplist;
  753. name: TIDString;
  754. srsym: tsym;
  755. srsymtable: tsymtable;
  756. begin
  757. { already created a message string table with this number of elements
  758. in this unit -> reuse the def }
  759. name:=basename+tostr(count);
  760. if searchsym_type(name,srsym,srsymtable) then
  761. begin
  762. recdef:=trecorddef(ttypesym(srsym).typedef);
  763. arrdef:=tarraydef(trecordsymtable(recdef.symtable).findfieldbyoffset(countdef.size).vardef);
  764. exit
  765. end;
  766. recdef:=crecorddef.create_global_internal('$'+basename+tostr(count),packrecords,
  767. targetinfos[target_info.system]^.alignment.recordalignmin,
  768. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  769. fields:=tfplist.create;
  770. fields.add(countdef);
  771. if count>0 then
  772. begin
  773. arrdef:=carraydef.create(0,count-1,ptruinttype);
  774. arrdef.elementdef:=elementdef;
  775. fields.add(arrdef);
  776. end
  777. else
  778. arrdef:=nil;
  779. recdef.add_fields_from_deflist(fields);
  780. fields.free;
  781. end;
  782. function TVMTWriter.getrecorddef(const name: string; const fields: array of tdef; packrecords: shortint): trecorddef;
  783. var
  784. fieldlist: tfplist;
  785. srsym: tsym;
  786. srsymtable: tsymtable;
  787. i: longint;
  788. begin
  789. if searchsym_type(name,srsym,srsymtable) then
  790. begin
  791. result:=trecorddef(ttypesym(srsym).typedef);
  792. exit
  793. end;
  794. fieldlist:=tfplist.create;
  795. for i:=low(fields) to high(fields) do
  796. fieldlist.add(fields[i]);
  797. result:=crecorddef.create_global_internal('$'+name,packrecords,
  798. targetinfos[target_info.system]^.alignment.recordalignmin,
  799. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  800. result.add_fields_from_deflist(fieldlist);
  801. fieldlist.free;
  802. end;
  803. { Write interface identifiers to the data section }
  804. procedure TVMTWriter.writeinterfaceids(list: TAsmList);
  805. var
  806. s : string;
  807. tcb : ttai_typedconstbuilder;
  808. def : tdef;
  809. begin
  810. if assigned(_class.iidguid) then
  811. begin
  812. s:=make_mangledname('IID',_class.owner,_class.objname^);
  813. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable]);
  814. tcb.emit_guid_const(_class.iidguid^);
  815. list.concatlist(tcb.get_final_asmlist(
  816. current_asmdata.DefineAsmSymbol(s,AB_GLOBAL,AT_DATA),
  817. rec_tguid,
  818. sec_rodata_norel,
  819. s,
  820. const_align(sizeof(pint))));
  821. tcb.free;
  822. end;
  823. s:=make_mangledname('IIDSTR',_class.owner,_class.objname^);
  824. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable]);
  825. def:=tcb.emit_shortstring_const(_class.iidstr^);
  826. list.concatlist(tcb.get_final_asmlist(
  827. current_asmdata.DefineAsmSymbol(s,AB_GLOBAL,AT_DATA),
  828. def,
  829. sec_rodata_norel,
  830. s,
  831. sizeof(pint)));
  832. tcb.free;
  833. end;
  834. class function TVMTWriter.use_vmt_writer: boolean;
  835. begin
  836. result:=true;
  837. end;
  838. function TVMTWriter.RedirectToEmpty(procdef : tprocdef) : boolean;
  839. var
  840. i : longint;
  841. hp : PCGParaLocation;
  842. begin
  843. result:=false;
  844. if procdef.isempty then
  845. begin
  846. {$ifdef x86}
  847. paramanager.create_funcretloc_info(procdef,calleeside);
  848. if (procdef.funcretloc[calleeside].Location^.loc=LOC_FPUREGISTER) then
  849. exit;
  850. {$endif x86}
  851. procdef.init_paraloc_info(callerside);
  852. { we can redirect the call if no memory parameter is passed }
  853. for i:=0 to procdef.paras.count-1 do
  854. begin
  855. hp:=tparavarsym(procdef.paras[i]).paraloc[callerside].Location;
  856. while assigned(hp) do
  857. begin
  858. if not(hp^.Loc in [LOC_REGISTER,LOC_MMREGISTER,LOC_FPUREGISTER]) then
  859. exit;
  860. hp:=hp^.Next;
  861. end;
  862. end;
  863. result:=true;
  864. end;
  865. end;
  866. procedure TVMTWriter.generate_abstract_stub(list:TAsmList;pd:tprocdef);
  867. var
  868. sym: TAsmSymbol;
  869. begin
  870. { Generate stubs for abstract methods, so their symbols are present and
  871. can be used e.g. to take address (see issue #24536). }
  872. if (po_global in pd.procoptions) and
  873. (pd.owner.defowner<>self._class) then
  874. exit;
  875. sym:=current_asmdata.GetAsmSymbol(pd.mangledname);
  876. if assigned(sym) and (sym.bind<>AB_EXTERNAL) then
  877. exit;
  878. maybe_new_object_file(list);
  879. new_section(list,sec_code,lower(pd.mangledname),target_info.alignment.procalign);
  880. if (po_global in pd.procoptions) then
  881. begin
  882. sym:=current_asmdata.DefineAsmSymbol(pd.mangledname,AB_GLOBAL,AT_FUNCTION);
  883. list.concat(Tai_symbol.Create_global(sym,0));
  884. end
  885. else
  886. begin
  887. sym:=current_asmdata.DefineAsmSymbol(pd.mangledname,AB_LOCAL,AT_FUNCTION);
  888. list.concat(Tai_symbol.Create(sym,0));
  889. end;
  890. hlcg.g_external_wrapper(list,pd,'FPC_ABSTRACTERROR');
  891. list.concat(Tai_symbol_end.Create(sym));
  892. end;
  893. procedure TVMTWriter.writevirtualmethods(List:TAsmList);
  894. var
  895. vmtpd : tprocdef;
  896. vmtentry : pvmtentry;
  897. i : longint;
  898. procname : TSymStr;
  899. {$ifdef vtentry}
  900. hs : string;
  901. {$endif vtentry}
  902. begin
  903. if not assigned(_class.VMTEntries) then
  904. exit;
  905. for i:=0 to _class.VMTEntries.Count-1 do
  906. begin
  907. vmtentry:=pvmtentry(_class.vmtentries[i]);
  908. vmtpd:=vmtentry^.procdef;
  909. { safety checks }
  910. if not(po_virtualmethod in vmtpd.procoptions) then
  911. internalerror(200611082);
  912. if vmtpd.extnumber<>i then
  913. internalerror(200611083);
  914. if (po_abstractmethod in vmtpd.procoptions) then
  915. begin
  916. procname:='FPC_ABSTRACTERROR';
  917. generate_abstract_stub(current_asmdata.AsmLists[al_procedures],vmtpd);
  918. end
  919. else if (cs_opt_remove_emtpy_proc in current_settings.optimizerswitches) and RedirectToEmpty(vmtpd) then
  920. procname:='FPC_EMPTYMETHOD'
  921. else if not wpoinfomanager.optimized_name_for_vmt(_class,vmtpd,procname) then
  922. procname:=vmtpd.mangledname;
  923. List.concat(Tai_const.createname(procname,AT_FUNCTION,0));
  924. {$ifdef vtentry}
  925. hs:='VTENTRY'+'_'+_class.vmt_mangledname+'$$'+tostr(_class.vmtmethodoffset(i) div sizeof(pint));
  926. current_asmdata.asmlists[al_globals].concat(tai_symbol.CreateName(hs,AT_DATA,0));
  927. {$endif vtentry}
  928. end;
  929. end;
  930. procedure TVMTWriter.writevmt;
  931. var
  932. methodnametable,intmessagetable,
  933. strmessagetable,classnamelabel,
  934. fieldtablelabel : tasmlabel;
  935. hs: string;
  936. {$ifdef WITHDMT}
  937. dmtlabel : tasmlabel;
  938. {$endif WITHDMT}
  939. interfacetable : tasmlabel;
  940. templist : TAsmList;
  941. tcb: ttai_typedconstbuilder;
  942. classnamedef: tdef;
  943. begin
  944. {$ifdef WITHDMT}
  945. dmtlabel:=gendmt;
  946. {$endif WITHDMT}
  947. { this code gets executed after the current module's symtable has
  948. already been removed from the symtablestack -> add it again, so that
  949. newly created defs here end up in the right unit }
  950. symtablestack.push(current_module.localsymtable);
  951. templist:=TAsmList.Create;
  952. strmessagetable:=nil;
  953. interfacetable:=nil;
  954. fieldtablelabel:=nil;
  955. methodnametable:=nil;
  956. intmessagetable:=nil;
  957. classnamelabel:=nil;
  958. { write tables for classes, this must be done before the actual
  959. class is written, because we need the labels defined }
  960. if is_class(_class) then
  961. begin
  962. { write class name }
  963. current_asmdata.getlabel(classnamelabel,alt_data);
  964. tcb:=ctai_typedconstbuilder.create([tcalo_is_lab]);
  965. hs:=_class.RttiName;
  966. classnamedef:=tcb.emit_shortstring_const(_class.RttiName);
  967. templist.concatlist(tcb.get_final_asmlist(classnamelabel,classnamedef,sec_rodata_norel,'',sizeof(pint)));
  968. tcb.free;
  969. { interface table }
  970. if _class.ImplementedInterfaces.count>0 then
  971. interfacetable:=intf_write_table(templist);
  972. methodnametable:=genpublishedmethodstable(templist);
  973. fieldtablelabel:=generate_field_table(templist);
  974. { generate message and dynamic tables }
  975. if (oo_has_msgstr in _class.objectoptions) then
  976. strmessagetable:=genstrmsgtab(templist);
  977. if (oo_has_msgint in _class.objectoptions) then
  978. intmessagetable:=genintmsgtab(templist);
  979. end;
  980. { write debug info }
  981. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  982. new_section(current_asmdata.asmlists[al_globals],sec_rodata,_class.vmt_mangledname,const_align(sizeof(pint)));
  983. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(_class.vmt_mangledname,AT_DATA,0));
  984. { determine the size with symtable.datasize, because }
  985. { size gives back 4 for classes }
  986. current_asmdata.asmlists[al_globals].concat(Tai_const.Create(aitconst_ptr,tObjectSymtable(_class.symtable).datasize));
  987. current_asmdata.asmlists[al_globals].concat(Tai_const.Create(aitconst_ptr,-int64(tObjectSymtable(_class.symtable).datasize)));
  988. {$ifdef WITHDMT}
  989. if _class.classtype=ct_object then
  990. begin
  991. if assigned(dmtlabel) then
  992. current_asmdata.asmlists[al_globals].concat(Tai_const_symbol.Create(dmtlabel)))
  993. else
  994. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_ptr(0));
  995. end;
  996. {$endif WITHDMT}
  997. { write pointer to parent VMT, this isn't implemented in TP }
  998. { but this is not used in FPC ? (PM) }
  999. { it's not used yet, but the delphi-operators as and is need it (FK) }
  1000. { it is not written for parents that don't have any vmt !! }
  1001. if assigned(_class.childof) and
  1002. (oo_has_vmt in _class.childof.objectoptions) then
  1003. current_asmdata.asmlists[al_globals].concat(Tai_const.Createname(_class.childof.vmt_mangledname,AT_DATA,0))
  1004. else
  1005. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_nil_dataptr);
  1006. { write extended info for classes, for the order see rtl/inc/objpash.inc }
  1007. if is_class(_class) then
  1008. begin
  1009. { pointer to class name string }
  1010. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(classnamelabel));
  1011. { pointer to dynamic table or nil }
  1012. if (oo_has_msgint in _class.objectoptions) then
  1013. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(intmessagetable))
  1014. else
  1015. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_nil_dataptr);
  1016. { pointer to method table or nil }
  1017. if assigned(methodnametable) then
  1018. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(methodnametable))
  1019. else
  1020. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_nil_dataptr);
  1021. { pointer to field table }
  1022. if assigned(fieldtablelabel) then
  1023. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(fieldtablelabel))
  1024. else
  1025. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_nil_dataptr);
  1026. { pointer to type info of published section }
  1027. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(RTTIWriter.get_rtti_label(_class,fullrtti)));
  1028. { inittable for con-/destruction }
  1029. if _class.members_need_inittable then
  1030. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(RTTIWriter.get_rtti_label(_class,initrtti)))
  1031. else
  1032. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_nil_dataptr);
  1033. { auto table }
  1034. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_nil_dataptr);
  1035. { interface table }
  1036. if _class.ImplementedInterfaces.count>0 then
  1037. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(interfacetable))
  1038. else if _class.implements_any_interfaces then
  1039. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_nil_dataptr)
  1040. else
  1041. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(current_asmdata.RefAsmSymbol('FPC_EMPTYINTF',AT_DATA)));
  1042. { table for string messages }
  1043. if (oo_has_msgstr in _class.objectoptions) then
  1044. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(strmessagetable))
  1045. else
  1046. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_nil_dataptr);
  1047. end;
  1048. { write virtual methods }
  1049. writevirtualmethods(current_asmdata.asmlists[al_globals]);
  1050. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_nil_codeptr);
  1051. { write the size of the VMT }
  1052. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname(_class.vmt_mangledname));
  1053. {$ifdef vtentry}
  1054. { write vtinherit symbol to notify the linker of the class inheritance tree }
  1055. hs:='VTINHERIT'+'_'+_class.vmt_mangledname+'$$';
  1056. if assigned(_class.childof) then
  1057. hs:=hs+_class.childof.vmt_mangledname
  1058. else
  1059. hs:=hs+_class.vmt_mangledname;
  1060. current_asmdata.asmlists[al_globals].concat(tai_symbol.CreateName(hs,AT_DATA,0));
  1061. {$endif vtentry}
  1062. if is_class(_class) then
  1063. current_asmdata.asmlists[al_globals].concatlist(templist);
  1064. templist.Free;
  1065. symtablestack.pop(current_module.localsymtable);
  1066. end;
  1067. procedure gen_intf_wrapper(list:TAsmList;_class:tobjectdef);
  1068. var
  1069. i,j : longint;
  1070. tmps : string;
  1071. pd : TProcdef;
  1072. ImplIntf : TImplementedInterface;
  1073. begin
  1074. for i:=0 to _class.ImplementedInterfaces.count-1 do
  1075. begin
  1076. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  1077. if (ImplIntf=ImplIntf.VtblImplIntf) and
  1078. assigned(ImplIntf.ProcDefs) then
  1079. begin
  1080. for j:=0 to ImplIntf.ProcDefs.Count-1 do
  1081. begin
  1082. pd:=TProcdef(ImplIntf.ProcDefs[j]);
  1083. { we don't track method calls via interfaces yet ->
  1084. assume that every method called via an interface call
  1085. is reachable for now }
  1086. if (po_virtualmethod in pd.procoptions) and
  1087. not is_objectpascal_helper(tprocdef(pd).struct) then
  1088. tobjectdef(tprocdef(pd).struct).register_vmt_call(tprocdef(pd).extnumber);
  1089. tmps:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+
  1090. ImplIntf.IntfDef.objname^+'_$_'+tostr(j)+'_$_'+pd.mangledname);
  1091. { create wrapper code }
  1092. new_section(list,sec_code,tmps,target_info.alignment.procalign);
  1093. hlcg.init_register_allocators;
  1094. hlcg.g_intf_wrapper(list,pd,tmps,ImplIntf.ioffset);
  1095. hlcg.done_register_allocators;
  1096. end;
  1097. end;
  1098. end;
  1099. end;
  1100. procedure do_write_persistent_type_info(st:tsymtable;is_global:boolean);
  1101. var
  1102. i : longint;
  1103. def : tdef;
  1104. vmtwriter : TVMTWriter;
  1105. begin
  1106. if not CVMTWriter.use_vmt_writer then
  1107. exit;
  1108. for i:=0 to st.DefList.Count-1 do
  1109. begin
  1110. def:=tdef(st.DefList[i]);
  1111. case def.typ of
  1112. recorddef :
  1113. do_write_persistent_type_info(trecorddef(def).symtable,is_global);
  1114. objectdef :
  1115. begin
  1116. { Skip generics and forward defs }
  1117. if ([df_generic,df_genconstraint]*def.defoptions<>[]) or
  1118. (oo_is_forward in tobjectdef(def).objectoptions) then
  1119. continue;
  1120. do_write_persistent_type_info(tobjectdef(def).symtable,is_global);
  1121. { Write also VMT if not done yet }
  1122. if not(ds_vmt_written in def.defstates) then
  1123. begin
  1124. vmtwriter:=CVMTWriter.create(tobjectdef(def));
  1125. if is_interface(tobjectdef(def)) then
  1126. vmtwriter.writeinterfaceids(current_asmdata.AsmLists[al_globals]);
  1127. if (oo_has_vmt in tobjectdef(def).objectoptions) then
  1128. vmtwriter.writevmt;
  1129. vmtwriter.free;
  1130. include(def.defstates,ds_vmt_written);
  1131. end;
  1132. if is_class(def) then
  1133. gen_intf_wrapper(current_asmdata.asmlists[al_globals],tobjectdef(def));
  1134. end;
  1135. procdef :
  1136. begin
  1137. if assigned(tprocdef(def).localst) and
  1138. (tprocdef(def).localst.symtabletype=localsymtable) then
  1139. do_write_persistent_type_info(tprocdef(def).localst,false);
  1140. if assigned(tprocdef(def).parast) then
  1141. do_write_persistent_type_info(tprocdef(def).parast,false);
  1142. end;
  1143. end;
  1144. { generate always persistent tables for types in the interface so it can
  1145. be reused in other units and give always the same pointer location. }
  1146. { Init }
  1147. if (
  1148. assigned(def.typesym) and
  1149. is_global and
  1150. not is_objc_class_or_protocol(def)
  1151. ) or
  1152. is_managed_type(def) or
  1153. (ds_init_table_used in def.defstates) then
  1154. RTTIWriter.write_rtti(def,initrtti);
  1155. { RTTI }
  1156. if (
  1157. assigned(def.typesym) and
  1158. is_global and
  1159. not is_objc_class_or_protocol(def)
  1160. ) or
  1161. (ds_rtti_table_used in def.defstates) then
  1162. RTTIWriter.write_rtti(def,fullrtti);
  1163. end;
  1164. end;
  1165. procedure write_persistent_type_info(st:tsymtable;is_global:boolean);
  1166. begin
  1167. create_hlcodegen;
  1168. do_write_persistent_type_info(st,is_global);
  1169. destroy_hlcodegen;
  1170. end;
  1171. end.