ppu.pas 26 KB

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