fppu.pas 62 KB

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