ncgvmt.pas 55 KB

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