2
0

fppu.pas 57 KB

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