ppu.pas 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  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. globtype,constexp;
  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 longint;
  32. pcrc_array = ^tcrc_array;
  33. {$endif Test_Double_checksum}
  34. const
  35. CurrentPPUVersion = 127;
  36. { buffer sizes }
  37. maxentrysize = 1024;
  38. ppubufsize = 16384;
  39. {ppu entries}
  40. mainentryid = 1;
  41. subentryid = 2;
  42. {special}
  43. iberror = 0;
  44. ibstartdefs = 248;
  45. ibenddefs = 249;
  46. ibstartsyms = 250;
  47. ibendsyms = 251;
  48. ibendinterface = 252;
  49. ibendimplementation = 253;
  50. // ibendbrowser = 254;
  51. ibend = 255;
  52. {general}
  53. ibmodulename = 1;
  54. ibsourcefiles = 2;
  55. ibloadunit = 3;
  56. ibinitunit = 4;
  57. iblinkunitofiles = 5;
  58. iblinkunitstaticlibs = 6;
  59. iblinkunitsharedlibs = 7;
  60. iblinkotherofiles = 8;
  61. iblinkotherstaticlibs = 9;
  62. iblinkothersharedlibs = 10;
  63. ibImportSymbols = 11;
  64. ibsymref = 12;
  65. ibdefref = 13;
  66. // ibendsymtablebrowser = 14;
  67. // ibbeginsymtablebrowser = 15;
  68. {$IFDEF MACRO_DIFF_HINT}
  69. ibusedmacros = 16;
  70. {$ENDIF}
  71. ibderefdata = 17;
  72. ibexportedmacros = 18;
  73. ibderefmap = 19;
  74. {syms}
  75. ibtypesym = 20;
  76. ibprocsym = 21;
  77. ibstaticvarsym = 22;
  78. ibconstsym = 23;
  79. ibenumsym = 24;
  80. // ibtypedconstsym = 25;
  81. ibabsolutevarsym = 26;
  82. ibpropertysym = 27;
  83. ibfieldvarsym = 28;
  84. ibunitsym = 29;
  85. iblabelsym = 30;
  86. ibsyssym = 31;
  87. // ibrttisym = 32;
  88. iblocalvarsym = 33;
  89. ibparavarsym = 34;
  90. ibmacrosym = 35;
  91. {definitions}
  92. iborddef = 40;
  93. ibpointerdef = 41;
  94. ibarraydef = 42;
  95. ibprocdef = 43;
  96. ibshortstringdef = 44;
  97. ibrecorddef = 45;
  98. ibfiledef = 46;
  99. ibformaldef = 47;
  100. ibobjectdef = 48;
  101. ibenumdef = 49;
  102. ibsetdef = 50;
  103. ibprocvardef = 51;
  104. ibfloatdef = 52;
  105. ibclassrefdef = 53;
  106. iblongstringdef = 54;
  107. ibansistringdef = 55;
  108. ibwidestringdef = 56;
  109. ibvariantdef = 57;
  110. ibundefineddef = 58;
  111. ibunicodestringdef = 59;
  112. {implementation/ObjData}
  113. ibnodetree = 80;
  114. ibasmsymbols = 81;
  115. ibresources = 82;
  116. ibcreatedobjtypes = 83;
  117. ibwpofile = 84;
  118. ibmoduleoptions = 85;
  119. ibmainname = 90;
  120. { target-specific things }
  121. iblinkotherframeworks = 100;
  122. { unit flags }
  123. uf_init = $000001; { unit has initialization section }
  124. uf_finalize = $000002; { unit has finalization section }
  125. uf_big_endian = $000004;
  126. //uf_has_browser = $000010;
  127. uf_in_library = $000020; { is the file in another file than <ppufile>.* ? }
  128. uf_smart_linked = $000040; { the ppu can be smartlinked }
  129. uf_static_linked = $000080; { the ppu can be linked static }
  130. uf_shared_linked = $000100; { the ppu can be linked shared }
  131. //uf_local_browser = $000200;
  132. uf_no_link = $000400; { unit has no .o generated, but can still have external linking! }
  133. uf_has_resourcestrings = $000800; { unit has resource string section }
  134. uf_little_endian = $001000;
  135. uf_release = $002000; { unit was compiled with -Ur option }
  136. uf_threadvars = $004000; { unit has threadvars }
  137. uf_fpu_emulation = $008000; { this unit was compiled with fpu emulation on }
  138. uf_has_stabs_debuginfo = $010000; { this unit has stabs debuginfo generated }
  139. uf_local_symtable = $020000; { this unit has a local symtable stored }
  140. uf_uses_variants = $040000; { this unit uses variants }
  141. uf_has_resourcefiles = $080000; { this unit has external resources (using $R directive)}
  142. uf_has_exports = $100000; { this module or a used unit has exports }
  143. uf_has_dwarf_debuginfo = $200000; { this unit has dwarf debuginfo generated }
  144. uf_wideinits = $400000; { this unit has winlike widestring typed constants }
  145. uf_classinits = $800000; { this unit has class constructors/destructors }
  146. type
  147. { bestreal is defined based on the target architecture }
  148. ppureal=bestreal;
  149. tppuerror=(ppuentrytoobig,ppuentryerror);
  150. tppuheader=record
  151. id : array[1..3] of char; { = 'PPU' }
  152. ver : array[1..3] of char;
  153. compiler : word;
  154. cpu : word;
  155. target : word;
  156. flags : longint;
  157. size : longint; { size of the ppufile without header }
  158. checksum : cardinal; { checksum for this ppufile }
  159. interface_checksum : cardinal;
  160. deflistsize,
  161. symlistsize : longint;
  162. indirect_checksum: cardinal;
  163. end;
  164. tppuentry=packed record
  165. size : longint;
  166. id : byte;
  167. nr : byte;
  168. end;
  169. tppufile=class
  170. private
  171. f : file;
  172. mode : byte; {0 - Closed, 1 - Reading, 2 - Writing}
  173. fname : string;
  174. fsize : integer;
  175. {$ifdef Test_Double_checksum}
  176. public
  177. crcindex,
  178. crc_index,
  179. crcindex2,
  180. crc_index2 : cardinal;
  181. crc_test,
  182. crc_test2 : pcrc_array;
  183. private
  184. {$endif def Test_Double_checksum}
  185. change_endian : boolean;
  186. buf : pchar;
  187. bufstart,
  188. bufsize,
  189. bufidx : integer;
  190. entrybufstart,
  191. entrystart,
  192. entryidx : integer;
  193. entry : tppuentry;
  194. closed,
  195. tempclosed : boolean;
  196. closepos : integer;
  197. public
  198. entrytyp : byte;
  199. header : tppuheader;
  200. size : integer;
  201. { crc for the entire unit }
  202. crc,
  203. { crc for the interface definitions in this unit }
  204. interface_crc,
  205. { crc of all object/class definitions in the interface of this unit, xor'ed
  206. by the crc's of all object/class definitions in the interfaces of units
  207. used by this unit. Reason: see mantis #13840 }
  208. indirect_crc : cardinal;
  209. error,
  210. do_crc,
  211. do_interface_crc,
  212. do_indirect_crc : boolean;
  213. crc_only : boolean; { used to calculate interface_crc before implementation }
  214. constructor Create(const fn:string);
  215. destructor Destroy;override;
  216. procedure flush;
  217. procedure closefile;
  218. function CheckPPUId:boolean;
  219. function GetPPUVersion:integer;
  220. procedure NewHeader;
  221. procedure NewEntry;
  222. {read}
  223. function openfile:boolean;
  224. procedure reloadbuf;
  225. procedure readdata(var b;len:integer);
  226. procedure skipdata(len:integer);
  227. function readentry:byte;
  228. function EndOfEntry:boolean;
  229. function entrysize:longint;
  230. procedure getdatabuf(var b;len:integer;var res:integer);
  231. procedure getdata(var b;len:integer);
  232. function getbyte:byte;
  233. function getword:word;
  234. function getdword:dword;
  235. function getlongint:longint;
  236. function getint64:int64;
  237. function getqword:qword;
  238. function getaint:aint;
  239. function getasizeint:asizeint;
  240. function getaword:aword;
  241. function getreal:ppureal;
  242. function getstring:string;
  243. procedure getnormalset(var b);
  244. procedure getsmallset(var b);
  245. function skipuntilentry(untilb:byte):boolean;
  246. {write}
  247. function createfile:boolean;
  248. procedure writeheader;
  249. procedure writebuf;
  250. procedure writedata(const b;len:integer);
  251. procedure writeentry(ibnr:byte);
  252. procedure putdata(const b;len:integer);
  253. procedure putbyte(b:byte);
  254. procedure putword(w:word);
  255. procedure putdword(w:dword);
  256. procedure putlongint(l:longint);
  257. procedure putint64(i:int64);
  258. procedure putqword(q:qword);
  259. procedure putaint(i:aint);
  260. procedure putaword(i:aword);
  261. procedure putreal(d:ppureal);
  262. procedure putstring(const s:string);
  263. procedure putnormalset(const b);
  264. procedure putsmallset(const b);
  265. procedure tempclose;
  266. function tempopen:boolean;
  267. end;
  268. implementation
  269. uses
  270. systems,
  271. {$ifdef Test_Double_checksum}
  272. comphook,
  273. {$endif def Test_Double_checksum}
  274. fpccrc,
  275. cutils;
  276. function swapendian_ppureal(d:ppureal):ppureal;
  277. type ppureal_bytes=array[0..sizeof(d)-1] of byte;
  278. var i:0..sizeof(d)-1;
  279. begin
  280. for i:=low(ppureal_bytes) to high(ppureal_bytes) do
  281. ppureal_bytes(swapendian_ppureal)[i]:=ppureal_bytes(d)[high(ppureal_bytes)-i];
  282. end;
  283. {*****************************************************************************
  284. TPPUFile
  285. *****************************************************************************}
  286. constructor tppufile.Create(const fn:string);
  287. begin
  288. fname:=fn;
  289. change_endian:=false;
  290. crc_only:=false;
  291. Mode:=0;
  292. NewHeader;
  293. Error:=false;
  294. closed:=true;
  295. tempclosed:=false;
  296. getmem(buf,ppubufsize);
  297. end;
  298. destructor tppufile.destroy;
  299. begin
  300. closefile;
  301. if assigned(buf) then
  302. freemem(buf,ppubufsize);
  303. end;
  304. procedure tppufile.flush;
  305. begin
  306. if Mode=2 then
  307. writebuf;
  308. end;
  309. procedure tppufile.closefile;
  310. begin
  311. {$ifdef Test_Double_checksum}
  312. if mode=2 then
  313. begin
  314. if assigned(crc_test) then
  315. dispose(crc_test);
  316. if assigned(crc_test2) then
  317. dispose(crc_test2);
  318. end;
  319. {$endif Test_Double_checksum}
  320. if Mode<>0 then
  321. begin
  322. Flush;
  323. {$I-}
  324. system.close(f);
  325. {$I+}
  326. if ioresult<>0 then;
  327. Mode:=0;
  328. closed:=true;
  329. end;
  330. end;
  331. function tppufile.CheckPPUId:boolean;
  332. begin
  333. CheckPPUId:=((Header.Id[1]='P') and (Header.Id[2]='P') and (Header.Id[3]='U'));
  334. end;
  335. function tppufile.GetPPUVersion:integer;
  336. var
  337. l : integer;
  338. code : integer;
  339. begin
  340. Val(header.ver[1]+header.ver[2]+header.ver[3],l,code);
  341. if code=0 then
  342. GetPPUVersion:=l
  343. else
  344. GetPPUVersion:=0;
  345. end;
  346. procedure tppufile.NewHeader;
  347. var
  348. s : string;
  349. begin
  350. fillchar(header,sizeof(tppuheader),0);
  351. str(currentppuversion,s);
  352. while length(s)<3 do
  353. s:='0'+s;
  354. with header do
  355. begin
  356. Id[1]:='P';
  357. Id[2]:='P';
  358. Id[3]:='U';
  359. Ver[1]:=s[1];
  360. Ver[2]:=s[2];
  361. Ver[3]:=s[3];
  362. end;
  363. end;
  364. {*****************************************************************************
  365. TPPUFile Reading
  366. *****************************************************************************}
  367. function tppufile.openfile:boolean;
  368. var
  369. ofmode : byte;
  370. i : integer;
  371. begin
  372. openfile:=false;
  373. assign(f,fname);
  374. ofmode:=filemode;
  375. filemode:=$0;
  376. {$I-}
  377. reset(f,1);
  378. {$I+}
  379. filemode:=ofmode;
  380. if ioresult<>0 then
  381. exit;
  382. closed:=false;
  383. {read ppuheader}
  384. fsize:=filesize(f);
  385. if fsize<sizeof(tppuheader) then
  386. exit;
  387. blockread(f,header,sizeof(tppuheader),i);
  388. { The header is always stored in little endian order }
  389. { therefore swap if on a big endian machine }
  390. {$IFDEF ENDIAN_BIG}
  391. header.compiler := swapendian(header.compiler);
  392. header.cpu := swapendian(header.cpu);
  393. header.target := swapendian(header.target);
  394. header.flags := swapendian(header.flags);
  395. header.size := swapendian(header.size);
  396. header.checksum := swapendian(header.checksum);
  397. header.interface_checksum := swapendian(header.interface_checksum);
  398. header.indirect_checksum := swapendian(header.indirect_checksum);
  399. header.deflistsize:=swapendian(header.deflistsize);
  400. header.symlistsize:=swapendian(header.symlistsize);
  401. {$ENDIF}
  402. { the PPU DATA is stored in native order }
  403. if (header.flags and uf_big_endian) = uf_big_endian then
  404. Begin
  405. {$IFDEF ENDIAN_LITTLE}
  406. change_endian := TRUE;
  407. {$ELSE}
  408. change_endian := FALSE;
  409. {$ENDIF}
  410. End
  411. else if (header.flags and uf_little_endian) = uf_little_endian then
  412. Begin
  413. {$IFDEF ENDIAN_BIG}
  414. change_endian := TRUE;
  415. {$ELSE}
  416. change_endian := FALSE;
  417. {$ENDIF}
  418. End;
  419. {reset buffer}
  420. bufstart:=i;
  421. bufsize:=0;
  422. bufidx:=0;
  423. Mode:=1;
  424. FillChar(entry,sizeof(tppuentry),0);
  425. entryidx:=0;
  426. entrystart:=0;
  427. entrybufstart:=0;
  428. Error:=false;
  429. openfile:=true;
  430. end;
  431. procedure tppufile.reloadbuf;
  432. begin
  433. inc(bufstart,bufsize);
  434. blockread(f,buf^,ppubufsize,bufsize);
  435. bufidx:=0;
  436. end;
  437. procedure tppufile.readdata(var b;len:integer);
  438. var
  439. p,pbuf : pchar;
  440. left : integer;
  441. begin
  442. p:=pchar(@b);
  443. pbuf:=@buf[bufidx];
  444. repeat
  445. left:=bufsize-bufidx;
  446. if len<left then
  447. break;
  448. move(pbuf^,p^,left);
  449. dec(len,left);
  450. inc(p,left);
  451. reloadbuf;
  452. pbuf:=@buf[bufidx];
  453. if bufsize=0 then
  454. exit;
  455. until false;
  456. move(pbuf^,p^,len);
  457. inc(bufidx,len);
  458. end;
  459. procedure tppufile.skipdata(len:integer);
  460. var
  461. left : integer;
  462. begin
  463. while len>0 do
  464. begin
  465. left:=bufsize-bufidx;
  466. if len>left then
  467. begin
  468. dec(len,left);
  469. reloadbuf;
  470. if bufsize=0 then
  471. exit;
  472. end
  473. else
  474. begin
  475. inc(bufidx,len);
  476. exit;
  477. end;
  478. end;
  479. end;
  480. function tppufile.readentry:byte;
  481. begin
  482. if entryidx<entry.size then
  483. skipdata(entry.size-entryidx);
  484. readdata(entry,sizeof(tppuentry));
  485. if change_endian then
  486. entry.size:=swapendian(entry.size);
  487. entrystart:=bufstart+bufidx;
  488. entryidx:=0;
  489. if not(entry.id in [mainentryid,subentryid]) then
  490. begin
  491. readentry:=iberror;
  492. error:=true;
  493. exit;
  494. end;
  495. readentry:=entry.nr;
  496. end;
  497. function tppufile.endofentry:boolean;
  498. begin
  499. endofentry:=(entryidx>=entry.size);
  500. end;
  501. function tppufile.entrysize:longint;
  502. begin
  503. entrysize:=entry.size;
  504. end;
  505. procedure tppufile.getdatabuf(var b;len:integer;var res:integer);
  506. begin
  507. if entryidx+len>entry.size then
  508. res:=entry.size-entryidx
  509. else
  510. res:=len;
  511. readdata(b,res);
  512. inc(entryidx,res);
  513. end;
  514. procedure tppufile.getdata(var b;len:integer);
  515. begin
  516. if entryidx+len>entry.size then
  517. begin
  518. error:=true;
  519. exit;
  520. end;
  521. readdata(b,len);
  522. inc(entryidx,len);
  523. end;
  524. function tppufile.getbyte:byte;
  525. begin
  526. if entryidx+1>entry.size then
  527. begin
  528. error:=true;
  529. result:=0;
  530. exit;
  531. end;
  532. if bufsize-bufidx>=1 then
  533. begin
  534. result:=pbyte(@buf[bufidx])^;
  535. inc(bufidx);
  536. end
  537. else
  538. readdata(result,1);
  539. inc(entryidx);
  540. end;
  541. function tppufile.getword:word;
  542. begin
  543. if entryidx+2>entry.size then
  544. begin
  545. error:=true;
  546. result:=0;
  547. exit;
  548. end;
  549. if bufsize-bufidx>=sizeof(word) then
  550. begin
  551. result:=Unaligned(pword(@buf[bufidx])^);
  552. inc(bufidx,sizeof(word));
  553. end
  554. else
  555. readdata(result,sizeof(word));
  556. if change_endian then
  557. result:=swapendian(result);
  558. inc(entryidx,2);
  559. end;
  560. function tppufile.getlongint:longint;
  561. begin
  562. if entryidx+4>entry.size then
  563. begin
  564. error:=true;
  565. result:=0;
  566. exit;
  567. end;
  568. if bufsize-bufidx>=sizeof(longint) then
  569. begin
  570. result:=Unaligned(plongint(@buf[bufidx])^);
  571. inc(bufidx,sizeof(longint));
  572. end
  573. else
  574. readdata(result,sizeof(longint));
  575. if change_endian then
  576. result:=swapendian(result);
  577. inc(entryidx,4);
  578. end;
  579. function tppufile.getdword:dword;
  580. begin
  581. if entryidx+4>entry.size then
  582. begin
  583. error:=true;
  584. result:=0;
  585. exit;
  586. end;
  587. if bufsize-bufidx>=sizeof(dword) then
  588. begin
  589. result:=Unaligned(plongint(@buf[bufidx])^);
  590. inc(bufidx,sizeof(longint));
  591. end
  592. else
  593. readdata(result,sizeof(dword));
  594. if change_endian then
  595. result:=swapendian(result);
  596. inc(entryidx,4);
  597. end;
  598. function tppufile.getint64:int64;
  599. begin
  600. if entryidx+8>entry.size then
  601. begin
  602. error:=true;
  603. result:=0;
  604. exit;
  605. end;
  606. if bufsize-bufidx>=sizeof(int64) then
  607. begin
  608. result:=Unaligned(pint64(@buf[bufidx])^);
  609. inc(bufidx,sizeof(int64));
  610. end
  611. else
  612. readdata(result,sizeof(int64));
  613. if change_endian then
  614. result:=swapendian(result);
  615. inc(entryidx,8);
  616. end;
  617. function tppufile.getqword:qword;
  618. begin
  619. if entryidx+8>entry.size then
  620. begin
  621. error:=true;
  622. result:=0;
  623. exit;
  624. end;
  625. if bufsize-bufidx>=sizeof(qword) then
  626. begin
  627. result:=Unaligned(pqword(@buf[bufidx])^);
  628. inc(bufidx,sizeof(qword));
  629. end
  630. else
  631. readdata(result,sizeof(qword));
  632. if change_endian then
  633. result:=swapendian(result);
  634. inc(entryidx,8);
  635. end;
  636. function tppufile.getaint:aint;
  637. begin
  638. {$ifdef cpu64bitalu}
  639. result:=getint64;
  640. {$else cpu64bitalu}
  641. result:=getlongint;
  642. {$endif cpu64bitalu}
  643. end;
  644. function tppufile.getasizeint:asizeint;
  645. begin
  646. {$ifdef cpu64bitaddr}
  647. result:=getint64;
  648. {$else cpu64bitaddr}
  649. result:=getlongint;
  650. {$endif cpu32bitaddr}
  651. end;
  652. function tppufile.getaword:aword;
  653. begin
  654. {$ifdef cpu64bitalu}
  655. result:=getqword;
  656. {$else cpu64bitalu}
  657. result:=getdword;
  658. {$endif cpu64bitalu}
  659. end;
  660. function tppufile.getreal:ppureal;
  661. var
  662. d : ppureal;
  663. hd : double;
  664. begin
  665. if target_info.system=system_x86_64_win64 then
  666. begin
  667. if entryidx+sizeof(hd)>entry.size then
  668. begin
  669. error:=true;
  670. getreal:=0;
  671. exit;
  672. end;
  673. readdata(hd,sizeof(hd));
  674. if change_endian then
  675. getreal:=swapendian(qword(hd))
  676. else
  677. getreal:=hd;
  678. inc(entryidx,sizeof(hd));
  679. end
  680. else
  681. begin
  682. if entryidx+sizeof(ppureal)>entry.size then
  683. begin
  684. error:=true;
  685. getreal:=0;
  686. exit;
  687. end;
  688. readdata(d,sizeof(ppureal));
  689. if change_endian then
  690. getreal:=swapendian_ppureal(d)
  691. else
  692. getreal:=d;
  693. inc(entryidx,sizeof(ppureal));
  694. end;
  695. end;
  696. function tppufile.getstring:string;
  697. begin
  698. result[0]:=chr(getbyte);
  699. if entryidx+length(result)>entry.size then
  700. begin
  701. error:=true;
  702. exit;
  703. end;
  704. ReadData(result[1],length(result));
  705. inc(entryidx,length(result));
  706. end;
  707. procedure tppufile.getsmallset(var b);
  708. var
  709. i : longint;
  710. begin
  711. getdata(b,4);
  712. if change_endian then
  713. for i:=0 to 3 do
  714. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  715. end;
  716. procedure tppufile.getnormalset(var b);
  717. var
  718. i : longint;
  719. begin
  720. getdata(b,32);
  721. if change_endian then
  722. for i:=0 to 31 do
  723. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  724. end;
  725. function tppufile.skipuntilentry(untilb:byte):boolean;
  726. var
  727. b : byte;
  728. begin
  729. repeat
  730. b:=readentry;
  731. until (b in [ibend,iberror]) or ((b=untilb) and (entry.id=mainentryid));
  732. skipuntilentry:=(b=untilb);
  733. end;
  734. {*****************************************************************************
  735. TPPUFile Writing
  736. *****************************************************************************}
  737. function tppufile.createfile:boolean;
  738. begin
  739. createfile:=false;
  740. {$ifdef INTFPPU}
  741. if crc_only then
  742. begin
  743. fname:=fname+'.intf';
  744. crc_only:=false;
  745. end;
  746. {$endif}
  747. if not crc_only then
  748. begin
  749. assign(f,fname);
  750. {$ifdef MACOS}
  751. {FPas is FreePascal's creator code on MacOS. See systems/mac_crea.txt}
  752. SetDefaultMacOSCreator('FPas');
  753. SetDefaultMacOSFiletype('FPPU');
  754. {$endif}
  755. {$I-}
  756. rewrite(f,1);
  757. {$I+}
  758. {$ifdef MACOS}
  759. SetDefaultMacOSCreator('MPS ');
  760. SetDefaultMacOSFiletype('TEXT');
  761. {$endif}
  762. if ioresult<>0 then
  763. exit;
  764. Mode:=2;
  765. {write header for sure}
  766. blockwrite(f,header,sizeof(tppuheader));
  767. end;
  768. bufsize:=ppubufsize;
  769. bufstart:=sizeof(tppuheader);
  770. bufidx:=0;
  771. {reset}
  772. crc:=0;
  773. interface_crc:=0;
  774. indirect_crc:=0;
  775. do_interface_crc:=true;
  776. do_indirect_crc:=false;
  777. Error:=false;
  778. do_crc:=true;
  779. size:=0;
  780. entrytyp:=mainentryid;
  781. {start}
  782. NewEntry;
  783. createfile:=true;
  784. end;
  785. procedure tppufile.writeheader;
  786. var
  787. opos : integer;
  788. begin
  789. if crc_only then
  790. exit;
  791. { flush buffer }
  792. writebuf;
  793. { update size (w/o header!) in the header }
  794. header.size:=bufstart-sizeof(tppuheader);
  795. { set the endian flag }
  796. {$ifndef FPC_BIG_ENDIAN}
  797. header.flags := header.flags or uf_little_endian;
  798. {$else not FPC_BIG_ENDIAN}
  799. header.flags := header.flags or uf_big_endian;
  800. { Now swap the header in the correct endian (always little endian) }
  801. header.compiler := swapendian(header.compiler);
  802. header.cpu := swapendian(header.cpu);
  803. header.target := swapendian(header.target);
  804. header.flags := swapendian(header.flags);
  805. header.size := swapendian(header.size);
  806. header.checksum := swapendian(header.checksum);
  807. header.interface_checksum := swapendian(header.interface_checksum);
  808. header.indirect_checksum := swapendian(header.indirect_checksum);
  809. header.deflistsize:=swapendian(header.deflistsize);
  810. header.symlistsize:=swapendian(header.symlistsize);
  811. {$endif not FPC_BIG_ENDIAN}
  812. { write header and restore filepos after it }
  813. opos:=filepos(f);
  814. seek(f,0);
  815. blockwrite(f,header,sizeof(tppuheader));
  816. seek(f,opos);
  817. end;
  818. procedure tppufile.writebuf;
  819. begin
  820. if not crc_only and
  821. (bufidx <> 0) then
  822. blockwrite(f,buf^,bufidx);
  823. inc(bufstart,bufidx);
  824. bufidx:=0;
  825. end;
  826. procedure tppufile.writedata(const b;len:integer);
  827. var
  828. p : pchar;
  829. left,
  830. idx : integer;
  831. begin
  832. if crc_only then
  833. exit;
  834. p:=pchar(@b);
  835. idx:=0;
  836. while len>0 do
  837. begin
  838. left:=bufsize-bufidx;
  839. if len>left then
  840. begin
  841. move(p[idx],buf[bufidx],left);
  842. dec(len,left);
  843. inc(idx,left);
  844. inc(bufidx,left);
  845. writebuf;
  846. end
  847. else
  848. begin
  849. move(p[idx],buf[bufidx],len);
  850. inc(bufidx,len);
  851. exit;
  852. end;
  853. end;
  854. end;
  855. procedure tppufile.NewEntry;
  856. begin
  857. with entry do
  858. begin
  859. id:=entrytyp;
  860. nr:=ibend;
  861. size:=0;
  862. end;
  863. {Reset Entry State}
  864. entryidx:=0;
  865. entrybufstart:=bufstart;
  866. entrystart:=bufstart+bufidx;
  867. {Alloc in buffer}
  868. writedata(entry,sizeof(tppuentry));
  869. end;
  870. procedure tppufile.writeentry(ibnr:byte);
  871. var
  872. opos : integer;
  873. begin
  874. {create entry}
  875. entry.id:=entrytyp;
  876. entry.nr:=ibnr;
  877. entry.size:=entryidx;
  878. {it's already been sent to disk ?}
  879. if entrybufstart<>bufstart then
  880. begin
  881. if not crc_only then
  882. begin
  883. {flush to be sure}
  884. WriteBuf;
  885. {write entry}
  886. opos:=filepos(f);
  887. seek(f,entrystart);
  888. blockwrite(f,entry,sizeof(tppuentry));
  889. seek(f,opos);
  890. end;
  891. entrybufstart:=bufstart;
  892. end
  893. else
  894. move(entry,buf[entrystart-bufstart],sizeof(entry));
  895. {Add New Entry, which is ibend by default}
  896. entrystart:=bufstart+bufidx; {next entry position}
  897. NewEntry;
  898. end;
  899. procedure tppufile.putdata(const b;len:integer);
  900. begin
  901. if do_crc then
  902. begin
  903. crc:=UpdateCrc32(crc,b,len);
  904. {$ifdef Test_Double_checksum}
  905. if crc_only then
  906. begin
  907. crc_test2^[crc_index2]:=crc;
  908. {$ifdef Test_Double_checksum_write}
  909. Writeln(CRCFile,crc);
  910. {$endif Test_Double_checksum_write}
  911. if crc_index2<crc_array_size then
  912. inc(crc_index2);
  913. end
  914. else
  915. begin
  916. if (crcindex2<crc_array_size) and (crcindex2<crc_index2) and
  917. (crc_test2^[crcindex2]<>crc) then
  918. Do_comment(V_Note,'impl CRC changed');
  919. {$ifdef Test_Double_checksum_write}
  920. Writeln(CRCFile,crc);
  921. {$endif Test_Double_checksum_write}
  922. inc(crcindex2);
  923. end;
  924. {$endif def Test_Double_checksum}
  925. if do_interface_crc then
  926. begin
  927. interface_crc:=UpdateCrc32(interface_crc,b,len);
  928. {$ifdef Test_Double_checksum}
  929. if crc_only then
  930. begin
  931. crc_test^[crc_index]:=interface_crc;
  932. {$ifdef Test_Double_checksum_write}
  933. Writeln(CRCFile,interface_crc);
  934. {$endif Test_Double_checksum_write}
  935. if crc_index<crc_array_size then
  936. inc(crc_index);
  937. end
  938. else
  939. begin
  940. if (crcindex<crc_array_size) and (crcindex<crc_index) and
  941. (crc_test^[crcindex]<>interface_crc) then
  942. Do_comment(V_Warning,'CRC changed');
  943. {$ifdef Test_Double_checksum_write}
  944. Writeln(CRCFile,interface_crc);
  945. {$endif Test_Double_checksum_write}
  946. inc(crcindex);
  947. end;
  948. {$endif def Test_Double_checksum}
  949. { indirect crc must only be calculated for the interface; changes
  950. to a class in the implementation cannot require another unit to
  951. be recompiled }
  952. if do_indirect_crc then
  953. indirect_crc:=UpdateCrc32(indirect_crc,b,len);
  954. end;
  955. end;
  956. if not crc_only then
  957. writedata(b,len);
  958. inc(entryidx,len);
  959. end;
  960. procedure tppufile.putbyte(b:byte);
  961. begin
  962. putdata(b,1);
  963. end;
  964. procedure tppufile.putword(w:word);
  965. begin
  966. putdata(w,2);
  967. end;
  968. procedure tppufile.putdword(w:dword);
  969. begin
  970. putdata(w,4);
  971. end;
  972. procedure tppufile.putlongint(l:longint);
  973. begin
  974. putdata(l,4);
  975. end;
  976. procedure tppufile.putint64(i:int64);
  977. begin
  978. putdata(i,8);
  979. end;
  980. procedure tppufile.putqword(q:qword);
  981. begin
  982. putdata(q,sizeof(qword));
  983. end;
  984. procedure tppufile.putaint(i:aint);
  985. begin
  986. putdata(i,sizeof(aint));
  987. end;
  988. procedure tppufile.putaword(i:aword);
  989. begin
  990. putdata(i,sizeof(aword));
  991. end;
  992. procedure tppufile.putreal(d:ppureal);
  993. var
  994. hd : double;
  995. begin
  996. if target_info.system=system_x86_64_win64 then
  997. begin
  998. hd:=d;
  999. putdata(hd,sizeof(hd));
  1000. end
  1001. else
  1002. putdata(d,sizeof(ppureal));
  1003. end;
  1004. procedure tppufile.putstring(const s:string);
  1005. begin
  1006. putdata(s,length(s)+1);
  1007. end;
  1008. procedure tppufile.putsmallset(const b);
  1009. var
  1010. l : longint;
  1011. begin
  1012. l:=longint(b);
  1013. putlongint(l);
  1014. end;
  1015. procedure tppufile.putnormalset(const b);
  1016. type
  1017. SetLongintArray = Array [0..7] of longint;
  1018. begin
  1019. putdata(b,32);
  1020. end;
  1021. procedure tppufile.tempclose;
  1022. begin
  1023. if not closed then
  1024. begin
  1025. closepos:=filepos(f);
  1026. {$I-}
  1027. system.close(f);
  1028. {$I+}
  1029. if ioresult<>0 then;
  1030. closed:=true;
  1031. tempclosed:=true;
  1032. end;
  1033. end;
  1034. function tppufile.tempopen:boolean;
  1035. var
  1036. ofm : byte;
  1037. begin
  1038. tempopen:=false;
  1039. if not closed or not tempclosed then
  1040. exit;
  1041. ofm:=filemode;
  1042. filemode:=0;
  1043. {$I-}
  1044. reset(f,1);
  1045. {$I+}
  1046. filemode:=ofm;
  1047. if ioresult<>0 then
  1048. exit;
  1049. closed:=false;
  1050. tempclosed:=false;
  1051. { restore state }
  1052. seek(f,closepos);
  1053. tempopen:=true;
  1054. end;
  1055. end.