ncgvmt.pas 53 KB

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