symppu.inc 19 KB

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