fppu.pas 58 KB

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