ncgvmt.pas 55 KB

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