2
0

nobj.pas 60 KB

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