ncgvmt.pas 53 KB

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