fppu.pas 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525
  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(for_recompile: boolean);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(for_recompile : boolean);
  161. begin
  162. inc(currentdefgeneration);
  163. discardppu;
  164. freederefunitimportsyms;
  165. unitimportsymsderefs.free;
  166. unitimportsymsderefs:=tfplist.create;
  167. inherited reset(for_recompile);
  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. if LoadCount=1 then
  1230. pu:=addusedunit(hp,false,nil)
  1231. else
  1232. begin
  1233. pu:=findusedunit(hp);
  1234. { Safety, normally this should not happen:
  1235. The used units list cannot change between loads unless recompiled and then loadcount is 1... }
  1236. if pu=nil then
  1237. pu:=addusedunit(hp,false,nil);
  1238. end;
  1239. pu.checksum:=checksum;
  1240. pu.interface_checksum:=intfchecksum;
  1241. pu.indirect_checksum:=indchecksum;
  1242. end;
  1243. in_interface:=false;
  1244. end;
  1245. procedure tppumodule.readlinkcontainer(var p:tlinkcontainer);
  1246. var
  1247. s : string;
  1248. m : longint;
  1249. begin
  1250. while not ppufile.endofentry do
  1251. begin
  1252. s:=ppufile.getstring;
  1253. m:=ppufile.getlongint;
  1254. p.add(s,m);
  1255. end;
  1256. end;
  1257. procedure tppumodule.readderefmap;
  1258. var
  1259. i : longint;
  1260. begin
  1261. { Load unit map used for resolving }
  1262. derefmapsize:=ppufile.getlongint;
  1263. derefmapcnt:=derefmapsize;
  1264. getmem(derefmap,derefmapsize*sizeof(tderefmaprec));
  1265. fillchar(derefmap^,derefmapsize*sizeof(tderefmaprec),0);
  1266. for i:=0 to derefmapsize-1 do
  1267. derefmap[i].modulename:=ppufile.getpshortstring;
  1268. end;
  1269. procedure tppumodule.readderefdata;
  1270. var
  1271. len,hlen : longint;
  1272. buf : array[0..1023] of byte;
  1273. begin
  1274. len:=ppufile.entrysize;
  1275. while (len>0) do
  1276. begin
  1277. if len>1024 then
  1278. hlen:=1024
  1279. else
  1280. hlen:=len;
  1281. ppufile.getdata(buf,hlen);
  1282. derefdata.write(buf,hlen);
  1283. dec(len,hlen);
  1284. end;
  1285. end;
  1286. procedure tppumodule.readImportSymbols;
  1287. var
  1288. j,
  1289. extsymcnt : longint;
  1290. ImportLibrary : TImportLibrary;
  1291. extsymname : string;
  1292. extsymmangledname : string;
  1293. extsymordnr : longint;
  1294. extsymisvar : boolean;
  1295. begin
  1296. while not ppufile.endofentry do
  1297. begin
  1298. ImportLibrary:=TImportLibrary.Create(ImportLibraryList,ppufile.getstring);
  1299. extsymcnt:=ppufile.getlongint;
  1300. for j:=0 to extsymcnt-1 do
  1301. begin
  1302. extsymname:=ppufile.getstring;
  1303. extsymmangledname:=ppufile.getstring;
  1304. extsymordnr:=ppufile.getlongint;
  1305. extsymisvar:=(ppufile.getbyte<>0);
  1306. TImportSymbol.Create(ImportLibrary.ImportSymbolList,extsymname,
  1307. extsymmangledname,extsymordnr,extsymisvar);
  1308. end;
  1309. end;
  1310. end;
  1311. procedure tppumodule.readResources;
  1312. begin
  1313. while not ppufile.endofentry do
  1314. resourcefiles.Insert(ppufile.getstring);
  1315. end;
  1316. procedure tppumodule.readOrderedSymbols;
  1317. begin
  1318. while not ppufile.endofentry do
  1319. linkorderedsymbols.Concat(ppufile.getstring);
  1320. end;
  1321. procedure tppumodule.readwpofile;
  1322. var
  1323. orgwpofilename: string;
  1324. orgwpofiletime: longint;
  1325. begin
  1326. { check whether we are using the same wpo feedback input file as when
  1327. this unit was compiled (same file name and file date)
  1328. }
  1329. orgwpofilename:=ppufile.getstring;
  1330. orgwpofiletime:=ppufile.getlongint;
  1331. if (extractfilename(orgwpofilename)<>extractfilename(wpofeedbackinput)) or
  1332. (orgwpofiletime<>GetNamedFileTime(orgwpofilename)) then
  1333. { make sure we don't throw away a precompiled unit if the user simply
  1334. forgot to specify the right wpo feedback file
  1335. }
  1336. message3(unit_e_different_wpo_file,ppufilename,orgwpofilename,filetimestring(orgwpofiletime));
  1337. end;
  1338. procedure tppumodule.readunitimportsyms;
  1339. var
  1340. c,i : longint;
  1341. deref : pderef;
  1342. begin
  1343. c:=ppufile.getlongint;
  1344. for i:=0 to c-1 do
  1345. begin
  1346. new(deref);
  1347. ppufile.getderef(deref^);
  1348. unitimportsymsderefs.add(deref);
  1349. end;
  1350. end;
  1351. procedure tppumodule.readasmsyms;
  1352. var
  1353. c,i : longint;
  1354. name : TSymStr;
  1355. bind : TAsmsymbind;
  1356. typ : TAsmsymtype;
  1357. list : tfphashobjectlist;
  1358. begin
  1359. case tunitasmlisttype(ppufile.getbyte) of
  1360. ualt_public:
  1361. list:=publicasmsyms;
  1362. ualt_extern:
  1363. list:=externasmsyms;
  1364. end;
  1365. c:=ppufile.getlongint;
  1366. for i:=0 to c-1 do
  1367. begin
  1368. name:=ppufile.getstring;
  1369. bind:=TAsmsymbind(ppufile.getbyte);
  1370. typ:=TAsmsymtype(ppufile.getbyte);
  1371. TAsmSymbol.Create(list,name,bind,typ);
  1372. end;
  1373. end;
  1374. procedure tppumodule.readextraheader;
  1375. begin
  1376. longversion:=cardinal(ppufile.getlongint);
  1377. ppufile.getset(tppuset4(moduleflags));
  1378. end;
  1379. procedure tppumodule.load_interface;
  1380. var
  1381. b : byte;
  1382. newmodulename : string;
  1383. begin
  1384. { read interface part }
  1385. repeat
  1386. b:=ppufile.readentry;
  1387. case b of
  1388. ibjvmnamespace :
  1389. begin
  1390. namespace:=ppufile.getpshortstring;
  1391. end;
  1392. ibmodulename :
  1393. begin
  1394. newmodulename:=ppufile.getstring;
  1395. if (cs_check_unit_name in current_settings.globalswitches) and
  1396. (upper(newmodulename)<>modulename^) then
  1397. Message2(unit_f_unit_name_error,realmodulename^,newmodulename);
  1398. stringdispose(modulename);
  1399. stringdispose(realmodulename);
  1400. modulename:=stringdup(upper(newmodulename));
  1401. realmodulename:=stringdup(newmodulename);
  1402. end;
  1403. ibextraheader:
  1404. begin
  1405. readextraheader;
  1406. end;
  1407. ibfeatures :
  1408. begin
  1409. ppufile.getset(tppuset4(features));
  1410. end;
  1411. ibmoduleoptions:
  1412. begin
  1413. ppufile.getset(tppuset1(moduleoptions));
  1414. if mo_has_deprecated_msg in moduleoptions then
  1415. begin
  1416. stringdispose(deprecatedmsg);
  1417. deprecatedmsg:=ppufile.getpshortstring;
  1418. end;
  1419. end;
  1420. ibsourcefiles :
  1421. readsourcefiles;
  1422. {$IFDEF MACRO_DIFF_HINT}
  1423. ibusedmacros :
  1424. readusedmacros;
  1425. {$ENDIF}
  1426. ibloadunit :
  1427. readloadunit;
  1428. iblinkunitofiles :
  1429. readlinkcontainer(LinkUnitOFiles);
  1430. iblinkunitstaticlibs :
  1431. readlinkcontainer(LinkUnitStaticLibs);
  1432. iblinkunitsharedlibs :
  1433. readlinkcontainer(LinkUnitSharedLibs);
  1434. iblinkotherofiles :
  1435. readlinkcontainer(LinkotherOFiles);
  1436. iblinkotherstaticlibs :
  1437. readlinkcontainer(LinkotherStaticLibs);
  1438. iblinkothersharedlibs :
  1439. readlinkcontainer(LinkotherSharedLibs);
  1440. iblinkotherframeworks :
  1441. readlinkcontainer(LinkOtherFrameworks);
  1442. ibmainname:
  1443. begin
  1444. mainname:=ppufile.getpshortstring;
  1445. if (mainaliasname<>defaultmainaliasname) then
  1446. Message1(scan_w_multiple_main_name_overrides,mainaliasname);
  1447. mainaliasname:=mainname^;
  1448. end;
  1449. ibImportSymbols :
  1450. readImportSymbols;
  1451. ibderefmap :
  1452. readderefmap;
  1453. ibderefdata :
  1454. readderefdata;
  1455. ibresources:
  1456. readResources;
  1457. iborderedsymbols:
  1458. readOrderedSymbols;
  1459. ibwpofile:
  1460. readwpofile;
  1461. ibendinterface :
  1462. break;
  1463. else
  1464. Message1(unit_f_ppu_invalid_entry,tostr(b));
  1465. end;
  1466. { we can already stop when we know that we must recompile }
  1467. if state=ms_compile then
  1468. exit;
  1469. until false;
  1470. end;
  1471. procedure tppumodule.load_implementation;
  1472. var
  1473. b : byte;
  1474. begin
  1475. { read implementation part }
  1476. repeat
  1477. b:=ppufile.readentry;
  1478. case b of
  1479. ibloadunit :
  1480. readloadunit;
  1481. ibasmsymbols :
  1482. readasmsyms;
  1483. ibunitimportsyms:
  1484. readunitimportsyms;
  1485. ibendimplementation :
  1486. break;
  1487. else
  1488. Message1(unit_f_ppu_invalid_entry,tostr(b));
  1489. end;
  1490. until false;
  1491. end;
  1492. procedure tppumodule.writeppu;
  1493. begin
  1494. Message1(unit_u_ppu_write,realmodulename^);
  1495. { create unit flags }
  1496. {$ifdef cpufpemu}
  1497. if (cs_fp_emulation in current_settings.moduleswitches) then
  1498. headerflags:=headerflags or uf_fpu_emulation;
  1499. {$endif cpufpemu}
  1500. { create new ppufile }
  1501. ppufile:=tcompilerppufile.create(ppufilename);
  1502. if not ppufile.createfile then
  1503. Message(unit_f_ppu_cannot_write);
  1504. {$ifdef Test_Double_checksum_write}
  1505. { Re-use the values collected in .INT part }
  1506. if assigned(interface_crc_array) then
  1507. begin
  1508. ppufile.implementation_write_crc_index:=implementation_write_crc_index;
  1509. ppufile.interface_write_crc_index:=interface_write_crc_index;
  1510. ppufile.indirect_write_crc_index:=indirect_write_crc_index;
  1511. if assigned(ppufile.interface_crc_array) then
  1512. begin
  1513. dispose(ppufile.interface_crc_array);
  1514. ppufile.interface_crc_array:=interface_crc_array;
  1515. end;
  1516. if assigned(ppufile.implementation_crc_array) then
  1517. begin
  1518. dispose(ppufile.implementation_crc_array);
  1519. ppufile.implementation_crc_array:=implementation_crc_array;
  1520. end;
  1521. if assigned(ppufile.indirect_crc_array) then
  1522. begin
  1523. dispose(ppufile.indirect_crc_array);
  1524. ppufile.indirect_crc_array:=indirect_crc_array;
  1525. end;
  1526. end;
  1527. if FileExists(ppufilename+'.IMP',false) then
  1528. RenameFile(ppufilename+'.IMP',ppufilename+'.IMP-old');
  1529. Assign(ppufile.CRCFile,ppufilename+'.IMP');
  1530. Rewrite(ppufile.CRCFile);
  1531. Writeln(ppufile.CRCFile,'CRC in writeppu method of implementation of ',ppufilename,' defsgeneration=',defsgeneration);
  1532. {$endif def Test_Double_checksum_write}
  1533. { extra header (sub version, module flags) }
  1534. writeextraheader;
  1535. { first the (JVM) namespace }
  1536. if assigned(namespace) then
  1537. begin
  1538. ppufile.putstring(namespace^);
  1539. ppufile.writeentry(ibjvmnamespace);
  1540. end;
  1541. { the unitname }
  1542. ppufile.putstring(realmodulename^);
  1543. ppufile.writeentry(ibmodulename);
  1544. ppufile.putset(tppuset1(moduleoptions));
  1545. if mo_has_deprecated_msg in moduleoptions then
  1546. ppufile.putstring(deprecatedmsg^);
  1547. ppufile.writeentry(ibmoduleoptions);
  1548. { write the alternate main procedure name if any }
  1549. if assigned(mainname) then
  1550. begin
  1551. ppufile.putstring(mainname^);
  1552. ppufile.writeentry(ibmainname);
  1553. end;
  1554. if cs_compilesystem in current_settings.moduleswitches then
  1555. begin
  1556. ppufile.putset(tppuset4(features));
  1557. ppufile.writeentry(ibfeatures);
  1558. end;
  1559. writesourcefiles;
  1560. {$IFDEF MACRO_DIFF_HINT}
  1561. writeusedmacros;
  1562. {$ENDIF}
  1563. { write interface uses }
  1564. writeusedunit(true);
  1565. { write the objectfiles and libraries that come for this unit,
  1566. preserve the containers because they are still needed to load
  1567. the link.res.
  1568. All doesn't depend on the crc! It doesn't matter
  1569. if a unit is in a .o or .a file }
  1570. ppufile.do_crc:=false;
  1571. { write after source files, so that we know whether or not the compiler
  1572. will recompile the unit when checking whether the correct wpo file is
  1573. used (if it will recompile the unit anyway, it doesn't matter)
  1574. }
  1575. if (wpofeedbackinput<>'') then
  1576. begin
  1577. ppufile.putstring(wpofeedbackinput);
  1578. ppufile.putlongint(getnamedfiletime(wpofeedbackinput));
  1579. ppufile.writeentry(ibwpofile);
  1580. end;
  1581. writelinkcontainer(linkunitofiles,iblinkunitofiles,true);
  1582. writelinkcontainer(linkunitstaticlibs,iblinkunitstaticlibs,true);
  1583. writelinkcontainer(linkunitsharedlibs,iblinkunitsharedlibs,true);
  1584. writelinkcontainer(linkotherofiles,iblinkotherofiles,false);
  1585. writelinkcontainer(linkotherstaticlibs,iblinkotherstaticlibs,true);
  1586. writelinkcontainer(linkothersharedlibs,iblinkothersharedlibs,true);
  1587. writelinkcontainer(linkotherframeworks,iblinkotherframeworks,true);
  1588. writeImportSymbols;
  1589. writeResources;
  1590. writeOrderedSymbols;
  1591. ppufile.do_crc:=true;
  1592. { generate implementation deref data, the interface deref data is
  1593. already generated when calculating the interface crc }
  1594. if (cs_compilesystem in current_settings.moduleswitches) then
  1595. begin
  1596. tstoredsymtable(globalsymtable).buildderef;
  1597. derefdataintflen:=derefdata.size;
  1598. end
  1599. else
  1600. { the unit may have been re-resolved, in which case the current
  1601. position in derefdata is not necessarily at the end }
  1602. derefdata.seek(derefdata.size);
  1603. tstoredsymtable(globalsymtable).buildderefimpl;
  1604. tunitwpoinfo(wpoinfo).buildderef;
  1605. tunitwpoinfo(wpoinfo).buildderefimpl;
  1606. if assigned(globalmacrosymtable) and (globalmacrosymtable.SymList.count > 0) then
  1607. begin
  1608. tstoredsymtable(globalmacrosymtable).buildderef;
  1609. tstoredsymtable(globalmacrosymtable).buildderefimpl;
  1610. end;
  1611. if mf_local_symtable in moduleflags then
  1612. tstoredsymtable(localsymtable).buildderef_registered;
  1613. buildderefunitimportsyms;
  1614. writederefmap;
  1615. writederefdata;
  1616. ppufile.writeentry(ibendinterface);
  1617. { write the symtable entries }
  1618. tstoredsymtable(globalsymtable).ppuwrite(ppufile);
  1619. if assigned(globalmacrosymtable) and (globalmacrosymtable.SymList.count > 0) then
  1620. begin
  1621. ppufile.putbyte(byte(true));
  1622. ppufile.writeentry(ibexportedmacros);
  1623. tstoredsymtable(globalmacrosymtable).ppuwrite(ppufile);
  1624. end
  1625. else
  1626. begin
  1627. ppufile.putbyte(byte(false));
  1628. ppufile.writeentry(ibexportedmacros);
  1629. end;
  1630. { everything after this doesn't affect the crc }
  1631. ppufile.do_crc:=false;
  1632. { write implementation uses }
  1633. writeusedunit(false);
  1634. { write all public assembler symbols }
  1635. writeasmsyms(ualt_public,publicasmsyms);
  1636. { write all external assembler symbols }
  1637. writeasmsyms(ualt_extern,externasmsyms);
  1638. { write all symbols imported from another unit }
  1639. writeunitimportsyms;
  1640. { end of implementation }
  1641. ppufile.writeentry(ibendimplementation);
  1642. { write static symtable
  1643. needed for local debugging of unit functions }
  1644. if mf_local_symtable in moduleflags then
  1645. tstoredsymtable(localsymtable).ppuwrite(ppufile);
  1646. { write whole program optimisation-related information }
  1647. tunitwpoinfo(wpoinfo).ppuwrite(ppufile);
  1648. { the last entry ibend is written automatically }
  1649. { flush to be sure }
  1650. ppufile.flush;
  1651. { create and write header }
  1652. ppufile.header.common.size:=ppufile.size;
  1653. ppufile.header.checksum:=ppufile.crc;
  1654. ppufile.header.interface_checksum:=ppufile.interface_crc;
  1655. ppufile.header.indirect_checksum:=ppufile.indirect_crc;
  1656. ppufile.header.common.compiler:=wordversion;
  1657. ppufile.header.common.cpu:=word(target_cpu);
  1658. ppufile.header.common.target:=word(target_info.system);
  1659. ppufile.header.common.flags:=headerflags;
  1660. ppufile.header.deflistsize:=current_module.deflist.count;
  1661. ppufile.header.symlistsize:=current_module.symlist.count;
  1662. ppufile.writeheader;
  1663. { save crc in current module also }
  1664. crc:=ppufile.crc;
  1665. interface_crc:=ppufile.interface_crc;
  1666. indirect_crc:=ppufile.indirect_crc;
  1667. {$ifdef Test_Double_checksum_write}
  1668. Writeln(ppufile.CRCFile,'End of implementation CRC in writeppu method of ',ppufilename,
  1669. ' implementation_crc=$',hexstr(ppufile.crc,8),
  1670. ' interface_crc=$',hexstr(ppufile.interface_crc,8),
  1671. ' indirect_crc=$',hexstr(ppufile.indirect_crc,8),
  1672. ' implementation_crc_size=',ppufile.implementation_read_crc_index,
  1673. ' interface_crc_size=',ppufile.interface_read_crc_index,
  1674. ' indirect_crc_size=',ppufile.indirect_read_crc_index,
  1675. ' defsgeneration=',defsgeneration);
  1676. close(ppufile.CRCFile);
  1677. {$endif Test_Double_checksum_write}
  1678. discardppu;
  1679. end;
  1680. procedure tppumodule.getppucrc;
  1681. begin
  1682. { create new ppufile }
  1683. ppufile:=tcompilerppufile.create(ppufilename);
  1684. ppufile.crc_only:=true;
  1685. if not ppufile.createfile then
  1686. Message(unit_f_ppu_cannot_write);
  1687. {$ifdef Test_Double_checksum_write}
  1688. if FileExists(ppufilename+'.INT',false) then
  1689. RenameFile(ppufilename+'.INT',ppufilename+'.INT-old');
  1690. Assign(ppufile.CRCFile,ppufilename+'.INT');
  1691. Rewrite(ppufile.CRCFile);
  1692. Writeln(ppufile.CRCFile,'CRC of getppucrc of ',ppufilename,
  1693. ' defsgeneration=',defsgeneration);
  1694. {$endif def Test_Double_checksum_write}
  1695. { first the (JVM) namespace }
  1696. if assigned(namespace) then
  1697. begin
  1698. ppufile.putstring(namespace^);
  1699. ppufile.writeentry(ibjvmnamespace);
  1700. end;
  1701. { the unitname }
  1702. ppufile.putstring(realmodulename^);
  1703. ppufile.writeentry(ibmodulename);
  1704. { extra header (sub version, module flags) }
  1705. writeextraheader;
  1706. ppufile.putset(tppuset1(moduleoptions));
  1707. if mo_has_deprecated_msg in moduleoptions then
  1708. ppufile.putstring(deprecatedmsg^);
  1709. ppufile.writeentry(ibmoduleoptions);
  1710. { the interface units affect the crc }
  1711. writeusedunit(true);
  1712. { deref data of interface that affect the crc }
  1713. derefdata.reset;
  1714. tstoredsymtable(globalsymtable).buildderef;
  1715. derefdataintflen:=derefdata.size;
  1716. writederefmap;
  1717. writederefdata;
  1718. ppufile.writeentry(ibendinterface);
  1719. { write the symtable entries }
  1720. tstoredsymtable(globalsymtable).ppuwrite(ppufile);
  1721. if assigned(globalmacrosymtable) and (globalmacrosymtable.SymList.count > 0) then
  1722. begin
  1723. ppufile.putbyte(byte(true));
  1724. ppufile.writeentry(ibexportedmacros);
  1725. tstoredsymtable(globalmacrosymtable).ppuwrite(ppufile);
  1726. end
  1727. else
  1728. begin
  1729. ppufile.putbyte(byte(false));
  1730. ppufile.writeentry(ibexportedmacros);
  1731. end;
  1732. { save crc }
  1733. crc:=ppufile.crc;
  1734. interface_crc:=ppufile.interface_crc;
  1735. indirect_crc:=ppufile.indirect_crc;
  1736. { end of implementation, to generate a correct ppufile
  1737. for ppudump when using INTFPPU define }
  1738. ppufile.writeentry(ibendimplementation);
  1739. {$ifdef Test_Double_checksum_write}
  1740. Writeln(ppufile.CRCFile,'End of CRC of getppucrc of ',ppufilename,
  1741. ' implementation_crc=$',hexstr(ppufile.crc,8),
  1742. ' interface_crc=$',hexstr(ppufile.interface_crc,8),
  1743. ' indirect_crc=$',hexstr(ppufile.indirect_crc,8),
  1744. ' implementation_crc_size=',ppufile.implementation_write_crc_index,
  1745. ' interface_crc_size=',ppufile.interface_write_crc_index,
  1746. ' indirect_crc_size=',ppufile.indirect_write_crc_index,
  1747. ' defsgeneration=',defsgeneration);
  1748. close(ppufile.CRCFile);
  1749. { Remember the values generated in .INT part }
  1750. implementation_write_crc_index:=ppufile.implementation_write_crc_index;
  1751. interface_write_crc_index:=ppufile.interface_write_crc_index;
  1752. indirect_write_crc_index:=ppufile.indirect_write_crc_index;
  1753. interface_crc_array:=ppufile.interface_crc_array;
  1754. ppufile.interface_crc_array:=nil;
  1755. implementation_crc_array:=ppufile.implementation_crc_array;
  1756. ppufile.implementation_crc_array:=nil;
  1757. indirect_crc_array:=ppufile.indirect_crc_array;
  1758. ppufile.indirect_crc_array:=nil;
  1759. {$endif Test_Double_checksum_write}
  1760. { create and write header, this will only be used
  1761. for debugging purposes }
  1762. ppufile.header.common.size:=ppufile.size;
  1763. ppufile.header.checksum:=ppufile.crc;
  1764. ppufile.header.interface_checksum:=ppufile.interface_crc;
  1765. ppufile.header.indirect_checksum:=ppufile.indirect_crc;
  1766. ppufile.header.common.compiler:=wordversion;
  1767. ppufile.header.common.cpu:=word(target_cpu);
  1768. ppufile.header.common.target:=word(target_info.system);
  1769. ppufile.header.common.flags:=headerflags;
  1770. ppufile.writeheader;
  1771. discardppu;
  1772. end;
  1773. procedure tppumodule.load_usedunits;
  1774. var
  1775. pu : tused_unit;
  1776. begin
  1777. if current_module<>self then
  1778. internalerror(200212284);
  1779. { load the used units from interface }
  1780. in_interface:=true;
  1781. pu:=tused_unit(used_units.first);
  1782. while assigned(pu) do
  1783. begin
  1784. if pu.in_interface then
  1785. begin
  1786. tppumodule(pu.u).loadppu(self);
  1787. { if this unit is scheduled for compilation or compiled we can stop }
  1788. if state in [ms_compile,ms_compiled,ms_processed] then
  1789. exit;
  1790. { add this unit to the dependencies }
  1791. pu.u.adddependency(self,true);
  1792. { need to recompile the current unit, check the interface
  1793. crc. And when not compiled with -Ur then check the complete
  1794. crc }
  1795. if (pu.u.interface_crc<>pu.interface_checksum) or
  1796. (pu.u.indirect_crc<>pu.indirect_checksum) or
  1797. (
  1798. (not(mf_release in moduleflags)) and
  1799. (pu.u.crc<>pu.checksum)
  1800. ) then
  1801. begin
  1802. Message2(unit_u_recompile_crc_change,realmodulename^,pu.u.ppufilename,@queuecomment);
  1803. {$ifdef DEBUG_UNIT_CRC_CHANGES}
  1804. if (pu.u.interface_crc<>pu.interface_checksum) then
  1805. Comment(V_Normal,' intfcrc change: '+hexstr(pu.u.interface_crc,8)+' for '+pu.u.ppufilename+' <> '+hexstr(pu.interface_checksum,8)+' in unit '+realmodulename^)
  1806. else if (pu.u.indirect_crc<>pu.indirect_checksum) then
  1807. Comment(V_Normal,' indcrc change: '+hexstr(pu.u.indirect_crc,8)+' for '+pu.u.ppufilename+' <> '+hexstr(pu.indirect_checksum,8)+' in unit '+realmodulename^)
  1808. else
  1809. Comment(V_Normal,' implcrc change: '+hexstr(pu.u.crc,8)+' for '+pu.u.ppufilename+' <> '+hexstr(pu.checksum,8)+' in unit '+realmodulename^);
  1810. {$endif DEBUG_UNIT_CRC_CHANGES}
  1811. recompile_reason:=rr_crcchanged;
  1812. state:=ms_compile;
  1813. exit;
  1814. end;
  1815. end;
  1816. pu:=tused_unit(pu.next);
  1817. end;
  1818. { ok, now load the interface of this unit }
  1819. if current_module<>self then
  1820. internalerror(200208187);
  1821. deflist.count:=ppufile.header.deflistsize;
  1822. symlist.count:=ppufile.header.symlistsize;
  1823. globalsymtable:=tglobalsymtable.create(modulename^,moduleid);
  1824. tstoredsymtable(globalsymtable).ppuload(ppufile);
  1825. if ppufile.readentry<>ibexportedmacros then
  1826. Message(unit_f_ppu_read_error);
  1827. if boolean(ppufile.getbyte) then
  1828. begin
  1829. globalmacrosymtable:=tmacrosymtable.Create(true);
  1830. tstoredsymtable(globalmacrosymtable).ppuload(ppufile)
  1831. end;
  1832. interface_compiled:=true;
  1833. { read the implementation part, containing
  1834. the implementation uses and ObjData }
  1835. in_interface:=false;
  1836. load_implementation;
  1837. { now only read the implementation uses }
  1838. pu:=tused_unit(used_units.first);
  1839. while assigned(pu) do
  1840. begin
  1841. if (not pu.in_interface) then
  1842. begin
  1843. tppumodule(pu.u).loadppu(self);
  1844. { if this unit is compiled we can stop }
  1845. if state=ms_compiled then
  1846. exit;
  1847. { add this unit to the dependencies }
  1848. pu.u.adddependency(self,false);
  1849. { need to recompile the current unit ? }
  1850. if (pu.u.interface_crc<>pu.interface_checksum) or
  1851. (pu.u.indirect_crc<>pu.indirect_checksum) then
  1852. begin
  1853. Message2(unit_u_recompile_crc_change,realmodulename^,pu.u.ppufilename+' {impl}',@queuecomment);
  1854. {$ifdef DEBUG_UNIT_CRC_CHANGES}
  1855. if (pu.u.interface_crc<>pu.interface_checksum) then
  1856. Comment(V_Normal,' intfcrc change (2): '+hexstr(pu.u.interface_crc,8)+' for '+pu.u.ppufilename+' <> '+hexstr(pu.interface_checksum,8)+' in unit '+realmodulename^)
  1857. else if (pu.u.indirect_crc<>pu.indirect_checksum) then
  1858. Comment(V_Normal,' indcrc change (2): '+hexstr(pu.u.indirect_crc,8)+' for '+pu.u.ppufilename+' <> '+hexstr(pu.indirect_checksum,8)+' in unit '+realmodulename^);
  1859. {$endif DEBUG_UNIT_CRC_CHANGES}
  1860. recompile_reason:=rr_crcchanged;
  1861. state:=ms_compile;
  1862. exit;
  1863. end;
  1864. end;
  1865. pu:=tused_unit(pu.next);
  1866. end;
  1867. { load implementation symtable }
  1868. if mf_local_symtable in moduleflags then
  1869. begin
  1870. localsymtable:=tstaticsymtable.create(modulename^,moduleid);
  1871. tstaticsymtable(localsymtable).ppuload(ppufile);
  1872. end;
  1873. { we can now derefence all pointers to the implementation parts }
  1874. tstoredsymtable(globalsymtable).derefimpl(false);
  1875. { we've just loaded the localsymtable from the ppu file, so everything
  1876. in it was registered by definition (otherwise it wouldn't have been in
  1877. there) }
  1878. if assigned(localsymtable) then
  1879. tstoredsymtable(localsymtable).derefimpl(false);
  1880. derefunitimportsyms;
  1881. { read whole program optimisation-related information }
  1882. wpoinfo:=tunitwpoinfo.ppuload(ppufile);
  1883. tunitwpoinfo(wpoinfo).deref;
  1884. tunitwpoinfo(wpoinfo).derefimpl;
  1885. end;
  1886. function tppumodule.needrecompile:boolean;
  1887. var
  1888. pu : tused_unit;
  1889. begin
  1890. result:=false;
  1891. pu:=tused_unit(used_units.first);
  1892. while assigned(pu) do
  1893. begin
  1894. { need to recompile the current unit, check the interface
  1895. crc. And when not compiled with -Ur then check the complete
  1896. crc }
  1897. if (pu.u.interface_crc<>pu.interface_checksum) or
  1898. (pu.u.indirect_crc<>pu.indirect_checksum) or
  1899. (
  1900. (pu.in_interface) and
  1901. (pu.u.crc<>pu.checksum)
  1902. ) then
  1903. begin
  1904. {$ifdef DEBUG_UNIT_CRC_CHANGES}
  1905. if (pu.u.interface_crc<>pu.interface_checksum) then
  1906. Comment(V_Normal,' intfcrc change (3): '+hexstr(pu.u.interface_crc,8)+' for '+pu.u.ppufilename+' <> '+hexstr(pu.interface_checksum,8)+' in unit '+realmodulename^)
  1907. else if (pu.u.indirect_crc<>pu.indirect_checksum) then
  1908. Comment(V_Normal,' indcrc change (3): '+hexstr(pu.u.indirect_crc,8)+' for '+pu.u.ppufilename+' <> '+hexstr(pu.indirect_checksum,8)+' in unit '+realmodulename^)
  1909. else
  1910. Comment(V_Normal,' implcrc change (3): '+hexstr(pu.u.crc,8)+' for '+pu.u.ppufilename+' <> '+hexstr(pu.checksum,8)+' in unit '+realmodulename^);
  1911. {$endif DEBUG_UNIT_CRC_CHANGES}
  1912. result:=true;
  1913. exit;
  1914. end;
  1915. pu:=tused_unit(pu.next);
  1916. end;
  1917. end;
  1918. procedure tppumodule.setdefgeneration;
  1919. begin
  1920. defsgeneration:=currentdefgeneration;
  1921. inc(currentdefgeneration);
  1922. end;
  1923. procedure tppumodule.reload_flagged_units;
  1924. var
  1925. hp : tppumodule;
  1926. begin
  1927. { now reload all dependent units with outdated defs }
  1928. hp:=tppumodule(loaded_units.first);
  1929. while assigned(hp) do
  1930. begin
  1931. if hp.do_reload and
  1932. (hp.defsgeneration<defsgeneration) then
  1933. begin
  1934. hp.defsgeneration:=defsgeneration;
  1935. hp.loadppu(self)
  1936. end
  1937. else
  1938. hp.do_reload:=false;
  1939. hp:=tppumodule(hp.next);
  1940. end;
  1941. end;
  1942. procedure tppumodule.end_of_parsing;
  1943. begin
  1944. { module is now compiled }
  1945. state:=ms_compiled;
  1946. { free ppu }
  1947. discardppu;
  1948. inherited end_of_parsing;
  1949. end;
  1950. procedure tppumodule.check_reload(from_module : tmodule; var do_load : boolean);
  1951. begin
  1952. { A force reload }
  1953. if not do_reload then
  1954. exit;
  1955. Message(unit_u_forced_reload);
  1956. do_reload:=false;
  1957. { When the unit is already loaded or being loaded
  1958. we can maybe skip a complete reload/recompile }
  1959. if assigned(globalsymtable) and
  1960. (not needrecompile) then
  1961. begin
  1962. { When we don't have any data stored yet there
  1963. is nothing to resolve }
  1964. if interface_compiled and
  1965. { it makes no sense to re-resolve the unit if it is already finally compiled }
  1966. not(state=ms_compiled) then
  1967. begin
  1968. re_resolve(from_module);
  1969. end
  1970. else
  1971. Message1(unit_u_skipping_reresolving_unit,modulename^);
  1972. do_load:=false;
  1973. end;
  1974. end;
  1975. { Returns true if the module was loaded from package }
  1976. function tppumodule.check_loadfrompackage : boolean;
  1977. begin
  1978. { try to load it as a package unit first }
  1979. Result:=(packagelist.count>0) and loadfrompackage;
  1980. if Result then
  1981. begin
  1982. do_reload:=false;
  1983. state:=ms_compiled;
  1984. { PPU is not needed anymore }
  1985. if assigned(ppufile) then
  1986. begin
  1987. discardppu;
  1988. end;
  1989. { add the unit to the used units list of the program }
  1990. usedunits.concat(tused_unit.create(self,true,false,nil));
  1991. end;
  1992. end;
  1993. procedure tppumodule.prepare_second_load(from_module: tmodule);
  1994. const
  1995. CompileStates = [ms_compile, ms_compiling_waitintf, ms_compiling_waitimpl,
  1996. ms_compiling_waitfinish, ms_compiling_wait, ms_compiled,
  1997. ms_processed];
  1998. begin
  1999. { try to load the unit a second time first }
  2000. Message1(unit_u_second_load_unit,modulename^);
  2001. Message2(unit_u_previous_state,modulename^,ModuleStateStr[state]);
  2002. { Flag modules to reload }
  2003. flagdependent(from_module);
  2004. { Reset the module }
  2005. reset(false);
  2006. if state in CompileStates then
  2007. begin
  2008. Message1(unit_u_second_compile_unit,modulename^);
  2009. state:=ms_compile;
  2010. end
  2011. else
  2012. state:=ms_load;
  2013. end;
  2014. procedure tppumodule.try_load_ppufile(from_module : tmodule);
  2015. begin
  2016. Message1(unit_u_loading_unit,modulename^);
  2017. if auPPU in search_unit_files(from_module,false) then
  2018. state:=ms_load
  2019. else
  2020. state:=ms_compile;
  2021. if not (state=ms_compile) then
  2022. begin
  2023. load_interface;
  2024. setdefgeneration;
  2025. if not (state=ms_compile) then
  2026. begin
  2027. load_usedunits;
  2028. if not (state=ms_compile) then
  2029. Message1(unit_u_finished_loading_unit,modulename^);
  2030. end;
  2031. end;
  2032. { PPU is not needed anymore }
  2033. if assigned(ppufile) then
  2034. discardppu;
  2035. end;
  2036. procedure tppumodule.recompile_from_sources(from_module : tmodule);
  2037. var
  2038. pu : tused_unit;
  2039. begin
  2040. { recompile the unit or give a fatal error if sources not available }
  2041. if not(sources_avail) then
  2042. begin
  2043. search_unit_files(from_module,true);
  2044. if not(sources_avail) then
  2045. begin
  2046. printcomments;
  2047. if recompile_reason=rr_noppu then
  2048. begin
  2049. pu:=tused_unit(from_module.used_units.first);
  2050. while assigned(pu) do
  2051. begin
  2052. if pu.u=self then
  2053. break;
  2054. pu:=tused_unit(pu.next);
  2055. end;
  2056. if assigned(pu) and assigned(pu.unitsym) then
  2057. MessagePos2(pu.unitsym.fileinfo,unit_f_cant_find_ppu,realmodulename^,from_module.realmodulename^)
  2058. else
  2059. Message2(unit_f_cant_find_ppu,realmodulename^,from_module.realmodulename^);
  2060. end
  2061. else
  2062. Message1(unit_f_cant_compile_unit,realmodulename^);
  2063. end;
  2064. end;
  2065. { we found the sources, we do not need the verbose messages anymore }
  2066. if comments <> nil then
  2067. begin
  2068. comments.free;
  2069. comments:=nil;
  2070. end;
  2071. { Flag modules to reload }
  2072. flagdependent(from_module);
  2073. { Reset the module }
  2074. reset(true);
  2075. { mark this module for recompilation }
  2076. if not (state in [ms_compile]) then
  2077. state:=ms_compile;
  2078. setdefgeneration;
  2079. end;
  2080. procedure tppumodule.post_load_or_compile(from_module : tmodule; second_time : boolean);
  2081. begin
  2082. if current_module<>self then
  2083. internalerror(200212282);
  2084. if in_interface then
  2085. internalerror(200212283);
  2086. { for a second_time recompile reload all dependent units,
  2087. for a first time compile register the unit _once_ }
  2088. if second_time then
  2089. reload_flagged_units;
  2090. { reopen the old module }
  2091. {$ifdef SHORT_ON_FILE_HANDLES}
  2092. if from_module.is_unit and
  2093. assigned(tppumodule(from_module).ppufile) then
  2094. tppumodule(from_module).ppufile.tempopen;
  2095. {$endif SHORT_ON_FILE_HANDLES}
  2096. state:=ms_processed;
  2097. end;
  2098. function tppumodule.loadppu(from_module : tmodule) : boolean;
  2099. const
  2100. ImplIntf : array[boolean] of string[15]=('implementation','interface');
  2101. var
  2102. do_load,
  2103. second_time : boolean;
  2104. begin
  2105. Inc(LoadCount);
  2106. Result:=false;
  2107. Message3(unit_u_load_unit,from_module.modulename^,
  2108. ImplIntf[from_module.in_interface],
  2109. modulename^);
  2110. { check if the globalsymtable is already available, but
  2111. we must reload when the do_reload flag is set }
  2112. if (not do_reload) and
  2113. assigned(globalsymtable) then
  2114. exit(True);
  2115. { reset }
  2116. do_load:=true;
  2117. second_time:=false;
  2118. set_current_module(self);
  2119. do_load:=not check_loadfrompackage;
  2120. { A force reload }
  2121. check_reload(from_module, do_load);
  2122. if not do_load then
  2123. begin
  2124. // No need to do anything, restore situation and exit.
  2125. set_current_module(from_module);
  2126. exit(state=ms_compiled);
  2127. end;
  2128. { loading the unit for a second time? }
  2129. if state=ms_registered then
  2130. state:=ms_load
  2131. else if (state in [ms_compile, ms_compiling_waitintf]) then
  2132. begin
  2133. { no use continuing if we must be compiled }
  2134. // but we still need to restore current_module!
  2135. set_current_module(from_module);
  2136. exit(false)
  2137. end
  2138. else
  2139. begin
  2140. second_time:=true;
  2141. prepare_second_load(from_module);
  2142. end;
  2143. { close old_current_ppu on system that are
  2144. short on file handles like DOS PM }
  2145. {$ifdef SHORT_ON_FILE_HANDLES}
  2146. if from_module.is_unit and
  2147. assigned(tppumodule(from_module).ppufile) then
  2148. tppumodule(from_module).ppufile.tempclose;
  2149. {$endif SHORT_ON_FILE_HANDLES}
  2150. { try to opening ppu, skip this when we already
  2151. know that we need to compile the unit }
  2152. if not (state=ms_compile) then
  2153. try_load_ppufile(from_module);
  2154. { Do we need to recompile the unit }
  2155. if (state=ms_compile) then
  2156. recompile_from_sources(from_module)
  2157. else
  2158. state:=ms_compiled;
  2159. Result:=(state=ms_compiled);
  2160. // We cannot do this here, the order is all messed up...
  2161. // if not second_time then
  2162. // usedunits.concat(tused_unit.create(self,true,false,nil));
  2163. if result then
  2164. post_load_or_compile(from_module,second_time);
  2165. { we are back, restore current_module }
  2166. set_current_module(from_module);
  2167. { safety, so it does not become negative }
  2168. if LoadCount>0 then
  2169. Dec(LoadCount);
  2170. end;
  2171. procedure tppumodule.discardppu;
  2172. begin
  2173. { PPU is not needed anymore }
  2174. if not assigned(ppufile) then
  2175. exit;
  2176. ppufile.closefile;
  2177. ppufile.free;
  2178. ppufile:=nil;
  2179. end;
  2180. {*****************************************************************************
  2181. RegisterUnit
  2182. *****************************************************************************}
  2183. function registerunit(callermodule:tmodule;const s : TIDString;const fn:string; out is_new:boolean) : tppumodule;
  2184. function FindCycle(aFile, SearchFor: TModule; var Cycle: TFPList): boolean;
  2185. // Note: when traversing, add every search file to Cycle, to avoid running in circles.
  2186. // When a cycle is detected, clear the Cycle list and build the cycle path
  2187. var
  2188. aParent: tdependent_unit;
  2189. begin
  2190. Cycle.Add(aFile);
  2191. aParent:=tdependent_unit(afile.dependent_units.First);
  2192. While Assigned(aParent) do
  2193. begin
  2194. if aParent.in_interface then
  2195. begin
  2196. // writeln('Registering ',Callermodule.get_modulename,': checking cyclic dependency of ',aFile.get_modulename, ' on ',aparent.u.get_modulename);
  2197. if aParent.u=SearchFor then
  2198. begin
  2199. // unit cycle found
  2200. Cycle.Clear;
  2201. Cycle.Add(aParent.u);
  2202. Cycle.Add(aFile);
  2203. // Writeln('exit at ',aParent.u.get_modulename);
  2204. exit(true);
  2205. end;
  2206. if Cycle.IndexOf(aParent.u)<0 then
  2207. if FindCycle(aParent.u,SearchFor,Cycle) then
  2208. begin
  2209. // Writeln('Cycle found, exit at ',aParent.u.get_modulename);
  2210. Cycle.Add(aFile);
  2211. exit(true);
  2212. end;
  2213. end;
  2214. aParent:=tdependent_unit(aParent.Next);
  2215. end;
  2216. Result:=false;
  2217. end;
  2218. var
  2219. ups : TIDString;
  2220. hp : tppumodule;
  2221. hp2 : tmodule;
  2222. cycle : TFPList;
  2223. havecycle: boolean;
  2224. {$IFDEF DEBUGCYCLE}
  2225. cyclepath : ansistring
  2226. {$ENDIF}
  2227. begin
  2228. { Info }
  2229. ups:=upper(s);
  2230. { search all loaded units }
  2231. hp:=tppumodule(loaded_units.first);
  2232. hp2:=nil;
  2233. while assigned(hp) do
  2234. begin
  2235. if hp.modulename^=ups then
  2236. begin
  2237. { only check for units. The main program is also
  2238. as a unit in the loaded_units list. We simply need
  2239. to ignore this entry (PFV) }
  2240. if hp.is_unit then
  2241. begin
  2242. { both units in interface ? }
  2243. if hp.in_interface and callermodule.in_interface then
  2244. begin
  2245. { check for a cycle }
  2246. Cycle:=TFPList.Create;
  2247. try
  2248. HaveCycle:=FindCycle(CallerModule,hp,Cycle);
  2249. if HaveCycle then
  2250. begin
  2251. {$IFDEF DEBUGCYCLE}
  2252. Writeln('Done cycle check');
  2253. CyclePath:='';
  2254. hp2:=TModule(Cycle[Cycle.Count-1]);
  2255. for i:=0 to Cycle.Count-1 do begin
  2256. if i>0 then CyclePath:=CyclePath+',';
  2257. CyclePath:=CyclePath+TModule(Cycle[i]).realmodulename^;
  2258. end;
  2259. Writeln('Unit cycle detected: ',CyclePath);
  2260. {$ENDIF}
  2261. Message2(unit_f_circular_unit_reference,callermodule.realmodulename^,hp.realmodulename^);
  2262. end;
  2263. finally
  2264. Cycle.Free;
  2265. end;
  2266. if assigned(hp2) then
  2267. Message2(unit_f_circular_unit_reference,callermodule.realmodulename^,hp.realmodulename^);
  2268. end;
  2269. break;
  2270. end;
  2271. end;
  2272. { the next unit }
  2273. hp:=tppumodule(hp.next);
  2274. end;
  2275. { the unit is not in the loaded units,
  2276. we create an entry and register the unit }
  2277. is_new:=not assigned(hp);
  2278. if is_new then
  2279. begin
  2280. Message1(unit_u_registering_new_unit,ups);
  2281. hp:=tppumodule.create(callermodule,s,fn,true);
  2282. addloadedunit(hp);
  2283. end;
  2284. { return }
  2285. registerunit:=hp;
  2286. end;
  2287. end.