fppu.pas 75 KB

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