ppu.pas 25 KB

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