ncgvmt.pas 53 KB

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