fppu.pas 51 KB

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