fppu.pas 68 KB

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