nobj.pas 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730
  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; out overridesclasshelper: boolean):boolean;
  33. procedure add_new_vmt_entry(pd:tprocdef; allowoverridingmethod: boolean);
  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. procedure generate_vmt;
  44. procedure build_interface_mappings;
  45. end;
  46. type
  47. pprocdeftree = ^tprocdeftree;
  48. tprocdeftree = record
  49. data : tprocdef;
  50. nl : tasmlabel;
  51. l,r : pprocdeftree;
  52. end;
  53. TVMTWriter=class
  54. private
  55. _Class : tobjectdef;
  56. { message tables }
  57. root : pprocdeftree;
  58. procedure disposeprocdeftree(p : pprocdeftree);
  59. procedure insertmsgint(p:TObject;arg:pointer);
  60. procedure insertmsgstr(p:TObject;arg:pointer);
  61. procedure insertint(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  62. procedure insertstr(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  63. function RedirectToEmpty(procdef: tprocdef): boolean;
  64. procedure writenames(list : TAsmList;p : pprocdeftree);
  65. procedure writeintentry(list : TAsmList;p : pprocdeftree);
  66. procedure writestrentry(list : TAsmList;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(list : TAsmList):TAsmLabel;
  83. { generates the message tables for a class }
  84. function genstrmsgtab(list : TAsmList) : tasmlabel;
  85. function genintmsgtab(list : TAsmList) : tasmlabel;
  86. function genpublishedmethodstable(list : TAsmList) : tasmlabel;
  87. function generate_field_table(list : TAsmList) : 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. { write the VMT to al_globals }
  95. procedure writevmt;
  96. procedure writeinterfaceids;
  97. end;
  98. implementation
  99. uses
  100. SysUtils,
  101. globals,verbose,systems,
  102. node,procinfo,
  103. symbase,symtable,symconst,symtype,defcmp,
  104. cgbase,parabase,paramgr,
  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. procedure TVMTBuilder.add_new_vmt_entry(pd:tprocdef; allowoverridingmethod: boolean);
  118. var
  119. i : longint;
  120. vmtentry : pvmtentry;
  121. vmtpd : tprocdef;
  122. begin
  123. { new entry is needed, override was not possible }
  124. { Allowed when overriding a category method for a parent class in a
  125. descendent Objective-C class }
  126. if not allowoverridingmethod and
  127. (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; out overridesclasshelper: boolean):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. srsym : tsym;
  210. st : tsymtable;
  211. // returns true if we can stop checking, false if we have to continue
  212. function found_entry(var vmtpd: tprocdef; var vmtentryvis: tvisibility; updatevalues: boolean): boolean;
  213. {$ifdef jvm}
  214. var
  215. javanewtreeok: boolean;
  216. {$endif jvm}
  217. begin
  218. result:=false;
  219. { ignore hidden entries (e.g. virtual overridden by a static) that are not visible anymore }
  220. if vmtentryvis=vis_hidden then
  221. exit;
  222. { ignore different names }
  223. if vmtpd.procsym.name<>pd.procsym.name then
  224. exit;
  225. { hide private methods that are not visible anymore. For this check we
  226. must override the visibility with the highest value in the override chain.
  227. This is required for case (see tw3292) with protected-private-protected where the
  228. same vmtentry is used (PFV) }
  229. if not is_visible_for_object(vmtpd.owner,vmtentryvis,_class) then
  230. exit;
  231. { inherit overload }
  232. if (po_overload in vmtpd.procoptions) then
  233. begin
  234. include(pd.procoptions,po_overload);
  235. pdoverload:=true;
  236. end;
  237. { compare parameter types only, no specifiers yet }
  238. hasequalpara:=(compare_paras(vmtpd.paras,pd.paras,cp_none,[cpo_ignoreuniv,cpo_ignorehidden])>=te_equal);
  239. { check that we are not trying to override a final method }
  240. { in Java, new virtual inheritance trees can never be started ->
  241. treat all methods as "overriding" in the context of this check
  242. (Java does check whether the mangled names are identical, so if they
  243. are not we can stil get away with it) }
  244. if (po_finalmethod in vmtpd.procoptions) and
  245. hasequalpara and
  246. ((po_overridingmethod in pd.procoptions) or
  247. (is_javaclass(_class) and
  248. (pd.mangledname=vmtpd.mangledname))) and
  249. (is_class(_class) or is_objectpascal_helper(_class) or is_javaclass(_class)) then
  250. MessagePos1(pd.fileinfo,parser_e_final_can_no_be_overridden,pd.fullprocname(false))
  251. else
  252. { old definition has virtual
  253. new definition has no virtual or override }
  254. if (po_virtualmethod in vmtpd.procoptions) and
  255. (
  256. not(po_virtualmethod in pd.procoptions) or
  257. (
  258. { new one does not have reintroduce in case of an objccategory }
  259. (is_objccategory(_class) and
  260. not(po_reintroduce in pd.procoptions)) or
  261. { new one does not have override in case of objpas/objc/java class/intf/proto }
  262. ((is_class_or_interface_or_objc_or_java(_class) or is_objectpascal_helper(_class)) and
  263. not is_objccategory(_class) and
  264. not(po_overridingmethod in pd.procoptions)
  265. )
  266. )
  267. ) then
  268. begin
  269. if (
  270. not(pdoverload or hasoverloads) or
  271. hasequalpara
  272. ) then
  273. begin
  274. {$ifdef jvm}
  275. { if the mangled names are different, the inheritance trees
  276. are different too in Java; exception: when the parent method
  277. is a virtual class method or virtual constructor, because
  278. those are looked up dynamicall by name }
  279. javanewtreeok:=
  280. is_java_class_or_interface(_class) and
  281. (pd.jvmmangledbasename(false)<>vmtpd.jvmmangledbasename(false)) and
  282. ((vmtpd.proctypeoption<>potype_constructor) and
  283. not(po_staticmethod in vmtpd.procoptions));
  284. {$endif}
  285. if not(po_reintroduce in pd.procoptions) and
  286. not(po_java_nonvirtual in vmtpd.procoptions) then
  287. if not(is_objc_class_or_protocol(_class))
  288. {$ifdef jvm}
  289. and (not is_java_class_or_interface(_class) or
  290. javanewtreeok)
  291. {$endif jvm}
  292. then
  293. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false))
  294. else
  295. begin
  296. { In Objective-C, you cannot create a new VMT entry to
  297. start a new inheritance tree. We therefore give an
  298. error when the class is implemented in Pascal, to
  299. avoid confusion due to things working differently
  300. with Object Pascal classes.
  301. In case of external classes, we only give a hint,
  302. because requiring override everywhere may make
  303. automated header translation tools too complex.
  304. The same goes for Java. }
  305. {$ifndef jvm}
  306. if hasequalpara then
  307. {$endif}
  308. begin
  309. if not(oo_is_external in _class.objectoptions) then
  310. if not is_objccategory(_class) then
  311. MessagePos1(pd.fileinfo,parser_e_must_use_override,FullTypeName(tdef(vmtpd.owner.defowner),nil))
  312. else
  313. MessagePos1(pd.fileinfo,parser_e_must_use_reintroduce_objc,FullTypeName(tdef(vmtpd.owner.defowner),nil))
  314. { there may be a lot of these in auto-translated
  315. headers, so only calculate the fulltypename if
  316. the hint will be shown }
  317. else if CheckVerbosity(V_Hint) then
  318. if not is_objccategory(_class) then
  319. MessagePos1(pd.fileinfo,parser_h_should_use_override,FullTypeName(tdef(vmtpd.owner.defowner),nil))
  320. else
  321. MessagePos1(pd.fileinfo,parser_h_should_use_reintroduce_objc,FullTypeName(tdef(vmtpd.owner.defowner),nil));
  322. end;
  323. { no new entry, but copy the message name if any from
  324. the procdef in the parent class }
  325. if not is_objc_class_or_protocol(_class) or
  326. hasequalpara then
  327. check_msg_str(vmtpd,pd);
  328. if updatevalues then
  329. begin
  330. { in case of Java, copy the real name from the parent,
  331. since overriding "Destroy" with "destroy" is not
  332. going to work very well }
  333. if is_java_class_or_interface(_class) and
  334. (pd.procsym.realname<>vmtpd.procsym.realname) then
  335. pd.procsym.realname:=vmtpd.procsym.realname;
  336. { in case we are overriding an abstract method,
  337. decrease the number of abstract methods in this class }
  338. if (po_abstractmethod in vmtpd.procoptions) then
  339. dec(tobjectdef(pd.owner.defowner).abstractcnt);
  340. if (vmtpd.extnumber<>i) then
  341. internalerror(2011083101);
  342. pd.extnumber:=vmtpd.extnumber;
  343. vmtpd:=pd;
  344. end;
  345. result:=true;
  346. exit;
  347. {$ifdef jvm}
  348. end
  349. else
  350. if not javanewtreeok and
  351. is_java_class_or_interface(_class) then
  352. begin
  353. { mangled names are the same -> can only override }
  354. MessagePos1(pd.fileinfo,parser_e_must_use_override,FullTypeName(tdef(vmtpd.owner.defowner),nil))
  355. {$endif jvm}
  356. end;
  357. { disable/hide old VMT entry }
  358. if updatevalues then
  359. vmtentryvis:=vis_hidden;
  360. end;
  361. end
  362. { both are virtual? }
  363. else if (po_virtualmethod in pd.procoptions) and
  364. (po_virtualmethod in vmtpd.procoptions) then
  365. begin
  366. { same parameter and return types (parameter specifiers will be checked below) }
  367. if hasequalpara and
  368. compatible_childmethod_resultdef(vmtpd.returndef,pd.returndef) then
  369. begin
  370. { inherite calling convention when it was explicit and the
  371. current definition has none explicit set }
  372. if (po_hascallingconvention in vmtpd.procoptions) and
  373. not(po_hascallingconvention in pd.procoptions) then
  374. begin
  375. pd.proccalloption:=vmtpd.proccalloption;
  376. include(pd.procoptions,po_hascallingconvention);
  377. end;
  378. { All parameter specifiers and some procedure the flags have to match
  379. except abstract and override }
  380. if (compare_paras(vmtpd.paras,pd.paras,cp_all,[cpo_ignoreuniv,cpo_ignorehidden])<te_equal) or
  381. (vmtpd.proccalloption<>pd.proccalloption) or
  382. (vmtpd.proctypeoption<>pd.proctypeoption) or
  383. ((vmtpd.procoptions*po_comp)<>(pd.procoptions*po_comp)) then
  384. begin
  385. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  386. tprocsym(vmtpd.procsym).write_parameter_lists(pd);
  387. end;
  388. check_msg_str(vmtpd,pd);
  389. { Give a note if the new visibility is lower. For a higher
  390. visibility update the vmt info }
  391. if vmtentryvis>pd.visibility then
  392. begin
  393. if po_auto_raised_visibility in vmtpd.procoptions then
  394. begin
  395. if updatevalues then
  396. pd.visibility:=vmtentryvis;
  397. end
  398. else
  399. {$ifdef jvm}
  400. MessagePos4(pd.fileinfo,parser_e_method_lower_visibility,
  401. {$else jvm}
  402. MessagePos4(pd.fileinfo,parser_n_ignore_lower_visibility,
  403. {$endif jvm}
  404. pd.fullprocname(false),
  405. visibilityname[pd.visibility],tobjectdef(vmtpd.owner.defowner).objrealname^,visibilityname[vmtentryvis])
  406. end
  407. else if pd.visibility>vmtentryvis then
  408. begin
  409. if updatevalues then
  410. vmtentryvis:=pd.visibility;
  411. end;
  412. { override old virtual method in VMT }
  413. if updatevalues then
  414. begin
  415. { in case we are overriding an abstract method,
  416. decrease the number of abstract methods in this class }
  417. if (po_overridingmethod in pd.procoptions) and
  418. (po_abstractmethod in vmtpd.procoptions) then
  419. dec(tobjectdef(pd.owner.defowner).abstractcnt);
  420. if (vmtpd.extnumber<>i) then
  421. internalerror(200611084);
  422. pd.extnumber:=vmtpd.extnumber;
  423. { in case of Java, copy the real name from the parent,
  424. since overriding "Destroy" with "destroy" is not
  425. going to work very well }
  426. if is_java_class_or_interface(_class) and
  427. (pd.procsym.realname<>vmtpd.procsym.realname) then
  428. pd.procsym.realname:=vmtpd.procsym.realname;
  429. vmtpd:=pd;
  430. end;
  431. result:=true;
  432. exit;
  433. end
  434. { different parameters }
  435. else
  436. begin
  437. { when we got an override directive then can search futher for
  438. the procedure to override.
  439. If we are starting a new virtual tree then hide the old tree }
  440. if not(po_overridingmethod in pd.procoptions) and
  441. not(pdoverload or hasoverloads) then
  442. begin
  443. if not(po_reintroduce in pd.procoptions) then
  444. begin
  445. if not is_object(_class) and
  446. not is_objc_class_or_protocol(_class) and
  447. not is_java_class_or_interface(_class) then
  448. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false))
  449. else
  450. { objects don't allow starting a new virtual tree
  451. and neither do Objective-C or Java }
  452. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,vmtpd.fullprocname(false));
  453. end;
  454. { disable/hide old VMT entry }
  455. if updatevalues then
  456. vmtentryvis:=vis_hidden;
  457. end;
  458. end;
  459. end;
  460. end;
  461. function found_category_method(st: tsymtable): boolean;
  462. var
  463. entrycount: longint;
  464. cat: tobjectdef;
  465. vmtpd: tprocdef;
  466. vmtvis: tvisibility;
  467. begin
  468. result:=false;
  469. if is_objccategory(tdef(st.defowner)) then
  470. begin
  471. cat:=tobjectdef(st.defowner);
  472. { go through all of the category's methods to find the
  473. vmtentry corresponding to the procdef we are handling }
  474. for entrycount:=0 to cat.vmtentries.Count-1 do
  475. begin
  476. vmtpd:=pvmtentry(cat.vmtentries[entrycount])^.procdef;
  477. vmtvis:=pvmtentry(cat.vmtentries[entrycount])^.visibility;
  478. { don't change the vmtentry of the category }
  479. if found_entry(vmtpd,vmtvis,false) then
  480. begin
  481. result:=true;
  482. exit;
  483. end;
  484. end;
  485. end;
  486. end;
  487. begin
  488. result:=false;
  489. overridesclasshelper:=false;
  490. { Load other values for easier readability }
  491. hasoverloads:=(tprocsym(pd.procsym).ProcdefList.Count>1);
  492. pdoverload:=(po_overload in pd.procoptions);
  493. { compare with all stored definitions }
  494. for i:=0 to _class.vmtentries.Count-1 do
  495. begin
  496. if found_entry(pvmtentry(_class.vmtentries[i])^.procdef, pvmtentry(_class.vmtentries[i])^.visibility,true) then
  497. exit;
  498. end;
  499. { in case of Objective-C, also check the categories that apply to this
  500. class' *parent* for methods to override (don't allow class X to
  501. "override" a method added by a category to class X itself, since in
  502. that case the category method will in fact replace class X'
  503. "overriding" method }
  504. if is_objcclass(_class) and
  505. assigned(_class.childof) and
  506. search_objc_helper(_class.childof,pd.procsym.name,srsym,st) then
  507. begin
  508. overridesclasshelper:=found_category_method(st);
  509. end;
  510. { No entry found, we need to create a new entry }
  511. result:=true;
  512. end;
  513. function TVMTBuilder.intf_search_procdef_by_name(proc: tprocdef;const name: string): tprocdef;
  514. const
  515. po_comp = [po_classmethod,po_staticmethod,po_interrupt,po_iocheck,po_msgint,
  516. po_exports,po_varargs,po_explicitparaloc,po_nostackframe];
  517. var
  518. implprocdef : Tprocdef;
  519. i: cardinal;
  520. hclass : tobjectdef;
  521. hashedid : THashedIDString;
  522. srsym : tsym;
  523. begin
  524. result:=nil;
  525. hashedid.id:=name;
  526. hclass:=_class;
  527. while assigned(hclass) do
  528. begin
  529. srsym:=tsym(hclass.symtable.FindWithHash(hashedid));
  530. if assigned(srsym) and
  531. (srsym.typ=procsym) then
  532. begin
  533. for i:=0 to Tprocsym(srsym).ProcdefList.Count-1 do
  534. begin
  535. implprocdef:=tprocdef(tprocsym(srsym).ProcdefList[i]);
  536. if (implprocdef.procsym=tprocsym(srsym)) and
  537. (compare_paras(proc.paras,implprocdef.paras,cp_all,[cpo_ignorehidden,cpo_ignoreuniv])>=te_equal) and
  538. (compare_defs(proc.returndef,implprocdef.returndef,nothingn)>=te_equal) and
  539. (proc.proccalloption=implprocdef.proccalloption) and
  540. (proc.proctypeoption=implprocdef.proctypeoption) and
  541. ((proc.procoptions*po_comp)=((implprocdef.procoptions+[po_virtualmethod])*po_comp)) and
  542. check_msg_str(proc,implprocdef) then
  543. begin
  544. { does the interface increase the visibility of the
  545. implementing method? }
  546. if implprocdef.visibility<proc.visibility then
  547. {$ifdef jvm}
  548. MessagePos2(implprocdef.fileinfo,type_e_interface_lower_visibility,proc.fullprocname(false),implprocdef.fullprocname(false));
  549. {$else}
  550. MessagePos2(implprocdef.fileinfo,type_w_interface_lower_visibility,proc.fullprocname(false),implprocdef.fullprocname(false));
  551. {$endif}
  552. result:=implprocdef;
  553. exit;
  554. end;
  555. end;
  556. end;
  557. hclass:=hclass.childof;
  558. end;
  559. end;
  560. procedure TVMTBuilder.intf_get_procdefs(ImplIntf:TImplementedInterface;IntfDef:TObjectDef);
  561. var
  562. i : longint;
  563. def : tdef;
  564. hs,
  565. prefix,
  566. mappedname: string;
  567. implprocdef: tprocdef;
  568. begin
  569. prefix:=ImplIntf.IntfDef.symtable.name^+'.';
  570. for i:=0 to IntfDef.symtable.DefList.Count-1 do
  571. begin
  572. def:=tdef(IntfDef.symtable.DefList[i]);
  573. if assigned(def) and
  574. (def.typ=procdef) then
  575. begin
  576. { Find implementing procdef
  577. 1. Check for mapped name
  578. 2. Use symbol name, but only if there's no mapping,
  579. or we're processing ancestor of interface.
  580. When modifying this code, ensure that webtbs/tw11862, webtbs/tw4950
  581. and webtbf/tw19591 stay correct. }
  582. implprocdef:=nil;
  583. hs:=prefix+tprocdef(def).procsym.name;
  584. mappedname:=ImplIntf.GetMapping(hs);
  585. if mappedname<>'' then
  586. implprocdef:=intf_search_procdef_by_name(tprocdef(def),mappedname);
  587. if not assigned(implprocdef) then
  588. if (mappedname='') or (ImplIntf.IntfDef<>IntfDef) then
  589. implprocdef:=intf_search_procdef_by_name(tprocdef(def),tprocdef(def).procsym.name);
  590. { Add procdef to the implemented interface }
  591. if assigned(implprocdef) then
  592. begin
  593. if (tobjectdef(implprocdef.struct).objecttype<>odt_objcclass) then
  594. begin
  595. { in case of Java, copy the real name from the parent,
  596. since overriding "Destroy" with "destroy" is not
  597. going to work very well }
  598. if is_javaclass(implprocdef.struct) and
  599. (implprocdef.procsym.realname<>tprocdef(def).procsym.realname) then
  600. implprocdef.procsym.realname:=tprocdef(def).procsym.realname;
  601. ImplIntf.AddImplProc(implprocdef);
  602. end
  603. else
  604. begin
  605. { If no message name has been specified for the method
  606. in the objcclass, copy it from the protocol
  607. definition. }
  608. if not(po_msgstr in tprocdef(def).procoptions) then
  609. begin
  610. include(tprocdef(def).procoptions,po_msgstr);
  611. implprocdef.messageinf.str:=stringdup(tprocdef(def).messageinf.str^);
  612. end
  613. else
  614. begin
  615. { If a message name has been specified in the
  616. objcclass, it has to match the message name in the
  617. protocol definition. }
  618. if (implprocdef.messageinf.str^<>tprocdef(def).messageinf.str^) then
  619. MessagePos2(implprocdef.fileinfo,parser_e_objc_message_name_changed,tprocdef(def).messageinf.str^,implprocdef.messageinf.str^);
  620. end;
  621. end;
  622. end
  623. else
  624. if (ImplIntf.IType=etStandard) and
  625. not(po_optional in tprocdef(def).procoptions) then
  626. MessagePos1(_Class.typesym.fileinfo,sym_e_no_matching_implementation_found,tprocdef(def).fullprocname(false));
  627. end;
  628. end;
  629. end;
  630. procedure TVMTBuilder.intf_get_procdefs_recursive(ImplIntf:TImplementedInterface;IntfDef:TObjectDef);
  631. begin
  632. if assigned(IntfDef.childof) then
  633. intf_get_procdefs_recursive(ImplIntf,IntfDef.childof);
  634. intf_get_procdefs(ImplIntf,IntfDef);
  635. end;
  636. procedure TVMTBuilder.prot_get_procdefs_recursive(ImplProt:TImplementedInterface;ProtDef:TObjectDef);
  637. var
  638. i: longint;
  639. begin
  640. { don't check the same protocol twice }
  641. if handledprotocols.IndexOf(ProtDef)<>-1 then
  642. exit;
  643. handledprotocols.add(ProtDef);
  644. for i:=0 to ProtDef.ImplementedInterfaces.count-1 do
  645. prot_get_procdefs_recursive(ImplProt,TImplementedInterface(ProtDef.ImplementedInterfaces[i]).intfdef);
  646. intf_get_procdefs(ImplProt,ProtDef);
  647. end;
  648. procedure TVMTBuilder.intf_optimize_vtbls;
  649. type
  650. tcompintfentry = record
  651. weight: longint;
  652. compintf: longint;
  653. end;
  654. { Max 1000 interface in the class header interfaces it's enough imho }
  655. tcompintfs = array[0..1000] of tcompintfentry;
  656. pcompintfs = ^tcompintfs;
  657. tequals = array[0..1000] of longint;
  658. pequals = ^tequals;
  659. timpls = array[0..1000] of longint;
  660. pimpls = ^timpls;
  661. var
  662. aequals: pequals;
  663. compats: pcompintfs;
  664. impls: pimpls;
  665. ImplIntfCount,
  666. w,i,j,k: longint;
  667. ImplIntfI,
  668. ImplIntfJ : TImplementedInterface;
  669. cij: boolean;
  670. cji: boolean;
  671. begin
  672. ImplIntfCount:=_class.ImplementedInterfaces.count;
  673. if ImplIntfCount>=High(tequals) then
  674. Internalerror(200006135);
  675. getmem(compats,sizeof(tcompintfentry)*ImplIntfCount);
  676. getmem(aequals,sizeof(longint)*ImplIntfCount);
  677. getmem(impls,sizeof(longint)*ImplIntfCount);
  678. filldword(compats^,(sizeof(tcompintfentry) div sizeof(dword))*ImplIntfCount,dword(-1));
  679. filldword(aequals^,ImplIntfCount,dword(-1));
  680. filldword(impls^,ImplIntfCount,dword(-1));
  681. { ismergepossible is a containing relation
  682. meaning of ismergepossible(a,b,w) =
  683. if implementorfunction map of a is contained implementorfunction map of b
  684. imp(a,b) and imp(b,c) => imp(a,c) ; imp(a,b) and imp(b,a) => a == b
  685. }
  686. { the order is very important for correct allocation }
  687. for i:=0 to ImplIntfCount-1 do
  688. begin
  689. for j:=i+1 to ImplIntfCount-1 do
  690. begin
  691. ImplIntfI:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  692. ImplIntfJ:=TImplementedInterface(_class.ImplementedInterfaces[j]);
  693. cij:=ImplIntfI.IsImplMergePossible(ImplIntfJ,w);
  694. cji:=ImplIntfJ.IsImplMergePossible(ImplIntfI,w);
  695. if cij and cji then { i equal j }
  696. begin
  697. { get minimum index of equal }
  698. if aequals^[j]=-1 then
  699. aequals^[j]:=i;
  700. end
  701. else if cij then
  702. begin
  703. { get minimum index of maximum weight }
  704. if compats^[i].weight<w then
  705. begin
  706. compats^[i].weight:=w;
  707. compats^[i].compintf:=j;
  708. end;
  709. end
  710. else if cji then
  711. begin
  712. { get minimum index of maximum weight }
  713. if (compats^[j].weight<w) then
  714. begin
  715. compats^[j].weight:=w;
  716. compats^[j].compintf:=i;
  717. end;
  718. end;
  719. end;
  720. end;
  721. { Reset, no replacements by default }
  722. for i:=0 to ImplIntfCount-1 do
  723. impls^[i]:=i;
  724. { Replace vtbls when equal or compat, repeat
  725. until there are no replacements possible anymore. This is
  726. needed for the cases like:
  727. First loop: 2->3, 3->1
  728. Second loop: 2->1 (because 3 was replaced with 1)
  729. }
  730. repeat
  731. k:=0;
  732. for i:=0 to ImplIntfCount-1 do
  733. begin
  734. if compats^[impls^[i]].compintf<>-1 then
  735. impls^[i]:=compats^[impls^[i]].compintf
  736. else if aequals^[impls^[i]]<>-1 then
  737. impls^[i]:=aequals^[impls^[i]]
  738. else
  739. inc(k);
  740. end;
  741. until k=ImplIntfCount;
  742. { Update the VtblImplIntf }
  743. for i:=0 to ImplIntfCount-1 do
  744. begin
  745. ImplIntfI:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  746. ImplIntfI.VtblImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[impls^[i]]);
  747. end;
  748. freemem(compats);
  749. freemem(aequals);
  750. freemem(impls);
  751. end;
  752. procedure TVMTBuilder.intf_allocate_vtbls;
  753. var
  754. i : longint;
  755. ImplIntf : TImplementedInterface;
  756. begin
  757. { Allocation vtbl space }
  758. for i:=0 to _class.ImplementedInterfaces.count-1 do
  759. begin
  760. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  761. { if it implements itself and if it's not implemented by delegation }
  762. if (ImplIntf.VtblImplIntf=ImplIntf) and (ImplIntf.IType=etStandard) then
  763. begin
  764. { allocate a pointer in the object memory }
  765. with tObjectSymtable(_class.symtable) do
  766. begin
  767. datasize:=align(datasize,sizeof(pint));
  768. ImplIntf.Ioffset:=datasize;
  769. datasize:=datasize+sizeof(pint);
  770. end;
  771. end;
  772. end;
  773. { Update ioffset of current interface with the ioffset from
  774. the interface that is reused to implements this interface }
  775. for i:=0 to _class.ImplementedInterfaces.count-1 do
  776. begin
  777. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  778. if ImplIntf.VtblImplIntf<>ImplIntf then
  779. ImplIntf.IOffset:=ImplIntf.VtblImplIntf.IOffset;
  780. end;
  781. end;
  782. procedure TVMTBuilder.generate_vmt;
  783. var
  784. i : longint;
  785. def : tdef;
  786. old_current_structdef : tabstractrecorddef;
  787. overridesclasshelper : boolean;
  788. begin
  789. old_current_structdef:=current_structdef;
  790. current_structdef:=_class;
  791. _class.resetvmtentries;
  792. { inherit (copy) VMT from parent object }
  793. if assigned(_class.childof) then
  794. begin
  795. if not assigned(_class.childof.vmtentries) then
  796. internalerror(200810281);
  797. _class.copyvmtentries(_class.childof);
  798. end;
  799. { process all procdefs, we must process the defs to
  800. keep the same order as that is written in the source
  801. to be compatible with the indexes in the interface vtable (PFV) }
  802. for i:=0 to _class.symtable.DefList.Count-1 do
  803. begin
  804. def:=tdef(_class.symtable.DefList[i]);
  805. if def.typ=procdef then
  806. begin
  807. { VMT entry }
  808. if is_new_vmt_entry(tprocdef(def),overridesclasshelper) then
  809. add_new_vmt_entry(tprocdef(def),overridesclasshelper);
  810. end;
  811. end;
  812. build_interface_mappings;
  813. if assigned(_class.ImplementedInterfaces) and
  814. not(is_objc_class_or_protocol(_class)) and
  815. not(is_java_class_or_interface(_class)) then
  816. begin
  817. { Optimize interface tables to reuse wrappers }
  818. intf_optimize_vtbls;
  819. { Allocate interface tables }
  820. intf_allocate_vtbls;
  821. end;
  822. current_structdef:=old_current_structdef;
  823. end;
  824. procedure TVMTBuilder.build_interface_mappings;
  825. var
  826. ImplIntf : TImplementedInterface;
  827. i: longint;
  828. begin
  829. { Find Procdefs implementing the interfaces (both Objective-C protocols
  830. and Java interfaces can have multiple parent interfaces, but in that
  831. case obviously no implementations are required) }
  832. if assigned(_class.ImplementedInterfaces) and
  833. not(_class.objecttype in [odt_objcprotocol,odt_interfacejava]) and
  834. // abstract java classes do not have to implement all interface
  835. // methods. todo: check that non-abstract descendents do!
  836. not((_class.objecttype=odt_javaclass) and (oo_is_abstract in _class.objectoptions)) then
  837. begin
  838. { Collect implementor functions into the tImplementedInterface.procdefs }
  839. case _class.objecttype of
  840. odt_class:
  841. begin
  842. for i:=0 to _class.ImplementedInterfaces.count-1 do
  843. begin
  844. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  845. intf_get_procdefs_recursive(ImplIntf,ImplIntf.IntfDef)
  846. end;
  847. end;
  848. odt_objcclass,
  849. odt_javaclass:
  850. begin
  851. { Object Pascal interfaces are afterwards optimized via the
  852. intf_optimize_vtbls() method, but we can't do this for
  853. protocols/Java interfaces -> check for duplicates here
  854. already. }
  855. handledprotocols:=tfpobjectlist.create(false);
  856. for i:=0 to _class.ImplementedInterfaces.count-1 do
  857. begin
  858. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  859. prot_get_procdefs_recursive(ImplIntf,ImplIntf.IntfDef);
  860. end;
  861. handledprotocols.free;
  862. end
  863. else
  864. internalerror(2009091801);
  865. end
  866. end;
  867. end;
  868. {*****************************************************************************
  869. TVMTWriter
  870. *****************************************************************************}
  871. constructor TVMTWriter.create(c:tobjectdef);
  872. begin
  873. inherited Create;
  874. _Class:=c;
  875. end;
  876. {**************************************
  877. Message Tables
  878. **************************************}
  879. procedure TVMTWriter.disposeprocdeftree(p : pprocdeftree);
  880. begin
  881. if assigned(p^.l) then
  882. disposeprocdeftree(p^.l);
  883. if assigned(p^.r) then
  884. disposeprocdeftree(p^.r);
  885. dispose(p);
  886. end;
  887. procedure TVMTWriter.insertint(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  888. begin
  889. if at=nil then
  890. begin
  891. at:=p;
  892. inc(count);
  893. end
  894. else
  895. begin
  896. if p^.data.messageinf.i<at^.data.messageinf.i then
  897. insertint(p,at^.l,count)
  898. else if p^.data.messageinf.i>at^.data.messageinf.i then
  899. insertint(p,at^.r,count)
  900. else
  901. Message1(parser_e_duplicate_message_label,tostr(p^.data.messageinf.i));
  902. end;
  903. end;
  904. procedure TVMTWriter.insertstr(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  905. var
  906. i : integer;
  907. begin
  908. if at=nil then
  909. begin
  910. at:=p;
  911. inc(count);
  912. end
  913. else
  914. begin
  915. i:=CompareStr(p^.data.messageinf.str^,at^.data.messageinf.str^);
  916. if i<0 then
  917. insertstr(p,at^.l,count)
  918. else if i>0 then
  919. insertstr(p,at^.r,count)
  920. else
  921. Message1(parser_e_duplicate_message_label,p^.data.messageinf.str^);
  922. end;
  923. end;
  924. procedure TVMTWriter.insertmsgint(p:TObject;arg:pointer);
  925. var
  926. i : longint;
  927. pd : Tprocdef;
  928. pt : pprocdeftree;
  929. begin
  930. if tsym(p).typ<>procsym then
  931. exit;
  932. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  933. begin
  934. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  935. if po_msgint in pd.procoptions then
  936. begin
  937. new(pt);
  938. pt^.data:=pd;
  939. pt^.l:=nil;
  940. pt^.r:=nil;
  941. insertint(pt,root,plongint(arg)^);
  942. end;
  943. end;
  944. end;
  945. procedure TVMTWriter.insertmsgstr(p:TObject;arg:pointer);
  946. var
  947. i : longint;
  948. pd : Tprocdef;
  949. pt : pprocdeftree;
  950. begin
  951. if tsym(p).typ<>procsym then
  952. exit;
  953. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  954. begin
  955. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  956. if po_msgstr in pd.procoptions then
  957. begin
  958. new(pt);
  959. pt^.data:=pd;
  960. pt^.l:=nil;
  961. pt^.r:=nil;
  962. insertstr(pt,root,plongint(arg)^);
  963. end;
  964. end;
  965. end;
  966. procedure TVMTWriter.writenames(list : TAsmList;p : pprocdeftree);
  967. var
  968. ca : pchar;
  969. len : byte;
  970. begin
  971. current_asmdata.getdatalabel(p^.nl);
  972. if (cs_create_pic in current_settings.moduleswitches) and
  973. assigned(current_procinfo) then
  974. include(current_procinfo.flags,pi_needs_got);
  975. if assigned(p^.l) then
  976. writenames(list,p^.l);
  977. list.concat(cai_align.create(const_align(sizeof(pint))));
  978. list.concat(Tai_label.Create(p^.nl));
  979. len:=length(p^.data.messageinf.str^);
  980. list.concat(tai_const.create_8bit(len));
  981. getmem(ca,len+1);
  982. move(p^.data.messageinf.str^[1],ca^,len);
  983. ca[len]:=#0;
  984. list.concat(Tai_string.Create_pchar(ca,len));
  985. if assigned(p^.r) then
  986. writenames(list,p^.r);
  987. end;
  988. procedure TVMTWriter.writestrentry(list : TAsmList;p : pprocdeftree);
  989. begin
  990. if assigned(p^.l) then
  991. writestrentry(list,p^.l);
  992. if (cs_create_pic in current_settings.moduleswitches) and
  993. assigned(current_procinfo) then
  994. include(current_procinfo.flags,pi_needs_got);
  995. { write name label }
  996. list.concat(cai_align.create(const_align(sizeof(pint))));
  997. list.concat(Tai_const.Create_sym(p^.nl));
  998. list.concat(cai_align.create(const_align(sizeof(pint))));
  999. list.concat(Tai_const.Createname(p^.data.mangledname,0));
  1000. if assigned(p^.r) then
  1001. writestrentry(list,p^.r);
  1002. end;
  1003. function TVMTWriter.genstrmsgtab(list : TAsmList) : tasmlabel;
  1004. var
  1005. count : longint;
  1006. begin
  1007. root:=nil;
  1008. count:=0;
  1009. { insert all message handlers into a tree, sorted by name }
  1010. _class.symtable.SymList.ForEachCall(@insertmsgstr,@count);
  1011. { write all names }
  1012. if assigned(root) then
  1013. writenames(list,root);
  1014. if (cs_create_pic in current_settings.moduleswitches) and
  1015. assigned(current_procinfo) then
  1016. include(current_procinfo.flags,pi_needs_got);
  1017. { now start writing of the message string table }
  1018. current_asmdata.getlabel(result,alt_data);
  1019. list.concat(cai_align.create(const_align(sizeof(pint))));
  1020. list.concat(Tai_label.Create(result));
  1021. list.concat(cai_align.create(const_align(sizeof(longint))));
  1022. list.concat(Tai_const.Create_32bit(count));
  1023. list.concat(cai_align.create(const_align(sizeof(pint))));
  1024. if assigned(root) then
  1025. begin
  1026. writestrentry(list,root);
  1027. disposeprocdeftree(root);
  1028. end;
  1029. end;
  1030. procedure TVMTWriter.writeintentry(list : TAsmList;p : pprocdeftree);
  1031. begin
  1032. if assigned(p^.l) then
  1033. writeintentry(list,p^.l);
  1034. if (cs_create_pic in current_settings.moduleswitches) and
  1035. assigned(current_procinfo) then
  1036. include(current_procinfo.flags,pi_needs_got);
  1037. { write name label }
  1038. list.concat(cai_align.create(const_align(sizeof(longint))));
  1039. list.concat(Tai_const.Create_32bit(p^.data.messageinf.i));
  1040. list.concat(cai_align.create(const_align(sizeof(pint))));
  1041. list.concat(Tai_const.Createname(p^.data.mangledname,0));
  1042. if assigned(p^.r) then
  1043. writeintentry(list,p^.r);
  1044. end;
  1045. function TVMTWriter.genintmsgtab(list : TAsmList) : tasmlabel;
  1046. var
  1047. r : tasmlabel;
  1048. count : longint;
  1049. begin
  1050. root:=nil;
  1051. count:=0;
  1052. { insert all message handlers into a tree, sorted by name }
  1053. _class.symtable.SymList.ForEachCall(@insertmsgint,@count);
  1054. if (cs_create_pic in current_settings.moduleswitches) and
  1055. assigned(current_procinfo) then
  1056. include(current_procinfo.flags,pi_needs_got);
  1057. { now start writing of the message string table }
  1058. current_asmdata.getlabel(r,alt_data);
  1059. list.concat(cai_align.create(const_align(sizeof(pint))));
  1060. list.concat(Tai_label.Create(r));
  1061. genintmsgtab:=r;
  1062. list.concat(cai_align.create(const_align(sizeof(longint))));
  1063. list.concat(Tai_const.Create_32bit(count));
  1064. list.concat(cai_align.create(const_align(sizeof(pint))));
  1065. if assigned(root) then
  1066. begin
  1067. writeintentry(list,root);
  1068. disposeprocdeftree(root);
  1069. end;
  1070. end;
  1071. {$ifdef WITHDMT}
  1072. {**************************************
  1073. DMT
  1074. **************************************}
  1075. procedure TVMTWriter.insertdmtentry(p:TObject;arg:pointer);
  1076. var
  1077. hp : tprocdef;
  1078. pt : pprocdeftree;
  1079. begin
  1080. if tsym(p).typ=procsym then
  1081. begin
  1082. hp:=tprocsym(p).definition;
  1083. while assigned(hp) do
  1084. begin
  1085. if (po_msgint in hp.procoptions) then
  1086. begin
  1087. new(pt);
  1088. pt^.p:=hp;
  1089. pt^.l:=nil;
  1090. pt^.r:=nil;
  1091. insertint(pt,root);
  1092. end;
  1093. hp:=hp.nextoverloaded;
  1094. end;
  1095. end;
  1096. end;
  1097. procedure TVMTWriter.writedmtindexentry(p : pprocdeftree);
  1098. begin
  1099. if assigned(p^.l) then
  1100. writedmtindexentry(p^.l);
  1101. al_globals.concat(Tai_const.Create_32bit(p^.data.messageinf.i));
  1102. if assigned(p^.r) then
  1103. writedmtindexentry(p^.r);
  1104. end;
  1105. procedure TVMTWriter.writedmtaddressentry(p : pprocdeftree);
  1106. begin
  1107. if assigned(p^.l) then
  1108. writedmtaddressentry(p^.l);
  1109. al_globals.concat(Tai_const_symbol.Createname(p^.data.mangledname,0));
  1110. if assigned(p^.r) then
  1111. writedmtaddressentry(p^.r);
  1112. end;
  1113. function TVMTWriter.gendmt : tasmlabel;
  1114. var
  1115. r : tasmlabel;
  1116. begin
  1117. root:=nil;
  1118. count:=0;
  1119. gendmt:=nil;
  1120. { insert all message handlers into a tree, sorted by number }
  1121. _class.symtable.SymList.ForEachCall(insertdmtentry);
  1122. if count>0 then
  1123. begin
  1124. current_asmdata.getdatalabel(r);
  1125. gendmt:=r;
  1126. al_globals.concat(cai_align.create(const_align(sizeof(pint))));
  1127. al_globals.concat(Tai_label.Create(r));
  1128. { entries for caching }
  1129. al_globals.concat(Tai_const.Create_ptr(0));
  1130. al_globals.concat(Tai_const.Create_ptr(0));
  1131. al_globals.concat(Tai_const.Create_32bit(count));
  1132. if assigned(root) then
  1133. begin
  1134. writedmtindexentry(root);
  1135. writedmtaddressentry(root);
  1136. disposeprocdeftree(root);
  1137. end;
  1138. end;
  1139. end;
  1140. {$endif WITHDMT}
  1141. {**************************************
  1142. Published Methods
  1143. **************************************}
  1144. procedure TVMTWriter.do_count_published_methods(p:TObject;arg:pointer);
  1145. var
  1146. i : longint;
  1147. pd : tprocdef;
  1148. begin
  1149. if (tsym(p).typ<>procsym) then
  1150. exit;
  1151. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  1152. begin
  1153. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  1154. if (pd.procsym=tsym(p)) and
  1155. (pd.visibility=vis_published) then
  1156. inc(plongint(arg)^);
  1157. end;
  1158. end;
  1159. procedure TVMTWriter.do_gen_published_methods(p:TObject;arg:pointer);
  1160. var
  1161. i : longint;
  1162. l : tasmlabel;
  1163. pd : tprocdef;
  1164. lists: ^TAsmList absolute arg;
  1165. begin
  1166. if (tsym(p).typ<>procsym) then
  1167. exit;
  1168. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  1169. begin
  1170. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  1171. if (pd.procsym=tsym(p)) and
  1172. (pd.visibility=vis_published) then
  1173. begin
  1174. current_asmdata.getlabel(l,alt_data);
  1175. lists[1].concat(cai_align.Create(const_align(sizeof(pint))));
  1176. lists[1].concat(Tai_label.Create(l));
  1177. lists[1].concat(Tai_const.Create_8bit(length(tsym(p).realname)));
  1178. lists[1].concat(Tai_string.Create(tsym(p).realname));
  1179. lists[0].concat(Tai_const.Create_sym(l));
  1180. if po_abstractmethod in pd.procoptions then
  1181. lists[0].concat(Tai_const.Create_sym(nil))
  1182. else
  1183. lists[0].concat(Tai_const.Createname(pd.mangledname,0));
  1184. end;
  1185. end;
  1186. end;
  1187. function TVMTWriter.genpublishedmethodstable(list : TAsmList) : tasmlabel;
  1188. var
  1189. l : tasmlabel;
  1190. count : longint;
  1191. lists : array[0..1] of TAsmList;
  1192. begin
  1193. count:=0;
  1194. _class.symtable.SymList.ForEachCall(@do_count_published_methods,@count);
  1195. if count>0 then
  1196. begin
  1197. lists[0]:=list;
  1198. lists[1]:=TAsmList.Create;
  1199. current_asmdata.getlabel(l,alt_data);
  1200. list.concat(cai_align.create(const_align(sizeof(pint))));
  1201. list.concat(Tai_label.Create(l));
  1202. list.concat(Tai_const.Create_32bit(count));
  1203. _class.symtable.SymList.ForEachCall(@do_gen_published_methods,@lists);
  1204. list.concatlist(lists[1]);
  1205. lists[1].Free;
  1206. genpublishedmethodstable:=l;
  1207. end
  1208. else
  1209. genpublishedmethodstable:=nil;
  1210. end;
  1211. function TVMTWriter.generate_field_table(list : TAsmList) : tasmlabel;
  1212. var
  1213. i : longint;
  1214. sym : tsym;
  1215. fieldtable,
  1216. classtable : tasmlabel;
  1217. classindex,
  1218. fieldcount : longint;
  1219. classtablelist : TFPList;
  1220. begin
  1221. classtablelist:=TFPList.Create;
  1222. { retrieve field info fields }
  1223. fieldcount:=0;
  1224. for i:=0 to _class.symtable.SymList.Count-1 do
  1225. begin
  1226. sym:=tsym(_class.symtable.SymList[i]);
  1227. if (sym.typ=fieldvarsym) and
  1228. (sym.visibility=vis_published) then
  1229. begin
  1230. if tfieldvarsym(sym).vardef.typ<>objectdef then
  1231. internalerror(200611032);
  1232. classindex:=classtablelist.IndexOf(tfieldvarsym(sym).vardef);
  1233. if classindex=-1 then
  1234. classtablelist.Add(tfieldvarsym(sym).vardef);
  1235. inc(fieldcount);
  1236. end;
  1237. end;
  1238. if fieldcount>0 then
  1239. begin
  1240. current_asmdata.getlabel(fieldtable,alt_data);
  1241. current_asmdata.getlabel(classtable,alt_data);
  1242. list.concat(cai_align.create(const_align(sizeof(pint))));
  1243. { write fields }
  1244. list.concat(Tai_label.Create(fieldtable));
  1245. list.concat(Tai_const.Create_16bit(fieldcount));
  1246. if (tf_requires_proper_alignment in target_info.flags) then
  1247. list.concat(cai_align.Create(sizeof(TConstPtrUInt)));
  1248. list.concat(Tai_const.Create_sym(classtable));
  1249. for i:=0 to _class.symtable.SymList.Count-1 do
  1250. begin
  1251. sym:=tsym(_class.symtable.SymList[i]);
  1252. if (sym.typ=fieldvarsym) and
  1253. (sym.visibility=vis_published) then
  1254. begin
  1255. if (tf_requires_proper_alignment in target_info.flags) then
  1256. list.concat(cai_align.Create(sizeof(pint)));
  1257. list.concat(Tai_const.Create_pint(tfieldvarsym(sym).fieldoffset));
  1258. classindex:=classtablelist.IndexOf(tfieldvarsym(sym).vardef);
  1259. if classindex=-1 then
  1260. internalerror(200611033);
  1261. list.concat(Tai_const.Create_16bit(classindex+1));
  1262. list.concat(Tai_const.Create_8bit(length(tfieldvarsym(sym).realname)));
  1263. list.concat(Tai_string.Create(tfieldvarsym(sym).realname));
  1264. end;
  1265. end;
  1266. { generate the class table }
  1267. list.concat(cai_align.create(const_align(sizeof(pint))));
  1268. list.concat(Tai_label.Create(classtable));
  1269. list.concat(Tai_const.Create_16bit(classtablelist.count));
  1270. if (tf_requires_proper_alignment in target_info.flags) then
  1271. list.concat(cai_align.Create(sizeof(TConstPtrUInt)));
  1272. for i:=0 to classtablelist.Count-1 do
  1273. list.concat(Tai_const.Createname(tobjectdef(classtablelist[i]).vmt_mangledname,0));
  1274. result:=fieldtable;
  1275. end
  1276. else
  1277. result:=nil;
  1278. classtablelist.free;
  1279. end;
  1280. {**************************************
  1281. Interface tables
  1282. **************************************}
  1283. function TVMTWriter.intf_get_vtbl_name(AImplIntf:TImplementedInterface): string;
  1284. begin
  1285. result:=make_mangledname('VTBL',_class.owner,_class.objname^+'_$_'+AImplIntf.IntfDef.objname^);
  1286. if (cs_create_pic in current_settings.moduleswitches) and
  1287. assigned(current_procinfo) then
  1288. include(current_procinfo.flags,pi_needs_got);
  1289. end;
  1290. procedure TVMTWriter.intf_create_vtbl(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  1291. var
  1292. pd : tprocdef;
  1293. vtblstr,
  1294. hs : string;
  1295. i : longint;
  1296. begin
  1297. vtblstr:=intf_get_vtbl_name(AImplIntf);
  1298. rawdata.concat(cai_align.create(const_align(sizeof(pint))));
  1299. rawdata.concat(tai_symbol.createname(vtblstr,AT_DATA,0));
  1300. if assigned(AImplIntf.procdefs) then
  1301. begin
  1302. for i:=0 to AImplIntf.procdefs.count-1 do
  1303. begin
  1304. pd:=tprocdef(AImplIntf.procdefs[i]);
  1305. hs:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+AImplIntf.IntfDef.objname^+'_$_'+
  1306. tostr(i)+'_$_'+pd.mangledname);
  1307. { create reference }
  1308. rawdata.concat(Tai_const.Createname(hs,0));
  1309. end;
  1310. end;
  1311. rawdata.concat(tai_symbol_end.createname(vtblstr));
  1312. end;
  1313. procedure TVMTWriter.intf_gen_intf_ref(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  1314. var
  1315. pd: tprocdef;
  1316. begin
  1317. { GUID (or nil for Corba interfaces) }
  1318. if AImplIntf.IntfDef.objecttype in [odt_interfacecom] then
  1319. rawdata.concat(Tai_const.CreateName(
  1320. make_mangledname('IID',AImplIntf.IntfDef.owner,AImplIntf.IntfDef.objname^),0))
  1321. else
  1322. rawdata.concat(Tai_const.Create_sym(nil));
  1323. { VTable }
  1324. rawdata.concat(Tai_const.Createname(intf_get_vtbl_name(AImplIntf.VtblImplIntf),0));
  1325. { IOffset field }
  1326. case AImplIntf.VtblImplIntf.IType of
  1327. etFieldValue, etFieldValueClass,
  1328. etStandard:
  1329. rawdata.concat(Tai_const.Create_pint(AImplIntf.VtblImplIntf.IOffset));
  1330. etStaticMethodResult, etStaticMethodClass:
  1331. rawdata.concat(Tai_const.Createname(
  1332. tprocdef(tpropertysym(AImplIntf.ImplementsGetter).propaccesslist[palt_read].procdef).mangledname,
  1333. 0
  1334. ));
  1335. etVirtualMethodResult, etVirtualMethodClass:
  1336. begin
  1337. pd := tprocdef(tpropertysym(AImplIntf.ImplementsGetter).propaccesslist[palt_read].procdef);
  1338. rawdata.concat(Tai_const.Create_pint(tobjectdef(pd.struct).vmtmethodoffset(pd.extnumber)));
  1339. end;
  1340. else
  1341. internalerror(200802162);
  1342. end;
  1343. { IIDStr }
  1344. rawdata.concat(Tai_const.CreateName(
  1345. make_mangledname('IIDSTR',AImplIntf.IntfDef.owner,AImplIntf.IntfDef.objname^),0));
  1346. { IType }
  1347. rawdata.concat(Tai_const.Create_pint(aint(AImplIntf.VtblImplIntf.IType)));
  1348. end;
  1349. function TVMTWriter.intf_write_table(list : TAsmList):TAsmLabel;
  1350. var
  1351. i : longint;
  1352. ImplIntf : TImplementedInterface;
  1353. begin
  1354. current_asmdata.getlabel(result,alt_data);
  1355. list.concat(cai_align.create(const_align(sizeof(pint))));
  1356. list.concat(Tai_label.Create(result));
  1357. list.concat(Tai_const.Create_pint(_class.ImplementedInterfaces.count));
  1358. { Write vtbl references }
  1359. for i:=0 to _class.ImplementedInterfaces.count-1 do
  1360. begin
  1361. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  1362. intf_gen_intf_ref(list,ImplIntf);
  1363. end;
  1364. { Write vtbls }
  1365. for i:=0 to _class.ImplementedInterfaces.count-1 do
  1366. begin
  1367. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  1368. if ImplIntf.VtblImplIntf=ImplIntf then
  1369. intf_create_vtbl(list,ImplIntf);
  1370. end;
  1371. end;
  1372. { Write interface identifiers to the data section }
  1373. procedure TVMTWriter.writeinterfaceids;
  1374. var
  1375. i : longint;
  1376. s : string;
  1377. begin
  1378. if assigned(_class.iidguid) then
  1379. begin
  1380. s:=make_mangledname('IID',_class.owner,_class.objname^);
  1381. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  1382. new_section(current_asmdata.asmlists[al_globals],sec_rodata,s,const_align(sizeof(pint)));
  1383. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  1384. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_32bit(longint(_class.iidguid^.D1)));
  1385. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_16bit(_class.iidguid^.D2));
  1386. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_16bit(_class.iidguid^.D3));
  1387. for i:=Low(_class.iidguid^.D4) to High(_class.iidguid^.D4) do
  1388. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_8bit(_class.iidguid^.D4[i]));
  1389. end;
  1390. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  1391. s:=make_mangledname('IIDSTR',_class.owner,_class.objname^);
  1392. new_section(current_asmdata.asmlists[al_globals],sec_rodata,s,sizeof(pint));
  1393. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  1394. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_8bit(length(_class.iidstr^)));
  1395. current_asmdata.asmlists[al_globals].concat(Tai_string.Create(_class.iidstr^));
  1396. end;
  1397. function TVMTWriter.RedirectToEmpty(procdef : tprocdef) : boolean;
  1398. var
  1399. i : longint;
  1400. hp : PCGParaLocation;
  1401. begin
  1402. result:=false;
  1403. if procdef.isempty then
  1404. begin
  1405. {$ifdef x86}
  1406. paramanager.create_funcretloc_info(procdef,calleeside);
  1407. if (procdef.funcretloc[calleeside].Location^.loc=LOC_FPUREGISTER) then
  1408. exit;
  1409. {$endif x86}
  1410. procdef.init_paraloc_info(calleeside);
  1411. { we can redirect the call if no memory parameter is passed }
  1412. for i:=0 to procdef.paras.count-1 do
  1413. begin
  1414. hp:=tparavarsym(procdef.paras[i]).paraloc[callerside].Location;
  1415. while assigned(hp) do
  1416. begin
  1417. if not(hp^.Loc in [LOC_REGISTER,LOC_MMREGISTER,LOC_FPUREGISTER]) then
  1418. exit;
  1419. hp:=hp^.Next;
  1420. end;
  1421. end;
  1422. result:=true;
  1423. end;
  1424. end;
  1425. procedure TVMTWriter.writevirtualmethods(List:TAsmList);
  1426. var
  1427. vmtpd : tprocdef;
  1428. vmtentry : pvmtentry;
  1429. i : longint;
  1430. procname : string;
  1431. {$ifdef vtentry}
  1432. hs : string;
  1433. {$endif vtentry}
  1434. begin
  1435. if not assigned(_class.VMTEntries) then
  1436. exit;
  1437. for i:=0 to _class.VMTEntries.Count-1 do
  1438. begin
  1439. vmtentry:=pvmtentry(_class.vmtentries[i]);
  1440. vmtpd:=vmtentry^.procdef;
  1441. { safety checks }
  1442. if not(po_virtualmethod in vmtpd.procoptions) then
  1443. internalerror(200611082);
  1444. if vmtpd.extnumber<>i then
  1445. internalerror(200611083);
  1446. if (po_abstractmethod in vmtpd.procoptions) then
  1447. procname:='FPC_ABSTRACTERROR'
  1448. else if (cs_opt_level2 in current_settings.optimizerswitches) and RedirectToEmpty(vmtpd) then
  1449. procname:='FPC_EMPTYMETHOD'
  1450. else if not wpoinfomanager.optimized_name_for_vmt(_class,vmtpd,procname) then
  1451. procname:=vmtpd.mangledname;
  1452. List.concat(Tai_const.createname(procname,0));
  1453. {$ifdef vtentry}
  1454. hs:='VTENTRY'+'_'+_class.vmt_mangledname+'$$'+tostr(_class.vmtmethodoffset(i) div sizeof(pint));
  1455. current_asmdata.asmlists[al_globals].concat(tai_symbol.CreateName(hs,AT_DATA,0));
  1456. {$endif vtentry}
  1457. end;
  1458. end;
  1459. procedure TVMTWriter.writevmt;
  1460. var
  1461. methodnametable,intmessagetable,
  1462. strmessagetable,classnamelabel,
  1463. fieldtablelabel : tasmlabel;
  1464. hs: string;
  1465. {$ifdef WITHDMT}
  1466. dmtlabel : tasmlabel;
  1467. {$endif WITHDMT}
  1468. interfacetable : tasmlabel;
  1469. templist : TAsmList;
  1470. begin
  1471. {$ifdef WITHDMT}
  1472. dmtlabel:=gendmt;
  1473. {$endif WITHDMT}
  1474. templist:=TAsmList.Create;
  1475. { write tables for classes, this must be done before the actual
  1476. class is written, because we need the labels defined }
  1477. if is_class(_class) then
  1478. begin
  1479. { write class name }
  1480. current_asmdata.getlabel(classnamelabel,alt_data);
  1481. templist.concat(cai_align.create(const_align(sizeof(pint))));
  1482. templist.concat(Tai_label.Create(classnamelabel));
  1483. hs:=_class.RttiName;
  1484. templist.concat(Tai_const.Create_8bit(length(hs)));
  1485. templist.concat(Tai_string.Create(hs));
  1486. { interface table }
  1487. if _class.ImplementedInterfaces.count>0 then
  1488. interfacetable:=intf_write_table(templist);
  1489. methodnametable:=genpublishedmethodstable(templist);
  1490. fieldtablelabel:=generate_field_table(templist);
  1491. { generate message and dynamic tables }
  1492. if (oo_has_msgstr in _class.objectoptions) then
  1493. strmessagetable:=genstrmsgtab(templist);
  1494. if (oo_has_msgint in _class.objectoptions) then
  1495. intmessagetable:=genintmsgtab(templist);
  1496. end;
  1497. { write debug info }
  1498. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  1499. new_section(current_asmdata.asmlists[al_globals],sec_rodata,_class.vmt_mangledname,const_align(sizeof(pint)));
  1500. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(_class.vmt_mangledname,AT_DATA,0));
  1501. { determine the size with symtable.datasize, because }
  1502. { size gives back 4 for classes }
  1503. current_asmdata.asmlists[al_globals].concat(Tai_const.Create(aitconst_ptr,tObjectSymtable(_class.symtable).datasize));
  1504. current_asmdata.asmlists[al_globals].concat(Tai_const.Create(aitconst_ptr,-int64(tObjectSymtable(_class.symtable).datasize)));
  1505. {$ifdef WITHDMT}
  1506. if _class.classtype=ct_object then
  1507. begin
  1508. if assigned(dmtlabel) then
  1509. current_asmdata.asmlists[al_globals].concat(Tai_const_symbol.Create(dmtlabel)))
  1510. else
  1511. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_ptr(0));
  1512. end;
  1513. {$endif WITHDMT}
  1514. { write pointer to parent VMT, this isn't implemented in TP }
  1515. { but this is not used in FPC ? (PM) }
  1516. { it's not used yet, but the delphi-operators as and is need it (FK) }
  1517. { it is not written for parents that don't have any vmt !! }
  1518. if assigned(_class.childof) and
  1519. (oo_has_vmt in _class.childof.objectoptions) then
  1520. current_asmdata.asmlists[al_globals].concat(Tai_const.Createname(_class.childof.vmt_mangledname,0))
  1521. else
  1522. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1523. { write extended info for classes, for the order see rtl/inc/objpash.inc }
  1524. if is_class(_class) then
  1525. begin
  1526. { pointer to class name string }
  1527. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(classnamelabel));
  1528. { pointer to dynamic table or nil }
  1529. if (oo_has_msgint in _class.objectoptions) then
  1530. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(intmessagetable))
  1531. else
  1532. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1533. { pointer to method table or nil }
  1534. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(methodnametable));
  1535. { pointer to field table }
  1536. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(fieldtablelabel));
  1537. { pointer to type info of published section }
  1538. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(RTTIWriter.get_rtti_label(_class,fullrtti)));
  1539. { inittable for con-/destruction }
  1540. if _class.members_need_inittable then
  1541. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(RTTIWriter.get_rtti_label(_class,initrtti)))
  1542. else
  1543. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1544. { auto table }
  1545. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1546. { interface table }
  1547. if _class.ImplementedInterfaces.count>0 then
  1548. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(interfacetable))
  1549. else if _class.implements_any_interfaces then
  1550. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil))
  1551. else
  1552. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(current_asmdata.RefAsmSymbol('FPC_EMPTYINTF')));
  1553. { table for string messages }
  1554. if (oo_has_msgstr in _class.objectoptions) then
  1555. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(strmessagetable))
  1556. else
  1557. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1558. end;
  1559. { write virtual methods }
  1560. writevirtualmethods(current_asmdata.asmlists[al_globals]);
  1561. current_asmdata.asmlists[al_globals].concat(Tai_const.create(aitconst_ptr,0));
  1562. { write the size of the VMT }
  1563. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname(_class.vmt_mangledname));
  1564. {$ifdef vtentry}
  1565. { write vtinherit symbol to notify the linker of the class inheritance tree }
  1566. hs:='VTINHERIT'+'_'+_class.vmt_mangledname+'$$';
  1567. if assigned(_class.childof) then
  1568. hs:=hs+_class.childof.vmt_mangledname
  1569. else
  1570. hs:=hs+_class.vmt_mangledname;
  1571. current_asmdata.asmlists[al_globals].concat(tai_symbol.CreateName(hs,AT_DATA,0));
  1572. {$endif vtentry}
  1573. if is_class(_class) then
  1574. current_asmdata.asmlists[al_globals].concatlist(templist);
  1575. templist.Free;
  1576. end;
  1577. end.