fppu.pas 74 KB

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