fppu.pas 68 KB

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