fppu.pas 86 KB

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