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