ppu.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Routines to read/write ppu files
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ppu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,constexp,entfile;
  22. { Also write the ppu if only crc if done, this can be used with ppudump to
  23. see the differences between the intf and implementation }
  24. { define DEBUG_GENERATE_INTERFACE_PPU}
  25. {$ifdef Test_Double_checksum}
  26. const
  27. CRC_array_Size = 200000;
  28. type
  29. tcrc_array = array[0..crc_array_size] of dword;
  30. pcrc_array = ^tcrc_array;
  31. {$endif Test_Double_checksum}
  32. const
  33. { only update this version if something change in the tppuheader:
  34. * the unit flags listed below
  35. * the format of the header itself
  36. This number cannot become bigger than 255 (it's stored in a byte) }
  37. CurrentPPUVersion = 208;
  38. { for any other changes to the ppu format, increase this version number
  39. (it's a cardinal) }
  40. CurrentPPULongVersion = 28;
  41. { unit flags }
  42. uf_big_endian = $000004;
  43. uf_in_library = $000020; { is the file in another file than <ppufile>.* ? }
  44. uf_smart_linked = $000040; { the ppu can be smartlinked }
  45. uf_static_linked = $000080; { the ppu can be linked static }
  46. uf_shared_linked = $000100; { the ppu can be linked shared }
  47. uf_lto_linked = $000200; { the ppu can be used with LTO }
  48. uf_no_link = $000400; { unit has no .o generated, but can still have external linking! }
  49. uf_little_endian = $001000;
  50. uf_fpu_emulation = $008000; { this unit was compiled with fpu emulation on }
  51. type
  52. { bestreal is defined based on the target architecture }
  53. ppureal=bestreal;
  54. { set type aliases. We cast all sets to a corresponding array type so
  55. that if the set changes size, compilation will fail and we know that
  56. we have have to increase the ppu version }
  57. tppuset1 = array[0..0] of byte;
  58. tppuset2 = array[0..1] of byte;
  59. { tppuset3 = array[0..2] of byte; (sets of 3 bytes are rounded up to 4 bytes) }
  60. tppuset4 = array[0..3] of byte;
  61. tppuset5 = array[0..4] of byte;
  62. tppuset6 = array[0..5] of byte;
  63. tppuset7 = array[0..6] of byte;
  64. tppuset8 = array[0..7] of byte;
  65. tppuset9 = array[0..8] of byte;
  66. tppuset10 = array[0..9] of byte;
  67. tppuset11 = array[0..10] of byte;
  68. tppuset12 = array[0..11] of byte;
  69. tppuset13 = array[0..12] of byte;
  70. tppuset14 = array[0..13] of byte;
  71. tppuset15 = array[0..14] of byte;
  72. tppuset16 = array[0..15] of byte;
  73. tppuset17 = array[0..16] of byte;
  74. tppuset18 = array[0..17] of byte;
  75. tppuset19 = array[0..18] of byte;
  76. tppuset20 = array[0..19] of byte;
  77. tppuset21 = array[0..20] of byte;
  78. tppuset22 = array[0..21] of byte;
  79. tppuset23 = array[0..22] of byte;
  80. tppuset24 = array[0..23] of byte;
  81. tppuset25 = array[0..24] of byte;
  82. tppuset26 = array[0..25] of byte;
  83. tppuset27 = array[0..26] of byte;
  84. tppuset28 = array[0..27] of byte;
  85. tppuset29 = array[0..28] of byte;
  86. tppuset30 = array[0..29] of byte;
  87. tppuset31 = array[0..30] of byte;
  88. tppuset32 = array[0..31] of byte;
  89. tppuerror=(ppuentrytoobig,ppuentryerror);
  90. tppuheader=record
  91. common : tentryheader;
  92. checksum : cardinal; { checksum for this ppufile }
  93. interface_checksum : cardinal;
  94. deflistsize,
  95. symlistsize : longint;
  96. indirect_checksum: cardinal;
  97. end;
  98. tppuentry=tentry;
  99. tunitasmlisttype=(ualt_public,ualt_extern);
  100. { tppufile }
  101. tppufile=class(tentryfile)
  102. {$ifdef Test_Double_checksum}
  103. public
  104. interface_read_crc_index,
  105. interface_write_crc_index,
  106. indirect_read_crc_index,
  107. indirect_write_crc_index,
  108. implementation_read_crc_index,
  109. implementation_write_crc_index : cardinal;
  110. interface_crc_array,
  111. indirect_crc_array,
  112. implementation_crc_array : pcrc_array;
  113. CRCFile : text;
  114. private
  115. {$endif def Test_Double_checksum}
  116. protected
  117. procedure newheader;override;
  118. function readheader: longint;override;
  119. function outputallowed: boolean;override;
  120. //procedure doputdata(const b;len:integer);override;
  121. function getheadersize:longint;override;
  122. function getheaderaddr:pentryheader;override;
  123. procedure resetfile;override;
  124. {$ifdef DEBUG_PPU}
  125. procedure ppu_log(st :string);override;
  126. {$endif}
  127. public
  128. header : tppuheader;
  129. { crc for the entire unit }
  130. crc,
  131. { crc for the interface definitions in this unit }
  132. interface_crc,
  133. { crc of all object/class definitions in the interface of this unit, xor'ed
  134. by the crc's of all object/class definitions in the interfaces of units
  135. used by this unit. Reason: see mantis #13840 }
  136. indirect_crc : cardinal;
  137. do_crc,
  138. do_interface_crc,
  139. do_indirect_crc : boolean;
  140. crc_only : boolean; { used to calculate interface_crc before implementation }
  141. constructor Create(const fn:string);
  142. destructor destroy;override;
  143. function CheckPPUId:boolean;
  144. {read}
  145. { nothing special currently }
  146. {write}
  147. function createfile:boolean;override;
  148. procedure writeheader;override;
  149. procedure putdata(const b;len:integer);override;
  150. procedure putdata(b : tbytedynarray);
  151. procedure putdata(b : tansichardynarray);
  152. end;
  153. implementation
  154. uses
  155. {$ifdef Test_Double_checksum}
  156. comphook,
  157. globals,
  158. {$endif def Test_Double_checksum}
  159. fpchash;
  160. {$ifdef Test_Double_checksum}
  161. {$ifdef TEST_CRC_ERROR}
  162. const
  163. CRC_Interface_Change_Message_Level=V_Error;
  164. CRC_Implementation_Change_Message_Level=V_Error;
  165. CRC_Indirect_Change_Message_Level=V_Error;
  166. {$else : not TEST_CRC_ERROR}
  167. const
  168. CRC_Interface_Change_Message_Level=V_Warning;
  169. CRC_Implementation_Change_Message_Level=V_Note;
  170. CRC_Indirect_Change_Message_Level=V_Note;
  171. {$endif : not TEST_CRC_ERROR}
  172. {$endif Test_Double_checksum}
  173. function swapendian_ppureal(d:ppureal):ppureal;
  174. type ppureal_bytes=array[0..sizeof(d)-1] of byte;
  175. var i:0..sizeof(d)-1;
  176. begin
  177. for i:=low(ppureal_bytes) to high(ppureal_bytes) do
  178. ppureal_bytes(swapendian_ppureal)[i]:=ppureal_bytes(d)[high(ppureal_bytes)-i];
  179. end;
  180. {*****************************************************************************
  181. TPPUFile
  182. *****************************************************************************}
  183. constructor tppufile.Create(const fn:string);
  184. begin
  185. inherited Create(fn);
  186. crc_only:=false;
  187. {$ifdef Test_Double_checksum}
  188. if not assigned(interface_crc_array) then
  189. begin
  190. new(interface_crc_array);
  191. fillchar(interface_crc_array^,sizeof(interface_crc_array),#$ff);
  192. end;
  193. if not assigned(indirect_crc_array) then
  194. begin
  195. new(indirect_crc_array);
  196. fillchar(indirect_crc_array^,sizeof(indirect_crc_array),#$ff);
  197. end;
  198. if not assigned(implementation_crc_array) then
  199. begin
  200. new(implementation_crc_array);
  201. fillchar(implementation_crc_array^,sizeof(implementation_crc_array),#$ff);
  202. end;
  203. {$endif Test_Double_checksum}
  204. end;
  205. destructor tppufile.destroy;
  206. begin
  207. {$ifdef Test_Double_checksum}
  208. if assigned(interface_crc_array) then
  209. dispose(interface_crc_array);
  210. interface_crc_array:=nil;
  211. if assigned(indirect_crc_array) then
  212. dispose(indirect_crc_array);
  213. indirect_crc_array:=nil;
  214. if assigned(implementation_crc_array) then
  215. dispose(implementation_crc_array);
  216. implementation_crc_array:=nil;
  217. {$endif Test_Double_checksum}
  218. inherited destroy;
  219. end;
  220. function tppufile.CheckPPUId:boolean;
  221. begin
  222. CheckPPUId:=((Header.common.Id[1]='P') and
  223. (Header.common.Id[2]='P') and
  224. (Header.common.Id[3]='U'));
  225. end;
  226. procedure tppufile.newheader;
  227. var
  228. s : string;
  229. begin
  230. fillchar(header,sizeof(tppuheader),0);
  231. str(currentppuversion,s);
  232. while length(s)<3 do
  233. s:='0'+s;
  234. with header.common do
  235. begin
  236. Id[1]:='P';
  237. Id[2]:='P';
  238. Id[3]:='U';
  239. Ver[1]:=s[1];
  240. Ver[2]:=s[2];
  241. Ver[3]:=s[3];
  242. end;
  243. end;
  244. function tppufile.readheader: longint;
  245. begin
  246. if fsize<sizeof(tppuheader) then
  247. exit(-1);
  248. result:=f.Read(header,sizeof(tppuheader));
  249. { The header is always stored in little endian order }
  250. { therefore swap if on a big endian machine }
  251. {$IFDEF ENDIAN_BIG}
  252. header.common.compiler := swapendian(header.common.compiler);
  253. header.common.cpu := swapendian(header.common.cpu);
  254. header.common.target := swapendian(header.common.target);
  255. header.common.flags := swapendian(header.common.flags);
  256. header.common.size := swapendian(header.common.size);
  257. header.checksum := swapendian(header.checksum);
  258. header.interface_checksum := swapendian(header.interface_checksum);
  259. header.indirect_checksum := swapendian(header.indirect_checksum);
  260. header.deflistsize:=swapendian(header.deflistsize);
  261. header.symlistsize:=swapendian(header.symlistsize);
  262. { the PPU DATA is stored in native order }
  263. change_endian := (header.common.flags and uf_little_endian) = uf_little_endian;
  264. {$ELSE not ENDIAN_BIG}
  265. change_endian := (header.common.flags and uf_big_endian) = uf_big_endian;
  266. {$ENDIF}
  267. end;
  268. function tppufile.outputallowed: boolean;
  269. begin
  270. result:=not crc_only;
  271. end;
  272. {$ifdef DEBUG_PPU}
  273. procedure tppufile.ppu_log(st :string);
  274. begin
  275. inherited ppu_log(st);
  276. if flog_open then
  277. begin
  278. if do_crc and (ppu_log_idx < bufstart+bufidx) then
  279. begin
  280. writeln(flog,'New crc : ',hexstr(dword(crc),8));
  281. writeln(flog,'New interface crc : ',hexstr(dword(interface_crc),8));
  282. writeln(flog,'New indirect crc : ',hexstr(dword(indirect_crc),8));
  283. ppu_log_idx:=bufstart+bufidx;
  284. end;
  285. end;
  286. {$ifdef IN_PPUDUMP}
  287. if update_crc then
  288. begin
  289. writeln('New crc : ',hexstr(dword(crc),8));
  290. writeln('New interface crc : ',hexstr(dword(interface_crc),8));
  291. writeln('New indirect crc : ',hexstr(dword(indirect_crc),8));
  292. end;
  293. {$endif}
  294. end;
  295. {$endif}
  296. {*****************************************************************************
  297. TPPUFile Reading
  298. *****************************************************************************}
  299. { nothing special currently }
  300. {*****************************************************************************
  301. TPPUFile Writing
  302. *****************************************************************************}
  303. function tppufile.createfile:boolean;
  304. begin
  305. {$ifdef DEBUG_GENERATE_INTERFACE_PPU}
  306. if crc_only then
  307. begin
  308. fname:=fname+'.intf';
  309. crc_only:=false;
  310. end;
  311. {$endif}
  312. result:=inherited createfile;
  313. end;
  314. procedure tppufile.writeheader;
  315. var
  316. opos : integer;
  317. begin
  318. if crc_only then
  319. exit;
  320. { flush buffer }
  321. writebuf;
  322. { update size (w/o header!) in the header }
  323. header.common.size:=bufstart-sizeof(tppuheader);
  324. { set the endian flag }
  325. {$ifndef FPC_BIG_ENDIAN}
  326. header.common.flags := header.common.flags or uf_little_endian;
  327. {$else not FPC_BIG_ENDIAN}
  328. header.common.flags := header.common.flags or uf_big_endian;
  329. { Now swap the header.common in the correct endian (always little endian) }
  330. header.common.compiler := swapendian(header.common.compiler);
  331. header.common.cpu := swapendian(header.common.cpu);
  332. header.common.target := swapendian(header.common.target);
  333. header.common.flags := swapendian(header.common.flags);
  334. header.common.size := swapendian(header.common.size);
  335. header.checksum := swapendian(header.checksum);
  336. header.interface_checksum := swapendian(header.interface_checksum);
  337. header.indirect_checksum := swapendian(header.indirect_checksum);
  338. header.deflistsize:=swapendian(header.deflistsize);
  339. header.symlistsize:=swapendian(header.symlistsize);
  340. {$endif not FPC_BIG_ENDIAN}
  341. { write header and restore filepos after it }
  342. opos:=f.Position;
  343. f.Position:=0;
  344. f.Write(header,sizeof(tppuheader));
  345. f.Position:=opos;
  346. end;
  347. procedure tppufile.putdata(const b;len:integer);
  348. {$ifdef Test_Double_checksum}
  349. var
  350. pb : pbyte;
  351. ind : integer;
  352. {$endif Test_Double_checksum}
  353. begin
  354. if do_crc then
  355. begin
  356. crc:=UpdateCrc32(crc,b,len);
  357. {$ifdef Test_Double_checksum}
  358. if crc_only then
  359. begin
  360. implementation_crc_array^[implementation_write_crc_index]:=crc;
  361. {$ifdef Test_Double_checksum_write}
  362. Write(CRCFile,'imp_crc ',implementation_write_crc_index:6,' $',hexstr(crc,8),' ',len);
  363. pb:=@b;
  364. for ind:=0 to len-1 do
  365. Write(CRCFile,' ',hexstr(pb[ind],2));
  366. Writeln(CRCFile);
  367. {$endif Test_Double_checksum_write}
  368. if implementation_write_crc_index<crc_array_size then
  369. inc(implementation_write_crc_index);
  370. end
  371. else
  372. begin
  373. if (implementation_read_crc_index<crc_array_size) and
  374. (implementation_crc_array^[implementation_read_crc_index]<>crc) then
  375. begin
  376. do_comment(CRC_implementation_Change_Message_Level,'implementation CRC changed at index '+tostr(implementation_read_crc_index));
  377. {$IFDEF TEST_CRC_ERROR}
  378. if CRC_implementation_Change_Message_Level=V_Error then
  379. do_internalerror(2020113001);
  380. {$ENDIF}
  381. {$ifdef Test_Double_checksum_write}
  382. Writeln(CRCFile,'!!!imp_crc ',implementation_read_crc_index:5,'$',hexstr(crc,8),'<>$',hexstr(implementation_crc_array^[implementation_read_crc_index],8));
  383. end
  384. else
  385. begin
  386. Writeln(CRCFile,'imp_crc ',implementation_read_crc_index:5,' OK');
  387. {$endif Test_Double_checksum_write}
  388. end;
  389. inc(implementation_read_crc_index);
  390. end;
  391. {$endif def Test_Double_checksum}
  392. if do_interface_crc then
  393. begin
  394. interface_crc:=UpdateCrc32(interface_crc,b,len);
  395. {$ifdef Test_Double_checksum}
  396. if crc_only then
  397. begin
  398. interface_crc_array^[interface_write_crc_index]:=interface_crc;
  399. {$ifdef Test_Double_checksum_write}
  400. Write(CRCFile,'int_crc ',interface_write_crc_index:5,' $',hexstr(interface_crc,8),' ',len);
  401. pb:=@b;
  402. for ind:=0 to len-1 do
  403. Write(CRCFile,' ',hexstr(pb[ind],2));
  404. Writeln(CRCFile);
  405. {$endif Test_Double_checksum_write}
  406. if interface_write_crc_index<crc_array_size then
  407. inc(interface_write_crc_index);
  408. end
  409. else
  410. begin
  411. if (interface_read_crc_index<crc_array_size) and
  412. (interface_crc_array^[interface_read_crc_index]<>interface_crc) then
  413. begin
  414. do_comment(CRC_Interface_Change_Message_Level,'interface CRC changed at index '+tostr(interface_read_crc_index));
  415. {$IFDEF TEST_CRC_ERROR}
  416. if CRC_interface_Change_Message_Level=V_Error then
  417. do_internalerror(2020113002);
  418. {$ENDIF}
  419. {$ifdef Test_Double_checksum_write}
  420. Writeln(CRCFile,'!!!int_crc ',interface_read_crc_index:5,'$',hexstr(interface_crc,8),'<>$',hexstr(interface_crc_array^[interface_read_crc_index],8));
  421. end
  422. else
  423. begin
  424. Writeln(CRCFile,'int_crc ',interface_read_crc_index:5,' OK');
  425. {$endif Test_Double_checksum_write}
  426. end;
  427. inc(interface_read_crc_index);
  428. end;
  429. {$endif def Test_Double_checksum}
  430. { indirect crc must only be calculated for the interface; changes
  431. to a class in the implementation cannot require another unit to
  432. be recompiled }
  433. if do_indirect_crc then
  434. begin
  435. indirect_crc:=UpdateCrc32(indirect_crc,b,len);
  436. {$ifdef Test_Double_checksum}
  437. if crc_only then
  438. begin
  439. indirect_crc_array^[indirect_write_crc_index]:=indirect_crc;
  440. {$ifdef Test_Double_checksum_write}
  441. Write(CRCFile,'ind_crc ',indirect_write_crc_index:5,' $',hexstr(indirect_crc,8),' ',len);
  442. pb:=@b;
  443. for ind:=0 to len-1 do
  444. Write(CRCFile,' ',hexstr(pb[ind],2));
  445. Writeln(CRCFile);
  446. {$endif Test_Double_checksum_write}
  447. if indirect_write_crc_index<crc_array_size then
  448. inc(indirect_write_crc_index);
  449. end
  450. else
  451. begin
  452. if (indirect_read_crc_index<crc_array_size) and
  453. (indirect_crc_array^[indirect_read_crc_index]<>indirect_crc) then
  454. begin
  455. do_comment(CRC_Indirect_Change_Message_Level,'Indirect CRC changed at index '+tostr(indirect_read_crc_index));
  456. {$IFDEF TEST_CRC_ERROR}
  457. if CRC_indirect_Change_Message_Level=V_Error then
  458. do_internalerror(2020113003);
  459. {$ENDIF}
  460. {$ifdef Test_Double_checksum_write}
  461. Writeln(CRCFile,'!!!ind_crc ',indirect_read_crc_index:5,'$',hexstr(indirect_crc,8),'<>$',hexstr(indirect_crc_array^[indirect_read_crc_index],8));
  462. end
  463. else
  464. begin
  465. Writeln(CRCFile,'ind_crc ',indirect_read_crc_index:5,' OK');
  466. {$endif Test_Double_checksum_write}
  467. end;
  468. inc(indirect_read_crc_index);
  469. end;
  470. {$endif def Test_Double_checksum}
  471. end;
  472. end;
  473. end;
  474. inherited putdata(b,len);
  475. (*if not crc_only then
  476. writedata(b,len);
  477. inc(entryidx,len);*)
  478. end;
  479. procedure tppufile.putdata(b: tbytedynarray);
  480. begin
  481. putdata(b[0],length(b));
  482. end;
  483. procedure tppufile.putdata(b: tansichardynarray);
  484. begin
  485. putdata(b[0],length(b));
  486. end;
  487. function tppufile.getheadersize: longint;
  488. begin
  489. result:=sizeof(header);
  490. end;
  491. function tppufile.getheaderaddr: pentryheader;
  492. begin
  493. result:=@header;
  494. end;
  495. procedure tppufile.resetfile;
  496. begin
  497. {$ifdef Test_Double_checksum_write}
  498. if (crc<>0) or (interface_crc<>0) or (indirect_crc<>0) then
  499. Writeln(CRCFile,'!!! tppufile.reset called',
  500. ' implementation_crc=$',hexstr(crc,8),
  501. ' interface_crc=$',hexstr(interface_crc,8),
  502. ' indirect_crc=$',hexstr(indirect_crc,8),
  503. ' implementation_crc_size=',implementation_write_crc_index,
  504. ' interface_crc_size=',interface_write_crc_index,
  505. ' indirect_crc_size=',indirect_write_crc_index);
  506. {$endif Test_Double_checksum_write}
  507. crc:=0;
  508. interface_crc:=0;
  509. indirect_crc:=0;
  510. do_interface_crc:=true;
  511. do_indirect_crc:=false;
  512. do_crc:=true;
  513. end;
  514. end.