fppu.pas 51 KB

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