ppu.pas 26 KB

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