nobj.pas 58 KB

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