fppu.pas 87 KB

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