ncgvmt.pas 53 KB

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