hcgdata.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. {
  2. $Id$
  3. Copyright (c) 1996-98 by Florian Klaempfl
  4. Routines for the code generation of data structures
  5. like VMT,Messages
  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 hcgdata;
  20. interface
  21. uses
  22. symtable,aasm;
  23. { generates the message tables for a class }
  24. function genstrmsgtab(_class : pobjectdef) : pasmlabel;
  25. function genintmsgtab(_class : pobjectdef) : pasmlabel;
  26. { generates a VMT for _class }
  27. procedure genvmt(_class : pobjectdef);
  28. implementation
  29. uses
  30. strings,cobjects,
  31. globtype,globals,verbose,
  32. types,
  33. hcodegen;
  34. {*****************************************************************************
  35. Message
  36. *****************************************************************************}
  37. type
  38. pprocdeftree = ^tprocdeftree;
  39. tprocdeftree = record
  40. p : pprocdef;
  41. nl : pasmlabel;
  42. l,r : pprocdeftree;
  43. end;
  44. var
  45. root : pprocdeftree;
  46. count : longint;
  47. procedure insertstr(p : pprocdeftree;var at : pprocdeftree);
  48. var
  49. i : longint;
  50. begin
  51. if at=nil then
  52. begin
  53. at:=p;
  54. inc(count);
  55. end
  56. else
  57. begin
  58. i:=strcomp(p^.p^.messageinf.str,at^.p^.messageinf.str);
  59. if i<0 then
  60. insertstr(p,at^.l)
  61. else if i>0 then
  62. insertstr(p,at^.r)
  63. else
  64. Message1(parser_e_duplicate_message_label,strpas(p^.p^.messageinf.str));
  65. end;
  66. end;
  67. procedure disposeprocdeftree(p : pprocdeftree);
  68. begin
  69. if assigned(p^.l) then
  70. disposeprocdeftree(p^.l);
  71. if assigned(p^.r) then
  72. disposeprocdeftree(p^.r);
  73. dispose(p);
  74. end;
  75. procedure insertmsgstr(p : pnamedindexobject);{$ifndef FPC}far;{$endif FPC}
  76. var
  77. hp : pprocdef;
  78. pt : pprocdeftree;
  79. begin
  80. if psym(p)^.typ=procsym then
  81. begin
  82. hp:=pprocsym(p)^.definition;
  83. while assigned(hp) do
  84. begin
  85. if (hp^.options and pomsgstr)<>0 then
  86. begin
  87. new(pt);
  88. pt^.p:=hp;
  89. pt^.l:=nil;
  90. pt^.r:=nil;
  91. insertstr(pt,root);
  92. end;
  93. hp:=hp^.nextoverloaded;
  94. end;
  95. end;
  96. end;
  97. procedure insertint(p : pprocdeftree;var at : pprocdeftree);
  98. var
  99. i : longint;
  100. begin
  101. if at=nil then
  102. begin
  103. at:=p;
  104. inc(count);
  105. end
  106. else
  107. begin
  108. i:=strcomp(p^.p^.messageinf.str,at^.p^.messageinf.str);
  109. if p^.p^.messageinf.i<at^.p^.messageinf.i then
  110. insertstr(p,at^.l)
  111. else if p^.p^.messageinf.i>at^.p^.messageinf.i then
  112. insertstr(p,at^.r)
  113. else
  114. Message1(parser_e_duplicate_message_label,tostr(p^.p^.messageinf.i));
  115. end;
  116. end;
  117. procedure insertmsgint(p : pnamedindexobject);{$ifndef FPC}far;{$endif FPC}
  118. var
  119. hp : pprocdef;
  120. pt : pprocdeftree;
  121. begin
  122. if psym(p)^.typ=procsym then
  123. begin
  124. hp:=pprocsym(p)^.definition;
  125. while assigned(hp) do
  126. begin
  127. if (hp^.options and pomsgint)<>0 then
  128. begin
  129. new(pt);
  130. pt^.p:=hp;
  131. pt^.l:=nil;
  132. pt^.r:=nil;
  133. insertint(pt,root);
  134. end;
  135. hp:=hp^.nextoverloaded;
  136. end;
  137. end;
  138. end;
  139. procedure writenames(p : pprocdeftree);
  140. begin
  141. getdatalabel(p^.nl);
  142. if assigned(p^.l) then
  143. writenames(p^.l);
  144. datasegment^.concat(new(pai_label,init(p^.nl)));
  145. datasegment^.concat(new(pai_const,init_8bit(strlen(p^.p^.messageinf.str))));
  146. datasegment^.concat(new(pai_string,init_pchar(p^.p^.messageinf.str)));
  147. if assigned(p^.r) then
  148. writenames(p^.r);
  149. end;
  150. procedure writestrentry(p : pprocdeftree);
  151. begin
  152. if assigned(p^.l) then
  153. writestrentry(p^.l);
  154. { write name label }
  155. datasegment^.concat(new(pai_const_symbol,init(p^.nl)));
  156. datasegment^.concat(new(pai_const_symbol,initname(p^.p^.mangledname)));
  157. if assigned(p^.r) then
  158. writestrentry(p^.r);
  159. end;
  160. function genstrmsgtab(_class : pobjectdef) : pasmlabel;
  161. var
  162. r : pasmlabel;
  163. begin
  164. root:=nil;
  165. count:=0;
  166. { insert all message handlers into a tree, sorted by name }
  167. _class^.publicsyms^.foreach({$ifdef fpc}@{$endif}insertmsgstr);
  168. { write all names }
  169. if assigned(root) then
  170. writenames(root);
  171. { now start writing of the message string table }
  172. getdatalabel(r);
  173. datasegment^.concat(new(pai_label,init(r)));
  174. genstrmsgtab:=r;
  175. datasegment^.concat(new(pai_const,init_32bit(count)));
  176. if assigned(root) then
  177. begin
  178. writestrentry(root);
  179. disposeprocdeftree(root);
  180. end;
  181. end;
  182. procedure writeintentry(p : pprocdeftree);
  183. begin
  184. if assigned(p^.l) then
  185. writeintentry(p^.l);
  186. { write name label }
  187. datasegment^.concat(new(pai_const,init_32bit(p^.p^.messageinf.i)));
  188. datasegment^.concat(new(pai_const_symbol,initname(p^.p^.mangledname)));
  189. if assigned(p^.r) then
  190. writeintentry(p^.r);
  191. end;
  192. function genintmsgtab(_class : pobjectdef) : pasmlabel;
  193. var
  194. r : pasmlabel;
  195. begin
  196. root:=nil;
  197. count:=0;
  198. { insert all message handlers into a tree, sorted by name }
  199. _class^.publicsyms^.foreach({$ifdef fpc}@{$endif}insertmsgint);
  200. { now start writing of the message string table }
  201. getdatalabel(r);
  202. datasegment^.concat(new(pai_label,init(r)));
  203. genintmsgtab:=r;
  204. datasegment^.concat(new(pai_const,init_32bit(count)));
  205. if assigned(root) then
  206. begin
  207. writeintentry(root);
  208. disposeprocdeftree(root);
  209. end;
  210. end;
  211. {*****************************************************************************
  212. VMT
  213. *****************************************************************************}
  214. type
  215. pprocdefcoll = ^tprocdefcoll;
  216. tprocdefcoll = record
  217. next : pprocdefcoll;
  218. data : pprocdef;
  219. end;
  220. psymcoll = ^tsymcoll;
  221. tsymcoll = record
  222. next : psymcoll;
  223. name : pstring;
  224. data : pprocdefcoll;
  225. end;
  226. var
  227. wurzel : psymcoll;
  228. nextvirtnumber : longint;
  229. _c : pobjectdef;
  230. has_constructor,has_virtual_method : boolean;
  231. procedure eachsym(sym : pnamedindexobject);{$ifndef FPC}far;{$endif FPC}
  232. var
  233. procdefcoll : pprocdefcoll;
  234. hp : pprocdef;
  235. symcoll : psymcoll;
  236. _name : string;
  237. stored : boolean;
  238. { creates a new entry in the procsym list }
  239. procedure newentry;
  240. begin
  241. { if not, generate a new symbol item }
  242. new(symcoll);
  243. symcoll^.name:=stringdup(sym^.name);
  244. symcoll^.next:=wurzel;
  245. symcoll^.data:=nil;
  246. wurzel:=symcoll;
  247. hp:=pprocsym(sym)^.definition;
  248. { inserts all definitions }
  249. while assigned(hp) do
  250. begin
  251. new(procdefcoll);
  252. procdefcoll^.data:=hp;
  253. procdefcoll^.next:=symcoll^.data;
  254. symcoll^.data:=procdefcoll;
  255. { if it's a virtual method }
  256. if (hp^.options and povirtualmethod)<>0 then
  257. begin
  258. { then it gets a number ... }
  259. hp^.extnumber:=nextvirtnumber;
  260. { and we inc the number }
  261. inc(nextvirtnumber);
  262. has_virtual_method:=true;
  263. end;
  264. if (hp^.options and poconstructor)<>0 then
  265. has_constructor:=true;
  266. { check, if a method should be overridden }
  267. if (hp^.options and pooverridingmethod)<>0 then
  268. Message1(parser_e_nothing_to_be_overridden,_c^.objname^+'.'+_name);
  269. { next overloaded method }
  270. hp:=hp^.nextoverloaded;
  271. end;
  272. end;
  273. begin
  274. { put only sub routines into the VMT }
  275. if psym(sym)^.typ=procsym then
  276. begin
  277. _name:=sym^.name;
  278. symcoll:=wurzel;
  279. while assigned(symcoll) do
  280. begin
  281. { does the symbol already exist in the list ? }
  282. if _name=symcoll^.name^ then
  283. begin
  284. { walk through all defs of the symbol }
  285. hp:=pprocsym(sym)^.definition;
  286. while assigned(hp) do
  287. begin
  288. { compare with all stored definitions }
  289. procdefcoll:=symcoll^.data;
  290. stored:=false;
  291. while assigned(procdefcoll) do
  292. begin
  293. { compare parameters }
  294. if equal_paras(procdefcoll^.data^.para1,hp^.para1,false) and
  295. (
  296. ((procdefcoll^.data^.options and povirtualmethod)<>0) or
  297. ((hp^.options and povirtualmethod)<>0)
  298. ) then
  299. begin
  300. { wenn sie gleich sind }
  301. { und eine davon virtual deklariert ist }
  302. { Fehler falls nur eine VIRTUAL }
  303. if (procdefcoll^.data^.options and povirtualmethod)<>
  304. (hp^.options and povirtualmethod) then
  305. begin
  306. { in classes, we hide the old method }
  307. if _c^.isclass then
  308. begin
  309. { warn only if it is the first time,
  310. we hide the method }
  311. if _c=hp^._class then
  312. Message1(parser_w_should_use_override,_c^.objname^+'.'+_name);
  313. newentry;
  314. exit;
  315. end
  316. else
  317. if _c=hp^._class then
  318. begin
  319. if (procdefcoll^.data^.options and povirtualmethod)<>0 then
  320. Message1(parser_w_overloaded_are_not_both_virtual,_c^.objname^+'.'+_name)
  321. else
  322. Message1(parser_w_overloaded_are_not_both_non_virtual,
  323. _c^.objname^+'.'+_name);
  324. newentry;
  325. exit;
  326. end;
  327. end;
  328. { check, if the overridden directive is set }
  329. { (povirtualmethod is set! }
  330. { class ? }
  331. if _c^.isclass and
  332. ((hp^.options and pooverridingmethod)=0) then
  333. begin
  334. { warn only if it is the first time,
  335. we hide the method }
  336. if _c=hp^._class then
  337. Message1(parser_w_should_use_override,_c^.objname^+'.'+_name);
  338. newentry;
  339. exit;
  340. end;
  341. { error, if the return types aren't equal }
  342. if not(is_equal(procdefcoll^.data^.retdef,hp^.retdef)) and
  343. not((procdefcoll^.data^.retdef^.deftype=objectdef) and
  344. (hp^.retdef^.deftype=objectdef) and
  345. (pobjectdef(procdefcoll^.data^.retdef)^.isclass) and
  346. (pobjectdef(hp^.retdef)^.isclass) and
  347. (pobjectdef(hp^.retdef)^.isrelated(pobjectdef(procdefcoll^.data^.retdef)))) then
  348. Message1(parser_e_overloaded_methodes_not_same_ret,_c^.objname^+'.'+_name);
  349. { the flags have to match }
  350. { except abstract and override }
  351. if (procdefcoll^.data^.options and not(poabstractmethod or pooverridingmethod))<>
  352. (hp^.options and not(poabstractmethod or pooverridingmethod)) then
  353. Message1(parser_e_header_dont_match_forward,_c^.objname^+'.'+_name);
  354. { now set the number }
  355. hp^.extnumber:=procdefcoll^.data^.extnumber;
  356. { and exchange }
  357. procdefcoll^.data:=hp;
  358. stored:=true;
  359. end;
  360. procdefcoll:=procdefcoll^.next;
  361. end;
  362. { if it isn't saved in the list }
  363. { we create a new entry }
  364. if not(stored) then
  365. begin
  366. new(procdefcoll);
  367. procdefcoll^.data:=hp;
  368. procdefcoll^.next:=symcoll^.data;
  369. symcoll^.data:=procdefcoll;
  370. { if the method is virtual ... }
  371. if (hp^.options and povirtualmethod)<>0 then
  372. begin
  373. { ... it will get a number }
  374. hp^.extnumber:=nextvirtnumber;
  375. inc(nextvirtnumber);
  376. end;
  377. { check, if a method should be overridden }
  378. if (hp^.options and pooverridingmethod)<>0 then
  379. Message1(parser_e_nothing_to_be_overridden,_c^.objname^+'.'+_name);
  380. end;
  381. hp:=hp^.nextoverloaded;
  382. end;
  383. exit;
  384. end;
  385. symcoll:=symcoll^.next;
  386. end;
  387. newentry;
  388. end;
  389. end;
  390. procedure genvmt(_class : pobjectdef);
  391. procedure do_genvmt(p : pobjectdef);
  392. begin
  393. { start with the base class }
  394. if assigned(p^.childof) then
  395. do_genvmt(p^.childof);
  396. { walk through all public syms }
  397. _c:=_class;
  398. p^.publicsyms^.foreach({$ifdef fpc}@{$endif}eachsym);
  399. end;
  400. var
  401. symcoll : psymcoll;
  402. procdefcoll : pprocdefcoll;
  403. i : longint;
  404. begin
  405. wurzel:=nil;
  406. nextvirtnumber:=0;
  407. has_constructor:=false;
  408. has_virtual_method:=false;
  409. { generates a tree of all used methods }
  410. do_genvmt(_class);
  411. if has_virtual_method and not(has_constructor) then
  412. Message1(parser_w_virtual_without_constructor,_class^.objname^);
  413. { generates the VMT }
  414. { walk trough all numbers for virtual methods and search }
  415. { the method }
  416. for i:=0 to nextvirtnumber-1 do
  417. begin
  418. symcoll:=wurzel;
  419. { walk trough all symbols }
  420. while assigned(symcoll) do
  421. begin
  422. { walk trough all methods }
  423. procdefcoll:=symcoll^.data;
  424. while assigned(procdefcoll) do
  425. begin
  426. { writes the addresses to the VMT }
  427. { but only this which are declared as virtual }
  428. if procdefcoll^.data^.extnumber=i then
  429. begin
  430. if (procdefcoll^.data^.options and povirtualmethod)<>0 then
  431. begin
  432. { if a method is abstract, then is also the }
  433. { class abstract and it's not allow to }
  434. { generates an instance }
  435. if (procdefcoll^.data^.options and poabstractmethod)<>0 then
  436. begin
  437. _class^.options:=_class^.options or oo_is_abstract;
  438. datasegment^.concat(new(pai_const_symbol,
  439. initname('FPC_ABSTRACTERROR')));
  440. end
  441. else
  442. begin
  443. datasegment^.concat(new(pai_const_symbol,
  444. initname(procdefcoll^.data^.mangledname)));
  445. end;
  446. end;
  447. end;
  448. procdefcoll:=procdefcoll^.next;
  449. end;
  450. symcoll:=symcoll^.next;
  451. end;
  452. end;
  453. { disposes the above generated tree }
  454. symcoll:=wurzel;
  455. while assigned(symcoll) do
  456. begin
  457. wurzel:=symcoll^.next;
  458. stringdispose(symcoll^.name);
  459. procdefcoll:=symcoll^.data;
  460. while assigned(procdefcoll) do
  461. begin
  462. symcoll^.data:=procdefcoll^.next;
  463. dispose(procdefcoll);
  464. procdefcoll:=symcoll^.data;
  465. end;
  466. dispose(symcoll);
  467. symcoll:=wurzel;
  468. end;
  469. end;
  470. end.
  471. {
  472. $Log$
  473. Revision 1.8 1999-06-01 14:45:49 peter
  474. * @procvar is now always needed for FPC
  475. Revision 1.7 1999/05/27 19:44:30 peter
  476. * removed oldasm
  477. * plabel -> pasmlabel
  478. * -a switches to source writing automaticly
  479. * assembler readers OOPed
  480. * asmsymbol automaticly external
  481. * jumptables and other label fixes for asm readers
  482. Revision 1.6 1999/05/21 13:55:00 peter
  483. * NEWLAB for label as symbol
  484. Revision 1.5 1999/05/17 21:57:07 florian
  485. * new temporary ansistring handling
  486. Revision 1.4 1999/05/13 21:59:27 peter
  487. * removed oldppu code
  488. * warning if objpas is loaded from uses
  489. * first things for new deref writing
  490. Revision 1.3 1999/04/26 13:31:34 peter
  491. * release storenumber,double_checksum
  492. Revision 1.2 1999/04/21 09:43:37 peter
  493. * storenumber works
  494. * fixed some typos in double_checksum
  495. + incompatible types type1 and type2 message (with storenumber)
  496. Revision 1.1 1999/03/24 23:17:00 peter
  497. * fixed bugs 212,222,225,227,229,231,233
  498. }