nobj.pas 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Routines for the code generation of data structures
  5. like VMT, Messages, VTables, Interfaces descs
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit nobj;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. {$ifdef Delphi}
  24. dmisc,
  25. {$endif}
  26. cutils,cclasses,
  27. globtype,
  28. symdef,
  29. aasmbase,aasmtai,
  30. cpuinfo
  31. ;
  32. type
  33. pprocdeftree = ^tprocdeftree;
  34. tprocdeftree = record
  35. data : tprocdef;
  36. nl : tasmlabel;
  37. l,r : pprocdeftree;
  38. end;
  39. pprocdefcoll = ^tprocdefcoll;
  40. tprocdefcoll = record
  41. data : tprocdef;
  42. hidden : boolean;
  43. next : pprocdefcoll;
  44. end;
  45. psymcoll = ^tsymcoll;
  46. tsymcoll = record
  47. speedvalue : cardinal;
  48. name : pstring;
  49. data : pprocdefcoll;
  50. next : psymcoll;
  51. end;
  52. tclassheader=class
  53. private
  54. _Class : tobjectdef;
  55. count : integer;
  56. private
  57. { message tables }
  58. root : pprocdeftree;
  59. procedure disposeprocdeftree(p : pprocdeftree);
  60. procedure insertmsgint(p : tnamedindexitem;arg:pointer);
  61. procedure insertmsgstr(p : tnamedindexitem;arg:pointer);
  62. procedure insertint(p : pprocdeftree;var at : pprocdeftree);
  63. procedure insertstr(p : pprocdeftree;var at : pprocdeftree);
  64. procedure writenames(p : pprocdeftree);
  65. procedure writeintentry(p : pprocdeftree);
  66. procedure writestrentry(p : pprocdeftree);
  67. {$ifdef WITHDMT}
  68. private
  69. { dmt }
  70. procedure insertdmtentry(p : tnamedindexitem;arg:pointer);
  71. procedure writedmtindexentry(p : pprocdeftree);
  72. procedure writedmtaddressentry(p : pprocdeftree);
  73. {$endif}
  74. private
  75. { published methods }
  76. procedure do_count(p : tnamedindexitem;arg:pointer);
  77. procedure genpubmethodtableentry(p : tnamedindexitem;arg:pointer);
  78. private
  79. { vmt }
  80. wurzel : psymcoll;
  81. nextvirtnumber : integer;
  82. has_constructor,
  83. has_virtual_method : boolean;
  84. procedure eachsym(sym : tnamedindexitem;arg:pointer);
  85. procedure disposevmttree;
  86. procedure writevirtualmethods(List:TAAsmoutput);
  87. private
  88. { interface tables }
  89. function gintfgetvtbllabelname(intfindex: integer): string;
  90. procedure gintfcreatevtbl(intfindex: integer; rawdata,rawcode: TAAsmoutput);
  91. procedure gintfgenentry(intfindex, contintfindex: integer; rawdata: TAAsmoutput);
  92. procedure gintfoptimizevtbls(implvtbl : plongintarray);
  93. procedure gintfwritedata;
  94. function gintfgetcprocdef(proc: tprocdef;const name: string): tprocdef;
  95. procedure gintfdoonintf(intf: tobjectdef; intfindex: longint);
  96. procedure gintfwalkdowninterface(intf: tobjectdef; intfindex: longint);
  97. protected
  98. { adjusts the self value with ioffset when casting a interface
  99. to a class
  100. }
  101. procedure adjustselfvalue(procdef: tprocdef;ioffset: aword);virtual;
  102. { generates the wrapper for a call to a method via an interface }
  103. procedure cgintfwrapper(asmlist: TAAsmoutput; procdef: tprocdef; const labelname: string; ioffset: longint);virtual;abstract;
  104. public
  105. constructor create(c:tobjectdef);
  106. destructor destroy;override;
  107. { generates the message tables for a class }
  108. function genstrmsgtab : tasmlabel;
  109. function genintmsgtab : tasmlabel;
  110. function genpublishedmethodstable : tasmlabel;
  111. { generates a VMT entries }
  112. procedure genvmt;
  113. {$ifdef WITHDMT}
  114. { generates a DMT for _class }
  115. function gendmt : tasmlabel;
  116. {$endif WITHDMT}
  117. { interfaces }
  118. function genintftable: tasmlabel;
  119. { write the VMT to datasegment }
  120. procedure writevmt;
  121. procedure writeinterfaceids;
  122. end;
  123. tclassheaderclass=class of tclassheader;
  124. var
  125. cclassheader : tclassheaderclass;
  126. implementation
  127. uses
  128. {$ifdef delphi}
  129. sysutils,
  130. {$else}
  131. strings,
  132. {$endif}
  133. globals,verbose,
  134. symtable,symconst,symtype,symsym,defcmp,paramgr,
  135. {$ifdef GDB}
  136. gdb,
  137. {$endif GDB}
  138. aasmcpu,
  139. cpubase,cgbase,
  140. cgobj
  141. ;
  142. {*****************************************************************************
  143. TClassHeader
  144. *****************************************************************************}
  145. constructor tclassheader.create(c:tobjectdef);
  146. begin
  147. inherited Create;
  148. _Class:=c;
  149. end;
  150. destructor tclassheader.destroy;
  151. begin
  152. disposevmttree;
  153. end;
  154. {**************************************
  155. Message Tables
  156. **************************************}
  157. procedure tclassheader.disposeprocdeftree(p : pprocdeftree);
  158. begin
  159. if assigned(p^.l) then
  160. disposeprocdeftree(p^.l);
  161. if assigned(p^.r) then
  162. disposeprocdeftree(p^.r);
  163. dispose(p);
  164. end;
  165. procedure tclassheader.insertint(p : pprocdeftree;var at : pprocdeftree);
  166. begin
  167. if at=nil then
  168. begin
  169. at:=p;
  170. inc(count);
  171. end
  172. else
  173. begin
  174. if p^.data.messageinf.i<at^.data.messageinf.i then
  175. insertint(p,at^.l)
  176. else if p^.data.messageinf.i>at^.data.messageinf.i then
  177. insertint(p,at^.r)
  178. else
  179. Message1(parser_e_duplicate_message_label,tostr(p^.data.messageinf.i));
  180. end;
  181. end;
  182. procedure tclassheader.insertstr(p : pprocdeftree;var at : pprocdeftree);
  183. var
  184. i : integer;
  185. begin
  186. if at=nil then
  187. begin
  188. at:=p;
  189. inc(count);
  190. end
  191. else
  192. begin
  193. i:=strcomp(p^.data.messageinf.str,at^.data.messageinf.str);
  194. if i<0 then
  195. insertstr(p,at^.l)
  196. else if i>0 then
  197. insertstr(p,at^.r)
  198. else
  199. Message1(parser_e_duplicate_message_label,strpas(p^.data.messageinf.str));
  200. end;
  201. end;
  202. procedure tclassheader.insertmsgint(p : tnamedindexitem;arg:pointer);
  203. var
  204. i : cardinal;
  205. def: Tprocdef;
  206. pt : pprocdeftree;
  207. begin
  208. if tsym(p).typ=procsym then
  209. for i:=1 to Tprocsym(p).procdef_count do
  210. begin
  211. def:=Tprocsym(p).procdef[i];
  212. if po_msgint in def.procoptions then
  213. begin
  214. new(pt);
  215. pt^.data:=def;
  216. pt^.l:=nil;
  217. pt^.r:=nil;
  218. insertint(pt,root);
  219. end;
  220. end;
  221. end;
  222. procedure tclassheader.insertmsgstr(p : tnamedindexitem;arg:pointer);
  223. var
  224. i : cardinal;
  225. def: Tprocdef;
  226. pt : pprocdeftree;
  227. begin
  228. if tsym(p).typ=procsym then
  229. for i:=1 to Tprocsym(p).procdef_count do
  230. begin
  231. def:=Tprocsym(p).procdef[i];
  232. if po_msgstr in def.procoptions then
  233. begin
  234. new(pt);
  235. pt^.data:=def;
  236. pt^.l:=nil;
  237. pt^.r:=nil;
  238. insertstr(pt,root);
  239. end;
  240. end;
  241. end;
  242. procedure tclassheader.writenames(p : pprocdeftree);
  243. var
  244. ca : pchar;
  245. len : longint;
  246. begin
  247. objectlibrary.getdatalabel(p^.nl);
  248. if assigned(p^.l) then
  249. writenames(p^.l);
  250. datasegment.concat(tai_align.create(const_align(POINTER_SIZE)));
  251. dataSegment.concat(Tai_label.Create(p^.nl));
  252. len:=strlen(p^.data.messageinf.str);
  253. datasegment.concat(tai_const.create_8bit(len));
  254. getmem(ca,len+1);
  255. move(p^.data.messageinf.str^,ca^,len+1);
  256. dataSegment.concat(Tai_string.Create_pchar(ca));
  257. if assigned(p^.r) then
  258. writenames(p^.r);
  259. end;
  260. procedure tclassheader.writestrentry(p : pprocdeftree);
  261. begin
  262. if assigned(p^.l) then
  263. writestrentry(p^.l);
  264. { write name label }
  265. dataSegment.concat(Tai_const_symbol.Create(p^.nl));
  266. dataSegment.concat(Tai_const_symbol.Createname(p^.data.mangledname));
  267. if assigned(p^.r) then
  268. writestrentry(p^.r);
  269. end;
  270. function tclassheader.genstrmsgtab : tasmlabel;
  271. var
  272. r : tasmlabel;
  273. begin
  274. root:=nil;
  275. count:=0;
  276. { insert all message handlers into a tree, sorted by name }
  277. _class.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}insertmsgstr,nil);
  278. { write all names }
  279. if assigned(root) then
  280. writenames(root);
  281. { now start writing of the message string table }
  282. objectlibrary.getdatalabel(r);
  283. datasegment.concat(tai_align.create(const_align(POINTER_SIZE)));
  284. dataSegment.concat(Tai_label.Create(r));
  285. genstrmsgtab:=r;
  286. dataSegment.concat(Tai_const.Create_32bit(count));
  287. if assigned(root) then
  288. begin
  289. writestrentry(root);
  290. disposeprocdeftree(root);
  291. end;
  292. end;
  293. procedure tclassheader.writeintentry(p : pprocdeftree);
  294. begin
  295. if assigned(p^.l) then
  296. writeintentry(p^.l);
  297. { write name label }
  298. dataSegment.concat(Tai_const.Create_32bit(p^.data.messageinf.i));
  299. dataSegment.concat(Tai_const_symbol.Createname(p^.data.mangledname));
  300. if assigned(p^.r) then
  301. writeintentry(p^.r);
  302. end;
  303. function tclassheader.genintmsgtab : tasmlabel;
  304. var
  305. r : tasmlabel;
  306. begin
  307. root:=nil;
  308. count:=0;
  309. { insert all message handlers into a tree, sorted by name }
  310. _class.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}insertmsgint,nil);
  311. { now start writing of the message string table }
  312. objectlibrary.getdatalabel(r);
  313. datasegment.concat(tai_align.create(const_align(POINTER_SIZE)));
  314. dataSegment.concat(Tai_label.Create(r));
  315. genintmsgtab:=r;
  316. dataSegment.concat(Tai_const.Create_32bit(count));
  317. if assigned(root) then
  318. begin
  319. writeintentry(root);
  320. disposeprocdeftree(root);
  321. end;
  322. end;
  323. {$ifdef WITHDMT}
  324. {**************************************
  325. DMT
  326. **************************************}
  327. procedure tclassheader.insertdmtentry(p : tnamedindexitem;arg:pointer);
  328. var
  329. hp : tprocdef;
  330. pt : pprocdeftree;
  331. begin
  332. if tsym(p).typ=procsym then
  333. begin
  334. hp:=tprocsym(p).definition;
  335. while assigned(hp) do
  336. begin
  337. if (po_msgint in hp.procoptions) then
  338. begin
  339. new(pt);
  340. pt^.p:=hp;
  341. pt^.l:=nil;
  342. pt^.r:=nil;
  343. insertint(pt,root);
  344. end;
  345. hp:=hp.nextoverloaded;
  346. end;
  347. end;
  348. end;
  349. procedure tclassheader.writedmtindexentry(p : pprocdeftree);
  350. begin
  351. if assigned(p^.l) then
  352. writedmtindexentry(p^.l);
  353. dataSegment.concat(Tai_const.Create_32bit(p^.data.messageinf.i));
  354. if assigned(p^.r) then
  355. writedmtindexentry(p^.r);
  356. end;
  357. procedure tclassheader.writedmtaddressentry(p : pprocdeftree);
  358. begin
  359. if assigned(p^.l) then
  360. writedmtaddressentry(p^.l);
  361. dataSegment.concat(Tai_const_symbol.Createname(p^.data.mangledname));
  362. if assigned(p^.r) then
  363. writedmtaddressentry(p^.r);
  364. end;
  365. function tclassheader.gendmt : tasmlabel;
  366. var
  367. r : tasmlabel;
  368. begin
  369. root:=nil;
  370. count:=0;
  371. gendmt:=nil;
  372. { insert all message handlers into a tree, sorted by number }
  373. _class.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}insertdmtentry);
  374. if count>0 then
  375. begin
  376. objectlibrary.getdatalabel(r);
  377. gendmt:=r;
  378. datasegment.concat(tai_align.create(const_align(POINTER_SIZE)));
  379. dataSegment.concat(Tai_label.Create(r));
  380. { entries for caching }
  381. dataSegment.concat(Tai_const.Create_32bit(0));
  382. dataSegment.concat(Tai_const.Create_32bit(0));
  383. dataSegment.concat(Tai_const.Create_32bit(count));
  384. if assigned(root) then
  385. begin
  386. writedmtindexentry(root);
  387. writedmtaddressentry(root);
  388. disposeprocdeftree(root);
  389. end;
  390. end;
  391. end;
  392. {$endif WITHDMT}
  393. {**************************************
  394. Published Methods
  395. **************************************}
  396. procedure tclassheader.do_count(p : tnamedindexitem;arg:pointer);
  397. begin
  398. if (tsym(p).typ=procsym) and (sp_published in tsym(p).symoptions) then
  399. inc(count);
  400. end;
  401. procedure tclassheader.genpubmethodtableentry(p : tnamedindexitem;arg:pointer);
  402. var
  403. hp : tprocdef;
  404. l : tasmlabel;
  405. begin
  406. if (tsym(p).typ=procsym) and (sp_published in tsym(p).symoptions) then
  407. begin
  408. if Tprocsym(p).procdef_count>1 then
  409. internalerror(1209992);
  410. hp:=tprocsym(p).first_procdef;
  411. objectlibrary.getdatalabel(l);
  412. consts.concat(tai_align.create(const_align(POINTER_SIZE)));
  413. Consts.concat(Tai_label.Create(l));
  414. Consts.concat(Tai_const.Create_8bit(length(p.name)));
  415. Consts.concat(Tai_string.Create(p.name));
  416. dataSegment.concat(Tai_const_symbol.Create(l));
  417. dataSegment.concat(Tai_const_symbol.Createname(hp.mangledname));
  418. end;
  419. end;
  420. function tclassheader.genpublishedmethodstable : tasmlabel;
  421. var
  422. l : tasmlabel;
  423. begin
  424. count:=0;
  425. _class.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}do_count,nil);
  426. if count>0 then
  427. begin
  428. objectlibrary.getdatalabel(l);
  429. datasegment.concat(tai_align.create(const_align(POINTER_SIZE)));
  430. dataSegment.concat(Tai_label.Create(l));
  431. dataSegment.concat(Tai_const.Create_32bit(count));
  432. _class.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}genpubmethodtableentry,nil);
  433. genpublishedmethodstable:=l;
  434. end
  435. else
  436. genpublishedmethodstable:=nil;
  437. end;
  438. {**************************************
  439. VMT
  440. **************************************}
  441. procedure tclassheader.eachsym(sym : tnamedindexitem;arg:pointer);
  442. var
  443. procdefcoll : pprocdefcoll;
  444. symcoll : psymcoll;
  445. _name : string;
  446. _speed : cardinal;
  447. procedure newdefentry(pd:tprocdef);
  448. begin
  449. new(procdefcoll);
  450. procdefcoll^.data:=pd;
  451. procdefcoll^.hidden:=false;
  452. procdefcoll^.next:=symcoll^.data;
  453. symcoll^.data:=procdefcoll;
  454. { if it's a virtual method }
  455. if (po_virtualmethod in pd.procoptions) then
  456. begin
  457. { then it gets a number ... }
  458. pd.extnumber:=nextvirtnumber;
  459. { and we inc the number }
  460. inc(nextvirtnumber);
  461. has_virtual_method:=true;
  462. end;
  463. if (pd.proctypeoption=potype_constructor) then
  464. has_constructor:=true;
  465. { check, if a method should be overridden }
  466. if (pd._class=_class) and
  467. (po_overridingmethod in pd.procoptions) then
  468. MessagePos1(pd.fileinfo,parser_e_nothing_to_be_overridden,pd.fullprocname(false));
  469. end;
  470. { creates a new entry in the procsym list }
  471. procedure newentry;
  472. var i:cardinal;
  473. begin
  474. { if not, generate a new symbol item }
  475. new(symcoll);
  476. symcoll^.speedvalue:=sym.speedvalue;
  477. symcoll^.name:=stringdup(sym.name);
  478. symcoll^.next:=wurzel;
  479. symcoll^.data:=nil;
  480. wurzel:=symcoll;
  481. { inserts all definitions }
  482. for i:=1 to Tprocsym(sym).procdef_count do
  483. newdefentry(Tprocsym(sym).procdef[i]);
  484. end;
  485. label
  486. handlenextdef;
  487. var
  488. pd : tprocdef;
  489. i : cardinal;
  490. is_visible,
  491. hasoverloads,
  492. pdoverload : boolean;
  493. begin
  494. { put only sub routines into the VMT, and routines
  495. that are visible to the current class. Skip private
  496. methods in other classes }
  497. if (tsym(sym).typ=procsym) then
  498. begin
  499. { is this symbol visible from the class that we are
  500. generating. This will be used to hide the other procdefs.
  501. When the symbol is not visible we don't hide the other
  502. procdefs, because they can be reused in the next class.
  503. The check to skip the invisible methods that are in the
  504. list is futher down in the code }
  505. is_visible:=tprocsym(sym).is_visible_for_object(_class);
  506. { check the current list of symbols }
  507. _name:=sym.name;
  508. _speed:=sym.speedvalue;
  509. symcoll:=wurzel;
  510. while assigned(symcoll) do
  511. begin
  512. { does the symbol already exist in the list? First
  513. compare speedvalue before doing the string compare to
  514. speed it up a little }
  515. if (_speed=symcoll^.speedvalue) and
  516. (_name=symcoll^.name^) then
  517. begin
  518. hasoverloads:=(Tprocsym(sym).procdef_count>1);
  519. { walk through all defs of the symbol }
  520. for i:=1 to Tprocsym(sym).procdef_count do
  521. begin
  522. pd:=Tprocsym(sym).procdef[i];
  523. if pd.procsym=sym then
  524. begin
  525. pdoverload:=(po_overload in pd.procoptions);
  526. { compare with all stored definitions }
  527. procdefcoll:=symcoll^.data;
  528. while assigned(procdefcoll) do
  529. begin
  530. { compare only if the definition is not hidden }
  531. if not procdefcoll^.hidden then
  532. begin
  533. { check if one of the two methods has virtual }
  534. if (po_virtualmethod in procdefcoll^.data.procoptions) or
  535. (po_virtualmethod in pd.procoptions) then
  536. begin
  537. { if the current definition has no virtual then hide the
  538. old virtual if the new definition has the same arguments or
  539. when it has no overload directive and no overloads }
  540. if not(po_virtualmethod in pd.procoptions) then
  541. begin
  542. if tstoredsym(procdefcoll^.data.procsym).is_visible_for_object(pd._class) and
  543. (not(pdoverload or hasoverloads) or
  544. (compare_paras(procdefcoll^.data.para,pd.para,cp_value_equal_const,[])>=te_equal)) then
  545. begin
  546. if is_visible then
  547. procdefcoll^.hidden:=true;
  548. if (_class=pd._class) and not(po_reintroduce in pd.procoptions) then
  549. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false));
  550. end;
  551. end
  552. { if both are virtual we check the header }
  553. else if (po_virtualmethod in pd.procoptions) and
  554. (po_virtualmethod in procdefcoll^.data.procoptions) then
  555. begin
  556. { new one has not override }
  557. if is_class(_class) and
  558. not(po_overridingmethod in pd.procoptions) then
  559. begin
  560. { we start a new virtual tree, hide the old }
  561. if (not(pdoverload or hasoverloads) or
  562. (compare_paras(procdefcoll^.data.para,pd.para,cp_value_equal_const,[])>=te_equal)) and
  563. (tstoredsym(procdefcoll^.data.procsym).is_visible_for_object(pd._class)) then
  564. begin
  565. if is_visible then
  566. procdefcoll^.hidden:=true;
  567. if (_class=pd._class) and not(po_reintroduce in pd.procoptions) then
  568. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false));
  569. end;
  570. end
  571. { check if the method to override is visible }
  572. else if (po_overridingmethod in pd.procoptions) and
  573. (not tstoredsym(procdefcoll^.data.procsym).is_visible_for_object(pd._class)) then
  574. begin
  575. { do nothing, the error will follow when adding the entry }
  576. end
  577. { same parameters }
  578. else if (compare_paras(procdefcoll^.data.para,pd.para,cp_value_equal_const,[])>=te_equal) then
  579. begin
  580. { overload is inherited }
  581. if (po_overload in procdefcoll^.data.procoptions) then
  582. include(pd.procoptions,po_overload);
  583. { inherite calling convention when it was force and the
  584. current definition has none force }
  585. if (po_hascallingconvention in procdefcoll^.data.procoptions) and
  586. not(po_hascallingconvention in pd.procoptions) then
  587. begin
  588. pd.proccalloption:=procdefcoll^.data.proccalloption;
  589. include(pd.procoptions,po_hascallingconvention);
  590. end;
  591. { the flags have to match except abstract and override }
  592. { only if both are virtual !! }
  593. if (procdefcoll^.data.proccalloption<>pd.proccalloption) or
  594. (procdefcoll^.data.proctypeoption<>pd.proctypeoption) or
  595. ((procdefcoll^.data.procoptions-
  596. [po_abstractmethod,po_overridingmethod,po_assembler,po_overload,po_public])<>
  597. (pd.procoptions-[po_abstractmethod,po_overridingmethod,po_assembler,po_overload,po_public])) then
  598. begin
  599. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  600. tprocsym(procdefcoll^.data.procsym).write_parameter_lists(pd);
  601. end;
  602. { error, if the return types aren't equal }
  603. if not(equal_defs(procdefcoll^.data.rettype.def,pd.rettype.def)) and
  604. not((procdefcoll^.data.rettype.def.deftype=objectdef) and
  605. (pd.rettype.def.deftype=objectdef) and
  606. is_class(procdefcoll^.data.rettype.def) and
  607. is_class(pd.rettype.def) and
  608. (tobjectdef(pd.rettype.def).is_related(
  609. tobjectdef(procdefcoll^.data.rettype.def)))) then
  610. Message2(parser_e_overridden_methods_not_same_ret,pd.fullprocname(false),
  611. procdefcoll^.data.fullprocname(false));
  612. { now set the number }
  613. pd.extnumber:=procdefcoll^.data.extnumber;
  614. { and exchange }
  615. procdefcoll^.data:=pd;
  616. goto handlenextdef;
  617. end
  618. { different parameters }
  619. else
  620. begin
  621. { when we got an override directive then can search futher for
  622. the procedure to override.
  623. If we are starting a new virtual tree then hide the old tree }
  624. if not(po_overridingmethod in pd.procoptions) and
  625. not pdoverload then
  626. begin
  627. if is_visible then
  628. procdefcoll^.hidden:=true;
  629. if (_class=pd._class) and not(po_reintroduce in pd.procoptions) then
  630. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false));
  631. end;
  632. end;
  633. end
  634. else
  635. begin
  636. { the new definition is virtual and the old static, we hide the old one
  637. if the new defintion has not the overload directive }
  638. if is_visible and
  639. ((not(pdoverload or hasoverloads)) or
  640. (compare_paras(procdefcoll^.data.para,pd.para,cp_value_equal_const,[])>=te_equal)) then
  641. procdefcoll^.hidden:=true;
  642. end;
  643. end
  644. else
  645. begin
  646. { both are static, we hide the old one if the new defintion
  647. has not the overload directive }
  648. if is_visible and
  649. ((not pdoverload) or
  650. (compare_paras(procdefcoll^.data.para,pd.para,cp_value_equal_const,[])>=te_equal)) then
  651. procdefcoll^.hidden:=true;
  652. end;
  653. end; { not hidden }
  654. procdefcoll:=procdefcoll^.next;
  655. end;
  656. { if it isn't saved in the list we create a new entry }
  657. newdefentry(pd);
  658. end;
  659. handlenextdef:
  660. end;
  661. exit;
  662. end;
  663. symcoll:=symcoll^.next;
  664. end;
  665. newentry;
  666. end;
  667. end;
  668. procedure tclassheader.disposevmttree;
  669. var
  670. symcoll : psymcoll;
  671. procdefcoll : pprocdefcoll;
  672. begin
  673. { disposes the above generated tree }
  674. symcoll:=wurzel;
  675. while assigned(symcoll) do
  676. begin
  677. wurzel:=symcoll^.next;
  678. stringdispose(symcoll^.name);
  679. procdefcoll:=symcoll^.data;
  680. while assigned(procdefcoll) do
  681. begin
  682. symcoll^.data:=procdefcoll^.next;
  683. dispose(procdefcoll);
  684. procdefcoll:=symcoll^.data;
  685. end;
  686. dispose(symcoll);
  687. symcoll:=wurzel;
  688. end;
  689. end;
  690. procedure tclassheader.genvmt;
  691. procedure do_genvmt(p : tobjectdef);
  692. begin
  693. { start with the base class }
  694. if assigned(p.childof) then
  695. do_genvmt(p.childof);
  696. { walk through all public syms }
  697. p.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}eachsym,nil);
  698. end;
  699. begin
  700. wurzel:=nil;
  701. nextvirtnumber:=0;
  702. has_constructor:=false;
  703. has_virtual_method:=false;
  704. { generates a tree of all used methods }
  705. do_genvmt(_class);
  706. if not(is_interface(_class)) and
  707. has_virtual_method and
  708. not(has_constructor) then
  709. Message1(parser_w_virtual_without_constructor,_class.objrealname^);
  710. end;
  711. {**************************************
  712. Interface tables
  713. **************************************}
  714. function tclassheader.gintfgetvtbllabelname(intfindex: integer): string;
  715. begin
  716. gintfgetvtbllabelname:=make_mangledname('VTBL',_class.owner,_class.objname^+
  717. '_$_'+_class.implementedinterfaces.interfaces(intfindex).objname^);
  718. end;
  719. procedure tclassheader.gintfcreatevtbl(intfindex: integer; rawdata,rawcode: TAAsmoutput);
  720. var
  721. implintf: timplementedinterfaces;
  722. curintf: tobjectdef;
  723. proccount: integer;
  724. tmps: string;
  725. i: longint;
  726. begin
  727. implintf:=_class.implementedinterfaces;
  728. curintf:=implintf.interfaces(intfindex);
  729. if (cs_create_smart in aktmoduleswitches) then
  730. rawdata.concat(Tai_symbol.Createname_global(gintfgetvtbllabelname(intfindex),0))
  731. else
  732. rawdata.concat(Tai_symbol.Createname(gintfgetvtbllabelname(intfindex),0));
  733. proccount:=implintf.implproccount(intfindex);
  734. for i:=1 to proccount do
  735. begin
  736. tmps:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+curintf.objname^+'_$_'+
  737. tostr(i)+'_$_'+
  738. implintf.implprocs(intfindex,i).mangledname);
  739. { create wrapper code }
  740. cgintfwrapper(rawcode,implintf.implprocs(intfindex,i),tmps,implintf.ioffsets(intfindex)^);
  741. { create reference }
  742. rawdata.concat(Tai_const_symbol.Createname(tmps));
  743. end;
  744. end;
  745. procedure tclassheader.gintfgenentry(intfindex, contintfindex: integer; rawdata: TAAsmoutput);
  746. var
  747. implintf: timplementedinterfaces;
  748. curintf: tobjectdef;
  749. tmplabel: tasmlabel;
  750. i: longint;
  751. begin
  752. implintf:=_class.implementedinterfaces;
  753. curintf:=implintf.interfaces(intfindex);
  754. { GUID }
  755. if curintf.objecttype in [odt_interfacecom] then
  756. begin
  757. { label for GUID }
  758. objectlibrary.getdatalabel(tmplabel);
  759. rawdata.concat(tai_align.create(const_align(pointer_size)));
  760. rawdata.concat(Tai_label.Create(tmplabel));
  761. rawdata.concat(Tai_const.Create_32bit(longint(curintf.iidguid^.D1)));
  762. rawdata.concat(Tai_const.Create_16bit(curintf.iidguid^.D2));
  763. rawdata.concat(Tai_const.Create_16bit(curintf.iidguid^.D3));
  764. for i:=Low(curintf.iidguid^.D4) to High(curintf.iidguid^.D4) do
  765. rawdata.concat(Tai_const.Create_8bit(curintf.iidguid^.D4[i]));
  766. dataSegment.concat(Tai_const_symbol.Create(tmplabel));
  767. end
  768. else
  769. begin
  770. { nil for Corba interfaces }
  771. dataSegment.concat(Tai_const.Create_32bit(0)); { nil }
  772. end;
  773. { VTable }
  774. dataSegment.concat(Tai_const_symbol.Createname(gintfgetvtbllabelname(contintfindex)));
  775. { IOffset field }
  776. dataSegment.concat(Tai_const.Create_32bit(implintf.ioffsets(contintfindex)^));
  777. { IIDStr }
  778. objectlibrary.getdatalabel(tmplabel);
  779. rawdata.concat(tai_align.create(const_align(pointer_size)));
  780. rawdata.concat(Tai_label.Create(tmplabel));
  781. rawdata.concat(Tai_const.Create_8bit(length(curintf.iidstr^)));
  782. if curintf.objecttype=odt_interfacecom then
  783. rawdata.concat(Tai_string.Create(upper(curintf.iidstr^)))
  784. else
  785. rawdata.concat(Tai_string.Create(curintf.iidstr^));
  786. dataSegment.concat(Tai_const_symbol.Create(tmplabel));
  787. end;
  788. procedure tclassheader.gintfoptimizevtbls(implvtbl : plongintarray);
  789. type
  790. tcompintfentry = record
  791. weight: longint;
  792. compintf: longint;
  793. end;
  794. { Max 1000 interface in the class header interfaces it's enough imho }
  795. tcompintfs = packed array[1..1000] of tcompintfentry;
  796. pcompintfs = ^tcompintfs;
  797. tequals = packed array[1..1000] of longint;
  798. pequals = ^tequals;
  799. var
  800. max: longint;
  801. equals: pequals;
  802. compats: pcompintfs;
  803. i: longint;
  804. j: longint;
  805. w: longint;
  806. cij: boolean;
  807. cji: boolean;
  808. begin
  809. max:=_class.implementedinterfaces.count;
  810. if max>High(tequals) then
  811. Internalerror(200006135);
  812. getmem(compats,sizeof(tcompintfentry)*max);
  813. getmem(equals,sizeof(longint)*max);
  814. fillchar(compats^,sizeof(tcompintfentry)*max,0);
  815. fillchar(equals^,sizeof(longint)*max,0);
  816. { ismergepossible is a containing relation
  817. meaning of ismergepossible(a,b,w) =
  818. if implementorfunction map of a is contained implementorfunction map of b
  819. imp(a,b) and imp(b,c) => imp(a,c) ; imp(a,b) and imp(b,a) => a == b
  820. }
  821. { the order is very important for correct allocation }
  822. for i:=1 to max do
  823. begin
  824. for j:=i+1 to max do
  825. begin
  826. cij:=_class.implementedinterfaces.isimplmergepossible(i,j,w);
  827. cji:=_class.implementedinterfaces.isimplmergepossible(j,i,w);
  828. if cij and cji then { i equal j }
  829. begin
  830. { get minimum index of equal }
  831. if equals^[j]=0 then
  832. equals^[j]:=i;
  833. end
  834. else if cij then
  835. begin
  836. { get minimum index of maximum weight }
  837. if compats^[i].weight<w then
  838. begin
  839. compats^[i].weight:=w;
  840. compats^[i].compintf:=j;
  841. end;
  842. end
  843. else if cji then
  844. begin
  845. { get minimum index of maximum weight }
  846. if (compats^[j].weight<w) then
  847. begin
  848. compats^[j].weight:=w;
  849. compats^[j].compintf:=i;
  850. end;
  851. end;
  852. end;
  853. end;
  854. for i:=1 to max do
  855. begin
  856. if compats^[i].compintf<>0 then
  857. implvtbl[i]:=compats^[i].compintf
  858. else if equals^[i]<>0 then
  859. implvtbl[i]:=equals^[i]
  860. else
  861. implvtbl[i]:=i;
  862. end;
  863. freemem(compats,sizeof(tcompintfentry)*max);
  864. freemem(equals,sizeof(longint)*max);
  865. end;
  866. procedure tclassheader.gintfwritedata;
  867. var
  868. rawdata,rawcode: taasmoutput;
  869. impintfindexes: plongintarray;
  870. max: longint;
  871. i: longint;
  872. begin
  873. max:=_class.implementedinterfaces.count;
  874. getmem(impintfindexes,(max+1)*sizeof(longint));
  875. gintfoptimizevtbls(impintfindexes);
  876. rawdata:=TAAsmOutput.Create;
  877. rawcode:=TAAsmOutput.Create;
  878. dataSegment.concat(Tai_const.Create_16bit(max));
  879. { Two pass, one for allocation and vtbl creation }
  880. for i:=1 to max do
  881. begin
  882. if impintfindexes[i]=i then { if implement itself }
  883. begin
  884. { allocate a pointer in the object memory }
  885. with tobjectsymtable(_class.symtable) do
  886. begin
  887. datasize:=align(datasize,min(POINTER_SIZE,fieldalignment));
  888. _class.implementedinterfaces.ioffsets(i)^:=datasize;
  889. inc(datasize,POINTER_SIZE);
  890. end;
  891. { write vtbl }
  892. gintfcreatevtbl(i,rawdata,rawcode);
  893. end;
  894. end;
  895. { second pass: for fill interfacetable and remained ioffsets }
  896. for i:=1 to max do
  897. begin
  898. if i<>impintfindexes[i] then { why execute x:=x ? }
  899. with _class.implementedinterfaces do
  900. ioffsets(i)^:=ioffsets(impintfindexes[i])^;
  901. gintfgenentry(i,impintfindexes[i],rawdata);
  902. end;
  903. dataSegment.concatlist(rawdata);
  904. rawdata.free;
  905. codeSegment.concatlist(rawcode);
  906. rawcode.free;
  907. freemem(impintfindexes,(max+1)*sizeof(longint));
  908. end;
  909. function tclassheader.gintfgetcprocdef(proc: tprocdef;const name: string): tprocdef;
  910. var
  911. sym: tsym;
  912. implprocdef : Tprocdef;
  913. i: cardinal;
  914. begin
  915. gintfgetcprocdef:=nil;
  916. sym:=tsym(search_class_member(_class,name));
  917. if assigned(sym) and
  918. (sym.typ=procsym) then
  919. begin
  920. { when the definition has overload directive set, we search for
  921. overloaded definitions in the class, this only needs to be done once
  922. for class entries as the tree keeps always the same }
  923. if (not tprocsym(sym).overloadchecked) and
  924. (po_overload in tprocsym(sym).first_procdef.procoptions) and
  925. (tprocsym(sym).owner.symtabletype=objectsymtable) then
  926. search_class_overloads(tprocsym(sym));
  927. for i:=1 to tprocsym(sym).procdef_count do
  928. begin
  929. implprocdef:=tprocsym(sym).procdef[i];
  930. if (compare_paras(proc.para,implprocdef.para,cp_none,[])>=te_equal) and
  931. (proc.proccalloption=implprocdef.proccalloption) then
  932. begin
  933. gintfgetcprocdef:=implprocdef;
  934. exit;
  935. end;
  936. end;
  937. end;
  938. end;
  939. procedure tclassheader.gintfdoonintf(intf: tobjectdef; intfindex: longint);
  940. var
  941. def: tdef;
  942. procname: string; { for error }
  943. mappedname: string;
  944. nextexist: pointer;
  945. implprocdef: tprocdef;
  946. begin
  947. def:=tdef(intf.symtable.defindex.first);
  948. while assigned(def) do
  949. begin
  950. if def.deftype=procdef then
  951. begin
  952. procname:='';
  953. implprocdef:=nil;
  954. nextexist:=nil;
  955. repeat
  956. mappedname:=_class.implementedinterfaces.getmappings(intfindex,tprocdef(def).procsym.name,nextexist);
  957. if procname='' then
  958. procname:=tprocdef(def).procsym.name;
  959. //mappedname; { for error messages }
  960. if mappedname<>'' then
  961. implprocdef:=gintfgetcprocdef(tprocdef(def),mappedname);
  962. until assigned(implprocdef) or not assigned(nextexist);
  963. if not assigned(implprocdef) then
  964. implprocdef:=gintfgetcprocdef(tprocdef(def),tprocdef(def).procsym.name);
  965. if procname='' then
  966. procname:=tprocdef(def).procsym.name;
  967. if assigned(implprocdef) then
  968. _class.implementedinterfaces.addimplproc(intfindex,implprocdef)
  969. else
  970. Message1(sym_e_no_matching_implementation_found,tprocdef(def).fullprocname(false));
  971. end;
  972. def:=tdef(def.indexnext);
  973. end;
  974. end;
  975. procedure tclassheader.gintfwalkdowninterface(intf: tobjectdef; intfindex: longint);
  976. begin
  977. if assigned(intf.childof) then
  978. gintfwalkdowninterface(intf.childof,intfindex);
  979. gintfdoonintf(intf,intfindex);
  980. end;
  981. function tclassheader.genintftable: tasmlabel;
  982. var
  983. intfindex: longint;
  984. curintf: tobjectdef;
  985. intftable: tasmlabel;
  986. begin
  987. { 1. step collect implementor functions into the implementedinterfaces.implprocs }
  988. for intfindex:=1 to _class.implementedinterfaces.count do
  989. begin
  990. curintf:=_class.implementedinterfaces.interfaces(intfindex);
  991. gintfwalkdowninterface(curintf,intfindex);
  992. end;
  993. { 2. step calc required fieldcount and their offsets in the object memory map
  994. and write data }
  995. objectlibrary.getdatalabel(intftable);
  996. dataSegment.concat(tai_align.create(const_align(POINTER_SIZE)));
  997. dataSegment.concat(Tai_label.Create(intftable));
  998. gintfwritedata;
  999. _class.implementedinterfaces.clearimplprocs; { release temporary information }
  1000. genintftable:=intftable;
  1001. end;
  1002. { Write interface identifiers to the data section }
  1003. procedure tclassheader.writeinterfaceids;
  1004. var
  1005. i: longint;
  1006. begin
  1007. if assigned(_class.iidguid) then
  1008. begin
  1009. if (cs_create_smart in aktmoduleswitches) then
  1010. dataSegment.concat(Tai_cut.Create);
  1011. dataSegment.concat(Tai_symbol.Createname_global(make_mangledname('IID',_class.owner,_class.objname^),0));
  1012. dataSegment.concat(Tai_const.Create_32bit(_class.iidguid^.D1));
  1013. dataSegment.concat(Tai_const.Create_16bit(_class.iidguid^.D2));
  1014. dataSegment.concat(Tai_const.Create_16bit(_class.iidguid^.D3));
  1015. for i:=Low(_class.iidguid^.D4) to High(_class.iidguid^.D4) do
  1016. dataSegment.concat(Tai_const.Create_8bit(_class.iidguid^.D4[i]));
  1017. end;
  1018. if (cs_create_smart in aktmoduleswitches) then
  1019. dataSegment.concat(Tai_cut.Create);
  1020. dataSegment.concat(Tai_symbol.Createname_global(make_mangledname('IIDSTR',_class.owner,_class.objname^),0));
  1021. dataSegment.concat(Tai_const.Create_8bit(length(_class.iidstr^)));
  1022. dataSegment.concat(Tai_string.Create(_class.iidstr^));
  1023. end;
  1024. procedure tclassheader.writevirtualmethods(List:TAAsmoutput);
  1025. var
  1026. symcoll : psymcoll;
  1027. procdefcoll : pprocdefcoll;
  1028. i : longint;
  1029. begin
  1030. { walk trough all numbers for virtual methods and search }
  1031. { the method }
  1032. for i:=0 to nextvirtnumber-1 do
  1033. begin
  1034. symcoll:=wurzel;
  1035. { walk trough all symbols }
  1036. while assigned(symcoll) do
  1037. begin
  1038. { walk trough all methods }
  1039. procdefcoll:=symcoll^.data;
  1040. while assigned(procdefcoll) do
  1041. begin
  1042. { writes the addresses to the VMT }
  1043. { but only this which are declared as virtual }
  1044. if procdefcoll^.data.extnumber=i then
  1045. begin
  1046. if (po_virtualmethod in procdefcoll^.data.procoptions) then
  1047. begin
  1048. { if a method is abstract, then is also the }
  1049. { class abstract and it's not allow to }
  1050. { generates an instance }
  1051. if (po_abstractmethod in procdefcoll^.data.procoptions) then
  1052. List.concat(Tai_const_symbol.Createname('FPC_ABSTRACTERROR'))
  1053. else
  1054. List.concat(Tai_const_symbol.createname(procdefcoll^.data.mangledname));
  1055. end;
  1056. end;
  1057. procdefcoll:=procdefcoll^.next;
  1058. end;
  1059. symcoll:=symcoll^.next;
  1060. end;
  1061. end;
  1062. end;
  1063. { generates the vmt for classes as well as for objects }
  1064. procedure tclassheader.writevmt;
  1065. var
  1066. methodnametable,intmessagetable,
  1067. strmessagetable,classnamelabel,
  1068. fieldtablelabel : tasmlabel;
  1069. {$ifdef WITHDMT}
  1070. dmtlabel : tasmlabel;
  1071. {$endif WITHDMT}
  1072. interfacetable : tasmlabel;
  1073. begin
  1074. {$ifdef WITHDMT}
  1075. dmtlabel:=gendmt;
  1076. {$endif WITHDMT}
  1077. if (cs_create_smart in aktmoduleswitches) then
  1078. dataSegment.concat(Tai_cut.Create);
  1079. { write tables for classes, this must be done before the actual
  1080. class is written, because we need the labels defined }
  1081. if is_class(_class) then
  1082. begin
  1083. { interface table }
  1084. if _class.implementedinterfaces.count>0 then
  1085. begin
  1086. if (cs_create_smart in aktmoduleswitches) then
  1087. codeSegment.concat(Tai_cut.Create);
  1088. interfacetable:=genintftable;
  1089. end;
  1090. methodnametable:=genpublishedmethodstable;
  1091. fieldtablelabel:=_class.generate_field_table;
  1092. { write class name }
  1093. objectlibrary.getdatalabel(classnamelabel);
  1094. dataSegment.concat(tai_align.create(const_align(POINTER_SIZE)));
  1095. dataSegment.concat(Tai_label.Create(classnamelabel));
  1096. dataSegment.concat(Tai_const.Create_8bit(length(_class.objrealname^)));
  1097. dataSegment.concat(Tai_string.Create(_class.objrealname^));
  1098. { generate message and dynamic tables }
  1099. if (oo_has_msgstr in _class.objectoptions) then
  1100. strmessagetable:=genstrmsgtab;
  1101. if (oo_has_msgint in _class.objectoptions) then
  1102. intmessagetable:=genintmsgtab;
  1103. end;
  1104. { write debug info }
  1105. {$ifdef GDB}
  1106. if (cs_debuginfo in aktmoduleswitches) then
  1107. begin
  1108. do_count_dbx:=true;
  1109. if assigned(_class.owner) and assigned(_class.owner.name) then
  1110. dataSegment.concat(Tai_stabs.Create(strpnew('"vmt_'+_class.owner.name^+_class.name+':S'+
  1111. typeglobalnumber('__vtbl_ptr_type')+'",'+tostr(N_STSYM)+',0,0,'+_class.vmt_mangledname)));
  1112. end;
  1113. {$endif GDB}
  1114. dataSegment.concat(tai_align.create(const_align(POINTER_SIZE)));
  1115. dataSegment.concat(Tai_symbol.Createdataname_global(_class.vmt_mangledname,0));
  1116. { determine the size with symtable.datasize, because }
  1117. { size gives back 4 for classes }
  1118. dataSegment.concat(Tai_const.Create_32bit(tobjectsymtable(_class.symtable).datasize));
  1119. dataSegment.concat(Tai_const.Create_32bit(Cardinal(-tobjectsymtable(_class.symtable).datasize)));
  1120. {$ifdef WITHDMT}
  1121. if _class.classtype=ct_object then
  1122. begin
  1123. if assigned(dmtlabel) then
  1124. dataSegment.concat(Tai_const_symbol.Create(dmtlabel)))
  1125. else
  1126. dataSegment.concat(Tai_const.Create_32bit(0));
  1127. end;
  1128. {$endif WITHDMT}
  1129. { write pointer to parent VMT, this isn't implemented in TP }
  1130. { but this is not used in FPC ? (PM) }
  1131. { it's not used yet, but the delphi-operators as and is need it (FK) }
  1132. { it is not written for parents that don't have any vmt !! }
  1133. if assigned(_class.childof) and
  1134. (oo_has_vmt in _class.childof.objectoptions) then
  1135. dataSegment.concat(Tai_const_symbol.Createname(_class.childof.vmt_mangledname))
  1136. else
  1137. dataSegment.concat(Tai_const.Create_32bit(0));
  1138. { write extended info for classes, for the order see rtl/inc/objpash.inc }
  1139. if is_class(_class) then
  1140. begin
  1141. { pointer to class name string }
  1142. dataSegment.concat(Tai_const_symbol.Create(classnamelabel));
  1143. { pointer to dynamic table }
  1144. if (oo_has_msgint in _class.objectoptions) then
  1145. dataSegment.concat(Tai_const_symbol.Create(intmessagetable))
  1146. else
  1147. dataSegment.concat(Tai_const.Create_32bit(0));
  1148. { pointer to method table }
  1149. if assigned(methodnametable) then
  1150. dataSegment.concat(Tai_const_symbol.Create(methodnametable))
  1151. else
  1152. dataSegment.concat(Tai_const.Create_32bit(0));
  1153. { pointer to field table }
  1154. dataSegment.concat(Tai_const_symbol.Create(fieldtablelabel));
  1155. { pointer to type info of published section }
  1156. if (oo_can_have_published in _class.objectoptions) then
  1157. dataSegment.concat(Tai_const_symbol.Create(_class.get_rtti_label(fullrtti)))
  1158. else
  1159. dataSegment.concat(Tai_const.Create_32bit(0));
  1160. { inittable for con-/destruction }
  1161. if _class.members_need_inittable then
  1162. dataSegment.concat(Tai_const_symbol.Create(_class.get_rtti_label(initrtti)))
  1163. else
  1164. dataSegment.concat(Tai_const.Create_32bit(0));
  1165. { auto table }
  1166. dataSegment.concat(Tai_const.Create_32bit(0));
  1167. { interface table }
  1168. if _class.implementedinterfaces.count>0 then
  1169. dataSegment.concat(Tai_const_symbol.Create(interfacetable))
  1170. else
  1171. dataSegment.concat(Tai_const.Create_32bit(0));
  1172. { table for string messages }
  1173. if (oo_has_msgstr in _class.objectoptions) then
  1174. dataSegment.concat(Tai_const_symbol.Create(strmessagetable))
  1175. else
  1176. dataSegment.concat(Tai_const.Create_32bit(0));
  1177. end;
  1178. { write virtual methods }
  1179. writevirtualmethods(dataSegment);
  1180. { write the size of the VMT }
  1181. dataSegment.concat(Tai_symbol_end.Createname(_class.vmt_mangledname));
  1182. end;
  1183. procedure tclassheader.adjustselfvalue(procdef: tprocdef;ioffset: aword);
  1184. var
  1185. hsym : tsym;
  1186. href : treference;
  1187. locpara : tparalocation;
  1188. begin
  1189. { calculate the parameter info for the procdef }
  1190. if not procdef.has_paraloc_info then
  1191. begin
  1192. paramanager.create_paraloc_info(procdef,callerside);
  1193. procdef.has_paraloc_info:=true;
  1194. end;
  1195. hsym:=tsym(procdef.parast.search('self'));
  1196. if not(assigned(hsym) and
  1197. (hsym.typ=varsym) and
  1198. assigned(tvarsym(hsym).paraitem)) then
  1199. internalerror(200305251);
  1200. locpara:=tvarsym(hsym).paraitem.paraloc[callerside];
  1201. case locpara.loc of
  1202. LOC_REGISTER:
  1203. cg.a_op_const_reg(exprasmlist,OP_SUB,locpara.size,ioffset,locpara.register);
  1204. LOC_REFERENCE:
  1205. begin
  1206. { offset in the wrapper needs to be adjusted for the stored
  1207. return address }
  1208. reference_reset_base(href,locpara.reference.index,locpara.reference.offset+POINTER_SIZE);
  1209. cg.a_op_const_ref(exprasmlist,OP_SUB,locpara.size,ioffset,href);
  1210. end
  1211. else
  1212. internalerror(200309189);
  1213. end;
  1214. end;
  1215. initialization
  1216. cclassheader:=tclassheader;
  1217. end.
  1218. {
  1219. $Log$
  1220. Revision 1.59 2004-01-28 20:30:18 peter
  1221. * record alignment splitted in fieldalignment and recordalignment,
  1222. the latter is used when this record is inserted in another record.
  1223. Revision 1.58 2004/01/21 14:22:00 florian
  1224. + reintroduce implemented
  1225. Revision 1.57 2003/12/08 22:34:24 peter
  1226. * tai_const.create_32bit changed to cardinal
  1227. Revision 1.56 2003/11/28 17:24:22 peter
  1228. * reversed offset calculation for caller side so it works
  1229. correctly for interfaces
  1230. Revision 1.55 2003/10/30 16:23:13 peter
  1231. * don't search for overloads in parents for constructors
  1232. Revision 1.54 2003/10/29 19:48:50 peter
  1233. * renamed mangeldname_prefix to make_mangledname and made it more
  1234. generic
  1235. * make_mangledname is now also used for internal threadvar/resstring
  1236. lists
  1237. * Add P$ in front of program modulename to prevent duplicated symbols
  1238. at assembler level, because the main program can have the same name
  1239. as a unit, see webtbs/tw1251b
  1240. Revision 1.53 2003/10/13 14:05:12 peter
  1241. * removed is_visible_for_proc
  1242. * search also for class overloads when finding interface
  1243. implementations
  1244. Revision 1.52 2003/10/10 17:48:13 peter
  1245. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  1246. * tregisteralloctor renamed to trgobj
  1247. * removed rgobj from a lot of units
  1248. * moved location_* and reference_* to cgobj
  1249. * first things for mmx register allocation
  1250. Revision 1.51 2003/10/07 21:14:32 peter
  1251. * compare_paras() has a parameter to ignore hidden parameters
  1252. * cross unit overload searching ignores hidden parameters when
  1253. comparing parameter lists. Now function(string):string is
  1254. not overriden with procedure(string) which has the same visible
  1255. parameter list
  1256. Revision 1.50 2003/10/07 20:44:22 peter
  1257. * inherited forced calling convention
  1258. * show hints when forward doesn't match
  1259. Revision 1.49 2003/10/01 20:34:49 peter
  1260. * procinfo unit contains tprocinfo
  1261. * cginfo renamed to cgbase
  1262. * moved cgmessage to verbose
  1263. * fixed ppc and sparc compiles
  1264. Revision 1.48 2003/09/23 17:56:05 peter
  1265. * locals and paras are allocated in the code generation
  1266. * tvarsym.localloc contains the location of para/local when
  1267. generating code for the current procedure
  1268. Revision 1.47 2003/08/21 14:47:41 peter
  1269. * remove convert_registers
  1270. Revision 1.46 2003/08/20 09:07:00 daniel
  1271. * New register coding now mandatory, some more convert_registers calls
  1272. removed.
  1273. Revision 1.45 2003/08/10 17:25:23 peter
  1274. * fixed some reported bugs
  1275. Revision 1.44 2003/06/01 21:38:06 peter
  1276. * getregisterfpu size parameter added
  1277. * op_const_reg size parameter added
  1278. * sparc updates
  1279. Revision 1.43 2003/05/23 14:27:35 peter
  1280. * remove some unit dependencies
  1281. * current_procinfo changes to store more info
  1282. Revision 1.42 2003/04/25 20:59:33 peter
  1283. * removed funcretn,funcretsym, function result is now in varsym
  1284. and aliases for result and function name are added using absolutesym
  1285. * vs_hidden parameter for funcret passed in parameter
  1286. * vs_hidden fixes
  1287. * writenode changed to printnode and released from extdebug
  1288. * -vp option added to generate a tree.log with the nodetree
  1289. * nicer printnode for statements, callnode
  1290. Revision 1.41 2003/04/23 10:11:22 peter
  1291. * range check error for GUID fixed
  1292. Revision 1.40 2003/01/13 14:54:34 daniel
  1293. * Further work to convert codegenerator register convention;
  1294. internalerror bug fixed.
  1295. Revision 1.39 2003/01/09 21:52:37 peter
  1296. * merged some verbosity options.
  1297. * V_LineInfo is a verbosity flag to include line info
  1298. Revision 1.38 2002/11/25 17:43:20 peter
  1299. * splitted defbase in defutil,symutil,defcmp
  1300. * merged isconvertable and is_equal into compare_defs(_ext)
  1301. * made operator search faster by walking the list only once
  1302. Revision 1.37 2002/11/17 16:31:56 carl
  1303. * memory optimization (3-4%) : cleanup of tai fields,
  1304. cleanup of tdef and tsym fields.
  1305. * make it work for m68k
  1306. Revision 1.36 2002/11/15 01:58:52 peter
  1307. * merged changes from 1.0.7 up to 04-11
  1308. - -V option for generating bug report tracing
  1309. - more tracing for option parsing
  1310. - errors for cdecl and high()
  1311. - win32 import stabs
  1312. - win32 records<=8 are returned in eax:edx (turned off by default)
  1313. - heaptrc update
  1314. - more info for temp management in .s file with EXTDEBUG
  1315. Revision 1.35 2002/11/09 16:19:43 carl
  1316. - remove superfluous data in classname
  1317. Revision 1.34 2002/11/09 15:35:35 carl
  1318. * major alignment updates for objects/class tables
  1319. Revision 1.33 2002/10/20 15:33:36 peter
  1320. * having overloads is the same as overload directive for hiding of
  1321. parent methods. This is required becuase it can be possible that a
  1322. method will then hide a method in the parent that an overloaded
  1323. method requires. See webbug tw2185
  1324. Revision 1.32 2002/10/19 15:09:24 peter
  1325. + tobjectdef.members_need_inittable that is used to generate only the
  1326. inittable when it is really used. This saves a lot of useless calls
  1327. to fpc_finalize when destroying classes
  1328. Revision 1.31 2002/10/15 19:00:42 peter
  1329. * small tweak to use speedvalue before comparing strings
  1330. Revision 1.30 2002/10/06 16:40:25 florian
  1331. * interface wrapper name mangling improved
  1332. Revision 1.29 2002/10/05 12:43:25 carl
  1333. * fixes for Delphi 6 compilation
  1334. (warning : Some features do not work under Delphi)
  1335. Revision 1.28 2002/09/16 14:11:13 peter
  1336. * add argument to equal_paras() to support default values or not
  1337. Revision 1.27 2002/09/03 16:26:26 daniel
  1338. * Make Tprocdef.defs protected
  1339. Revision 1.26 2002/09/03 15:44:44 peter
  1340. * fixed private methods hiding public virtual methods
  1341. Revision 1.25 2002/08/11 14:32:27 peter
  1342. * renamed current_library to objectlibrary
  1343. Revision 1.24 2002/08/11 13:24:12 peter
  1344. * saving of asmsymbols in ppu supported
  1345. * asmsymbollist global is removed and moved into a new class
  1346. tasmlibrarydata that will hold the info of a .a file which
  1347. corresponds with a single module. Added librarydata to tmodule
  1348. to keep the library info stored for the module. In the future the
  1349. objectfiles will also be stored to the tasmlibrarydata class
  1350. * all getlabel/newasmsymbol and friends are moved to the new class
  1351. Revision 1.23 2002/08/09 07:33:01 florian
  1352. * a couple of interface related fixes
  1353. Revision 1.22 2002/07/20 11:57:55 florian
  1354. * types.pas renamed to defbase.pas because D6 contains a types
  1355. unit so this would conflicts if D6 programms are compiled
  1356. + Willamette/SSE2 instructions to assembler added
  1357. Revision 1.21 2002/07/01 18:46:23 peter
  1358. * internal linker
  1359. * reorganized aasm layer
  1360. Revision 1.20 2002/05/18 13:34:10 peter
  1361. * readded missing revisions
  1362. Revision 1.19 2002/05/16 19:46:39 carl
  1363. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1364. + try to fix temp allocation (still in ifdef)
  1365. + generic constructor calls
  1366. + start of tassembler / tmodulebase class cleanup
  1367. Revision 1.17 2002/05/12 16:53:08 peter
  1368. * moved entry and exitcode to ncgutil and cgobj
  1369. * foreach gets extra argument for passing local data to the
  1370. iterator function
  1371. * -CR checks also class typecasts at runtime by changing them
  1372. into as
  1373. * fixed compiler to cycle with the -CR option
  1374. * fixed stabs with elf writer, finally the global variables can
  1375. be watched
  1376. * removed a lot of routines from cga unit and replaced them by
  1377. calls to cgobj
  1378. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1379. u32bit then the other is typecasted also to u32bit without giving
  1380. a rangecheck warning/error.
  1381. * fixed pascal calling method with reversing also the high tree in
  1382. the parast, detected by tcalcst3 test
  1383. Revision 1.16 2002/04/20 21:32:24 carl
  1384. + generic FPC_CHECKPOINTER
  1385. + first parameter offset in stack now portable
  1386. * rename some constants
  1387. + move some cpu stuff to other units
  1388. - remove unused constents
  1389. * fix stacksize for some targets
  1390. * fix generic size problems which depend now on EXTEND_SIZE constant
  1391. Revision 1.15 2002/04/19 15:46:01 peter
  1392. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  1393. in most cases and not written to the ppu
  1394. * add mangeledname_prefix() routine to generate the prefix of
  1395. manglednames depending on the current procedure, object and module
  1396. * removed static procprefix since the mangledname is now build only
  1397. on demand from tprocdef.mangledname
  1398. Revision 1.14 2002/04/15 18:59:07 carl
  1399. + target_info.size_of_pointer -> pointer_Size
  1400. Revision 1.13 2002/02/11 18:51:35 peter
  1401. * fixed vmt generation for private procedures that were skipped after
  1402. my previous changes
  1403. }