fppu.pas 57 KB

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