2
0

fppu.pas 75 KB

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