ncgvmt.pas 54 KB

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