ncgvmt.pas 52 KB

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