ncgvmt.pas 49 KB

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