ncgvmt.pas 55 KB

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