fppu.pas 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the first loading and searching of the modules
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit fppu;
  18. {$i fpcdefs.inc}
  19. { close ppufiles on system that are
  20. short on file handles like DOS system PM }
  21. {$ifdef GO32V2}
  22. {$define SHORT_ON_FILE_HANDLES}
  23. {$endif GO32V2}
  24. {$ifdef WATCOM}
  25. {$define SHORT_ON_FILE_HANDLES}
  26. {$endif WATCOM}
  27. interface
  28. uses
  29. cutils,cclasses,
  30. globtype,globals,finput,fmodule,
  31. symbase,ppu,symtype;
  32. type
  33. tppumodule = class(tmodule)
  34. ppufile : tcompilerppufile; { the PPU file }
  35. sourcefn : pstring; { Source specified with "uses .. in '..'" }
  36. {$ifdef Test_Double_checksum}
  37. crc_array : pointer;
  38. crc_size : longint;
  39. crc_array2 : pointer;
  40. crc_size2 : longint;
  41. {$endif def Test_Double_checksum}
  42. constructor create(LoadedFrom:TModule;const s:string;const fn:string;_is_unit:boolean);
  43. destructor destroy;override;
  44. procedure reset;override;
  45. function openppu:boolean;
  46. procedure getppucrc;
  47. procedure writeppu;
  48. procedure loadppu;
  49. function needrecompile:boolean;
  50. private
  51. function search_unit(onlysource,shortname:boolean):boolean;
  52. procedure load_interface;
  53. procedure load_implementation;
  54. procedure load_symtable_refs;
  55. procedure load_usedunits;
  56. procedure writesourcefiles;
  57. procedure writeusedunit(intf:boolean);
  58. procedure writelinkcontainer(var p:tlinkcontainer;id:byte;strippath:boolean);
  59. procedure writederefmap;
  60. procedure writederefdata;
  61. procedure putasmsymbol_in_idx(s:tnamedindexitem;arg:pointer);
  62. procedure writeasmsymbols;
  63. procedure readsourcefiles;
  64. procedure readloadunit;
  65. procedure readlinkcontainer(var p:tlinkcontainer);
  66. procedure readderefmap;
  67. procedure readderefdata;
  68. procedure readasmsymbols;
  69. {$IFDEF MACRO_DIFF_HINT}
  70. procedure writeusedmacro(p:TNamedIndexItem;arg:pointer);
  71. procedure writeusedmacros;
  72. procedure readusedmacros;
  73. {$ENDIF}
  74. end;
  75. procedure reload_flagged_units;
  76. function registerunit(callermodule:tmodule;const s : stringid;const fn:string) : tppumodule;
  77. implementation
  78. uses
  79. verbose,systems,version,
  80. symtable, symsym,
  81. scanner,
  82. aasmbase,
  83. parser;
  84. {****************************************************************************
  85. Helpers
  86. ****************************************************************************}
  87. procedure reload_flagged_units;
  88. var
  89. hp : tmodule;
  90. begin
  91. { now reload all dependent units }
  92. hp:=tmodule(loaded_units.first);
  93. while assigned(hp) do
  94. begin
  95. if hp.do_reload then
  96. tppumodule(hp).loadppu;
  97. hp:=tmodule(hp.next);
  98. end;
  99. end;
  100. {****************************************************************************
  101. TPPUMODULE
  102. ****************************************************************************}
  103. constructor tppumodule.create(LoadedFrom:TModule;const s:string;const fn:string;_is_unit:boolean);
  104. begin
  105. inherited create(LoadedFrom,s,_is_unit);
  106. ppufile:=nil;
  107. sourcefn:=stringdup(fn);
  108. end;
  109. destructor tppumodule.Destroy;
  110. begin
  111. if assigned(ppufile) then
  112. ppufile.free;
  113. ppufile:=nil;
  114. stringdispose(sourcefn);
  115. inherited Destroy;
  116. end;
  117. procedure tppumodule.reset;
  118. begin
  119. if assigned(ppufile) then
  120. begin
  121. ppufile.free;
  122. ppufile:=nil;
  123. end;
  124. inherited reset;
  125. end;
  126. function tppumodule.openppu:boolean;
  127. var
  128. ppufiletime : longint;
  129. begin
  130. openppu:=false;
  131. Message1(unit_t_ppu_loading,ppufilename^);
  132. { Get ppufile time (also check if the file exists) }
  133. ppufiletime:=getnamedfiletime(ppufilename^);
  134. if ppufiletime=-1 then
  135. exit;
  136. { Open the ppufile }
  137. Message1(unit_u_ppu_name,ppufilename^);
  138. ppufile:=tcompilerppufile.create(ppufilename^);
  139. if not ppufile.openfile then
  140. begin
  141. ppufile.free;
  142. ppufile:=nil;
  143. Message(unit_u_ppu_file_too_short);
  144. exit;
  145. end;
  146. { check for a valid PPU file }
  147. if not ppufile.CheckPPUId then
  148. begin
  149. ppufile.free;
  150. ppufile:=nil;
  151. Message(unit_u_ppu_invalid_header);
  152. exit;
  153. end;
  154. { check for allowed PPU versions }
  155. if not (ppufile.GetPPUVersion = CurrentPPUVersion) then
  156. begin
  157. Message1(unit_u_ppu_invalid_version,tostr(ppufile.GetPPUVersion));
  158. ppufile.free;
  159. ppufile:=nil;
  160. exit;
  161. end;
  162. { check the target processor }
  163. if tsystemcpu(ppufile.header.cpu)<>target_cpu then
  164. begin
  165. ppufile.free;
  166. ppufile:=nil;
  167. Message(unit_u_ppu_invalid_processor);
  168. exit;
  169. end;
  170. { check target }
  171. if tsystem(ppufile.header.target)<>target_info.system then
  172. begin
  173. ppufile.free;
  174. ppufile:=nil;
  175. Message(unit_u_ppu_invalid_target);
  176. exit;
  177. end;
  178. {$ifdef cpufpemu}
  179. { check if floating point emulation is on?}
  180. { fpu emulation isn't unit levelwise
  181. if ((ppufile.header.flags and uf_fpu_emulation)<>0) and
  182. (cs_fp_emulation in aktmoduleswitches) then
  183. begin
  184. ppufile.free;
  185. ppufile:=nil;
  186. Message(unit_u_ppu_invalid_fpumode);
  187. exit;
  188. end;
  189. }
  190. {$endif cpufpemu}
  191. { Load values to be access easier }
  192. flags:=ppufile.header.flags;
  193. crc:=ppufile.header.checksum;
  194. interface_crc:=ppufile.header.interface_checksum;
  195. { Show Debug info }
  196. Message1(unit_u_ppu_time,filetimestring(ppufiletime));
  197. Message1(unit_u_ppu_flags,tostr(flags));
  198. Message1(unit_u_ppu_crc,hexstr(ppufile.header.checksum,8));
  199. Message1(unit_u_ppu_crc,hexstr(ppufile.header.interface_checksum,8)+' (intfc)');
  200. do_compile:=false;
  201. openppu:=true;
  202. end;
  203. function tppumodule.search_unit(onlysource,shortname:boolean):boolean;
  204. var
  205. singlepathstring,
  206. filename : string;
  207. Function UnitExists(const ext:string;var foundfile:string):boolean;
  208. begin
  209. Message1(unit_t_unitsearch,Singlepathstring+filename+ext);
  210. UnitExists:=FindFile(FileName+ext,Singlepathstring,foundfile);
  211. end;
  212. Function PPUSearchPath(const s:string):boolean;
  213. var
  214. found : boolean;
  215. hs : string;
  216. begin
  217. Found:=false;
  218. singlepathstring:=FixPath(s,false);
  219. { Check for PPU file }
  220. Found:=UnitExists(target_info.unitext,hs);
  221. if Found then
  222. Begin
  223. SetFileName(hs,false);
  224. Found:=OpenPPU;
  225. End;
  226. PPUSearchPath:=Found;
  227. end;
  228. Function SourceSearchPath(const s:string):boolean;
  229. var
  230. found : boolean;
  231. hs : string;
  232. begin
  233. Found:=false;
  234. singlepathstring:=FixPath(s,false);
  235. { Check for Sources }
  236. ppufile:=nil;
  237. do_compile:=true;
  238. recompile_reason:=rr_noppu;
  239. {Check for .pp file}
  240. Found:=UnitExists(sourceext,hs);
  241. if not Found then
  242. begin
  243. { Check for .pas }
  244. Found:=UnitExists(pasext,hs);
  245. end;
  246. if not Found and (m_mac in aktmodeswitches) then
  247. begin
  248. { Check for .p, if mode is macpas}
  249. Found:=UnitExists(pext,hs);
  250. end;
  251. stringdispose(mainsource);
  252. if Found then
  253. begin
  254. sources_avail:=true;
  255. { Load Filenames when found }
  256. mainsource:=StringDup(hs);
  257. SetFileName(hs,false);
  258. end
  259. else
  260. sources_avail:=false;
  261. SourceSearchPath:=Found;
  262. end;
  263. Function SearchPath(const s:string):boolean;
  264. var
  265. found : boolean;
  266. begin
  267. { First check for a ppu, then for the source }
  268. found:=false;
  269. if not onlysource then
  270. found:=PPUSearchPath(s);
  271. if not found then
  272. found:=SourceSearchPath(s);
  273. SearchPath:=found;
  274. end;
  275. Function SearchPathList(list:TSearchPathList):boolean;
  276. var
  277. hp : TStringListItem;
  278. found : boolean;
  279. begin
  280. found:=false;
  281. hp:=TStringListItem(list.First);
  282. while assigned(hp) do
  283. begin
  284. found:=SearchPath(hp.Str);
  285. if found then
  286. break;
  287. hp:=TStringListItem(hp.next);
  288. end;
  289. SearchPathList:=found;
  290. end;
  291. var
  292. fnd : boolean;
  293. hs : string;
  294. begin
  295. if shortname then
  296. filename:=FixFileName(Copy(realmodulename^,1,8))
  297. else
  298. filename:=FixFileName(realmodulename^);
  299. { try to find unit
  300. 1. look for ppu in cwd
  301. 2. look for ppu in outputpath if set, this is tp7 compatible (PFV)
  302. 3. look for the specified source file (from the uses line)
  303. 4. look for source in cwd
  304. 5. look in same path as local unit
  305. 6. local unit pathlist
  306. 7. global unit pathlist }
  307. fnd:=false;
  308. if not onlysource then
  309. begin
  310. fnd:=PPUSearchPath('.');
  311. if (not fnd) and (outputpath^<>'') then
  312. fnd:=PPUSearchPath(outputpath^);
  313. end;
  314. if (not fnd) and (sourcefn^<>'') then
  315. begin
  316. { the full filename is specified so we can't use here the
  317. searchpath (PFV) }
  318. Message1(unit_t_unitsearch,AddExtension(sourcefn^,sourceext));
  319. fnd:=FindFile(AddExtension(sourcefn^,sourceext),'',hs);
  320. if not fnd then
  321. begin
  322. Message1(unit_t_unitsearch,AddExtension(sourcefn^,pasext));
  323. fnd:=FindFile(AddExtension(sourcefn^,pasext),'',hs);
  324. end;
  325. if not fnd and ((m_mac in aktmodeswitches) or target_info.p_ext_support) then
  326. begin
  327. Message1(unit_t_unitsearch,AddExtension(sourcefn^,pext));
  328. fnd:=FindFile(AddExtension(sourcefn^,pext),'',hs);
  329. end;
  330. if fnd then
  331. begin
  332. sources_avail:=true;
  333. do_compile:=true;
  334. recompile_reason:=rr_noppu;
  335. stringdispose(mainsource);
  336. mainsource:=StringDup(hs);
  337. SetFileName(hs,false);
  338. end;
  339. end;
  340. if not fnd then
  341. fnd:=SourceSearchPath('.');
  342. if (not fnd) and Assigned(Loaded_From) then
  343. begin
  344. fnd:=PPUSearchPath(Loaded_From.Path^);
  345. if not fnd then
  346. fnd:=SourceSearchPath(Loaded_From.Path^);
  347. if not fnd then
  348. fnd:=SearchPathList(Loaded_From.LocalUnitSearchPath);
  349. end;
  350. if not fnd then
  351. fnd:=SearchPathList(UnitSearchPath);
  352. { try to find a file with the first 8 chars of the modulename, like
  353. dos }
  354. if (not fnd) and (length(filename)>8) then
  355. begin
  356. filename:=copy(filename,1,8);
  357. fnd:=SearchPath('.');
  358. if (not fnd) then
  359. fnd:=SearchPathList(LocalUnitSearchPath);
  360. if not fnd then
  361. fnd:=SearchPathList(UnitSearchPath);
  362. end;
  363. search_unit:=fnd;
  364. end;
  365. {**********************************
  366. PPU Reading/Writing Helpers
  367. ***********************************}
  368. {$IFDEF MACRO_DIFF_HINT}
  369. var
  370. is_initial: Boolean;
  371. procedure tppumodule.writeusedmacro(p:TNamedIndexItem;arg:pointer);
  372. begin
  373. if tmacro(p).is_used or is_initial then
  374. begin
  375. ppufile.putstring(p.name);
  376. ppufile.putbyte(byte(is_initial));
  377. ppufile.putbyte(byte(tmacro(p).is_used));
  378. end;
  379. end;
  380. procedure tppumodule.writeusedmacros;
  381. begin
  382. ppufile.do_crc:=false;
  383. is_initial:= true;
  384. initialmacrosymtable.foreach(@writeusedmacro,nil);
  385. is_initial:= false;
  386. if assigned(globalmacrosymtable) then
  387. globalmacrosymtable.foreach(@writeusedmacro,nil);
  388. localmacrosymtable.foreach(@writeusedmacro,nil);
  389. ppufile.writeentry(ibusedmacros);
  390. ppufile.do_crc:=true;
  391. end;
  392. {$ENDIF}
  393. procedure tppumodule.writesourcefiles;
  394. var
  395. hp : tinputfile;
  396. i,j : longint;
  397. begin
  398. { second write the used source files }
  399. ppufile.do_crc:=false;
  400. hp:=sourcefiles.files;
  401. { write source files directly in good order }
  402. j:=0;
  403. while assigned(hp) do
  404. begin
  405. inc(j);
  406. hp:=hp.ref_next;
  407. end;
  408. while j>0 do
  409. begin
  410. hp:=sourcefiles.files;
  411. for i:=1 to j-1 do
  412. hp:=hp.ref_next;
  413. ppufile.putstring(hp.name^);
  414. ppufile.putlongint(hp.getfiletime);
  415. dec(j);
  416. end;
  417. ppufile.writeentry(ibsourcefiles);
  418. ppufile.do_crc:=true;
  419. end;
  420. procedure tppumodule.writeusedunit(intf:boolean);
  421. var
  422. hp : tused_unit;
  423. oldcrc : boolean;
  424. begin
  425. { write a reference for each used unit }
  426. hp:=tused_unit(used_units.first);
  427. while assigned(hp) do
  428. begin
  429. if hp.in_interface=intf then
  430. begin
  431. ppufile.putstring(hp.u.realmodulename^);
  432. { the checksum should not affect the crc of this unit ! (PFV) }
  433. oldcrc:=ppufile.do_crc;
  434. ppufile.do_crc:=false;
  435. ppufile.putlongint(longint(hp.checksum));
  436. ppufile.putlongint(longint(hp.interface_checksum));
  437. ppufile.do_crc:=oldcrc;
  438. end;
  439. hp:=tused_unit(hp.next);
  440. end;
  441. ppufile.do_interface_crc:=true;
  442. ppufile.writeentry(ibloadunit);
  443. end;
  444. procedure tppumodule.writelinkcontainer(var p:tlinkcontainer;id:byte;strippath:boolean);
  445. var
  446. hcontainer : tlinkcontainer;
  447. s : string;
  448. mask : cardinal;
  449. begin
  450. hcontainer:=TLinkContainer.Create;
  451. while not p.empty do
  452. begin
  453. s:=p.get(mask);
  454. if strippath then
  455. ppufile.putstring(SplitFileName(s))
  456. else
  457. ppufile.putstring(s);
  458. ppufile.putlongint(mask);
  459. hcontainer.add(s,mask);
  460. end;
  461. ppufile.writeentry(id);
  462. p.Free;
  463. p:=hcontainer;
  464. end;
  465. procedure tppumodule.writederefmap;
  466. var
  467. i : longint;
  468. oldcrc : boolean;
  469. begin
  470. { This does not influence crc }
  471. oldcrc:=ppufile.do_crc;
  472. ppufile.do_crc:=false;
  473. { The unit map used for resolving }
  474. ppufile.putlongint(derefmapcnt);
  475. for i:=0 to derefmapcnt-1 do
  476. begin
  477. if not assigned(derefmap[i].u) then
  478. internalerror(2005011512);
  479. ppufile.putstring(derefmap[i].u.modulename^)
  480. end;
  481. ppufile.writeentry(ibderefmap);
  482. ppufile.do_crc:=oldcrc;
  483. end;
  484. procedure tppumodule.writederefdata;
  485. var
  486. oldcrc : boolean;
  487. len,hlen : longint;
  488. buf : array[0..1023] of byte;
  489. begin
  490. if derefdataintflen>derefdata.size then
  491. internalerror(200310223);
  492. derefdata.seek(0);
  493. { Write interface data }
  494. len:=derefdataintflen;
  495. while (len>0) do
  496. begin
  497. if len>1024 then
  498. hlen:=1024
  499. else
  500. hlen:=len;
  501. derefdata.read(buf,hlen);
  502. ppufile.putdata(buf,hlen);
  503. dec(len,hlen);
  504. end;
  505. { Write implementation data, this does not influence crc }
  506. oldcrc:=ppufile.do_crc;
  507. ppufile.do_crc:=false;
  508. len:=derefdata.size-derefdataintflen;
  509. while (len>0) do
  510. begin
  511. if len>1024 then
  512. hlen:=1024
  513. else
  514. hlen:=len;
  515. derefdata.read(buf,hlen);
  516. ppufile.putdata(buf,hlen);
  517. dec(len,hlen);
  518. end;
  519. if derefdata.pos<>derefdata.size then
  520. internalerror(200310224);
  521. ppufile.do_crc:=oldcrc;
  522. ppufile.writeentry(ibderefdata);
  523. end;
  524. procedure tppumodule.putasmsymbol_in_idx(s:tnamedindexitem;arg:pointer);
  525. begin
  526. if tasmsymbol(s).ppuidx<>-1 then
  527. librarydata.asmsymbolidx^[tasmsymbol(s).ppuidx-1]:=tasmsymbol(s);
  528. end;
  529. procedure tppumodule.writeasmsymbols;
  530. var
  531. s : tasmsymbol;
  532. i : longint;
  533. asmsymtype : byte;
  534. begin
  535. { get an ordered list of all symbols to put in the ppu }
  536. getmem(librarydata.asmsymbolidx,librarydata.asmsymbolppuidx*sizeof(pointer));
  537. fillchar(librarydata.asmsymbolidx^,librarydata.asmsymbolppuidx*sizeof(pointer),0);
  538. librarydata.symbolsearch.foreach(@putasmsymbol_in_idx,nil);
  539. { write the number of symbols }
  540. ppufile.putlongint(librarydata.asmsymbolppuidx);
  541. { write the symbols from the indexed list to the ppu }
  542. for i:=1 to librarydata.asmsymbolppuidx do
  543. begin
  544. s:=librarydata.asmsymbolidx^[i-1];
  545. if not assigned(s) then
  546. internalerror(200208071);
  547. asmsymtype:=1;
  548. if s.Classtype=tasmlabel then
  549. begin
  550. if tasmlabel(s).is_addr then
  551. asmsymtype:=4
  552. else if tasmlabel(s).typ=AT_DATA then
  553. asmsymtype:=3
  554. else
  555. asmsymtype:=2;
  556. end;
  557. ppufile.putbyte(asmsymtype);
  558. case asmsymtype of
  559. 1 :
  560. ppufile.putstring(s.name);
  561. 2..4 :
  562. ppufile.putlongint(tasmlabel(s).labelnr);
  563. end;
  564. ppufile.putbyte(byte(s.defbind));
  565. ppufile.putbyte(byte(s.typ));
  566. end;
  567. ppufile.writeentry(ibasmsymbols);
  568. end;
  569. {$IFDEF MACRO_DIFF_HINT}
  570. {
  571. Define MACRO_DIFF_HINT for the whole compiler (and ppudump)
  572. to turn this facility on. Also the hint messages defined
  573. below must be commented in in the msg/errore.msg file.
  574. There is some problems with this, thats why it is shut off:
  575. At the first compilation, consider a macro which is not initially
  576. defined, but it is used (e g the check that it is undefined is true).
  577. Since it do not exist, there is no macro object where the is_used
  578. flag can be set. Later on when the macro is defined, and the ppu
  579. is opened, the check cannot detect this.
  580. Also, in which macro object should this flag be set ? It cant be set
  581. for macros in the initialmacrosymboltable since this table is shared
  582. between different files.
  583. }
  584. procedure tppumodule.readusedmacros;
  585. var
  586. hs : string;
  587. mac : tmacro;
  588. was_initial,
  589. was_used : boolean;
  590. {Reads macros which was defined or used when the module was compiled.
  591. This is done when a ppu file is open, before it possibly is parsed.}
  592. begin
  593. while not ppufile.endofentry do
  594. begin
  595. hs:=ppufile.getstring;
  596. was_initial:=boolean(ppufile.getbyte);
  597. was_used:=boolean(ppufile.getbyte);
  598. mac:=tmacro(initialmacrosymtable.search(hs));
  599. if assigned(mac) then
  600. begin
  601. {$ifndef EXTDEBUG}
  602. { if we don't have the sources why tell }
  603. if sources_avail then
  604. {$endif ndef EXTDEBUG}
  605. if (not was_initial) and was_used then
  606. Message2(unit_h_cond_not_set_in_last_compile,hs,mainsource^);
  607. end
  608. else { not assigned }
  609. if was_initial and
  610. was_used then
  611. Message2(unit_h_cond_set_in_last_compile,hs,mainsource^);
  612. end;
  613. end;
  614. {$ENDIF}
  615. procedure tppumodule.readsourcefiles;
  616. var
  617. temp,hs : string;
  618. temp_dir : string;
  619. main_dir : string;
  620. found,
  621. is_main : boolean;
  622. orgfiletime,
  623. source_time : longint;
  624. hp : tinputfile;
  625. begin
  626. sources_avail:=true;
  627. is_main:=true;
  628. main_dir:='';
  629. while not ppufile.endofentry do
  630. begin
  631. hs:=ppufile.getstring;
  632. orgfiletime:=ppufile.getlongint;
  633. temp_dir:='';
  634. if (flags and uf_in_library)<>0 then
  635. begin
  636. sources_avail:=false;
  637. temp:=' library';
  638. end
  639. else if pos('Macro ',hs)=1 then
  640. begin
  641. { we don't want to find this file }
  642. { but there is a problem with file indexing !! }
  643. temp:='';
  644. end
  645. else
  646. begin
  647. { check the date of the source files:
  648. 1 path of ppu
  649. 2 path of main source
  650. 3 current dir
  651. 4 include/unit path }
  652. Source_Time:=GetNamedFileTime(path^+hs);
  653. found:=false;
  654. if Source_Time<>-1 then
  655. hs:=path^+hs
  656. else
  657. if not(is_main) then
  658. begin
  659. Source_Time:=GetNamedFileTime(main_dir+hs);
  660. if Source_Time<>-1 then
  661. hs:=main_dir+hs;
  662. end;
  663. if Source_Time=-1 then
  664. Source_Time:=GetNamedFileTime(hs);
  665. if (Source_Time=-1) then
  666. begin
  667. if is_main then
  668. found:=unitsearchpath.FindFile(hs,temp_dir)
  669. else
  670. found:=includesearchpath.FindFile(hs,temp_dir);
  671. if found then
  672. begin
  673. Source_Time:=GetNamedFileTime(temp_dir);
  674. if Source_Time<>-1 then
  675. hs:=temp_dir;
  676. end;
  677. end;
  678. if Source_Time<>-1 then
  679. begin
  680. if is_main then
  681. main_dir:=splitpath(hs);
  682. temp:=' time '+filetimestring(source_time);
  683. if (orgfiletime<>-1) and
  684. (source_time<>orgfiletime) then
  685. begin
  686. if ((flags and uf_release)=0) then
  687. begin
  688. do_compile:=true;
  689. recompile_reason:=rr_sourcenewer;
  690. end
  691. else
  692. Message2(unit_h_source_modified,hs,ppufilename^);
  693. temp:=temp+' *';
  694. end;
  695. end
  696. else
  697. begin
  698. sources_avail:=false;
  699. temp:=' not found';
  700. end;
  701. hp:=tinputfile.create(hs);
  702. { the indexing is wrong here PM }
  703. sourcefiles.register_file(hp);
  704. end;
  705. if is_main then
  706. begin
  707. stringdispose(mainsource);
  708. mainsource:=stringdup(hs);
  709. end;
  710. Message1(unit_u_ppu_source,hs+temp);
  711. is_main:=false;
  712. end;
  713. { check if we want to rebuild every unit, only if the sources are
  714. available }
  715. if do_build and sources_avail and
  716. ((flags and uf_release)=0) then
  717. begin
  718. do_compile:=true;
  719. recompile_reason:=rr_build;
  720. end;
  721. end;
  722. procedure tppumodule.readloadunit;
  723. var
  724. hs : string;
  725. pu : tused_unit;
  726. hp : tppumodule;
  727. intfchecksum,
  728. checksum : cardinal;
  729. begin
  730. while not ppufile.endofentry do
  731. begin
  732. hs:=ppufile.getstring;
  733. checksum:=cardinal(ppufile.getlongint);
  734. intfchecksum:=cardinal(ppufile.getlongint);
  735. { set the state of this unit before registering, this is
  736. needed for a correct circular dependency check }
  737. hp:=registerunit(self,hs,'');
  738. pu:=addusedunit(hp,false,nil);
  739. pu.checksum:=checksum;
  740. pu.interface_checksum:=intfchecksum;
  741. end;
  742. in_interface:=false;
  743. end;
  744. procedure tppumodule.readlinkcontainer(var p:tlinkcontainer);
  745. var
  746. s : string;
  747. m : longint;
  748. begin
  749. while not ppufile.endofentry do
  750. begin
  751. s:=ppufile.getstring;
  752. m:=ppufile.getlongint;
  753. p.add(s,m);
  754. end;
  755. end;
  756. procedure tppumodule.readderefmap;
  757. var
  758. i : longint;
  759. begin
  760. { Load unit map used for resolving }
  761. derefmapsize:=ppufile.getlongint;
  762. getmem(derefmap,derefmapsize*sizeof(tderefmaprec));
  763. fillchar(derefmap^,derefmapsize*sizeof(tderefmaprec),0);
  764. for i:=0 to derefmapsize-1 do
  765. derefmap[i].modulename:=stringdup(ppufile.getstring);
  766. end;
  767. procedure tppumodule.readderefdata;
  768. var
  769. len,hlen : longint;
  770. buf : array[0..1023] of byte;
  771. begin
  772. len:=ppufile.entrysize;
  773. while (len>0) do
  774. begin
  775. if len>1024 then
  776. hlen:=1024
  777. else
  778. hlen:=len;
  779. ppufile.getdata(buf,hlen);
  780. derefdata.write(buf,hlen);
  781. dec(len,hlen);
  782. end;
  783. end;
  784. procedure tppumodule.readasmsymbols;
  785. var
  786. labelnr,
  787. i : longint;
  788. name : string;
  789. bind : TAsmSymBind;
  790. typ : TAsmSymType;
  791. asmsymtype : byte;
  792. begin
  793. librarydata.asmsymbolppuidx:=ppufile.getlongint;
  794. if librarydata.asmsymbolppuidx>0 then
  795. begin
  796. getmem(librarydata.asmsymbolidx,librarydata.asmsymbolppuidx*sizeof(pointer));
  797. fillchar(librarydata.asmsymbolidx^,librarydata.asmsymbolppuidx*sizeof(pointer),0);
  798. for i:=1 to librarydata.asmsymbolppuidx do
  799. begin
  800. asmsymtype:=ppufile.getbyte;
  801. case asmsymtype of
  802. 1 :
  803. name:=ppufile.getstring;
  804. 2..4 :
  805. labelnr:=ppufile.getlongint;
  806. else
  807. internalerror(200208192);
  808. end;
  809. bind:=tasmsymbind(ppufile.getbyte);
  810. typ:=tasmsymtype(ppufile.getbyte);
  811. case asmsymtype of
  812. 1 :
  813. librarydata.asmsymbolidx^[i-1]:=librarydata.newasmsymbol(name,bind,typ);
  814. 2 :
  815. librarydata.asmsymbolidx^[i-1]:=librarydata.newasmlabel(labelnr,false,false);
  816. 3 :
  817. librarydata.asmsymbolidx^[i-1]:=librarydata.newasmlabel(labelnr,false,true);
  818. 4 :
  819. librarydata.asmsymbolidx^[i-1]:=librarydata.newasmlabel(labelnr,true,false);
  820. end;
  821. end;
  822. end;
  823. end;
  824. procedure tppumodule.load_interface;
  825. var
  826. b : byte;
  827. newmodulename : string;
  828. begin
  829. { read interface part }
  830. repeat
  831. b:=ppufile.readentry;
  832. case b of
  833. ibmodulename :
  834. begin
  835. newmodulename:=ppufile.getstring;
  836. if (cs_check_unit_name in aktglobalswitches) and
  837. (upper(newmodulename)<>modulename^) then
  838. Message2(unit_f_unit_name_error,realmodulename^,newmodulename);
  839. stringdispose(modulename);
  840. stringdispose(realmodulename);
  841. modulename:=stringdup(upper(newmodulename));
  842. realmodulename:=stringdup(newmodulename);
  843. end;
  844. ibsourcefiles :
  845. readsourcefiles;
  846. {$IFDEF MACRO_DIFF_HINT}
  847. ibusedmacros :
  848. readusedmacros;
  849. {$ENDIF}
  850. ibloadunit :
  851. readloadunit;
  852. iblinkunitofiles :
  853. readlinkcontainer(LinkUnitOFiles);
  854. iblinkunitstaticlibs :
  855. readlinkcontainer(LinkUnitStaticLibs);
  856. iblinkunitsharedlibs :
  857. readlinkcontainer(LinkUnitSharedLibs);
  858. iblinkotherofiles :
  859. readlinkcontainer(LinkotherOFiles);
  860. iblinkotherstaticlibs :
  861. readlinkcontainer(LinkotherStaticLibs);
  862. iblinkothersharedlibs :
  863. readlinkcontainer(LinkotherSharedLibs);
  864. ibderefmap :
  865. readderefmap;
  866. ibderefdata :
  867. readderefdata;
  868. ibendinterface :
  869. break;
  870. else
  871. Message1(unit_f_ppu_invalid_entry,tostr(b));
  872. end;
  873. { we can already stop when we know that we must recompile }
  874. if do_compile then
  875. exit;
  876. until false;
  877. end;
  878. procedure tppumodule.load_implementation;
  879. var
  880. b : byte;
  881. begin
  882. { read implementation part }
  883. repeat
  884. b:=ppufile.readentry;
  885. case b of
  886. ibloadunit :
  887. readloadunit;
  888. ibasmsymbols :
  889. readasmsymbols;
  890. ibendimplementation :
  891. break;
  892. else
  893. Message1(unit_f_ppu_invalid_entry,tostr(b));
  894. end;
  895. until false;
  896. end;
  897. procedure tppumodule.load_symtable_refs;
  898. var
  899. b : byte;
  900. i : longint;
  901. begin
  902. if (flags and uf_has_browser)<>0 then
  903. begin
  904. tstoredsymtable(globalsymtable).load_references(ppufile,true);
  905. for i:=0 to unitmapsize-1 do
  906. tstoredsymtable(globalsymtable).load_references(ppufile,false);
  907. b:=ppufile.readentry;
  908. if b<>ibendbrowser then
  909. Message1(unit_f_ppu_invalid_entry,tostr(b));
  910. end;
  911. if ((flags and uf_local_browser)<>0) then
  912. tstaticsymtable(localsymtable).load_references(ppufile,true);
  913. end;
  914. procedure tppumodule.writeppu;
  915. var
  916. pu : tused_unit;
  917. begin
  918. Message1(unit_u_ppu_write,realmodulename^);
  919. { create unit flags }
  920. {$ifdef GDB}
  921. if cs_gdb_dbx in aktglobalswitches then
  922. flags:=flags or uf_has_dbx;
  923. {$endif GDB}
  924. if cs_browser in aktmoduleswitches then
  925. flags:=flags or uf_has_browser;
  926. if cs_local_browser in aktmoduleswitches then
  927. flags:=flags or uf_local_browser;
  928. if do_release then
  929. flags:=flags or uf_release;
  930. if assigned(localsymtable) then
  931. flags:=flags or uf_local_symtable;
  932. {$ifdef cpufpemu}
  933. if (cs_fp_emulation in aktmoduleswitches) then
  934. flags:=flags or uf_fpu_emulation;
  935. {$endif cpufpemu}
  936. {$ifdef Test_Double_checksum_write}
  937. Assign(CRCFile,s+'.IMP');
  938. Rewrite(CRCFile);
  939. {$endif def Test_Double_checksum_write}
  940. { create new ppufile }
  941. ppufile:=tcompilerppufile.create(ppufilename^);
  942. if not ppufile.createfile then
  943. Message(unit_f_ppu_cannot_write);
  944. { first the unitname }
  945. ppufile.putstring(realmodulename^);
  946. ppufile.writeentry(ibmodulename);
  947. writesourcefiles;
  948. {$IFDEF MACRO_DIFF_HINT}
  949. writeusedmacros;
  950. {$ENDIF}
  951. { write interface uses }
  952. writeusedunit(true);
  953. { write the objectfiles and libraries that come for this unit,
  954. preserve the containers becuase they are still needed to load
  955. the link.res. All doesn't depend on the crc! It doesn't matter
  956. if a unit is in a .o or .a file }
  957. ppufile.do_crc:=false;
  958. writelinkcontainer(linkunitofiles,iblinkunitofiles,true);
  959. writelinkcontainer(linkunitstaticlibs,iblinkunitstaticlibs,true);
  960. writelinkcontainer(linkunitsharedlibs,iblinkunitsharedlibs,true);
  961. writelinkcontainer(linkotherofiles,iblinkotherofiles,false);
  962. writelinkcontainer(linkotherstaticlibs,iblinkotherstaticlibs,true);
  963. writelinkcontainer(linkothersharedlibs,iblinkothersharedlibs,true);
  964. ppufile.do_crc:=true;
  965. { generate implementation deref data, the interface deref data is
  966. already generated when calculating the interface crc }
  967. if (cs_compilesystem in aktmoduleswitches) then
  968. begin
  969. tstoredsymtable(globalsymtable).buildderef;
  970. derefdataintflen:=derefdata.size;
  971. end;
  972. tstoredsymtable(globalsymtable).buildderefimpl;
  973. if (flags and uf_local_symtable)<>0 then
  974. begin
  975. tstoredsymtable(localsymtable).buildderef;
  976. tstoredsymtable(localsymtable).buildderefimpl;
  977. end;
  978. writederefmap;
  979. writederefdata;
  980. ppufile.writeentry(ibendinterface);
  981. { write the symtable entries }
  982. tstoredsymtable(globalsymtable).ppuwrite(ppufile);
  983. if assigned(globalmacrosymtable) and (globalmacrosymtable.symindex.count > 0) then
  984. begin
  985. ppufile.putbyte(byte(true));
  986. ppufile.writeentry(ibexportedmacros);
  987. tstoredsymtable(globalmacrosymtable).ppuwrite(ppufile);
  988. end
  989. else
  990. begin
  991. ppufile.putbyte(byte(false));
  992. ppufile.writeentry(ibexportedmacros);
  993. end;
  994. { everything after this doesn't affect the crc }
  995. ppufile.do_crc:=false;
  996. { write implementation uses }
  997. writeusedunit(false);
  998. { write asmsymbols }
  999. writeasmsymbols;
  1000. { end of implementation }
  1001. ppufile.writeentry(ibendimplementation);
  1002. { write static symtable
  1003. needed for local debugging of unit functions }
  1004. if (flags and uf_local_symtable)<>0 then
  1005. tstoredsymtable(localsymtable).ppuwrite(ppufile);
  1006. { write all browser section }
  1007. if (flags and uf_has_browser)<>0 then
  1008. begin
  1009. tstoredsymtable(globalsymtable).write_references(ppufile,true);
  1010. pu:=tused_unit(used_units.first);
  1011. while assigned(pu) do
  1012. begin
  1013. tstoredsymtable(pu.u.globalsymtable).write_references(ppufile,false);
  1014. pu:=tused_unit(pu.next);
  1015. end;
  1016. ppufile.writeentry(ibendbrowser);
  1017. end;
  1018. if ((flags and uf_local_browser)<>0) then
  1019. begin
  1020. if not assigned(localsymtable) then
  1021. internalerror(200408271);
  1022. tstaticsymtable(localsymtable).write_references(ppufile,true);
  1023. end;
  1024. { the last entry ibend is written automaticly }
  1025. { flush to be sure }
  1026. ppufile.flush;
  1027. { create and write header }
  1028. ppufile.header.size:=ppufile.size;
  1029. ppufile.header.checksum:=ppufile.crc;
  1030. ppufile.header.interface_checksum:=ppufile.interface_crc;
  1031. ppufile.header.compiler:=wordversion;
  1032. ppufile.header.cpu:=word(target_cpu);
  1033. ppufile.header.target:=word(target_info.system);
  1034. ppufile.header.flags:=flags;
  1035. ppufile.writeheader;
  1036. { save crc in current module also }
  1037. crc:=ppufile.crc;
  1038. interface_crc:=ppufile.interface_crc;
  1039. {$ifdef Test_Double_checksum_write}
  1040. close(CRCFile);
  1041. {$endif Test_Double_checksum_write}
  1042. ppufile.closefile;
  1043. ppufile.free;
  1044. ppufile:=nil;
  1045. end;
  1046. procedure tppumodule.getppucrc;
  1047. begin
  1048. {$ifdef Test_Double_checksum_write}
  1049. Assign(CRCFile,s+'.INT')
  1050. Rewrite(CRCFile);
  1051. {$endif def Test_Double_checksum_write}
  1052. { create new ppufile }
  1053. ppufile:=tcompilerppufile.create(ppufilename^);
  1054. ppufile.crc_only:=true;
  1055. if not ppufile.createfile then
  1056. Message(unit_f_ppu_cannot_write);
  1057. { first the unitname }
  1058. ppufile.putstring(realmodulename^);
  1059. ppufile.writeentry(ibmodulename);
  1060. { the interface units affect the crc }
  1061. writeusedunit(true);
  1062. { deref data of interface that affect the crc }
  1063. derefdata.reset;
  1064. tstoredsymtable(globalsymtable).buildderef;
  1065. derefdataintflen:=derefdata.size;
  1066. writederefmap;
  1067. writederefdata;
  1068. ppufile.writeentry(ibendinterface);
  1069. { write the symtable entries }
  1070. tstoredsymtable(globalsymtable).ppuwrite(ppufile);
  1071. if assigned(globalmacrosymtable) and (globalmacrosymtable.symindex.count > 0) then
  1072. begin
  1073. ppufile.putbyte(byte(true));
  1074. ppufile.writeentry(ibexportedmacros);
  1075. tstoredsymtable(globalmacrosymtable).ppuwrite(ppufile);
  1076. end
  1077. else
  1078. begin
  1079. ppufile.putbyte(byte(false));
  1080. ppufile.writeentry(ibexportedmacros);
  1081. end;
  1082. { save crc }
  1083. crc:=ppufile.crc;
  1084. interface_crc:=ppufile.interface_crc;
  1085. { end of implementation, to generate a correct ppufile
  1086. for ppudump when using INTFPPU define }
  1087. ppufile.writeentry(ibendimplementation);
  1088. {$ifdef Test_Double_checksum}
  1089. crc_array:=ppufile.crc_test;
  1090. ppufile.crc_test:=nil;
  1091. crc_size:=ppufile.crc_index2;
  1092. crc_array2:=ppufile.crc_test2;
  1093. ppufile.crc_test2:=nil;
  1094. crc_size2:=ppufile.crc_index2;
  1095. {$endif Test_Double_checksum}
  1096. {$ifdef Test_Double_checksum_write}
  1097. close(CRCFile);
  1098. {$endif Test_Double_checksum_write}
  1099. { create and write header, this will only be used
  1100. for debugging purposes }
  1101. ppufile.header.size:=ppufile.size;
  1102. ppufile.header.checksum:=ppufile.crc;
  1103. ppufile.header.interface_checksum:=ppufile.interface_crc;
  1104. ppufile.header.compiler:=wordversion;
  1105. ppufile.header.cpu:=word(target_cpu);
  1106. ppufile.header.target:=word(target_info.system);
  1107. ppufile.header.flags:=flags;
  1108. ppufile.writeheader;
  1109. ppufile.closefile;
  1110. ppufile.free;
  1111. ppufile:=nil;
  1112. end;
  1113. procedure tppumodule.load_usedunits;
  1114. var
  1115. pu : tused_unit;
  1116. load_refs : boolean;
  1117. oldobjectlibrary : tasmlibrarydata;
  1118. begin
  1119. if current_module<>self then
  1120. internalerror(200212284);
  1121. load_refs:=true;
  1122. { load the used units from interface }
  1123. in_interface:=true;
  1124. pu:=tused_unit(used_units.first);
  1125. while assigned(pu) do
  1126. begin
  1127. if pu.in_interface then
  1128. begin
  1129. tppumodule(pu.u).loadppu;
  1130. { if this unit is compiled we can stop }
  1131. if state=ms_compiled then
  1132. exit;
  1133. { add this unit to the dependencies }
  1134. pu.u.adddependency(self);
  1135. { need to recompile the current unit, check the interface
  1136. crc. And when not compiled with -Ur then check the complete
  1137. crc }
  1138. if (pu.u.interface_crc<>pu.interface_checksum) or
  1139. (
  1140. ((ppufile.header.flags and uf_release)=0) and
  1141. (pu.u.crc<>pu.checksum)
  1142. ) then
  1143. begin
  1144. Message2(unit_u_recompile_crc_change,realmodulename^,pu.u.realmodulename^);
  1145. recompile_reason:=rr_crcchanged;
  1146. do_compile:=true;
  1147. exit;
  1148. end;
  1149. end;
  1150. pu:=tused_unit(pu.next);
  1151. end;
  1152. { ok, now load the interface of this unit }
  1153. if current_module<>self then
  1154. internalerror(200208187);
  1155. globalsymtable:=tglobalsymtable.create(modulename^,moduleid);
  1156. tstoredsymtable(globalsymtable).ppuload(ppufile);
  1157. if ppufile.readentry<>ibexportedmacros then
  1158. Message(unit_f_ppu_read_error);
  1159. if boolean(ppufile.getbyte) then
  1160. begin
  1161. globalmacrosymtable:=tmacrosymtable.Create(true);
  1162. tstoredsymtable(globalmacrosymtable).ppuload(ppufile)
  1163. end;
  1164. interface_compiled:=true;
  1165. { read the implementation part, containing
  1166. the implementation uses and objectdata }
  1167. in_interface:=false;
  1168. load_implementation;
  1169. { now only read the implementation uses }
  1170. pu:=tused_unit(used_units.first);
  1171. while assigned(pu) do
  1172. begin
  1173. if (not pu.in_interface) then
  1174. begin
  1175. tppumodule(pu.u).loadppu;
  1176. { if this unit is compiled we can stop }
  1177. if state=ms_compiled then
  1178. exit;
  1179. { add this unit to the dependencies }
  1180. pu.u.adddependency(self);
  1181. { need to recompile the current unit ? }
  1182. if (pu.u.interface_crc<>pu.interface_checksum) then
  1183. begin
  1184. Message2(unit_u_recompile_crc_change,realmodulename^,pu.u.realmodulename^+' {impl}');
  1185. recompile_reason:=rr_crcchanged;
  1186. do_compile:=true;
  1187. exit;
  1188. end;
  1189. end;
  1190. pu:=tused_unit(pu.next);
  1191. end;
  1192. { load implementation symtable }
  1193. if (flags and uf_local_symtable)<>0 then
  1194. begin
  1195. localsymtable:=tstaticsymtable.create(modulename^,moduleid);
  1196. tstaticsymtable(localsymtable).ppuload(ppufile);
  1197. end;
  1198. { we can now derefence all pointers to the implementation parts }
  1199. oldobjectlibrary:=objectlibrary;
  1200. objectlibrary:=librarydata;
  1201. tstoredsymtable(globalsymtable).derefimpl;
  1202. if assigned(localsymtable) then
  1203. tstoredsymtable(localsymtable).derefimpl;
  1204. objectlibrary:=oldobjectlibrary;
  1205. { load browser info if stored }
  1206. if ((flags and uf_has_browser)<>0) and load_refs then
  1207. begin
  1208. if current_module<>self then
  1209. internalerror(200208188);
  1210. load_symtable_refs;
  1211. end;
  1212. end;
  1213. function tppumodule.needrecompile:boolean;
  1214. var
  1215. pu : tused_unit;
  1216. begin
  1217. result:=false;
  1218. pu:=tused_unit(used_units.first);
  1219. while assigned(pu) do
  1220. begin
  1221. { need to recompile the current unit, check the interface
  1222. crc. And when not compiled with -Ur then check the complete
  1223. crc }
  1224. if (pu.u.interface_crc<>pu.interface_checksum) or
  1225. (
  1226. (pu.in_interface) and
  1227. (pu.u.crc<>pu.checksum)
  1228. ) then
  1229. begin
  1230. result:=true;
  1231. exit;
  1232. end;
  1233. pu:=tused_unit(pu.next);
  1234. end;
  1235. end;
  1236. procedure tppumodule.loadppu;
  1237. const
  1238. ImplIntf : array[boolean] of string[15]=('implementation','interface');
  1239. var
  1240. do_load,
  1241. second_time : boolean;
  1242. old_current_module : tmodule;
  1243. begin
  1244. old_current_module:=current_module;
  1245. Message3(unit_u_load_unit,old_current_module.modulename^,
  1246. ImplIntf[old_current_module.in_interface],
  1247. modulename^);
  1248. { Update loaded_from to detect cycles }
  1249. loaded_from:=old_current_module;
  1250. { check if the globalsymtable is already available, but
  1251. we must reload when the do_reload flag is set }
  1252. if (not do_reload) and
  1253. assigned(globalsymtable) then
  1254. exit;
  1255. { reset }
  1256. do_load:=true;
  1257. second_time:=false;
  1258. current_module:=self;
  1259. SetCompileModule(current_module);
  1260. Fillchar(aktfilepos,0,sizeof(aktfilepos));
  1261. { A force reload }
  1262. if do_reload then
  1263. begin
  1264. Message(unit_u_forced_reload);
  1265. do_reload:=false;
  1266. { When the unit is already loaded or being loaded
  1267. we can maybe skip a complete reload/recompile }
  1268. if assigned(globalsymtable) and
  1269. (not needrecompile) then
  1270. begin
  1271. { When we don't have any data stored yet there
  1272. is nothing to resolve }
  1273. if interface_compiled then
  1274. begin
  1275. Message1(unit_u_reresolving_unit,modulename^);
  1276. tstoredsymtable(globalsymtable).deref;
  1277. tstoredsymtable(globalsymtable).derefimpl;
  1278. if assigned(localsymtable) then
  1279. begin
  1280. tstoredsymtable(localsymtable).deref;
  1281. tstoredsymtable(localsymtable).derefimpl;
  1282. end;
  1283. end
  1284. else
  1285. Message1(unit_u_skipping_reresolving_unit,modulename^);
  1286. do_load:=false;
  1287. end;
  1288. end;
  1289. if do_load then
  1290. begin
  1291. { we are loading a new module, save the state of the scanner
  1292. and reset scanner+module }
  1293. if assigned(current_scanner) then
  1294. current_scanner.tempcloseinputfile;
  1295. current_scanner:=nil;
  1296. { loading the unit for a second time? }
  1297. if state=ms_registered then
  1298. state:=ms_load
  1299. else
  1300. begin
  1301. { try to load the unit a second time first }
  1302. Message1(unit_u_second_load_unit,modulename^);
  1303. Message2(unit_u_previous_state,modulename^,ModuleStateStr[state]);
  1304. { Flag modules to reload }
  1305. flagdependent(old_current_module);
  1306. { Reset the module }
  1307. reset;
  1308. if state in [ms_compile,ms_second_compile] then
  1309. begin
  1310. Message1(unit_u_second_compile_unit,modulename^);
  1311. state:=ms_second_compile;
  1312. do_compile:=true;
  1313. end
  1314. else
  1315. state:=ms_second_load;
  1316. second_time:=true;
  1317. end;
  1318. { close old_current_ppu on system that are
  1319. short on file handles like DOS PM }
  1320. {$ifdef SHORT_ON_FILE_HANDLES}
  1321. if old_current_module.is_unit and
  1322. assigned(tppumodule(old_current_module).ppufile) then
  1323. tppumodule(old_current_module).ppufile.tempclose;
  1324. {$endif SHORT_ON_FILE_HANDLES}
  1325. { try to opening ppu, skip this when we already
  1326. know that we need to compile the unit }
  1327. if not do_compile then
  1328. begin
  1329. Message1(unit_u_loading_unit,modulename^);
  1330. search_unit(false,false);
  1331. if not do_compile then
  1332. begin
  1333. load_interface;
  1334. if not do_compile then
  1335. begin
  1336. load_usedunits;
  1337. if not do_compile then
  1338. Message1(unit_u_finished_loading_unit,modulename^);
  1339. end;
  1340. end;
  1341. { PPU is not needed anymore }
  1342. if assigned(ppufile) then
  1343. begin
  1344. ppufile.closefile;
  1345. ppufile.free;
  1346. ppufile:=nil;
  1347. end;
  1348. end;
  1349. { Do we need to recompile the unit }
  1350. if do_compile then
  1351. begin
  1352. { recompile the unit or give a fatal error if sources not available }
  1353. if not(sources_avail) then
  1354. begin
  1355. if (not search_unit(true,false)) and
  1356. (length(modulename^)>8) then
  1357. search_unit(true,true);
  1358. if not(sources_avail) then
  1359. begin
  1360. if recompile_reason=rr_noppu then
  1361. Message1(unit_f_cant_find_ppu,realmodulename^)
  1362. else
  1363. Message1(unit_f_cant_compile_unit,realmodulename^);
  1364. end;
  1365. end;
  1366. { Flag modules to reload }
  1367. flagdependent(old_current_module);
  1368. { Reset the module }
  1369. reset;
  1370. { compile this module }
  1371. if not(state in [ms_compile,ms_second_compile]) then
  1372. state:=ms_compile;
  1373. compile(mainsource^);
  1374. end
  1375. else
  1376. state:=ms_compiled;
  1377. if current_module<>self then
  1378. internalerror(200212282);
  1379. if in_interface then
  1380. internalerror(200212283);
  1381. { for a second_time recompile reload all dependent units,
  1382. for a first time compile register the unit _once_ }
  1383. if second_time then
  1384. reload_flagged_units
  1385. else
  1386. usedunits.concat(tused_unit.create(self,true,false,nil));
  1387. { reopen the old module }
  1388. {$ifdef SHORT_ON_FILE_HANDLES}
  1389. if old_current_module.is_unit and
  1390. assigned(tppumodule(old_current_module).ppufile) then
  1391. tppumodule(old_current_module).ppufile.tempopen;
  1392. {$endif SHORT_ON_FILE_HANDLES}
  1393. { reload old scanner }
  1394. current_scanner:=tscannerfile(old_current_module.scanner);
  1395. if assigned(current_scanner) then
  1396. begin
  1397. current_scanner.tempopeninputfile;
  1398. current_scanner.gettokenpos
  1399. end
  1400. else
  1401. fillchar(aktfilepos,sizeof(aktfilepos),0);
  1402. end;
  1403. { we are back, restore current_module }
  1404. current_module:=old_current_module;
  1405. SetCompileModule(current_module);
  1406. end;
  1407. {*****************************************************************************
  1408. RegisterUnit
  1409. *****************************************************************************}
  1410. function registerunit(callermodule:tmodule;const s : stringid;const fn:string) : tppumodule;
  1411. var
  1412. ups : stringid;
  1413. hp : tppumodule;
  1414. hp2 : tmodule;
  1415. begin
  1416. { Info }
  1417. ups:=upper(s);
  1418. { search all loaded units }
  1419. hp:=tppumodule(loaded_units.first);
  1420. while assigned(hp) do
  1421. begin
  1422. if hp.modulename^=ups then
  1423. begin
  1424. { only check for units. The main program is also
  1425. as a unit in the loaded_units list. We simply need
  1426. to ignore this entry (PFV) }
  1427. if hp.is_unit then
  1428. begin
  1429. { both units in interface ? }
  1430. if callermodule.in_interface and
  1431. hp.in_interface then
  1432. begin
  1433. { check for a cycle }
  1434. hp2:=callermodule.loaded_from;
  1435. while assigned(hp2) and (hp2<>hp) do
  1436. begin
  1437. if hp2.in_interface then
  1438. hp2:=hp2.loaded_from
  1439. else
  1440. hp2:=nil;
  1441. end;
  1442. if assigned(hp2) then
  1443. Message2(unit_f_circular_unit_reference,callermodule.realmodulename^,hp.realmodulename^);
  1444. end;
  1445. break;
  1446. end;
  1447. end;
  1448. { the next unit }
  1449. hp:=tppumodule(hp.next);
  1450. end;
  1451. { the unit is not in the loaded units,
  1452. we create an entry and register the unit }
  1453. if not assigned(hp) then
  1454. begin
  1455. Message1(unit_u_registering_new_unit,Upper(s));
  1456. hp:=tppumodule.create(callermodule,s,fn,true);
  1457. hp.loaded_from:=callermodule;
  1458. addloadedunit(hp);
  1459. end;
  1460. { return }
  1461. registerunit:=hp;
  1462. end;
  1463. end.