ppu.pas 12 KB

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