ncgvmt.pas 54 KB

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