fppu.pas 57 KB

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