fppu.pas 67 KB

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