fppu.pas 69 KB

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