ppu.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. systems,globtype,constexp,cstreams,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. CurrentPPUVersion = 172;
  36. ppubufsize = 16384;
  37. { unit flags }
  38. uf_init = $000001; { unit has initialization section }
  39. uf_finalize = $000002; { unit has finalization section }
  40. uf_big_endian = $000004;
  41. //uf_has_browser = $000010;
  42. uf_in_library = $000020; { is the file in another file than <ppufile>.* ? }
  43. uf_smart_linked = $000040; { the ppu can be smartlinked }
  44. uf_static_linked = $000080; { the ppu can be linked static }
  45. uf_shared_linked = $000100; { the ppu can be linked shared }
  46. //uf_local_browser = $000200;
  47. uf_no_link = $000400; { unit has no .o generated, but can still have external linking! }
  48. uf_has_resourcestrings = $000800; { unit has resource string section }
  49. uf_little_endian = $001000;
  50. uf_release = $002000; { unit was compiled with -Ur option }
  51. uf_threadvars = $004000; { unit has threadvars }
  52. uf_fpu_emulation = $008000; { this unit was compiled with fpu emulation on }
  53. uf_has_stabs_debuginfo = $010000; { this unit has stabs debuginfo generated }
  54. uf_local_symtable = $020000; { this unit has a local symtable stored }
  55. uf_uses_variants = $040000; { this unit uses variants }
  56. uf_has_resourcefiles = $080000; { this unit has external resources (using $R directive)}
  57. uf_has_exports = $100000; { this module or a used unit has exports }
  58. uf_has_dwarf_debuginfo = $200000; { this unit has dwarf debuginfo generated }
  59. uf_wideinits = $400000; { this unit has winlike widestring typed constants }
  60. uf_classinits = $800000; { this unit has class constructors/destructors }
  61. uf_resstrinits = $1000000; { this unit has string consts referencing resourcestrings }
  62. uf_i8086_far_code = $2000000; { this unit uses an i8086 memory model with far code (i.e. medium, large or huge) }
  63. uf_i8086_far_data = $4000000; { this unit uses an i8086 memory model with far data (i.e. compact or large) }
  64. uf_i8086_huge_data = $8000000; { this unit uses an i8086 memory model with huge data (i.e. huge) }
  65. uf_i8086_cs_equals_ds = $10000000; { this unit uses an i8086 memory model with CS=DS (i.e. tiny) }
  66. uf_package_deny = $20000000; { this unit must not be part of a package }
  67. uf_package_weak = $40000000; { this unit may be completely contained in a package }
  68. type
  69. { bestreal is defined based on the target architecture }
  70. ppureal=bestreal;
  71. tppuerror=(ppuentrytoobig,ppuentryerror);
  72. tppuheader=record
  73. common : tentryheader;
  74. checksum : cardinal; { checksum for this ppufile }
  75. interface_checksum : cardinal;
  76. deflistsize,
  77. symlistsize : longint;
  78. indirect_checksum: cardinal;
  79. end;
  80. tppuentry=tentry;
  81. { tppufile }
  82. tppufile=class(tentryfile)
  83. {$ifdef Test_Double_checksum}
  84. public
  85. crcindex,
  86. crc_index,
  87. crcindex2,
  88. crc_index2 : cardinal;
  89. crc_test,
  90. crc_test2 : pcrc_array;
  91. private
  92. {$endif def Test_Double_checksum}
  93. protected
  94. procedure newheader;override;
  95. function readheader: longint;override;
  96. function outputallowed: boolean;override;
  97. //procedure doputdata(const b;len:integer);override;
  98. function getheadersize:longint;override;
  99. function getheaderaddr:pentryheader;override;
  100. procedure resetfile;override;
  101. public
  102. header : tppuheader;
  103. { crc for the entire unit }
  104. crc,
  105. { crc for the interface definitions in this unit }
  106. interface_crc,
  107. { crc of all object/class definitions in the interface of this unit, xor'ed
  108. by the crc's of all object/class definitions in the interfaces of units
  109. used by this unit. Reason: see mantis #13840 }
  110. indirect_crc : cardinal;
  111. do_crc,
  112. do_interface_crc,
  113. do_indirect_crc : boolean;
  114. crc_only : boolean; { used to calculate interface_crc before implementation }
  115. constructor Create(const fn:string);
  116. procedure closefile;override;
  117. function CheckPPUId:boolean;
  118. {read}
  119. { nothing special currently }
  120. {write}
  121. function createfile:boolean;override;
  122. procedure writeheader;override;
  123. procedure putdata(const b;len:integer);override;
  124. end;
  125. implementation
  126. uses
  127. {$ifdef Test_Double_checksum}
  128. comphook,
  129. {$endif def Test_Double_checksum}
  130. fpccrc,
  131. cutils;
  132. function swapendian_ppureal(d:ppureal):ppureal;
  133. type ppureal_bytes=array[0..sizeof(d)-1] of byte;
  134. var i:0..sizeof(d)-1;
  135. begin
  136. for i:=low(ppureal_bytes) to high(ppureal_bytes) do
  137. ppureal_bytes(swapendian_ppureal)[i]:=ppureal_bytes(d)[high(ppureal_bytes)-i];
  138. end;
  139. {*****************************************************************************
  140. TPPUFile
  141. *****************************************************************************}
  142. constructor tppufile.Create(const fn:string);
  143. begin
  144. inherited Create(fn);
  145. crc_only:=false;
  146. end;
  147. procedure tppufile.closefile;
  148. begin
  149. {$ifdef Test_Double_checksum}
  150. if mode=2 then
  151. begin
  152. if assigned(crc_test) then
  153. dispose(crc_test);
  154. if assigned(crc_test2) then
  155. dispose(crc_test2);
  156. end;
  157. {$endif Test_Double_checksum}
  158. inherited closefile;
  159. end;
  160. function tppufile.CheckPPUId:boolean;
  161. begin
  162. CheckPPUId:=((Header.common.Id[1]='P') and
  163. (Header.common.Id[2]='P') and
  164. (Header.common.Id[3]='U'));
  165. end;
  166. procedure tppufile.newheader;
  167. var
  168. s : string;
  169. begin
  170. fillchar(header,sizeof(tppuheader),0);
  171. str(currentppuversion,s);
  172. while length(s)<3 do
  173. s:='0'+s;
  174. with header.common do
  175. begin
  176. Id[1]:='P';
  177. Id[2]:='P';
  178. Id[3]:='U';
  179. Ver[1]:=s[1];
  180. Ver[2]:=s[2];
  181. Ver[3]:=s[3];
  182. end;
  183. end;
  184. function tppufile.readheader: longint;
  185. begin
  186. if fsize<sizeof(tppuheader) then
  187. exit(0);
  188. result:=f.Read(header,sizeof(tppuheader));
  189. { The header is always stored in little endian order }
  190. { therefore swap if on a big endian machine }
  191. {$IFDEF ENDIAN_BIG}
  192. header.common.compiler := swapendian(header.common.compiler);
  193. header.common.cpu := swapendian(header.common.cpu);
  194. header.common.target := swapendian(header.common.target);
  195. header.common.flags := swapendian(header.common.flags);
  196. header.common.size := swapendian(header.common.size);
  197. header.checksum := swapendian(header.checksum);
  198. header.interface_checksum := swapendian(header.interface_checksum);
  199. header.indirect_checksum := swapendian(header.indirect_checksum);
  200. header.deflistsize:=swapendian(header.deflistsize);
  201. header.symlistsize:=swapendian(header.symlistsize);
  202. {$ENDIF}
  203. { the PPU DATA is stored in native order }
  204. if (header.common.flags and uf_big_endian) = uf_big_endian then
  205. Begin
  206. {$IFDEF ENDIAN_LITTLE}
  207. change_endian := TRUE;
  208. {$ELSE}
  209. change_endian := FALSE;
  210. {$ENDIF}
  211. End
  212. else if (header.common.flags and uf_little_endian) = uf_little_endian then
  213. Begin
  214. {$IFDEF ENDIAN_BIG}
  215. change_endian := TRUE;
  216. {$ELSE}
  217. change_endian := FALSE;
  218. {$ENDIF}
  219. End;
  220. end;
  221. function tppufile.outputallowed: boolean;
  222. begin
  223. result:=not crc_only;
  224. end;
  225. {*****************************************************************************
  226. TPPUFile Reading
  227. *****************************************************************************}
  228. { nothing special currently }
  229. {*****************************************************************************
  230. TPPUFile Writing
  231. *****************************************************************************}
  232. function tppufile.createfile:boolean;
  233. begin
  234. {$ifdef INTFPPU}
  235. if crc_only then
  236. begin
  237. fname:=fname+'.intf';
  238. crc_only:=false;
  239. end;
  240. {$endif}
  241. result:=inherited createfile;
  242. end;
  243. procedure tppufile.writeheader;
  244. var
  245. opos : integer;
  246. begin
  247. if crc_only then
  248. exit;
  249. { flush buffer }
  250. writebuf;
  251. { update size (w/o header!) in the header }
  252. header.common.size:=bufstart-sizeof(tppuheader);
  253. { set the endian flag }
  254. {$ifndef FPC_BIG_ENDIAN}
  255. header.common.flags := header.common.flags or uf_little_endian;
  256. {$else not FPC_BIG_ENDIAN}
  257. header.common.flags := header.common.flags or uf_big_endian;
  258. { Now swap the header.common in the correct endian (always little endian) }
  259. header.common.compiler := swapendian(header.common.compiler);
  260. header.common.cpu := swapendian(header.common.cpu);
  261. header.common.target := swapendian(header.common.target);
  262. header.common.flags := swapendian(header.common.flags);
  263. header.common.size := swapendian(header.common.size);
  264. header.checksum := swapendian(header.checksum);
  265. header.interface_checksum := swapendian(header.interface_checksum);
  266. header.indirect_checksum := swapendian(header.indirect_checksum);
  267. header.deflistsize:=swapendian(header.deflistsize);
  268. header.symlistsize:=swapendian(header.symlistsize);
  269. {$endif not FPC_BIG_ENDIAN}
  270. { write header and restore filepos after it }
  271. opos:=f.Position;
  272. f.Position:=0;
  273. f.Write(header,sizeof(tppuheader));
  274. f.Position:=opos;
  275. end;
  276. procedure tppufile.putdata(const b;len:integer);
  277. begin
  278. if do_crc then
  279. begin
  280. crc:=UpdateCrc32(crc,b,len);
  281. {$ifdef Test_Double_checksum}
  282. if crc_only then
  283. begin
  284. crc_test2^[crc_index2]:=crc;
  285. {$ifdef Test_Double_checksum_write}
  286. Writeln(CRCFile,crc);
  287. {$endif Test_Double_checksum_write}
  288. if crc_index2<crc_array_size then
  289. inc(crc_index2);
  290. end
  291. else
  292. begin
  293. if (crcindex2<crc_array_size) and (crcindex2<crc_index2) and
  294. (crc_test2^[crcindex2]<>crc) then
  295. Do_comment(V_Note,'impl CRC changed');
  296. {$ifdef Test_Double_checksum_write}
  297. Writeln(CRCFile,crc);
  298. {$endif Test_Double_checksum_write}
  299. inc(crcindex2);
  300. end;
  301. {$endif def Test_Double_checksum}
  302. if do_interface_crc then
  303. begin
  304. interface_crc:=UpdateCrc32(interface_crc,b,len);
  305. {$ifdef Test_Double_checksum}
  306. if crc_only then
  307. begin
  308. crc_test^[crc_index]:=interface_crc;
  309. {$ifdef Test_Double_checksum_write}
  310. Writeln(CRCFile,interface_crc);
  311. {$endif Test_Double_checksum_write}
  312. if crc_index<crc_array_size then
  313. inc(crc_index);
  314. end
  315. else
  316. begin
  317. if (crcindex<crc_array_size) and (crcindex<crc_index) and
  318. (crc_test^[crcindex]<>interface_crc) then
  319. Do_comment(V_Warning,'CRC changed');
  320. {$ifdef Test_Double_checksum_write}
  321. Writeln(CRCFile,interface_crc);
  322. {$endif Test_Double_checksum_write}
  323. inc(crcindex);
  324. end;
  325. {$endif def Test_Double_checksum}
  326. { indirect crc must only be calculated for the interface; changes
  327. to a class in the implementation cannot require another unit to
  328. be recompiled }
  329. if do_indirect_crc then
  330. indirect_crc:=UpdateCrc32(indirect_crc,b,len);
  331. end;
  332. end;
  333. inherited putdata(b,len);
  334. (*if not crc_only then
  335. writedata(b,len);
  336. inc(entryidx,len);*)
  337. end;
  338. function tppufile.getheadersize: longint;
  339. begin
  340. result:=sizeof(header);
  341. end;
  342. function tppufile.getheaderaddr: pentryheader;
  343. begin
  344. result:=@header;
  345. end;
  346. procedure tppufile.resetfile;
  347. begin
  348. crc:=0;
  349. interface_crc:=0;
  350. indirect_crc:=0;
  351. do_interface_crc:=true;
  352. do_indirect_crc:=false;
  353. do_crc:=true;
  354. end;
  355. end.