ncgvmt.pas 54 KB

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