ncgvmt.pas 52 KB

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