ppu.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Routines to read/write ppu files
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ppu;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. globtype;
  23. { Also write the ppu if only crc if done, this can be used with ppudump to
  24. see the differences between the intf and implementation }
  25. { define INTFPPU}
  26. {$ifdef Test_Double_checksum}
  27. var
  28. CRCFile : text;
  29. const
  30. CRC_array_Size = 200000;
  31. type
  32. tcrc_array = array[0..crc_array_size] of longint;
  33. pcrc_array = ^tcrc_array;
  34. {$endif Test_Double_checksum}
  35. const
  36. CurrentPPUVersion=47;
  37. { buffer sizes }
  38. maxentrysize = 1024;
  39. ppubufsize = 16384;
  40. {ppu entries}
  41. mainentryid = 1;
  42. subentryid = 2;
  43. {special}
  44. iberror = 0;
  45. ibstartdefs = 248;
  46. ibenddefs = 249;
  47. ibstartsyms = 250;
  48. ibendsyms = 251;
  49. ibendinterface = 252;
  50. ibendimplementation = 253;
  51. ibendbrowser = 254;
  52. ibend = 255;
  53. {general}
  54. ibmodulename = 1;
  55. ibsourcefiles = 2;
  56. ibloadunit = 3;
  57. ibinitunit = 4;
  58. iblinkunitofiles = 5;
  59. iblinkunitstaticlibs = 6;
  60. iblinkunitsharedlibs = 7;
  61. iblinkotherofiles = 8;
  62. iblinkotherstaticlibs = 9;
  63. iblinkothersharedlibs = 10;
  64. ibdbxcount = 11;
  65. ibsymref = 12;
  66. ibdefref = 13;
  67. ibendsymtablebrowser = 14;
  68. ibbeginsymtablebrowser = 15;
  69. {$IFDEF MACRO_DIFF_HINT}
  70. ibusedmacros = 16;
  71. {$ENDIF}
  72. ibderefdata = 17;
  73. ibexportedmacros = 18;
  74. {syms}
  75. ibtypesym = 20;
  76. ibprocsym = 21;
  77. ibglobalvarsym = 22;
  78. ibconstsym = 23;
  79. ibenumsym = 24;
  80. ibtypedconstsym = 25;
  81. ibabsolutevarsym = 26;
  82. ibpropertysym = 27;
  83. ibfieldvarsym = 28;
  84. ibunitsym = 29; { needed for browser }
  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. {$ifdef ansistring_bits}
  108. ibansistring16def = 58;
  109. ibansistring32def = 55;
  110. ibansistring64def = 59;
  111. {$else}
  112. ibansistringdef = 55;
  113. {$endif}
  114. ibwidestringdef = 56;
  115. ibvariantdef = 57;
  116. {implementation/objectdata}
  117. ibnodetree = 80;
  118. ibasmsymbols = 81;
  119. { unit flags }
  120. uf_init = $1;
  121. uf_finalize = $2;
  122. uf_big_endian = $4;
  123. uf_has_dbx = $8;
  124. uf_has_browser = $10;
  125. uf_in_library = $20; { is the file in another file than <ppufile>.* ? }
  126. uf_smart_linked = $40; { the ppu can be smartlinked }
  127. uf_static_linked = $80; { the ppu can be linked static }
  128. uf_shared_linked = $100; { the ppu can be linked shared }
  129. uf_local_browser = $200;
  130. uf_no_link = $400; { unit has no .o generated, but can still have
  131. external linking! }
  132. uf_has_resources = $800; { unit has resource section }
  133. uf_little_endian = $1000;
  134. uf_release = $2000; { unit was compiled with -Ur option }
  135. uf_threadvars = $4000; { unit has threadvars }
  136. uf_fpu_emulation = $8000; { this unit was compiled with fpu emulation on }
  137. uf_has_debuginfo = $10000; { this unit has debuginfo generated }
  138. uf_local_symtable = $20000; { this unit has a local symtable stored }
  139. uf_uses_variants = $40000; { this unit uses variants }
  140. type
  141. ppureal=extended;
  142. tppuerror=(ppuentrytoobig,ppuentryerror);
  143. tppuheader=record
  144. id : array[1..3] of char; { = 'PPU' }
  145. ver : array[1..3] of char;
  146. compiler : word;
  147. cpu : word;
  148. target : word;
  149. flags : longint;
  150. size : longint; { size of the ppufile without header }
  151. checksum : cardinal; { checksum for this ppufile }
  152. interface_checksum : cardinal;
  153. future : array[0..2] of longint;
  154. end;
  155. tppuentry=packed record
  156. size : longint;
  157. id : byte;
  158. nr : byte;
  159. end;
  160. tppufile=class
  161. private
  162. f : file;
  163. mode : byte; {0 - Closed, 1 - Reading, 2 - Writing}
  164. fname : string;
  165. fsize : integer;
  166. {$ifdef Test_Double_checksum}
  167. public
  168. crcindex,
  169. crc_index,
  170. crcindex2,
  171. crc_index2 : cardinal;
  172. crc_test,
  173. crc_test2 : pcrc_array;
  174. private
  175. {$endif def Test_Double_checksum}
  176. change_endian : boolean;
  177. buf : pchar;
  178. bufstart,
  179. bufsize,
  180. bufidx : integer;
  181. entrybufstart,
  182. entrystart,
  183. entryidx : integer;
  184. entry : tppuentry;
  185. closed,
  186. tempclosed : boolean;
  187. closepos : integer;
  188. public
  189. entrytyp : byte;
  190. header : tppuheader;
  191. size : integer;
  192. crc,
  193. interface_crc : cardinal;
  194. error,
  195. do_crc,
  196. do_interface_crc : boolean;
  197. crc_only : boolean; { used to calculate interface_crc before implementation }
  198. constructor Create(const fn:string);
  199. destructor Destroy;override;
  200. procedure flush;
  201. procedure closefile;
  202. function CheckPPUId:boolean;
  203. function GetPPUVersion:integer;
  204. procedure NewHeader;
  205. procedure NewEntry;
  206. {read}
  207. function openfile:boolean;
  208. procedure reloadbuf;
  209. procedure readdata(var b;len:integer);
  210. procedure skipdata(len:integer);
  211. function readentry:byte;
  212. function EndOfEntry:boolean;
  213. function entrysize:longint;
  214. procedure getdatabuf(var b;len:integer;var res:integer);
  215. procedure getdata(var b;len:integer);
  216. function getbyte:byte;
  217. function getword:word;
  218. function getlongint:longint;
  219. function getint64:int64;
  220. function getaint:aint;
  221. function getreal:ppureal;
  222. function getstring:string;
  223. procedure getnormalset(var b);
  224. procedure getsmallset(var b);
  225. function skipuntilentry(untilb:byte):boolean;
  226. {write}
  227. function createfile:boolean;
  228. procedure writeheader;
  229. procedure writebuf;
  230. procedure writedata(const b;len:integer);
  231. procedure writeentry(ibnr:byte);
  232. procedure putdata(const b;len:integer);
  233. procedure putbyte(b:byte);
  234. procedure putword(w:word);
  235. procedure putlongint(l:longint);
  236. procedure putint64(i:int64);
  237. procedure putaint(i:aint);
  238. procedure putreal(d:ppureal);
  239. procedure putstring(s:string);
  240. procedure putnormalset(const b);
  241. procedure putsmallset(const b);
  242. procedure tempclose;
  243. function tempopen:boolean;
  244. end;
  245. implementation
  246. uses
  247. {$ifdef Test_Double_checksum}
  248. comphook,
  249. {$endif def Test_Double_checksum}
  250. crc,
  251. cutils;
  252. {*****************************************************************************
  253. Endian Handling
  254. *****************************************************************************}
  255. Function SwapLong(x : longint): longint;
  256. var
  257. y : word;
  258. z : word;
  259. Begin
  260. y := x shr 16;
  261. y := word(longint(y) shl 8) or (y shr 8);
  262. z := x and $FFFF;
  263. z := word(longint(z) shl 8) or (z shr 8);
  264. SwapLong := (longint(z) shl 16) or longint(y);
  265. End;
  266. Function SwapWord(x : word): word;
  267. var
  268. z : byte;
  269. Begin
  270. z := x shr 8;
  271. x := x and $ff;
  272. x := word(x shl 8);
  273. SwapWord := x or z;
  274. End;
  275. {*****************************************************************************
  276. TPPUFile
  277. *****************************************************************************}
  278. constructor tppufile.Create(const fn:string);
  279. begin
  280. fname:=fn;
  281. change_endian:=false;
  282. crc_only:=false;
  283. Mode:=0;
  284. NewHeader;
  285. Error:=false;
  286. closed:=true;
  287. tempclosed:=false;
  288. getmem(buf,ppubufsize);
  289. end;
  290. destructor tppufile.destroy;
  291. begin
  292. closefile;
  293. if assigned(buf) then
  294. freemem(buf,ppubufsize);
  295. end;
  296. procedure tppufile.flush;
  297. begin
  298. if Mode=2 then
  299. writebuf;
  300. end;
  301. procedure tppufile.closefile;
  302. begin
  303. {$ifdef Test_Double_checksum}
  304. if mode=2 then
  305. begin
  306. if assigned(crc_test) then
  307. dispose(crc_test);
  308. if assigned(crc_test2) then
  309. dispose(crc_test2);
  310. end;
  311. {$endif Test_Double_checksum}
  312. if Mode<>0 then
  313. begin
  314. Flush;
  315. {$I-}
  316. system.close(f);
  317. {$I+}
  318. if ioresult<>0 then;
  319. Mode:=0;
  320. closed:=true;
  321. end;
  322. end;
  323. function tppufile.CheckPPUId:boolean;
  324. begin
  325. CheckPPUId:=((Header.Id[1]='P') and (Header.Id[2]='P') and (Header.Id[3]='U'));
  326. end;
  327. function tppufile.GetPPUVersion:integer;
  328. var
  329. l : integer;
  330. code : integer;
  331. begin
  332. Val(header.ver[1]+header.ver[2]+header.ver[3],l,code);
  333. if code=0 then
  334. GetPPUVersion:=l
  335. else
  336. GetPPUVersion:=0;
  337. end;
  338. procedure tppufile.NewHeader;
  339. var
  340. s : string;
  341. begin
  342. fillchar(header,sizeof(tppuheader),0);
  343. str(currentppuversion,s);
  344. while length(s)<3 do
  345. s:='0'+s;
  346. with header do
  347. begin
  348. Id[1]:='P';
  349. Id[2]:='P';
  350. Id[3]:='U';
  351. Ver[1]:=s[1];
  352. Ver[2]:=s[2];
  353. Ver[3]:=s[3];
  354. end;
  355. end;
  356. {*****************************************************************************
  357. TPPUFile Reading
  358. *****************************************************************************}
  359. function tppufile.openfile:boolean;
  360. var
  361. ofmode : byte;
  362. i : integer;
  363. begin
  364. openfile:=false;
  365. assign(f,fname);
  366. ofmode:=filemode;
  367. filemode:=$0;
  368. {$I-}
  369. reset(f,1);
  370. {$I+}
  371. filemode:=ofmode;
  372. if ioresult<>0 then
  373. exit;
  374. closed:=false;
  375. {read ppuheader}
  376. fsize:=filesize(f);
  377. if fsize<sizeof(tppuheader) then
  378. exit;
  379. blockread(f,header,sizeof(tppuheader),i);
  380. { The header is always stored in little endian order }
  381. { therefore swap if on a big endian machine }
  382. {$IFDEF ENDIAN_BIG}
  383. header.compiler := SwapWord(header.compiler);
  384. header.cpu := SwapWord(header.cpu);
  385. header.target := SwapWord(header.target);
  386. header.flags := SwapLong(header.flags);
  387. header.size := SwapLong(header.size);
  388. header.checksum := cardinal(SwapLong(longint(header.checksum)));
  389. header.interface_checksum := cardinal(SwapLong(longint(header.interface_checksum)));
  390. {$ENDIF}
  391. { the PPU DATA is stored in native order }
  392. if (header.flags and uf_big_endian) = uf_big_endian then
  393. Begin
  394. {$IFDEF ENDIAN_LITTLE}
  395. change_endian := TRUE;
  396. {$ELSE}
  397. change_endian := FALSE;
  398. {$ENDIF}
  399. End
  400. else if (header.flags and uf_little_endian) = uf_little_endian then
  401. Begin
  402. {$IFDEF ENDIAN_BIG}
  403. change_endian := TRUE;
  404. {$ELSE}
  405. change_endian := FALSE;
  406. {$ENDIF}
  407. End;
  408. {reset buffer}
  409. bufstart:=i;
  410. bufsize:=0;
  411. bufidx:=0;
  412. Mode:=1;
  413. FillChar(entry,sizeof(tppuentry),0);
  414. entryidx:=0;
  415. entrystart:=0;
  416. entrybufstart:=0;
  417. Error:=false;
  418. openfile:=true;
  419. end;
  420. procedure tppufile.reloadbuf;
  421. begin
  422. inc(bufstart,bufsize);
  423. blockread(f,buf^,ppubufsize,bufsize);
  424. bufidx:=0;
  425. end;
  426. procedure tppufile.readdata(var b;len:integer);
  427. var
  428. p : pchar;
  429. left,
  430. idx : integer;
  431. begin
  432. p:=pchar(@b);
  433. idx:=0;
  434. while len>0 do
  435. begin
  436. left:=bufsize-bufidx;
  437. if len>left then
  438. begin
  439. move(buf[bufidx],p[idx],left);
  440. dec(len,left);
  441. inc(idx,left);
  442. reloadbuf;
  443. if bufsize=0 then
  444. exit;
  445. end
  446. else
  447. begin
  448. move(buf[bufidx],p[idx],len);
  449. inc(bufidx,len);
  450. exit;
  451. end;
  452. end;
  453. end;
  454. procedure tppufile.skipdata(len:integer);
  455. var
  456. left : integer;
  457. begin
  458. while len>0 do
  459. begin
  460. left:=bufsize-bufidx;
  461. if len>left then
  462. begin
  463. dec(len,left);
  464. reloadbuf;
  465. if bufsize=0 then
  466. exit;
  467. end
  468. else
  469. begin
  470. inc(bufidx,len);
  471. exit;
  472. end;
  473. end;
  474. end;
  475. function tppufile.readentry:byte;
  476. begin
  477. if entryidx<entry.size then
  478. skipdata(entry.size-entryidx);
  479. readdata(entry,sizeof(tppuentry));
  480. if change_endian then
  481. entry.size:=swaplong(entry.size);
  482. entrystart:=bufstart+bufidx;
  483. entryidx:=0;
  484. if not(entry.id in [mainentryid,subentryid]) then
  485. begin
  486. readentry:=iberror;
  487. error:=true;
  488. exit;
  489. end;
  490. readentry:=entry.nr;
  491. end;
  492. function tppufile.endofentry:boolean;
  493. begin
  494. endofentry:=(entryidx>=entry.size);
  495. end;
  496. function tppufile.entrysize:longint;
  497. begin
  498. entrysize:=entry.size;
  499. end;
  500. procedure tppufile.getdatabuf(var b;len:integer;var res:integer);
  501. begin
  502. if entryidx+len>entry.size then
  503. res:=entry.size-entryidx
  504. else
  505. res:=len;
  506. readdata(b,res);
  507. inc(entryidx,res);
  508. end;
  509. procedure tppufile.getdata(var b;len:integer);
  510. begin
  511. if entryidx+len>entry.size then
  512. begin
  513. error:=true;
  514. exit;
  515. end;
  516. readdata(b,len);
  517. inc(entryidx,len);
  518. end;
  519. function tppufile.getbyte:byte;
  520. var
  521. b : byte;
  522. begin
  523. if entryidx+1>entry.size then
  524. begin
  525. error:=true;
  526. getbyte:=0;
  527. exit;
  528. end;
  529. readdata(b,1);
  530. getbyte:=b;
  531. inc(entryidx);
  532. end;
  533. function tppufile.getword:word;
  534. var
  535. w : word;
  536. begin
  537. if entryidx+2>entry.size then
  538. begin
  539. error:=true;
  540. getword:=0;
  541. exit;
  542. end;
  543. readdata(w,2);
  544. if change_endian then
  545. getword:=swapword(w)
  546. else
  547. getword:=w;
  548. inc(entryidx,2);
  549. end;
  550. function tppufile.getlongint:longint;
  551. var
  552. l : longint;
  553. begin
  554. if entryidx+4>entry.size then
  555. begin
  556. error:=true;
  557. getlongint:=0;
  558. exit;
  559. end;
  560. readdata(l,4);
  561. if change_endian then
  562. getlongint:=swaplong(l)
  563. else
  564. getlongint:=l;
  565. inc(entryidx,4);
  566. end;
  567. function tppufile.getint64:int64;
  568. var
  569. i : int64;
  570. begin
  571. if entryidx+8>entry.size then
  572. begin
  573. error:=true;
  574. result:=0;
  575. exit;
  576. end;
  577. readdata(i,8);
  578. if change_endian then
  579. result:=swapint64(i)
  580. else
  581. result:=i;
  582. inc(entryidx,8);
  583. end;
  584. function tppufile.getaint:aint;
  585. begin
  586. {$ifdef cpu64bit}
  587. result:=getint64;
  588. {$else cpu64bit}
  589. result:=getlongint;
  590. {$endif cpu64bit}
  591. end;
  592. function tppufile.getreal:ppureal;
  593. var
  594. d : ppureal;
  595. begin
  596. if entryidx+sizeof(ppureal)>entry.size then
  597. begin
  598. error:=true;
  599. getreal:=0;
  600. exit;
  601. end;
  602. readdata(d,sizeof(ppureal));
  603. getreal:=d;
  604. inc(entryidx,sizeof(ppureal));
  605. end;
  606. function tppufile.getstring:string;
  607. var
  608. s : string;
  609. begin
  610. s[0]:=chr(getbyte);
  611. if entryidx+length(s)>entry.size then
  612. begin
  613. error:=true;
  614. exit;
  615. end;
  616. ReadData(s[1],length(s));
  617. getstring:=s;
  618. inc(entryidx,length(s));
  619. end;
  620. procedure tppufile.getsmallset(var b);
  621. var
  622. l : longint;
  623. begin
  624. l:=getlongint;
  625. longint(b):=l;
  626. end;
  627. procedure tppufile.getnormalset(var b);
  628. type
  629. SetLongintArray = Array [0..7] of longint;
  630. var
  631. i : longint;
  632. begin
  633. if change_endian then
  634. begin
  635. for i:=0 to 7 do
  636. SetLongintArray(b)[i]:=getlongint;
  637. end
  638. else
  639. getdata(b,32);
  640. end;
  641. function tppufile.skipuntilentry(untilb:byte):boolean;
  642. var
  643. b : byte;
  644. begin
  645. repeat
  646. b:=readentry;
  647. until (b in [ibend,iberror]) or ((b=untilb) and (entry.id=mainentryid));
  648. skipuntilentry:=(b=untilb);
  649. end;
  650. {*****************************************************************************
  651. TPPUFile Writing
  652. *****************************************************************************}
  653. function tppufile.createfile:boolean;
  654. begin
  655. createfile:=false;
  656. {$ifdef INTFPPU}
  657. if crc_only then
  658. begin
  659. fname:=fname+'.intf';
  660. crc_only:=false;
  661. end;
  662. {$endif}
  663. if not crc_only then
  664. begin
  665. assign(f,fname);
  666. {$I-}
  667. rewrite(f,1);
  668. {$I+}
  669. if ioresult<>0 then
  670. exit;
  671. Mode:=2;
  672. {write header for sure}
  673. blockwrite(f,header,sizeof(tppuheader));
  674. end;
  675. bufsize:=ppubufsize;
  676. bufstart:=sizeof(tppuheader);
  677. bufidx:=0;
  678. {reset}
  679. crc:=cardinal($ffffffff);
  680. interface_crc:=cardinal($ffffffff);
  681. do_interface_crc:=true;
  682. Error:=false;
  683. do_crc:=true;
  684. size:=0;
  685. entrytyp:=mainentryid;
  686. {start}
  687. NewEntry;
  688. createfile:=true;
  689. end;
  690. procedure tppufile.writeheader;
  691. var
  692. opos : integer;
  693. begin
  694. if crc_only then
  695. exit;
  696. { flush buffer }
  697. writebuf;
  698. { update size (w/o header!) in the header }
  699. header.size:=bufstart-sizeof(tppuheader);
  700. { set the endian flag }
  701. {$ifndef FPC_BIG_ENDIAN}
  702. header.flags := header.flags or uf_little_endian;
  703. {$else not FPC_BIG_ENDIAN}
  704. header.flags := header.flags or uf_big_endian;
  705. { Now swap the header in the correct endian (always little endian) }
  706. header.compiler := SwapWord(header.compiler);
  707. header.cpu := SwapWord(header.cpu);
  708. header.target := SwapWord(header.target);
  709. header.flags := SwapLong(header.flags);
  710. header.size := SwapLong(header.size);
  711. header.checksum := cardinal(SwapLong(longint(header.checksum)));
  712. header.interface_checksum := cardinal(SwapLong(longint(header.interface_checksum)));
  713. {$endif not FPC_BIG_ENDIAN}
  714. { write header and restore filepos after it }
  715. opos:=filepos(f);
  716. seek(f,0);
  717. blockwrite(f,header,sizeof(tppuheader));
  718. seek(f,opos);
  719. end;
  720. procedure tppufile.writebuf;
  721. begin
  722. if not crc_only then
  723. blockwrite(f,buf^,bufidx);
  724. inc(bufstart,bufidx);
  725. bufidx:=0;
  726. end;
  727. procedure tppufile.writedata(const b;len:integer);
  728. var
  729. p : pchar;
  730. left,
  731. idx : integer;
  732. begin
  733. if crc_only then
  734. exit;
  735. p:=pchar(@b);
  736. idx:=0;
  737. while len>0 do
  738. begin
  739. left:=bufsize-bufidx;
  740. if len>left then
  741. begin
  742. move(p[idx],buf[bufidx],left);
  743. dec(len,left);
  744. inc(idx,left);
  745. inc(bufidx,left);
  746. writebuf;
  747. end
  748. else
  749. begin
  750. move(p[idx],buf[bufidx],len);
  751. inc(bufidx,len);
  752. exit;
  753. end;
  754. end;
  755. end;
  756. procedure tppufile.NewEntry;
  757. begin
  758. with entry do
  759. begin
  760. id:=entrytyp;
  761. nr:=ibend;
  762. size:=0;
  763. end;
  764. {Reset Entry State}
  765. entryidx:=0;
  766. entrybufstart:=bufstart;
  767. entrystart:=bufstart+bufidx;
  768. {Alloc in buffer}
  769. writedata(entry,sizeof(tppuentry));
  770. end;
  771. procedure tppufile.writeentry(ibnr:byte);
  772. var
  773. opos : integer;
  774. begin
  775. {create entry}
  776. entry.id:=entrytyp;
  777. entry.nr:=ibnr;
  778. entry.size:=entryidx;
  779. {it's already been sent to disk ?}
  780. if entrybufstart<>bufstart then
  781. begin
  782. if not crc_only then
  783. begin
  784. {flush to be sure}
  785. WriteBuf;
  786. {write entry}
  787. opos:=filepos(f);
  788. seek(f,entrystart);
  789. blockwrite(f,entry,sizeof(tppuentry));
  790. seek(f,opos);
  791. end;
  792. entrybufstart:=bufstart;
  793. end
  794. else
  795. move(entry,buf[entrystart-bufstart],sizeof(entry));
  796. {Add New Entry, which is ibend by default}
  797. entrystart:=bufstart+bufidx; {next entry position}
  798. NewEntry;
  799. end;
  800. procedure tppufile.putdata(const b;len:integer);
  801. begin
  802. if do_crc then
  803. begin
  804. crc:=UpdateCrc32(crc,b,len);
  805. {$ifdef Test_Double_checksum}
  806. if crc_only then
  807. begin
  808. crc_test2^[crc_index2]:=crc;
  809. {$ifdef Test_Double_checksum_write}
  810. Writeln(CRCFile,crc);
  811. {$endif Test_Double_checksum_write}
  812. if crc_index2<crc_array_size then
  813. inc(crc_index2);
  814. end
  815. else
  816. begin
  817. if (crcindex2<crc_array_size) and (crcindex2<crc_index2) and
  818. (crc_test2^[crcindex2]<>crc) then
  819. Do_comment(V_Note,'impl CRC changed');
  820. {$ifdef Test_Double_checksum_write}
  821. Writeln(CRCFile,crc);
  822. {$endif Test_Double_checksum_write}
  823. inc(crcindex2);
  824. end;
  825. {$endif def Test_Double_checksum}
  826. if do_interface_crc then
  827. begin
  828. interface_crc:=UpdateCrc32(interface_crc,b,len);
  829. {$ifdef Test_Double_checksum}
  830. if crc_only then
  831. begin
  832. crc_test^[crc_index]:=interface_crc;
  833. {$ifdef Test_Double_checksum_write}
  834. Writeln(CRCFile,interface_crc);
  835. {$endif Test_Double_checksum_write}
  836. if crc_index<crc_array_size then
  837. inc(crc_index);
  838. end
  839. else
  840. begin
  841. if (crcindex<crc_array_size) and (crcindex<crc_index) and
  842. (crc_test^[crcindex]<>interface_crc) then
  843. Do_comment(V_Warning,'CRC changed');
  844. {$ifdef Test_Double_checksum_write}
  845. Writeln(CRCFile,interface_crc);
  846. {$endif Test_Double_checksum_write}
  847. inc(crcindex);
  848. end;
  849. {$endif def Test_Double_checksum}
  850. end;
  851. end;
  852. if not crc_only then
  853. writedata(b,len);
  854. inc(entryidx,len);
  855. end;
  856. procedure tppufile.putbyte(b:byte);
  857. begin
  858. putdata(b,1);
  859. end;
  860. procedure tppufile.putword(w:word);
  861. begin
  862. putdata(w,2);
  863. end;
  864. procedure tppufile.putlongint(l:longint);
  865. begin
  866. putdata(l,4);
  867. end;
  868. procedure tppufile.putint64(i:int64);
  869. begin
  870. putdata(i,8);
  871. end;
  872. procedure tppufile.putaint(i:aint);
  873. begin
  874. putdata(i,sizeof(aint));
  875. end;
  876. procedure tppufile.putreal(d:ppureal);
  877. begin
  878. putdata(d,sizeof(ppureal));
  879. end;
  880. procedure tppufile.putstring(s:string);
  881. begin
  882. putdata(s,length(s)+1);
  883. end;
  884. procedure tppufile.putsmallset(const b);
  885. var
  886. l : longint;
  887. begin
  888. l:=longint(b);
  889. putlongint(l);
  890. end;
  891. procedure tppufile.putnormalset(const b);
  892. type
  893. SetLongintArray = Array [0..7] of longint;
  894. var
  895. i : longint;
  896. tempb : setlongintarray;
  897. begin
  898. if change_endian then
  899. begin
  900. for i:=0 to 7 do
  901. tempb[i]:=SwapLong(SetLongintArray(b)[i]);
  902. putdata(tempb,32);
  903. end
  904. else
  905. putdata(b,32);
  906. end;
  907. procedure tppufile.tempclose;
  908. begin
  909. if not closed then
  910. begin
  911. closepos:=filepos(f);
  912. {$I-}
  913. system.close(f);
  914. {$I+}
  915. if ioresult<>0 then;
  916. closed:=true;
  917. tempclosed:=true;
  918. end;
  919. end;
  920. function tppufile.tempopen:boolean;
  921. var
  922. ofm : byte;
  923. begin
  924. tempopen:=false;
  925. if not closed or not tempclosed then
  926. exit;
  927. ofm:=filemode;
  928. filemode:=0;
  929. {$I-}
  930. reset(f,1);
  931. {$I+}
  932. filemode:=ofm;
  933. if ioresult<>0 then
  934. exit;
  935. closed:=false;
  936. tempclosed:=false;
  937. { restore state }
  938. seek(f,closepos);
  939. tempopen:=true;
  940. end;
  941. end.
  942. {
  943. $Log$
  944. Revision 1.61 2005-01-09 20:24:43 olle
  945. * rework of macro subsystem
  946. + exportable macros for mode macpas
  947. Revision 1.60 2004/12/06 19:23:05 peter
  948. implicit load of variants unit
  949. Revision 1.59 2004/11/15 23:35:31 peter
  950. * tparaitem removed, use tparavarsym instead
  951. * parameter order is now calculated from paranr value in tparavarsym
  952. Revision 1.58 2004/11/08 22:09:59 peter
  953. * tvarsym splitted
  954. Revision 1.57 2004/09/21 17:25:12 peter
  955. * paraloc branch merged
  956. Revision 1.56.4.1 2004/09/12 14:01:23 peter
  957. * entry.size need endian update
  958. Revision 1.56 2004/08/27 21:59:26 peter
  959. browser disabled
  960. uf_local_symtable ppu flag when a localsymtable is stored
  961. Revision 1.55 2004/08/22 20:11:38 florian
  962. * morphos now takes any pointer var. as libbase
  963. * alignment for sparc fixed
  964. * int -> double conversion on sparc fixed
  965. Revision 1.54 2004/08/17 16:29:21 jonas
  966. + padalgingment field for recordsymtables (saved by recorddefs)
  967. + support for Macintosh PowerPC alignment (if the first field of a record
  968. or union has an alignment > 4, then the record or union size must be
  969. padded to a multiple of this size)
  970. Revision 1.53 2004/07/12 09:14:04 jonas
  971. * inline procedures at the node tree level, but only under some very
  972. limited circumstances for now (only procedures, and only if they have
  973. no or only vs_out/vs_var parameters).
  974. * fixed ppudump for inline procedures
  975. * fixed ppudump for ppc
  976. Revision 1.52 2004/07/09 23:11:05 peter
  977. * new format
  978. Revision 1.51 2004/06/20 08:55:30 florian
  979. * logs truncated
  980. Revision 1.50 2004/06/16 20:07:09 florian
  981. * dwarf branch merged
  982. Revision 1.49 2004/05/19 21:16:13 peter
  983. * add DEBUGINFO symbol to reference the .o file that includes the
  984. stabs info for types and global/static variables
  985. * debuginfo flag added to ppu to indicate whether debuginfo is
  986. generated or not
  987. Revision 1.48 2004/04/29 19:56:37 daniel
  988. * Prepare compiler infrastructure for multiple ansistring types
  989. Revision 1.47.2.1 2004/05/01 16:02:09 peter
  990. * POINTER_SIZE replaced with sizeof(aint)
  991. * aint,aword,tconst*int moved to globtype
  992. Revision 1.47 2004/03/23 22:34:49 peter
  993. * constants ordinals now always have a type assigned
  994. * integer constants have the smallest type, unsigned prefered over
  995. signed
  996. }