ncgvmt.pas 51 KB

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