nobj.pas 51 KB

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