2
0

ppu.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. 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 INTFPPU}
  25. {$ifdef Test_Double_checksum}
  26. var
  27. CRCFile : text;
  28. const
  29. CRC_array_Size = 200000;
  30. type
  31. tcrc_array = array[0..crc_array_size] of dword;
  32. pcrc_array = ^tcrc_array;
  33. {$endif Test_Double_checksum}
  34. const
  35. { only update this version if something change in the tppuheader:
  36. * the unit flags listed below
  37. * the format of the header itslf
  38. This number cannot become bigger than 255 (it's stored in a byte) }
  39. CurrentPPUVersion = 207;
  40. { for any other changes to the ppu format, increase this version number
  41. (it's a cardinal) }
  42. CurrentPPULongVersion = 8;
  43. { unit flags }
  44. uf_big_endian = $000004;
  45. uf_in_library = $000020; { is the file in another file than <ppufile>.* ? }
  46. uf_smart_linked = $000040; { the ppu can be smartlinked }
  47. uf_static_linked = $000080; { the ppu can be linked static }
  48. uf_shared_linked = $000100; { the ppu can be linked shared }
  49. uf_lto_linked = $000200; { the ppu can be used with LTO }
  50. uf_no_link = $000400; { unit has no .o generated, but can still have external linking! }
  51. uf_little_endian = $001000;
  52. uf_fpu_emulation = $008000; { this unit was compiled with fpu emulation on }
  53. type
  54. { bestreal is defined based on the target architecture }
  55. ppureal=bestreal;
  56. { set type aliases. We cast all sets to a corresponding array type so
  57. that if the set changes size, compilation will fail and we know that
  58. we have have to increase the ppu version }
  59. tppuset1 = array[0..0] of byte;
  60. tppuset2 = array[0..1] of byte;
  61. { tppuset3 = array[0..2] of byte; (sets of 3 bytes are rounded up to 4 bytes) }
  62. tppuset4 = array[0..3] of byte;
  63. tppuset5 = array[0..4] of byte;
  64. tppuset6 = array[0..5] of byte;
  65. tppuset7 = array[0..6] of byte;
  66. tppuset8 = array[0..7] of byte;
  67. tppuset9 = array[0..8] of byte;
  68. tppuset10 = array[0..9] of byte;
  69. tppuset11 = array[0..10] of byte;
  70. tppuset12 = array[0..11] of byte;
  71. tppuset13 = array[0..12] of byte;
  72. tppuset14 = array[0..13] of byte;
  73. tppuset15 = array[0..14] of byte;
  74. tppuset16 = array[0..15] of byte;
  75. tppuset17 = array[0..16] of byte;
  76. tppuset18 = array[0..17] of byte;
  77. tppuset19 = array[0..18] of byte;
  78. tppuset20 = array[0..19] of byte;
  79. tppuset21 = array[0..20] of byte;
  80. tppuset22 = array[0..21] of byte;
  81. tppuset23 = array[0..22] of byte;
  82. tppuset24 = array[0..23] of byte;
  83. tppuset25 = array[0..24] of byte;
  84. tppuset26 = array[0..25] of byte;
  85. tppuset27 = array[0..26] of byte;
  86. tppuset28 = array[0..27] of byte;
  87. tppuset29 = array[0..28] of byte;
  88. tppuset30 = array[0..29] of byte;
  89. tppuset31 = array[0..30] of byte;
  90. tppuset32 = array[0..31] of byte;
  91. tppuerror=(ppuentrytoobig,ppuentryerror);
  92. tppuheader=record
  93. common : tentryheader;
  94. checksum : cardinal; { checksum for this ppufile }
  95. interface_checksum : cardinal;
  96. deflistsize,
  97. symlistsize : longint;
  98. indirect_checksum: cardinal;
  99. end;
  100. tppuentry=tentry;
  101. tunitasmlisttype=(ualt_public,ualt_extern);
  102. { tppufile }
  103. tppufile=class(tentryfile)
  104. {$ifdef Test_Double_checksum}
  105. public
  106. crcindex,
  107. crc_index,
  108. crcindex2,
  109. crc_index2 : cardinal;
  110. crc_test,
  111. crc_test2 : pcrc_array;
  112. private
  113. {$endif def Test_Double_checksum}
  114. protected
  115. procedure newheader;override;
  116. function readheader: longint;override;
  117. function outputallowed: boolean;override;
  118. //procedure doputdata(const b;len:integer);override;
  119. function getheadersize:longint;override;
  120. function getheaderaddr:pentryheader;override;
  121. procedure resetfile;override;
  122. {$ifdef DEBUG_PPU}
  123. procedure ppu_log(st :string);override;
  124. {$endif}
  125. public
  126. header : tppuheader;
  127. { crc for the entire unit }
  128. crc,
  129. { crc for the interface definitions in this unit }
  130. interface_crc,
  131. { crc of all object/class definitions in the interface of this unit, xor'ed
  132. by the crc's of all object/class definitions in the interfaces of units
  133. used by this unit. Reason: see mantis #13840 }
  134. indirect_crc : cardinal;
  135. do_crc,
  136. do_interface_crc,
  137. do_indirect_crc : boolean;
  138. crc_only : boolean; { used to calculate interface_crc before implementation }
  139. constructor Create(const fn:string);
  140. destructor destroy;override;
  141. function CheckPPUId:boolean;
  142. {read}
  143. { nothing special currently }
  144. {write}
  145. function createfile:boolean;override;
  146. procedure writeheader;override;
  147. procedure putdata(const b;len:integer);override;
  148. end;
  149. implementation
  150. uses
  151. {$ifdef Test_Double_checksum}
  152. comphook,
  153. {$endif def Test_Double_checksum}
  154. fpccrc;
  155. function swapendian_ppureal(d:ppureal):ppureal;
  156. type ppureal_bytes=array[0..sizeof(d)-1] of byte;
  157. var i:0..sizeof(d)-1;
  158. begin
  159. for i:=low(ppureal_bytes) to high(ppureal_bytes) do
  160. ppureal_bytes(swapendian_ppureal)[i]:=ppureal_bytes(d)[high(ppureal_bytes)-i];
  161. end;
  162. {*****************************************************************************
  163. TPPUFile
  164. *****************************************************************************}
  165. constructor tppufile.Create(const fn:string);
  166. begin
  167. inherited Create(fn);
  168. crc_only:=false;
  169. {$ifdef Test_Double_checksum}
  170. if not assigned(crc_test) then
  171. new(crc_test);
  172. if not assigned(crc_test2) then
  173. new(crc_test2);
  174. {$endif Test_Double_checksum}
  175. end;
  176. destructor tppufile.destroy;
  177. begin
  178. {$ifdef Test_Double_checksum}
  179. if assigned(crc_test) then
  180. dispose(crc_test);
  181. crc_test:=nil;
  182. if assigned(crc_test2) then
  183. dispose(crc_test2);
  184. crc_test2:=nil;
  185. {$endif Test_Double_checksum}
  186. inherited destroy;
  187. end;
  188. function tppufile.CheckPPUId:boolean;
  189. begin
  190. CheckPPUId:=((Header.common.Id[1]='P') and
  191. (Header.common.Id[2]='P') and
  192. (Header.common.Id[3]='U'));
  193. end;
  194. procedure tppufile.newheader;
  195. var
  196. s : string;
  197. begin
  198. fillchar(header,sizeof(tppuheader),0);
  199. str(currentppuversion,s);
  200. while length(s)<3 do
  201. s:='0'+s;
  202. with header.common do
  203. begin
  204. Id[1]:='P';
  205. Id[2]:='P';
  206. Id[3]:='U';
  207. Ver[1]:=s[1];
  208. Ver[2]:=s[2];
  209. Ver[3]:=s[3];
  210. end;
  211. end;
  212. function tppufile.readheader: longint;
  213. begin
  214. if fsize<sizeof(tppuheader) then
  215. exit(0);
  216. result:=f.Read(header,sizeof(tppuheader));
  217. { The header is always stored in little endian order }
  218. { therefore swap if on a big endian machine }
  219. {$IFDEF ENDIAN_BIG}
  220. header.common.compiler := swapendian(header.common.compiler);
  221. header.common.cpu := swapendian(header.common.cpu);
  222. header.common.target := swapendian(header.common.target);
  223. header.common.flags := swapendian(header.common.flags);
  224. header.common.size := swapendian(header.common.size);
  225. header.checksum := swapendian(header.checksum);
  226. header.interface_checksum := swapendian(header.interface_checksum);
  227. header.indirect_checksum := swapendian(header.indirect_checksum);
  228. header.deflistsize:=swapendian(header.deflistsize);
  229. header.symlistsize:=swapendian(header.symlistsize);
  230. { the PPU DATA is stored in native order }
  231. change_endian := (header.common.flags and uf_little_endian) = uf_little_endian;
  232. {$ELSE not ENDIAN_BIG}
  233. change_endian := (header.common.flags and uf_big_endian) = uf_big_endian;
  234. {$ENDIF}
  235. end;
  236. function tppufile.outputallowed: boolean;
  237. begin
  238. result:=not crc_only;
  239. end;
  240. {$ifdef DEBUG_PPU}
  241. procedure tppufile.ppu_log(st :string);
  242. begin
  243. inherited ppu_log(st);
  244. if flog_open then
  245. begin
  246. if do_crc and (ppu_log_idx < bufstart+bufidx) then
  247. begin
  248. writeln(flog,'New crc : ',hexstr(dword(crc),8));
  249. writeln(flog,'New interface crc : ',hexstr(dword(interface_crc),8));
  250. writeln(flog,'New indirect crc : ',hexstr(dword(indirect_crc),8));
  251. ppu_log_idx:=bufstart+bufidx;
  252. end;
  253. end;
  254. {$ifdef IN_PPUDUMP}
  255. if update_crc then
  256. begin
  257. writeln('New crc : ',hexstr(dword(crc),8));
  258. writeln('New interface crc : ',hexstr(dword(interface_crc),8));
  259. writeln('New indirect crc : ',hexstr(dword(indirect_crc),8));
  260. end;
  261. {$endif}
  262. end;
  263. {$endif}
  264. {*****************************************************************************
  265. TPPUFile Reading
  266. *****************************************************************************}
  267. { nothing special currently }
  268. {*****************************************************************************
  269. TPPUFile Writing
  270. *****************************************************************************}
  271. function tppufile.createfile:boolean;
  272. begin
  273. {$ifdef INTFPPU}
  274. if crc_only then
  275. begin
  276. fname:=fname+'.intf';
  277. crc_only:=false;
  278. end;
  279. {$endif}
  280. result:=inherited createfile;
  281. end;
  282. procedure tppufile.writeheader;
  283. var
  284. opos : integer;
  285. begin
  286. if crc_only then
  287. exit;
  288. { flush buffer }
  289. writebuf;
  290. { update size (w/o header!) in the header }
  291. header.common.size:=bufstart-sizeof(tppuheader);
  292. { set the endian flag }
  293. {$ifndef FPC_BIG_ENDIAN}
  294. header.common.flags := header.common.flags or uf_little_endian;
  295. {$else not FPC_BIG_ENDIAN}
  296. header.common.flags := header.common.flags or uf_big_endian;
  297. { Now swap the header.common in the correct endian (always little endian) }
  298. header.common.compiler := swapendian(header.common.compiler);
  299. header.common.cpu := swapendian(header.common.cpu);
  300. header.common.target := swapendian(header.common.target);
  301. header.common.flags := swapendian(header.common.flags);
  302. header.common.size := swapendian(header.common.size);
  303. header.checksum := swapendian(header.checksum);
  304. header.interface_checksum := swapendian(header.interface_checksum);
  305. header.indirect_checksum := swapendian(header.indirect_checksum);
  306. header.deflistsize:=swapendian(header.deflistsize);
  307. header.symlistsize:=swapendian(header.symlistsize);
  308. {$endif not FPC_BIG_ENDIAN}
  309. { write header and restore filepos after it }
  310. opos:=f.Position;
  311. f.Position:=0;
  312. f.Write(header,sizeof(tppuheader));
  313. f.Position:=opos;
  314. end;
  315. procedure tppufile.putdata(const b;len:integer);
  316. begin
  317. if do_crc then
  318. begin
  319. crc:=UpdateCrc32(crc,b,len);
  320. {$ifdef Test_Double_checksum}
  321. if crc_only then
  322. begin
  323. crc_test2^[crc_index2]:=crc;
  324. {$ifdef Test_Double_checksum_write}
  325. Writeln(CRCFile,crc);
  326. {$endif Test_Double_checksum_write}
  327. if crc_index2<crc_array_size then
  328. inc(crc_index2);
  329. end
  330. else
  331. begin
  332. if (crcindex2<crc_array_size) and (crcindex2<crc_index2) and
  333. (crc_test2^[crcindex2]<>crc) then
  334. Do_comment(V_Note,'impl CRC changed');
  335. {$ifdef Test_Double_checksum_write}
  336. Writeln(CRCFile,crc);
  337. {$endif Test_Double_checksum_write}
  338. inc(crcindex2);
  339. end;
  340. {$endif def Test_Double_checksum}
  341. if do_interface_crc then
  342. begin
  343. interface_crc:=UpdateCrc32(interface_crc,b,len);
  344. {$ifdef Test_Double_checksum}
  345. if crc_only then
  346. begin
  347. crc_test^[crc_index]:=interface_crc;
  348. {$ifdef Test_Double_checksum_write}
  349. Writeln(CRCFile,interface_crc);
  350. {$endif Test_Double_checksum_write}
  351. if crc_index<crc_array_size then
  352. inc(crc_index);
  353. end
  354. else
  355. begin
  356. if (crcindex<crc_array_size) and (crcindex<crc_index) and
  357. (crc_test^[crcindex]<>interface_crc) then
  358. Do_comment(V_Warning,'CRC changed');
  359. {$ifdef Test_Double_checksum_write}
  360. Writeln(CRCFile,interface_crc);
  361. {$endif Test_Double_checksum_write}
  362. inc(crcindex);
  363. end;
  364. {$endif def Test_Double_checksum}
  365. { indirect crc must only be calculated for the interface; changes
  366. to a class in the implementation cannot require another unit to
  367. be recompiled }
  368. if do_indirect_crc then
  369. indirect_crc:=UpdateCrc32(indirect_crc,b,len);
  370. end;
  371. end;
  372. inherited putdata(b,len);
  373. (*if not crc_only then
  374. writedata(b,len);
  375. inc(entryidx,len);*)
  376. end;
  377. function tppufile.getheadersize: longint;
  378. begin
  379. result:=sizeof(header);
  380. end;
  381. function tppufile.getheaderaddr: pentryheader;
  382. begin
  383. result:=@header;
  384. end;
  385. procedure tppufile.resetfile;
  386. begin
  387. crc:=0;
  388. interface_crc:=0;
  389. indirect_crc:=0;
  390. do_interface_crc:=true;
  391. do_indirect_crc:=false;
  392. do_crc:=true;
  393. end;
  394. end.