ppu.pas 27 KB

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