fppu.pas 80 KB

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