symppu.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. {*****************************************************************************
  29. PPU Writing
  30. *****************************************************************************}
  31. {$ifdef NEWPPU}
  32. procedure writebyte(b:byte);
  33. begin
  34. ppufile^.putbyte(b);
  35. end;
  36. procedure writeword(w:word);
  37. begin
  38. ppufile^.putword(w);
  39. end;
  40. procedure writelong(l:longint);
  41. begin
  42. ppufile^.putlongint(l);
  43. end;
  44. procedure writedouble(d:double);
  45. begin
  46. ppufile^.putdata(d,sizeof(double));
  47. end;
  48. procedure writestring(const s:string);
  49. begin
  50. ppufile^.putstring(s);
  51. end;
  52. procedure writeset(var s); {You cannot pass an array[0..31] of byte!}
  53. begin
  54. ppufile^.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. ppufile^.putstring(s);
  67. if hold then
  68. hcontainer.insert(s);
  69. end;
  70. ppufile^.writeentry(id);
  71. if hold then
  72. p:=hcontainer;
  73. end;
  74. procedure writeposinfo(const p:tfileposinfo);
  75. begin
  76. writeword(p.fileindex);
  77. writelong(p.line);
  78. writeword(p.column);
  79. end;
  80. procedure writedefref(p : pdef);
  81. begin
  82. if p=nil then
  83. ppufile^.putlongint($ffffffff)
  84. else
  85. begin
  86. if (p^.owner^.symtabletype in [recordsymtable,objectsymtable]) then
  87. ppufile^.putword($ffff)
  88. else
  89. ppufile^.putword(p^.owner^.unitid);
  90. ppufile^.putword(p^.indexnb);
  91. end;
  92. end;
  93. procedure writesymref(p : psym);
  94. begin
  95. if p=nil then
  96. writelong($ffffffff)
  97. else
  98. begin
  99. if (p^.owner^.symtabletype in [recordsymtable,objectsymtable]) then
  100. writeword($ffff)
  101. else
  102. writeword(p^.owner^.unitid);
  103. writeword(p^.indexnb);
  104. end;
  105. end;
  106. procedure writeunitas(const s : string;unittable : punitsymtable);
  107. begin
  108. Message1(unit_u_ppu_write,s);
  109. { create unit flags }
  110. with Current_Module^ do
  111. begin
  112. if cs_smartlink in aktswitches then
  113. begin
  114. flags:=flags or uf_smartlink;
  115. if SplitName(ppufilename^)<>SplitName(libfilename^) then
  116. flags:=flags or uf_in_library;
  117. end;
  118. if use_dbx then
  119. flags:=flags or uf_has_dbx;
  120. if target_os.endian=en_big_endian then
  121. flags:=flags or uf_big_endian;
  122. {$ifdef UseBrowser}
  123. if cs_browser in aktswitches then
  124. flags:=flags or uf_has_browser;
  125. {$endif UseBrowser}
  126. end;
  127. { open ppufile }
  128. ppufile:=new(pppufile,init(s));
  129. ppufile^.change_endian:=source_os.endian<>target_os.endian;
  130. if not ppufile^.create then
  131. Message(unit_f_ppu_cannot_write);
  132. { write symbols and definitions }
  133. unittable^.writeasunit;
  134. { flush to be sure }
  135. ppufile^.flush;
  136. { create and write header }
  137. ppufile^.header.size:=ppufile^.size;
  138. ppufile^.header.checksum:=ppufile^.crc;
  139. ppufile^.header.compiler:=wordversion;
  140. ppufile^.header.target:=word(target_info.target);
  141. ppufile^.header.flags:=current_module^.flags;
  142. ppufile^.writeheader;
  143. { save crc in current_module also }
  144. current_module^.crc:=ppufile^.crc;
  145. { close }
  146. ppufile^.close;
  147. dispose(ppufile,done);
  148. end;
  149. {$else NEWPPU}
  150. procedure writebyte(b:byte);
  151. begin
  152. ppufile.write_data(b,1);
  153. end;
  154. procedure writeword(w:word);
  155. begin
  156. ppufile.write_data(w,2);
  157. end;
  158. procedure writelong(l:longint);
  159. begin
  160. ppufile.write_data(l,4);
  161. end;
  162. procedure writedouble(d:double);
  163. begin
  164. ppufile.write_data(d,sizeof(double));
  165. end;
  166. procedure writestring(s : string);
  167. begin
  168. ppufile.write_data(s,length(s)+1);
  169. end;
  170. procedure writeset(var s); {You cannot pass an array[0..31] of byte!}
  171. begin
  172. ppufile.write_data(s,32);
  173. end;
  174. procedure writecontainer(var p:tstringcontainer;id:byte;hold:boolean);
  175. var
  176. hcontainer : tstringcontainer;
  177. s : string;
  178. begin
  179. if hold then
  180. hcontainer.init;
  181. while not p.empty do
  182. begin
  183. writebyte(id);
  184. s:=p.get;
  185. writestring(s);
  186. if hold then
  187. hcontainer.insert(s);
  188. end;
  189. if hold then
  190. p:=hcontainer;
  191. end;
  192. procedure writeposinfo(const p:tfileposinfo);
  193. begin
  194. writeword(p.fileindex);
  195. writelong(p.line);
  196. writeword(p.column);
  197. end;
  198. procedure writedefref(p : pdef);
  199. begin
  200. if p=nil then
  201. writelong($ffffffff)
  202. else
  203. begin
  204. if (p^.owner^.symtabletype in [recordsymtable,objectsymtable]) then
  205. writeword($ffff)
  206. else
  207. writeword(p^.owner^.unitid);
  208. writeword(p^.indexnb);
  209. end;
  210. end;
  211. procedure writesymref(p : psym);
  212. begin
  213. if p=nil then
  214. writelong($ffffffff)
  215. else
  216. begin
  217. if (p^.owner^.symtabletype in [recordsymtable,objectsymtable]) then
  218. writeword($ffff)
  219. else
  220. writeword(p^.owner^.unitid);
  221. writeword(p^.indexnb);
  222. end;
  223. end;
  224. procedure writeunitas(const s : string;unittable : punitsymtable);
  225. {$ifdef UseBrowser}
  226. var
  227. pus : punitsymtable;
  228. {$endif UseBrowser}
  229. begin
  230. Message1(unit_u_ppu_write,s);
  231. { create unit flags }
  232. with Current_Module^ do
  233. begin
  234. if cs_smartlink in aktswitches then
  235. begin
  236. flags:=flags or uf_smartlink;
  237. if SplitName(ppufilename^)<>SplitName(libfilename^) then
  238. flags:=flags or uf_in_library;
  239. end;
  240. if use_dbx then
  241. flags:=flags or uf_has_dbx;
  242. if target_os.endian=en_big_endian then
  243. flags:=flags or uf_big_endian;
  244. {$ifdef UseBrowser}
  245. if use_browser then
  246. flags:=flags or uf_uses_browser;
  247. {$endif UseBrowser}
  248. end;
  249. { open en init ppufile }
  250. ppufile.init(s,ppubufsize);
  251. ppufile.change_endian:=source_os.endian<>target_os.endian;
  252. ppufile.rewrite;
  253. if ioresult<>0 then
  254. Message(unit_f_ppu_cannot_write);
  255. { create and write header }
  256. unitheader[8]:=char(byte(target_info.target));
  257. unitheader[9]:=char(current_module^.flags);
  258. ppufile.write_data(unitheader,sizeof(unitheader));
  259. ppufile.clear_crc;
  260. ppufile.do_crc:=true;
  261. unittable^.writeasunit;
  262. ppufile.flush;
  263. ppufile.do_crc:=false;
  264. {$ifdef UseBrowser}
  265. { write all new references to old unit elements }
  266. pus:=punitsymtable(unittable^.next);
  267. if use_browser then
  268. while assigned(pus) do
  269. begin
  270. if pus^.symtabletype = unitsymtable then
  271. pus^.write_external_references;
  272. pus:=punitsymtable(pus^.next);
  273. end;
  274. {$endif UseBrowser}
  275. { writes the checksum }
  276. ppufile.seek(10);
  277. current_module^.crc:=ppufile.getcrc;
  278. ppufile.write_data(current_module^.crc,4);
  279. ppufile.flush;
  280. ppufile.done;
  281. end;
  282. {$endif NEWPPU}
  283. {*****************************************************************************
  284. PPU Reading
  285. *****************************************************************************}
  286. {$ifdef NEWPPU}
  287. function readbyte:byte;
  288. begin
  289. readbyte:=ppufile^.getbyte;
  290. if ppufile^.error then
  291. Message(unit_f_ppu_read_error);
  292. end;
  293. function readword:word;
  294. begin
  295. readword:=ppufile^.getword;
  296. if ppufile^.error then
  297. Message(unit_f_ppu_read_error);
  298. end;
  299. function readlong:longint;
  300. begin
  301. readlong:=ppufile^.getlongint;
  302. if ppufile^.error then
  303. Message(unit_f_ppu_read_error);
  304. end;
  305. function readdouble : double;
  306. var
  307. d : double;
  308. begin
  309. ppufile^.getdata(d,sizeof(double));
  310. if ppufile^.error then
  311. Message(unit_f_ppu_read_error);
  312. readdouble:=d;
  313. end;
  314. function readstring : string;
  315. begin
  316. readstring:=ppufile^.getstring;
  317. if ppufile^.error then
  318. Message(unit_f_ppu_read_error);
  319. end;
  320. procedure readset(var s); {You cannot pass an array [0..31] of byte.}
  321. begin
  322. ppufile^.getdata(s,32);
  323. if ppufile^.error then
  324. Message(unit_f_ppu_read_error);
  325. end;
  326. procedure readcontainer(var p:tstringcontainer);
  327. begin
  328. while not current_module^.ppufile^.endofentry do
  329. p.insert(current_module^.ppufile^.getstring);
  330. end;
  331. procedure readposinfo(var p:tfileposinfo);
  332. begin
  333. p.fileindex:=readword;
  334. p.line:=readlong;
  335. p.column:=readword;
  336. end;
  337. function readdefref : pdef;
  338. var
  339. hd : pdef;
  340. begin
  341. longint(hd):=readword;
  342. longint(hd):=longint(hd) or (longint(readword) shl 16);
  343. readdefref:=hd;
  344. end;
  345. {$ifdef UseBrowser}
  346. function readsymref : psym;
  347. var
  348. hd : psym;
  349. begin
  350. longint(hd):=readword;
  351. longint(hd):=longint(hd) or (longint(readword) shl 16);
  352. readsymref:=hd;
  353. end;
  354. {$endif}
  355. {$else NEWPPU}
  356. function readbyte : byte;
  357. var
  358. count : longint;
  359. b : byte;
  360. begin
  361. current_module^.ppufile^.read_data(b,sizeof(byte),count);
  362. readbyte:=b;
  363. if count<>1 then
  364. Message(unit_f_ppu_read_error);
  365. end;
  366. function readword : word;
  367. var
  368. count : longint;
  369. w : word;
  370. begin
  371. current_module^.ppufile^.read_data(w,sizeof(word),count);
  372. readword:=w;
  373. if count<>sizeof(word) then
  374. Message(unit_f_ppu_read_error);
  375. end;
  376. function readlong : longint;
  377. var
  378. count,l : longint;
  379. begin
  380. current_module^.ppufile^.read_data(l,sizeof(longint),count);
  381. readlong:=l;
  382. if count<>sizeof(longint) then
  383. Message(unit_f_ppu_read_error);
  384. end;
  385. function readdouble : double;
  386. var
  387. count : longint;
  388. d : double;
  389. begin
  390. current_module^.ppufile^.read_data(d,sizeof(double),count);
  391. readdouble:=d;
  392. if count<>sizeof(double) then
  393. Message(unit_f_ppu_read_error);
  394. end;
  395. function readstring : string;
  396. var
  397. s : string;
  398. count : longint;
  399. begin
  400. s[0]:=char(readbyte);
  401. current_module^.ppufile^.read_data(s[1],ord(s[0]),count);
  402. if count<>ord(s[0]) then
  403. Message(unit_f_ppu_read_error);
  404. readstring:=s;
  405. end;
  406. {***SETCONST}
  407. procedure readset(var s); {You cannot pass an array [0..31] of byte.}
  408. var count:longint;
  409. begin
  410. current_module^.ppufile^.read_data(s,32,count);
  411. if count<>32 then
  412. Message(unit_f_ppu_read_error);
  413. end;
  414. {***}
  415. procedure readposinfo(var p:tfileposinfo);
  416. begin
  417. p.fileindex:=readword;
  418. p.line:=readlong;
  419. p.column:=readword;
  420. end;
  421. function readdefref : pdef;
  422. var
  423. hd : pdef;
  424. begin
  425. longint(hd):=readword;
  426. longint(hd):=longint(hd) or (longint(readword) shl 16);
  427. readdefref:=hd;
  428. end;
  429. {$ifdef UseBrowser}
  430. function readsymref : psym;
  431. var
  432. hd : psym;
  433. begin
  434. longint(hd):=readword;
  435. longint(hd):=longint(hd) or (longint(readword) shl 16);
  436. readsymref:=hd;
  437. end;
  438. {$endif UseBrowser}
  439. {$endif NEWPPU}
  440. {
  441. $Log$
  442. Revision 1.3 1998-06-13 00:10:17 peter
  443. * working browser and newppu
  444. * some small fixes against crashes which occured in bp7 (but not in
  445. fpc?!)
  446. Revision 1.2 1998/05/28 14:40:28 peter
  447. * fixes for newppu, remake3 works now with it
  448. Revision 1.1 1998/05/27 19:45:09 peter
  449. * symtable.pas splitted into includefiles
  450. * symtable adapted for $ifdef NEWPPU
  451. }