ncgvmt.pas 37 KB

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