fppu.pas 52 KB

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