fppu.pas 49 KB

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