ppu.pas 22 KB

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