ncgvmt.pas 52 KB

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