fppu.pas 61 KB

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