fppu.pas 82 KB

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