fppu.pas 75 KB

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