nobj.pas 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Routines for the code generation of data structures
  4. like VMT, Messages, VTables, Interfaces descs
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit nobj;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cutils,cclasses,
  23. globtype,
  24. symdef,symsym,
  25. aasmbase,aasmtai,aasmdata
  26. ;
  27. type
  28. TVMTBuilder=class
  29. private
  30. _Class : tobjectdef;
  31. function is_new_vmt_entry(pd:tprocdef):boolean;
  32. procedure add_new_vmt_entry(pd:tprocdef);
  33. function check_msg_str(vmtpd, pd: tprocdef):boolean;
  34. function intf_search_procdef_by_name(proc: tprocdef;const name: string): tprocdef;
  35. procedure intf_get_procdefs(ImplIntf:TImplementedInterface;IntfDef:TObjectDef);
  36. procedure intf_get_procdefs_recursive(ImplIntf:TImplementedInterface;IntfDef:TObjectDef);
  37. procedure intf_optimize_vtbls;
  38. procedure intf_allocate_vtbls;
  39. public
  40. constructor create(c:tobjectdef);
  41. destructor destroy;override;
  42. procedure generate_vmt;
  43. procedure build_interface_mappings;
  44. end;
  45. type
  46. pprocdeftree = ^tprocdeftree;
  47. tprocdeftree = record
  48. data : tprocdef;
  49. nl : tasmlabel;
  50. l,r : pprocdeftree;
  51. end;
  52. TVMTWriter=class
  53. private
  54. _Class : tobjectdef;
  55. { message tables }
  56. root : pprocdeftree;
  57. procedure disposeprocdeftree(p : pprocdeftree);
  58. procedure insertmsgint(p:TObject;arg:pointer);
  59. procedure insertmsgstr(p:TObject;arg:pointer);
  60. procedure insertint(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  61. procedure insertstr(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  62. procedure writenames(p : pprocdeftree);
  63. procedure writeintentry(p : pprocdeftree);
  64. procedure writestrentry(p : pprocdeftree);
  65. {$ifdef WITHDMT}
  66. { dmt }
  67. procedure insertdmtentry(p:TObject;arg:pointer);
  68. procedure writedmtindexentry(p : pprocdeftree);
  69. procedure writedmtaddressentry(p : pprocdeftree);
  70. {$endif}
  71. { published methods }
  72. procedure do_count_published_methods(p:TObject;arg:pointer);
  73. procedure do_gen_published_methods(p:TObject;arg:pointer);
  74. { virtual methods }
  75. procedure writevirtualmethods(List:TAsmList);
  76. { interface tables }
  77. function intf_get_vtbl_name(AImplIntf:TImplementedInterface): string;
  78. procedure intf_create_vtbl(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  79. procedure intf_gen_intf_ref(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  80. function intf_write_table:TAsmLabel;
  81. { generates the message tables for a class }
  82. function genstrmsgtab : tasmlabel;
  83. function genintmsgtab : tasmlabel;
  84. function genpublishedmethodstable : tasmlabel;
  85. function generate_field_table : tasmlabel;
  86. {$ifdef WITHDMT}
  87. { generates a DMT for _class }
  88. function gendmt : tasmlabel;
  89. {$endif WITHDMT}
  90. public
  91. constructor create(c:tobjectdef);
  92. destructor destroy;override;
  93. { write the VMT to al_globals }
  94. procedure writevmt;
  95. procedure writeinterfaceids;
  96. end;
  97. implementation
  98. uses
  99. SysUtils,
  100. globals,verbose,systems,
  101. node,
  102. symbase,symtable,symconst,symtype,defcmp,
  103. dbgbase,
  104. ncgrtti,
  105. wpobase
  106. ;
  107. {*****************************************************************************
  108. TVMTBuilder
  109. *****************************************************************************}
  110. constructor TVMTBuilder.create(c:tobjectdef);
  111. begin
  112. inherited Create;
  113. _Class:=c;
  114. end;
  115. destructor TVMTBuilder.destroy;
  116. begin
  117. end;
  118. procedure TVMTBuilder.add_new_vmt_entry(pd:tprocdef);
  119. var
  120. i : longint;
  121. vmtentry : pvmtentry;
  122. vmtpd : tprocdef;
  123. begin
  124. { new entry is needed, override was not possible }
  125. if (po_overridingmethod in pd.procoptions) then
  126. MessagePos1(pd.fileinfo,parser_e_nothing_to_be_overridden,pd.fullprocname(false));
  127. { check that all methods have overload directive }
  128. if not(m_fpc in current_settings.modeswitches) then
  129. begin
  130. for i:=0 to _class.vmtentries.count-1 do
  131. begin
  132. vmtentry:=pvmtentry(_class.vmtentries[i]);
  133. vmtpd:=tprocdef(vmtentry^.procdef);
  134. if (vmtpd.procsym=pd.procsym) and
  135. (not(po_overload in pd.procoptions) or
  136. not(po_overload in vmtpd.procoptions)) then
  137. begin
  138. MessagePos1(pd.fileinfo,parser_e_no_overload_for_all_procs,pd.procsym.realname);
  139. { recover }
  140. include(vmtpd.procoptions,po_overload);
  141. include(pd.procoptions,po_overload);
  142. end;
  143. end;
  144. end;
  145. { Register virtual method and give it a number }
  146. if (po_virtualmethod in pd.procoptions) then
  147. begin
  148. { store vmt entry number in procdef }
  149. if (pd.extnumber<>$ffff) and
  150. (pd.extnumber<>_class.VMTEntries.Count) then
  151. internalerror(200810283);
  152. pd.extnumber:=_class.VMTEntries.Count;
  153. new(vmtentry);
  154. vmtentry^.procdef:=pd;
  155. vmtentry^.procdefderef.reset;
  156. vmtentry^.visibility:=pd.visibility;
  157. _class.VMTEntries.Add(vmtentry);
  158. end;
  159. end;
  160. function TVMTBuilder.check_msg_str(vmtpd, pd: tprocdef): boolean;
  161. begin
  162. result:=true;
  163. if not(is_objc_class_or_protocol(_class)) then
  164. begin
  165. { the only requirement for normal methods is that both either
  166. have a message string or not (the value is irrelevant) }
  167. if ((pd.procoptions * [po_msgstr]) <> (vmtpd.procoptions * [po_msgstr])) then
  168. begin
  169. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  170. tprocsym(vmtpd.procsym).write_parameter_lists(pd);
  171. result:=false;
  172. end
  173. end
  174. else
  175. begin
  176. { the compiler should have ensured that the protocol or parent
  177. class method has a message name specified }
  178. if not(po_msgstr in vmtpd.procoptions) then
  179. internalerror(2009070601);
  180. if not(po_msgstr in pd.procoptions) then
  181. begin
  182. { copy the protocol's/parent class' message name to the one in
  183. the class if none has been specified there }
  184. include(pd.procoptions,po_msgstr);
  185. pd.messageinf.str:=stringdup(vmtpd.messageinf.str^);
  186. end
  187. else
  188. begin
  189. { if both have a message name, make sure they are equal }
  190. if (vmtpd.messageinf.str^<>pd.messageinf.str^) then
  191. begin
  192. MessagePos2(pd.fileinfo,parser_e_objc_message_name_changed,vmtpd.messageinf.str^,pd.messageinf.str^);
  193. result:=false;
  194. end;
  195. end;
  196. end;
  197. end;
  198. function TVMTBuilder.is_new_vmt_entry(pd:tprocdef):boolean;
  199. const
  200. po_comp = [po_classmethod,po_virtualmethod,po_staticmethod,po_interrupt,po_iocheck,po_msgint,
  201. po_exports,po_varargs,po_explicitparaloc,po_nostackframe];
  202. var
  203. i : longint;
  204. hasequalpara,
  205. hasoverloads,
  206. pdoverload : boolean;
  207. vmtentry : pvmtentry;
  208. vmtpd : tprocdef;
  209. begin
  210. result:=false;
  211. { Load other values for easier readability }
  212. hasoverloads:=(tprocsym(pd.procsym).ProcdefList.Count>1);
  213. pdoverload:=(po_overload in pd.procoptions);
  214. { compare with all stored definitions }
  215. for i:=0 to _class.vmtentries.Count-1 do
  216. begin
  217. vmtentry:=pvmtentry(_class.vmtentries[i]);
  218. vmtpd:=tprocdef(vmtentry^.procdef);
  219. { ignore hidden entries (e.g. virtual overridden by a static) that are not visible anymore }
  220. if vmtentry^.visibility=vis_hidden then
  221. continue;
  222. { ignore different names }
  223. if vmtpd.procsym.name<>pd.procsym.name then
  224. continue;
  225. { hide private methods that are not visible anymore. For this check we
  226. must override the visibility with the highest value in the override chain.
  227. This is required for case (see tw3292) with protected-private-protected where the
  228. same vmtentry is used (PFV) }
  229. if not is_visible_for_object(vmtpd.owner,vmtentry^.visibility,_class) then
  230. continue;
  231. { inherit overload }
  232. if (po_overload in vmtpd.procoptions) then
  233. begin
  234. include(pd.procoptions,po_overload);
  235. pdoverload:=true;
  236. end;
  237. { compare parameter types only, no specifiers yet }
  238. hasequalpara:=(compare_paras(vmtpd.paras,pd.paras,cp_none,[])>=te_equal);
  239. { old definition has virtual
  240. new definition has no virtual or override }
  241. if (po_virtualmethod in vmtpd.procoptions) and
  242. (
  243. not(po_virtualmethod in pd.procoptions) or
  244. { new one has not override }
  245. (is_class_or_interface_or_objc(_class) and not(po_overridingmethod in pd.procoptions))
  246. ) then
  247. begin
  248. if (
  249. not(pdoverload or hasoverloads) or
  250. hasequalpara
  251. ) then
  252. begin
  253. if not(po_reintroduce in pd.procoptions) then
  254. if not(is_objc_class_or_protocol(_class)) then
  255. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false))
  256. else
  257. begin
  258. { In Objective-C, you cannot create a new VMT entry to
  259. start a new inheritance tree. We therefore give an
  260. error when the class is implemented in Pascal, to
  261. avoid confusion due to things working differently
  262. with Object Pascal classes.
  263. In case of external classes, we only give a hint,
  264. because requiring override everywhere may make
  265. automated header translation tools too complex. }
  266. if not(oo_is_external in _class.objectoptions) then
  267. MessagePos1(pd.fileinfo,parser_e_must_use_override_objc,pd.fullprocname(false))
  268. { there may be a lot of these in auto-translated
  269. heaeders, so only calculate the fullprocname if
  270. the hint will be shown }
  271. else if CheckVerbosity(V_Hint) then
  272. MessagePos1(pd.fileinfo,parser_h_should_use_override_objc,pd.fullprocname(false));
  273. { no new entry, but copy the message name if any from
  274. the procdef in the parent class }
  275. check_msg_str(vmtpd,pd);
  276. exit;
  277. end;
  278. { disable/hide old VMT entry }
  279. vmtentry^.visibility:=vis_hidden;
  280. end;
  281. end
  282. { both are virtual? }
  283. else if (po_virtualmethod in pd.procoptions) and
  284. (po_virtualmethod in vmtpd.procoptions) then
  285. begin
  286. { same parameter and return types (parameter specifiers will be checked below) }
  287. if hasequalpara and
  288. compatible_childmethod_resultdef(vmtpd.returndef,pd.returndef) then
  289. begin
  290. { inherite calling convention when it was explicit and the
  291. current definition has none explicit set }
  292. if (po_hascallingconvention in vmtpd.procoptions) and
  293. not(po_hascallingconvention in pd.procoptions) then
  294. begin
  295. pd.proccalloption:=vmtpd.proccalloption;
  296. include(pd.procoptions,po_hascallingconvention);
  297. end;
  298. { All parameter specifiers and some procedure the flags have to match
  299. except abstract and override }
  300. if (compare_paras(vmtpd.paras,pd.paras,cp_all,[])<te_equal) or
  301. (vmtpd.proccalloption<>pd.proccalloption) or
  302. (vmtpd.proctypeoption<>pd.proctypeoption) or
  303. ((vmtpd.procoptions*po_comp)<>(pd.procoptions*po_comp)) then
  304. begin
  305. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  306. tprocsym(vmtpd.procsym).write_parameter_lists(pd);
  307. end;
  308. check_msg_str(vmtpd,pd);
  309. { Give a note if the new visibility is lower. For a higher
  310. visibility update the vmt info }
  311. if vmtentry^.visibility>pd.visibility then
  312. MessagePos4(pd.fileinfo,parser_n_ignore_lower_visibility,pd.fullprocname(false),
  313. visibilityname[pd.visibility],tobjectdef(vmtpd.owner.defowner).objrealname^,visibilityname[vmtentry^.visibility])
  314. else if pd.visibility>vmtentry^.visibility then
  315. vmtentry^.visibility:=pd.visibility;
  316. { override old virtual method in VMT }
  317. if (vmtpd.extnumber<>i) then
  318. internalerror(200611084);
  319. pd.extnumber:=vmtpd.extnumber;
  320. vmtentry^.procdef:=pd;
  321. exit;
  322. end
  323. { different parameters }
  324. else
  325. begin
  326. { when we got an override directive then can search futher for
  327. the procedure to override.
  328. If we are starting a new virtual tree then hide the old tree }
  329. if not(po_overridingmethod in pd.procoptions) and
  330. not(pdoverload or hasoverloads) then
  331. begin
  332. if not(po_reintroduce in pd.procoptions) then
  333. begin
  334. if not is_object(_class) and
  335. not is_objc_class_or_protocol(_class) then
  336. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false))
  337. else
  338. { objects don't allow starting a new virtual tree
  339. and neither does Objective-C }
  340. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,vmtpd.fullprocname(false));
  341. end;
  342. { disable/hide old VMT entry }
  343. vmtentry^.visibility:=vis_hidden;
  344. end;
  345. end;
  346. end;
  347. end;
  348. { No entry found, we need to create a new entry }
  349. result:=true;
  350. end;
  351. function TVMTBuilder.intf_search_procdef_by_name(proc: tprocdef;const name: string): tprocdef;
  352. const
  353. po_comp = [po_classmethod,po_staticmethod,po_interrupt,po_iocheck,po_msgint,
  354. po_exports,po_varargs,po_explicitparaloc,po_nostackframe];
  355. var
  356. implprocdef : Tprocdef;
  357. i: cardinal;
  358. hclass : tobjectdef;
  359. hashedid : THashedIDString;
  360. srsym : tsym;
  361. begin
  362. result:=nil;
  363. hashedid.id:=name;
  364. hclass:=_class;
  365. while assigned(hclass) do
  366. begin
  367. srsym:=tsym(hclass.symtable.FindWithHash(hashedid));
  368. if assigned(srsym) and
  369. (srsym.typ=procsym) then
  370. begin
  371. for i:=0 to Tprocsym(srsym).ProcdefList.Count-1 do
  372. begin
  373. implprocdef:=tprocdef(tprocsym(srsym).ProcdefList[i]);
  374. if (implprocdef.procsym=tprocsym(srsym)) and
  375. (compare_paras(proc.paras,implprocdef.paras,cp_all,[cpo_ignorehidden,cpo_comparedefaultvalue])>=te_equal) and
  376. (compare_defs(proc.returndef,implprocdef.returndef,nothingn)>=te_equal) and
  377. (proc.proccalloption=implprocdef.proccalloption) and
  378. (proc.proctypeoption=implprocdef.proctypeoption) and
  379. ((proc.procoptions*po_comp)=((implprocdef.procoptions+[po_virtualmethod])*po_comp)) and
  380. check_msg_str(proc,implprocdef) then
  381. begin
  382. result:=implprocdef;
  383. exit;
  384. end;
  385. end;
  386. end;
  387. hclass:=hclass.childof;
  388. end;
  389. end;
  390. procedure TVMTBuilder.intf_get_procdefs(ImplIntf:TImplementedInterface;IntfDef:TObjectDef);
  391. var
  392. i : longint;
  393. def : tdef;
  394. hs,
  395. prefix,
  396. mappedname: string;
  397. implprocdef: tprocdef;
  398. begin
  399. prefix:=ImplIntf.IntfDef.symtable.name^+'.';
  400. for i:=0 to IntfDef.symtable.DefList.Count-1 do
  401. begin
  402. def:=tdef(IntfDef.symtable.DefList[i]);
  403. if assigned(def) and
  404. (def.typ=procdef) then
  405. begin
  406. { Find implementing procdef
  407. 1. Check for mapped name
  408. 2. Use symbol name }
  409. implprocdef:=nil;
  410. hs:=prefix+tprocdef(def).procsym.name;
  411. mappedname:=ImplIntf.GetMapping(hs);
  412. if mappedname<>'' then
  413. implprocdef:=intf_search_procdef_by_name(tprocdef(def),mappedname);
  414. if not assigned(implprocdef) then
  415. implprocdef:=intf_search_procdef_by_name(tprocdef(def),tprocdef(def).procsym.name);
  416. { Add procdef to the implemented interface }
  417. if assigned(implprocdef) then
  418. begin
  419. if (implprocdef._class.objecttype<>odt_objcclass) then
  420. ImplIntf.AddImplProc(implprocdef)
  421. else
  422. begin
  423. { If no message name has been specified for the method
  424. in the objcclass, copy it from the protocol
  425. definition. }
  426. if not(po_msgstr in tprocdef(def).procoptions) then
  427. begin
  428. include(tprocdef(def).procoptions,po_msgstr);
  429. implprocdef.messageinf.str:=stringdup(tprocdef(def).messageinf.str^);
  430. end
  431. else
  432. begin
  433. { If a message name has been specified in the
  434. objcclass, it has to match the message name in the
  435. protocol definition. }
  436. if (implprocdef.messageinf.str^<>tprocdef(def).messageinf.str^) then
  437. MessagePos2(implprocdef.fileinfo,parser_e_objc_message_name_changed,tprocdef(def).messageinf.str^,implprocdef.messageinf.str^);
  438. end;
  439. end;
  440. end
  441. else
  442. if (ImplIntf.IType=etStandard) and
  443. not(tprocdef(def).optional) then
  444. Message1(sym_e_no_matching_implementation_found,tprocdef(def).fullprocname(false));
  445. end;
  446. end;
  447. end;
  448. procedure TVMTBuilder.intf_get_procdefs_recursive(ImplIntf:TImplementedInterface;IntfDef:TObjectDef);
  449. begin
  450. if assigned(IntfDef.childof) then
  451. intf_get_procdefs_recursive(ImplIntf,IntfDef.childof);
  452. intf_get_procdefs(ImplIntf,IntfDef);
  453. end;
  454. procedure TVMTBuilder.intf_optimize_vtbls;
  455. type
  456. tcompintfentry = record
  457. weight: longint;
  458. compintf: longint;
  459. end;
  460. { Max 1000 interface in the class header interfaces it's enough imho }
  461. tcompintfs = array[0..1000] of tcompintfentry;
  462. pcompintfs = ^tcompintfs;
  463. tequals = array[0..1000] of longint;
  464. pequals = ^tequals;
  465. timpls = array[0..1000] of longint;
  466. pimpls = ^timpls;
  467. var
  468. equals: pequals;
  469. compats: pcompintfs;
  470. impls: pimpls;
  471. ImplIntfCount,
  472. w,i,j,k: longint;
  473. ImplIntfI,
  474. ImplIntfJ : TImplementedInterface;
  475. cij: boolean;
  476. cji: boolean;
  477. begin
  478. ImplIntfCount:=_class.ImplementedInterfaces.count;
  479. if ImplIntfCount>=High(tequals) then
  480. Internalerror(200006135);
  481. getmem(compats,sizeof(tcompintfentry)*ImplIntfCount);
  482. getmem(equals,sizeof(longint)*ImplIntfCount);
  483. getmem(impls,sizeof(longint)*ImplIntfCount);
  484. filldword(compats^,(sizeof(tcompintfentry) div sizeof(dword))*ImplIntfCount,dword(-1));
  485. filldword(equals^,ImplIntfCount,dword(-1));
  486. filldword(impls^,ImplIntfCount,dword(-1));
  487. { ismergepossible is a containing relation
  488. meaning of ismergepossible(a,b,w) =
  489. if implementorfunction map of a is contained implementorfunction map of b
  490. imp(a,b) and imp(b,c) => imp(a,c) ; imp(a,b) and imp(b,a) => a == b
  491. }
  492. { the order is very important for correct allocation }
  493. for i:=0 to ImplIntfCount-1 do
  494. begin
  495. for j:=i+1 to ImplIntfCount-1 do
  496. begin
  497. ImplIntfI:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  498. ImplIntfJ:=TImplementedInterface(_class.ImplementedInterfaces[j]);
  499. cij:=ImplIntfI.IsImplMergePossible(ImplIntfJ,w);
  500. cji:=ImplIntfJ.IsImplMergePossible(ImplIntfI,w);
  501. if cij and cji then { i equal j }
  502. begin
  503. { get minimum index of equal }
  504. if equals^[j]=-1 then
  505. equals^[j]:=i;
  506. end
  507. else if cij then
  508. begin
  509. { get minimum index of maximum weight }
  510. if compats^[i].weight<w then
  511. begin
  512. compats^[i].weight:=w;
  513. compats^[i].compintf:=j;
  514. end;
  515. end
  516. else if cji then
  517. begin
  518. { get minimum index of maximum weight }
  519. if (compats^[j].weight<w) then
  520. begin
  521. compats^[j].weight:=w;
  522. compats^[j].compintf:=i;
  523. end;
  524. end;
  525. end;
  526. end;
  527. { Reset, no replacements by default }
  528. for i:=0 to ImplIntfCount-1 do
  529. impls^[i]:=i;
  530. { Replace vtbls when equal or compat, repeat
  531. until there are no replacements possible anymore. This is
  532. needed for the cases like:
  533. First loop: 2->3, 3->1
  534. Second loop: 2->1 (because 3 was replaced with 1)
  535. }
  536. repeat
  537. k:=0;
  538. for i:=0 to ImplIntfCount-1 do
  539. begin
  540. if compats^[impls^[i]].compintf<>-1 then
  541. impls^[i]:=compats^[impls^[i]].compintf
  542. else if equals^[impls^[i]]<>-1 then
  543. impls^[i]:=equals^[impls^[i]]
  544. else
  545. inc(k);
  546. end;
  547. until k=ImplIntfCount;
  548. { Update the VtblImplIntf }
  549. for i:=0 to ImplIntfCount-1 do
  550. begin
  551. ImplIntfI:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  552. ImplIntfI.VtblImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[impls^[i]]);
  553. end;
  554. freemem(compats);
  555. freemem(equals);
  556. freemem(impls);
  557. end;
  558. procedure TVMTBuilder.intf_allocate_vtbls;
  559. var
  560. i : longint;
  561. ImplIntf : TImplementedInterface;
  562. begin
  563. { Allocation vtbl space }
  564. for i:=0 to _class.ImplementedInterfaces.count-1 do
  565. begin
  566. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  567. { if it implements itself and if it's not implemented by delegation }
  568. if (ImplIntf.VtblImplIntf=ImplIntf) and (ImplIntf.IType=etStandard) then
  569. begin
  570. { allocate a pointer in the object memory }
  571. with tObjectSymtable(_class.symtable) do
  572. begin
  573. datasize:=align(datasize,sizeof(pint));
  574. ImplIntf.Ioffset:=datasize;
  575. datasize:=datasize+sizeof(pint);
  576. end;
  577. end;
  578. end;
  579. { Update ioffset of current interface with the ioffset from
  580. the interface that is reused to implements this interface }
  581. for i:=0 to _class.ImplementedInterfaces.count-1 do
  582. begin
  583. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  584. if ImplIntf.VtblImplIntf<>ImplIntf then
  585. ImplIntf.IOffset:=ImplIntf.VtblImplIntf.IOffset;
  586. end;
  587. end;
  588. procedure TVMTBuilder.generate_vmt;
  589. var
  590. i : longint;
  591. def : tdef;
  592. old_current_objectdef : tobjectdef;
  593. begin
  594. old_current_objectdef:=current_objectdef;
  595. current_objectdef:=_class;
  596. _class.resetvmtentries;
  597. { inherit (copy) VMT from parent object }
  598. if assigned(_class.childof) then
  599. begin
  600. if not assigned(_class.childof.vmtentries) then
  601. internalerror(200810281);
  602. _class.copyvmtentries(_class.childof);
  603. end;
  604. { process all procdefs, we must process the defs to
  605. keep the same order as that is written in the source
  606. to be compatible with the indexes in the interface vtable (PFV) }
  607. for i:=0 to _class.symtable.DefList.Count-1 do
  608. begin
  609. def:=tdef(_class.symtable.DefList[i]);
  610. if def.typ=procdef then
  611. begin
  612. { VMT entry }
  613. if is_new_vmt_entry(tprocdef(def)) then
  614. add_new_vmt_entry(tprocdef(def));
  615. end;
  616. end;
  617. build_interface_mappings;
  618. if assigned(_class.ImplementedInterfaces) and
  619. not(is_objc_class_or_protocol(_class)) then
  620. begin
  621. { Optimize interface tables to reuse wrappers }
  622. intf_optimize_vtbls;
  623. { Allocate interface tables }
  624. intf_allocate_vtbls;
  625. end;
  626. current_objectdef:=old_current_objectdef;
  627. end;
  628. procedure TVMTBuilder.build_interface_mappings;
  629. var
  630. ImplIntf : TImplementedInterface;
  631. i: longint;
  632. begin
  633. { Find Procdefs implementing the interfaces }
  634. if assigned(_class.ImplementedInterfaces) then
  635. begin
  636. { Collect implementor functions into the tImplementedInterface.procdefs }
  637. for i:=0 to _class.ImplementedInterfaces.count-1 do
  638. begin
  639. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  640. intf_get_procdefs_recursive(ImplIntf,ImplIntf.IntfDef);
  641. end;
  642. end;
  643. end;
  644. {*****************************************************************************
  645. TVMTWriter
  646. *****************************************************************************}
  647. constructor TVMTWriter.create(c:tobjectdef);
  648. begin
  649. inherited Create;
  650. _Class:=c;
  651. end;
  652. destructor TVMTWriter.destroy;
  653. begin
  654. end;
  655. {**************************************
  656. Message Tables
  657. **************************************}
  658. procedure TVMTWriter.disposeprocdeftree(p : pprocdeftree);
  659. begin
  660. if assigned(p^.l) then
  661. disposeprocdeftree(p^.l);
  662. if assigned(p^.r) then
  663. disposeprocdeftree(p^.r);
  664. dispose(p);
  665. end;
  666. procedure TVMTWriter.insertint(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  667. begin
  668. if at=nil then
  669. begin
  670. at:=p;
  671. inc(count);
  672. end
  673. else
  674. begin
  675. if p^.data.messageinf.i<at^.data.messageinf.i then
  676. insertint(p,at^.l,count)
  677. else if p^.data.messageinf.i>at^.data.messageinf.i then
  678. insertint(p,at^.r,count)
  679. else
  680. Message1(parser_e_duplicate_message_label,tostr(p^.data.messageinf.i));
  681. end;
  682. end;
  683. procedure TVMTWriter.insertstr(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  684. var
  685. i : integer;
  686. begin
  687. if at=nil then
  688. begin
  689. at:=p;
  690. inc(count);
  691. end
  692. else
  693. begin
  694. i:=CompareStr(p^.data.messageinf.str^,at^.data.messageinf.str^);
  695. if i<0 then
  696. insertstr(p,at^.l,count)
  697. else if i>0 then
  698. insertstr(p,at^.r,count)
  699. else
  700. Message1(parser_e_duplicate_message_label,p^.data.messageinf.str^);
  701. end;
  702. end;
  703. procedure TVMTWriter.insertmsgint(p:TObject;arg:pointer);
  704. var
  705. i : longint;
  706. pd : Tprocdef;
  707. pt : pprocdeftree;
  708. begin
  709. if tsym(p).typ<>procsym then
  710. exit;
  711. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  712. begin
  713. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  714. if po_msgint in pd.procoptions then
  715. begin
  716. new(pt);
  717. pt^.data:=pd;
  718. pt^.l:=nil;
  719. pt^.r:=nil;
  720. insertint(pt,root,plongint(arg)^);
  721. end;
  722. end;
  723. end;
  724. procedure TVMTWriter.insertmsgstr(p:TObject;arg:pointer);
  725. var
  726. i : longint;
  727. pd : Tprocdef;
  728. pt : pprocdeftree;
  729. begin
  730. if tsym(p).typ<>procsym then
  731. exit;
  732. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  733. begin
  734. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  735. if po_msgstr in pd.procoptions then
  736. begin
  737. new(pt);
  738. pt^.data:=pd;
  739. pt^.l:=nil;
  740. pt^.r:=nil;
  741. insertstr(pt,root,plongint(arg)^);
  742. end;
  743. end;
  744. end;
  745. procedure TVMTWriter.writenames(p : pprocdeftree);
  746. var
  747. ca : pchar;
  748. len : byte;
  749. begin
  750. current_asmdata.getdatalabel(p^.nl);
  751. if assigned(p^.l) then
  752. writenames(p^.l);
  753. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  754. current_asmdata.asmlists[al_globals].concat(Tai_label.Create(p^.nl));
  755. len:=length(p^.data.messageinf.str^);
  756. current_asmdata.asmlists[al_globals].concat(tai_const.create_8bit(len));
  757. getmem(ca,len+1);
  758. move(p^.data.messageinf.str^[1],ca^,len);
  759. ca[len]:=#0;
  760. current_asmdata.asmlists[al_globals].concat(Tai_string.Create_pchar(ca,len));
  761. if assigned(p^.r) then
  762. writenames(p^.r);
  763. end;
  764. procedure TVMTWriter.writestrentry(p : pprocdeftree);
  765. begin
  766. if assigned(p^.l) then
  767. writestrentry(p^.l);
  768. { write name label }
  769. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(p^.nl));
  770. current_asmdata.asmlists[al_globals].concat(Tai_const.Createname(p^.data.mangledname,0));
  771. if assigned(p^.r) then
  772. writestrentry(p^.r);
  773. end;
  774. function TVMTWriter.genstrmsgtab : tasmlabel;
  775. var
  776. count : aint;
  777. begin
  778. root:=nil;
  779. count:=0;
  780. { insert all message handlers into a tree, sorted by name }
  781. _class.symtable.SymList.ForEachCall(@insertmsgstr,@count);
  782. { write all names }
  783. if assigned(root) then
  784. writenames(root);
  785. { now start writing of the message string table }
  786. current_asmdata.getdatalabel(result);
  787. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  788. current_asmdata.asmlists[al_globals].concat(Tai_label.Create(result));
  789. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_pint(count));
  790. if assigned(root) then
  791. begin
  792. writestrentry(root);
  793. disposeprocdeftree(root);
  794. end;
  795. end;
  796. procedure TVMTWriter.writeintentry(p : pprocdeftree);
  797. begin
  798. if assigned(p^.l) then
  799. writeintentry(p^.l);
  800. { write name label }
  801. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_32bit(p^.data.messageinf.i));
  802. current_asmdata.asmlists[al_globals].concat(Tai_const.Createname(p^.data.mangledname,0));
  803. if assigned(p^.r) then
  804. writeintentry(p^.r);
  805. end;
  806. function TVMTWriter.genintmsgtab : tasmlabel;
  807. var
  808. r : tasmlabel;
  809. count : longint;
  810. begin
  811. root:=nil;
  812. count:=0;
  813. { insert all message handlers into a tree, sorted by name }
  814. _class.symtable.SymList.ForEachCall(@insertmsgint,@count);
  815. { now start writing of the message string table }
  816. current_asmdata.getdatalabel(r);
  817. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  818. current_asmdata.asmlists[al_globals].concat(Tai_label.Create(r));
  819. genintmsgtab:=r;
  820. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_32bit(count));
  821. if assigned(root) then
  822. begin
  823. writeintentry(root);
  824. disposeprocdeftree(root);
  825. end;
  826. end;
  827. {$ifdef WITHDMT}
  828. {**************************************
  829. DMT
  830. **************************************}
  831. procedure TVMTWriter.insertdmtentry(p:TObject;arg:pointer);
  832. var
  833. hp : tprocdef;
  834. pt : pprocdeftree;
  835. begin
  836. if tsym(p).typ=procsym then
  837. begin
  838. hp:=tprocsym(p).definition;
  839. while assigned(hp) do
  840. begin
  841. if (po_msgint in hp.procoptions) then
  842. begin
  843. new(pt);
  844. pt^.p:=hp;
  845. pt^.l:=nil;
  846. pt^.r:=nil;
  847. insertint(pt,root);
  848. end;
  849. hp:=hp.nextoverloaded;
  850. end;
  851. end;
  852. end;
  853. procedure TVMTWriter.writedmtindexentry(p : pprocdeftree);
  854. begin
  855. if assigned(p^.l) then
  856. writedmtindexentry(p^.l);
  857. al_globals.concat(Tai_const.Create_32bit(p^.data.messageinf.i));
  858. if assigned(p^.r) then
  859. writedmtindexentry(p^.r);
  860. end;
  861. procedure TVMTWriter.writedmtaddressentry(p : pprocdeftree);
  862. begin
  863. if assigned(p^.l) then
  864. writedmtaddressentry(p^.l);
  865. al_globals.concat(Tai_const_symbol.Createname(p^.data.mangledname,0));
  866. if assigned(p^.r) then
  867. writedmtaddressentry(p^.r);
  868. end;
  869. function TVMTWriter.gendmt : tasmlabel;
  870. var
  871. r : tasmlabel;
  872. begin
  873. root:=nil;
  874. count:=0;
  875. gendmt:=nil;
  876. { insert all message handlers into a tree, sorted by number }
  877. _class.symtable.SymList.ForEachCall(insertdmtentry);
  878. if count>0 then
  879. begin
  880. current_asmdata.getdatalabel(r);
  881. gendmt:=r;
  882. al_globals.concat(cai_align.create(const_align(sizeof(pint))));
  883. al_globals.concat(Tai_label.Create(r));
  884. { entries for caching }
  885. al_globals.concat(Tai_const.Create_ptr(0));
  886. al_globals.concat(Tai_const.Create_ptr(0));
  887. al_globals.concat(Tai_const.Create_32bit(count));
  888. if assigned(root) then
  889. begin
  890. writedmtindexentry(root);
  891. writedmtaddressentry(root);
  892. disposeprocdeftree(root);
  893. end;
  894. end;
  895. end;
  896. {$endif WITHDMT}
  897. {**************************************
  898. Published Methods
  899. **************************************}
  900. procedure TVMTWriter.do_count_published_methods(p:TObject;arg:pointer);
  901. var
  902. i : longint;
  903. pd : tprocdef;
  904. begin
  905. if (tsym(p).typ<>procsym) then
  906. exit;
  907. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  908. begin
  909. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  910. if (pd.procsym=tsym(p)) and
  911. (pd.visibility=vis_published) then
  912. inc(plongint(arg)^);
  913. end;
  914. end;
  915. procedure TVMTWriter.do_gen_published_methods(p:TObject;arg:pointer);
  916. var
  917. i : longint;
  918. l : tasmlabel;
  919. pd : tprocdef;
  920. begin
  921. if (tsym(p).typ<>procsym) then
  922. exit;
  923. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  924. begin
  925. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  926. if (pd.procsym=tsym(p)) and
  927. (pd.visibility=vis_published) then
  928. begin
  929. current_asmdata.getdatalabel(l);
  930. current_asmdata.asmlists[al_typedconsts].concat(cai_align.create(const_align(sizeof(pint))));
  931. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(l));
  932. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_8bit(length(tsym(p).realname)));
  933. current_asmdata.asmlists[al_typedconsts].concat(Tai_string.Create(tsym(p).realname));
  934. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(l));
  935. if po_abstractmethod in pd.procoptions then
  936. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil))
  937. else
  938. current_asmdata.asmlists[al_globals].concat(Tai_const.Createname(pd.mangledname,0));
  939. end;
  940. end;
  941. end;
  942. function TVMTWriter.genpublishedmethodstable : tasmlabel;
  943. var
  944. l : tasmlabel;
  945. count : longint;
  946. begin
  947. count:=0;
  948. _class.symtable.SymList.ForEachCall(@do_count_published_methods,@count);
  949. if count>0 then
  950. begin
  951. current_asmdata.getdatalabel(l);
  952. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  953. current_asmdata.asmlists[al_globals].concat(Tai_label.Create(l));
  954. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_32bit(count));
  955. _class.symtable.SymList.ForEachCall(@do_gen_published_methods,nil);
  956. genpublishedmethodstable:=l;
  957. end
  958. else
  959. genpublishedmethodstable:=nil;
  960. end;
  961. function TVMTWriter.generate_field_table : tasmlabel;
  962. var
  963. i : longint;
  964. sym : tsym;
  965. fieldtable,
  966. classtable : tasmlabel;
  967. classindex,
  968. fieldcount : longint;
  969. classtablelist : TFPList;
  970. begin
  971. classtablelist:=TFPList.Create;
  972. current_asmdata.getdatalabel(fieldtable);
  973. current_asmdata.getdatalabel(classtable);
  974. maybe_new_object_file(current_asmdata.asmlists[al_rtti]);
  975. new_section(current_asmdata.asmlists[al_rtti],sec_rodata,classtable.name,const_align(sizeof(pint)));
  976. { retrieve field info fields }
  977. fieldcount:=0;
  978. for i:=0 to _class.symtable.SymList.Count-1 do
  979. begin
  980. sym:=tsym(_class.symtable.SymList[i]);
  981. if (sym.typ=fieldvarsym) and
  982. (sym.visibility=vis_published) then
  983. begin
  984. if tfieldvarsym(sym).vardef.typ<>objectdef then
  985. internalerror(200611032);
  986. classindex:=classtablelist.IndexOf(tfieldvarsym(sym).vardef);
  987. if classindex=-1 then
  988. classtablelist.Add(tfieldvarsym(sym).vardef);
  989. inc(fieldcount);
  990. end;
  991. end;
  992. { write fields }
  993. current_asmdata.asmlists[al_rtti].concat(Tai_label.Create(fieldtable));
  994. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(fieldcount));
  995. if (tf_requires_proper_alignment in target_info.flags) then
  996. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  997. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(classtable));
  998. for i:=0 to _class.symtable.SymList.Count-1 do
  999. begin
  1000. sym:=tsym(_class.symtable.SymList[i]);
  1001. if (sym.typ=fieldvarsym) and
  1002. (sym.visibility=vis_published) then
  1003. begin
  1004. if (tf_requires_proper_alignment in target_info.flags) then
  1005. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(pint)));
  1006. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_pint(tfieldvarsym(sym).fieldoffset));
  1007. classindex:=classtablelist.IndexOf(tfieldvarsym(sym).vardef);
  1008. if classindex=-1 then
  1009. internalerror(200611033);
  1010. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(classindex+1));
  1011. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(tfieldvarsym(sym).realname)));
  1012. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(tfieldvarsym(sym).realname));
  1013. end;
  1014. end;
  1015. { generate the class table }
  1016. current_asmdata.asmlists[al_rtti].concat(cai_align.create(const_align(sizeof(pint))));
  1017. current_asmdata.asmlists[al_rtti].concat(Tai_label.Create(classtable));
  1018. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(classtablelist.count));
  1019. if (tf_requires_proper_alignment in target_info.flags) then
  1020. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  1021. for i:=0 to classtablelist.Count-1 do
  1022. current_asmdata.asmlists[al_rtti].concat(Tai_const.Createname(tobjectdef(classtablelist[i]).vmt_mangledname,0));
  1023. classtablelist.free;
  1024. result:=fieldtable;
  1025. end;
  1026. {**************************************
  1027. Interface tables
  1028. **************************************}
  1029. function TVMTWriter.intf_get_vtbl_name(AImplIntf:TImplementedInterface): string;
  1030. begin
  1031. result:=make_mangledname('VTBL',_class.owner,_class.objname^+'_$_'+AImplIntf.IntfDef.objname^);
  1032. end;
  1033. procedure TVMTWriter.intf_create_vtbl(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  1034. var
  1035. pd : tprocdef;
  1036. vtblstr,
  1037. hs : string;
  1038. i : longint;
  1039. begin
  1040. vtblstr:=intf_get_vtbl_name(AImplIntf);
  1041. section_symbol_start(rawdata,vtblstr,AT_DATA,true,sec_data,const_align(sizeof(pint)));
  1042. if assigned(AImplIntf.procdefs) then
  1043. begin
  1044. for i:=0 to AImplIntf.procdefs.count-1 do
  1045. begin
  1046. pd:=tprocdef(AImplIntf.procdefs[i]);
  1047. hs:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+AImplIntf.IntfDef.objname^+'_$_'+
  1048. tostr(i)+'_$_'+pd.mangledname);
  1049. { create reference }
  1050. rawdata.concat(Tai_const.Createname(hs,0));
  1051. end;
  1052. end;
  1053. section_symbol_end(rawdata,vtblstr);
  1054. end;
  1055. procedure TVMTWriter.intf_gen_intf_ref(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  1056. var
  1057. iidlabel,
  1058. guidlabel : tasmlabel;
  1059. i: longint;
  1060. begin
  1061. { GUID }
  1062. if AImplIntf.IntfDef.objecttype in [odt_interfacecom] then
  1063. begin
  1064. { label for GUID }
  1065. current_asmdata.getdatalabel(guidlabel);
  1066. rawdata.concat(cai_align.create(const_align(sizeof(pint))));
  1067. rawdata.concat(Tai_label.Create(guidlabel));
  1068. with AImplIntf.IntfDef.iidguid^ do
  1069. begin
  1070. rawdata.concat(Tai_const.Create_32bit(longint(D1)));
  1071. rawdata.concat(Tai_const.Create_16bit(D2));
  1072. rawdata.concat(Tai_const.Create_16bit(D3));
  1073. for i:=Low(D4) to High(D4) do
  1074. rawdata.concat(Tai_const.Create_8bit(D4[i]));
  1075. end;
  1076. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(guidlabel));
  1077. end
  1078. else
  1079. begin
  1080. { nil for Corba interfaces }
  1081. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1082. end;
  1083. { VTable }
  1084. current_asmdata.asmlists[al_globals].concat(Tai_const.Createname(intf_get_vtbl_name(AImplIntf.VtblImplIntf),0));
  1085. { IOffset field }
  1086. case AImplIntf.VtblImplIntf.IType of
  1087. etFieldValue,
  1088. etStandard:
  1089. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_pint(AImplIntf.VtblImplIntf.IOffset));
  1090. etVirtualMethodResult,
  1091. etStaticMethodResult:
  1092. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_pint(0));
  1093. else
  1094. internalerror(200802162);
  1095. end;
  1096. { IIDStr }
  1097. current_asmdata.getdatalabel(iidlabel);
  1098. rawdata.concat(cai_align.create(const_align(sizeof(pint))));
  1099. rawdata.concat(Tai_label.Create(iidlabel));
  1100. rawdata.concat(Tai_const.Create_8bit(length(AImplIntf.IntfDef.iidstr^)));
  1101. if AImplIntf.IntfDef.objecttype=odt_interfacecom then
  1102. rawdata.concat(Tai_string.Create(upper(AImplIntf.IntfDef.iidstr^)))
  1103. else
  1104. rawdata.concat(Tai_string.Create(AImplIntf.IntfDef.iidstr^));
  1105. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(iidlabel));
  1106. { IType }
  1107. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_pint(aint(AImplIntf.VtblImplIntf.IType)));
  1108. end;
  1109. function TVMTWriter.intf_write_table:TAsmLabel;
  1110. var
  1111. rawdata : TAsmList;
  1112. i : longint;
  1113. ImplIntf : TImplementedInterface;
  1114. intftablelab : tasmlabel;
  1115. begin
  1116. current_asmdata.getdatalabel(intftablelab);
  1117. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  1118. current_asmdata.asmlists[al_globals].concat(Tai_label.Create(intftablelab));
  1119. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_pint(_class.ImplementedInterfaces.count));
  1120. rawdata:=TAsmList.Create;
  1121. { Write vtbls }
  1122. for i:=0 to _class.ImplementedInterfaces.count-1 do
  1123. begin
  1124. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  1125. if ImplIntf.VtblImplIntf=ImplIntf then
  1126. intf_create_vtbl(rawdata,ImplIntf);
  1127. end;
  1128. { Write vtbl references }
  1129. for i:=0 to _class.ImplementedInterfaces.count-1 do
  1130. begin
  1131. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  1132. intf_gen_intf_ref(rawdata,ImplIntf);
  1133. end;
  1134. { Write interface table }
  1135. current_asmdata.asmlists[al_globals].concatlist(rawdata);
  1136. rawdata.free;
  1137. result:=intftablelab;
  1138. end;
  1139. { Write interface identifiers to the data section }
  1140. procedure TVMTWriter.writeinterfaceids;
  1141. var
  1142. i : longint;
  1143. s : string;
  1144. begin
  1145. if assigned(_class.iidguid) then
  1146. begin
  1147. s:=make_mangledname('IID',_class.owner,_class.objname^);
  1148. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  1149. new_section(current_asmdata.asmlists[al_globals],sec_rodata,s,const_align(sizeof(pint)));
  1150. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  1151. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_32bit(longint(_class.iidguid^.D1)));
  1152. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_16bit(_class.iidguid^.D2));
  1153. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_16bit(_class.iidguid^.D3));
  1154. for i:=Low(_class.iidguid^.D4) to High(_class.iidguid^.D4) do
  1155. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_8bit(_class.iidguid^.D4[i]));
  1156. end;
  1157. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  1158. s:=make_mangledname('IIDSTR',_class.owner,_class.objname^);
  1159. new_section(current_asmdata.asmlists[al_globals],sec_rodata,s,0);
  1160. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  1161. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_8bit(length(_class.iidstr^)));
  1162. current_asmdata.asmlists[al_globals].concat(Tai_string.Create(_class.iidstr^));
  1163. end;
  1164. procedure TVMTWriter.writevirtualmethods(List:TAsmList);
  1165. var
  1166. vmtpd : tprocdef;
  1167. vmtentry : pvmtentry;
  1168. i : longint;
  1169. procname : string;
  1170. {$ifdef vtentry}
  1171. hs : string;
  1172. {$endif vtentry}
  1173. begin
  1174. if not assigned(_class.VMTEntries) then
  1175. exit;
  1176. for i:=0 to _class.VMTEntries.Count-1 do
  1177. begin
  1178. vmtentry:=pvmtentry(_class.vmtentries[i]);
  1179. vmtpd:=vmtentry^.procdef;
  1180. { safety checks }
  1181. if not(po_virtualmethod in vmtpd.procoptions) then
  1182. internalerror(200611082);
  1183. if vmtpd.extnumber<>i then
  1184. internalerror(200611083);
  1185. if (po_abstractmethod in vmtpd.procoptions) then
  1186. procname:='FPC_ABSTRACTERROR'
  1187. else if not wpoinfomanager.optimized_name_for_vmt(_class,vmtpd,procname) then
  1188. procname:=vmtpd.mangledname;
  1189. List.concat(Tai_const.createname(procname,0));
  1190. {$ifdef vtentry}
  1191. hs:='VTENTRY'+'_'+_class.vmt_mangledname+'$$'+tostr(_class.vmtmethodoffset(i) div sizeof(pint));
  1192. current_asmdata.asmlists[al_globals].concat(tai_symbol.CreateName(hs,AT_DATA,0));
  1193. {$endif vtentry}
  1194. end;
  1195. end;
  1196. procedure TVMTWriter.writevmt;
  1197. var
  1198. methodnametable,intmessagetable,
  1199. strmessagetable,classnamelabel,
  1200. fieldtablelabel : tasmlabel;
  1201. {$ifdef WITHDMT}
  1202. dmtlabel : tasmlabel;
  1203. {$endif WITHDMT}
  1204. interfacetable : tasmlabel;
  1205. {$ifdef vtentry}
  1206. hs: string;
  1207. {$endif vtentry}
  1208. begin
  1209. {$ifdef WITHDMT}
  1210. dmtlabel:=gendmt;
  1211. {$endif WITHDMT}
  1212. { write tables for classes, this must be done before the actual
  1213. class is written, because we need the labels defined }
  1214. if is_class(_class) then
  1215. begin
  1216. current_asmdata.getdatalabel(classnamelabel);
  1217. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  1218. new_section(current_asmdata.asmlists[al_globals],sec_rodata,classnamelabel.name,const_align(sizeof(pint)));
  1219. { interface table }
  1220. if _class.ImplementedInterfaces.count>0 then
  1221. interfacetable:=intf_write_table;
  1222. methodnametable:=genpublishedmethodstable;
  1223. fieldtablelabel:=generate_field_table;
  1224. { write class name }
  1225. current_asmdata.asmlists[al_globals].concat(Tai_label.Create(classnamelabel));
  1226. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_8bit(length(_class.objrealname^)));
  1227. current_asmdata.asmlists[al_globals].concat(Tai_string.Create(_class.objrealname^));
  1228. { generate message and dynamic tables }
  1229. if (oo_has_msgstr in _class.objectoptions) then
  1230. strmessagetable:=genstrmsgtab;
  1231. if (oo_has_msgint in _class.objectoptions) then
  1232. intmessagetable:=genintmsgtab;
  1233. end;
  1234. { write debug info }
  1235. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  1236. new_section(current_asmdata.asmlists[al_globals],sec_rodata,_class.vmt_mangledname,const_align(sizeof(pint)));
  1237. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(_class.vmt_mangledname,AT_DATA,0));
  1238. { determine the size with symtable.datasize, because }
  1239. { size gives back 4 for classes }
  1240. current_asmdata.asmlists[al_globals].concat(Tai_const.Create(aitconst_ptr,tObjectSymtable(_class.symtable).datasize));
  1241. current_asmdata.asmlists[al_globals].concat(Tai_const.Create(aitconst_ptr,-int64(tObjectSymtable(_class.symtable).datasize)));
  1242. {$ifdef WITHDMT}
  1243. if _class.classtype=ct_object then
  1244. begin
  1245. if assigned(dmtlabel) then
  1246. current_asmdata.asmlists[al_globals].concat(Tai_const_symbol.Create(dmtlabel)))
  1247. else
  1248. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_ptr(0));
  1249. end;
  1250. {$endif WITHDMT}
  1251. { write pointer to parent VMT, this isn't implemented in TP }
  1252. { but this is not used in FPC ? (PM) }
  1253. { it's not used yet, but the delphi-operators as and is need it (FK) }
  1254. { it is not written for parents that don't have any vmt !! }
  1255. if assigned(_class.childof) and
  1256. (oo_has_vmt in _class.childof.objectoptions) then
  1257. current_asmdata.asmlists[al_globals].concat(Tai_const.Createname(_class.childof.vmt_mangledname,0))
  1258. else
  1259. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1260. { write extended info for classes, for the order see rtl/inc/objpash.inc }
  1261. if is_class(_class) then
  1262. begin
  1263. { pointer to class name string }
  1264. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(classnamelabel));
  1265. { pointer to dynamic table or nil }
  1266. if (oo_has_msgint in _class.objectoptions) then
  1267. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(intmessagetable))
  1268. else
  1269. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1270. { pointer to method table or nil }
  1271. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(methodnametable));
  1272. { pointer to field table }
  1273. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(fieldtablelabel));
  1274. { pointer to type info of published section }
  1275. if (oo_can_have_published in _class.objectoptions) then
  1276. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(RTTIWriter.get_rtti_label(_class,fullrtti)))
  1277. else
  1278. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1279. { inittable for con-/destruction }
  1280. if _class.members_need_inittable then
  1281. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(RTTIWriter.get_rtti_label(_class,initrtti)))
  1282. else
  1283. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1284. { auto table }
  1285. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1286. { interface table }
  1287. if _class.ImplementedInterfaces.count>0 then
  1288. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(interfacetable))
  1289. else if _class.implements_any_interfaces then
  1290. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil))
  1291. else
  1292. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(current_asmdata.RefAsmSymbol('FPC_EMPTYINTF')));
  1293. { table for string messages }
  1294. if (oo_has_msgstr in _class.objectoptions) then
  1295. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(strmessagetable))
  1296. else
  1297. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1298. end;
  1299. { write virtual methods }
  1300. writevirtualmethods(current_asmdata.asmlists[al_globals]);
  1301. current_asmdata.asmlists[al_globals].concat(Tai_const.create(aitconst_ptr,0));
  1302. { write the size of the VMT }
  1303. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname(_class.vmt_mangledname));
  1304. {$ifdef vtentry}
  1305. { write vtinherit symbol to notify the linker of the class inheritance tree }
  1306. hs:='VTINHERIT'+'_'+_class.vmt_mangledname+'$$';
  1307. if assigned(_class.childof) then
  1308. hs:=hs+_class.childof.vmt_mangledname
  1309. else
  1310. hs:=hs+_class.vmt_mangledname;
  1311. current_asmdata.asmlists[al_globals].concat(tai_symbol.CreateName(hs,AT_DATA,0));
  1312. {$endif vtentry}
  1313. end;
  1314. end.