fppu.pas 86 KB

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