symppu.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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^.number);
  91. end;
  92. end;
  93. {$ifdef UseBrowser}
  94. procedure writesymref(p : psym);
  95. begin
  96. if p=nil then
  97. writelong($ffffffff)
  98. else
  99. begin
  100. if (p^.owner^.symtabletype in [recordsymtable,objectsymtable]) then
  101. writeword($ffff)
  102. else
  103. writeword(p^.owner^.unitid);
  104. writeword(p^.indexnb);
  105. end;
  106. end;
  107. {$endif UseBrowser}
  108. procedure writeunitas(const s : string;unit_symtable : punitsymtable);
  109. {$ifdef UseBrowser}
  110. var
  111. pus : punitsymtable;
  112. {$endif UseBrowser}
  113. begin
  114. Message1(unit_u_ppu_write,s);
  115. { create unit flags }
  116. with Current_Module^ do
  117. begin
  118. if cs_smartlink in aktswitches then
  119. begin
  120. flags:=flags or uf_smartlink;
  121. if SplitName(ppufilename^)<>SplitName(libfilename^) then
  122. flags:=flags or uf_in_library;
  123. end;
  124. if use_dbx then
  125. flags:=flags or uf_uses_dbx;
  126. if target_os.endian=en_big_endian then
  127. flags:=flags or uf_big_endian;
  128. {$ifdef UseBrowser}
  129. if use_browser then
  130. flags:=flags or uf_uses_browser;
  131. {$endif UseBrowser}
  132. end;
  133. ppufile:=new(pppufile,init(s));
  134. ppufile^.change_endian:=source_os.endian<>target_os.endian;
  135. if not ppufile^.create then
  136. Message(unit_f_ppu_cannot_write);
  137. unit_symtable^.writeasunit;
  138. {$ifdef UseBrowser}
  139. { write all new references to old unit elements }
  140. pus:=punitsymtable(unit_symtable^.next);
  141. if use_browser then
  142. while assigned(pus) do
  143. begin
  144. if pus^.symtabletype = unitsymtable then
  145. pus^.write_external_references;
  146. pus:=punitsymtable(pus^.next);
  147. end;
  148. {$endif UseBrowser}
  149. ppufile^.flush;
  150. { create and write header }
  151. ppufile^.header.size:=ppufile^.size;
  152. ppufile^.header.checksum:=ppufile^.crc;
  153. ppufile^.header.compiler:=wordversion;
  154. ppufile^.header.target:=word(target_info.target);
  155. ppufile^.header.flags:=current_module^.flags;
  156. ppufile^.writeheader;
  157. { save crc in current_module also }
  158. current_module^.crc:=ppufile^.crc;
  159. { close }
  160. ppufile^.close;
  161. dispose(ppufile,done);
  162. end;
  163. {$else NEWPPU}
  164. procedure writebyte(b:byte);
  165. begin
  166. ppufile.write_data(b,1);
  167. end;
  168. procedure writeword(w:word);
  169. begin
  170. ppufile.write_data(w,2);
  171. end;
  172. procedure writelong(l:longint);
  173. begin
  174. ppufile.write_data(l,4);
  175. end;
  176. procedure writedouble(d:double);
  177. begin
  178. ppufile.write_data(d,sizeof(double));
  179. end;
  180. procedure writestring(s : string);
  181. begin
  182. ppufile.write_data(s,length(s)+1);
  183. end;
  184. procedure writeset(var s); {You cannot pass an array[0..31] of byte!}
  185. begin
  186. ppufile.write_data(s,32);
  187. end;
  188. procedure writecontainer(var p:tstringcontainer;id:byte;hold:boolean);
  189. var
  190. hcontainer : tstringcontainer;
  191. s : string;
  192. begin
  193. if hold then
  194. hcontainer.init;
  195. while not p.empty do
  196. begin
  197. writebyte(id);
  198. s:=p.get;
  199. writestring(s);
  200. if hold then
  201. hcontainer.insert(s);
  202. end;
  203. if hold then
  204. p:=hcontainer;
  205. end;
  206. procedure writeposinfo(const p:tfileposinfo);
  207. begin
  208. writeword(p.fileindex);
  209. writelong(p.line);
  210. writeword(p.column);
  211. end;
  212. procedure writedefref(p : pdef);
  213. begin
  214. if p=nil then
  215. writelong($ffffffff)
  216. else
  217. begin
  218. if (p^.owner^.symtabletype in [recordsymtable,objectsymtable]) then
  219. writeword($ffff)
  220. else
  221. writeword(p^.owner^.unitid);
  222. writeword(p^.number);
  223. end;
  224. end;
  225. {$ifdef UseBrowser}
  226. procedure writesymref(p : psym);
  227. begin
  228. if p=nil then
  229. writelong($ffffffff)
  230. else
  231. begin
  232. if (p^.owner^.symtabletype in [recordsymtable,objectsymtable]) then
  233. writeword($ffff)
  234. else
  235. writeword(p^.owner^.unitid);
  236. writeword(p^.indexnb);
  237. end;
  238. end;
  239. {$endif UseBrowser}
  240. procedure writeunitas(const s : string;unit_symtable : punitsymtable);
  241. {$ifdef UseBrowser}
  242. var
  243. pus : punitsymtable;
  244. {$endif UseBrowser}
  245. begin
  246. Message1(unit_u_ppu_write,s);
  247. { create unit flags }
  248. with Current_Module^ do
  249. begin
  250. if cs_smartlink in aktswitches then
  251. begin
  252. flags:=flags or uf_smartlink;
  253. if SplitName(ppufilename^)<>SplitName(libfilename^) then
  254. flags:=flags or uf_in_library;
  255. end;
  256. if use_dbx then
  257. flags:=flags or uf_uses_dbx;
  258. if target_os.endian=en_big_endian then
  259. flags:=flags or uf_big_endian;
  260. {$ifdef UseBrowser}
  261. if use_browser then
  262. flags:=flags or uf_uses_browser;
  263. {$endif UseBrowser}
  264. end;
  265. { open en init ppufile }
  266. ppufile.init(s,ppubufsize);
  267. ppufile.change_endian:=source_os.endian<>target_os.endian;
  268. ppufile.rewrite;
  269. if ioresult<>0 then
  270. Message(unit_f_ppu_cannot_write);
  271. { create and write header }
  272. unitheader[8]:=char(byte(target_info.target));
  273. unitheader[9]:=char(current_module^.flags);
  274. ppufile.write_data(unitheader,sizeof(unitheader));
  275. ppufile.clear_crc;
  276. ppufile.do_crc:=true;
  277. unit_symtable^.writeasunit;
  278. ppufile.flush;
  279. ppufile.do_crc:=false;
  280. {$ifdef UseBrowser}
  281. { write all new references to old unit elements }
  282. pus:=punitsymtable(unit_symtable^.next);
  283. if use_browser then
  284. while assigned(pus) do
  285. begin
  286. if pus^.symtabletype = unitsymtable then
  287. pus^.write_external_references;
  288. pus:=punitsymtable(pus^.next);
  289. end;
  290. {$endif UseBrowser}
  291. { writes the checksum }
  292. ppufile.seek(10);
  293. current_module^.crc:=ppufile.getcrc;
  294. ppufile.write_data(current_module^.crc,4);
  295. ppufile.flush;
  296. ppufile.done;
  297. end;
  298. {$endif NEWPPU}
  299. {*****************************************************************************
  300. PPU Reading
  301. *****************************************************************************}
  302. {$ifdef NEWPPU}
  303. function readbyte:byte;
  304. begin
  305. readbyte:=ppufile^.getbyte;
  306. if ppufile^.error then
  307. Message(unit_f_ppu_read_error);
  308. end;
  309. function readword:word;
  310. begin
  311. readword:=ppufile^.getword;
  312. if ppufile^.error then
  313. Message(unit_f_ppu_read_error);
  314. end;
  315. function readlong:longint;
  316. begin
  317. readlong:=ppufile^.getlongint;
  318. if ppufile^.error then
  319. Message(unit_f_ppu_read_error);
  320. end;
  321. function readdouble : double;
  322. var
  323. d : double;
  324. begin
  325. ppufile^.getdata(d,sizeof(double));
  326. if ppufile^.error then
  327. Message(unit_f_ppu_read_error);
  328. readdouble:=d;
  329. end;
  330. function readstring : string;
  331. begin
  332. readstring:=ppufile^.getstring;
  333. if ppufile^.error then
  334. Message(unit_f_ppu_read_error);
  335. end;
  336. procedure readset(var s); {You cannot pass an array [0..31] of byte.}
  337. begin
  338. ppufile^.getdata(s,32);
  339. if ppufile^.error then
  340. Message(unit_f_ppu_read_error);
  341. end;
  342. procedure readcontainer(var p:tstringcontainer);
  343. begin
  344. while not current_module^.ppufile^.endofentry do
  345. p.insert(current_module^.ppufile^.getstring);
  346. end;
  347. procedure readposinfo(var p:tfileposinfo);
  348. begin
  349. p.fileindex:=readword;
  350. p.line:=readlong;
  351. p.column:=readword;
  352. end;
  353. function readdefref : pdef;
  354. var
  355. hd : pdef;
  356. begin
  357. longint(hd):=readword;
  358. longint(hd):=longint(hd) or (longint(readword) shl 16);
  359. readdefref:=hd;
  360. end;
  361. {$ifdef UseBrowser}
  362. function readsymref : psym;
  363. var
  364. hd : psym;
  365. begin
  366. longint(hd):=readword;
  367. longint(hd):=longint(hd) or (longint(readword) shl 16);
  368. readsymref:=hd;
  369. end;
  370. {$endif}
  371. {$else NEWPPU}
  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. {$endif NEWPPU}
  456. {
  457. $Log$
  458. Revision 1.2 1998-05-28 14:40:28 peter
  459. * fixes for newppu, remake3 works now with it
  460. Revision 1.1 1998/05/27 19:45:09 peter
  461. * symtable.pas splitted into includefiles
  462. * symtable adapted for $ifdef NEWPPU
  463. }