fppu.pas 52 KB

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