fppu.pas 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342
  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 sourcefn
  1039. 2 path of ppu
  1040. 3 path of main source
  1041. 4 current dir
  1042. 5 include/unit path }
  1043. found:=false;
  1044. if sourcefn<>'' then
  1045. begin
  1046. temp_dir:=ExtractFilePath(SetDirSeparators(sourcefn));
  1047. Source_Time:=GetNamedFileTime(temp_dir+hs);
  1048. if Source_Time<>-1 then
  1049. hs:=temp_dir+hs;
  1050. end else
  1051. Source_Time:=-1;
  1052. if Source_Time=-1 then
  1053. begin
  1054. Source_Time:=GetNamedFileTime(path+hs);
  1055. if Source_Time<>-1 then
  1056. hs:=path+hs
  1057. else
  1058. if not(is_main) then
  1059. begin
  1060. Source_Time:=GetNamedFileTime(main_dir+hs);
  1061. if Source_Time<>-1 then
  1062. hs:=main_dir+hs;
  1063. end;
  1064. end;
  1065. if Source_Time=-1 then
  1066. Source_Time:=GetNamedFileTime(hs);
  1067. if (Source_Time=-1) then
  1068. begin
  1069. if is_main then
  1070. found:=unitsearchpath.FindFile(hs,true,temp_dir)
  1071. else
  1072. found:=includesearchpath.FindFile(hs,true,temp_dir);
  1073. if found then
  1074. begin
  1075. Source_Time:=GetNamedFileTime(temp_dir);
  1076. if Source_Time<>-1 then
  1077. hs:=temp_dir;
  1078. end;
  1079. end;
  1080. if Source_Time<>-1 then
  1081. begin
  1082. if is_main then
  1083. main_dir:=ExtractFilePath(hs);
  1084. temp:=' time '+filetimestring(source_time);
  1085. if (orgfiletime<>-1) and
  1086. (source_time<>orgfiletime) then
  1087. begin
  1088. do_compile:=true;
  1089. recompile_reason:=rr_sourcenewer;
  1090. Message2(unit_u_source_modified,hs,ppufilename,@queuecomment);
  1091. temp:=temp+' *';
  1092. end;
  1093. end
  1094. else
  1095. begin
  1096. sources_avail:=false;
  1097. temp:=' not found';
  1098. end;
  1099. hp:=tdosinputfile.create(hs);
  1100. hp.inc_path:=inc_path;
  1101. { the indexing is wrong here PM }
  1102. sourcefiles.register_file(hp);
  1103. end;
  1104. end
  1105. else
  1106. begin
  1107. { still register the source module for proper error messages
  1108. since source_avail for the module is still false, this should not hurt }
  1109. sourcefiles.register_file(tdosinputfile.create(hs));
  1110. temp:=' not available';
  1111. end;
  1112. if is_main then
  1113. begin
  1114. mainsource:=hs;
  1115. end;
  1116. Message1(unit_u_ppu_source,hs+temp,@queuecomment);
  1117. is_main:=false;
  1118. end;
  1119. { check if we want to rebuild every unit, only if the sources are
  1120. available }
  1121. if do_build and sources_avail then
  1122. begin
  1123. do_compile:=true;
  1124. recompile_reason:=rr_build;
  1125. end;
  1126. end;
  1127. procedure tppumodule.readloadunit;
  1128. var
  1129. hs : string;
  1130. pu : tused_unit;
  1131. hp : tppumodule;
  1132. indchecksum,
  1133. intfchecksum,
  1134. checksum : cardinal;
  1135. begin
  1136. while not ppufile.endofentry do
  1137. begin
  1138. hs:=ppufile.getstring;
  1139. checksum:=cardinal(ppufile.getlongint);
  1140. intfchecksum:=cardinal(ppufile.getlongint);
  1141. indchecksum:=cardinal(ppufile.getlongint);
  1142. { set the state of this unit before registering, this is
  1143. needed for a correct circular dependency check }
  1144. hp:=registerunit(self,hs,'');
  1145. pu:=addusedunit(hp,false,nil);
  1146. pu.checksum:=checksum;
  1147. pu.interface_checksum:=intfchecksum;
  1148. pu.indirect_checksum:=indchecksum;
  1149. end;
  1150. in_interface:=false;
  1151. end;
  1152. procedure tppumodule.readlinkcontainer(var p:tlinkcontainer);
  1153. var
  1154. s : string;
  1155. m : longint;
  1156. begin
  1157. while not ppufile.endofentry do
  1158. begin
  1159. s:=ppufile.getstring;
  1160. m:=ppufile.getlongint;
  1161. p.add(s,m);
  1162. end;
  1163. end;
  1164. procedure tppumodule.readderefmap;
  1165. var
  1166. i : longint;
  1167. begin
  1168. { Load unit map used for resolving }
  1169. derefmapsize:=ppufile.getlongint;
  1170. derefmapcnt:=derefmapsize;
  1171. getmem(derefmap,derefmapsize*sizeof(tderefmaprec));
  1172. fillchar(derefmap^,derefmapsize*sizeof(tderefmaprec),0);
  1173. for i:=0 to derefmapsize-1 do
  1174. derefmap[i].modulename:=ppufile.getpshortstring;
  1175. end;
  1176. procedure tppumodule.readderefdata;
  1177. var
  1178. len,hlen : longint;
  1179. buf : array[0..1023] of byte;
  1180. begin
  1181. len:=ppufile.entrysize;
  1182. while (len>0) do
  1183. begin
  1184. if len>1024 then
  1185. hlen:=1024
  1186. else
  1187. hlen:=len;
  1188. ppufile.getdata(buf,hlen);
  1189. derefdata.write(buf,hlen);
  1190. dec(len,hlen);
  1191. end;
  1192. end;
  1193. procedure tppumodule.readImportSymbols;
  1194. var
  1195. j,
  1196. extsymcnt : longint;
  1197. ImportLibrary : TImportLibrary;
  1198. extsymname : string;
  1199. extsymmangledname : string;
  1200. extsymordnr : longint;
  1201. extsymisvar : boolean;
  1202. begin
  1203. while not ppufile.endofentry do
  1204. begin
  1205. ImportLibrary:=TImportLibrary.Create(ImportLibraryList,ppufile.getstring);
  1206. extsymcnt:=ppufile.getlongint;
  1207. for j:=0 to extsymcnt-1 do
  1208. begin
  1209. extsymname:=ppufile.getstring;
  1210. extsymmangledname:=ppufile.getstring;
  1211. extsymordnr:=ppufile.getlongint;
  1212. extsymisvar:=(ppufile.getbyte<>0);
  1213. TImportSymbol.Create(ImportLibrary.ImportSymbolList,extsymname,
  1214. extsymmangledname,extsymordnr,extsymisvar);
  1215. end;
  1216. end;
  1217. end;
  1218. procedure tppumodule.readResources;
  1219. begin
  1220. while not ppufile.endofentry do
  1221. resourcefiles.Insert(ppufile.getstring);
  1222. end;
  1223. procedure tppumodule.readOrderedSymbols;
  1224. begin
  1225. while not ppufile.endofentry do
  1226. linkorderedsymbols.Concat(ppufile.getstring);
  1227. end;
  1228. procedure tppumodule.readwpofile;
  1229. var
  1230. orgwpofilename: string;
  1231. orgwpofiletime: longint;
  1232. begin
  1233. { check whether we are using the same wpo feedback input file as when
  1234. this unit was compiled (same file name and file date)
  1235. }
  1236. orgwpofilename:=ppufile.getstring;
  1237. orgwpofiletime:=ppufile.getlongint;
  1238. if (extractfilename(orgwpofilename)<>extractfilename(wpofeedbackinput)) or
  1239. (orgwpofiletime<>GetNamedFileTime(orgwpofilename)) then
  1240. { make sure we don't throw away a precompiled unit if the user simply
  1241. forgot to specify the right wpo feedback file
  1242. }
  1243. message3(unit_e_different_wpo_file,ppufilename,orgwpofilename,filetimestring(orgwpofiletime));
  1244. end;
  1245. procedure tppumodule.readunitimportsyms;
  1246. var
  1247. c,i : longint;
  1248. deref : pderef;
  1249. begin
  1250. c:=ppufile.getlongint;
  1251. for i:=0 to c-1 do
  1252. begin
  1253. new(deref);
  1254. ppufile.getderef(deref^);
  1255. unitimportsymsderefs.add(deref);
  1256. end;
  1257. end;
  1258. procedure tppumodule.readasmsyms;
  1259. var
  1260. c,i : longint;
  1261. name : TSymStr;
  1262. bind : TAsmsymbind;
  1263. typ : TAsmsymtype;
  1264. list : tfphashobjectlist;
  1265. begin
  1266. case tunitasmlisttype(ppufile.getbyte) of
  1267. ualt_public:
  1268. list:=publicasmsyms;
  1269. ualt_extern:
  1270. list:=externasmsyms;
  1271. end;
  1272. c:=ppufile.getlongint;
  1273. for i:=0 to c-1 do
  1274. begin
  1275. name:=ppufile.getstring;
  1276. bind:=TAsmsymbind(ppufile.getbyte);
  1277. typ:=TAsmsymtype(ppufile.getbyte);
  1278. TAsmSymbol.Create(list,name,bind,typ);
  1279. end;
  1280. end;
  1281. procedure tppumodule.readextraheader;
  1282. begin
  1283. longversion:=cardinal(ppufile.getlongint);
  1284. ppufile.getset(tppuset4(moduleflags));
  1285. end;
  1286. procedure tppumodule.load_interface;
  1287. var
  1288. b : byte;
  1289. newmodulename : string;
  1290. begin
  1291. { read interface part }
  1292. repeat
  1293. b:=ppufile.readentry;
  1294. case b of
  1295. ibjvmnamespace :
  1296. begin
  1297. namespace:=ppufile.getpshortstring;
  1298. end;
  1299. ibmodulename :
  1300. begin
  1301. newmodulename:=ppufile.getstring;
  1302. if (cs_check_unit_name in current_settings.globalswitches) and
  1303. (upper(newmodulename)<>modulename^) then
  1304. Message2(unit_f_unit_name_error,realmodulename^,newmodulename);
  1305. stringdispose(modulename);
  1306. stringdispose(realmodulename);
  1307. modulename:=stringdup(upper(newmodulename));
  1308. realmodulename:=stringdup(newmodulename);
  1309. end;
  1310. ibextraheader:
  1311. begin
  1312. readextraheader;
  1313. end;
  1314. ibfeatures :
  1315. begin
  1316. ppufile.getset(tppuset4(features));
  1317. end;
  1318. ibmoduleoptions:
  1319. begin
  1320. ppufile.getset(tppuset1(moduleoptions));
  1321. if mo_has_deprecated_msg in moduleoptions then
  1322. begin
  1323. stringdispose(deprecatedmsg);
  1324. deprecatedmsg:=ppufile.getpshortstring;
  1325. end;
  1326. end;
  1327. ibsourcefiles :
  1328. readsourcefiles;
  1329. {$IFDEF MACRO_DIFF_HINT}
  1330. ibusedmacros :
  1331. readusedmacros;
  1332. {$ENDIF}
  1333. ibloadunit :
  1334. readloadunit;
  1335. iblinkunitofiles :
  1336. readlinkcontainer(LinkUnitOFiles);
  1337. iblinkunitstaticlibs :
  1338. readlinkcontainer(LinkUnitStaticLibs);
  1339. iblinkunitsharedlibs :
  1340. readlinkcontainer(LinkUnitSharedLibs);
  1341. iblinkotherofiles :
  1342. readlinkcontainer(LinkotherOFiles);
  1343. iblinkotherstaticlibs :
  1344. readlinkcontainer(LinkotherStaticLibs);
  1345. iblinkothersharedlibs :
  1346. readlinkcontainer(LinkotherSharedLibs);
  1347. iblinkotherframeworks :
  1348. readlinkcontainer(LinkOtherFrameworks);
  1349. ibmainname:
  1350. begin
  1351. mainname:=ppufile.getpshortstring;
  1352. if (mainaliasname<>defaultmainaliasname) then
  1353. Message1(scan_w_multiple_main_name_overrides,mainaliasname);
  1354. mainaliasname:=mainname^;
  1355. end;
  1356. ibImportSymbols :
  1357. readImportSymbols;
  1358. ibderefmap :
  1359. readderefmap;
  1360. ibderefdata :
  1361. readderefdata;
  1362. ibresources:
  1363. readResources;
  1364. iborderedsymbols:
  1365. readOrderedSymbols;
  1366. ibwpofile:
  1367. readwpofile;
  1368. ibendinterface :
  1369. break;
  1370. else
  1371. Message1(unit_f_ppu_invalid_entry,tostr(b));
  1372. end;
  1373. { we can already stop when we know that we must recompile }
  1374. if do_compile then
  1375. exit;
  1376. until false;
  1377. end;
  1378. procedure tppumodule.load_implementation;
  1379. var
  1380. b : byte;
  1381. begin
  1382. { read implementation part }
  1383. repeat
  1384. b:=ppufile.readentry;
  1385. case b of
  1386. ibloadunit :
  1387. readloadunit;
  1388. ibasmsymbols :
  1389. readasmsyms;
  1390. ibunitimportsyms:
  1391. readunitimportsyms;
  1392. ibendimplementation :
  1393. break;
  1394. else
  1395. Message1(unit_f_ppu_invalid_entry,tostr(b));
  1396. end;
  1397. until false;
  1398. end;
  1399. procedure tppumodule.writeppu;
  1400. begin
  1401. Message1(unit_u_ppu_write,realmodulename^);
  1402. { create unit flags }
  1403. {$ifdef cpufpemu}
  1404. if (cs_fp_emulation in current_settings.moduleswitches) then
  1405. headerflags:=headerflags or uf_fpu_emulation;
  1406. {$endif cpufpemu}
  1407. { create new ppufile }
  1408. ppufile:=tcompilerppufile.create(ppufilename);
  1409. if not ppufile.createfile then
  1410. Message(unit_f_ppu_cannot_write);
  1411. {$ifdef Test_Double_checksum_write}
  1412. { Re-use the values collected in .INT part }
  1413. if assigned(interface_crc_array) then
  1414. begin
  1415. ppufile.implementation_write_crc_index:=implementation_write_crc_index;
  1416. ppufile.interface_write_crc_index:=interface_write_crc_index;
  1417. ppufile.indirect_write_crc_index:=indirect_write_crc_index;
  1418. if assigned(ppufile.interface_crc_array) then
  1419. begin
  1420. dispose(ppufile.interface_crc_array);
  1421. ppufile.interface_crc_array:=interface_crc_array;
  1422. end;
  1423. if assigned(ppufile.implementation_crc_array) then
  1424. begin
  1425. dispose(ppufile.implementation_crc_array);
  1426. ppufile.implementation_crc_array:=implementation_crc_array;
  1427. end;
  1428. if assigned(ppufile.indirect_crc_array) then
  1429. begin
  1430. dispose(ppufile.indirect_crc_array);
  1431. ppufile.indirect_crc_array:=indirect_crc_array;
  1432. end;
  1433. end;
  1434. if FileExists(ppufilename+'.IMP',false) then
  1435. RenameFile(ppufilename+'.IMP',ppufilename+'.IMP-old');
  1436. Assign(ppufile.CRCFile,ppufilename+'.IMP');
  1437. Rewrite(ppufile.CRCFile);
  1438. Writeln(ppufile.CRCFile,'CRC in writeppu method of implementation of ',ppufilename,' defsgeneration=',defsgeneration);
  1439. {$endif def Test_Double_checksum_write}
  1440. { extra header (sub version, module flags) }
  1441. writeextraheader;
  1442. { first the (JVM) namespace }
  1443. if assigned(namespace) then
  1444. begin
  1445. ppufile.putstring(namespace^);
  1446. ppufile.writeentry(ibjvmnamespace);
  1447. end;
  1448. { the unitname }
  1449. ppufile.putstring(realmodulename^);
  1450. ppufile.writeentry(ibmodulename);
  1451. ppufile.putset(tppuset1(moduleoptions));
  1452. if mo_has_deprecated_msg in moduleoptions then
  1453. ppufile.putstring(deprecatedmsg^);
  1454. ppufile.writeentry(ibmoduleoptions);
  1455. { write the alternate main procedure name if any }
  1456. if assigned(mainname) then
  1457. begin
  1458. ppufile.putstring(mainname^);
  1459. ppufile.writeentry(ibmainname);
  1460. end;
  1461. if cs_compilesystem in current_settings.moduleswitches then
  1462. begin
  1463. ppufile.putset(tppuset4(features));
  1464. ppufile.writeentry(ibfeatures);
  1465. end;
  1466. writesourcefiles;
  1467. {$IFDEF MACRO_DIFF_HINT}
  1468. writeusedmacros;
  1469. {$ENDIF}
  1470. { write interface uses }
  1471. writeusedunit(true);
  1472. { write the objectfiles and libraries that come for this unit,
  1473. preserve the containers because they are still needed to load
  1474. the link.res.
  1475. All doesn't depend on the crc! It doesn't matter
  1476. if a unit is in a .o or .a file }
  1477. ppufile.do_crc:=false;
  1478. { write after source files, so that we know whether or not the compiler
  1479. will recompile the unit when checking whether the correct wpo file is
  1480. used (if it will recompile the unit anyway, it doesn't matter)
  1481. }
  1482. if (wpofeedbackinput<>'') then
  1483. begin
  1484. ppufile.putstring(wpofeedbackinput);
  1485. ppufile.putlongint(getnamedfiletime(wpofeedbackinput));
  1486. ppufile.writeentry(ibwpofile);
  1487. end;
  1488. writelinkcontainer(linkunitofiles,iblinkunitofiles,true);
  1489. writelinkcontainer(linkunitstaticlibs,iblinkunitstaticlibs,true);
  1490. writelinkcontainer(linkunitsharedlibs,iblinkunitsharedlibs,true);
  1491. writelinkcontainer(linkotherofiles,iblinkotherofiles,false);
  1492. writelinkcontainer(linkotherstaticlibs,iblinkotherstaticlibs,true);
  1493. writelinkcontainer(linkothersharedlibs,iblinkothersharedlibs,true);
  1494. writelinkcontainer(linkotherframeworks,iblinkotherframeworks,true);
  1495. writeImportSymbols;
  1496. writeResources;
  1497. writeOrderedSymbols;
  1498. ppufile.do_crc:=true;
  1499. { generate implementation deref data, the interface deref data is
  1500. already generated when calculating the interface crc }
  1501. if (cs_compilesystem in current_settings.moduleswitches) then
  1502. begin
  1503. tstoredsymtable(globalsymtable).buildderef;
  1504. derefdataintflen:=derefdata.size;
  1505. end
  1506. else
  1507. { the unit may have been re-resolved, in which case the current
  1508. position in derefdata is not necessarily at the end }
  1509. derefdata.seek(derefdata.size);
  1510. tstoredsymtable(globalsymtable).buildderefimpl;
  1511. tunitwpoinfo(wpoinfo).buildderef;
  1512. tunitwpoinfo(wpoinfo).buildderefimpl;
  1513. if assigned(globalmacrosymtable) and (globalmacrosymtable.SymList.count > 0) then
  1514. begin
  1515. tstoredsymtable(globalmacrosymtable).buildderef;
  1516. tstoredsymtable(globalmacrosymtable).buildderefimpl;
  1517. end;
  1518. if mf_local_symtable in moduleflags then
  1519. tstoredsymtable(localsymtable).buildderef_registered;
  1520. buildderefunitimportsyms;
  1521. writederefmap;
  1522. writederefdata;
  1523. ppufile.writeentry(ibendinterface);
  1524. { write the symtable entries }
  1525. tstoredsymtable(globalsymtable).ppuwrite(ppufile);
  1526. if assigned(globalmacrosymtable) and (globalmacrosymtable.SymList.count > 0) then
  1527. begin
  1528. ppufile.putbyte(byte(true));
  1529. ppufile.writeentry(ibexportedmacros);
  1530. tstoredsymtable(globalmacrosymtable).ppuwrite(ppufile);
  1531. end
  1532. else
  1533. begin
  1534. ppufile.putbyte(byte(false));
  1535. ppufile.writeentry(ibexportedmacros);
  1536. end;
  1537. { everything after this doesn't affect the crc }
  1538. ppufile.do_crc:=false;
  1539. { write implementation uses }
  1540. writeusedunit(false);
  1541. { write all public assembler symbols }
  1542. writeasmsyms(ualt_public,publicasmsyms);
  1543. { write all external assembler symbols }
  1544. writeasmsyms(ualt_extern,externasmsyms);
  1545. { write all symbols imported from another unit }
  1546. writeunitimportsyms;
  1547. { end of implementation }
  1548. ppufile.writeentry(ibendimplementation);
  1549. { write static symtable
  1550. needed for local debugging of unit functions }
  1551. if mf_local_symtable in moduleflags then
  1552. tstoredsymtable(localsymtable).ppuwrite(ppufile);
  1553. { write whole program optimisation-related information }
  1554. tunitwpoinfo(wpoinfo).ppuwrite(ppufile);
  1555. { the last entry ibend is written automatically }
  1556. { flush to be sure }
  1557. ppufile.flush;
  1558. { create and write header }
  1559. ppufile.header.common.size:=ppufile.size;
  1560. ppufile.header.checksum:=ppufile.crc;
  1561. ppufile.header.interface_checksum:=ppufile.interface_crc;
  1562. ppufile.header.indirect_checksum:=ppufile.indirect_crc;
  1563. ppufile.header.common.compiler:=wordversion;
  1564. ppufile.header.common.cpu:=word(target_cpu);
  1565. ppufile.header.common.target:=word(target_info.system);
  1566. ppufile.header.common.flags:=headerflags;
  1567. ppufile.header.deflistsize:=current_module.deflist.count;
  1568. ppufile.header.symlistsize:=current_module.symlist.count;
  1569. ppufile.writeheader;
  1570. { save crc in current module also }
  1571. crc:=ppufile.crc;
  1572. interface_crc:=ppufile.interface_crc;
  1573. indirect_crc:=ppufile.indirect_crc;
  1574. {$ifdef Test_Double_checksum_write}
  1575. Writeln(ppufile.CRCFile,'End of implementation CRC in writeppu method of ',ppufilename,
  1576. ' implementation_crc=$',hexstr(ppufile.crc,8),
  1577. ' interface_crc=$',hexstr(ppufile.interface_crc,8),
  1578. ' indirect_crc=$',hexstr(ppufile.indirect_crc,8),
  1579. ' implementation_crc_size=',ppufile.implementation_read_crc_index,
  1580. ' interface_crc_size=',ppufile.interface_read_crc_index,
  1581. ' indirect_crc_size=',ppufile.indirect_read_crc_index,
  1582. ' defsgeneration=',defsgeneration);
  1583. close(ppufile.CRCFile);
  1584. {$endif Test_Double_checksum_write}
  1585. ppufile.closefile;
  1586. ppufile.free;
  1587. ppufile:=nil;
  1588. end;
  1589. procedure tppumodule.getppucrc;
  1590. begin
  1591. { create new ppufile }
  1592. ppufile:=tcompilerppufile.create(ppufilename);
  1593. ppufile.crc_only:=true;
  1594. if not ppufile.createfile then
  1595. Message(unit_f_ppu_cannot_write);
  1596. {$ifdef Test_Double_checksum_write}
  1597. if FileExists(ppufilename+'.INT',false) then
  1598. RenameFile(ppufilename+'.INT',ppufilename+'.INT-old');
  1599. Assign(ppufile.CRCFile,ppufilename+'.INT');
  1600. Rewrite(ppufile.CRCFile);
  1601. Writeln(ppufile.CRCFile,'CRC of getppucrc of ',ppufilename,
  1602. ' defsgeneration=',defsgeneration);
  1603. {$endif def Test_Double_checksum_write}
  1604. { first the (JVM) namespace }
  1605. if assigned(namespace) then
  1606. begin
  1607. ppufile.putstring(namespace^);
  1608. ppufile.writeentry(ibjvmnamespace);
  1609. end;
  1610. { the unitname }
  1611. ppufile.putstring(realmodulename^);
  1612. ppufile.writeentry(ibmodulename);
  1613. { extra header (sub version, module flags) }
  1614. writeextraheader;
  1615. ppufile.putset(tppuset1(moduleoptions));
  1616. if mo_has_deprecated_msg in moduleoptions then
  1617. ppufile.putstring(deprecatedmsg^);
  1618. ppufile.writeentry(ibmoduleoptions);
  1619. { the interface units affect the crc }
  1620. writeusedunit(true);
  1621. { deref data of interface that affect the crc }
  1622. derefdata.reset;
  1623. tstoredsymtable(globalsymtable).buildderef;
  1624. derefdataintflen:=derefdata.size;
  1625. writederefmap;
  1626. writederefdata;
  1627. ppufile.writeentry(ibendinterface);
  1628. { write the symtable entries }
  1629. tstoredsymtable(globalsymtable).ppuwrite(ppufile);
  1630. if assigned(globalmacrosymtable) and (globalmacrosymtable.SymList.count > 0) then
  1631. begin
  1632. ppufile.putbyte(byte(true));
  1633. ppufile.writeentry(ibexportedmacros);
  1634. tstoredsymtable(globalmacrosymtable).ppuwrite(ppufile);
  1635. end
  1636. else
  1637. begin
  1638. ppufile.putbyte(byte(false));
  1639. ppufile.writeentry(ibexportedmacros);
  1640. end;
  1641. { save crc }
  1642. crc:=ppufile.crc;
  1643. interface_crc:=ppufile.interface_crc;
  1644. indirect_crc:=ppufile.indirect_crc;
  1645. { end of implementation, to generate a correct ppufile
  1646. for ppudump when using INTFPPU define }
  1647. ppufile.writeentry(ibendimplementation);
  1648. {$ifdef Test_Double_checksum_write}
  1649. Writeln(ppufile.CRCFile,'End of CRC of getppucrc of ',ppufilename,
  1650. ' implementation_crc=$',hexstr(ppufile.crc,8),
  1651. ' interface_crc=$',hexstr(ppufile.interface_crc,8),
  1652. ' indirect_crc=$',hexstr(ppufile.indirect_crc,8),
  1653. ' implementation_crc_size=',ppufile.implementation_write_crc_index,
  1654. ' interface_crc_size=',ppufile.interface_write_crc_index,
  1655. ' indirect_crc_size=',ppufile.indirect_write_crc_index,
  1656. ' defsgeneration=',defsgeneration);
  1657. close(ppufile.CRCFile);
  1658. { Remember the values generated in .INT part }
  1659. implementation_write_crc_index:=ppufile.implementation_write_crc_index;
  1660. interface_write_crc_index:=ppufile.interface_write_crc_index;
  1661. indirect_write_crc_index:=ppufile.indirect_write_crc_index;
  1662. interface_crc_array:=ppufile.interface_crc_array;
  1663. ppufile.interface_crc_array:=nil;
  1664. implementation_crc_array:=ppufile.implementation_crc_array;
  1665. ppufile.implementation_crc_array:=nil;
  1666. indirect_crc_array:=ppufile.indirect_crc_array;
  1667. ppufile.indirect_crc_array:=nil;
  1668. {$endif Test_Double_checksum_write}
  1669. { create and write header, this will only be used
  1670. for debugging purposes }
  1671. ppufile.header.common.size:=ppufile.size;
  1672. ppufile.header.checksum:=ppufile.crc;
  1673. ppufile.header.interface_checksum:=ppufile.interface_crc;
  1674. ppufile.header.indirect_checksum:=ppufile.indirect_crc;
  1675. ppufile.header.common.compiler:=wordversion;
  1676. ppufile.header.common.cpu:=word(target_cpu);
  1677. ppufile.header.common.target:=word(target_info.system);
  1678. ppufile.header.common.flags:=headerflags;
  1679. ppufile.writeheader;
  1680. ppufile.closefile;
  1681. ppufile.free;
  1682. ppufile:=nil;
  1683. end;
  1684. procedure tppumodule.load_usedunits;
  1685. var
  1686. pu : tused_unit;
  1687. begin
  1688. if current_module<>self then
  1689. internalerror(200212284);
  1690. { load the used units from interface }
  1691. in_interface:=true;
  1692. pu:=tused_unit(used_units.first);
  1693. while assigned(pu) do
  1694. begin
  1695. if pu.in_interface then
  1696. begin
  1697. tppumodule(pu.u).loadppu;
  1698. { if this unit is compiled we can stop }
  1699. if state=ms_compiled then
  1700. exit;
  1701. { add this unit to the dependencies }
  1702. pu.u.adddependency(self);
  1703. { need to recompile the current unit, check the interface
  1704. crc. And when not compiled with -Ur then check the complete
  1705. crc }
  1706. if (pu.u.interface_crc<>pu.interface_checksum) or
  1707. (pu.u.indirect_crc<>pu.indirect_checksum) or
  1708. (
  1709. (not(mf_release in moduleflags)) and
  1710. (pu.u.crc<>pu.checksum)
  1711. ) then
  1712. begin
  1713. Message2(unit_u_recompile_crc_change,realmodulename^,pu.u.ppufilename,@queuecomment);
  1714. {$ifdef DEBUG_UNIT_CRC_CHANGES}
  1715. if (pu.u.interface_crc<>pu.interface_checksum) then
  1716. Comment(V_Normal,' intfcrc change: '+hexstr(pu.u.interface_crc,8)+' for '+pu.u.ppufilename+' <> '+hexstr(pu.interface_checksum,8)+' in unit '+realmodulename^)
  1717. else if (pu.u.indirect_crc<>pu.indirect_checksum) then
  1718. Comment(V_Normal,' indcrc change: '+hexstr(pu.u.indirect_crc,8)+' for '+pu.u.ppufilename+' <> '+hexstr(pu.indirect_checksum,8)+' in unit '+realmodulename^)
  1719. else
  1720. Comment(V_Normal,' implcrc change: '+hexstr(pu.u.crc,8)+' for '+pu.u.ppufilename+' <> '+hexstr(pu.checksum,8)+' in unit '+realmodulename^);
  1721. {$endif DEBUG_UNIT_CRC_CHANGES}
  1722. recompile_reason:=rr_crcchanged;
  1723. do_compile:=true;
  1724. exit;
  1725. end;
  1726. end;
  1727. pu:=tused_unit(pu.next);
  1728. end;
  1729. { ok, now load the interface of this unit }
  1730. if current_module<>self then
  1731. internalerror(200208187);
  1732. deflist.count:=ppufile.header.deflistsize;
  1733. symlist.count:=ppufile.header.symlistsize;
  1734. globalsymtable:=tglobalsymtable.create(modulename^,moduleid);
  1735. tstoredsymtable(globalsymtable).ppuload(ppufile);
  1736. if ppufile.readentry<>ibexportedmacros then
  1737. Message(unit_f_ppu_read_error);
  1738. if boolean(ppufile.getbyte) then
  1739. begin
  1740. globalmacrosymtable:=tmacrosymtable.Create(true);
  1741. tstoredsymtable(globalmacrosymtable).ppuload(ppufile)
  1742. end;
  1743. interface_compiled:=true;
  1744. { read the implementation part, containing
  1745. the implementation uses and ObjData }
  1746. in_interface:=false;
  1747. load_implementation;
  1748. { now only read the implementation uses }
  1749. pu:=tused_unit(used_units.first);
  1750. while assigned(pu) do
  1751. begin
  1752. if (not pu.in_interface) then
  1753. begin
  1754. tppumodule(pu.u).loadppu;
  1755. { if this unit is compiled we can stop }
  1756. if state=ms_compiled then
  1757. exit;
  1758. { add this unit to the dependencies }
  1759. pu.u.adddependency(self);
  1760. { need to recompile the current unit ? }
  1761. if (pu.u.interface_crc<>pu.interface_checksum) or
  1762. (pu.u.indirect_crc<>pu.indirect_checksum) then
  1763. begin
  1764. Message2(unit_u_recompile_crc_change,realmodulename^,pu.u.ppufilename+' {impl}',@queuecomment);
  1765. {$ifdef DEBUG_UNIT_CRC_CHANGES}
  1766. if (pu.u.interface_crc<>pu.interface_checksum) then
  1767. Comment(V_Normal,' intfcrc change (2): '+hexstr(pu.u.interface_crc,8)+' for '+pu.u.ppufilename+' <> '+hexstr(pu.interface_checksum,8)+' in unit '+realmodulename^)
  1768. else if (pu.u.indirect_crc<>pu.indirect_checksum) then
  1769. Comment(V_Normal,' indcrc change (2): '+hexstr(pu.u.indirect_crc,8)+' for '+pu.u.ppufilename+' <> '+hexstr(pu.indirect_checksum,8)+' in unit '+realmodulename^);
  1770. {$endif DEBUG_UNIT_CRC_CHANGES}
  1771. recompile_reason:=rr_crcchanged;
  1772. do_compile:=true;
  1773. exit;
  1774. end;
  1775. end;
  1776. pu:=tused_unit(pu.next);
  1777. end;
  1778. { load implementation symtable }
  1779. if mf_local_symtable in moduleflags then
  1780. begin
  1781. localsymtable:=tstaticsymtable.create(modulename^,moduleid);
  1782. tstaticsymtable(localsymtable).ppuload(ppufile);
  1783. end;
  1784. { we can now derefence all pointers to the implementation parts }
  1785. tstoredsymtable(globalsymtable).derefimpl(false);
  1786. { we've just loaded the localsymtable from the ppu file, so everything
  1787. in it was registered by definition (otherwise it wouldn't have been in
  1788. there) }
  1789. if assigned(localsymtable) then
  1790. tstoredsymtable(localsymtable).derefimpl(false);
  1791. derefunitimportsyms;
  1792. { read whole program optimisation-related information }
  1793. wpoinfo:=tunitwpoinfo.ppuload(ppufile);
  1794. tunitwpoinfo(wpoinfo).deref;
  1795. tunitwpoinfo(wpoinfo).derefimpl;
  1796. end;
  1797. function tppumodule.needrecompile:boolean;
  1798. var
  1799. pu : tused_unit;
  1800. begin
  1801. result:=false;
  1802. pu:=tused_unit(used_units.first);
  1803. while assigned(pu) do
  1804. begin
  1805. { need to recompile the current unit, check the interface
  1806. crc. And when not compiled with -Ur then check the complete
  1807. crc }
  1808. if (pu.u.interface_crc<>pu.interface_checksum) or
  1809. (pu.u.indirect_crc<>pu.indirect_checksum) or
  1810. (
  1811. (pu.in_interface) and
  1812. (pu.u.crc<>pu.checksum)
  1813. ) then
  1814. begin
  1815. {$ifdef DEBUG_UNIT_CRC_CHANGES}
  1816. if (pu.u.interface_crc<>pu.interface_checksum) then
  1817. Comment(V_Normal,' intfcrc change (3): '+hexstr(pu.u.interface_crc,8)+' for '+pu.u.ppufilename+' <> '+hexstr(pu.interface_checksum,8)+' in unit '+realmodulename^)
  1818. else if (pu.u.indirect_crc<>pu.indirect_checksum) then
  1819. Comment(V_Normal,' indcrc change (3): '+hexstr(pu.u.indirect_crc,8)+' for '+pu.u.ppufilename+' <> '+hexstr(pu.indirect_checksum,8)+' in unit '+realmodulename^)
  1820. else
  1821. Comment(V_Normal,' implcrc change (3): '+hexstr(pu.u.crc,8)+' for '+pu.u.ppufilename+' <> '+hexstr(pu.checksum,8)+' in unit '+realmodulename^);
  1822. {$endif DEBUG_UNIT_CRC_CHANGES}
  1823. result:=true;
  1824. exit;
  1825. end;
  1826. pu:=tused_unit(pu.next);
  1827. end;
  1828. end;
  1829. procedure tppumodule.setdefgeneration;
  1830. begin
  1831. defsgeneration:=currentdefgeneration;
  1832. inc(currentdefgeneration);
  1833. end;
  1834. procedure tppumodule.reload_flagged_units;
  1835. var
  1836. hp : tppumodule;
  1837. begin
  1838. { now reload all dependent units with outdated defs }
  1839. hp:=tppumodule(loaded_units.first);
  1840. while assigned(hp) do
  1841. begin
  1842. if hp.do_reload and
  1843. (hp.defsgeneration<defsgeneration) then
  1844. begin
  1845. hp.defsgeneration:=defsgeneration;
  1846. hp.loadppu
  1847. end
  1848. else
  1849. hp.do_reload:=false;
  1850. hp:=tppumodule(hp.next);
  1851. end;
  1852. end;
  1853. procedure tppumodule.end_of_parsing;
  1854. begin
  1855. { module is now compiled }
  1856. state:=ms_compiled;
  1857. { free ppu }
  1858. if assigned(ppufile) then
  1859. begin
  1860. ppufile.free;
  1861. ppufile:=nil;
  1862. end;
  1863. inherited end_of_parsing;
  1864. end;
  1865. procedure tppumodule.loadppu;
  1866. const
  1867. ImplIntf : array[boolean] of string[15]=('implementation','interface');
  1868. var
  1869. do_load,
  1870. second_time : boolean;
  1871. old_current_module : tmodule;
  1872. pu : tused_unit;
  1873. begin
  1874. old_current_module:=current_module;
  1875. Message3(unit_u_load_unit,old_current_module.modulename^,
  1876. ImplIntf[old_current_module.in_interface],
  1877. modulename^);
  1878. { Update loaded_from to detect cycles }
  1879. loaded_from:=old_current_module;
  1880. { check if the globalsymtable is already available, but
  1881. we must reload when the do_reload flag is set }
  1882. if (not do_reload) and
  1883. assigned(globalsymtable) then
  1884. exit;
  1885. { reset }
  1886. do_load:=true;
  1887. second_time:=false;
  1888. set_current_module(self);
  1889. { try to load it as a package unit first }
  1890. if (packagelist.count>0) and loadfrompackage then
  1891. begin
  1892. do_load:=false;
  1893. do_reload:=false;
  1894. state:=ms_compiled;
  1895. { PPU is not needed anymore }
  1896. if assigned(ppufile) then
  1897. begin
  1898. ppufile.closefile;
  1899. ppufile.free;
  1900. ppufile:=nil;
  1901. end;
  1902. { add the unit to the used units list of the program }
  1903. usedunits.concat(tused_unit.create(self,true,false,nil));
  1904. end;
  1905. { A force reload }
  1906. if do_reload then
  1907. begin
  1908. Message(unit_u_forced_reload);
  1909. do_reload:=false;
  1910. { When the unit is already loaded or being loaded
  1911. we can maybe skip a complete reload/recompile }
  1912. if assigned(globalsymtable) and
  1913. (not needrecompile) then
  1914. begin
  1915. { When we don't have any data stored yet there
  1916. is nothing to resolve }
  1917. if interface_compiled and
  1918. { it makes no sense to re-resolve the unit if it is already finally compiled }
  1919. not(state=ms_compiled) then
  1920. begin
  1921. Message1(unit_u_reresolving_unit,modulename^);
  1922. tstoredsymtable(globalsymtable).deref(false);
  1923. tstoredsymtable(globalsymtable).derefimpl(false);
  1924. if assigned(localsymtable) then
  1925. begin
  1926. { we have only builderef(impl)'d the registered symbols of
  1927. the localsymtable -> also only deref those again }
  1928. tstoredsymtable(localsymtable).deref(true);
  1929. tstoredsymtable(localsymtable).derefimpl(true);
  1930. end;
  1931. if assigned(wpoinfo) then
  1932. begin
  1933. tunitwpoinfo(wpoinfo).deref;
  1934. tunitwpoinfo(wpoinfo).derefimpl;
  1935. end;
  1936. { We have to flag the units that depend on this unit even
  1937. though it didn't change, because they might also
  1938. indirectly depend on the unit that did change (e.g.,
  1939. in case rgobj, rgx86 and rgcpu have been compiled
  1940. already, and then rgobj is recompiled for some reason
  1941. -> rgx86 is re-reresolved, but the vmtentries of trgcpu
  1942. must also be re-resolved, because they will also contain
  1943. pointers to procdefs in the old trgobj (in case of a
  1944. recompile, all old defs are freed) }
  1945. flagdependent(old_current_module);
  1946. reload_flagged_units;
  1947. end
  1948. else
  1949. Message1(unit_u_skipping_reresolving_unit,modulename^);
  1950. do_load:=false;
  1951. end;
  1952. end;
  1953. if do_load then
  1954. begin
  1955. { loading the unit for a second time? }
  1956. if state=ms_registered then
  1957. state:=ms_load
  1958. else
  1959. begin
  1960. { try to load the unit a second time first }
  1961. Message1(unit_u_second_load_unit,modulename^);
  1962. Message2(unit_u_previous_state,modulename^,ModuleStateStr[state]);
  1963. { Flag modules to reload }
  1964. flagdependent(old_current_module);
  1965. { Reset the module }
  1966. reset;
  1967. if state in [ms_compile,ms_second_compile] then
  1968. begin
  1969. Message1(unit_u_second_compile_unit,modulename^);
  1970. state:=ms_second_compile;
  1971. do_compile:=true;
  1972. end
  1973. else
  1974. state:=ms_second_load;
  1975. second_time:=true;
  1976. end;
  1977. { close old_current_ppu on system that are
  1978. short on file handles like DOS PM }
  1979. {$ifdef SHORT_ON_FILE_HANDLES}
  1980. if old_current_module.is_unit and
  1981. assigned(tppumodule(old_current_module).ppufile) then
  1982. tppumodule(old_current_module).ppufile.tempclose;
  1983. {$endif SHORT_ON_FILE_HANDLES}
  1984. { try to opening ppu, skip this when we already
  1985. know that we need to compile the unit }
  1986. if not do_compile then
  1987. begin
  1988. Message1(unit_u_loading_unit,modulename^);
  1989. search_unit_files(false);
  1990. if not do_compile then
  1991. begin
  1992. load_interface;
  1993. setdefgeneration;
  1994. if not do_compile then
  1995. begin
  1996. load_usedunits;
  1997. if not do_compile then
  1998. Message1(unit_u_finished_loading_unit,modulename^);
  1999. end;
  2000. end;
  2001. { PPU is not needed anymore }
  2002. if assigned(ppufile) then
  2003. begin
  2004. ppufile.closefile;
  2005. ppufile.free;
  2006. ppufile:=nil;
  2007. end;
  2008. end;
  2009. { Do we need to recompile the unit }
  2010. if do_compile then
  2011. begin
  2012. { recompile the unit or give a fatal error if sources not available }
  2013. if not(sources_avail) then
  2014. begin
  2015. search_unit_files(true);
  2016. if not(sources_avail) then
  2017. begin
  2018. printcomments;
  2019. if recompile_reason=rr_noppu then
  2020. begin
  2021. pu:=tused_unit(loaded_from.used_units.first);
  2022. while assigned(pu) do
  2023. begin
  2024. if pu.u=self then
  2025. break;
  2026. pu:=tused_unit(pu.next);
  2027. end;
  2028. if assigned(pu) and assigned(pu.unitsym) then
  2029. MessagePos2(pu.unitsym.fileinfo,unit_f_cant_find_ppu,realmodulename^,loaded_from.realmodulename^)
  2030. else
  2031. Message2(unit_f_cant_find_ppu,realmodulename^,loaded_from.realmodulename^);
  2032. end
  2033. else
  2034. Message1(unit_f_cant_compile_unit,realmodulename^);
  2035. end;
  2036. end;
  2037. { we found the sources, we do not need the verbose messages anymore }
  2038. if comments <> nil then
  2039. begin
  2040. comments.free;
  2041. comments:=nil;
  2042. end;
  2043. { Flag modules to reload }
  2044. flagdependent(old_current_module);
  2045. { Reset the module }
  2046. reset;
  2047. { compile this module }
  2048. if not(state in [ms_compile,ms_second_compile]) then
  2049. state:=ms_compile;
  2050. compile(mainsource);
  2051. setdefgeneration;
  2052. end
  2053. else
  2054. state:=ms_compiled;
  2055. if current_module<>self then
  2056. internalerror(200212282);
  2057. if in_interface then
  2058. internalerror(200212283);
  2059. { for a second_time recompile reload all dependent units,
  2060. for a first time compile register the unit _once_ }
  2061. if second_time then
  2062. reload_flagged_units
  2063. else
  2064. usedunits.concat(tused_unit.create(self,true,false,nil));
  2065. { reopen the old module }
  2066. {$ifdef SHORT_ON_FILE_HANDLES}
  2067. if old_current_module.is_unit and
  2068. assigned(tppumodule(old_current_module).ppufile) then
  2069. tppumodule(old_current_module).ppufile.tempopen;
  2070. {$endif SHORT_ON_FILE_HANDLES}
  2071. end;
  2072. { we are back, restore current_module }
  2073. set_current_module(old_current_module);
  2074. end;
  2075. {*****************************************************************************
  2076. RegisterUnit
  2077. *****************************************************************************}
  2078. function registerunit(callermodule:tmodule;const s : TIDString;const fn:string) : tppumodule;
  2079. var
  2080. ups : TIDString;
  2081. hp : tppumodule;
  2082. hp2 : tmodule;
  2083. begin
  2084. { Info }
  2085. ups:=upper(s);
  2086. { search all loaded units }
  2087. hp:=tppumodule(loaded_units.first);
  2088. while assigned(hp) do
  2089. begin
  2090. if hp.modulename^=ups then
  2091. begin
  2092. { only check for units. The main program is also
  2093. as a unit in the loaded_units list. We simply need
  2094. to ignore this entry (PFV) }
  2095. if hp.is_unit then
  2096. begin
  2097. { both units in interface ? }
  2098. if callermodule.in_interface and
  2099. hp.in_interface then
  2100. begin
  2101. { check for a cycle }
  2102. hp2:=callermodule.loaded_from;
  2103. while assigned(hp2) and (hp2<>hp) do
  2104. begin
  2105. if hp2.in_interface then
  2106. hp2:=hp2.loaded_from
  2107. else
  2108. hp2:=nil;
  2109. end;
  2110. if assigned(hp2) then
  2111. Message2(unit_f_circular_unit_reference,callermodule.realmodulename^,hp.realmodulename^);
  2112. end;
  2113. break;
  2114. end;
  2115. end;
  2116. { the next unit }
  2117. hp:=tppumodule(hp.next);
  2118. end;
  2119. { the unit is not in the loaded units,
  2120. we create an entry and register the unit }
  2121. if not assigned(hp) then
  2122. begin
  2123. Message1(unit_u_registering_new_unit,Upper(s));
  2124. hp:=tppumodule.create(callermodule,s,fn,true);
  2125. hp.loaded_from:=callermodule;
  2126. addloadedunit(hp);
  2127. end;
  2128. { return }
  2129. registerunit:=hp;
  2130. end;
  2131. end.