ppu.pas 25 KB

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