fppu.pas 61 KB

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