ppu.pas 12 KB

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