fppu.pas 79 KB

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