fppu.pas 50 KB

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