ncgvmt.pas 51 KB

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