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 = 118;
  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. var
  689. s : string;
  690. begin
  691. s[0]:=chr(getbyte);
  692. if entryidx+length(s)>entry.size then
  693. begin
  694. error:=true;
  695. exit;
  696. end;
  697. ReadData(s[1],length(s));
  698. getstring:=s;
  699. inc(entryidx,length(s));
  700. end;
  701. procedure tppufile.getsmallset(var b);
  702. var
  703. i : longint;
  704. begin
  705. getdata(b,4);
  706. if change_endian then
  707. for i:=0 to 3 do
  708. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  709. end;
  710. procedure tppufile.getnormalset(var b);
  711. var
  712. i : longint;
  713. begin
  714. getdata(b,32);
  715. if change_endian then
  716. for i:=0 to 31 do
  717. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  718. end;
  719. function tppufile.skipuntilentry(untilb:byte):boolean;
  720. var
  721. b : byte;
  722. begin
  723. repeat
  724. b:=readentry;
  725. until (b in [ibend,iberror]) or ((b=untilb) and (entry.id=mainentryid));
  726. skipuntilentry:=(b=untilb);
  727. end;
  728. {*****************************************************************************
  729. TPPUFile Writing
  730. *****************************************************************************}
  731. function tppufile.createfile:boolean;
  732. begin
  733. createfile:=false;
  734. {$ifdef INTFPPU}
  735. if crc_only then
  736. begin
  737. fname:=fname+'.intf';
  738. crc_only:=false;
  739. end;
  740. {$endif}
  741. if not crc_only then
  742. begin
  743. assign(f,fname);
  744. {$ifdef MACOS}
  745. {FPas is FreePascal's creator code on MacOS. See systems/mac_crea.txt}
  746. SetDefaultMacOSCreator('FPas');
  747. SetDefaultMacOSFiletype('FPPU');
  748. {$endif}
  749. {$I-}
  750. rewrite(f,1);
  751. {$I+}
  752. {$ifdef MACOS}
  753. SetDefaultMacOSCreator('MPS ');
  754. SetDefaultMacOSFiletype('TEXT');
  755. {$endif}
  756. if ioresult<>0 then
  757. exit;
  758. Mode:=2;
  759. {write header for sure}
  760. blockwrite(f,header,sizeof(tppuheader));
  761. end;
  762. bufsize:=ppubufsize;
  763. bufstart:=sizeof(tppuheader);
  764. bufidx:=0;
  765. {reset}
  766. crc:=0;
  767. interface_crc:=0;
  768. indirect_crc:=0;
  769. do_interface_crc:=true;
  770. do_indirect_crc:=false;
  771. Error:=false;
  772. do_crc:=true;
  773. size:=0;
  774. entrytyp:=mainentryid;
  775. {start}
  776. NewEntry;
  777. createfile:=true;
  778. end;
  779. procedure tppufile.writeheader;
  780. var
  781. opos : integer;
  782. begin
  783. if crc_only then
  784. exit;
  785. { flush buffer }
  786. writebuf;
  787. { update size (w/o header!) in the header }
  788. header.size:=bufstart-sizeof(tppuheader);
  789. { set the endian flag }
  790. {$ifndef FPC_BIG_ENDIAN}
  791. header.flags := header.flags or uf_little_endian;
  792. {$else not FPC_BIG_ENDIAN}
  793. header.flags := header.flags or uf_big_endian;
  794. { Now swap the header in the correct endian (always little endian) }
  795. header.compiler := swapendian(header.compiler);
  796. header.cpu := swapendian(header.cpu);
  797. header.target := swapendian(header.target);
  798. header.flags := swapendian(header.flags);
  799. header.size := swapendian(header.size);
  800. header.checksum := swapendian(header.checksum);
  801. header.interface_checksum := swapendian(header.interface_checksum);
  802. header.indirect_checksum := swapendian(header.indirect_checksum);
  803. header.deflistsize:=swapendian(header.deflistsize);
  804. header.symlistsize:=swapendian(header.symlistsize);
  805. {$endif not FPC_BIG_ENDIAN}
  806. { write header and restore filepos after it }
  807. opos:=filepos(f);
  808. seek(f,0);
  809. blockwrite(f,header,sizeof(tppuheader));
  810. seek(f,opos);
  811. end;
  812. procedure tppufile.writebuf;
  813. begin
  814. if not crc_only and
  815. (bufidx <> 0) then
  816. blockwrite(f,buf^,bufidx);
  817. inc(bufstart,bufidx);
  818. bufidx:=0;
  819. end;
  820. procedure tppufile.writedata(const b;len:integer);
  821. var
  822. p : pchar;
  823. left,
  824. idx : integer;
  825. begin
  826. if crc_only then
  827. exit;
  828. p:=pchar(@b);
  829. idx:=0;
  830. while len>0 do
  831. begin
  832. left:=bufsize-bufidx;
  833. if len>left then
  834. begin
  835. move(p[idx],buf[bufidx],left);
  836. dec(len,left);
  837. inc(idx,left);
  838. inc(bufidx,left);
  839. writebuf;
  840. end
  841. else
  842. begin
  843. move(p[idx],buf[bufidx],len);
  844. inc(bufidx,len);
  845. exit;
  846. end;
  847. end;
  848. end;
  849. procedure tppufile.NewEntry;
  850. begin
  851. with entry do
  852. begin
  853. id:=entrytyp;
  854. nr:=ibend;
  855. size:=0;
  856. end;
  857. {Reset Entry State}
  858. entryidx:=0;
  859. entrybufstart:=bufstart;
  860. entrystart:=bufstart+bufidx;
  861. {Alloc in buffer}
  862. writedata(entry,sizeof(tppuentry));
  863. end;
  864. procedure tppufile.writeentry(ibnr:byte);
  865. var
  866. opos : integer;
  867. begin
  868. {create entry}
  869. entry.id:=entrytyp;
  870. entry.nr:=ibnr;
  871. entry.size:=entryidx;
  872. {it's already been sent to disk ?}
  873. if entrybufstart<>bufstart then
  874. begin
  875. if not crc_only then
  876. begin
  877. {flush to be sure}
  878. WriteBuf;
  879. {write entry}
  880. opos:=filepos(f);
  881. seek(f,entrystart);
  882. blockwrite(f,entry,sizeof(tppuentry));
  883. seek(f,opos);
  884. end;
  885. entrybufstart:=bufstart;
  886. end
  887. else
  888. move(entry,buf[entrystart-bufstart],sizeof(entry));
  889. {Add New Entry, which is ibend by default}
  890. entrystart:=bufstart+bufidx; {next entry position}
  891. NewEntry;
  892. end;
  893. procedure tppufile.putdata(const b;len:integer);
  894. begin
  895. if do_crc then
  896. begin
  897. crc:=UpdateCrc32(crc,b,len);
  898. {$ifdef Test_Double_checksum}
  899. if crc_only then
  900. begin
  901. crc_test2^[crc_index2]:=crc;
  902. {$ifdef Test_Double_checksum_write}
  903. Writeln(CRCFile,crc);
  904. {$endif Test_Double_checksum_write}
  905. if crc_index2<crc_array_size then
  906. inc(crc_index2);
  907. end
  908. else
  909. begin
  910. if (crcindex2<crc_array_size) and (crcindex2<crc_index2) and
  911. (crc_test2^[crcindex2]<>crc) then
  912. Do_comment(V_Note,'impl CRC changed');
  913. {$ifdef Test_Double_checksum_write}
  914. Writeln(CRCFile,crc);
  915. {$endif Test_Double_checksum_write}
  916. inc(crcindex2);
  917. end;
  918. {$endif def Test_Double_checksum}
  919. if do_interface_crc then
  920. begin
  921. interface_crc:=UpdateCrc32(interface_crc,b,len);
  922. {$ifdef Test_Double_checksum}
  923. if crc_only then
  924. begin
  925. crc_test^[crc_index]:=interface_crc;
  926. {$ifdef Test_Double_checksum_write}
  927. Writeln(CRCFile,interface_crc);
  928. {$endif Test_Double_checksum_write}
  929. if crc_index<crc_array_size then
  930. inc(crc_index);
  931. end
  932. else
  933. begin
  934. if (crcindex<crc_array_size) and (crcindex<crc_index) and
  935. (crc_test^[crcindex]<>interface_crc) then
  936. Do_comment(V_Warning,'CRC changed');
  937. {$ifdef Test_Double_checksum_write}
  938. Writeln(CRCFile,interface_crc);
  939. {$endif Test_Double_checksum_write}
  940. inc(crcindex);
  941. end;
  942. {$endif def Test_Double_checksum}
  943. { indirect crc must only be calculated for the interface; changes
  944. to a class in the implementation cannot require another unit to
  945. be recompiled }
  946. if do_indirect_crc then
  947. indirect_crc:=UpdateCrc32(indirect_crc,b,len);
  948. end;
  949. end;
  950. if not crc_only then
  951. writedata(b,len);
  952. inc(entryidx,len);
  953. end;
  954. procedure tppufile.putbyte(b:byte);
  955. begin
  956. putdata(b,1);
  957. end;
  958. procedure tppufile.putword(w:word);
  959. begin
  960. putdata(w,2);
  961. end;
  962. procedure tppufile.putdword(w:dword);
  963. begin
  964. putdata(w,4);
  965. end;
  966. procedure tppufile.putlongint(l:longint);
  967. begin
  968. putdata(l,4);
  969. end;
  970. procedure tppufile.putint64(i:int64);
  971. begin
  972. putdata(i,8);
  973. end;
  974. procedure tppufile.putqword(q:qword);
  975. begin
  976. putdata(q,sizeof(qword));
  977. end;
  978. procedure tppufile.putaint(i:aint);
  979. begin
  980. putdata(i,sizeof(aint));
  981. end;
  982. procedure tppufile.putaword(i:aword);
  983. begin
  984. putdata(i,sizeof(aword));
  985. end;
  986. procedure tppufile.putreal(d:ppureal);
  987. var
  988. hd : double;
  989. begin
  990. if target_info.system=system_x86_64_win64 then
  991. begin
  992. hd:=d;
  993. putdata(hd,sizeof(hd));
  994. end
  995. else
  996. putdata(d,sizeof(ppureal));
  997. end;
  998. procedure tppufile.putstring(const s:string);
  999. begin
  1000. putdata(s,length(s)+1);
  1001. end;
  1002. procedure tppufile.putsmallset(const b);
  1003. var
  1004. l : longint;
  1005. begin
  1006. l:=longint(b);
  1007. putlongint(l);
  1008. end;
  1009. procedure tppufile.putnormalset(const b);
  1010. type
  1011. SetLongintArray = Array [0..7] of longint;
  1012. begin
  1013. putdata(b,32);
  1014. end;
  1015. procedure tppufile.tempclose;
  1016. begin
  1017. if not closed then
  1018. begin
  1019. closepos:=filepos(f);
  1020. {$I-}
  1021. system.close(f);
  1022. {$I+}
  1023. if ioresult<>0 then;
  1024. closed:=true;
  1025. tempclosed:=true;
  1026. end;
  1027. end;
  1028. function tppufile.tempopen:boolean;
  1029. var
  1030. ofm : byte;
  1031. begin
  1032. tempopen:=false;
  1033. if not closed or not tempclosed then
  1034. exit;
  1035. ofm:=filemode;
  1036. filemode:=0;
  1037. {$I-}
  1038. reset(f,1);
  1039. {$I+}
  1040. filemode:=ofm;
  1041. if ioresult<>0 then
  1042. exit;
  1043. closed:=false;
  1044. tempclosed:=false;
  1045. { restore state }
  1046. seek(f,closepos);
  1047. tempopen:=true;
  1048. end;
  1049. end.