fppu.pas 59 KB

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