2
0

ppu.pas 12 KB

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