fppu.pas 67 KB

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