ppu.pas 26 KB

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