nobj.pas 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  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,rgobj
  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,false,false)>=te_equal)) then
  545. begin
  546. if is_visible then
  547. procdefcoll^.hidden:=true;
  548. if _class=pd._class 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,false,false)>=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 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,false,false)>=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 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,false,false)>=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,false,false)>=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:=mangledname_prefix('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:=mangledname_prefix('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. if (dataalignment>=pointer_size) then
  888. datasize:=align(datasize,dataalignment)
  889. else
  890. datasize:=align(datasize,pointer_size);
  891. _class.implementedinterfaces.ioffsets(i)^:=datasize;
  892. datasize:=datasize+pointer_size;
  893. end;
  894. { write vtbl }
  895. gintfcreatevtbl(i,rawdata,rawcode);
  896. end;
  897. end;
  898. { second pass: for fill interfacetable and remained ioffsets }
  899. for i:=1 to max do
  900. begin
  901. if i<>impintfindexes[i] then { why execute x:=x ? }
  902. with _class.implementedinterfaces do
  903. ioffsets(i)^:=ioffsets(impintfindexes[i])^;
  904. gintfgenentry(i,impintfindexes[i],rawdata);
  905. end;
  906. dataSegment.concatlist(rawdata);
  907. rawdata.free;
  908. codeSegment.concatlist(rawcode);
  909. rawcode.free;
  910. freemem(impintfindexes,(max+1)*sizeof(longint));
  911. end;
  912. function tclassheader.gintfgetcprocdef(proc: tprocdef;const name: string): tprocdef;
  913. var
  914. sym: tprocsym;
  915. implprocdef : Tprocdef;
  916. i: cardinal;
  917. begin
  918. gintfgetcprocdef:=nil;
  919. sym:=tprocsym(search_class_member(_class,name));
  920. if assigned(sym) and (sym.typ=procsym) then
  921. for i:=1 to sym.procdef_count do
  922. begin
  923. implprocdef:=sym.procdef[i];
  924. if (compare_paras(proc.para,implprocdef.para,cp_none,false,false)>=te_equal) and
  925. (proc.proccalloption=implprocdef.proccalloption) then
  926. begin
  927. gintfgetcprocdef:=implprocdef;
  928. exit;
  929. end;
  930. end;
  931. end;
  932. procedure tclassheader.gintfdoonintf(intf: tobjectdef; intfindex: longint);
  933. var
  934. i: longint;
  935. proc: tprocdef;
  936. procname: string; { for error }
  937. mappedname: string;
  938. nextexist: pointer;
  939. implprocdef: tprocdef;
  940. begin
  941. for i:=1 to intf.symtable.defindex.count do
  942. begin
  943. proc:=tprocdef(intf.symtable.defindex.search(i));
  944. if proc.deftype=procdef then
  945. begin
  946. procname:='';
  947. implprocdef:=nil;
  948. nextexist:=nil;
  949. repeat
  950. mappedname:=_class.implementedinterfaces.getmappings(intfindex,proc.procsym.name,nextexist);
  951. if procname='' then
  952. procname:=proc.procsym.name;
  953. //mappedname; { for error messages }
  954. if mappedname<>'' then
  955. implprocdef:=gintfgetcprocdef(proc,mappedname);
  956. until assigned(implprocdef) or not assigned(nextexist);
  957. if not assigned(implprocdef) then
  958. implprocdef:=gintfgetcprocdef(proc,proc.procsym.name);
  959. if procname='' then
  960. procname:=proc.procsym.name;
  961. if assigned(implprocdef) then
  962. _class.implementedinterfaces.addimplproc(intfindex,implprocdef)
  963. else
  964. Message1(sym_e_no_matching_implementation_found,proc.fullprocname(false));
  965. end;
  966. end;
  967. end;
  968. procedure tclassheader.gintfwalkdowninterface(intf: tobjectdef; intfindex: longint);
  969. begin
  970. if assigned(intf.childof) then
  971. gintfwalkdowninterface(intf.childof,intfindex);
  972. gintfdoonintf(intf,intfindex);
  973. end;
  974. function tclassheader.genintftable: tasmlabel;
  975. var
  976. intfindex: longint;
  977. curintf: tobjectdef;
  978. intftable: tasmlabel;
  979. begin
  980. { 1. step collect implementor functions into the implementedinterfaces.implprocs }
  981. for intfindex:=1 to _class.implementedinterfaces.count do
  982. begin
  983. curintf:=_class.implementedinterfaces.interfaces(intfindex);
  984. gintfwalkdowninterface(curintf,intfindex);
  985. end;
  986. { 2. step calc required fieldcount and their offsets in the object memory map
  987. and write data }
  988. objectlibrary.getdatalabel(intftable);
  989. dataSegment.concat(tai_align.create(const_align(POINTER_SIZE)));
  990. dataSegment.concat(Tai_label.Create(intftable));
  991. gintfwritedata;
  992. _class.implementedinterfaces.clearimplprocs; { release temporary information }
  993. genintftable:=intftable;
  994. end;
  995. { Write interface identifiers to the data section }
  996. procedure tclassheader.writeinterfaceids;
  997. var
  998. i: longint;
  999. begin
  1000. if assigned(_class.iidguid) then
  1001. begin
  1002. if (cs_create_smart in aktmoduleswitches) then
  1003. dataSegment.concat(Tai_cut.Create);
  1004. dataSegment.concat(Tai_symbol.Createname_global(mangledname_prefix('IID',_class.owner)+_class.objname^,0));
  1005. dataSegment.concat(Tai_const.Create_32bit(longint(_class.iidguid^.D1)));
  1006. dataSegment.concat(Tai_const.Create_16bit(_class.iidguid^.D2));
  1007. dataSegment.concat(Tai_const.Create_16bit(_class.iidguid^.D3));
  1008. for i:=Low(_class.iidguid^.D4) to High(_class.iidguid^.D4) do
  1009. dataSegment.concat(Tai_const.Create_8bit(_class.iidguid^.D4[i]));
  1010. end;
  1011. if (cs_create_smart in aktmoduleswitches) then
  1012. dataSegment.concat(Tai_cut.Create);
  1013. dataSegment.concat(Tai_symbol.Createname_global(mangledname_prefix('IIDSTR',_class.owner)+_class.objname^,0));
  1014. dataSegment.concat(Tai_const.Create_8bit(length(_class.iidstr^)));
  1015. dataSegment.concat(Tai_string.Create(_class.iidstr^));
  1016. end;
  1017. procedure tclassheader.writevirtualmethods(List:TAAsmoutput);
  1018. var
  1019. symcoll : psymcoll;
  1020. procdefcoll : pprocdefcoll;
  1021. i : longint;
  1022. begin
  1023. { walk trough all numbers for virtual methods and search }
  1024. { the method }
  1025. for i:=0 to nextvirtnumber-1 do
  1026. begin
  1027. symcoll:=wurzel;
  1028. { walk trough all symbols }
  1029. while assigned(symcoll) do
  1030. begin
  1031. { walk trough all methods }
  1032. procdefcoll:=symcoll^.data;
  1033. while assigned(procdefcoll) do
  1034. begin
  1035. { writes the addresses to the VMT }
  1036. { but only this which are declared as virtual }
  1037. if procdefcoll^.data.extnumber=i then
  1038. begin
  1039. if (po_virtualmethod in procdefcoll^.data.procoptions) then
  1040. begin
  1041. { if a method is abstract, then is also the }
  1042. { class abstract and it's not allow to }
  1043. { generates an instance }
  1044. if (po_abstractmethod in procdefcoll^.data.procoptions) then
  1045. List.concat(Tai_const_symbol.Createname('FPC_ABSTRACTERROR'))
  1046. else
  1047. List.concat(Tai_const_symbol.createname(procdefcoll^.data.mangledname));
  1048. end;
  1049. end;
  1050. procdefcoll:=procdefcoll^.next;
  1051. end;
  1052. symcoll:=symcoll^.next;
  1053. end;
  1054. end;
  1055. end;
  1056. { generates the vmt for classes as well as for objects }
  1057. procedure tclassheader.writevmt;
  1058. var
  1059. methodnametable,intmessagetable,
  1060. strmessagetable,classnamelabel,
  1061. fieldtablelabel : tasmlabel;
  1062. {$ifdef WITHDMT}
  1063. dmtlabel : tasmlabel;
  1064. {$endif WITHDMT}
  1065. interfacetable : tasmlabel;
  1066. begin
  1067. {$ifdef WITHDMT}
  1068. dmtlabel:=gendmt;
  1069. {$endif WITHDMT}
  1070. if (cs_create_smart in aktmoduleswitches) then
  1071. dataSegment.concat(Tai_cut.Create);
  1072. { write tables for classes, this must be done before the actual
  1073. class is written, because we need the labels defined }
  1074. if is_class(_class) then
  1075. begin
  1076. { interface table }
  1077. if _class.implementedinterfaces.count>0 then
  1078. begin
  1079. if (cs_create_smart in aktmoduleswitches) then
  1080. codeSegment.concat(Tai_cut.Create);
  1081. interfacetable:=genintftable;
  1082. end;
  1083. methodnametable:=genpublishedmethodstable;
  1084. fieldtablelabel:=_class.generate_field_table;
  1085. { write class name }
  1086. objectlibrary.getdatalabel(classnamelabel);
  1087. dataSegment.concat(tai_align.create(const_align(POINTER_SIZE)));
  1088. dataSegment.concat(Tai_label.Create(classnamelabel));
  1089. dataSegment.concat(Tai_const.Create_8bit(length(_class.objrealname^)));
  1090. dataSegment.concat(Tai_string.Create(_class.objrealname^));
  1091. { generate message and dynamic tables }
  1092. if (oo_has_msgstr in _class.objectoptions) then
  1093. strmessagetable:=genstrmsgtab;
  1094. if (oo_has_msgint in _class.objectoptions) then
  1095. intmessagetable:=genintmsgtab;
  1096. end;
  1097. { write debug info }
  1098. {$ifdef GDB}
  1099. if (cs_debuginfo in aktmoduleswitches) then
  1100. begin
  1101. do_count_dbx:=true;
  1102. if assigned(_class.owner) and assigned(_class.owner.name) then
  1103. dataSegment.concat(Tai_stabs.Create(strpnew('"vmt_'+_class.owner.name^+_class.name+':S'+
  1104. typeglobalnumber('__vtbl_ptr_type')+'",'+tostr(N_STSYM)+',0,0,'+_class.vmt_mangledname)));
  1105. end;
  1106. {$endif GDB}
  1107. dataSegment.concat(tai_align.create(const_align(POINTER_SIZE)));
  1108. dataSegment.concat(Tai_symbol.Createdataname_global(_class.vmt_mangledname,0));
  1109. { determine the size with symtable.datasize, because }
  1110. { size gives back 4 for classes }
  1111. dataSegment.concat(Tai_const.Create_32bit(tobjectsymtable(_class.symtable).datasize));
  1112. dataSegment.concat(Tai_const.Create_32bit(-tobjectsymtable(_class.symtable).datasize));
  1113. {$ifdef WITHDMT}
  1114. if _class.classtype=ct_object then
  1115. begin
  1116. if assigned(dmtlabel) then
  1117. dataSegment.concat(Tai_const_symbol.Create(dmtlabel)))
  1118. else
  1119. dataSegment.concat(Tai_const.Create_32bit(0));
  1120. end;
  1121. {$endif WITHDMT}
  1122. { write pointer to parent VMT, this isn't implemented in TP }
  1123. { but this is not used in FPC ? (PM) }
  1124. { it's not used yet, but the delphi-operators as and is need it (FK) }
  1125. { it is not written for parents that don't have any vmt !! }
  1126. if assigned(_class.childof) and
  1127. (oo_has_vmt in _class.childof.objectoptions) then
  1128. dataSegment.concat(Tai_const_symbol.Createname(_class.childof.vmt_mangledname))
  1129. else
  1130. dataSegment.concat(Tai_const.Create_32bit(0));
  1131. { write extended info for classes, for the order see rtl/inc/objpash.inc }
  1132. if is_class(_class) then
  1133. begin
  1134. { pointer to class name string }
  1135. dataSegment.concat(Tai_const_symbol.Create(classnamelabel));
  1136. { pointer to dynamic table }
  1137. if (oo_has_msgint in _class.objectoptions) then
  1138. dataSegment.concat(Tai_const_symbol.Create(intmessagetable))
  1139. else
  1140. dataSegment.concat(Tai_const.Create_32bit(0));
  1141. { pointer to method table }
  1142. if assigned(methodnametable) then
  1143. dataSegment.concat(Tai_const_symbol.Create(methodnametable))
  1144. else
  1145. dataSegment.concat(Tai_const.Create_32bit(0));
  1146. { pointer to field table }
  1147. dataSegment.concat(Tai_const_symbol.Create(fieldtablelabel));
  1148. { pointer to type info of published section }
  1149. if (oo_can_have_published in _class.objectoptions) then
  1150. dataSegment.concat(Tai_const_symbol.Create(_class.get_rtti_label(fullrtti)))
  1151. else
  1152. dataSegment.concat(Tai_const.Create_32bit(0));
  1153. { inittable for con-/destruction }
  1154. if _class.members_need_inittable then
  1155. dataSegment.concat(Tai_const_symbol.Create(_class.get_rtti_label(initrtti)))
  1156. else
  1157. dataSegment.concat(Tai_const.Create_32bit(0));
  1158. { auto table }
  1159. dataSegment.concat(Tai_const.Create_32bit(0));
  1160. { interface table }
  1161. if _class.implementedinterfaces.count>0 then
  1162. dataSegment.concat(Tai_const_symbol.Create(interfacetable))
  1163. else
  1164. dataSegment.concat(Tai_const.Create_32bit(0));
  1165. { table for string messages }
  1166. if (oo_has_msgstr in _class.objectoptions) then
  1167. dataSegment.concat(Tai_const_symbol.Create(strmessagetable))
  1168. else
  1169. dataSegment.concat(Tai_const.Create_32bit(0));
  1170. end;
  1171. { write virtual methods }
  1172. writevirtualmethods(dataSegment);
  1173. { write the size of the VMT }
  1174. dataSegment.concat(Tai_symbol_end.Createname(_class.vmt_mangledname));
  1175. end;
  1176. procedure tclassheader.adjustselfvalue(procdef: tprocdef;ioffset: aword);
  1177. var
  1178. hsym : tsym;
  1179. href : treference;
  1180. locpara : tparalocation;
  1181. begin
  1182. { calculate the parameter info for the procdef }
  1183. if not procdef.has_paraloc_info then
  1184. begin
  1185. paramanager.create_paraloc_info(procdef,callerside);
  1186. procdef.has_paraloc_info:=true;
  1187. end;
  1188. hsym:=tsym(procdef.parast.search('self'));
  1189. if not(assigned(hsym) and
  1190. (hsym.typ=varsym) and
  1191. assigned(tvarsym(hsym).paraitem)) then
  1192. internalerror(200305251);
  1193. locpara:=tvarsym(hsym).paraitem.paraloc[callerside];
  1194. case locpara.loc of
  1195. LOC_REGISTER:
  1196. cg.a_op_const_reg(exprasmlist,OP_SUB,locpara.size,ioffset,locpara.register);
  1197. LOC_REFERENCE:
  1198. begin
  1199. reference_reset_base(href,locpara.reference.index,locpara.reference.offset);
  1200. cg.a_op_const_ref(exprasmlist,OP_SUB,locpara.size,ioffset,href);
  1201. end
  1202. else
  1203. internalerror(200309189);
  1204. end;
  1205. end;
  1206. initialization
  1207. cclassheader:=tclassheader;
  1208. end.
  1209. {
  1210. $Log$
  1211. Revision 1.51 2003-10-07 21:14:32 peter
  1212. * compare_paras() has a parameter to ignore hidden parameters
  1213. * cross unit overload searching ignores hidden parameters when
  1214. comparing parameter lists. Now function(string):string is
  1215. not overriden with procedure(string) which has the same visible
  1216. parameter list
  1217. Revision 1.50 2003/10/07 20:44:22 peter
  1218. * inherited forced calling convention
  1219. * show hints when forward doesn't match
  1220. Revision 1.49 2003/10/01 20:34:49 peter
  1221. * procinfo unit contains tprocinfo
  1222. * cginfo renamed to cgbase
  1223. * moved cgmessage to verbose
  1224. * fixed ppc and sparc compiles
  1225. Revision 1.48 2003/09/23 17:56:05 peter
  1226. * locals and paras are allocated in the code generation
  1227. * tvarsym.localloc contains the location of para/local when
  1228. generating code for the current procedure
  1229. Revision 1.47 2003/08/21 14:47:41 peter
  1230. * remove convert_registers
  1231. Revision 1.46 2003/08/20 09:07:00 daniel
  1232. * New register coding now mandatory, some more convert_registers calls
  1233. removed.
  1234. Revision 1.45 2003/08/10 17:25:23 peter
  1235. * fixed some reported bugs
  1236. Revision 1.44 2003/06/01 21:38:06 peter
  1237. * getregisterfpu size parameter added
  1238. * op_const_reg size parameter added
  1239. * sparc updates
  1240. Revision 1.43 2003/05/23 14:27:35 peter
  1241. * remove some unit dependencies
  1242. * current_procinfo changes to store more info
  1243. Revision 1.42 2003/04/25 20:59:33 peter
  1244. * removed funcretn,funcretsym, function result is now in varsym
  1245. and aliases for result and function name are added using absolutesym
  1246. * vs_hidden parameter for funcret passed in parameter
  1247. * vs_hidden fixes
  1248. * writenode changed to printnode and released from extdebug
  1249. * -vp option added to generate a tree.log with the nodetree
  1250. * nicer printnode for statements, callnode
  1251. Revision 1.41 2003/04/23 10:11:22 peter
  1252. * range check error for GUID fixed
  1253. Revision 1.40 2003/01/13 14:54:34 daniel
  1254. * Further work to convert codegenerator register convention;
  1255. internalerror bug fixed.
  1256. Revision 1.39 2003/01/09 21:52:37 peter
  1257. * merged some verbosity options.
  1258. * V_LineInfo is a verbosity flag to include line info
  1259. Revision 1.38 2002/11/25 17:43:20 peter
  1260. * splitted defbase in defutil,symutil,defcmp
  1261. * merged isconvertable and is_equal into compare_defs(_ext)
  1262. * made operator search faster by walking the list only once
  1263. Revision 1.37 2002/11/17 16:31:56 carl
  1264. * memory optimization (3-4%) : cleanup of tai fields,
  1265. cleanup of tdef and tsym fields.
  1266. * make it work for m68k
  1267. Revision 1.36 2002/11/15 01:58:52 peter
  1268. * merged changes from 1.0.7 up to 04-11
  1269. - -V option for generating bug report tracing
  1270. - more tracing for option parsing
  1271. - errors for cdecl and high()
  1272. - win32 import stabs
  1273. - win32 records<=8 are returned in eax:edx (turned off by default)
  1274. - heaptrc update
  1275. - more info for temp management in .s file with EXTDEBUG
  1276. Revision 1.35 2002/11/09 16:19:43 carl
  1277. - remove superfluous data in classname
  1278. Revision 1.34 2002/11/09 15:35:35 carl
  1279. * major alignment updates for objects/class tables
  1280. Revision 1.33 2002/10/20 15:33:36 peter
  1281. * having overloads is the same as overload directive for hiding of
  1282. parent methods. This is required becuase it can be possible that a
  1283. method will then hide a method in the parent that an overloaded
  1284. method requires. See webbug tw2185
  1285. Revision 1.32 2002/10/19 15:09:24 peter
  1286. + tobjectdef.members_need_inittable that is used to generate only the
  1287. inittable when it is really used. This saves a lot of useless calls
  1288. to fpc_finalize when destroying classes
  1289. Revision 1.31 2002/10/15 19:00:42 peter
  1290. * small tweak to use speedvalue before comparing strings
  1291. Revision 1.30 2002/10/06 16:40:25 florian
  1292. * interface wrapper name mangling improved
  1293. Revision 1.29 2002/10/05 12:43:25 carl
  1294. * fixes for Delphi 6 compilation
  1295. (warning : Some features do not work under Delphi)
  1296. Revision 1.28 2002/09/16 14:11:13 peter
  1297. * add argument to equal_paras() to support default values or not
  1298. Revision 1.27 2002/09/03 16:26:26 daniel
  1299. * Make Tprocdef.defs protected
  1300. Revision 1.26 2002/09/03 15:44:44 peter
  1301. * fixed private methods hiding public virtual methods
  1302. Revision 1.25 2002/08/11 14:32:27 peter
  1303. * renamed current_library to objectlibrary
  1304. Revision 1.24 2002/08/11 13:24:12 peter
  1305. * saving of asmsymbols in ppu supported
  1306. * asmsymbollist global is removed and moved into a new class
  1307. tasmlibrarydata that will hold the info of a .a file which
  1308. corresponds with a single module. Added librarydata to tmodule
  1309. to keep the library info stored for the module. In the future the
  1310. objectfiles will also be stored to the tasmlibrarydata class
  1311. * all getlabel/newasmsymbol and friends are moved to the new class
  1312. Revision 1.23 2002/08/09 07:33:01 florian
  1313. * a couple of interface related fixes
  1314. Revision 1.22 2002/07/20 11:57:55 florian
  1315. * types.pas renamed to defbase.pas because D6 contains a types
  1316. unit so this would conflicts if D6 programms are compiled
  1317. + Willamette/SSE2 instructions to assembler added
  1318. Revision 1.21 2002/07/01 18:46:23 peter
  1319. * internal linker
  1320. * reorganized aasm layer
  1321. Revision 1.20 2002/05/18 13:34:10 peter
  1322. * readded missing revisions
  1323. Revision 1.19 2002/05/16 19:46:39 carl
  1324. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1325. + try to fix temp allocation (still in ifdef)
  1326. + generic constructor calls
  1327. + start of tassembler / tmodulebase class cleanup
  1328. Revision 1.17 2002/05/12 16:53:08 peter
  1329. * moved entry and exitcode to ncgutil and cgobj
  1330. * foreach gets extra argument for passing local data to the
  1331. iterator function
  1332. * -CR checks also class typecasts at runtime by changing them
  1333. into as
  1334. * fixed compiler to cycle with the -CR option
  1335. * fixed stabs with elf writer, finally the global variables can
  1336. be watched
  1337. * removed a lot of routines from cga unit and replaced them by
  1338. calls to cgobj
  1339. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1340. u32bit then the other is typecasted also to u32bit without giving
  1341. a rangecheck warning/error.
  1342. * fixed pascal calling method with reversing also the high tree in
  1343. the parast, detected by tcalcst3 test
  1344. Revision 1.16 2002/04/20 21:32:24 carl
  1345. + generic FPC_CHECKPOINTER
  1346. + first parameter offset in stack now portable
  1347. * rename some constants
  1348. + move some cpu stuff to other units
  1349. - remove unused constents
  1350. * fix stacksize for some targets
  1351. * fix generic size problems which depend now on EXTEND_SIZE constant
  1352. Revision 1.15 2002/04/19 15:46:01 peter
  1353. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  1354. in most cases and not written to the ppu
  1355. * add mangeledname_prefix() routine to generate the prefix of
  1356. manglednames depending on the current procedure, object and module
  1357. * removed static procprefix since the mangledname is now build only
  1358. on demand from tprocdef.mangledname
  1359. Revision 1.14 2002/04/15 18:59:07 carl
  1360. + target_info.size_of_pointer -> pointer_Size
  1361. Revision 1.13 2002/02/11 18:51:35 peter
  1362. * fixed vmt generation for private procedures that were skipped after
  1363. my previous changes
  1364. }