2
0

fppu.pas 88 KB

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