2
0

ppu.pas 13 KB

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