fppu.pas 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  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. ibmainname:
  872. begin
  873. mainname:=stringdup(ppufile.getstring);
  874. if (mainaliasname<>defaultmainaliasname) then
  875. Message1(scan_w_multiple_main_name_overrides,mainaliasname);
  876. mainaliasname:=mainname^;
  877. end;
  878. ibImportSymbols :
  879. readImportSymbols;
  880. ibderefmap :
  881. readderefmap;
  882. ibderefdata :
  883. readderefdata;
  884. ibresources:
  885. readResources;
  886. ibendinterface :
  887. break;
  888. else
  889. Message1(unit_f_ppu_invalid_entry,tostr(b));
  890. end;
  891. { we can already stop when we know that we must recompile }
  892. if do_compile then
  893. exit;
  894. until false;
  895. end;
  896. procedure tppumodule.load_implementation;
  897. var
  898. b : byte;
  899. begin
  900. { read implementation part }
  901. repeat
  902. b:=ppufile.readentry;
  903. case b of
  904. ibloadunit :
  905. readloadunit;
  906. ibasmsymbols :
  907. {$warning TODO Remove ibasmsymbols}
  908. ;
  909. ibendimplementation :
  910. break;
  911. else
  912. Message1(unit_f_ppu_invalid_entry,tostr(b));
  913. end;
  914. until false;
  915. end;
  916. procedure tppumodule.writeppu;
  917. begin
  918. Message1(unit_u_ppu_write,realmodulename^);
  919. { create unit flags }
  920. if do_release then
  921. flags:=flags or uf_release;
  922. if assigned(localsymtable) then
  923. flags:=flags or uf_local_symtable;
  924. {$ifdef cpufpemu}
  925. if (cs_fp_emulation in current_settings.moduleswitches) then
  926. flags:=flags or uf_fpu_emulation;
  927. {$endif cpufpemu}
  928. {$ifdef Test_Double_checksum_write}
  929. Assign(CRCFile,s+'.IMP');
  930. Rewrite(CRCFile);
  931. {$endif def Test_Double_checksum_write}
  932. { create new ppufile }
  933. ppufile:=tcompilerppufile.create(ppufilename^);
  934. if not ppufile.createfile then
  935. Message(unit_f_ppu_cannot_write);
  936. { first the unitname }
  937. ppufile.putstring(realmodulename^);
  938. ppufile.writeentry(ibmodulename);
  939. { write the alternate main procedure name if any }
  940. if assigned(mainname) then
  941. begin
  942. ppufile.putstring(mainname^);
  943. ppufile.writeentry(ibmainname);
  944. end;
  945. writesourcefiles;
  946. {$IFDEF MACRO_DIFF_HINT}
  947. writeusedmacros;
  948. {$ENDIF}
  949. { write interface uses }
  950. writeusedunit(true);
  951. { write the objectfiles and libraries that come for this unit,
  952. preserve the containers becuase they are still needed to load
  953. the link.res. All doesn't depend on the crc! It doesn't matter
  954. if a unit is in a .o or .a file }
  955. ppufile.do_crc:=false;
  956. writelinkcontainer(linkunitofiles,iblinkunitofiles,true);
  957. writelinkcontainer(linkunitstaticlibs,iblinkunitstaticlibs,true);
  958. writelinkcontainer(linkunitsharedlibs,iblinkunitsharedlibs,true);
  959. writelinkcontainer(linkotherofiles,iblinkotherofiles,false);
  960. writelinkcontainer(linkotherstaticlibs,iblinkotherstaticlibs,true);
  961. writelinkcontainer(linkothersharedlibs,iblinkothersharedlibs,true);
  962. writelinkcontainer(linkotherframeworks,iblinkotherframeworks,true);
  963. writeImportSymbols;
  964. writeResources;
  965. ppufile.do_crc:=true;
  966. { generate implementation deref data, the interface deref data is
  967. already generated when calculating the interface crc }
  968. if (cs_compilesystem in current_settings.moduleswitches) then
  969. begin
  970. tstoredsymtable(globalsymtable).buildderef;
  971. derefdataintflen:=derefdata.size;
  972. end;
  973. tstoredsymtable(globalsymtable).buildderefimpl;
  974. if (flags and uf_local_symtable)<>0 then
  975. begin
  976. tstoredsymtable(localsymtable).buildderef;
  977. tstoredsymtable(localsymtable).buildderefimpl;
  978. end;
  979. writederefmap;
  980. writederefdata;
  981. ppufile.writeentry(ibendinterface);
  982. { write the symtable entries }
  983. tstoredsymtable(globalsymtable).ppuwrite(ppufile);
  984. if assigned(globalmacrosymtable) and (globalmacrosymtable.SymList.count > 0) then
  985. begin
  986. ppufile.putbyte(byte(true));
  987. ppufile.writeentry(ibexportedmacros);
  988. tstoredsymtable(globalmacrosymtable).ppuwrite(ppufile);
  989. end
  990. else
  991. begin
  992. ppufile.putbyte(byte(false));
  993. ppufile.writeentry(ibexportedmacros);
  994. end;
  995. { everything after this doesn't affect the crc }
  996. ppufile.do_crc:=false;
  997. { write implementation uses }
  998. writeusedunit(false);
  999. { end of implementation }
  1000. ppufile.writeentry(ibendimplementation);
  1001. { write static symtable
  1002. needed for local debugging of unit functions }
  1003. if (flags and uf_local_symtable)<>0 then
  1004. tstoredsymtable(localsymtable).ppuwrite(ppufile);
  1005. { the last entry ibend is written automaticly }
  1006. { flush to be sure }
  1007. ppufile.flush;
  1008. { create and write header }
  1009. ppufile.header.size:=ppufile.size;
  1010. ppufile.header.checksum:=ppufile.crc;
  1011. ppufile.header.interface_checksum:=ppufile.interface_crc;
  1012. ppufile.header.compiler:=wordversion;
  1013. ppufile.header.cpu:=word(target_cpu);
  1014. ppufile.header.target:=word(target_info.system);
  1015. ppufile.header.flags:=flags;
  1016. ppufile.header.deflistsize:=current_module.deflist.count;
  1017. ppufile.header.symlistsize:=current_module.symlist.count;
  1018. ppufile.writeheader;
  1019. { save crc in current module also }
  1020. crc:=ppufile.crc;
  1021. interface_crc:=ppufile.interface_crc;
  1022. {$ifdef Test_Double_checksum_write}
  1023. close(CRCFile);
  1024. {$endif Test_Double_checksum_write}
  1025. ppufile.closefile;
  1026. ppufile.free;
  1027. ppufile:=nil;
  1028. end;
  1029. procedure tppumodule.getppucrc;
  1030. begin
  1031. {$ifdef Test_Double_checksum_write}
  1032. Assign(CRCFile,s+'.INT')
  1033. Rewrite(CRCFile);
  1034. {$endif def Test_Double_checksum_write}
  1035. { create new ppufile }
  1036. ppufile:=tcompilerppufile.create(ppufilename^);
  1037. ppufile.crc_only:=true;
  1038. if not ppufile.createfile then
  1039. Message(unit_f_ppu_cannot_write);
  1040. { first the unitname }
  1041. ppufile.putstring(realmodulename^);
  1042. ppufile.writeentry(ibmodulename);
  1043. { the interface units affect the crc }
  1044. writeusedunit(true);
  1045. { deref data of interface that affect the crc }
  1046. derefdata.reset;
  1047. tstoredsymtable(globalsymtable).buildderef;
  1048. derefdataintflen:=derefdata.size;
  1049. writederefmap;
  1050. writederefdata;
  1051. ppufile.writeentry(ibendinterface);
  1052. { write the symtable entries }
  1053. tstoredsymtable(globalsymtable).ppuwrite(ppufile);
  1054. if assigned(globalmacrosymtable) and (globalmacrosymtable.SymList.count > 0) then
  1055. begin
  1056. ppufile.putbyte(byte(true));
  1057. ppufile.writeentry(ibexportedmacros);
  1058. tstoredsymtable(globalmacrosymtable).ppuwrite(ppufile);
  1059. end
  1060. else
  1061. begin
  1062. ppufile.putbyte(byte(false));
  1063. ppufile.writeentry(ibexportedmacros);
  1064. end;
  1065. { save crc }
  1066. crc:=ppufile.crc;
  1067. interface_crc:=ppufile.interface_crc;
  1068. { end of implementation, to generate a correct ppufile
  1069. for ppudump when using INTFPPU define }
  1070. ppufile.writeentry(ibendimplementation);
  1071. {$ifdef Test_Double_checksum}
  1072. crc_array:=ppufile.crc_test;
  1073. ppufile.crc_test:=nil;
  1074. crc_size:=ppufile.crc_index2;
  1075. crc_array2:=ppufile.crc_test2;
  1076. ppufile.crc_test2:=nil;
  1077. crc_size2:=ppufile.crc_index2;
  1078. {$endif Test_Double_checksum}
  1079. {$ifdef Test_Double_checksum_write}
  1080. close(CRCFile);
  1081. {$endif Test_Double_checksum_write}
  1082. { create and write header, this will only be used
  1083. for debugging purposes }
  1084. ppufile.header.size:=ppufile.size;
  1085. ppufile.header.checksum:=ppufile.crc;
  1086. ppufile.header.interface_checksum:=ppufile.interface_crc;
  1087. ppufile.header.compiler:=wordversion;
  1088. ppufile.header.cpu:=word(target_cpu);
  1089. ppufile.header.target:=word(target_info.system);
  1090. ppufile.header.flags:=flags;
  1091. ppufile.writeheader;
  1092. ppufile.closefile;
  1093. ppufile.free;
  1094. ppufile:=nil;
  1095. end;
  1096. procedure tppumodule.load_usedunits;
  1097. var
  1098. pu : tused_unit;
  1099. begin
  1100. if current_module<>self then
  1101. internalerror(200212284);
  1102. { load the used units from interface }
  1103. in_interface:=true;
  1104. pu:=tused_unit(used_units.first);
  1105. while assigned(pu) do
  1106. begin
  1107. if pu.in_interface then
  1108. begin
  1109. tppumodule(pu.u).loadppu;
  1110. { if this unit is compiled we can stop }
  1111. if state=ms_compiled then
  1112. exit;
  1113. { add this unit to the dependencies }
  1114. pu.u.adddependency(self);
  1115. { need to recompile the current unit, check the interface
  1116. crc. And when not compiled with -Ur then check the complete
  1117. crc }
  1118. if (pu.u.interface_crc<>pu.interface_checksum) or
  1119. (
  1120. ((ppufile.header.flags and uf_release)=0) and
  1121. (pu.u.crc<>pu.checksum)
  1122. ) then
  1123. begin
  1124. Message2(unit_u_recompile_crc_change,realmodulename^,pu.u.realmodulename^,@queuecomment);
  1125. recompile_reason:=rr_crcchanged;
  1126. do_compile:=true;
  1127. exit;
  1128. end;
  1129. end;
  1130. pu:=tused_unit(pu.next);
  1131. end;
  1132. { ok, now load the interface of this unit }
  1133. if current_module<>self then
  1134. internalerror(200208187);
  1135. deflist.count:=ppufile.header.deflistsize;
  1136. symlist.count:=ppufile.header.symlistsize;
  1137. globalsymtable:=tglobalsymtable.create(modulename^,moduleid);
  1138. tstoredsymtable(globalsymtable).ppuload(ppufile);
  1139. if ppufile.readentry<>ibexportedmacros then
  1140. Message(unit_f_ppu_read_error);
  1141. if boolean(ppufile.getbyte) then
  1142. begin
  1143. globalmacrosymtable:=tmacrosymtable.Create(true);
  1144. tstoredsymtable(globalmacrosymtable).ppuload(ppufile)
  1145. end;
  1146. interface_compiled:=true;
  1147. { read the implementation part, containing
  1148. the implementation uses and ObjData }
  1149. in_interface:=false;
  1150. load_implementation;
  1151. { now only read the implementation uses }
  1152. pu:=tused_unit(used_units.first);
  1153. while assigned(pu) do
  1154. begin
  1155. if (not pu.in_interface) then
  1156. begin
  1157. tppumodule(pu.u).loadppu;
  1158. { if this unit is compiled we can stop }
  1159. if state=ms_compiled then
  1160. exit;
  1161. { add this unit to the dependencies }
  1162. pu.u.adddependency(self);
  1163. { need to recompile the current unit ? }
  1164. if (pu.u.interface_crc<>pu.interface_checksum) then
  1165. begin
  1166. Message2(unit_u_recompile_crc_change,realmodulename^,pu.u.realmodulename^+' {impl}',@queuecomment);
  1167. recompile_reason:=rr_crcchanged;
  1168. do_compile:=true;
  1169. exit;
  1170. end;
  1171. end;
  1172. pu:=tused_unit(pu.next);
  1173. end;
  1174. { load implementation symtable }
  1175. if (flags and uf_local_symtable)<>0 then
  1176. begin
  1177. localsymtable:=tstaticsymtable.create(modulename^,moduleid);
  1178. tstaticsymtable(localsymtable).ppuload(ppufile);
  1179. end;
  1180. { we can now derefence all pointers to the implementation parts }
  1181. tstoredsymtable(globalsymtable).derefimpl;
  1182. if assigned(localsymtable) then
  1183. tstoredsymtable(localsymtable).derefimpl;
  1184. end;
  1185. function tppumodule.needrecompile:boolean;
  1186. var
  1187. pu : tused_unit;
  1188. begin
  1189. result:=false;
  1190. pu:=tused_unit(used_units.first);
  1191. while assigned(pu) do
  1192. begin
  1193. { need to recompile the current unit, check the interface
  1194. crc. And when not compiled with -Ur then check the complete
  1195. crc }
  1196. if (pu.u.interface_crc<>pu.interface_checksum) or
  1197. (
  1198. (pu.in_interface) and
  1199. (pu.u.crc<>pu.checksum)
  1200. ) then
  1201. begin
  1202. result:=true;
  1203. exit;
  1204. end;
  1205. pu:=tused_unit(pu.next);
  1206. end;
  1207. end;
  1208. procedure tppumodule.loadppu;
  1209. const
  1210. ImplIntf : array[boolean] of string[15]=('implementation','interface');
  1211. var
  1212. do_load,
  1213. second_time : boolean;
  1214. old_current_module : tmodule;
  1215. begin
  1216. old_current_module:=current_module;
  1217. Message3(unit_u_load_unit,old_current_module.modulename^,
  1218. ImplIntf[old_current_module.in_interface],
  1219. modulename^);
  1220. { Update loaded_from to detect cycles }
  1221. loaded_from:=old_current_module;
  1222. { check if the globalsymtable is already available, but
  1223. we must reload when the do_reload flag is set }
  1224. if (not do_reload) and
  1225. assigned(globalsymtable) then
  1226. exit;
  1227. { reset }
  1228. do_load:=true;
  1229. second_time:=false;
  1230. set_current_module(self);
  1231. { A force reload }
  1232. if do_reload then
  1233. begin
  1234. Message(unit_u_forced_reload);
  1235. do_reload:=false;
  1236. { When the unit is already loaded or being loaded
  1237. we can maybe skip a complete reload/recompile }
  1238. if assigned(globalsymtable) and
  1239. (not needrecompile) then
  1240. begin
  1241. { When we don't have any data stored yet there
  1242. is nothing to resolve }
  1243. if interface_compiled then
  1244. begin
  1245. Message1(unit_u_reresolving_unit,modulename^);
  1246. tstoredsymtable(globalsymtable).deref;
  1247. tstoredsymtable(globalsymtable).derefimpl;
  1248. if assigned(localsymtable) then
  1249. begin
  1250. tstoredsymtable(localsymtable).deref;
  1251. tstoredsymtable(localsymtable).derefimpl;
  1252. end;
  1253. end
  1254. else
  1255. Message1(unit_u_skipping_reresolving_unit,modulename^);
  1256. do_load:=false;
  1257. end;
  1258. end;
  1259. if do_load then
  1260. begin
  1261. { loading the unit for a second time? }
  1262. if state=ms_registered then
  1263. state:=ms_load
  1264. else
  1265. begin
  1266. { try to load the unit a second time first }
  1267. Message1(unit_u_second_load_unit,modulename^);
  1268. Message2(unit_u_previous_state,modulename^,ModuleStateStr[state]);
  1269. { Flag modules to reload }
  1270. flagdependent(old_current_module);
  1271. { Reset the module }
  1272. reset;
  1273. if state in [ms_compile,ms_second_compile] then
  1274. begin
  1275. Message1(unit_u_second_compile_unit,modulename^);
  1276. state:=ms_second_compile;
  1277. do_compile:=true;
  1278. end
  1279. else
  1280. state:=ms_second_load;
  1281. second_time:=true;
  1282. end;
  1283. { close old_current_ppu on system that are
  1284. short on file handles like DOS PM }
  1285. {$ifdef SHORT_ON_FILE_HANDLES}
  1286. if old_current_module.is_unit and
  1287. assigned(tppumodule(old_current_module).ppufile) then
  1288. tppumodule(old_current_module).ppufile.tempclose;
  1289. {$endif SHORT_ON_FILE_HANDLES}
  1290. { try to opening ppu, skip this when we already
  1291. know that we need to compile the unit }
  1292. if not do_compile then
  1293. begin
  1294. Message1(unit_u_loading_unit,modulename^);
  1295. search_unit(false,false);
  1296. if not do_compile then
  1297. begin
  1298. load_interface;
  1299. if not do_compile then
  1300. begin
  1301. load_usedunits;
  1302. if not do_compile then
  1303. Message1(unit_u_finished_loading_unit,modulename^);
  1304. end;
  1305. end;
  1306. { PPU is not needed anymore }
  1307. if assigned(ppufile) then
  1308. begin
  1309. ppufile.closefile;
  1310. ppufile.free;
  1311. ppufile:=nil;
  1312. end;
  1313. end;
  1314. { Do we need to recompile the unit }
  1315. if do_compile then
  1316. begin
  1317. { recompile the unit or give a fatal error if sources not available }
  1318. if not(sources_avail) then
  1319. begin
  1320. if (not search_unit(true,false)) and
  1321. (length(modulename^)>8) then
  1322. search_unit(true,true);
  1323. if not(sources_avail) then
  1324. begin
  1325. printcomments;
  1326. if recompile_reason=rr_noppu then
  1327. Message2(unit_f_cant_find_ppu,realmodulename^,loaded_from.realmodulename^)
  1328. else
  1329. Message1(unit_f_cant_compile_unit,realmodulename^);
  1330. end;
  1331. end;
  1332. { we found the sources, we do not need the verbose messages anymore }
  1333. if comments <> nil then
  1334. begin
  1335. comments.free;
  1336. comments:=nil;
  1337. end;
  1338. { Flag modules to reload }
  1339. flagdependent(old_current_module);
  1340. { Reset the module }
  1341. reset;
  1342. { compile this module }
  1343. if not(state in [ms_compile,ms_second_compile]) then
  1344. state:=ms_compile;
  1345. compile(mainsource^);
  1346. end
  1347. else
  1348. state:=ms_compiled;
  1349. if current_module<>self then
  1350. internalerror(200212282);
  1351. if in_interface then
  1352. internalerror(200212283);
  1353. { for a second_time recompile reload all dependent units,
  1354. for a first time compile register the unit _once_ }
  1355. if second_time then
  1356. reload_flagged_units
  1357. else
  1358. usedunits.concat(tused_unit.create(self,true,false,nil));
  1359. { reopen the old module }
  1360. {$ifdef SHORT_ON_FILE_HANDLES}
  1361. if old_current_module.is_unit and
  1362. assigned(tppumodule(old_current_module).ppufile) then
  1363. tppumodule(old_current_module).ppufile.tempopen;
  1364. {$endif SHORT_ON_FILE_HANDLES}
  1365. end;
  1366. { we are back, restore current_module }
  1367. set_current_module(old_current_module);
  1368. end;
  1369. {*****************************************************************************
  1370. RegisterUnit
  1371. *****************************************************************************}
  1372. function registerunit(callermodule:tmodule;const s : TIDString;const fn:string) : tppumodule;
  1373. var
  1374. ups : TIDString;
  1375. hp : tppumodule;
  1376. hp2 : tmodule;
  1377. begin
  1378. { Info }
  1379. ups:=upper(s);
  1380. { search all loaded units }
  1381. hp:=tppumodule(loaded_units.first);
  1382. while assigned(hp) do
  1383. begin
  1384. if hp.modulename^=ups then
  1385. begin
  1386. { only check for units. The main program is also
  1387. as a unit in the loaded_units list. We simply need
  1388. to ignore this entry (PFV) }
  1389. if hp.is_unit then
  1390. begin
  1391. { both units in interface ? }
  1392. if callermodule.in_interface and
  1393. hp.in_interface then
  1394. begin
  1395. { check for a cycle }
  1396. hp2:=callermodule.loaded_from;
  1397. while assigned(hp2) and (hp2<>hp) do
  1398. begin
  1399. if hp2.in_interface then
  1400. hp2:=hp2.loaded_from
  1401. else
  1402. hp2:=nil;
  1403. end;
  1404. if assigned(hp2) then
  1405. Message2(unit_f_circular_unit_reference,callermodule.realmodulename^,hp.realmodulename^);
  1406. end;
  1407. break;
  1408. end;
  1409. end;
  1410. { the next unit }
  1411. hp:=tppumodule(hp.next);
  1412. end;
  1413. { the unit is not in the loaded units,
  1414. we create an entry and register the unit }
  1415. if not assigned(hp) then
  1416. begin
  1417. Message1(unit_u_registering_new_unit,Upper(s));
  1418. hp:=tppumodule.create(callermodule,s,fn,true);
  1419. hp.loaded_from:=callermodule;
  1420. addloadedunit(hp);
  1421. end;
  1422. { return }
  1423. registerunit:=hp;
  1424. end;
  1425. end.