ncgvmt.pas 53 KB

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