fppu.pas 62 KB

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