ppu.pas 26 KB

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