fppu.pas 50 KB

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