nobj.pas 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Routines for the code generation of data structures
  4. like VMT, Messages, VTables, Interfaces descs
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit nobj;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cutils,cclasses,
  23. globtype,
  24. symdef,symsym,
  25. aasmbase,aasmtai
  26. ;
  27. type
  28. pprocdeftree = ^tprocdeftree;
  29. tprocdeftree = record
  30. data : tprocdef;
  31. nl : tasmlabel;
  32. l,r : pprocdeftree;
  33. end;
  34. pprocdefcoll = ^tprocdefcoll;
  35. tprocdefcoll = record
  36. data : tprocdef;
  37. hidden : boolean;
  38. visible : boolean;
  39. next : pprocdefcoll;
  40. end;
  41. pvmtentry = ^tvmtentry;
  42. tvmtentry = record
  43. speedvalue : cardinal;
  44. name : pstring;
  45. firstprocdef : pprocdefcoll;
  46. next : pvmtentry;
  47. end;
  48. tclassheader=class
  49. private
  50. _Class : tobjectdef;
  51. private
  52. { message tables }
  53. root : pprocdeftree;
  54. procedure disposeprocdeftree(p : pprocdeftree);
  55. procedure insertmsgint(p : tnamedindexitem;arg:pointer);
  56. procedure insertmsgstr(p : tnamedindexitem;arg:pointer);
  57. procedure insertint(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  58. procedure insertstr(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  59. procedure writenames(p : pprocdeftree);
  60. procedure writeintentry(p : pprocdeftree);
  61. procedure writestrentry(p : pprocdeftree);
  62. {$ifdef WITHDMT}
  63. private
  64. { dmt }
  65. procedure insertdmtentry(p : tnamedindexitem;arg:pointer);
  66. procedure writedmtindexentry(p : pprocdeftree);
  67. procedure writedmtaddressentry(p : pprocdeftree);
  68. {$endif}
  69. private
  70. { published methods }
  71. procedure do_count_published_methods(p : tnamedindexitem;arg:pointer);
  72. procedure do_gen_published_methods(p : tnamedindexitem;arg:pointer);
  73. private
  74. { vmt }
  75. firstvmtentry : pvmtentry;
  76. nextvirtnumber : integer;
  77. has_constructor,
  78. has_virtual_method : boolean;
  79. procedure newdefentry(vmtentry:pvmtentry;pd:tprocdef;is_visible:boolean);
  80. function newvmtentry(sym:tprocsym):pvmtentry;
  81. procedure eachsym(sym : tnamedindexitem;arg:pointer);
  82. procedure disposevmttree;
  83. procedure writevirtualmethods(List:TAAsmoutput);
  84. private
  85. { interface tables }
  86. function gintfgetvtbllabelname(intfindex: integer): string;
  87. procedure gintfcreatevtbl(intfindex: integer; rawdata: TAAsmoutput);
  88. procedure gintfgenentry(intfindex, contintfindex: integer; rawdata: TAAsmoutput);
  89. procedure gintfoptimizevtbls;
  90. procedure gintfwritedata;
  91. function gintfgetcprocdef(proc: tprocdef;const name: string): tprocdef;
  92. procedure gintfdoonintf(intf: tobjectdef; intfindex: longint);
  93. procedure gintfwalkdowninterface(intf: tobjectdef; intfindex: longint);
  94. public
  95. constructor create(c:tobjectdef);
  96. destructor destroy;override;
  97. { generates the message tables for a class }
  98. function genstrmsgtab : tasmlabel;
  99. function genintmsgtab : tasmlabel;
  100. function genpublishedmethodstable : tasmlabel;
  101. { generates a VMT entries }
  102. procedure genvmt;
  103. {$ifdef WITHDMT}
  104. { generates a DMT for _class }
  105. function gendmt : tasmlabel;
  106. {$endif WITHDMT}
  107. { interfaces }
  108. function genintftable: tasmlabel;
  109. { write the VMT to al_globals }
  110. procedure writevmt;
  111. procedure writeinterfaceids;
  112. end;
  113. implementation
  114. uses
  115. strings,
  116. globals,verbose,systems,
  117. symtable,symconst,symtype,defcmp,
  118. dbgbase
  119. ;
  120. {*****************************************************************************
  121. TClassHeader
  122. *****************************************************************************}
  123. constructor tclassheader.create(c:tobjectdef);
  124. begin
  125. inherited Create;
  126. _Class:=c;
  127. end;
  128. destructor tclassheader.destroy;
  129. begin
  130. disposevmttree;
  131. end;
  132. {**************************************
  133. Message Tables
  134. **************************************}
  135. procedure tclassheader.disposeprocdeftree(p : pprocdeftree);
  136. begin
  137. if assigned(p^.l) then
  138. disposeprocdeftree(p^.l);
  139. if assigned(p^.r) then
  140. disposeprocdeftree(p^.r);
  141. dispose(p);
  142. end;
  143. procedure tclassheader.insertint(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  144. begin
  145. if at=nil then
  146. begin
  147. at:=p;
  148. inc(count);
  149. end
  150. else
  151. begin
  152. if p^.data.messageinf.i<at^.data.messageinf.i then
  153. insertint(p,at^.l,count)
  154. else if p^.data.messageinf.i>at^.data.messageinf.i then
  155. insertint(p,at^.r,count)
  156. else
  157. Message1(parser_e_duplicate_message_label,tostr(p^.data.messageinf.i));
  158. end;
  159. end;
  160. procedure tclassheader.insertstr(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  161. var
  162. i : integer;
  163. begin
  164. if at=nil then
  165. begin
  166. at:=p;
  167. inc(count);
  168. end
  169. else
  170. begin
  171. i:=strcomp(p^.data.messageinf.str,at^.data.messageinf.str);
  172. if i<0 then
  173. insertstr(p,at^.l,count)
  174. else if i>0 then
  175. insertstr(p,at^.r,count)
  176. else
  177. Message1(parser_e_duplicate_message_label,strpas(p^.data.messageinf.str));
  178. end;
  179. end;
  180. procedure tclassheader.insertmsgint(p : tnamedindexitem;arg:pointer);
  181. var
  182. i : cardinal;
  183. def: Tprocdef;
  184. pt : pprocdeftree;
  185. begin
  186. if tsym(p).typ=procsym then
  187. for i:=1 to Tprocsym(p).procdef_count do
  188. begin
  189. def:=Tprocsym(p).procdef[i];
  190. if po_msgint in def.procoptions then
  191. begin
  192. new(pt);
  193. pt^.data:=def;
  194. pt^.l:=nil;
  195. pt^.r:=nil;
  196. insertint(pt,root,plongint(arg)^);
  197. end;
  198. end;
  199. end;
  200. procedure tclassheader.insertmsgstr(p : tnamedindexitem;arg:pointer);
  201. var
  202. i : cardinal;
  203. def: Tprocdef;
  204. pt : pprocdeftree;
  205. begin
  206. if tsym(p).typ=procsym then
  207. for i:=1 to Tprocsym(p).procdef_count do
  208. begin
  209. def:=Tprocsym(p).procdef[i];
  210. if po_msgstr in def.procoptions then
  211. begin
  212. new(pt);
  213. pt^.data:=def;
  214. pt^.l:=nil;
  215. pt^.r:=nil;
  216. insertstr(pt,root,plongint(arg)^);
  217. end;
  218. end;
  219. end;
  220. procedure tclassheader.writenames(p : pprocdeftree);
  221. var
  222. ca : pchar;
  223. len : longint;
  224. begin
  225. objectlibrary.getdatalabel(p^.nl);
  226. if assigned(p^.l) then
  227. writenames(p^.l);
  228. asmlist[al_globals].concat(cai_align.create(const_align(sizeof(aint))));
  229. asmlist[al_globals].concat(Tai_label.Create(p^.nl));
  230. len:=strlen(p^.data.messageinf.str);
  231. asmlist[al_globals].concat(tai_const.create_8bit(len));
  232. getmem(ca,len+1);
  233. move(p^.data.messageinf.str^,ca^,len+1);
  234. asmlist[al_globals].concat(Tai_string.Create_pchar(ca,len));
  235. if assigned(p^.r) then
  236. writenames(p^.r);
  237. end;
  238. procedure tclassheader.writestrentry(p : pprocdeftree);
  239. begin
  240. if assigned(p^.l) then
  241. writestrentry(p^.l);
  242. { write name label }
  243. asmlist[al_globals].concat(Tai_const.Create_sym(p^.nl));
  244. asmlist[al_globals].concat(Tai_const.Createname(p^.data.mangledname,AT_FUNCTION,0));
  245. if assigned(p^.r) then
  246. writestrentry(p^.r);
  247. end;
  248. function tclassheader.genstrmsgtab : tasmlabel;
  249. var
  250. r : tasmlabel;
  251. count : longint;
  252. begin
  253. root:=nil;
  254. count:=0;
  255. { insert all message handlers into a tree, sorted by name }
  256. _class.symtable.foreach(@insertmsgstr,@count);
  257. { write all names }
  258. if assigned(root) then
  259. writenames(root);
  260. { now start writing of the message string table }
  261. objectlibrary.getdatalabel(r);
  262. asmlist[al_globals].concat(cai_align.create(const_align(sizeof(aint))));
  263. asmlist[al_globals].concat(Tai_label.Create(r));
  264. genstrmsgtab:=r;
  265. asmlist[al_globals].concat(Tai_const.Create_32bit(count));
  266. if assigned(root) then
  267. begin
  268. writestrentry(root);
  269. disposeprocdeftree(root);
  270. end;
  271. end;
  272. procedure tclassheader.writeintentry(p : pprocdeftree);
  273. begin
  274. if assigned(p^.l) then
  275. writeintentry(p^.l);
  276. { write name label }
  277. asmlist[al_globals].concat(Tai_const.Create_32bit(p^.data.messageinf.i));
  278. asmlist[al_globals].concat(Tai_const.Createname(p^.data.mangledname,AT_FUNCTION,0));
  279. if assigned(p^.r) then
  280. writeintentry(p^.r);
  281. end;
  282. function tclassheader.genintmsgtab : tasmlabel;
  283. var
  284. r : tasmlabel;
  285. count : longint;
  286. begin
  287. root:=nil;
  288. count:=0;
  289. { insert all message handlers into a tree, sorted by name }
  290. _class.symtable.foreach(@insertmsgint,@count);
  291. { now start writing of the message string table }
  292. objectlibrary.getdatalabel(r);
  293. asmlist[al_globals].concat(cai_align.create(const_align(sizeof(aint))));
  294. asmlist[al_globals].concat(Tai_label.Create(r));
  295. genintmsgtab:=r;
  296. asmlist[al_globals].concat(Tai_const.Create_32bit(count));
  297. if assigned(root) then
  298. begin
  299. writeintentry(root);
  300. disposeprocdeftree(root);
  301. end;
  302. end;
  303. {$ifdef WITHDMT}
  304. {**************************************
  305. DMT
  306. **************************************}
  307. procedure tclassheader.insertdmtentry(p : tnamedindexitem;arg:pointer);
  308. var
  309. hp : tprocdef;
  310. pt : pprocdeftree;
  311. begin
  312. if tsym(p).typ=procsym then
  313. begin
  314. hp:=tprocsym(p).definition;
  315. while assigned(hp) do
  316. begin
  317. if (po_msgint in hp.procoptions) then
  318. begin
  319. new(pt);
  320. pt^.p:=hp;
  321. pt^.l:=nil;
  322. pt^.r:=nil;
  323. insertint(pt,root);
  324. end;
  325. hp:=hp.nextoverloaded;
  326. end;
  327. end;
  328. end;
  329. procedure tclassheader.writedmtindexentry(p : pprocdeftree);
  330. begin
  331. if assigned(p^.l) then
  332. writedmtindexentry(p^.l);
  333. al_globals.concat(Tai_const.Create_32bit(p^.data.messageinf.i));
  334. if assigned(p^.r) then
  335. writedmtindexentry(p^.r);
  336. end;
  337. procedure tclassheader.writedmtaddressentry(p : pprocdeftree);
  338. begin
  339. if assigned(p^.l) then
  340. writedmtaddressentry(p^.l);
  341. al_globals.concat(Tai_const_symbol.Createname(p^.data.mangledname,AT_FUNCTION,0));
  342. if assigned(p^.r) then
  343. writedmtaddressentry(p^.r);
  344. end;
  345. function tclassheader.gendmt : tasmlabel;
  346. var
  347. r : tasmlabel;
  348. begin
  349. root:=nil;
  350. count:=0;
  351. gendmt:=nil;
  352. { insert all message handlers into a tree, sorted by number }
  353. _class.symtable.foreach(insertdmtentry);
  354. if count>0 then
  355. begin
  356. objectlibrary.getdatalabel(r);
  357. gendmt:=r;
  358. al_globals.concat(cai_align.create(const_align(sizeof(aint))));
  359. al_globals.concat(Tai_label.Create(r));
  360. { entries for caching }
  361. al_globals.concat(Tai_const.Create_ptr(0));
  362. al_globals.concat(Tai_const.Create_ptr(0));
  363. al_globals.concat(Tai_const.Create_32bit(count));
  364. if assigned(root) then
  365. begin
  366. writedmtindexentry(root);
  367. writedmtaddressentry(root);
  368. disposeprocdeftree(root);
  369. end;
  370. end;
  371. end;
  372. {$endif WITHDMT}
  373. {**************************************
  374. Published Methods
  375. **************************************}
  376. procedure tclassheader.do_count_published_methods(p : tnamedindexitem;arg:pointer);
  377. var
  378. i : longint;
  379. pd : tprocdef;
  380. begin
  381. if (tsym(p).typ=procsym) then
  382. begin
  383. for i:=1 to tprocsym(p).procdef_count do
  384. begin
  385. pd:=tprocsym(p).procdef[i];
  386. if (pd.procsym=tsym(p)) and
  387. (sp_published in pd.symoptions) then
  388. inc(plongint(arg)^);
  389. end;
  390. end;
  391. end;
  392. procedure tclassheader.do_gen_published_methods(p : tnamedindexitem;arg:pointer);
  393. var
  394. i : longint;
  395. l : tasmlabel;
  396. pd : tprocdef;
  397. begin
  398. if (tsym(p).typ=procsym) then
  399. begin
  400. for i:=1 to tprocsym(p).procdef_count do
  401. begin
  402. pd:=tprocsym(p).procdef[i];
  403. if (pd.procsym=tsym(p)) and
  404. (sp_published in pd.symoptions) then
  405. begin
  406. objectlibrary.getdatalabel(l);
  407. asmlist[al_typedconsts].concat(cai_align.create(const_align(sizeof(aint))));
  408. asmlist[al_typedconsts].concat(Tai_label.Create(l));
  409. asmlist[al_typedconsts].concat(Tai_const.Create_8bit(length(tsym(p).realname)));
  410. asmlist[al_typedconsts].concat(Tai_string.Create(tsym(p).realname));
  411. asmlist[al_globals].concat(Tai_const.Create_sym(l));
  412. if po_abstractmethod in pd.procoptions then
  413. asmlist[al_globals].concat(Tai_const.Create_sym(nil))
  414. else
  415. asmlist[al_globals].concat(Tai_const.Createname(pd.mangledname,AT_FUNCTION,0));
  416. end;
  417. end;
  418. end;
  419. end;
  420. function tclassheader.genpublishedmethodstable : tasmlabel;
  421. var
  422. l : tasmlabel;
  423. count : longint;
  424. begin
  425. count:=0;
  426. _class.symtable.foreach(@do_count_published_methods,@count);
  427. if count>0 then
  428. begin
  429. objectlibrary.getdatalabel(l);
  430. asmlist[al_globals].concat(cai_align.create(const_align(sizeof(aint))));
  431. asmlist[al_globals].concat(Tai_label.Create(l));
  432. asmlist[al_globals].concat(Tai_const.Create_32bit(count));
  433. _class.symtable.foreach(@do_gen_published_methods,nil);
  434. genpublishedmethodstable:=l;
  435. end
  436. else
  437. genpublishedmethodstable:=nil;
  438. end;
  439. {**************************************
  440. VMT
  441. **************************************}
  442. procedure tclassheader.newdefentry(vmtentry:pvmtentry;pd:tprocdef;is_visible:boolean);
  443. var
  444. procdefcoll : pprocdefcoll;
  445. begin
  446. if (_class=pd._class) then
  447. begin
  448. { new entry is needed, override was not possible }
  449. if (po_overridingmethod in pd.procoptions) then
  450. MessagePos1(pd.fileinfo,parser_e_nothing_to_be_overridden,pd.fullprocname(false));
  451. { check that all methods have overload directive }
  452. if not(m_fpc in aktmodeswitches) then
  453. begin
  454. procdefcoll:=vmtentry^.firstprocdef;
  455. while assigned(procdefcoll) do
  456. begin
  457. if (procdefcoll^.data._class=pd._class) and
  458. ((po_overload in pd.procoptions)<>(po_overload in procdefcoll^.data.procoptions)) then
  459. begin
  460. MessagePos1(pd.fileinfo,parser_e_no_overload_for_all_procs,pd.procsym.realname);
  461. { recover }
  462. include(procdefcoll^.data.procoptions,po_overload);
  463. include(pd.procoptions,po_overload);
  464. end;
  465. procdefcoll:=procdefcoll^.next;
  466. end;
  467. end;
  468. end;
  469. { generate new entry }
  470. new(procdefcoll);
  471. procdefcoll^.data:=pd;
  472. procdefcoll^.hidden:=false;
  473. procdefcoll^.visible:=is_visible;
  474. procdefcoll^.next:=vmtentry^.firstprocdef;
  475. vmtentry^.firstprocdef:=procdefcoll;
  476. { give virtual method a number }
  477. if (po_virtualmethod in pd.procoptions) then
  478. begin
  479. pd.extnumber:=nextvirtnumber;
  480. inc(nextvirtnumber);
  481. has_virtual_method:=true;
  482. end;
  483. if (pd.proctypeoption=potype_constructor) then
  484. has_constructor:=true;
  485. end;
  486. function tclassheader.newvmtentry(sym:tprocsym):pvmtentry;
  487. begin
  488. { generate new vmtentry }
  489. new(result);
  490. result^.speedvalue:=sym.speedvalue;
  491. result^.name:=stringdup(sym.name);
  492. result^.next:=firstvmtentry;
  493. result^.firstprocdef:=nil;
  494. firstvmtentry:=result;
  495. end;
  496. procedure tclassheader.eachsym(sym : tnamedindexitem;arg:pointer);
  497. const
  498. po_comp = [po_classmethod,po_virtualmethod,po_staticmethod,po_interrupt,po_iocheck,po_msgstr,po_msgint,
  499. po_exports,po_varargs,po_explicitparaloc,po_nostackframe];
  500. label
  501. handlenextdef;
  502. var
  503. pd : tprocdef;
  504. i : cardinal;
  505. is_visible,
  506. hasoverloads,
  507. pdoverload : boolean;
  508. procdefcoll : pprocdefcoll;
  509. vmtentry : pvmtentry;
  510. _name : string;
  511. _speed : cardinal;
  512. begin
  513. if (tsym(sym).typ<>procsym) then
  514. exit;
  515. { check the current list of symbols }
  516. _name:=sym.name;
  517. _speed:=sym.speedvalue;
  518. vmtentry:=firstvmtentry;
  519. while assigned(vmtentry) do
  520. begin
  521. { does the symbol already exist in the list? First
  522. compare speedvalue before doing the string compare to
  523. speed it up a little }
  524. if (_speed=vmtentry^.speedvalue) and
  525. (_name=vmtentry^.name^) then
  526. begin
  527. hasoverloads:=(Tprocsym(sym).procdef_count>1);
  528. { walk through all defs of the symbol }
  529. for i:=1 to Tprocsym(sym).procdef_count do
  530. begin
  531. pd:=Tprocsym(sym).procdef[i];
  532. { is this procdef visible from the class that we are
  533. generating. This will be used to hide the other procdefs.
  534. When the symbol is not visible we don't hide the other
  535. procdefs, because they can be reused in the next class.
  536. The check to skip the invisible methods that are in the
  537. list is futher down in the code }
  538. is_visible:=pd.is_visible_for_object(_class);
  539. if pd.procsym=sym then
  540. begin
  541. pdoverload:=(po_overload in pd.procoptions);
  542. { compare with all stored definitions }
  543. procdefcoll:=vmtentry^.firstprocdef;
  544. while assigned(procdefcoll) do
  545. begin
  546. { compare only if the definition is not hidden }
  547. if not procdefcoll^.hidden then
  548. begin
  549. { check if one of the two methods has virtual }
  550. if (po_virtualmethod in procdefcoll^.data.procoptions) or
  551. (po_virtualmethod in pd.procoptions) then
  552. begin
  553. { if the current definition has no virtual then hide the
  554. old virtual if the new definition has the same arguments or
  555. when it has no overload directive and no overloads }
  556. if not(po_virtualmethod in pd.procoptions) then
  557. begin
  558. if procdefcoll^.visible and
  559. (not(pdoverload or hasoverloads) or
  560. (compare_paras(procdefcoll^.data.paras,pd.paras,cp_all,[])>=te_equal)) then
  561. begin
  562. if is_visible then
  563. procdefcoll^.hidden:=true;
  564. if (_class=pd._class) and not(po_reintroduce in pd.procoptions) then
  565. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false));
  566. end;
  567. end
  568. { if both are virtual we check the header }
  569. else if (po_virtualmethod in pd.procoptions) and
  570. (po_virtualmethod in procdefcoll^.data.procoptions) then
  571. begin
  572. { new one has not override }
  573. if is_class(_class) and
  574. not(po_overridingmethod in pd.procoptions) then
  575. begin
  576. { we start a new virtual tree, hide the old }
  577. if (not(pdoverload or hasoverloads) or
  578. (compare_paras(procdefcoll^.data.paras,pd.paras,cp_all,[])>=te_equal)) and
  579. (procdefcoll^.visible) then
  580. begin
  581. if is_visible then
  582. procdefcoll^.hidden:=true;
  583. if (_class=pd._class) and not(po_reintroduce in pd.procoptions) then
  584. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false));
  585. end;
  586. end
  587. { same parameters }
  588. else if (compare_paras(procdefcoll^.data.paras,pd.paras,cp_all,[])>=te_equal) then
  589. begin
  590. { overload is inherited }
  591. if (po_overload in procdefcoll^.data.procoptions) then
  592. include(pd.procoptions,po_overload);
  593. { inherite calling convention when it was force and the
  594. current definition has none force }
  595. if (po_hascallingconvention in procdefcoll^.data.procoptions) and
  596. not(po_hascallingconvention in pd.procoptions) then
  597. begin
  598. pd.proccalloption:=procdefcoll^.data.proccalloption;
  599. include(pd.procoptions,po_hascallingconvention);
  600. end;
  601. { the flags have to match except abstract and override }
  602. { only if both are virtual !! }
  603. if (procdefcoll^.data.proccalloption<>pd.proccalloption) or
  604. (procdefcoll^.data.proctypeoption<>pd.proctypeoption) or
  605. ((procdefcoll^.data.procoptions*po_comp)<>(pd.procoptions*po_comp)) then
  606. begin
  607. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  608. tprocsym(procdefcoll^.data.procsym).write_parameter_lists(pd);
  609. end;
  610. { error, if the return types aren't equal }
  611. if not(equal_defs(procdefcoll^.data.rettype.def,pd.rettype.def)) and
  612. not((procdefcoll^.data.rettype.def.deftype=objectdef) and
  613. (pd.rettype.def.deftype=objectdef) and
  614. is_class(procdefcoll^.data.rettype.def) and
  615. is_class(pd.rettype.def) and
  616. (tobjectdef(pd.rettype.def).is_related(
  617. tobjectdef(procdefcoll^.data.rettype.def)))) then
  618. Message2(parser_e_overridden_methods_not_same_ret,pd.fullprocname(false),
  619. procdefcoll^.data.fullprocname(false));
  620. { check if the method to override is visible, check is only needed
  621. for the current parsed class. Parent classes are already validated and
  622. need to include all virtual methods including the ones not visible in the
  623. current class }
  624. if (_class=pd._class) and
  625. (po_overridingmethod in pd.procoptions) and
  626. (not procdefcoll^.visible) then
  627. MessagePos1(pd.fileinfo,parser_e_nothing_to_be_overridden,pd.fullprocname(false));
  628. { override old virtual method in VMT }
  629. pd.extnumber:=procdefcoll^.data.extnumber;
  630. procdefcoll^.data:=pd;
  631. if is_visible then
  632. procdefcoll^.visible:=true;
  633. goto handlenextdef;
  634. end
  635. { different parameters }
  636. else
  637. begin
  638. { when we got an override directive then can search futher for
  639. the procedure to override.
  640. If we are starting a new virtual tree then hide the old tree }
  641. if not(po_overridingmethod in pd.procoptions) and
  642. not pdoverload then
  643. begin
  644. if is_visible then
  645. procdefcoll^.hidden:=true;
  646. if (_class=pd._class) and not(po_reintroduce in pd.procoptions) then
  647. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false));
  648. end;
  649. end;
  650. end
  651. else
  652. begin
  653. { the new definition is virtual and the old static, we hide the old one
  654. if the new defintion has not the overload directive }
  655. if is_visible and
  656. ((not(pdoverload or hasoverloads)) or
  657. (compare_paras(procdefcoll^.data.paras,pd.paras,cp_all,[])>=te_equal)) then
  658. procdefcoll^.hidden:=true;
  659. end;
  660. end
  661. else
  662. begin
  663. { both are static, we hide the old one if the new defintion
  664. has not the overload directive }
  665. if is_visible and
  666. ((not pdoverload) or
  667. (compare_paras(procdefcoll^.data.paras,pd.paras,cp_all,[])>=te_equal)) then
  668. procdefcoll^.hidden:=true;
  669. end;
  670. end; { not hidden }
  671. procdefcoll:=procdefcoll^.next;
  672. end;
  673. { if it isn't saved in the list we create a new entry }
  674. newdefentry(vmtentry,pd,is_visible);
  675. end;
  676. handlenextdef:
  677. end;
  678. exit;
  679. end;
  680. vmtentry:=vmtentry^.next;
  681. end;
  682. { Generate new procsym entry in vmt }
  683. vmtentry:=newvmtentry(tprocsym(sym));
  684. { Add procdefs }
  685. for i:=1 to Tprocsym(sym).procdef_count do
  686. begin
  687. pd:=Tprocsym(sym).procdef[i];
  688. newdefentry(vmtentry,pd,pd.is_visible_for_object(_class));
  689. end;
  690. end;
  691. procedure tclassheader.disposevmttree;
  692. var
  693. vmtentry : pvmtentry;
  694. procdefcoll : pprocdefcoll;
  695. begin
  696. { disposes the above generated tree }
  697. vmtentry:=firstvmtentry;
  698. while assigned(vmtentry) do
  699. begin
  700. firstvmtentry:=vmtentry^.next;
  701. stringdispose(vmtentry^.name);
  702. procdefcoll:=vmtentry^.firstprocdef;
  703. while assigned(procdefcoll) do
  704. begin
  705. vmtentry^.firstprocdef:=procdefcoll^.next;
  706. dispose(procdefcoll);
  707. procdefcoll:=vmtentry^.firstprocdef;
  708. end;
  709. dispose(vmtentry);
  710. vmtentry:=firstvmtentry;
  711. end;
  712. end;
  713. procedure tclassheader.genvmt;
  714. procedure do_genvmt(p : tobjectdef);
  715. begin
  716. { start with the base class }
  717. if assigned(p.childof) then
  718. do_genvmt(p.childof);
  719. { walk through all public syms }
  720. p.symtable.foreach(@eachsym,nil);
  721. end;
  722. begin
  723. firstvmtentry:=nil;
  724. nextvirtnumber:=0;
  725. has_constructor:=false;
  726. has_virtual_method:=false;
  727. { generates a tree of all used methods }
  728. do_genvmt(_class);
  729. if not(is_interface(_class)) and
  730. has_virtual_method and
  731. not(has_constructor) then
  732. Message1(parser_w_virtual_without_constructor,_class.objrealname^);
  733. end;
  734. {**************************************
  735. Interface tables
  736. **************************************}
  737. function tclassheader.gintfgetvtbllabelname(intfindex: integer): string;
  738. begin
  739. gintfgetvtbllabelname:=make_mangledname('VTBL',_class.owner,_class.objname^+
  740. '_$_'+_class.implementedinterfaces.interfaces(intfindex).objname^);
  741. end;
  742. procedure tclassheader.gintfcreatevtbl(intfindex: integer; rawdata: TAAsmoutput);
  743. var
  744. implintf: timplementedinterfaces;
  745. curintf: tobjectdef;
  746. proccount: integer;
  747. tmps: string;
  748. i: longint;
  749. begin
  750. implintf:=_class.implementedinterfaces;
  751. curintf:=implintf.interfaces(intfindex);
  752. section_symbol_start(rawdata,gintfgetvtbllabelname(intfindex),AT_DATA,true,sec_data,const_align(sizeof(aint)));
  753. proccount:=implintf.implproccount(intfindex);
  754. for i:=1 to proccount do
  755. begin
  756. tmps:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+curintf.objname^+'_$_'+
  757. tostr(i)+'_$_'+
  758. implintf.implprocs(intfindex,i).mangledname);
  759. { create reference }
  760. rawdata.concat(Tai_const.Createname(tmps,AT_FUNCTION,0));
  761. end;
  762. section_symbol_end(rawdata,gintfgetvtbllabelname(intfindex));
  763. end;
  764. procedure tclassheader.gintfgenentry(intfindex, contintfindex: integer; rawdata: TAAsmoutput);
  765. var
  766. implintf: timplementedinterfaces;
  767. curintf: tobjectdef;
  768. tmplabel: tasmlabel;
  769. i: longint;
  770. begin
  771. implintf:=_class.implementedinterfaces;
  772. curintf:=implintf.interfaces(intfindex);
  773. { GUID }
  774. if curintf.objecttype in [odt_interfacecom] then
  775. begin
  776. { label for GUID }
  777. objectlibrary.getdatalabel(tmplabel);
  778. rawdata.concat(cai_align.create(const_align(sizeof(aint))));
  779. rawdata.concat(Tai_label.Create(tmplabel));
  780. rawdata.concat(Tai_const.Create_32bit(longint(curintf.iidguid^.D1)));
  781. rawdata.concat(Tai_const.Create_16bit(curintf.iidguid^.D2));
  782. rawdata.concat(Tai_const.Create_16bit(curintf.iidguid^.D3));
  783. for i:=Low(curintf.iidguid^.D4) to High(curintf.iidguid^.D4) do
  784. rawdata.concat(Tai_const.Create_8bit(curintf.iidguid^.D4[i]));
  785. asmlist[al_globals].concat(Tai_const.Create_sym(tmplabel));
  786. end
  787. else
  788. begin
  789. { nil for Corba interfaces }
  790. asmlist[al_globals].concat(Tai_const.Create_sym(nil));
  791. end;
  792. { VTable }
  793. asmlist[al_globals].concat(Tai_const.Createname(gintfgetvtbllabelname(contintfindex),AT_DATA,0));
  794. { IOffset field }
  795. asmlist[al_globals].concat(Tai_const.Create_32bit(implintf.ioffsets(contintfindex)));
  796. { IIDStr }
  797. objectlibrary.getdatalabel(tmplabel);
  798. rawdata.concat(cai_align.create(const_align(sizeof(aint))));
  799. rawdata.concat(Tai_label.Create(tmplabel));
  800. rawdata.concat(Tai_const.Create_8bit(length(curintf.iidstr^)));
  801. if curintf.objecttype=odt_interfacecom then
  802. rawdata.concat(Tai_string.Create(upper(curintf.iidstr^)))
  803. else
  804. rawdata.concat(Tai_string.Create(curintf.iidstr^));
  805. asmlist[al_globals].concat(Tai_const.Create_sym(tmplabel));
  806. end;
  807. procedure tclassheader.gintfoptimizevtbls;
  808. type
  809. tcompintfentry = record
  810. weight: longint;
  811. compintf: longint;
  812. end;
  813. { Max 1000 interface in the class header interfaces it's enough imho }
  814. tcompintfs = array[1..1000] of tcompintfentry;
  815. pcompintfs = ^tcompintfs;
  816. tequals = array[1..1000] of longint;
  817. pequals = ^tequals;
  818. timpls = array[1..1000] of longint;
  819. pimpls = ^timpls;
  820. var
  821. max: longint;
  822. equals: pequals;
  823. compats: pcompintfs;
  824. impls: pimpls;
  825. w,i,j,k: longint;
  826. cij: boolean;
  827. cji: boolean;
  828. begin
  829. max:=_class.implementedinterfaces.count;
  830. if max>High(tequals) then
  831. Internalerror(200006135);
  832. getmem(compats,sizeof(tcompintfentry)*max);
  833. getmem(equals,sizeof(longint)*max);
  834. getmem(impls,sizeof(longint)*max);
  835. fillchar(compats^,sizeof(tcompintfentry)*max,0);
  836. fillchar(equals^,sizeof(longint)*max,0);
  837. fillchar(impls^,sizeof(longint)*max,0);
  838. { ismergepossible is a containing relation
  839. meaning of ismergepossible(a,b,w) =
  840. if implementorfunction map of a is contained implementorfunction map of b
  841. imp(a,b) and imp(b,c) => imp(a,c) ; imp(a,b) and imp(b,a) => a == b
  842. }
  843. { the order is very important for correct allocation }
  844. for i:=1 to max do
  845. begin
  846. for j:=i+1 to max do
  847. begin
  848. cij:=_class.implementedinterfaces.isimplmergepossible(i,j,w);
  849. cji:=_class.implementedinterfaces.isimplmergepossible(j,i,w);
  850. if cij and cji then { i equal j }
  851. begin
  852. { get minimum index of equal }
  853. if equals^[j]=0 then
  854. equals^[j]:=i;
  855. end
  856. else if cij then
  857. begin
  858. { get minimum index of maximum weight }
  859. if compats^[i].weight<w then
  860. begin
  861. compats^[i].weight:=w;
  862. compats^[i].compintf:=j;
  863. end;
  864. end
  865. else if cji then
  866. begin
  867. { get minimum index of maximum weight }
  868. if (compats^[j].weight<w) then
  869. begin
  870. compats^[j].weight:=w;
  871. compats^[j].compintf:=i;
  872. end;
  873. end;
  874. end;
  875. end;
  876. { Reset, no replacements by default }
  877. for i:=1 to max do
  878. impls^[i]:=i;
  879. { Replace vtbls when equal or compat, repeat
  880. until there are no replacements possible anymore. This is
  881. needed for the cases like:
  882. First loop: 2->3, 3->1
  883. Second loop: 2->1 (because 3 was replaced with 1)
  884. }
  885. repeat
  886. k:=0;
  887. for i:=1 to max do
  888. begin
  889. if compats^[impls^[i]].compintf<>0 then
  890. impls^[i]:=compats^[impls^[i]].compintf
  891. else if equals^[impls^[i]]<>0 then
  892. impls^[i]:=equals^[impls^[i]]
  893. else
  894. inc(k);
  895. end;
  896. until k=max;
  897. { Update the implindex }
  898. for i:=1 to max do
  899. _class.implementedinterfaces.setimplindex(i,impls^[i]);
  900. freemem(compats);
  901. freemem(equals);
  902. freemem(impls);
  903. end;
  904. procedure tclassheader.gintfwritedata;
  905. var
  906. rawdata: taasmoutput;
  907. max,i,j : smallint;
  908. begin
  909. max:=_class.implementedinterfaces.count;
  910. rawdata:=TAAsmOutput.Create;
  911. asmlist[al_globals].concat(Tai_const.Create_16bit(max));
  912. { Two pass, one for allocation and vtbl creation }
  913. for i:=1 to max do
  914. begin
  915. if _class.implementedinterfaces.implindex(i)=i then { if implement itself }
  916. begin
  917. { allocate a pointer in the object memory }
  918. with tobjectsymtable(_class.symtable) do
  919. begin
  920. datasize:=align(datasize,min(sizeof(aint),fieldalignment));
  921. _class.implementedinterfaces.setioffsets(i,datasize);
  922. inc(datasize,sizeof(aint));
  923. end;
  924. { write vtbl }
  925. gintfcreatevtbl(i,rawdata);
  926. end;
  927. end;
  928. { second pass: for fill interfacetable and remained ioffsets }
  929. for i:=1 to max do
  930. begin
  931. j:=_class.implementedinterfaces.implindex(i);
  932. if j<>i then
  933. _class.implementedinterfaces.setioffsets(i,_class.implementedinterfaces.ioffsets(j));
  934. gintfgenentry(i,j,rawdata);
  935. end;
  936. asmlist[al_globals].concatlist(rawdata);
  937. rawdata.free;
  938. end;
  939. function tclassheader.gintfgetcprocdef(proc: tprocdef;const name: string): tprocdef;
  940. const
  941. po_comp = [po_classmethod,po_staticmethod,po_interrupt,po_iocheck,po_msgstr,po_msgint,
  942. po_exports,po_varargs,po_explicitparaloc,po_nostackframe];
  943. var
  944. sym: tsym;
  945. implprocdef : Tprocdef;
  946. i: cardinal;
  947. begin
  948. gintfgetcprocdef:=nil;
  949. sym:=tsym(search_class_member(_class,name));
  950. if assigned(sym) and
  951. (sym.typ=procsym) then
  952. begin
  953. { when the definition has overload directive set, we search for
  954. overloaded definitions in the class, this only needs to be done once
  955. for class entries as the tree keeps always the same }
  956. if (not tprocsym(sym).overloadchecked) and
  957. (po_overload in tprocsym(sym).first_procdef.procoptions) and
  958. (tprocsym(sym).owner.symtabletype=objectsymtable) then
  959. search_class_overloads(tprocsym(sym));
  960. for i:=1 to tprocsym(sym).procdef_count do
  961. begin
  962. implprocdef:=tprocsym(sym).procdef[i];
  963. if (compare_paras(proc.paras,implprocdef.paras,cp_none,[])>=te_equal) and
  964. (proc.proccalloption=implprocdef.proccalloption) and
  965. (proc.proctypeoption=implprocdef.proctypeoption) and
  966. ((proc.procoptions*po_comp)=((implprocdef.procoptions+[po_virtualmethod])*po_comp)) then
  967. begin
  968. gintfgetcprocdef:=implprocdef;
  969. exit;
  970. end;
  971. end;
  972. end;
  973. end;
  974. procedure tclassheader.gintfdoonintf(intf: tobjectdef; intfindex: longint);
  975. var
  976. def: tdef;
  977. mappedname: string;
  978. nextexist: pointer;
  979. implprocdef: tprocdef;
  980. begin
  981. def:=tdef(intf.symtable.defindex.first);
  982. while assigned(def) do
  983. begin
  984. if def.deftype=procdef then
  985. begin
  986. implprocdef:=nil;
  987. nextexist:=nil;
  988. repeat
  989. mappedname:=_class.implementedinterfaces.getmappings(intfindex,tprocdef(def).procsym.name,nextexist);
  990. if mappedname<>'' then
  991. implprocdef:=gintfgetcprocdef(tprocdef(def),mappedname);
  992. until assigned(implprocdef) or not assigned(nextexist);
  993. if not assigned(implprocdef) then
  994. implprocdef:=gintfgetcprocdef(tprocdef(def),tprocdef(def).procsym.name);
  995. if assigned(implprocdef) then
  996. _class.implementedinterfaces.addimplproc(intfindex,implprocdef)
  997. else
  998. Message1(sym_e_no_matching_implementation_found,tprocdef(def).fullprocname(false));
  999. end;
  1000. def:=tdef(def.indexnext);
  1001. end;
  1002. end;
  1003. procedure tclassheader.gintfwalkdowninterface(intf: tobjectdef; intfindex: longint);
  1004. begin
  1005. if assigned(intf.childof) then
  1006. gintfwalkdowninterface(intf.childof,intfindex);
  1007. gintfdoonintf(intf,intfindex);
  1008. end;
  1009. function tclassheader.genintftable: tasmlabel;
  1010. var
  1011. intfindex: longint;
  1012. curintf: tobjectdef;
  1013. intftable: tasmlabel;
  1014. begin
  1015. { 1. step collect implementor functions into the implementedinterfaces.implprocs }
  1016. for intfindex:=1 to _class.implementedinterfaces.count do
  1017. begin
  1018. curintf:=_class.implementedinterfaces.interfaces(intfindex);
  1019. gintfwalkdowninterface(curintf,intfindex);
  1020. end;
  1021. { 2. step calc required fieldcount and their offsets in the object memory map
  1022. and write data }
  1023. objectlibrary.getdatalabel(intftable);
  1024. asmlist[al_globals].concat(cai_align.create(const_align(sizeof(aint))));
  1025. asmlist[al_globals].concat(Tai_label.Create(intftable));
  1026. { Optimize interface tables to reuse wrappers }
  1027. gintfoptimizevtbls;
  1028. { Write interface tables }
  1029. gintfwritedata;
  1030. genintftable:=intftable;
  1031. end;
  1032. { Write interface identifiers to the data section }
  1033. procedure tclassheader.writeinterfaceids;
  1034. var
  1035. i : longint;
  1036. s : string;
  1037. begin
  1038. if assigned(_class.iidguid) then
  1039. begin
  1040. s:=make_mangledname('IID',_class.owner,_class.objname^);
  1041. maybe_new_object_file(asmlist[al_globals]);
  1042. new_section(asmlist[al_globals],sec_rodata,s,const_align(sizeof(aint)));
  1043. asmlist[al_globals].concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  1044. asmlist[al_globals].concat(Tai_const.Create_32bit(longint(_class.iidguid^.D1)));
  1045. asmlist[al_globals].concat(Tai_const.Create_16bit(_class.iidguid^.D2));
  1046. asmlist[al_globals].concat(Tai_const.Create_16bit(_class.iidguid^.D3));
  1047. for i:=Low(_class.iidguid^.D4) to High(_class.iidguid^.D4) do
  1048. asmlist[al_globals].concat(Tai_const.Create_8bit(_class.iidguid^.D4[i]));
  1049. end;
  1050. maybe_new_object_file(asmlist[al_globals]);
  1051. s:=make_mangledname('IIDSTR',_class.owner,_class.objname^);
  1052. new_section(asmlist[al_globals],sec_rodata,s,0);
  1053. asmlist[al_globals].concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  1054. asmlist[al_globals].concat(Tai_const.Create_8bit(length(_class.iidstr^)));
  1055. asmlist[al_globals].concat(Tai_string.Create(_class.iidstr^));
  1056. end;
  1057. procedure tclassheader.writevirtualmethods(List:TAAsmoutput);
  1058. var
  1059. vmtentry : pvmtentry;
  1060. procdefcoll : pprocdefcoll;
  1061. i : longint;
  1062. begin
  1063. { walk trough all numbers for virtual methods and search }
  1064. { the method }
  1065. for i:=0 to nextvirtnumber-1 do
  1066. begin
  1067. { walk trough all symbols }
  1068. vmtentry:=firstvmtentry;
  1069. while assigned(vmtentry) do
  1070. begin
  1071. { walk trough all methods }
  1072. procdefcoll:=vmtentry^.firstprocdef;
  1073. while assigned(procdefcoll) do
  1074. begin
  1075. { writes the addresses to the VMT }
  1076. { but only this which are declared as virtual }
  1077. if procdefcoll^.data.extnumber=i then
  1078. begin
  1079. if (po_virtualmethod in procdefcoll^.data.procoptions) then
  1080. begin
  1081. { if a method is abstract, then is also the }
  1082. { class abstract and it's not allow to }
  1083. { generates an instance }
  1084. if (po_abstractmethod in procdefcoll^.data.procoptions) then
  1085. List.concat(Tai_const.Createname('FPC_ABSTRACTERROR',AT_FUNCTION,0))
  1086. else
  1087. List.concat(Tai_const.createname(procdefcoll^.data.mangledname,AT_FUNCTION,0));
  1088. end;
  1089. end;
  1090. procdefcoll:=procdefcoll^.next;
  1091. end;
  1092. vmtentry:=vmtentry^.next;
  1093. end;
  1094. end;
  1095. end;
  1096. { generates the vmt for classes as well as for objects }
  1097. procedure tclassheader.writevmt;
  1098. var
  1099. methodnametable,intmessagetable,
  1100. strmessagetable,classnamelabel,
  1101. fieldtablelabel : tasmlabel;
  1102. {$ifdef WITHDMT}
  1103. dmtlabel : tasmlabel;
  1104. {$endif WITHDMT}
  1105. interfacetable : tasmlabel;
  1106. begin
  1107. {$ifdef WITHDMT}
  1108. dmtlabel:=gendmt;
  1109. {$endif WITHDMT}
  1110. { write tables for classes, this must be done before the actual
  1111. class is written, because we need the labels defined }
  1112. if is_class(_class) then
  1113. begin
  1114. objectlibrary.getdatalabel(classnamelabel);
  1115. maybe_new_object_file(asmlist[al_globals]);
  1116. new_section(asmlist[al_globals],sec_rodata,classnamelabel.name,const_align(sizeof(aint)));
  1117. { interface table }
  1118. if _class.implementedinterfaces.count>0 then
  1119. interfacetable:=genintftable;
  1120. methodnametable:=genpublishedmethodstable;
  1121. fieldtablelabel:=_class.generate_field_table;
  1122. { write class name }
  1123. asmlist[al_globals].concat(Tai_label.Create(classnamelabel));
  1124. asmlist[al_globals].concat(Tai_const.Create_8bit(length(_class.objrealname^)));
  1125. asmlist[al_globals].concat(Tai_string.Create(_class.objrealname^));
  1126. { generate message and dynamic tables }
  1127. if (oo_has_msgstr in _class.objectoptions) then
  1128. strmessagetable:=genstrmsgtab;
  1129. if (oo_has_msgint in _class.objectoptions) then
  1130. intmessagetable:=genintmsgtab;
  1131. end;
  1132. { write debug info }
  1133. maybe_new_object_file(asmlist[al_globals]);
  1134. new_section(asmlist[al_globals],sec_rodata,_class.vmt_mangledname,const_align(sizeof(aint)));
  1135. if (cs_debuginfo in aktmoduleswitches) then
  1136. debuginfo.insertvmt(asmlist[al_globals],_class);
  1137. asmlist[al_globals].concat(Tai_symbol.Createname_global(_class.vmt_mangledname,AT_DATA,0));
  1138. { determine the size with symtable.datasize, because }
  1139. { size gives back 4 for classes }
  1140. asmlist[al_globals].concat(Tai_const.Create(ait_const_ptr,tobjectsymtable(_class.symtable).datasize));
  1141. asmlist[al_globals].concat(Tai_const.Create(ait_const_ptr,-int64(tobjectsymtable(_class.symtable).datasize)));
  1142. {$ifdef WITHDMT}
  1143. if _class.classtype=ct_object then
  1144. begin
  1145. if assigned(dmtlabel) then
  1146. asmlist[al_globals].concat(Tai_const_symbol.Create(dmtlabel)))
  1147. else
  1148. asmlist[al_globals].concat(Tai_const.Create_ptr(0));
  1149. end;
  1150. {$endif WITHDMT}
  1151. { write pointer to parent VMT, this isn't implemented in TP }
  1152. { but this is not used in FPC ? (PM) }
  1153. { it's not used yet, but the delphi-operators as and is need it (FK) }
  1154. { it is not written for parents that don't have any vmt !! }
  1155. if assigned(_class.childof) and
  1156. (oo_has_vmt in _class.childof.objectoptions) then
  1157. asmlist[al_globals].concat(Tai_const.Createname(_class.childof.vmt_mangledname,AT_DATA,0))
  1158. else
  1159. asmlist[al_globals].concat(Tai_const.Create_sym(nil));
  1160. { write extended info for classes, for the order see rtl/inc/objpash.inc }
  1161. if is_class(_class) then
  1162. begin
  1163. { pointer to class name string }
  1164. asmlist[al_globals].concat(Tai_const.Create_sym(classnamelabel));
  1165. { pointer to dynamic table or nil }
  1166. if (oo_has_msgint in _class.objectoptions) then
  1167. asmlist[al_globals].concat(Tai_const.Create_sym(intmessagetable))
  1168. else
  1169. asmlist[al_globals].concat(Tai_const.Create_sym(nil));
  1170. { pointer to method table or nil }
  1171. asmlist[al_globals].concat(Tai_const.Create_sym(methodnametable));
  1172. { pointer to field table }
  1173. asmlist[al_globals].concat(Tai_const.Create_sym(fieldtablelabel));
  1174. { pointer to type info of published section }
  1175. if (oo_can_have_published in _class.objectoptions) then
  1176. asmlist[al_globals].concat(Tai_const.Create_sym(_class.get_rtti_label(fullrtti)))
  1177. else
  1178. asmlist[al_globals].concat(Tai_const.Create_sym(nil));
  1179. { inittable for con-/destruction }
  1180. if _class.members_need_inittable then
  1181. asmlist[al_globals].concat(Tai_const.Create_sym(_class.get_rtti_label(initrtti)))
  1182. else
  1183. asmlist[al_globals].concat(Tai_const.Create_sym(nil));
  1184. { auto table }
  1185. asmlist[al_globals].concat(Tai_const.Create_sym(nil));
  1186. { interface table }
  1187. if _class.implementedinterfaces.count>0 then
  1188. asmlist[al_globals].concat(Tai_const.Create_sym(interfacetable))
  1189. else
  1190. asmlist[al_globals].concat(Tai_const.Create_sym(nil));
  1191. { table for string messages }
  1192. if (oo_has_msgstr in _class.objectoptions) then
  1193. asmlist[al_globals].concat(Tai_const.Create_sym(strmessagetable))
  1194. else
  1195. asmlist[al_globals].concat(Tai_const.Create_sym(nil));
  1196. end;
  1197. { write virtual methods }
  1198. writevirtualmethods(asmlist[al_globals]);
  1199. asmlist[al_globals].concat(Tai_const.create(ait_const_ptr,0));
  1200. { write the size of the VMT }
  1201. asmlist[al_globals].concat(Tai_symbol_end.Createname(_class.vmt_mangledname));
  1202. end;
  1203. end.