ncgvmt.pas 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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. maybe_new_object_file(list);
  836. for j:=0 to ImplIntf.ProcDefs.Count-1 do
  837. begin
  838. pd:=TProcdef(ImplIntf.ProcDefs[j]);
  839. { we don't track method calls via interfaces yet ->
  840. assume that every method called via an interface call
  841. is reachable for now }
  842. if (po_virtualmethod in pd.procoptions) and
  843. not is_objectpascal_helper(tprocdef(pd).struct) then
  844. tobjectdef(tprocdef(pd).struct).register_vmt_call(tprocdef(pd).extnumber);
  845. tmps:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+
  846. ImplIntf.IntfDef.objname^+'_$_'+tostr(j)+'_$_'+pd.mangledname);
  847. { create wrapper code }
  848. new_section(list,sec_code,tmps,0);
  849. hlcg.init_register_allocators;
  850. cg.g_intf_wrapper(list,pd,tmps,ImplIntf.ioffset);
  851. hlcg.done_register_allocators;
  852. end;
  853. end;
  854. end;
  855. end;
  856. procedure gen_intf_wrappers(list:TAsmList;st:TSymtable);
  857. var
  858. i : longint;
  859. def : tdef;
  860. begin
  861. for i:=0 to st.DefList.Count-1 do
  862. begin
  863. def:=tdef(st.DefList[i]);
  864. { if def can contain nested types then handle it symtable }
  865. if def.typ in [objectdef,recorddef] then
  866. gen_intf_wrappers(list,tabstractrecorddef(def).symtable);
  867. if is_class(def) then
  868. gen_intf_wrapper(list,tobjectdef(def));
  869. end;
  870. end;
  871. procedure do_write_persistent_type_info(st:tsymtable;is_global:boolean);
  872. var
  873. i : longint;
  874. def : tdef;
  875. vmtwriter : TVMTWriter;
  876. begin
  877. {$ifdef jvm}
  878. { no Delphi-style RTTI }
  879. exit;
  880. {$endif jvm}
  881. for i:=0 to st.DefList.Count-1 do
  882. begin
  883. def:=tdef(st.DefList[i]);
  884. case def.typ of
  885. recorddef :
  886. do_write_persistent_type_info(trecorddef(def).symtable,is_global);
  887. objectdef :
  888. begin
  889. { Skip generics and forward defs }
  890. if ([df_generic,df_genconstraint]*def.defoptions<>[]) or
  891. (oo_is_forward in tobjectdef(def).objectoptions) then
  892. continue;
  893. do_write_persistent_type_info(tobjectdef(def).symtable,is_global);
  894. { Write also VMT if not done yet }
  895. if not(ds_vmt_written in def.defstates) then
  896. begin
  897. vmtwriter:=TVMTWriter.create(tobjectdef(def));
  898. if is_interface(tobjectdef(def)) then
  899. vmtwriter.writeinterfaceids(current_asmdata.AsmLists[al_globals]);
  900. if (oo_has_vmt in tobjectdef(def).objectoptions) then
  901. vmtwriter.writevmt;
  902. vmtwriter.free;
  903. include(def.defstates,ds_vmt_written);
  904. end;
  905. end;
  906. procdef :
  907. begin
  908. if assigned(tprocdef(def).localst) and
  909. (tprocdef(def).localst.symtabletype=localsymtable) then
  910. do_write_persistent_type_info(tprocdef(def).localst,false);
  911. if assigned(tprocdef(def).parast) then
  912. do_write_persistent_type_info(tprocdef(def).parast,false);
  913. end;
  914. end;
  915. { generate always persistent tables for types in the interface so it can
  916. be reused in other units and give always the same pointer location. }
  917. { Init }
  918. if (
  919. assigned(def.typesym) and
  920. is_global and
  921. not is_objc_class_or_protocol(def)
  922. ) or
  923. is_managed_type(def) or
  924. (ds_init_table_used in def.defstates) then
  925. RTTIWriter.write_rtti(def,initrtti);
  926. { RTTI }
  927. if (
  928. assigned(def.typesym) and
  929. is_global and
  930. not is_objc_class_or_protocol(def)
  931. ) or
  932. (ds_rtti_table_used in def.defstates) then
  933. RTTIWriter.write_rtti(def,fullrtti);
  934. end;
  935. end;
  936. procedure write_persistent_type_info(st:tsymtable;is_global:boolean);
  937. begin
  938. create_hlcodegen;
  939. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],st);
  940. do_write_persistent_type_info(st,is_global);
  941. destroy_hlcodegen;
  942. end;
  943. end.