fppu.pas 71 KB

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