ncgvmt.pas 51 KB

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