fppu.pas 51 KB

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