fppu.pas 70 KB

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