ncgvmt.pas 53 KB

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