ppu.pas 25 KB

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