ppu.pas 12 KB

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