fppu.pas 70 KB

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