ppu.pas 26 KB

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