fppu.pas 51 KB

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