ncgvmt.pas 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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. symbase;
  22. { generate persistent type information like VMT, RTTI and inittables }
  23. procedure write_persistent_type_info(st:tsymtable;is_global:boolean);
  24. implementation
  25. uses
  26. cutils,cclasses,
  27. globtype,globals,verbose,constexp,
  28. systems,
  29. symconst,symtype,symdef,symsym,symtable,defutil,
  30. aasmbase,aasmtai,aasmdata,
  31. wpobase,
  32. nobj,
  33. cgbase,parabase,paramgr,cgobj,cgcpu,hlcgobj,hlcgcpu,
  34. ncgrtti;
  35. type
  36. pprocdeftree = ^tprocdeftree;
  37. tprocdeftree = record
  38. data : tprocdef;
  39. nl : tasmlabel;
  40. l,r : pprocdeftree;
  41. end;
  42. TVMTWriter=class
  43. private
  44. _Class : tobjectdef;
  45. { message tables }
  46. root : pprocdeftree;
  47. procedure disposeprocdeftree(p : pprocdeftree);
  48. procedure insertmsgint(p:TObject;arg:pointer);
  49. procedure insertmsgstr(p:TObject;arg:pointer);
  50. procedure insertint(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  51. procedure insertstr(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  52. function RedirectToEmpty(procdef: tprocdef): boolean;
  53. procedure writenames(list : TAsmList;p : pprocdeftree);
  54. procedure writeintentry(list : TAsmList;p : pprocdeftree);
  55. procedure writestrentry(list : TAsmList;p : pprocdeftree);
  56. {$ifdef WITHDMT}
  57. { dmt }
  58. procedure insertdmtentry(p:TObject;arg:pointer);
  59. procedure writedmtindexentry(p : pprocdeftree);
  60. procedure writedmtaddressentry(p : pprocdeftree);
  61. {$endif}
  62. { published methods }
  63. procedure do_count_published_methods(p:TObject;arg:pointer);
  64. procedure do_gen_published_methods(p:TObject;arg:pointer);
  65. { virtual methods }
  66. procedure writevirtualmethods(List:TAsmList);
  67. { interface tables }
  68. function intf_get_vtbl_name(AImplIntf:TImplementedInterface): string;
  69. procedure intf_create_vtbl(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  70. procedure intf_gen_intf_ref(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  71. function intf_write_table(list : TAsmList):TAsmLabel;
  72. { generates the message tables for a class }
  73. function genstrmsgtab(list : TAsmList) : tasmlabel;
  74. function genintmsgtab(list : TAsmList) : tasmlabel;
  75. function genpublishedmethodstable(list : TAsmList) : tasmlabel;
  76. function generate_field_table(list : TAsmList) : tasmlabel;
  77. procedure generate_abstract_stub(list:TAsmList;pd:tprocdef);
  78. {$ifdef WITHDMT}
  79. { generates a DMT for _class }
  80. function gendmt : tasmlabel;
  81. {$endif WITHDMT}
  82. public
  83. constructor create(c:tobjectdef);
  84. { write the VMT to al_globals }
  85. procedure writevmt;
  86. procedure writeinterfaceids(list: TAsmList);
  87. end;
  88. {*****************************************************************************
  89. TVMTWriter
  90. *****************************************************************************}
  91. constructor TVMTWriter.create(c:tobjectdef);
  92. begin
  93. inherited Create;
  94. _Class:=c;
  95. end;
  96. {**************************************
  97. Message Tables
  98. **************************************}
  99. procedure TVMTWriter.disposeprocdeftree(p : pprocdeftree);
  100. begin
  101. if assigned(p^.l) then
  102. disposeprocdeftree(p^.l);
  103. if assigned(p^.r) then
  104. disposeprocdeftree(p^.r);
  105. dispose(p);
  106. end;
  107. procedure TVMTWriter.insertint(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  108. begin
  109. if at=nil then
  110. begin
  111. at:=p;
  112. inc(count);
  113. end
  114. else
  115. begin
  116. if p^.data.messageinf.i<at^.data.messageinf.i then
  117. insertint(p,at^.l,count)
  118. else if p^.data.messageinf.i>at^.data.messageinf.i then
  119. insertint(p,at^.r,count)
  120. else
  121. Message1(parser_e_duplicate_message_label,tostr(p^.data.messageinf.i));
  122. end;
  123. end;
  124. procedure TVMTWriter.insertstr(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  125. var
  126. i : integer;
  127. begin
  128. if at=nil then
  129. begin
  130. at:=p;
  131. inc(count);
  132. end
  133. else
  134. begin
  135. i:=CompareStr(p^.data.messageinf.str^,at^.data.messageinf.str^);
  136. if i<0 then
  137. insertstr(p,at^.l,count)
  138. else if i>0 then
  139. insertstr(p,at^.r,count)
  140. else
  141. Message1(parser_e_duplicate_message_label,p^.data.messageinf.str^);
  142. end;
  143. end;
  144. procedure TVMTWriter.insertmsgint(p:TObject;arg:pointer);
  145. var
  146. i : longint;
  147. pd : Tprocdef;
  148. pt : pprocdeftree;
  149. begin
  150. if tsym(p).typ<>procsym then
  151. exit;
  152. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  153. begin
  154. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  155. if po_msgint in pd.procoptions then
  156. begin
  157. new(pt);
  158. pt^.data:=pd;
  159. pt^.l:=nil;
  160. pt^.r:=nil;
  161. insertint(pt,root,plongint(arg)^);
  162. end;
  163. end;
  164. end;
  165. procedure TVMTWriter.insertmsgstr(p:TObject;arg:pointer);
  166. var
  167. i : longint;
  168. pd : Tprocdef;
  169. pt : pprocdeftree;
  170. begin
  171. if tsym(p).typ<>procsym then
  172. exit;
  173. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  174. begin
  175. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  176. if po_msgstr in pd.procoptions then
  177. begin
  178. new(pt);
  179. pt^.data:=pd;
  180. pt^.l:=nil;
  181. pt^.r:=nil;
  182. insertstr(pt,root,plongint(arg)^);
  183. end;
  184. end;
  185. end;
  186. procedure TVMTWriter.writenames(list : TAsmList;p : pprocdeftree);
  187. var
  188. ca : pchar;
  189. len : byte;
  190. begin
  191. current_asmdata.getdatalabel(p^.nl);
  192. if assigned(p^.l) then
  193. writenames(list,p^.l);
  194. list.concat(cai_align.create(const_align(sizeof(pint))));
  195. list.concat(Tai_label.Create(p^.nl));
  196. len:=length(p^.data.messageinf.str^);
  197. list.concat(tai_const.create_8bit(len));
  198. getmem(ca,len+1);
  199. move(p^.data.messageinf.str^[1],ca^,len);
  200. ca[len]:=#0;
  201. list.concat(Tai_string.Create_pchar(ca,len));
  202. if assigned(p^.r) then
  203. writenames(list,p^.r);
  204. end;
  205. procedure TVMTWriter.writestrentry(list : TAsmList;p : pprocdeftree);
  206. begin
  207. if assigned(p^.l) then
  208. writestrentry(list,p^.l);
  209. { write name label }
  210. list.concat(cai_align.create(const_align(sizeof(pint))));
  211. list.concat(Tai_const.Create_sym(p^.nl));
  212. list.concat(cai_align.create(const_align(sizeof(pint))));
  213. list.concat(Tai_const.Createname(p^.data.mangledname,0));
  214. if assigned(p^.r) then
  215. writestrentry(list,p^.r);
  216. end;
  217. function TVMTWriter.genstrmsgtab(list : TAsmList) : tasmlabel;
  218. var
  219. count : longint;
  220. begin
  221. root:=nil;
  222. count:=0;
  223. { insert all message handlers into a tree, sorted by name }
  224. _class.symtable.SymList.ForEachCall(@insertmsgstr,@count);
  225. { write all names }
  226. if assigned(root) then
  227. writenames(list,root);
  228. { now start writing of the message string table }
  229. current_asmdata.getlabel(result,alt_data);
  230. list.concat(cai_align.create(const_align(sizeof(pint))));
  231. list.concat(Tai_label.Create(result));
  232. list.concat(cai_align.create(const_align(sizeof(longint))));
  233. list.concat(Tai_const.Create_32bit(count));
  234. list.concat(cai_align.create(const_align(sizeof(pint))));
  235. if assigned(root) then
  236. begin
  237. writestrentry(list,root);
  238. disposeprocdeftree(root);
  239. end;
  240. end;
  241. procedure TVMTWriter.writeintentry(list : TAsmList;p : pprocdeftree);
  242. begin
  243. if assigned(p^.l) then
  244. writeintentry(list,p^.l);
  245. { write name label }
  246. list.concat(cai_align.create(const_align(sizeof(longint))));
  247. list.concat(Tai_const.Create_32bit(p^.data.messageinf.i));
  248. list.concat(cai_align.create(const_align(sizeof(pint))));
  249. list.concat(Tai_const.Createname(p^.data.mangledname,0));
  250. if assigned(p^.r) then
  251. writeintentry(list,p^.r);
  252. end;
  253. function TVMTWriter.genintmsgtab(list : TAsmList) : tasmlabel;
  254. var
  255. r : tasmlabel;
  256. count : longint;
  257. begin
  258. root:=nil;
  259. count:=0;
  260. { insert all message handlers into a tree, sorted by name }
  261. _class.symtable.SymList.ForEachCall(@insertmsgint,@count);
  262. { now start writing of the message string table }
  263. current_asmdata.getlabel(r,alt_data);
  264. list.concat(cai_align.create(const_align(sizeof(pint))));
  265. list.concat(Tai_label.Create(r));
  266. genintmsgtab:=r;
  267. list.concat(cai_align.create(const_align(sizeof(longint))));
  268. list.concat(Tai_const.Create_32bit(count));
  269. list.concat(cai_align.create(const_align(sizeof(pint))));
  270. if assigned(root) then
  271. begin
  272. writeintentry(list,root);
  273. disposeprocdeftree(root);
  274. end;
  275. end;
  276. {$ifdef WITHDMT}
  277. {**************************************
  278. DMT
  279. **************************************}
  280. procedure TVMTWriter.insertdmtentry(p:TObject;arg:pointer);
  281. var
  282. hp : tprocdef;
  283. pt : pprocdeftree;
  284. begin
  285. if tsym(p).typ=procsym then
  286. begin
  287. hp:=tprocsym(p).definition;
  288. while assigned(hp) do
  289. begin
  290. if (po_msgint in hp.procoptions) then
  291. begin
  292. new(pt);
  293. pt^.p:=hp;
  294. pt^.l:=nil;
  295. pt^.r:=nil;
  296. insertint(pt,root);
  297. end;
  298. hp:=hp.nextoverloaded;
  299. end;
  300. end;
  301. end;
  302. procedure TVMTWriter.writedmtindexentry(p : pprocdeftree);
  303. begin
  304. if assigned(p^.l) then
  305. writedmtindexentry(p^.l);
  306. al_globals.concat(Tai_const.Create_32bit(p^.data.messageinf.i));
  307. if assigned(p^.r) then
  308. writedmtindexentry(p^.r);
  309. end;
  310. procedure TVMTWriter.writedmtaddressentry(p : pprocdeftree);
  311. begin
  312. if assigned(p^.l) then
  313. writedmtaddressentry(p^.l);
  314. al_globals.concat(Tai_const_symbol.Createname(p^.data.mangledname,0));
  315. if assigned(p^.r) then
  316. writedmtaddressentry(p^.r);
  317. end;
  318. function TVMTWriter.gendmt : tasmlabel;
  319. var
  320. r : tasmlabel;
  321. begin
  322. root:=nil;
  323. count:=0;
  324. gendmt:=nil;
  325. { insert all message handlers into a tree, sorted by number }
  326. _class.symtable.SymList.ForEachCall(insertdmtentry);
  327. if count>0 then
  328. begin
  329. current_asmdata.getdatalabel(r);
  330. gendmt:=r;
  331. al_globals.concat(cai_align.create(const_align(sizeof(pint))));
  332. al_globals.concat(Tai_label.Create(r));
  333. { entries for caching }
  334. al_globals.concat(Tai_const.Create_ptr(0));
  335. al_globals.concat(Tai_const.Create_ptr(0));
  336. al_globals.concat(Tai_const.Create_32bit(count));
  337. if assigned(root) then
  338. begin
  339. writedmtindexentry(root);
  340. writedmtaddressentry(root);
  341. disposeprocdeftree(root);
  342. end;
  343. end;
  344. end;
  345. {$endif WITHDMT}
  346. {**************************************
  347. Published Methods
  348. **************************************}
  349. procedure TVMTWriter.do_count_published_methods(p:TObject;arg:pointer);
  350. var
  351. i : longint;
  352. pd : tprocdef;
  353. begin
  354. if (tsym(p).typ<>procsym) then
  355. exit;
  356. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  357. begin
  358. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  359. if (pd.procsym=tsym(p)) and
  360. (pd.visibility=vis_published) then
  361. inc(plongint(arg)^);
  362. end;
  363. end;
  364. procedure TVMTWriter.do_gen_published_methods(p:TObject;arg:pointer);
  365. var
  366. i : longint;
  367. l : tasmlabel;
  368. pd : tprocdef;
  369. lists: ^TAsmList absolute arg;
  370. begin
  371. if (tsym(p).typ<>procsym) then
  372. exit;
  373. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  374. begin
  375. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  376. if (pd.procsym=tsym(p)) and
  377. (pd.visibility=vis_published) then
  378. begin
  379. current_asmdata.getlabel(l,alt_data);
  380. lists[1].concat(cai_align.Create(const_align(sizeof(pint))));
  381. lists[1].concat(Tai_label.Create(l));
  382. lists[1].concat(Tai_const.Create_8bit(length(tsym(p).realname)));
  383. lists[1].concat(Tai_string.Create(tsym(p).realname));
  384. lists[0].concat(Tai_const.Create_sym(l));
  385. if po_abstractmethod in pd.procoptions then
  386. lists[0].concat(Tai_const.Create_nil_codeptr)
  387. else
  388. lists[0].concat(Tai_const.Createname(pd.mangledname,0));
  389. end;
  390. end;
  391. end;
  392. function TVMTWriter.genpublishedmethodstable(list : TAsmList) : tasmlabel;
  393. var
  394. l : tasmlabel;
  395. count : longint;
  396. lists : array[0..1] of TAsmList;
  397. begin
  398. count:=0;
  399. _class.symtable.SymList.ForEachCall(@do_count_published_methods,@count);
  400. if count>0 then
  401. begin
  402. lists[0]:=list;
  403. lists[1]:=TAsmList.Create;
  404. current_asmdata.getlabel(l,alt_data);
  405. list.concat(cai_align.create(const_align(sizeof(pint))));
  406. list.concat(Tai_label.Create(l));
  407. list.concat(Tai_const.Create_32bit(count));
  408. _class.symtable.SymList.ForEachCall(@do_gen_published_methods,@lists);
  409. list.concatlist(lists[1]);
  410. lists[1].Free;
  411. genpublishedmethodstable:=l;
  412. end
  413. else
  414. genpublishedmethodstable:=nil;
  415. end;
  416. function TVMTWriter.generate_field_table(list : TAsmList) : tasmlabel;
  417. var
  418. i : longint;
  419. sym : tsym;
  420. fieldtable,
  421. classtable : tasmlabel;
  422. classindex,
  423. fieldcount : longint;
  424. classtablelist : TFPList;
  425. begin
  426. classtablelist:=TFPList.Create;
  427. { retrieve field info fields }
  428. fieldcount:=0;
  429. for i:=0 to _class.symtable.SymList.Count-1 do
  430. begin
  431. sym:=tsym(_class.symtable.SymList[i]);
  432. if (sym.typ=fieldvarsym) and
  433. (sym.visibility=vis_published) then
  434. begin
  435. if tfieldvarsym(sym).vardef.typ<>objectdef then
  436. internalerror(200611032);
  437. classindex:=classtablelist.IndexOf(tfieldvarsym(sym).vardef);
  438. if classindex=-1 then
  439. classtablelist.Add(tfieldvarsym(sym).vardef);
  440. inc(fieldcount);
  441. end;
  442. end;
  443. if fieldcount>0 then
  444. begin
  445. current_asmdata.getlabel(fieldtable,alt_data);
  446. current_asmdata.getlabel(classtable,alt_data);
  447. list.concat(cai_align.create(const_align(sizeof(pint))));
  448. { write fields }
  449. list.concat(Tai_label.Create(fieldtable));
  450. list.concat(Tai_const.Create_16bit(fieldcount));
  451. if (tf_requires_proper_alignment in target_info.flags) then
  452. list.concat(cai_align.Create(sizeof(TConstPtrUInt)));
  453. list.concat(Tai_const.Create_sym(classtable));
  454. for i:=0 to _class.symtable.SymList.Count-1 do
  455. begin
  456. sym:=tsym(_class.symtable.SymList[i]);
  457. if (sym.typ=fieldvarsym) and
  458. (sym.visibility=vis_published) then
  459. begin
  460. if (tf_requires_proper_alignment in target_info.flags) then
  461. list.concat(cai_align.Create(sizeof(pint)));
  462. list.concat(Tai_const.Create_pint(tfieldvarsym(sym).fieldoffset));
  463. classindex:=classtablelist.IndexOf(tfieldvarsym(sym).vardef);
  464. if classindex=-1 then
  465. internalerror(200611033);
  466. list.concat(Tai_const.Create_16bit(classindex+1));
  467. list.concat(Tai_const.Create_8bit(length(tfieldvarsym(sym).realname)));
  468. list.concat(Tai_string.Create(tfieldvarsym(sym).realname));
  469. end;
  470. end;
  471. { generate the class table }
  472. list.concat(cai_align.create(const_align(sizeof(pint))));
  473. list.concat(Tai_label.Create(classtable));
  474. list.concat(Tai_const.Create_16bit(classtablelist.count));
  475. if (tf_requires_proper_alignment in target_info.flags) then
  476. list.concat(cai_align.Create(sizeof(TConstPtrUInt)));
  477. for i:=0 to classtablelist.Count-1 do
  478. list.concat(Tai_const.Createname(tobjectdef(classtablelist[i]).vmt_mangledname,0));
  479. result:=fieldtable;
  480. end
  481. else
  482. result:=nil;
  483. classtablelist.free;
  484. end;
  485. {**************************************
  486. Interface tables
  487. **************************************}
  488. function TVMTWriter.intf_get_vtbl_name(AImplIntf:TImplementedInterface): string;
  489. begin
  490. result:=make_mangledname('VTBL',_class.owner,_class.objname^+'_$_'+AImplIntf.IntfDef.objname^);
  491. end;
  492. procedure TVMTWriter.intf_create_vtbl(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  493. var
  494. pd : tprocdef;
  495. vtblstr,
  496. hs : string;
  497. i : longint;
  498. begin
  499. vtblstr:=intf_get_vtbl_name(AImplIntf);
  500. rawdata.concat(cai_align.create(const_align(sizeof(pint))));
  501. rawdata.concat(tai_symbol.createname(vtblstr,AT_DATA,0));
  502. if assigned(AImplIntf.procdefs) then
  503. begin
  504. for i:=0 to AImplIntf.procdefs.count-1 do
  505. begin
  506. pd:=tprocdef(AImplIntf.procdefs[i]);
  507. hs:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+AImplIntf.IntfDef.objname^+'_$_'+
  508. tostr(i)+'_$_'+pd.mangledname);
  509. { create reference }
  510. rawdata.concat(Tai_const.Createname(hs,0));
  511. end;
  512. end;
  513. rawdata.concat(tai_symbol_end.createname(vtblstr));
  514. end;
  515. procedure TVMTWriter.intf_gen_intf_ref(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  516. var
  517. pd: tprocdef;
  518. begin
  519. { GUID (or nil for Corba interfaces) }
  520. if AImplIntf.IntfDef.objecttype in [odt_interfacecom] then
  521. rawdata.concat(Tai_const.CreateName(
  522. make_mangledname('IID',AImplIntf.IntfDef.owner,AImplIntf.IntfDef.objname^),0))
  523. else
  524. rawdata.concat(Tai_const.Create_nil_dataptr);
  525. { VTable }
  526. rawdata.concat(Tai_const.Createname(intf_get_vtbl_name(AImplIntf.VtblImplIntf),0));
  527. { IOffset field }
  528. case AImplIntf.VtblImplIntf.IType of
  529. etFieldValue, etFieldValueClass,
  530. etStandard:
  531. rawdata.concat(Tai_const.Create_pint(AImplIntf.VtblImplIntf.IOffset));
  532. etStaticMethodResult, etStaticMethodClass:
  533. rawdata.concat(Tai_const.Createname(
  534. tprocdef(tpropertysym(AImplIntf.ImplementsGetter).propaccesslist[palt_read].procdef).mangledname,
  535. 0
  536. ));
  537. etVirtualMethodResult, etVirtualMethodClass:
  538. begin
  539. pd := tprocdef(tpropertysym(AImplIntf.ImplementsGetter).propaccesslist[palt_read].procdef);
  540. rawdata.concat(Tai_const.Create_pint(tobjectdef(pd.struct).vmtmethodoffset(pd.extnumber)));
  541. end;
  542. else
  543. internalerror(200802162);
  544. end;
  545. { IIDStr }
  546. rawdata.concat(Tai_const.CreateName(
  547. make_mangledname('IIDSTR',AImplIntf.IntfDef.owner,AImplIntf.IntfDef.objname^),0));
  548. { IType }
  549. rawdata.concat(Tai_const.Create_pint(aint(AImplIntf.VtblImplIntf.IType)));
  550. end;
  551. function TVMTWriter.intf_write_table(list : TAsmList):TAsmLabel;
  552. var
  553. i : longint;
  554. ImplIntf : TImplementedInterface;
  555. begin
  556. current_asmdata.getlabel(result,alt_data);
  557. list.concat(cai_align.create(const_align(sizeof(pint))));
  558. list.concat(Tai_label.Create(result));
  559. list.concat(Tai_const.Create_pint(_class.ImplementedInterfaces.count));
  560. { Write vtbl references }
  561. for i:=0 to _class.ImplementedInterfaces.count-1 do
  562. begin
  563. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  564. intf_gen_intf_ref(list,ImplIntf);
  565. end;
  566. { Write vtbls }
  567. for i:=0 to _class.ImplementedInterfaces.count-1 do
  568. begin
  569. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  570. if ImplIntf.VtblImplIntf=ImplIntf then
  571. intf_create_vtbl(list,ImplIntf);
  572. end;
  573. end;
  574. { Write interface identifiers to the data section }
  575. procedure TVMTWriter.writeinterfaceids(list: TAsmList);
  576. var
  577. i : longint;
  578. s : string;
  579. begin
  580. if assigned(_class.iidguid) then
  581. begin
  582. s:=make_mangledname('IID',_class.owner,_class.objname^);
  583. maybe_new_object_file(list);
  584. new_section(list,sec_rodata_norel,s,const_align(sizeof(pint)));
  585. list.concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  586. list.concat(Tai_const.Create_32bit(longint(_class.iidguid^.D1)));
  587. list.concat(Tai_const.Create_16bit(_class.iidguid^.D2));
  588. list.concat(Tai_const.Create_16bit(_class.iidguid^.D3));
  589. for i:=Low(_class.iidguid^.D4) to High(_class.iidguid^.D4) do
  590. list.concat(Tai_const.Create_8bit(_class.iidguid^.D4[i]));
  591. end;
  592. maybe_new_object_file(list);
  593. s:=make_mangledname('IIDSTR',_class.owner,_class.objname^);
  594. new_section(list,sec_rodata_norel,s,sizeof(pint));
  595. list.concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  596. list.concat(Tai_const.Create_8bit(length(_class.iidstr^)));
  597. list.concat(Tai_string.Create(_class.iidstr^));
  598. end;
  599. function TVMTWriter.RedirectToEmpty(procdef : tprocdef) : boolean;
  600. var
  601. i : longint;
  602. hp : PCGParaLocation;
  603. begin
  604. result:=false;
  605. if procdef.isempty then
  606. begin
  607. {$ifdef x86}
  608. paramanager.create_funcretloc_info(procdef,calleeside);
  609. if (procdef.funcretloc[calleeside].Location^.loc=LOC_FPUREGISTER) then
  610. exit;
  611. {$endif x86}
  612. procdef.init_paraloc_info(callerside);
  613. { we can redirect the call if no memory parameter is passed }
  614. for i:=0 to procdef.paras.count-1 do
  615. begin
  616. hp:=tparavarsym(procdef.paras[i]).paraloc[callerside].Location;
  617. while assigned(hp) do
  618. begin
  619. if not(hp^.Loc in [LOC_REGISTER,LOC_MMREGISTER,LOC_FPUREGISTER]) then
  620. exit;
  621. hp:=hp^.Next;
  622. end;
  623. end;
  624. result:=true;
  625. end;
  626. end;
  627. procedure TVMTWriter.generate_abstract_stub(list:TAsmList;pd:tprocdef);
  628. var
  629. sym: TAsmSymbol;
  630. begin
  631. { Generate stubs for abstract methods, so their symbols are present and
  632. can be used e.g. to take address (see issue #24536). }
  633. if (po_global in pd.procoptions) and
  634. (pd.owner.defowner<>self._class) then
  635. exit;
  636. sym:=current_asmdata.GetAsmSymbol(pd.mangledname);
  637. if assigned(sym) and (sym.bind<>AB_EXTERNAL) then
  638. exit;
  639. maybe_new_object_file(list);
  640. new_section(list,sec_code,lower(pd.mangledname),target_info.alignment.procalign);
  641. if (po_global in pd.procoptions) then
  642. begin
  643. sym:=current_asmdata.DefineAsmSymbol(pd.mangledname,AB_GLOBAL,AT_FUNCTION);
  644. list.concat(Tai_symbol.Create_global(sym,0));
  645. end
  646. else
  647. begin
  648. sym:=current_asmdata.DefineAsmSymbol(pd.mangledname,AB_LOCAL,AT_FUNCTION);
  649. list.concat(Tai_symbol.Create(sym,0));
  650. end;
  651. cg.g_external_wrapper(list,pd,'FPC_ABSTRACTERROR');
  652. list.concat(Tai_symbol_end.Create(sym));
  653. end;
  654. procedure TVMTWriter.writevirtualmethods(List:TAsmList);
  655. var
  656. vmtpd : tprocdef;
  657. vmtentry : pvmtentry;
  658. i : longint;
  659. procname : string;
  660. {$ifdef vtentry}
  661. hs : string;
  662. {$endif vtentry}
  663. begin
  664. if not assigned(_class.VMTEntries) then
  665. exit;
  666. for i:=0 to _class.VMTEntries.Count-1 do
  667. begin
  668. vmtentry:=pvmtentry(_class.vmtentries[i]);
  669. vmtpd:=vmtentry^.procdef;
  670. { safety checks }
  671. if not(po_virtualmethod in vmtpd.procoptions) then
  672. internalerror(200611082);
  673. if vmtpd.extnumber<>i then
  674. internalerror(200611083);
  675. if (po_abstractmethod in vmtpd.procoptions) then
  676. begin
  677. procname:='FPC_ABSTRACTERROR';
  678. generate_abstract_stub(current_asmdata.AsmLists[al_procedures],vmtpd);
  679. end
  680. else if (cs_opt_remove_emtpy_proc in current_settings.optimizerswitches) and RedirectToEmpty(vmtpd) then
  681. procname:='FPC_EMPTYMETHOD'
  682. else if not wpoinfomanager.optimized_name_for_vmt(_class,vmtpd,procname) then
  683. procname:=vmtpd.mangledname;
  684. List.concat(Tai_const.createname(procname,0));
  685. {$ifdef vtentry}
  686. hs:='VTENTRY'+'_'+_class.vmt_mangledname+'$$'+tostr(_class.vmtmethodoffset(i) div sizeof(pint));
  687. current_asmdata.asmlists[al_globals].concat(tai_symbol.CreateName(hs,AT_DATA,0));
  688. {$endif vtentry}
  689. end;
  690. end;
  691. procedure TVMTWriter.writevmt;
  692. var
  693. methodnametable,intmessagetable,
  694. strmessagetable,classnamelabel,
  695. fieldtablelabel : tasmlabel;
  696. hs: string;
  697. {$ifdef WITHDMT}
  698. dmtlabel : tasmlabel;
  699. {$endif WITHDMT}
  700. interfacetable : tasmlabel;
  701. templist : TAsmList;
  702. begin
  703. {$ifdef WITHDMT}
  704. dmtlabel:=gendmt;
  705. {$endif WITHDMT}
  706. templist:=TAsmList.Create;
  707. { write tables for classes, this must be done before the actual
  708. class is written, because we need the labels defined }
  709. if is_class(_class) then
  710. begin
  711. { write class name }
  712. current_asmdata.getlabel(classnamelabel,alt_data);
  713. templist.concat(cai_align.create(const_align(sizeof(pint))));
  714. templist.concat(Tai_label.Create(classnamelabel));
  715. hs:=_class.RttiName;
  716. templist.concat(Tai_const.Create_8bit(length(hs)));
  717. templist.concat(Tai_string.Create(hs));
  718. { interface table }
  719. if _class.ImplementedInterfaces.count>0 then
  720. interfacetable:=intf_write_table(templist);
  721. methodnametable:=genpublishedmethodstable(templist);
  722. fieldtablelabel:=generate_field_table(templist);
  723. { generate message and dynamic tables }
  724. if (oo_has_msgstr in _class.objectoptions) then
  725. strmessagetable:=genstrmsgtab(templist);
  726. if (oo_has_msgint in _class.objectoptions) then
  727. intmessagetable:=genintmsgtab(templist);
  728. end;
  729. { write debug info }
  730. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  731. new_section(current_asmdata.asmlists[al_globals],sec_rodata,_class.vmt_mangledname,const_align(sizeof(pint)));
  732. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(_class.vmt_mangledname,AT_DATA,0));
  733. { determine the size with symtable.datasize, because }
  734. { size gives back 4 for classes }
  735. current_asmdata.asmlists[al_globals].concat(Tai_const.Create(aitconst_ptr,tObjectSymtable(_class.symtable).datasize));
  736. current_asmdata.asmlists[al_globals].concat(Tai_const.Create(aitconst_ptr,-int64(tObjectSymtable(_class.symtable).datasize)));
  737. {$ifdef WITHDMT}
  738. if _class.classtype=ct_object then
  739. begin
  740. if assigned(dmtlabel) then
  741. current_asmdata.asmlists[al_globals].concat(Tai_const_symbol.Create(dmtlabel)))
  742. else
  743. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_ptr(0));
  744. end;
  745. {$endif WITHDMT}
  746. { write pointer to parent VMT, this isn't implemented in TP }
  747. { but this is not used in FPC ? (PM) }
  748. { it's not used yet, but the delphi-operators as and is need it (FK) }
  749. { it is not written for parents that don't have any vmt !! }
  750. if assigned(_class.childof) and
  751. (oo_has_vmt in _class.childof.objectoptions) then
  752. current_asmdata.asmlists[al_globals].concat(Tai_const.Createname(_class.childof.vmt_mangledname,0))
  753. else
  754. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_nil_dataptr);
  755. { write extended info for classes, for the order see rtl/inc/objpash.inc }
  756. if is_class(_class) then
  757. begin
  758. { pointer to class name string }
  759. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(classnamelabel));
  760. { pointer to dynamic table or nil }
  761. if (oo_has_msgint in _class.objectoptions) then
  762. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(intmessagetable))
  763. else
  764. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_nil_dataptr);
  765. { pointer to method table or nil }
  766. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(methodnametable));
  767. { pointer to field table }
  768. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(fieldtablelabel));
  769. { pointer to type info of published section }
  770. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(RTTIWriter.get_rtti_label(_class,fullrtti)));
  771. { inittable for con-/destruction }
  772. if _class.members_need_inittable then
  773. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(RTTIWriter.get_rtti_label(_class,initrtti)))
  774. else
  775. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_nil_dataptr);
  776. { auto table }
  777. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_nil_dataptr);
  778. { interface table }
  779. if _class.ImplementedInterfaces.count>0 then
  780. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(interfacetable))
  781. else if _class.implements_any_interfaces then
  782. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_nil_dataptr)
  783. else
  784. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(current_asmdata.RefAsmSymbol('FPC_EMPTYINTF')));
  785. { table for string messages }
  786. if (oo_has_msgstr in _class.objectoptions) then
  787. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(strmessagetable))
  788. else
  789. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_nil_dataptr);
  790. end;
  791. { write virtual methods }
  792. writevirtualmethods(current_asmdata.asmlists[al_globals]);
  793. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_nil_codeptr);
  794. { write the size of the VMT }
  795. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname(_class.vmt_mangledname));
  796. {$ifdef vtentry}
  797. { write vtinherit symbol to notify the linker of the class inheritance tree }
  798. hs:='VTINHERIT'+'_'+_class.vmt_mangledname+'$$';
  799. if assigned(_class.childof) then
  800. hs:=hs+_class.childof.vmt_mangledname
  801. else
  802. hs:=hs+_class.vmt_mangledname;
  803. current_asmdata.asmlists[al_globals].concat(tai_symbol.CreateName(hs,AT_DATA,0));
  804. {$endif vtentry}
  805. if is_class(_class) then
  806. current_asmdata.asmlists[al_globals].concatlist(templist);
  807. templist.Free;
  808. end;
  809. procedure gen_intf_wrapper(list:TAsmList;_class:tobjectdef);
  810. var
  811. i,j : longint;
  812. tmps : string;
  813. pd : TProcdef;
  814. ImplIntf : TImplementedInterface;
  815. begin
  816. for i:=0 to _class.ImplementedInterfaces.count-1 do
  817. begin
  818. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  819. if (ImplIntf=ImplIntf.VtblImplIntf) and
  820. assigned(ImplIntf.ProcDefs) then
  821. begin
  822. maybe_new_object_file(list);
  823. for j:=0 to ImplIntf.ProcDefs.Count-1 do
  824. begin
  825. pd:=TProcdef(ImplIntf.ProcDefs[j]);
  826. { we don't track method calls via interfaces yet ->
  827. assume that every method called via an interface call
  828. is reachable for now }
  829. if (po_virtualmethod in pd.procoptions) and
  830. not is_objectpascal_helper(tprocdef(pd).struct) then
  831. tobjectdef(tprocdef(pd).struct).register_vmt_call(tprocdef(pd).extnumber);
  832. tmps:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+
  833. ImplIntf.IntfDef.objname^+'_$_'+tostr(j)+'_$_'+pd.mangledname);
  834. { create wrapper code }
  835. new_section(list,sec_code,tmps,0);
  836. hlcg.init_register_allocators;
  837. cg.g_intf_wrapper(list,pd,tmps,ImplIntf.ioffset);
  838. hlcg.done_register_allocators;
  839. end;
  840. end;
  841. end;
  842. end;
  843. procedure gen_intf_wrappers(list:TAsmList;st:TSymtable);
  844. var
  845. i : longint;
  846. def : tdef;
  847. begin
  848. for i:=0 to st.DefList.Count-1 do
  849. begin
  850. def:=tdef(st.DefList[i]);
  851. { if def can contain nested types then handle it symtable }
  852. if def.typ in [objectdef,recorddef] then
  853. gen_intf_wrappers(list,tabstractrecorddef(def).symtable);
  854. if is_class(def) then
  855. gen_intf_wrapper(list,tobjectdef(def));
  856. end;
  857. end;
  858. procedure do_write_persistent_type_info(st:tsymtable;is_global:boolean);
  859. var
  860. i : longint;
  861. def : tdef;
  862. vmtwriter : TVMTWriter;
  863. begin
  864. {$ifdef jvm}
  865. { no Delphi-style RTTI }
  866. exit;
  867. {$endif jvm}
  868. for i:=0 to st.DefList.Count-1 do
  869. begin
  870. def:=tdef(st.DefList[i]);
  871. case def.typ of
  872. recorddef :
  873. do_write_persistent_type_info(trecorddef(def).symtable,is_global);
  874. objectdef :
  875. begin
  876. { Skip generics and forward defs }
  877. if ([df_generic,df_genconstraint]*def.defoptions<>[]) or
  878. (oo_is_forward in tobjectdef(def).objectoptions) then
  879. continue;
  880. do_write_persistent_type_info(tobjectdef(def).symtable,is_global);
  881. { Write also VMT if not done yet }
  882. if not(ds_vmt_written in def.defstates) then
  883. begin
  884. vmtwriter:=TVMTWriter.create(tobjectdef(def));
  885. if is_interface(tobjectdef(def)) then
  886. vmtwriter.writeinterfaceids(current_asmdata.AsmLists[al_globals]);
  887. if (oo_has_vmt in tobjectdef(def).objectoptions) then
  888. vmtwriter.writevmt;
  889. vmtwriter.free;
  890. include(def.defstates,ds_vmt_written);
  891. end;
  892. end;
  893. procdef :
  894. begin
  895. if assigned(tprocdef(def).localst) and
  896. (tprocdef(def).localst.symtabletype=localsymtable) then
  897. do_write_persistent_type_info(tprocdef(def).localst,false);
  898. if assigned(tprocdef(def).parast) then
  899. do_write_persistent_type_info(tprocdef(def).parast,false);
  900. end;
  901. end;
  902. { generate always persistent tables for types in the interface so it can
  903. be reused in other units and give always the same pointer location. }
  904. { Init }
  905. if (
  906. assigned(def.typesym) and
  907. is_global and
  908. not is_objc_class_or_protocol(def)
  909. ) or
  910. is_managed_type(def) or
  911. (ds_init_table_used in def.defstates) then
  912. RTTIWriter.write_rtti(def,initrtti);
  913. { RTTI }
  914. if (
  915. assigned(def.typesym) and
  916. is_global and
  917. not is_objc_class_or_protocol(def)
  918. ) or
  919. (ds_rtti_table_used in def.defstates) then
  920. RTTIWriter.write_rtti(def,fullrtti);
  921. end;
  922. end;
  923. procedure write_persistent_type_info(st:tsymtable;is_global:boolean);
  924. begin
  925. create_hlcodegen;
  926. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],st);
  927. do_write_persistent_type_info(st,is_global);
  928. destroy_hlcodegen;
  929. end;
  930. end.