fppu.pas 70 KB

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