fppu.pas 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702
  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. procedure setdefgeneration;
  53. procedure reload_flagged_units;
  54. private
  55. { Each time a unit's defs are (re)created, its defsgeneration is
  56. set to the value of a global counter, and the global counter is
  57. increased. We only reresolve its dependent units' defs in case
  58. they have been resolved only for an older generation, in order to
  59. avoid endless resolving loops in case of cyclic dependencies. }
  60. defsgeneration : longint;
  61. function search_unit(onlysource,shortname:boolean):boolean;
  62. procedure load_interface;
  63. procedure load_implementation;
  64. procedure load_usedunits;
  65. procedure printcomments;
  66. procedure queuecomment(s:string;v,w:longint);
  67. procedure writesourcefiles;
  68. procedure writeusedunit(intf:boolean);
  69. procedure writelinkcontainer(var p:tlinkcontainer;id:byte;strippath:boolean);
  70. procedure writederefmap;
  71. procedure writederefdata;
  72. procedure writeImportSymbols;
  73. procedure writeResources;
  74. procedure readsourcefiles;
  75. procedure readloadunit;
  76. procedure readlinkcontainer(var p:tlinkcontainer);
  77. procedure readderefmap;
  78. procedure readderefdata;
  79. procedure readImportSymbols;
  80. procedure readResources;
  81. procedure readwpofile;
  82. {$IFDEF MACRO_DIFF_HINT}
  83. procedure writeusedmacro(p:TNamedIndexItem;arg:pointer);
  84. procedure writeusedmacros;
  85. procedure readusedmacros;
  86. {$ENDIF}
  87. end;
  88. function registerunit(callermodule:tmodule;const s : TIDString;const fn:string) : tppumodule;
  89. implementation
  90. uses
  91. SysUtils,
  92. cfileutl,
  93. verbose,systems,version,
  94. symtable, symsym,
  95. wpoinfo,
  96. scanner,
  97. aasmbase,ogbase,
  98. parser,
  99. comphook;
  100. var
  101. currentdefgeneration: longint;
  102. {****************************************************************************
  103. TPPUMODULE
  104. ****************************************************************************}
  105. constructor tppumodule.create(LoadedFrom:TModule;const s:string;const fn:string;_is_unit:boolean);
  106. begin
  107. inherited create(LoadedFrom,s,_is_unit);
  108. ppufile:=nil;
  109. sourcefn:=stringdup(fn);
  110. end;
  111. destructor tppumodule.Destroy;
  112. begin
  113. if assigned(ppufile) then
  114. ppufile.free;
  115. ppufile:=nil;
  116. comments.free;
  117. comments:=nil;
  118. stringdispose(sourcefn);
  119. inherited Destroy;
  120. end;
  121. procedure tppumodule.reset;
  122. begin
  123. inc(currentdefgeneration);
  124. if assigned(ppufile) then
  125. begin
  126. ppufile.free;
  127. ppufile:=nil;
  128. end;
  129. inherited reset;
  130. end;
  131. procedure tppumodule.queuecomment(s:string;v,w:longint);
  132. begin
  133. if comments = nil then
  134. comments := TCmdStrList.create;
  135. comments.insert(s);
  136. end;
  137. procedure tppumodule.printcomments;
  138. var
  139. comment: string;
  140. begin
  141. if comments = nil then
  142. exit;
  143. { comments are inserted in reverse order }
  144. repeat
  145. comment := comments.getlast;
  146. if length(comment) = 0 then
  147. exit;
  148. do_comment(v_normal, comment);
  149. until false;
  150. end;
  151. function tppumodule.openppu:boolean;
  152. var
  153. ppufiletime : longint;
  154. begin
  155. openppu:=false;
  156. Message1(unit_t_ppu_loading,ppufilename^,@queuecomment);
  157. { Get ppufile time (also check if the file exists) }
  158. ppufiletime:=getnamedfiletime(ppufilename^);
  159. if ppufiletime=-1 then
  160. exit;
  161. { Open the ppufile }
  162. Message1(unit_u_ppu_name,ppufilename^);
  163. ppufile:=tcompilerppufile.create(ppufilename^);
  164. if not ppufile.openfile then
  165. begin
  166. ppufile.free;
  167. ppufile:=nil;
  168. Message(unit_u_ppu_file_too_short);
  169. exit;
  170. end;
  171. { check for a valid PPU file }
  172. if not ppufile.CheckPPUId then
  173. begin
  174. ppufile.free;
  175. ppufile:=nil;
  176. Message(unit_u_ppu_invalid_header);
  177. exit;
  178. end;
  179. { check for allowed PPU versions }
  180. if not (ppufile.GetPPUVersion = CurrentPPUVersion) then
  181. begin
  182. Message1(unit_u_ppu_invalid_version,tostr(ppufile.GetPPUVersion),@queuecomment);
  183. ppufile.free;
  184. ppufile:=nil;
  185. exit;
  186. end;
  187. { check the target processor }
  188. if tsystemcpu(ppufile.header.cpu)<>target_cpu then
  189. begin
  190. ppufile.free;
  191. ppufile:=nil;
  192. Message(unit_u_ppu_invalid_processor,@queuecomment);
  193. exit;
  194. end;
  195. { check target }
  196. if tsystem(ppufile.header.target)<>target_info.system then
  197. begin
  198. ppufile.free;
  199. ppufile:=nil;
  200. Message(unit_u_ppu_invalid_target,@queuecomment);
  201. exit;
  202. end;
  203. {$ifdef cpufpemu}
  204. { check if floating point emulation is on?
  205. fpu emulation isn't unit levelwise because it affects calling convention }
  206. if ((ppufile.header.flags and uf_fpu_emulation)<>0) xor
  207. (cs_fp_emulation in current_settings.moduleswitches) then
  208. begin
  209. ppufile.free;
  210. ppufile:=nil;
  211. Message(unit_u_ppu_invalid_fpumode,@queuecomment);
  212. exit;
  213. end;
  214. {$endif cpufpemu}
  215. { Load values to be access easier }
  216. flags:=ppufile.header.flags;
  217. crc:=ppufile.header.checksum;
  218. interface_crc:=ppufile.header.interface_checksum;
  219. indirect_crc:=ppufile.header.indirect_checksum;
  220. { Show Debug info }
  221. Message1(unit_u_ppu_time,filetimestring(ppufiletime));
  222. Message1(unit_u_ppu_flags,tostr(flags));
  223. Message1(unit_u_ppu_crc,hexstr(ppufile.header.checksum,8));
  224. Message1(unit_u_ppu_crc,hexstr(ppufile.header.interface_checksum,8)+' (intfc)');
  225. Message1(unit_u_ppu_crc,hexstr(ppufile.header.indirect_checksum,8)+' (indc)');
  226. Comment(V_used,'Number of definitions: '+tostr(ppufile.header.deflistsize));
  227. Comment(V_used,'Number of symbols: '+tostr(ppufile.header.symlistsize));
  228. do_compile:=false;
  229. openppu:=true;
  230. end;
  231. function tppumodule.search_unit(onlysource,shortname:boolean):boolean;
  232. var
  233. singlepathstring,
  234. filename : TCmdStr;
  235. Function UnitExists(const ext:string;var foundfile:TCmdStr):boolean;
  236. begin
  237. if CheckVerbosity(V_Tried) then
  238. Message1(unit_t_unitsearch,Singlepathstring+filename+ext);
  239. UnitExists:=FindFile(FileName+ext,Singlepathstring,true,foundfile);
  240. end;
  241. Function PPUSearchPath(const s:TCmdStr):boolean;
  242. var
  243. found : boolean;
  244. hs : TCmdStr;
  245. begin
  246. Found:=false;
  247. singlepathstring:=FixPath(s,false);
  248. { Check for PPU file }
  249. Found:=UnitExists(target_info.unitext,hs);
  250. if Found then
  251. Begin
  252. SetFileName(hs,false);
  253. Found:=OpenPPU;
  254. End;
  255. PPUSearchPath:=Found;
  256. end;
  257. Function SourceSearchPath(const s:TCmdStr):boolean;
  258. var
  259. found : boolean;
  260. hs : TCmdStr;
  261. begin
  262. Found:=false;
  263. singlepathstring:=FixPath(s,false);
  264. { Check for Sources }
  265. ppufile:=nil;
  266. do_compile:=true;
  267. recompile_reason:=rr_noppu;
  268. {Check for .pp file}
  269. Found:=UnitExists(sourceext,hs);
  270. if not Found then
  271. begin
  272. { Check for .pas }
  273. Found:=UnitExists(pasext,hs);
  274. end;
  275. if not Found and (m_mac in current_settings.modeswitches) then
  276. begin
  277. { Check for .p, if mode is macpas}
  278. Found:=UnitExists(pext,hs);
  279. end;
  280. stringdispose(mainsource);
  281. if Found then
  282. begin
  283. sources_avail:=true;
  284. { Load Filenames when found }
  285. mainsource:=StringDup(hs);
  286. SetFileName(hs,false);
  287. end
  288. else
  289. sources_avail:=false;
  290. SourceSearchPath:=Found;
  291. end;
  292. Function SearchPath(const s:TCmdStr):boolean;
  293. var
  294. found : boolean;
  295. begin
  296. { First check for a ppu, then for the source }
  297. found:=false;
  298. if not onlysource then
  299. found:=PPUSearchPath(s);
  300. if not found then
  301. found:=SourceSearchPath(s);
  302. SearchPath:=found;
  303. end;
  304. Function SearchPathList(list:TSearchPathList):boolean;
  305. var
  306. hp : TCmdStrListItem;
  307. found : boolean;
  308. begin
  309. found:=false;
  310. hp:=TCmdStrListItem(list.First);
  311. while assigned(hp) do
  312. begin
  313. found:=SearchPath(hp.Str);
  314. if found then
  315. break;
  316. hp:=TCmdStrListItem(hp.next);
  317. end;
  318. SearchPathList:=found;
  319. end;
  320. var
  321. fnd : boolean;
  322. hs : TCmdStr;
  323. begin
  324. if shortname then
  325. filename:=FixFileName(Copy(realmodulename^,1,8))
  326. else
  327. filename:=FixFileName(realmodulename^);
  328. { try to find unit
  329. 1. look for ppu in cwd
  330. 2. look for ppu in outputpath if set, this is tp7 compatible (PFV)
  331. 3. look for ppu in maindir
  332. 4. look for the specified source file (from the uses line)
  333. 5. look for source in cwd
  334. 6. look for source in maindir
  335. 7. local unit pathlist
  336. 8. global unit pathlist }
  337. fnd:=false;
  338. if not onlysource then
  339. begin
  340. fnd:=PPUSearchPath('.');
  341. if (not fnd) and (outputpath^<>'') then
  342. fnd:=PPUSearchPath(outputpath^);
  343. if (not fnd) and Assigned(main_module) and (main_module.Path^<>'') then
  344. fnd:=PPUSearchPath(main_module.Path^);
  345. end;
  346. if (not fnd) and (sourcefn^<>'') then
  347. begin
  348. { the full filename is specified so we can't use here the
  349. searchpath (PFV) }
  350. if CheckVerbosity(V_Tried) then
  351. Message1(unit_t_unitsearch,ChangeFileExt(sourcefn^,sourceext));
  352. fnd:=FindFile(ChangeFileExt(sourcefn^,sourceext),'',true,hs);
  353. if not fnd then
  354. begin
  355. if CheckVerbosity(V_Tried) then
  356. Message1(unit_t_unitsearch,ChangeFileExt(sourcefn^,pasext));
  357. fnd:=FindFile(ChangeFileExt(sourcefn^,pasext),'',true,hs);
  358. end;
  359. if not fnd and ((m_mac in current_settings.modeswitches) or (tf_p_ext_support in target_info.flags)) then
  360. begin
  361. if CheckVerbosity(V_Tried) then
  362. Message1(unit_t_unitsearch,ChangeFileExt(sourcefn^,pext));
  363. fnd:=FindFile(ChangeFileExt(sourcefn^,pext),'',true,hs);
  364. end;
  365. if fnd then
  366. begin
  367. sources_avail:=true;
  368. do_compile:=true;
  369. recompile_reason:=rr_noppu;
  370. stringdispose(mainsource);
  371. mainsource:=StringDup(hs);
  372. SetFileName(hs,false);
  373. end;
  374. end;
  375. if not fnd then
  376. fnd:=SourceSearchPath('.');
  377. if (not fnd) and Assigned(main_module) and (main_module.Path^<>'') then
  378. fnd:=SourceSearchPath(main_module.Path^);
  379. if (not fnd) and Assigned(loaded_from) then
  380. fnd:=SearchPathList(loaded_from.LocalUnitSearchPath);
  381. if not fnd then
  382. fnd:=SearchPathList(UnitSearchPath);
  383. { try to find a file with the first 8 chars of the modulename, like
  384. dos }
  385. if (not fnd) and (length(filename)>8) then
  386. begin
  387. filename:=copy(filename,1,8);
  388. fnd:=SearchPath('.');
  389. if (not fnd) then
  390. fnd:=SearchPathList(LocalUnitSearchPath);
  391. if not fnd then
  392. fnd:=SearchPathList(UnitSearchPath);
  393. end;
  394. search_unit:=fnd;
  395. end;
  396. {**********************************
  397. PPU Reading/Writing Helpers
  398. ***********************************}
  399. {$IFDEF MACRO_DIFF_HINT}
  400. var
  401. is_initial: Boolean;
  402. procedure tppumodule.writeusedmacro(p:TNamedIndexItem;arg:pointer);
  403. begin
  404. if tmacro(p).is_used or is_initial then
  405. begin
  406. ppufile.putstring(p.name);
  407. ppufile.putbyte(byte(is_initial));
  408. ppufile.putbyte(byte(tmacro(p).is_used));
  409. end;
  410. end;
  411. procedure tppumodule.writeusedmacros;
  412. begin
  413. ppufile.do_crc:=false;
  414. is_initial:= true;
  415. initialmacrosymtable.foreach(@writeusedmacro,nil);
  416. is_initial:= false;
  417. if assigned(globalmacrosymtable) then
  418. globalmacrosymtable.foreach(@writeusedmacro,nil);
  419. localmacrosymtable.foreach(@writeusedmacro,nil);
  420. ppufile.writeentry(ibusedmacros);
  421. ppufile.do_crc:=true;
  422. end;
  423. {$ENDIF}
  424. procedure tppumodule.writesourcefiles;
  425. var
  426. hp : tinputfile;
  427. i,j : longint;
  428. begin
  429. { second write the used source files }
  430. ppufile.do_crc:=false;
  431. hp:=sourcefiles.files;
  432. { write source files directly in good order }
  433. j:=0;
  434. while assigned(hp) do
  435. begin
  436. inc(j);
  437. hp:=hp.ref_next;
  438. end;
  439. while j>0 do
  440. begin
  441. hp:=sourcefiles.files;
  442. for i:=1 to j-1 do
  443. hp:=hp.ref_next;
  444. ppufile.putstring(hp.name^);
  445. ppufile.putlongint(hp.getfiletime);
  446. dec(j);
  447. end;
  448. ppufile.writeentry(ibsourcefiles);
  449. ppufile.do_crc:=true;
  450. end;
  451. procedure tppumodule.writeusedunit(intf:boolean);
  452. var
  453. hp : tused_unit;
  454. oldcrc : boolean;
  455. begin
  456. { write a reference for each used unit }
  457. hp:=tused_unit(used_units.first);
  458. while assigned(hp) do
  459. begin
  460. if hp.in_interface=intf then
  461. begin
  462. ppufile.putstring(hp.u.realmodulename^);
  463. { the checksum should not affect the crc of this unit ! (PFV) }
  464. oldcrc:=ppufile.do_crc;
  465. ppufile.do_crc:=false;
  466. ppufile.putlongint(longint(hp.checksum));
  467. ppufile.putlongint(longint(hp.interface_checksum));
  468. ppufile.putlongint(longint(hp.indirect_checksum));
  469. ppufile.do_crc:=oldcrc;
  470. { combine all indirect checksums from units used by this unit }
  471. if intf then
  472. ppufile.indirect_crc:=ppufile.indirect_crc xor hp.indirect_checksum;
  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. indchecksum,
  748. intfchecksum,
  749. checksum : cardinal;
  750. begin
  751. while not ppufile.endofentry do
  752. begin
  753. hs:=ppufile.getstring;
  754. checksum:=cardinal(ppufile.getlongint);
  755. intfchecksum:=cardinal(ppufile.getlongint);
  756. indchecksum:=cardinal(ppufile.getlongint);
  757. { set the state of this unit before registering, this is
  758. needed for a correct circular dependency check }
  759. hp:=registerunit(self,hs,'');
  760. pu:=addusedunit(hp,false,nil);
  761. pu.checksum:=checksum;
  762. pu.interface_checksum:=intfchecksum;
  763. pu.indirect_checksum:=indchecksum;
  764. end;
  765. in_interface:=false;
  766. end;
  767. procedure tppumodule.readlinkcontainer(var p:tlinkcontainer);
  768. var
  769. s : string;
  770. m : longint;
  771. begin
  772. while not ppufile.endofentry do
  773. begin
  774. s:=ppufile.getstring;
  775. m:=ppufile.getlongint;
  776. p.add(s,m);
  777. end;
  778. end;
  779. procedure tppumodule.readderefmap;
  780. var
  781. i : longint;
  782. begin
  783. { Load unit map used for resolving }
  784. derefmapsize:=ppufile.getlongint;
  785. derefmapcnt:=derefmapsize;
  786. getmem(derefmap,derefmapsize*sizeof(tderefmaprec));
  787. fillchar(derefmap^,derefmapsize*sizeof(tderefmaprec),0);
  788. for i:=0 to derefmapsize-1 do
  789. derefmap[i].modulename:=stringdup(ppufile.getstring);
  790. end;
  791. procedure tppumodule.readderefdata;
  792. var
  793. len,hlen : longint;
  794. buf : array[0..1023] of byte;
  795. begin
  796. len:=ppufile.entrysize;
  797. while (len>0) do
  798. begin
  799. if len>1024 then
  800. hlen:=1024
  801. else
  802. hlen:=len;
  803. ppufile.getdata(buf,hlen);
  804. derefdata.write(buf,hlen);
  805. dec(len,hlen);
  806. end;
  807. end;
  808. procedure tppumodule.readImportSymbols;
  809. var
  810. j,
  811. extsymcnt : longint;
  812. ImportLibrary : TImportLibrary;
  813. extsymname : string;
  814. extsymordnr : longint;
  815. extsymisvar : boolean;
  816. begin
  817. while not ppufile.endofentry do
  818. begin
  819. ImportLibrary:=TImportLibrary.Create(ImportLibraryList,ppufile.getstring);
  820. extsymcnt:=ppufile.getlongint;
  821. for j:=0 to extsymcnt-1 do
  822. begin
  823. extsymname:=ppufile.getstring;
  824. extsymordnr:=ppufile.getlongint;
  825. extsymisvar:=(ppufile.getbyte<>0);
  826. TImportSymbol.Create(ImportLibrary.ImportSymbolList,extsymname,extsymordnr,extsymisvar);
  827. end;
  828. end;
  829. end;
  830. procedure tppumodule.readResources;
  831. begin
  832. while not ppufile.endofentry do
  833. resourcefiles.Insert(ppufile.getstring);
  834. end;
  835. procedure tppumodule.readwpofile;
  836. var
  837. orgwpofilename: string;
  838. orgwpofiletime: longint;
  839. begin
  840. { check whether we are using the same wpo feedback input file as when
  841. this unit was compiled (same file name and file date)
  842. }
  843. orgwpofilename:=ppufile.getstring;
  844. orgwpofiletime:=ppufile.getlongint;
  845. if (extractfilename(orgwpofilename)<>extractfilename(wpofeedbackinput)) or
  846. (orgwpofiletime<>GetNamedFileTime(orgwpofilename)) then
  847. { make sure we don't throw away a precompiled unit if the user simply
  848. forgot to specify the right wpo feedback file
  849. }
  850. message3(unit_e_different_wpo_file,ppufilename^,orgwpofilename,filetimestring(orgwpofiletime));
  851. end;
  852. procedure tppumodule.load_interface;
  853. var
  854. b : byte;
  855. newmodulename : string;
  856. begin
  857. { read interface part }
  858. repeat
  859. b:=ppufile.readentry;
  860. case b of
  861. ibmodulename :
  862. begin
  863. newmodulename:=ppufile.getstring;
  864. if (cs_check_unit_name in current_settings.globalswitches) and
  865. (upper(newmodulename)<>modulename^) then
  866. Message2(unit_f_unit_name_error,realmodulename^,newmodulename);
  867. stringdispose(modulename);
  868. stringdispose(realmodulename);
  869. modulename:=stringdup(upper(newmodulename));
  870. realmodulename:=stringdup(newmodulename);
  871. end;
  872. ibsourcefiles :
  873. readsourcefiles;
  874. {$IFDEF MACRO_DIFF_HINT}
  875. ibusedmacros :
  876. readusedmacros;
  877. {$ENDIF}
  878. ibloadunit :
  879. readloadunit;
  880. iblinkunitofiles :
  881. readlinkcontainer(LinkUnitOFiles);
  882. iblinkunitstaticlibs :
  883. readlinkcontainer(LinkUnitStaticLibs);
  884. iblinkunitsharedlibs :
  885. readlinkcontainer(LinkUnitSharedLibs);
  886. iblinkotherofiles :
  887. readlinkcontainer(LinkotherOFiles);
  888. iblinkotherstaticlibs :
  889. readlinkcontainer(LinkotherStaticLibs);
  890. iblinkothersharedlibs :
  891. readlinkcontainer(LinkotherSharedLibs);
  892. iblinkotherframeworks :
  893. readlinkcontainer(LinkOtherFrameworks);
  894. ibmainname:
  895. begin
  896. mainname:=stringdup(ppufile.getstring);
  897. if (mainaliasname<>defaultmainaliasname) then
  898. Message1(scan_w_multiple_main_name_overrides,mainaliasname);
  899. mainaliasname:=mainname^;
  900. end;
  901. ibImportSymbols :
  902. readImportSymbols;
  903. ibderefmap :
  904. readderefmap;
  905. ibderefdata :
  906. readderefdata;
  907. ibresources:
  908. readResources;
  909. ibwpofile:
  910. readwpofile;
  911. ibendinterface :
  912. break;
  913. else
  914. Message1(unit_f_ppu_invalid_entry,tostr(b));
  915. end;
  916. { we can already stop when we know that we must recompile }
  917. if do_compile then
  918. exit;
  919. until false;
  920. end;
  921. procedure tppumodule.load_implementation;
  922. var
  923. b : byte;
  924. begin
  925. { read implementation part }
  926. repeat
  927. b:=ppufile.readentry;
  928. case b of
  929. ibloadunit :
  930. readloadunit;
  931. ibasmsymbols :
  932. { TODO: Remove ibasmsymbols}
  933. ;
  934. ibendimplementation :
  935. break;
  936. else
  937. Message1(unit_f_ppu_invalid_entry,tostr(b));
  938. end;
  939. until false;
  940. end;
  941. procedure tppumodule.writeppu;
  942. begin
  943. Message1(unit_u_ppu_write,realmodulename^);
  944. { create unit flags }
  945. if do_release then
  946. flags:=flags or uf_release;
  947. if assigned(localsymtable) then
  948. flags:=flags or uf_local_symtable;
  949. {$ifdef cpufpemu}
  950. if (cs_fp_emulation in current_settings.moduleswitches) then
  951. flags:=flags or uf_fpu_emulation;
  952. {$endif cpufpemu}
  953. {$ifdef Test_Double_checksum_write}
  954. Assign(CRCFile,s+'.IMP');
  955. Rewrite(CRCFile);
  956. {$endif def Test_Double_checksum_write}
  957. { create new ppufile }
  958. ppufile:=tcompilerppufile.create(ppufilename^);
  959. if not ppufile.createfile then
  960. Message(unit_f_ppu_cannot_write);
  961. { first the unitname }
  962. ppufile.putstring(realmodulename^);
  963. ppufile.writeentry(ibmodulename);
  964. { write the alternate main procedure name if any }
  965. if assigned(mainname) then
  966. begin
  967. ppufile.putstring(mainname^);
  968. ppufile.writeentry(ibmainname);
  969. end;
  970. writesourcefiles;
  971. {$IFDEF MACRO_DIFF_HINT}
  972. writeusedmacros;
  973. {$ENDIF}
  974. { write interface uses }
  975. writeusedunit(true);
  976. { write the objectfiles and libraries that come for this unit,
  977. preserve the containers becuase they are still needed to load
  978. the link.res.
  979. All doesn't depend on the crc! It doesn't matter
  980. if a unit is in a .o or .a file }
  981. ppufile.do_crc:=false;
  982. { write after source files, so that we know whether or not the compiler
  983. will recompile the unit when checking whether the correct wpo file is
  984. used (if it will recompile the unit anyway, it doesn't matter)
  985. }
  986. if (wpofeedbackinput<>'') then
  987. begin
  988. ppufile.putstring(wpofeedbackinput);
  989. ppufile.putlongint(getnamedfiletime(wpofeedbackinput));
  990. ppufile.writeentry(ibwpofile);
  991. end;
  992. writelinkcontainer(linkunitofiles,iblinkunitofiles,true);
  993. writelinkcontainer(linkunitstaticlibs,iblinkunitstaticlibs,true);
  994. writelinkcontainer(linkunitsharedlibs,iblinkunitsharedlibs,true);
  995. writelinkcontainer(linkotherofiles,iblinkotherofiles,false);
  996. writelinkcontainer(linkotherstaticlibs,iblinkotherstaticlibs,true);
  997. writelinkcontainer(linkothersharedlibs,iblinkothersharedlibs,true);
  998. writelinkcontainer(linkotherframeworks,iblinkotherframeworks,true);
  999. writeImportSymbols;
  1000. writeResources;
  1001. ppufile.do_crc:=true;
  1002. { generate implementation deref data, the interface deref data is
  1003. already generated when calculating the interface crc }
  1004. if (cs_compilesystem in current_settings.moduleswitches) then
  1005. begin
  1006. tstoredsymtable(globalsymtable).buildderef;
  1007. derefdataintflen:=derefdata.size;
  1008. end
  1009. else
  1010. { the unit may have been re-resolved, in which case the current
  1011. position in derefdata is not necessarily at the end }
  1012. derefdata.seek(derefdata.size);
  1013. tstoredsymtable(globalsymtable).buildderefimpl;
  1014. if (flags and uf_local_symtable)<>0 then
  1015. begin
  1016. tstoredsymtable(localsymtable).buildderef;
  1017. tstoredsymtable(localsymtable).buildderefimpl;
  1018. end;
  1019. tunitwpoinfo(wpoinfo).buildderef;
  1020. tunitwpoinfo(wpoinfo).buildderefimpl;
  1021. writederefmap;
  1022. writederefdata;
  1023. ppufile.writeentry(ibendinterface);
  1024. { write the symtable entries }
  1025. tstoredsymtable(globalsymtable).ppuwrite(ppufile);
  1026. if assigned(globalmacrosymtable) and (globalmacrosymtable.SymList.count > 0) then
  1027. begin
  1028. ppufile.putbyte(byte(true));
  1029. ppufile.writeentry(ibexportedmacros);
  1030. tstoredsymtable(globalmacrosymtable).ppuwrite(ppufile);
  1031. end
  1032. else
  1033. begin
  1034. ppufile.putbyte(byte(false));
  1035. ppufile.writeentry(ibexportedmacros);
  1036. end;
  1037. { everything after this doesn't affect the crc }
  1038. ppufile.do_crc:=false;
  1039. { write implementation uses }
  1040. writeusedunit(false);
  1041. { end of implementation }
  1042. ppufile.writeentry(ibendimplementation);
  1043. { write static symtable
  1044. needed for local debugging of unit functions }
  1045. if (flags and uf_local_symtable)<>0 then
  1046. tstoredsymtable(localsymtable).ppuwrite(ppufile);
  1047. { write whole program optimisation-related information }
  1048. tunitwpoinfo(wpoinfo).ppuwrite(ppufile);
  1049. { the last entry ibend is written automaticly }
  1050. { flush to be sure }
  1051. ppufile.flush;
  1052. { create and write header }
  1053. ppufile.header.size:=ppufile.size;
  1054. ppufile.header.checksum:=ppufile.crc;
  1055. ppufile.header.interface_checksum:=ppufile.interface_crc;
  1056. ppufile.header.indirect_checksum:=ppufile.indirect_crc;
  1057. ppufile.header.compiler:=wordversion;
  1058. ppufile.header.cpu:=word(target_cpu);
  1059. ppufile.header.target:=word(target_info.system);
  1060. ppufile.header.flags:=flags;
  1061. ppufile.header.deflistsize:=current_module.deflist.count;
  1062. ppufile.header.symlistsize:=current_module.symlist.count;
  1063. ppufile.writeheader;
  1064. { save crc in current module also }
  1065. crc:=ppufile.crc;
  1066. interface_crc:=ppufile.interface_crc;
  1067. indirect_crc:=ppufile.indirect_crc;
  1068. {$ifdef Test_Double_checksum_write}
  1069. close(CRCFile);
  1070. {$endif Test_Double_checksum_write}
  1071. ppufile.closefile;
  1072. ppufile.free;
  1073. ppufile:=nil;
  1074. end;
  1075. procedure tppumodule.getppucrc;
  1076. begin
  1077. {$ifdef Test_Double_checksum_write}
  1078. Assign(CRCFile,s+'.INT')
  1079. Rewrite(CRCFile);
  1080. {$endif def Test_Double_checksum_write}
  1081. { create new ppufile }
  1082. ppufile:=tcompilerppufile.create(ppufilename^);
  1083. ppufile.crc_only:=true;
  1084. if not ppufile.createfile then
  1085. Message(unit_f_ppu_cannot_write);
  1086. { first the unitname }
  1087. ppufile.putstring(realmodulename^);
  1088. ppufile.writeentry(ibmodulename);
  1089. { the interface units affect the crc }
  1090. writeusedunit(true);
  1091. { deref data of interface that affect the crc }
  1092. derefdata.reset;
  1093. tstoredsymtable(globalsymtable).buildderef;
  1094. derefdataintflen:=derefdata.size;
  1095. writederefmap;
  1096. writederefdata;
  1097. ppufile.writeentry(ibendinterface);
  1098. { write the symtable entries }
  1099. tstoredsymtable(globalsymtable).ppuwrite(ppufile);
  1100. if assigned(globalmacrosymtable) and (globalmacrosymtable.SymList.count > 0) then
  1101. begin
  1102. ppufile.putbyte(byte(true));
  1103. ppufile.writeentry(ibexportedmacros);
  1104. tstoredsymtable(globalmacrosymtable).ppuwrite(ppufile);
  1105. end
  1106. else
  1107. begin
  1108. ppufile.putbyte(byte(false));
  1109. ppufile.writeentry(ibexportedmacros);
  1110. end;
  1111. { save crc }
  1112. crc:=ppufile.crc;
  1113. interface_crc:=ppufile.interface_crc;
  1114. indirect_crc:=ppufile.indirect_crc;
  1115. { end of implementation, to generate a correct ppufile
  1116. for ppudump when using INTFPPU define }
  1117. ppufile.writeentry(ibendimplementation);
  1118. {$ifdef Test_Double_checksum}
  1119. crc_array:=ppufile.crc_test;
  1120. ppufile.crc_test:=nil;
  1121. crc_size:=ppufile.crc_index2;
  1122. crc_array2:=ppufile.crc_test2;
  1123. ppufile.crc_test2:=nil;
  1124. crc_size2:=ppufile.crc_index2;
  1125. {$endif Test_Double_checksum}
  1126. {$ifdef Test_Double_checksum_write}
  1127. close(CRCFile);
  1128. {$endif Test_Double_checksum_write}
  1129. { create and write header, this will only be used
  1130. for debugging purposes }
  1131. ppufile.header.size:=ppufile.size;
  1132. ppufile.header.checksum:=ppufile.crc;
  1133. ppufile.header.interface_checksum:=ppufile.interface_crc;
  1134. ppufile.header.indirect_checksum:=ppufile.indirect_crc;
  1135. ppufile.header.compiler:=wordversion;
  1136. ppufile.header.cpu:=word(target_cpu);
  1137. ppufile.header.target:=word(target_info.system);
  1138. ppufile.header.flags:=flags;
  1139. ppufile.writeheader;
  1140. ppufile.closefile;
  1141. ppufile.free;
  1142. ppufile:=nil;
  1143. end;
  1144. procedure tppumodule.load_usedunits;
  1145. var
  1146. pu : tused_unit;
  1147. begin
  1148. if current_module<>self then
  1149. internalerror(200212284);
  1150. { load the used units from interface }
  1151. in_interface:=true;
  1152. pu:=tused_unit(used_units.first);
  1153. while assigned(pu) do
  1154. begin
  1155. if pu.in_interface then
  1156. begin
  1157. tppumodule(pu.u).loadppu;
  1158. { if this unit is compiled we can stop }
  1159. if state=ms_compiled then
  1160. exit;
  1161. { add this unit to the dependencies }
  1162. pu.u.adddependency(self);
  1163. { need to recompile the current unit, check the interface
  1164. crc. And when not compiled with -Ur then check the complete
  1165. crc }
  1166. if (pu.u.interface_crc<>pu.interface_checksum) or
  1167. (pu.u.indirect_crc<>pu.indirect_checksum) or
  1168. (
  1169. ((ppufile.header.flags and uf_release)=0) and
  1170. (pu.u.crc<>pu.checksum)
  1171. ) then
  1172. begin
  1173. Message2(unit_u_recompile_crc_change,realmodulename^,pu.u.realmodulename^,@queuecomment);
  1174. {$ifdef DEBUG_UNIT_CRC_CHANGES}
  1175. if (pu.u.interface_crc<>pu.interface_checksum) then
  1176. writeln(' intfcrc change: ',hexstr(pu.u.interface_crc,8),' <> ',hexstr(pu.interface_checksum,8))
  1177. else if (pu.u.indirect_crc<>pu.indirect_checksum) then
  1178. writeln(' indcrc change: ',hexstr(pu.u.indirect_crc,8),' <> ',hexstr(pu.indirect_checksum,8))
  1179. else
  1180. writeln(' implcrc change: ',hexstr(pu.u.crc,8),' <> ',hexstr(pu.checksum,8));
  1181. {$endif DEBUG_UNIT_CRC_CHANGES}
  1182. recompile_reason:=rr_crcchanged;
  1183. do_compile:=true;
  1184. exit;
  1185. end;
  1186. end;
  1187. pu:=tused_unit(pu.next);
  1188. end;
  1189. { ok, now load the interface of this unit }
  1190. if current_module<>self then
  1191. internalerror(200208187);
  1192. deflist.count:=ppufile.header.deflistsize;
  1193. symlist.count:=ppufile.header.symlistsize;
  1194. globalsymtable:=tglobalsymtable.create(modulename^,moduleid);
  1195. tstoredsymtable(globalsymtable).ppuload(ppufile);
  1196. if ppufile.readentry<>ibexportedmacros then
  1197. Message(unit_f_ppu_read_error);
  1198. if boolean(ppufile.getbyte) then
  1199. begin
  1200. globalmacrosymtable:=tmacrosymtable.Create(true);
  1201. tstoredsymtable(globalmacrosymtable).ppuload(ppufile)
  1202. end;
  1203. interface_compiled:=true;
  1204. { read the implementation part, containing
  1205. the implementation uses and ObjData }
  1206. in_interface:=false;
  1207. load_implementation;
  1208. { now only read the implementation uses }
  1209. pu:=tused_unit(used_units.first);
  1210. while assigned(pu) do
  1211. begin
  1212. if (not pu.in_interface) then
  1213. begin
  1214. tppumodule(pu.u).loadppu;
  1215. { if this unit is compiled we can stop }
  1216. if state=ms_compiled then
  1217. exit;
  1218. { add this unit to the dependencies }
  1219. pu.u.adddependency(self);
  1220. { need to recompile the current unit ? }
  1221. if (pu.u.interface_crc<>pu.interface_checksum) or
  1222. (pu.u.indirect_crc<>pu.indirect_checksum) then
  1223. begin
  1224. Message2(unit_u_recompile_crc_change,realmodulename^,pu.u.realmodulename^+' {impl}',@queuecomment);
  1225. {$ifdef DEBUG_UNIT_CRC_CHANGES}
  1226. if (pu.u.interface_crc<>pu.interface_checksum) then
  1227. writeln(' intfcrc change (2): ',hexstr(pu.u.interface_crc,8),' <> ',hexstr(pu.interface_checksum,8))
  1228. else if (pu.u.indirect_crc<>pu.indirect_checksum) then
  1229. writeln(' indcrc change (2): ',hexstr(pu.u.indirect_crc,8),' <> ',hexstr(pu.indirect_checksum,8));
  1230. {$endif DEBUG_UNIT_CRC_CHANGES}
  1231. recompile_reason:=rr_crcchanged;
  1232. do_compile:=true;
  1233. exit;
  1234. end;
  1235. end;
  1236. pu:=tused_unit(pu.next);
  1237. end;
  1238. { load implementation symtable }
  1239. if (flags and uf_local_symtable)<>0 then
  1240. begin
  1241. localsymtable:=tstaticsymtable.create(modulename^,moduleid);
  1242. tstaticsymtable(localsymtable).ppuload(ppufile);
  1243. end;
  1244. { we can now derefence all pointers to the implementation parts }
  1245. tstoredsymtable(globalsymtable).derefimpl;
  1246. if assigned(localsymtable) then
  1247. tstoredsymtable(localsymtable).derefimpl;
  1248. { read whole program optimisation-related information }
  1249. wpoinfo:=tunitwpoinfo.ppuload(ppufile);
  1250. tunitwpoinfo(wpoinfo).deref;
  1251. tunitwpoinfo(wpoinfo).derefimpl;
  1252. end;
  1253. function tppumodule.needrecompile:boolean;
  1254. var
  1255. pu : tused_unit;
  1256. begin
  1257. result:=false;
  1258. pu:=tused_unit(used_units.first);
  1259. while assigned(pu) do
  1260. begin
  1261. { need to recompile the current unit, check the interface
  1262. crc. And when not compiled with -Ur then check the complete
  1263. crc }
  1264. if (pu.u.interface_crc<>pu.interface_checksum) or
  1265. (pu.u.indirect_crc<>pu.indirect_checksum) or
  1266. (
  1267. (pu.in_interface) and
  1268. (pu.u.crc<>pu.checksum)
  1269. ) then
  1270. begin
  1271. {$ifdef DEBUG_UNIT_CRC_CHANGES}
  1272. if (pu.u.interface_crc<>pu.interface_checksum) then
  1273. writeln(' intfcrc change (3): ',hexstr(pu.u.interface_crc,8),' <> ',hexstr(pu.interface_checksum,8))
  1274. else if (pu.u.indirect_crc<>pu.indirect_checksum) then
  1275. writeln(' indcrc change (3): ',hexstr(pu.u.indirect_crc,8),' <> ',hexstr(pu.indirect_checksum,8))
  1276. else
  1277. writeln(' implcrc change (3): ',hexstr(pu.u.crc,8),' <> ',hexstr(pu.checksum,8));
  1278. {$endif DEBUG_UNIT_CRC_CHANGES}
  1279. result:=true;
  1280. exit;
  1281. end;
  1282. pu:=tused_unit(pu.next);
  1283. end;
  1284. end;
  1285. procedure tppumodule.setdefgeneration;
  1286. begin
  1287. defsgeneration:=currentdefgeneration;
  1288. inc(currentdefgeneration);
  1289. end;
  1290. procedure tppumodule.reload_flagged_units;
  1291. var
  1292. hp : tppumodule;
  1293. begin
  1294. { now reload all dependent units with outdated defs }
  1295. hp:=tppumodule(loaded_units.first);
  1296. while assigned(hp) do
  1297. begin
  1298. if hp.do_reload and
  1299. (hp.defsgeneration<defsgeneration) then
  1300. begin
  1301. hp.defsgeneration:=defsgeneration;
  1302. hp.loadppu
  1303. end
  1304. else
  1305. hp.do_reload:=false;
  1306. hp:=tppumodule(hp.next);
  1307. end;
  1308. end;
  1309. procedure tppumodule.loadppu;
  1310. const
  1311. ImplIntf : array[boolean] of string[15]=('implementation','interface');
  1312. var
  1313. do_load,
  1314. second_time : boolean;
  1315. old_current_module : tmodule;
  1316. begin
  1317. old_current_module:=current_module;
  1318. Message3(unit_u_load_unit,old_current_module.modulename^,
  1319. ImplIntf[old_current_module.in_interface],
  1320. modulename^);
  1321. { Update loaded_from to detect cycles }
  1322. loaded_from:=old_current_module;
  1323. { check if the globalsymtable is already available, but
  1324. we must reload when the do_reload flag is set }
  1325. if (not do_reload) and
  1326. assigned(globalsymtable) then
  1327. exit;
  1328. { reset }
  1329. do_load:=true;
  1330. second_time:=false;
  1331. set_current_module(self);
  1332. { A force reload }
  1333. if do_reload then
  1334. begin
  1335. Message(unit_u_forced_reload);
  1336. do_reload:=false;
  1337. { When the unit is already loaded or being loaded
  1338. we can maybe skip a complete reload/recompile }
  1339. if assigned(globalsymtable) and
  1340. (not needrecompile) then
  1341. begin
  1342. { When we don't have any data stored yet there
  1343. is nothing to resolve }
  1344. if interface_compiled then
  1345. begin
  1346. Message1(unit_u_reresolving_unit,modulename^);
  1347. tstoredsymtable(globalsymtable).deref;
  1348. tstoredsymtable(globalsymtable).derefimpl;
  1349. if assigned(localsymtable) then
  1350. begin
  1351. tstoredsymtable(localsymtable).deref;
  1352. tstoredsymtable(localsymtable).derefimpl;
  1353. end;
  1354. if assigned(wpoinfo) then
  1355. begin
  1356. tunitwpoinfo(wpoinfo).deref;
  1357. tunitwpoinfo(wpoinfo).derefimpl;
  1358. end;
  1359. { We have to flag the units that depend on this unit even
  1360. though it didn't change, because they might also
  1361. indirectly depend on the unit that did change (e.g.,
  1362. in case rgobj, rgx86 and rgcpu have been compiled
  1363. already, and then rgobj is recompiled for some reason
  1364. -> rgx86 is re-reresolved, but the vmtentries of trgcpu
  1365. must also be re-resolved, because they will also contain
  1366. pointers to procdefs in the old trgobj (in case of a
  1367. recompile, all old defs are freed) }
  1368. flagdependent(old_current_module);
  1369. reload_flagged_units;
  1370. end
  1371. else
  1372. Message1(unit_u_skipping_reresolving_unit,modulename^);
  1373. do_load:=false;
  1374. end;
  1375. end;
  1376. if do_load then
  1377. begin
  1378. { loading the unit for a second time? }
  1379. if state=ms_registered then
  1380. state:=ms_load
  1381. else
  1382. begin
  1383. { try to load the unit a second time first }
  1384. Message1(unit_u_second_load_unit,modulename^);
  1385. Message2(unit_u_previous_state,modulename^,ModuleStateStr[state]);
  1386. { Flag modules to reload }
  1387. flagdependent(old_current_module);
  1388. { Reset the module }
  1389. reset;
  1390. if state in [ms_compile,ms_second_compile] then
  1391. begin
  1392. Message1(unit_u_second_compile_unit,modulename^);
  1393. state:=ms_second_compile;
  1394. do_compile:=true;
  1395. end
  1396. else
  1397. state:=ms_second_load;
  1398. second_time:=true;
  1399. end;
  1400. { close old_current_ppu on system that are
  1401. short on file handles like DOS PM }
  1402. {$ifdef SHORT_ON_FILE_HANDLES}
  1403. if old_current_module.is_unit and
  1404. assigned(tppumodule(old_current_module).ppufile) then
  1405. tppumodule(old_current_module).ppufile.tempclose;
  1406. {$endif SHORT_ON_FILE_HANDLES}
  1407. { try to opening ppu, skip this when we already
  1408. know that we need to compile the unit }
  1409. if not do_compile then
  1410. begin
  1411. Message1(unit_u_loading_unit,modulename^);
  1412. search_unit(false,false);
  1413. if not do_compile then
  1414. begin
  1415. load_interface;
  1416. setdefgeneration;
  1417. if not do_compile then
  1418. begin
  1419. load_usedunits;
  1420. if not do_compile then
  1421. Message1(unit_u_finished_loading_unit,modulename^);
  1422. end;
  1423. end;
  1424. { PPU is not needed anymore }
  1425. if assigned(ppufile) then
  1426. begin
  1427. ppufile.closefile;
  1428. ppufile.free;
  1429. ppufile:=nil;
  1430. end;
  1431. end;
  1432. { Do we need to recompile the unit }
  1433. if do_compile then
  1434. begin
  1435. { recompile the unit or give a fatal error if sources not available }
  1436. if not(sources_avail) then
  1437. begin
  1438. if (not search_unit(true,false)) and
  1439. (length(modulename^)>8) then
  1440. search_unit(true,true);
  1441. if not(sources_avail) then
  1442. begin
  1443. printcomments;
  1444. if recompile_reason=rr_noppu then
  1445. Message2(unit_f_cant_find_ppu,realmodulename^,loaded_from.realmodulename^)
  1446. else
  1447. Message1(unit_f_cant_compile_unit,realmodulename^);
  1448. end;
  1449. end;
  1450. { we found the sources, we do not need the verbose messages anymore }
  1451. if comments <> nil then
  1452. begin
  1453. comments.free;
  1454. comments:=nil;
  1455. end;
  1456. { Flag modules to reload }
  1457. flagdependent(old_current_module);
  1458. { Reset the module }
  1459. reset;
  1460. { compile this module }
  1461. if not(state in [ms_compile,ms_second_compile]) then
  1462. state:=ms_compile;
  1463. compile(mainsource^);
  1464. setdefgeneration;
  1465. end
  1466. else
  1467. state:=ms_compiled;
  1468. if current_module<>self then
  1469. internalerror(200212282);
  1470. if in_interface then
  1471. internalerror(200212283);
  1472. { for a second_time recompile reload all dependent units,
  1473. for a first time compile register the unit _once_ }
  1474. if second_time then
  1475. reload_flagged_units
  1476. else
  1477. usedunits.concat(tused_unit.create(self,true,false,nil));
  1478. { reopen the old module }
  1479. {$ifdef SHORT_ON_FILE_HANDLES}
  1480. if old_current_module.is_unit and
  1481. assigned(tppumodule(old_current_module).ppufile) then
  1482. tppumodule(old_current_module).ppufile.tempopen;
  1483. {$endif SHORT_ON_FILE_HANDLES}
  1484. end;
  1485. { we are back, restore current_module }
  1486. set_current_module(old_current_module);
  1487. end;
  1488. {*****************************************************************************
  1489. RegisterUnit
  1490. *****************************************************************************}
  1491. function registerunit(callermodule:tmodule;const s : TIDString;const fn:string) : tppumodule;
  1492. var
  1493. ups : TIDString;
  1494. hp : tppumodule;
  1495. hp2 : tmodule;
  1496. begin
  1497. { Info }
  1498. ups:=upper(s);
  1499. { search all loaded units }
  1500. hp:=tppumodule(loaded_units.first);
  1501. while assigned(hp) do
  1502. begin
  1503. if hp.modulename^=ups then
  1504. begin
  1505. { only check for units. The main program is also
  1506. as a unit in the loaded_units list. We simply need
  1507. to ignore this entry (PFV) }
  1508. if hp.is_unit then
  1509. begin
  1510. { both units in interface ? }
  1511. if callermodule.in_interface and
  1512. hp.in_interface then
  1513. begin
  1514. { check for a cycle }
  1515. hp2:=callermodule.loaded_from;
  1516. while assigned(hp2) and (hp2<>hp) do
  1517. begin
  1518. if hp2.in_interface then
  1519. hp2:=hp2.loaded_from
  1520. else
  1521. hp2:=nil;
  1522. end;
  1523. if assigned(hp2) then
  1524. Message2(unit_f_circular_unit_reference,callermodule.realmodulename^,hp.realmodulename^);
  1525. end;
  1526. break;
  1527. end;
  1528. end;
  1529. { the next unit }
  1530. hp:=tppumodule(hp.next);
  1531. end;
  1532. { the unit is not in the loaded units,
  1533. we create an entry and register the unit }
  1534. if not assigned(hp) then
  1535. begin
  1536. Message1(unit_u_registering_new_unit,Upper(s));
  1537. hp:=tppumodule.create(callermodule,s,fn,true);
  1538. hp.loaded_from:=callermodule;
  1539. addloadedunit(hp);
  1540. end;
  1541. { return }
  1542. registerunit:=hp;
  1543. end;
  1544. end.