ppu.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. comphook,
  156. globals,
  157. fpchash;
  158. {$ifdef Test_Double_checksum}
  159. {$ifdef TEST_CRC_ERROR}
  160. const
  161. CRC_Interface_Change_Message_Level=V_Error;
  162. CRC_Implementation_Change_Message_Level=V_Error;
  163. CRC_Indirect_Change_Message_Level=V_Error;
  164. {$else : not TEST_CRC_ERROR}
  165. const
  166. CRC_Interface_Change_Message_Level=V_Warning;
  167. CRC_Implementation_Change_Message_Level=V_Note;
  168. CRC_Indirect_Change_Message_Level=V_Note;
  169. {$endif : not TEST_CRC_ERROR}
  170. {$endif Test_Double_checksum}
  171. function swapendian_ppureal(d:ppureal):ppureal;
  172. type ppureal_bytes=array[0..sizeof(d)-1] of byte;
  173. var i:0..sizeof(d)-1;
  174. begin
  175. for i:=low(ppureal_bytes) to high(ppureal_bytes) do
  176. ppureal_bytes(swapendian_ppureal)[i]:=ppureal_bytes(d)[high(ppureal_bytes)-i];
  177. end;
  178. {*****************************************************************************
  179. TPPUFile
  180. *****************************************************************************}
  181. constructor tppufile.Create(const fn:string);
  182. begin
  183. inherited Create(fn);
  184. crc_only:=false;
  185. {$ifdef Test_Double_checksum}
  186. if not assigned(interface_crc_array) then
  187. begin
  188. new(interface_crc_array);
  189. fillchar(interface_crc_array^,sizeof(tcrc_array),#$ff);
  190. end;
  191. if not assigned(indirect_crc_array) then
  192. begin
  193. new(indirect_crc_array);
  194. fillchar(indirect_crc_array^,sizeof(tcrc_array),#$ff);
  195. end;
  196. if not assigned(implementation_crc_array) then
  197. begin
  198. new(implementation_crc_array);
  199. fillchar(implementation_crc_array^,sizeof(tcrc_array),#$ff);
  200. end;
  201. {$endif Test_Double_checksum}
  202. end;
  203. destructor tppufile.destroy;
  204. begin
  205. {$ifdef Test_Double_checksum}
  206. if assigned(interface_crc_array) then
  207. dispose(interface_crc_array);
  208. interface_crc_array:=nil;
  209. if assigned(indirect_crc_array) then
  210. dispose(indirect_crc_array);
  211. indirect_crc_array:=nil;
  212. if assigned(implementation_crc_array) then
  213. dispose(implementation_crc_array);
  214. implementation_crc_array:=nil;
  215. {$endif Test_Double_checksum}
  216. inherited destroy;
  217. end;
  218. function tppufile.CheckPPUId:boolean;
  219. begin
  220. CheckPPUId:=((Header.common.Id[1]='P') and
  221. (Header.common.Id[2]='P') and
  222. (Header.common.Id[3]='U'));
  223. end;
  224. procedure tppufile.newheader;
  225. var
  226. s : string;
  227. begin
  228. fillchar(header,sizeof(tppuheader),0);
  229. str(currentppuversion,s);
  230. while length(s)<3 do
  231. s:='0'+s;
  232. with header.common do
  233. begin
  234. Id[1]:='P';
  235. Id[2]:='P';
  236. Id[3]:='U';
  237. Ver[1]:=s[1];
  238. Ver[2]:=s[2];
  239. Ver[3]:=s[3];
  240. end;
  241. end;
  242. function tppufile.readheader: longint;
  243. var
  244. is_valid : boolean;
  245. begin
  246. is_valid:=true;
  247. result:=fsize;
  248. if result<sizeof(tppuheader) then
  249. is_valid:=false;
  250. result:=f.Read(header,sizeof(tppuheader));
  251. if (result<>sizeof(tppuheader)) then
  252. is_valid:=false;
  253. if not is_valid then
  254. begin
  255. fillchar(header,sizeof(tppuheader),#0);
  256. do_comment(V_Error,' Invalid header size '+tostr(result)+' (expecting '+tostr(sizeof(tppuheader))+')');
  257. exit(-1);
  258. end;
  259. { The header is always stored in little endian order }
  260. { therefore swap if on a big endian machine }
  261. {$IFDEF ENDIAN_BIG}
  262. header.common.compiler := swapendian(header.common.compiler);
  263. header.common.cpu := swapendian(header.common.cpu);
  264. header.common.target := swapendian(header.common.target);
  265. header.common.flags := swapendian(header.common.flags);
  266. header.common.size := swapendian(header.common.size);
  267. header.checksum := swapendian(header.checksum);
  268. header.interface_checksum := swapendian(header.interface_checksum);
  269. header.indirect_checksum := swapendian(header.indirect_checksum);
  270. header.deflistsize:=swapendian(header.deflistsize);
  271. header.symlistsize:=swapendian(header.symlistsize);
  272. { the PPU DATA is stored in native order }
  273. change_endian := (header.common.flags and uf_little_endian) = uf_little_endian;
  274. {$ELSE not ENDIAN_BIG}
  275. change_endian := (header.common.flags and uf_big_endian) = uf_big_endian;
  276. {$ENDIF}
  277. end;
  278. function tppufile.outputallowed: boolean;
  279. begin
  280. result:=not crc_only;
  281. end;
  282. {$ifdef DEBUG_PPU}
  283. procedure tppufile.ppu_log(st :string);
  284. begin
  285. inherited ppu_log(st);
  286. if flog_open then
  287. begin
  288. if do_crc and (ppu_log_idx < bufstart+bufidx) then
  289. begin
  290. writeln(flog,'New crc : ',hexstr(dword(crc),8));
  291. writeln(flog,'New interface crc : ',hexstr(dword(interface_crc),8));
  292. writeln(flog,'New indirect crc : ',hexstr(dword(indirect_crc),8));
  293. ppu_log_idx:=bufstart+bufidx;
  294. end;
  295. end;
  296. {$ifdef IN_PPUDUMP}
  297. if update_crc then
  298. begin
  299. writeln('New crc : ',hexstr(dword(crc),8));
  300. writeln('New interface crc : ',hexstr(dword(interface_crc),8));
  301. writeln('New indirect crc : ',hexstr(dword(indirect_crc),8));
  302. end;
  303. {$endif}
  304. end;
  305. {$endif}
  306. {*****************************************************************************
  307. TPPUFile Reading
  308. *****************************************************************************}
  309. { nothing special currently }
  310. {*****************************************************************************
  311. TPPUFile Writing
  312. *****************************************************************************}
  313. function tppufile.createfile:boolean;
  314. begin
  315. {$ifdef DEBUG_GENERATE_INTERFACE_PPU}
  316. if crc_only then
  317. begin
  318. fname:=fname+'.intf';
  319. crc_only:=false;
  320. end;
  321. {$endif}
  322. result:=inherited createfile;
  323. end;
  324. procedure tppufile.writeheader;
  325. var
  326. opos : integer;
  327. begin
  328. if crc_only then
  329. exit;
  330. { flush buffer }
  331. writebuf;
  332. { update size (w/o header!) in the header }
  333. header.common.size:=bufstart-sizeof(tppuheader);
  334. { set the endian flag }
  335. {$ifndef FPC_BIG_ENDIAN}
  336. header.common.flags := header.common.flags or uf_little_endian;
  337. {$else not FPC_BIG_ENDIAN}
  338. header.common.flags := header.common.flags or uf_big_endian;
  339. { Now swap the header.common in the correct endian (always little endian) }
  340. header.common.compiler := swapendian(header.common.compiler);
  341. header.common.cpu := swapendian(header.common.cpu);
  342. header.common.target := swapendian(header.common.target);
  343. header.common.flags := swapendian(header.common.flags);
  344. header.common.size := swapendian(header.common.size);
  345. header.checksum := swapendian(header.checksum);
  346. header.interface_checksum := swapendian(header.interface_checksum);
  347. header.indirect_checksum := swapendian(header.indirect_checksum);
  348. header.deflistsize:=swapendian(header.deflistsize);
  349. header.symlistsize:=swapendian(header.symlistsize);
  350. {$endif not FPC_BIG_ENDIAN}
  351. { write header and restore filepos after it }
  352. opos:=f.Position;
  353. f.Position:=0;
  354. f.Write(header,sizeof(tppuheader));
  355. f.Position:=opos;
  356. end;
  357. procedure tppufile.putdata(const b;len:integer);
  358. {$ifdef Test_Double_checksum}
  359. var
  360. pb : pbyte;
  361. ind : integer;
  362. {$endif Test_Double_checksum}
  363. begin
  364. if do_crc then
  365. begin
  366. crc:=UpdateCrc32(crc,b,len);
  367. {$ifdef Test_Double_checksum}
  368. if crc_only then
  369. begin
  370. implementation_crc_array^[implementation_write_crc_index]:=crc;
  371. {$ifdef Test_Double_checksum_write}
  372. Write(CRCFile,'imp_crc ',implementation_write_crc_index:6,' $',hexstr(crc,8),' ',len);
  373. pb:=@b;
  374. for ind:=0 to len-1 do
  375. Write(CRCFile,' ',hexstr(pb[ind],2));
  376. Writeln(CRCFile);
  377. {$endif Test_Double_checksum_write}
  378. if implementation_write_crc_index<crc_array_size then
  379. inc(implementation_write_crc_index);
  380. end
  381. else
  382. begin
  383. if (implementation_read_crc_index<crc_array_size) and
  384. (implementation_read_crc_index<implementation_write_crc_index) and
  385. (implementation_crc_array^[implementation_read_crc_index]<>crc) then
  386. begin
  387. do_comment(CRC_implementation_Change_Message_Level,'implementation CRC changed at index '+tostr(implementation_read_crc_index));
  388. {$IFDEF TEST_CRC_ERROR}
  389. if CRC_implementation_Change_Message_Level=V_Error then
  390. do_internalerror(2020113001);
  391. {$ENDIF}
  392. {$ifdef Test_Double_checksum_write}
  393. Writeln(CRCFile,'!!!imp_crc ',implementation_read_crc_index:5,' $',hexstr(crc,8),'<>$',hexstr(implementation_crc_array^[implementation_read_crc_index],8));
  394. end
  395. else
  396. begin
  397. Writeln(CRCFile,'imp_crc ',implementation_read_crc_index:5,' OK');
  398. {$endif Test_Double_checksum_write}
  399. end;
  400. inc(implementation_read_crc_index);
  401. end;
  402. {$endif def Test_Double_checksum}
  403. if do_interface_crc then
  404. begin
  405. interface_crc:=UpdateCrc32(interface_crc,b,len);
  406. {$ifdef Test_Double_checksum}
  407. if crc_only then
  408. begin
  409. interface_crc_array^[interface_write_crc_index]:=interface_crc;
  410. {$ifdef Test_Double_checksum_write}
  411. Write(CRCFile,'int_crc ',interface_write_crc_index:5,' $',hexstr(interface_crc,8),' ',len);
  412. pb:=@b;
  413. for ind:=0 to len-1 do
  414. Write(CRCFile,' ',hexstr(pb[ind],2));
  415. Writeln(CRCFile);
  416. {$endif Test_Double_checksum_write}
  417. if interface_write_crc_index<crc_array_size then
  418. inc(interface_write_crc_index);
  419. end
  420. else
  421. begin
  422. if (interface_read_crc_index<crc_array_size) and
  423. (interface_read_crc_index<interface_write_crc_index) and
  424. (interface_crc_array^[interface_read_crc_index]<>interface_crc) then
  425. begin
  426. do_comment(CRC_Interface_Change_Message_Level,'interface CRC changed at index '+tostr(interface_read_crc_index));
  427. {$IFDEF TEST_CRC_ERROR}
  428. if CRC_interface_Change_Message_Level=V_Error then
  429. do_internalerror(2020113002);
  430. {$ENDIF}
  431. {$ifdef Test_Double_checksum_write}
  432. Writeln(CRCFile,'!!!int_crc ',interface_read_crc_index:5,' $',hexstr(interface_crc,8),'<>$',hexstr(interface_crc_array^[interface_read_crc_index],8));
  433. end
  434. else
  435. begin
  436. Writeln(CRCFile,'int_crc ',interface_read_crc_index:5,' OK');
  437. {$endif Test_Double_checksum_write}
  438. end;
  439. inc(interface_read_crc_index);
  440. end;
  441. {$endif def Test_Double_checksum}
  442. { indirect crc must only be calculated for the interface; changes
  443. to a class in the implementation cannot require another unit to
  444. be recompiled }
  445. if do_indirect_crc then
  446. begin
  447. indirect_crc:=UpdateCrc32(indirect_crc,b,len);
  448. {$ifdef Test_Double_checksum}
  449. if crc_only then
  450. begin
  451. indirect_crc_array^[indirect_write_crc_index]:=indirect_crc;
  452. {$ifdef Test_Double_checksum_write}
  453. Write(CRCFile,'ind_crc ',indirect_write_crc_index:5,' $',hexstr(indirect_crc,8),' ',len);
  454. pb:=@b;
  455. for ind:=0 to len-1 do
  456. Write(CRCFile,' ',hexstr(pb[ind],2));
  457. Writeln(CRCFile);
  458. {$endif Test_Double_checksum_write}
  459. if indirect_write_crc_index<crc_array_size then
  460. inc(indirect_write_crc_index);
  461. end
  462. else
  463. begin
  464. if (indirect_read_crc_index<crc_array_size) and
  465. (indirect_read_crc_index<indirect_write_crc_index) and
  466. (indirect_crc_array^[indirect_read_crc_index]<>indirect_crc) then
  467. begin
  468. do_comment(CRC_Indirect_Change_Message_Level,'Indirect CRC changed at index '+tostr(indirect_read_crc_index));
  469. {$IFDEF TEST_CRC_ERROR}
  470. if CRC_indirect_Change_Message_Level=V_Error then
  471. do_internalerror(2020113003);
  472. {$ENDIF}
  473. {$ifdef Test_Double_checksum_write}
  474. Writeln(CRCFile,'!!!ind_crc ',indirect_read_crc_index:5,' $',hexstr(indirect_crc,8),'<>$',hexstr(indirect_crc_array^[indirect_read_crc_index],8));
  475. end
  476. else
  477. begin
  478. Writeln(CRCFile,'ind_crc ',indirect_read_crc_index:5,' OK');
  479. {$endif Test_Double_checksum_write}
  480. end;
  481. inc(indirect_read_crc_index);
  482. end;
  483. {$endif def Test_Double_checksum}
  484. end;
  485. end;
  486. end;
  487. inherited putdata(b,len);
  488. (*if not crc_only then
  489. writedata(b,len);
  490. inc(entryidx,len);*)
  491. end;
  492. procedure tppufile.putdata(b: tbytedynarray);
  493. begin
  494. putdata(b[0],length(b));
  495. end;
  496. procedure tppufile.putdata(b: tansichardynarray);
  497. begin
  498. putdata(b[0],length(b));
  499. end;
  500. function tppufile.getheadersize: longint;
  501. begin
  502. result:=sizeof(header);
  503. end;
  504. function tppufile.getheaderaddr: pentryheader;
  505. begin
  506. result:=@header;
  507. end;
  508. procedure tppufile.resetfile;
  509. begin
  510. {$ifdef Test_Double_checksum_write}
  511. if (crc<>0) or (interface_crc<>0) or (indirect_crc<>0) then
  512. Writeln(CRCFile,'!!! tppufile.reset called',
  513. ' implementation_crc=$',hexstr(crc,8),
  514. ' interface_crc=$',hexstr(interface_crc,8),
  515. ' indirect_crc=$',hexstr(indirect_crc,8),
  516. ' implementation_crc_size=',implementation_write_crc_index,
  517. ' interface_crc_size=',interface_write_crc_index,
  518. ' indirect_crc_size=',indirect_write_crc_index);
  519. {$endif Test_Double_checksum_write}
  520. crc:=0;
  521. interface_crc:=0;
  522. indirect_crc:=0;
  523. do_interface_crc:=true;
  524. do_indirect_crc:=false;
  525. do_crc:=true;
  526. end;
  527. end.