fppu.pas 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029
  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. { now load the unit and all used units }
  514. load_interface;
  515. setdefgeneration;
  516. load_usedunits;
  517. Message1(unit_u_finished_loading_unit,modulename^);
  518. result:=true;
  519. break;
  520. end;
  521. end;
  522. end;
  523. {**********************************
  524. PPU Reading/Writing Helpers
  525. ***********************************}
  526. {$IFDEF MACRO_DIFF_HINT}
  527. var
  528. is_initial: Boolean;
  529. procedure tppumodule.writeusedmacro(p:TNamedIndexItem;arg:pointer);
  530. begin
  531. if tmacro(p).is_used or is_initial then
  532. begin
  533. ppufile.putstring(p.name);
  534. ppufile.putbyte(byte(is_initial));
  535. ppufile.putbyte(byte(tmacro(p).is_used));
  536. end;
  537. end;
  538. procedure tppumodule.writeusedmacros;
  539. begin
  540. ppufile.do_crc:=false;
  541. is_initial:= true;
  542. initialmacrosymtable.foreach(@writeusedmacro,nil);
  543. is_initial:= false;
  544. if assigned(globalmacrosymtable) then
  545. globalmacrosymtable.foreach(@writeusedmacro,nil);
  546. localmacrosymtable.foreach(@writeusedmacro,nil);
  547. ppufile.writeentry(ibusedmacros);
  548. ppufile.do_crc:=true;
  549. end;
  550. {$ENDIF}
  551. procedure tppumodule.writesourcefiles;
  552. var
  553. hp : tinputfile;
  554. i,j : longint;
  555. begin
  556. { second write the used source files }
  557. ppufile.do_crc:=false;
  558. hp:=sourcefiles.files;
  559. { write source files directly in good order }
  560. j:=0;
  561. while assigned(hp) do
  562. begin
  563. inc(j);
  564. hp:=hp.ref_next;
  565. end;
  566. while j>0 do
  567. begin
  568. hp:=sourcefiles.files;
  569. for i:=1 to j-1 do
  570. hp:=hp.ref_next;
  571. ppufile.putstring(hp.inc_path+hp.name);
  572. ppufile.putlongint(hp.getfiletime);
  573. dec(j);
  574. end;
  575. ppufile.writeentry(ibsourcefiles);
  576. ppufile.do_crc:=true;
  577. end;
  578. procedure tppumodule.writeusedunit(intf:boolean);
  579. var
  580. hp : tused_unit;
  581. oldcrc : boolean;
  582. begin
  583. { write a reference for each used unit }
  584. hp:=tused_unit(used_units.first);
  585. while assigned(hp) do
  586. begin
  587. if hp.in_interface=intf then
  588. begin
  589. ppufile.putstring(hp.u.realmodulename^);
  590. { the checksum should not affect the crc of this unit ! (PFV) }
  591. oldcrc:=ppufile.do_crc;
  592. ppufile.do_crc:=false;
  593. ppufile.putlongint(longint(hp.checksum));
  594. ppufile.putlongint(longint(hp.interface_checksum));
  595. ppufile.putlongint(longint(hp.indirect_checksum));
  596. ppufile.do_crc:=oldcrc;
  597. { combine all indirect checksums from units used by this unit }
  598. if intf then
  599. ppufile.indirect_crc:=ppufile.indirect_crc xor hp.indirect_checksum;
  600. end;
  601. hp:=tused_unit(hp.next);
  602. end;
  603. ppufile.do_interface_crc:=true;
  604. ppufile.writeentry(ibloadunit);
  605. end;
  606. procedure tppumodule.writelinkcontainer(var p:tlinkcontainer;id:byte;strippath:boolean);
  607. var
  608. hcontainer : tlinkcontainer;
  609. s : TPathStr;
  610. mask : cardinal;
  611. begin
  612. hcontainer:=TLinkContainer.Create;
  613. while not p.empty do
  614. begin
  615. s:=p.get(mask);
  616. if strippath then
  617. ppufile.putstring(ExtractFileName(s))
  618. else
  619. ppufile.putstring(s);
  620. ppufile.putlongint(mask);
  621. hcontainer.add(s,mask);
  622. end;
  623. ppufile.writeentry(id);
  624. p.Free;
  625. p:=hcontainer;
  626. end;
  627. procedure tppumodule.writederefmap;
  628. var
  629. i : longint;
  630. oldcrc : boolean;
  631. begin
  632. { This does not influence crc }
  633. oldcrc:=ppufile.do_crc;
  634. ppufile.do_crc:=false;
  635. { The unit map used for resolving }
  636. ppufile.putlongint(derefmapcnt);
  637. for i:=0 to derefmapcnt-1 do
  638. begin
  639. if not assigned(derefmap[i].u) then
  640. internalerror(2005011512);
  641. ppufile.putstring(derefmap[i].u.modulename^)
  642. end;
  643. ppufile.writeentry(ibderefmap);
  644. ppufile.do_crc:=oldcrc;
  645. end;
  646. procedure tppumodule.writederefdata;
  647. var
  648. oldcrc : boolean;
  649. len,hlen : longint;
  650. buf : array[0..1023] of byte;
  651. begin
  652. if longword(derefdataintflen)>derefdata.size then
  653. internalerror(200310223);
  654. derefdata.seek(0);
  655. { Write interface data }
  656. len:=derefdataintflen;
  657. while (len>0) do
  658. begin
  659. if len>1024 then
  660. hlen:=1024
  661. else
  662. hlen:=len;
  663. derefdata.read(buf,hlen);
  664. ppufile.putdata(buf,hlen);
  665. dec(len,hlen);
  666. end;
  667. { Write implementation data, this does not influence crc }
  668. oldcrc:=ppufile.do_crc;
  669. ppufile.do_crc:=false;
  670. len:=derefdata.size-derefdataintflen;
  671. while (len>0) do
  672. begin
  673. if len>1024 then
  674. hlen:=1024
  675. else
  676. hlen:=len;
  677. derefdata.read(buf,hlen);
  678. ppufile.putdata(buf,hlen);
  679. dec(len,hlen);
  680. end;
  681. if derefdata.pos<>derefdata.size then
  682. internalerror(200310224);
  683. ppufile.do_crc:=oldcrc;
  684. ppufile.writeentry(ibderefdata);
  685. end;
  686. procedure tppumodule.writeImportSymbols;
  687. var
  688. i,j : longint;
  689. ImportLibrary : TImportLibrary;
  690. ImportSymbol : TImportSymbol;
  691. begin
  692. for i:=0 to ImportLibraryList.Count-1 do
  693. begin
  694. ImportLibrary:=TImportLibrary(ImportLibraryList[i]);
  695. ppufile.putstring(ImportLibrary.Name);
  696. ppufile.putlongint(ImportLibrary.ImportSymbolList.Count);
  697. for j:=0 to ImportLibrary.ImportSymbolList.Count-1 do
  698. begin
  699. ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList[j]);
  700. ppufile.putstring(ImportSymbol.Name);
  701. ppufile.putstring(ImportSymbol.MangledName);
  702. ppufile.putlongint(ImportSymbol.OrdNr);
  703. ppufile.putbyte(byte(ImportSymbol.IsVar));
  704. end;
  705. end;
  706. ppufile.writeentry(ibImportSymbols);
  707. end;
  708. procedure tppumodule.writeasmsymbols;
  709. var
  710. sym : tasmsymbol;
  711. i : longint;
  712. begin
  713. ppufile.putlongint(globalasmsyms.count);
  714. for i:=0 to globalasmsyms.count-1 do
  715. begin
  716. sym:=tasmsymbol(globalasmsyms[i]);
  717. ppufile.putstring(sym.name);
  718. ppufile.putbyte(ord(sym.bind));
  719. ppufile.putbyte(ord(sym.typ));
  720. end;
  721. ppufile.writeentry(ibasmsymbols);
  722. end;
  723. procedure tppumodule.writeResources;
  724. var
  725. res : TCmdStrListItem;
  726. begin
  727. res:=TCmdStrListItem(ResourceFiles.First);
  728. while res<>nil do
  729. begin
  730. ppufile.putstring(res.FPStr);
  731. res:=TCmdStrListItem(res.Next);
  732. end;
  733. ppufile.writeentry(ibresources);
  734. end;
  735. procedure tppumodule.buildderefunitimportsyms;
  736. var
  737. deref : pderef;
  738. i : longint;
  739. begin
  740. for i:=0 to unitimportsyms.count-1 do
  741. begin
  742. new(deref);
  743. deref^.build(unitimportsyms[i]);
  744. unitimportsymsderefs.add(deref);
  745. end;
  746. end;
  747. procedure tppumodule.derefunitimportsyms;
  748. var
  749. i : longint;
  750. sym : tsym;
  751. begin
  752. { since this list can get quite large we clear it immediately after we derefd it }
  753. for i:=0 to unitimportsymsderefs.count-1 do
  754. begin
  755. sym:=tsym(pderef(unitimportsymsderefs[i])^.resolve);
  756. unitimportsyms.add(sym);
  757. dispose(pderef(unitimportsymsderefs[i]));
  758. end;
  759. unitimportsymsderefs.clear;
  760. end;
  761. procedure tppumodule.readunitimportsyms;
  762. var
  763. c,i : longint;
  764. deref : pderef;
  765. begin
  766. c:=ppufile.getlongint;
  767. writeln('loading: unit ', modulename^, ' has ', c, ' imported symbols');
  768. for i:=0 to c-1 do
  769. begin
  770. new(deref);
  771. ppufile.getderef(deref^);
  772. unitimportsymsderefs.add(deref);
  773. end;
  774. end;
  775. procedure tppumodule.writeunitimportsyms;
  776. var
  777. i : longint;
  778. begin
  779. writeln('writing: unit ', modulename^, ' has ', unitimportsymsderefs.Count, ' imported symbols');
  780. ppufile.putlongint(unitimportsymsderefs.count);
  781. for i:=0 to unitimportsymsderefs.count-1 do
  782. ppufile.putderef(pderef(unitimportsymsderefs[i])^);
  783. ppufile.writeentry(ibunitimportsyms);
  784. end;
  785. {$IFDEF MACRO_DIFF_HINT}
  786. {
  787. Define MACRO_DIFF_HINT for the whole compiler (and ppudump)
  788. to turn this facility on. Also the hint messages defined
  789. below must be commented in in the msg/errore.msg file.
  790. There is some problems with this, thats why it is shut off:
  791. At the first compilation, consider a macro which is not initially
  792. defined, but it is used (e g the check that it is undefined is true).
  793. Since it do not exist, there is no macro object where the is_used
  794. flag can be set. Later on when the macro is defined, and the ppu
  795. is opened, the check cannot detect this.
  796. Also, in which macro object should this flag be set ? It cant be set
  797. for macros in the initialmacrosymboltable since this table is shared
  798. between different files.
  799. }
  800. procedure tppumodule.readusedmacros;
  801. var
  802. hs : string;
  803. mac : tmacro;
  804. was_initial,
  805. was_used : boolean;
  806. {Reads macros which was defined or used when the module was compiled.
  807. This is done when a ppu file is open, before it possibly is parsed.}
  808. begin
  809. while not ppufile.endofentry do
  810. begin
  811. hs:=ppufile.getstring;
  812. was_initial:=boolean(ppufile.getbyte);
  813. was_used:=boolean(ppufile.getbyte);
  814. mac:=tmacro(initialmacrosymtable.Find(hs));
  815. if assigned(mac) then
  816. begin
  817. {$ifndef EXTDEBUG}
  818. { if we don't have the sources why tell }
  819. if sources_avail then
  820. {$endif ndef EXTDEBUG}
  821. if (not was_initial) and was_used then
  822. Message2(unit_h_cond_not_set_in_last_compile,hs,mainsource^);
  823. end
  824. else { not assigned }
  825. if was_initial and
  826. was_used then
  827. Message2(unit_h_cond_set_in_last_compile,hs,mainsource^);
  828. end;
  829. end;
  830. {$ENDIF}
  831. procedure tppumodule.readsourcefiles;
  832. var
  833. temp,hs : string;
  834. inc_path : string;
  835. temp_dir : TCmdStr;
  836. main_dir : TCmdStr;
  837. found,
  838. is_main : boolean;
  839. orgfiletime,
  840. source_time : longint;
  841. hp : tinputfile;
  842. begin
  843. sources_avail:=(flags and uf_release) = 0;
  844. is_main:=true;
  845. main_dir:='';
  846. while not ppufile.endofentry do
  847. begin
  848. hs:=SetDirSeparators(ppufile.getstring);
  849. inc_path:=ExtractFilePath(hs);
  850. orgfiletime:=ppufile.getlongint;
  851. temp_dir:='';
  852. if sources_avail then
  853. begin
  854. if (flags and uf_in_library)<>0 then
  855. begin
  856. sources_avail:=false;
  857. temp:=' library';
  858. end
  859. else if pos('Macro ',hs)=1 then
  860. begin
  861. { we don't want to find this file }
  862. { but there is a problem with file indexing !! }
  863. temp:='';
  864. end
  865. else
  866. begin
  867. { check the date of the source files:
  868. 1 path of ppu
  869. 2 path of main source
  870. 3 current dir
  871. 4 include/unit path }
  872. Source_Time:=GetNamedFileTime(path+hs);
  873. found:=false;
  874. if Source_Time<>-1 then
  875. hs:=path+hs
  876. else
  877. if not(is_main) then
  878. begin
  879. Source_Time:=GetNamedFileTime(main_dir+hs);
  880. if Source_Time<>-1 then
  881. hs:=main_dir+hs;
  882. end;
  883. if Source_Time=-1 then
  884. Source_Time:=GetNamedFileTime(hs);
  885. if (Source_Time=-1) then
  886. begin
  887. if is_main then
  888. found:=unitsearchpath.FindFile(hs,true,temp_dir)
  889. else
  890. found:=includesearchpath.FindFile(hs,true,temp_dir);
  891. if found then
  892. begin
  893. Source_Time:=GetNamedFileTime(temp_dir);
  894. if Source_Time<>-1 then
  895. hs:=temp_dir;
  896. end;
  897. end;
  898. if Source_Time<>-1 then
  899. begin
  900. if is_main then
  901. main_dir:=ExtractFilePath(hs);
  902. temp:=' time '+filetimestring(source_time);
  903. if (orgfiletime<>-1) and
  904. (source_time<>orgfiletime) then
  905. begin
  906. do_compile:=true;
  907. recompile_reason:=rr_sourcenewer;
  908. Message2(unit_u_source_modified,hs,ppufilename,@queuecomment);
  909. temp:=temp+' *';
  910. end;
  911. end
  912. else
  913. begin
  914. sources_avail:=false;
  915. temp:=' not found';
  916. end;
  917. hp:=tdosinputfile.create(hs);
  918. hp.inc_path:=inc_path;
  919. { the indexing is wrong here PM }
  920. sourcefiles.register_file(hp);
  921. end;
  922. end
  923. else
  924. temp:=' not available';
  925. if is_main then
  926. begin
  927. mainsource:=hs;
  928. end;
  929. Message1(unit_u_ppu_source,hs+temp,@queuecomment);
  930. is_main:=false;
  931. end;
  932. { check if we want to rebuild every unit, only if the sources are
  933. available }
  934. if do_build and sources_avail then
  935. begin
  936. do_compile:=true;
  937. recompile_reason:=rr_build;
  938. end;
  939. end;
  940. procedure tppumodule.readloadunit;
  941. var
  942. hs : string;
  943. pu : tused_unit;
  944. hp : tppumodule;
  945. indchecksum,
  946. intfchecksum,
  947. checksum : cardinal;
  948. begin
  949. while not ppufile.endofentry do
  950. begin
  951. hs:=ppufile.getstring;
  952. checksum:=cardinal(ppufile.getlongint);
  953. intfchecksum:=cardinal(ppufile.getlongint);
  954. indchecksum:=cardinal(ppufile.getlongint);
  955. { set the state of this unit before registering, this is
  956. needed for a correct circular dependency check }
  957. hp:=registerunit(self,hs,'');
  958. pu:=addusedunit(hp,false,nil);
  959. pu.checksum:=checksum;
  960. pu.interface_checksum:=intfchecksum;
  961. pu.indirect_checksum:=indchecksum;
  962. end;
  963. in_interface:=false;
  964. end;
  965. procedure tppumodule.readlinkcontainer(var p:tlinkcontainer);
  966. var
  967. s : string;
  968. m : longint;
  969. begin
  970. while not ppufile.endofentry do
  971. begin
  972. s:=ppufile.getstring;
  973. m:=ppufile.getlongint;
  974. p.add(s,m);
  975. end;
  976. end;
  977. procedure tppumodule.readderefmap;
  978. var
  979. i : longint;
  980. begin
  981. { Load unit map used for resolving }
  982. derefmapsize:=ppufile.getlongint;
  983. derefmapcnt:=derefmapsize;
  984. getmem(derefmap,derefmapsize*sizeof(tderefmaprec));
  985. fillchar(derefmap^,derefmapsize*sizeof(tderefmaprec),0);
  986. for i:=0 to derefmapsize-1 do
  987. derefmap[i].modulename:=stringdup(ppufile.getstring);
  988. end;
  989. procedure tppumodule.readderefdata;
  990. var
  991. len,hlen : longint;
  992. buf : array[0..1023] of byte;
  993. begin
  994. len:=ppufile.entrysize;
  995. while (len>0) do
  996. begin
  997. if len>1024 then
  998. hlen:=1024
  999. else
  1000. hlen:=len;
  1001. ppufile.getdata(buf,hlen);
  1002. derefdata.write(buf,hlen);
  1003. dec(len,hlen);
  1004. end;
  1005. end;
  1006. procedure tppumodule.readImportSymbols;
  1007. var
  1008. j,
  1009. extsymcnt : longint;
  1010. ImportLibrary : TImportLibrary;
  1011. extsymname : string;
  1012. extsymmangledname : string;
  1013. extsymordnr : longint;
  1014. extsymisvar : boolean;
  1015. begin
  1016. while not ppufile.endofentry do
  1017. begin
  1018. ImportLibrary:=TImportLibrary.Create(ImportLibraryList,ppufile.getstring);
  1019. extsymcnt:=ppufile.getlongint;
  1020. for j:=0 to extsymcnt-1 do
  1021. begin
  1022. extsymname:=ppufile.getstring;
  1023. extsymmangledname:=ppufile.getstring;
  1024. extsymordnr:=ppufile.getlongint;
  1025. extsymisvar:=(ppufile.getbyte<>0);
  1026. TImportSymbol.Create(ImportLibrary.ImportSymbolList,extsymname,
  1027. extsymmangledname,extsymordnr,extsymisvar);
  1028. end;
  1029. end;
  1030. end;
  1031. procedure tppumodule.readResources;
  1032. begin
  1033. while not ppufile.endofentry do
  1034. resourcefiles.Insert(ppufile.getstring);
  1035. end;
  1036. procedure tppumodule.readasmsymbols;
  1037. var
  1038. cnt : longint;
  1039. s : string;
  1040. bind : tasmsymbind;
  1041. typ : tasmsymtype;
  1042. begin
  1043. cnt:=ppufile.getlongint;
  1044. writeln('loading: unit ', modulename^, ' has ', cnt, ' imported asm symbols');
  1045. while not ppufile.endofentry and not ppufile.error do
  1046. begin
  1047. s:=ppufile.getstring;
  1048. bind:=tasmsymbind(ppufile.getbyte);
  1049. typ:=tasmsymtype(ppufile.getbyte);
  1050. tasmsymbol.create(globalasmsyms,s,bind,typ);
  1051. end;
  1052. end;
  1053. procedure tppumodule.readwpofile;
  1054. var
  1055. orgwpofilename: string;
  1056. orgwpofiletime: longint;
  1057. begin
  1058. { check whether we are using the same wpo feedback input file as when
  1059. this unit was compiled (same file name and file date)
  1060. }
  1061. orgwpofilename:=ppufile.getstring;
  1062. orgwpofiletime:=ppufile.getlongint;
  1063. if (extractfilename(orgwpofilename)<>extractfilename(wpofeedbackinput)) or
  1064. (orgwpofiletime<>GetNamedFileTime(orgwpofilename)) then
  1065. { make sure we don't throw away a precompiled unit if the user simply
  1066. forgot to specify the right wpo feedback file
  1067. }
  1068. message3(unit_e_different_wpo_file,ppufilename,orgwpofilename,filetimestring(orgwpofiletime));
  1069. end;
  1070. procedure tppumodule.load_interface;
  1071. var
  1072. b : byte;
  1073. newmodulename : string;
  1074. begin
  1075. { read interface part }
  1076. repeat
  1077. b:=ppufile.readentry;
  1078. case b of
  1079. ibjvmnamespace :
  1080. begin
  1081. namespace:=stringdup(ppufile.getstring);
  1082. end;
  1083. ibmodulename :
  1084. begin
  1085. newmodulename:=ppufile.getstring;
  1086. if (cs_check_unit_name in current_settings.globalswitches) and
  1087. (upper(newmodulename)<>modulename^) then
  1088. Message2(unit_f_unit_name_error,realmodulename^,newmodulename);
  1089. stringdispose(modulename);
  1090. stringdispose(realmodulename);
  1091. modulename:=stringdup(upper(newmodulename));
  1092. realmodulename:=stringdup(newmodulename);
  1093. end;
  1094. ibmoduleoptions:
  1095. begin
  1096. ppufile.getsmallset(moduleoptions);
  1097. if mo_has_deprecated_msg in moduleoptions then
  1098. begin
  1099. stringdispose(deprecatedmsg);
  1100. deprecatedmsg:=stringdup(ppufile.getstring);
  1101. end;
  1102. end;
  1103. ibsourcefiles :
  1104. readsourcefiles;
  1105. {$IFDEF MACRO_DIFF_HINT}
  1106. ibusedmacros :
  1107. readusedmacros;
  1108. {$ENDIF}
  1109. ibloadunit :
  1110. readloadunit;
  1111. iblinkunitofiles :
  1112. readlinkcontainer(LinkUnitOFiles);
  1113. iblinkunitstaticlibs :
  1114. readlinkcontainer(LinkUnitStaticLibs);
  1115. iblinkunitsharedlibs :
  1116. readlinkcontainer(LinkUnitSharedLibs);
  1117. iblinkotherofiles :
  1118. readlinkcontainer(LinkotherOFiles);
  1119. iblinkotherstaticlibs :
  1120. readlinkcontainer(LinkotherStaticLibs);
  1121. iblinkothersharedlibs :
  1122. readlinkcontainer(LinkotherSharedLibs);
  1123. iblinkotherframeworks :
  1124. readlinkcontainer(LinkOtherFrameworks);
  1125. ibmainname:
  1126. begin
  1127. mainname:=stringdup(ppufile.getstring);
  1128. if (mainaliasname<>defaultmainaliasname) then
  1129. Message1(scan_w_multiple_main_name_overrides,mainaliasname);
  1130. mainaliasname:=mainname^;
  1131. end;
  1132. ibImportSymbols :
  1133. readImportSymbols;
  1134. ibderefmap :
  1135. readderefmap;
  1136. ibderefdata :
  1137. readderefdata;
  1138. ibresources:
  1139. readResources;
  1140. ibwpofile:
  1141. readwpofile;
  1142. ibendinterface :
  1143. break;
  1144. else
  1145. Message1(unit_f_ppu_invalid_entry,tostr(b));
  1146. end;
  1147. { we can already stop when we know that we must recompile }
  1148. if do_compile then
  1149. exit;
  1150. until false;
  1151. end;
  1152. procedure tppumodule.load_implementation;
  1153. var
  1154. b : byte;
  1155. begin
  1156. { read implementation part }
  1157. repeat
  1158. b:=ppufile.readentry;
  1159. case b of
  1160. ibloadunit :
  1161. readloadunit;
  1162. ibasmsymbols :
  1163. readasmsymbols;
  1164. ibunitimportsyms :
  1165. readunitimportsyms;
  1166. ibendimplementation :
  1167. break;
  1168. else
  1169. Message1(unit_f_ppu_invalid_entry,tostr(b));
  1170. end;
  1171. until false;
  1172. end;
  1173. procedure tppumodule.writeppu;
  1174. begin
  1175. Message1(unit_u_ppu_write,realmodulename^);
  1176. { create unit flags }
  1177. if do_release then
  1178. flags:=flags or uf_release;
  1179. if assigned(localsymtable) then
  1180. flags:=flags or uf_local_symtable;
  1181. {$ifdef i8086}
  1182. if current_settings.x86memorymodel in [mm_medium,mm_large,mm_huge] then
  1183. flags:=flags or uf_i8086_far_code;
  1184. if current_settings.x86memorymodel in [mm_compact,mm_large] then
  1185. flags:=flags or uf_i8086_far_data;
  1186. if current_settings.x86memorymodel=mm_huge then
  1187. flags:=flags or uf_i8086_huge_data;
  1188. if current_settings.x86memorymodel=mm_tiny then
  1189. flags:=flags or uf_i8086_cs_equals_ds;
  1190. {$endif i8086}
  1191. {$ifdef cpufpemu}
  1192. if (cs_fp_emulation in current_settings.moduleswitches) then
  1193. flags:=flags or uf_fpu_emulation;
  1194. {$endif cpufpemu}
  1195. {$ifdef Test_Double_checksum_write}
  1196. Assign(CRCFile,s+'.IMP');
  1197. Rewrite(CRCFile);
  1198. {$endif def Test_Double_checksum_write}
  1199. { create new ppufile }
  1200. ppufile:=tcompilerppufile.create(ppufilename);
  1201. if not ppufile.createfile then
  1202. Message(unit_f_ppu_cannot_write);
  1203. { first the (JVM) namespace }
  1204. if assigned(namespace) then
  1205. begin
  1206. ppufile.putstring(namespace^);
  1207. ppufile.writeentry(ibjvmnamespace);
  1208. end;
  1209. { the unitname }
  1210. ppufile.putstring(realmodulename^);
  1211. ppufile.writeentry(ibmodulename);
  1212. ppufile.putsmallset(moduleoptions);
  1213. if mo_has_deprecated_msg in moduleoptions then
  1214. ppufile.putstring(deprecatedmsg^);
  1215. ppufile.writeentry(ibmoduleoptions);
  1216. { write the alternate main procedure name if any }
  1217. if assigned(mainname) then
  1218. begin
  1219. ppufile.putstring(mainname^);
  1220. ppufile.writeentry(ibmainname);
  1221. end;
  1222. writesourcefiles;
  1223. {$IFDEF MACRO_DIFF_HINT}
  1224. writeusedmacros;
  1225. {$ENDIF}
  1226. { write interface uses }
  1227. writeusedunit(true);
  1228. { write the objectfiles and libraries that come for this unit,
  1229. preserve the containers because they are still needed to load
  1230. the link.res.
  1231. All doesn't depend on the crc! It doesn't matter
  1232. if a unit is in a .o or .a file }
  1233. ppufile.do_crc:=false;
  1234. { write after source files, so that we know whether or not the compiler
  1235. will recompile the unit when checking whether the correct wpo file is
  1236. used (if it will recompile the unit anyway, it doesn't matter)
  1237. }
  1238. if (wpofeedbackinput<>'') then
  1239. begin
  1240. ppufile.putstring(wpofeedbackinput);
  1241. ppufile.putlongint(getnamedfiletime(wpofeedbackinput));
  1242. ppufile.writeentry(ibwpofile);
  1243. end;
  1244. writelinkcontainer(linkunitofiles,iblinkunitofiles,true);
  1245. writelinkcontainer(linkunitstaticlibs,iblinkunitstaticlibs,true);
  1246. writelinkcontainer(linkunitsharedlibs,iblinkunitsharedlibs,true);
  1247. writelinkcontainer(linkotherofiles,iblinkotherofiles,false);
  1248. writelinkcontainer(linkotherstaticlibs,iblinkotherstaticlibs,true);
  1249. writelinkcontainer(linkothersharedlibs,iblinkothersharedlibs,true);
  1250. writelinkcontainer(linkotherframeworks,iblinkotherframeworks,true);
  1251. writeImportSymbols;
  1252. writeResources;
  1253. ppufile.do_crc:=true;
  1254. { generate implementation deref data, the interface deref data is
  1255. already generated when calculating the interface crc }
  1256. if (cs_compilesystem in current_settings.moduleswitches) then
  1257. begin
  1258. tstoredsymtable(globalsymtable).buildderef;
  1259. derefdataintflen:=derefdata.size;
  1260. end
  1261. else
  1262. { the unit may have been re-resolved, in which case the current
  1263. position in derefdata is not necessarily at the end }
  1264. derefdata.seek(derefdata.size);
  1265. tstoredsymtable(globalsymtable).buildderefimpl;
  1266. if (flags and uf_local_symtable)<>0 then
  1267. begin
  1268. tstoredsymtable(localsymtable).buildderef;
  1269. tstoredsymtable(localsymtable).buildderefimpl;
  1270. end;
  1271. tunitwpoinfo(wpoinfo).buildderef;
  1272. tunitwpoinfo(wpoinfo).buildderefimpl;
  1273. buildderefunitimportsyms;
  1274. writederefmap;
  1275. writederefdata;
  1276. ppufile.writeentry(ibendinterface);
  1277. { write the symtable entries }
  1278. tstoredsymtable(globalsymtable).ppuwrite(ppufile);
  1279. if assigned(globalmacrosymtable) and (globalmacrosymtable.SymList.count > 0) then
  1280. begin
  1281. ppufile.putbyte(byte(true));
  1282. ppufile.writeentry(ibexportedmacros);
  1283. tstoredsymtable(globalmacrosymtable).ppuwrite(ppufile);
  1284. end
  1285. else
  1286. begin
  1287. ppufile.putbyte(byte(false));
  1288. ppufile.writeentry(ibexportedmacros);
  1289. end;
  1290. { everything after this doesn't affect the crc }
  1291. ppufile.do_crc:=false;
  1292. { write implementation uses }
  1293. writeusedunit(false);
  1294. { write global assembler symbols }
  1295. writeasmsymbols;
  1296. { write all symbols imported from another unit }
  1297. writeunitimportsyms;
  1298. { end of implementation }
  1299. ppufile.writeentry(ibendimplementation);
  1300. { write static symtable
  1301. needed for local debugging of unit functions }
  1302. if (flags and uf_local_symtable)<>0 then
  1303. tstoredsymtable(localsymtable).ppuwrite(ppufile);
  1304. { write whole program optimisation-related information }
  1305. tunitwpoinfo(wpoinfo).ppuwrite(ppufile);
  1306. { the last entry ibend is written automatically }
  1307. { flush to be sure }
  1308. ppufile.flush;
  1309. { create and write header }
  1310. ppufile.header.common.size:=ppufile.size;
  1311. ppufile.header.checksum:=ppufile.crc;
  1312. ppufile.header.interface_checksum:=ppufile.interface_crc;
  1313. ppufile.header.indirect_checksum:=ppufile.indirect_crc;
  1314. ppufile.header.common.compiler:=wordversion;
  1315. ppufile.header.common.cpu:=word(target_cpu);
  1316. ppufile.header.common.target:=word(target_info.system);
  1317. ppufile.header.common.flags:=flags;
  1318. ppufile.header.deflistsize:=current_module.deflist.count;
  1319. ppufile.header.symlistsize:=current_module.symlist.count;
  1320. ppufile.writeheader;
  1321. { save crc in current module also }
  1322. crc:=ppufile.crc;
  1323. interface_crc:=ppufile.interface_crc;
  1324. indirect_crc:=ppufile.indirect_crc;
  1325. {$ifdef Test_Double_checksum_write}
  1326. close(CRCFile);
  1327. {$endif Test_Double_checksum_write}
  1328. ppufile.closefile;
  1329. ppufile.free;
  1330. ppufile:=nil;
  1331. end;
  1332. procedure tppumodule.getppucrc;
  1333. begin
  1334. {$ifdef Test_Double_checksum_write}
  1335. Assign(CRCFile,s+'.INT')
  1336. Rewrite(CRCFile);
  1337. {$endif def Test_Double_checksum_write}
  1338. { create new ppufile }
  1339. ppufile:=tcompilerppufile.create(ppufilename);
  1340. ppufile.crc_only:=true;
  1341. if not ppufile.createfile then
  1342. Message(unit_f_ppu_cannot_write);
  1343. { first the (JVM) namespace }
  1344. if assigned(namespace) then
  1345. begin
  1346. ppufile.putstring(namespace^);
  1347. ppufile.writeentry(ibjvmnamespace);
  1348. end;
  1349. { the unitname }
  1350. ppufile.putstring(realmodulename^);
  1351. ppufile.writeentry(ibmodulename);
  1352. ppufile.putsmallset(moduleoptions);
  1353. if mo_has_deprecated_msg in moduleoptions then
  1354. ppufile.putstring(deprecatedmsg^);
  1355. ppufile.writeentry(ibmoduleoptions);
  1356. { the interface units affect the crc }
  1357. writeusedunit(true);
  1358. { deref data of interface that affect the crc }
  1359. derefdata.reset;
  1360. tstoredsymtable(globalsymtable).buildderef;
  1361. derefdataintflen:=derefdata.size;
  1362. writederefmap;
  1363. writederefdata;
  1364. ppufile.writeentry(ibendinterface);
  1365. { write the symtable entries }
  1366. tstoredsymtable(globalsymtable).ppuwrite(ppufile);
  1367. if assigned(globalmacrosymtable) and (globalmacrosymtable.SymList.count > 0) then
  1368. begin
  1369. ppufile.putbyte(byte(true));
  1370. ppufile.writeentry(ibexportedmacros);
  1371. tstoredsymtable(globalmacrosymtable).ppuwrite(ppufile);
  1372. end
  1373. else
  1374. begin
  1375. ppufile.putbyte(byte(false));
  1376. ppufile.writeentry(ibexportedmacros);
  1377. end;
  1378. { save crc }
  1379. crc:=ppufile.crc;
  1380. interface_crc:=ppufile.interface_crc;
  1381. indirect_crc:=ppufile.indirect_crc;
  1382. { end of implementation, to generate a correct ppufile
  1383. for ppudump when using INTFPPU define }
  1384. ppufile.writeentry(ibendimplementation);
  1385. {$ifdef Test_Double_checksum}
  1386. crc_array:=ppufile.crc_test;
  1387. ppufile.crc_test:=nil;
  1388. crc_size:=ppufile.crc_index2;
  1389. crc_array2:=ppufile.crc_test2;
  1390. ppufile.crc_test2:=nil;
  1391. crc_size2:=ppufile.crc_index2;
  1392. {$endif Test_Double_checksum}
  1393. {$ifdef Test_Double_checksum_write}
  1394. close(CRCFile);
  1395. {$endif Test_Double_checksum_write}
  1396. { create and write header, this will only be used
  1397. for debugging purposes }
  1398. ppufile.header.common.size:=ppufile.size;
  1399. ppufile.header.checksum:=ppufile.crc;
  1400. ppufile.header.interface_checksum:=ppufile.interface_crc;
  1401. ppufile.header.indirect_checksum:=ppufile.indirect_crc;
  1402. ppufile.header.common.compiler:=wordversion;
  1403. ppufile.header.common.cpu:=word(target_cpu);
  1404. ppufile.header.common.target:=word(target_info.system);
  1405. ppufile.header.common.flags:=flags;
  1406. ppufile.writeheader;
  1407. ppufile.closefile;
  1408. ppufile.free;
  1409. ppufile:=nil;
  1410. end;
  1411. procedure tppumodule.load_usedunits;
  1412. var
  1413. pu : tused_unit;
  1414. begin
  1415. if current_module<>self then
  1416. internalerror(200212284);
  1417. { load the used units from interface }
  1418. in_interface:=true;
  1419. pu:=tused_unit(used_units.first);
  1420. while assigned(pu) do
  1421. begin
  1422. if pu.in_interface then
  1423. begin
  1424. tppumodule(pu.u).loadppu;
  1425. { if this unit is compiled we can stop }
  1426. if state=ms_compiled then
  1427. exit;
  1428. { add this unit to the dependencies }
  1429. pu.u.adddependency(self);
  1430. { need to recompile the current unit, check the interface
  1431. crc. And when not compiled with -Ur then check the complete
  1432. crc }
  1433. if (pu.u.interface_crc<>pu.interface_checksum) or
  1434. (pu.u.indirect_crc<>pu.indirect_checksum) or
  1435. (
  1436. ((ppufile.header.common.flags and uf_release)=0) and
  1437. (pu.u.crc<>pu.checksum)
  1438. ) then
  1439. begin
  1440. Message2(unit_u_recompile_crc_change,realmodulename^,pu.u.realmodulename^,@queuecomment);
  1441. {$ifdef DEBUG_UNIT_CRC_CHANGES}
  1442. if (pu.u.interface_crc<>pu.interface_checksum) then
  1443. writeln(' intfcrc change: ',hexstr(pu.u.interface_crc,8),' <> ',hexstr(pu.interface_checksum,8))
  1444. else if (pu.u.indirect_crc<>pu.indirect_checksum) then
  1445. writeln(' indcrc change: ',hexstr(pu.u.indirect_crc,8),' <> ',hexstr(pu.indirect_checksum,8))
  1446. else
  1447. writeln(' implcrc change: ',hexstr(pu.u.crc,8),' <> ',hexstr(pu.checksum,8));
  1448. {$endif DEBUG_UNIT_CRC_CHANGES}
  1449. recompile_reason:=rr_crcchanged;
  1450. do_compile:=true;
  1451. exit;
  1452. end;
  1453. end;
  1454. pu:=tused_unit(pu.next);
  1455. end;
  1456. { ok, now load the interface of this unit }
  1457. if current_module<>self then
  1458. internalerror(200208187);
  1459. deflist.count:=ppufile.header.deflistsize;
  1460. symlist.count:=ppufile.header.symlistsize;
  1461. globalsymtable:=tglobalsymtable.create(modulename^,moduleid);
  1462. tstoredsymtable(globalsymtable).ppuload(ppufile);
  1463. if ppufile.readentry<>ibexportedmacros then
  1464. Message(unit_f_ppu_read_error);
  1465. if boolean(ppufile.getbyte) then
  1466. begin
  1467. globalmacrosymtable:=tmacrosymtable.Create(true);
  1468. tstoredsymtable(globalmacrosymtable).ppuload(ppufile)
  1469. end;
  1470. interface_compiled:=true;
  1471. { read the implementation part, containing
  1472. the implementation uses and ObjData }
  1473. in_interface:=false;
  1474. load_implementation;
  1475. { now only read the implementation uses }
  1476. pu:=tused_unit(used_units.first);
  1477. while assigned(pu) do
  1478. begin
  1479. if (not pu.in_interface) then
  1480. begin
  1481. tppumodule(pu.u).loadppu;
  1482. { if this unit is compiled we can stop }
  1483. if state=ms_compiled then
  1484. exit;
  1485. { add this unit to the dependencies }
  1486. pu.u.adddependency(self);
  1487. { need to recompile the current unit ? }
  1488. if (pu.u.interface_crc<>pu.interface_checksum) or
  1489. (pu.u.indirect_crc<>pu.indirect_checksum) then
  1490. begin
  1491. Message2(unit_u_recompile_crc_change,realmodulename^,pu.u.realmodulename^+' {impl}',@queuecomment);
  1492. {$ifdef DEBUG_UNIT_CRC_CHANGES}
  1493. if (pu.u.interface_crc<>pu.interface_checksum) then
  1494. writeln(' intfcrc change (2): ',hexstr(pu.u.interface_crc,8),' <> ',hexstr(pu.interface_checksum,8))
  1495. else if (pu.u.indirect_crc<>pu.indirect_checksum) then
  1496. writeln(' indcrc change (2): ',hexstr(pu.u.indirect_crc,8),' <> ',hexstr(pu.indirect_checksum,8));
  1497. {$endif DEBUG_UNIT_CRC_CHANGES}
  1498. recompile_reason:=rr_crcchanged;
  1499. do_compile:=true;
  1500. exit;
  1501. end;
  1502. end;
  1503. pu:=tused_unit(pu.next);
  1504. end;
  1505. { load implementation symtable }
  1506. if (flags and uf_local_symtable)<>0 then
  1507. begin
  1508. localsymtable:=tstaticsymtable.create(modulename^,moduleid);
  1509. tstaticsymtable(localsymtable).ppuload(ppufile);
  1510. end;
  1511. { we can now derefence all pointers to the implementation parts }
  1512. tstoredsymtable(globalsymtable).derefimpl;
  1513. if assigned(localsymtable) then
  1514. tstoredsymtable(localsymtable).derefimpl;
  1515. derefunitimportsyms;
  1516. { read whole program optimisation-related information }
  1517. wpoinfo:=tunitwpoinfo.ppuload(ppufile);
  1518. tunitwpoinfo(wpoinfo).deref;
  1519. tunitwpoinfo(wpoinfo).derefimpl;
  1520. end;
  1521. function tppumodule.needrecompile:boolean;
  1522. var
  1523. pu : tused_unit;
  1524. begin
  1525. result:=false;
  1526. pu:=tused_unit(used_units.first);
  1527. while assigned(pu) do
  1528. begin
  1529. { need to recompile the current unit, check the interface
  1530. crc. And when not compiled with -Ur then check the complete
  1531. crc }
  1532. if (pu.u.interface_crc<>pu.interface_checksum) or
  1533. (pu.u.indirect_crc<>pu.indirect_checksum) or
  1534. (
  1535. (pu.in_interface) and
  1536. (pu.u.crc<>pu.checksum)
  1537. ) then
  1538. begin
  1539. {$ifdef DEBUG_UNIT_CRC_CHANGES}
  1540. if (pu.u.interface_crc<>pu.interface_checksum) then
  1541. writeln(' intfcrc change (3): ',hexstr(pu.u.interface_crc,8),' <> ',hexstr(pu.interface_checksum,8))
  1542. else if (pu.u.indirect_crc<>pu.indirect_checksum) then
  1543. writeln(' indcrc change (3): ',hexstr(pu.u.indirect_crc,8),' <> ',hexstr(pu.indirect_checksum,8))
  1544. else
  1545. writeln(' implcrc change (3): ',hexstr(pu.u.crc,8),' <> ',hexstr(pu.checksum,8));
  1546. {$endif DEBUG_UNIT_CRC_CHANGES}
  1547. result:=true;
  1548. exit;
  1549. end;
  1550. pu:=tused_unit(pu.next);
  1551. end;
  1552. end;
  1553. procedure tppumodule.setdefgeneration;
  1554. begin
  1555. defsgeneration:=currentdefgeneration;
  1556. inc(currentdefgeneration);
  1557. end;
  1558. procedure tppumodule.reload_flagged_units;
  1559. var
  1560. hp : tppumodule;
  1561. begin
  1562. { now reload all dependent units with outdated defs }
  1563. hp:=tppumodule(loaded_units.first);
  1564. while assigned(hp) do
  1565. begin
  1566. if hp.do_reload and
  1567. (hp.defsgeneration<defsgeneration) then
  1568. begin
  1569. hp.defsgeneration:=defsgeneration;
  1570. hp.loadppu
  1571. end
  1572. else
  1573. hp.do_reload:=false;
  1574. hp:=tppumodule(hp.next);
  1575. end;
  1576. end;
  1577. procedure tppumodule.end_of_parsing;
  1578. begin
  1579. { module is now compiled }
  1580. state:=ms_compiled;
  1581. { free ppu }
  1582. if assigned(ppufile) then
  1583. begin
  1584. ppufile.free;
  1585. ppufile:=nil;
  1586. end;
  1587. inherited end_of_parsing;
  1588. end;
  1589. procedure tppumodule.loadppu;
  1590. const
  1591. ImplIntf : array[boolean] of string[15]=('implementation','interface');
  1592. var
  1593. do_load,
  1594. second_time : boolean;
  1595. old_current_module : tmodule;
  1596. pu : tused_unit;
  1597. begin
  1598. old_current_module:=current_module;
  1599. Message3(unit_u_load_unit,old_current_module.modulename^,
  1600. ImplIntf[old_current_module.in_interface],
  1601. modulename^);
  1602. { Update loaded_from to detect cycles }
  1603. loaded_from:=old_current_module;
  1604. { check if the globalsymtable is already available, but
  1605. we must reload when the do_reload flag is set }
  1606. if (not do_reload) and
  1607. assigned(globalsymtable) then
  1608. exit;
  1609. { reset }
  1610. do_load:=true;
  1611. second_time:=false;
  1612. set_current_module(self);
  1613. { try to load it as a package unit first }
  1614. if (packagelist.count>0) and loadfrompackage then
  1615. begin
  1616. do_load:=false;
  1617. do_reload:=false;
  1618. state:=ms_load;
  1619. { add the unit to the used units list of the program }
  1620. usedunits.concat(tused_unit.create(self,true,false,nil));
  1621. end;
  1622. { A force reload }
  1623. if do_reload then
  1624. begin
  1625. Message(unit_u_forced_reload);
  1626. do_reload:=false;
  1627. { When the unit is already loaded or being loaded
  1628. we can maybe skip a complete reload/recompile }
  1629. if assigned(globalsymtable) and
  1630. (not needrecompile) then
  1631. begin
  1632. { When we don't have any data stored yet there
  1633. is nothing to resolve }
  1634. if interface_compiled then
  1635. begin
  1636. Message1(unit_u_reresolving_unit,modulename^);
  1637. tstoredsymtable(globalsymtable).deref;
  1638. tstoredsymtable(globalsymtable).derefimpl;
  1639. if assigned(localsymtable) then
  1640. begin
  1641. tstoredsymtable(localsymtable).deref;
  1642. tstoredsymtable(localsymtable).derefimpl;
  1643. end;
  1644. if assigned(wpoinfo) then
  1645. begin
  1646. tunitwpoinfo(wpoinfo).deref;
  1647. tunitwpoinfo(wpoinfo).derefimpl;
  1648. end;
  1649. { We have to flag the units that depend on this unit even
  1650. though it didn't change, because they might also
  1651. indirectly depend on the unit that did change (e.g.,
  1652. in case rgobj, rgx86 and rgcpu have been compiled
  1653. already, and then rgobj is recompiled for some reason
  1654. -> rgx86 is re-reresolved, but the vmtentries of trgcpu
  1655. must also be re-resolved, because they will also contain
  1656. pointers to procdefs in the old trgobj (in case of a
  1657. recompile, all old defs are freed) }
  1658. flagdependent(old_current_module);
  1659. reload_flagged_units;
  1660. end
  1661. else
  1662. Message1(unit_u_skipping_reresolving_unit,modulename^);
  1663. do_load:=false;
  1664. end;
  1665. end;
  1666. if do_load then
  1667. begin
  1668. { loading the unit for a second time? }
  1669. if state=ms_registered then
  1670. state:=ms_load
  1671. else
  1672. begin
  1673. { try to load the unit a second time first }
  1674. Message1(unit_u_second_load_unit,modulename^);
  1675. Message2(unit_u_previous_state,modulename^,ModuleStateStr[state]);
  1676. { Flag modules to reload }
  1677. flagdependent(old_current_module);
  1678. { Reset the module }
  1679. reset;
  1680. if state in [ms_compile,ms_second_compile] then
  1681. begin
  1682. Message1(unit_u_second_compile_unit,modulename^);
  1683. state:=ms_second_compile;
  1684. do_compile:=true;
  1685. end
  1686. else
  1687. state:=ms_second_load;
  1688. second_time:=true;
  1689. end;
  1690. { close old_current_ppu on system that are
  1691. short on file handles like DOS PM }
  1692. {$ifdef SHORT_ON_FILE_HANDLES}
  1693. if old_current_module.is_unit and
  1694. assigned(tppumodule(old_current_module).ppufile) then
  1695. tppumodule(old_current_module).ppufile.tempclose;
  1696. {$endif SHORT_ON_FILE_HANDLES}
  1697. { try to opening ppu, skip this when we already
  1698. know that we need to compile the unit }
  1699. if not do_compile then
  1700. begin
  1701. Message1(unit_u_loading_unit,modulename^);
  1702. search_unit_files(false);
  1703. if not do_compile then
  1704. begin
  1705. load_interface;
  1706. setdefgeneration;
  1707. if not do_compile then
  1708. begin
  1709. load_usedunits;
  1710. if not do_compile then
  1711. Message1(unit_u_finished_loading_unit,modulename^);
  1712. end;
  1713. end;
  1714. { PPU is not needed anymore }
  1715. if assigned(ppufile) then
  1716. begin
  1717. ppufile.closefile;
  1718. ppufile.free;
  1719. ppufile:=nil;
  1720. end;
  1721. end;
  1722. { Do we need to recompile the unit }
  1723. if do_compile then
  1724. begin
  1725. { recompile the unit or give a fatal error if sources not available }
  1726. if not(sources_avail) then
  1727. begin
  1728. search_unit_files(true);
  1729. if not(sources_avail) then
  1730. begin
  1731. printcomments;
  1732. if recompile_reason=rr_noppu then
  1733. begin
  1734. pu:=tused_unit(loaded_from.used_units.first);
  1735. while assigned(pu) do
  1736. begin
  1737. if pu.u=self then
  1738. break;
  1739. pu:=tused_unit(pu.next);
  1740. end;
  1741. if assigned(pu) and assigned(pu.unitsym) then
  1742. MessagePos2(pu.unitsym.fileinfo,unit_f_cant_find_ppu,realmodulename^,loaded_from.realmodulename^)
  1743. else
  1744. Message2(unit_f_cant_find_ppu,realmodulename^,loaded_from.realmodulename^);
  1745. end
  1746. else
  1747. Message1(unit_f_cant_compile_unit,realmodulename^);
  1748. end;
  1749. end;
  1750. { we found the sources, we do not need the verbose messages anymore }
  1751. if comments <> nil then
  1752. begin
  1753. comments.free;
  1754. comments:=nil;
  1755. end;
  1756. { Flag modules to reload }
  1757. flagdependent(old_current_module);
  1758. { Reset the module }
  1759. reset;
  1760. { compile this module }
  1761. if not(state in [ms_compile,ms_second_compile]) then
  1762. state:=ms_compile;
  1763. compile(mainsource);
  1764. setdefgeneration;
  1765. end
  1766. else
  1767. state:=ms_compiled;
  1768. if current_module<>self then
  1769. internalerror(200212282);
  1770. if in_interface then
  1771. internalerror(200212283);
  1772. { for a second_time recompile reload all dependent units,
  1773. for a first time compile register the unit _once_ }
  1774. if second_time then
  1775. reload_flagged_units
  1776. else
  1777. usedunits.concat(tused_unit.create(self,true,false,nil));
  1778. { reopen the old module }
  1779. {$ifdef SHORT_ON_FILE_HANDLES}
  1780. if old_current_module.is_unit and
  1781. assigned(tppumodule(old_current_module).ppufile) then
  1782. tppumodule(old_current_module).ppufile.tempopen;
  1783. {$endif SHORT_ON_FILE_HANDLES}
  1784. end;
  1785. { we are back, restore current_module }
  1786. set_current_module(old_current_module);
  1787. end;
  1788. {*****************************************************************************
  1789. RegisterUnit
  1790. *****************************************************************************}
  1791. function registerunit(callermodule:tmodule;const s : TIDString;const fn:string) : tppumodule;
  1792. var
  1793. ups : TIDString;
  1794. hp : tppumodule;
  1795. hp2 : tmodule;
  1796. begin
  1797. { Info }
  1798. ups:=upper(s);
  1799. { search all loaded units }
  1800. hp:=tppumodule(loaded_units.first);
  1801. while assigned(hp) do
  1802. begin
  1803. if hp.modulename^=ups then
  1804. begin
  1805. { only check for units. The main program is also
  1806. as a unit in the loaded_units list. We simply need
  1807. to ignore this entry (PFV) }
  1808. if hp.is_unit then
  1809. begin
  1810. { both units in interface ? }
  1811. if callermodule.in_interface and
  1812. hp.in_interface then
  1813. begin
  1814. { check for a cycle }
  1815. hp2:=callermodule.loaded_from;
  1816. while assigned(hp2) and (hp2<>hp) do
  1817. begin
  1818. if hp2.in_interface then
  1819. hp2:=hp2.loaded_from
  1820. else
  1821. hp2:=nil;
  1822. end;
  1823. if assigned(hp2) then
  1824. Message2(unit_f_circular_unit_reference,callermodule.realmodulename^,hp.realmodulename^);
  1825. end;
  1826. break;
  1827. end;
  1828. end;
  1829. { the next unit }
  1830. hp:=tppumodule(hp.next);
  1831. end;
  1832. { the unit is not in the loaded units,
  1833. we create an entry and register the unit }
  1834. if not assigned(hp) then
  1835. begin
  1836. Message1(unit_u_registering_new_unit,Upper(s));
  1837. hp:=tppumodule.create(callermodule,s,fn,true);
  1838. hp.loaded_from:=callermodule;
  1839. addloadedunit(hp);
  1840. end;
  1841. { return }
  1842. registerunit:=hp;
  1843. end;
  1844. end.