ncgvmt.pas 53 KB

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