ncgvmt.pas 56 KB

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