ncgvmt.pas 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generates VMT for classes/objects and interface wrappers
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ncgvmt;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. aasmdata,aasmbase,aasmcnst,
  22. symbase,symconst,symtype,symdef;
  23. type
  24. pprocdeftree = ^tprocdeftree;
  25. tprocdeftree = record
  26. data : tprocdef;
  27. nl : tasmlabel;
  28. l,r : pprocdeftree;
  29. end;
  30. TVMTWriter=class
  31. protected
  32. _Class : tobjectdef;
  33. { message tables }
  34. root : pprocdeftree;
  35. { implemented interface vtables }
  36. fintfvtablelabels: array of TAsmLabel;
  37. procedure disposeprocdeftree(p : pprocdeftree);
  38. procedure insertmsgint(p:TObject;arg:pointer);
  39. procedure insertmsgstr(p:TObject;arg:pointer);
  40. procedure insertint(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  41. procedure insertstr(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  42. function RedirectToEmpty(procdef: tprocdef): boolean;
  43. procedure writenames(tcb: ttai_typedconstbuilder; p: pprocdeftree);
  44. procedure writeintentry(tcb: ttai_typedconstbuilder; p: pprocdeftree; entrydef: tdef);
  45. procedure writestrentry(tcb: ttai_typedconstbuilder; p: pprocdeftree; entrydef: tdef);
  46. {$ifdef WITHDMT}
  47. { dmt }
  48. procedure insertdmtentry(p:TObject;arg:pointer);
  49. procedure writedmtindexentry(p : pprocdeftree);
  50. procedure writedmtaddressentry(p : pprocdeftree);
  51. {$endif}
  52. { published methods }
  53. procedure do_count_published_methods(p:TObject;arg:pointer);
  54. procedure do_gen_published_methods(p:TObject;arg:pointer);
  55. { virtual methods }
  56. procedure writevirtualmethods(tcb: ttai_typedconstbuilder);
  57. { interface tables }
  58. procedure intf_create_vtbl(tcb: ttai_typedconstbuilder; AImplIntf: TImplementedInterface; intfindex: longint);
  59. procedure intf_gen_intf_ref(tcb: ttai_typedconstbuilder; AImplIntf: TImplementedInterface; intfindex: longint; interfaceentrydef, interfaceentrytypedef: tdef);
  60. procedure intf_write_table(tcb: ttai_typedconstbuilder; out lab: TAsmLabel; out intftabledef: trecorddef);
  61. { 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. datatcb: ttai_typedconstbuilder;
  462. packrecords: longint;
  463. begin
  464. // TODO(ryan): is extended method table packed?
  465. if (tf_requires_proper_alignment in target_info.flags) then
  466. packrecords:=0
  467. else
  468. packrecords:=1;
  469. count:=0;
  470. _class.symtable.SymList.ForEachCall(@do_count_published_methods,@count);
  471. if (count>0) or (_class.rtti.options[ro_methods]<>[]) then
  472. begin
  473. { in the list of the published methods (from objpas.inc):
  474. tmethodnamerec = packed record
  475. name : pshortstring;
  476. addr : codepointer;
  477. end;
  478. }
  479. lists.methodnamerec:=get_recorddef(itp_vmt_intern_tmethodnamerec,[cpointerdef.getreusable(cshortstringtype),voidcodepointertype],1);
  480. { from objpas.inc:
  481. tmethodnametable = packed record
  482. count : dword;
  483. entries : packed array[0..0] of tmethodnamerec;
  484. end;
  485. }
  486. tcb.start_internal_data_builder(current_asmdata.AsmLists[al_const],sec_rodata,_class.vmt_mangledname,lists.pubmethodstcb,lab);
  487. get_tabledef(itp_vmt_intern_tmethodnametable,u32inttype,lists.methodnamerec,count,1,pubmethodsdef,pubmethodsarraydef);
  488. { begin tmethodnametable }
  489. lists.pubmethodstcb.maybe_begin_aggregate(pubmethodsdef);
  490. { emit count field }
  491. lists.pubmethodstcb.emit_tai(Tai_const.Create_32bit(count),u32inttype);
  492. if count>0 then
  493. begin
  494. { begin entries field (array) }
  495. lists.pubmethodstcb.maybe_begin_aggregate(pubmethodsarraydef);
  496. { add all entries elements }
  497. _class.symtable.SymList.ForEachCall(@do_gen_published_methods,@lists);
  498. { end entries field (array) }
  499. lists.pubmethodstcb.maybe_end_aggregate(pubmethodsarraydef);
  500. end;
  501. { end methodnametable }
  502. lists.pubmethodstcb.maybe_end_aggregate(pubmethodsdef);
  503. { write extended method rtti }
  504. RTTIWriter.write_extended_method_table(lists.pubmethodstcb,_class,packrecords);
  505. tcb.finish_internal_data_builder(lists.pubmethodstcb,lab,pubmethodsdef,sizeof(pint));
  506. end
  507. else
  508. begin
  509. lab:=nil;
  510. pubmethodsdef:=nil;
  511. end;
  512. end;
  513. procedure TVMTWriter.generate_field_table(tcb: ttai_typedconstbuilder; out lab: tasmlabel; out fieldtabledef: trecorddef);
  514. var
  515. i : longint;
  516. sym : tsym;
  517. classtable : tasmlabel;
  518. classindex,
  519. fieldcount : longint;
  520. classtablelist : TFPList;
  521. datatcb: ttai_typedconstbuilder;
  522. packrecords: longint;
  523. classdef: tobjectdef;
  524. classtabledef: trecorddef;
  525. begin
  526. classtable:=nil;
  527. classtablelist:=TFPList.Create;
  528. classtabledef:=nil;
  529. { retrieve field info fields }
  530. fieldcount:=0;
  531. for i:=0 to _class.symtable.SymList.Count-1 do
  532. begin
  533. sym:=tsym(_class.symtable.SymList[i]);
  534. if (sym.typ=fieldvarsym) and
  535. not(sp_static in sym.symoptions) and
  536. (sym.visibility=vis_published) then
  537. begin
  538. if tfieldvarsym(sym).vardef.typ<>objectdef then
  539. internalerror(200611032);
  540. classindex:=classtablelist.IndexOf(tfieldvarsym(sym).vardef);
  541. if classindex=-1 then
  542. classtablelist.Add(tfieldvarsym(sym).vardef);
  543. inc(fieldcount);
  544. end;
  545. end;
  546. if (fieldcount>0) or (_class.rtti.options[ro_fields]<>[]) then
  547. begin
  548. if (tf_requires_proper_alignment in target_info.flags) then
  549. packrecords:=0
  550. else
  551. packrecords:=1;
  552. { generate the class table }
  553. if classtablelist.count>0 then
  554. begin
  555. tcb.start_internal_data_builder(current_asmdata.AsmLists[al_const],sec_rodata,_class.vmt_mangledname,datatcb,classtable);
  556. datatcb.begin_anonymous_record('$fpc_intern_classtable_'+tostr(classtablelist.Count-1),
  557. packrecords,1,
  558. targetinfos[target_info.system]^.alignment.recordalignmin);
  559. datatcb.emit_tai(Tai_const.Create_16bit(classtablelist.count),u16inttype);
  560. for i:=0 to classtablelist.Count-1 do
  561. begin
  562. classdef:=tobjectdef(classtablelist[i]);
  563. { type of the field }
  564. datatcb.queue_init(voidpointertype);
  565. { reference to the vmt }
  566. datatcb.queue_emit_asmsym(
  567. current_asmdata.RefAsmSymbol(classdef.vmt_mangledname,AT_DATA,true),
  568. tfieldvarsym(classdef.vmt_field).vardef);
  569. end;
  570. classtabledef:=datatcb.end_anonymous_record;
  571. tcb.finish_internal_data_builder(datatcb,classtable,classtabledef,sizeof(pint));
  572. end;
  573. { write fields }
  574. {
  575. TFieldTable =
  576. $ifndef FPC_REQUIRES_PROPER_ALIGNMENT
  577. packed
  578. $endif FPC_REQUIRES_PROPER_ALIGNMENT
  579. record
  580. FieldCount: Word;
  581. ClassTable: Pointer;
  582. Fields: array[0..0] of TFieldInfo
  583. end;
  584. }
  585. tcb.start_internal_data_builder(current_asmdata.AsmLists[al_const],sec_rodata,_class.vmt_mangledname,datatcb,lab);
  586. { can't easily specify a name here for reuse of the constructed def,
  587. since it's full of variable length shortstrings (-> all of those
  588. lengths and their order would have to incorporated in the name,
  589. plus there would be very little chance that it could actually be
  590. reused }
  591. datatcb.begin_anonymous_record('',packrecords,1,
  592. targetinfos[target_info.system]^.alignment.recordalignmin);
  593. datatcb.emit_tai(Tai_const.Create_16bit(fieldcount),u16inttype);
  594. if classtable<>nil then
  595. datatcb.emit_tai(Tai_const.Create_sym(classtable),cpointerdef.getreusable(classtabledef))
  596. else
  597. datatcb.emit_tai(tai_const.Create_nil_codeptr,voidpointertype);
  598. if fieldcount>0 then
  599. begin
  600. for i:=0 to _class.symtable.SymList.Count-1 do
  601. begin
  602. sym:=tsym(_class.symtable.SymList[i]);
  603. if (sym.typ=fieldvarsym) and
  604. not(sp_static in sym.symoptions) and
  605. (sym.visibility=vis_published) then
  606. begin
  607. { skip non-object defs for legacy rtti }
  608. if tfieldvarsym(sym).vardef.typ<>objectdef then
  609. continue;
  610. {
  611. TFieldInfo =
  612. $ifndef FPC_REQUIRES_PROPER_ALIGNMENT
  613. packed
  614. $endif FPC_REQUIRES_PROPER_ALIGNMENT
  615. record
  616. FieldOffset: SizeUInt;
  617. ClassTypeIndex: Word;
  618. Name: ShortString;
  619. end;
  620. }
  621. datatcb.begin_anonymous_record('$fpc_intern_fieldinfo_'+tostr(length(tfieldvarsym(sym).realname)),packrecords,1,
  622. targetinfos[target_info.system]^.alignment.recordalignmin);
  623. datatcb.emit_tai(Tai_const.Create_sizeint(tfieldvarsym(sym).fieldoffset),sizeuinttype);
  624. classindex:=classtablelist.IndexOf(tfieldvarsym(sym).vardef);
  625. if classindex=-1 then
  626. internalerror(200611033);
  627. datatcb.emit_tai(Tai_const.Create_16bit(classindex+1),u16inttype);
  628. datatcb.emit_shortstring_const(tfieldvarsym(sym).realname);
  629. datatcb.end_anonymous_record;
  630. end;
  631. end;
  632. end;
  633. { append the extended rtti table }
  634. RTTIWriter.write_extended_field_table(datatcb,_class,packrecords);
  635. fieldtabledef:=datatcb.end_anonymous_record;
  636. tcb.finish_internal_data_builder(datatcb,lab,fieldtabledef,sizeof(pint));
  637. end
  638. else
  639. begin
  640. fieldtabledef:=nil;
  641. lab:=nil;
  642. end;
  643. classtablelist.free;
  644. end;
  645. {**************************************
  646. Interface tables
  647. **************************************}
  648. function CreateWrapperName(_class : tobjectdef;AImplIntf : TImplementedInterface;i : longint;pd : tprocdef) : TSymStr;
  649. var
  650. realintfdef: tobjectdef;
  651. tmpstr : AnsiString;
  652. hs : TSymStr;
  653. crc : DWord;
  654. begin
  655. realintfdef:=AImplIntf.IntfDef;
  656. while realintfdef.is_unique_objpasdef do
  657. realintfdef:=realintfdef.childof;
  658. tmpstr:=_class.objname^+'_$_'+make_mangledname('',realintfdef.owner,'')+'_$$_'+realintfdef.objname^+'_$_'+tostr(i)+'_$_'+pd.mangledname;
  659. if length(tmpstr)>50 then
  660. begin
  661. crc:=0;
  662. crc:=UpdateCrc32(crc,tmpstr[51],length(tmpstr)-50);
  663. hs:=copy(tmpstr,1,50)+'$CRC'+hexstr(crc,8);
  664. end
  665. else
  666. hs:=tmpstr;
  667. result:=make_mangledname('WRPR',_class.owner,hs);
  668. end;
  669. procedure TVMTWriter.intf_create_vtbl(tcb: ttai_typedconstbuilder; AImplIntf: TImplementedInterface; intfindex: longint);
  670. var
  671. datatcb : ttai_typedconstbuilder;
  672. pd : tprocdef;
  673. hs : TSymStr;
  674. i : longint;
  675. begin
  676. tcb.start_internal_data_builder(current_asmdata.AsmLists[al_const],sec_rodata,'',datatcb,fintfvtablelabels[intfindex]);
  677. datatcb.begin_anonymous_record('',0,1,
  678. targetinfos[target_info.system]^.alignment.recordalignmin);
  679. if assigned(AImplIntf.procdefs) then
  680. begin
  681. for i:=0 to AImplIntf.procdefs.count-1 do
  682. begin
  683. pd:=tprocdef(AImplIntf.procdefs[i]);
  684. hs:=CreateWrapperName(_Class,AImplIntf,i,pd);
  685. { create reference }
  686. datatcb.emit_tai(Tai_const.Createname(hs,AT_FUNCTION,0),cprocvardef.getreusableprocaddr(pd,pc_address_only));
  687. end;
  688. end
  689. else
  690. { can't have an empty symbol on LLVM }
  691. datatcb.emit_tai(tai_const.Create_nil_codeptr,voidpointertype);
  692. tcb.finish_internal_data_builder(datatcb,fintfvtablelabels[intfindex],
  693. datatcb.end_anonymous_record,sizeof(pint));
  694. end;
  695. procedure TVMTWriter.intf_gen_intf_ref(tcb: ttai_typedconstbuilder; AImplIntf: TImplementedInterface; intfindex: longint; interfaceentrydef, interfaceentrytypedef: tdef);
  696. var
  697. pd: tprocdef;
  698. siid,
  699. siidstr: tsymstr;
  700. nonuniqueintf: tobjectdef;
  701. begin
  702. nonuniqueintf:=AImplIntf.IntfDef;
  703. while nonuniqueintf.is_unique_objpasdef do
  704. nonuniqueintf:=nonuniqueintf.childof;
  705. tcb.maybe_begin_aggregate(interfaceentrydef);
  706. { GUID (or nil for Corba interfaces) }
  707. tcb.next_field:=tabstractrecorddef(interfaceentrydef).symtable.Find('IIDREF') as tfieldvarsym;
  708. siid:='';
  709. if nonuniqueintf.objecttype in [odt_interfacecom] then
  710. begin
  711. siid:=make_mangledname('IID',nonuniqueintf.owner,nonuniqueintf.objname^);
  712. tcb.emit_tai(Tai_const.Create_sym_offset(
  713. current_asmdata.RefAsmSymbol(siid,AT_DATA,true),0),cpointerdef.getreusable(rec_tguid));
  714. end
  715. else
  716. tcb.emit_tai(Tai_const.Create_nil_dataptr,cpointerdef.getreusable(rec_tguid));
  717. { VTable }
  718. tcb.next_field:=tabstractrecorddef(interfaceentrydef).symtable.Find('VTABLE') as tfieldvarsym;
  719. tcb.queue_init(voidpointertype);
  720. tcb.queue_emit_asmsym(fintfvtablelabels[intfindex],nonuniqueintf);
  721. { IOffset field }
  722. case AImplIntf.VtblImplIntf.IType of
  723. etFieldValue, etFieldValueClass,
  724. etStandard:
  725. begin
  726. tcb.next_field:=tabstractrecorddef(interfaceentrydef).symtable.Find('IOFFSET') as tfieldvarsym;
  727. tcb.emit_tai(Tai_const.Create_sizeint(AImplIntf.VtblImplIntf.IOffset),sizeuinttype);
  728. end;
  729. etStaticMethodResult, etStaticMethodClass:
  730. begin
  731. tcb.next_field:=tabstractrecorddef(interfaceentrydef).symtable.Find('IOFFSETASCODEPTR') as tfieldvarsym;
  732. pd:=tprocdef(tpropertysym(AImplIntf.ImplementsGetter).propaccesslist[palt_read].procdef);
  733. tcb.queue_init(codeptruinttype);
  734. tcb.queue_emit_proc(pd);
  735. end;
  736. etVirtualMethodResult, etVirtualMethodClass:
  737. begin
  738. tcb.next_field:=tabstractrecorddef(interfaceentrydef).symtable.Find('IOFFSET') as tfieldvarsym;
  739. pd:=tprocdef(tpropertysym(AImplIntf.ImplementsGetter).propaccesslist[palt_read].procdef);
  740. tcb.emit_tai(Tai_const.Create_sizeint(tobjectdef(pd.struct).vmtmethodoffset(pd.extnumber)),sizeuinttype);
  741. end;
  742. end;
  743. { IIDStr }
  744. tcb.next_field:=tabstractrecorddef(interfaceentrydef).symtable.Find('IIDSTRREF') as tfieldvarsym;
  745. siidstr:=make_mangledname('IIDSTR',nonuniqueintf.owner,nonuniqueintf.objname^);
  746. tcb.queue_init(cpointerdef.getreusable(cshortstringtype));
  747. tcb.queue_emit_asmsym(
  748. current_asmdata.RefAsmSymbol(
  749. siidstr,
  750. AT_DATA,
  751. true),
  752. cpointerdef.getreusable(carraydef.getreusable(cansichartype,length(nonuniqueintf.iidstr^)+1)));
  753. { IType }
  754. tcb.next_field:=tabstractrecorddef(interfaceentrydef).symtable.Find('ITYPE') as tfieldvarsym;
  755. tcb.emit_ord_const(aint(AImplIntf.VtblImplIntf.IType),interfaceentrytypedef);
  756. tcb.maybe_end_aggregate(interfaceentrydef);
  757. if findunitsymtable(nonuniqueintf.owner).moduleid<>findunitsymtable(_Class.owner).moduleid then
  758. begin
  759. if siid<>'' then
  760. current_module.add_extern_asmsym(siid,AB_EXTERNAL,AT_DATA);
  761. current_module.add_extern_asmsym(siidstr,AB_EXTERNAL,AT_DATA);
  762. end;
  763. end;
  764. procedure TVMTWriter.intf_write_table(tcb: ttai_typedconstbuilder; out lab: TAsmLabel; out intftabledef: trecorddef);
  765. var
  766. i : longint;
  767. ImplIntf : TImplementedInterface;
  768. datatcb : ttai_typedconstbuilder;
  769. interfaceentrydef : tdef;
  770. interfaceentrytypedef: tdef;
  771. interfacearray: tdef;
  772. begin
  773. setlength(fintfvtablelabels,_class.ImplementedInterfaces.count);
  774. { Write unique vtbls }
  775. for i:=0 to _class.ImplementedInterfaces.count-1 do
  776. begin
  777. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  778. if ImplIntf.VtblImplIntf=ImplIntf then
  779. intf_create_vtbl(tcb,ImplIntf,i)
  780. end;
  781. { Set labels for aliased vtbls (after all unique vtbls have been
  782. written, so all labels have been defined already) }
  783. for i:=0 to _class.ImplementedInterfaces.count-1 do
  784. begin
  785. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  786. if ImplIntf.VtblImplIntf<>ImplIntf then
  787. fintfvtablelabels[i]:=fintfvtablelabels[_class.ImplementedInterfaces.IndexOf(ImplIntf.VtblImplIntf)];
  788. end;
  789. tcb.start_internal_data_builder(current_asmdata.AsmLists[al_const],sec_rodata,_class.vmt_mangledname,datatcb,lab);
  790. datatcb.begin_anonymous_record('',default_settings.packrecords,1,
  791. targetinfos[target_info.system]^.alignment.recordalignmin);
  792. datatcb.emit_tai(Tai_const.Create_sizeint(_class.ImplementedInterfaces.count),sizeuinttype);
  793. interfaceentrydef:=search_system_type('TINTERFACEENTRY').typedef;
  794. interfaceentrytypedef:=search_system_type('TINTERFACEENTRYTYPE').typedef;
  795. if _class.ImplementedInterfaces.count>0 then
  796. begin
  797. interfacearray:=carraydef.getreusable(interfaceentrydef,_class.ImplementedInterfaces.count);
  798. datatcb.maybe_begin_aggregate(interfacearray);
  799. { Write vtbl references }
  800. for i:=0 to _class.ImplementedInterfaces.count-1 do
  801. begin
  802. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  803. intf_gen_intf_ref(datatcb,ImplIntf,i,interfaceentrydef,interfaceentrytypedef);
  804. end;
  805. datatcb.maybe_end_aggregate(interfacearray);
  806. end;
  807. intftabledef:=datatcb.end_anonymous_record;
  808. tcb.finish_internal_data_builder(datatcb,lab,intftabledef,intftabledef.alignment);
  809. end;
  810. { Write interface identifiers to the data section }
  811. procedure TVMTWriter.writeinterfaceids(list: TAsmList);
  812. var
  813. s : string;
  814. tcb : ttai_typedconstbuilder;
  815. def : tdef;
  816. sym : tasmsymbol;
  817. begin
  818. if assigned(_class.iidguid) then
  819. begin
  820. s:=make_mangledname('IID',_class.owner,_class.objname^);
  821. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable,tcalo_data_force_indirect]);
  822. tcb.emit_guid_const(_class.iidguid^);
  823. sym:=current_asmdata.DefineAsmSymbol(s,AB_GLOBAL,AT_DATA_NOINDIRECT,rec_tguid);
  824. list.concatlist(tcb.get_final_asmlist(
  825. sym,
  826. rec_tguid,
  827. sec_rodata,
  828. s,
  829. sizeof(pint)));
  830. tcb.free;
  831. current_module.add_public_asmsym(sym);
  832. end;
  833. s:=make_mangledname('IIDSTR',_class.owner,_class.objname^);
  834. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable,tcalo_data_force_indirect]);
  835. def:=tcb.emit_shortstring_const(_class.iidstr^);
  836. sym:=current_asmdata.DefineAsmSymbol(s,AB_GLOBAL,AT_DATA_NOINDIRECT,def);
  837. list.concatlist(tcb.get_final_asmlist(
  838. sym,
  839. def,
  840. sec_rodata,
  841. s,
  842. sizeof(pint)));
  843. tcb.free;
  844. current_module.add_public_asmsym(sym);
  845. end;
  846. class function TVMTWriter.use_vmt_writer: boolean;
  847. begin
  848. result:=true;
  849. end;
  850. function TVMTWriter.RedirectToEmpty(procdef : tprocdef) : boolean;
  851. var
  852. i : longint;
  853. hp : PCGParaLocation;
  854. begin
  855. result:=false;
  856. if procdef.isempty then
  857. begin
  858. {$ifdef x86}
  859. paramanager.create_funcretloc_info(procdef,calleeside);
  860. if (procdef.funcretloc[calleeside].Location^.loc=LOC_FPUREGISTER) then
  861. exit;
  862. {$endif x86}
  863. procdef.init_paraloc_info(callerside);
  864. { we can redirect the call if no memory parameter is passed }
  865. for i:=0 to procdef.paras.count-1 do
  866. begin
  867. hp:=tparavarsym(procdef.paras[i]).paraloc[callerside].Location;
  868. while assigned(hp) do
  869. begin
  870. if not(hp^.Loc in [LOC_REGISTER,LOC_MMREGISTER,LOC_FPUREGISTER]) then
  871. exit;
  872. hp:=hp^.Next;
  873. end;
  874. end;
  875. result:=true;
  876. end;
  877. end;
  878. procedure TVMTWriter.generate_abstract_stub(list:TAsmList;pd:tprocdef);
  879. begin
  880. { Generate stubs for abstract methods, so their symbols are present and
  881. can be used e.g. to take address (see issue #24536). }
  882. if (po_global in pd.procoptions) and
  883. (pd.owner.defowner<>self._class) then
  884. exit;
  885. pd.synthetickind:=tsk_call_no_parameters;
  886. pd.skpara:=search_system_proc('ABSTRACTERROR');
  887. { abstract methods are not marked as forwarddef, because otherwise
  888. the compiler would accept implementations for them (and complain if
  889. they were missing); we are now after parsing the user code, so we can
  890. mark them again as forwarddef to indicate they need to be parsed }
  891. pd.forwarddef:=true;
  892. end;
  893. procedure TVMTWriter.writevirtualmethods(tcb: ttai_typedconstbuilder);
  894. var
  895. vmtpd : tprocdef;
  896. vmtentry : pvmtentry;
  897. i : longint;
  898. procname : TSymStr;
  899. {$ifdef vtentry}
  900. hs : string;
  901. {$endif vtentry}
  902. begin
  903. for i:=0 to _class.VMTEntries.Count-1 do
  904. begin
  905. vmtentry:=pvmtentry(_class.vmtentries[i]);
  906. vmtpd:=vmtentry^.procdef;
  907. { safety checks }
  908. if not(po_virtualmethod in vmtpd.procoptions) then
  909. internalerror(200611082);
  910. if vmtpd.extnumber<>i then
  911. internalerror(200611083);
  912. if (po_abstractmethod in vmtpd.procoptions) then
  913. begin
  914. procname:='FPC_ABSTRACTERROR';
  915. generate_abstract_stub(current_asmdata.AsmLists[al_procedures],vmtpd);
  916. end
  917. else if (cs_opt_remove_empty_proc in current_settings.optimizerswitches) and RedirectToEmpty(vmtpd) then
  918. begin
  919. procname:='FPC_EMPTYMETHOD';
  920. if current_module.globalsymtable<>systemunit then
  921. current_module.add_extern_asmsym(procname,AB_EXTERNAL,AT_FUNCTION);
  922. end
  923. else if not wpoinfomanager.optimized_name_for_vmt(_class,vmtpd,procname) then
  924. begin
  925. procname:=vmtpd.mangledname;
  926. if current_module.moduleid<>vmtpd.owner.moduleid then
  927. current_module.addimportedsym(vmtpd.procsym);
  928. end;
  929. tcb.emit_tai(Tai_const.Createname(procname,AT_FUNCTION,0),cprocvardef.getreusableprocaddr(vmtpd,pc_address_only));
  930. {$ifdef vtentry}
  931. hs:='VTENTRY'+'_'+_class.vmt_mangledname+'$$'+tostr(_class.vmtmethodoffset(i) div sizeof(pint));
  932. current_asmdata.asmlists[al_globals].concat(tai_symbol.CreateName(hs,AT_DATA,0,voidpointerdef));
  933. {$endif vtentry}
  934. end;
  935. end;
  936. procedure TVMTWriter.writevmt;
  937. var
  938. methodnametable,intmessagetable,
  939. strmessagetable,classnamelabel,
  940. fieldtablelabel : tasmlabel;
  941. {$ifdef vtentry}
  942. hs: string;
  943. {$endif vtentry}
  944. {$ifdef WITHDMT}
  945. dmtlabel : tasmlabel;
  946. {$endif WITHDMT}
  947. interfacetable : tasmlabel;
  948. tcb, datatcb: ttai_typedconstbuilder;
  949. classnamedef: tdef;
  950. methodnametabledef,
  951. fieldtabledef,
  952. interfacetabledef,
  953. strmessagetabledef,
  954. intmessagetabledef: trecorddef;
  955. parentvmtdef: tdef;
  956. pinterfacetabledef,
  957. pstringmessagetabledef: tdef;
  958. vmttypesym: ttypesym;
  959. vmtdef: tdef;
  960. sym : TAsmSymbol;
  961. begin
  962. {$ifdef WITHDMT}
  963. dmtlabel:=gendmt;
  964. {$endif WITHDMT}
  965. { this code gets executed after the current module's symtable has
  966. already been removed from the symtablestack -> add it again, so that
  967. newly created defs here end up in the right unit }
  968. symtablestack.push(current_module.localsymtable);
  969. strmessagetable:=nil;
  970. interfacetable:=nil;
  971. fieldtablelabel:=nil;
  972. methodnametable:=nil;
  973. intmessagetable:=nil;
  974. classnamelabel:=nil;
  975. classnamedef:=nil;
  976. methodnametabledef:=nil;
  977. fieldtabledef:=nil;
  978. interfacetabledef:=nil;
  979. strmessagetabledef:=nil;
  980. intmessagetabledef:=nil;
  981. { generate VMT }
  982. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable,tcalo_data_force_indirect]);
  983. { write tables for classes, this must be done before the actual
  984. class is written, because we need the labels defined }
  985. if is_class(_class) then
  986. begin
  987. { write class name }
  988. tcb.start_internal_data_builder(current_asmdata.asmlists[al_const],sec_rodata,_class.vmt_mangledname,datatcb,classnamelabel);
  989. classnamedef:=datatcb.emit_shortstring_const(_class.RttiName);
  990. tcb.finish_internal_data_builder(datatcb,classnamelabel,classnamedef,sizeof(pint));
  991. { interface table }
  992. if _class.implements_any_interfaces then
  993. intf_write_table(tcb,interfacetable,interfacetabledef);
  994. genpublishedmethodstable(tcb,methodnametable,methodnametabledef);
  995. generate_field_table(tcb,fieldtablelabel,fieldtabledef);
  996. { generate message and dynamic tables }
  997. if (oo_has_msgstr in _class.objectoptions) then
  998. genstrmsgtab(tcb,strmessagetable,strmessagetabledef);
  999. if (oo_has_msgint in _class.objectoptions) then
  1000. genintmsgtab(tcb,intmessagetable,intmessagetabledef);
  1001. end;
  1002. { reuse the type created in nobj, so we get internal consistency
  1003. checking for free }
  1004. vmttypesym:=ttypesym(_class.symtable.find('vmtdef'));
  1005. if not assigned(vmttypesym) or
  1006. (vmttypesym.typ<>typesym) or
  1007. (vmttypesym.typedef.typ<>recorddef) then
  1008. internalerror(2015071403);
  1009. vmtdef:=trecorddef(vmttypesym.typedef);
  1010. tcb.maybe_begin_aggregate(vmtdef);
  1011. { determine the size with symtable.datasize, because }
  1012. { size gives back 4 for classes }
  1013. tcb.emit_ord_const(tObjectSymtable(_class.symtable).datasize,sizesinttype);
  1014. tcb.emit_ord_const(-int64(tObjectSymtable(_class.symtable).datasize),sizesinttype);
  1015. {$ifdef WITHDMT}
  1016. if _class.classtype=ct_object then
  1017. begin
  1018. if assigned(dmtlabel) then
  1019. tcb.emit_tai(dmtlabel,voidpointertype)
  1020. else
  1021. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1022. end;
  1023. {$endif WITHDMT}
  1024. { write pointer to parent VMT, this isn't implemented in TP }
  1025. { but this is not used in FPC ? (PM) }
  1026. { it's not used yet, but the delphi-operators as and is need it (FK) }
  1027. { it is not written for parents that don't have any vmt !! }
  1028. if is_class(_class) then
  1029. parentvmtdef:=cpointerdef.getreusable(search_system_type('TVMT').typedef)
  1030. else
  1031. parentvmtdef:=voidpointertype;
  1032. if assigned(_class.childof) and
  1033. (oo_has_vmt in _class.childof.objectoptions) then
  1034. begin
  1035. tcb.queue_init(parentvmtdef);
  1036. sym:=current_asmdata.RefAsmSymbol(_class.childof.vmt_mangledname,AT_DATA,true);
  1037. tcb.queue_emit_asmsym(
  1038. sym,
  1039. tfieldvarsym(_class.childof.vmt_field).vardef);
  1040. if _class.childof.owner.moduleid<>current_module.moduleid then
  1041. current_module.add_extern_asmsym(sym);
  1042. end
  1043. else
  1044. tcb.emit_tai(Tai_const.Create_nil_dataptr,parentvmtdef);
  1045. { write extended info for classes, for the order see rtl/inc/objpash.inc }
  1046. if is_class(_class) then
  1047. begin
  1048. { pointer to class name string }
  1049. tcb.queue_init(cpointerdef.getreusable(cshortstringtype));
  1050. tcb.queue_emit_asmsym(classnamelabel,classnamedef);
  1051. { pointer to dynamic table or nil }
  1052. if (oo_has_msgint in _class.objectoptions) then
  1053. begin
  1054. tcb.queue_init(voidpointertype);
  1055. tcb.queue_emit_asmsym(intmessagetable,cpointerdef.getreusable(intmessagetabledef));
  1056. end
  1057. else
  1058. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1059. { pointer to method table or nil }
  1060. if assigned(methodnametable) then
  1061. begin
  1062. tcb.queue_init(voidpointertype);
  1063. tcb.queue_emit_asmsym(methodnametable,cpointerdef.getreusable(methodnametabledef))
  1064. end
  1065. else
  1066. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1067. { pointer to field table }
  1068. if assigned(fieldtablelabel) then
  1069. begin
  1070. tcb.queue_init(voidpointertype);
  1071. tcb.queue_emit_asmsym(fieldtablelabel,fieldtabledef)
  1072. end
  1073. else
  1074. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1075. { pointer to type info of published section }
  1076. tcb.emit_tai(Tai_const.Create_sym(RTTIWriter.get_rtti_label(_class,fullrtti,false)),voidpointertype);
  1077. { inittable for con-/destruction }
  1078. if _class.members_need_inittable then
  1079. tcb.emit_tai(Tai_const.Create_sym(RTTIWriter.get_rtti_label(_class,initrtti,false)),voidpointertype)
  1080. else
  1081. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1082. { auto table }
  1083. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1084. { interface table }
  1085. pinterfacetabledef:=search_system_type('PINTERFACETABLE').typedef;
  1086. if _class.implements_any_interfaces then
  1087. begin
  1088. tcb.queue_init(pinterfacetabledef);
  1089. tcb.queue_emit_asmsym(interfacetable,interfacetabledef)
  1090. end
  1091. else
  1092. tcb.emit_tai(Tai_const.Create_nil_dataptr,pinterfacetabledef);
  1093. { table for string messages }
  1094. pstringmessagetabledef:=search_system_type('PSTRINGMESSAGETABLE').typedef;
  1095. if (oo_has_msgstr in _class.objectoptions) then
  1096. begin
  1097. tcb.queue_init(voidpointertype);
  1098. tcb.queue_emit_asmsym(strmessagetable,pstringmessagetabledef);
  1099. end
  1100. else
  1101. tcb.emit_tai(Tai_const.Create_nil_dataptr,pstringmessagetabledef);
  1102. end;
  1103. { write virtual methods }
  1104. writevirtualmethods(tcb);
  1105. tcb.emit_tai(Tai_const.Create_nil_codeptr,voidcodepointertype);
  1106. tcb.maybe_end_aggregate(vmtdef);
  1107. sym:=current_asmdata.DefineAsmSymbol(_class.vmt_mangledname,AB_GLOBAL,AT_DATA_NOINDIRECT,vmtdef);
  1108. current_module.add_public_asmsym(sym);
  1109. { concatenate the VMT to the asmlist }
  1110. current_asmdata.asmlists[al_globals].concatlist(
  1111. tcb.get_final_asmlist(
  1112. sym,
  1113. vmtdef,sec_rodata,_class.vmt_mangledname,sizeof(pint)
  1114. )
  1115. );
  1116. tcb.free;
  1117. {$ifdef vtentry}
  1118. { write vtinherit symbol to notify the linker of the class inheritance tree }
  1119. hs:='VTINHERIT'+'_'+_class.vmt_mangledname+'$$';
  1120. if assigned(_class.childof) then
  1121. hs:=hs+_class.childof.vmt_mangledname
  1122. else
  1123. hs:=hs+_class.vmt_mangledname;
  1124. current_asmdata.asmlists[al_globals].concat(tai_symbol.CreateName(hs,AT_DATA,0,voidpointerdef));
  1125. {$endif vtentry}
  1126. symtablestack.pop(current_module.localsymtable);
  1127. end;
  1128. procedure gen_intf_wrapper(list:TAsmList;_class:tobjectdef);
  1129. var
  1130. i,j : longint;
  1131. tmps : string;
  1132. pd : TProcdef;
  1133. ImplIntf : TImplementedInterface;
  1134. wrapperpd: tprocdef;
  1135. wrapperinfo: pskpara_interface_wrapper;
  1136. tmplist: tasmlist;
  1137. oldfileposinfo: tfileposinfo;
  1138. usehighlevelwrapper: Boolean;
  1139. begin
  1140. {$if defined(cpuhighleveltarget)}
  1141. usehighlevelwrapper:=true;
  1142. {$else defined(cpuhighleveltarget)}
  1143. { on PPC systems that use a TOC the linker needs to be able to insert
  1144. an instruction to restore the TOC register after every branch
  1145. between code fragments that use a different TOC (which has to be
  1146. executed when that "branch" returns). So we can't use tail call
  1147. branches to routines potentially using a different TOC there }
  1148. if target_info.abi in abis_ppc_toc then
  1149. usehighlevelwrapper:=true
  1150. else
  1151. usehighlevelwrapper:=false;
  1152. {$endif defined(cpuhighleveltarget)}
  1153. for i:=0 to _class.ImplementedInterfaces.count-1 do
  1154. begin
  1155. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  1156. if (ImplIntf=ImplIntf.VtblImplIntf) and
  1157. assigned(ImplIntf.ProcDefs) then
  1158. begin
  1159. for j:=0 to ImplIntf.ProcDefs.Count-1 do
  1160. begin
  1161. pd:=TProcdef(ImplIntf.ProcDefs[j]);
  1162. { we don't track method calls via interfaces yet ->
  1163. assume that every method called via an interface call
  1164. is reachable for now }
  1165. if (po_virtualmethod in pd.procoptions) and
  1166. not is_objectpascal_helper(tprocdef(pd).struct) then
  1167. tobjectdef(tprocdef(pd).struct).register_vmt_call(tprocdef(pd).extnumber);
  1168. tmps:=CreateWrapperName(_Class,ImplIntf,j,pd);
  1169. if usehighlevelwrapper then
  1170. begin
  1171. new(wrapperinfo);
  1172. wrapperinfo^.pd:=pd;
  1173. wrapperinfo^.offset:=ImplIntf.ioffset;
  1174. { insert the wrapper procdef in the current unit's local
  1175. symbol table, but set the owning "struct" to the current
  1176. class (so self will have the correct type) }
  1177. wrapperpd:=create_procdef_alias(pd,tmps,tmps,
  1178. current_module.localsymtable,_class,
  1179. tsk_interface_wrapper,wrapperinfo);
  1180. include(wrapperpd.implprocoptions,pio_thunk);
  1181. end
  1182. else
  1183. begin
  1184. oldfileposinfo:=current_filepos;
  1185. if pd.owner.iscurrentunit then
  1186. current_filepos:=pd.fileinfo
  1187. else
  1188. begin
  1189. current_filepos.moduleindex:=current_module.unit_index;
  1190. current_filepos.fileindex:=1;
  1191. current_filepos.line:=1;
  1192. current_filepos.column:=1;
  1193. end;
  1194. { create wrapper code }
  1195. tmplist:=tasmlist.create;
  1196. new_section(tmplist,sec_code,tmps,target_info.alignment.procalign);
  1197. tmplist.Concat(tai_function_name.create(tmps));
  1198. hlcg.init_register_allocators;
  1199. hlcg.g_intf_wrapper(tmplist,pd,tmps,ImplIntf.ioffset);
  1200. hlcg.done_register_allocators;
  1201. if ((cs_debuginfo in current_settings.moduleswitches) or
  1202. (cs_use_lineinfo in current_settings.globalswitches)) and
  1203. (target_dbg.id<>dbg_stabx) then
  1204. current_debuginfo.insertlineinfo(tmplist);
  1205. list.concatlist(tmplist);
  1206. tmplist.Free;
  1207. current_filepos:=oldfileposinfo;
  1208. end;
  1209. end;
  1210. end;
  1211. end;
  1212. end;
  1213. procedure do_write_vmts(st:tsymtable;is_global:boolean);
  1214. var
  1215. i : longint;
  1216. def : tdef;
  1217. vmtwriter : TVMTWriter;
  1218. begin
  1219. if not CVMTWriter.use_vmt_writer then
  1220. exit;
  1221. for i:=0 to st.DefList.Count-1 do
  1222. begin
  1223. def:=tdef(st.DefList[i]);
  1224. case def.typ of
  1225. recorddef :
  1226. do_write_vmts(trecorddef(def).symtable,is_global);
  1227. objectdef :
  1228. begin
  1229. { Skip generics and forward defs }
  1230. if ([df_generic,df_genconstraint]*def.defoptions<>[]) or
  1231. (oo_is_forward in tobjectdef(def).objectoptions) then
  1232. continue;
  1233. if tobjectdef(def).is_unique_objpasdef then
  1234. continue;
  1235. do_write_vmts(tobjectdef(def).symtable,is_global);
  1236. { Write also VMT if not done yet }
  1237. if not(ds_vmt_written in def.defstates) then
  1238. begin
  1239. vmtwriter:=CVMTWriter.create(tobjectdef(def));
  1240. if is_interface(tobjectdef(def)) then
  1241. vmtwriter.writeinterfaceids(current_asmdata.AsmLists[al_globals]);
  1242. if (oo_has_vmt in tobjectdef(def).objectoptions) then
  1243. vmtwriter.writevmt;
  1244. vmtwriter.free;
  1245. include(def.defstates,ds_vmt_written);
  1246. end;
  1247. if is_class(def) then
  1248. gen_intf_wrapper(current_asmdata.asmlists[al_procedures],tobjectdef(def));
  1249. end;
  1250. procdef :
  1251. begin
  1252. if assigned(tprocdef(def).localst) and
  1253. (tprocdef(def).localst.symtabletype=localsymtable) then
  1254. do_write_vmts(tprocdef(def).localst,false);
  1255. if assigned(tprocdef(def).parast) then
  1256. do_write_vmts(tprocdef(def).parast,false);
  1257. end;
  1258. else
  1259. ;
  1260. end;
  1261. end;
  1262. end;
  1263. procedure write_vmts(st:tsymtable;is_global:boolean);
  1264. begin
  1265. {$ifndef cpuhighleveltarget}
  1266. create_hlcodegen;
  1267. {$endif}
  1268. do_write_vmts(st,is_global);
  1269. {$ifndef cpuhighleveltarget}
  1270. destroy_hlcodegen;
  1271. {$endif}
  1272. end;
  1273. end.