symppu.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl, Pierre Muller
  4. Implementation of the reading of PPU Files for the symtable
  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. const
  19. {$ifdef FPC}
  20. ppubufsize=32768;
  21. {$ELSE}
  22. {$IFDEF USEOVERLAY}
  23. ppubufsize=512;
  24. {$ELSE}
  25. ppubufsize=4096;
  26. {$ENDIF}
  27. {$ENDIF}
  28. {$ifndef OLDPPU}
  29. {*****************************************************************************
  30. PPU Writing
  31. *****************************************************************************}
  32. procedure writebyte(b:byte);
  33. begin
  34. current_ppu^.putbyte(b);
  35. end;
  36. procedure writeword(w:word);
  37. begin
  38. current_ppu^.putword(w);
  39. end;
  40. procedure writelong(l:longint);
  41. begin
  42. current_ppu^.putlongint(l);
  43. end;
  44. procedure writedouble(d:double);
  45. begin
  46. current_ppu^.putdata(d,sizeof(double));
  47. end;
  48. procedure writestring(const s:string);
  49. begin
  50. current_ppu^.putstring(s);
  51. end;
  52. procedure writeset(var s); {You cannot pass an array[0..31] of byte!}
  53. begin
  54. current_ppu^.putdata(s,32);
  55. end;
  56. procedure writecontainer(var p:tstringcontainer;id:byte;hold:boolean);
  57. var
  58. hcontainer : tstringcontainer;
  59. s : string;
  60. begin
  61. if hold then
  62. hcontainer.init;
  63. while not p.empty do
  64. begin
  65. s:=p.get;
  66. current_ppu^.putstring(s);
  67. if hold then
  68. hcontainer.insert(s);
  69. end;
  70. current_ppu^.writeentry(id);
  71. if hold then
  72. p:=hcontainer;
  73. end;
  74. procedure writeposinfo(const p:tfileposinfo);
  75. begin
  76. current_ppu^.putword(p.fileindex);
  77. current_ppu^.putlongint(p.line);
  78. current_ppu^.putword(p.column);
  79. end;
  80. procedure writedefref(p : pdef);
  81. begin
  82. if p=nil then
  83. current_ppu^.putlongint($ffffffff)
  84. else
  85. begin
  86. if (p^.owner^.symtabletype in [recordsymtable,objectsymtable]) then
  87. current_ppu^.putword($ffff)
  88. else
  89. current_ppu^.putword(p^.owner^.unitid);
  90. current_ppu^.putword(p^.indexnb);
  91. end;
  92. end;
  93. procedure writesymref(p : psym);
  94. begin
  95. if p=nil then
  96. current_ppu^.putlongint($ffffffff)
  97. else
  98. begin
  99. if (p^.owner^.symtabletype in [recordsymtable,objectsymtable]) then
  100. current_ppu^.putword($ffff)
  101. else
  102. current_ppu^.putword(p^.owner^.unitid);
  103. current_ppu^.putword(p^.indexnb);
  104. end;
  105. end;
  106. {$ifdef NEWINPUT}
  107. procedure writesourcefiles;
  108. var
  109. hp : pinputfile;
  110. index : longint;
  111. begin
  112. { second write the used source files }
  113. hp:=current_module^.sourcefiles.files;
  114. index:=current_module^.sourcefiles.last_ref_index;
  115. while assigned(hp) do
  116. begin
  117. { only name and extension }
  118. current_ppu^.putstring(hp^.name^);
  119. { index in that order }
  120. hp^.ref_index:=index;
  121. dec(index);
  122. hp:=hp^.ref_next;
  123. end;
  124. current_ppu^.writeentry(ibsourcefiles);
  125. end;
  126. {$else}
  127. procedure writesourcefiles;
  128. var
  129. hp2 : pextfile;
  130. index : longint;
  131. begin
  132. { second write the used source files }
  133. hp2:=current_module^.sourcefiles.files;
  134. index:=current_module^.sourcefiles.last_ref_index;
  135. while assigned(hp2) do
  136. begin
  137. { only name and extension }
  138. current_ppu^.putstring(hp2^.name^+hp2^.ext^);
  139. { index in that order }
  140. hp2^.ref_index:=index;
  141. dec(index);
  142. hp2:=hp2^._next;
  143. end;
  144. current_ppu^.writeentry(ibsourcefiles);
  145. end;
  146. {$endif NEWINPUT}
  147. procedure writeusedunit;
  148. var
  149. hp : pused_unit;
  150. begin
  151. numberunits;
  152. hp:=pused_unit(current_module^.used_units.first);
  153. while assigned(hp) do
  154. begin
  155. current_ppu^.putstring(hp^.name^);
  156. current_ppu^.putlongint(hp^.checksum);
  157. current_ppu^.putbyte(byte(hp^.in_interface));
  158. hp:=pused_unit(hp^.next);
  159. end;
  160. current_ppu^.writeentry(ibloadunit_int);
  161. end;
  162. procedure writeunitas(const s : string;unittable : punitsymtable);
  163. begin
  164. Message1(unit_u_ppu_write,s);
  165. { create unit flags }
  166. with Current_Module^ do
  167. begin
  168. if cs_smartlink in aktswitches then
  169. begin
  170. flags:=flags or uf_smartlink;
  171. if SplitName(ppufilename^)<>SplitName(libfilename^) then
  172. flags:=flags or uf_in_library;
  173. end;
  174. if use_dbx then
  175. flags:=flags or uf_has_dbx;
  176. if target_os.endian=en_big_endian then
  177. flags:=flags or uf_big_endian;
  178. {$ifdef UseBrowser}
  179. if cs_browser in aktswitches then
  180. flags:=flags or uf_has_browser;
  181. {$endif UseBrowser}
  182. end;
  183. { open ppufile }
  184. current_ppu:=new(pppufile,init(s));
  185. current_ppu^.change_endian:=source_os.endian<>target_os.endian;
  186. if not current_ppu^.create then
  187. Message(unit_f_ppu_cannot_write);
  188. { write symbols and definitions }
  189. unittable^.writeasunit;
  190. { flush to be sure }
  191. current_ppu^.flush;
  192. { create and write header }
  193. current_ppu^.header.size:=current_ppu^.size;
  194. current_ppu^.header.checksum:=current_ppu^.crc;
  195. current_ppu^.header.compiler:=wordversion;
  196. current_ppu^.header.cpu:=word(target_cpu);
  197. current_ppu^.header.target:=word(target_info.target);
  198. current_ppu^.header.flags:=current_module^.flags;
  199. current_ppu^.writeheader;
  200. { save crc in current_module also }
  201. current_module^.crc:=current_ppu^.crc;
  202. { close }
  203. current_ppu^.close;
  204. dispose(current_ppu,done);
  205. end;
  206. {*****************************************************************************
  207. PPU Reading
  208. *****************************************************************************}
  209. function readbyte:byte;
  210. begin
  211. readbyte:=current_ppu^.getbyte;
  212. if current_ppu^.error then
  213. Message(unit_f_ppu_read_error);
  214. end;
  215. function readword:word;
  216. begin
  217. readword:=current_ppu^.getword;
  218. if current_ppu^.error then
  219. Message(unit_f_ppu_read_error);
  220. end;
  221. function readlong:longint;
  222. begin
  223. readlong:=current_ppu^.getlongint;
  224. if current_ppu^.error then
  225. Message(unit_f_ppu_read_error);
  226. end;
  227. function readdouble : double;
  228. var
  229. d : double;
  230. begin
  231. current_ppu^.getdata(d,sizeof(double));
  232. if current_ppu^.error then
  233. Message(unit_f_ppu_read_error);
  234. readdouble:=d;
  235. end;
  236. function readstring : string;
  237. begin
  238. readstring:=current_ppu^.getstring;
  239. if current_ppu^.error then
  240. Message(unit_f_ppu_read_error);
  241. end;
  242. procedure readset(var s); {You cannot pass an array [0..31] of byte.}
  243. begin
  244. current_ppu^.getdata(s,32);
  245. if current_ppu^.error then
  246. Message(unit_f_ppu_read_error);
  247. end;
  248. procedure readcontainer(var p:tstringcontainer);
  249. begin
  250. while not current_ppu^.endofentry do
  251. p.insert(current_ppu^.getstring);
  252. end;
  253. procedure readposinfo(var p:tfileposinfo);
  254. begin
  255. p.fileindex:=current_ppu^.getword;
  256. p.line:=current_ppu^.getlongint;
  257. p.column:=current_ppu^.getword;
  258. end;
  259. function readdefref : pdef;
  260. var
  261. hd : pdef;
  262. begin
  263. longint(hd):=current_ppu^.getword;
  264. longint(hd):=longint(hd) or (longint(current_ppu^.getword) shl 16);
  265. readdefref:=hd;
  266. end;
  267. {$ifdef UseBrowser}
  268. function readsymref : psym;
  269. var
  270. hd : psym;
  271. begin
  272. longint(hd):=current_ppu^.getword;
  273. longint(hd):=longint(hd) or (longint(current_ppu^.getword) shl 16);
  274. readsymref:=hd;
  275. end;
  276. {$endif}
  277. procedure readsourcefiles;
  278. var
  279. temp,hs : string;
  280. incfile_found : boolean;
  281. ppufiletime,
  282. source_time : longint;
  283. {$ifdef UseBrowser}
  284. hp : pextfile;
  285. _d,_n,_e : string;
  286. {$endif UseBrowser}
  287. begin
  288. ppufiletime:=getnamedfiletime(current_module^.ppufilename^);
  289. current_module^.sources_avail:=true;
  290. while not current_ppu^.endofentry do
  291. begin
  292. hs:=current_ppu^.getstring;
  293. temp:='';
  294. if (current_module^.flags and uf_in_library)<>0 then
  295. begin
  296. current_module^.sources_avail:=false;
  297. temp:=' library';
  298. end
  299. else if pos('Macro ',hs)=1 then
  300. begin
  301. { we don't want to find this file }
  302. { but there is a problem with file indexing !! }
  303. temp:='';
  304. end
  305. else
  306. begin
  307. { check the date of the source files }
  308. Source_Time:=GetNamedFileTime(current_module^.path^+hs);
  309. if Source_Time=-1 then
  310. begin
  311. { search for include files in the includepathlist }
  312. temp:=search(hs,includesearchpath,incfile_found);
  313. if incfile_found then
  314. begin
  315. hs:=temp+hs;
  316. Source_Time:=GetNamedFileTime(hs);
  317. end;
  318. end
  319. else
  320. hs:=current_module^.path^+hs;
  321. if Source_Time=-1 then
  322. begin
  323. current_module^.sources_avail:=false;
  324. temp:=' not found';
  325. end
  326. else
  327. begin
  328. temp:=' time '+filetimestring(source_time);
  329. if (source_time>ppufiletime) then
  330. begin
  331. current_module^.do_compile:=true;
  332. temp:=temp+' *'
  333. end;
  334. end;
  335. end;
  336. Message1(unit_t_ppu_source,hs+temp);
  337. {$ifdef UseBrowser}
  338. fsplit(hs,_d,_n,_e);
  339. new(hp,init(_d,_n,_e));
  340. { the indexing should match what is done in writeasunit }
  341. current_module^.sourcefiles.register_file(hp);
  342. {$endif UseBrowser}
  343. end;
  344. { main source is always the last }
  345. stringdispose(current_module^.mainsource);
  346. current_module^.mainsource:=stringdup(hs);
  347. { check if we want to rebuild every unit, only if the sources are
  348. available }
  349. if do_build and current_module^.sources_avail then
  350. current_module^.do_compile:=true;
  351. end;
  352. procedure readloadunit;
  353. var
  354. hs : string;
  355. checksum : longint;
  356. in_interface : boolean;
  357. begin
  358. while not current_ppu^.endofentry do
  359. begin
  360. hs:=current_ppu^.getstring;
  361. checksum:=current_ppu^.getlongint;
  362. in_interface:=(current_ppu^.getbyte<>0);
  363. current_module^.used_units.concat(new(pused_unit,init_to_load(hs,checksum,in_interface)));
  364. end;
  365. end;
  366. procedure load_interface;
  367. var
  368. b : byte;
  369. begin
  370. { read interface part }
  371. repeat
  372. b:=current_ppu^.readentry;
  373. case b of
  374. { ibinitunit : usedunits^.insert(readstring); }
  375. ibmodulename : begin
  376. stringdispose(current_module^.modulename);
  377. current_module^.modulename:=stringdup(current_ppu^.getstring);
  378. end;
  379. ibsourcefiles : readsourcefiles;
  380. ibloadunit_int : readloadunit;
  381. iblinksharedlibs : readcontainer(current_module^.LinkSharedLibs);
  382. iblinkstaticlibs : readcontainer(current_module^.LinkStaticLibs);
  383. iblinkofiles : readcontainer(current_module^.LinkOFiles);
  384. ibendinterface : break;
  385. else
  386. Message1(unit_f_ppu_invalid_entry,tostr(b));
  387. end;
  388. until false;
  389. end;
  390. {$else OLDPPU}
  391. {*****************************************************************************
  392. Old PPU
  393. *****************************************************************************}
  394. function readbyte : byte;
  395. var
  396. count : longint;
  397. b : byte;
  398. begin
  399. current_module^.ppufile^.read_data(b,sizeof(byte),count);
  400. readbyte:=b;
  401. if count<>1 then
  402. Message(unit_f_ppu_read_error);
  403. end;
  404. function readword : word;
  405. var
  406. count : longint;
  407. w : word;
  408. begin
  409. current_module^.ppufile^.read_data(w,sizeof(word),count);
  410. readword:=w;
  411. if count<>sizeof(word) then
  412. Message(unit_f_ppu_read_error);
  413. end;
  414. function readlong : longint;
  415. var
  416. count,l : longint;
  417. begin
  418. current_module^.ppufile^.read_data(l,sizeof(longint),count);
  419. readlong:=l;
  420. if count<>sizeof(longint) then
  421. Message(unit_f_ppu_read_error);
  422. end;
  423. function readdouble : double;
  424. var
  425. count : longint;
  426. d : double;
  427. begin
  428. current_module^.ppufile^.read_data(d,sizeof(double),count);
  429. readdouble:=d;
  430. if count<>sizeof(double) then
  431. Message(unit_f_ppu_read_error);
  432. end;
  433. function readstring : string;
  434. var
  435. s : string;
  436. count : longint;
  437. begin
  438. s[0]:=char(readbyte);
  439. current_module^.ppufile^.read_data(s[1],ord(s[0]),count);
  440. if count<>ord(s[0]) then
  441. Message(unit_f_ppu_read_error);
  442. readstring:=s;
  443. end;
  444. {***SETCONST}
  445. procedure readset(var s); {You cannot pass an array [0..31] of byte.}
  446. var count:longint;
  447. begin
  448. current_module^.ppufile^.read_data(s,32,count);
  449. if count<>32 then
  450. Message(unit_f_ppu_read_error);
  451. end;
  452. {***}
  453. procedure readposinfo(var p:tfileposinfo);
  454. begin
  455. p.fileindex:=readword;
  456. p.line:=readlong;
  457. p.column:=readword;
  458. end;
  459. function readdefref : pdef;
  460. var
  461. hd : pdef;
  462. begin
  463. longint(hd):=readword;
  464. longint(hd):=longint(hd) or (longint(readword) shl 16);
  465. readdefref:=hd;
  466. end;
  467. {$ifdef UseBrowser}
  468. function readsymref : psym;
  469. var
  470. hd : psym;
  471. begin
  472. longint(hd):=readword;
  473. longint(hd):=longint(hd) or (longint(readword) shl 16);
  474. readsymref:=hd;
  475. end;
  476. {$endif UseBrowser}
  477. procedure writebyte(b:byte);
  478. begin
  479. ppufile.write_data(b,1);
  480. end;
  481. procedure writeword(w:word);
  482. begin
  483. ppufile.write_data(w,2);
  484. end;
  485. procedure writelong(l:longint);
  486. begin
  487. ppufile.write_data(l,4);
  488. end;
  489. procedure writedouble(d:double);
  490. begin
  491. ppufile.write_data(d,sizeof(double));
  492. end;
  493. procedure writestring(s : string);
  494. begin
  495. ppufile.write_data(s,length(s)+1);
  496. end;
  497. procedure writeset(var s); {You cannot pass an array[0..31] of byte!}
  498. begin
  499. ppufile.write_data(s,32);
  500. end;
  501. procedure writecontainer(var p:tstringcontainer;id:byte;hold:boolean);
  502. var
  503. hcontainer : tstringcontainer;
  504. s : string;
  505. begin
  506. if hold then
  507. hcontainer.init;
  508. while not p.empty do
  509. begin
  510. writebyte(id);
  511. s:=p.get;
  512. writestring(s);
  513. if hold then
  514. hcontainer.insert(s);
  515. end;
  516. if hold then
  517. p:=hcontainer;
  518. end;
  519. procedure writeposinfo(const p:tfileposinfo);
  520. begin
  521. writeword(p.fileindex);
  522. writelong(p.line);
  523. writeword(p.column);
  524. end;
  525. procedure writedefref(p : pdef);
  526. begin
  527. if p=nil then
  528. writelong($ffffffff)
  529. else
  530. begin
  531. if (p^.owner^.symtabletype in [recordsymtable,objectsymtable]) then
  532. writeword($ffff)
  533. else
  534. writeword(p^.owner^.unitid);
  535. writeword(p^.indexnb);
  536. end;
  537. end;
  538. procedure writesymref(p : psym);
  539. begin
  540. if p=nil then
  541. writelong($ffffffff)
  542. else
  543. begin
  544. if (p^.owner^.symtabletype in [recordsymtable,objectsymtable]) then
  545. writeword($ffff)
  546. else
  547. writeword(p^.owner^.unitid);
  548. writeword(p^.indexnb);
  549. end;
  550. end;
  551. procedure writeunitas(const s : string;unittable : punitsymtable);
  552. {$ifdef UseBrowser}
  553. var
  554. pus : punitsymtable;
  555. {$endif UseBrowser}
  556. begin
  557. Message1(unit_u_ppu_write,s);
  558. { create unit flags }
  559. with Current_Module^ do
  560. begin
  561. if cs_smartlink in aktswitches then
  562. begin
  563. flags:=flags or uf_smartlink;
  564. if SplitName(ppufilename^)<>SplitName(libfilename^) then
  565. flags:=flags or uf_in_library;
  566. end;
  567. if use_dbx then
  568. flags:=flags or uf_has_dbx;
  569. if target_os.endian=en_big_endian then
  570. flags:=flags or uf_big_endian;
  571. {$ifdef UseBrowser}
  572. if use_browser then
  573. flags:=flags or uf_uses_browser;
  574. {$endif UseBrowser}
  575. end;
  576. { open en init ppufile }
  577. ppufile.init(s,ppubufsize);
  578. ppufile.change_endian:=source_os.endian<>target_os.endian;
  579. ppufile.rewrite;
  580. if ioresult<>0 then
  581. Message(unit_f_ppu_cannot_write);
  582. { create and write header }
  583. unitheader[8]:=char(byte(target_info.target));
  584. unitheader[9]:=char(current_module^.flags);
  585. ppufile.write_data(unitheader,sizeof(unitheader));
  586. ppufile.clear_crc;
  587. ppufile.do_crc:=true;
  588. unittable^.writeasunit;
  589. ppufile.flush;
  590. ppufile.do_crc:=false;
  591. {$ifdef UseBrowser}
  592. { write all new references to old unit elements }
  593. pus:=punitsymtable(unittable^.next);
  594. if use_browser then
  595. while assigned(pus) do
  596. begin
  597. if pus^.symtabletype = unitsymtable then
  598. pus^.write_external_references;
  599. pus:=punitsymtable(pus^.next);
  600. end;
  601. {$endif UseBrowser}
  602. { writes the checksum }
  603. ppufile.seek(10);
  604. current_module^.crc:=ppufile.getcrc;
  605. ppufile.write_data(current_module^.crc,4);
  606. ppufile.flush;
  607. ppufile.done;
  608. end;
  609. {$endif OLDPPU}
  610. {
  611. $Log$
  612. Revision 1.6 1998-07-07 11:20:14 peter
  613. + NEWINPUT for a better inputfile and scanner object
  614. Revision 1.5 1998/06/24 14:48:39 peter
  615. * ifdef newppu -> ifndef oldppu
  616. Revision 1.4 1998/06/16 08:56:32 peter
  617. + targetcpu
  618. * cleaner pmodules for newppu
  619. Revision 1.3 1998/06/13 00:10:17 peter
  620. * working browser and newppu
  621. * some small fixes against crashes which occured in bp7 (but not in
  622. fpc?!)
  623. Revision 1.2 1998/05/28 14:40:28 peter
  624. * fixes for newppu, remake3 works now with it
  625. Revision 1.1 1998/05/27 19:45:09 peter
  626. * symtable.pas splitted into includefiles
  627. * symtable adapted for $ifdef NEWPPU
  628. }