symppu.inc 20 KB

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