fppu.pas 49 KB

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