fppu.pas 70 KB

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