fppu.pas 50 KB

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