ncgvmt.pas 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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. symbase;
  22. { generate persistent type information like VMT, RTTI and inittables }
  23. procedure write_persistent_type_info(st:tsymtable;is_global:boolean);
  24. implementation
  25. uses
  26. cutils,cclasses,
  27. globtype,globals,verbose,constexp,
  28. systems,
  29. symconst,symtype,symdef,symsym,symtable,defutil,
  30. aasmbase,aasmtai,aasmdata,
  31. wpobase,
  32. nobj,
  33. cgbase,parabase,paramgr,cgobj,cgcpu,hlcgobj,hlcgcpu,
  34. ncgrtti;
  35. type
  36. pprocdeftree = ^tprocdeftree;
  37. tprocdeftree = record
  38. data : tprocdef;
  39. nl : tasmlabel;
  40. l,r : pprocdeftree;
  41. end;
  42. TVMTWriter=class
  43. private
  44. _Class : tobjectdef;
  45. { message tables }
  46. root : pprocdeftree;
  47. procedure disposeprocdeftree(p : pprocdeftree);
  48. procedure insertmsgint(p:TObject;arg:pointer);
  49. procedure insertmsgstr(p:TObject;arg:pointer);
  50. procedure insertint(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  51. procedure insertstr(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  52. function RedirectToEmpty(procdef: tprocdef): boolean;
  53. procedure writenames(list : TAsmList;p : pprocdeftree);
  54. procedure writeintentry(list : TAsmList;p : pprocdeftree);
  55. procedure writestrentry(list : TAsmList;p : pprocdeftree);
  56. {$ifdef WITHDMT}
  57. { dmt }
  58. procedure insertdmtentry(p:TObject;arg:pointer);
  59. procedure writedmtindexentry(p : pprocdeftree);
  60. procedure writedmtaddressentry(p : pprocdeftree);
  61. {$endif}
  62. { published methods }
  63. procedure do_count_published_methods(p:TObject;arg:pointer);
  64. procedure do_gen_published_methods(p:TObject;arg:pointer);
  65. { virtual methods }
  66. procedure writevirtualmethods(List:TAsmList);
  67. { interface tables }
  68. function intf_get_vtbl_name(AImplIntf:TImplementedInterface): string;
  69. procedure intf_create_vtbl(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  70. procedure intf_gen_intf_ref(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  71. function intf_write_table(list : TAsmList):TAsmLabel;
  72. { generates the message tables for a class }
  73. function genstrmsgtab(list : TAsmList) : tasmlabel;
  74. function genintmsgtab(list : TAsmList) : tasmlabel;
  75. function genpublishedmethodstable(list : TAsmList) : tasmlabel;
  76. function generate_field_table(list : TAsmList) : tasmlabel;
  77. procedure generate_abstract_stub(list:TAsmList;pd:tprocdef);
  78. {$ifdef WITHDMT}
  79. { generates a DMT for _class }
  80. function gendmt : tasmlabel;
  81. {$endif WITHDMT}
  82. public
  83. constructor create(c:tobjectdef);
  84. { write the VMT to al_globals }
  85. procedure writevmt;
  86. procedure writeinterfaceids(list: TAsmList);
  87. end;
  88. {*****************************************************************************
  89. TVMTWriter
  90. *****************************************************************************}
  91. constructor TVMTWriter.create(c:tobjectdef);
  92. begin
  93. inherited Create;
  94. _Class:=c;
  95. end;
  96. {**************************************
  97. Message Tables
  98. **************************************}
  99. procedure TVMTWriter.disposeprocdeftree(p : pprocdeftree);
  100. begin
  101. if assigned(p^.l) then
  102. disposeprocdeftree(p^.l);
  103. if assigned(p^.r) then
  104. disposeprocdeftree(p^.r);
  105. dispose(p);
  106. end;
  107. procedure TVMTWriter.insertint(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  108. begin
  109. if at=nil then
  110. begin
  111. at:=p;
  112. inc(count);
  113. end
  114. else
  115. begin
  116. if p^.data.messageinf.i<at^.data.messageinf.i then
  117. insertint(p,at^.l,count)
  118. else if p^.data.messageinf.i>at^.data.messageinf.i then
  119. insertint(p,at^.r,count)
  120. else
  121. Message1(parser_e_duplicate_message_label,tostr(p^.data.messageinf.i));
  122. end;
  123. end;
  124. procedure TVMTWriter.insertstr(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  125. var
  126. i : integer;
  127. begin
  128. if at=nil then
  129. begin
  130. at:=p;
  131. inc(count);
  132. end
  133. else
  134. begin
  135. i:=CompareStr(p^.data.messageinf.str^,at^.data.messageinf.str^);
  136. if i<0 then
  137. insertstr(p,at^.l,count)
  138. else if i>0 then
  139. insertstr(p,at^.r,count)
  140. else
  141. Message1(parser_e_duplicate_message_label,p^.data.messageinf.str^);
  142. end;
  143. end;
  144. procedure TVMTWriter.insertmsgint(p:TObject;arg:pointer);
  145. var
  146. i : longint;
  147. pd : Tprocdef;
  148. pt : pprocdeftree;
  149. begin
  150. if tsym(p).typ<>procsym then
  151. exit;
  152. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  153. begin
  154. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  155. if po_msgint in pd.procoptions then
  156. begin
  157. new(pt);
  158. pt^.data:=pd;
  159. pt^.l:=nil;
  160. pt^.r:=nil;
  161. insertint(pt,root,plongint(arg)^);
  162. end;
  163. end;
  164. end;
  165. procedure TVMTWriter.insertmsgstr(p:TObject;arg:pointer);
  166. var
  167. i : longint;
  168. pd : Tprocdef;
  169. pt : pprocdeftree;
  170. begin
  171. if tsym(p).typ<>procsym then
  172. exit;
  173. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  174. begin
  175. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  176. if po_msgstr in pd.procoptions then
  177. begin
  178. new(pt);
  179. pt^.data:=pd;
  180. pt^.l:=nil;
  181. pt^.r:=nil;
  182. insertstr(pt,root,plongint(arg)^);
  183. end;
  184. end;
  185. end;
  186. procedure TVMTWriter.writenames(list : TAsmList;p : pprocdeftree);
  187. var
  188. ca : pchar;
  189. len : byte;
  190. begin
  191. current_asmdata.getdatalabel(p^.nl);
  192. if assigned(p^.l) then
  193. writenames(list,p^.l);
  194. list.concat(cai_align.create(const_align(sizeof(pint))));
  195. list.concat(Tai_label.Create(p^.nl));
  196. len:=length(p^.data.messageinf.str^);
  197. list.concat(tai_const.create_8bit(len));
  198. getmem(ca,len+1);
  199. move(p^.data.messageinf.str^[1],ca^,len);
  200. ca[len]:=#0;
  201. list.concat(Tai_string.Create_pchar(ca,len));
  202. if assigned(p^.r) then
  203. writenames(list,p^.r);
  204. end;
  205. procedure TVMTWriter.writestrentry(list : TAsmList;p : pprocdeftree);
  206. begin
  207. if assigned(p^.l) then
  208. writestrentry(list,p^.l);
  209. { write name label }
  210. list.concat(cai_align.create(const_align(sizeof(pint))));
  211. list.concat(Tai_const.Create_sym(p^.nl));
  212. list.concat(cai_align.create(const_align(sizeof(pint))));
  213. list.concat(Tai_const.Createname(p^.data.mangledname,AT_FUNCTION,0));
  214. if assigned(p^.r) then
  215. writestrentry(list,p^.r);
  216. end;
  217. function TVMTWriter.genstrmsgtab(list : TAsmList) : tasmlabel;
  218. var
  219. count : longint;
  220. begin
  221. root:=nil;
  222. count:=0;
  223. { insert all message handlers into a tree, sorted by name }
  224. _class.symtable.SymList.ForEachCall(@insertmsgstr,@count);
  225. { write all names }
  226. if assigned(root) then
  227. writenames(list,root);
  228. { now start writing of the message string table }
  229. current_asmdata.getlabel(result,alt_data);
  230. list.concat(cai_align.create(const_align(sizeof(pint))));
  231. list.concat(Tai_label.Create(result));
  232. list.concat(cai_align.create(const_align(sizeof(longint))));
  233. list.concat(Tai_const.Create_32bit(count));
  234. list.concat(cai_align.create(const_align(sizeof(pint))));
  235. if assigned(root) then
  236. begin
  237. writestrentry(list,root);
  238. disposeprocdeftree(root);
  239. end;
  240. end;
  241. procedure TVMTWriter.writeintentry(list : TAsmList;p : pprocdeftree);
  242. begin
  243. if assigned(p^.l) then
  244. writeintentry(list,p^.l);
  245. { write name label }
  246. list.concat(cai_align.create(const_align(sizeof(longint))));
  247. list.concat(Tai_const.Create_32bit(p^.data.messageinf.i));
  248. list.concat(cai_align.create(const_align(sizeof(pint))));
  249. list.concat(Tai_const.Createname(p^.data.mangledname,AT_FUNCTION,0));
  250. if assigned(p^.r) then
  251. writeintentry(list,p^.r);
  252. end;
  253. function TVMTWriter.genintmsgtab(list : TAsmList) : tasmlabel;
  254. var
  255. r : tasmlabel;
  256. count : longint;
  257. begin
  258. root:=nil;
  259. count:=0;
  260. { insert all message handlers into a tree, sorted by name }
  261. _class.symtable.SymList.ForEachCall(@insertmsgint,@count);
  262. { now start writing of the message string table }
  263. current_asmdata.getlabel(r,alt_data);
  264. list.concat(cai_align.create(const_align(sizeof(pint))));
  265. list.concat(Tai_label.Create(r));
  266. genintmsgtab:=r;
  267. list.concat(cai_align.create(const_align(sizeof(longint))));
  268. list.concat(Tai_const.Create_32bit(count));
  269. list.concat(cai_align.create(const_align(sizeof(pint))));
  270. if assigned(root) then
  271. begin
  272. writeintentry(list,root);
  273. disposeprocdeftree(root);
  274. end;
  275. end;
  276. {$ifdef WITHDMT}
  277. {**************************************
  278. DMT
  279. **************************************}
  280. procedure TVMTWriter.insertdmtentry(p:TObject;arg:pointer);
  281. var
  282. hp : tprocdef;
  283. pt : pprocdeftree;
  284. begin
  285. if tsym(p).typ=procsym then
  286. begin
  287. hp:=tprocsym(p).definition;
  288. while assigned(hp) do
  289. begin
  290. if (po_msgint in hp.procoptions) then
  291. begin
  292. new(pt);
  293. pt^.p:=hp;
  294. pt^.l:=nil;
  295. pt^.r:=nil;
  296. insertint(pt,root);
  297. end;
  298. hp:=hp.nextoverloaded;
  299. end;
  300. end;
  301. end;
  302. procedure TVMTWriter.writedmtindexentry(p : pprocdeftree);
  303. begin
  304. if assigned(p^.l) then
  305. writedmtindexentry(p^.l);
  306. al_globals.concat(Tai_const.Create_32bit(p^.data.messageinf.i));
  307. if assigned(p^.r) then
  308. writedmtindexentry(p^.r);
  309. end;
  310. procedure TVMTWriter.writedmtaddressentry(p : pprocdeftree);
  311. begin
  312. if assigned(p^.l) then
  313. writedmtaddressentry(p^.l);
  314. al_globals.concat(Tai_const_symbol.Createname(p^.data.mangledname,0));
  315. if assigned(p^.r) then
  316. writedmtaddressentry(p^.r);
  317. end;
  318. function TVMTWriter.gendmt : tasmlabel;
  319. var
  320. r : tasmlabel;
  321. begin
  322. root:=nil;
  323. count:=0;
  324. gendmt:=nil;
  325. { insert all message handlers into a tree, sorted by number }
  326. _class.symtable.SymList.ForEachCall(insertdmtentry);
  327. if count>0 then
  328. begin
  329. current_asmdata.getdatalabel(r);
  330. gendmt:=r;
  331. al_globals.concat(cai_align.create(const_align(sizeof(pint))));
  332. al_globals.concat(Tai_label.Create(r));
  333. { entries for caching }
  334. al_globals.concat(Tai_const.Create_ptr(0));
  335. al_globals.concat(Tai_const.Create_ptr(0));
  336. al_globals.concat(Tai_const.Create_32bit(count));
  337. if assigned(root) then
  338. begin
  339. writedmtindexentry(root);
  340. writedmtaddressentry(root);
  341. disposeprocdeftree(root);
  342. end;
  343. end;
  344. end;
  345. {$endif WITHDMT}
  346. {**************************************
  347. Published Methods
  348. **************************************}
  349. procedure TVMTWriter.do_count_published_methods(p:TObject;arg:pointer);
  350. var
  351. i : longint;
  352. pd : tprocdef;
  353. begin
  354. if (tsym(p).typ<>procsym) then
  355. exit;
  356. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  357. begin
  358. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  359. if (pd.procsym=tsym(p)) and
  360. (pd.visibility=vis_published) then
  361. inc(plongint(arg)^);
  362. end;
  363. end;
  364. procedure TVMTWriter.do_gen_published_methods(p:TObject;arg:pointer);
  365. var
  366. i : longint;
  367. l : tasmlabel;
  368. pd : tprocdef;
  369. lists: ^TAsmList absolute arg;
  370. begin
  371. if (tsym(p).typ<>procsym) then
  372. exit;
  373. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  374. begin
  375. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  376. if (pd.procsym=tsym(p)) and
  377. (pd.visibility=vis_published) then
  378. begin
  379. current_asmdata.getlabel(l,alt_data);
  380. lists[1].concat(cai_align.Create(const_align(sizeof(pint))));
  381. lists[1].concat(Tai_label.Create(l));
  382. lists[1].concat(Tai_const.Create_8bit(length(tsym(p).realname)));
  383. lists[1].concat(Tai_string.Create(tsym(p).realname));
  384. lists[0].concat(Tai_const.Create_sym(l));
  385. if po_abstractmethod in pd.procoptions then
  386. lists[0].concat(Tai_const.Create_nil_codeptr)
  387. else
  388. lists[0].concat(Tai_const.Createname(pd.mangledname,AT_FUNCTION,0));
  389. end;
  390. end;
  391. end;
  392. function TVMTWriter.genpublishedmethodstable(list : TAsmList) : tasmlabel;
  393. var
  394. l : tasmlabel;
  395. count : longint;
  396. lists : array[0..1] of TAsmList;
  397. begin
  398. count:=0;
  399. _class.symtable.SymList.ForEachCall(@do_count_published_methods,@count);
  400. if count>0 then
  401. begin
  402. lists[0]:=list;
  403. lists[1]:=TAsmList.Create;
  404. current_asmdata.getlabel(l,alt_data);
  405. list.concat(cai_align.create(const_align(sizeof(pint))));
  406. list.concat(Tai_label.Create(l));
  407. list.concat(Tai_const.Create_32bit(count));
  408. _class.symtable.SymList.ForEachCall(@do_gen_published_methods,@lists);
  409. list.concatlist(lists[1]);
  410. lists[1].Free;
  411. genpublishedmethodstable:=l;
  412. end
  413. else
  414. genpublishedmethodstable:=nil;
  415. end;
  416. function TVMTWriter.generate_field_table(list : TAsmList) : tasmlabel;
  417. var
  418. i : longint;
  419. sym : tsym;
  420. fieldtable,
  421. classtable : tasmlabel;
  422. classindex,
  423. fieldcount : longint;
  424. classtablelist : TFPList;
  425. begin
  426. classtablelist:=TFPList.Create;
  427. { retrieve field info fields }
  428. fieldcount:=0;
  429. for i:=0 to _class.symtable.SymList.Count-1 do
  430. begin
  431. sym:=tsym(_class.symtable.SymList[i]);
  432. if (sym.typ=fieldvarsym) and
  433. (sym.visibility=vis_published) then
  434. begin
  435. if tfieldvarsym(sym).vardef.typ<>objectdef then
  436. internalerror(200611032);
  437. classindex:=classtablelist.IndexOf(tfieldvarsym(sym).vardef);
  438. if classindex=-1 then
  439. classtablelist.Add(tfieldvarsym(sym).vardef);
  440. inc(fieldcount);
  441. end;
  442. end;
  443. if fieldcount>0 then
  444. begin
  445. current_asmdata.getlabel(fieldtable,alt_data);
  446. current_asmdata.getlabel(classtable,alt_data);
  447. list.concat(cai_align.create(const_align(sizeof(pint))));
  448. { write fields }
  449. list.concat(Tai_label.Create(fieldtable));
  450. list.concat(Tai_const.Create_16bit(fieldcount));
  451. if (tf_requires_proper_alignment in target_info.flags) then
  452. list.concat(cai_align.Create(sizeof(TConstPtrUInt)));
  453. list.concat(Tai_const.Create_sym(classtable));
  454. for i:=0 to _class.symtable.SymList.Count-1 do
  455. begin
  456. sym:=tsym(_class.symtable.SymList[i]);
  457. if (sym.typ=fieldvarsym) and
  458. (sym.visibility=vis_published) then
  459. begin
  460. if (tf_requires_proper_alignment in target_info.flags) then
  461. list.concat(cai_align.Create(sizeof(pint)));
  462. list.concat(Tai_const.Create_pint(tfieldvarsym(sym).fieldoffset));
  463. classindex:=classtablelist.IndexOf(tfieldvarsym(sym).vardef);
  464. if classindex=-1 then
  465. internalerror(200611033);
  466. list.concat(Tai_const.Create_16bit(classindex+1));
  467. list.concat(Tai_const.Create_8bit(length(tfieldvarsym(sym).realname)));
  468. list.concat(Tai_string.Create(tfieldvarsym(sym).realname));
  469. end;
  470. end;
  471. { generate the class table }
  472. list.concat(cai_align.create(const_align(sizeof(pint))));
  473. list.concat(Tai_label.Create(classtable));
  474. list.concat(Tai_const.Create_16bit(classtablelist.count));
  475. if (tf_requires_proper_alignment in target_info.flags) then
  476. list.concat(cai_align.Create(sizeof(TConstPtrUInt)));
  477. for i:=0 to classtablelist.Count-1 do
  478. list.concat(Tai_const.Createname(tobjectdef(classtablelist[i]).vmt_mangledname,AT_DATA,0));
  479. result:=fieldtable;
  480. end
  481. else
  482. result:=nil;
  483. classtablelist.free;
  484. end;
  485. {**************************************
  486. Interface tables
  487. **************************************}
  488. function TVMTWriter.intf_get_vtbl_name(AImplIntf:TImplementedInterface): string;
  489. begin
  490. result:=make_mangledname('VTBL',_class.owner,_class.objname^+'_$_'+AImplIntf.IntfDef.objname^);
  491. end;
  492. procedure TVMTWriter.intf_create_vtbl(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  493. var
  494. pd : tprocdef;
  495. vtblstr,
  496. hs : string;
  497. i : longint;
  498. begin
  499. vtblstr:=intf_get_vtbl_name(AImplIntf);
  500. rawdata.concat(cai_align.create(const_align(sizeof(pint))));
  501. rawdata.concat(tai_symbol.createname(vtblstr,AT_DATA,0));
  502. if assigned(AImplIntf.procdefs) then
  503. begin
  504. for i:=0 to AImplIntf.procdefs.count-1 do
  505. begin
  506. pd:=tprocdef(AImplIntf.procdefs[i]);
  507. hs:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+AImplIntf.IntfDef.objname^+'_$_'+
  508. tostr(i)+'_$_'+pd.mangledname);
  509. { create reference }
  510. rawdata.concat(Tai_const.Createname(hs,AT_FUNCTION,0));
  511. end;
  512. end;
  513. rawdata.concat(tai_symbol_end.createname(vtblstr));
  514. end;
  515. procedure TVMTWriter.intf_gen_intf_ref(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  516. var
  517. pd: tprocdef;
  518. begin
  519. { GUID (or nil for Corba interfaces) }
  520. if AImplIntf.IntfDef.objecttype in [odt_interfacecom] then
  521. rawdata.concat(Tai_const.CreateName(
  522. make_mangledname('IID',AImplIntf.IntfDef.owner,AImplIntf.IntfDef.objname^),AT_DATA,0))
  523. else
  524. rawdata.concat(Tai_const.Create_nil_dataptr);
  525. { VTable }
  526. rawdata.concat(Tai_const.Createname(intf_get_vtbl_name(AImplIntf.VtblImplIntf),AT_DATA,0));
  527. { IOffset field }
  528. case AImplIntf.VtblImplIntf.IType of
  529. etFieldValue, etFieldValueClass,
  530. etStandard:
  531. rawdata.concat(Tai_const.Create_pint(AImplIntf.VtblImplIntf.IOffset));
  532. etStaticMethodResult, etStaticMethodClass:
  533. rawdata.concat(Tai_const.Createname(
  534. tprocdef(tpropertysym(AImplIntf.ImplementsGetter).propaccesslist[palt_read].procdef).mangledname,
  535. AT_FUNCTION,
  536. 0
  537. ));
  538. etVirtualMethodResult, etVirtualMethodClass:
  539. begin
  540. pd := tprocdef(tpropertysym(AImplIntf.ImplementsGetter).propaccesslist[palt_read].procdef);
  541. rawdata.concat(Tai_const.Create_pint(tobjectdef(pd.struct).vmtmethodoffset(pd.extnumber)));
  542. end;
  543. else
  544. internalerror(200802162);
  545. end;
  546. { IIDStr }
  547. rawdata.concat(Tai_const.CreateName(
  548. make_mangledname('IIDSTR',AImplIntf.IntfDef.owner,AImplIntf.IntfDef.objname^),AT_DATA,0));
  549. { IType }
  550. rawdata.concat(Tai_const.Create_pint(aint(AImplIntf.VtblImplIntf.IType)));
  551. end;
  552. function TVMTWriter.intf_write_table(list : TAsmList):TAsmLabel;
  553. var
  554. i : longint;
  555. ImplIntf : TImplementedInterface;
  556. begin
  557. current_asmdata.getlabel(result,alt_data);
  558. list.concat(cai_align.create(const_align(sizeof(pint))));
  559. list.concat(Tai_label.Create(result));
  560. list.concat(Tai_const.Create_pint(_class.ImplementedInterfaces.count));
  561. { Write vtbl references }
  562. for i:=0 to _class.ImplementedInterfaces.count-1 do
  563. begin
  564. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  565. intf_gen_intf_ref(list,ImplIntf);
  566. end;
  567. { Write vtbls }
  568. for i:=0 to _class.ImplementedInterfaces.count-1 do
  569. begin
  570. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  571. if ImplIntf.VtblImplIntf=ImplIntf then
  572. intf_create_vtbl(list,ImplIntf);
  573. end;
  574. end;
  575. { Write interface identifiers to the data section }
  576. procedure TVMTWriter.writeinterfaceids(list: TAsmList);
  577. var
  578. i : longint;
  579. s : string;
  580. begin
  581. if assigned(_class.iidguid) then
  582. begin
  583. s:=make_mangledname('IID',_class.owner,_class.objname^);
  584. maybe_new_object_file(list);
  585. new_section(list,sec_rodata_norel,s,const_align(sizeof(pint)));
  586. list.concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  587. list.concat(Tai_const.Create_32bit(longint(_class.iidguid^.D1)));
  588. list.concat(Tai_const.Create_16bit(_class.iidguid^.D2));
  589. list.concat(Tai_const.Create_16bit(_class.iidguid^.D3));
  590. for i:=Low(_class.iidguid^.D4) to High(_class.iidguid^.D4) do
  591. list.concat(Tai_const.Create_8bit(_class.iidguid^.D4[i]));
  592. end;
  593. maybe_new_object_file(list);
  594. s:=make_mangledname('IIDSTR',_class.owner,_class.objname^);
  595. new_section(list,sec_rodata_norel,s,sizeof(pint));
  596. list.concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  597. list.concat(Tai_const.Create_8bit(length(_class.iidstr^)));
  598. list.concat(Tai_string.Create(_class.iidstr^));
  599. end;
  600. function TVMTWriter.RedirectToEmpty(procdef : tprocdef) : boolean;
  601. var
  602. i : longint;
  603. hp : PCGParaLocation;
  604. begin
  605. result:=false;
  606. if procdef.isempty then
  607. begin
  608. {$ifdef x86}
  609. paramanager.create_funcretloc_info(procdef,calleeside);
  610. if (procdef.funcretloc[calleeside].Location^.loc=LOC_FPUREGISTER) then
  611. exit;
  612. {$endif x86}
  613. procdef.init_paraloc_info(callerside);
  614. { we can redirect the call if no memory parameter is passed }
  615. for i:=0 to procdef.paras.count-1 do
  616. begin
  617. hp:=tparavarsym(procdef.paras[i]).paraloc[callerside].Location;
  618. while assigned(hp) do
  619. begin
  620. if not(hp^.Loc in [LOC_REGISTER,LOC_MMREGISTER,LOC_FPUREGISTER]) then
  621. exit;
  622. hp:=hp^.Next;
  623. end;
  624. end;
  625. result:=true;
  626. end;
  627. end;
  628. procedure TVMTWriter.generate_abstract_stub(list:TAsmList;pd:tprocdef);
  629. var
  630. sym: TAsmSymbol;
  631. begin
  632. { Generate stubs for abstract methods, so their symbols are present and
  633. can be used e.g. to take address (see issue #24536). }
  634. if (po_global in pd.procoptions) and
  635. (pd.owner.defowner<>self._class) then
  636. exit;
  637. sym:=current_asmdata.GetAsmSymbol(pd.mangledname);
  638. if assigned(sym) and (sym.bind<>AB_EXTERNAL) then
  639. exit;
  640. maybe_new_object_file(list);
  641. new_section(list,sec_code,lower(pd.mangledname),target_info.alignment.procalign);
  642. if (po_global in pd.procoptions) then
  643. begin
  644. sym:=current_asmdata.DefineAsmSymbol(pd.mangledname,AB_GLOBAL,AT_FUNCTION);
  645. list.concat(Tai_symbol.Create_global(sym,0));
  646. end
  647. else
  648. begin
  649. sym:=current_asmdata.DefineAsmSymbol(pd.mangledname,AB_LOCAL,AT_FUNCTION);
  650. list.concat(Tai_symbol.Create(sym,0));
  651. end;
  652. cg.g_external_wrapper(list,pd,'FPC_ABSTRACTERROR');
  653. list.concat(Tai_symbol_end.Create(sym));
  654. end;
  655. procedure TVMTWriter.writevirtualmethods(List:TAsmList);
  656. var
  657. vmtpd : tprocdef;
  658. vmtentry : pvmtentry;
  659. i : longint;
  660. procname : string;
  661. {$ifdef vtentry}
  662. hs : string;
  663. {$endif vtentry}
  664. begin
  665. if not assigned(_class.VMTEntries) then
  666. exit;
  667. for i:=0 to _class.VMTEntries.Count-1 do
  668. begin
  669. vmtentry:=pvmtentry(_class.vmtentries[i]);
  670. vmtpd:=vmtentry^.procdef;
  671. { safety checks }
  672. if not(po_virtualmethod in vmtpd.procoptions) then
  673. internalerror(200611082);
  674. if vmtpd.extnumber<>i then
  675. internalerror(200611083);
  676. if (po_abstractmethod in vmtpd.procoptions) then
  677. begin
  678. procname:='FPC_ABSTRACTERROR';
  679. generate_abstract_stub(current_asmdata.AsmLists[al_procedures],vmtpd);
  680. end
  681. else if (cs_opt_remove_emtpy_proc in current_settings.optimizerswitches) and RedirectToEmpty(vmtpd) then
  682. procname:='FPC_EMPTYMETHOD'
  683. else if not wpoinfomanager.optimized_name_for_vmt(_class,vmtpd,procname) then
  684. procname:=vmtpd.mangledname;
  685. List.concat(Tai_const.createname(procname,AT_FUNCTION,0));
  686. {$ifdef vtentry}
  687. hs:='VTENTRY'+'_'+_class.vmt_mangledname+'$$'+tostr(_class.vmtmethodoffset(i) div sizeof(pint));
  688. current_asmdata.asmlists[al_globals].concat(tai_symbol.CreateName(hs,AT_DATA,0));
  689. {$endif vtentry}
  690. end;
  691. end;
  692. procedure TVMTWriter.writevmt;
  693. var
  694. methodnametable,intmessagetable,
  695. strmessagetable,classnamelabel,
  696. fieldtablelabel : tasmlabel;
  697. hs: string;
  698. {$ifdef WITHDMT}
  699. dmtlabel : tasmlabel;
  700. {$endif WITHDMT}
  701. interfacetable : tasmlabel;
  702. templist : TAsmList;
  703. begin
  704. {$ifdef WITHDMT}
  705. dmtlabel:=gendmt;
  706. {$endif WITHDMT}
  707. templist:=TAsmList.Create;
  708. strmessagetable:=nil;
  709. interfacetable:=nil;
  710. fieldtablelabel:=nil;
  711. methodnametable:=nil;
  712. intmessagetable:=nil;
  713. classnamelabel:=nil;
  714. { write tables for classes, this must be done before the actual
  715. class is written, because we need the labels defined }
  716. if is_class(_class) then
  717. begin
  718. { write class name }
  719. current_asmdata.getlabel(classnamelabel,alt_data);
  720. templist.concat(cai_align.create(const_align(sizeof(pint))));
  721. templist.concat(Tai_label.Create(classnamelabel));
  722. hs:=_class.RttiName;
  723. templist.concat(Tai_const.Create_8bit(length(hs)));
  724. templist.concat(Tai_string.Create(hs));
  725. { interface table }
  726. if _class.ImplementedInterfaces.count>0 then
  727. interfacetable:=intf_write_table(templist);
  728. methodnametable:=genpublishedmethodstable(templist);
  729. fieldtablelabel:=generate_field_table(templist);
  730. { generate message and dynamic tables }
  731. if (oo_has_msgstr in _class.objectoptions) then
  732. strmessagetable:=genstrmsgtab(templist);
  733. if (oo_has_msgint in _class.objectoptions) then
  734. intmessagetable:=genintmsgtab(templist);
  735. end;
  736. { write debug info }
  737. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  738. new_section(current_asmdata.asmlists[al_globals],sec_rodata,_class.vmt_mangledname,const_align(sizeof(pint)));
  739. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(_class.vmt_mangledname,AT_DATA,0));
  740. { determine the size with symtable.datasize, because }
  741. { size gives back 4 for classes }
  742. current_asmdata.asmlists[al_globals].concat(Tai_const.Create(aitconst_ptr,tObjectSymtable(_class.symtable).datasize));
  743. current_asmdata.asmlists[al_globals].concat(Tai_const.Create(aitconst_ptr,-int64(tObjectSymtable(_class.symtable).datasize)));
  744. {$ifdef WITHDMT}
  745. if _class.classtype=ct_object then
  746. begin
  747. if assigned(dmtlabel) then
  748. current_asmdata.asmlists[al_globals].concat(Tai_const_symbol.Create(dmtlabel)))
  749. else
  750. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_ptr(0));
  751. end;
  752. {$endif WITHDMT}
  753. { write pointer to parent VMT, this isn't implemented in TP }
  754. { but this is not used in FPC ? (PM) }
  755. { it's not used yet, but the delphi-operators as and is need it (FK) }
  756. { it is not written for parents that don't have any vmt !! }
  757. if assigned(_class.childof) and
  758. (oo_has_vmt in _class.childof.objectoptions) then
  759. current_asmdata.asmlists[al_globals].concat(Tai_const.Createname(_class.childof.vmt_mangledname,AT_DATA,0))
  760. else
  761. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_nil_dataptr);
  762. { write extended info for classes, for the order see rtl/inc/objpash.inc }
  763. if is_class(_class) then
  764. begin
  765. { pointer to class name string }
  766. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(classnamelabel));
  767. { pointer to dynamic table or nil }
  768. if (oo_has_msgint in _class.objectoptions) then
  769. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(intmessagetable))
  770. else
  771. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_nil_dataptr);
  772. { pointer to method table or nil }
  773. if assigned(methodnametable) then
  774. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(methodnametable))
  775. else
  776. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_nil_dataptr);
  777. { pointer to field table }
  778. if assigned(fieldtablelabel) then
  779. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(fieldtablelabel))
  780. else
  781. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_nil_dataptr);
  782. { pointer to type info of published section }
  783. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(RTTIWriter.get_rtti_label(_class,fullrtti)));
  784. { inittable for con-/destruction }
  785. if _class.members_need_inittable then
  786. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(RTTIWriter.get_rtti_label(_class,initrtti)))
  787. else
  788. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_nil_dataptr);
  789. { auto table }
  790. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_nil_dataptr);
  791. { interface table }
  792. if _class.ImplementedInterfaces.count>0 then
  793. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(interfacetable))
  794. else if _class.implements_any_interfaces then
  795. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_nil_dataptr)
  796. else
  797. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(current_asmdata.RefAsmSymbol('FPC_EMPTYINTF',AT_DATA)));
  798. { table for string messages }
  799. if (oo_has_msgstr in _class.objectoptions) then
  800. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(strmessagetable))
  801. else
  802. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_nil_dataptr);
  803. end;
  804. { write virtual methods }
  805. writevirtualmethods(current_asmdata.asmlists[al_globals]);
  806. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_nil_codeptr);
  807. { write the size of the VMT }
  808. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname(_class.vmt_mangledname));
  809. {$ifdef vtentry}
  810. { write vtinherit symbol to notify the linker of the class inheritance tree }
  811. hs:='VTINHERIT'+'_'+_class.vmt_mangledname+'$$';
  812. if assigned(_class.childof) then
  813. hs:=hs+_class.childof.vmt_mangledname
  814. else
  815. hs:=hs+_class.vmt_mangledname;
  816. current_asmdata.asmlists[al_globals].concat(tai_symbol.CreateName(hs,AT_DATA,0));
  817. {$endif vtentry}
  818. if is_class(_class) then
  819. current_asmdata.asmlists[al_globals].concatlist(templist);
  820. templist.Free;
  821. end;
  822. procedure gen_intf_wrapper(list:TAsmList;_class:tobjectdef);
  823. var
  824. i,j : longint;
  825. tmps : string;
  826. pd : TProcdef;
  827. ImplIntf : TImplementedInterface;
  828. begin
  829. for i:=0 to _class.ImplementedInterfaces.count-1 do
  830. begin
  831. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  832. if (ImplIntf=ImplIntf.VtblImplIntf) and
  833. assigned(ImplIntf.ProcDefs) then
  834. begin
  835. for j:=0 to ImplIntf.ProcDefs.Count-1 do
  836. begin
  837. pd:=TProcdef(ImplIntf.ProcDefs[j]);
  838. { we don't track method calls via interfaces yet ->
  839. assume that every method called via an interface call
  840. is reachable for now }
  841. if (po_virtualmethod in pd.procoptions) and
  842. not is_objectpascal_helper(tprocdef(pd).struct) then
  843. tobjectdef(tprocdef(pd).struct).register_vmt_call(tprocdef(pd).extnumber);
  844. tmps:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+
  845. ImplIntf.IntfDef.objname^+'_$_'+tostr(j)+'_$_'+pd.mangledname);
  846. { create wrapper code }
  847. new_section(list,sec_code,tmps,target_info.alignment.procalign);
  848. hlcg.init_register_allocators;
  849. cg.g_intf_wrapper(list,pd,tmps,ImplIntf.ioffset);
  850. hlcg.done_register_allocators;
  851. end;
  852. end;
  853. end;
  854. end;
  855. procedure do_write_persistent_type_info(st:tsymtable;is_global:boolean);
  856. var
  857. i : longint;
  858. def : tdef;
  859. vmtwriter : TVMTWriter;
  860. begin
  861. {$ifdef jvm}
  862. { no Delphi-style RTTI }
  863. exit;
  864. {$endif jvm}
  865. for i:=0 to st.DefList.Count-1 do
  866. begin
  867. def:=tdef(st.DefList[i]);
  868. case def.typ of
  869. recorddef :
  870. do_write_persistent_type_info(trecorddef(def).symtable,is_global);
  871. objectdef :
  872. begin
  873. { Skip generics and forward defs }
  874. if ([df_generic,df_genconstraint]*def.defoptions<>[]) or
  875. (oo_is_forward in tobjectdef(def).objectoptions) then
  876. continue;
  877. do_write_persistent_type_info(tobjectdef(def).symtable,is_global);
  878. { Write also VMT if not done yet }
  879. if not(ds_vmt_written in def.defstates) then
  880. begin
  881. vmtwriter:=TVMTWriter.create(tobjectdef(def));
  882. if is_interface(tobjectdef(def)) then
  883. vmtwriter.writeinterfaceids(current_asmdata.AsmLists[al_globals]);
  884. if (oo_has_vmt in tobjectdef(def).objectoptions) then
  885. vmtwriter.writevmt;
  886. vmtwriter.free;
  887. include(def.defstates,ds_vmt_written);
  888. end;
  889. if is_class(def) then
  890. gen_intf_wrapper(current_asmdata.asmlists[al_globals],tobjectdef(def));
  891. end;
  892. procdef :
  893. begin
  894. if assigned(tprocdef(def).localst) and
  895. (tprocdef(def).localst.symtabletype=localsymtable) then
  896. do_write_persistent_type_info(tprocdef(def).localst,false);
  897. if assigned(tprocdef(def).parast) then
  898. do_write_persistent_type_info(tprocdef(def).parast,false);
  899. end;
  900. end;
  901. { generate always persistent tables for types in the interface so it can
  902. be reused in other units and give always the same pointer location. }
  903. { Init }
  904. if (
  905. assigned(def.typesym) and
  906. is_global and
  907. not is_objc_class_or_protocol(def)
  908. ) or
  909. is_managed_type(def) or
  910. (ds_init_table_used in def.defstates) then
  911. RTTIWriter.write_rtti(def,initrtti);
  912. { RTTI }
  913. if (
  914. assigned(def.typesym) and
  915. is_global and
  916. not is_objc_class_or_protocol(def)
  917. ) or
  918. (ds_rtti_table_used in def.defstates) then
  919. RTTIWriter.write_rtti(def,fullrtti);
  920. end;
  921. end;
  922. procedure write_persistent_type_info(st:tsymtable;is_global:boolean);
  923. begin
  924. create_hlcodegen;
  925. do_write_persistent_type_info(st,is_global);
  926. destroy_hlcodegen;
  927. end;
  928. end.