fppu.pas 60 KB

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