symppu.inc 19 KB

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