ppu.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 = 6;
  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. tppuerror=(ppuentrytoobig,ppuentryerror);
  57. tppuheader=record
  58. common : tentryheader;
  59. checksum : cardinal; { checksum for this ppufile }
  60. interface_checksum : cardinal;
  61. deflistsize,
  62. symlistsize : longint;
  63. indirect_checksum: cardinal;
  64. end;
  65. tppuentry=tentry;
  66. tunitasmlisttype=(ualt_public,ualt_extern);
  67. { tppufile }
  68. tppufile=class(tentryfile)
  69. {$ifdef Test_Double_checksum}
  70. public
  71. crcindex,
  72. crc_index,
  73. crcindex2,
  74. crc_index2 : cardinal;
  75. crc_test,
  76. crc_test2 : pcrc_array;
  77. private
  78. {$endif def Test_Double_checksum}
  79. protected
  80. procedure newheader;override;
  81. function readheader: longint;override;
  82. function outputallowed: boolean;override;
  83. //procedure doputdata(const b;len:integer);override;
  84. function getheadersize:longint;override;
  85. function getheaderaddr:pentryheader;override;
  86. procedure resetfile;override;
  87. {$ifdef DEBUG_PPU}
  88. procedure ppu_log(st :string);override;
  89. {$endif}
  90. public
  91. header : tppuheader;
  92. { crc for the entire unit }
  93. crc,
  94. { crc for the interface definitions in this unit }
  95. interface_crc,
  96. { crc of all object/class definitions in the interface of this unit, xor'ed
  97. by the crc's of all object/class definitions in the interfaces of units
  98. used by this unit. Reason: see mantis #13840 }
  99. indirect_crc : cardinal;
  100. do_crc,
  101. do_interface_crc,
  102. do_indirect_crc : boolean;
  103. crc_only : boolean; { used to calculate interface_crc before implementation }
  104. constructor Create(const fn:string);
  105. destructor destroy;override;
  106. function CheckPPUId:boolean;
  107. {read}
  108. { nothing special currently }
  109. {write}
  110. function createfile:boolean;override;
  111. procedure writeheader;override;
  112. procedure putdata(const b;len:integer);override;
  113. end;
  114. implementation
  115. uses
  116. {$ifdef Test_Double_checksum}
  117. comphook,
  118. {$endif def Test_Double_checksum}
  119. fpccrc;
  120. function swapendian_ppureal(d:ppureal):ppureal;
  121. type ppureal_bytes=array[0..sizeof(d)-1] of byte;
  122. var i:0..sizeof(d)-1;
  123. begin
  124. for i:=low(ppureal_bytes) to high(ppureal_bytes) do
  125. ppureal_bytes(swapendian_ppureal)[i]:=ppureal_bytes(d)[high(ppureal_bytes)-i];
  126. end;
  127. {*****************************************************************************
  128. TPPUFile
  129. *****************************************************************************}
  130. constructor tppufile.Create(const fn:string);
  131. begin
  132. inherited Create(fn);
  133. crc_only:=false;
  134. {$ifdef Test_Double_checksum}
  135. if not assigned(crc_test) then
  136. new(crc_test);
  137. if not assigned(crc_test2) then
  138. new(crc_test2);
  139. {$endif Test_Double_checksum}
  140. end;
  141. destructor tppufile.destroy;
  142. begin
  143. {$ifdef Test_Double_checksum}
  144. if assigned(crc_test) then
  145. dispose(crc_test);
  146. crc_test:=nil;
  147. if assigned(crc_test2) then
  148. dispose(crc_test2);
  149. crc_test2:=nil;
  150. {$endif Test_Double_checksum}
  151. inherited destroy;
  152. end;
  153. function tppufile.CheckPPUId:boolean;
  154. begin
  155. CheckPPUId:=((Header.common.Id[1]='P') and
  156. (Header.common.Id[2]='P') and
  157. (Header.common.Id[3]='U'));
  158. end;
  159. procedure tppufile.newheader;
  160. var
  161. s : string;
  162. begin
  163. fillchar(header,sizeof(tppuheader),0);
  164. str(currentppuversion,s);
  165. while length(s)<3 do
  166. s:='0'+s;
  167. with header.common do
  168. begin
  169. Id[1]:='P';
  170. Id[2]:='P';
  171. Id[3]:='U';
  172. Ver[1]:=s[1];
  173. Ver[2]:=s[2];
  174. Ver[3]:=s[3];
  175. end;
  176. end;
  177. function tppufile.readheader: longint;
  178. begin
  179. if fsize<sizeof(tppuheader) then
  180. exit(0);
  181. result:=f.Read(header,sizeof(tppuheader));
  182. { The header is always stored in little endian order }
  183. { therefore swap if on a big endian machine }
  184. {$IFDEF ENDIAN_BIG}
  185. header.common.compiler := swapendian(header.common.compiler);
  186. header.common.cpu := swapendian(header.common.cpu);
  187. header.common.target := swapendian(header.common.target);
  188. header.common.flags := swapendian(header.common.flags);
  189. header.common.size := swapendian(header.common.size);
  190. header.checksum := swapendian(header.checksum);
  191. header.interface_checksum := swapendian(header.interface_checksum);
  192. header.indirect_checksum := swapendian(header.indirect_checksum);
  193. header.deflistsize:=swapendian(header.deflistsize);
  194. header.symlistsize:=swapendian(header.symlistsize);
  195. { the PPU DATA is stored in native order }
  196. change_endian := (header.common.flags and uf_little_endian) = uf_little_endian;
  197. {$ELSE not ENDIAN_BIG}
  198. change_endian := (header.common.flags and uf_big_endian) = uf_big_endian;
  199. {$ENDIF}
  200. end;
  201. function tppufile.outputallowed: boolean;
  202. begin
  203. result:=not crc_only;
  204. end;
  205. {$ifdef DEBUG_PPU}
  206. procedure tppufile.ppu_log(st :string);
  207. begin
  208. inherited ppu_log(st);
  209. if flog_open then
  210. begin
  211. if do_crc and (ppu_log_idx < bufstart+bufidx) then
  212. begin
  213. writeln(flog,'New crc : ',hexstr(dword(crc),8));
  214. writeln(flog,'New interface crc : ',hexstr(dword(interface_crc),8));
  215. writeln(flog,'New indirect crc : ',hexstr(dword(indirect_crc),8));
  216. ppu_log_idx:=bufstart+bufidx;
  217. end;
  218. end;
  219. {$ifdef IN_PPUDUMP}
  220. if update_crc then
  221. begin
  222. writeln('New crc : ',hexstr(dword(crc),8));
  223. writeln('New interface crc : ',hexstr(dword(interface_crc),8));
  224. writeln('New indirect crc : ',hexstr(dword(indirect_crc),8));
  225. end;
  226. {$endif}
  227. end;
  228. {$endif}
  229. {*****************************************************************************
  230. TPPUFile Reading
  231. *****************************************************************************}
  232. { nothing special currently }
  233. {*****************************************************************************
  234. TPPUFile Writing
  235. *****************************************************************************}
  236. function tppufile.createfile:boolean;
  237. begin
  238. {$ifdef INTFPPU}
  239. if crc_only then
  240. begin
  241. fname:=fname+'.intf';
  242. crc_only:=false;
  243. end;
  244. {$endif}
  245. result:=inherited createfile;
  246. end;
  247. procedure tppufile.writeheader;
  248. var
  249. opos : integer;
  250. begin
  251. if crc_only then
  252. exit;
  253. { flush buffer }
  254. writebuf;
  255. { update size (w/o header!) in the header }
  256. header.common.size:=bufstart-sizeof(tppuheader);
  257. { set the endian flag }
  258. {$ifndef FPC_BIG_ENDIAN}
  259. header.common.flags := header.common.flags or uf_little_endian;
  260. {$else not FPC_BIG_ENDIAN}
  261. header.common.flags := header.common.flags or uf_big_endian;
  262. { Now swap the header.common in the correct endian (always little endian) }
  263. header.common.compiler := swapendian(header.common.compiler);
  264. header.common.cpu := swapendian(header.common.cpu);
  265. header.common.target := swapendian(header.common.target);
  266. header.common.flags := swapendian(header.common.flags);
  267. header.common.size := swapendian(header.common.size);
  268. header.checksum := swapendian(header.checksum);
  269. header.interface_checksum := swapendian(header.interface_checksum);
  270. header.indirect_checksum := swapendian(header.indirect_checksum);
  271. header.deflistsize:=swapendian(header.deflistsize);
  272. header.symlistsize:=swapendian(header.symlistsize);
  273. {$endif not FPC_BIG_ENDIAN}
  274. { write header and restore filepos after it }
  275. opos:=f.Position;
  276. f.Position:=0;
  277. f.Write(header,sizeof(tppuheader));
  278. f.Position:=opos;
  279. end;
  280. procedure tppufile.putdata(const b;len:integer);
  281. begin
  282. if do_crc then
  283. begin
  284. crc:=UpdateCrc32(crc,b,len);
  285. {$ifdef Test_Double_checksum}
  286. if crc_only then
  287. begin
  288. crc_test2^[crc_index2]:=crc;
  289. {$ifdef Test_Double_checksum_write}
  290. Writeln(CRCFile,crc);
  291. {$endif Test_Double_checksum_write}
  292. if crc_index2<crc_array_size then
  293. inc(crc_index2);
  294. end
  295. else
  296. begin
  297. if (crcindex2<crc_array_size) and (crcindex2<crc_index2) and
  298. (crc_test2^[crcindex2]<>crc) then
  299. Do_comment(V_Note,'impl CRC changed');
  300. {$ifdef Test_Double_checksum_write}
  301. Writeln(CRCFile,crc);
  302. {$endif Test_Double_checksum_write}
  303. inc(crcindex2);
  304. end;
  305. {$endif def Test_Double_checksum}
  306. if do_interface_crc then
  307. begin
  308. interface_crc:=UpdateCrc32(interface_crc,b,len);
  309. {$ifdef Test_Double_checksum}
  310. if crc_only then
  311. begin
  312. crc_test^[crc_index]:=interface_crc;
  313. {$ifdef Test_Double_checksum_write}
  314. Writeln(CRCFile,interface_crc);
  315. {$endif Test_Double_checksum_write}
  316. if crc_index<crc_array_size then
  317. inc(crc_index);
  318. end
  319. else
  320. begin
  321. if (crcindex<crc_array_size) and (crcindex<crc_index) and
  322. (crc_test^[crcindex]<>interface_crc) then
  323. Do_comment(V_Warning,'CRC changed');
  324. {$ifdef Test_Double_checksum_write}
  325. Writeln(CRCFile,interface_crc);
  326. {$endif Test_Double_checksum_write}
  327. inc(crcindex);
  328. end;
  329. {$endif def Test_Double_checksum}
  330. { indirect crc must only be calculated for the interface; changes
  331. to a class in the implementation cannot require another unit to
  332. be recompiled }
  333. if do_indirect_crc then
  334. indirect_crc:=UpdateCrc32(indirect_crc,b,len);
  335. end;
  336. end;
  337. inherited putdata(b,len);
  338. (*if not crc_only then
  339. writedata(b,len);
  340. inc(entryidx,len);*)
  341. end;
  342. function tppufile.getheadersize: longint;
  343. begin
  344. result:=sizeof(header);
  345. end;
  346. function tppufile.getheaderaddr: pentryheader;
  347. begin
  348. result:=@header;
  349. end;
  350. procedure tppufile.resetfile;
  351. begin
  352. crc:=0;
  353. interface_crc:=0;
  354. indirect_crc:=0;
  355. do_interface_crc:=true;
  356. do_indirect_crc:=false;
  357. do_crc:=true;
  358. end;
  359. end.